smoldot 1.0.8 → 1.0.9

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.
@@ -275,8 +275,26 @@ function start(options, wasmModule, platformBindings) {
275
275
  potentialRelayChainsIds.push(id);
276
276
  }
277
277
  }
278
+ // Sanitize `jsonRpcMaxPendingRequests`.
279
+ let jsonRpcMaxPendingRequests = options.jsonRpcMaxPendingRequests === undefined ? Infinity : options.jsonRpcMaxPendingRequests;
280
+ jsonRpcMaxPendingRequests = Math.floor(jsonRpcMaxPendingRequests);
281
+ if (jsonRpcMaxPendingRequests <= 0 || isNaN(jsonRpcMaxPendingRequests)) {
282
+ throw new public_types_js_1.AddChainError("Invalid value for `jsonRpcMaxPendingRequests`");
283
+ }
284
+ if (jsonRpcMaxPendingRequests > 0xffffffff) {
285
+ jsonRpcMaxPendingRequests = 0xffffffff;
286
+ }
287
+ // Sanitize `jsonRpcMaxSubscriptions`.
288
+ let jsonRpcMaxSubscriptions = options.jsonRpcMaxSubscriptions === undefined ? Infinity : options.jsonRpcMaxSubscriptions;
289
+ jsonRpcMaxSubscriptions = Math.floor(jsonRpcMaxSubscriptions);
290
+ if (jsonRpcMaxSubscriptions < 0 || isNaN(jsonRpcMaxSubscriptions)) {
291
+ throw new public_types_js_1.AddChainError("Invalid value for `jsonRpcMaxSubscriptions`");
292
+ }
293
+ if (jsonRpcMaxSubscriptions > 0xffffffff) {
294
+ jsonRpcMaxSubscriptions = 0xffffffff;
295
+ }
278
296
  const promise = new Promise((resolve) => state.addChainResults.push(resolve));
279
- state.instance.instance.addChain(options.chainSpec, typeof options.databaseContent === 'string' ? options.databaseContent : "", potentialRelayChainsIds, !!options.disableJsonRpc);
297
+ state.instance.instance.addChain(options.chainSpec, typeof options.databaseContent === 'string' ? options.databaseContent : "", potentialRelayChainsIds, !!options.disableJsonRpc, jsonRpcMaxPendingRequests, jsonRpcMaxSubscriptions);
280
298
  const outcome = yield promise;
281
299
  if (!outcome.success)
282
300
  throw new public_types_js_1.AddChainError(outcome.error);
@@ -94,7 +94,7 @@ export type ParsedMultiaddr = {
94
94
  export interface Instance {
95
95
  request: (request: string, chainId: number) => number;
96
96
  peekJsonRpcResponse: (chainId: number) => string | null;
97
- addChain: (chainSpec: string, databaseContent: string, potentialRelayChains: number[], disableJsonRpc: boolean) => void;
97
+ addChain: (chainSpec: string, databaseContent: string, potentialRelayChains: number[], disableJsonRpc: boolean, jsonRpcMaxPendingRequests: number, jsonRpcMaxSubscriptions: number) => void;
98
98
  removeChain: (chainId: number) => void;
99
99
  /**
100
100
  * Notifies the background executor that it should stop. Once it has effectively stopped,
@@ -423,11 +423,13 @@ function startLocalInstance(config, wasmModule, eventCallback) {
423
423
  return null;
424
424
  }
425
425
  },
426
- addChain: (chainSpec, databaseContent, potentialRelayChains, disableJsonRpc) => {
426
+ addChain: (chainSpec, databaseContent, potentialRelayChains, disableJsonRpc, jsonRpcMaxPendingRequests, jsonRpcMaxSubscriptions) => {
427
427
  if (!state.instance) {
428
428
  eventCallback({ ty: "add-chain-result", success: false, error: "Smoldot has crashed" });
429
429
  return;
430
430
  }
431
+ // The caller is supposed to avoid this situation.
432
+ console.assert(disableJsonRpc || jsonRpcMaxPendingRequests != 0, "invalid jsonRpcMaxPendingRequests value passed to local-instance::addChain");
431
433
  // `add_chain` unconditionally allocates a chain id. If an error occurs, however, this chain
432
434
  // id will refer to an *erroneous* chain. `chain_is_ok` is used below to determine whether it
433
435
  // has succeeeded or not.
@@ -438,7 +440,7 @@ function startLocalInstance(config, wasmModule, eventCallback) {
438
440
  buffer.writeUInt32LE(potentialRelayChainsEncoded, idx * 4, potentialRelayChains[idx]);
439
441
  }
440
442
  state.bufferIndices[2] = potentialRelayChainsEncoded;
441
- const chainId = state.instance.exports.add_chain(0, 1, disableJsonRpc ? 0 : 1, 2);
443
+ const chainId = state.instance.exports.add_chain(0, 1, disableJsonRpc ? 0 : jsonRpcMaxPendingRequests, jsonRpcMaxSubscriptions, 2);
442
444
  delete state.bufferIndices[0];
443
445
  delete state.bufferIndices[1];
444
446
  delete state.bufferIndices[2];
@@ -146,9 +146,9 @@ function connectToInstanceServer(config) {
146
146
  config.eventCallback(message);
147
147
  };
148
148
  return {
149
- addChain(chainSpec, databaseContent, potentialRelayChains, disableJsonRpc) {
149
+ addChain(chainSpec, databaseContent, potentialRelayChains, disableJsonRpc, jsonRpcMaxPendingRequests, jsonRpcMaxSubscriptions) {
150
150
  return __awaiter(this, void 0, void 0, function* () {
151
- const msg = { ty: "add-chain", chainSpec, databaseContent, potentialRelayChains, disableJsonRpc };
151
+ const msg = { ty: "add-chain", chainSpec, databaseContent, potentialRelayChains, disableJsonRpc, jsonRpcMaxPendingRequests, jsonRpcMaxSubscriptions };
152
152
  portToServer.postMessage(msg);
153
153
  });
154
154
  },
@@ -286,7 +286,7 @@ function startInstanceServer(config, initPortToClient) {
286
286
  const message = messageEvent.data;
287
287
  switch (message.ty) {
288
288
  case "add-chain": {
289
- state.instance.addChain(message.chainSpec, message.databaseContent, message.potentialRelayChains, message.disableJsonRpc);
289
+ state.instance.addChain(message.chainSpec, message.databaseContent, message.potentialRelayChains, message.disableJsonRpc, message.jsonRpcMaxPendingRequests, message.jsonRpcMaxSubscriptions);
290
290
  break;
291
291
  }
292
292
  case "remove-chain": {
@@ -132,10 +132,9 @@ function connect(config) {
132
132
  })
133
133
  };
134
134
  socket.inner = socket.inner.then((established) => {
135
- // TODO: at the time of writing of this comment, `setNoDelay` is still unstable
136
- //established.setNoDelay();
137
135
  if (socket.destroyed)
138
136
  return established;
137
+ established === null || established === void 0 ? void 0 : established.setNoDelay();
139
138
  config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux', initialWritableBytes: 1024 * 1024, writeClosable: true });
140
139
  // Spawns an asynchronous task that continuously reads from the socket.
141
140
  // Every time data is read, the task re-executes itself in order to continue reading.
@@ -365,4 +365,25 @@ export interface AddChainOptions {
365
365
  * this chain.
366
366
  */
367
367
  disableJsonRpc?: boolean;
368
+ /**
369
+ * Maximum number of JSON-RPC requests that haven't been answered yet. Any further request will
370
+ * be rejected.
371
+ *
372
+ * This field is ignored if {@link AddChainOptions.disableJsonRpc} is `true`.
373
+ *
374
+ * A zero, negative or NaN value is invalid.
375
+ *
376
+ * If this value is not set, it means that there is no maximum.
377
+ */
378
+ jsonRpcMaxPendingRequests?: number;
379
+ /**
380
+ * Maximum number of active JSON-RPC subscriptions. Any further subscription will be rejected.
381
+ *
382
+ * This field is ignored if {@link AddChainOptions.disableJsonRpc} is `true`.
383
+ *
384
+ * A negative or NaN value is invalid.
385
+ *
386
+ * If this value is not set, it means that there is no maximum.
387
+ */
388
+ jsonRpcMaxSubscriptions?: number;
368
389
  }