smoldot 1.0.0 → 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/dist/cjs/index-browser.js +66 -19
- package/dist/cjs/index-deno.js +79 -56
- package/dist/cjs/index-nodejs.js +91 -40
- package/dist/cjs/instance/autogen/wasm0.js +1 -1
- package/dist/cjs/instance/autogen/wasm1.js +1 -1
- package/dist/cjs/instance/autogen/wasm2.js +1 -1
- package/dist/cjs/instance/bindings-smoldot-light.d.ts +42 -2
- package/dist/cjs/instance/bindings-smoldot-light.js +46 -26
- package/dist/cjs/instance/bindings.d.ts +8 -8
- package/dist/cjs/instance/instance.js +19 -25
- package/dist/cjs/instance/raw-instance.d.ts +1 -1
- package/dist/cjs/instance/raw-instance.js +3 -2
- package/dist/mjs/index-browser.js +66 -19
- package/dist/mjs/index-deno.js +79 -56
- package/dist/mjs/index-nodejs.js +91 -40
- package/dist/mjs/instance/autogen/wasm0.js +1 -1
- package/dist/mjs/instance/autogen/wasm1.js +1 -1
- package/dist/mjs/instance/autogen/wasm2.js +1 -1
- package/dist/mjs/instance/bindings-smoldot-light.d.ts +42 -2
- package/dist/mjs/instance/bindings-smoldot-light.js +46 -26
- package/dist/mjs/instance/bindings.d.ts +8 -8
- package/dist/mjs/instance/instance.js +19 -25
- package/dist/mjs/instance/raw-instance.d.ts +1 -1
- package/dist/mjs/instance/raw-instance.js +3 -2
- package/package.json +3 -3
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { SmoldotWasmInstance } from './bindings.js';
|
|
2
2
|
export interface Config {
|
|
3
3
|
instance?: SmoldotWasmInstance;
|
|
4
|
+
/**
|
|
5
|
+
* Array used to store the buffers provided to the Rust code.
|
|
6
|
+
*
|
|
7
|
+
* When `buffer_size` or `buffer_index` are called, the buffer is found here.
|
|
8
|
+
*/
|
|
9
|
+
bufferIndices: Array<Uint8Array>;
|
|
4
10
|
/**
|
|
5
11
|
* Returns the number of milliseconds since an arbitrary epoch.
|
|
6
12
|
*/
|
|
@@ -63,12 +69,33 @@ export interface Connection {
|
|
|
63
69
|
/**
|
|
64
70
|
* Queues data to be sent on the given connection.
|
|
65
71
|
*
|
|
66
|
-
* The connection must currently be in the `Open` state.
|
|
72
|
+
* The connection and stream must currently be in the `Open` state.
|
|
73
|
+
*
|
|
74
|
+
* The number of bytes most never exceed the number of "writable bytes" of the stream.
|
|
75
|
+
* `onWritableBytes` can be used in order to notify that more writable bytes are available.
|
|
67
76
|
*
|
|
68
77
|
* The `streamId` must be provided if and only if the connection is of type "multi-stream".
|
|
69
78
|
* It indicates which substream to send the data on.
|
|
79
|
+
*
|
|
80
|
+
* Must not be called after `closeSend` has been called.
|
|
70
81
|
*/
|
|
71
82
|
send(data: Uint8Array, streamId?: number): void;
|
|
83
|
+
/**
|
|
84
|
+
* Closes the writing side of the given stream of the given connection.
|
|
85
|
+
*
|
|
86
|
+
* Never called for connection types where this isn't possible to implement (i.e. WebSocket
|
|
87
|
+
* and WebRTC at the moment).
|
|
88
|
+
*
|
|
89
|
+
* The connection and stream must currently be in the `Open` state.
|
|
90
|
+
*
|
|
91
|
+
* Implicitly sets the "writable bytes" of the stream to zero.
|
|
92
|
+
*
|
|
93
|
+
* The `streamId` must be provided if and only if the connection is of type "multi-stream".
|
|
94
|
+
* It indicates which substream to send the data on.
|
|
95
|
+
*
|
|
96
|
+
* Must only be called once per stream.
|
|
97
|
+
*/
|
|
98
|
+
closeSend(streamId?: number): void;
|
|
72
99
|
/**
|
|
73
100
|
* Start opening an additional outbound substream on the given connection.
|
|
74
101
|
*
|
|
@@ -104,6 +131,8 @@ export interface ConnectionConfig {
|
|
|
104
131
|
onOpen: (info: {
|
|
105
132
|
type: 'single-stream';
|
|
106
133
|
handshake: 'multistream-select-noise-yamux';
|
|
134
|
+
initialWritableBytes: number;
|
|
135
|
+
writeClosable: boolean;
|
|
107
136
|
} | {
|
|
108
137
|
type: 'multi-stream';
|
|
109
138
|
handshake: 'webrtc';
|
|
@@ -121,7 +150,7 @@ export interface ConnectionConfig {
|
|
|
121
150
|
*
|
|
122
151
|
* This function must only be called for connections of type "multi-stream".
|
|
123
152
|
*/
|
|
124
|
-
onStreamOpened: (streamId: number, direction: 'inbound' | 'outbound') => void;
|
|
153
|
+
onStreamOpened: (streamId: number, direction: 'inbound' | 'outbound', initialWritableBytes: number) => void;
|
|
125
154
|
/**
|
|
126
155
|
* Callback called when a stream transitions to the `Reset` state.
|
|
127
156
|
*
|
|
@@ -130,6 +159,17 @@ export interface ConnectionConfig {
|
|
|
130
159
|
* This function must only be called for connections of type "multi-stream".
|
|
131
160
|
*/
|
|
132
161
|
onStreamReset: (streamId: number) => void;
|
|
162
|
+
/**
|
|
163
|
+
* Callback called when more data can be written on the stream.
|
|
164
|
+
*
|
|
165
|
+
* Can only happen while the connection is in the `Open` state.
|
|
166
|
+
*
|
|
167
|
+
* This callback must not be called after `closeSend` has been called.
|
|
168
|
+
*
|
|
169
|
+
* The `streamId` parameter must be provided if and only if the connection is of type
|
|
170
|
+
* "multi-stream".
|
|
171
|
+
*/
|
|
172
|
+
onWritableBytes: (numExtra: number, streamId?: number) => void;
|
|
133
173
|
/**
|
|
134
174
|
* Callback called when a message sent by the remote has been received.
|
|
135
175
|
*
|
|
@@ -55,6 +55,16 @@ function default_1(config) {
|
|
|
55
55
|
const message = buffer.utf8BytesToString(new Uint8Array(instance.exports.memory.buffer), ptr, len);
|
|
56
56
|
config.onPanic(message);
|
|
57
57
|
},
|
|
58
|
+
buffer_size: (bufferIndex) => {
|
|
59
|
+
const buf = config.bufferIndices[bufferIndex];
|
|
60
|
+
return buf.byteLength;
|
|
61
|
+
},
|
|
62
|
+
buffer_copy: (bufferIndex, targetPtr) => {
|
|
63
|
+
const instance = config.instance;
|
|
64
|
+
targetPtr = targetPtr >>> 0;
|
|
65
|
+
const buf = config.bufferIndices[bufferIndex];
|
|
66
|
+
new Uint8Array(instance.exports.memory.buffer).set(buf, targetPtr);
|
|
67
|
+
},
|
|
58
68
|
// Used by the Rust side to notify that a JSON-RPC response or subscription notification
|
|
59
69
|
// is available in the queue of JSON-RPC responses.
|
|
60
70
|
json_rpc_responses_non_empty: (chainId) => {
|
|
@@ -97,7 +107,7 @@ function default_1(config) {
|
|
|
97
107
|
// In browsers, `setTimeout` works as expected when `ms` equals 0. However, NodeJS
|
|
98
108
|
// requires a minimum of 1 millisecond (if `0` is passed, it is automatically replaced
|
|
99
109
|
// with `1`) and wants you to use `setImmediate` instead.
|
|
100
|
-
if (ms
|
|
110
|
+
if (ms < 1 && typeof setImmediate === "function") {
|
|
101
111
|
setImmediate(() => {
|
|
102
112
|
if (killedTracked.killed)
|
|
103
113
|
return;
|
|
@@ -120,11 +130,11 @@ function default_1(config) {
|
|
|
120
130
|
},
|
|
121
131
|
// Must create a new connection object. This implementation stores the created object in
|
|
122
132
|
// `connections`.
|
|
123
|
-
connection_new: (connectionId, addrPtr, addrLen,
|
|
133
|
+
connection_new: (connectionId, addrPtr, addrLen, errorBufferIndexPtr) => {
|
|
124
134
|
const instance = config.instance;
|
|
125
135
|
addrPtr >>>= 0;
|
|
126
136
|
addrLen >>>= 0;
|
|
127
|
-
|
|
137
|
+
errorBufferIndexPtr >>>= 0;
|
|
128
138
|
if (!!connections[connectionId]) {
|
|
129
139
|
throw new Error("internal error: connection already allocated");
|
|
130
140
|
}
|
|
@@ -140,17 +150,17 @@ function default_1(config) {
|
|
|
140
150
|
try {
|
|
141
151
|
switch (info.type) {
|
|
142
152
|
case 'single-stream': {
|
|
143
|
-
instance.exports.connection_open_single_stream(connectionId, 0);
|
|
153
|
+
instance.exports.connection_open_single_stream(connectionId, 0, info.initialWritableBytes, info.writeClosable ? 1 : 0);
|
|
144
154
|
break;
|
|
145
155
|
}
|
|
146
156
|
case 'multi-stream': {
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
const handshakeTy = new Uint8Array(1 + info.localTlsCertificateMultihash.length + info.remoteTlsCertificateMultihash.length);
|
|
158
|
+
buffer.writeUInt8(handshakeTy, 0, 0);
|
|
159
|
+
handshakeTy.set(info.localTlsCertificateMultihash, 1);
|
|
160
|
+
handshakeTy.set(info.remoteTlsCertificateMultihash, 1 + info.localTlsCertificateMultihash.length);
|
|
161
|
+
config.bufferIndices[0] = handshakeTy;
|
|
162
|
+
instance.exports.connection_open_multi_stream(connectionId, 0);
|
|
163
|
+
delete config.bufferIndices[0];
|
|
154
164
|
break;
|
|
155
165
|
}
|
|
156
166
|
}
|
|
@@ -161,10 +171,17 @@ function default_1(config) {
|
|
|
161
171
|
if (killedTracked.killed)
|
|
162
172
|
return;
|
|
163
173
|
try {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
174
|
+
config.bufferIndices[0] = new TextEncoder().encode(message);
|
|
175
|
+
instance.exports.connection_reset(connectionId, 0);
|
|
176
|
+
delete config.bufferIndices[0];
|
|
177
|
+
}
|
|
178
|
+
catch (_error) { }
|
|
179
|
+
},
|
|
180
|
+
onWritableBytes: (numExtra, streamId) => {
|
|
181
|
+
if (killedTracked.killed)
|
|
182
|
+
return;
|
|
183
|
+
try {
|
|
184
|
+
instance.exports.stream_writable_bytes(connectionId, streamId || 0, numExtra);
|
|
168
185
|
}
|
|
169
186
|
catch (_error) { }
|
|
170
187
|
},
|
|
@@ -172,17 +189,17 @@ function default_1(config) {
|
|
|
172
189
|
if (killedTracked.killed)
|
|
173
190
|
return;
|
|
174
191
|
try {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
192
|
+
config.bufferIndices[0] = message;
|
|
193
|
+
instance.exports.stream_message(connectionId, streamId || 0, 0);
|
|
194
|
+
delete config.bufferIndices[0];
|
|
178
195
|
}
|
|
179
196
|
catch (_error) { }
|
|
180
197
|
},
|
|
181
|
-
onStreamOpened: (streamId, direction) => {
|
|
198
|
+
onStreamOpened: (streamId, direction, initialWritableBytes) => {
|
|
182
199
|
if (killedTracked.killed)
|
|
183
200
|
return;
|
|
184
201
|
try {
|
|
185
|
-
instance.exports.connection_stream_opened(connectionId, streamId, direction === 'outbound' ? 1 : 0);
|
|
202
|
+
instance.exports.connection_stream_opened(connectionId, streamId, direction === 'outbound' ? 1 : 0, initialWritableBytes);
|
|
186
203
|
}
|
|
187
204
|
catch (_error) { }
|
|
188
205
|
},
|
|
@@ -205,12 +222,9 @@ function default_1(config) {
|
|
|
205
222
|
errorStr = error.toString();
|
|
206
223
|
}
|
|
207
224
|
const mem = new Uint8Array(instance.exports.memory.buffer);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
buffer.writeUInt32LE(mem, errorPtrPtr, ptr);
|
|
212
|
-
buffer.writeUInt32LE(mem, errorPtrPtr + 4, encoded.length);
|
|
213
|
-
buffer.writeUInt8(mem, errorPtrPtr + 8, isBadAddress ? 1 : 0);
|
|
225
|
+
config.bufferIndices[0] = new TextEncoder().encode(errorStr);
|
|
226
|
+
buffer.writeUInt32LE(mem, errorBufferIndexPtr, 0);
|
|
227
|
+
buffer.writeUInt8(mem, errorBufferIndexPtr + 4, isBadAddress ? 1 : 0);
|
|
214
228
|
return 1;
|
|
215
229
|
}
|
|
216
230
|
},
|
|
@@ -244,6 +258,12 @@ function default_1(config) {
|
|
|
244
258
|
const connection = connections[connectionId];
|
|
245
259
|
connection.send(data, streamId); // TODO: docs says the streamId is provided only for multi-stream connections, but here it's always provided
|
|
246
260
|
},
|
|
261
|
+
stream_send_close: (connectionId, streamId) => {
|
|
262
|
+
if (killedTracked.killed)
|
|
263
|
+
return;
|
|
264
|
+
const connection = connections[connectionId];
|
|
265
|
+
connection.closeSend(streamId); // TODO: docs says the streamId is provided only for multi-stream connections, but here it's always provided
|
|
266
|
+
},
|
|
247
267
|
current_task_entered: (ptr, len) => {
|
|
248
268
|
if (killedTracked.killed)
|
|
249
269
|
return;
|
|
@@ -9,21 +9,21 @@ export interface SmoldotWasmExports extends WebAssembly.Exports {
|
|
|
9
9
|
init: (maxLogLevel: number, enableCurrentTask: number, cpuRateLimit: number, periodicallyYield: number) => void;
|
|
10
10
|
set_periodically_yield: (periodicallyYield: number) => void;
|
|
11
11
|
start_shutdown: () => void;
|
|
12
|
-
|
|
13
|
-
add_chain: (chainSpecPointer: number, chainSpecLen: number, databaseContentPointer: number, databaseContentLen: number, jsonRpcRunning: number, potentialRelayChainsPtr: number, potentialRelayChainsLen: number) => number;
|
|
12
|
+
add_chain: (chainSpecBufferIndex: number, databaseContentBufferIndex: number, jsonRpcRunning: number, potentialRelayChainsBufferIndex: number) => number;
|
|
14
13
|
remove_chain: (chainId: number) => void;
|
|
15
14
|
chain_is_ok: (chainId: number) => number;
|
|
16
15
|
chain_error_len: (chainId: number) => number;
|
|
17
16
|
chain_error_ptr: (chainId: number) => number;
|
|
18
|
-
json_rpc_send: (
|
|
17
|
+
json_rpc_send: (textBufferIndex: number, chainId: number) => number;
|
|
19
18
|
json_rpc_responses_peek: (chainId: number) => number;
|
|
20
19
|
json_rpc_responses_pop: (chainId: number) => void;
|
|
21
20
|
timer_finished: (timerId: number) => void;
|
|
22
|
-
connection_open_single_stream: (connectionId: number, handshakeTy: number) => void;
|
|
23
|
-
connection_open_multi_stream: (connectionId: number,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
connection_open_single_stream: (connectionId: number, handshakeTy: number, initialWritableBytes: number, writeClosable: number) => void;
|
|
22
|
+
connection_open_multi_stream: (connectionId: number, handshakeTyBufferIndex: number) => void;
|
|
23
|
+
stream_writable_bytes: (connectionId: number, streamId: number, numBytes: number) => void;
|
|
24
|
+
stream_message: (connectionId: number, streamId: number, bufferIndex: number) => void;
|
|
25
|
+
connection_stream_opened: (connectionId: number, streamId: number, outbound: number, initialWritableBytes: number) => void;
|
|
26
|
+
connection_reset: (connectionId: number, bufferIndex: number) => void;
|
|
27
27
|
stream_reset: (connectionId: number, streamId: number) => void;
|
|
28
28
|
}
|
|
29
29
|
export interface SmoldotWasmInstance extends WebAssembly.Instance {
|
|
@@ -104,7 +104,7 @@ function start(configMessage, platformBindings) {
|
|
|
104
104
|
cpuRateLimit: configMessage.cpuRateLimit,
|
|
105
105
|
};
|
|
106
106
|
state = {
|
|
107
|
-
initialized: false, promise: instance.startInstance(config, platformBindings).then((instance) => {
|
|
107
|
+
initialized: false, promise: instance.startInstance(config, platformBindings).then(([instance, bufferIndices]) => {
|
|
108
108
|
// `config.cpuRateLimit` is a floating point that should be between 0 and 1, while the value
|
|
109
109
|
// to pass as parameter must be between `0` and `2^32-1`.
|
|
110
110
|
// The few lines of code below should handle all possible values of `number`, including
|
|
@@ -127,8 +127,8 @@ function start(configMessage, platformBindings) {
|
|
|
127
127
|
}
|
|
128
128
|
});
|
|
129
129
|
instance.exports.init(configMessage.maxLogLevel, configMessage.enableCurrentTask ? 1 : 0, cpuRateLimit, periodicallyYield ? 1 : 0);
|
|
130
|
-
state = { initialized: true, instance, unregisterCallback };
|
|
131
|
-
return instance;
|
|
130
|
+
state = { initialized: true, instance, bufferIndices, unregisterCallback };
|
|
131
|
+
return [instance, bufferIndices];
|
|
132
132
|
})
|
|
133
133
|
};
|
|
134
134
|
function queueOperation(operation) {
|
|
@@ -138,11 +138,11 @@ function start(configMessage, platformBindings) {
|
|
|
138
138
|
if (!state.initialized) {
|
|
139
139
|
// A message has been received while the Wasm VM is still initializing. Queue it for when
|
|
140
140
|
// initialization is over.
|
|
141
|
-
return state.promise.then((instance) => operation(instance));
|
|
141
|
+
return state.promise.then(([instance, bufferIndices]) => operation(instance, bufferIndices));
|
|
142
142
|
}
|
|
143
143
|
else {
|
|
144
144
|
// Everything is already initialized. Process the message synchronously.
|
|
145
|
-
return operation(state.instance);
|
|
145
|
+
return operation(state.instance, state.bufferIndices);
|
|
146
146
|
}
|
|
147
147
|
});
|
|
148
148
|
}
|
|
@@ -157,10 +157,8 @@ function start(configMessage, platformBindings) {
|
|
|
157
157
|
throw crashError.error;
|
|
158
158
|
let retVal;
|
|
159
159
|
try {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
new Uint8Array(state.instance.exports.memory.buffer).set(encoded, ptr);
|
|
163
|
-
retVal = state.instance.exports.json_rpc_send(ptr, encoded.length, chainId) >>> 0;
|
|
160
|
+
state.bufferIndices[0] = new TextEncoder().encode(request);
|
|
161
|
+
retVal = state.instance.exports.json_rpc_send(0, chainId) >>> 0;
|
|
164
162
|
}
|
|
165
163
|
catch (_error) {
|
|
166
164
|
console.assert(crashError.error);
|
|
@@ -207,29 +205,25 @@ function start(configMessage, platformBindings) {
|
|
|
207
205
|
}
|
|
208
206
|
}),
|
|
209
207
|
addChain: (chainSpec, databaseContent, potentialRelayChains, disableJsonRpc) => {
|
|
210
|
-
return queueOperation((instance) => {
|
|
208
|
+
return queueOperation((instance, bufferIndices) => {
|
|
211
209
|
if (crashError.error)
|
|
212
210
|
throw crashError.error;
|
|
213
211
|
try {
|
|
214
|
-
// Write the chain specification into memory.
|
|
215
|
-
const chainSpecEncoded = new TextEncoder().encode(chainSpec);
|
|
216
|
-
const chainSpecPtr = instance.exports.alloc(chainSpecEncoded.length) >>> 0;
|
|
217
|
-
new Uint8Array(instance.exports.memory.buffer).set(chainSpecEncoded, chainSpecPtr);
|
|
218
|
-
// Write the database content into memory.
|
|
219
|
-
const databaseContentEncoded = new TextEncoder().encode(databaseContent);
|
|
220
|
-
const databaseContentPtr = instance.exports.alloc(databaseContentEncoded.length) >>> 0;
|
|
221
|
-
new Uint8Array(instance.exports.memory.buffer).set(databaseContentEncoded, databaseContentPtr);
|
|
222
|
-
// Write the potential relay chains into memory.
|
|
223
|
-
const potentialRelayChainsLen = potentialRelayChains.length;
|
|
224
|
-
const potentialRelayChainsPtr = instance.exports.alloc(potentialRelayChainsLen * 4) >>> 0;
|
|
225
|
-
for (let idx = 0; idx < potentialRelayChains.length; ++idx) {
|
|
226
|
-
buffer.writeUInt32LE(new Uint8Array(instance.exports.memory.buffer), potentialRelayChainsPtr + idx * 4, potentialRelayChains[idx]);
|
|
227
|
-
}
|
|
228
212
|
// `add_chain` unconditionally allocates a chain id. If an error occurs, however, this chain
|
|
229
213
|
// id will refer to an *erroneous* chain. `chain_is_ok` is used below to determine whether it
|
|
230
214
|
// has succeeeded or not.
|
|
231
215
|
// Note that `add_chain` properly de-allocates buffers even if it failed.
|
|
232
|
-
|
|
216
|
+
bufferIndices[0] = new TextEncoder().encode(chainSpec);
|
|
217
|
+
bufferIndices[1] = new TextEncoder().encode(databaseContent);
|
|
218
|
+
const potentialRelayChainsEncoded = new Uint8Array(potentialRelayChains.length * 4);
|
|
219
|
+
for (let idx = 0; idx < potentialRelayChains.length; ++idx) {
|
|
220
|
+
buffer.writeUInt32LE(potentialRelayChainsEncoded, idx * 4, potentialRelayChains[idx]);
|
|
221
|
+
}
|
|
222
|
+
bufferIndices[2] = potentialRelayChainsEncoded;
|
|
223
|
+
const chainId = instance.exports.add_chain(0, 1, disableJsonRpc ? 0 : 1, 2);
|
|
224
|
+
delete bufferIndices[0];
|
|
225
|
+
delete bufferIndices[1];
|
|
226
|
+
delete bufferIndices[2];
|
|
233
227
|
if (instance.exports.chain_is_ok(chainId) != 0) {
|
|
234
228
|
console.assert(!chains.has(chainId));
|
|
235
229
|
chains.set(chainId, {
|
|
@@ -62,4 +62,4 @@ export interface PlatformBindings {
|
|
|
62
62
|
*/
|
|
63
63
|
connect(config: ConnectionConfig): Connection;
|
|
64
64
|
}
|
|
65
|
-
export declare function startInstance(config: Config, platformBindings: PlatformBindings): Promise<SmoldotWasmInstance>;
|
|
65
|
+
export declare function startInstance(config: Config, platformBindings: PlatformBindings): Promise<[SmoldotWasmInstance, Array<Uint8Array>]>;
|
|
@@ -36,8 +36,9 @@ function startInstance(config, platformBindings) {
|
|
|
36
36
|
// cross-platform cross-bundler approach.
|
|
37
37
|
const wasmBytecode = yield platformBindings.trustedBase64DecodeAndZlibInflate(wasm_js_1.default);
|
|
38
38
|
let killAll;
|
|
39
|
+
const bufferIndices = new Array;
|
|
39
40
|
// Used to bind with the smoldot-light bindings. See the `bindings-smoldot-light.js` file.
|
|
40
|
-
const smoldotJsConfig = Object.assign({ performanceNow: platformBindings.performanceNow, connect: platformBindings.connect, onPanic: (message) => {
|
|
41
|
+
const smoldotJsConfig = Object.assign({ bufferIndices, performanceNow: platformBindings.performanceNow, connect: platformBindings.connect, onPanic: (message) => {
|
|
41
42
|
killAll();
|
|
42
43
|
config.onWasmPanic(message);
|
|
43
44
|
throw new Error();
|
|
@@ -66,7 +67,7 @@ function startInstance(config, platformBindings) {
|
|
|
66
67
|
const instance = result.instance;
|
|
67
68
|
smoldotJsConfig.instance = instance;
|
|
68
69
|
wasiConfig.instance = instance;
|
|
69
|
-
return instance;
|
|
70
|
+
return [instance, bufferIndices];
|
|
70
71
|
});
|
|
71
72
|
}
|
|
72
73
|
exports.startInstance = startInstance;
|
|
@@ -40,6 +40,8 @@ export function start(options) {
|
|
|
40
40
|
return Promise.resolve(inflate(classicDecode(input)));
|
|
41
41
|
},
|
|
42
42
|
registerShouldPeriodicallyYield: (callback) => {
|
|
43
|
+
if (typeof document === 'undefined') // We might be in a web worker.
|
|
44
|
+
return [false, () => { }];
|
|
43
45
|
const wrappedCallback = () => callback(document.visibilityState === 'visible');
|
|
44
46
|
document.addEventListener('visibilitychange', wrappedCallback);
|
|
45
47
|
return [document.visibilityState === 'visible', () => { document.removeEventListener('visibilitychange', wrappedCallback); }];
|
|
@@ -68,9 +70,8 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
68
70
|
// Attempt to parse the multiaddress.
|
|
69
71
|
// TODO: remove support for `/wss` in a long time (https://github.com/paritytech/smoldot/issues/1940)
|
|
70
72
|
const wsParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)\/(ws|wss|tls\/ws)$/);
|
|
71
|
-
const webRTCParsed = config.address.match(/^\/(ip4|ip6)\/(.*?)\/udp\/(.*?)\/webrtc\/certhash\/(.*?)$/);
|
|
73
|
+
const webRTCParsed = config.address.match(/^\/(ip4|ip6)\/(.*?)\/udp\/(.*?)\/webrtc-direct\/certhash\/(.*?)$/);
|
|
72
74
|
if (wsParsed != null) {
|
|
73
|
-
let connection;
|
|
74
75
|
const proto = (wsParsed[4] == 'ws') ? 'ws' : 'wss';
|
|
75
76
|
if ((proto == 'ws' && forbidWs) ||
|
|
76
77
|
(proto == 'ws' && wsParsed[2] != 'localhost' && wsParsed[2] != '127.0.0.1' && forbidNonLocalWs) ||
|
|
@@ -80,10 +81,36 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
80
81
|
const url = (wsParsed[1] == 'ip6') ?
|
|
81
82
|
(proto + "://[" + wsParsed[2] + "]:" + wsParsed[3]) :
|
|
82
83
|
(proto + "://" + wsParsed[2] + ":" + wsParsed[3]);
|
|
83
|
-
connection = new WebSocket(url);
|
|
84
|
+
const connection = new WebSocket(url);
|
|
84
85
|
connection.binaryType = 'arraybuffer';
|
|
86
|
+
const bufferedAmountCheck = { quenedUnreportedBytes: 0, nextTimeout: 10 };
|
|
87
|
+
const checkBufferedAmount = () => {
|
|
88
|
+
if (connection.readyState != 1)
|
|
89
|
+
return;
|
|
90
|
+
// Note that we might expect `bufferedAmount` to always be <= the sum of the lengths
|
|
91
|
+
// of all the data that has been sent, but that might not be the case. For this
|
|
92
|
+
// reason, we use `bufferedAmount` as a hint rather than a correct value.
|
|
93
|
+
const bufferedAmount = connection.bufferedAmount;
|
|
94
|
+
let wasSent = bufferedAmountCheck.quenedUnreportedBytes - bufferedAmount;
|
|
95
|
+
if (wasSent < 0)
|
|
96
|
+
wasSent = 0;
|
|
97
|
+
bufferedAmountCheck.quenedUnreportedBytes -= wasSent;
|
|
98
|
+
if (bufferedAmountCheck.quenedUnreportedBytes != 0) {
|
|
99
|
+
setTimeout(checkBufferedAmount, bufferedAmountCheck.nextTimeout);
|
|
100
|
+
bufferedAmountCheck.nextTimeout *= 2;
|
|
101
|
+
if (bufferedAmountCheck.nextTimeout > 500)
|
|
102
|
+
bufferedAmountCheck.nextTimeout = 500;
|
|
103
|
+
}
|
|
104
|
+
// Note: it is important to call `onWritableBytes` at the very end, as it might
|
|
105
|
+
// trigger a call to `send`.
|
|
106
|
+
if (wasSent != 0)
|
|
107
|
+
config.onWritableBytes(wasSent);
|
|
108
|
+
};
|
|
85
109
|
connection.onopen = () => {
|
|
86
|
-
config.onOpen({
|
|
110
|
+
config.onOpen({
|
|
111
|
+
type: 'single-stream', handshake: 'multistream-select-noise-yamux',
|
|
112
|
+
initialWritableBytes: 1024 * 1024, writeClosable: false,
|
|
113
|
+
});
|
|
87
114
|
};
|
|
88
115
|
connection.onclose = (event) => {
|
|
89
116
|
const message = "Error code " + event.code + (!!event.reason ? (": " + event.reason) : "");
|
|
@@ -102,7 +129,13 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
102
129
|
},
|
|
103
130
|
send: (data) => {
|
|
104
131
|
connection.send(data);
|
|
132
|
+
if (bufferedAmountCheck.quenedUnreportedBytes == 0) {
|
|
133
|
+
bufferedAmountCheck.nextTimeout = 10;
|
|
134
|
+
setTimeout(checkBufferedAmount, 10);
|
|
135
|
+
}
|
|
136
|
+
bufferedAmountCheck.quenedUnreportedBytes += data.length;
|
|
105
137
|
},
|
|
138
|
+
closeSend: () => { throw new Error('Wrong connection type'); },
|
|
106
139
|
openOutSubstream: () => { throw new Error('Wrong connection type'); }
|
|
107
140
|
};
|
|
108
141
|
}
|
|
@@ -152,16 +185,18 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
152
185
|
pc.onnegotiationneeded = null;
|
|
153
186
|
pc.ondatachannel = null;
|
|
154
187
|
for (const channel of Array.from(dataChannels.values())) {
|
|
155
|
-
channel.onopen = null;
|
|
156
|
-
channel.onerror = null;
|
|
157
|
-
channel.onclose = null;
|
|
158
|
-
channel.
|
|
188
|
+
channel.channel.onopen = null;
|
|
189
|
+
channel.channel.onerror = null;
|
|
190
|
+
channel.channel.onclose = null;
|
|
191
|
+
channel.channel.onbufferedamountlow = null;
|
|
192
|
+
channel.channel.onmessage = null;
|
|
159
193
|
}
|
|
160
194
|
dataChannels.clear();
|
|
161
195
|
if (handshakeDataChannel) {
|
|
162
196
|
handshakeDataChannel.onopen = null;
|
|
163
197
|
handshakeDataChannel.onerror = null;
|
|
164
198
|
handshakeDataChannel.onclose = null;
|
|
199
|
+
handshakeDataChannel.onbufferedamountlow = null;
|
|
165
200
|
handshakeDataChannel.onmessage = null;
|
|
166
201
|
}
|
|
167
202
|
handshakeDataChannel = undefined;
|
|
@@ -190,7 +225,7 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
190
225
|
}
|
|
191
226
|
else {
|
|
192
227
|
console.assert(direction !== 'outbound' || !handshakeDataChannel, "handshakeDataChannel still defined");
|
|
193
|
-
config.onStreamOpened(dataChannelId, direction);
|
|
228
|
+
config.onStreamOpened(dataChannelId, direction, 65536);
|
|
194
229
|
}
|
|
195
230
|
};
|
|
196
231
|
dataChannel.onerror = dataChannel.onclose = (_error) => {
|
|
@@ -212,6 +247,7 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
212
247
|
handshakeDataChannel.onopen = null;
|
|
213
248
|
handshakeDataChannel.onerror = null;
|
|
214
249
|
handshakeDataChannel.onclose = null;
|
|
250
|
+
handshakeDataChannel.onbufferedamountlow = null;
|
|
215
251
|
handshakeDataChannel.onmessage = null;
|
|
216
252
|
handshakeDataChannel = undefined;
|
|
217
253
|
}
|
|
@@ -228,12 +264,18 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
228
264
|
config.onStreamReset(dataChannelId);
|
|
229
265
|
}
|
|
230
266
|
};
|
|
267
|
+
dataChannel.onbufferedamountlow = () => {
|
|
268
|
+
const channel = dataChannels.get(dataChannelId);
|
|
269
|
+
const val = channel.bufferedBytes;
|
|
270
|
+
channel.bufferedBytes = 0;
|
|
271
|
+
config.onWritableBytes(val, dataChannelId);
|
|
272
|
+
};
|
|
231
273
|
dataChannel.onmessage = (m) => {
|
|
232
274
|
// The `data` field is an `ArrayBuffer`.
|
|
233
275
|
config.onMessage(new Uint8Array(m.data), dataChannelId);
|
|
234
276
|
};
|
|
235
277
|
if (direction !== 'first-outbound')
|
|
236
|
-
dataChannels.set(dataChannelId, dataChannel);
|
|
278
|
+
dataChannels.set(dataChannelId, { channel: dataChannel, bufferedBytes: 0 });
|
|
237
279
|
else
|
|
238
280
|
handshakeDataChannel = dataChannel;
|
|
239
281
|
};
|
|
@@ -375,7 +417,8 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
375
417
|
// (UDP or TCP)
|
|
376
418
|
"a=sctp-port:5000" + "\n" +
|
|
377
419
|
// The maximum SCTP user message size (in bytes) (RFC8841)
|
|
378
|
-
|
|
420
|
+
// Setting this field is part of the libp2p spec.
|
|
421
|
+
"a=max-message-size:16384" + "\n" +
|
|
379
422
|
// A transport address for a candidate that can be used for connectivity
|
|
380
423
|
// checks (RFC8839).
|
|
381
424
|
"a=candidate:1 1 UDP 1 " + targetIp + " " + targetPort + " typ host" + "\n";
|
|
@@ -404,17 +447,21 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
404
447
|
}
|
|
405
448
|
else {
|
|
406
449
|
const channel = dataChannels.get(streamId);
|
|
407
|
-
channel.onopen = null;
|
|
408
|
-
channel.onerror = null;
|
|
409
|
-
channel.onclose = null;
|
|
410
|
-
channel.
|
|
411
|
-
channel.
|
|
450
|
+
channel.channel.onopen = null;
|
|
451
|
+
channel.channel.onerror = null;
|
|
452
|
+
channel.channel.onclose = null;
|
|
453
|
+
channel.channel.onbufferedamountlow = null;
|
|
454
|
+
channel.channel.onmessage = null;
|
|
455
|
+
channel.channel.close();
|
|
412
456
|
dataChannels.delete(streamId);
|
|
413
457
|
}
|
|
414
458
|
},
|
|
415
459
|
send: (data, streamId) => {
|
|
416
|
-
dataChannels.get(streamId)
|
|
460
|
+
const channel = dataChannels.get(streamId);
|
|
461
|
+
channel.channel.send(data);
|
|
462
|
+
channel.bufferedBytes += data.length;
|
|
417
463
|
},
|
|
464
|
+
closeSend: () => { throw new Error('Wrong connection type'); },
|
|
418
465
|
openOutSubstream: () => {
|
|
419
466
|
// `openOutSubstream` can only be called after we have called `config.onOpen`, therefore
|
|
420
467
|
// `pc` is guaranteed to be non-null.
|
|
@@ -426,8 +473,8 @@ function connect(config, forbidWs, forbidNonLocalWs, forbidWss, forbidWebRTC) {
|
|
|
426
473
|
// We need to check again if `handshakeDataChannel` is still defined, as the
|
|
427
474
|
// connection might have been closed.
|
|
428
475
|
if (handshakeDataChannel) {
|
|
429
|
-
config.onStreamOpened(handshakeDataChannel.id, 'outbound');
|
|
430
|
-
dataChannels.set(handshakeDataChannel.id, handshakeDataChannel);
|
|
476
|
+
config.onStreamOpened(handshakeDataChannel.id, 'outbound', 1024 * 1024);
|
|
477
|
+
dataChannels.set(handshakeDataChannel.id, { channel: handshakeDataChannel, bufferedBytes: 0 });
|
|
431
478
|
handshakeDataChannel = undefined;
|
|
432
479
|
}
|
|
433
480
|
}))();
|