Compare commits
4 Commits
6323bc6b29
...
6c6db87073
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 6c6db87073 | |
Derrick Hammer | 1cc1a0d2b1 | |
Derrick Hammer | bb36a80913 | |
Derrick Hammer | 5e4617ac55 |
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAEA,OAAO,sBAAsB,MAAM,kBAAkB,CAAC;AAItD,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,OAAO,CAAC,uBAAuB,CAAC,CAAgB;IAChD,OAAO,CAAC,uBAAuB,CAAC,CAAW;gBAC/B,KAAK,MAAmB;IAKpC,OAAO,CAAC,QAAQ,CAA4D;IAE5E,IAAI,OAAO,IAAI,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAEtC;IAED,OAAO,CAAC,QAAQ,CAAoC;IAEpD,IAAI,OAAO,IAAI,sBAAsB,CAEpC;IAED,OAAO,CAAC,MAAM,CAAoB;IAElC,IAAI,KAAK,QAER;IAED,OAAO,CAAC,kBAAkB,CAAQ;IAElC,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED,IAAI,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAElC;IAED,OAAO,CAAC,aAAa,CAAM;IAE3B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,aAAa,CAAK;IAE1B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,OAAO,CAAiD;IAEhE,IAAI,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAElC;IAED,OAAO,CAAC,MAAM,CAAC,CAAgB;IAE/B,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAczB;IAED,IAAI,eAAe,IA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E,MAAM,EAAE,CAAC,CAElC;IAED,OAAO,CAAC,MAAM,CAAC,CAAgB;IAE/B,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAQzB;IAED,IAAI,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAEnC;IAED,OAAO,CAAC,YAAY,CAAkB;IAEtC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,IAAI,WAAW,CAAC,KAAK,EAAE,OAAO,EAE7B;IAEM,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAchD,QAAQ,CAAC,MAAM,EAAE,MAAM;IAQ9B,OAAO,CAAC,IAAI;IA0CZ,OAAO,CAAC,iBAAiB;CAK1B"}
|
|
@ -2,7 +2,7 @@
|
||||||
import Hyperswarm from "hyperswarm";
|
import Hyperswarm from "hyperswarm";
|
||||||
import RpcNetworkQueryFactory from "./query/index.js";
|
import RpcNetworkQueryFactory from "./query/index.js";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import { createHash, isPromise } from "./util.js";
|
import { createHash, maybeGetAsyncProperty } from "./util.js";
|
||||||
export default class RpcNetwork {
|
export default class RpcNetwork {
|
||||||
_relaysAvailablePromise;
|
_relaysAvailablePromise;
|
||||||
_relaysAvailableResolve;
|
_relaysAvailableResolve;
|
||||||
|
@ -50,16 +50,7 @@ export default class RpcNetwork {
|
||||||
_ready;
|
_ready;
|
||||||
get ready() {
|
get ready() {
|
||||||
if (!this._ready) {
|
if (!this._ready) {
|
||||||
let dht = this._swarm.dht;
|
this._ready = maybeGetAsyncProperty(this._swarm.dht).then((dht) => dht.ready());
|
||||||
if (typeof dht === "function") {
|
|
||||||
dht = dht();
|
|
||||||
}
|
|
||||||
if (isPromise(dht)) {
|
|
||||||
this._ready = dht.then((dht) => dht.ready());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this._ready = this._swarm.dht.ready();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this._ready;
|
return this._ready;
|
||||||
}
|
}
|
||||||
|
@ -91,7 +82,9 @@ export default class RpcNetwork {
|
||||||
this._swarm.join(createHash("lumeweb"));
|
this._swarm.join(createHash("lumeweb"));
|
||||||
this.setupRelayPromise();
|
this.setupRelayPromise();
|
||||||
this._swarm.on("connection", async (relay) => {
|
this._swarm.on("connection", async (relay) => {
|
||||||
const pubkey = b4a.from(relay.remotePublicKey).toString("hex");
|
const pubkey = b4a
|
||||||
|
.from(await maybeGetAsyncProperty(relay.remotePublicKey))
|
||||||
|
.toString("hex");
|
||||||
relay.once("close", () => {
|
relay.once("close", () => {
|
||||||
this._methods.forEach((item) => {
|
this._methods.forEach((item) => {
|
||||||
if (item.has(pubkey)) {
|
if (item.has(pubkey)) {
|
||||||
|
|
|
@ -15,4 +15,5 @@ export declare function validateTimestampedResponse(
|
||||||
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;
|
export declare function setupStream(stream: any): any;
|
||||||
|
export declare function maybeGetAsyncProperty(object: any): Promise<any>;
|
||||||
//# sourceMappingURL=util.d.ts.map
|
//# 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,0BAA0B,CAAC;AAOxE,eAAO,MAAM,mBAAmB,eAAwB,CAAC;AAEzD,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"}
|
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAOxE,eAAO,MAAM,mBAAmB,eAAwB,CAAC;AAEzD,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;AAED,wBAAsB,qBAAqB,CAAC,MAAM,EAAE,GAAG,gBAUtD"}
|
|
@ -88,3 +88,12 @@ export function setupStream(stream) {
|
||||||
stream[RPC_PROTOCOL_SYMBOL] = new RPC(stream);
|
stream[RPC_PROTOCOL_SYMBOL] = new RPC(stream);
|
||||||
return stream[RPC_PROTOCOL_SYMBOL];
|
return stream[RPC_PROTOCOL_SYMBOL];
|
||||||
}
|
}
|
||||||
|
export async function maybeGetAsyncProperty(object) {
|
||||||
|
if (typeof object === "function") {
|
||||||
|
object = object();
|
||||||
|
}
|
||||||
|
if (isPromise(object)) {
|
||||||
|
object = await object;
|
||||||
|
}
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
import Hyperswarm from "hyperswarm";
|
import Hyperswarm from "hyperswarm";
|
||||||
import RpcNetworkQueryFactory from "./query/index.js";
|
import RpcNetworkQueryFactory from "./query/index.js";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import { createHash, isPromise } from "./util.js";
|
import { createHash, maybeGetAsyncProperty } from "./util.js";
|
||||||
|
|
||||||
export default class RpcNetwork {
|
export default class RpcNetwork {
|
||||||
private _relaysAvailablePromise?: Promise<void>;
|
private _relaysAvailablePromise?: Promise<void>;
|
||||||
|
@ -70,15 +70,9 @@ export default class RpcNetwork {
|
||||||
|
|
||||||
get ready(): Promise<void> {
|
get ready(): Promise<void> {
|
||||||
if (!this._ready) {
|
if (!this._ready) {
|
||||||
let dht = this._swarm.dht;
|
this._ready = maybeGetAsyncProperty(this._swarm.dht).then((dht: any) =>
|
||||||
if (typeof dht === "function") {
|
dht.ready()
|
||||||
dht = dht();
|
) as Promise<void>;
|
||||||
}
|
|
||||||
if (isPromise(dht)) {
|
|
||||||
this._ready = dht.then((dht: any) => dht.ready());
|
|
||||||
} else {
|
|
||||||
this._ready = this._swarm.dht.ready() as Promise<void>;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._ready as Promise<void>;
|
return this._ready as Promise<void>;
|
||||||
|
@ -125,7 +119,9 @@ export default class RpcNetwork {
|
||||||
this.setupRelayPromise();
|
this.setupRelayPromise();
|
||||||
|
|
||||||
this._swarm.on("connection", async (relay: any) => {
|
this._swarm.on("connection", async (relay: any) => {
|
||||||
const pubkey = b4a.from(relay.remotePublicKey).toString("hex");
|
const pubkey = b4a
|
||||||
|
.from(await maybeGetAsyncProperty(relay.remotePublicKey))
|
||||||
|
.toString("hex");
|
||||||
relay.once("close", () => {
|
relay.once("close", () => {
|
||||||
this._methods.forEach((item) => {
|
this._methods.forEach((item) => {
|
||||||
if (item.has(pubkey)) {
|
if (item.has(pubkey)) {
|
||||||
|
|
12
src/util.ts
12
src/util.ts
|
@ -125,3 +125,15 @@ export function setupStream(stream: any) {
|
||||||
|
|
||||||
return stream[RPC_PROTOCOL_SYMBOL];
|
return stream[RPC_PROTOCOL_SYMBOL];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function maybeGetAsyncProperty(object: any) {
|
||||||
|
if (typeof object === "function") {
|
||||||
|
object = object();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPromise(object)) {
|
||||||
|
object = await object;
|
||||||
|
}
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue