#!/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;
}
