smoldot 1.0.4 → 1.0.6
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/README.md +58 -0
- package/dist/cjs/bytecode-browser.d.ts +5 -0
- package/dist/cjs/bytecode-browser.js +42 -0
- package/dist/cjs/bytecode-deno.d.ts +5 -0
- package/dist/cjs/bytecode-deno.js +87 -0
- package/dist/cjs/bytecode-nodejs.d.ts +5 -0
- package/dist/cjs/bytecode-nodejs.js +41 -0
- package/dist/cjs/index-browser.d.ts +2 -2
- package/dist/cjs/index-browser.js +10 -507
- package/dist/cjs/index-deno.d.ts +2 -2
- package/dist/cjs/index-deno.js +10 -283
- package/dist/cjs/index-nodejs.d.ts +2 -2
- package/dist/cjs/index-nodejs.js +10 -211
- package/dist/cjs/internals/bytecode/wasm0.js +4 -0
- package/dist/cjs/internals/bytecode/wasm1.js +4 -0
- package/dist/cjs/internals/bytecode/wasm2.js +4 -0
- package/dist/cjs/{instance/bindings-smoldot-light.d.ts → internals/client.d.ts} +15 -34
- package/dist/cjs/internals/client.js +376 -0
- package/dist/cjs/internals/local-instance.d.ts +144 -0
- package/dist/cjs/internals/local-instance.js +566 -0
- package/dist/cjs/internals/remote-instance.d.ts +38 -0
- package/dist/cjs/internals/remote-instance.js +374 -0
- package/dist/cjs/no-auto-bytecode-browser.d.ts +11 -0
- package/dist/cjs/no-auto-bytecode-browser.js +481 -0
- package/dist/cjs/no-auto-bytecode-deno.d.ts +10 -0
- package/dist/cjs/no-auto-bytecode-deno.js +220 -0
- package/dist/cjs/no-auto-bytecode-nodejs.d.ts +10 -0
- package/dist/cjs/no-auto-bytecode-nodejs.js +189 -0
- package/dist/cjs/{client.d.ts → public-types.d.ts} +51 -3
- package/dist/cjs/public-types.js +76 -0
- package/dist/cjs/worker-browser.d.ts +11 -0
- package/dist/cjs/worker-browser.js +63 -0
- package/dist/cjs/worker-deno.d.ts +11 -0
- package/dist/cjs/worker-deno.js +54 -0
- package/dist/cjs/worker-nodejs.d.ts +11 -0
- package/dist/cjs/worker-nodejs.js +55 -0
- package/dist/mjs/bytecode-browser.d.ts +5 -0
- package/dist/mjs/bytecode-browser.js +38 -0
- package/dist/mjs/bytecode-deno.d.ts +5 -0
- package/dist/mjs/bytecode-deno.js +83 -0
- package/dist/mjs/bytecode-nodejs.d.ts +5 -0
- package/dist/mjs/bytecode-nodejs.js +37 -0
- package/dist/mjs/index-browser.d.ts +2 -2
- package/dist/mjs/index-browser.js +4 -501
- package/dist/mjs/index-deno.d.ts +2 -2
- package/dist/mjs/index-deno.js +4 -277
- package/dist/mjs/index-nodejs.d.ts +2 -2
- package/dist/mjs/index-nodejs.js +4 -205
- package/dist/mjs/internals/bytecode/wasm0.js +1 -0
- package/dist/mjs/internals/bytecode/wasm1.js +1 -0
- package/dist/mjs/internals/bytecode/wasm2.js +1 -0
- package/dist/mjs/{instance/bindings-smoldot-light.d.ts → internals/client.d.ts} +15 -34
- package/dist/mjs/internals/client.js +372 -0
- package/dist/mjs/internals/local-instance.d.ts +144 -0
- package/dist/mjs/internals/local-instance.js +561 -0
- package/dist/mjs/internals/remote-instance.d.ts +38 -0
- package/dist/mjs/internals/remote-instance.js +369 -0
- package/dist/mjs/no-auto-bytecode-browser.d.ts +11 -0
- package/dist/mjs/no-auto-bytecode-browser.js +471 -0
- package/dist/mjs/no-auto-bytecode-deno.d.ts +10 -0
- package/dist/mjs/no-auto-bytecode-deno.js +210 -0
- package/dist/mjs/no-auto-bytecode-nodejs.d.ts +10 -0
- package/dist/mjs/no-auto-bytecode-nodejs.js +179 -0
- package/dist/mjs/{client.d.ts → public-types.d.ts} +51 -3
- package/dist/mjs/public-types.js +67 -0
- package/dist/mjs/worker-browser.d.ts +11 -0
- package/dist/mjs/worker-browser.js +59 -0
- package/dist/mjs/worker-deno.d.ts +11 -0
- package/dist/mjs/worker-deno.js +50 -0
- package/dist/mjs/worker-nodejs.d.ts +11 -0
- package/dist/mjs/worker-nodejs.js +51 -0
- package/package.json +40 -8
- package/dist/cjs/client.js +0 -172
- package/dist/cjs/instance/autogen/wasm0.js +0 -4
- package/dist/cjs/instance/autogen/wasm1.js +0 -4
- package/dist/cjs/instance/autogen/wasm2.js +0 -4
- package/dist/cjs/instance/bindings-smoldot-light.js +0 -285
- package/dist/cjs/instance/bindings-wasi.d.ts +0 -27
- package/dist/cjs/instance/bindings-wasi.js +0 -172
- package/dist/cjs/instance/bindings.d.ts +0 -31
- package/dist/cjs/instance/bindings.js +0 -5
- package/dist/cjs/instance/instance.d.ts +0 -45
- package/dist/cjs/instance/instance.js +0 -277
- package/dist/cjs/instance/raw-instance.d.ts +0 -56
- package/dist/cjs/instance/raw-instance.js +0 -129
- package/dist/mjs/client.js +0 -162
- package/dist/mjs/instance/autogen/wasm0.js +0 -1
- package/dist/mjs/instance/autogen/wasm1.js +0 -1
- package/dist/mjs/instance/autogen/wasm2.js +0 -1
- package/dist/mjs/instance/bindings-smoldot-light.js +0 -280
- package/dist/mjs/instance/bindings-wasi.d.ts +0 -27
- package/dist/mjs/instance/bindings-wasi.js +0 -170
- package/dist/mjs/instance/bindings.d.ts +0 -31
- package/dist/mjs/instance/bindings.js +0 -4
- package/dist/mjs/instance/instance.d.ts +0 -45
- package/dist/mjs/instance/instance.js +0 -269
- package/dist/mjs/instance/raw-instance.d.ts +0 -56
- package/dist/mjs/instance/raw-instance.js +0 -124
- /package/dist/cjs/{base64.d.ts → internals/base64.d.ts} +0 -0
- /package/dist/cjs/{base64.js → internals/base64.js} +0 -0
- /package/dist/cjs/{instance → internals}/buffer.d.ts +0 -0
- /package/dist/cjs/{instance → internals}/buffer.js +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm.d.ts +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm.js +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm0.d.ts +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm1.d.ts +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm2.d.ts +0 -0
- /package/dist/mjs/{base64.d.ts → internals/base64.d.ts} +0 -0
- /package/dist/mjs/{base64.js → internals/base64.js} +0 -0
- /package/dist/mjs/{instance → internals}/buffer.d.ts +0 -0
- /package/dist/mjs/{instance → internals}/buffer.js +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm.d.ts +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm.js +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm0.d.ts +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm1.d.ts +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm2.d.ts +0 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Smoldot
|
|
3
|
+
// Copyright (C) 2023 Pierre Krieger
|
|
4
|
+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
5
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
6
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
7
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
9
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
10
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
11
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.startInstanceServer = exports.connectToInstanceServer = void 0;
|
|
16
|
+
// This program is free software: you can redistribute it and/or modify
|
|
17
|
+
// it under the terms of the GNU General Public License as published by
|
|
18
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
19
|
+
// (at your option) any later version.
|
|
20
|
+
// This program is distributed in the hope that it will be useful,
|
|
21
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
23
|
+
// GNU General Public License for more details.
|
|
24
|
+
// You should have received a copy of the GNU General Public License
|
|
25
|
+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
26
|
+
// Contains an implementation of `Instance` that is remote.
|
|
27
|
+
//
|
|
28
|
+
// In terms of implementation, the logic is pretty straight forward, with two exceptions:
|
|
29
|
+
//
|
|
30
|
+
// - Connections are tracked on both sides in order to handle situations where one side has
|
|
31
|
+
// reset a connection or stream but the other is sending messages about this connection/stream.
|
|
32
|
+
//
|
|
33
|
+
// - JSON-RPC requests aren't sent back lazily one at a time. Instead, the client indicates that it
|
|
34
|
+
// is ready to accept more JSON-RPC responses, after which the server can send responses at any
|
|
35
|
+
// time and the client queues them locally.
|
|
36
|
+
const instance = require("./local-instance.js");
|
|
37
|
+
// Implementation note: it is unclear even in the official specification
|
|
38
|
+
// (https://html.spec.whatwg.org/multipage/web-messaging.html) whether both sides of a
|
|
39
|
+
// `MessagePort` should be closed, or if one is enough.
|
|
40
|
+
//
|
|
41
|
+
// It has been noticed that doing `port.postMessage(...); port.close();` doesn't deliver the
|
|
42
|
+
// message on Firefox (but it does on Chrome). The code below takes note of this, and only closes
|
|
43
|
+
// a port upon *receiving* the last possible message. It therefore assumes that closing only one
|
|
44
|
+
// side is enough. It is unclear whether this causes any memory leak.
|
|
45
|
+
function connectToInstanceServer(config) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
// Send the wasm module and configuration to the server.
|
|
48
|
+
// Note that we await the `wasmModule` `Promise` here.
|
|
49
|
+
// If instead we used `wasmModule.then(...)`, the user would be able to start using the
|
|
50
|
+
// returned instance before the module has been sent to the server.
|
|
51
|
+
// In order to simplify the implementation, we create new ports and send one of them to
|
|
52
|
+
// the server. This is necessary so that the server can pause receiving messages while the
|
|
53
|
+
// instance is being initialized.
|
|
54
|
+
const { port1: portToServer, port2: serverToClient } = new MessageChannel();
|
|
55
|
+
const initialPort = config.portToServer;
|
|
56
|
+
const initialMessage = {
|
|
57
|
+
wasmModule: yield config.wasmModule,
|
|
58
|
+
serverToClient,
|
|
59
|
+
maxLogLevel: config.maxLogLevel,
|
|
60
|
+
cpuRateLimit: config.cpuRateLimit,
|
|
61
|
+
forbidWs: config.forbidWs,
|
|
62
|
+
forbidWss: config.forbidWss,
|
|
63
|
+
forbidNonLocalWs: config.forbidNonLocalWs,
|
|
64
|
+
forbidTcp: config.forbidTcp,
|
|
65
|
+
forbidWebRtc: config.forbidWebRtc
|
|
66
|
+
};
|
|
67
|
+
initialPort.postMessage(initialMessage, [serverToClient]);
|
|
68
|
+
// Note that closing `initialPort` here will lead to the message not being delivered on Firefox
|
|
69
|
+
// for some reason. It is therefore closed only on shutdown.
|
|
70
|
+
const state = {
|
|
71
|
+
jsonRpcResponses: new Map(),
|
|
72
|
+
connections: new Map(),
|
|
73
|
+
};
|
|
74
|
+
portToServer.onmessage = (messageEvent) => {
|
|
75
|
+
const message = messageEvent.data;
|
|
76
|
+
// Update some local state.
|
|
77
|
+
switch (message.ty) {
|
|
78
|
+
case "wasm-panic":
|
|
79
|
+
case "executor-shutdown": {
|
|
80
|
+
portToServer.close();
|
|
81
|
+
initialPort.close();
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case "add-chain-result": {
|
|
85
|
+
if (message.success) {
|
|
86
|
+
state.jsonRpcResponses.set(message.chainId, new Array);
|
|
87
|
+
const moreAccepted = { ty: "accept-more-json-rpc-answers", chainId: message.chainId };
|
|
88
|
+
for (let i = 0; i < 10; ++i)
|
|
89
|
+
portToServer.postMessage(moreAccepted);
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "new-connection": {
|
|
94
|
+
state.connections.set(message.connectionId, new Set());
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "connection-reset": {
|
|
98
|
+
// The connection might have been reset locally in the past.
|
|
99
|
+
if (!state.connections.has(message.connectionId))
|
|
100
|
+
return;
|
|
101
|
+
state.connections.delete(message.connectionId);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case "connection-stream-open": {
|
|
105
|
+
// The connection might have been reset locally in the past.
|
|
106
|
+
if (!state.connections.has(message.connectionId))
|
|
107
|
+
return;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case "connection-stream-reset": {
|
|
111
|
+
// The connection might have been reset locally in the past.
|
|
112
|
+
if (!state.connections.has(message.connectionId))
|
|
113
|
+
return;
|
|
114
|
+
// The stream might have been reset locally in the past.
|
|
115
|
+
if (!state.connections.get(message.connectionId).has(message.streamId))
|
|
116
|
+
return;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case "stream-send": {
|
|
120
|
+
// The connection might have been reset locally in the past.
|
|
121
|
+
if (!state.connections.has(message.connectionId))
|
|
122
|
+
return;
|
|
123
|
+
// The stream might have been reset locally in the past.
|
|
124
|
+
if (message.streamId && !state.connections.get(message.connectionId).has(message.streamId))
|
|
125
|
+
return;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case "stream-send-close": {
|
|
129
|
+
// The connection might have been reset locally in the past.
|
|
130
|
+
if (!state.connections.has(message.connectionId))
|
|
131
|
+
return;
|
|
132
|
+
// The stream might have been reset locally in the past.
|
|
133
|
+
if (message.streamId && !state.connections.get(message.connectionId).has(message.streamId))
|
|
134
|
+
return;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case "json-rpc-response": {
|
|
138
|
+
const queue = state.jsonRpcResponses.get(message.chainId);
|
|
139
|
+
// The chain might have been removed locally in the past.
|
|
140
|
+
if (queue)
|
|
141
|
+
queue.push(message.response);
|
|
142
|
+
config.eventCallback({ ty: "json-rpc-responses-non-empty", chainId: message.chainId });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
config.eventCallback(message);
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
addChain(chainSpec, databaseContent, potentialRelayChains, disableJsonRpc) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
const msg = { ty: "add-chain", chainSpec, databaseContent, potentialRelayChains, disableJsonRpc };
|
|
152
|
+
portToServer.postMessage(msg);
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
removeChain(chainId) {
|
|
156
|
+
state.jsonRpcResponses.delete(chainId);
|
|
157
|
+
const msg = { ty: "remove-chain", chainId };
|
|
158
|
+
portToServer.postMessage(msg);
|
|
159
|
+
},
|
|
160
|
+
request(request, chainId) {
|
|
161
|
+
const msg = { ty: "request", chainId, request };
|
|
162
|
+
portToServer.postMessage(msg);
|
|
163
|
+
return 0; // TODO: wrong return value
|
|
164
|
+
},
|
|
165
|
+
peekJsonRpcResponse(chainId) {
|
|
166
|
+
const item = state.jsonRpcResponses.get(chainId).shift();
|
|
167
|
+
if (!item)
|
|
168
|
+
return null;
|
|
169
|
+
const msg = { ty: "accept-more-json-rpc-answers", chainId };
|
|
170
|
+
portToServer.postMessage(msg);
|
|
171
|
+
return item;
|
|
172
|
+
},
|
|
173
|
+
shutdownExecutor() {
|
|
174
|
+
const msg = { ty: "shutdown" };
|
|
175
|
+
portToServer.postMessage(msg);
|
|
176
|
+
},
|
|
177
|
+
connectionReset(connectionId, message) {
|
|
178
|
+
state.connections.delete(connectionId);
|
|
179
|
+
const msg = { ty: "connection-reset", connectionId, message };
|
|
180
|
+
portToServer.postMessage(msg);
|
|
181
|
+
},
|
|
182
|
+
connectionOpened(connectionId, info) {
|
|
183
|
+
const msg = { ty: "connection-opened", connectionId, info };
|
|
184
|
+
portToServer.postMessage(msg);
|
|
185
|
+
},
|
|
186
|
+
streamMessage(connectionId, message, streamId) {
|
|
187
|
+
const msg = { ty: "stream-message", connectionId, message, streamId };
|
|
188
|
+
portToServer.postMessage(msg);
|
|
189
|
+
},
|
|
190
|
+
streamOpened(connectionId, streamId, direction, initialWritableBytes) {
|
|
191
|
+
state.connections.get(connectionId).add(streamId);
|
|
192
|
+
const msg = { ty: "stream-opened", connectionId, streamId, direction, initialWritableBytes };
|
|
193
|
+
portToServer.postMessage(msg);
|
|
194
|
+
},
|
|
195
|
+
streamWritableBytes(connectionId, numExtra, streamId) {
|
|
196
|
+
const msg = { ty: "stream-writable-bytes", connectionId, numExtra, streamId };
|
|
197
|
+
portToServer.postMessage(msg);
|
|
198
|
+
},
|
|
199
|
+
streamReset(connectionId, streamId) {
|
|
200
|
+
state.connections.get(connectionId).delete(streamId);
|
|
201
|
+
const msg = { ty: "stream-reset", connectionId, streamId };
|
|
202
|
+
portToServer.postMessage(msg);
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
exports.connectToInstanceServer = connectToInstanceServer;
|
|
208
|
+
/**
|
|
209
|
+
* Returns a `Promise` that resolves when the instance shuts down. Since the function is also
|
|
210
|
+
* an asynchronous function, the actual return type is `Promise<Promise<void>>`. That is, the
|
|
211
|
+
* outer `Promise` yields once the instance starts, and the inner `Promise` yields once the
|
|
212
|
+
* instance shuts down.
|
|
213
|
+
*/
|
|
214
|
+
function startInstanceServer(config, initPortToClient) {
|
|
215
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
+
const { serverToClient: portToClient, wasmModule, maxLogLevel, cpuRateLimit, forbidTcp, forbidWs, forbidWss, forbidNonLocalWs, forbidWebRtc } = yield new Promise((resolve) => {
|
|
217
|
+
initPortToClient.onmessage = (event) => resolve(event.data);
|
|
218
|
+
});
|
|
219
|
+
initPortToClient.close();
|
|
220
|
+
const state = {
|
|
221
|
+
instance: null,
|
|
222
|
+
connections: new Map(),
|
|
223
|
+
acceptedJsonRpcResponses: new Map(),
|
|
224
|
+
};
|
|
225
|
+
const eventsCallback = (event) => {
|
|
226
|
+
switch (event.ty) {
|
|
227
|
+
case "add-chain-result": {
|
|
228
|
+
if (event.success) {
|
|
229
|
+
state.acceptedJsonRpcResponses.set(event.chainId, 0);
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case "executor-shutdown":
|
|
234
|
+
case "wasm-panic": {
|
|
235
|
+
if (state.onExecutorShutdownOrWasmPanic) {
|
|
236
|
+
const cb = state.onExecutorShutdownOrWasmPanic;
|
|
237
|
+
delete state.onExecutorShutdownOrWasmPanic;
|
|
238
|
+
cb();
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case "json-rpc-responses-non-empty": {
|
|
243
|
+
// Process this event asynchronously because we can't call into `instance`
|
|
244
|
+
// from within the events callback itself.
|
|
245
|
+
// TODO: do better than setTimeout?
|
|
246
|
+
setTimeout(() => {
|
|
247
|
+
const numAccepted = state.acceptedJsonRpcResponses.get(event.chainId);
|
|
248
|
+
if (numAccepted == 0)
|
|
249
|
+
return;
|
|
250
|
+
const response = state.instance.peekJsonRpcResponse(event.chainId);
|
|
251
|
+
if (response) {
|
|
252
|
+
state.acceptedJsonRpcResponses.set(event.chainId, numAccepted - 1);
|
|
253
|
+
const msg = { ty: "json-rpc-response", chainId: event.chainId, response };
|
|
254
|
+
portToClient.postMessage(msg);
|
|
255
|
+
}
|
|
256
|
+
}, 0);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
case "new-connection": {
|
|
260
|
+
state.connections.set(event.connectionId, new Set());
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
case "connection-reset": {
|
|
264
|
+
state.connections.delete(event.connectionId);
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case "connection-stream-reset": {
|
|
268
|
+
state.connections.get(event.connectionId).delete(event.streamId);
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const ev = event;
|
|
273
|
+
portToClient.postMessage(ev);
|
|
274
|
+
};
|
|
275
|
+
// We create the `Promise` ahead of time in order to potentially catch potential `wasm-panic`
|
|
276
|
+
// events as early as during initialization.
|
|
277
|
+
const execFinishedPromise = new Promise((resolve) => state.onExecutorShutdownOrWasmPanic = resolve);
|
|
278
|
+
state.instance = yield instance.startLocalInstance(Object.assign({ forbidTcp,
|
|
279
|
+
forbidWs,
|
|
280
|
+
forbidNonLocalWs,
|
|
281
|
+
forbidWss,
|
|
282
|
+
forbidWebRtc,
|
|
283
|
+
cpuRateLimit,
|
|
284
|
+
maxLogLevel }, config), wasmModule, eventsCallback);
|
|
285
|
+
portToClient.onmessage = (messageEvent) => {
|
|
286
|
+
const message = messageEvent.data;
|
|
287
|
+
switch (message.ty) {
|
|
288
|
+
case "add-chain": {
|
|
289
|
+
state.instance.addChain(message.chainSpec, message.databaseContent, message.potentialRelayChains, message.disableJsonRpc);
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
case "remove-chain": {
|
|
293
|
+
state.instance.removeChain(message.chainId);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case "request": {
|
|
297
|
+
state.instance.request(message.request, message.chainId); // TODO: return value unused
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
case "accept-more-json-rpc-answers": {
|
|
301
|
+
const response = state.instance.peekJsonRpcResponse(message.chainId);
|
|
302
|
+
if (response) {
|
|
303
|
+
const msg = { ty: "json-rpc-response", chainId: message.chainId, response };
|
|
304
|
+
portToClient.postMessage(msg);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
const numAccepted = state.acceptedJsonRpcResponses.get(message.chainId);
|
|
308
|
+
state.acceptedJsonRpcResponses.set(message.chainId, numAccepted + 1);
|
|
309
|
+
}
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
case "shutdown": {
|
|
313
|
+
state.instance.shutdownExecutor();
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
case "connection-reset": {
|
|
317
|
+
// The connection might have been reset locally in the past.
|
|
318
|
+
if (!state.connections.has(message.connectionId))
|
|
319
|
+
return;
|
|
320
|
+
state.instance.connectionReset(message.connectionId, message.message);
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case "connection-opened": {
|
|
324
|
+
// The connection might have been reset locally in the past.
|
|
325
|
+
if (!state.connections.has(message.connectionId))
|
|
326
|
+
return;
|
|
327
|
+
state.instance.connectionOpened(message.connectionId, message.info);
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
case "stream-message": {
|
|
331
|
+
// The connection might have been reset locally in the past.
|
|
332
|
+
if (!state.connections.has(message.connectionId))
|
|
333
|
+
return;
|
|
334
|
+
// The stream might have been reset locally in the past.
|
|
335
|
+
if (message.streamId && !state.connections.get(message.connectionId).has(message.streamId))
|
|
336
|
+
return;
|
|
337
|
+
state.instance.streamMessage(message.connectionId, message.message, message.streamId);
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
case "stream-opened": {
|
|
341
|
+
// The connection might have been reset locally in the past.
|
|
342
|
+
if (!state.connections.has(message.connectionId))
|
|
343
|
+
return;
|
|
344
|
+
state.connections.get(message.connectionId).add(message.streamId);
|
|
345
|
+
state.instance.streamOpened(message.connectionId, message.streamId, message.direction, message.initialWritableBytes);
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case "stream-writable-bytes": {
|
|
349
|
+
// The connection might have been reset locally in the past.
|
|
350
|
+
if (!state.connections.has(message.connectionId))
|
|
351
|
+
return;
|
|
352
|
+
// The stream might have been reset locally in the past.
|
|
353
|
+
if (message.streamId && !state.connections.get(message.connectionId).has(message.streamId))
|
|
354
|
+
return;
|
|
355
|
+
state.instance.streamWritableBytes(message.connectionId, message.numExtra, message.streamId);
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
case "stream-reset": {
|
|
359
|
+
// The connection might have been reset locally in the past.
|
|
360
|
+
if (!state.connections.has(message.connectionId))
|
|
361
|
+
return;
|
|
362
|
+
// The stream might have been reset locally in the past.
|
|
363
|
+
if (message.streamId && !state.connections.get(message.connectionId).has(message.streamId))
|
|
364
|
+
return;
|
|
365
|
+
state.connections.get(message.connectionId).delete(message.streamId);
|
|
366
|
+
state.instance.streamReset(message.connectionId, message.streamId);
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
return execFinishedPromise;
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
exports.startInstanceServer = startInstanceServer;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { Client, ClientOptionsWithBytecode } from './public-types.js';
|
|
3
|
+
export { AddChainError, AddChainOptions, AlreadyDestroyedError, Chain, Client, ClientOptions, ClientOptionsWithBytecode, SmoldotBytecode, CrashError, JsonRpcDisabledError, MalformedJsonRpcError, QueueFullError, LogCallback } from './public-types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Initializes a new client. This is a pre-requisite to connecting to a blockchain.
|
|
6
|
+
*
|
|
7
|
+
* Can never fail.
|
|
8
|
+
*
|
|
9
|
+
* @param options Configuration of the client.
|
|
10
|
+
*/
|
|
11
|
+
export declare function startWithBytecode(options: ClientOptionsWithBytecode): Client;
|