Compare commits

...

2 Commits

7 changed files with 39 additions and 7 deletions

View File

@ -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"}

View File

@ -1,6 +1,5 @@
import b4a from "b4a"; 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 { ERR_INVALID_SIGNATURE } from "../error.js";
import RpcQueryBase from "./base.js"; import RpcQueryBase from "./base.js";
export default class SimpleRpcQuery extends RpcQueryBase { export default class SimpleRpcQuery extends RpcQueryBase {
@ -43,7 +42,7 @@ export default class SimpleRpcQuery extends RpcQueryBase {
} }
this._relay = socket; this._relay = socket;
await socket.opened; await socket.opened;
const rpc = new RPC(socket); const rpc = setupStream(socket);
if (this._query.bypassCache) { if (this._query.bypassCache) {
delete this._query.bypassCache; delete this._query.bypassCache;
await this.queryRpc(rpc, { await this.queryRpc(rpc, {

2
dist/util.d.ts vendored
View File

@ -1,5 +1,6 @@
/// <reference types="node" /> /// <reference types="node" />
import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types"; 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 isPromise(obj: Promise<any>): boolean;
export declare function flatten(target: any, opts?: any): any[]; export declare function flatten(target: any, opts?: any): any[];
export declare function validateResponse( export declare function validateResponse(
@ -13,4 +14,5 @@ export declare function validateTimestampedResponse(
): boolean; ): boolean;
export declare function hashQuery(query: RPCRequest): string; export declare function hashQuery(query: RPCRequest): string;
export declare function createHash(data: string): Buffer; export declare function createHash(data: string): Buffer;
export declare function setupStream(stream: any): any;
//# sourceMappingURL=util.d.ts.map //# sourceMappingURL=util.d.ts.map

2
dist/util.d.ts.map vendored
View File

@ -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"}

11
dist/util.js vendored
View File

@ -5,6 +5,9 @@ import crypto from "hypercore-crypto";
// @ts-ignore // @ts-ignore
import sodium from "sodium-universal"; import sodium from "sodium-universal";
import b4a from "b4a"; 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) { export function isPromise(obj) {
return (!!obj && return (!!obj &&
(typeof obj === "object" || typeof obj === "function") && (typeof obj === "object" || typeof obj === "function") &&
@ -78,3 +81,11 @@ export function createHash(data) {
sodium.crypto_generichash(hash, buffer); sodium.crypto_generichash(hash, buffer);
return hash; 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];
}

View File

@ -2,7 +2,12 @@ import RpcNetwork from "../network.js";
import { ClientRPCRequest, RPCResponse } from "@lumeweb/relay-types"; import { ClientRPCRequest, RPCResponse } from "@lumeweb/relay-types";
import { RpcQueryOptions } from "../types.js"; import { RpcQueryOptions } from "../types.js";
import b4a from "b4a"; 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 RPC from "@lumeweb/rpc";
import { ERR_INVALID_SIGNATURE } from "../error.js"; import { ERR_INVALID_SIGNATURE } from "../error.js";
import RpcQueryBase from "./base.js"; import RpcQueryBase from "./base.js";
@ -69,7 +74,7 @@ export default class SimpleRpcQuery extends RpcQueryBase {
await socket.opened; await socket.opened;
const rpc = new RPC(socket); const rpc = setupStream(socket);
if (this._query.bypassCache) { if (this._query.bypassCache) {
delete this._query.bypassCache; delete this._query.bypassCache;

View File

@ -6,6 +6,10 @@ import crypto from "hypercore-crypto";
// @ts-ignore // @ts-ignore
import sodium from "sodium-universal"; import sodium from "sodium-universal";
import b4a from "b4a"; 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>) { export function isPromise(obj: Promise<any>) {
return ( return (
@ -112,3 +116,14 @@ export function createHash(data: string): Buffer {
return hash; 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];
}