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

Commit

Permalink
Support for Node v0.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Apr 6, 2017
1 parent d27e49a commit 53f692c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 38 deletions.
14 changes: 14 additions & 0 deletions build/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;'
Expand Down
19 changes: 19 additions & 0 deletions build/test-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [
Expand Down Expand Up @@ -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/
, ''
]
]

4 changes: 3 additions & 1 deletion lib/string_decoder.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-string-decoder-end.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -9,15 +11,15 @@ 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.
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);
Expand Down
67 changes: 33 additions & 34 deletions test/parallel/test-string-decoder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var bufferShim = require('buffer-shims');
require('../common');
var assert = require('assert');
var inspect = require('util').inspect;
Expand All @@ -12,18 +13,18 @@ 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
// U+0064 -> 64
// 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
Expand All @@ -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 () {
Expand Down

0 comments on commit 53f692c

Please sign in to comment.