123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- 'use strict';
- if (process.addAsyncListener) throw new Error("Don't require polyfill unless needed");
- var shimmer = require('shimmer')
- , semver = require('semver')
- , wrap = shimmer.wrap
- , massWrap = shimmer.massWrap
- , wrapCallback = require('./glue.js')
- , util = require('util')
- ;
- var v6plus = semver.gte(process.version, '6.0.0');
- var v7plus = semver.gte(process.version, '7.0.0');
- var v8plus = semver.gte(process.version, '8.0.0');
- var v11plus = semver.gte(process.version, '11.0.0');
- var net = require('net');
- if (v7plus && !net._normalizeArgs) {
-
- net._normalizeArgs = function (args) {
- if (args.length === 0) {
- return [{}, null];
- }
- var arg0 = args[0];
- var options = {};
- if (typeof arg0 === 'object' && arg0 !== null) {
-
- options = arg0;
- } else if (isPipeName(arg0)) {
-
- options.path = arg0;
- } else {
-
- options.port = arg0;
- if (args.length > 1 && typeof args[1] === 'string') {
- options.host = args[1];
- }
- }
- var cb = args[args.length - 1];
- if (typeof cb !== 'function')
- return [options, null];
- else
- return [options, cb];
- }
- } else if (!v7plus && !net._normalizeConnectArgs) {
-
- net._normalizeConnectArgs = function (args) {
- var options = {};
- function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; }
- if (typeof args[0] === 'object' && args[0] !== null) {
-
- options = args[0];
- }
- else if (typeof args[0] === 'string' && toNumber(args[0]) === false) {
-
- options.path = args[0];
- }
- else {
-
- options.port = args[0];
- if (typeof args[1] === 'string') {
- options.host = args[1];
- }
- }
- var cb = args[args.length - 1];
- return typeof cb === 'function' ? [options, cb] : [options];
- };
- }
- if ('_setUpListenHandle' in net.Server.prototype) {
- wrap(net.Server.prototype, '_setUpListenHandle', wrapSetUpListenHandle);
- } else {
- wrap(net.Server.prototype, '_listen2', wrapSetUpListenHandle);
- }
- function wrapSetUpListenHandle(original) {
- return function () {
- this.on('connection', function (socket) {
- if (socket._handle) {
- socket._handle.onread = wrapCallback(socket._handle.onread);
- }
- });
- try {
- return original.apply(this, arguments);
- }
- finally {
-
- if (this._handle && this._handle.onconnection) {
- this._handle.onconnection = wrapCallback(this._handle.onconnection);
- }
- }
- };
- }
- function patchOnRead(ctx) {
- if (ctx && ctx._handle) {
- var handle = ctx._handle;
- if (!handle._originalOnread) {
- handle._originalOnread = handle.onread;
- }
- handle.onread = wrapCallback(handle._originalOnread);
- }
- }
- wrap(net.Socket.prototype, 'connect', function (original) {
- return function () {
- var args;
-
-
-
-
-
- if (v8plus &&
- Array.isArray(arguments[0]) &&
- Object.getOwnPropertySymbols(arguments[0]).length > 0) {
-
- args = arguments[0];
- } else {
-
- args = v7plus
- ? net._normalizeArgs(arguments)
- : net._normalizeConnectArgs(arguments);
- }
- if (args[1]) args[1] = wrapCallback(args[1]);
- var result = original.apply(this, args);
- patchOnRead(this);
- return result;
- };
- });
- var http = require('http');
- wrap(http.Agent.prototype, 'addRequest', function (original) {
- return function (req) {
- var onSocket = req.onSocket;
- req.onSocket = wrapCallback(function (socket) {
- patchOnRead(socket);
- return onSocket.apply(this, arguments);
- });
- return original.apply(this, arguments);
- };
- });
- var childProcess = require('child_process');
- function wrapChildProcess(child) {
- if (Array.isArray(child.stdio)) {
- child.stdio.forEach(function (socket) {
- if (socket && socket._handle) {
- socket._handle.onread = wrapCallback(socket._handle.onread);
- wrap(socket._handle, 'close', activatorFirst);
- }
- });
- }
- if (child._handle) {
- child._handle.onexit = wrapCallback(child._handle.onexit);
- }
- }
- if (childProcess.ChildProcess) {
- wrap(childProcess.ChildProcess.prototype, 'spawn', function (original) {
- return function () {
- var result = original.apply(this, arguments);
- wrapChildProcess(this);
- return result;
- };
- });
- } else {
- massWrap(childProcess, [
- 'execFile',
- 'fork',
- 'spawn'
- ], function (original) {
- return function () {
- var result = original.apply(this, arguments);
- wrapChildProcess(result);
- return result;
- };
- });
- }
- if (!process._fatalException) {
- process._originalNextTick = process.nextTick;
- }
- var processors = [];
- if (process._nextDomainTick) processors.push('_nextDomainTick');
- if (process._tickDomainCallback) processors.push('_tickDomainCallback');
- massWrap(
- process,
- processors,
- activator
- );
- wrap(process, 'nextTick', activatorFirst);
- var asynchronizers = [
- 'setTimeout',
- 'setInterval'
- ];
- if (global.setImmediate) asynchronizers.push('setImmediate');
- var timers = require('timers');
- var patchGlobalTimers = global.setTimeout === timers.setTimeout;
- massWrap(
- timers,
- asynchronizers,
- activatorFirst
- );
- if (patchGlobalTimers) {
- massWrap(
- global,
- asynchronizers,
- activatorFirst
- );
- }
- var dns = require('dns');
- massWrap(
- dns,
- [
- 'lookup',
- 'resolve',
- 'resolve4',
- 'resolve6',
- 'resolveCname',
- 'resolveMx',
- 'resolveNs',
- 'resolveTxt',
- 'resolveSrv',
- 'reverse'
- ],
- activator
- );
- if (dns.resolveNaptr) wrap(dns, 'resolveNaptr', activator);
- var fs = require('fs');
- massWrap(
- fs,
- [
- 'watch',
- 'rename',
- 'truncate',
- 'chown',
- 'fchown',
- 'chmod',
- 'fchmod',
- 'stat',
- 'lstat',
- 'fstat',
- 'link',
- 'symlink',
- 'readlink',
- 'realpath',
- 'unlink',
- 'rmdir',
- 'mkdir',
- 'readdir',
- 'close',
- 'open',
- 'utimes',
- 'futimes',
- 'fsync',
- 'write',
- 'read',
- 'readFile',
- 'writeFile',
- 'appendFile',
- 'watchFile',
- 'unwatchFile',
- "exists",
- ],
- activator
- );
- if (fs.lchown) wrap(fs, 'lchown', activator);
- if (fs.lchmod) wrap(fs, 'lchmod', activator);
- if (fs.ftruncate) wrap(fs, 'ftruncate', activator);
- var zlib;
- try { zlib = require('zlib'); } catch (err) { }
- if (zlib && zlib.Deflate && zlib.Deflate.prototype) {
- var proto = Object.getPrototypeOf(zlib.Deflate.prototype);
- if (proto._transform) {
-
- wrap(proto, "_transform", activator);
- }
- else if (proto.write && proto.flush && proto.end) {
-
- massWrap(
- proto,
- [
- 'write',
- 'flush',
- 'end'
- ],
- activator
- );
- }
- }
- var crypto;
- try { crypto = require('crypto'); } catch (err) { }
- if (crypto) {
- var toWrap = [
- 'pbkdf2',
- 'randomBytes',
- ];
- if (!v11plus) {
- toWrap.push('pseudoRandomBytes');
- }
- massWrap(crypto, toWrap, activator);
- }
- var instrumentPromise = !!global.Promise &&
- Promise.toString() === 'function Promise() { [native code] }' &&
- Promise.toString.toString() === 'function toString() { [native code] }';
- if (instrumentPromise) {
-
- var promiseListener = process.addAsyncListener({
- create: function create() {
- instrumentPromise = false;
- }
- });
-
- global.Promise.resolve(true).then(function notSync() {
- instrumentPromise = false;
- });
- process.removeAsyncListener(promiseListener);
- }
- if (instrumentPromise) {
- wrapPromise();
- }
- function wrapPromise() {
- var Promise = global.Promise;
-
-
- function wrappedPromise(executor) {
- if (!(this instanceof wrappedPromise)) {
- return Promise(executor);
- }
- if (typeof executor !== 'function') {
- return new Promise(executor);
- }
- var context, args;
- var promise = new Promise(wrappedExecutor);
- promise.__proto__ = wrappedPromise.prototype;
- try {
- executor.apply(context, args);
- } catch (err) {
- args[1](err);
- }
- return promise;
- function wrappedExecutor(resolve, reject) {
- context = this;
- args = [wrappedResolve, wrappedReject];
-
-
- function wrappedResolve(val) {
- ensureAslWrapper(promise, false);
- return resolve(val);
- }
- function wrappedReject(val) {
- ensureAslWrapper(promise, false);
- return reject(val);
- }
- }
- }
- util.inherits(wrappedPromise, Promise);
- wrap(Promise.prototype, 'then', wrapThen);
-
- if (Promise.prototype.chain) {
- wrap(Promise.prototype, 'chain', wrapThen);
- }
- if (v6plus) {
- global.Promise = require('./es6-wrapped-promise.js')(Promise, ensureAslWrapper);
- } else {
- var PromiseFunctions = [
- 'all',
- 'race',
- 'reject',
- 'resolve',
- 'accept',
- 'defer'
- ];
- PromiseFunctions.forEach(function(key) {
-
- if (typeof Promise[key] === 'function') {
- wrappedPromise[key] = Promise[key];
- }
- });
- global.Promise = wrappedPromise
- }
- function ensureAslWrapper(promise, overwrite) {
- if (!promise.__asl_wrapper || overwrite) {
- promise.__asl_wrapper = wrapCallback(propagateAslWrapper);
- }
- }
- function propagateAslWrapper(ctx, fn, result, next) {
- var nextResult;
- try {
- nextResult = fn.call(ctx, result);
- return {returnVal: nextResult, error: false}
- } catch (err) {
- return {errorVal: err, error: true}
- } finally {
-
- if (nextResult instanceof Promise) {
- next.__asl_wrapper = function proxyWrapper() {
- var aslWrapper = nextResult.__asl_wrapper || propagateAslWrapper;
- return aslWrapper.apply(this, arguments);
- }
- } else {
- ensureAslWrapper(next, true);
- }
- }
- }
- function wrapThen(original) {
- return function wrappedThen() {
- var promise = this;
- var next = original.apply(promise, Array.prototype.map.call(arguments, bind));
- next.__asl_wrapper = function proxyWrapper(ctx, fn, val, last) {
- if (promise.__asl_wrapper) {
- promise.__asl_wrapper(ctx, function () {}, null, next);
- return next.__asl_wrapper(ctx, fn, val, last);
- }
- return propagateAslWrapper(ctx, fn, val, last);
- }
- return next;
-
-
- function bind(fn) {
- if (typeof fn !== 'function') return fn;
- return wrapCallback(function (val) {
- var result = (promise.__asl_wrapper || propagateAslWrapper)(this, fn, val, next);
- if (result.error) {
- throw result.errorVal
- } else {
- return result.returnVal
- }
- });
- }
- }
- }
- }
- function activator(fn) {
- var fallback = function () {
- var args;
- var cbIdx = arguments.length - 1;
- if (typeof arguments[cbIdx] === "function") {
- args = Array(arguments.length)
- for (var i = 0; i < arguments.length - 1; i++) {
- args[i] = arguments[i];
- }
- args[cbIdx] = wrapCallback(arguments[cbIdx]);
- }
- return fn.apply(this, args || arguments);
- };
-
- switch (fn.length) {
- case 1:
- return function (cb) {
- if (arguments.length !== 1) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, cb);
- };
- case 2:
- return function (a, cb) {
- if (arguments.length !== 2) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, a, cb);
- };
- case 3:
- return function (a, b, cb) {
- if (arguments.length !== 3) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, a, b, cb);
- };
- case 4:
- return function (a, b, c, cb) {
- if (arguments.length !== 4) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, a, b, c, cb);
- };
- case 5:
- return function (a, b, c, d, cb) {
- if (arguments.length !== 5) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, a, b, c, d, cb);
- };
- case 6:
- return function (a, b, c, d, e, cb) {
- if (arguments.length !== 6) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, a, b, c, d, e, cb);
- };
- default:
- return fallback;
- }
- }
- function activatorFirst(fn) {
- var fallback = function () {
- var args;
- if (typeof arguments[0] === "function") {
- args = Array(arguments.length)
- args[0] = wrapCallback(arguments[0]);
- for (var i = 1; i < arguments.length; i++) {
- args[i] = arguments[i];
- }
- }
- return fn.apply(this, args || arguments);
- };
-
- switch (fn.length) {
- case 1:
- return function (cb) {
- if (arguments.length !== 1) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, cb);
- };
- case 2:
- return function (cb, a) {
- if (arguments.length !== 2) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, cb, a);
- };
- case 3:
- return function (cb, a, b) {
- if (arguments.length !== 3) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, cb, a, b);
- };
- case 4:
- return function (cb, a, b, c) {
- if (arguments.length !== 4) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, cb, a, b, c);
- };
- case 5:
- return function (cb, a, b, c, d) {
- if (arguments.length !== 5) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, cb, a, b, c, d);
- };
- case 6:
- return function (cb, a, b, c, d, e) {
- if (arguments.length !== 6) return fallback.apply(this, arguments);
- if (typeof cb === "function") cb = wrapCallback(cb);
- return fn.call(this, cb, a, b, c, d, e);
- };
- default:
- return fallback;
- }
- }
- function toNumber(x) {
- return (x = Number(x)) >= 0 ? x : false;
- }
- function isPipeName(s) {
- return typeof s === 'string' && toNumber(s) === false;
- }
|