From 99f988addd9730fd8989cfbd8c9990e866b07c71 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 19 Mar 2023 10:35:54 -0400 Subject: [PATCH] *Update dist --- dist/query/simple.d.ts.map | 2 +- dist/query/simple.js | 5 ++--- dist/util.d.ts | 2 ++ dist/util.d.ts.map | 2 +- dist/util.js | 11 +++++++++++ 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dist/query/simple.d.ts.map b/dist/query/simple.d.ts.map index ac5e831..77b38b1 100644 --- a/dist/query/simple.d.ts.map +++ b/dist/query/simple.d.ts.map @@ -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"} \ No newline at end of file +{"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"} \ No newline at end of file diff --git a/dist/query/simple.js b/dist/query/simple.js index 5f80140..78f95c6 100644 --- a/dist/query/simple.js +++ b/dist/query/simple.js @@ -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, { diff --git a/dist/util.d.ts b/dist/util.d.ts index ef21ada..804d006 100644 --- a/dist/util.d.ts +++ b/dist/util.d.ts @@ -1,5 +1,6 @@ /// import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types"; +export declare const RPC_PROTOCOL_SYMBOL: unique symbol; export declare function isPromise(obj: Promise): 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 diff --git a/dist/util.d.ts.map b/dist/util.d.ts.map index 4ec7187..f242909 100644 --- a/dist/util.d.ts.map +++ b/dist/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"} \ No newline at end of file +{"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"} \ No newline at end of file diff --git a/dist/util.js b/dist/util.js index 53f20dc..19a8084 100644 --- a/dist/util.js +++ b/dist/util.js @@ -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]; +}