import { getRedactionHoldbackLength, redactSensitiveConfig } from './chunk-PRASPAE4.js';
import './chunk-UWUBFNLX.js';
import { __name } from './chunk-IBAHCH6O.js';

// src/runtime/lib/redact-stream.ts
var FLUSH_TIMEOUT_MS = 100;
function createRedactedStreamWriter(stream) {
  let pending = "";
  let flushTimeout;
  const clearFlushTimeout = /* @__PURE__ */ __name(() => {
    if (flushTimeout !== void 0) {
      clearTimeout(flushTimeout);
      flushTimeout = void 0;
    }
  }, "clearFlushTimeout");
  const flush = /* @__PURE__ */ __name(() => {
    clearFlushTimeout();
    if (!pending) return;
    stream.write(redactSensitiveConfig(pending));
    pending = "";
  }, "flush");
  const write = /* @__PURE__ */ __name((chunk) => {
    clearFlushTimeout();
    pending += chunk.toString();
    const holdbackLength = getRedactionHoldbackLength(pending);
    const emittable = holdbackLength ? pending.slice(0, -holdbackLength) : pending;
    pending = holdbackLength ? pending.slice(-holdbackLength) : "";
    if (emittable) stream.write(redactSensitiveConfig(emittable));
    if (pending) {
      flushTimeout = setTimeout(flush, FLUSH_TIMEOUT_MS);
      flushTimeout.unref?.();
    }
  }, "write");
  return { write, flush };
}
__name(createRedactedStreamWriter, "createRedactedStreamWriter");

export { createRedactedStreamWriter };
//# sourceMappingURL=redact-stream-HPOXW4IG.js.map
//# sourceMappingURL=redact-stream-HPOXW4IG.js.map