-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: use .chunk when calling adapters's writev
Fix: #42157 PR-URL: #42161 Fixes: #42157 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
cd914a5
commit 352cbd0
Showing
3 changed files
with
406 additions
and
4 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
166 changes: 166 additions & 0 deletions
166
test/parallel/test-whatwg-webstreams-adapters-to-streamduplex.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,166 @@ | ||
// Flags: --no-warnings --expose-internals | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const assert = require('assert'); | ||
|
||
const { | ||
TransformStream, | ||
} = require('stream/web'); | ||
|
||
const { | ||
newStreamDuplexFromReadableWritablePair, | ||
} = require('internal/webstreams/adapters'); | ||
|
||
const { | ||
finished, | ||
pipeline, | ||
Readable, | ||
Writable, | ||
} = require('stream'); | ||
|
||
const { | ||
kState, | ||
} = require('internal/webstreams/util'); | ||
|
||
{ | ||
const transform = new TransformStream(); | ||
const duplex = newStreamDuplexFromReadableWritablePair(transform); | ||
|
||
assert(transform.readable.locked); | ||
assert(transform.writable.locked); | ||
|
||
duplex.destroy(); | ||
|
||
duplex.on('close', common.mustCall(() => { | ||
assert.strictEqual(transform.readable[kState].state, 'closed'); | ||
assert.strictEqual(transform.writable[kState].state, 'errored'); | ||
})); | ||
} | ||
|
||
{ | ||
const error = new Error('boom'); | ||
const transform = new TransformStream(); | ||
const duplex = newStreamDuplexFromReadableWritablePair(transform); | ||
|
||
assert(transform.readable.locked); | ||
assert(transform.writable.locked); | ||
|
||
duplex.destroy(error); | ||
duplex.on('error', common.mustCall((reason) => { | ||
assert.strictEqual(reason, error); | ||
})); | ||
|
||
duplex.on('close', common.mustCall(() => { | ||
assert.strictEqual(transform.readable[kState].state, 'closed'); | ||
assert.strictEqual(transform.writable[kState].state, 'errored'); | ||
assert.strictEqual(transform.writable[kState].storedError, error); | ||
})); | ||
} | ||
|
||
{ | ||
const transform = new TransformStream(); | ||
const duplex = new newStreamDuplexFromReadableWritablePair(transform); | ||
|
||
duplex.end(); | ||
duplex.resume(); | ||
|
||
duplex.on('close', common.mustCall(() => { | ||
assert.strictEqual(transform.readable[kState].state, 'closed'); | ||
assert.strictEqual(transform.writable[kState].state, 'closed'); | ||
})); | ||
} | ||
|
||
{ | ||
const ec = new TextEncoder(); | ||
const dc = new TextDecoder(); | ||
const transform = new TransformStream({ | ||
transform(chunk, controller) { | ||
const text = dc.decode(chunk); | ||
controller.enqueue(ec.encode(text.toUpperCase())); | ||
} | ||
}); | ||
const duplex = new newStreamDuplexFromReadableWritablePair(transform, { | ||
encoding: 'utf8', | ||
}); | ||
|
||
duplex.end('hello'); | ||
duplex.on('data', common.mustCall((chunk) => { | ||
assert.strictEqual(chunk, 'HELLO'); | ||
})); | ||
duplex.on('end', common.mustCall()); | ||
|
||
duplex.on('close', common.mustCall(() => { | ||
assert.strictEqual(transform.readable[kState].state, 'closed'); | ||
assert.strictEqual(transform.writable[kState].state, 'closed'); | ||
})); | ||
} | ||
|
||
{ | ||
const ec = new TextEncoder(); | ||
const dc = new TextDecoder(); | ||
const transform = new TransformStream({ | ||
transform: common.mustCall((chunk, controller) => { | ||
const text = dc.decode(chunk); | ||
controller.enqueue(ec.encode(text.toUpperCase())); | ||
}) | ||
}); | ||
const duplex = new newStreamDuplexFromReadableWritablePair(transform, { | ||
encoding: 'utf8', | ||
}); | ||
|
||
finished(duplex, common.mustCall()); | ||
|
||
duplex.end('hello'); | ||
duplex.resume(); | ||
} | ||
|
||
{ | ||
const ec = new TextEncoder(); | ||
const dc = new TextDecoder(); | ||
const transform = new TransformStream({ | ||
transform: common.mustCall((chunk, controller) => { | ||
const text = dc.decode(chunk); | ||
controller.enqueue(ec.encode(text.toUpperCase())); | ||
}) | ||
}); | ||
const duplex = new newStreamDuplexFromReadableWritablePair(transform, { | ||
encoding: 'utf8', | ||
}); | ||
|
||
const readable = new Readable({ | ||
read() { | ||
readable.push(Buffer.from('hello')); | ||
readable.push(null); | ||
} | ||
}); | ||
|
||
const writable = new Writable({ | ||
write: common.mustCall((chunk, encoding, callback) => { | ||
assert.strictEqual(dc.decode(chunk), 'HELLO'); | ||
assert.strictEqual(encoding, 'buffer'); | ||
callback(); | ||
}) | ||
}); | ||
|
||
finished(duplex, common.mustCall()); | ||
pipeline(readable, duplex, writable, common.mustCall()); | ||
} | ||
|
||
{ | ||
const transform = new TransformStream(); | ||
const duplex = newStreamDuplexFromReadableWritablePair(transform); | ||
duplex.setEncoding('utf-8'); | ||
duplex.on('data', common.mustCall((data) => { | ||
assert.strictEqual(data, 'hello'); | ||
}, 5)); | ||
|
||
duplex.write(Buffer.from('hello')); | ||
duplex.write(Buffer.from('hello')); | ||
duplex.write(Buffer.from('hello')); | ||
duplex.write(Buffer.from('hello')); | ||
duplex.write(Buffer.from('hello')); | ||
|
||
duplex.end(); | ||
} |
Oops, something went wrong.