-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support async iterables for response.body
Closes #267
- Loading branch information
Showing
4 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2018-2021 the oak authors. All rights reserved. MIT license. | ||
|
||
import { copyBytes } from "./deps.ts"; | ||
|
||
export class AsyncIterableReader<T> implements Deno.Reader { | ||
#asyncIterator: AsyncIterator<T>; | ||
#closed = false; | ||
#current: Uint8Array | undefined; | ||
#processValue: (value: T) => Uint8Array; | ||
|
||
constructor( | ||
asyncIterable: AsyncIterable<T>, | ||
processValue: (value: T) => Uint8Array, | ||
) { | ||
this.#asyncIterator = asyncIterable[Symbol.asyncIterator](); | ||
this.#processValue = processValue; | ||
} | ||
|
||
#close = () => { | ||
if (this.#asyncIterator.return) { | ||
this.#asyncIterator.return(); | ||
} | ||
// deno-lint-ignore no-explicit-any | ||
(this as any).#asyncIterator = undefined; | ||
this.#closed = true; | ||
}; | ||
|
||
async read(p: Uint8Array): Promise<number | null> { | ||
if (this.#closed) { | ||
return null; | ||
} | ||
if (p.byteLength === 0) { | ||
this.#close(); | ||
return 0; | ||
} | ||
if (!this.#current) { | ||
const { value, done } = await this.#asyncIterator.next(); | ||
if (done) { | ||
this.#close(); | ||
} | ||
if (value !== undefined) { | ||
this.#current = this.#processValue(value); | ||
} | ||
} | ||
if (!this.#current) { | ||
if (!this.#closed) { | ||
this.#close(); | ||
} | ||
return null; | ||
} | ||
const len = copyBytes(this.#current, p); | ||
if (len >= this.#current.byteLength) { | ||
this.#current = undefined; | ||
} else { | ||
this.#current = this.#current.slice(len); | ||
} | ||
return len; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2018-2021 the oak authors. All rights reserved. MIT license. | ||
|
||
import { assert, assertEquals, test } from "./test_deps.ts"; | ||
|
||
import { AsyncIterableReader } from "./async_iterable_reader.ts"; | ||
|
||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
|
||
test({ | ||
name: "AsyncIterableReader - basic", | ||
async fn() { | ||
const rs = new ReadableStream<string>({ | ||
start(controller) { | ||
controller.enqueue("hello deno"); | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
const air = new AsyncIterableReader(rs, encoder.encode); | ||
|
||
let buf = new Uint8Array(1000); | ||
let bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, 10); | ||
assert(decoder.decode(buf).startsWith("hello deno")); | ||
|
||
buf = new Uint8Array(1000); | ||
bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, null); | ||
}, | ||
}); | ||
|
||
test({ | ||
name: "AsyncIterableReader - multiple chunks", | ||
async fn() { | ||
const rs = new ReadableStream<string>({ | ||
start(controller) { | ||
controller.enqueue("hello"); | ||
controller.enqueue("deno"); | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
const air = new AsyncIterableReader(rs, encoder.encode); | ||
|
||
let buf = new Uint8Array(1000); | ||
let bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, 5); | ||
assert(decoder.decode(buf).startsWith("hello")); | ||
|
||
buf = new Uint8Array(1000); | ||
bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, 4); | ||
assert(decoder.decode(buf).startsWith("deno")); | ||
|
||
buf = new Uint8Array(1000); | ||
bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, null); | ||
}, | ||
}); | ||
|
||
test({ | ||
name: "AsyncIterableReader - overflow", | ||
async fn() { | ||
const rs = new ReadableStream<string>({ | ||
start(controller) { | ||
controller.enqueue("hello deno"); | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
const air = new AsyncIterableReader(rs, encoder.encode); | ||
|
||
let buf = new Uint8Array(5); | ||
let bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, 5); | ||
assert(decoder.decode(buf).startsWith("hello")); | ||
|
||
buf = new Uint8Array(5); | ||
bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, 5); | ||
assert(decoder.decode(buf).startsWith(" deno")); | ||
|
||
buf = new Uint8Array(5); | ||
bytesRead = await air.read(buf); | ||
assertEquals(bytesRead, null); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters