Something went wrong. Try again.
presents omp as codex for use with chatgpt remote
Something went wrong. Try again.
1.4 kB · 32 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233import test from "node:test";import assert from "node:assert/strict";import crypto from "node:crypto";import { WebSocketFrameDecoder, encodeWebSocketFrame } from "../src/transports/websocket.mjs";
function maskedClientFrame(text) { const payload = Buffer.from(text); const mask = crypto.randomBytes(4); const header = payload.length < 126 ? Buffer.from([0x81, 0x80 | payload.length]) : (() => { const value = Buffer.alloc(4); value[0] = 0x81; value[1] = 0x80 | 126; value.writeUInt16BE(payload.length, 2); return value; })(); const body = Buffer.from(payload); for (let i = 0; i < body.length; i += 1) body[i] ^= mask[i % 4]; return Buffer.concat([header, mask, body]);}
test("WebSocket decoder accepts fragmented network chunks", () => { const decoder = new WebSocketFrameDecoder(); const frame = maskedClientFrame(JSON.stringify({ id: 1, method: "initialize" })); const first = decoder.push(frame.subarray(0, 3)); const second = decoder.push(frame.subarray(3)); assert.equal(first.length, 0); assert.equal(second[0].type, "text"); assert.equal(JSON.parse(second[0].payload.toString()).method, "initialize");});
test("server frames are unmasked text frames", () => { const frame = encodeWebSocketFrame("hello"); assert.equal(frame[0], 0x81); assert.equal(frame[1] & 0x80, 0); assert.equal(frame.subarray(2).toString(), "hello");});