Skip to content

Commit

Permalink
Adding support for stream based on iconv-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
fguitton committed Mar 30, 2020
1 parent f665f0d commit 3e93928
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
35 changes: 26 additions & 9 deletions lib/TextDecoder-impl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const { labelToName, isSupported, decode } = require("./whatwg-encoding");
const { labelToName, isSupported } = require("./whatwg-encoding");
const iconvLite = require("iconv-lite");

exports.implementation = class TextDecoderImpl {
constructor(globalObject, [label, options = {}]) {
Expand All @@ -26,16 +27,32 @@ exports.implementation = class TextDecoderImpl {
}

decode(input, options = {}) {
if (options.stream === true) {
// TODO: Implement stream support
let bytes;
if (input === undefined) {
bytes = new Uint8Array(0);
} else if (input.buffer === undefined) {
bytes = Buffer.from(input);
} else {
bytes = input;
}
try {
return decode(input, this._encoding, this._ignoreBOM);
} catch (exception) {
if (this._errorMode === "fatal") {
throw new TypeError(exception);

let output = "";

if (options.stream === true) {
if (this._decoder === undefined) {
this._decoder = iconvLite.decodeStream(this._encoding);
}
this._decoder.write(input);
let chunk;
while ((chunk = this._decoder.read()) !== null) {
output += chunk;
}
return exception;
} else {
output = iconvLite.decode(bytes, this._encoding, {
stripBOM: !this.ignoreBOM
});
}

return output;
}
};
14 changes: 3 additions & 11 deletions lib/whatwg-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,22 @@ exports.labelToName = label => {
};

// https://encoding.spec.whatwg.org/#decode
exports.decode = (input, fallbackEncodingName, ignoreBOM) => {
exports.decode = (buffer, fallbackEncodingName) => {
let encoding = fallbackEncodingName;
if (!exports.isSupported(encoding)) {
throw new RangeError(`"${encoding}" is not a supported encoding name`);
}

const bomEncoding = exports.getBOMEncoding(input);
const bomEncoding = exports.getBOMEncoding(buffer);
if (bomEncoding !== null) {
encoding = bomEncoding;
}

// iconv-lite will strip BOMs for us, so no need to do the stuff the spec does

const decodeOptions = {
stripBOM: !ignoreBOM
};

if (input.buffer !== undefined)
return iconvLite.decode(input, encoding, decodeOptions);
else
return iconvLite.decode(Buffer.from(input), encoding, decodeOptions);
return iconvLite.decode(buffer, encoding);
};


// https://encoding.spec.whatwg.org/#encode
exports.encode = (buffer) => {
return iconvLite.encode(buffer, "UTF-8")
Expand Down
13 changes: 9 additions & 4 deletions scripts/get-latest-platform-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,30 @@ for (const file of [
// Missing testharness 'createBuffer' function
// "textdecoder-copy.any.js",

// Package 'iconv-lite' does not support 'x-mac-cyrillic'
// Package 'iconv-lite' does not support fatal throw and encoding 'x-mac-cyrillic'
// "textdecoder-fatal-single-byte.any.js",

// Stream support not implemented
// Package 'iconv-lite' does not support fatal throw
// "textdecoder-fatal-streaming.any.js",

// Package 'iconv-lite' does not support fatal throw
// "textdecoder-fatal.any.js",

// Package 'iconv-lite'
"textdecoder-ignorebom.any.js",

// Infinite looping
// Infinite looping does not support many encodings
// "textdecoder-labels.any.js",

// Stream support not implemented
// Missing testharness 'createBuffer' function
// "textdecoder-streaming.any.js",

// "textdecoder-utf16-surrogates.any.js"
// Issue with package 'iconv-lite' not converting all inputs properly
// "textdecoder-utf16-surrogates.any.js",

// Package 'iconv-lite' does not support 'x-mac-cyrillic', 'ISO-8859-8-I', 'ISO-2022-JP', 'x-user-defined'
// Package 'iconv-lite' does not support encoding 'x-mac-cyrillic', 'ISO-8859-8-I', 'ISO-2022-JP', 'x-user-defined'
// "textencoder-constructor-non-utf.any.js",

"textencoder-utf16-surrogates.any.js",
Expand Down
8 changes: 4 additions & 4 deletions test/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ module.exports = {
assert.deepStrictEqual([...actual], [...expected]);
},

assert_throws(code, func) {
assert.throws(func);
assert_throws(code, func, message) {
assert.throws(func, message);
},

assert_throws_js(code, func) {
assert.throws(func);
assert_throws_js(code, func, message) {
assert.throws(func, message);
},

assert_unreached() {
Expand Down

0 comments on commit 3e93928

Please sign in to comment.