GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: do not chunk strings and Buffer in Readable.from · nodejs/node@8cd8cd7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • stream.md
  • lib/internal/streams
    • from.js
  • test/parallel
    • test-readable-from.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/stream.md‎

Copy file name to clipboardExpand all lines: doc/api/stream.md
+4Lines changed: 4 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,10 @@ readable.on('data', (chunk) => {
16411641
});
16421642
```
16431643

1644+
Calling `Readable.from(string)` or `Readable.from(buffer)` will not have
1645+
the strings or buffers be iterated to match the other streams semantics
1646+
for performance reasons.
1647+
16441648
## API for Stream Implementers
16451649

16461650
<!--type=misc-->
Collapse file

‎lib/internal/streams/from.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/from.js
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ const {
44
SymbolAsyncIterator,
55
SymbolIterator
66
} = primordials;
7+
const { Buffer } = require('buffer');
78

89
const {
910
ERR_INVALID_ARG_TYPE
1011
} = require('internal/errors').codes;
1112

1213
function from(Readable, iterable, opts) {
1314
let iterator;
15+
if (typeof iterable === 'string' || iterable instanceof Buffer) {
16+
return new Readable({
17+
objectMode: true,
18+
...opts,
19+
read() {
20+
this.push(iterable);
21+
this.push(null);
22+
}
23+
});
24+
}
25+
1426
if (iterable && iterable[SymbolAsyncIterator])
1527
iterator = iterable[SymbolAsyncIterator]();
1628
else if (iterable && iterable[SymbolIterator])
Collapse file

‎test/parallel/test-readable-from.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-readable-from.js
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,23 @@ async function toReadablePromises() {
5656
async function toReadableString() {
5757
const stream = Readable.from('abc');
5858

59-
const expected = ['a', 'b', 'c'];
59+
const expected = ['abc'];
6060

6161
for await (const chunk of stream) {
6262
strictEqual(chunk, expected.shift());
6363
}
6464
}
6565

66+
async function toReadableBuffer() {
67+
const stream = Readable.from(Buffer.from('abc'));
68+
69+
const expected = ['abc'];
70+
71+
for await (const chunk of stream) {
72+
strictEqual(chunk.toString(), expected.shift());
73+
}
74+
}
75+
6676
async function toReadableOnData() {
6777
async function* generate() {
6878
yield 'a';
@@ -154,6 +164,7 @@ Promise.all([
154164
toReadableSyncIterator(),
155165
toReadablePromises(),
156166
toReadableString(),
167+
toReadableBuffer(),
157168
toReadableOnData(),
158169
toReadableOnDataNonObject(),
159170
destroysTheStreamWhenThrowing(),

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.