smoldot 3.5.0 → 3.7.0

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.
@@ -30,6 +30,17 @@ export function startWithBytecode(options) {
30
30
  }
31
31
  });
32
32
  }
33
+ /**
34
+ * Human-readable reason for a socket error. An `AggregateError` (every address of a
35
+ * multi-address host failed) has an empty `message`; its causes are in `errors`.
36
+ */
37
+ function errorReason(error) {
38
+ var _a;
39
+ return ((_a = error.errors) === null || _a === void 0 ? void 0 : _a.map((cause) => cause.message).filter(Boolean).join("; "))
40
+ || error.message
41
+ || error.code
42
+ || "Error";
43
+ }
33
44
  /**
34
45
  * Tries to open a new connection using the given configuration.
35
46
  *
@@ -77,7 +88,7 @@ function connect(config) {
77
88
  socket.onerror = () => { };
78
89
  };
79
90
  socket.onerror = (event) => {
80
- config.onConnectionReset(event.message);
91
+ config.onConnectionReset(errorReason(event.error));
81
92
  socket.onopen = () => { };
82
93
  socket.onclose = () => { };
83
94
  socket.onmessage = () => { };
@@ -117,30 +128,35 @@ function connect(config) {
117
128
  });
118
129
  // Number of bytes queued using `socket.write` and where `write` has returned false.
119
130
  const drainingBytes = { num: 0 };
131
+ // Set by `reset()`. Events that arrive after that must be ignored. This can't be
132
+ // `socket.destroyed`: NodeJS marks the socket destroyed before emitting `close`, so
133
+ // that check would swallow every close, including remote ones.
134
+ let resetCalled = false;
135
+ // Message of the last `error` event, reported through the `close` event that follows it.
136
+ let lastError = null;
120
137
  socket.setNoDelay();
121
138
  socket.on('connect', () => {
122
- if (socket.destroyed)
139
+ if (resetCalled)
123
140
  return;
124
141
  config.onWritableBytes(socket.writableHighWaterMark);
125
142
  });
143
+ socket.on('error', (error) => {
144
+ lastError = errorReason(error);
145
+ });
126
146
  socket.on('close', (hasError) => {
127
- if (socket.destroyed)
147
+ if (resetCalled)
128
148
  return;
129
- // NodeJS doesn't provide a reason why the closing happened, but only
130
- // whether it was caused by an error.
131
- const message = hasError ? "Error" : "Closed gracefully";
132
- config.onConnectionReset(message);
149
+ config.onConnectionReset(lastError !== null && lastError !== void 0 ? lastError : (hasError ? "Error" : "Closed gracefully"));
133
150
  });
134
- socket.on('error', () => { });
135
151
  socket.on('data', (message) => {
136
- if (socket.destroyed)
152
+ if (resetCalled)
137
153
  return;
138
154
  config.onMessage(new Uint8Array(message.buffer));
139
155
  });
140
156
  socket.on('drain', () => {
141
157
  // The bytes queued using `socket.write` and where `write` has returned false have now
142
158
  // been sent. Notify the API that it can write more data.
143
- if (socket.destroyed)
159
+ if (resetCalled)
144
160
  return;
145
161
  const val = drainingBytes.num;
146
162
  drainingBytes.num = 0;
@@ -148,6 +164,7 @@ function connect(config) {
148
164
  });
149
165
  return {
150
166
  reset: () => {
167
+ resetCalled = true;
151
168
  socket.destroy();
152
169
  },
153
170
  send: (data) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smoldot",
3
- "version": "3.5.0",
3
+ "version": "3.7.0",
4
4
  "description": "Light client that connects to Polkadot and Substrate-based blockchains",
5
5
  "contributors": [
6
6
  "Parity Technologies <admin@parity.io>",