-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): introduces stream utilities
- Loading branch information
Showing
6 changed files
with
174 additions
and
67 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './handler' | ||
export * from './headers' | ||
export * from './stream' |
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,76 @@ | ||
import { Readable } from 'node:stream' | ||
|
||
interface FromWebOptions { | ||
objectMode?: boolean | ||
highWaterMark?: number | ||
encoding?: BufferEncoding | ||
signal?: AbortSignal | ||
} | ||
|
||
/** | ||
* Code adapted from Node's stream.Readable.fromWeb() | ||
* @see https://github.com/nodejs/node/blob/bd462ad81bc30e547e52e699ee3b6fa3d7c882c9/lib/internal/webstreams/adapters.js#L458 | ||
*/ | ||
export function transformToReadable( | ||
webStream: ReadableStream, | ||
options: FromWebOptions = {} | ||
) { | ||
const reader = webStream.getReader() | ||
let closed = false | ||
const { highWaterMark, encoding, objectMode = false, signal } = options | ||
|
||
const readable = new Readable({ | ||
objectMode, | ||
highWaterMark, | ||
encoding, | ||
// @ts-ignore signal exist only since Node@17 | ||
signal, | ||
read() { | ||
reader.read().then( | ||
(chunk: any) => { | ||
if (chunk.done) { | ||
readable.push(null) | ||
} else { | ||
readable.push(chunk.value) | ||
} | ||
}, | ||
(error: any) => readable.destroy(error) | ||
) | ||
}, | ||
|
||
destroy(error: any, callback: (arg0: any) => void) { | ||
function done() { | ||
try { | ||
callback(error) | ||
} catch (error) { | ||
// In a next tick because this is happening within | ||
// a promise context, and if there are any errors | ||
// thrown we don't want those to cause an unhandled | ||
// rejection. Let's just escape the promise and | ||
// handle it separately. | ||
process.nextTick(() => { | ||
throw error | ||
}) | ||
} | ||
} | ||
|
||
if (!closed) { | ||
reader.cancel(error).then(done, done) | ||
return | ||
} | ||
done() | ||
}, | ||
}) | ||
|
||
reader.closed.then( | ||
() => { | ||
closed = true | ||
}, | ||
(error: any) => { | ||
closed = true | ||
readable.destroy(error) | ||
} | ||
) | ||
|
||
return readable | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ReadableStream } from '@edge-runtime/primitives' | ||
import { Readable } from 'node:stream' | ||
import { transformToReadable } from '../../src' | ||
|
||
describe('transformToReadable()', () => { | ||
it('handles a web ReadableStream', async () => { | ||
const readableStream = new ReadableStream({ | ||
async start(controller) { | ||
const encoder = new TextEncoder() | ||
controller.enqueue(encoder.encode('hello')) | ||
await new Promise((resolve) => setTimeout(resolve, 200)) | ||
controller.enqueue(encoder.encode(' world')) | ||
controller.close() | ||
}, | ||
}) | ||
|
||
const readable = transformToReadable(readableStream) | ||
expect((await transformToBuffer(readable)).toString()).toEqual( | ||
'hello world' | ||
) | ||
}) | ||
}) | ||
|
||
async function transformToBuffer(stream: Readable) { | ||
const buffers = [] | ||
for await (const data of stream) { | ||
buffers.push(data) | ||
} | ||
return Buffer.concat(buffers) | ||
} |