Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ npm install --save readable-stream

This package is a mirror of the streams implementations in Node.js.

Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v10.15.3/docs/api/stream.html).
Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v10.16.0/docs/api/stream.html).

If you want to guarantee a stable streams base, regardless of what version of
Node you, or the users of your libraries are using, use **readable-stream** *only* and avoid the *"stream"* module in Node-core, for background see [this blogpost](http://r.va.gg/2014/06/why-i-dont-use-nodes-core-stream-module.html).
Expand Down
31 changes: 31 additions & 0 deletions build/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,25 @@ function CorkedRequest(state) {
/const \{ once \} = require\('internal\/util'\);/
, 'function once(callback) { let called = false; return function(...args) { if (called) return; called = true; callback(...args); }; }'
]
, nextTickUsage = [
/process\.nextTick\((.+?)\)/g
, 'fakeProcess.nextTick($1)'
]
, fakeProcessDeclaration = depth => [
/^('use strict';)$/m
, `$1\n\nvar fakeProcess = require('${'../'.repeat(depth)}fake-process.js');`
]
, isStdStreamCheck = [
/dest !== process\.(stdout|stderr)/g
, `!isStdStream(dest, '$1')`
]
, isStdStreamDeclaration = [
/$/
, `
function isStdStream(stream, type) {
return typeof process === 'undefined' ? false : stream === process[type];
}`
]

module.exports['_stream_duplex.js'] = [
requireReplacement
Expand All @@ -222,6 +241,8 @@ module.exports['_stream_duplex.js'] = [
, objectKeysReplacement
, objectKeysDefine
, errorsOneLevel
, fakeProcessDeclaration(1)
, nextTickUsage
]

module.exports['_stream_passthrough.js'] = [
Expand Down Expand Up @@ -257,6 +278,10 @@ module.exports['_stream_readable.js'] = [
, numberIE11
, noAsyncIterators1
, noAsyncIterators2
, fakeProcessDeclaration(1)
, nextTickUsage
, isStdStreamCheck
, isStdStreamDeclaration
]

module.exports['_stream_transform.js'] = [
Expand Down Expand Up @@ -295,6 +320,8 @@ module.exports['_stream_writable.js'] = [
, useCorkedRequest
, addConstructors
, errorsOneLevel
, fakeProcessDeclaration(1)
, nextTickUsage
]

module.exports['internal/streams/buffer_list.js'] = [
Expand All @@ -312,6 +339,8 @@ const custom = inspect && inspect.custom || 'inspect'
]
module.exports['internal/streams/destroy.js'] = [
errorsTwoLevel
, fakeProcessDeclaration(3)
, nextTickUsage
]

module.exports['internal/streams/state.js'] = [
Expand All @@ -333,6 +362,8 @@ module.exports['internal/streams/async_iterator.js'] = [
/ return\(\)/,
'[Symbol.asyncIterator]() { return this },\n return\(\)'
]
, fakeProcessDeclaration(3)
, nextTickUsage
]

module.exports['internal/streams/end-of-stream.js'] = [
Expand Down
4 changes: 4 additions & 0 deletions build/test-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ module.exports.all = [
/require\(['"]assert['"]\)/g
, 'require(\'assert/\')'
]
, [
/.*--expose.internals.*/
, ''
]
]

module.exports['test-stream2-basic.js'] = [
Expand Down
155 changes: 155 additions & 0 deletions fake-process-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
module.exports = {
nextTick: nextTick
};

// cached from whatever global is present so that test runners that stub it
// don't break things. But we need to wrap it in a try catch in case it is
// wrapped in strict mode code which doesn't define any globals. It's inside a
// function because try/catches deoptimize in certain engines.

var cachedSetTimeout;
var cachedClearTimeout;

function defaultSetTimeout() {
throw new Error('setTimeout has not been defined');
}

function defaultClearTimeout () {
throw new Error('clearTimeout has not been defined');
}

(function () {
try {
if (typeof setTimeout === 'function') {
cachedSetTimeout = setTimeout;
} else {
cachedSetTimeout = defaultSetTimeout;
}
} catch (e) {
cachedSetTimeout = defaultSetTimeout;
}
try {
if (typeof clearTimeout === 'function') {
cachedClearTimeout = clearTimeout;
} else {
cachedClearTimeout = defaultClearTimeout;
}
} catch (e) {
cachedClearTimeout = defaultClearTimeout;
}
}());

function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
//normal enviroments in sane situations
return setTimeout(fun, 0);
}
// if setTimeout wasn't available but was latter defined
if ((cachedSetTimeout === defaultSetTimeout || !cachedSetTimeout) && setTimeout) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedSetTimeout(fun, 0);
} catch (e) {
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedSetTimeout.call(null, fun, 0);
} catch(e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
return cachedSetTimeout.call(this, fun, 0);
}
}
}

function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
//normal enviroments in sane situations
return clearTimeout(marker);
}
// if clearTimeout wasn't available but was latter defined
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedClearTimeout(marker);
} catch (e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedClearTimeout.call(null, marker);
} catch (e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
return cachedClearTimeout.call(this, marker);
}
}
}

var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;

function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}

function drainQueue() {
if (draining) {
return;
}
var timeout = runTimeout(cleanUpNextTick);
draining = true;

var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}

function nextTick(fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
}

function Item(fun, array) {
this.fun = fun;
this.array = array;
}

Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
1 change: 1 addition & 0 deletions fake-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = process;
5 changes: 4 additions & 1 deletion lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
// prototypally inherits from Readable, and then parasitically from
// Writable.
'use strict';

var fakeProcess = require('../fake-process.js');
/*<replacement>*/


var objectKeys = Object.keys || function (obj) {
var keys = [];

Expand Down Expand Up @@ -105,7 +108,7 @@ function onend() {
if (this._writableState.ended) return; // no more data can be written.
// But allow more writes to happen in this tick.

process.nextTick(onEndNT, this);
fakeProcess.nextTick(onEndNT, this);
}

function onEndNT(self) {
Expand Down
Loading