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.
- package/README.md +58 -0
- package/dist/cjs/bytecode-browser.d.ts +5 -0
- package/dist/cjs/bytecode-browser.js +42 -0
- package/dist/cjs/bytecode-deno.d.ts +5 -0
- package/dist/cjs/bytecode-deno.js +87 -0
- package/dist/cjs/bytecode-nodejs.d.ts +5 -0
- package/dist/cjs/bytecode-nodejs.js +41 -0
- package/dist/cjs/index-browser.d.ts +2 -2
- package/dist/cjs/index-browser.js +10 -507
- package/dist/cjs/index-deno.d.ts +2 -2
- package/dist/cjs/index-deno.js +10 -283
- package/dist/cjs/index-nodejs.d.ts +2 -2
- package/dist/cjs/index-nodejs.js +10 -211
- package/dist/cjs/internals/bytecode/wasm0.js +4 -0
- package/dist/cjs/internals/bytecode/wasm1.js +4 -0
- package/dist/cjs/internals/bytecode/wasm2.js +4 -0
- package/dist/cjs/{instance/bindings-smoldot-light.d.ts → internals/client.d.ts} +15 -34
- package/dist/cjs/internals/client.js +376 -0
- package/dist/cjs/internals/local-instance.d.ts +144 -0
- package/dist/cjs/internals/local-instance.js +566 -0
- package/dist/cjs/internals/remote-instance.d.ts +38 -0
- package/dist/cjs/internals/remote-instance.js +374 -0
- package/dist/cjs/no-auto-bytecode-browser.d.ts +11 -0
- package/dist/cjs/no-auto-bytecode-browser.js +481 -0
- package/dist/cjs/no-auto-bytecode-deno.d.ts +10 -0
- package/dist/cjs/no-auto-bytecode-deno.js +220 -0
- package/dist/cjs/no-auto-bytecode-nodejs.d.ts +10 -0
- package/dist/cjs/no-auto-bytecode-nodejs.js +189 -0
- package/dist/cjs/{client.d.ts → public-types.d.ts} +51 -3
- package/dist/cjs/public-types.js +76 -0
- package/dist/cjs/worker-browser.d.ts +11 -0
- package/dist/cjs/worker-browser.js +63 -0
- package/dist/cjs/worker-deno.d.ts +11 -0
- package/dist/cjs/worker-deno.js +54 -0
- package/dist/cjs/worker-nodejs.d.ts +11 -0
- package/dist/cjs/worker-nodejs.js +55 -0
- package/dist/mjs/bytecode-browser.d.ts +5 -0
- package/dist/mjs/bytecode-browser.js +38 -0
- package/dist/mjs/bytecode-deno.d.ts +5 -0
- package/dist/mjs/bytecode-deno.js +83 -0
- package/dist/mjs/bytecode-nodejs.d.ts +5 -0
- package/dist/mjs/bytecode-nodejs.js +37 -0
- package/dist/mjs/index-browser.d.ts +2 -2
- package/dist/mjs/index-browser.js +4 -501
- package/dist/mjs/index-deno.d.ts +2 -2
- package/dist/mjs/index-deno.js +4 -277
- package/dist/mjs/index-nodejs.d.ts +2 -2
- package/dist/mjs/index-nodejs.js +4 -205
- package/dist/mjs/internals/bytecode/wasm0.js +1 -0
- package/dist/mjs/internals/bytecode/wasm1.js +1 -0
- package/dist/mjs/internals/bytecode/wasm2.js +1 -0
- package/dist/mjs/{instance/bindings-smoldot-light.d.ts → internals/client.d.ts} +15 -34
- package/dist/mjs/internals/client.js +372 -0
- package/dist/mjs/internals/local-instance.d.ts +144 -0
- package/dist/mjs/internals/local-instance.js +561 -0
- package/dist/mjs/internals/remote-instance.d.ts +38 -0
- package/dist/mjs/internals/remote-instance.js +369 -0
- package/dist/mjs/no-auto-bytecode-browser.d.ts +11 -0
- package/dist/mjs/no-auto-bytecode-browser.js +471 -0
- package/dist/mjs/no-auto-bytecode-deno.d.ts +10 -0
- package/dist/mjs/no-auto-bytecode-deno.js +210 -0
- package/dist/mjs/no-auto-bytecode-nodejs.d.ts +10 -0
- package/dist/mjs/no-auto-bytecode-nodejs.js +179 -0
- package/dist/mjs/{client.d.ts → public-types.d.ts} +51 -3
- package/dist/mjs/public-types.js +67 -0
- package/dist/mjs/worker-browser.d.ts +11 -0
- package/dist/mjs/worker-browser.js +59 -0
- package/dist/mjs/worker-deno.d.ts +11 -0
- package/dist/mjs/worker-deno.js +50 -0
- package/dist/mjs/worker-nodejs.d.ts +11 -0
- package/dist/mjs/worker-nodejs.js +51 -0
- package/package.json +40 -8
- package/dist/cjs/client.js +0 -172
- package/dist/cjs/instance/autogen/wasm0.js +0 -4
- package/dist/cjs/instance/autogen/wasm1.js +0 -4
- package/dist/cjs/instance/autogen/wasm2.js +0 -4
- package/dist/cjs/instance/bindings-smoldot-light.js +0 -285
- package/dist/cjs/instance/bindings-wasi.d.ts +0 -27
- package/dist/cjs/instance/bindings-wasi.js +0 -172
- package/dist/cjs/instance/bindings.d.ts +0 -31
- package/dist/cjs/instance/bindings.js +0 -5
- package/dist/cjs/instance/instance.d.ts +0 -45
- package/dist/cjs/instance/instance.js +0 -277
- package/dist/cjs/instance/raw-instance.d.ts +0 -56
- package/dist/cjs/instance/raw-instance.js +0 -129
- package/dist/mjs/client.js +0 -162
- package/dist/mjs/instance/autogen/wasm0.js +0 -1
- package/dist/mjs/instance/autogen/wasm1.js +0 -1
- package/dist/mjs/instance/autogen/wasm2.js +0 -1
- package/dist/mjs/instance/bindings-smoldot-light.js +0 -280
- package/dist/mjs/instance/bindings-wasi.d.ts +0 -27
- package/dist/mjs/instance/bindings-wasi.js +0 -170
- package/dist/mjs/instance/bindings.d.ts +0 -31
- package/dist/mjs/instance/bindings.js +0 -4
- package/dist/mjs/instance/instance.d.ts +0 -45
- package/dist/mjs/instance/instance.js +0 -269
- package/dist/mjs/instance/raw-instance.d.ts +0 -56
- package/dist/mjs/instance/raw-instance.js +0 -124
- /package/dist/cjs/{base64.d.ts → internals/base64.d.ts} +0 -0
- /package/dist/cjs/{base64.js → internals/base64.js} +0 -0
- /package/dist/cjs/{instance → internals}/buffer.d.ts +0 -0
- /package/dist/cjs/{instance → internals}/buffer.js +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm.d.ts +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm.js +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm0.d.ts +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm1.d.ts +0 -0
- /package/dist/cjs/{instance/autogen → internals/bytecode}/wasm2.d.ts +0 -0
- /package/dist/mjs/{base64.d.ts → internals/base64.d.ts} +0 -0
- /package/dist/mjs/{base64.js → internals/base64.js} +0 -0
- /package/dist/mjs/{instance → internals}/buffer.d.ts +0 -0
- /package/dist/mjs/{instance → internals}/buffer.js +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm.d.ts +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm.js +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm0.d.ts +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm1.d.ts +0 -0
- /package/dist/mjs/{instance/autogen → internals/bytecode}/wasm2.d.ts +0 -0
package/dist/cjs/index-deno.js
CHANGED
|
@@ -2,37 +2,17 @@
|
|
|
2
2
|
// Smoldot
|
|
3
3
|
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
|
|
4
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
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
6
|
exports.start = exports.JsonRpcDisabledError = exports.QueueFullError = exports.MalformedJsonRpcError = exports.CrashError = exports.AlreadyDestroyedError = exports.AddChainError = void 0;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
26
|
-
const client_js_1 = require("./client.js");
|
|
27
|
-
const instance_js_1 = require("./instance/instance.js");
|
|
28
|
-
const wasm_js_1 = require("./instance/autogen/wasm.js");
|
|
29
|
-
var client_js_2 = require("./client.js");
|
|
30
|
-
Object.defineProperty(exports, "AddChainError", { enumerable: true, get: function () { return client_js_2.AddChainError; } });
|
|
31
|
-
Object.defineProperty(exports, "AlreadyDestroyedError", { enumerable: true, get: function () { return client_js_2.AlreadyDestroyedError; } });
|
|
32
|
-
Object.defineProperty(exports, "CrashError", { enumerable: true, get: function () { return client_js_2.CrashError; } });
|
|
33
|
-
Object.defineProperty(exports, "MalformedJsonRpcError", { enumerable: true, get: function () { return client_js_2.MalformedJsonRpcError; } });
|
|
34
|
-
Object.defineProperty(exports, "QueueFullError", { enumerable: true, get: function () { return client_js_2.QueueFullError; } });
|
|
35
|
-
Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get: function () { return client_js_2.JsonRpcDisabledError; } });
|
|
7
|
+
const no_auto_bytecode_deno_js_1 = require("./no-auto-bytecode-deno.js");
|
|
8
|
+
const bytecode_deno_js_1 = require("./bytecode-deno.js");
|
|
9
|
+
var public_types_js_1 = require("./public-types.js");
|
|
10
|
+
Object.defineProperty(exports, "AddChainError", { enumerable: true, get: function () { return public_types_js_1.AddChainError; } });
|
|
11
|
+
Object.defineProperty(exports, "AlreadyDestroyedError", { enumerable: true, get: function () { return public_types_js_1.AlreadyDestroyedError; } });
|
|
12
|
+
Object.defineProperty(exports, "CrashError", { enumerable: true, get: function () { return public_types_js_1.CrashError; } });
|
|
13
|
+
Object.defineProperty(exports, "MalformedJsonRpcError", { enumerable: true, get: function () { return public_types_js_1.MalformedJsonRpcError; } });
|
|
14
|
+
Object.defineProperty(exports, "QueueFullError", { enumerable: true, get: function () { return public_types_js_1.QueueFullError; } });
|
|
15
|
+
Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get: function () { return public_types_js_1.JsonRpcDisabledError; } });
|
|
36
16
|
/**
|
|
37
17
|
* Initializes a new client. This is a pre-requisite to connecting to a blockchain.
|
|
38
18
|
*
|
|
@@ -42,259 +22,6 @@ Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get:
|
|
|
42
22
|
*/
|
|
43
23
|
function start(options) {
|
|
44
24
|
options = options || {};
|
|
45
|
-
|
|
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, {
|
|
51
|
-
registerShouldPeriodicallyYield: (_callback) => {
|
|
52
|
-
return [true, () => { }];
|
|
53
|
-
},
|
|
54
|
-
performanceNow: () => {
|
|
55
|
-
return performance.now();
|
|
56
|
-
},
|
|
57
|
-
getRandomValues: (buffer) => {
|
|
58
|
-
const crypto = globalThis.crypto;
|
|
59
|
-
if (!crypto)
|
|
60
|
-
throw new Error('randomness not available');
|
|
61
|
-
crypto.getRandomValues(buffer);
|
|
62
|
-
},
|
|
63
|
-
connect: (config) => {
|
|
64
|
-
return connect(config, (options === null || options === void 0 ? void 0 : options.forbidTcp) || false, (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);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
25
|
+
return (0, no_auto_bytecode_deno_js_1.startWithBytecode)(Object.assign({ bytecode: (0, bytecode_deno_js_1.compileBytecode)() }, options));
|
|
67
26
|
}
|
|
68
27
|
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
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Decodes a base64 string.
|
|
101
|
-
*
|
|
102
|
-
* The input is assumed to be correct.
|
|
103
|
-
*/
|
|
104
|
-
function trustedBase64Decode(base64) {
|
|
105
|
-
// This code is a bit sketchy due to the fact that we decode into a string, but it seems to
|
|
106
|
-
// work.
|
|
107
|
-
const binaryString = atob(base64);
|
|
108
|
-
const size = binaryString.length;
|
|
109
|
-
const bytes = new Uint8Array(size);
|
|
110
|
-
for (let i = 0; i < size; i++) {
|
|
111
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
112
|
-
}
|
|
113
|
-
return bytes;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Tries to open a new connection using the given configuration.
|
|
117
|
-
*
|
|
118
|
-
* @see Connection
|
|
119
|
-
* @throws {@link ConnectionError} If the multiaddress couldn't be parsed or contains an invalid protocol.
|
|
120
|
-
*/
|
|
121
|
-
function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
|
|
122
|
-
// Attempt to parse the multiaddress.
|
|
123
|
-
// TODO: remove support for `/wss` in a long time (https://github.com/paritytech/smoldot/issues/1940)
|
|
124
|
-
const wsParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)\/(ws|wss|tls\/ws)$/);
|
|
125
|
-
const tcpParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)$/);
|
|
126
|
-
if (wsParsed != null) {
|
|
127
|
-
const proto = (wsParsed[4] == 'ws') ? 'ws' : 'wss';
|
|
128
|
-
if ((proto == 'ws' && forbidWs) ||
|
|
129
|
-
(proto == 'ws' && wsParsed[2] != 'localhost' && wsParsed[2] != '127.0.0.1' && forbidNonLocalWs) ||
|
|
130
|
-
(proto == 'wss' && forbidWss)) {
|
|
131
|
-
throw new instance_js_1.ConnectionError('Connection type not allowed');
|
|
132
|
-
}
|
|
133
|
-
const url = (wsParsed[1] == 'ip6') ?
|
|
134
|
-
(proto + "://[" + wsParsed[2] + "]:" + wsParsed[3]) :
|
|
135
|
-
(proto + "://" + wsParsed[2] + ":" + wsParsed[3]);
|
|
136
|
-
const socket = new WebSocket(url);
|
|
137
|
-
socket.binaryType = 'arraybuffer';
|
|
138
|
-
const bufferedAmountCheck = { quenedUnreportedBytes: 0, nextTimeout: 10 };
|
|
139
|
-
const checkBufferedAmount = () => {
|
|
140
|
-
if (socket.readyState != 1)
|
|
141
|
-
return;
|
|
142
|
-
// Note that we might expect `bufferedAmount` to always be <= the sum of the lengths
|
|
143
|
-
// of all the data that has been sent, but that might not be the case. For this
|
|
144
|
-
// reason, we use `bufferedAmount` as a hint rather than a correct value.
|
|
145
|
-
const bufferedAmount = socket.bufferedAmount;
|
|
146
|
-
let wasSent = bufferedAmountCheck.quenedUnreportedBytes - bufferedAmount;
|
|
147
|
-
if (wasSent < 0)
|
|
148
|
-
wasSent = 0;
|
|
149
|
-
bufferedAmountCheck.quenedUnreportedBytes -= wasSent;
|
|
150
|
-
if (bufferedAmountCheck.quenedUnreportedBytes != 0) {
|
|
151
|
-
setTimeout(checkBufferedAmount, bufferedAmountCheck.nextTimeout);
|
|
152
|
-
bufferedAmountCheck.nextTimeout *= 2;
|
|
153
|
-
if (bufferedAmountCheck.nextTimeout > 500)
|
|
154
|
-
bufferedAmountCheck.nextTimeout = 500;
|
|
155
|
-
}
|
|
156
|
-
// Note: it is important to call `onWritableBytes` at the very end, as it might
|
|
157
|
-
// trigger a call to `send`.
|
|
158
|
-
if (wasSent != 0)
|
|
159
|
-
config.onWritableBytes(wasSent);
|
|
160
|
-
};
|
|
161
|
-
socket.onopen = () => {
|
|
162
|
-
config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux', initialWritableBytes: 1024 * 1024, writeClosable: false });
|
|
163
|
-
};
|
|
164
|
-
socket.onclose = (event) => {
|
|
165
|
-
const message = "Error code " + event.code + (!!event.reason ? (": " + event.reason) : "");
|
|
166
|
-
config.onConnectionReset(message);
|
|
167
|
-
};
|
|
168
|
-
socket.onmessage = (msg) => {
|
|
169
|
-
config.onMessage(new Uint8Array(msg.data));
|
|
170
|
-
};
|
|
171
|
-
return {
|
|
172
|
-
reset: () => {
|
|
173
|
-
// We can't set these fields to null because the TypeScript definitions don't
|
|
174
|
-
// allow it, but we can set them to dummy values.
|
|
175
|
-
socket.onopen = () => { };
|
|
176
|
-
socket.onclose = () => { };
|
|
177
|
-
socket.onmessage = () => { };
|
|
178
|
-
socket.onerror = () => { };
|
|
179
|
-
socket.close();
|
|
180
|
-
},
|
|
181
|
-
send: (data) => {
|
|
182
|
-
// The WebSocket library that we use seems to spontaneously transition connections
|
|
183
|
-
// to the "closed" state but not call the `onclosed` callback immediately. Calling
|
|
184
|
-
// `send` on that object throws an exception. In order to avoid panicking smoldot,
|
|
185
|
-
// we thus absorb any exception thrown here.
|
|
186
|
-
// See also <https://github.com/paritytech/smoldot/issues/2937>.
|
|
187
|
-
try {
|
|
188
|
-
socket.send(data);
|
|
189
|
-
if (bufferedAmountCheck.quenedUnreportedBytes == 0) {
|
|
190
|
-
bufferedAmountCheck.nextTimeout = 10;
|
|
191
|
-
setTimeout(checkBufferedAmount, 10);
|
|
192
|
-
}
|
|
193
|
-
bufferedAmountCheck.quenedUnreportedBytes += data.length;
|
|
194
|
-
}
|
|
195
|
-
catch (_error) { }
|
|
196
|
-
},
|
|
197
|
-
closeSend: () => { throw new Error('Wrong connection type'); },
|
|
198
|
-
openOutSubstream: () => { throw new Error('Wrong connection type'); }
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
else if (tcpParsed != null) {
|
|
202
|
-
if (forbidTcp) {
|
|
203
|
-
throw new instance_js_1.ConnectionError('TCP connections not available');
|
|
204
|
-
}
|
|
205
|
-
const socket = {
|
|
206
|
-
destroyed: false,
|
|
207
|
-
inner: Deno.connect({
|
|
208
|
-
hostname: tcpParsed[2],
|
|
209
|
-
port: parseInt(tcpParsed[3], 10),
|
|
210
|
-
}).catch((error) => {
|
|
211
|
-
socket.destroyed = true;
|
|
212
|
-
config.onConnectionReset(error.toString());
|
|
213
|
-
return null;
|
|
214
|
-
})
|
|
215
|
-
};
|
|
216
|
-
socket.inner = socket.inner.then((established) => {
|
|
217
|
-
// TODO: at the time of writing of this comment, `setNoDelay` is still unstable
|
|
218
|
-
//established.setNoDelay();
|
|
219
|
-
if (socket.destroyed)
|
|
220
|
-
return established;
|
|
221
|
-
config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux', initialWritableBytes: 1024 * 1024, writeClosable: true });
|
|
222
|
-
// Spawns an asynchronous task that continuously reads from the socket.
|
|
223
|
-
// Every time data is read, the task re-executes itself in order to continue reading.
|
|
224
|
-
// The task ends automatically if an EOF or error is detected, which should also happen
|
|
225
|
-
// if the user calls `close()`.
|
|
226
|
-
const read = (readBuffer) => __awaiter(this, void 0, void 0, function* () {
|
|
227
|
-
if (socket.destroyed || established === null)
|
|
228
|
-
return;
|
|
229
|
-
let outcome = null;
|
|
230
|
-
try {
|
|
231
|
-
outcome = yield established.read(readBuffer);
|
|
232
|
-
}
|
|
233
|
-
catch (error) {
|
|
234
|
-
// The type of `error` is unclear, but we assume that it implements `Error`
|
|
235
|
-
outcome = error.toString();
|
|
236
|
-
}
|
|
237
|
-
if (socket.destroyed)
|
|
238
|
-
return;
|
|
239
|
-
if (typeof outcome !== 'number' || outcome === null) {
|
|
240
|
-
// The socket is reported closed, but `socket.destroyed` is still `false` (see
|
|
241
|
-
// check above). As such, we must inform the inner layers.
|
|
242
|
-
socket.destroyed = true;
|
|
243
|
-
config.onConnectionReset(outcome === null ? "EOF when reading socket" : outcome);
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
console.assert(outcome !== 0); // `read` guarantees to return a non-zero value.
|
|
247
|
-
config.onMessage(readBuffer.slice(0, outcome));
|
|
248
|
-
return read(readBuffer);
|
|
249
|
-
});
|
|
250
|
-
read(new Uint8Array(32768));
|
|
251
|
-
return established;
|
|
252
|
-
});
|
|
253
|
-
return {
|
|
254
|
-
reset: () => {
|
|
255
|
-
socket.destroyed = true;
|
|
256
|
-
socket.inner.then((connec) => connec.close());
|
|
257
|
-
},
|
|
258
|
-
send: (data) => {
|
|
259
|
-
let dataCopy = Uint8Array.from(data); // Deep copy of the data
|
|
260
|
-
socket.inner = socket.inner.then((c) => __awaiter(this, void 0, void 0, function* () {
|
|
261
|
-
while (dataCopy.length > 0) {
|
|
262
|
-
if (socket.destroyed || c === null)
|
|
263
|
-
return c;
|
|
264
|
-
let outcome;
|
|
265
|
-
try {
|
|
266
|
-
outcome = yield c.write(dataCopy);
|
|
267
|
-
config.onWritableBytes(dataCopy.length);
|
|
268
|
-
}
|
|
269
|
-
catch (error) {
|
|
270
|
-
// The type of `error` is unclear, but we assume that it implements `Error`
|
|
271
|
-
outcome = error.toString();
|
|
272
|
-
}
|
|
273
|
-
if (typeof outcome !== 'number') {
|
|
274
|
-
// The socket is reported closed, but `socket.destroyed` is still
|
|
275
|
-
// `false` (see check above). As such, we must inform the inner layers.
|
|
276
|
-
socket.destroyed = true;
|
|
277
|
-
config.onConnectionReset(outcome);
|
|
278
|
-
return c;
|
|
279
|
-
}
|
|
280
|
-
// Note that, contrary to `read`, it is possible for `outcome` to be 0.
|
|
281
|
-
// This happen if the write had to be interrupted, and the only thing
|
|
282
|
-
// we have to do is try writing again.
|
|
283
|
-
dataCopy = dataCopy.slice(outcome);
|
|
284
|
-
}
|
|
285
|
-
return c;
|
|
286
|
-
}));
|
|
287
|
-
},
|
|
288
|
-
closeSend: () => {
|
|
289
|
-
socket.inner = socket.inner.then((c) => __awaiter(this, void 0, void 0, function* () {
|
|
290
|
-
yield (c === null || c === void 0 ? void 0 : c.closeWrite());
|
|
291
|
-
return c;
|
|
292
|
-
}));
|
|
293
|
-
},
|
|
294
|
-
openOutSubstream: () => { throw new Error('Wrong connection type'); }
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
else {
|
|
298
|
-
throw new instance_js_1.ConnectionError('Unrecognized multiaddr format');
|
|
299
|
-
}
|
|
300
|
-
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Client, ClientOptions } from './
|
|
2
|
-
export { AddChainError, AddChainOptions, AlreadyDestroyedError, Chain, Client, ClientOptions, CrashError, MalformedJsonRpcError, QueueFullError, JsonRpcDisabledError, LogCallback } from './
|
|
1
|
+
import { Client, ClientOptions } from './public-types.js';
|
|
2
|
+
export { AddChainError, AddChainOptions, AlreadyDestroyedError, Chain, Client, ClientOptions, ClientOptionsWithBytecode, SmoldotBytecode, CrashError, MalformedJsonRpcError, QueueFullError, JsonRpcDisabledError, LogCallback } from './public-types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Initializes a new client. This is a pre-requisite to connecting to a blockchain.
|
|
5
5
|
*
|
package/dist/cjs/index-nodejs.js
CHANGED
|
@@ -4,34 +4,15 @@
|
|
|
4
4
|
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.start = exports.JsonRpcDisabledError = exports.QueueFullError = exports.MalformedJsonRpcError = exports.CrashError = exports.AlreadyDestroyedError = exports.AddChainError = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
-
// Note: if you modify these imports, please test both the ModuleJS and CommonJS generated
|
|
18
|
-
// bindings. JavaScript being JavaScript, some libraries (such as `websocket`) have issues working
|
|
19
|
-
// with both at the same time.
|
|
20
|
-
const client_js_1 = require("./client.js");
|
|
21
|
-
const instance_js_1 = require("./instance/instance.js");
|
|
22
|
-
const wasm_js_1 = require("./instance/autogen/wasm.js");
|
|
23
|
-
const ws_1 = require("ws");
|
|
24
|
-
const pako_1 = require("pako");
|
|
25
|
-
const node_perf_hooks_1 = require("node:perf_hooks");
|
|
26
|
-
const node_net_1 = require("node:net");
|
|
27
|
-
const node_crypto_1 = require("node:crypto");
|
|
28
|
-
var client_js_2 = require("./client.js");
|
|
29
|
-
Object.defineProperty(exports, "AddChainError", { enumerable: true, get: function () { return client_js_2.AddChainError; } });
|
|
30
|
-
Object.defineProperty(exports, "AlreadyDestroyedError", { enumerable: true, get: function () { return client_js_2.AlreadyDestroyedError; } });
|
|
31
|
-
Object.defineProperty(exports, "CrashError", { enumerable: true, get: function () { return client_js_2.CrashError; } });
|
|
32
|
-
Object.defineProperty(exports, "MalformedJsonRpcError", { enumerable: true, get: function () { return client_js_2.MalformedJsonRpcError; } });
|
|
33
|
-
Object.defineProperty(exports, "QueueFullError", { enumerable: true, get: function () { return client_js_2.QueueFullError; } });
|
|
34
|
-
Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get: function () { return client_js_2.JsonRpcDisabledError; } });
|
|
7
|
+
const no_auto_bytecode_nodejs_js_1 = require("./no-auto-bytecode-nodejs.js");
|
|
8
|
+
const bytecode_nodejs_js_1 = require("./bytecode-nodejs.js");
|
|
9
|
+
var public_types_js_1 = require("./public-types.js");
|
|
10
|
+
Object.defineProperty(exports, "AddChainError", { enumerable: true, get: function () { return public_types_js_1.AddChainError; } });
|
|
11
|
+
Object.defineProperty(exports, "AlreadyDestroyedError", { enumerable: true, get: function () { return public_types_js_1.AlreadyDestroyedError; } });
|
|
12
|
+
Object.defineProperty(exports, "CrashError", { enumerable: true, get: function () { return public_types_js_1.CrashError; } });
|
|
13
|
+
Object.defineProperty(exports, "MalformedJsonRpcError", { enumerable: true, get: function () { return public_types_js_1.MalformedJsonRpcError; } });
|
|
14
|
+
Object.defineProperty(exports, "QueueFullError", { enumerable: true, get: function () { return public_types_js_1.QueueFullError; } });
|
|
15
|
+
Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get: function () { return public_types_js_1.JsonRpcDisabledError; } });
|
|
35
16
|
/**
|
|
36
17
|
* Initializes a new client. This is a pre-requisite to connecting to a blockchain.
|
|
37
18
|
*
|
|
@@ -41,188 +22,6 @@ Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get:
|
|
|
41
22
|
*/
|
|
42
23
|
function start(options) {
|
|
43
24
|
options = options || {};
|
|
44
|
-
|
|
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, {
|
|
50
|
-
registerShouldPeriodicallyYield: (_callback) => {
|
|
51
|
-
return [true, () => { }];
|
|
52
|
-
},
|
|
53
|
-
performanceNow: () => {
|
|
54
|
-
return node_perf_hooks_1.performance.now();
|
|
55
|
-
},
|
|
56
|
-
getRandomValues: (buffer) => {
|
|
57
|
-
if (buffer.length >= 1024 * 1024)
|
|
58
|
-
throw new Error('getRandomValues buffer too large');
|
|
59
|
-
(0, node_crypto_1.randomFillSync)(buffer);
|
|
60
|
-
},
|
|
61
|
-
connect: (config) => {
|
|
62
|
-
return connect(config, (options === null || options === void 0 ? void 0 : options.forbidTcp) || false, (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);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
25
|
+
return (0, no_auto_bytecode_nodejs_js_1.startWithBytecode)(Object.assign({ bytecode: (0, bytecode_nodejs_js_1.compileBytecode)() }, options));
|
|
65
26
|
}
|
|
66
27
|
exports.start = start;
|
|
67
|
-
/**
|
|
68
|
-
* Tries to open a new connection using the given configuration.
|
|
69
|
-
*
|
|
70
|
-
* @see Connection
|
|
71
|
-
* @throws {@link ConnectionError} If the multiaddress couldn't be parsed or contains an invalid protocol.
|
|
72
|
-
*/
|
|
73
|
-
function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
|
|
74
|
-
// Attempt to parse the multiaddress.
|
|
75
|
-
// TODO: remove support for `/wss` in a long time (https://github.com/paritytech/smoldot/issues/1940)
|
|
76
|
-
const wsParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)\/(ws|wss|tls\/ws)$/);
|
|
77
|
-
const tcpParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)$/);
|
|
78
|
-
if (wsParsed != null) {
|
|
79
|
-
const proto = (wsParsed[4] == 'ws') ? 'ws' : 'wss';
|
|
80
|
-
if ((proto == 'ws' && forbidWs) ||
|
|
81
|
-
(proto == 'ws' && wsParsed[2] != 'localhost' && wsParsed[2] != '127.0.0.1' && forbidNonLocalWs) ||
|
|
82
|
-
(proto == 'wss' && forbidWss)) {
|
|
83
|
-
throw new instance_js_1.ConnectionError('Connection type not allowed');
|
|
84
|
-
}
|
|
85
|
-
const url = (wsParsed[1] == 'ip6') ?
|
|
86
|
-
(proto + "://[" + wsParsed[2] + "]:" + wsParsed[3]) :
|
|
87
|
-
(proto + "://" + wsParsed[2] + ":" + wsParsed[3]);
|
|
88
|
-
const socket = new ws_1.WebSocket(url);
|
|
89
|
-
socket.binaryType = 'arraybuffer';
|
|
90
|
-
const bufferedAmountCheck = { quenedUnreportedBytes: 0, nextTimeout: 10 };
|
|
91
|
-
const checkBufferedAmount = () => {
|
|
92
|
-
if (socket.readyState != 1)
|
|
93
|
-
return;
|
|
94
|
-
// Note that we might expect `bufferedAmount` to always be <= the sum of the lengths
|
|
95
|
-
// of all the data that has been sent, but that seems to not be the case. It is
|
|
96
|
-
// unclear whether this is intended or a bug, but is is likely that `bufferedAmount`
|
|
97
|
-
// also includes WebSocket headers. For this reason, we use `bufferedAmount` as a hint
|
|
98
|
-
// rather than a correct value.
|
|
99
|
-
const bufferedAmount = socket.bufferedAmount;
|
|
100
|
-
let wasSent = bufferedAmountCheck.quenedUnreportedBytes - bufferedAmount;
|
|
101
|
-
if (wasSent < 0)
|
|
102
|
-
wasSent = 0;
|
|
103
|
-
bufferedAmountCheck.quenedUnreportedBytes -= wasSent;
|
|
104
|
-
if (bufferedAmountCheck.quenedUnreportedBytes != 0) {
|
|
105
|
-
setTimeout(checkBufferedAmount, bufferedAmountCheck.nextTimeout);
|
|
106
|
-
bufferedAmountCheck.nextTimeout *= 2;
|
|
107
|
-
if (bufferedAmountCheck.nextTimeout > 500)
|
|
108
|
-
bufferedAmountCheck.nextTimeout = 500;
|
|
109
|
-
}
|
|
110
|
-
// Note: it is important to call `onWritableBytes` at the very end, as it might
|
|
111
|
-
// trigger a call to `send`.
|
|
112
|
-
if (wasSent != 0)
|
|
113
|
-
config.onWritableBytes(wasSent);
|
|
114
|
-
};
|
|
115
|
-
socket.onopen = () => {
|
|
116
|
-
config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux', initialWritableBytes: 1024 * 1024, writeClosable: false });
|
|
117
|
-
};
|
|
118
|
-
socket.onclose = (event) => {
|
|
119
|
-
const message = "Error code " + event.code + (!!event.reason ? (": " + event.reason) : "");
|
|
120
|
-
config.onConnectionReset(message);
|
|
121
|
-
socket.onopen = () => { };
|
|
122
|
-
socket.onclose = () => { };
|
|
123
|
-
socket.onmessage = () => { };
|
|
124
|
-
socket.onerror = () => { };
|
|
125
|
-
};
|
|
126
|
-
socket.onerror = (event) => {
|
|
127
|
-
config.onConnectionReset(event.message);
|
|
128
|
-
socket.onopen = () => { };
|
|
129
|
-
socket.onclose = () => { };
|
|
130
|
-
socket.onmessage = () => { };
|
|
131
|
-
socket.onerror = () => { };
|
|
132
|
-
};
|
|
133
|
-
socket.onmessage = (msg) => {
|
|
134
|
-
config.onMessage(new Uint8Array(msg.data));
|
|
135
|
-
};
|
|
136
|
-
return {
|
|
137
|
-
reset: () => {
|
|
138
|
-
// We can't set these fields to null because the TypeScript definitions don't
|
|
139
|
-
// allow it, but we can set them to dummy values.
|
|
140
|
-
socket.onopen = () => { };
|
|
141
|
-
socket.onclose = () => { };
|
|
142
|
-
socket.onmessage = () => { };
|
|
143
|
-
socket.onerror = () => { };
|
|
144
|
-
socket.close();
|
|
145
|
-
},
|
|
146
|
-
send: (data) => {
|
|
147
|
-
socket.send(data);
|
|
148
|
-
if (bufferedAmountCheck.quenedUnreportedBytes == 0) {
|
|
149
|
-
bufferedAmountCheck.nextTimeout = 10;
|
|
150
|
-
setTimeout(checkBufferedAmount, 10);
|
|
151
|
-
}
|
|
152
|
-
bufferedAmountCheck.quenedUnreportedBytes += data.length;
|
|
153
|
-
},
|
|
154
|
-
closeSend: () => { throw new Error('Wrong connection type'); },
|
|
155
|
-
openOutSubstream: () => { throw new Error('Wrong connection type'); }
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
else if (tcpParsed != null) {
|
|
159
|
-
// `net` module will be missing when we're not in NodeJS.
|
|
160
|
-
if (forbidTcp) {
|
|
161
|
-
throw new instance_js_1.ConnectionError('TCP connections not available');
|
|
162
|
-
}
|
|
163
|
-
const socket = (0, node_net_1.createConnection)({
|
|
164
|
-
host: tcpParsed[2],
|
|
165
|
-
port: parseInt(tcpParsed[3], 10),
|
|
166
|
-
});
|
|
167
|
-
// Number of bytes queued using `socket.write` and where `write` has returned false.
|
|
168
|
-
const drainingBytes = { num: 0 };
|
|
169
|
-
socket.setNoDelay();
|
|
170
|
-
socket.on('connect', () => {
|
|
171
|
-
if (socket.destroyed)
|
|
172
|
-
return;
|
|
173
|
-
config.onOpen({
|
|
174
|
-
type: 'single-stream', handshake: 'multistream-select-noise-yamux',
|
|
175
|
-
initialWritableBytes: socket.writableHighWaterMark, writeClosable: true
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
socket.on('close', (hasError) => {
|
|
179
|
-
if (socket.destroyed)
|
|
180
|
-
return;
|
|
181
|
-
// NodeJS doesn't provide a reason why the closing happened, but only
|
|
182
|
-
// whether it was caused by an error.
|
|
183
|
-
const message = hasError ? "Error" : "Closed gracefully";
|
|
184
|
-
config.onConnectionReset(message);
|
|
185
|
-
});
|
|
186
|
-
socket.on('error', () => { });
|
|
187
|
-
socket.on('data', (message) => {
|
|
188
|
-
if (socket.destroyed)
|
|
189
|
-
return;
|
|
190
|
-
config.onMessage(new Uint8Array(message.buffer));
|
|
191
|
-
});
|
|
192
|
-
socket.on('drain', () => {
|
|
193
|
-
// The bytes queued using `socket.write` and where `write` has returned false have now
|
|
194
|
-
// been sent. Notify the API that it can write more data.
|
|
195
|
-
if (socket.destroyed)
|
|
196
|
-
return;
|
|
197
|
-
const val = drainingBytes.num;
|
|
198
|
-
drainingBytes.num = 0;
|
|
199
|
-
config.onWritableBytes(val);
|
|
200
|
-
});
|
|
201
|
-
return {
|
|
202
|
-
reset: () => {
|
|
203
|
-
socket.destroy();
|
|
204
|
-
},
|
|
205
|
-
send: (data) => {
|
|
206
|
-
const dataLen = data.length;
|
|
207
|
-
const allWritten = socket.write(data);
|
|
208
|
-
if (allWritten) {
|
|
209
|
-
setImmediate(() => {
|
|
210
|
-
if (!socket.writable)
|
|
211
|
-
return;
|
|
212
|
-
config.onWritableBytes(dataLen);
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
drainingBytes.num += dataLen;
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
closeSend: () => {
|
|
220
|
-
socket.end();
|
|
221
|
-
},
|
|
222
|
-
openOutSubstream: () => { throw new Error('Wrong connection type'); }
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
throw new instance_js_1.ConnectionError('Unrecognized multiaddr format');
|
|
227
|
-
}
|
|
228
|
-
}
|