GeezerVison is showing github.com — stream: readable read one buffer at a time · nodejs/node@fadb214
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/readable.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -636,9 +636,14 @@ function howMuchToRead(n, state) { | 636 | 636 | if ((state[kState] & kObjectMode) !== 0) | 637 | 637 | return 1; | 638 | 638 | if (NumberIsNaN(n)) { | | 639 | + // Fast path for buffers. | | 640 | + if ((state[kState] & kDecoder) === 0 && state.length) | | 641 | + return state.buffer[state.bufferIndex].length; | | 642 | + | 639 | 643 | // Only flow one buffer at a time. | 640 | 644 | if ((state[kState] & kFlowing) !== 0 && state.length) | 641 | 645 | return state.buffer[state.bufferIndex].length; | | 646 | + | 642 | 647 | return state.length; | 643 | 648 | } | 644 | 649 | if (n <= state.length) | |
Collapse file Copy file name to clipboardExpand all lines: test/common/heap.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,8 @@ const { getHeapSnapshot } = require('v8'); | 21 | 21 |
| 22 | 22 | function createJSHeapSnapshot(stream = getHeapSnapshot()) { | 23 | 23 | stream.pause(); | 24 | | - const dump = JSON.parse(stream.read()); | | 24 | + stream.read(0); | | 25 | + const dump = JSON.parse(stream.read(stream.readableLength)); | 25 | 26 | const meta = dump.snapshot.meta; | 26 | 27 |
| 27 | 28 | const nodes = | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-cipheriv-decipheriv.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,11 +31,11 @@ function testCipher1(key, iv) { | 31 | 31 | // quite small, so there's no harm. | 32 | 32 | const cStream = crypto.createCipheriv('des-ede3-cbc', key, iv); | 33 | 33 | cStream.end(plaintext); | 34 | | - ciph = cStream.read(); | | 34 | + ciph = cStream.read(cStream.readableLength); | 35 | 35 |
| 36 | 36 | const dStream = crypto.createDecipheriv('des-ede3-cbc', key, iv); | 37 | 37 | dStream.end(ciph); | 38 | | - txt = dStream.read().toString('utf8'); | | 38 | + txt = dStream.read(dStream.readableLength).toString('utf8'); | 39 | 39 |
| 40 | 40 | assert.strictEqual(txt, plaintext, | 41 | 41 | `streaming cipher with key ${key} and iv ${iv}`); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-runner-run.mjs +16-18Lines changed: 16 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ import * as fixtures from '../common/fixtures.mjs'; | 3 | 3 | import { join } from 'node:path'; | 4 | 4 | import { describe, it, run } from 'node:test'; | 5 | 5 | import { dot, spec, tap } from 'node:test/reporters'; | | 6 | +import consumers from 'node:stream/consumers'; | 6 | 7 | import assert from 'node:assert'; | 7 | 8 | import util from 'node:util'; | 8 | 9 |
| @@ -111,34 +112,31 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { | 111 | 112 | describe('should be piped with spec reporter', () => { | 112 | 113 | it('new spec', async () => { | 113 | 114 | const specReporter = new spec(); | 114 | | - const result = await run({ | | 115 | + const result = await consumers.text(run({ | 115 | 116 | files: [join(testFixtures, 'default-behavior/test/random.cjs')] | 116 | | - }).compose(specReporter).toArray(); | 117 | | - const stringResults = result.map((bfr) => bfr.toString()); | 118 | | - assert.match(stringResults[0], /this should pass/); | 119 | | - assert.match(stringResults[1], /tests 1/); | 120 | | - assert.match(stringResults[1], /pass 1/); | | 117 | + }).compose(specReporter)); | | 118 | + assert.match(result, /this should pass/); | | 119 | + assert.match(result, /tests 1/); | | 120 | + assert.match(result, /pass 1/); | 121 | 121 | }); | 122 | 122 |
| 123 | 123 | it('spec()', async () => { | 124 | 124 | const specReporter = spec(); | 125 | | - const result = await run({ | | 125 | + const result = await consumers.text(run({ | 126 | 126 | files: [join(testFixtures, 'default-behavior/test/random.cjs')] | 127 | | - }).compose(specReporter).toArray(); | 128 | | - const stringResults = result.map((bfr) => bfr.toString()); | 129 | | - assert.match(stringResults[0], /this should pass/); | 130 | | - assert.match(stringResults[1], /tests 1/); | 131 | | - assert.match(stringResults[1], /pass 1/); | | 127 | + }).compose(specReporter)); | | 128 | + assert.match(result, /this should pass/); | | 129 | + assert.match(result, /tests 1/); | | 130 | + assert.match(result, /pass 1/); | 132 | 131 | }); | 133 | 132 |
| 134 | 133 | it('spec', async () => { | 135 | | - const result = await run({ | | 134 | + const result = await consumers.text(run({ | 136 | 135 | files: [join(testFixtures, 'default-behavior/test/random.cjs')] | 137 | | - }).compose(spec).toArray(); | 138 | | - const stringResults = result.map((bfr) => bfr.toString()); | 139 | | - assert.match(stringResults[0], /this should pass/); | 140 | | - assert.match(stringResults[1], /tests 1/); | 141 | | - assert.match(stringResults[1], /pass 1/); | | 136 | + }).compose(spec)); | | 137 | + assert.match(result, /this should pass/); | | 138 | + assert.match(result, /tests 1/); | | 139 | + assert.match(result, /pass 1/); | 142 | 140 | }); | 143 | 141 | }); | 144 | 142 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-compose.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -490,7 +490,7 @@ const assert = require('assert'); | 490 | 490 |
| 491 | 491 | newStream.end(); | 492 | 492 |
| 493 | | - assert.deepStrictEqual(await newStream.toArray(), [Buffer.from('Steve RogersOn your left')]); | | 493 | + assert.deepStrictEqual(await newStream.toArray(), [Buffer.from('Steve Rogers'), Buffer.from('On your left')]); | 494 | 494 | })().then(common.mustCall()); | 495 | 495 | } | 496 | 496 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-push-strings.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,7 +59,7 @@ ms.on('readable', function() { | 59 | 59 | results.push(String(chunk)); | 60 | 60 | }); | 61 | 61 |
| 62 | | -const expect = [ 'first chunksecond to last chunk', 'last chunk' ]; | | 62 | +const expect = [ 'first chunk', 'second to last chunk', 'last chunk' ]; | 63 | 63 | process.on('exit', function() { | 64 | 64 | assert.strictEqual(ms._chunks, -1); | 65 | 65 | assert.deepStrictEqual(results, expect); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-readable-emittedReadable.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,7 +10,7 @@ const readable = new Readable({ | 10 | 10 | // Initialized to false. | 11 | 11 | assert.strictEqual(readable._readableState.emittedReadable, false); | 12 | 12 |
| 13 | | -const expected = [Buffer.from('foobar'), Buffer.from('quo'), null]; | | 13 | +const expected = [Buffer.from('foo'), Buffer.from('bar'), Buffer.from('quo'), null]; | 14 | 14 | readable.on('readable', common.mustCall(() => { | 15 | 15 | // emittedReadable should be true when the readable event is emitted | 16 | 16 | assert.strictEqual(readable._readableState.emittedReadable, true); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-readable-infinite-read.js +3-11Lines changed: 3 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,24 +10,16 @@ const readable = new Readable({ | 10 | 10 | highWaterMark: 16 * 1024, | 11 | 11 | read: common.mustCall(function() { | 12 | 12 | this.push(buf); | 13 | | - }, 31) | | 13 | + }, 12) | 14 | 14 | }); | 15 | 15 |
| 16 | 16 | let i = 0; | 17 | 17 |
| 18 | 18 | readable.on('readable', common.mustCall(function() { | 19 | 19 | if (i++ === 10) { | 20 | | - // We will just terminate now. | 21 | | - process.removeAllListeners('readable'); | | 20 | + readable.removeAllListeners('readable'); | 22 | 21 | return; | 23 | 22 | } | 24 | 23 |
| 25 | | - const data = readable.read(); | 26 | | - // TODO(mcollina): there is something odd in the highWaterMark logic | 27 | | - // investigate. | 28 | | - if (i === 1) { | 29 | | - assert.strictEqual(data.length, 8192 * 2); | 30 | | - } else { | 31 | | - assert.strictEqual(data.length, 8192 * 3); | 32 | | - } | | 24 | + assert.strictEqual(readable.read().length, 8192); | 33 | 25 | }, 11)); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-readable-needReadable.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,7 @@ const asyncReadable = new Readable({ | 32 | 32 | }); | 33 | 33 |
| 34 | 34 | asyncReadable.on('readable', common.mustCall(() => { | 35 | | - if (asyncReadable.read() !== null) { | | 35 | + if (asyncReadable.read(asyncReadable.readableLength) !== null) { | 36 | 36 | // After each read(), the buffer is empty. | 37 | 37 | // If the stream doesn't end now, | 38 | 38 | // then we need to notify the reader on future changes. | |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +require('../common'); | | 3 | +const assert = require('assert'); | | 4 | + | | 5 | +const { Readable } = require('stream'); | | 6 | + | | 7 | +// Read one buffer at a time and don't waste cycles allocating | | 8 | +// and copying into a new larger buffer. | | 9 | +{ | | 10 | + const r = new Readable({ | | 11 | + read() {} | | 12 | + }); | | 13 | + const buffers = [Buffer.allocUnsafe(5), Buffer.allocUnsafe(10)]; | | 14 | + for (const buf of buffers) { | | 15 | + r.push(buf); | | 16 | + } | | 17 | + for (const buf of buffers) { | | 18 | + assert.strictEqual(r.read(), buf); | | 19 | + } | | 20 | +} |
|