iosignal 9.21.0 → 9.22.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.
- package/README.ko.md +6 -0
- package/README.md +8 -0
- package/dist/browser/esm/broker.js +24 -0
- package/dist/browser/esm/broker.js.map +1 -0
- package/dist/browser/esm/io.d.ts +14 -0
- package/dist/browser/esm/io.js +1 -1
- package/dist/browser/esm/io.js.map +1 -1
- package/dist/browser/iife/io.js +1 -1
- package/dist/browser/iife/io.js.map +1 -1
- package/dist/node/iosignal.cjs +138 -96
- package/dist/node/iosignal.d.cts +1 -1
- package/dist/node/iosignal.d.ts +1 -1
- package/dist/node/iosignal.js +138 -96
- package/dist/types/client/browser/IOWebSocket.d.ts +14 -0
- package/package.json +4 -1
package/dist/node/iosignal.cjs
CHANGED
|
@@ -23,7 +23,7 @@ var node_os = require('node:os');
|
|
|
23
23
|
var path = require('path');
|
|
24
24
|
|
|
25
25
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
26
|
-
var version$1 = "9.
|
|
26
|
+
var version$1 = "9.22.0";
|
|
27
27
|
var pkg = {
|
|
28
28
|
version: version$1};
|
|
29
29
|
|
|
@@ -2071,6 +2071,9 @@ class IOCore extends EventEmitter {
|
|
|
2071
2071
|
|
|
2072
2072
|
}
|
|
2073
2073
|
|
|
2074
|
+
// CongSocket framing without Node.js streams: pack() and the frame parser. The server's Remote
|
|
2075
|
+
// uses CongParser directly; CongRx (CongPacket.js) wraps the same parser in a stream Transform.
|
|
2076
|
+
|
|
2074
2077
|
const CongType = {
|
|
2075
2078
|
TYPE_LEN1: 1,
|
|
2076
2079
|
TYPE_LEN2: 2,
|
|
@@ -2082,7 +2085,7 @@ const CongType = {
|
|
|
2082
2085
|
function pack(payload) {
|
|
2083
2086
|
|
|
2084
2087
|
if( payload.byteLength == undefined ) payload = Buffer.from(payload);
|
|
2085
|
-
|
|
2088
|
+
|
|
2086
2089
|
if (payload.byteLength < 256) { //one byte len
|
|
2087
2090
|
return $$1.pack(
|
|
2088
2091
|
$$1.MB('#type', '8', CongType.TYPE_LEN1),
|
|
@@ -2117,6 +2120,130 @@ function pack(payload) {
|
|
|
2117
2120
|
|
|
2118
2121
|
}
|
|
2119
2122
|
|
|
2123
|
+
/**
|
|
2124
|
+
* Adds a received chunk to a parser state and parses the complete frames into state.frames.
|
|
2125
|
+
* state: { buffer, frames, rxi, rxi_zero, maxFrameSize, skipBytes } (a CongParser or CongRx).
|
|
2126
|
+
* emit(event, value): 'wrong' (the unparsable buffer) or 'oversize' (payload size; the frame is
|
|
2127
|
+
* skipped without buffering). Shared by CongParser and CongRx so both parse alike.
|
|
2128
|
+
*/
|
|
2129
|
+
function addCongData(state, chunk, emit) {
|
|
2130
|
+
let c = chunk.byteLength;
|
|
2131
|
+
let i = 0;
|
|
2132
|
+
while (c--) {
|
|
2133
|
+
state.rxi++;
|
|
2134
|
+
if (chunk[i++] == 0) {
|
|
2135
|
+
state.rxi_zero++;
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
if (state.skipBytes > 0) {
|
|
2140
|
+
if (chunk.byteLength <= state.skipBytes) {
|
|
2141
|
+
state.skipBytes -= chunk.byteLength;
|
|
2142
|
+
return;
|
|
2143
|
+
}
|
|
2144
|
+
chunk = chunk.subarray(state.skipBytes);
|
|
2145
|
+
state.skipBytes = 0;
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
if (state.buffer.byteLength > 0) {
|
|
2149
|
+
state.buffer = Buffer.concat([state.buffer, chunk]);
|
|
2150
|
+
} else {
|
|
2151
|
+
state.buffer = chunk;
|
|
2152
|
+
}
|
|
2153
|
+
parseCongFrames(state, emit);
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2156
|
+
function parseCongFrames(state, emit) {
|
|
2157
|
+
while (state.buffer.byteLength > 0) {
|
|
2158
|
+
const head = state.buffer[0];
|
|
2159
|
+
let headerLen;
|
|
2160
|
+
let payloadSize;
|
|
2161
|
+
|
|
2162
|
+
if (head === CongType.TYPE_LEN1) {
|
|
2163
|
+
headerLen = 2;
|
|
2164
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2165
|
+
payloadSize = state.buffer.readUint8(1);
|
|
2166
|
+
} else if (head === CongType.TYPE_LEN2) {
|
|
2167
|
+
headerLen = 3;
|
|
2168
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2169
|
+
payloadSize = state.buffer.readUint16LE(1);
|
|
2170
|
+
} else if (head === CongType.TYPE_LEN3) {
|
|
2171
|
+
headerLen = 4;
|
|
2172
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2173
|
+
payloadSize = state.buffer.readUint16LE(1) + state.buffer.readUint8(3) * 65536;
|
|
2174
|
+
} else if (head === CongType.TYPE_LEN4) {
|
|
2175
|
+
headerLen = 5;
|
|
2176
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2177
|
+
payloadSize = state.buffer.readUint32LE(1);
|
|
2178
|
+
} else {
|
|
2179
|
+
emit('wrong', state.buffer);
|
|
2180
|
+
state.buffer = Buffer.alloc(0);
|
|
2181
|
+
return;
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
// Decide as soon as the length is known: the header can advertise up to 4 GiB.
|
|
2185
|
+
// Skipping keeps frame boundaries, so later frames still parse.
|
|
2186
|
+
if (payloadSize > state.maxFrameSize) {
|
|
2187
|
+
emit('oversize', payloadSize);
|
|
2188
|
+
const available = state.buffer.byteLength - headerLen;
|
|
2189
|
+
if (available < payloadSize) {
|
|
2190
|
+
state.skipBytes = payloadSize - available;
|
|
2191
|
+
state.buffer = Buffer.alloc(0);
|
|
2192
|
+
return;
|
|
2193
|
+
}
|
|
2194
|
+
state.buffer = state.buffer.subarray(headerLen + payloadSize);
|
|
2195
|
+
continue;
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
const frameSize = headerLen + payloadSize;
|
|
2199
|
+
if (state.buffer.byteLength < frameSize) return;
|
|
2200
|
+
|
|
2201
|
+
state.frames.push(state.buffer.subarray(headerLen, frameSize));
|
|
2202
|
+
if (state.buffer.byteLength === frameSize) {
|
|
2203
|
+
state.buffer = Buffer.alloc(0);
|
|
2204
|
+
return;
|
|
2205
|
+
}
|
|
2206
|
+
// Consume each complete frame without growing the call stack.
|
|
2207
|
+
state.buffer = state.buffer.subarray(frameSize);
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
/**
|
|
2212
|
+
* CongSocket frame parser without streams: write(chunk) calls onFrame(payload) for each complete
|
|
2213
|
+
* frame, onWrong(buffer) for an unparsable stream and onOversize(payloadSize) for a frame larger
|
|
2214
|
+
* than maxFrameSize (skipped without buffering).
|
|
2215
|
+
*/
|
|
2216
|
+
class CongParser {
|
|
2217
|
+
constructor({ maxFrameSize = Infinity, onFrame = null, onWrong = null, onOversize = null } = {}) {
|
|
2218
|
+
this.buffer = Buffer.alloc(0);
|
|
2219
|
+
this.frames = [];
|
|
2220
|
+
this.rxi = 0;
|
|
2221
|
+
this.rxi_zero = 0;
|
|
2222
|
+
this.maxFrameSize = maxFrameSize;
|
|
2223
|
+
this.skipBytes = 0; // remaining payload bytes of an oversized frame to discard
|
|
2224
|
+
this.onFrame = onFrame;
|
|
2225
|
+
this.onWrong = onWrong;
|
|
2226
|
+
this.onOversize = onOversize;
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
write(chunk) {
|
|
2230
|
+
addCongData(this, chunk, (event, value) => {
|
|
2231
|
+
if (event === 'wrong') this.onWrong?.(value);
|
|
2232
|
+
else this.onOversize?.(value);
|
|
2233
|
+
});
|
|
2234
|
+
const frames = this.frames;
|
|
2235
|
+
this.frames = [];
|
|
2236
|
+
for (const frame of frames) this.onFrame?.(frame);
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
/** Stops calling the handlers (the connection closed). */
|
|
2240
|
+
clear() {
|
|
2241
|
+
this.onFrame = this.onWrong = this.onOversize = null;
|
|
2242
|
+
this.buffer = Buffer.alloc(0);
|
|
2243
|
+
this.frames = [];
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2120
2247
|
class CongRx extends require$$0.Transform {
|
|
2121
2248
|
/**
|
|
2122
2249
|
* @param {import('stream').TransformOptions & { maxFrameSize?: number }} [options] Stream options.
|
|
@@ -2149,86 +2276,7 @@ class CongRx extends require$$0.Transform {
|
|
|
2149
2276
|
|
|
2150
2277
|
|
|
2151
2278
|
addData(chunk) {
|
|
2152
|
-
|
|
2153
|
-
let i = 0;
|
|
2154
|
-
while (c--) {
|
|
2155
|
-
this.rxi++;
|
|
2156
|
-
if (chunk[i++] == 0) {
|
|
2157
|
-
this.rxi_zero++;
|
|
2158
|
-
}
|
|
2159
|
-
}
|
|
2160
|
-
|
|
2161
|
-
if (this.skipBytes > 0) {
|
|
2162
|
-
if (chunk.byteLength <= this.skipBytes) {
|
|
2163
|
-
this.skipBytes -= chunk.byteLength;
|
|
2164
|
-
return;
|
|
2165
|
-
}
|
|
2166
|
-
chunk = chunk.subarray(this.skipBytes);
|
|
2167
|
-
this.skipBytes = 0;
|
|
2168
|
-
}
|
|
2169
|
-
|
|
2170
|
-
if (this.buffer.byteLength > 0) {
|
|
2171
|
-
this.buffer = Buffer.concat([this.buffer, chunk]);
|
|
2172
|
-
} else {
|
|
2173
|
-
this.buffer = chunk;
|
|
2174
|
-
}
|
|
2175
|
-
this.parse();
|
|
2176
|
-
}
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
parse() {
|
|
2180
|
-
while (this.buffer.byteLength > 0) {
|
|
2181
|
-
const head = this.buffer[0];
|
|
2182
|
-
let headerLen;
|
|
2183
|
-
let payloadSize;
|
|
2184
|
-
|
|
2185
|
-
if (head === CongType.TYPE_LEN1) {
|
|
2186
|
-
headerLen = 2;
|
|
2187
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2188
|
-
payloadSize = this.buffer.readUint8(1);
|
|
2189
|
-
} else if (head === CongType.TYPE_LEN2) {
|
|
2190
|
-
headerLen = 3;
|
|
2191
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2192
|
-
payloadSize = this.buffer.readUint16LE(1);
|
|
2193
|
-
} else if (head === CongType.TYPE_LEN3) {
|
|
2194
|
-
headerLen = 4;
|
|
2195
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2196
|
-
payloadSize = this.buffer.readUint16LE(1) + this.buffer.readUint8(3) * 65536;
|
|
2197
|
-
} else if (head === CongType.TYPE_LEN4) {
|
|
2198
|
-
headerLen = 5;
|
|
2199
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2200
|
-
payloadSize = this.buffer.readUint32LE(1);
|
|
2201
|
-
} else {
|
|
2202
|
-
this.emit('wrong', this.buffer);
|
|
2203
|
-
this.buffer = Buffer.alloc(0);
|
|
2204
|
-
return;
|
|
2205
|
-
}
|
|
2206
|
-
|
|
2207
|
-
// Decide as soon as the length is known: the header can advertise up to 4 GiB.
|
|
2208
|
-
// Skipping keeps frame boundaries, so later frames still parse.
|
|
2209
|
-
if (payloadSize > this.maxFrameSize) {
|
|
2210
|
-
this.emit('oversize', payloadSize);
|
|
2211
|
-
const available = this.buffer.byteLength - headerLen;
|
|
2212
|
-
if (available < payloadSize) {
|
|
2213
|
-
this.skipBytes = payloadSize - available;
|
|
2214
|
-
this.buffer = Buffer.alloc(0);
|
|
2215
|
-
return;
|
|
2216
|
-
}
|
|
2217
|
-
this.buffer = this.buffer.subarray(headerLen + payloadSize);
|
|
2218
|
-
continue;
|
|
2219
|
-
}
|
|
2220
|
-
|
|
2221
|
-
const frameSize = headerLen + payloadSize;
|
|
2222
|
-
if (this.buffer.byteLength < frameSize) return;
|
|
2223
|
-
|
|
2224
|
-
this.frames.push(this.buffer.subarray(headerLen, frameSize));
|
|
2225
|
-
if (this.buffer.byteLength === frameSize) {
|
|
2226
|
-
this.buffer = Buffer.alloc(0);
|
|
2227
|
-
return;
|
|
2228
|
-
}
|
|
2229
|
-
// Consume each complete frame without growing the call stack.
|
|
2230
|
-
this.buffer = this.buffer.subarray(frameSize);
|
|
2231
|
-
}
|
|
2279
|
+
addCongData(this, chunk, (event, value) => this.emit(event, value));
|
|
2232
2280
|
}
|
|
2233
2281
|
}
|
|
2234
2282
|
|
|
@@ -8375,26 +8423,20 @@ class Remote extends RemoteCore {
|
|
|
8375
8423
|
this.txDrainMaxMs = Math.max(this.txDrainMaxMs, node_perf_hooks.performance.now() - this.txBlockedSince);
|
|
8376
8424
|
this.txBlockedSince = null;
|
|
8377
8425
|
});
|
|
8378
|
-
|
|
8426
|
+
// tcpSocket -> congRx (frame parser, no stream) -> onSocketMessage
|
|
8427
|
+
this.congRx = new CongParser({
|
|
8428
|
+
maxFrameSize: this.rxFrameLimit(),
|
|
8429
|
+
onFrame: (serverOption.debug.slow ? this.onTimeDelayMessage : this.onSocketMessage).bind(this),
|
|
8430
|
+
onWrong: this.onWrongCongPacketMessage.bind(this),
|
|
8431
|
+
onOversize: this.onOversizeCongPacket.bind(this)
|
|
8432
|
+
});
|
|
8379
8433
|
socket.on('data', data => { this.congRx.write(data); });
|
|
8380
8434
|
|
|
8381
|
-
this.congRx.on('wrong', this.onWrongCongPacketMessage.bind(this));
|
|
8382
|
-
this.congRx.on('oversize', this.onOversizeCongPacket.bind(this));
|
|
8383
|
-
// readable.pipe( destination )
|
|
8384
|
-
// tcpSocket -> congRx(parser) -> onMessage
|
|
8385
|
-
// socket.pipe( this.congRx )
|
|
8386
|
-
|
|
8387
|
-
if (serverOption.debug.slow) {
|
|
8388
|
-
this.congRx.on('data', this.onTimeDelayMessage.bind(this));
|
|
8389
|
-
} else {
|
|
8390
|
-
this.congRx.on('data', this.onSocketMessage.bind(this));
|
|
8391
|
-
}
|
|
8392
|
-
|
|
8393
8435
|
socket.on('error', e => { console.log('TCP Socket error', e); });
|
|
8394
8436
|
socket.on('close', e => {
|
|
8395
8437
|
// Clean up listeners on both the socket and the parser
|
|
8396
8438
|
socket.removeAllListeners();
|
|
8397
|
-
this.congRx.
|
|
8439
|
+
this.congRx.clear();
|
|
8398
8440
|
this.manager.removeRemote(this);
|
|
8399
8441
|
});
|
|
8400
8442
|
|
package/dist/node/iosignal.d.cts
CHANGED
|
@@ -635,6 +635,7 @@ declare class IOCore extends EventEmitter<string | symbol, any> {
|
|
|
635
635
|
type Buffer$1 = Boho.Buffer;
|
|
636
636
|
|
|
637
637
|
declare function pack(payload: any): Buffer<ArrayBufferLike>;
|
|
638
|
+
|
|
638
639
|
declare class CongRx extends Transform {
|
|
639
640
|
/**
|
|
640
641
|
* @param {import('stream').TransformOptions & { maxFrameSize?: number }} [options] Stream options.
|
|
@@ -652,7 +653,6 @@ declare class CongRx extends Transform {
|
|
|
652
653
|
skipBytes: number;
|
|
653
654
|
_transform(chunk: any, encoding: any, callback: any): void;
|
|
654
655
|
addData(chunk: any): void;
|
|
655
|
-
parse(): void;
|
|
656
656
|
}
|
|
657
657
|
|
|
658
658
|
declare class IOCongSocket extends IOCore {
|
package/dist/node/iosignal.d.ts
CHANGED
|
@@ -635,6 +635,7 @@ declare class IOCore extends EventEmitter<string | symbol, any> {
|
|
|
635
635
|
type Buffer$1 = Boho.Buffer;
|
|
636
636
|
|
|
637
637
|
declare function pack(payload: any): Buffer<ArrayBufferLike>;
|
|
638
|
+
|
|
638
639
|
declare class CongRx extends Transform {
|
|
639
640
|
/**
|
|
640
641
|
* @param {import('stream').TransformOptions & { maxFrameSize?: number }} [options] Stream options.
|
|
@@ -652,7 +653,6 @@ declare class CongRx extends Transform {
|
|
|
652
653
|
skipBytes: number;
|
|
653
654
|
_transform(chunk: any, encoding: any, callback: any): void;
|
|
654
655
|
addData(chunk: any): void;
|
|
655
|
-
parse(): void;
|
|
656
656
|
}
|
|
657
657
|
|
|
658
658
|
declare class IOCongSocket extends IOCore {
|
package/dist/node/iosignal.js
CHANGED
|
@@ -20,7 +20,7 @@ import { readFileSync } from 'node:fs';
|
|
|
20
20
|
import { totalmem } from 'node:os';
|
|
21
21
|
import path from 'path';
|
|
22
22
|
|
|
23
|
-
var version$1 = "9.
|
|
23
|
+
var version$1 = "9.22.0";
|
|
24
24
|
var pkg = {
|
|
25
25
|
version: version$1};
|
|
26
26
|
|
|
@@ -2068,6 +2068,9 @@ class IOCore extends EventEmitter {
|
|
|
2068
2068
|
|
|
2069
2069
|
}
|
|
2070
2070
|
|
|
2071
|
+
// CongSocket framing without Node.js streams: pack() and the frame parser. The server's Remote
|
|
2072
|
+
// uses CongParser directly; CongRx (CongPacket.js) wraps the same parser in a stream Transform.
|
|
2073
|
+
|
|
2071
2074
|
const CongType = {
|
|
2072
2075
|
TYPE_LEN1: 1,
|
|
2073
2076
|
TYPE_LEN2: 2,
|
|
@@ -2079,7 +2082,7 @@ const CongType = {
|
|
|
2079
2082
|
function pack(payload) {
|
|
2080
2083
|
|
|
2081
2084
|
if( payload.byteLength == undefined ) payload = Buffer.from(payload);
|
|
2082
|
-
|
|
2085
|
+
|
|
2083
2086
|
if (payload.byteLength < 256) { //one byte len
|
|
2084
2087
|
return $$1.pack(
|
|
2085
2088
|
$$1.MB('#type', '8', CongType.TYPE_LEN1),
|
|
@@ -2114,6 +2117,130 @@ function pack(payload) {
|
|
|
2114
2117
|
|
|
2115
2118
|
}
|
|
2116
2119
|
|
|
2120
|
+
/**
|
|
2121
|
+
* Adds a received chunk to a parser state and parses the complete frames into state.frames.
|
|
2122
|
+
* state: { buffer, frames, rxi, rxi_zero, maxFrameSize, skipBytes } (a CongParser or CongRx).
|
|
2123
|
+
* emit(event, value): 'wrong' (the unparsable buffer) or 'oversize' (payload size; the frame is
|
|
2124
|
+
* skipped without buffering). Shared by CongParser and CongRx so both parse alike.
|
|
2125
|
+
*/
|
|
2126
|
+
function addCongData(state, chunk, emit) {
|
|
2127
|
+
let c = chunk.byteLength;
|
|
2128
|
+
let i = 0;
|
|
2129
|
+
while (c--) {
|
|
2130
|
+
state.rxi++;
|
|
2131
|
+
if (chunk[i++] == 0) {
|
|
2132
|
+
state.rxi_zero++;
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
if (state.skipBytes > 0) {
|
|
2137
|
+
if (chunk.byteLength <= state.skipBytes) {
|
|
2138
|
+
state.skipBytes -= chunk.byteLength;
|
|
2139
|
+
return;
|
|
2140
|
+
}
|
|
2141
|
+
chunk = chunk.subarray(state.skipBytes);
|
|
2142
|
+
state.skipBytes = 0;
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
if (state.buffer.byteLength > 0) {
|
|
2146
|
+
state.buffer = Buffer.concat([state.buffer, chunk]);
|
|
2147
|
+
} else {
|
|
2148
|
+
state.buffer = chunk;
|
|
2149
|
+
}
|
|
2150
|
+
parseCongFrames(state, emit);
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
function parseCongFrames(state, emit) {
|
|
2154
|
+
while (state.buffer.byteLength > 0) {
|
|
2155
|
+
const head = state.buffer[0];
|
|
2156
|
+
let headerLen;
|
|
2157
|
+
let payloadSize;
|
|
2158
|
+
|
|
2159
|
+
if (head === CongType.TYPE_LEN1) {
|
|
2160
|
+
headerLen = 2;
|
|
2161
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2162
|
+
payloadSize = state.buffer.readUint8(1);
|
|
2163
|
+
} else if (head === CongType.TYPE_LEN2) {
|
|
2164
|
+
headerLen = 3;
|
|
2165
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2166
|
+
payloadSize = state.buffer.readUint16LE(1);
|
|
2167
|
+
} else if (head === CongType.TYPE_LEN3) {
|
|
2168
|
+
headerLen = 4;
|
|
2169
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2170
|
+
payloadSize = state.buffer.readUint16LE(1) + state.buffer.readUint8(3) * 65536;
|
|
2171
|
+
} else if (head === CongType.TYPE_LEN4) {
|
|
2172
|
+
headerLen = 5;
|
|
2173
|
+
if (state.buffer.byteLength < headerLen) return;
|
|
2174
|
+
payloadSize = state.buffer.readUint32LE(1);
|
|
2175
|
+
} else {
|
|
2176
|
+
emit('wrong', state.buffer);
|
|
2177
|
+
state.buffer = Buffer.alloc(0);
|
|
2178
|
+
return;
|
|
2179
|
+
}
|
|
2180
|
+
|
|
2181
|
+
// Decide as soon as the length is known: the header can advertise up to 4 GiB.
|
|
2182
|
+
// Skipping keeps frame boundaries, so later frames still parse.
|
|
2183
|
+
if (payloadSize > state.maxFrameSize) {
|
|
2184
|
+
emit('oversize', payloadSize);
|
|
2185
|
+
const available = state.buffer.byteLength - headerLen;
|
|
2186
|
+
if (available < payloadSize) {
|
|
2187
|
+
state.skipBytes = payloadSize - available;
|
|
2188
|
+
state.buffer = Buffer.alloc(0);
|
|
2189
|
+
return;
|
|
2190
|
+
}
|
|
2191
|
+
state.buffer = state.buffer.subarray(headerLen + payloadSize);
|
|
2192
|
+
continue;
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
const frameSize = headerLen + payloadSize;
|
|
2196
|
+
if (state.buffer.byteLength < frameSize) return;
|
|
2197
|
+
|
|
2198
|
+
state.frames.push(state.buffer.subarray(headerLen, frameSize));
|
|
2199
|
+
if (state.buffer.byteLength === frameSize) {
|
|
2200
|
+
state.buffer = Buffer.alloc(0);
|
|
2201
|
+
return;
|
|
2202
|
+
}
|
|
2203
|
+
// Consume each complete frame without growing the call stack.
|
|
2204
|
+
state.buffer = state.buffer.subarray(frameSize);
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
/**
|
|
2209
|
+
* CongSocket frame parser without streams: write(chunk) calls onFrame(payload) for each complete
|
|
2210
|
+
* frame, onWrong(buffer) for an unparsable stream and onOversize(payloadSize) for a frame larger
|
|
2211
|
+
* than maxFrameSize (skipped without buffering).
|
|
2212
|
+
*/
|
|
2213
|
+
class CongParser {
|
|
2214
|
+
constructor({ maxFrameSize = Infinity, onFrame = null, onWrong = null, onOversize = null } = {}) {
|
|
2215
|
+
this.buffer = Buffer.alloc(0);
|
|
2216
|
+
this.frames = [];
|
|
2217
|
+
this.rxi = 0;
|
|
2218
|
+
this.rxi_zero = 0;
|
|
2219
|
+
this.maxFrameSize = maxFrameSize;
|
|
2220
|
+
this.skipBytes = 0; // remaining payload bytes of an oversized frame to discard
|
|
2221
|
+
this.onFrame = onFrame;
|
|
2222
|
+
this.onWrong = onWrong;
|
|
2223
|
+
this.onOversize = onOversize;
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
write(chunk) {
|
|
2227
|
+
addCongData(this, chunk, (event, value) => {
|
|
2228
|
+
if (event === 'wrong') this.onWrong?.(value);
|
|
2229
|
+
else this.onOversize?.(value);
|
|
2230
|
+
});
|
|
2231
|
+
const frames = this.frames;
|
|
2232
|
+
this.frames = [];
|
|
2233
|
+
for (const frame of frames) this.onFrame?.(frame);
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
/** Stops calling the handlers (the connection closed). */
|
|
2237
|
+
clear() {
|
|
2238
|
+
this.onFrame = this.onWrong = this.onOversize = null;
|
|
2239
|
+
this.buffer = Buffer.alloc(0);
|
|
2240
|
+
this.frames = [];
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2117
2244
|
class CongRx extends Transform {
|
|
2118
2245
|
/**
|
|
2119
2246
|
* @param {import('stream').TransformOptions & { maxFrameSize?: number }} [options] Stream options.
|
|
@@ -2146,86 +2273,7 @@ class CongRx extends Transform {
|
|
|
2146
2273
|
|
|
2147
2274
|
|
|
2148
2275
|
addData(chunk) {
|
|
2149
|
-
|
|
2150
|
-
let i = 0;
|
|
2151
|
-
while (c--) {
|
|
2152
|
-
this.rxi++;
|
|
2153
|
-
if (chunk[i++] == 0) {
|
|
2154
|
-
this.rxi_zero++;
|
|
2155
|
-
}
|
|
2156
|
-
}
|
|
2157
|
-
|
|
2158
|
-
if (this.skipBytes > 0) {
|
|
2159
|
-
if (chunk.byteLength <= this.skipBytes) {
|
|
2160
|
-
this.skipBytes -= chunk.byteLength;
|
|
2161
|
-
return;
|
|
2162
|
-
}
|
|
2163
|
-
chunk = chunk.subarray(this.skipBytes);
|
|
2164
|
-
this.skipBytes = 0;
|
|
2165
|
-
}
|
|
2166
|
-
|
|
2167
|
-
if (this.buffer.byteLength > 0) {
|
|
2168
|
-
this.buffer = Buffer.concat([this.buffer, chunk]);
|
|
2169
|
-
} else {
|
|
2170
|
-
this.buffer = chunk;
|
|
2171
|
-
}
|
|
2172
|
-
this.parse();
|
|
2173
|
-
}
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
parse() {
|
|
2177
|
-
while (this.buffer.byteLength > 0) {
|
|
2178
|
-
const head = this.buffer[0];
|
|
2179
|
-
let headerLen;
|
|
2180
|
-
let payloadSize;
|
|
2181
|
-
|
|
2182
|
-
if (head === CongType.TYPE_LEN1) {
|
|
2183
|
-
headerLen = 2;
|
|
2184
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2185
|
-
payloadSize = this.buffer.readUint8(1);
|
|
2186
|
-
} else if (head === CongType.TYPE_LEN2) {
|
|
2187
|
-
headerLen = 3;
|
|
2188
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2189
|
-
payloadSize = this.buffer.readUint16LE(1);
|
|
2190
|
-
} else if (head === CongType.TYPE_LEN3) {
|
|
2191
|
-
headerLen = 4;
|
|
2192
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2193
|
-
payloadSize = this.buffer.readUint16LE(1) + this.buffer.readUint8(3) * 65536;
|
|
2194
|
-
} else if (head === CongType.TYPE_LEN4) {
|
|
2195
|
-
headerLen = 5;
|
|
2196
|
-
if (this.buffer.byteLength < headerLen) return;
|
|
2197
|
-
payloadSize = this.buffer.readUint32LE(1);
|
|
2198
|
-
} else {
|
|
2199
|
-
this.emit('wrong', this.buffer);
|
|
2200
|
-
this.buffer = Buffer.alloc(0);
|
|
2201
|
-
return;
|
|
2202
|
-
}
|
|
2203
|
-
|
|
2204
|
-
// Decide as soon as the length is known: the header can advertise up to 4 GiB.
|
|
2205
|
-
// Skipping keeps frame boundaries, so later frames still parse.
|
|
2206
|
-
if (payloadSize > this.maxFrameSize) {
|
|
2207
|
-
this.emit('oversize', payloadSize);
|
|
2208
|
-
const available = this.buffer.byteLength - headerLen;
|
|
2209
|
-
if (available < payloadSize) {
|
|
2210
|
-
this.skipBytes = payloadSize - available;
|
|
2211
|
-
this.buffer = Buffer.alloc(0);
|
|
2212
|
-
return;
|
|
2213
|
-
}
|
|
2214
|
-
this.buffer = this.buffer.subarray(headerLen + payloadSize);
|
|
2215
|
-
continue;
|
|
2216
|
-
}
|
|
2217
|
-
|
|
2218
|
-
const frameSize = headerLen + payloadSize;
|
|
2219
|
-
if (this.buffer.byteLength < frameSize) return;
|
|
2220
|
-
|
|
2221
|
-
this.frames.push(this.buffer.subarray(headerLen, frameSize));
|
|
2222
|
-
if (this.buffer.byteLength === frameSize) {
|
|
2223
|
-
this.buffer = Buffer.alloc(0);
|
|
2224
|
-
return;
|
|
2225
|
-
}
|
|
2226
|
-
// Consume each complete frame without growing the call stack.
|
|
2227
|
-
this.buffer = this.buffer.subarray(frameSize);
|
|
2228
|
-
}
|
|
2276
|
+
addCongData(this, chunk, (event, value) => this.emit(event, value));
|
|
2229
2277
|
}
|
|
2230
2278
|
}
|
|
2231
2279
|
|
|
@@ -8372,26 +8420,20 @@ class Remote extends RemoteCore {
|
|
|
8372
8420
|
this.txDrainMaxMs = Math.max(this.txDrainMaxMs, performance.now() - this.txBlockedSince);
|
|
8373
8421
|
this.txBlockedSince = null;
|
|
8374
8422
|
});
|
|
8375
|
-
|
|
8423
|
+
// tcpSocket -> congRx (frame parser, no stream) -> onSocketMessage
|
|
8424
|
+
this.congRx = new CongParser({
|
|
8425
|
+
maxFrameSize: this.rxFrameLimit(),
|
|
8426
|
+
onFrame: (serverOption.debug.slow ? this.onTimeDelayMessage : this.onSocketMessage).bind(this),
|
|
8427
|
+
onWrong: this.onWrongCongPacketMessage.bind(this),
|
|
8428
|
+
onOversize: this.onOversizeCongPacket.bind(this)
|
|
8429
|
+
});
|
|
8376
8430
|
socket.on('data', data => { this.congRx.write(data); });
|
|
8377
8431
|
|
|
8378
|
-
this.congRx.on('wrong', this.onWrongCongPacketMessage.bind(this));
|
|
8379
|
-
this.congRx.on('oversize', this.onOversizeCongPacket.bind(this));
|
|
8380
|
-
// readable.pipe( destination )
|
|
8381
|
-
// tcpSocket -> congRx(parser) -> onMessage
|
|
8382
|
-
// socket.pipe( this.congRx )
|
|
8383
|
-
|
|
8384
|
-
if (serverOption.debug.slow) {
|
|
8385
|
-
this.congRx.on('data', this.onTimeDelayMessage.bind(this));
|
|
8386
|
-
} else {
|
|
8387
|
-
this.congRx.on('data', this.onSocketMessage.bind(this));
|
|
8388
|
-
}
|
|
8389
|
-
|
|
8390
8432
|
socket.on('error', e => { console.log('TCP Socket error', e); });
|
|
8391
8433
|
socket.on('close', e => {
|
|
8392
8434
|
// Clean up listeners on both the socket and the parser
|
|
8393
8435
|
socket.removeAllListeners();
|
|
8394
|
-
this.congRx.
|
|
8436
|
+
this.congRx.clear();
|
|
8395
8437
|
this.manager.removeRemote(this);
|
|
8396
8438
|
});
|
|
8397
8439
|
|
|
@@ -33,6 +33,13 @@ export default class IO extends IOCore {
|
|
|
33
33
|
* @type {object}
|
|
34
34
|
*/
|
|
35
35
|
static constants: object;
|
|
36
|
+
/**
|
|
37
|
+
* The WebSocket class to connect with; null uses the global WebSocket. Set it on IO or on a
|
|
38
|
+
* subclass to connect through something with the browser WebSocket API (readyState,
|
|
39
|
+
* binaryType, onopen/onmessage/onerror/onclose, send, close), e.g. a test double.
|
|
40
|
+
* @type {(new (url: string) => WebSocket) | null}
|
|
41
|
+
*/
|
|
42
|
+
static WebSocket: (new (url: string) => WebSocket) | null;
|
|
36
43
|
/**
|
|
37
44
|
* Tracks the number of IO instances created.
|
|
38
45
|
* @type {number}
|
|
@@ -52,6 +59,13 @@ export default class IO extends IOCore {
|
|
|
52
59
|
* Keeps the connection alive by re-connecting if closed.
|
|
53
60
|
*/
|
|
54
61
|
keepConnection(): void;
|
|
62
|
+
/**
|
|
63
|
+
* Makes the socket for a connection: new IO.WebSocket(url) (the class set on this client's
|
|
64
|
+
* class), else the global WebSocket. Override it to choose the socket by URL.
|
|
65
|
+
* @param {string} url - The URL to connect to.
|
|
66
|
+
* @returns {WebSocket}
|
|
67
|
+
*/
|
|
68
|
+
createWebSocket(url: string): WebSocket;
|
|
55
69
|
/**
|
|
56
70
|
* Creates a new WebSocket connection.
|
|
57
71
|
* @param {string} url - The WebSocket URL to connect to.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "iosignal",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.22.0",
|
|
4
4
|
"description": "iosignal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"types": "./dist/browser/esm/io.d.ts",
|
|
23
23
|
"default": "./dist/browser/esm/io.js"
|
|
24
24
|
},
|
|
25
|
+
"./broker": "./dist/browser/esm/broker.js",
|
|
25
26
|
"./browser/*": "./dist/browser/*"
|
|
26
27
|
},
|
|
27
28
|
"types": "./dist/node/iosignal.d.ts",
|
|
@@ -80,6 +81,8 @@
|
|
|
80
81
|
"auth"
|
|
81
82
|
],
|
|
82
83
|
"files": [
|
|
84
|
+
"dist/browser/esm/broker.js",
|
|
85
|
+
"dist/browser/esm/broker.js.map",
|
|
83
86
|
"dist/browser/esm/io.d.ts",
|
|
84
87
|
"dist/browser/esm/io.js",
|
|
85
88
|
"dist/browser/esm/io.js.map",
|