Files
portal/tools/router-stdin-helper.mjs
T
Дмитрий d7d8c5edac feat(router): UTF-8 safe stdin helper for three hooks
StringDecoder correctly assembles multi-byte chars (Cyrillic) across
stdin chunk boundaries. Closes Windows Node quirk where Russian prompts
were turned into mojibake before sending to Anthropic API (Layer 2 escalation).

Stage 3 follow-up fix 1/3 (helper).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-24 15:26:18 +03:00

23 lines
700 B
JavaScript

#!/usr/bin/env node
/**
* UTF-8 safe stdin reader for hooks.
* Fixes Windows Node stdin quirk: default `for await (chunk of stdin)` interprets
* chunks as Buffer, and `input += chunk` calls .toString() which uses utf-8 BUT
* fails on chunk boundaries that fall inside multi-byte sequences (e.g. cyrillic
* 2-byte chars split across chunks).
*
* Uses StringDecoder to handle multi-byte chars across chunks correctly.
*/
import { StringDecoder } from 'string_decoder';
export async function readStdinAsUtf8(stdin) {
const decoder = new StringDecoder('utf-8');
let out = '';
for await (const chunk of stdin) {
out += decoder.write(chunk);
}
out += decoder.end();
return out;
}