smoldot 0.3.6 → 0.7.11
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 +18 -21
- package/dist/cjs/base64.d.ts +22 -0
- package/dist/cjs/base64.js +104 -0
- package/dist/cjs/client.d.ts +287 -0
- package/dist/cjs/client.js +175 -0
- package/dist/cjs/index-browser.d.ts +11 -0
- package/dist/cjs/index-browser.js +465 -0
- package/dist/cjs/index-deno.d.ts +10 -0
- package/dist/cjs/index-deno.js +263 -0
- package/dist/cjs/index-nodejs.d.ts +10 -0
- package/dist/cjs/index-nodejs.js +174 -0
- package/dist/cjs/instance/autogen/wasm.d.ts +2 -0
- package/dist/cjs/instance/autogen/wasm.js +6 -0
- package/dist/cjs/instance/autogen/wasm0.d.ts +1 -0
- package/dist/cjs/instance/autogen/wasm0.js +4 -0
- package/dist/cjs/instance/autogen/wasm1.d.ts +1 -0
- package/dist/cjs/instance/autogen/wasm1.js +4 -0
- package/dist/cjs/instance/autogen/wasm2.d.ts +1 -0
- package/dist/cjs/instance/autogen/wasm2.js +4 -0
- package/dist/cjs/instance/bindings-smoldot-light.d.ts +154 -0
- package/dist/cjs/instance/bindings-smoldot-light.js +266 -0
- package/dist/cjs/instance/bindings-wasi.d.ts +23 -0
- package/dist/cjs/instance/bindings-wasi.js +142 -0
- package/dist/cjs/instance/bindings.d.ts +31 -0
- package/dist/cjs/instance/bindings.js +5 -0
- package/dist/cjs/instance/buffer.d.ts +10 -0
- package/dist/cjs/instance/buffer.js +53 -0
- package/dist/cjs/instance/instance.d.ts +45 -0
- package/dist/cjs/instance/instance.js +301 -0
- package/dist/cjs/instance/raw-instance.d.ts +65 -0
- package/dist/cjs/instance/raw-instance.js +72 -0
- package/dist/cjs/package.json +3 -0
- package/dist/mjs/base64.d.ts +22 -0
- package/dist/mjs/base64.js +97 -0
- package/dist/mjs/client.d.ts +287 -0
- package/dist/mjs/client.js +165 -0
- package/dist/mjs/index-browser.d.ts +11 -0
- package/dist/mjs/index-browser.js +455 -0
- package/dist/mjs/index-deno.d.ts +10 -0
- package/dist/mjs/index-deno.js +253 -0
- package/dist/mjs/index-nodejs.d.ts +10 -0
- package/dist/mjs/index-nodejs.js +164 -0
- package/dist/mjs/instance/autogen/wasm.d.ts +2 -0
- package/dist/mjs/instance/autogen/wasm.js +4 -0
- package/dist/mjs/instance/autogen/wasm0.d.ts +1 -0
- package/dist/mjs/instance/autogen/wasm0.js +1 -0
- package/dist/mjs/instance/autogen/wasm1.d.ts +1 -0
- package/dist/mjs/instance/autogen/wasm1.js +1 -0
- package/dist/mjs/instance/autogen/wasm2.d.ts +1 -0
- package/dist/mjs/instance/autogen/wasm2.js +1 -0
- package/dist/mjs/instance/bindings-smoldot-light.d.ts +154 -0
- package/dist/mjs/instance/bindings-smoldot-light.js +261 -0
- package/dist/mjs/instance/bindings-wasi.d.ts +23 -0
- package/{src → dist/mjs/instance}/bindings-wasi.js +47 -58
- package/dist/mjs/instance/bindings.d.ts +31 -0
- package/dist/mjs/instance/bindings.js +4 -0
- package/dist/mjs/instance/buffer.d.ts +10 -0
- package/dist/mjs/instance/buffer.js +46 -0
- package/dist/mjs/instance/instance.d.ts +45 -0
- package/dist/mjs/instance/instance.js +293 -0
- package/dist/mjs/instance/raw-instance.d.ts +65 -0
- package/dist/mjs/instance/raw-instance.js +67 -0
- package/dist/mjs/package.json +3 -0
- package/package.json +35 -19
- package/src/autogen/wasm.js +0 -1
- package/src/bindings-smoldot-js.js +0 -223
- package/src/compat-browser.js +0 -25
- package/src/compat-nodejs.js +0 -30
- package/src/health.js +0 -206
- package/src/index.d.ts +0 -68
- package/src/index.js +0 -192
- package/src/index.test-d.ts +0 -36
- package/src/tsconfig.json +0 -13
- package/src/worker.js +0 -151
package/src/index.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
// Smoldot
|
|
2
|
-
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
|
|
3
|
-
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
4
|
-
|
|
5
|
-
// This program is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License as published by
|
|
7
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
-
// (at your option) any later version.
|
|
9
|
-
|
|
10
|
-
// This program is distributed in the hope that it will be useful,
|
|
11
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
-
// GNU General Public License for more details.
|
|
14
|
-
|
|
15
|
-
// You should have received a copy of the GNU General Public License
|
|
16
|
-
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
-
|
|
18
|
-
import { Worker, workerOnError, workerOnMessage } from './compat-nodejs.js';
|
|
19
|
-
export * from './health.js';
|
|
20
|
-
|
|
21
|
-
export class SmoldotError extends Error {
|
|
22
|
-
constructor(message) {
|
|
23
|
-
super(message);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export async function start(config) {
|
|
28
|
-
config = config || {};
|
|
29
|
-
|
|
30
|
-
const logCallback = config.logCallback || ((level, target, message) => {
|
|
31
|
-
if (level <= 1) {
|
|
32
|
-
console.error("[" + target + "]", message);
|
|
33
|
-
} else if (level == 2) {
|
|
34
|
-
console.warn("[" + target + "]", message);
|
|
35
|
-
} else if (level == 3) {
|
|
36
|
-
console.info("[" + target + "]", message);
|
|
37
|
-
} else if (level == 4) {
|
|
38
|
-
console.debug("[" + target + "]", message);
|
|
39
|
-
} else {
|
|
40
|
-
console.trace("[" + target + "]", message);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// The actual execution of Smoldot is performed in a worker thread.
|
|
45
|
-
//
|
|
46
|
-
// The line of code below (`new Worker(...)`) is designed to hopefully work across all
|
|
47
|
-
// platforms. It should work in NodeJS, browsers, webpack
|
|
48
|
-
// (https://webpack.js.org/guides/web-workers/), and parcel
|
|
49
|
-
// (https://github.com/parcel-bundler/parcel/pull/5846)
|
|
50
|
-
const worker = new Worker(new URL('./worker.js', import.meta.url));
|
|
51
|
-
let workerError = null;
|
|
52
|
-
|
|
53
|
-
// Whenever an `addChain` or `removeChain` message is sent to the worker, a corresponding entry
|
|
54
|
-
// is pushed to this array. The worker needs to send back a confirmation, which pops the first
|
|
55
|
-
// element of this array. In the case of `addChain`, additional fields are stored in this array
|
|
56
|
-
// to finish the initialization of the chain.
|
|
57
|
-
let pendingConfirmations = [];
|
|
58
|
-
|
|
59
|
-
// For each chain that is currently running, contains the callback to use to send back JSON-RPC
|
|
60
|
-
// responses corresponding to this chain.
|
|
61
|
-
// Entries are instantly removed when the user desires to remove a chain even before the worker
|
|
62
|
-
// has confirmed the removal. Doing so avoids a race condition where the worker sends back a
|
|
63
|
-
// JSON-RPC response even though we've already sent a `removeChain` message to it.
|
|
64
|
-
let chainsJsonRpcCallbacks = {};
|
|
65
|
-
|
|
66
|
-
// The worker can send us messages whose type is identified through a `kind` field.
|
|
67
|
-
workerOnMessage(worker, (message) => {
|
|
68
|
-
if (message.kind == 'jsonrpc') {
|
|
69
|
-
const cb = chainsJsonRpcCallbacks[message.chainId];
|
|
70
|
-
if (cb) cb(message.data);
|
|
71
|
-
|
|
72
|
-
} else if (message.kind == 'chainAddedOk') {
|
|
73
|
-
const expected = pendingConfirmations.pop();
|
|
74
|
-
const chainId = message.chainId;
|
|
75
|
-
|
|
76
|
-
if (!!chainsJsonRpcCallbacks[chainId]) // Sanity check.
|
|
77
|
-
throw 'Unexpected reuse of a chain ID';
|
|
78
|
-
chainsJsonRpcCallbacks[chainId] = expected.jsonRpcCallback;
|
|
79
|
-
|
|
80
|
-
// `expected` was pushed by the `addChain` method.
|
|
81
|
-
// Resolve the promise that `addChain` returned to the user.
|
|
82
|
-
expected.resolve({
|
|
83
|
-
sendJsonRpc: (request) => {
|
|
84
|
-
if (workerError)
|
|
85
|
-
throw workerError;
|
|
86
|
-
if (!chainsJsonRpcCallbacks[message.chainId])
|
|
87
|
-
throw new SmoldotError('Chain isn\'t capable of serving JSON-RPC requests');
|
|
88
|
-
worker.postMessage({ ty: 'request', request, chainId });
|
|
89
|
-
},
|
|
90
|
-
remove: () => {
|
|
91
|
-
if (workerError)
|
|
92
|
-
throw workerError;
|
|
93
|
-
pendingConfirmations.push({ ty: 'chainRemoved', chainId });
|
|
94
|
-
worker.postMessage({ ty: 'removeChain', chainId });
|
|
95
|
-
// Because the `removeChain` message is asynchronous, it is possible for a JSON-RPC
|
|
96
|
-
// response concerning that `chainId` to arrive after the `remove` function has
|
|
97
|
-
// returned. We solve that by removing the callback immediately.
|
|
98
|
-
delete chainsJsonRpcCallbacks[message.chainId];
|
|
99
|
-
},
|
|
100
|
-
// Hacky internal method that later lets us access the `chainId` of this chain for
|
|
101
|
-
// implementation reasons.
|
|
102
|
-
__internal_smoldot_id: () => chainId,
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
} else if (message.kind == 'chainAddedErr') {
|
|
106
|
-
const expected = pendingConfirmations.pop();
|
|
107
|
-
// `expected` was pushed by the `addChain` method.
|
|
108
|
-
// Reject the promise that `addChain` returned to the user.
|
|
109
|
-
expected.reject(message.error);
|
|
110
|
-
|
|
111
|
-
} else if (message.kind == 'chainRemoved') {
|
|
112
|
-
pendingConfirmations.pop();
|
|
113
|
-
|
|
114
|
-
} else if (message.kind == 'log') {
|
|
115
|
-
logCallback(message.level, message.target, message.message);
|
|
116
|
-
|
|
117
|
-
} else {
|
|
118
|
-
console.error('Unknown message type', message);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
workerOnError(worker, (error) => {
|
|
123
|
-
// A worker error should only happen in case of a critical error as the result of a bug
|
|
124
|
-
// somewhere. Consequently, nothing is really in place to cleanly report the error.
|
|
125
|
-
console.error(error);
|
|
126
|
-
workerError = error;
|
|
127
|
-
|
|
128
|
-
// Reject all promises returned by `addChain`.
|
|
129
|
-
for (var pending of pendingConfirmations) {
|
|
130
|
-
if (pending.ty == 'chainAdded')
|
|
131
|
-
pending.reject(error);
|
|
132
|
-
}
|
|
133
|
-
pendingConfirmations = [];
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
// The first message expected by the worker contains the configuration.
|
|
137
|
-
worker.postMessage({
|
|
138
|
-
// Maximum level of log entries sent by the client.
|
|
139
|
-
// 0 = Logging disabled, 1 = Error, 2 = Warn, 3 = Info, 4 = Debug, 5 = Trace
|
|
140
|
-
maxLogLevel: config.maxLogLevel || 3,
|
|
141
|
-
forbidTcp: config.forbidTcp,
|
|
142
|
-
forbidWs: config.forbidWs,
|
|
143
|
-
forbidWss: config.forbidWss,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
return {
|
|
147
|
-
addChain: (options) => {
|
|
148
|
-
if (workerError)
|
|
149
|
-
throw workerError;
|
|
150
|
-
|
|
151
|
-
let potentialRelayChainsIds = [];
|
|
152
|
-
if (!!options.potentialRelayChains) {
|
|
153
|
-
for (var chain of options.potentialRelayChains) {
|
|
154
|
-
// The content of `options.potentialRelayChains` are supposed to be chains earlier
|
|
155
|
-
// returned by `addChain`. The hacky `__internal_smoldot_id` method lets us obtain the
|
|
156
|
-
// internal ID of these chains.
|
|
157
|
-
const id = chain.__internal_smoldot_id();
|
|
158
|
-
potentialRelayChainsIds.push(id);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Build a promise that will be resolved or rejected after the chain has been added.
|
|
163
|
-
let chainAddedPromiseResolve;
|
|
164
|
-
let chainAddedPromiseReject;
|
|
165
|
-
const chainAddedPromise = new Promise((resolve, reject) => {
|
|
166
|
-
chainAddedPromiseResolve = resolve;
|
|
167
|
-
chainAddedPromiseReject = reject;
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
pendingConfirmations.push({
|
|
171
|
-
ty: 'chainAdded',
|
|
172
|
-
reject: chainAddedPromiseReject,
|
|
173
|
-
resolve: chainAddedPromiseResolve,
|
|
174
|
-
jsonRpcCallback: options.jsonRpcCallback,
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
worker.postMessage({
|
|
178
|
-
ty: 'addChain',
|
|
179
|
-
chainSpec: options.chainSpec,
|
|
180
|
-
potentialRelayChains: potentialRelayChainsIds,
|
|
181
|
-
jsonRpcRunning: !!options.jsonRpcCallback,
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
return chainAddedPromise;
|
|
185
|
-
},
|
|
186
|
-
terminate: () => {
|
|
187
|
-
worker.terminate();
|
|
188
|
-
if (!workerError)
|
|
189
|
-
workerError = new Error("terminate() has been called");
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
package/src/index.test-d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import smoldot, { Smoldot, SmoldotClient } from 'smoldot';
|
|
2
|
-
|
|
3
|
-
// Test the export type
|
|
4
|
-
|
|
5
|
-
// smoldot; // $ExpectType Smoldot
|
|
6
|
-
|
|
7
|
-
// Test when supplying all options and all params to logCallback
|
|
8
|
-
|
|
9
|
-
// $ExpectType Promise<SmoldotClient>
|
|
10
|
-
let sp = smoldot.start({
|
|
11
|
-
maxLogLevel: 3,
|
|
12
|
-
logCallback: (level, target, message) => { },
|
|
13
|
-
forbidTcp: false,
|
|
14
|
-
forbidWs: false,
|
|
15
|
-
forbidWss: false,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
// Test when not supplying optional options and optional params
|
|
19
|
-
|
|
20
|
-
// $ExpectType Promise<SmoldotClient>
|
|
21
|
-
sp = smoldot.start();
|
|
22
|
-
|
|
23
|
-
sp.then(async (sm) => {
|
|
24
|
-
// $ExpectType Promise<SmoldotChain>
|
|
25
|
-
const chain1 = sm.addChain({ chainSpec: '' });
|
|
26
|
-
// $ExpectType Promise<SmoldotChain>
|
|
27
|
-
const chain2Promise = sm.addChain({ chainSpec: '', potentialRelayChains: [await chain1], jsonRpcCallback: (resp) => { } });
|
|
28
|
-
// $ExpectType SmoldotChain
|
|
29
|
-
const chain2 = await chain2Promise;
|
|
30
|
-
// $ExpectType void
|
|
31
|
-
chain2.sendJsonRpc('{"id":8,"jsonrpc":"2.0","method":"system_health","params":[]}');
|
|
32
|
-
// $ExpectType void
|
|
33
|
-
chain2.remove();
|
|
34
|
-
// $ExpectType void
|
|
35
|
-
sm.terminate();
|
|
36
|
-
});
|
package/src/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "commonjs",
|
|
4
|
-
"lib": ["es6"],
|
|
5
|
-
"noImplicitAny": true,
|
|
6
|
-
"noImplicitThis": true,
|
|
7
|
-
"strictNullChecks": true,
|
|
8
|
-
"strictFunctionTypes": true,
|
|
9
|
-
"noEmit": true,
|
|
10
|
-
"baseUrl": "./",
|
|
11
|
-
"paths": { "smoldot": ["./"] }
|
|
12
|
-
}
|
|
13
|
-
}
|
package/src/worker.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
// Smoldot
|
|
2
|
-
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
|
|
3
|
-
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
4
|
-
|
|
5
|
-
// This program is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License as published by
|
|
7
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
-
// (at your option) any later version.
|
|
9
|
-
|
|
10
|
-
// This program is distributed in the hope that it will be useful,
|
|
11
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
-
// GNU General Public License for more details.
|
|
14
|
-
|
|
15
|
-
// You should have received a copy of the GNU General Public License
|
|
16
|
-
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
-
|
|
18
|
-
import { Buffer } from 'buffer';
|
|
19
|
-
import * as compat from './compat-nodejs.js';
|
|
20
|
-
import { default as smoldot_js_builder } from './bindings-smoldot-js.js';
|
|
21
|
-
import { default as wasi_builder } from './bindings-wasi.js';
|
|
22
|
-
|
|
23
|
-
import { default as wasm_base64 } from './autogen/wasm.js';
|
|
24
|
-
|
|
25
|
-
// This variable represents the state of the worker, and serves three different purposes:
|
|
26
|
-
//
|
|
27
|
-
// - At initialization, it is set to `null`.
|
|
28
|
-
// - Once the first message, containing the configuration, has been received from the parent, it
|
|
29
|
-
// becomes an array filled with JSON-RPC requests that are received while the Wasm VM is still
|
|
30
|
-
// initializing.
|
|
31
|
-
// - After the Wasm VM has finished initialization, contains the `WebAssembly.Instance` object.
|
|
32
|
-
//
|
|
33
|
-
let state = null;
|
|
34
|
-
|
|
35
|
-
// Inject a message coming from `index.js` to a running Wasm VM.
|
|
36
|
-
const injectMessage = (instance, message) => {
|
|
37
|
-
if (message.ty == 'request') {
|
|
38
|
-
const len = Buffer.byteLength(message.request, 'utf8');
|
|
39
|
-
const ptr = instance.exports.alloc(len);
|
|
40
|
-
Buffer.from(instance.exports.memory.buffer).write(message.request, ptr);
|
|
41
|
-
instance.exports.json_rpc_send(ptr, len, message.chainId);
|
|
42
|
-
|
|
43
|
-
} else if (message.ty == 'addChain') {
|
|
44
|
-
// Write the chain specification into memory.
|
|
45
|
-
const chainSpecLen = Buffer.byteLength(message.chainSpec, 'utf8');
|
|
46
|
-
const chainSpecPtr = instance.exports.alloc(chainSpecLen);
|
|
47
|
-
Buffer.from(instance.exports.memory.buffer)
|
|
48
|
-
.write(message.chainSpec, chainSpecPtr);
|
|
49
|
-
|
|
50
|
-
// Write the potential relay chains into memory.
|
|
51
|
-
const potentialRelayChainsLen = message.potentialRelayChains.length;
|
|
52
|
-
const potentialRelayChainsPtr = instance.exports.alloc(potentialRelayChainsLen * 4);
|
|
53
|
-
for (let idx in message.potentialRelayChains) {
|
|
54
|
-
Buffer.from(instance.exports.memory.buffer)
|
|
55
|
-
.writeUInt32LE(message.potentialRelayChains[idx], potentialRelayChainsPtr + idx * 4);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const result = instance.exports.add_chain(
|
|
59
|
-
chainSpecPtr, chainSpecLen,
|
|
60
|
-
message.jsonRpcRunning,
|
|
61
|
-
potentialRelayChainsPtr, potentialRelayChainsLen
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
if (result >= 0x80000000 || result < 0) { // TODO: really crappy error code handling
|
|
65
|
-
// TODO: better error message
|
|
66
|
-
// Note that `add_chain` properly de-allocates buffers even if it failed.
|
|
67
|
-
compat.postMessage({ kind: 'chainAddedErr', error: new Error('Failed to initialize chain') });
|
|
68
|
-
} else {
|
|
69
|
-
compat.postMessage({ kind: 'chainAddedOk', chainId: result });
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
} else if (message.ty == 'removeChain') {
|
|
73
|
-
instance.exports.remove_chain(message.chainId);
|
|
74
|
-
// `compat.postMessage` is the same as `postMessage`, but works across environments.
|
|
75
|
-
compat.postMessage({ kind: 'chainRemoved' });
|
|
76
|
-
|
|
77
|
-
} else
|
|
78
|
-
throw new Error('unrecognized message type');
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const startInstance = async (config) => {
|
|
82
|
-
// The actual Wasm bytecode is base64-decoded from a constant found in a different file.
|
|
83
|
-
// This is suboptimal compared to using `instantiateStreaming`, but it is the most
|
|
84
|
-
// cross-platform cross-bundler approach.
|
|
85
|
-
const wasmBytecode = new Uint8Array(Buffer.from(wasm_base64, 'base64'));
|
|
86
|
-
|
|
87
|
-
// Used to bind with the smoldot-js bindings. See the `bindings-smoldot-js.js` file.
|
|
88
|
-
const smoldotJsConfig = {
|
|
89
|
-
logCallback: (level, target, message) => {
|
|
90
|
-
// `compat.postMessage` is the same as `postMessage`, but works across environments.
|
|
91
|
-
compat.postMessage({ kind: 'log', level, target, message });
|
|
92
|
-
},
|
|
93
|
-
jsonRpcCallback: (data, chainId) => {
|
|
94
|
-
// `compat.postMessage` is the same as `postMessage`, but works across environments.
|
|
95
|
-
compat.postMessage({ kind: 'jsonrpc', data, chainId });
|
|
96
|
-
},
|
|
97
|
-
forbidTcp: config.forbidTcp,
|
|
98
|
-
forbidWs: config.forbidWs,
|
|
99
|
-
forbidWss: config.forbidWss,
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const { bindings: smoldotJsBindings } = smoldot_js_builder(smoldotJsConfig);
|
|
103
|
-
|
|
104
|
-
// Used to bind with the Wasi bindings. See the `bindings-wasi.js` file.
|
|
105
|
-
const wasiConfig = {};
|
|
106
|
-
|
|
107
|
-
// Start the Wasm virtual machine.
|
|
108
|
-
// The Rust code defines a list of imports that must be fulfilled by the environment. The second
|
|
109
|
-
// parameter provides their implementations.
|
|
110
|
-
const result = await WebAssembly.instantiate(wasmBytecode, {
|
|
111
|
-
// The functions with the "smoldot" prefix are specific to smoldot.
|
|
112
|
-
"smoldot": smoldotJsBindings,
|
|
113
|
-
// As the Rust code is compiled for wasi, some more wasi-specific imports exist.
|
|
114
|
-
"wasi_snapshot_preview1": wasi_builder(wasiConfig),
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
smoldotJsConfig.instance = result.instance;
|
|
118
|
-
wasiConfig.instance = result.instance;
|
|
119
|
-
|
|
120
|
-
// Start initialization of smoldot.
|
|
121
|
-
result.instance.exports.init(config.maxLogLevel);
|
|
122
|
-
|
|
123
|
-
// Smoldot has finished initializing.
|
|
124
|
-
// Since this function is an asynchronous function, it is possible that messages have been
|
|
125
|
-
// received from the parent while it was executing. These messages are now handled.
|
|
126
|
-
state.forEach((message) => {
|
|
127
|
-
injectMessage(result.instance, message);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
state = result.instance;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
// `compat.setOnMessage` is the same as `onmessage = ...`, but works across environments.
|
|
134
|
-
compat.setOnMessage((message) => {
|
|
135
|
-
// What to do depends on the type of `state`.
|
|
136
|
-
// See the documentation of the `state` variable for information.
|
|
137
|
-
if (state == null) {
|
|
138
|
-
// First ever message received by the worker. Always contains the initial configuration.
|
|
139
|
-
state = [];
|
|
140
|
-
startInstance(message) // Note that `startInstance` is `async`.
|
|
141
|
-
|
|
142
|
-
} else if (Array.isArray(state)) {
|
|
143
|
-
// A JSON-RPC request has been received while the Wasm VM is still initializing. Queue it
|
|
144
|
-
// for when initialization is over.
|
|
145
|
-
state.push(message);
|
|
146
|
-
|
|
147
|
-
} else {
|
|
148
|
-
// Everything is already initialized. Process the message synchronously.
|
|
149
|
-
injectMessage(state, message);
|
|
150
|
-
}
|
|
151
|
-
});
|