Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

3.6 KB
Raw Blame
export type ByteSource = string | Uint8Array | AsyncIterable<Uint8Array>;

const encoder = new TextEncoder();

export const encodeUtf8 = (value: string) => encoder.encode(value);

export const getUtf8ByteLength = (value: string) =>
  encodeUtf8(value).byteLength;

export class ByteLimitExceededError extends Error {
  constructor() {
    super("Byte stream exceeds the configured limit");
    this.name = "ByteLimitExceededError";
  }
}

const concatenateChunks = (chunks: readonly Uint8Array[], length: number) => {
  const bytes = new Uint8Array(length);
  let offset = 0;
  for (const chunk of chunks) {
    bytes.set(chunk, offset);
    offset += chunk.byteLength;
  }
  return bytes;
};

export const toByteChunks = (source: ByteSource): AsyncIterable<Uint8Array> => {
  if (typeof source === "string") {
    return {
      async *[Symbol.asyncIterator]() {
        yield encodeUtf8(source);
      },
    };
  }
  if (source instanceof Uint8Array) {
    return {
      async *[Symbol.asyncIterator]() {
        yield source;
      },
    };
  }
  return source;
};

export const selectByteRange = (
  source: ByteSource,
  range?: { offset: number; length: number }
): AsyncIterable<Uint8Array> => {
  if (range === undefined) {
    return toByteChunks(source);
  }
  return {
    async *[Symbol.asyncIterator]() {
      let skipped = 0;
      let emitted = 0;
      for await (const chunk of toByteChunks(source)) {
        if (emitted === range.length) {
          return;
        }
        if (skipped + chunk.byteLength <= range.offset) {
          skipped += chunk.byteLength;
          continue;
        }
        const start = Math.max(0, range.offset - skipped);
        const length = Math.min(
          chunk.byteLength - start,
          range.length - emitted
        );
        if (length > 0) {
          yield chunk.subarray(start, start + length);
          emitted += length;
        }
        skipped += chunk.byteLength;
      }
    },
  };
};

export const readableStreamToAsyncIterable = (
  stream: ReadableStream<Uint8Array>
): AsyncIterable<Uint8Array> => ({
  async *[Symbol.asyncIterator]() {
    const reader = stream.getReader();
    let completed = false;
    try {
      for (;;) {
        const result = await reader.read();
        if (result.done) {
          completed = true;
          return;
        }
        yield result.value;
      }
    } finally {
      if (completed === false) {
        // Cancelling a tee waits for its other branch. Start cleanup without
        // delaying the consumer or replacing its error if cancellation fails.
        reader.cancel().catch(() => {});
      }
      reader.releaseLock();
    }
  },
});

export const readBytePrefix = async (
  source: ByteSource,
  maximumBytes: number
) => {
  const chunks: Uint8Array[] = [];
  let length = 0;
  for await (const chunk of toByteChunks(source)) {
    const remaining = maximumBytes - length;
    if (remaining <= 0) {
      break;
    }
    const retained = chunk.subarray(0, remaining);
    chunks.push(retained);
    length += retained.byteLength;
    if (length === maximumBytes) {
      break;
    }
  }
  return concatenateChunks(chunks, length);
};

export const readBoundedBytes = async (
  source: ByteSource,
  maximumBytes: number,
  limitError = new ByteLimitExceededError()
) => {
  const bytes = await readBytePrefix(source, maximumBytes + 1);
  if (bytes.byteLength > maximumBytes) {
    throw limitError;
  }
  return bytes;
};

export const decodeUtf8 = (bytes: Uint8Array) =>
  new TextDecoder("utf-8", { fatal: true }).decode(bytes);

export const stripUtf8ByteOrderMark = (value: string) =>
  value.charCodeAt(0) === 0xfeff ? value.slice(1) : value;