Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose TextEncoder and TextDecoder #11

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
coverage/
test/web-platform-tests/
lib/Function.js
lib/TextEncoder.js
lib/TextDecoder.js
lib/TextDecodeOptions.js
lib/TextDecoderOptions.js
lib/TextEncoderEncodeIntoResult.js
lib/VoidFunction.js
lib/whatwg-encoding.js
lib/utils.js
live-viewer/whatwg-encoding.js
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# lint requires lf line endings
*.js text eol=lf
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
fail-fast: false
matrix:
node-version:
- 12
- 14
- 16
architecture:
Expand Down
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/node_modules/
/npm-debug.log

/coverage/
/test/web-platform-tests/
/lib/*.json
/lib/Function.js
/lib/TextEncoder.js
/lib/TextDecoder.js
/lib/TextDecodeOptions.js
/lib/TextDecoderOptions.js
/lib/TextEncoderEncodeIntoResult.js
/lib/VoidFunction.js
/lib/utils.js
/live-viewer/whatwg-encoding.js
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

const { TextEncoder, TextDecoder } = require("./webidl2js-wrapper");
const whatwgEncoding = require("./lib/whatwg-encoding");

const sharedGlobalObject = globalThis;
TextEncoder.install(sharedGlobalObject, ["Window"]);
TextDecoder.install(sharedGlobalObject, ["Window"]);

exports.TextEncoder = sharedGlobalObject.TextEncoder;
exports.TextDecoder = sharedGlobalObject.TextDecoder;

exports.decode = whatwgEncoding.decode;
exports.getBOMEncoding = whatwgEncoding.getBOMEncoding;
exports.isSupported = whatwgEncoding.isSupported;
exports.labelToName = whatwgEncoding.labelToName;
58 changes: 58 additions & 0 deletions lib/TextDecoder-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"use strict";

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

exports.implementation = class TextDecoderImpl {
constructor(globalObject, [label, options = {}]) {
const encoding = labelToName(label);
if (encoding === null || !isSupported(encoding) || encoding === "replacement") {
throw new RangeError(`"${label}" is not a supported encoding name`);
}
this._encoding = encoding;
this._errorMode = options.fatal === true ? "fatal" : "replacement";
this._ignoreBOM = options.ignoreBOM || false;
}

get encoding() {
return this._encoding.toLowerCase();
}

get fatal() {
return this._errorMode === "fatal";
}

get ignoreBOM() {
return this._ignoreBOM;
}

decode(input, options = {}) {
let bytes;
if (input === undefined) {
bytes = new Uint8Array(0);
} else if (input.buffer === undefined) {
bytes = Buffer.from(input);
} else {
bytes = input;
}

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;
}
} else {
output = iconvLite.decode(bytes, this._encoding, {
stripBOM: !this.ignoreBOM
});
}

return output;
}
};
197 changes: 197 additions & 0 deletions lib/TextDecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
"use strict";

const conversions = require("webidl-conversions");
const utils = require("./utils.js");

const TextDecoderOptions = require("./TextDecoderOptions.js");
const TextDecodeOptions = require("./TextDecodeOptions.js");
const implSymbol = utils.implSymbol;
const ctorRegistrySymbol = utils.ctorRegistrySymbol;

const interfaceName = "TextDecoder";

exports.is = value => {
return utils.isObject(value) && utils.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
};
exports.isImpl = value => {
return utils.isObject(value) && value instanceof Impl.implementation;
};
exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
if (exports.is(value)) {
return utils.implForWrapper(value);
}
throw new globalObject.TypeError(`${context} is not of type 'TextDecoder'.`);
};

function makeWrapper(globalObject, newTarget) {
let proto;
if (newTarget !== undefined) {
proto = newTarget.prototype;
}

if (!utils.isObject(proto)) {
proto = globalObject[ctorRegistrySymbol]["TextDecoder"].prototype;
}

return Object.create(proto);
}

exports.create = (globalObject, constructorArgs, privateData) => {
const wrapper = makeWrapper(globalObject);
return exports.setup(wrapper, globalObject, constructorArgs, privateData);
};

exports.createImpl = (globalObject, constructorArgs, privateData) => {
const wrapper = exports.create(globalObject, constructorArgs, privateData);
return utils.implForWrapper(wrapper);
};

exports._internalSetup = (wrapper, globalObject) => {};

exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
privateData.wrapper = wrapper;

exports._internalSetup(wrapper, globalObject);
Object.defineProperty(wrapper, implSymbol, {
value: new Impl.implementation(globalObject, constructorArgs, privateData),
configurable: true
});

wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
if (Impl.init) {
Impl.init(wrapper[implSymbol]);
}
return wrapper;
};

exports.new = (globalObject, newTarget) => {
const wrapper = makeWrapper(globalObject, newTarget);

exports._internalSetup(wrapper, globalObject);
Object.defineProperty(wrapper, implSymbol, {
value: Object.create(Impl.implementation.prototype),
configurable: true
});

wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
if (Impl.init) {
Impl.init(wrapper[implSymbol]);
}
return wrapper[implSymbol];
};

const exposed = new Set(["Window", "Worker"]);

exports.install = (globalObject, globalNames) => {
if (!globalNames.some(globalName => exposed.has(globalName))) {
return;
}

const ctorRegistry = utils.initCtorRegistry(globalObject);
class TextDecoder {
constructor() {
const args = [];
{
let curArg = arguments[0];
if (curArg !== undefined) {
curArg = conversions["DOMString"](curArg, {
context: "Failed to construct 'TextDecoder': parameter 1",
globals: globalObject
});
} else {
curArg = "utf-8";
}
args.push(curArg);
}
{
let curArg = arguments[1];
curArg = TextDecoderOptions.convert(globalObject, curArg, {
context: "Failed to construct 'TextDecoder': parameter 2"
});
args.push(curArg);
}
return exports.setup(Object.create(new.target.prototype), globalObject, args);
}

decode() {
const esValue = this !== null && this !== undefined ? this : globalObject;
if (!exports.is(esValue)) {
throw new globalObject.TypeError("'decode' called on an object that is not a valid instance of TextDecoder.");
}
const args = [];
{
let curArg = arguments[0];
if (curArg !== undefined) {
if (utils.isArrayBuffer(curArg)) {
} else if (ArrayBuffer.isView(curArg)) {
} else {
throw new globalObject.TypeError(
"Failed to execute 'decode' on 'TextDecoder': parameter 1" + " is not of any supported type."
);
}
}
args.push(curArg);
}
{
let curArg = arguments[1];
curArg = TextDecodeOptions.convert(globalObject, curArg, {
context: "Failed to execute 'decode' on 'TextDecoder': parameter 2"
});
args.push(curArg);
}
return esValue[implSymbol].decode(...args);
}

get encoding() {
const esValue = this !== null && this !== undefined ? this : globalObject;

if (!exports.is(esValue)) {
throw new globalObject.TypeError(
"'get encoding' called on an object that is not a valid instance of TextDecoder."
);
}

return esValue[implSymbol]["encoding"];
}

get fatal() {
const esValue = this !== null && this !== undefined ? this : globalObject;

if (!exports.is(esValue)) {
throw new globalObject.TypeError(
"'get fatal' called on an object that is not a valid instance of TextDecoder."
);
}

return esValue[implSymbol]["fatal"];
}

get ignoreBOM() {
const esValue = this !== null && this !== undefined ? this : globalObject;

if (!exports.is(esValue)) {
throw new globalObject.TypeError(
"'get ignoreBOM' called on an object that is not a valid instance of TextDecoder."
);
}

return esValue[implSymbol]["ignoreBOM"];
}
}
Object.defineProperties(TextDecoder.prototype, {
decode: { enumerable: true },
encoding: { enumerable: true },
fatal: { enumerable: true },
ignoreBOM: { enumerable: true },
[Symbol.toStringTag]: { value: "TextDecoder", configurable: true }
});
ctorRegistry[interfaceName] = TextDecoder;

Object.defineProperty(globalObject, interfaceName, {
configurable: true,
writable: true,
value: TextDecoder
});
};

const Impl = require("./TextDecoder-impl.js");
21 changes: 21 additions & 0 deletions lib/TextEncoder-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

const { encode } = require("./whatwg-encoding");

exports.implementation = class TextEncoderImpl {
constructor() {
this._encoding = "utf-8";
}

get encoding() {
return this._encoding;
}

encode(input = "") {
return encode(Buffer.from(input));
}

encodeInto(/* source, destination */) {
// TODO Implement stream copy support
}
};
Loading