smoldot 0.3.6 → 0.7.11

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 (74) hide show
  1. package/README.md +18 -21
  2. package/dist/cjs/base64.d.ts +22 -0
  3. package/dist/cjs/base64.js +104 -0
  4. package/dist/cjs/client.d.ts +287 -0
  5. package/dist/cjs/client.js +175 -0
  6. package/dist/cjs/index-browser.d.ts +11 -0
  7. package/dist/cjs/index-browser.js +465 -0
  8. package/dist/cjs/index-deno.d.ts +10 -0
  9. package/dist/cjs/index-deno.js +263 -0
  10. package/dist/cjs/index-nodejs.d.ts +10 -0
  11. package/dist/cjs/index-nodejs.js +174 -0
  12. package/dist/cjs/instance/autogen/wasm.d.ts +2 -0
  13. package/dist/cjs/instance/autogen/wasm.js +6 -0
  14. package/dist/cjs/instance/autogen/wasm0.d.ts +1 -0
  15. package/dist/cjs/instance/autogen/wasm0.js +4 -0
  16. package/dist/cjs/instance/autogen/wasm1.d.ts +1 -0
  17. package/dist/cjs/instance/autogen/wasm1.js +4 -0
  18. package/dist/cjs/instance/autogen/wasm2.d.ts +1 -0
  19. package/dist/cjs/instance/autogen/wasm2.js +4 -0
  20. package/dist/cjs/instance/bindings-smoldot-light.d.ts +154 -0
  21. package/dist/cjs/instance/bindings-smoldot-light.js +266 -0
  22. package/dist/cjs/instance/bindings-wasi.d.ts +23 -0
  23. package/dist/cjs/instance/bindings-wasi.js +142 -0
  24. package/dist/cjs/instance/bindings.d.ts +31 -0
  25. package/dist/cjs/instance/bindings.js +5 -0
  26. package/dist/cjs/instance/buffer.d.ts +10 -0
  27. package/dist/cjs/instance/buffer.js +53 -0
  28. package/dist/cjs/instance/instance.d.ts +45 -0
  29. package/dist/cjs/instance/instance.js +301 -0
  30. package/dist/cjs/instance/raw-instance.d.ts +65 -0
  31. package/dist/cjs/instance/raw-instance.js +72 -0
  32. package/dist/cjs/package.json +3 -0
  33. package/dist/mjs/base64.d.ts +22 -0
  34. package/dist/mjs/base64.js +97 -0
  35. package/dist/mjs/client.d.ts +287 -0
  36. package/dist/mjs/client.js +165 -0
  37. package/dist/mjs/index-browser.d.ts +11 -0
  38. package/dist/mjs/index-browser.js +455 -0
  39. package/dist/mjs/index-deno.d.ts +10 -0
  40. package/dist/mjs/index-deno.js +253 -0
  41. package/dist/mjs/index-nodejs.d.ts +10 -0
  42. package/dist/mjs/index-nodejs.js +164 -0
  43. package/dist/mjs/instance/autogen/wasm.d.ts +2 -0
  44. package/dist/mjs/instance/autogen/wasm.js +4 -0
  45. package/dist/mjs/instance/autogen/wasm0.d.ts +1 -0
  46. package/dist/mjs/instance/autogen/wasm0.js +1 -0
  47. package/dist/mjs/instance/autogen/wasm1.d.ts +1 -0
  48. package/dist/mjs/instance/autogen/wasm1.js +1 -0
  49. package/dist/mjs/instance/autogen/wasm2.d.ts +1 -0
  50. package/dist/mjs/instance/autogen/wasm2.js +1 -0
  51. package/dist/mjs/instance/bindings-smoldot-light.d.ts +154 -0
  52. package/dist/mjs/instance/bindings-smoldot-light.js +261 -0
  53. package/dist/mjs/instance/bindings-wasi.d.ts +23 -0
  54. package/{src → dist/mjs/instance}/bindings-wasi.js +47 -58
  55. package/dist/mjs/instance/bindings.d.ts +31 -0
  56. package/dist/mjs/instance/bindings.js +4 -0
  57. package/dist/mjs/instance/buffer.d.ts +10 -0
  58. package/dist/mjs/instance/buffer.js +46 -0
  59. package/dist/mjs/instance/instance.d.ts +45 -0
  60. package/dist/mjs/instance/instance.js +293 -0
  61. package/dist/mjs/instance/raw-instance.d.ts +65 -0
  62. package/dist/mjs/instance/raw-instance.js +67 -0
  63. package/dist/mjs/package.json +3 -0
  64. package/package.json +35 -19
  65. package/src/autogen/wasm.js +0 -1
  66. package/src/bindings-smoldot-js.js +0 -223
  67. package/src/compat-browser.js +0 -25
  68. package/src/compat-nodejs.js +0 -30
  69. package/src/health.js +0 -206
  70. package/src/index.d.ts +0 -68
  71. package/src/index.js +0 -192
  72. package/src/index.test-d.ts +0 -36
  73. package/src/tsconfig.json +0 -13
  74. package/src/worker.js +0 -151
@@ -0,0 +1,263 @@
1
+ "use strict";
2
+ // Smoldot
3
+ // Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
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.start = exports.JsonRpcDisabledError = exports.QueueFullError = exports.MalformedJsonRpcError = exports.CrashError = exports.AlreadyDestroyedError = exports.AddChainError = 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 client_js_1 = require("./client.js");
27
+ const instance_js_1 = require("./instance/instance.js");
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; } });
35
+ /**
36
+ * Initializes a new client. This is a pre-requisite to connecting to a blockchain.
37
+ *
38
+ * Can never fail.
39
+ *
40
+ * @param options Configuration of the client. Defaults to `{}`.
41
+ */
42
+ function start(options) {
43
+ 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
+ }),
71
+ registerShouldPeriodicallyYield: (_callback) => {
72
+ return [true, () => { }];
73
+ },
74
+ performanceNow: () => {
75
+ return performance.now();
76
+ },
77
+ getRandomValues: (buffer) => {
78
+ const crypto = globalThis.crypto;
79
+ if (!crypto)
80
+ throw new Error('randomness not available');
81
+ crypto.getRandomValues(buffer);
82
+ },
83
+ connect: (config) => {
84
+ 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);
85
+ }
86
+ });
87
+ }
88
+ exports.start = start;
89
+ /**
90
+ * Decodes a base64 string.
91
+ *
92
+ * The input is assumed to be correct.
93
+ */
94
+ function trustedBase64Decode(base64) {
95
+ // This code is a bit sketchy due to the fact that we decode into a string, but it seems to
96
+ // work.
97
+ const binaryString = atob(base64);
98
+ const size = binaryString.length;
99
+ const bytes = new Uint8Array(size);
100
+ for (let i = 0; i < size; i++) {
101
+ bytes[i] = binaryString.charCodeAt(i);
102
+ }
103
+ return bytes;
104
+ }
105
+ /**
106
+ * Tries to open a new connection using the given configuration.
107
+ *
108
+ * @see Connection
109
+ * @throws {@link ConnectionError} If the multiaddress couldn't be parsed or contains an invalid protocol.
110
+ */
111
+ function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
112
+ let connection;
113
+ // Attempt to parse the multiaddress.
114
+ // TODO: remove support for `/wss` in a long time (https://github.com/paritytech/smoldot/issues/1940)
115
+ const wsParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)\/(ws|wss|tls\/ws)$/);
116
+ const tcpParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)$/);
117
+ if (wsParsed != null) {
118
+ const proto = (wsParsed[4] == 'ws') ? 'ws' : 'wss';
119
+ if ((proto == 'ws' && forbidWs) ||
120
+ (proto == 'ws' && wsParsed[2] != 'localhost' && wsParsed[2] != '127.0.0.1' && forbidNonLocalWs) ||
121
+ (proto == 'wss' && forbidWss)) {
122
+ throw new instance_js_1.ConnectionError('Connection type not allowed');
123
+ }
124
+ const url = (wsParsed[1] == 'ip6') ?
125
+ (proto + "://[" + wsParsed[2] + "]:" + wsParsed[3]) :
126
+ (proto + "://" + wsParsed[2] + ":" + wsParsed[3]);
127
+ connection = {
128
+ ty: 'websocket',
129
+ socket: new WebSocket(url)
130
+ };
131
+ connection.socket.binaryType = 'arraybuffer';
132
+ connection.socket.onopen = () => {
133
+ config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux' });
134
+ };
135
+ connection.socket.onclose = (event) => {
136
+ const message = "Error code " + event.code + (!!event.reason ? (": " + event.reason) : "");
137
+ config.onConnectionReset(message);
138
+ };
139
+ connection.socket.onmessage = (msg) => {
140
+ config.onMessage(new Uint8Array(msg.data));
141
+ };
142
+ }
143
+ else if (tcpParsed != null) {
144
+ // `net` module will be missing when we're not in NodeJS.
145
+ if (forbidTcp) {
146
+ throw new instance_js_1.ConnectionError('TCP connections not available');
147
+ }
148
+ const socket = {
149
+ destroyed: false,
150
+ inner: Deno.connect({
151
+ hostname: tcpParsed[2],
152
+ port: parseInt(tcpParsed[3], 10),
153
+ })
154
+ };
155
+ connection = { ty: 'tcp', socket };
156
+ socket.inner = socket.inner.then((established) => {
157
+ // TODO: at the time of writing of this comment, `setNoDelay` is still unstable
158
+ //established.setNoDelay();
159
+ if (socket.destroyed)
160
+ return established;
161
+ config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux' });
162
+ // Spawns an asynchronous task that continuously reads from the socket.
163
+ // Every time data is read, the task re-executes itself in order to continue reading.
164
+ // The task ends automatically if an EOF or error is detected, which should also happen
165
+ // if the user calls `close()`.
166
+ const read = (readBuffer) => __awaiter(this, void 0, void 0, function* () {
167
+ if (socket.destroyed)
168
+ return;
169
+ let outcome = null;
170
+ try {
171
+ outcome = yield established.read(readBuffer);
172
+ }
173
+ catch (error) {
174
+ // The type of `error` is unclear, but we assume that it implements `Error`
175
+ outcome = error.toString();
176
+ }
177
+ if (socket.destroyed)
178
+ return;
179
+ if (typeof outcome !== 'number' || outcome === null) {
180
+ // The socket is reported closed, but `socket.destroyed` is still `false` (see
181
+ // check above). As such, we must inform the inner layers.
182
+ socket.destroyed = true;
183
+ config.onConnectionReset(outcome === null ? "EOF when reading socket" : outcome);
184
+ return;
185
+ }
186
+ console.assert(outcome !== 0); // `read` guarantees to return a non-zero value.
187
+ config.onMessage(readBuffer.slice(0, outcome));
188
+ return read(readBuffer);
189
+ });
190
+ read(new Uint8Array(1024));
191
+ return established;
192
+ });
193
+ }
194
+ else {
195
+ throw new instance_js_1.ConnectionError('Unrecognized multiaddr format');
196
+ }
197
+ return {
198
+ reset: () => {
199
+ if (connection.ty == 'websocket') {
200
+ // WebSocket
201
+ // We can't set these fields to null because the TypeScript definitions don't
202
+ // allow it, but we can set them to dummy values.
203
+ connection.socket.onopen = () => { };
204
+ connection.socket.onclose = () => { };
205
+ connection.socket.onmessage = () => { };
206
+ connection.socket.onerror = () => { };
207
+ connection.socket.close();
208
+ }
209
+ else {
210
+ // TCP
211
+ connection.socket.destroyed = true;
212
+ connection.socket.inner.then((connec) => connec.close());
213
+ }
214
+ },
215
+ send: (data) => {
216
+ if (connection.ty == 'websocket') {
217
+ // WebSocket
218
+ // The WebSocket library that we use seems to spontaneously transition connections
219
+ // to the "closed" state but not call the `onclosed` callback immediately. Calling
220
+ // `send` on that object throws an exception. In order to avoid panicking smoldot,
221
+ // we thus absorb any exception thrown here.
222
+ // See also <https://github.com/paritytech/smoldot/issues/2937>.
223
+ try {
224
+ connection.socket.send(data);
225
+ }
226
+ catch (_error) { }
227
+ }
228
+ else {
229
+ // TCP
230
+ // TODO: at the moment, sending data doesn't have any back-pressure mechanism; as such, we just buffer data indefinitely
231
+ let dataCopy = Uint8Array.from(data); // Deep copy of the data
232
+ const socket = connection.socket;
233
+ connection.socket.inner = connection.socket.inner.then((c) => __awaiter(this, void 0, void 0, function* () {
234
+ while (dataCopy.length > 0) {
235
+ if (socket.destroyed)
236
+ return c;
237
+ let outcome;
238
+ try {
239
+ outcome = yield c.write(dataCopy);
240
+ }
241
+ catch (error) {
242
+ // The type of `error` is unclear, but we assume that it implements `Error`
243
+ outcome = error.toString();
244
+ }
245
+ if (typeof outcome !== 'number') {
246
+ // The socket is reported closed, but `socket.destroyed` is still
247
+ // `false` (see check above). As such, we must inform the inner layers.
248
+ socket.destroyed = true;
249
+ config.onConnectionReset(outcome);
250
+ return c;
251
+ }
252
+ // Note that, contrary to `read`, it is possible for `outcome` to be 0.
253
+ // This happen if the write had to be interrupted, and the only thing
254
+ // we have to do is try writing again.
255
+ dataCopy = dataCopy.slice(outcome);
256
+ }
257
+ return c;
258
+ }));
259
+ }
260
+ },
261
+ openOutSubstream: () => { throw new Error('Wrong connection type'); }
262
+ };
263
+ }
@@ -0,0 +1,10 @@
1
+ import { Client, ClientOptions } from './client.js';
2
+ export { AddChainError, AddChainOptions, AlreadyDestroyedError, Chain, Client, ClientOptions, CrashError, MalformedJsonRpcError, QueueFullError, JsonRpcDisabledError, LogCallback } from './client.js';
3
+ /**
4
+ * Initializes a new client. This is a pre-requisite to connecting to a blockchain.
5
+ *
6
+ * Can never fail.
7
+ *
8
+ * @param options Configuration of the client. Defaults to `{}`.
9
+ */
10
+ export declare function start(options?: ClientOptions): Client;
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ // Smoldot
3
+ // Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
4
+ // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.start = exports.JsonRpcDisabledError = exports.QueueFullError = exports.MalformedJsonRpcError = exports.CrashError = exports.AlreadyDestroyedError = exports.AddChainError = void 0;
7
+ // This program is free software: you can redistribute it and/or modify
8
+ // it under the terms of the GNU General Public License as published by
9
+ // the Free Software Foundation, either version 3 of the License, or
10
+ // (at your option) any later version.
11
+ // This program is distributed in the hope that it will be useful,
12
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ // GNU General Public License for more details.
15
+ // You should have received a copy of the GNU General Public License
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 ws_1 = require("ws");
23
+ const pako_1 = require("pako");
24
+ const node_perf_hooks_1 = require("node:perf_hooks");
25
+ const node_net_1 = require("node:net");
26
+ const node_crypto_1 = require("node:crypto");
27
+ var client_js_2 = require("./client.js");
28
+ Object.defineProperty(exports, "AddChainError", { enumerable: true, get: function () { return client_js_2.AddChainError; } });
29
+ Object.defineProperty(exports, "AlreadyDestroyedError", { enumerable: true, get: function () { return client_js_2.AlreadyDestroyedError; } });
30
+ Object.defineProperty(exports, "CrashError", { enumerable: true, get: function () { return client_js_2.CrashError; } });
31
+ Object.defineProperty(exports, "MalformedJsonRpcError", { enumerable: true, get: function () { return client_js_2.MalformedJsonRpcError; } });
32
+ Object.defineProperty(exports, "QueueFullError", { enumerable: true, get: function () { return client_js_2.QueueFullError; } });
33
+ Object.defineProperty(exports, "JsonRpcDisabledError", { enumerable: true, get: function () { return client_js_2.JsonRpcDisabledError; } });
34
+ /**
35
+ * Initializes a new client. This is a pre-requisite to connecting to a blockchain.
36
+ *
37
+ * Can never fail.
38
+ *
39
+ * @param options Configuration of the client. Defaults to `{}`.
40
+ */
41
+ function start(options) {
42
+ 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
+ },
47
+ registerShouldPeriodicallyYield: (_callback) => {
48
+ return [true, () => { }];
49
+ },
50
+ performanceNow: () => {
51
+ return node_perf_hooks_1.performance.now();
52
+ },
53
+ getRandomValues: (buffer) => {
54
+ if (buffer.length >= 65536)
55
+ throw new Error('getRandomValues buffer too large');
56
+ (0, node_crypto_1.randomFillSync)(buffer);
57
+ },
58
+ connect: (config) => {
59
+ 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);
60
+ }
61
+ });
62
+ }
63
+ exports.start = start;
64
+ /**
65
+ * Tries to open a new connection using the given configuration.
66
+ *
67
+ * @see Connection
68
+ * @throws {@link ConnectionError} If the multiaddress couldn't be parsed or contains an invalid protocol.
69
+ */
70
+ function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
71
+ let connection;
72
+ // Attempt to parse the multiaddress.
73
+ // TODO: remove support for `/wss` in a long time (https://github.com/paritytech/smoldot/issues/1940)
74
+ const wsParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)\/(ws|wss|tls\/ws)$/);
75
+ const tcpParsed = config.address.match(/^\/(ip4|ip6|dns4|dns6|dns)\/(.*?)\/tcp\/(.*?)$/);
76
+ if (wsParsed != null) {
77
+ const proto = (wsParsed[4] == 'ws') ? 'ws' : 'wss';
78
+ if ((proto == 'ws' && forbidWs) ||
79
+ (proto == 'ws' && wsParsed[2] != 'localhost' && wsParsed[2] != '127.0.0.1' && forbidNonLocalWs) ||
80
+ (proto == 'wss' && forbidWss)) {
81
+ throw new instance_js_1.ConnectionError('Connection type not allowed');
82
+ }
83
+ const url = (wsParsed[1] == 'ip6') ?
84
+ (proto + "://[" + wsParsed[2] + "]:" + wsParsed[3]) :
85
+ (proto + "://" + wsParsed[2] + ":" + wsParsed[3]);
86
+ const socket = new ws_1.WebSocket(url);
87
+ socket.binaryType = 'arraybuffer';
88
+ socket.onopen = () => {
89
+ config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux' });
90
+ };
91
+ socket.onclose = (event) => {
92
+ const message = "Error code " + event.code + (!!event.reason ? (": " + event.reason) : "");
93
+ config.onConnectionReset(message);
94
+ socket.onopen = () => { };
95
+ socket.onclose = () => { };
96
+ socket.onmessage = () => { };
97
+ socket.onerror = () => { };
98
+ };
99
+ socket.onerror = (event) => {
100
+ config.onConnectionReset(event.message);
101
+ socket.onopen = () => { };
102
+ socket.onclose = () => { };
103
+ socket.onmessage = () => { };
104
+ socket.onerror = () => { };
105
+ };
106
+ socket.onmessage = (msg) => {
107
+ config.onMessage(new Uint8Array(msg.data));
108
+ };
109
+ connection = { ty: 'websocket', socket };
110
+ }
111
+ else if (tcpParsed != null) {
112
+ // `net` module will be missing when we're not in NodeJS.
113
+ if (forbidTcp) {
114
+ throw new instance_js_1.ConnectionError('TCP connections not available');
115
+ }
116
+ const socket = (0, node_net_1.createConnection)({
117
+ host: tcpParsed[2],
118
+ port: parseInt(tcpParsed[3], 10),
119
+ });
120
+ connection = { ty: 'tcp', socket };
121
+ connection.socket.setNoDelay();
122
+ connection.socket.on('connect', () => {
123
+ if (socket.destroyed)
124
+ return;
125
+ config.onOpen({ type: 'single-stream', handshake: 'multistream-select-noise-yamux' });
126
+ });
127
+ connection.socket.on('close', (hasError) => {
128
+ if (socket.destroyed)
129
+ return;
130
+ // NodeJS doesn't provide a reason why the closing happened, but only
131
+ // whether it was caused by an error.
132
+ const message = hasError ? "Error" : "Closed gracefully";
133
+ config.onConnectionReset(message);
134
+ });
135
+ connection.socket.on('error', () => { });
136
+ connection.socket.on('data', (message) => {
137
+ if (socket.destroyed)
138
+ return;
139
+ config.onMessage(new Uint8Array(message.buffer));
140
+ });
141
+ }
142
+ else {
143
+ throw new instance_js_1.ConnectionError('Unrecognized multiaddr format');
144
+ }
145
+ return {
146
+ reset: () => {
147
+ if (connection.ty == 'websocket') {
148
+ // WebSocket
149
+ // We can't set these fields to null because the TypeScript definitions don't
150
+ // allow it, but we can set them to dummy values.
151
+ connection.socket.onopen = () => { };
152
+ connection.socket.onclose = () => { };
153
+ connection.socket.onmessage = () => { };
154
+ connection.socket.onerror = () => { };
155
+ connection.socket.close();
156
+ }
157
+ else {
158
+ // TCP
159
+ connection.socket.destroy();
160
+ }
161
+ },
162
+ send: (data) => {
163
+ if (connection.ty == 'websocket') {
164
+ // WebSocket
165
+ connection.socket.send(data);
166
+ }
167
+ else {
168
+ // TCP
169
+ connection.socket.write(data);
170
+ }
171
+ },
172
+ openOutSubstream: () => { throw new Error('Wrong connection type'); }
173
+ };
174
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: string;
2
+ export default _default;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const wasm0_js_1 = require("./wasm0.js");
4
+ const wasm1_js_1 = require("./wasm1.js");
5
+ const wasm2_js_1 = require("./wasm2.js");
6
+ exports.default = "" + (0, wasm0_js_1.default)() + (0, wasm1_js_1.default)() + (0, wasm2_js_1.default)();
@@ -0,0 +1 @@
1
+ export default function (): string;