@peerbit/rpc
Advanced tools
Comparing version 2.1.17 to 3.0.1
@@ -11,6 +11,6 @@ import { AbstractType } from "@dao-xyz/borsh"; | ||
responseHandler?: ResponseHandler<Q, R>; | ||
subscriptionData?: Uint8Array; | ||
}; | ||
export type RequestContext = { | ||
from?: PublicSignKey; | ||
timestamp: bigint; | ||
}; | ||
@@ -27,3 +27,2 @@ export type ResponseHandler<Q, R> = (query: Q, context: RequestContext) => Promise<R | undefined> | R | undefined; | ||
private _onMessageBinded; | ||
private _subscriptionMetaData; | ||
private _keypair; | ||
@@ -37,3 +36,3 @@ private _getResponseValueFn; | ||
private _subscribing; | ||
subscribe(data?: Uint8Array | undefined): Promise<void>; | ||
subscribe(): Promise<void>; | ||
_onMessage(evt: CustomEvent<DataEvent>): Promise<void>; | ||
@@ -40,0 +39,0 @@ private seal; |
@@ -14,3 +14,2 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { waitFor } from "@peerbit/time"; | ||
import { equals } from "uint8arrays"; | ||
const createValueResolver = (type) => { | ||
@@ -33,3 +32,2 @@ if (type === Uint8Array) { | ||
_onMessageBinded = undefined; | ||
_subscriptionMetaData; | ||
_keypair; | ||
@@ -48,3 +46,3 @@ _getResponseValueFn; | ||
this._keypair = await X25519Keypair.create(); | ||
await this.subscribe(args.subscriptionData); | ||
await this.subscribe(); | ||
} | ||
@@ -75,32 +73,12 @@ async _close(from) { | ||
_subscribing; | ||
async subscribe(data = this._subscriptionMetaData) { | ||
async subscribe() { | ||
await this._subscribing; | ||
if (this._subscribed && | ||
(this._subscriptionMetaData === data || | ||
(this._subscriptionMetaData && | ||
data && | ||
equals(this._subscriptionMetaData, data)))) { | ||
if (this._subscribed) { | ||
return; | ||
} | ||
const prevSubscriptionData = this._subscriptionMetaData; | ||
this._subscriptionMetaData = data; | ||
const wasSubscribed = this._subscribed; | ||
this._subscribed = true; | ||
this._onMessageBinded = this._onMessageBinded || this._onMessage.bind(this); | ||
if (wasSubscribed) { | ||
await this.node.services.pubsub.unsubscribe(this.rpcTopic, { | ||
data: prevSubscriptionData | ||
}); | ||
} | ||
this._subscribing = this.node.services.pubsub | ||
.subscribe(this.rpcTopic, { data }) | ||
.then(() => { | ||
if (!wasSubscribed) { | ||
this.node.services.pubsub.addEventListener("data", this._onMessageBinded); | ||
} | ||
}); | ||
this.node.services.pubsub.addEventListener("data", this._onMessageBinded); | ||
this._subscribing = this.node.services.pubsub.subscribe(this.rpcTopic); | ||
await this._subscribing; | ||
if (!wasSubscribed) { | ||
await this.node.services.pubsub.requestSubscribers(this.rpcTopic); | ||
} | ||
logger.debug("subscribing to query topic (responses): " + this.rpcTopic); | ||
@@ -116,5 +94,6 @@ } | ||
const maybeEncrypted = rpcMessage.request; | ||
const decrypted = await maybeEncrypted.decrypt(this.node.keychain); | ||
const decrypted = await maybeEncrypted.decrypt(this.node.services.keychain); | ||
const response = await this._responseHandler(this._getRequestValueFn(decrypted), { | ||
from: message.sender | ||
from: message.header.signatures.publicKeys[0], | ||
timestamp: message.header.timetamp | ||
}); | ||
@@ -132,3 +111,3 @@ if (response && rpcMessage.respondTo) { | ||
let maybeEncryptedMessage = decryptedMessage; | ||
maybeEncryptedMessage = await decryptedMessage.encrypt(this._keypair, rpcMessage.respondTo); | ||
maybeEncryptedMessage = await decryptedMessage.encrypt(this._keypair, [rpcMessage.respondTo]); | ||
await this.node.services.pubsub.publish(serialize(new ResponseV0({ | ||
@@ -139,4 +118,3 @@ response: maybeEncryptedMessage, | ||
topics: [this.rpcTopic], | ||
to: [message.sender], | ||
strict: true | ||
to: [message.header.signatures.publicKeys[0]] | ||
}); | ||
@@ -182,3 +160,3 @@ } | ||
options?.encryption?.responders.length > 0) { | ||
maybeEncryptedMessage = await decryptedMessage.encrypt(options.encryption.key, ...options.encryption.responders); | ||
maybeEncryptedMessage = await decryptedMessage.encrypt(options.encryption.key, options.encryption.responders); | ||
} | ||
@@ -193,4 +171,8 @@ const requestMessage = new RequestV0({ | ||
return options?.to | ||
? { to: options.to, strict: true, topics: [this.rpcTopic] } | ||
: { topics: [this.rpcTopic] }; | ||
? { | ||
mode: options?.mode, | ||
to: options.to, | ||
topics: [this.rpcTopic] | ||
} | ||
: { mode: options?.mode, topics: [this.rpcTopic] }; | ||
} | ||
@@ -209,3 +191,3 @@ /** | ||
const { response, message } = properties; | ||
const from = message.sender; | ||
const from = message.header.signatures.publicKeys[0]; | ||
if (options?.isTrusted && !(await options?.isTrusted(from))) { | ||
@@ -240,3 +222,3 @@ return; | ||
} | ||
if (error instanceof BorshError && !options?.strict) { | ||
if (error instanceof BorshError) { | ||
logger.debug("Namespace error"); | ||
@@ -243,0 +225,0 @@ return; // Name space conflict most likely |
import { X25519PublicKey, Ed25519PublicKey, PublicSignKey, X25519Keypair } from "@peerbit/crypto"; | ||
export declare const logger: import("pino").Logger<import("pino").LoggerOptions | import("pino").DestinationStream>; | ||
import { DeliveryMode } from "@peerbit/stream-interface"; | ||
export declare const logger: import("pino").Logger<never>; | ||
export type RPCOptions<R> = { | ||
@@ -18,2 +19,3 @@ amount?: number; | ||
strict?: boolean; | ||
mode?: DeliveryMode; | ||
}; | ||
@@ -20,0 +22,0 @@ export type RPCResponse<R> = { |
{ | ||
"name": "@peerbit/rpc", | ||
"version": "2.1.17", | ||
"version": "3.0.1", | ||
"description": "RPC calls for peers", | ||
@@ -35,11 +35,11 @@ "type": "module", | ||
"@dao-xyz/borsh": "^5.1.8", | ||
"@peerbit/crypto": "1.0.10", | ||
"@peerbit/logger": "1.0.1", | ||
"@peerbit/program": "2.4.2", | ||
"@peerbit/time": "1.0.4" | ||
"@peerbit/crypto": "2.0.0", | ||
"@peerbit/logger": "1.0.2", | ||
"@peerbit/program": "3.0.1", | ||
"@peerbit/time": "2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@peerbit/test-utils": "^1.0.33" | ||
"@peerbit/test-utils": "^2.0.1" | ||
}, | ||
"gitHead": "6b3febe03977ab07c14e22ac693e68367fab9a22" | ||
"gitHead": "c48cb37d237a25b0bcc849482b43f6941d53e3d5" | ||
} |
@@ -19,6 +19,4 @@ import { | ||
import { RPCOptions, logger, RPCResponse, PublishOptions } from "./io.js"; | ||
import { Address } from "@peerbit/program"; | ||
import { | ||
DataEvent, | ||
PubSubData, | ||
PublishOptions as PubSubPublishOptions | ||
@@ -30,3 +28,2 @@ } from "@peerbit/pubsub-interface"; | ||
import { waitFor } from "@peerbit/time"; | ||
import { equals } from "uint8arrays"; | ||
@@ -38,6 +35,6 @@ export type RPCSetupOptions<Q, R> = { | ||
responseHandler?: ResponseHandler<Q, R>; | ||
subscriptionData?: Uint8Array; | ||
}; | ||
export type RequestContext = { | ||
from?: PublicSignKey; | ||
timestamp: bigint; | ||
}; | ||
@@ -72,3 +69,2 @@ export type ResponseHandler<Q, R> = ( | ||
private _onMessageBinded: ((arg: any) => any) | undefined = undefined; | ||
private _subscriptionMetaData: Uint8Array | undefined; | ||
@@ -90,3 +86,3 @@ private _keypair: X25519Keypair; | ||
this._keypair = await X25519Keypair.create(); | ||
await this.subscribe(args.subscriptionData); | ||
await this.subscribe(); | ||
} | ||
@@ -122,18 +118,9 @@ | ||
private _subscribing: Promise<void>; | ||
async subscribe(data = this._subscriptionMetaData): Promise<void> { | ||
private _subscribing: Promise<void> | void; | ||
async subscribe(): Promise<void> { | ||
await this._subscribing; | ||
if ( | ||
this._subscribed && | ||
(this._subscriptionMetaData === data || | ||
(this._subscriptionMetaData && | ||
data && | ||
equals(this._subscriptionMetaData, data))) | ||
) { | ||
if (this._subscribed) { | ||
return; | ||
} | ||
const prevSubscriptionData = this._subscriptionMetaData; | ||
this._subscriptionMetaData = data; | ||
const wasSubscribed = this._subscribed; | ||
this._subscribed = true; | ||
@@ -143,23 +130,7 @@ | ||
if (wasSubscribed) { | ||
await this.node.services.pubsub.unsubscribe(this.rpcTopic, { | ||
data: prevSubscriptionData | ||
}); | ||
} | ||
this.node.services.pubsub.addEventListener("data", this._onMessageBinded!); | ||
this._subscribing = this.node.services.pubsub | ||
.subscribe(this.rpcTopic, { data }) | ||
.then(() => { | ||
if (!wasSubscribed) { | ||
this.node.services.pubsub.addEventListener( | ||
"data", | ||
this._onMessageBinded! | ||
); | ||
} | ||
}); | ||
this._subscribing = this.node.services.pubsub.subscribe(this.rpcTopic); | ||
await this._subscribing; | ||
if (!wasSubscribed) { | ||
await this.node.services.pubsub.requestSubscribers(this.rpcTopic); | ||
} | ||
logger.debug("subscribing to query topic (responses): " + this.rpcTopic); | ||
@@ -177,10 +148,12 @@ } | ||
const maybeEncrypted = rpcMessage.request; | ||
const decrypted = await maybeEncrypted.decrypt(this.node.keychain); | ||
const decrypted = await maybeEncrypted.decrypt( | ||
this.node.services.keychain | ||
); | ||
const response = await this._responseHandler( | ||
this._getRequestValueFn(decrypted), | ||
{ | ||
from: message.sender | ||
from: message.header.signatures!.publicKeys[0], | ||
timestamp: message.header.timetamp | ||
} | ||
); | ||
if (response && rpcMessage.respondTo) { | ||
@@ -203,3 +176,3 @@ // send query and wait for replies in a generator like behaviour | ||
this._keypair, | ||
rpcMessage.respondTo | ||
[rpcMessage.respondTo] | ||
); | ||
@@ -216,4 +189,3 @@ | ||
topics: [this.rpcTopic], | ||
to: [message.sender], | ||
strict: true | ||
to: [message.header.signatures!.publicKeys[0]] | ||
} | ||
@@ -244,2 +216,3 @@ ); | ||
} | ||
logger.error( | ||
@@ -274,3 +247,3 @@ "Error handling query: " + | ||
options.encryption.key, | ||
...options.encryption.responders | ||
options.encryption.responders | ||
); | ||
@@ -289,4 +262,8 @@ } | ||
return options?.to | ||
? { to: options.to, strict: true, topics: [this.rpcTopic] } | ||
: { topics: [this.rpcTopic] }; | ||
? { | ||
mode: options?.mode, | ||
to: options.to, | ||
topics: [this.rpcTopic] | ||
} | ||
: { mode: options?.mode, topics: [this.rpcTopic] }; | ||
} | ||
@@ -320,3 +297,3 @@ | ||
const { response, message } = properties; | ||
const from = message.sender; | ||
const from = message.header.signatures!.publicKeys[0]; | ||
@@ -355,3 +332,3 @@ if (options?.isTrusted && !(await options?.isTrusted(from))) { | ||
if (error instanceof BorshError && !options?.strict) { | ||
if (error instanceof BorshError) { | ||
logger.debug("Namespace error"); | ||
@@ -402,3 +379,3 @@ return; // Name space conflict most likely | ||
options.to.map((x) => (typeof x === "string" ? x : x.hashcode())) | ||
) | ||
) | ||
: undefined; | ||
@@ -405,0 +382,0 @@ |
@@ -8,2 +8,3 @@ import { | ||
import { logger as loggerFn } from "@peerbit/logger"; | ||
import { DeliveryMode } from "@peerbit/stream-interface"; | ||
@@ -27,4 +28,5 @@ export const logger = loggerFn({ module: "rpc" }); | ||
strict?: boolean; | ||
mode?: DeliveryMode; | ||
}; | ||
export type RPCResponse<R> = { response: R; from?: PublicSignKey }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
63446
1004
+ Added@libp2p/crypto@3.0.4(transitive)
+ Added@libp2p/interface@1.7.0(transitive)
+ Added@libp2p/peer-id@4.2.4(transitive)
+ Added@peerbit/any-store@1.0.152.1.6(transitive)
+ Added@peerbit/any-store-interface@1.0.0(transitive)
+ Added@peerbit/any-store-opfs@1.0.6(transitive)
+ Added@peerbit/crypto@2.0.02.1.7(transitive)
+ Added@peerbit/keychain@1.0.24(transitive)
+ Added@peerbit/logger@1.0.21.0.3(transitive)
+ Added@peerbit/program@3.0.1(transitive)
+ Added@peerbit/pubsub-interface@2.0.13(transitive)
+ Added@peerbit/stream-interface@3.0.9(transitive)
+ Added@peerbit/time@2.0.02.0.62.0.8(transitive)
+ Added@stablelib/binary@1.0.1(transitive)
+ Added@stablelib/hash@1.0.1(transitive)
+ Added@stablelib/int@1.0.1(transitive)
+ Added@stablelib/sha256@1.0.1(transitive)
+ Added@stablelib/wipe@1.0.1(transitive)
+ Addeduuid@10.0.09.0.1(transitive)
- Removed@libp2p/crypto@2.0.8(transitive)
- Removed@libp2p/interface@0.1.6(transitive)
- Removed@libp2p/interfaces@3.3.2(transitive)
- Removed@libp2p/peer-id@3.0.6(transitive)
- Removed@peerbit/cache@1.1.1(transitive)
- Removed@peerbit/crypto@1.0.10(transitive)
- Removed@peerbit/lazy-level@1.2.1(transitive)
- Removed@peerbit/logger@1.0.1(transitive)
- Removed@peerbit/program@2.4.2(transitive)
- Removed@peerbit/pubsub-interface@1.1.5(transitive)
- Removed@peerbit/stream-interface@1.0.11(transitive)
- Removed@peerbit/time@1.0.4(transitive)
- Removedabortable-iterator@5.1.0(transitive)
- Removedget-iterator@2.0.1(transitive)
- Removedrace-signal@1.1.3(transitive)
Updated@peerbit/crypto@2.0.0
Updated@peerbit/logger@1.0.2
Updated@peerbit/program@3.0.1
Updated@peerbit/time@2.0.0