Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/remove node globals #435

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
37 changes: 34 additions & 3 deletions build/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,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
, 'nextTick($1)'
]
, nextTickDeclaration = [
/^('use strict';)$/m
, `$1\n\nvar nextTick = require('next-tick');\n`
]
, 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 @@ -230,6 +249,8 @@ module.exports['_stream_duplex.js'] = [
, objectKeysReplacement
, objectKeysDefine
, errorsOneLevel
, nextTickDeclaration
, nextTickUsage
]

module.exports['_stream_passthrough.js'] = [
Expand Down Expand Up @@ -266,7 +287,11 @@ module.exports['_stream_readable.js'] = [
, noAsyncIterators1
, noAsyncIterators2
, noAsyncIteratorsFrom1
, noAsyncIteratorsFrom2
, noAsyncIteratorsFrom2,
, nextTickDeclaration
, nextTickUsage
, isStdStreamCheck
, isStdStreamDeclaration
]

module.exports['_stream_transform.js'] = [
Expand Down Expand Up @@ -305,6 +330,8 @@ module.exports['_stream_writable.js'] = [
, useCorkedRequest
, addConstructors
, errorsOneLevel
, nextTickDeclaration
, nextTickUsage
]

module.exports['internal/streams/buffer_list.js'] = [
Expand All @@ -315,13 +342,15 @@ module.exports['internal/streams/buffer_list.js'] = [
[
/const \{ inspect \} = require\('util'\);/,
`
const { inspect } = require('util')
const inspect = require('browser-util-inspect')
const custom = inspect && inspect.custom || 'inspect'
`
]
]
module.exports['internal/streams/destroy.js'] = [
errorsTwoLevel
errorsTwoLevel,
, nextTickDeclaration
, nextTickUsage
]

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

module.exports['internal/streams/end-of-stream.js'] = [
Expand Down
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 nextTick = require('next-tick');
/*<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);
nextTick(onEndNT, this);
}

function onEndNT(self) {
Expand Down
24 changes: 15 additions & 9 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';

var nextTick = require('next-tick');

module.exports = Readable;
/*<replacement>*/

Expand Down Expand Up @@ -526,7 +528,7 @@ function emitReadable(stream) {
if (!state.emittedReadable) {
debug('emitReadable', state.flowing);
state.emittedReadable = true;
process.nextTick(emitReadable_, stream);
nextTick(emitReadable_, stream);
}
}

Expand Down Expand Up @@ -558,7 +560,7 @@ function emitReadable_(stream) {
function maybeReadMore(stream, state) {
if (!state.readingMore) {
state.readingMore = true;
process.nextTick(maybeReadMore_, stream, state);
nextTick(maybeReadMore_, stream, state);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not work with the polyfill. I think our browser testing is adding process.nextTick.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its just using the first if here https:/medikoo/next-tick/blob/master/index.js#L47-L49 because airtap is injecting everything, its hard to validate this with the current repo setup i will try to fix this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly. However if those transform are not put in there, it won't work.

}
}

Expand Down Expand Up @@ -625,9 +627,9 @@ Readable.prototype.pipe = function (dest, pipeOpts) {

state.pipesCount += 1;
debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
var doEnd = (!pipeOpts || pipeOpts.end !== false) && !isStdStream(dest, 'stdout') && !isStdStream(dest, 'stderr');
var endFn = doEnd ? onend : unpipe;
if (state.endEmitted) process.nextTick(endFn);else src.once('end', endFn);
if (state.endEmitted) nextTick(endFn);else src.once('end', endFn);
dest.on('unpipe', onunpipe);

function onunpipe(readable, unpipeInfo) {
Expand Down Expand Up @@ -821,7 +823,7 @@ Readable.prototype.on = function (ev, fn) {
if (state.length) {
emitReadable(this);
} else if (!state.reading) {
process.nextTick(nReadingNextTick, this);
nextTick(nReadingNextTick, this);
}
}
}
Expand All @@ -841,7 +843,7 @@ Readable.prototype.removeListener = function (ev, fn) {
// support once('readable', fn) cycles. This means that calling
// resume within the same tick will have no
// effect.
process.nextTick(updateReadableListening, this);
nextTick(updateReadableListening, this);
}

return res;
Expand All @@ -857,7 +859,7 @@ Readable.prototype.removeAllListeners = function (ev) {
// support once('readable', fn) cycles. This means that calling
// resume within the same tick will have no
// effect.
process.nextTick(updateReadableListening, this);
nextTick(updateReadableListening, this);
}

return res;
Expand Down Expand Up @@ -902,7 +904,7 @@ Readable.prototype.resume = function () {
function resume(stream, state) {
if (!state.resumeScheduled) {
state.resumeScheduled = true;
process.nextTick(resume_, stream, state);
nextTick(resume_, stream, state);
}
}

Expand Down Expand Up @@ -1081,7 +1083,7 @@ function endReadable(stream) {

if (!state.endEmitted) {
state.ended = true;
process.nextTick(endReadableNT, state, stream);
nextTick(endReadableNT, state, stream);
}
}

Expand Down Expand Up @@ -1121,4 +1123,8 @@ function indexOf(xs, x) {
}

return -1;
}

function isStdStream(stream, type) {
return typeof process === 'undefined' ? false : stream === process[type];
}
16 changes: 9 additions & 7 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
// the drain event emission and buffering.
'use strict';

var nextTick = require('next-tick');

module.exports = Writable;
/* <replacement> */

Expand Down Expand Up @@ -261,7 +263,7 @@ function writeAfterEnd(stream, cb) {
var er = new ERR_STREAM_WRITE_AFTER_END(); // TODO: defer error events consistently everywhere, not just the cb

errorOrDestroy(stream, er);
process.nextTick(cb, er);
nextTick(cb, er);
} // Checks that a user-supplied chunk is valid, especially for the particular
// mode the stream is in. Currently this means that `null` is never accepted
// and undefined/non-string values are only allowed in object mode.
Expand All @@ -278,7 +280,7 @@ function validChunk(stream, state, chunk, cb) {

if (er) {
errorOrDestroy(stream, er);
process.nextTick(cb, er);
nextTick(cb, er);
return false;
}

Expand Down Expand Up @@ -416,10 +418,10 @@ function onwriteError(stream, state, sync, er, cb) {
if (sync) {
// defer the callback if we are being called synchronously
// to avoid piling up things on the stack
process.nextTick(cb, er); // this can emit finish, and it will always happen
nextTick(cb, er); // this can emit finish, and it will always happen
// after error

process.nextTick(finishMaybe, stream, state);
nextTick(finishMaybe, stream, state);
stream._writableState.errorEmitted = true;
errorOrDestroy(stream, er);
} else {
Expand Down Expand Up @@ -456,7 +458,7 @@ function onwrite(stream, er) {
}

if (sync) {
process.nextTick(afterWrite, stream, state, finished, cb);
nextTick(afterWrite, stream, state, finished, cb);
} else {
afterWrite(stream, state, finished, cb);
}
Expand Down Expand Up @@ -605,7 +607,7 @@ function prefinish(stream, state) {
if (typeof stream._final === 'function' && !state.destroyed) {
state.pendingcb++;
state.finalCalled = true;
process.nextTick(callFinal, stream, state);
nextTick(callFinal, stream, state);
} else {
state.prefinished = true;
stream.emit('prefinish');
Expand Down Expand Up @@ -643,7 +645,7 @@ function endWritable(stream, state, cb) {
finishMaybe(stream, state);

if (cb) {
if (state.finished) process.nextTick(cb);else stream.once('finish', cb);
if (state.finished) nextTick(cb);else stream.once('finish', cb);
}

state.ended = true;
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/streams/async_iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var _Object$setPrototypeO;

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var nextTick = require('next-tick');

var finished = require('./end-of-stream');

var kLastResolve = Symbol('lastResolve');
Expand Down Expand Up @@ -41,7 +43,7 @@ function readAndResolve(iter) {
function onReadable(iter) {
// we wait for the next tick, because it might
// emit an error with process.nextTick
process.nextTick(readAndResolve, iter);
nextTick(readAndResolve, iter);
}

function wrapForNext(lastPromise, iter) {
Expand Down Expand Up @@ -84,7 +86,7 @@ var ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf((_Object$setPro
// we cannot guarantee that there is no error lingering around
// waiting to be emitted.
return new Promise(function (resolve, reject) {
process.nextTick(function () {
nextTick(function () {
if (_this[kError]) {
reject(_this[kError]);
} else {
Expand Down
4 changes: 1 addition & 3 deletions lib/internal/streams/buffer_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ function copyBuffer(src, target, offset) {
Buffer.prototype.copy.call(src, target, offset);
}

module.exports =
/*#__PURE__*/
function () {
module.exports = /*#__PURE__*/function () {
function BufferList() {
_classCallCheck(this, BufferList);

Expand Down
19 changes: 11 additions & 8 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict'; // undocumented cb() API, needed for core, not for public API
'use strict';

var nextTick = require('next-tick'); // undocumented cb() API, needed for core, not for public API


function destroy(err, cb) {
var _this = this;
Expand All @@ -11,10 +14,10 @@ function destroy(err, cb) {
cb(err);
} else if (err) {
if (!this._writableState) {
process.nextTick(emitErrorNT, this, err);
nextTick(emitErrorNT, this, err);
} else if (!this._writableState.errorEmitted) {
this._writableState.errorEmitted = true;
process.nextTick(emitErrorNT, this, err);
nextTick(emitErrorNT, this, err);
}
}

Expand All @@ -35,18 +38,18 @@ function destroy(err, cb) {
this._destroy(err || null, function (err) {
if (!cb && err) {
if (!_this._writableState) {
process.nextTick(emitErrorAndCloseNT, _this, err);
nextTick(emitErrorAndCloseNT, _this, err);
} else if (!_this._writableState.errorEmitted) {
_this._writableState.errorEmitted = true;
process.nextTick(emitErrorAndCloseNT, _this, err);
nextTick(emitErrorAndCloseNT, _this, err);
} else {
process.nextTick(emitCloseNT, _this);
nextTick(emitCloseNT, _this);
}
} else if (cb) {
process.nextTick(emitCloseNT, _this);
nextTick(emitCloseNT, _this);
cb(err);
} else {
process.nextTick(emitCloseNT, _this);
nextTick(emitCloseNT, _this);
}
});

Expand Down
6 changes: 3 additions & 3 deletions lib/internal/streams/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ function from(Readable, iterable, opts) {
function _next2() {
_next2 = _asyncToGenerator(function* () {
try {
var _ref = yield iterator.next(),
value = _ref.value,
done = _ref.done;
var _yield$iterator$next = yield iterator.next(),
value = _yield$iterator$next.value,
done = _yield$iterator$next.done;

if (done) {
readable.push(null);
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"node": ">= 6"
},
"dependencies": {
"buffer": "^5.6.0",
"events": "^3.1.0",
"inherits": "^2.0.3",
"next-tick": "^1.1.0",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
},
Expand Down
4 changes: 1 addition & 3 deletions test/common/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ var kCallback = Symbol('callback');

var common = require('./');

var Countdown =
/*#__PURE__*/
function () {
var Countdown = /*#__PURE__*/function () {
function Countdown(limit, cb) {
_classCallCheck(this, Countdown);

Expand Down
Loading