Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: edge case in toAsyncIterator #2335

merged 2 commits into from May 11, 2019
Changes from all commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.


Just for now

@@ -29,6 +29,38 @@ testPerm({ read: true }, async function filesToAsyncIterator(): Promise<void> {
assertEquals(totalSize, 12);

test(async function readerToAsyncIterator(): Promise<void> {
// ref:
const encoder = new TextEncoder();

class TestReader implements Deno.Reader {
private offset = 0;
private buf = new Uint8Array(encoder.encode(this.s));

constructor(private readonly s: string) {}

async read(p: Uint8Array): Promise<Deno.ReadResult> {
const n = Math.min(p.byteLength, this.buf.byteLength - this.offset);
p.set(this.buf.slice(this.offset, this.offset + n));
this.offset += n;

return {
nread: n,
eof: this.offset === this.buf.byteLength

const reader = new TestReader("hello world!");

let totalSize = 0;
for await (const buf of Deno.toAsyncIterator(reader)) {
totalSize += buf.byteLength;

assertEquals(totalSize, 12);

testPerm({ write: false }, async function writePermFailure(): Promise<void> {
const filename = "tests/hello.txt";
const writeModes: Deno.OpenMode[] = ["w", "a", "x"];
@@ -144,17 +144,31 @@ export async function copy(dst: Writer, src: Reader): Promise<number> {
export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array> {
const b = new Uint8Array(1024);
// Keep track if end-of-file has been reached, then
// signal that iterator is done during subsequent next()
// call. This is required because `r` can return a `ReadResult`
// with data read and EOF reached. But if iterator returns
// `done` then `value` is discarded.
// See for reference.
let sawEof = false;

return {
[Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> {
return this;

async next(): Promise<IteratorResult<Uint8Array>> {
if (sawEof) {
return { value: new Uint8Array(), done: true };
This conversation was marked as resolved by ry

This comment has been minimized.

Copy link

ry May 11, 2019


can “value” be left undefined here?

This comment has been minimized.

Copy link

bartlomieju May 11, 2019

Author Contributor

Not really, TS complains:

Type 'undefined' is not assignable to type 'Uint8Array'.

This comment has been minimized.

Copy link

ry May 11, 2019




const result = await;
sawEof = result.eof;

return {
value: b.subarray(0, result.nread),
done: result.eof
done: false
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.