Files
scriptc/tests/corpus/net-socket-encoding-pipe.cjs
T
Chris Tate f67678b422 Pipe decoded socket strings and give lane jobs their own tmpdirs
After socket.setEncoding(enc), socket.pipe(destination) wrote the raw
bytes it read to the destination. It now writes the decoded strings, as
Node's pipe writes each string chunk: the destination receives their
UTF-8 bytes, and a partial character the decoder holds at EOF is written
before the destination ends.

tests/node-parallel/run.mjs runs each job's files with its own
TEST_THREAD_ID, as Node's test runner does, so test/common/tmpdir gives
concurrent files different directories instead of one shared .tmp.0.
2026-10-29 23:16:13 -05:00

45 lines
1.6 KiB
JavaScript

// socket.pipe(destination) after socket.setEncoding(enc) writes the
// decoded strings to the destination, as Node's pipe writes each string
// chunk: the destination receives their UTF-8 bytes, a split character
// arrives whole, and a partial character held at EOF is written before the
// destination ends.
"use strict";
const net = require("net");
const source = net.createServer((socket) => {
socket.on("error", () => {});
socket.write(Buffer.from([0x61, 0xc3]));
setTimeout(() => socket.end(Buffer.from([0xa9, 0x62, 0xe2, 0x82])), 50);
});
const received = new Map();
const collector = net.createServer((socket) => {
const chunks = [];
let label = null;
socket.on("data", (chunk) => chunks.push(chunk));
socket.on("end", () => {
const all = Buffer.concat(chunks);
const newline = all.indexOf(0x0a);
label = all.subarray(0, newline).toString();
received.set(label, all.subarray(newline + 1).toString("hex"));
if (received.size === encodings.length) {
for (const name of encodings) console.log(name, received.get(name));
source.close();
collector.close();
}
});
});
const encodings = ["utf8", "hex", "base64", "latin1", "ucs2"];
source.listen(0, "127.0.0.1", () => {
collector.listen(0, "127.0.0.1", () => {
for (const encoding of encodings) {
const destination = net.connect(collector.address().port, "127.0.0.1");
destination.write(encoding + "\n");
const client = net.connect(source.address().port, "127.0.0.1");
client.setEncoding(encoding);
client.pipe(destination);
}
});
});