forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Returns a promise fulfilling with a sensible representation of the entire stream's data, depending on the stream type. Buffer streams collect to a single contiguous Buffer. Buffer streams with encoding set collect to a single contiguous string. Object-mode streams collect to an array of objects. Limiting is allowed, enabling situations like capping request body sizes, or only consuming a limited amount of process.stdin. Ref: nodejs#35192 Ref: nodejs/tooling#68
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 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,127 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const { Readable } = require('stream'); | ||
const assert = require('assert'); | ||
|
||
const hello = Buffer.from('hello, '); | ||
const world = Buffer.from('world'); | ||
|
||
async function tests() { | ||
{ // buffer, no limit | ||
const r = new Readable(); | ||
|
||
r.push(hello); | ||
r.push(world); | ||
r.push(null); | ||
|
||
const result = await r.collect(); | ||
|
||
assert.strictEqual(result.compare(Buffer.from('hello, world')), 0); | ||
} | ||
|
||
{ // buffer, under limit | ||
const r = new Readable(); | ||
|
||
r.push(hello); | ||
r.push(world); | ||
r.push(null); | ||
|
||
await assert.doesNotReject(r.collect(50)); | ||
} | ||
|
||
{ // buffer, over limit | ||
const r = new Readable(); | ||
r.push(hello); | ||
r.push(world); | ||
r.push(null); | ||
|
||
await assert.rejects(r.collect(6), { | ||
code: 'ERR_STREAM_COLLECT_LIMIT_EXCEEDED', | ||
}); | ||
} | ||
|
||
{ // buffer, setEncoding, no limit | ||
const r = new Readable(); | ||
|
||
r.setEncoding('utf8'); | ||
|
||
r.push(hello); | ||
r.push(world); | ||
r.push(null); | ||
|
||
const result = await r.collect(); | ||
|
||
assert.strictEqual(result, 'hello, world'); | ||
} | ||
|
||
{ // buffer, setEncoding, under limit | ||
const r = new Readable(); | ||
|
||
r.setEncoding('utf8'); | ||
|
||
r.push(hello); | ||
r.push(world); | ||
r.push(null); | ||
|
||
await assert.doesNotReject(r.collect(50)); | ||
} | ||
|
||
{ // buffer, setEncoding, over limit | ||
const r = new Readable(); | ||
|
||
r.setEncoding('utf8'); | ||
|
||
r.push(hello); | ||
r.push(world); | ||
r.push(null); | ||
|
||
await assert.rejects(r.collect(6), { | ||
code: 'ERR_STREAM_COLLECT_LIMIT_EXCEEDED', | ||
}); | ||
} | ||
|
||
{ // object, no limit | ||
const r = new Readable({ objectMode: true }); | ||
|
||
const objs = [{ n: 0 }, { n: 1 }]; | ||
|
||
r.push(objs[0]); | ||
r.push(objs[1]); | ||
r.push(null); | ||
|
||
const result = await r.collect(); | ||
|
||
assert.deepStrictEqual(result, objs); | ||
} | ||
|
||
{ // object, under limit | ||
const r = new Readable({ objectMode: true }); | ||
|
||
const objs = [{ n: 0 }, { n: 1 }]; | ||
|
||
r.push(objs[0]); | ||
r.push(objs[1]); | ||
r.push(null); | ||
|
||
assert.doesNotReject(r.collect(5)); | ||
} | ||
|
||
{ // object, over limit | ||
const r = new Readable({ objectMode: true }); | ||
|
||
const objs = [{ n: 0 }, { n: 1 }, { n: 2 }]; | ||
|
||
r.push(objs[0]); | ||
r.push(objs[1]); | ||
r.push(objs[2]); | ||
r.push(null); | ||
|
||
await assert.rejects(r.collect(2), { | ||
code: 'ERR_STREAM_COLLECT_LIMIT_EXCEEDED', | ||
}); | ||
} | ||
} | ||
|
||
tests().then(common.mustCall()); |