smoldot 1.0.1 → 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.
@@ -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
  */
@@ -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) => {
@@ -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, errorPtrPtr) => {
133
+ connection_new: (connectionId, addrPtr, addrLen, errorBufferIndexPtr) => {
124
134
  const instance = config.instance;
125
135
  addrPtr >>>= 0;
126
136
  addrLen >>>= 0;
127
- errorPtrPtr >>>= 0;
137
+ errorBufferIndexPtr >>>= 0;
128
138
  if (!!connections[connectionId]) {
129
139
  throw new Error("internal error: connection already allocated");
130
140
  }
@@ -144,13 +154,13 @@ function default_1(config) {
144
154
  break;
145
155
  }
146
156
  case 'multi-stream': {
147
- const bufferLen = 1 + info.localTlsCertificateMultihash.length + info.remoteTlsCertificateMultihash.length;
148
- const ptr = instance.exports.alloc(bufferLen) >>> 0;
149
- const mem = new Uint8Array(instance.exports.memory.buffer);
150
- buffer.writeUInt8(mem, ptr, 0);
151
- mem.set(info.localTlsCertificateMultihash, ptr + 1);
152
- mem.set(info.remoteTlsCertificateMultihash, ptr + 1 + info.localTlsCertificateMultihash.length);
153
- instance.exports.connection_open_multi_stream(connectionId, ptr, bufferLen);
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,9 @@ function default_1(config) {
161
171
  if (killedTracked.killed)
162
172
  return;
163
173
  try {
164
- const encoded = new TextEncoder().encode(message);
165
- const ptr = instance.exports.alloc(encoded.length) >>> 0;
166
- new Uint8Array(instance.exports.memory.buffer).set(encoded, ptr);
167
- instance.exports.connection_reset(connectionId, ptr, encoded.length);
174
+ config.bufferIndices[0] = new TextEncoder().encode(message);
175
+ instance.exports.connection_reset(connectionId, 0);
176
+ delete config.bufferIndices[0];
168
177
  }
169
178
  catch (_error) { }
170
179
  },
@@ -180,9 +189,9 @@ function default_1(config) {
180
189
  if (killedTracked.killed)
181
190
  return;
182
191
  try {
183
- const ptr = instance.exports.alloc(message.length) >>> 0;
184
- new Uint8Array(instance.exports.memory.buffer).set(message, ptr);
185
- instance.exports.stream_message(connectionId, streamId || 0, ptr, message.length);
192
+ config.bufferIndices[0] = message;
193
+ instance.exports.stream_message(connectionId, streamId || 0, 0);
194
+ delete config.bufferIndices[0];
186
195
  }
187
196
  catch (_error) { }
188
197
  },
@@ -213,12 +222,9 @@ function default_1(config) {
213
222
  errorStr = error.toString();
214
223
  }
215
224
  const mem = new Uint8Array(instance.exports.memory.buffer);
216
- const encoded = new TextEncoder().encode(errorStr);
217
- const ptr = instance.exports.alloc(encoded.length) >>> 0;
218
- mem.set(encoded, ptr);
219
- buffer.writeUInt32LE(mem, errorPtrPtr, ptr);
220
- buffer.writeUInt32LE(mem, errorPtrPtr + 4, encoded.length);
221
- 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);
222
228
  return 1;
223
229
  }
224
230
  },
@@ -9,22 +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
- alloc: (len: number) => number;
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: (textPtr: number, textLen: number, chainId: number) => number;
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
21
  connection_open_single_stream: (connectionId: number, handshakeTy: number, initialWritableBytes: number, writeClosable: number) => void;
23
- connection_open_multi_stream: (connectionId: number, handshakeTyPtr: number, handshakeTyLen: number) => void;
22
+ connection_open_multi_stream: (connectionId: number, handshakeTyBufferIndex: number) => void;
24
23
  stream_writable_bytes: (connectionId: number, streamId: number, numBytes: number) => void;
25
- stream_message: (connectionId: number, streamId: number, ptr: number, len: number) => void;
24
+ stream_message: (connectionId: number, streamId: number, bufferIndex: number) => void;
26
25
  connection_stream_opened: (connectionId: number, streamId: number, outbound: number, initialWritableBytes: number) => void;
27
- connection_reset: (connectionId: number, ptr: number, len: number) => void;
26
+ connection_reset: (connectionId: number, bufferIndex: number) => void;
28
27
  stream_reset: (connectionId: number, streamId: number) => void;
29
28
  }
30
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
- const encoded = new TextEncoder().encode(request);
161
- const ptr = state.instance.exports.alloc(encoded.length) >>> 0;
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
- const chainId = instance.exports.add_chain(chainSpecPtr, chainSpecEncoded.length, databaseContentPtr, databaseContentEncoded.length, disableJsonRpc ? 0 : 1, potentialRelayChainsPtr, potentialRelayChainsLen);
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;