chore: restart the repository with a clean history
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
This commit is contained in:
438 files changed
+45070
No files matched your search
@@ -0,0 +1,91 @@
|
||||
const { randomBytes } = require("node:crypto");
|
||||
|
||||
const MAX_FRAME = 16 * 1024 * 1024;
|
||||
|
||||
class FrameParser {
|
||||
constructor(onMessage, { skipHead = false } = {}) {
|
||||
this.onMessage = onMessage;
|
||||
this.buffer = Buffer.alloc(0);
|
||||
this.fragments = null;
|
||||
this.dead = false;
|
||||
this.awaitingHead = skipHead;
|
||||
}
|
||||
|
||||
push(chunk) {
|
||||
if (this.dead) return;
|
||||
this.buffer = this.buffer.length ? Buffer.concat([this.buffer, chunk]) : chunk;
|
||||
if (this.awaitingHead) {
|
||||
const end = this.buffer.indexOf("\r\n\r\n");
|
||||
if (end === -1) return;
|
||||
this.buffer = this.buffer.subarray(end + 4);
|
||||
this.awaitingHead = false;
|
||||
}
|
||||
this.drain();
|
||||
}
|
||||
|
||||
drain() {
|
||||
for (;;) {
|
||||
const buffer = this.buffer;
|
||||
if (buffer.length < 2) return;
|
||||
const fin = (buffer[0] & 0x80) !== 0;
|
||||
const opcode = buffer[0] & 0x0f;
|
||||
const masked = (buffer[1] & 0x80) !== 0;
|
||||
let length = buffer[1] & 0x7f;
|
||||
let offset = 2;
|
||||
if (length === 126) {
|
||||
if (buffer.length < 4) return;
|
||||
length = buffer.readUInt16BE(2);
|
||||
offset = 4;
|
||||
} else if (length === 127) {
|
||||
if (buffer.length < 10) return;
|
||||
length = Number(buffer.readBigUInt64BE(2));
|
||||
offset = 10;
|
||||
}
|
||||
if (length > MAX_FRAME) {
|
||||
this.dead = true;
|
||||
this.buffer = Buffer.alloc(0);
|
||||
return;
|
||||
}
|
||||
const start = offset + (masked ? 4 : 0);
|
||||
if (buffer.length < start + length) return;
|
||||
let payload = Buffer.from(buffer.subarray(start, start + length));
|
||||
if (masked) {
|
||||
const key = buffer.subarray(offset, offset + 4);
|
||||
for (let i = 0; i < length; i++) payload[i] ^= key[i & 3];
|
||||
}
|
||||
this.buffer = buffer.subarray(start + length);
|
||||
|
||||
if (opcode >= 8) {
|
||||
this.onMessage(opcode, payload);
|
||||
} else if (opcode === 0) {
|
||||
if (!this.fragments) continue;
|
||||
this.fragments.parts.push(payload);
|
||||
if (fin) {
|
||||
const { opcode: first, parts } = this.fragments;
|
||||
this.fragments = null;
|
||||
this.onMessage(first, Buffer.concat(parts));
|
||||
}
|
||||
} else if (fin) {
|
||||
this.onMessage(opcode, payload);
|
||||
} else {
|
||||
this.fragments = { opcode, parts: [payload] };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const encodeFrame = (opcode, payload, masked) => {
|
||||
const length = payload.length;
|
||||
const header = [0x80 | opcode];
|
||||
if (length < 126) header.push(length);
|
||||
else if (length < 65536) header.push(126, length >> 8, length & 0xff);
|
||||
else header.push(127, 0, 0, 0, 0, length >>> 24, (length >>> 16) & 0xff, (length >>> 8) & 0xff, length & 0xff);
|
||||
if (!masked) return Buffer.concat([Buffer.from(header), payload]);
|
||||
header[1] |= 0x80;
|
||||
const key = randomBytes(4);
|
||||
const body = Buffer.allocUnsafe(length);
|
||||
for (let i = 0; i < length; i++) body[i] = payload[i] ^ key[i & 3];
|
||||
return Buffer.concat([Buffer.from(header), key, body]);
|
||||
};
|
||||
|
||||
module.exports = { FrameParser, encodeFrame };
|
||||
Reference in new issue
Block a user