-
Notifications
You must be signed in to change notification settings - Fork 30.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! streams: implement streams to webstreams adapters
- Loading branch information
Showing
3 changed files
with
178 additions
and
12 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
129 changes: 129 additions & 0 deletions
129
test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js
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,129 @@ | ||
// Flags: --no-warnings | ||
|
||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const assert = require('assert'); | ||
|
||
const { | ||
newWritableStreamFromStreamWritable, | ||
} = require('stream/web'); | ||
|
||
const { | ||
Writable, | ||
} = require('stream'); | ||
|
||
class TestWritable extends Writable { | ||
constructor(asyncWrite = false) { | ||
super(); | ||
this.chunks = []; | ||
this.asyncWrite = asyncWrite; | ||
} | ||
|
||
_write(chunk, encoding, callback) { | ||
this.chunks.push({ chunk, encoding }); | ||
if (this.asyncWrite) { | ||
setImmediate(() => callback()); | ||
return; | ||
} | ||
callback(); | ||
} | ||
} | ||
|
||
{ | ||
// Closing the WritableStream normally closes the stream.Writable | ||
// without errors. | ||
|
||
const writable = new TestWritable(); | ||
writable.on('error', common.mustNotCall()); | ||
writable.on('finish', common.mustCall()); | ||
writable.on('close', common.mustCall()); | ||
|
||
const writableStream = newWritableStreamFromStreamWritable(writable); | ||
|
||
writableStream.close().then(common.mustCall(() => { | ||
assert(writable.destroyed); | ||
})); | ||
} | ||
|
||
{ | ||
// Aborting the WritableStream errors the stream.Writable | ||
|
||
const error = new Error('boom'); | ||
const writable = new TestWritable(); | ||
writable.on('error', common.mustCall((reason) => { | ||
assert.strictEqual(reason, error); | ||
})); | ||
writable.on('finish', common.mustNotCall()); | ||
writable.on('close', common.mustCall()); | ||
|
||
const writableStream = newWritableStreamFromStreamWritable(writable); | ||
|
||
writableStream.abort(error).then(common.mustCall(() => { | ||
assert(writable.destroyed); | ||
})); | ||
} | ||
|
||
{ | ||
// Destroying the stream.Writable prematurely errors the | ||
// WritableStream | ||
|
||
const error = new Error('boom'); | ||
const writable = new TestWritable(); | ||
|
||
const writableStream = newWritableStreamFromStreamWritable(writable); | ||
assert.rejects(writableStream.close(), error); | ||
writable.destroy(error); | ||
} | ||
|
||
{ | ||
// Ending the stream.Writable directly errors the WritableStream | ||
const writable = new TestWritable(); | ||
|
||
const writableStream = newWritableStreamFromStreamWritable(writable); | ||
|
||
assert.rejects(writableStream.close(), { | ||
code: 'ERR_INVALID_STATE' | ||
}); | ||
|
||
writable.end(); | ||
} | ||
|
||
{ | ||
const writable = new TestWritable(); | ||
const writableStream = newWritableStreamFromStreamWritable(writable); | ||
const writer = writableStream.getWriter(); | ||
const ec = new TextEncoder(); | ||
writer.write(ec.encode('hello')).then(common.mustCall(() => { | ||
assert.strictEqual(writable.chunks.length, 1); | ||
assert.deepStrictEqual( | ||
writable.chunks[0], | ||
{ | ||
chunk: Buffer.from('hello'), | ||
encoding: 'buffer' | ||
}); | ||
})); | ||
} | ||
|
||
{ | ||
const writable = new TestWritable(true); | ||
|
||
writable.on('error', common.mustNotCall()); | ||
writable.on('close', common.mustCall()); | ||
writable.on('finish', common.mustCall()); | ||
|
||
const writableStream = newWritableStreamFromStreamWritable(writable); | ||
const writer = writableStream.getWriter(); | ||
const ec = new TextEncoder(); | ||
writer.write(ec.encode('hello')).then(common.mustCall(() => { | ||
assert.strictEqual(writable.chunks.length, 1); | ||
assert.deepStrictEqual( | ||
writable.chunks[0], | ||
{ | ||
chunk: Buffer.from('hello'), | ||
encoding: 'buffer' | ||
}); | ||
writer.close().then(common.mustCall()); | ||
})); | ||
} |