@peerbit/rpc 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +65 -0
- package/lib/esm/controller.d.ts +62 -0
- package/lib/esm/controller.js +266 -0
- package/lib/esm/controller.js.map +1 -0
- package/lib/esm/encoding.d.ts +19 -0
- package/lib/esm/encoding.js +59 -0
- package/lib/esm/encoding.js.map +1 -0
- package/lib/esm/index.d.ts +4 -0
- package/lib/esm/index.js +5 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/io.d.ts +22 -0
- package/lib/esm/io.js +3 -0
- package/lib/esm/io.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/utils.d.ts +6 -0
- package/lib/esm/utils.js +57 -0
- package/lib/esm/utils.js.map +1 -0
- package/package.json +45 -0
- package/src/controller.ts +413 -0
- package/src/encoding.ts +41 -0
- package/src/index.ts +4 -0
- package/src/io.ts +28 -0
- package/src/utils.ts +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2022 dao.xyz
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# RPC
|
|
2
|
+
|
|
3
|
+
This module contains a communication protocol for a generic request and response communication, this modules can be used as a component in other modules. [Documents](./../../data/document), [DString](./../../data/string) for examples
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## How it works
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
@variant("hello")
|
|
11
|
+
class Hello {
|
|
12
|
+
|
|
13
|
+
// add payload properties here
|
|
14
|
+
constructor() {
|
|
15
|
+
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@variant("world")
|
|
20
|
+
class World {
|
|
21
|
+
|
|
22
|
+
// add payload properties here
|
|
23
|
+
constructor() {
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@variant("rpc-test")
|
|
29
|
+
class RPCTest extends Program {
|
|
30
|
+
|
|
31
|
+
@field({ type: RPC })
|
|
32
|
+
rpc: RPC<Hello, World>;
|
|
33
|
+
|
|
34
|
+
async setup(): Promise<void> {
|
|
35
|
+
await this.rpc.setup({
|
|
36
|
+
responseType: Hello,
|
|
37
|
+
queryType: World,
|
|
38
|
+
context: this,
|
|
39
|
+
responseHandler: (resp, from) => {
|
|
40
|
+
return resp;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// later (assume that some other peers also opens the RPCTest program)
|
|
47
|
+
const peer = await Peerbit.create ()
|
|
48
|
+
const rpcTest = peer.open(new RPCTest());
|
|
49
|
+
|
|
50
|
+
await rpcTest.rpc.request( new Hello(), (resp) => { console.log(resp) })
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Visualized
|
|
54
|
+
|
|
55
|
+
<p align="center">
|
|
56
|
+
<img width="400" src="./documentation/rpc1.png" alt="p1">
|
|
57
|
+
</p>
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
<p align="center">
|
|
62
|
+
<img width="400" src="./documentation/rpc2.png" alt="p1">
|
|
63
|
+
</p>
|
|
64
|
+
|
|
65
|
+
See [Document store](./../data/document) and [Clock Service](./../clock-service) as implementation examples
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { AbstractType } from "@dao-xyz/borsh";
|
|
2
|
+
import { PublicSignKey } from "@peerbit/crypto";
|
|
3
|
+
import { RPCOptions, RPCResponse, PublishOptions } from "./io.js";
|
|
4
|
+
import { AbstractProgram, Address } from "@peerbit/program";
|
|
5
|
+
import { PubSubData } from "@peerbit/pubsub-interface";
|
|
6
|
+
import { ComposableProgram } from "@peerbit/program";
|
|
7
|
+
import { DataMessage } from "@peerbit/stream-interface";
|
|
8
|
+
export type SearchContext = (() => Address) | AbstractProgram;
|
|
9
|
+
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
10
|
+
export type RPCSetupOptions<Q, R> = {
|
|
11
|
+
topic: string;
|
|
12
|
+
queryType: AbstractType<Q>;
|
|
13
|
+
responseType: AbstractType<R>;
|
|
14
|
+
canRead?: CanRead;
|
|
15
|
+
responseHandler?: ResponseHandler<Q, R>;
|
|
16
|
+
subscriptionData?: Uint8Array;
|
|
17
|
+
};
|
|
18
|
+
export type QueryContext = {
|
|
19
|
+
from?: PublicSignKey;
|
|
20
|
+
address: string;
|
|
21
|
+
};
|
|
22
|
+
export type ResponseHandler<Q, R> = (query: Q, context: QueryContext) => Promise<R | undefined> | R | undefined;
|
|
23
|
+
export declare class RPC<Q, R> extends ComposableProgram<RPCSetupOptions<Q, R>> {
|
|
24
|
+
canRead: CanRead;
|
|
25
|
+
private _subscribed;
|
|
26
|
+
private _responseHandler?;
|
|
27
|
+
private _responseResolver;
|
|
28
|
+
private _requestType;
|
|
29
|
+
private _responseType;
|
|
30
|
+
private _rpcTopic;
|
|
31
|
+
private _onMessageBinded;
|
|
32
|
+
private _subscriptionMetaData;
|
|
33
|
+
private _keypair;
|
|
34
|
+
private _getResponseValueFn;
|
|
35
|
+
private _getRequestValueFn;
|
|
36
|
+
open(args: RPCSetupOptions<Q, R>): Promise<void>;
|
|
37
|
+
close(from?: AbstractProgram): Promise<boolean>;
|
|
38
|
+
private _subscribing;
|
|
39
|
+
private _subscribe;
|
|
40
|
+
_onMessage(evt: CustomEvent<{
|
|
41
|
+
data: PubSubData;
|
|
42
|
+
message: DataMessage;
|
|
43
|
+
}>): Promise<void>;
|
|
44
|
+
private seal;
|
|
45
|
+
private getPublishOptions;
|
|
46
|
+
/**
|
|
47
|
+
* Send message and don't expect any response
|
|
48
|
+
* @param message
|
|
49
|
+
* @param options
|
|
50
|
+
*/
|
|
51
|
+
send(message: Q, options?: PublishOptions): Promise<void>;
|
|
52
|
+
private createResponseHandler;
|
|
53
|
+
/**
|
|
54
|
+
* Send a request and expect a response
|
|
55
|
+
* @param request
|
|
56
|
+
* @param options
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
request(request: Q, options?: RPCOptions<R>): Promise<RPCResponse<R>[]>;
|
|
60
|
+
get rpcTopic(): string;
|
|
61
|
+
getTopics(): string[];
|
|
62
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { BorshError, deserialize, serialize, variant, } from "@dao-xyz/borsh";
|
|
8
|
+
import { DecryptedThing, toBase64, AccessError, X25519Keypair, } from "@peerbit/crypto";
|
|
9
|
+
import { RequestV0, ResponseV0, RPCMessage } from "./encoding.js";
|
|
10
|
+
import { logger } from "./io.js";
|
|
11
|
+
import { ComposableProgram } from "@peerbit/program";
|
|
12
|
+
import pDefer from "p-defer";
|
|
13
|
+
import { waitFor } from "@peerbit/time";
|
|
14
|
+
const createValueResolver = (type) => {
|
|
15
|
+
if (type === Uint8Array) {
|
|
16
|
+
return (decrypted) => decrypted._data;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return (decrypted) => decrypted.getValue(type);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export let RPC = class RPC extends ComposableProgram {
|
|
23
|
+
canRead;
|
|
24
|
+
_subscribed = false;
|
|
25
|
+
_responseHandler;
|
|
26
|
+
_responseResolver;
|
|
27
|
+
_requestType;
|
|
28
|
+
_responseType;
|
|
29
|
+
_rpcTopic;
|
|
30
|
+
_onMessageBinded = undefined;
|
|
31
|
+
_subscriptionMetaData;
|
|
32
|
+
_keypair;
|
|
33
|
+
_getResponseValueFn;
|
|
34
|
+
_getRequestValueFn;
|
|
35
|
+
async open(args) {
|
|
36
|
+
this._rpcTopic = args.topic ?? this._rpcTopic;
|
|
37
|
+
this._responseHandler = args.responseHandler;
|
|
38
|
+
this._requestType = args.queryType;
|
|
39
|
+
this._responseType = args.responseType;
|
|
40
|
+
this._responseResolver = new Map();
|
|
41
|
+
this._subscriptionMetaData = args.subscriptionData;
|
|
42
|
+
this.canRead = args.canRead || (() => Promise.resolve(true));
|
|
43
|
+
this._getResponseValueFn = createValueResolver(this._responseType);
|
|
44
|
+
this._getRequestValueFn = createValueResolver(this._requestType);
|
|
45
|
+
this._keypair = await X25519Keypair.create();
|
|
46
|
+
await this._subscribe();
|
|
47
|
+
}
|
|
48
|
+
async close(from) {
|
|
49
|
+
if (this._subscribed) {
|
|
50
|
+
await this.node.services.pubsub.unsubscribe(this.rpcTopic);
|
|
51
|
+
await this.node.services.pubsub.removeEventListener("data", this._onMessage);
|
|
52
|
+
this._subscribed = false;
|
|
53
|
+
}
|
|
54
|
+
return super.close(from);
|
|
55
|
+
}
|
|
56
|
+
_subscribing;
|
|
57
|
+
async _subscribe() {
|
|
58
|
+
await this._subscribing;
|
|
59
|
+
if (this._subscribed) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this._subscribed = true;
|
|
63
|
+
this._onMessageBinded = this._onMessageBinded || this._onMessage.bind(this);
|
|
64
|
+
this._subscribing = this.node.services.pubsub
|
|
65
|
+
.subscribe(this.rpcTopic, { data: this._subscriptionMetaData })
|
|
66
|
+
.then(() => {
|
|
67
|
+
this.node.services.pubsub.addEventListener("data", this._onMessageBinded);
|
|
68
|
+
});
|
|
69
|
+
await this._subscribing;
|
|
70
|
+
await this.node.services.pubsub.requestSubscribers(this.rpcTopic);
|
|
71
|
+
logger.debug("subscribing to query topic (responses): " + this.rpcTopic);
|
|
72
|
+
}
|
|
73
|
+
async _onMessage(evt) {
|
|
74
|
+
const { data, message } = evt.detail;
|
|
75
|
+
if (data?.topics.find((x) => x === this.rpcTopic) != null) {
|
|
76
|
+
try {
|
|
77
|
+
const rpcMessage = deserialize(data.data, RPCMessage);
|
|
78
|
+
if (rpcMessage instanceof RequestV0) {
|
|
79
|
+
if (this._responseHandler) {
|
|
80
|
+
const maybeEncrypted = rpcMessage.request;
|
|
81
|
+
const decrypted = await maybeEncrypted.decrypt(this.node.keychain);
|
|
82
|
+
if (!(await this.canRead(message.sender))) {
|
|
83
|
+
throw new AccessError();
|
|
84
|
+
}
|
|
85
|
+
const response = await this._responseHandler(this._getRequestValueFn(decrypted), {
|
|
86
|
+
address: this.rpcTopic,
|
|
87
|
+
from: message.sender,
|
|
88
|
+
});
|
|
89
|
+
if (response && rpcMessage.respondTo) {
|
|
90
|
+
// send query and wait for replies in a generator like behaviour
|
|
91
|
+
const serializedResponse = serialize(response);
|
|
92
|
+
// we use the peerId/libp2p identity for signatures, since we want to be able to send a message
|
|
93
|
+
// with pubsub with a certain reciever. If we use (this.identity) we are going to use an identity
|
|
94
|
+
// that is now known in the .pubsub network, hence the message might not be delivired if we
|
|
95
|
+
// send with { to: [RECIEVER] } param
|
|
96
|
+
const decryptedMessage = new DecryptedThing({
|
|
97
|
+
data: serializedResponse,
|
|
98
|
+
});
|
|
99
|
+
let maybeEncryptedMessage = decryptedMessage;
|
|
100
|
+
maybeEncryptedMessage = await decryptedMessage.encrypt(this._keypair, rpcMessage.respondTo);
|
|
101
|
+
await this.node.services.pubsub.publish(serialize(new ResponseV0({
|
|
102
|
+
response: maybeEncryptedMessage,
|
|
103
|
+
requestId: message.id,
|
|
104
|
+
})), {
|
|
105
|
+
topics: [this.rpcTopic],
|
|
106
|
+
to: [message.sender],
|
|
107
|
+
strict: true,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (rpcMessage instanceof ResponseV0) {
|
|
113
|
+
const id = toBase64(rpcMessage.requestId);
|
|
114
|
+
let handler = this._responseResolver.get(id);
|
|
115
|
+
if (!handler) {
|
|
116
|
+
handler = await waitFor(() => this._responseResolver.get(id));
|
|
117
|
+
}
|
|
118
|
+
handler({
|
|
119
|
+
message,
|
|
120
|
+
response: rpcMessage,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
if (error instanceof AccessError) {
|
|
126
|
+
logger.debug("Got message I could not decrypt");
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (error instanceof BorshError) {
|
|
130
|
+
logger.error("Got message for a different namespace");
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
logger.error("Error handling query: " +
|
|
134
|
+
(error?.message ? error?.message?.toString() : error));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async seal(request, respondTo, options) {
|
|
139
|
+
const requestData = this._requestType === Uint8Array
|
|
140
|
+
? request
|
|
141
|
+
: serialize(request);
|
|
142
|
+
const decryptedMessage = new DecryptedThing({
|
|
143
|
+
data: requestData,
|
|
144
|
+
});
|
|
145
|
+
let maybeEncryptedMessage = decryptedMessage;
|
|
146
|
+
if (options?.encryption?.responders &&
|
|
147
|
+
options?.encryption?.responders.length > 0) {
|
|
148
|
+
maybeEncryptedMessage = await decryptedMessage.encrypt(options.encryption.key, ...options.encryption.responders);
|
|
149
|
+
}
|
|
150
|
+
const requestMessage = new RequestV0({
|
|
151
|
+
request: maybeEncryptedMessage,
|
|
152
|
+
respondTo,
|
|
153
|
+
});
|
|
154
|
+
return requestMessage;
|
|
155
|
+
}
|
|
156
|
+
getPublishOptions(options) {
|
|
157
|
+
return options?.to
|
|
158
|
+
? { to: options.to, strict: true, topics: [this.rpcTopic] }
|
|
159
|
+
: { topics: [this.rpcTopic] };
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Send message and don't expect any response
|
|
163
|
+
* @param message
|
|
164
|
+
* @param options
|
|
165
|
+
*/
|
|
166
|
+
async send(message, options) {
|
|
167
|
+
await this.node.services.pubsub.publish(serialize(await this.seal(message, undefined, options)), this.getPublishOptions(options));
|
|
168
|
+
}
|
|
169
|
+
createResponseHandler(promise, keypair, allResults, responders, expectedResponders, options) {
|
|
170
|
+
return async (properties) => {
|
|
171
|
+
try {
|
|
172
|
+
const { response, message } = properties;
|
|
173
|
+
const from = message.sender;
|
|
174
|
+
if (options?.isTrusted && !(await options?.isTrusted(from))) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const maybeEncrypted = response.response;
|
|
178
|
+
const decrypted = await maybeEncrypted.decrypt(keypair);
|
|
179
|
+
const resultData = this._getResponseValueFn(decrypted);
|
|
180
|
+
if (expectedResponders) {
|
|
181
|
+
if (from && expectedResponders?.has(from.hashcode())) {
|
|
182
|
+
options?.onResponse && options?.onResponse(resultData, from);
|
|
183
|
+
allResults.push({ response: resultData, from });
|
|
184
|
+
responders.add(from.hashcode());
|
|
185
|
+
if (responders.size === expectedResponders.size) {
|
|
186
|
+
promise.resolve();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
options?.onResponse && options?.onResponse(resultData, from);
|
|
192
|
+
allResults.push({ response: resultData, from });
|
|
193
|
+
if (options?.amount != null &&
|
|
194
|
+
allResults.length >= options?.amount) {
|
|
195
|
+
promise.resolve();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
if (error instanceof AccessError) {
|
|
201
|
+
return; // Ignore things we can not open
|
|
202
|
+
}
|
|
203
|
+
if (error instanceof BorshError && !options?.strict) {
|
|
204
|
+
logger.debug("Namespace error");
|
|
205
|
+
return; // Name space conflict most likely
|
|
206
|
+
}
|
|
207
|
+
console.error("failed ot deserialize query response", error);
|
|
208
|
+
promise.reject(error);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Send a request and expect a response
|
|
214
|
+
* @param request
|
|
215
|
+
* @param options
|
|
216
|
+
* @returns
|
|
217
|
+
*/
|
|
218
|
+
async request(request, options) {
|
|
219
|
+
// We are generatinga new encryption keypair for each send, so we now that when we get the responses, they are encrypted specifcally for me, and for this request
|
|
220
|
+
// this allows us to easily disregard a bunch of message just beacuse they are for a different reciever!
|
|
221
|
+
const keypair = await X25519Keypair.create();
|
|
222
|
+
// send query and wait for replies in a generator like behaviour
|
|
223
|
+
let timeoutFn = undefined;
|
|
224
|
+
const requestMessage = await this.seal(request, keypair.publicKey, options);
|
|
225
|
+
const requestBytes = serialize(requestMessage);
|
|
226
|
+
const allResults = [];
|
|
227
|
+
const deferredPromise = pDefer();
|
|
228
|
+
options?.stopper && options.stopper(deferredPromise.resolve);
|
|
229
|
+
timeoutFn = setTimeout(() => {
|
|
230
|
+
deferredPromise.resolve();
|
|
231
|
+
}, options?.timeout || 10 * 1000);
|
|
232
|
+
const expectedResponders = options?.to && options.to.length > 0
|
|
233
|
+
? new Set(options.to.map((x) => (typeof x === "string" ? x : x.hashcode())))
|
|
234
|
+
: undefined;
|
|
235
|
+
const responders = new Set();
|
|
236
|
+
const id = toBase64(await this.node.services.pubsub.publish(requestBytes, this.getPublishOptions(options)));
|
|
237
|
+
this._responseResolver.set(id, this.createResponseHandler(deferredPromise, keypair, allResults, responders, expectedResponders, options));
|
|
238
|
+
try {
|
|
239
|
+
await deferredPromise.promise;
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
// timeout
|
|
243
|
+
if (error.constructor.name != "TimeoutError") {
|
|
244
|
+
throw new Error("Got unexpected error when query: " + error.constructor.name);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
clearTimeout(timeoutFn);
|
|
249
|
+
}
|
|
250
|
+
this._responseResolver.delete(id);
|
|
251
|
+
return allResults;
|
|
252
|
+
}
|
|
253
|
+
get rpcTopic() {
|
|
254
|
+
if (!this._rpcTopic) {
|
|
255
|
+
throw new Error("Not initialized");
|
|
256
|
+
}
|
|
257
|
+
return this._rpcTopic;
|
|
258
|
+
}
|
|
259
|
+
getTopics() {
|
|
260
|
+
return [this.rpcTopic];
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
RPC = __decorate([
|
|
264
|
+
variant("rpc")
|
|
265
|
+
], RPC);
|
|
266
|
+
//# sourceMappingURL=controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAEN,UAAU,EACV,WAAW,EACX,SAAS,EACT,OAAO,GACP,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,cAAc,EAGd,QAAQ,EACR,WAAW,EAEX,aAAa,GACb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,EAAc,MAAM,EAA+B,MAAM,SAAS,CAAC;AAM1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,MAA2B,MAAM,SAAS,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAsBxC,MAAM,mBAAmB,GAAG,CAC3B,IAAkC,EACY,EAAE;IAChD,IAAK,IAAY,KAAK,UAAU,EAAE;QACjC,OAAO,CAAC,SAAS,EAAE,EAAE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ACxB;QAED,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAClC,OAAO,UAAU,CAAC;IACnB,CAAC;IAED,IAAW,QAAQ;QAClB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACpB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;SACnC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAED,SAAS;QACR,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACxB,CAAC;CACD,CAAA;AAjWY,GAAG;IADf,OAAO,CAAC,KAAK,CAAC;GACF,GAAG,CAiWf"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MaybeEncrypted, X25519PublicKey } from "@peerbit/crypto";
|
|
2
|
+
export declare abstract class RPCMessage {
|
|
3
|
+
}
|
|
4
|
+
export declare class RequestV0 extends RPCMessage {
|
|
5
|
+
respondTo?: X25519PublicKey;
|
|
6
|
+
request: MaybeEncrypted<any>;
|
|
7
|
+
constructor(properties: {
|
|
8
|
+
request: MaybeEncrypted<any>;
|
|
9
|
+
respondTo?: X25519PublicKey;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export declare class ResponseV0 extends RPCMessage {
|
|
13
|
+
requestId: Uint8Array;
|
|
14
|
+
response: MaybeEncrypted<any>;
|
|
15
|
+
constructor(properties: {
|
|
16
|
+
response: MaybeEncrypted<any>;
|
|
17
|
+
requestId: Uint8Array;
|
|
18
|
+
});
|
|
19
|
+
}
|