[Pkg-javascript-commits] [node-string-decoder] 20/27: Support for Node v0.8.
Bastien Roucariès
rouca at moszumanska.debian.org
Thu May 11 15:12:32 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-string-decoder.
commit 53f692ce616c4b03c0c804aa2eee492b0fe8c642
Author: Matteo Collina <hello at matteocollina.com>
Date: Thu Apr 6 12:05:23 2017 +0200
Support for Node v0.8.
---
build/files.js | 14 +++++++
build/test-replacements.js | 19 +++++++++
lib/string_decoder.js | 4 +-
package.json | 4 +-
test/parallel/test-string-decoder-end.js | 6 ++-
test/parallel/test-string-decoder.js | 67 ++++++++++++++++----------------
6 files changed, 76 insertions(+), 38 deletions(-)
diff --git a/build/files.js b/build/files.js
index 72407ce..647cf1c 100644
--- a/build/files.js
+++ b/build/files.js
@@ -14,11 +14,25 @@ module.exports['string_decoder.js'] = [
, ''
]
+ , [
+ /const Buffer = require\('buffer'\).Buffer;/
+ , 'var Buffer = require(\'buffer\').Buffer;\n' +
+ 'var bufferShim = require(\'buffer-shims\');'
+ ]
+
+ // allocUnsafe
+
+ , [
+ /Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
+ 'bufferShim.$1'
+ ]
+
// add Buffer.isEncoding where missing
, [
/const isEncoding = Buffer\[internalUtil.kIsEncodingSymbol\];/
, '\nvar isEncoding = Buffer.isEncoding'
+ '\n || function(encoding) {'
+ + '\n encoding = \'\' + encoding'
+ '\n switch (encoding && encoding.toLowerCase()) {'
+ '\n case \'hex\': case \'utf8\': case \'utf-8\': case \'ascii\': case \'binary\': case \'base64\': case \'ucs2\': case \'ucs-2\': case \'utf16le\': case \'utf-16le\': case \'raw\': return true;'
+ '\n default: return false;'
diff --git a/build/test-replacements.js b/build/test-replacements.js
index f886f8b..8989250 100644
--- a/build/test-replacements.js
+++ b/build/test-replacements.js
@@ -4,6 +4,16 @@ module.exports.all = [
, 'require(\'../../\')'
]
+ , [
+ /Buffer\.((?:alloc)|(?:allocUnsafe)|(?:from))/g,
+ 'bufferShim.$1'
+ ]
+
+ , [
+ /^('use strict';)$/m,
+ '$1\nconst bufferShim = require(\'buffer-shims\');'
+ ]
+
]
module.exports['common.js'] = [
@@ -42,3 +52,12 @@ module.exports['common.js'] = [
+ ' /*</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/
+ , ''
+ ]
+]
+
diff --git a/lib/string_decoder.js b/lib/string_decoder.js
index 6c598d2..696d7ab 100644
--- a/lib/string_decoder.js
+++ b/lib/string_decoder.js
@@ -1,8 +1,10 @@
'use strict';
var Buffer = require('buffer').Buffer;
+var bufferShim = require('buffer-shims');
var isEncoding = Buffer.isEncoding || function (encoding) {
+ encoding = '' + encoding;
switch (encoding && encoding.toLowerCase()) {
case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw':
return true;
@@ -76,7 +78,7 @@ function StringDecoder(encoding) {
}
this.lastNeed = 0;
this.lastTotal = 0;
- this.lastChar = Buffer.allocUnsafe(nb);
+ this.lastChar = bufferShim.allocUnsafe(nb);
}
StringDecoder.prototype.write = function (buf) {
diff --git a/package.json b/package.json
index e1c2e54..94668a8 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,9 @@
"version": "0.11.10-3",
"description": "The string_decoder module from Node core",
"main": "lib/string_decoder.js",
- "dependencies": {},
+ "dependencies": {
+ "buffer-shims": "^1.0.0"
+ },
"devDependencies": {
"babel-polyfill": "^6.23.0",
"tap": "~0.4.8"
diff --git a/test/parallel/test-string-decoder-end.js b/test/parallel/test-string-decoder-end.js
index 0031adf..b82c3ca 100644
--- a/test/parallel/test-string-decoder-end.js
+++ b/test/parallel/test-string-decoder-end.js
@@ -1,4 +1,6 @@
'use strict';
+
+var bufferShim = require('buffer-shims');
// verify that the string decoder works getting 1 byte at a time,
// the whole buffer at once, and that both match the .toString(enc)
// result of the entire buffer.
@@ -9,7 +11,7 @@ var SD = require('../../').StringDecoder;
var encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];
var bufs = ['☃💩', 'asdf'].map(function (b) {
- return Buffer.from(b);
+ return bufferShim.from(b);
});
// also test just arbitrary bytes from 0-15.
@@ -17,7 +19,7 @@ for (var i = 1; i <= 16; i++) {
var bytes = '.'.repeat(i - 1).split('.').map(function (_, j) {
return j + 0x78;
});
- bufs.push(Buffer.from(bytes));
+ bufs.push(bufferShim.from(bytes));
}
encodings.forEach(testEncoding);
diff --git a/test/parallel/test-string-decoder.js b/test/parallel/test-string-decoder.js
index 9345e03..38e21d2 100644
--- a/test/parallel/test-string-decoder.js
+++ b/test/parallel/test-string-decoder.js
@@ -1,5 +1,6 @@
'use strict';
+var bufferShim = require('buffer-shims');
require('../common');
var assert = require('assert');
var inspect = require('util').inspect;
@@ -12,10 +13,10 @@ assert.strictEqual(decoder.encoding, 'utf8');
process.stdout.write('scanning ');
// UTF-8
-test('utf-8', Buffer.from('$', 'utf-8'), '$');
-test('utf-8', Buffer.from('¢', 'utf-8'), '¢');
-test('utf-8', Buffer.from('€', 'utf-8'), '€');
-test('utf-8', Buffer.from('𤭢', 'utf-8'), '𤭢');
+test('utf-8', bufferShim.from('$', 'utf-8'), '$');
+test('utf-8', bufferShim.from('¢', 'utf-8'), '¢');
+test('utf-8', bufferShim.from('€', 'utf-8'), '€');
+test('utf-8', bufferShim.from('𤭢', 'utf-8'), '𤭢');
// A mixed ascii and non-ascii string
// Test stolen from deps/v8/test/cctest/test-strings.cc
// U+02E4 -> CB A4
@@ -23,7 +24,7 @@ test('utf-8', Buffer.from('𤭢', 'utf-8'), '𤭢');
// U+12E4 -> E1 8B A4
// U+0030 -> 30
// U+3045 -> E3 81 85
-test('utf-8', Buffer.from([0xCB, 0xA4, 0x64, 0xE1, 0x8B, 0xA4, 0x30, 0xE3, 0x81, 0x85]), '\u02e4\u0064\u12e4\u0030\u3045');
+test('utf-8', bufferShim.from([0xCB, 0xA4, 0x64, 0xE1, 0x8B, 0xA4, 0x30, 0xE3, 0x81, 0x85]), '\u02e4\u0064\u12e4\u0030\u3045');
// Some invalid input, known to have caused trouble with chunking
// in https://github.com/nodejs/node/pull/7310#issuecomment-226445923
@@ -35,72 +36,70 @@ test('utf-8', Buffer.from([0xCB, 0xA4, 0x64, 0xE1, 0x8B, 0xA4, 0x30, 0xE3, 0x81,
// F0: 11110|000 four-byte head
// F1: 11110|001'another four-byte head
// FB: 111110|11 "five-byte head", not UTF-8
-test('utf-8', Buffer.from('C9B5A941', 'hex'), '\u0275\ufffdA');
-test('utf-8', Buffer.from('E2', 'hex'), '\ufffd');
-test('utf-8', Buffer.from('E241', 'hex'), '\ufffdA');
-test('utf-8', Buffer.from('CCCCB8', 'hex'), '\ufffd\u0338');
-test('utf-8', Buffer.from('F0B841', 'hex'), '\ufffd\ufffdA');
-test('utf-8', Buffer.from('F1CCB8', 'hex'), '\ufffd\u0338');
-test('utf-8', Buffer.from('F0FB00', 'hex'), '\ufffd\ufffd\0');
-test('utf-8', Buffer.from('CCE2B8B8', 'hex'), '\ufffd\u2e38');
-test('utf-8', Buffer.from('E2B8CCB8', 'hex'), '\ufffd\ufffd\u0338');
-test('utf-8', Buffer.from('E2FBCC01', 'hex'), '\ufffd\ufffd\ufffd\u0001');
-test('utf-8', Buffer.from('EDA0B5EDB08D', 'hex'), // CESU-8 of U+1D40D
-'\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd');
-test('utf-8', Buffer.from('CCB8CDB9', 'hex'), '\u0338\u0379');
+test('utf-8', bufferShim.from('C9B5A941', 'hex'), '\u0275\ufffdA');
+test('utf-8', bufferShim.from('E2', 'hex'), '\ufffd');
+test('utf-8', bufferShim.from('E241', 'hex'), '\ufffdA');
+test('utf-8', bufferShim.from('CCCCB8', 'hex'), '\ufffd\u0338');
+test('utf-8', bufferShim.from('F0B841', 'hex'), '\ufffd\ufffdA');
+test('utf-8', bufferShim.from('F1CCB8', 'hex'), '\ufffd\u0338');
+test('utf-8', bufferShim.from('F0FB00', 'hex'), '\ufffd\ufffd\0');
+test('utf-8', bufferShim.from('CCE2B8B8', 'hex'), '\ufffd\u2e38');
+test('utf-8', bufferShim.from('E2B8CCB8', 'hex'), '\ufffd\ufffd\u0338');
+test('utf-8', bufferShim.from('E2FBCC01', 'hex'), '\ufffd\ufffd\ufffd\u0001');
+test('utf-8', bufferShim.from('CCB8CDB9', 'hex'), '\u0338\u0379');
// UCS-2
-test('ucs2', Buffer.from('ababc', 'ucs2'), 'ababc');
+test('ucs2', bufferShim.from('ababc', 'ucs2'), 'ababc');
// UTF-16LE
-test('utf16le', Buffer.from('3DD84DDC', 'hex'), '\ud83d\udc4d'); // thumbs up
+test('utf16le', bufferShim.from('3DD84DDC', 'hex'), '\ud83d\udc4d'); // thumbs up
console.log(' crayon!');
// Additional UTF-8 tests
decoder = new StringDecoder('utf8');
-assert.strictEqual(decoder.write(Buffer.from('E1', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('E1', 'hex')), '');
assert.strictEqual(decoder.end(), '\ufffd');
decoder = new StringDecoder('utf8');
-assert.strictEqual(decoder.write(Buffer.from('E18B', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('E18B', 'hex')), '');
assert.strictEqual(decoder.end(), '\ufffd\ufffd');
decoder = new StringDecoder('utf8');
-assert.strictEqual(decoder.write(Buffer.from('\ufffd')), '\ufffd');
+assert.strictEqual(decoder.write(bufferShim.from('\ufffd')), '\ufffd');
assert.strictEqual(decoder.end(), '');
decoder = new StringDecoder('utf8');
-assert.strictEqual(decoder.write(Buffer.from('\ufffd\ufffd\ufffd')), '\ufffd\ufffd\ufffd');
+assert.strictEqual(decoder.write(bufferShim.from('\ufffd\ufffd\ufffd')), '\ufffd\ufffd\ufffd');
assert.strictEqual(decoder.end(), '');
decoder = new StringDecoder('utf8');
-assert.strictEqual(decoder.write(Buffer.from('EFBFBDE2', 'hex')), '\ufffd');
+assert.strictEqual(decoder.write(bufferShim.from('EFBFBDE2', 'hex')), '\ufffd');
assert.strictEqual(decoder.end(), '\ufffd');
decoder = new StringDecoder('utf8');
-assert.strictEqual(decoder.write(Buffer.from('F1', 'hex')), '');
-assert.strictEqual(decoder.write(Buffer.from('41F2', 'hex')), '\ufffdA');
+assert.strictEqual(decoder.write(bufferShim.from('F1', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('41F2', 'hex')), '\ufffdA');
assert.strictEqual(decoder.end(), '\ufffd');
// Additional utf8Text test
decoder = new StringDecoder('utf8');
-assert.strictEqual(decoder.text(Buffer.from([0x41]), 2), '');
+assert.strictEqual(decoder.text(bufferShim.from([0x41]), 2), '');
// Additional UTF-16LE surrogate pair tests
decoder = new StringDecoder('utf16le');
-assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
-assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
-assert.strictEqual(decoder.write(Buffer.from('DC', 'hex')), '\ud83d\udc4d');
+assert.strictEqual(decoder.write(bufferShim.from('3DD8', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('4D', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('DC', 'hex')), '\ud83d\udc4d');
assert.strictEqual(decoder.end(), '');
decoder = new StringDecoder('utf16le');
-assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('3DD8', 'hex')), '');
assert.strictEqual(decoder.end(), '\ud83d');
decoder = new StringDecoder('utf16le');
-assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
-assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('3DD8', 'hex')), '');
+assert.strictEqual(decoder.write(bufferShim.from('4D', 'hex')), '');
assert.strictEqual(decoder.end(), '\ud83d');
assert.throws(function () {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-string-decoder.git
More information about the Pkg-javascript-commits
mailing list