Skip to content

Commit

Permalink
Back support for Node < 8.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Mar 5, 2018
1 parent 8db89fd commit 3dbdece
Show file tree
Hide file tree
Showing 14 changed files with 653 additions and 158 deletions.
59 changes: 59 additions & 0 deletions build/common-replacements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports.altForEachImplReplacement = [
/$/
, '\nfunction forEach (xs, f) {\n'
+ ' for (var i = 0, l = xs.length; i < l; i++) {\n'
+ ' f(xs[i], i);\n'
+ ' }\n'
+ '}\n'
]

module.exports.altForEachUseReplacement = [
/(\W)([\w\.\(\),\[\] ']+)(\.forEach\()/gm
, '$1forEach($2, '
]

module.exports.specialForEachReplacment = [
/(\W)(\[(?:\d\,\s)+\d\])(\.forEach\()/gm
, '$1forEach($2, '
]

module.exports.altIndexOfImplReplacement = [
/$/
, '\nfunction indexOf (xs, x) {\n'
+ ' for (var i = 0, l = xs.length; i < l; i++) {\n'
+ ' if (xs[i] === x) return i;\n'
+ ' }\n'
+ ' return -1;\n'
+ '}\n'
]

module.exports.altIndexOfUseReplacement = [
/(\W)([\w\.\(\),\[\]]+)(\.indexOf\()/gm
, '$1indexOf($2, '
]
module.exports.objectKeysDefine = [
/^('use strict';)$/m
, '$1\n\n/*<replacement>*/\nvar objectKeys = Object.keys || function (obj) {\n'
+ ' var keys = [];\n'
+ ' for (var key in obj) keys.push(key);\n'
+ ' return keys;\n'
+ '}\n/*</replacement>*/\n'
]

module.exports.objectKeysReplacement = [
/Object\.keys/g
, 'objectKeys'
]


module.exports.bufferShimFix = [
/^('use strict';)$/m,
`/*<replacement>*/
const bufferShim = require('safe-buffer').Buffer;
/*</replacement>*/`
]

module.exports.bufferStaticMethods = [
/Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
`bufferShim.$1`
]
152 changes: 125 additions & 27 deletions build/test-replacements.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const altForEachImplReplacement = require('./common-replacements').altForEachImplReplacement
, altForEachUseReplacement = require('./common-replacements').altForEachUseReplacement
, objectKeysDefine = require('./common-replacements').objectKeysDefine
, objectKeysReplacement = require('./common-replacements').objectKeysReplacement

module.exports.all = [
[
/require\(['"]string_decoder['"]\)/g
Expand All @@ -16,27 +21,57 @@ module.exports.all = [

]

module.exports['common.js'] = [

module.exports['test-string-decoder.js'] = [
// test removed because it is V8-version dependant.
[
/^ setImmediate,$/m
, ' typeof setImmediate == \'undefined\' ? null : setImmediate,'
/test\('utf-8', bufferShim\.from\('EDA0B5EDB08D'.*\n.*\n/
, ''
],
, [
/test\('utf-8', bufferShim\.from\('F0B841', 'hex'.*\n/
, ''
]

, [
/^ clearImmediate,$/m
, ' typeof clearImmediate == \'undefined\' ? null : clearImmediate,'
/test\('utf-8', bufferShim\.from\('CCE2B8B8', 'hex'.*\n/
, ''
]
, [
/test\('utf-8', bufferShim\.from\('E2B8CCB8', 'hex'.*\n/
, ''
]

, [
/^ global];$/m
, ' global].filter(Boolean);'
/assert\.strictEqual\(decoder\.end(), '\ufffd'\);\n/
, ''
]
]

module.exports['common.js'] = [
objectKeysDefine
, objectKeysReplacement
, altForEachImplReplacement
, altForEachUseReplacement

, [
/^/
, 'require(\'babel-polyfill\');'
/(exports.mustCall[\s\S]*)/m
, '$1\n'
+ 'if (!util._errnoException) {\n'
+ ' var uv;\n'
+ ' util._errnoException = function(err, syscall) {\n'
+ ' if (util.isUndefined(uv)) try { uv = process.binding(\'uv\'); } catch (e) {}\n'
+ ' var errname = uv ? uv.errname(err) : \'\';\n'
+ ' var e = new Error(syscall + \' \' + errname);\n'
+ ' e.code = errname;\n'
+ ' e.errno = errname;\n'
+ ' e.syscall = syscall;\n'
+ ' return e;\n'
+ ' };\n'
+ '}\n'
]

// for streams2 on node 0.11
// and dtrace in 0.10
// and coverage in all
, [
/^( for \(var x in global\) \{|function leakedGlobals\(\) \{)$/m
, ' /*<replacement>*/\n'
Expand All @@ -51,26 +86,89 @@ module.exports['common.js'] = [
+ '\'core,__core-js_shared__,Promise,Map,Set,WeakMap,WeakSet,Reflect,System,asap,Observable,regeneratorRuntime,_babelPolyfill\'.split(\',\').filter(function (item) { return typeof global[item] !== undefined}).forEach(function (item) {knownGlobals.push(global[item])})'
+ ' /*</replacement>*/\n\n$1'
]
]

module.exports['test-string-decoder.js'] = [
// test removed because it is V8-version dependant.
[
/test\('utf-8', bufferShim\.from\('EDA0B5EDB08D'.*\n.*\n/
, ''
],
// for node 0.8
, [
/test\('utf-8', bufferShim\.from\('F0B841', 'hex'.*\n/
, ''
/^/
, '/*<replacement>*/'
+ '\nif (!global.setImmediate) {\n'
+ ' global.setImmediate = function setImmediate(fn) {\n'

+ ' return setTimeout(fn.bind.apply(fn, arguments), 4);\n'
+ ' };\n'
+ '}\n'
+ 'if (!global.clearImmediate) {\n'
+ ' global.clearImmediate = function clearImmediate(i) {\n'
+ ' return clearTimeout(i);\n'
+ ' };\n'
+ '}\n'
+ '/*</replacement>*/\n'
]
, [
/test\('utf-8', bufferShim\.from\('CCE2B8B8', 'hex'.*\n/
, ''
/^if \(global\.ArrayBuffer\) \{([^\}]+)\}$/m
, '/*<replacement>*/if (!process.browser) {'
+ '\nif \(global\.ArrayBuffer\) {$1}\n'
+ '}/*</replacement>*/\n'
]
, [
/test\('utf-8', bufferShim\.from\('E2B8CCB8', 'hex'.*\n/
, ''
, [
/^Object\.defineProperty\(([\w\W]+?)\}\)\;/mg
, '/*<replacement>*/if (!process.browser) {'
+ '\nObject\.defineProperty($1});\n'
+ '}/*</replacement>*/\n'
]
, [
/if \(!process\.send\)/
, 'if (!process.send && !process.browser)'
]
, [
/assert\.strictEqual\(decoder\.end(), '\ufffd'\);\n/
, [
/^/,
`/*<replacement>*/
require('babel-polyfill');
var util = require('util');
for (var i in util) exports[i] = util[i];
/*</replacement>*/`
],
[
/var regexp = `\^\(\\\\w\+\)\\\\s\+\\\\s\$\{port\}\/\$\{protocol\}\\\\s`;/,
`var regexp = '^(\\w+)\\s+\\s' + port + '/' + protocol + '\\s';`
],
[
/^var util = require\('util'\);/m
, '\n/*<replacement>*/\nvar util = require(\'core-util-is\');\n'
+ 'util.inherits = require(\'inherits\');\n/*</replacement>*/\n'
],
[
/^const util = require\('util'\);/m
, '\n/*<replacement>*/\nvar util = require(\'core-util-is\');\n'
+ 'util.inherits = require(\'inherits\');\n/*</replacement>*/\n'
]
, [
/process\.binding\('timer_wrap'\)\.Timer;/,
'{now: function (){}}'
],
[
/(exports\.enoughTestCpu[^;]+;)/,
'/*$1*/'
],
[
/exports\.buildType/,
'//exports.buildType'
],
[
/require\('async_hooks'\)/,
'/*require(\'async_hooks\')'
],
[
/\}\).enable\(\);/,
'}).enable();*/'
],
[
/(?:var|const) async_wrap = process\.binding\('async_wrap'\);\n.*(?:var|const) (?:{ )?kCheck(?: })? = async_wrap\.constants(?:\.kCheck)?;/gm,
'// const async_wrap = process.binding(\'async_wrap\');\n' +
' // const kCheck = async_wrap.constants.kCheck;'
],
[
/async_wrap\.async_hook_fields\[kCheck\] \+= 1;/,
'// async_wrap.async_hook_fields[kCheck] += 1;'
]
]
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
},
"devDependencies": {
"babel-polyfill": "^6.23.0",
"core-util-is": "^1.0.2",
"inherits": "^2.0.3",
"tap": "~0.4.8"
},
"scripts": {
Expand Down
24 changes: 23 additions & 1 deletion test/common/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
require('babel-polyfill');# Node.js Core Test Common Modules
/*<replacement>*/
require('babel-polyfill');
var util = require('util');
for (var i in util) exports[i] = util[i];
/*</replacement>*//*<replacement>*/
if (!global.setImmediate) {
global.setImmediate = function setImmediate(fn) {
return setTimeout(fn.bind.apply(fn, arguments), 4);
};
}
if (!global.clearImmediate) {
global.clearImmediate = function clearImmediate(i) {
return clearTimeout(i);
};
}
/*</replacement>*/
# Node.js Core Test Common Modules

This directory contains modules used to test the Node.js implementation.

Expand Down Expand Up @@ -517,3 +533,9 @@ implementation with tests from
[`common.hijackStdErr()`]: #hijackstderrlistener
[`common.hijackStdOut()`]: #hijackstdoutlistener
[internationalization]: https://github.com/nodejs/node/wiki/Intl

function forEach (xs, f) {
for (var i = 0, l = xs.length; i < l; i++) {
f(xs[i], i);
}
}
40 changes: 36 additions & 4 deletions test/common/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
require('babel-polyfill'); /* eslint-disable required-modules */
/*<replacement>*/
require('babel-polyfill');
var util = require('util');
for (var i in util) {
exports[i] = util[i];
} /*</replacement>*/ /*<replacement>*/
if (!global.setImmediate) {
global.setImmediate = function setImmediate(fn) {
return setTimeout(fn.bind.apply(fn, arguments), 4);
};
}
if (!global.clearImmediate) {
global.clearImmediate = function clearImmediate(i) {
return clearTimeout(i);
};
}
/*</replacement>*/
/* eslint-disable required-modules */

'use strict';

/*<replacement>*/
var objectKeys = objectKeys || function (obj) {
var keys = [];
for (var key in obj) {
keys.push(key);
}return keys;
};
/*</replacement>*/

var assert = require('assert');
var fork = require('child_process').fork;
var path = require('path');
Expand All @@ -11,9 +37,9 @@ var runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
function runBenchmark(name, args, env) {
var argv = [];

for (var i = 0; i < args.length; i++) {
for (var _i = 0; _i < args.length; _i++) {
argv.push('--set');
argv.push(args[i]);
argv.push(args[_i]);
}

argv.push(name);
Expand All @@ -27,4 +53,10 @@ function runBenchmark(name, args, env) {
});
}

module.exports = runBenchmark;
module.exports = runBenchmark;

function forEach(xs, f) {
for (var i = 0, l = xs.length; i < l; i++) {
f(xs[i], i);
}
}
36 changes: 34 additions & 2 deletions test/common/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@ var _createClass = function () { function defineProperties(target, props) { for

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

require('babel-polyfill'); /* eslint-disable required-modules */
/*<replacement>*/
require('babel-polyfill');
var util = require('util');
for (var i in util) {
exports[i] = util[i];
} /*</replacement>*/ /*<replacement>*/
if (!global.setImmediate) {
global.setImmediate = function setImmediate(fn) {
return setTimeout(fn.bind.apply(fn, arguments), 4);
};
}
if (!global.clearImmediate) {
global.clearImmediate = function clearImmediate(i) {
return clearTimeout(i);
};
}
/*</replacement>*/
/* eslint-disable required-modules */
'use strict';

/*<replacement>*/
var objectKeys = objectKeys || function (obj) {
var keys = [];
for (var key in obj) {
keys.push(key);
}return keys;
};
/*</replacement>*/

var assert = require('assert');
var kLimit = Symbol('limit');
var kCallback = Symbol('callback');
Expand Down Expand Up @@ -35,4 +61,10 @@ var Countdown = function () {
return Countdown;
}();

module.exports = Countdown;
module.exports = Countdown;

function forEach(xs, f) {
for (var i = 0, l = xs.length; i < l; i++) {
f(xs[i], i);
}
}
Loading

0 comments on commit 3dbdece

Please sign in to comment.