Skip to content

Commit dd72d9d

Browse files
authoredJun 28, 2020
Support Uint8Array-s instead of Buffers when decoding (#246)
1 parent 3331bbc commit dd72d9d

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed
 

‎encodings/internal.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,20 @@ if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
5353

5454

5555
function InternalDecoder(options, codec) {
56-
StringDecoder.call(this, codec.enc);
56+
this.decoder = new StringDecoder(codec.enc);
5757
}
5858

59-
InternalDecoder.prototype = StringDecoder.prototype;
59+
InternalDecoder.prototype.write = function(buf) {
60+
if (!Buffer.isBuffer(buf)) {
61+
buf = Buffer.from(buf);
62+
}
63+
64+
return this.decoder.write(buf);
65+
}
66+
67+
InternalDecoder.prototype.end = function() {
68+
return this.decoder.end();
69+
}
6070

6171

6272
//------------------------------------------------------------------------------

‎encodings/utf16.js

+5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ function Utf16Decoder(options, codec) {
112112

113113
Utf16Decoder.prototype.write = function(buf) {
114114
if (!this.decoder) {
115+
// Support Uint8Array
116+
if (!Buffer.isBuffer(buf)) {
117+
buf = Buffer.from(buf)
118+
}
119+
115120
// Codec is not chosen yet. Accumulate initial bytes.
116121
this.initialBytes.push(buf);
117122
this.initialBytesLen += buf.length;

‎encodings/utf32.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ Utf32Decoder.prototype.write = function(src) {
107107
if (src.length === 0)
108108
return '';
109109

110+
// Support Uint8Array
111+
if (!Buffer.isBuffer(src)) {
112+
src = Buffer.from(src);
113+
}
114+
110115
if (this.overflow)
111116
src = Buffer.concat([this.overflow, src]);
112117

@@ -203,7 +208,12 @@ function Utf32AutoDecoder(options, codec) {
203208
}
204209

205210
Utf32AutoDecoder.prototype.write = function(buf) {
206-
if (!this.decoder) {
211+
if (!this.decoder) {
212+
// Support Uint8Array
213+
if (!Buffer.isBuffer(buf)) {
214+
buf = Buffer.from(buf);
215+
}
216+
207217
// Codec is not chosen yet. Accumulate initial bytes.
208218
this.initialBytes.push(buf);
209219
this.initialBytesLen += buf.length;

‎test/webpack/basic-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ describe("iconv-lite", function() {
3636
var str = iconv.decode(buf, "utf8");
3737
assert.equal(str, "💩");
3838
});
39+
40+
it("supports passing Uint8Array to decode for all encodings", function() {
41+
iconv.encode('', 'utf8'); // Load all encodings.
42+
43+
var encodings = Object.keys(iconv.encodings)
44+
encodings
45+
.filter(encoding =>
46+
!encoding.startsWith('_')
47+
// https://github.com/ashtuchkin/iconv-lite/issues/231
48+
&& encoding !== 'base64' && encoding !== 'hex'
49+
)
50+
.forEach(function(encoding) {
51+
var expected = 'Lorem ipsum';
52+
53+
var encoded = iconv.encode(expected, encoding);
54+
var uint8Array = Uint8Array.from(encoded);
55+
56+
var actual = iconv.decode(uint8Array, encoding);
57+
assert.equal(actual, expected, encoding);
58+
})
59+
});
3960
});
4061

4162
describe("stream module", function() {

‎test/webpack/karma.conf.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Karma configuration
22
// Generated on Sat May 23 2020 18:02:48 GMT-0400 (Eastern Daylight Time)
3+
process.env.CHROME_BIN = require('puppeteer').executablePath()
34

45
module.exports = function(config) {
56
config.set({
@@ -64,7 +65,7 @@ module.exports = function(config) {
6465

6566
// start these browsers
6667
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
67-
browsers: ['PhantomJS'],
68+
browsers: ['ChromeHeadless'],
6869

6970

7071
// Continuous Integration mode

‎test/webpack/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
},
88
"devDependencies": {
99
"karma": "^5.0.9",
10+
"karma-chrome-launcher": "^3.1.0",
1011
"karma-mocha": "^2.0.1",
11-
"karma-phantomjs-launcher": "^1.0.4",
1212
"karma-webpack": "^4.0.2",
1313
"mocha": "^7.2.0",
14-
"phantomjs-prebuilt": "^2.1.16",
14+
"puppeteer": "^4.0.0",
1515
"webpack": "^4.43.0"
1616
},
1717
"dependencies": {

0 commit comments

Comments
 (0)
Please sign in to comment.