var d = Object.defineProperty;
var s = (a, n) => d(a, "name", { value: n, configurable: !0 });
import { ConnectError as g } from "../connect-error.js";
import { Code as b } from "../code.js";
function p(a) {
  let n, e = new Uint8Array(0);
  function f(r) {
    const t = new Uint8Array(e.length + r.length);
    t.set(e), t.set(r, e.length), e = t;
  }
  return s(f, "append"), new ReadableStream({
    start() {
      n = a.getReader();
    },
    async pull(r) {
      let t;
      for (; ; ) {
        if (t === void 0 && e.byteLength >= 5) {
          let i = 0;
          for (let l = 1; l < 5; l++)
            i = (i << 8) + e[l];
          t = { flags: e[0], length: i };
        }
        if (t !== void 0 && e.byteLength >= t.length + 5)
          break;
        const o = await n.read();
        if (o.done)
          break;
        f(o.value);
      }
      if (t === void 0) {
        if (e.byteLength == 0) {
          r.close();
          return;
        }
        r.error(new g("premature end of stream", b.DataLoss));
        return;
      }
      const u = e.subarray(5, 5 + t.length);
      e = e.subarray(5 + t.length), r.enqueue({
        flags: t.flags,
        data: u
      });
    }
  });
}
s(p, "createEnvelopeReadableStream");
function m(a, n) {
  const e = new Uint8Array(n.length + 5);
  e.set(n, 5);
  const f = new DataView(e.buffer, e.byteOffset, e.byteLength);
  return f.setUint8(0, a), f.setUint32(1, n.length), e;
}
s(m, "encodeEnvelope");
export {
  p as createEnvelopeReadableStream,
  m as encodeEnvelope
};
