Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from dollarshaveclub/nodejs-text-encoding
Browse files Browse the repository at this point in the history
Use TextEncoder and TextDecoder from node's util package
  • Loading branch information
hankjacobs authored Feb 19, 2019
2 parents a885a38 + 9660627 commit 05cf9f4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 86 deletions.
8 changes: 4 additions & 4 deletions lib/__tests__/cloudworker-e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()
})
Expand Down
40 changes: 40 additions & 0 deletions lib/runtime/__tests__/text-encoder.test.js
Original file line number Diff line number Diff line change
@@ -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'))
})
})
28 changes: 0 additions & 28 deletions lib/runtime/__tests__/textencoder.test.js

This file was deleted.

44 changes: 9 additions & 35 deletions lib/runtime/text-encoder.js
Original file line number Diff line number Diff line change
@@ -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
46 changes: 30 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 05cf9f4

Please sign in to comment.