2024-02-19 10:24:22 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// This module is browser compatible.
|
|
|
|
|
|
|
|
import { DEFAULT_BUFFER_SIZE } from "./_constants.ts";
|
|
|
|
import type { Reader, ReaderSync } from "./types.ts";
|
|
|
|
|
|
|
|
export type { Reader, ReaderSync };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns a {@linkcode Reader} into an async iterator.
|
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* @example Usage
|
|
|
|
* ```ts no-assert
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { iterateReader } from "@std/io/iterate-reader";
|
2024-02-19 10:24:22 +00:00
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* using file = await Deno.open("README.md");
|
2024-02-19 10:24:22 +00:00
|
|
|
* for await (const chunk of iterateReader(file)) {
|
|
|
|
* console.log(chunk);
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* @example Usage with buffer size
|
|
|
|
* ```ts no-assert
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { iterateReader } from "@std/io/iterate-reader";
|
2024-02-19 10:24:22 +00:00
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* using file = await Deno.open("README.md");
|
2024-02-19 10:24:22 +00:00
|
|
|
* const iter = iterateReader(file, {
|
|
|
|
* bufSize: 1024 * 1024
|
|
|
|
* });
|
|
|
|
* for await (const chunk of iter) {
|
|
|
|
* console.log(chunk);
|
|
|
|
* }
|
|
|
|
* ```
|
2024-08-08 14:20:43 +00:00
|
|
|
*
|
|
|
|
* @param reader The reader to read from
|
|
|
|
* @param options The options
|
|
|
|
* @param options.bufSize The size of the buffer to use
|
|
|
|
* @returns The async iterator of Uint8Array chunks
|
2024-02-19 10:24:22 +00:00
|
|
|
*/
|
|
|
|
export async function* iterateReader(
|
|
|
|
reader: Reader,
|
|
|
|
options?: {
|
|
|
|
bufSize?: number;
|
|
|
|
},
|
|
|
|
): AsyncIterableIterator<Uint8Array> {
|
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
|
|
|
while (true) {
|
|
|
|
const result = await reader.read(b);
|
|
|
|
if (result === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield b.slice(0, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns a {@linkcode ReaderSync} into an iterator.
|
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* @example Usage
|
2024-02-19 10:24:22 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { iterateReaderSync } from "@std/io/iterate-reader";
|
2024-08-08 14:20:43 +00:00
|
|
|
* import { assert } from "@std/assert/assert"
|
2024-02-19 10:24:22 +00:00
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* using file = Deno.openSync("README.md");
|
2024-02-19 10:24:22 +00:00
|
|
|
* for (const chunk of iterateReaderSync(file)) {
|
2024-08-08 14:20:43 +00:00
|
|
|
* assert(chunk instanceof Uint8Array);
|
2024-02-19 10:24:22 +00:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Second argument can be used to tune size of a buffer.
|
|
|
|
* Default size of the buffer is 32kB.
|
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* @example Usage with buffer size
|
2024-02-19 10:24:22 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { iterateReaderSync } from "@std/io/iterate-reader";
|
2024-08-08 14:20:43 +00:00
|
|
|
* import { assert } from "@std/assert/assert"
|
|
|
|
*
|
|
|
|
* using file = await Deno.open("README.md");
|
2024-02-19 10:24:22 +00:00
|
|
|
* const iter = iterateReaderSync(file, {
|
|
|
|
* bufSize: 1024 * 1024
|
|
|
|
* });
|
|
|
|
* for (const chunk of iter) {
|
2024-08-08 14:20:43 +00:00
|
|
|
* assert(chunk instanceof Uint8Array);
|
2024-02-19 10:24:22 +00:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Iterator uses an internal buffer of fixed size for efficiency; it returns
|
|
|
|
* a view on that buffer on each iteration. It is therefore caller's
|
|
|
|
* responsibility to copy contents of the buffer if needed; otherwise the
|
|
|
|
* next iteration will overwrite contents of previously returned chunk.
|
2024-08-08 14:20:43 +00:00
|
|
|
*
|
|
|
|
* @param reader The reader to read from
|
|
|
|
* @param options The options
|
|
|
|
* @returns The iterator of Uint8Array chunks
|
2024-02-19 10:24:22 +00:00
|
|
|
*/
|
|
|
|
export function* iterateReaderSync(
|
|
|
|
reader: ReaderSync,
|
|
|
|
options?: {
|
|
|
|
bufSize?: number;
|
|
|
|
},
|
|
|
|
): IterableIterator<Uint8Array> {
|
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
|
|
|
while (true) {
|
|
|
|
const result = reader.readSync(b);
|
|
|
|
if (result === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield b.slice(0, result);
|
|
|
|
}
|
|
|
|
}
|