From 9660627a220b74130accbb4f95298120cf9246cb Mon Sep 17 00:00:00 2001 From: Hank Jacobs Date: Tue, 19 Feb 2019 14:15:36 -0800 Subject: [PATCH] Use TextEncoder and TextDecoder from node's util package --- lib/__tests__/cloudworker-e2e.test.js | 8 ++-- lib/runtime/__tests__/text-encoder.test.js | 40 +++++++++++++++++++ lib/runtime/__tests__/textencoder.test.js | 28 ------------- lib/runtime/text-encoder.js | 44 +++++---------------- package-lock.json | 46 ++++++++++++++-------- package.json | 5 +-- 6 files changed, 85 insertions(+), 86 deletions(-) create mode 100644 lib/runtime/__tests__/text-encoder.test.js delete mode 100644 lib/runtime/__tests__/textencoder.test.js diff --git a/lib/__tests__/cloudworker-e2e.test.js b/lib/__tests__/cloudworker-e2e.test.js index be3e43e..ed1ee5a 100644 --- a/lib/__tests__/cloudworker-e2e.test.js +++ b/lib/__tests__/cloudworker-e2e.test.js @@ -312,13 +312,13 @@ describe('cloudworker-e2e', async () => { cb() }) - test('test ascii decoder can be specified', async (cb) => { + test('utf-8 decoder can be specified', async (cb) => { const script = ` addEventListener('fetch', event => { const euroSymbol = new Uint8Array([226, 130, 172]) const decoder = new TextDecoder() - const asciiDecoder = new TextDecoder('ascii') - const sameDecodedValues = (decoder.decode(euroSymbol)) === (asciiDecoder.decode(euroSymbol)) + const utfDecoder = new TextDecoder('utf-8') + const sameDecodedValues = (decoder.decode(euroSymbol)) === (utfDecoder.decode(euroSymbol)) const { readable, writable } = new TransformStream() const writer = writable.getWriter() writer.write(new TextEncoder().encode(sameDecodedValues)).then(() => writer.close()) @@ -328,7 +328,7 @@ describe('cloudworker-e2e', async () => { const server = new Cloudworker(script).listen(8080) const res = await axios.get('http://localhost:8080', defaultAxiosOpts) expect(res.status).toEqual(200) - expect(res.data).toEqual(false) + expect(res.data).toEqual(true) await server.close() cb() }) diff --git a/lib/runtime/__tests__/text-encoder.test.js b/lib/runtime/__tests__/text-encoder.test.js new file mode 100644 index 0000000..0b1692b --- /dev/null +++ b/lib/runtime/__tests__/text-encoder.test.js @@ -0,0 +1,40 @@ +const textEncoding = require('../text-encoder.js') + +describe('text-encoder', () => { + test('text decoder defaults to utf-8', () => { + const decoder = new textEncoding.TextDecoder() + expect(decoder.encoding).toEqual('utf-8') + }) + + test('able to fetch encoding from text decoder', () => { + const decoder = new textEncoding.TextDecoder('utf-8') + expect(decoder.encoding).toEqual('utf-8') + }) + + test('able to obtain ignoreBom from text decoder', () => { + const decoder = new textEncoding.TextDecoder('utf-8', { ignoreBOM: false }) + expect(decoder.encoding).toEqual('utf-8') + expect(decoder.ignoreBOM).toEqual(false) + + const decoder2 = new textEncoding.TextDecoder('utf-8', { ignoreBOM: true }) + expect(decoder2.encoding).toEqual('utf-8') + expect(decoder2.ignoreBOM).toEqual(true) + }) + + test('able to obtain fatal from text decoder', () => { + const decoder = new textEncoding.TextDecoder('utf-8', { fatal: false }) + expect(decoder.encoding).toEqual('utf-8') + expect(decoder.fatal).toEqual(false) + + const decoder2 = new textEncoding.TextDecoder('utf-8', { fatal: true }) + expect(decoder2.encoding).toEqual('utf-8') + expect(decoder2.fatal).toEqual(true) + }) + + test('text decoder throws on anything other than utf-8', () => { + expect(() => { + const decoder = new textEncoding.TextDecoder('ascii', { fatal: false }) + console.log(decoder) // prevent linting error due to decoder not being used + }).toThrow(new ReferenceError('TextDecoder only supports utf-8 encoding')) + }) +}) diff --git a/lib/runtime/__tests__/textencoder.test.js b/lib/runtime/__tests__/textencoder.test.js deleted file mode 100644 index 9e9f58a..0000000 --- a/lib/runtime/__tests__/textencoder.test.js +++ /dev/null @@ -1,28 +0,0 @@ -const textEncoding = require('../text-encoder.js') - -describe('text-encoder', () => { - test('able to fetch encoding from text decoder', () => { - const asciiDecoder = new textEncoding.TextDecoder('cp1251') - expect(asciiDecoder.encoding).toEqual('windows-1251') - }) - - test('able to obtain ignoreBom from text decoder', () => { - const asciiDecoder = new textEncoding.TextDecoder('ascii', { ignoreBOM: false }) - expect(asciiDecoder.encoding).toEqual('windows-1252') - expect(asciiDecoder.ignoreBOM).toEqual(false) - - const asciiDecoder2 = new textEncoding.TextDecoder('ascii', { ignoreBOM: true }) - expect(asciiDecoder2.encoding).toEqual('windows-1252') - expect(asciiDecoder2.ignoreBOM).toEqual(true) - }) - - test('able to obtain fatal from text decoder', () => { - const asciiDecoder = new textEncoding.TextDecoder('ascii', { fatal: false }) - expect(asciiDecoder.encoding).toEqual('windows-1252') - expect(asciiDecoder.fatal).toEqual(false) - - const asciiDecoder2 = new textEncoding.TextDecoder('ascii', { fatal: true }) - expect(asciiDecoder2.encoding).toEqual('windows-1252') - expect(asciiDecoder2.fatal).toEqual(true) - }) -}) diff --git a/lib/runtime/text-encoder.js b/lib/runtime/text-encoder.js index 2b6e95c..76e6956 100644 --- a/lib/runtime/text-encoder.js +++ b/lib/runtime/text-encoder.js @@ -1,40 +1,14 @@ -const textEncoding = require('text-encoding') +const util = require('util') -function CustomTextDecoder (label, options) { - if (!(this instanceof CustomTextDecoder)) { - throw TypeError('Called as a function. Did you forget \'new\'?') - } - this.decoder = new textEncoding.TextDecoder(label, options) - return this -} +class TextDecoder extends util.TextDecoder { + constructor () { + if (arguments.length > 0 && arguments[0] !== 'utf-8') { + throw new RangeError('TextDecoder only supports utf-8 encoding') + } -CustomTextDecoder.prototype.decode = function decode (input, options) { - if (Object.prototype.toString.call(input) === '[object Uint8Array]') { - const buffer = new ArrayBuffer(input.length) - const view = new Uint8Array(buffer) - view.set(input) - return this.decoder.decode( - view, - options) + super(...arguments) } - - return this.decoder.decode(input, options) } -Object.defineProperty(CustomTextDecoder.prototype, 'encoding', { - /** @this {CustomTextDecoder} */ - get: function get () { return this.decoder.encoding }, -}) - -Object.defineProperty(CustomTextDecoder.prototype, 'fatal', { - /** @this {CustomTextDecoder} */ - get: function get () { return this.decoder.fatal }, -}) - -Object.defineProperty(CustomTextDecoder.prototype, 'ignoreBOM', { - /** @this {CustomTextDecoder} */ - get: function get () { return this.decoder.ignoreBOM }, -}) - -module.exports.TextDecoder = CustomTextDecoder -module.exports.TextEncoder = textEncoding.TextEncoder +module.exports.TextDecoder = TextDecoder +module.exports.TextEncoder = util.TextEncoder diff --git a/package-lock.json b/package-lock.json index 432f7c1..d6ccabd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2799,7 +2799,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2820,12 +2821,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2840,17 +2843,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2967,7 +2973,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2979,6 +2986,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2993,6 +3001,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3000,12 +3009,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -3024,6 +3035,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3104,7 +3116,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3116,6 +3129,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3201,7 +3215,8 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -3237,6 +3252,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3256,6 +3272,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3299,12 +3316,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -6703,11 +6722,6 @@ } } }, - "text-encoding": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/text-encoding/-/text-encoding-0.7.0.tgz", - "integrity": "sha512-oJQ3f1hrOnbRLOcwKz0Liq2IcrvDeZRHXhd9RgLrsT+DjWY/nty1Hi7v3dtkaEYbPYe0mUoOfzRrMwfXXwgPUA==" - }, "text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", diff --git a/package.json b/package.json index abfcea7..f5e5577 100644 --- a/package.json +++ b/package.json @@ -35,13 +35,12 @@ "@dollarshaveclub/node-fetch": "^3.1.0", "@mattiasbuelens/web-streams-polyfill": "^0.1.0", "arraybuffer-equal": "^1.0.4", + "b2a": "^1.0.10", "commander": "^2.19.0", "http-cache-semantics": "^4.0.1", "lru-cache": "^5.1.1", "moment": "^2.22.2", - "text-encoding": "^0.7.0", - "node-webcrypto-ossl": "^1.0.39", - "b2a": "^1.0.10" + "node-webcrypto-ossl": "^1.0.39" }, "devDependencies": { "@types/node": "^10.12.18",