smoldot 1.0.4 → 1.0.6

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 (116) hide show
  1. package/README.md +58 -0
  2. package/dist/cjs/bytecode-browser.d.ts +5 -0
  3. package/dist/cjs/bytecode-browser.js +42 -0
  4. package/dist/cjs/bytecode-deno.d.ts +5 -0
  5. package/dist/cjs/bytecode-deno.js +87 -0
  6. package/dist/cjs/bytecode-nodejs.d.ts +5 -0
  7. package/dist/cjs/bytecode-nodejs.js +41 -0
  8. package/dist/cjs/index-browser.d.ts +2 -2
  9. package/dist/cjs/index-browser.js +10 -507
  10. package/dist/cjs/index-deno.d.ts +2 -2
  11. package/dist/cjs/index-deno.js +10 -283
  12. package/dist/cjs/index-nodejs.d.ts +2 -2
  13. package/dist/cjs/index-nodejs.js +10 -211
  14. package/dist/cjs/internals/bytecode/wasm0.js +4 -0
  15. package/dist/cjs/internals/bytecode/wasm1.js +4 -0
  16. package/dist/cjs/internals/bytecode/wasm2.js +4 -0
  17. package/dist/cjs/{instance/bindings-smoldot-light.d.ts → internals/client.d.ts} +15 -34
  18. package/dist/cjs/internals/client.js +376 -0
  19. package/dist/cjs/internals/local-instance.d.ts +144 -0
  20. package/dist/cjs/internals/local-instance.js +566 -0
  21. package/dist/cjs/internals/remote-instance.d.ts +38 -0
  22. package/dist/cjs/internals/remote-instance.js +374 -0
  23. package/dist/cjs/no-auto-bytecode-browser.d.ts +11 -0
  24. package/dist/cjs/no-auto-bytecode-browser.js +481 -0
  25. package/dist/cjs/no-auto-bytecode-deno.d.ts +10 -0
  26. package/dist/cjs/no-auto-bytecode-deno.js +220 -0
  27. package/dist/cjs/no-auto-bytecode-nodejs.d.ts +10 -0
  28. package/dist/cjs/no-auto-bytecode-nodejs.js +189 -0
  29. package/dist/cjs/{client.d.ts → public-types.d.ts} +51 -3
  30. package/dist/cjs/public-types.js +76 -0
  31. package/dist/cjs/worker-browser.d.ts +11 -0
  32. package/dist/cjs/worker-browser.js +63 -0
  33. package/dist/cjs/worker-deno.d.ts +11 -0
  34. package/dist/cjs/worker-deno.js +54 -0
  35. package/dist/cjs/worker-nodejs.d.ts +11 -0
  36. package/dist/cjs/worker-nodejs.js +55 -0
  37. package/dist/mjs/bytecode-browser.d.ts +5 -0
  38. package/dist/mjs/bytecode-browser.js +38 -0
  39. package/dist/mjs/bytecode-deno.d.ts +5 -0
  40. package/dist/mjs/bytecode-deno.js +83 -0
  41. package/dist/mjs/bytecode-nodejs.d.ts +5 -0
  42. package/dist/mjs/bytecode-nodejs.js +37 -0
  43. package/dist/mjs/index-browser.d.ts +2 -2
  44. package/dist/mjs/index-browser.js +4 -501
  45. package/dist/mjs/index-deno.d.ts +2 -2
  46. package/dist/mjs/index-deno.js +4 -277
  47. package/dist/mjs/index-nodejs.d.ts +2 -2
  48. package/dist/mjs/index-nodejs.js +4 -205
  49. package/dist/mjs/internals/bytecode/wasm0.js +1 -0
  50. package/dist/mjs/internals/bytecode/wasm1.js +1 -0
  51. package/dist/mjs/internals/bytecode/wasm2.js +1 -0
  52. package/dist/mjs/{instance/bindings-smoldot-light.d.ts → internals/client.d.ts} +15 -34
  53. package/dist/mjs/internals/client.js +372 -0
  54. package/dist/mjs/internals/local-instance.d.ts +144 -0
  55. package/dist/mjs/internals/local-instance.js +561 -0
  56. package/dist/mjs/internals/remote-instance.d.ts +38 -0
  57. package/dist/mjs/internals/remote-instance.js +369 -0
  58. package/dist/mjs/no-auto-bytecode-browser.d.ts +11 -0
  59. package/dist/mjs/no-auto-bytecode-browser.js +471 -0
  60. package/dist/mjs/no-auto-bytecode-deno.d.ts +10 -0
  61. package/dist/mjs/no-auto-bytecode-deno.js +210 -0
  62. package/dist/mjs/no-auto-bytecode-nodejs.d.ts +10 -0
  63. package/dist/mjs/no-auto-bytecode-nodejs.js +179 -0
  64. package/dist/mjs/{client.d.ts → public-types.d.ts} +51 -3
  65. package/dist/mjs/public-types.js +67 -0
  66. package/dist/mjs/worker-browser.d.ts +11 -0
  67. package/dist/mjs/worker-browser.js +59 -0
  68. package/dist/mjs/worker-deno.d.ts +11 -0
  69. package/dist/mjs/worker-deno.js +50 -0
  70. package/dist/mjs/worker-nodejs.d.ts +11 -0
  71. package/dist/mjs/worker-nodejs.js +51 -0
  72. package/package.json +40 -8
  73. package/dist/cjs/client.js +0 -172
  74. package/dist/cjs/instance/autogen/wasm0.js +0 -4
  75. package/dist/cjs/instance/autogen/wasm1.js +0 -4
  76. package/dist/cjs/instance/autogen/wasm2.js +0 -4
  77. package/dist/cjs/instance/bindings-smoldot-light.js +0 -285
  78. package/dist/cjs/instance/bindings-wasi.d.ts +0 -27
  79. package/dist/cjs/instance/bindings-wasi.js +0 -172
  80. package/dist/cjs/instance/bindings.d.ts +0 -31
  81. package/dist/cjs/instance/bindings.js +0 -5
  82. package/dist/cjs/instance/instance.d.ts +0 -45
  83. package/dist/cjs/instance/instance.js +0 -277
  84. package/dist/cjs/instance/raw-instance.d.ts +0 -56
  85. package/dist/cjs/instance/raw-instance.js +0 -129
  86. package/dist/mjs/client.js +0 -162
  87. package/dist/mjs/instance/autogen/wasm0.js +0 -1
  88. package/dist/mjs/instance/autogen/wasm1.js +0 -1
  89. package/dist/mjs/instance/autogen/wasm2.js +0 -1
  90. package/dist/mjs/instance/bindings-smoldot-light.js +0 -280
  91. package/dist/mjs/instance/bindings-wasi.d.ts +0 -27
  92. package/dist/mjs/instance/bindings-wasi.js +0 -170
  93. package/dist/mjs/instance/bindings.d.ts +0 -31
  94. package/dist/mjs/instance/bindings.js +0 -4
  95. package/dist/mjs/instance/instance.d.ts +0 -45
  96. package/dist/mjs/instance/instance.js +0 -269
  97. package/dist/mjs/instance/raw-instance.d.ts +0 -56
  98. package/dist/mjs/instance/raw-instance.js +0 -124
  99. /package/dist/cjs/{base64.d.ts → internals/base64.d.ts} +0 -0
  100. /package/dist/cjs/{base64.js → internals/base64.js} +0 -0
  101. /package/dist/cjs/{instance → internals}/buffer.d.ts +0 -0
  102. /package/dist/cjs/{instance → internals}/buffer.js +0 -0
  103. /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm.d.ts +0 -0
  104. /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm.js +0 -0
  105. /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm0.d.ts +0 -0
  106. /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm1.d.ts +0 -0
  107. /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm2.d.ts +0 -0
  108. /package/dist/mjs/{base64.d.ts → internals/base64.d.ts} +0 -0
  109. /package/dist/mjs/{base64.js → internals/base64.js} +0 -0
  110. /package/dist/mjs/{instance → internals}/buffer.d.ts +0 -0
  111. /package/dist/mjs/{instance → internals}/buffer.js +0 -0
  112. /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm.d.ts +0 -0
  113. /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm.js +0 -0
  114. /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm0.d.ts +0 -0
  115. /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm1.d.ts +0 -0
  116. /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm2.d.ts +0 -0
package/README.md CHANGED
@@ -67,3 +67,61 @@ chains must be passed as parameter to `addChain` as well. In situations where th
67
67
  specifications passed to `addChain` are not trusted, it is important for security reasons to not
68
68
  establish a parachain-relay-chain link between two chains that aren't part of the same "trust
69
69
  sandbox".
70
+
71
+ ## Usage with a worker
72
+
73
+ By default, calling `start()` will run smoldot entirely in the current thread. This can cause
74
+ performance issues if other CPU-heavy operations are done in that thread.
75
+
76
+ In order to help with this, it possible to use smoldot in conjunction with a worker.
77
+ To do so, you must first create a worker. Since creating a worker has some subtle differences
78
+ depending on the platform, this is outside of the responsibility of smoldot.
79
+
80
+ Once the worker is created, create two `MessagePort`s using `new MessageChannel`, and send one
81
+ of them to the worker. Then, pass one port to the `ClientOptions.portToWorker` field and the
82
+ other port to the `run()` function of smoldot, which can be imported with
83
+ `import { run } from 'smoldot/worker';` (on Deno, it is found in `worker-deno.ts`).
84
+
85
+ Another optimization that is orthogonal to but is related to running smoldot in a worker consists
86
+ in also loading the smoldot bytecode in that worker. The smoldot bytecode weights several
87
+ megabytes, and loading it in a worker rather than the main thread makes it possible to load the
88
+ UI while smoldot is still initializing. This is especially important when smoldot is included in
89
+ an application served over the web.
90
+
91
+ In order to load the smoldot bytecode in a worker, import `compileBytecode` with
92
+ `import { compileBytecode } from 'smoldot/bytecode';` (on Deno: `bytecode-deno.ts`), then call the
93
+ function and send the result to the main thread. From the main thread, rather than using the
94
+ `start` function imported from `smoldot`, use the `startWithBytecode` function that can be imported
95
+ using `import { startWithBytecode } from 'smoldot/no-auto-bytecode';` (on Deno:
96
+ `no-auto-bytecode-deno.ts`). The options provided to `startWithBytecode` are the same as the ones
97
+ passed to `start`, except for an additional `bytecode` field that must be set to the bytecode
98
+ created in the worker.
99
+
100
+ Here is an example of all this, assuming a browser environment:
101
+
102
+ ```ts
103
+ import * as smoldot from 'smoldot/no-auto-bytecode';
104
+
105
+ const worker = new Worker(new URL('./worker.js', import.meta.url));
106
+
107
+ const bytecode = new Promise((resolve) => {
108
+ worker.onmessage = (event) => resolve(event.data);
109
+ });
110
+
111
+ const { port1, port2 } = new MessageChannel();
112
+ worker.postMessage(port1, [port1]);
113
+
114
+ const client = smoldot.startWithBytecode({
115
+ bytecode,
116
+ portToWorker: port2,
117
+ });
118
+
119
+
120
+ // `worker.ts`
121
+
122
+ import * as smoldot from 'smoldot/worker';
123
+ import { compileBytecode } from 'smoldot/bytecode';
124
+
125
+ compileBytecode().then((bytecode) => postMessage(bytecode))
126
+ onmessage = (msg) => smoldot.run(msg.data);
127
+ ```
@@ -0,0 +1,5 @@
1
+ import { SmoldotBytecode } from './public-types.js';
2
+ /**
3
+ * Compiles and returns the smoldot WebAssembly binary.
4
+ */
5
+ export declare function compileBytecode(): Promise<SmoldotBytecode>;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ // Smoldot
3
+ // Copyright (C) 2023 Pierre Krieger
4
+ // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.compileBytecode = void 0;
16
+ // This program is free software: you can redistribute it and/or modify
17
+ // it under the terms of the GNU General Public License as published by
18
+ // the Free Software Foundation, either version 3 of the License, or
19
+ // (at your option) any later version.
20
+ // This program is distributed in the hope that it will be useful,
21
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ // GNU General Public License for more details.
24
+ // You should have received a copy of the GNU General Public License
25
+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
26
+ const wasm_js_1 = require("./internals/bytecode/wasm.js");
27
+ const base64_js_1 = require("./internals/base64.js");
28
+ const pako_1 = require("pako");
29
+ /**
30
+ * Compiles and returns the smoldot WebAssembly binary.
31
+ */
32
+ function compileBytecode() {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ // The actual Wasm bytecode is base64-decoded then deflate-decoded from a constant found in a
35
+ // different file.
36
+ // This is suboptimal compared to using `instantiateStreaming`, but it is the most
37
+ // cross-platform cross-bundler approach.
38
+ return WebAssembly.compile((0, pako_1.inflate)((0, base64_js_1.classicDecode)(wasm_js_1.default)))
39
+ .then((m) => { return { wasm: m }; });
40
+ });
41
+ }
42
+ exports.compileBytecode = compileBytecode;
@@ -0,0 +1,5 @@
1
+ import { SmoldotBytecode } from './public-types.js';
2
+ /**
3
+ * Compiles and returns the smoldot WebAssembly binary.
4
+ */
5
+ export declare function compileBytecode(): Promise<SmoldotBytecode>;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ // Smoldot
3
+ // Copyright (C) 2023 Pierre Krieger
4
+ // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.compileBytecode = void 0;
16
+ // This program is free software: you can redistribute it and/or modify
17
+ // it under the terms of the GNU General Public License as published by
18
+ // the Free Software Foundation, either version 3 of the License, or
19
+ // (at your option) any later version.
20
+ // This program is distributed in the hope that it will be useful,
21
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ // GNU General Public License for more details.
24
+ // You should have received a copy of the GNU General Public License
25
+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
26
+ const wasm_js_1 = require("./internals/bytecode/wasm.js");
27
+ /**
28
+ * Compiles and returns the smoldot WebAssembly binary.
29
+ */
30
+ function compileBytecode() {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ // The actual Wasm bytecode is base64-decoded then deflate-decoded from a constant found in a
33
+ // different file.
34
+ // This is suboptimal compared to using `instantiateStreaming`, but it is the most
35
+ // cross-platform cross-bundler approach.
36
+ return zlibInflate(trustedBase64Decode(wasm_js_1.default))
37
+ .then(((bytecode) => WebAssembly.compile(bytecode)))
38
+ .then((m) => { return { wasm: m }; });
39
+ });
40
+ }
41
+ exports.compileBytecode = compileBytecode;
42
+ /**
43
+ * Applies the zlib inflate algorithm on the buffer.
44
+ */
45
+ function zlibInflate(buffer) {
46
+ return __awaiter(this, void 0, void 0, function* () {
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
+ });
71
+ }
72
+ /**
73
+ * Decodes a base64 string.
74
+ *
75
+ * The input is assumed to be correct.
76
+ */
77
+ function trustedBase64Decode(base64) {
78
+ // This code is a bit sketchy due to the fact that we decode into a string, but it seems to
79
+ // work.
80
+ const binaryString = atob(base64);
81
+ const size = binaryString.length;
82
+ const bytes = new Uint8Array(size);
83
+ for (let i = 0; i < size; i++) {
84
+ bytes[i] = binaryString.charCodeAt(i);
85
+ }
86
+ return bytes;
87
+ }
@@ -0,0 +1,5 @@
1
+ import { SmoldotBytecode } from './public-types.js';
2
+ /**
3
+ * Compiles and returns the smoldot WebAssembly binary.
4
+ */
5
+ export declare function compileBytecode(): Promise<SmoldotBytecode>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ // Smoldot
3
+ // Copyright (C) 2023 Pierre Krieger
4
+ // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.compileBytecode = void 0;
16
+ // This program is free software: you can redistribute it and/or modify
17
+ // it under the terms of the GNU General Public License as published by
18
+ // the Free Software Foundation, either version 3 of the License, or
19
+ // (at your option) any later version.
20
+ // This program is distributed in the hope that it will be useful,
21
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ // GNU General Public License for more details.
24
+ // You should have received a copy of the GNU General Public License
25
+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
26
+ const wasm_js_1 = require("./internals/bytecode/wasm.js");
27
+ const pako_1 = require("pako");
28
+ /**
29
+ * Compiles and returns the smoldot WebAssembly binary.
30
+ */
31
+ function compileBytecode() {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ // The actual Wasm bytecode is base64-decoded then deflate-decoded from a constant found in a
34
+ // different file.
35
+ // This is suboptimal compared to using `instantiateStreaming`, but it is the most
36
+ // cross-platform cross-bundler approach.
37
+ return WebAssembly.compile((0, pako_1.inflate)(Buffer.from(wasm_js_1.default, 'base64')))
38
+ .then((m) => { return { wasm: m }; });
39
+ });
40
+ }
41
+ exports.compileBytecode = compileBytecode;
@@ -1,6 +1,6 @@
1
1
  /// <reference lib="dom" />
2
- import { Client, ClientOptions } from './client.js';
3
- export { AddChainError, AddChainOptions, AlreadyDestroyedError, Chain, Client, ClientOptions, CrashError, JsonRpcDisabledError, MalformedJsonRpcError, QueueFullError, LogCallback } from './client.js';
2
+ import { Client, ClientOptions } from './public-types.js';
3
+ export { AddChainError, AddChainOptions, AlreadyDestroyedError, Chain, Client, ClientOptions, ClientOptionsWithBytecode, SmoldotBytecode, CrashError, JsonRpcDisabledError, MalformedJsonRpcError, QueueFullError, LogCallback } from './public-types.js';
4
4
  /**
5
5
  * Initializes a new client. This is a pre-requisite to connecting to a blockchain.
6
6
  *