Compare commits
2 Commits
f875cd116f
...
99f988addd
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 99f988addd | |
Derrick Hammer | 0dc8eebc7e |
|
@ -1 +1 @@
|
|||
{"version":3,"file":"simple.d.ts","sourceRoot":"","sources":["../../src/query/simple.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAe,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAK9C,OAAO,YAAY,MAAM,WAAW,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,YAAY;IACtD,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IAChC,UAAkB,MAAM,EAAE,gBAAgB,CAAC;gBAE/B,EACV,OAAO,EACP,KAAK,EACL,KAAK,EACL,OAAO,GACR,EAAE;QACD,OAAO,EAAE,UAAU,CAAC;QACpB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,GAAG,CAAC;QAC9B,KAAK,EAAE,gBAAgB,CAAC;QACxB,OAAO,EAAE,eAAe,CAAC;KAC1B;cASe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAKrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cAwD1B,cAAc;CAmB/B"}
|
||||
{"version":3,"file":"simple.d.ts","sourceRoot":"","sources":["../../src/query/simple.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAe,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAU9C,OAAO,YAAY,MAAM,WAAW,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,YAAY;IACtD,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IAChC,UAAkB,MAAM,EAAE,gBAAgB,CAAC;gBAE/B,EACV,OAAO,EACP,KAAK,EACL,KAAK,EACL,OAAO,GACR,EAAE;QACD,OAAO,EAAE,UAAU,CAAC;QACpB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,GAAG,CAAC;QAC9B,KAAK,EAAE,gBAAgB,CAAC;QACxB,OAAO,EAAE,eAAe,CAAC;KAC1B;cASe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAKrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cAwD1B,cAAc;CAmB/B"}
|
|
@ -1,6 +1,5 @@
|
|||
import b4a from "b4a";
|
||||
import { hashQuery, isPromise, validateTimestampedResponse } from "../util.js";
|
||||
import RPC from "@lumeweb/rpc";
|
||||
import { hashQuery, isPromise, setupStream, validateTimestampedResponse, } from "../util.js";
|
||||
import { ERR_INVALID_SIGNATURE } from "../error.js";
|
||||
import RpcQueryBase from "./base.js";
|
||||
export default class SimpleRpcQuery extends RpcQueryBase {
|
||||
|
@ -43,7 +42,7 @@ export default class SimpleRpcQuery extends RpcQueryBase {
|
|||
}
|
||||
this._relay = socket;
|
||||
await socket.opened;
|
||||
const rpc = new RPC(socket);
|
||||
const rpc = setupStream(socket);
|
||||
if (this._query.bypassCache) {
|
||||
delete this._query.bypassCache;
|
||||
await this.queryRpc(rpc, {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/// <reference types="node" />
|
||||
import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types";
|
||||
export declare const RPC_PROTOCOL_SYMBOL: unique symbol;
|
||||
export declare function isPromise(obj: Promise<any>): boolean;
|
||||
export declare function flatten(target: any, opts?: any): any[];
|
||||
export declare function validateResponse(
|
||||
|
@ -13,4 +14,5 @@ export declare function validateTimestampedResponse(
|
|||
): boolean;
|
||||
export declare function hashQuery(query: RPCRequest): string;
|
||||
export declare function createHash(data: string): Buffer;
|
||||
export declare function setupStream(stream: any): any;
|
||||
//# sourceMappingURL=util.d.ts.map
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAOpE,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,WAM1C;AAKD,wBAAgB,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,GAAE,GAAQ,GAAG,GAAG,EAAE,CA0C1D;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,EACrB,WAAW,UAAQ,GAClB,OAAO,CAkBT;AAED,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,GACpB,OAAO,CAET;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAUnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM/C"}
|
||||
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AASpE,eAAO,MAAM,mBAAmB,eAAyC,CAAC;AAE1E,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,WAM1C;AAKD,wBAAgB,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,GAAE,GAAQ,GAAG,GAAG,EAAE,CA0C1D;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,EACrB,WAAW,UAAQ,GAClB,OAAO,CAkBT;AAED,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,GACpB,OAAO,CAET;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAUnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM/C;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,GAAG,OAStC"}
|
|
@ -5,6 +5,9 @@ import crypto from "hypercore-crypto";
|
|||
// @ts-ignore
|
||||
import sodium from "sodium-universal";
|
||||
import b4a from "b4a";
|
||||
import RPC from "@lumeweb/rpc";
|
||||
const RPC_PROTOCOL_ID = b4a.from("lumeweb");
|
||||
export const RPC_PROTOCOL_SYMBOL = Symbol.for(RPC_PROTOCOL_ID.toString());
|
||||
export function isPromise(obj) {
|
||||
return (!!obj &&
|
||||
(typeof obj === "object" || typeof obj === "function") &&
|
||||
|
@ -78,3 +81,11 @@ export function createHash(data) {
|
|||
sodium.crypto_generichash(hash, buffer);
|
||||
return hash;
|
||||
}
|
||||
export function setupStream(stream) {
|
||||
const existing = stream[RPC_PROTOCOL_SYMBOL];
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
stream[RPC_PROTOCOL_SYMBOL] = new RPC(stream);
|
||||
return stream[RPC_PROTOCOL_SYMBOL];
|
||||
}
|
||||
|
|
|
@ -2,7 +2,12 @@ import RpcNetwork from "../network.js";
|
|||
import { ClientRPCRequest, RPCResponse } from "@lumeweb/relay-types";
|
||||
import { RpcQueryOptions } from "../types.js";
|
||||
import b4a from "b4a";
|
||||
import { hashQuery, isPromise, validateTimestampedResponse } from "../util.js";
|
||||
import {
|
||||
hashQuery,
|
||||
isPromise,
|
||||
setupStream,
|
||||
validateTimestampedResponse,
|
||||
} from "../util.js";
|
||||
import RPC from "@lumeweb/rpc";
|
||||
import { ERR_INVALID_SIGNATURE } from "../error.js";
|
||||
import RpcQueryBase from "./base.js";
|
||||
|
@ -69,7 +74,7 @@ export default class SimpleRpcQuery extends RpcQueryBase {
|
|||
|
||||
await socket.opened;
|
||||
|
||||
const rpc = new RPC(socket);
|
||||
const rpc = setupStream(socket);
|
||||
|
||||
if (this._query.bypassCache) {
|
||||
delete this._query.bypassCache;
|
||||
|
|
15
src/util.ts
15
src/util.ts
|
@ -6,6 +6,10 @@ import crypto from "hypercore-crypto";
|
|||
// @ts-ignore
|
||||
import sodium from "sodium-universal";
|
||||
import b4a from "b4a";
|
||||
import RPC from "@lumeweb/rpc";
|
||||
|
||||
const RPC_PROTOCOL_ID = b4a.from("lumeweb");
|
||||
export const RPC_PROTOCOL_SYMBOL = Symbol.for(RPC_PROTOCOL_ID.toString());
|
||||
|
||||
export function isPromise(obj: Promise<any>) {
|
||||
return (
|
||||
|
@ -112,3 +116,14 @@ export function createHash(data: string): Buffer {
|
|||
|
||||
return hash;
|
||||
}
|
||||
|
||||
export function setupStream(stream: any) {
|
||||
const existing = stream[RPC_PROTOCOL_SYMBOL];
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
stream[RPC_PROTOCOL_SYMBOL] = new RPC(stream);
|
||||
|
||||
return stream[RPC_PROTOCOL_SYMBOL];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue