@peerbit/canonical-client 0.0.0-e209d2e
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/dist/src/auto.d.ts +41 -0
- package/dist/src/auto.d.ts.map +1 -0
- package/dist/src/auto.js +104 -0
- package/dist/src/auto.js.map +1 -0
- package/dist/src/client.d.ts +76 -0
- package/dist/src/client.d.ts.map +1 -0
- package/dist/src/client.js +709 -0
- package/dist/src/client.js.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/peerbit.d.ts +51 -0
- package/dist/src/peerbit.d.ts.map +1 -0
- package/dist/src/peerbit.js +253 -0
- package/dist/src/peerbit.js.map +1 -0
- package/dist/src/service-worker.d.ts +19 -0
- package/dist/src/service-worker.d.ts.map +1 -0
- package/dist/src/service-worker.js +89 -0
- package/dist/src/service-worker.js.map +1 -0
- package/dist/src/window.d.ts +17 -0
- package/dist/src/window.d.ts.map +1 -0
- package/dist/src/window.js +80 -0
- package/dist/src/window.js.map +1 -0
- package/package.json +90 -0
- package/src/auto.ts +166 -0
- package/src/client.ts +722 -0
- package/src/index.ts +9 -0
- package/src/peerbit.ts +354 -0
- package/src/service-worker.ts +118 -0
- package/src/window.ts +112 -0
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
import { deserialize, field, serialize, variant } from "@dao-xyz/borsh";
|
|
36
|
+
import { CanonicalBootstrapRequest, CanonicalConnection, CanonicalControlRequest, CanonicalControlResponse, CanonicalLoadProgramRequest, CanonicalSignRequest, createMessagePortTransport as createControlTransport, createRpcTransport as createRpcTransportBase, } from "@peerbit/canonical-transport";
|
|
37
|
+
import { PreHash, PublicSignKey, SignatureWithKey, } from "@peerbit/crypto";
|
|
38
|
+
class RpcMessage {
|
|
39
|
+
}
|
|
40
|
+
let RpcRequestHeader = (() => {
|
|
41
|
+
let _id_decorators;
|
|
42
|
+
let _id_initializers = [];
|
|
43
|
+
let _id_extraInitializers = [];
|
|
44
|
+
let _method_decorators;
|
|
45
|
+
let _method_initializers = [];
|
|
46
|
+
let _method_extraInitializers = [];
|
|
47
|
+
return class RpcRequestHeader {
|
|
48
|
+
static {
|
|
49
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
50
|
+
_id_decorators = [field({ type: "u32" })];
|
|
51
|
+
_method_decorators = [field({ type: "string" })];
|
|
52
|
+
__esDecorate(null, null, _id_decorators, { kind: "field", name: "id", static: false, private: false, access: { has: obj => "id" in obj, get: obj => obj.id, set: (obj, value) => { obj.id = value; } }, metadata: _metadata }, _id_initializers, _id_extraInitializers);
|
|
53
|
+
__esDecorate(null, null, _method_decorators, { kind: "field", name: "method", static: false, private: false, access: { has: obj => "method" in obj, get: obj => obj.method, set: (obj, value) => { obj.method = value; } }, metadata: _metadata }, _method_initializers, _method_extraInitializers);
|
|
54
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
55
|
+
}
|
|
56
|
+
id = __runInitializers(this, _id_initializers, void 0);
|
|
57
|
+
method = (__runInitializers(this, _id_extraInitializers), __runInitializers(this, _method_initializers, void 0));
|
|
58
|
+
constructor(properties) {
|
|
59
|
+
__runInitializers(this, _method_extraInitializers);
|
|
60
|
+
this.id = properties.id;
|
|
61
|
+
this.method = properties.method;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
})();
|
|
65
|
+
let RpcResponseHeader = (() => {
|
|
66
|
+
let _id_decorators;
|
|
67
|
+
let _id_initializers = [];
|
|
68
|
+
let _id_extraInitializers = [];
|
|
69
|
+
let _method_decorators;
|
|
70
|
+
let _method_initializers = [];
|
|
71
|
+
let _method_extraInitializers = [];
|
|
72
|
+
return class RpcResponseHeader {
|
|
73
|
+
static {
|
|
74
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
75
|
+
_id_decorators = [field({ type: "u32" })];
|
|
76
|
+
_method_decorators = [field({ type: "string" })];
|
|
77
|
+
__esDecorate(null, null, _id_decorators, { kind: "field", name: "id", static: false, private: false, access: { has: obj => "id" in obj, get: obj => obj.id, set: (obj, value) => { obj.id = value; } }, metadata: _metadata }, _id_initializers, _id_extraInitializers);
|
|
78
|
+
__esDecorate(null, null, _method_decorators, { kind: "field", name: "method", static: false, private: false, access: { has: obj => "method" in obj, get: obj => obj.method, set: (obj, value) => { obj.method = value; } }, metadata: _metadata }, _method_initializers, _method_extraInitializers);
|
|
79
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
80
|
+
}
|
|
81
|
+
id = __runInitializers(this, _id_initializers, void 0);
|
|
82
|
+
method = (__runInitializers(this, _id_extraInitializers), __runInitializers(this, _method_initializers, void 0));
|
|
83
|
+
constructor(properties) {
|
|
84
|
+
__runInitializers(this, _method_extraInitializers);
|
|
85
|
+
this.id = properties.id;
|
|
86
|
+
this.method = properties.method;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
})();
|
|
90
|
+
let RpcRequest = (() => {
|
|
91
|
+
let _classDecorators = [variant(0)];
|
|
92
|
+
let _classDescriptor;
|
|
93
|
+
let _classExtraInitializers = [];
|
|
94
|
+
let _classThis;
|
|
95
|
+
let _classSuper = RpcMessage;
|
|
96
|
+
let _header_decorators;
|
|
97
|
+
let _header_initializers = [];
|
|
98
|
+
let _header_extraInitializers = [];
|
|
99
|
+
let _payload_decorators;
|
|
100
|
+
let _payload_initializers = [];
|
|
101
|
+
let _payload_extraInitializers = [];
|
|
102
|
+
var RpcRequest = class extends _classSuper {
|
|
103
|
+
static { _classThis = this; }
|
|
104
|
+
static {
|
|
105
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
106
|
+
_header_decorators = [field({ type: RpcRequestHeader })];
|
|
107
|
+
_payload_decorators = [field({ type: Uint8Array })];
|
|
108
|
+
__esDecorate(null, null, _header_decorators, { kind: "field", name: "header", static: false, private: false, access: { has: obj => "header" in obj, get: obj => obj.header, set: (obj, value) => { obj.header = value; } }, metadata: _metadata }, _header_initializers, _header_extraInitializers);
|
|
109
|
+
__esDecorate(null, null, _payload_decorators, { kind: "field", name: "payload", static: false, private: false, access: { has: obj => "payload" in obj, get: obj => obj.payload, set: (obj, value) => { obj.payload = value; } }, metadata: _metadata }, _payload_initializers, _payload_extraInitializers);
|
|
110
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
111
|
+
RpcRequest = _classThis = _classDescriptor.value;
|
|
112
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
113
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
114
|
+
}
|
|
115
|
+
header = __runInitializers(this, _header_initializers, void 0);
|
|
116
|
+
payload = (__runInitializers(this, _header_extraInitializers), __runInitializers(this, _payload_initializers, void 0));
|
|
117
|
+
constructor(properties) {
|
|
118
|
+
super();
|
|
119
|
+
__runInitializers(this, _payload_extraInitializers);
|
|
120
|
+
this.header = properties.header;
|
|
121
|
+
this.payload = properties.payload;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
return RpcRequest = _classThis;
|
|
125
|
+
})();
|
|
126
|
+
let RpcOk = (() => {
|
|
127
|
+
let _classDecorators = [variant(1)];
|
|
128
|
+
let _classDescriptor;
|
|
129
|
+
let _classExtraInitializers = [];
|
|
130
|
+
let _classThis;
|
|
131
|
+
let _classSuper = RpcMessage;
|
|
132
|
+
let _header_decorators;
|
|
133
|
+
let _header_initializers = [];
|
|
134
|
+
let _header_extraInitializers = [];
|
|
135
|
+
let _payload_decorators;
|
|
136
|
+
let _payload_initializers = [];
|
|
137
|
+
let _payload_extraInitializers = [];
|
|
138
|
+
var RpcOk = class extends _classSuper {
|
|
139
|
+
static { _classThis = this; }
|
|
140
|
+
static {
|
|
141
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
142
|
+
_header_decorators = [field({ type: RpcResponseHeader })];
|
|
143
|
+
_payload_decorators = [field({ type: Uint8Array })];
|
|
144
|
+
__esDecorate(null, null, _header_decorators, { kind: "field", name: "header", static: false, private: false, access: { has: obj => "header" in obj, get: obj => obj.header, set: (obj, value) => { obj.header = value; } }, metadata: _metadata }, _header_initializers, _header_extraInitializers);
|
|
145
|
+
__esDecorate(null, null, _payload_decorators, { kind: "field", name: "payload", static: false, private: false, access: { has: obj => "payload" in obj, get: obj => obj.payload, set: (obj, value) => { obj.payload = value; } }, metadata: _metadata }, _payload_initializers, _payload_extraInitializers);
|
|
146
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
147
|
+
RpcOk = _classThis = _classDescriptor.value;
|
|
148
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
149
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
150
|
+
}
|
|
151
|
+
header = __runInitializers(this, _header_initializers, void 0);
|
|
152
|
+
payload = (__runInitializers(this, _header_extraInitializers), __runInitializers(this, _payload_initializers, void 0));
|
|
153
|
+
constructor(properties) {
|
|
154
|
+
super();
|
|
155
|
+
__runInitializers(this, _payload_extraInitializers);
|
|
156
|
+
this.header = properties.header;
|
|
157
|
+
this.payload = properties.payload;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
return RpcOk = _classThis;
|
|
161
|
+
})();
|
|
162
|
+
let RpcErr = (() => {
|
|
163
|
+
let _classDecorators = [variant(2)];
|
|
164
|
+
let _classDescriptor;
|
|
165
|
+
let _classExtraInitializers = [];
|
|
166
|
+
let _classThis;
|
|
167
|
+
let _classSuper = RpcMessage;
|
|
168
|
+
let _header_decorators;
|
|
169
|
+
let _header_initializers = [];
|
|
170
|
+
let _header_extraInitializers = [];
|
|
171
|
+
let _message_decorators;
|
|
172
|
+
let _message_initializers = [];
|
|
173
|
+
let _message_extraInitializers = [];
|
|
174
|
+
var RpcErr = class extends _classSuper {
|
|
175
|
+
static { _classThis = this; }
|
|
176
|
+
static {
|
|
177
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
178
|
+
_header_decorators = [field({ type: RpcResponseHeader })];
|
|
179
|
+
_message_decorators = [field({ type: "string" })];
|
|
180
|
+
__esDecorate(null, null, _header_decorators, { kind: "field", name: "header", static: false, private: false, access: { has: obj => "header" in obj, get: obj => obj.header, set: (obj, value) => { obj.header = value; } }, metadata: _metadata }, _header_initializers, _header_extraInitializers);
|
|
181
|
+
__esDecorate(null, null, _message_decorators, { kind: "field", name: "message", static: false, private: false, access: { has: obj => "message" in obj, get: obj => obj.message, set: (obj, value) => { obj.message = value; } }, metadata: _metadata }, _message_initializers, _message_extraInitializers);
|
|
182
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
183
|
+
RpcErr = _classThis = _classDescriptor.value;
|
|
184
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
185
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
186
|
+
}
|
|
187
|
+
header = __runInitializers(this, _header_initializers, void 0);
|
|
188
|
+
message = (__runInitializers(this, _header_extraInitializers), __runInitializers(this, _message_initializers, void 0));
|
|
189
|
+
constructor(properties) {
|
|
190
|
+
super();
|
|
191
|
+
__runInitializers(this, _message_extraInitializers);
|
|
192
|
+
this.header = properties.header;
|
|
193
|
+
this.message = properties.message;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
return RpcErr = _classThis;
|
|
197
|
+
})();
|
|
198
|
+
let RpcStream = (() => {
|
|
199
|
+
let _classDecorators = [variant(3)];
|
|
200
|
+
let _classDescriptor;
|
|
201
|
+
let _classExtraInitializers = [];
|
|
202
|
+
let _classThis;
|
|
203
|
+
let _classSuper = RpcMessage;
|
|
204
|
+
let _header_decorators;
|
|
205
|
+
let _header_initializers = [];
|
|
206
|
+
let _header_extraInitializers = [];
|
|
207
|
+
let _payload_decorators;
|
|
208
|
+
let _payload_initializers = [];
|
|
209
|
+
let _payload_extraInitializers = [];
|
|
210
|
+
var RpcStream = class extends _classSuper {
|
|
211
|
+
static { _classThis = this; }
|
|
212
|
+
static {
|
|
213
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
214
|
+
_header_decorators = [field({ type: RpcResponseHeader })];
|
|
215
|
+
_payload_decorators = [field({ type: Uint8Array })];
|
|
216
|
+
__esDecorate(null, null, _header_decorators, { kind: "field", name: "header", static: false, private: false, access: { has: obj => "header" in obj, get: obj => obj.header, set: (obj, value) => { obj.header = value; } }, metadata: _metadata }, _header_initializers, _header_extraInitializers);
|
|
217
|
+
__esDecorate(null, null, _payload_decorators, { kind: "field", name: "payload", static: false, private: false, access: { has: obj => "payload" in obj, get: obj => obj.payload, set: (obj, value) => { obj.payload = value; } }, metadata: _metadata }, _payload_initializers, _payload_extraInitializers);
|
|
218
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
219
|
+
RpcStream = _classThis = _classDescriptor.value;
|
|
220
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
221
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
222
|
+
}
|
|
223
|
+
header = __runInitializers(this, _header_initializers, void 0);
|
|
224
|
+
payload = (__runInitializers(this, _header_extraInitializers), __runInitializers(this, _payload_initializers, void 0));
|
|
225
|
+
constructor(properties) {
|
|
226
|
+
super();
|
|
227
|
+
__runInitializers(this, _payload_extraInitializers);
|
|
228
|
+
this.header = properties.header;
|
|
229
|
+
this.payload = properties.payload;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
return RpcStream = _classThis;
|
|
233
|
+
})();
|
|
234
|
+
let RpcStreamEnd = (() => {
|
|
235
|
+
let _classDecorators = [variant(4)];
|
|
236
|
+
let _classDescriptor;
|
|
237
|
+
let _classExtraInitializers = [];
|
|
238
|
+
let _classThis;
|
|
239
|
+
let _classSuper = RpcMessage;
|
|
240
|
+
let _header_decorators;
|
|
241
|
+
let _header_initializers = [];
|
|
242
|
+
let _header_extraInitializers = [];
|
|
243
|
+
var RpcStreamEnd = class extends _classSuper {
|
|
244
|
+
static { _classThis = this; }
|
|
245
|
+
static {
|
|
246
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
247
|
+
_header_decorators = [field({ type: RpcResponseHeader })];
|
|
248
|
+
__esDecorate(null, null, _header_decorators, { kind: "field", name: "header", static: false, private: false, access: { has: obj => "header" in obj, get: obj => obj.header, set: (obj, value) => { obj.header = value; } }, metadata: _metadata }, _header_initializers, _header_extraInitializers);
|
|
249
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
250
|
+
RpcStreamEnd = _classThis = _classDescriptor.value;
|
|
251
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
252
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
253
|
+
}
|
|
254
|
+
header = __runInitializers(this, _header_initializers, void 0);
|
|
255
|
+
constructor(properties) {
|
|
256
|
+
super();
|
|
257
|
+
__runInitializers(this, _header_extraInitializers);
|
|
258
|
+
this.header = properties.header;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
return RpcStreamEnd = _classThis;
|
|
262
|
+
})();
|
|
263
|
+
let RpcStreamErr = (() => {
|
|
264
|
+
let _classDecorators = [variant(5)];
|
|
265
|
+
let _classDescriptor;
|
|
266
|
+
let _classExtraInitializers = [];
|
|
267
|
+
let _classThis;
|
|
268
|
+
let _classSuper = RpcMessage;
|
|
269
|
+
let _header_decorators;
|
|
270
|
+
let _header_initializers = [];
|
|
271
|
+
let _header_extraInitializers = [];
|
|
272
|
+
let _message_decorators;
|
|
273
|
+
let _message_initializers = [];
|
|
274
|
+
let _message_extraInitializers = [];
|
|
275
|
+
var RpcStreamErr = class extends _classSuper {
|
|
276
|
+
static { _classThis = this; }
|
|
277
|
+
static {
|
|
278
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
279
|
+
_header_decorators = [field({ type: RpcResponseHeader })];
|
|
280
|
+
_message_decorators = [field({ type: "string" })];
|
|
281
|
+
__esDecorate(null, null, _header_decorators, { kind: "field", name: "header", static: false, private: false, access: { has: obj => "header" in obj, get: obj => obj.header, set: (obj, value) => { obj.header = value; } }, metadata: _metadata }, _header_initializers, _header_extraInitializers);
|
|
282
|
+
__esDecorate(null, null, _message_decorators, { kind: "field", name: "message", static: false, private: false, access: { has: obj => "message" in obj, get: obj => obj.message, set: (obj, value) => { obj.message = value; } }, metadata: _metadata }, _message_initializers, _message_extraInitializers);
|
|
283
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
284
|
+
RpcStreamErr = _classThis = _classDescriptor.value;
|
|
285
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
286
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
287
|
+
}
|
|
288
|
+
header = __runInitializers(this, _header_initializers, void 0);
|
|
289
|
+
message = (__runInitializers(this, _header_extraInitializers), __runInitializers(this, _message_initializers, void 0));
|
|
290
|
+
constructor(properties) {
|
|
291
|
+
super();
|
|
292
|
+
__runInitializers(this, _message_extraInitializers);
|
|
293
|
+
this.header = properties.header;
|
|
294
|
+
this.message = properties.message;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
return RpcStreamErr = _classThis;
|
|
298
|
+
})();
|
|
299
|
+
const isMessagePort = (value) => typeof value.postMessage === "function";
|
|
300
|
+
const toRpcMessage = (data) => {
|
|
301
|
+
try {
|
|
302
|
+
return deserialize(data, RpcMessage);
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
const createCloseAwareRpcTransport = (base, options) => {
|
|
309
|
+
const closeErrorMessage = options.closeErrorMessage ?? "RPC transport closed";
|
|
310
|
+
const handlers = new Set();
|
|
311
|
+
const inFlight = new Map();
|
|
312
|
+
let closed = false;
|
|
313
|
+
let offClose;
|
|
314
|
+
const ignoreTimeoutForMethod = options.ignoreTimeoutForMethod ??
|
|
315
|
+
((method) => method.startsWith("$events:") ||
|
|
316
|
+
method.startsWith("$watch:") ||
|
|
317
|
+
method.startsWith("$presentWatch:"));
|
|
318
|
+
const resolveTimeoutMs = (method) => {
|
|
319
|
+
if (ignoreTimeoutForMethod(method))
|
|
320
|
+
return undefined;
|
|
321
|
+
const configured = options.requestTimeoutMs;
|
|
322
|
+
if (configured == null)
|
|
323
|
+
return undefined;
|
|
324
|
+
const value = typeof configured === "function" ? configured(method) : configured;
|
|
325
|
+
if (value == null || value <= 0)
|
|
326
|
+
return undefined;
|
|
327
|
+
return value;
|
|
328
|
+
};
|
|
329
|
+
const emit = (data) => {
|
|
330
|
+
for (const handler of handlers) {
|
|
331
|
+
try {
|
|
332
|
+
handler(data);
|
|
333
|
+
}
|
|
334
|
+
catch { }
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
const unsubscribeBase = base.onMessage((data) => {
|
|
338
|
+
const msg = toRpcMessage(data);
|
|
339
|
+
if (msg instanceof RpcOk ||
|
|
340
|
+
msg instanceof RpcErr ||
|
|
341
|
+
msg instanceof RpcStream ||
|
|
342
|
+
msg instanceof RpcStreamEnd ||
|
|
343
|
+
msg instanceof RpcStreamErr) {
|
|
344
|
+
const id = msg.header.id;
|
|
345
|
+
const entry = inFlight.get(id);
|
|
346
|
+
if (entry?.timeout) {
|
|
347
|
+
clearTimeout(entry.timeout);
|
|
348
|
+
entry.timeout = undefined;
|
|
349
|
+
}
|
|
350
|
+
if (msg instanceof RpcOk ||
|
|
351
|
+
msg instanceof RpcErr ||
|
|
352
|
+
msg instanceof RpcStreamEnd ||
|
|
353
|
+
msg instanceof RpcStreamErr) {
|
|
354
|
+
inFlight.delete(id);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
emit(data);
|
|
358
|
+
});
|
|
359
|
+
const close = (message = closeErrorMessage) => {
|
|
360
|
+
if (closed)
|
|
361
|
+
return;
|
|
362
|
+
closed = true;
|
|
363
|
+
if (offClose)
|
|
364
|
+
offClose();
|
|
365
|
+
unsubscribeBase();
|
|
366
|
+
for (const [id, entry] of inFlight) {
|
|
367
|
+
if (entry.timeout)
|
|
368
|
+
clearTimeout(entry.timeout);
|
|
369
|
+
emit(serialize(new RpcErr({
|
|
370
|
+
header: new RpcResponseHeader({ id, method: entry.method }),
|
|
371
|
+
message,
|
|
372
|
+
})));
|
|
373
|
+
}
|
|
374
|
+
inFlight.clear();
|
|
375
|
+
};
|
|
376
|
+
if (options.onClose) {
|
|
377
|
+
offClose = options.onClose(() => close(closeErrorMessage));
|
|
378
|
+
}
|
|
379
|
+
return {
|
|
380
|
+
send: (data) => {
|
|
381
|
+
const msg = toRpcMessage(data);
|
|
382
|
+
if (closed) {
|
|
383
|
+
if (msg instanceof RpcRequest) {
|
|
384
|
+
emit(serialize(new RpcErr({
|
|
385
|
+
header: new RpcResponseHeader({
|
|
386
|
+
id: msg.header.id,
|
|
387
|
+
method: msg.header.method,
|
|
388
|
+
}),
|
|
389
|
+
message: closeErrorMessage,
|
|
390
|
+
})));
|
|
391
|
+
}
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (msg instanceof RpcRequest) {
|
|
395
|
+
const method = msg.header.method;
|
|
396
|
+
const timeoutMs = resolveTimeoutMs(method);
|
|
397
|
+
const id = msg.header.id;
|
|
398
|
+
const entry = {
|
|
399
|
+
method,
|
|
400
|
+
timeout: timeoutMs
|
|
401
|
+
? setTimeout(() => {
|
|
402
|
+
const current = inFlight.get(id);
|
|
403
|
+
if (!current)
|
|
404
|
+
return;
|
|
405
|
+
inFlight.delete(id);
|
|
406
|
+
emit(serialize(new RpcErr({
|
|
407
|
+
header: new RpcResponseHeader({
|
|
408
|
+
id,
|
|
409
|
+
method: current.method,
|
|
410
|
+
}),
|
|
411
|
+
message: `RPC request timeout (${current.method})`,
|
|
412
|
+
})));
|
|
413
|
+
}, timeoutMs)
|
|
414
|
+
: undefined,
|
|
415
|
+
};
|
|
416
|
+
inFlight.set(id, entry);
|
|
417
|
+
}
|
|
418
|
+
base.send(data);
|
|
419
|
+
},
|
|
420
|
+
onMessage: (handler) => {
|
|
421
|
+
handlers.add(handler);
|
|
422
|
+
return () => {
|
|
423
|
+
handlers.delete(handler);
|
|
424
|
+
};
|
|
425
|
+
},
|
|
426
|
+
close,
|
|
427
|
+
};
|
|
428
|
+
};
|
|
429
|
+
export const createMessagePortTransport = (port, options) => {
|
|
430
|
+
const base = createRpcTransportBase(port);
|
|
431
|
+
const shouldWrap = typeof options?.requestTimeoutMs !== "undefined" ||
|
|
432
|
+
typeof options?.ignoreTimeoutForMethod === "function" ||
|
|
433
|
+
typeof options?.closeErrorMessage === "string";
|
|
434
|
+
if (isMessagePort(port)) {
|
|
435
|
+
if (!shouldWrap)
|
|
436
|
+
return base;
|
|
437
|
+
return createCloseAwareRpcTransport(base, {
|
|
438
|
+
requestTimeoutMs: options?.requestTimeoutMs,
|
|
439
|
+
ignoreTimeoutForMethod: options?.ignoreTimeoutForMethod,
|
|
440
|
+
closeErrorMessage: options?.closeErrorMessage,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
const channel = port;
|
|
444
|
+
if (typeof channel.onClose !== "function") {
|
|
445
|
+
if (!shouldWrap)
|
|
446
|
+
return base;
|
|
447
|
+
return createCloseAwareRpcTransport(base, {
|
|
448
|
+
requestTimeoutMs: options?.requestTimeoutMs,
|
|
449
|
+
ignoreTimeoutForMethod: options?.ignoreTimeoutForMethod,
|
|
450
|
+
closeErrorMessage: options?.closeErrorMessage,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
return createCloseAwareRpcTransport(base, {
|
|
454
|
+
onClose: (handler) => channel.onClose?.(handler) ?? (() => { }),
|
|
455
|
+
requestTimeoutMs: options?.requestTimeoutMs,
|
|
456
|
+
ignoreTimeoutForMethod: options?.ignoreTimeoutForMethod,
|
|
457
|
+
closeErrorMessage: options?.closeErrorMessage,
|
|
458
|
+
});
|
|
459
|
+
};
|
|
460
|
+
export class CanonicalClient {
|
|
461
|
+
nextId = 1;
|
|
462
|
+
pending = new Map();
|
|
463
|
+
connection;
|
|
464
|
+
unsubscribe;
|
|
465
|
+
keepAliveTimer;
|
|
466
|
+
keepAliveInFlight;
|
|
467
|
+
requestTimeoutMs;
|
|
468
|
+
cachedPeerId;
|
|
469
|
+
cachedPublicKey;
|
|
470
|
+
cachedMultiaddrs;
|
|
471
|
+
cachedIdentity;
|
|
472
|
+
constructor(control, options) {
|
|
473
|
+
const transport = typeof control.postMessage === "function"
|
|
474
|
+
? createControlTransport(control)
|
|
475
|
+
: control;
|
|
476
|
+
this.connection = new CanonicalConnection(transport);
|
|
477
|
+
this.requestTimeoutMs = options?.requestTimeoutMs;
|
|
478
|
+
this.unsubscribe = this.connection.onControl((frame) => {
|
|
479
|
+
if (frame instanceof CanonicalControlResponse) {
|
|
480
|
+
this.onMessage(frame);
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
static async create(control, options) {
|
|
485
|
+
const client = new CanonicalClient(control, options);
|
|
486
|
+
await client.init();
|
|
487
|
+
return client;
|
|
488
|
+
}
|
|
489
|
+
async init() {
|
|
490
|
+
if (this.cachedPublicKey)
|
|
491
|
+
return;
|
|
492
|
+
await this.peerInfo();
|
|
493
|
+
}
|
|
494
|
+
close() {
|
|
495
|
+
if (this.keepAliveTimer) {
|
|
496
|
+
clearInterval(this.keepAliveTimer);
|
|
497
|
+
this.keepAliveTimer = undefined;
|
|
498
|
+
}
|
|
499
|
+
this.keepAliveInFlight = undefined;
|
|
500
|
+
this.unsubscribe();
|
|
501
|
+
this.connection.close();
|
|
502
|
+
for (const [id, p] of this.pending) {
|
|
503
|
+
if (p.timeout)
|
|
504
|
+
clearTimeout(p.timeout);
|
|
505
|
+
p.reject(new Error("CanonicalClient closed"));
|
|
506
|
+
this.pending.delete(id);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
async peerId() {
|
|
510
|
+
if (this.cachedPeerId)
|
|
511
|
+
return this.cachedPeerId;
|
|
512
|
+
const resp = await this.request({ op: "peerId" });
|
|
513
|
+
if (!resp.peerId) {
|
|
514
|
+
throw new Error("Canonical peerId response missing peerId");
|
|
515
|
+
}
|
|
516
|
+
this.cachedPeerId = resp.peerId;
|
|
517
|
+
return resp.peerId;
|
|
518
|
+
}
|
|
519
|
+
async peerInfo() {
|
|
520
|
+
const resp = await this.request({ op: "peerInfo" });
|
|
521
|
+
if (!resp.peerId) {
|
|
522
|
+
throw new Error("Canonical peerInfo response missing peerId");
|
|
523
|
+
}
|
|
524
|
+
if (!resp.payload) {
|
|
525
|
+
throw new Error("Canonical peerInfo response missing payload");
|
|
526
|
+
}
|
|
527
|
+
const publicKey = deserialize(resp.payload, PublicSignKey);
|
|
528
|
+
const multiaddrs = resp.strings ?? [];
|
|
529
|
+
this.cachedPeerId = resp.peerId;
|
|
530
|
+
this.cachedPublicKey = publicKey;
|
|
531
|
+
this.cachedMultiaddrs = multiaddrs;
|
|
532
|
+
this.cachedIdentity = undefined;
|
|
533
|
+
return { peerId: resp.peerId, publicKey, multiaddrs };
|
|
534
|
+
}
|
|
535
|
+
async multiaddrs() {
|
|
536
|
+
if (this.cachedMultiaddrs)
|
|
537
|
+
return this.cachedMultiaddrs;
|
|
538
|
+
const info = await this.peerInfo();
|
|
539
|
+
return info.multiaddrs;
|
|
540
|
+
}
|
|
541
|
+
getMultiaddrs() {
|
|
542
|
+
return this.multiaddrs();
|
|
543
|
+
}
|
|
544
|
+
async dial(address) {
|
|
545
|
+
const value = typeof address === "string" ? address : address.toString();
|
|
546
|
+
await this.request({ op: "dial", name: value });
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
async hangUp(address) {
|
|
550
|
+
const value = typeof address === "string" ? address : address.toString();
|
|
551
|
+
await this.request({ op: "hangUp", name: value });
|
|
552
|
+
}
|
|
553
|
+
async start() {
|
|
554
|
+
await this.request({ op: "start" });
|
|
555
|
+
}
|
|
556
|
+
async stop() {
|
|
557
|
+
await this.request({ op: "stop" });
|
|
558
|
+
}
|
|
559
|
+
async bootstrap(addresses) {
|
|
560
|
+
const payload = addresses && addresses.length > 0
|
|
561
|
+
? serialize(new CanonicalBootstrapRequest({ addresses }))
|
|
562
|
+
: undefined;
|
|
563
|
+
await this.request({ op: "bootstrap", payload });
|
|
564
|
+
}
|
|
565
|
+
async loadProgram(address, options) {
|
|
566
|
+
if (!address) {
|
|
567
|
+
throw new Error("Canonical loadProgram requires address to be set");
|
|
568
|
+
}
|
|
569
|
+
const payload = options?.timeoutMs != null
|
|
570
|
+
? serialize(new CanonicalLoadProgramRequest({ timeoutMs: options.timeoutMs }))
|
|
571
|
+
: undefined;
|
|
572
|
+
const resp = await this.request({
|
|
573
|
+
op: "loadProgram",
|
|
574
|
+
name: address,
|
|
575
|
+
payload,
|
|
576
|
+
});
|
|
577
|
+
if (!resp.payload) {
|
|
578
|
+
throw new Error("Canonical loadProgram response missing payload");
|
|
579
|
+
}
|
|
580
|
+
return resp.payload;
|
|
581
|
+
}
|
|
582
|
+
async sign(data, prehash = PreHash.NONE) {
|
|
583
|
+
const resp = await this.request({
|
|
584
|
+
op: "sign",
|
|
585
|
+
payload: serialize(new CanonicalSignRequest({
|
|
586
|
+
data,
|
|
587
|
+
prehash,
|
|
588
|
+
})),
|
|
589
|
+
});
|
|
590
|
+
if (!resp.payload) {
|
|
591
|
+
throw new Error("Canonical sign response missing payload");
|
|
592
|
+
}
|
|
593
|
+
return deserialize(resp.payload, SignatureWithKey);
|
|
594
|
+
}
|
|
595
|
+
get identity() {
|
|
596
|
+
if (!this.cachedPublicKey) {
|
|
597
|
+
throw new Error("CanonicalClient not initialized (missing publicKey); call await CanonicalClient.create(...) or await client.init()");
|
|
598
|
+
}
|
|
599
|
+
if (this.cachedIdentity)
|
|
600
|
+
return this.cachedIdentity;
|
|
601
|
+
const publicKey = this.cachedPublicKey;
|
|
602
|
+
this.cachedIdentity = {
|
|
603
|
+
publicKey,
|
|
604
|
+
sign: (data, prehash) => this.sign(data, prehash ?? PreHash.NONE),
|
|
605
|
+
signer: (prehash = PreHash.NONE) => (data) => this.sign(data, prehash),
|
|
606
|
+
};
|
|
607
|
+
return this.cachedIdentity;
|
|
608
|
+
}
|
|
609
|
+
async openPort(name, payload) {
|
|
610
|
+
const resp = await this.request({
|
|
611
|
+
op: "open",
|
|
612
|
+
name,
|
|
613
|
+
payload,
|
|
614
|
+
});
|
|
615
|
+
if (resp.channelId == null) {
|
|
616
|
+
throw new Error("Canonical open response missing channelId");
|
|
617
|
+
}
|
|
618
|
+
return this.connection.createChannel(resp.channelId);
|
|
619
|
+
}
|
|
620
|
+
async ping() {
|
|
621
|
+
await this.request({ op: "ping" });
|
|
622
|
+
}
|
|
623
|
+
startKeepAlive(options) {
|
|
624
|
+
if (this.keepAliveTimer) {
|
|
625
|
+
clearInterval(this.keepAliveTimer);
|
|
626
|
+
}
|
|
627
|
+
const interval = Math.max(1000, options?.intervalMs ?? 30_000);
|
|
628
|
+
const timeoutMs = Math.max(1000, options?.timeoutMs ?? this.requestTimeoutMs ?? 10_000);
|
|
629
|
+
const closeOnFail = options?.closeOnFail ?? true;
|
|
630
|
+
const maxFailures = Math.max(1, options?.maxFailures ?? 2);
|
|
631
|
+
let failures = 0;
|
|
632
|
+
this.keepAliveTimer = setInterval(() => {
|
|
633
|
+
if (this.keepAliveInFlight)
|
|
634
|
+
return;
|
|
635
|
+
this.keepAliveInFlight = this.request({ op: "ping" }, { timeoutMs })
|
|
636
|
+
.then(() => {
|
|
637
|
+
failures = 0;
|
|
638
|
+
})
|
|
639
|
+
.catch(() => {
|
|
640
|
+
failures += 1;
|
|
641
|
+
if (closeOnFail && failures >= maxFailures) {
|
|
642
|
+
this.close();
|
|
643
|
+
}
|
|
644
|
+
})
|
|
645
|
+
.finally(() => {
|
|
646
|
+
this.keepAliveInFlight = undefined;
|
|
647
|
+
});
|
|
648
|
+
}, interval);
|
|
649
|
+
return () => {
|
|
650
|
+
if (this.keepAliveTimer) {
|
|
651
|
+
clearInterval(this.keepAliveTimer);
|
|
652
|
+
this.keepAliveTimer = undefined;
|
|
653
|
+
}
|
|
654
|
+
this.keepAliveInFlight = undefined;
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
onMessage(message) {
|
|
658
|
+
const entry = this.pending.get(message.id);
|
|
659
|
+
if (!entry)
|
|
660
|
+
return;
|
|
661
|
+
this.pending.delete(message.id);
|
|
662
|
+
if (entry.timeout)
|
|
663
|
+
clearTimeout(entry.timeout);
|
|
664
|
+
if (message.ok === false) {
|
|
665
|
+
entry.reject(new Error(message.error ?? "Unknown error"));
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
entry.resolve(message);
|
|
669
|
+
}
|
|
670
|
+
request(message, options) {
|
|
671
|
+
const id = this.nextId++;
|
|
672
|
+
const timeoutMs = options?.timeoutMs ?? this.requestTimeoutMs;
|
|
673
|
+
return new Promise((resolve, reject) => {
|
|
674
|
+
const entry = { resolve, reject };
|
|
675
|
+
if (timeoutMs && timeoutMs > 0) {
|
|
676
|
+
entry.timeout = setTimeout(() => {
|
|
677
|
+
if (!this.pending.has(id))
|
|
678
|
+
return;
|
|
679
|
+
this.pending.delete(id);
|
|
680
|
+
reject(new Error(`Canonical request timeout (${message.op})`));
|
|
681
|
+
}, timeoutMs);
|
|
682
|
+
}
|
|
683
|
+
this.pending.set(id, entry);
|
|
684
|
+
try {
|
|
685
|
+
this.connection.sendControl(new CanonicalControlRequest({
|
|
686
|
+
id,
|
|
687
|
+
op: message.op,
|
|
688
|
+
...("name" in message ? { name: message.name } : {}),
|
|
689
|
+
...("payload" in message ? { payload: message.payload } : {}),
|
|
690
|
+
}));
|
|
691
|
+
}
|
|
692
|
+
catch (e) {
|
|
693
|
+
if (entry.timeout)
|
|
694
|
+
clearTimeout(entry.timeout);
|
|
695
|
+
this.pending.delete(id);
|
|
696
|
+
reject(e);
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
export const connectSharedWorker = async (worker, options) => {
|
|
702
|
+
return CanonicalClient.create(worker.port, options);
|
|
703
|
+
};
|
|
704
|
+
export const connectSharedWorkerPeerbit = async (worker, options) => {
|
|
705
|
+
const canonical = await connectSharedWorker(worker);
|
|
706
|
+
const { PeerbitCanonicalClient } = await import("./peerbit.js");
|
|
707
|
+
return PeerbitCanonicalClient.create(canonical, options);
|
|
708
|
+
};
|
|
709
|
+
//# sourceMappingURL=client.js.map
|