8000 safe-buffer · rvagg/archived-string_decoder@19b53f3 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 5, 2018. It is now read-only.

Commit 19b53f3

Browse files
committed
safe-buffer
1 parent 0cd0754 commit 19b53f3

File tree

8 files changed

+57
-168
lines changed

8 files changed

+57
-168
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm install --save string_decoder
1515

1616
This package is a mirror of the string_decoder implementation in Node-core.
1717

18-
Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v7.8.0/docs/api/).
18+
Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v7.0.0/docs/api/).
1919

2020
As of version 1.0.0 **string_decoder** uses semantic versioning.
2121

build/files.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ module.exports['string_decoder.js'] = [
1616

1717
, [
1818
/const Buffer = require\('buffer'\).Buffer;/
19-
, 'var Buffer = require(\'buffer\').Buffer;\n' +
20-
'var bufferShim = require(\'buffer-shims\');'
21-
]
22-
23-
// allocUnsafe
24-
25-
, [
26-
/Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
27-
'bufferShim.$1'
19+
, 'var Buffer = require(\'safe-buffer\').Buffer;\n'
2820
]
2921

3022
// add Buffer.isEncoding where missing

build/test-replacements.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports.all = [
1111

1212
, [
1313
/^('use strict';)$/m,
14-
'$1\nconst bufferShim = require(\'buffer-shims\');'
14+
'$1\nconst bufferShim = require(\'safe-buffer\').Buffer;'
1515
]
1616

1717
]
@@ -60,4 +60,3 @@ module.exports['test-string-decoder.js'] = [
6060
, ''
6161
]
6262
]
63-

lib/string_decoder.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
var Buffer = require('buffer').Buffer;
4-
var bufferShim = require('buffer-shims');
3+
var Buffer = require('safe-buffer').Buffer;
54

65
var isEncoding = Buffer.isEncoding || function (encoding) {
76
encoding = '' + encoding;
@@ -78,7 +77,7 @@ function StringDecoder(encoding) {
7877
}
7978
this.lastNeed = 0;
8079
this.lastTotal = 0;
81-
this.lastChar = bufferShim.allocUnsafe(nb);
80+
this.lastChar = Buffer.allocUnsafe(nb);
8281
}
8382

8483
StringDecoder.prototype.write = function (buf) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "The string_decoder module from Node core",
55
"main": "lib/string_decoder.js",
66
"dependencies": {
7-
"buffer-shims": "~1.0.0"
7+
"safe-buffer": "^5.0.1"
88
},
99
"devDependencies": {
1010
"babel-polyfill": "^6.23.0",

test/common.js

Lines changed: 43 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ var child_process = require('child_process');
88
var stream = require('stream');
99
var util = require('util');
1010
var Timer = process.binding('timer_wrap').Timer;
11-
var execSync = require('child_process').execSync;
1211

13-
var testRoot = process.env.NODE_TEST_DIR ? fs.realpathSync(process.env.NODE_TEST_DIR) : __dirname;
12+
var testRoot = process.env.NODE_TEST_DIR ? path.resolve(process.env.NODE_TEST_DIR) : __dirname;
1413

15-
exports.fixturesDir = path.join(__dirname, 'fixtures');
14+
exports.testDir = __dirname;
15+
exports.fixturesDir = path.join(exports.testDir, 'fixtures');
1616
exports.tmpDirName = 'tmp';
1717
// PORT should match the definition in test/testpy/__init__.py.
1818
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
@@ -28,15 +28,14 @@ exports.isOSX = process.platform === 'darwin';
2828
exports.enoughTestMem = os.totalmem() > 0x40000000; /* 1 Gb */
2929

3030
var cpus = os.cpus();
31-
exports.enoughTestCpu = Array.isArray(cpus) && (cpus.length > 1 || cpus[0].speed > 999);
31+
exports.enoughTestCpu = cpus.length > 1 || cpus[0].speed > 999;
3232

3333
exports.rootDir = exports.isWindows ? 'c:\\' : '/';
3434
exports.buildType = process.config.target_defaults.default_configuration;
3535

3636
function rimrafSync(p) {
37-
var st = void 0;
3837
try {
39-
st = fs.lstatSync(p);
38+
var st = fs.lstatSync(p);
4039
} catch (e) {
4140
if (e.code === 'ENOENT') return;
4241
}
@@ -147,8 +146,8 @@ Object.defineProperty(exports, 'opensslCli', { get: function () {
147146

148147
if (exports.isWindows) opensslCli += '.exe';
149148

150-
var opensslCmd = child_process.spawnSync(opensslCli, ['version']);
151-
if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) {
149+
var openssl_cmd = child_process.spawnSync(opensslCli, ['version']);
150+
if (openssl_cmd.status !== 0 || openssl_cmd.error !== undefined) {
152151
// openssl command cannot be executed
153152
opensslCli = false;
154153
}
@@ -176,6 +175,12 @@ if (exports.isWindows) {
176175
exports.PIPE = exports.tmpDir + '/test.sock';
177176
}
178177

178+
if (exports.isWindows) {
179+
exports.faketimeCli = false;
180+
} else {
181+
exports.faketimeCli = path.join(__dirname, '..', 'tools', 'faketime', 'src', 'faketime');
182+
}
183+
179184
var ifaces = os.networkInterfaces();
180185
exports.hasIPv6 = Object.keys(ifaces).some(function (name) {
181186
return (/lo/.test(name) && ifaces[name].some(function (info) {
@@ -184,27 +189,6 @@ exports.hasIPv6 = Object.keys(ifaces).some(function (name) {
184189
);
185190
});
186191

187-
/*
188-
* Check that when running a test with
189-
* `$node --abort-on-uncaught-exception $file child`
190-
* the process aborts.
191-
*/
192-
exports.childShouldThrowAndAbort = function () {
193-
var testCmd = '';
194-
if (!exports.isWindows) {
195-
// Do not create core files, as it can take a lot of disk space on
196-
// continuous testing and developers' machines
197-
testCmd += 'ulimit -c 0 && ';
198-
}
199-
testCmd += process.argv[0] + ' --abort-on-uncaught-exception ';
200-
testCmd += process.argv[1] + ' child';
201-
var child = child_process.exec(testCmd);
202-
child.on('exit', function onExit(exitCode, signal) {
203-
var errMsg = 'Test should have aborted ' + ('but instead exited with exit code ' + exitCode) + (' and signal ' + signal);
204-
assert(exports.nodeProcessAborted(exitCode, signal), errMsg);
205-
});
206-
};
207-
208192
exports.ddCommand = function (filename, kilobytes) {
209193
if (exports.isWindows) {
210194
var p = path.resolve(exports.fixturesDir, 'create-file.js');
@@ -214,6 +198,26 @@ exports.ddCommand = function (filename, kilobytes) {
214198
}
215199
};
216200

201+
exports.spawnCat = function (options) {
202+
var spawn = require('child_process').spawn;
203+
204+
if (exports.isWindows) {
205+
return spawn('more', [], options);
206+
} else {
207+
return spawn('cat', [], options);
208+
}
209+
};
210+
211+
exports.spawnSyncCat = function (options) {
212+
var spawnSync = require('child_process').spawnSync;
213+
214+
if (exports.isWindows) {
215+
return spawnSync('more', [], options);
216+
} else {
217+
return spawnSync('cat', [], options);
218+
}
219+
};
220+
217221
exports.spawnPwd = function (options) {
218222
var spawn = require('child_process').spawn;
219223

@@ -237,8 +241,6 @@ exports.spawnSyncPwd = function (options) {
237241
exports.platformTimeout = function (ms) {
238242
if (process.config.target_defaults.default_configuration === 'Debug') ms = 2 * ms;
239243

240-
if (global.__coverage__) ms = 4 * ms;
241-
242244
if (exports.isAix) return 2 * ms; // default localhost speed is slower on AIX
243245

244246
if (process.arch !== 'arm') return ms;
@@ -252,8 +254,8 @@ exports.platformTimeout = function (ms) {
252254
return ms; // ARMv8+
253255
};
254256

255-
var knownGlobals = [Buffer, clearImmediate, clearInterval, clearTimeout, console, constructor, // Enumerable in V8 3.21.
256-
global, process, setImmediate, setInterval, setTimeout];
257+
var knownGlobals = [setTimeout, setInterval, setImmediate, clearTimeout, clearInterval, clearImmediate, console, constructor, // Enumerable in V8 3.21.
258+
Buffer, process, global];
257259

258260
if (global.gc) {
259261
knownGlobals.push(global.gc);
@@ -333,14 +335,8 @@ function leakedGlobals() {
333335
var leaked = [];
334336

335337
for (var val in global) {
336-
if (!knownGlobals.includes(global[val])) leaked.push(val);
337-
}if (global.__coverage__) {
338-
return leaked.filter(function (varname) {
339-
return !/^(cov_|__cov)/.test(varname);
340-
});
341-
} else {
342-
return leaked;
343-
}
338+
if (-1 === knownGlobals.indexOf(global[val])) leaked.push(val);
339+
}return leaked;
344340
}
345341
exports.leakedGlobals = leakedGlobals;
346342

@@ -351,7 +347,8 @@ process.on('exit', function () {
351347
if (!exports.globalCheck) return;
352348
var leaked = leakedGlobals();
353349
if (leaked.length > 0) {
354-
fail('Unexpected global(s) found: ' + leaked.join(', '));
350+
console.error('Unknown globals: %s', leaked);
351+
assert.ok(false, 'Unknown global found');
355352
}
356353
});
357354

@@ -373,7 +370,7 @@ function runCallChecks(exitCode) {
373370
}
374371

375372
exports.mustCall = function (fn, expected) {
376-
if (expected === undefined) expected = 1;else if (typeof expected !== 'number') throw new TypeError('Invalid expected value: ' + expected);
373+
if (typeof expected !== 'number') expected = 1;
377374

378375
var context = {
379376
expected: expected,
@@ -410,42 +407,8 @@ exports.fileExists = function (pathname) {
410407
}
411408
};
412409

413-
exports.canCreateSymLink = function () {
414-
// On Windows, creating symlinks requires admin privileges.
415-
// We'll only try to run symlink test if we have enough privileges.
416-
// On other platforms, creating symlinks shouldn't need admin privileges
417-
if (exports.isWindows) {
418-
// whoami.exe needs to be the one from System32
419-
// If unix tools are in the path, they can shadow the one we want,
420-
// so use the full path while executing whoami
421-
var whoamiPath = path.join(process.env['SystemRoot'], 'System32', 'whoami.exe');
422-
423-
var err = false;
424-
var output = '';
425-
426-
try {
427-
output = execSync(whoamiPath + ' /priv', { timout: 1000 });
428-
} catch (e) {
429-
err = true;
430-
} finally {
431-
if (err || !output.includes('SeCreateSymbolicLinkPrivilege')) {
432-
return false;
433-
}
434-
}
435-
}
436-
437-
return true;
438-
};
439-
440-
function fail(msg) {
410+
exports.fail = function (msg) {
441411
assert.fail(null, null, msg);
442-
}
443-
exports.fail = fail;
444-
445-
exports.mustNotCall = function (msg) {
446-
return function mustNotCall() {
447-
fail(msg || 'function should not have been called');
448-
};
449412
};
450413

451414
exports.skip = function (msg) {
@@ -498,9 +461,9 @@ exports.nodeProcessAborted = function nodeProcessAborted(exitCode, signal) {
498461
// one of them (exit code or signal) needs to be set to one of
499462
// the expected exit codes or signals.
500463
if (signal !== null) {
501-
return expectedSignals.includes(signal);
464+
return expectedSignals.indexOf(signal) > -1;
502465
} else {
503-
return expectedExitCodes.includes(exitCode);
466+
return expectedExitCodes.indexOf(exitCode) > -1;
504467
}
505468
};
506469

@@ -528,56 +491,4 @@ exports.expectWarning = function (name, expected) {
528491
// get each message only once.
529492
expected.splice(expected.indexOf(warning.message), 1);
530493
}, expected.length));
531-
};
532-
533-
Object.defineProperty(exports, 'hasIntl', {
534-
get: function () {
535-
return process.binding('config').hasIntl;
536-
}
537-
});
538-
539-
// https://github.com/w3c/testharness.js/blob/master/testharness.js
540-
exports.WPT = {
541-
test: function (fn, desc) {
542-
try {
543-
fn();
544-
} catch (err) {
545-
if (err instanceof Error) err.message = 'In ' + desc + ':\n ' + err.message;
546-
throw err;
547-
}
548-
},
549-
assert_equals: assert.strictEqual,
550-
assert_true: function (value, message) {
551-
return assert.strictEqual(value, true, message);
552-
},
553-
assert_false: function (value, message) {
554-
return assert.strictEqual(value, false, message);
555-
},
556-
assert_throws: function (code, func, desc) {
557-
assert.throws(func, function (err) {
558-
return typeof err === 'object' && 'name' in err && err.name === code.name;
559-
}, desc);
560-
},
561-
assert_array_equals: assert.deepStrictEqual,
562-
assert_unreached: function (desc) {
563-
assert.fail(undefined, undefined, 'Reached unreachable code: ' + desc);
564-
}
565-
};
566-
567-
// Useful for testing expected internal/error objects
568-
exports.expectsError = function expectsError(_ref) {
569-
var code = _ref.code,
570-
type = _ref.type,
571-
message = _ref.message;
572-
573-
return function (error) {
574-
assert.strictEqual(error.code, code);
575-
if (type !== undefined) assert(error instanceof type, error + ' is not the expected type ' + type);
576-
if (message instanceof RegExp) {
577-
assert(message.test(error.message), error.message + ' does not match ' + message);
578-
} else if (typeof message === 'string') {
579-
assert.strictEqual(error.message, message);
580-
}
581-
return true;
582-
};
583494
};

test/parallel/test-string-decoder-end.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var bufferShim = require('buffer-shims');
3+
var bufferShim = require('safe-buffer').Buffer;
44
// verify that the string decoder works getting 1 byte at a time,
55
// the whole buffer at once, and that both match the .toString(enc)
66
// result of the entire buffer.
@@ -16,7 +16,7 @@ var bufs = ['☃💩', 'asdf'].map(function (b) {
1616

1717
// also test just arbitrary bytes from 0-15.
1818
for (var i = 1; i <= 16; i++) {
19-
var bytes = '.'.repeat(i - 1).split('.').map(function (_, j) {
19+
var bytes = new Array(i).join('.').split('.').map(function (_, j) {
2020
return j + 0x78;
2121
});
2222
bufs.push(bufferShim.from(bytes));
@@ -38,8 +38,8 @@ function testBuf(encoding, buf) {
3838
// write one byte at a time.
3939
var s = new SD(encoding);
4040
var res1 = '';
41-
for (var _i = 0; _i < buf.length; _i++) {
42-
res1 += s.write(buf.slice(_i, _i + 1));
41+
for (var i = 0; i < buf.length; i++) {
42+
res1 += s.write(buf.slice(i, i + 1));
4343
}
4444
res1 += s.end();
4545

@@ -55,6 +55,6 @@ function testBuf(encoding, buf) {
5555
console.log('expect=%j', res3);
5656
console.log('res1=%j', res1);
5757
console.log('res2=%j', res2);
58-
assert.strictEqual(res1, res3, 'one byte at a time should match toString');
59-
assert.strictEqual(res2, res3, 'all bytes at once should match toString');
58+
assert.equal(res1, res3, 'one byte at a time should match toString');
59+
assert.equal(res2, res3, 'all bytes at once should match toString');
6060
}

0 commit comments

Comments
 (0)
0