-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: initial approach to include strategy options on Readable.toWeb()
PR-URL: #43515 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
f32aec8
commit a057510
Showing
4 changed files
with
109 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
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,75 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const { Readable } = require('stream'); | ||
const assert = require('assert'); | ||
const { strictEqual } = require('assert'); | ||
|
||
{ | ||
// Strategy 2 | ||
const streamData = ['a', 'b', 'c', null]; | ||
|
||
// Fulfill a Readable object | ||
const readable = new Readable({ | ||
read: common.mustCall(() => { | ||
process.nextTick(() => { | ||
readable.push(streamData.shift()); | ||
}); | ||
}, streamData.length), | ||
}); | ||
|
||
// Use helper to convert it to a Web ReadableStream using ByteLength strategy | ||
const readableStream = Readable.toWeb(readable, { | ||
strategy: new ByteLengthQueuingStrategy({ highWaterMark: 1 }), | ||
}); | ||
|
||
assert(!readableStream.locked); | ||
readableStream.getReader().read().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Strategy 2 | ||
const streamData = ['a', 'b', 'c', null]; | ||
|
||
// Fulfill a Readable object | ||
const readable = new Readable({ | ||
read: common.mustCall(() => { | ||
process.nextTick(() => { | ||
readable.push(streamData.shift()); | ||
}); | ||
}, streamData.length), | ||
}); | ||
|
||
// Use helper to convert it to a Web ReadableStream using Count strategy | ||
const readableStream = Readable.toWeb(readable, { | ||
strategy: new CountQueuingStrategy({ highWaterMark: 1 }), | ||
}); | ||
|
||
assert(!readableStream.locked); | ||
readableStream.getReader().read().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
const desireSizeExpected = 2; | ||
|
||
const stringStream = new ReadableStream( | ||
{ | ||
start(controller) { | ||
// Check if the strategy is being assigned on the init of the ReadableStream | ||
strictEqual(controller.desiredSize, desireSizeExpected); | ||
controller.enqueue('a'); | ||
controller.enqueue('b'); | ||
controller.close(); | ||
}, | ||
}, | ||
new CountQueuingStrategy({ highWaterMark: desireSizeExpected }) | ||
); | ||
|
||
const reader = stringStream.getReader(); | ||
|
||
reader.read().then(common.mustCall()); | ||
reader.read().then(common.mustCall()); | ||
reader.read().then(({ value, done }) => { | ||
strictEqual(value, undefined); | ||
strictEqual(done, true); | ||
}); | ||
} |