smoldot 1.0.2 → 1.0.4

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.
Files changed (39) hide show
  1. package/dist/cjs/client.d.ts +1 -1
  2. package/dist/cjs/client.js +15 -18
  3. package/dist/cjs/index-browser.js +17 -5
  4. package/dist/cjs/index-deno.js +38 -28
  5. package/dist/cjs/index-nodejs.js +7 -4
  6. package/dist/cjs/instance/autogen/wasm0.js +1 -1
  7. package/dist/cjs/instance/autogen/wasm1.js +1 -1
  8. package/dist/cjs/instance/autogen/wasm2.js +1 -1
  9. package/dist/cjs/instance/bindings-smoldot-light.d.ts +7 -6
  10. package/dist/cjs/instance/bindings-smoldot-light.js +6 -7
  11. package/dist/cjs/instance/bindings-wasi.d.ts +4 -0
  12. package/dist/cjs/instance/bindings-wasi.js +30 -0
  13. package/dist/cjs/instance/bindings.d.ts +3 -3
  14. package/dist/cjs/instance/buffer.d.ts +1 -0
  15. package/dist/cjs/instance/buffer.js +13 -1
  16. package/dist/cjs/instance/instance.d.ts +1 -1
  17. package/dist/cjs/instance/instance.js +41 -59
  18. package/dist/cjs/instance/raw-instance.d.ts +2 -11
  19. package/dist/cjs/instance/raw-instance.js +65 -9
  20. package/dist/mjs/client.d.ts +1 -1
  21. package/dist/mjs/client.js +15 -18
  22. package/dist/mjs/index-browser.js +17 -5
  23. package/dist/mjs/index-deno.js +38 -28
  24. package/dist/mjs/index-nodejs.js +7 -4
  25. package/dist/mjs/instance/autogen/wasm0.js +1 -1
  26. package/dist/mjs/instance/autogen/wasm1.js +1 -1
  27. package/dist/mjs/instance/autogen/wasm2.js +1 -1
  28. package/dist/mjs/instance/bindings-smoldot-light.d.ts +7 -6
  29. package/dist/mjs/instance/bindings-smoldot-light.js +6 -7
  30. package/dist/mjs/instance/bindings-wasi.d.ts +4 -0
  31. package/dist/mjs/instance/bindings-wasi.js +30 -0
  32. package/dist/mjs/instance/bindings.d.ts +3 -3
  33. package/dist/mjs/instance/buffer.d.ts +1 -0
  34. package/dist/mjs/instance/buffer.js +11 -0
  35. package/dist/mjs/instance/instance.d.ts +1 -1
  36. package/dist/mjs/instance/instance.js +41 -59
  37. package/dist/mjs/instance/raw-instance.d.ts +2 -11
  38. package/dist/mjs/instance/raw-instance.js +65 -9
  39. package/package.json +2 -2
@@ -317,4 +317,4 @@ export interface AddChainOptions {
317
317
  */
318
318
  disableJsonRpc?: boolean;
319
319
  }
320
- export declare function start(options: ClientOptions, platformBindings: PlatformBindings): Client;
320
+ export declare function start(options: ClientOptions, wasmModule: Promise<WebAssembly.Module>, platformBindings: PlatformBindings): Client;
@@ -62,7 +62,7 @@ exports.JsonRpcDisabledError = JsonRpcDisabledError;
62
62
  // This function is similar to the `start` function found in `index.ts`, except with an extra
63
63
  // parameter containing the platform-specific bindings.
64
64
  // Contrary to the one within `index.js`, this function is not supposed to be directly used.
65
- function start(options, platformBindings) {
65
+ function start(options, wasmModule, platformBindings) {
66
66
  const logCallback = options.logCallback || ((level, target, message) => {
67
67
  // The first parameter of the methods of `console` has some printf-like substitution
68
68
  // capabilities. We don't really need to use this, but not using it means that the logs might
@@ -86,25 +86,22 @@ function start(options, platformBindings) {
86
86
  // For each chain object returned by `addChain`, the associated internal chain id.
87
87
  //
88
88
  // Immediately cleared when `remove()` is called on a chain.
89
- let chainIds = new WeakMap();
89
+ const chainIds = new WeakMap();
90
90
  // If `Client.terminate()̀ is called, this error is set to a value.
91
91
  // All the functions of the public API check if this contains a value.
92
- let alreadyDestroyedError = null;
92
+ const alreadyDestroyedError = { value: null };
93
93
  const instance = (0, instance_js_1.start)({
94
+ wasmModule,
94
95
  // Maximum level of log entries sent by the client.
95
96
  // 0 = Logging disabled, 1 = Error, 2 = Warn, 3 = Info, 4 = Debug, 5 = Trace
96
97
  maxLogLevel: options.maxLogLevel || 3,
97
98
  logCallback,
98
- // `enableCurrentTask` adds a small performance hit, but adds some additional information to
99
- // crash reports. Whether this should be enabled is very opiniated and not that important. At
100
- // the moment, we enable it all the time, except if the user has logging disabled altogether.
101
- enableCurrentTask: options.maxLogLevel ? options.maxLogLevel >= 1 : true,
102
99
  cpuRateLimit: options.cpuRateLimit || 1.0,
103
100
  }, platformBindings);
104
101
  return {
105
102
  addChain: (options) => __awaiter(this, void 0, void 0, function* () {
106
- if (alreadyDestroyedError)
107
- throw alreadyDestroyedError;
103
+ if (alreadyDestroyedError.value)
104
+ throw alreadyDestroyedError.value;
108
105
  // Passing a JSON object for the chain spec is an easy mistake, so we provide a more
109
106
  // readable error.
110
107
  if (!(typeof options.chainSpec === 'string'))
@@ -129,8 +126,8 @@ function start(options, platformBindings) {
129
126
  // Resolve the promise that `addChain` returned to the user.
130
127
  const newChain = {
131
128
  sendJsonRpc: (request) => {
132
- if (alreadyDestroyedError)
133
- throw alreadyDestroyedError;
129
+ if (alreadyDestroyedError.value)
130
+ throw alreadyDestroyedError.value;
134
131
  if (wasDestroyed.destroyed)
135
132
  throw new AlreadyDestroyedError();
136
133
  if (options.disableJsonRpc)
@@ -142,8 +139,8 @@ function start(options, platformBindings) {
142
139
  instance.request(request, chainId);
143
140
  },
144
141
  nextJsonRpcResponse: () => {
145
- if (alreadyDestroyedError)
146
- return Promise.reject(alreadyDestroyedError);
142
+ if (alreadyDestroyedError.value)
143
+ return Promise.reject(alreadyDestroyedError.value);
147
144
  if (wasDestroyed.destroyed)
148
145
  return Promise.reject(new AlreadyDestroyedError());
149
146
  if (options.disableJsonRpc)
@@ -151,8 +148,8 @@ function start(options, platformBindings) {
151
148
  return instance.nextJsonRpcResponse(chainId);
152
149
  },
153
150
  remove: () => {
154
- if (alreadyDestroyedError)
155
- throw alreadyDestroyedError;
151
+ if (alreadyDestroyedError.value)
152
+ throw alreadyDestroyedError.value;
156
153
  if (wasDestroyed.destroyed)
157
154
  throw new AlreadyDestroyedError();
158
155
  wasDestroyed.destroyed = true;
@@ -165,9 +162,9 @@ function start(options, platformBindings) {
165
162
  return newChain;
166
163
  }),
167
164
  terminate: () => __awaiter(this, void 0, void 0, function* () {
168
- if (alreadyDestroyedError)
169
- throw alreadyDestroyedError;
170
- alreadyDestroyedError = new AlreadyDestroyedError();
165
+ if (alreadyDestroyedError.value)
166
+ throw alreadyDestroyedError.value;
167
+ alreadyDestroyedError.value = new AlreadyDestroyedError();
171
168
  instance.startShutdown();
172
169
  })
173
170
  };
@@ -28,6 +28,7 @@ const client_js_1 = require("./client.js");
28
28
  const instance_js_1 = require("./instance/instance.js");
29
29
  const base64_js_1 = require("./base64.js");
30
30
  const pako_1 = require("pako");
31
+ const wasm_js_1 = require("./instance/autogen/wasm.js");
31
32
  var client_js_2 = require("./client.js");
32
33
  Object.defineProperty(exports, "AddChainError", { enumerable: true, get: function () { return client_js_2.AddChainError; } });
33
34
  Object.defineProperty(exports, "AlreadyDestroyedError", { enumerable: true, get: function () { return client_js_2.AlreadyDestroyedError; } });
@@ -44,10 +45,12 @@ Object.defineProperty(exports, "QueueFullError", { enumerable: true, get: functi
44
45
  */
45
46
  function start(options) {
46
47
  options = options || {};
47
- return (0, client_js_1.start)(options, {
48
- trustedBase64DecodeAndZlibInflate: (input) => {
49
- return Promise.resolve((0, pako_1.inflate)((0, base64_js_1.classicDecode)(input)));
50
- },
48
+ // The actual Wasm bytecode is base64-decoded then deflate-decoded from a constant found in a
49
+ // different file.
50
+ // This is suboptimal compared to using `instantiateStreaming`, but it is the most
51
+ // cross-platform cross-bundler approach.
52
+ const wasmModule = WebAssembly.compile((0, pako_1.inflate)((0, base64_js_1.classicDecode)(wasm_js_1.default)));
53
+ return (0, client_js_1.start)(options, wasmModule, {
51
54
  registerShouldPeriodicallyYield: (callback) => {
52
55
  if (typeof document === 'undefined') // We might be in a web worker.
53
56
  return [false, () => { }];
@@ -62,7 +65,16 @@ function start(options) {
62
65
  const crypto = globalThis.crypto;
63
66
  if (!crypto)
64
67
  throw new Error('randomness not available');
65
- crypto.getRandomValues(buffer);
68
+ // Browsers have this completely undocumented behavior (it's not even part of a spec)
69
+ // that for some reason `getRandomValues` can't be called on arrayviews back by
70
+ // `SharedArrayBuffer`s and they throw an exception if you try.
71
+ if (buffer.buffer instanceof ArrayBuffer)
72
+ crypto.getRandomValues(buffer);
73
+ else {
74
+ const tmpArray = new Uint8Array(buffer.length);
75
+ crypto.getRandomValues(tmpArray);
76
+ buffer.set(tmpArray);
77
+ }
66
78
  },
67
79
  connect: (config) => {
68
80
  return connect(config, (options === null || options === void 0 ? void 0 : options.forbidWs) || false, (options === null || options === void 0 ? void 0 : options.forbidNonLocalWs) || false, (options === null || options === void 0 ? void 0 : options.forbidWss) || false, (options === null || options === void 0 ? void 0 : options.forbidWebRtc) || false);
@@ -25,6 +25,7 @@ exports.start = exports.JsonRpcDisabledError = exports.QueueFullError = exports.
25
25
  // along with this program. If not, see <http://www.gnu.org/licenses/>.
26
26
  const client_js_1 = require("./client.js");
27
27
  const instance_js_1 = require("./instance/instance.js");
28
+ const wasm_js_1 = require("./instance/autogen/wasm.js");
28
29
  var client_js_2 = require("./client.js");
29
30
  Object.defineProperty(exports, "AddChainError", { enumerable: true, get: function () { return client_js_2.AddChainError; } });
30
31
  Object.defineProperty(exports, "AlreadyDestroyedError", { enumerable: true, get: function () { return client_js_2.AlreadyDestroyedError; } });
@@ -41,33 +42,12 @@ Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get:
41
42
  */
42
43
  function start(options) {
43
44
  options = options || {};
44
- return (0, client_js_1.start)(options || {}, {
45
- trustedBase64DecodeAndZlibInflate: (input) => __awaiter(this, void 0, void 0, function* () {
46
- const buffer = trustedBase64Decode(input);
47
- // This code has been copy-pasted from the official streams draft specification.
48
- // At the moment, it is found here: https://wicg.github.io/compression/#example-deflate-compress
49
- const ds = new DecompressionStream('deflate');
50
- const writer = ds.writable.getWriter();
51
- writer.write(buffer);
52
- writer.close();
53
- const output = [];
54
- const reader = ds.readable.getReader();
55
- let totalSize = 0;
56
- while (true) {
57
- const { value, done } = yield reader.read();
58
- if (done)
59
- break;
60
- output.push(value);
61
- totalSize += value.byteLength;
62
- }
63
- const concatenated = new Uint8Array(totalSize);
64
- let offset = 0;
65
- for (const array of output) {
66
- concatenated.set(array, offset);
67
- offset += array.byteLength;
68
- }
69
- return concatenated;
70
- }),
45
+ // The actual Wasm bytecode is base64-decoded then deflate-decoded from a constant found in a
46
+ // different file.
47
+ // This is suboptimal compared to using `instantiateStreaming`, but it is the most
48
+ // cross-platform cross-bundler approach.
49
+ const wasmModule = zlibInflate(trustedBase64Decode(wasm_js_1.default)).then(((bytecode) => WebAssembly.compile(bytecode)));
50
+ return (0, client_js_1.start)(options || {}, wasmModule, {
71
51
  registerShouldPeriodicallyYield: (_callback) => {
72
52
  return [true, () => { }];
73
53
  },
@@ -86,6 +66,36 @@ function start(options) {
86
66
  });
87
67
  }
88
68
  exports.start = start;
69
+ /**
70
+ * Applies the zlib inflate algorithm on the buffer.
71
+ */
72
+ function zlibInflate(buffer) {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ // This code has been copy-pasted from the official streams draft specification.
75
+ // At the moment, it is found here: https://wicg.github.io/compression/#example-deflate-compress
76
+ const ds = new DecompressionStream('deflate');
77
+ const writer = ds.writable.getWriter();
78
+ writer.write(buffer);
79
+ writer.close();
80
+ const output = [];
81
+ const reader = ds.readable.getReader();
82
+ let totalSize = 0;
83
+ while (true) {
84
+ const { value, done } = yield reader.read();
85
+ if (done)
86
+ break;
87
+ output.push(value);
88
+ totalSize += value.byteLength;
89
+ }
90
+ const concatenated = new Uint8Array(totalSize);
91
+ let offset = 0;
92
+ for (const array of output) {
93
+ concatenated.set(array, offset);
94
+ offset += array.byteLength;
95
+ }
96
+ return concatenated;
97
+ });
98
+ }
89
99
  /**
90
100
  * Decodes a base64 string.
91
101
  *
@@ -237,7 +247,7 @@ function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
237
247
  config.onMessage(readBuffer.slice(0, outcome));
238
248
  return read(readBuffer);
239
249
  });
240
- read(new Uint8Array(1024));
250
+ read(new Uint8Array(32768));
241
251
  return established;
242
252
  });
243
253
  return {
@@ -19,6 +19,7 @@ exports.start = exports.JsonRpcDisabledError = exports.QueueFullError = exports.
19
19
  // with both at the same time.
20
20
  const client_js_1 = require("./client.js");
21
21
  const instance_js_1 = require("./instance/instance.js");
22
+ const wasm_js_1 = require("./instance/autogen/wasm.js");
22
23
  const ws_1 = require("ws");
23
24
  const pako_1 = require("pako");
24
25
  const node_perf_hooks_1 = require("node:perf_hooks");
@@ -40,10 +41,12 @@ Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get:
40
41
  */
41
42
  function start(options) {
42
43
  options = options || {};
43
- return (0, client_js_1.start)(options || {}, {
44
- trustedBase64DecodeAndZlibInflate: (input) => {
45
- return Promise.resolve((0, pako_1.inflate)(Buffer.from(input, 'base64')));
46
- },
44
+ // The actual Wasm bytecode is base64-decoded then deflate-decoded from a constant found in a
45
+ // different file.
46
+ // This is suboptimal compared to using `instantiateStreaming`, but it is the most
47
+ // cross-platform cross-bundler approach.
48
+ const wasmModule = WebAssembly.compile((0, pako_1.inflate)(Buffer.from(wasm_js_1.default, 'base64')));
49
+ return (0, client_js_1.start)(options || {}, wasmModule, {
47
50
  registerShouldPeriodicallyYield: (_callback) => {
48
51
  return [true, () => { }];
49
52
  },