MCPcopy Index your code
hub / github.com/nodejs/node / from

Function from

lib/internal/streams/from.js:18–217  ·  view source on GitHub ↗
(Readable, iterable, opts)

Source from the content-addressed store, hash-verified

16} = require('internal/errors');
17
18function from(Readable, iterable, opts) {
19 let iterator;
20 if (typeof iterable === 'string' || iterable instanceof Buffer) {
21 return new Readable({
22 objectMode: true,
23 ...opts,
24 read() {
25 this.push(iterable);
26 this.push(null);
27 },
28 });
29 }
30
31 let isAsync;
32 if (iterable?.[SymbolAsyncIterator]) {
33 isAsync = true;
34 iterator = iterable[SymbolAsyncIterator]();
35 } else if (iterable?.[SymbolIterator]) {
36 isAsync = false;
37 iterator = iterable[SymbolIterator]();
38 } else {
39 throw new ERR_INVALID_ARG_TYPE('iterable', ['Iterable'], iterable);
40 }
41
42
43 const readable = new Readable({
44 objectMode: true,
45 highWaterMark: 1,
46 // TODO(ronag): What options should be allowed?
47 ...opts,
48 });
49 const originalDestroy = readable._destroy;
50
51 // Flag to protect against _read
52 // being called before last iteration completion.
53 let reading = false;
54 let isAsyncValues = false;
55
56 readable._read = function() {
57 if (!reading) {
58 reading = true;
59
60 if (isAsync) {
61 nextAsync();
62 } else if (isAsyncValues) {
63 nextSyncWithAsyncValues();
64 } else {
65 nextSyncWithSyncValues();
66 }
67 }
68 };
69
70 readable._destroy = function(error, cb) {
71 originalDestroy.call(this, error, (destroyError) => {
72 const combinedError = destroyError || error;
73 PromisePrototypeThen(
74 close(combinedError),
75 // nextTick is here in case cb throws

Callers 15

readable.jsFile · 0.70
duplexify.jsFile · 0.70
buffer.jsFile · 0.50
benchFromFirstBatchFunction · 0.50
benchIterFunction · 0.50
testBytesAsyncFunction · 0.50
testBytesAsyncLimitFunction · 0.50
testBytesAsyncAbortFunction · 0.50
testBytesEmptyFunction · 0.50
testArrayBufferAsyncFunction · 0.50
testBasicWriteFunction · 0.50
testPipeToWithTransformFunction · 0.50

Calls 6

nextAsyncFunction · 0.85
nextSyncWithAsyncValuesFunction · 0.85
nextSyncWithSyncValuesFunction · 0.85
aggregateTwoErrorsFunction · 0.85
closeFunction · 0.70
callMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…