From 5ce94bf4b40bcb44db98bf7fbc1928c15cf3826c Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 29 Jan 2024 14:16:53 +1100 Subject: [PATCH 1/7] feat(io): re-introduce `iterate()` and `iterateSync()` --- io/iterate.ts | 104 +++++++++++++++++++++++++++++++++++ io/iterate_test.ts | 111 ++++++++++++++++++++++++++++++++++++++ io/mod.ts | 1 + streams/iterate_reader.ts | 32 +++-------- 4 files changed, 223 insertions(+), 25 deletions(-) create mode 100644 io/iterate.ts create mode 100644 io/iterate_test.ts diff --git a/io/iterate.ts b/io/iterate.ts new file mode 100644 index 000000000000..5391e2db873d --- /dev/null +++ b/io/iterate.ts @@ -0,0 +1,104 @@ +// 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. + * + * @example + * ```ts + * import { iterate } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + * + * using file = await Deno.open("/etc/passwd"); + * for await (const chunk of iterate(file)) { + * console.log(chunk); + * } + * ``` + * + * Second argument can be used to tune size of a buffer. + * Default size of the buffer is 32kB. + * + * @example + * ```ts + * import { iterate } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + * + * using file = await Deno.open("/etc/passwd"); + * const iter = iterate(file, { + * bufSize: 1024 * 1024 + * }); + * for await (const chunk of iter) { + * console.log(chunk); + * } + * ``` + */ +export async function* iterate( + reader: Reader, + options?: { + bufSize?: number; + }, +): AsyncIterableIterator { + 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. + * + * ```ts + * import { iterateSync } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + * + * using file = Deno.openSync("/etc/passwd"); + * for (const chunk of iterateSync(file)) { + * console.log(chunk); + * } + * ``` + * + * Second argument can be used to tune size of a buffer. + * Default size of the buffer is 32kB. + * + * ```ts + * import { iterateSync } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + + * using file = await Deno.open("/etc/passwd"); + * const iter = iterateSync(file, { + * bufSize: 1024 * 1024 + * }); + * for (const chunk of iter) { + * console.log(chunk); + * } + * ``` + * + * 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. + */ +export function* iterateSync( + reader: ReaderSync, + options?: { + bufSize?: number; + }, +): IterableIterator { + 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); + } +} diff --git a/io/iterate_test.ts b/io/iterate_test.ts new file mode 100644 index 000000000000..018272943f3e --- /dev/null +++ b/io/iterate_test.ts @@ -0,0 +1,111 @@ +import { assertEquals } from "../assert/assert_equals.ts"; +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { iterate, iterateSync } from "./iterate.ts"; +import { readerFromIterable } from "../streams/reader_from_iterable.ts"; +import { delay } from "../async/delay.ts"; +import type { Reader, ReaderSync } from "../io/types.ts"; + +Deno.test("iterate()", async () => { + // ref: https://github.com/denoland/deno/issues/2330 + const encoder = new TextEncoder(); + + class TestReader implements Reader { + #offset = 0; + #buf: Uint8Array; + + constructor(s: string) { + this.#buf = new Uint8Array(encoder.encode(s)); + } + + read(p: Uint8Array): Promise { + const n = Math.min(p.byteLength, this.#buf.byteLength - this.#offset); + p.set(this.#buf.slice(this.#offset, this.#offset + n)); + this.#offset += n; + + if (n === 0) { + return Promise.resolve(null); + } + + return Promise.resolve(n); + } + } + + const reader = new TestReader("hello world!"); + + let totalSize = 0; + await Array.fromAsync( + iterate(reader), + (buf) => totalSize += buf.byteLength, + ); + + assertEquals(totalSize, 12); +}); + +Deno.test("iterate() works with slow consumer", async () => { + const a = new Uint8Array([97]); + const b = new Uint8Array([98]); + const iter = iterate(readerFromIterable([a, b])); + const promises = []; + for await (const bytes of iter) { + promises.push(delay(10).then(() => bytes)); + } + assertEquals([a, b], await Promise.all(promises)); +}); + +Deno.test("iterateSync()", () => { + // ref: https://github.com/denoland/deno/issues/2330 + const encoder = new TextEncoder(); + + class TestReader implements ReaderSync { + #offset = 0; + #buf: Uint8Array; + + constructor(s: string) { + this.#buf = new Uint8Array(encoder.encode(s)); + } + + readSync(p: Uint8Array): number | null { + const n = Math.min(p.byteLength, this.#buf.byteLength - this.#offset); + p.set(this.#buf.slice(this.#offset, this.#offset + n)); + this.#offset += n; + + if (n === 0) { + return null; + } + + return n; + } + } + + const reader = new TestReader("hello world!"); + + let totalSize = 0; + for (const buf of iterateSync(reader)) { + totalSize += buf.byteLength; + } + + assertEquals(totalSize, 12); +}); + +Deno.test("iterateSync() works with slow consumer", async () => { + const a = new Uint8Array([97]); + const b = new Uint8Array([98]); + const data = [a, b]; + const readerSync = { + readSync(u8: Uint8Array) { + const bytes = data.shift(); + if (bytes) { + u8.set(bytes); + return bytes.length; + } + return null; + }, + }; + const iter = iterateSync(readerSync); + const promises = []; + for (const bytes of iter) { + promises.push(delay(10).then(() => bytes)); + } + assertEquals([a, b], await Promise.all(promises)); +}); diff --git a/io/mod.ts b/io/mod.ts index c172819dfae3..b5bbca4a343c 100644 --- a/io/mod.ts +++ b/io/mod.ts @@ -14,6 +14,7 @@ export * from "./buf_writer.ts"; export * from "./buffer.ts"; export * from "./copy.ts"; export * from "./copy_n.ts"; +export * from "./iterate.ts"; export * from "./limited_reader.ts"; export * from "./multi_reader.ts"; export * from "./read_all.ts"; diff --git a/streams/iterate_reader.ts b/streams/iterate_reader.ts index 131f95fdbd84..de86902eb00e 100644 --- a/streams/iterate_reader.ts +++ b/streams/iterate_reader.ts @@ -1,7 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. -import { DEFAULT_BUFFER_SIZE } from "./_common.ts"; +import { iterate, iterateSync } from "../io/iterate.ts"; import type { Reader, ReaderSync } from "../io/types.ts"; export type { Reader, ReaderSync }; @@ -35,24 +35,15 @@ export type { Reader, ReaderSync }; * } * ``` * - * @deprecated (will be removed after 1.0.0) Use {@linkcode ReadableStreamDefaultReader} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode iterate} instead. */ -export async function* iterateReader( +export function iterateReader( r: Reader, options?: { bufSize?: number; }, ): AsyncIterableIterator { - const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE; - const b = new Uint8Array(bufSize); - while (true) { - const result = await r.read(b); - if (result === null) { - break; - } - - yield b.slice(0, result); - } + return iterate(r, options); } /** @@ -87,22 +78,13 @@ export async function* iterateReader( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. * - * @deprecated (will be removed after 1.0.0) Use {@linkcode ReadableStream} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode iterateSync} instead. */ -export function* iterateReaderSync( +export function iterateReaderSync( r: ReaderSync, options?: { bufSize?: number; }, ): IterableIterator { - const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE; - const b = new Uint8Array(bufSize); - while (true) { - const result = r.readSync(b); - if (result === null) { - break; - } - - yield b.slice(0, result); - } + return iterateSync(r, options); } From f3f4177c9ed3bccb8711a389c4aa291940e485b5 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 29 Jan 2024 17:03:03 +1100 Subject: [PATCH 2/7] rename to `toIterator()` --- io/mod.ts | 2 +- io/{iterate.ts => to_iterator.ts} | 20 ++++++++++---------- io/{iterate_test.ts => to_iterator_test.ts} | 20 ++++++++++---------- streams/iterate_reader.ts | 10 +++++----- 4 files changed, 26 insertions(+), 26 deletions(-) rename io/{iterate.ts => to_iterator.ts} (78%) rename io/{iterate_test.ts => to_iterator_test.ts} (82%) diff --git a/io/mod.ts b/io/mod.ts index b5bbca4a343c..cf02b102dc6c 100644 --- a/io/mod.ts +++ b/io/mod.ts @@ -14,7 +14,7 @@ export * from "./buf_writer.ts"; export * from "./buffer.ts"; export * from "./copy.ts"; export * from "./copy_n.ts"; -export * from "./iterate.ts"; +export * from "./to_iterator.ts"; export * from "./limited_reader.ts"; export * from "./multi_reader.ts"; export * from "./read_all.ts"; diff --git a/io/iterate.ts b/io/to_iterator.ts similarity index 78% rename from io/iterate.ts rename to io/to_iterator.ts index 5391e2db873d..fe98840f8a42 100644 --- a/io/iterate.ts +++ b/io/to_iterator.ts @@ -11,10 +11,10 @@ export type { Reader, ReaderSync }; * * @example * ```ts - * import { iterate } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + * import { toIterator } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; * * using file = await Deno.open("/etc/passwd"); - * for await (const chunk of iterate(file)) { + * for await (const chunk of toIterator(file)) { * console.log(chunk); * } * ``` @@ -24,10 +24,10 @@ export type { Reader, ReaderSync }; * * @example * ```ts - * import { iterate } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + * import { toIterator } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; * * using file = await Deno.open("/etc/passwd"); - * const iter = iterate(file, { + * const iter = toIterator(file, { * bufSize: 1024 * 1024 * }); * for await (const chunk of iter) { @@ -35,7 +35,7 @@ export type { Reader, ReaderSync }; * } * ``` */ -export async function* iterate( +export async function* toIterator( reader: Reader, options?: { bufSize?: number; @@ -57,10 +57,10 @@ export async function* iterate( * Turns a {@linkcode ReaderSync} into an iterator. * * ```ts - * import { iterateSync } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + * import { toIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; * * using file = Deno.openSync("/etc/passwd"); - * for (const chunk of iterateSync(file)) { + * for (const chunk of toIteratorSync(file)) { * console.log(chunk); * } * ``` @@ -69,10 +69,10 @@ export async function* iterate( * Default size of the buffer is 32kB. * * ```ts - * import { iterateSync } from "https://deno.land/std@$STD_VERSION/io/iterate.ts"; + * import { toIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; * using file = await Deno.open("/etc/passwd"); - * const iter = iterateSync(file, { + * const iter = toIteratorSync(file, { * bufSize: 1024 * 1024 * }); * for (const chunk of iter) { @@ -85,7 +85,7 @@ export async function* iterate( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. */ -export function* iterateSync( +export function* toIteratorSync( reader: ReaderSync, options?: { bufSize?: number; diff --git a/io/iterate_test.ts b/io/to_iterator_test.ts similarity index 82% rename from io/iterate_test.ts rename to io/to_iterator_test.ts index 018272943f3e..31f7482bbf7f 100644 --- a/io/iterate_test.ts +++ b/io/to_iterator_test.ts @@ -1,12 +1,12 @@ import { assertEquals } from "../assert/assert_equals.ts"; // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { iterate, iterateSync } from "./iterate.ts"; +import { toIterator, toIteratorSync } from "./to_iterator.ts"; import { readerFromIterable } from "../streams/reader_from_iterable.ts"; import { delay } from "../async/delay.ts"; -import type { Reader, ReaderSync } from "../io/types.ts"; +import type { Reader, ReaderSync } from "./types.ts"; -Deno.test("iterate()", async () => { +Deno.test("toIterator()", async () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder(); @@ -35,17 +35,17 @@ Deno.test("iterate()", async () => { let totalSize = 0; await Array.fromAsync( - iterate(reader), + toIterator(reader), (buf) => totalSize += buf.byteLength, ); assertEquals(totalSize, 12); }); -Deno.test("iterate() works with slow consumer", async () => { +Deno.test("toIterator() works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); - const iter = iterate(readerFromIterable([a, b])); + const iter = toIterator(readerFromIterable([a, b])); const promises = []; for await (const bytes of iter) { promises.push(delay(10).then(() => bytes)); @@ -53,7 +53,7 @@ Deno.test("iterate() works with slow consumer", async () => { assertEquals([a, b], await Promise.all(promises)); }); -Deno.test("iterateSync()", () => { +Deno.test("toIteratorSync()", () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder(); @@ -81,14 +81,14 @@ Deno.test("iterateSync()", () => { const reader = new TestReader("hello world!"); let totalSize = 0; - for (const buf of iterateSync(reader)) { + for (const buf of toIteratorSync(reader)) { totalSize += buf.byteLength; } assertEquals(totalSize, 12); }); -Deno.test("iterateSync() works with slow consumer", async () => { +Deno.test("toIteratorSync() works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); const data = [a, b]; @@ -102,7 +102,7 @@ Deno.test("iterateSync() works with slow consumer", async () => { return null; }, }; - const iter = iterateSync(readerSync); + const iter = toIteratorSync(readerSync); const promises = []; for (const bytes of iter) { promises.push(delay(10).then(() => bytes)); diff --git a/streams/iterate_reader.ts b/streams/iterate_reader.ts index de86902eb00e..a2f08a1e04b9 100644 --- a/streams/iterate_reader.ts +++ b/streams/iterate_reader.ts @@ -1,7 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. -import { iterate, iterateSync } from "../io/iterate.ts"; +import { toIterator, toIteratorSync } from "../io/to_iterator.ts"; import type { Reader, ReaderSync } from "../io/types.ts"; export type { Reader, ReaderSync }; @@ -35,7 +35,7 @@ export type { Reader, ReaderSync }; * } * ``` * - * @deprecated (will be removed in 0.216.0) Use {@linkcode iterate} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode toIterator} instead. */ export function iterateReader( r: Reader, @@ -43,7 +43,7 @@ export function iterateReader( bufSize?: number; }, ): AsyncIterableIterator { - return iterate(r, options); + return toIterator(r, options); } /** @@ -78,7 +78,7 @@ export function iterateReader( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. * - * @deprecated (will be removed in 0.216.0) Use {@linkcode iterateSync} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode toIteratorSync} instead. */ export function iterateReaderSync( r: ReaderSync, @@ -86,5 +86,5 @@ export function iterateReaderSync( bufSize?: number; }, ): IterableIterator { - return iterateSync(r, options); + return toIteratorSync(r, options); } From e687797c983330471ab5b6cbcd0deee6f2a0c5f7 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 29 Jan 2024 17:11:27 +1100 Subject: [PATCH 3/7] tweaks --- io/types.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/io/types.ts b/io/types.ts index 5bf45f057bf5..5e0fac6eae93 100644 --- a/io/types.ts +++ b/io/types.ts @@ -24,8 +24,9 @@ export interface Reader { * * Implementations should not retain a reference to `p`. * - * Use iterateReader() from https://deno.land/std@$STD_VERSION/streams/iterate_reader.ts to turn a Reader into an - * AsyncIterator. + * Use + * {@linkcode https://deno.land/std@$STD_VERSION/io/to_iterator.ts?s=toIterator} + * to turn a {@linkcode Reader} into an {@linkcode AsyncIterableIterator}. */ read(p: Uint8Array): Promise; } @@ -52,8 +53,9 @@ export interface ReaderSync { * * Implementations should not retain a reference to `p`. * - * Use iterateReaderSync() from https://deno.land/std@$STD_VERSION/streams/iterate_reader.ts to turn a ReaderSync - * into an Iterator. + * Use + * {@linkcode https://deno.land/std@$STD_VERSION/io/to_iterator.ts?s=toIteratorSync} + * to turn a {@linkcode ReaderSync} into an {@linkcode IterableIterator}. */ readSync(p: Uint8Array): number | null; } From 174ab81394072908224666baf43963bff36ae1c6 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 1 Feb 2024 09:14:33 +1100 Subject: [PATCH 4/7] rename to `toReaderIterator()` --- io/mod.ts | 2 +- io/{to_iterator.ts => to_reader_iterator.ts} | 20 +++++++++--------- ...tor_test.ts => to_reader_iterator_test.ts} | 21 +++++++++++-------- streams/iterate_reader.ts | 13 +++++++----- 4 files changed, 31 insertions(+), 25 deletions(-) rename io/{to_iterator.ts => to_reader_iterator.ts} (75%) rename io/{to_iterator_test.ts => to_reader_iterator_test.ts} (82%) diff --git a/io/mod.ts b/io/mod.ts index cf02b102dc6c..9b4ea3c0d22f 100644 --- a/io/mod.ts +++ b/io/mod.ts @@ -14,7 +14,7 @@ export * from "./buf_writer.ts"; export * from "./buffer.ts"; export * from "./copy.ts"; export * from "./copy_n.ts"; -export * from "./to_iterator.ts"; +export * from "./to_reader_iterator.ts"; export * from "./limited_reader.ts"; export * from "./multi_reader.ts"; export * from "./read_all.ts"; diff --git a/io/to_iterator.ts b/io/to_reader_iterator.ts similarity index 75% rename from io/to_iterator.ts rename to io/to_reader_iterator.ts index fe98840f8a42..40717e7918be 100644 --- a/io/to_iterator.ts +++ b/io/to_reader_iterator.ts @@ -11,10 +11,10 @@ export type { Reader, ReaderSync }; * * @example * ```ts - * import { toIterator } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; + * import { toReaderIterator } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; * * using file = await Deno.open("/etc/passwd"); - * for await (const chunk of toIterator(file)) { + * for await (const chunk of toReaderIterator(file)) { * console.log(chunk); * } * ``` @@ -24,10 +24,10 @@ export type { Reader, ReaderSync }; * * @example * ```ts - * import { toIterator } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; + * import { toReaderIterator } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; * * using file = await Deno.open("/etc/passwd"); - * const iter = toIterator(file, { + * const iter = toReaderIterator(file, { * bufSize: 1024 * 1024 * }); * for await (const chunk of iter) { @@ -35,7 +35,7 @@ export type { Reader, ReaderSync }; * } * ``` */ -export async function* toIterator( +export async function* toReaderIterator( reader: Reader, options?: { bufSize?: number; @@ -57,10 +57,10 @@ export async function* toIterator( * Turns a {@linkcode ReaderSync} into an iterator. * * ```ts - * import { toIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; + * import { toReaderIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; * * using file = Deno.openSync("/etc/passwd"); - * for (const chunk of toIteratorSync(file)) { + * for (const chunk of toReaderIteratorSync(file)) { * console.log(chunk); * } * ``` @@ -69,10 +69,10 @@ export async function* toIterator( * Default size of the buffer is 32kB. * * ```ts - * import { toIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_iterator.ts"; + * import { toReaderIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; * using file = await Deno.open("/etc/passwd"); - * const iter = toIteratorSync(file, { + * const iter = toReaderIteratorSync(file, { * bufSize: 1024 * 1024 * }); * for (const chunk of iter) { @@ -85,7 +85,7 @@ export async function* toIterator( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. */ -export function* toIteratorSync( +export function* toReaderIteratorSync( reader: ReaderSync, options?: { bufSize?: number; diff --git a/io/to_iterator_test.ts b/io/to_reader_iterator_test.ts similarity index 82% rename from io/to_iterator_test.ts rename to io/to_reader_iterator_test.ts index 31f7482bbf7f..18fcc6b5efff 100644 --- a/io/to_iterator_test.ts +++ b/io/to_reader_iterator_test.ts @@ -1,12 +1,15 @@ import { assertEquals } from "../assert/assert_equals.ts"; // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { toIterator, toIteratorSync } from "./to_iterator.ts"; +import { + toReaderIterator, + toReaderIteratorSync, +} from "./to_reader_iterator.ts"; import { readerFromIterable } from "../streams/reader_from_iterable.ts"; import { delay } from "../async/delay.ts"; import type { Reader, ReaderSync } from "./types.ts"; -Deno.test("toIterator()", async () => { +Deno.test("toReaderIterator()", async () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder(); @@ -35,17 +38,17 @@ Deno.test("toIterator()", async () => { let totalSize = 0; await Array.fromAsync( - toIterator(reader), + toReaderIterator(reader), (buf) => totalSize += buf.byteLength, ); assertEquals(totalSize, 12); }); -Deno.test("toIterator() works with slow consumer", async () => { +Deno.test("toReaderIterator() works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); - const iter = toIterator(readerFromIterable([a, b])); + const iter = toReaderIterator(readerFromIterable([a, b])); const promises = []; for await (const bytes of iter) { promises.push(delay(10).then(() => bytes)); @@ -53,7 +56,7 @@ Deno.test("toIterator() works with slow consumer", async () => { assertEquals([a, b], await Promise.all(promises)); }); -Deno.test("toIteratorSync()", () => { +Deno.test("toReaderIteratorSync()", () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder(); @@ -81,14 +84,14 @@ Deno.test("toIteratorSync()", () => { const reader = new TestReader("hello world!"); let totalSize = 0; - for (const buf of toIteratorSync(reader)) { + for (const buf of toReaderIteratorSync(reader)) { totalSize += buf.byteLength; } assertEquals(totalSize, 12); }); -Deno.test("toIteratorSync() works with slow consumer", async () => { +Deno.test("toReaderIteratorSync() works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); const data = [a, b]; @@ -102,7 +105,7 @@ Deno.test("toIteratorSync() works with slow consumer", async () => { return null; }, }; - const iter = toIteratorSync(readerSync); + const iter = toReaderIteratorSync(readerSync); const promises = []; for (const bytes of iter) { promises.push(delay(10).then(() => bytes)); diff --git a/streams/iterate_reader.ts b/streams/iterate_reader.ts index a2f08a1e04b9..560cac6c3aca 100644 --- a/streams/iterate_reader.ts +++ b/streams/iterate_reader.ts @@ -1,7 +1,10 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. -import { toIterator, toIteratorSync } from "../io/to_iterator.ts"; +import { + toReaderIterator, + toReaderIteratorSync, +} from "../io/to_reader_iterator.ts"; import type { Reader, ReaderSync } from "../io/types.ts"; export type { Reader, ReaderSync }; @@ -35,7 +38,7 @@ export type { Reader, ReaderSync }; * } * ``` * - * @deprecated (will be removed in 0.216.0) Use {@linkcode toIterator} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode toReaderIterator} instead. */ export function iterateReader( r: Reader, @@ -43,7 +46,7 @@ export function iterateReader( bufSize?: number; }, ): AsyncIterableIterator { - return toIterator(r, options); + return toReaderIterator(r, options); } /** @@ -78,7 +81,7 @@ export function iterateReader( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. * - * @deprecated (will be removed in 0.216.0) Use {@linkcode toIteratorSync} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode toReaderIteratorSync} instead. */ export function iterateReaderSync( r: ReaderSync, @@ -86,5 +89,5 @@ export function iterateReaderSync( bufSize?: number; }, ): IterableIterator { - return toIteratorSync(r, options); + return toReaderIteratorSync(r, options); } From e00196cd90319a4c44e55be909faebc37a893f72 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 5 Feb 2024 07:41:29 +1100 Subject: [PATCH 5/7] `iterateReader()` --- ...o_reader_iterator.ts => iterate_reader.ts} | 20 +++++++++--------- ...terator_test.ts => iterate_reader_test.ts} | 21 ++++++++----------- io/mod.ts | 2 +- streams/iterate_reader.ts | 14 ++++++------- 4 files changed, 27 insertions(+), 30 deletions(-) rename io/{to_reader_iterator.ts => iterate_reader.ts} (75%) rename io/{to_reader_iterator_test.ts => iterate_reader_test.ts} (82%) diff --git a/io/to_reader_iterator.ts b/io/iterate_reader.ts similarity index 75% rename from io/to_reader_iterator.ts rename to io/iterate_reader.ts index 40717e7918be..d9d370127405 100644 --- a/io/to_reader_iterator.ts +++ b/io/iterate_reader.ts @@ -11,10 +11,10 @@ export type { Reader, ReaderSync }; * * @example * ```ts - * import { toReaderIterator } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; + * import { iterateReader } from "https://deno.land/std@$STD_VERSION/io/iterate_reader.ts"; * * using file = await Deno.open("/etc/passwd"); - * for await (const chunk of toReaderIterator(file)) { + * for await (const chunk of iterateReader(file)) { * console.log(chunk); * } * ``` @@ -24,10 +24,10 @@ export type { Reader, ReaderSync }; * * @example * ```ts - * import { toReaderIterator } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; + * import { iterateReader } from "https://deno.land/std@$STD_VERSION/io/iterate_reader.ts"; * * using file = await Deno.open("/etc/passwd"); - * const iter = toReaderIterator(file, { + * const iter = iterateReader(file, { * bufSize: 1024 * 1024 * }); * for await (const chunk of iter) { @@ -35,7 +35,7 @@ export type { Reader, ReaderSync }; * } * ``` */ -export async function* toReaderIterator( +export async function* iterateReader( reader: Reader, options?: { bufSize?: number; @@ -57,10 +57,10 @@ export async function* toReaderIterator( * Turns a {@linkcode ReaderSync} into an iterator. * * ```ts - * import { toReaderIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; + * import { iterateReaderSync } from "https://deno.land/std@$STD_VERSION/io/iterate_reader.ts"; * * using file = Deno.openSync("/etc/passwd"); - * for (const chunk of toReaderIteratorSync(file)) { + * for (const chunk of iterateReaderSync(file)) { * console.log(chunk); * } * ``` @@ -69,10 +69,10 @@ export async function* toReaderIterator( * Default size of the buffer is 32kB. * * ```ts - * import { toReaderIteratorSync } from "https://deno.land/std@$STD_VERSION/io/to_reader_iterator.ts"; + * import { iterateReaderSync } from "https://deno.land/std@$STD_VERSION/io/iterate_reader.ts"; * using file = await Deno.open("/etc/passwd"); - * const iter = toReaderIteratorSync(file, { + * const iter = iterateReaderSync(file, { * bufSize: 1024 * 1024 * }); * for (const chunk of iter) { @@ -85,7 +85,7 @@ export async function* toReaderIterator( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. */ -export function* toReaderIteratorSync( +export function* iterateReaderSync( reader: ReaderSync, options?: { bufSize?: number; diff --git a/io/to_reader_iterator_test.ts b/io/iterate_reader_test.ts similarity index 82% rename from io/to_reader_iterator_test.ts rename to io/iterate_reader_test.ts index 18fcc6b5efff..b2adea11051b 100644 --- a/io/to_reader_iterator_test.ts +++ b/io/iterate_reader_test.ts @@ -1,15 +1,12 @@ import { assertEquals } from "../assert/assert_equals.ts"; // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { - toReaderIterator, - toReaderIteratorSync, -} from "./to_reader_iterator.ts"; +import { iterateReader, iterateReaderSync } from "./iterate_reader.ts"; import { readerFromIterable } from "../streams/reader_from_iterable.ts"; import { delay } from "../async/delay.ts"; import type { Reader, ReaderSync } from "./types.ts"; -Deno.test("toReaderIterator()", async () => { +Deno.test("iterateReader()", async () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder(); @@ -38,17 +35,17 @@ Deno.test("toReaderIterator()", async () => { let totalSize = 0; await Array.fromAsync( - toReaderIterator(reader), + iterateReader(reader), (buf) => totalSize += buf.byteLength, ); assertEquals(totalSize, 12); }); -Deno.test("toReaderIterator() works with slow consumer", async () => { +Deno.test("iterateReader() works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); - const iter = toReaderIterator(readerFromIterable([a, b])); + const iter = iterateReader(readerFromIterable([a, b])); const promises = []; for await (const bytes of iter) { promises.push(delay(10).then(() => bytes)); @@ -56,7 +53,7 @@ Deno.test("toReaderIterator() works with slow consumer", async () => { assertEquals([a, b], await Promise.all(promises)); }); -Deno.test("toReaderIteratorSync()", () => { +Deno.test("iterateReaderSync()", () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder(); @@ -84,14 +81,14 @@ Deno.test("toReaderIteratorSync()", () => { const reader = new TestReader("hello world!"); let totalSize = 0; - for (const buf of toReaderIteratorSync(reader)) { + for (const buf of iterateReaderSync(reader)) { totalSize += buf.byteLength; } assertEquals(totalSize, 12); }); -Deno.test("toReaderIteratorSync() works with slow consumer", async () => { +Deno.test("iterateReaderSync() works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); const data = [a, b]; @@ -105,7 +102,7 @@ Deno.test("toReaderIteratorSync() works with slow consumer", async () => { return null; }, }; - const iter = toReaderIteratorSync(readerSync); + const iter = iterateReaderSync(readerSync); const promises = []; for (const bytes of iter) { promises.push(delay(10).then(() => bytes)); diff --git a/io/mod.ts b/io/mod.ts index 9b4ea3c0d22f..8885e99f6e7b 100644 --- a/io/mod.ts +++ b/io/mod.ts @@ -14,7 +14,7 @@ export * from "./buf_writer.ts"; export * from "./buffer.ts"; export * from "./copy.ts"; export * from "./copy_n.ts"; -export * from "./to_reader_iterator.ts"; +export * from "./iterate_reader.ts"; export * from "./limited_reader.ts"; export * from "./multi_reader.ts"; export * from "./read_all.ts"; diff --git a/streams/iterate_reader.ts b/streams/iterate_reader.ts index 560cac6c3aca..820acf37665a 100644 --- a/streams/iterate_reader.ts +++ b/streams/iterate_reader.ts @@ -2,9 +2,9 @@ // This module is browser compatible. import { - toReaderIterator, - toReaderIteratorSync, -} from "../io/to_reader_iterator.ts"; + iterateReader as _iterateReader, + iterateReaderSync as _iterateReaderSync, +} from "../io/iterate_reader.ts"; import type { Reader, ReaderSync } from "../io/types.ts"; export type { Reader, ReaderSync }; @@ -38,7 +38,7 @@ export type { Reader, ReaderSync }; * } * ``` * - * @deprecated (will be removed in 0.216.0) Use {@linkcode toReaderIterator} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode iterateReader} instead. */ export function iterateReader( r: Reader, @@ -46,7 +46,7 @@ export function iterateReader( bufSize?: number; }, ): AsyncIterableIterator { - return toReaderIterator(r, options); + return _iterateReader(r, options); } /** @@ -81,7 +81,7 @@ export function iterateReader( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. * - * @deprecated (will be removed in 0.216.0) Use {@linkcode toReaderIteratorSync} instead. + * @deprecated (will be removed in 0.216.0) Use {@linkcode iterateReaderSync} instead. */ export function iterateReaderSync( r: ReaderSync, @@ -89,5 +89,5 @@ export function iterateReaderSync( bufSize?: number; }, ): IterableIterator { - return toReaderIteratorSync(r, options); + return _iterateReaderSync(r, options); } From a6bc5f0013a2358fbb65f49538cd6742227e7e93 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 19 Feb 2024 20:54:45 +1100 Subject: [PATCH 6/7] tweak --- streams/iterate_reader.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/streams/iterate_reader.ts b/streams/iterate_reader.ts index 820acf37665a..11c1e247607c 100644 --- a/streams/iterate_reader.ts +++ b/streams/iterate_reader.ts @@ -38,7 +38,7 @@ export type { Reader, ReaderSync }; * } * ``` * - * @deprecated (will be removed in 0.216.0) Use {@linkcode iterateReader} instead. + * @deprecated (will be removed in 1.0.0) Use {@linkcode iterateReader} instead. */ export function iterateReader( r: Reader, @@ -81,7 +81,7 @@ export function iterateReader( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. * - * @deprecated (will be removed in 0.216.0) Use {@linkcode iterateReaderSync} instead. + * @deprecated (will be removed in 1.0.0) Use {@linkcode iterateReaderSync} instead. */ export function iterateReaderSync( r: ReaderSync, From 76e7f5302197c2c28a0058a507bfa0d9ac70de64 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 19 Feb 2024 21:01:07 +1100 Subject: [PATCH 7/7] tweak --- streams/iterate_reader.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/streams/iterate_reader.ts b/streams/iterate_reader.ts index 11c1e247607c..2057ce0dbdf3 100644 --- a/streams/iterate_reader.ts +++ b/streams/iterate_reader.ts @@ -38,7 +38,7 @@ export type { Reader, ReaderSync }; * } * ``` * - * @deprecated (will be removed in 1.0.0) Use {@linkcode iterateReader} instead. + * @deprecated (will be removed in 1.0.0) Import from {@link https://deno.land/std/io/iterate_reader.ts} instead. */ export function iterateReader( r: Reader, @@ -81,7 +81,7 @@ export function iterateReader( * responsibility to copy contents of the buffer if needed; otherwise the * next iteration will overwrite contents of previously returned chunk. * - * @deprecated (will be removed in 1.0.0) Use {@linkcode iterateReaderSync} instead. + * @deprecated (will be removed in 1.0.0) Import from {@link https://deno.land/std/io/iterate_reader.ts} instead. */ export function iterateReaderSync( r: ReaderSync,