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

Expose TextEncoder for use in workers #31

Merged
merged 8 commits into from
Jan 15, 2019
Merged
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
58 changes: 58 additions & 0 deletions lib/__tests__/cloudworker-e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,62 @@ describe('cloudworker-e2e', async () => {
await server.close()
cb()
})

test('simple text encoder', async (cb) => {
const script = `
addEventListener('fetch', event => {
const encoder = new TextEncoder()
const { readable, writable } = new TransformStream()
const writer = writable.getWriter()
writer.write(encoder.encode('hello')).then(() => writer.close())
event.respondWith(new Response(readable, {status: 200}))
})
`
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('hello')
await server.close()
cb()
})

test('simple text encoder and decoder', async (cb) => {
const script = `
addEventListener('fetch', event => {
const helloBytes = new Uint8Array([104, 101, 108, 108, 111])
const decoder = new TextDecoder()
const { readable, writable } = new TransformStream()
const writer = writable.getWriter()
writer.write(new TextEncoder().encode(decoder.decode(helloBytes))).then(() => writer.close())
event.respondWith(new Response(readable, {status: 200}))
})
`
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('hello')
await server.close()
cb()
})

test('test ascii 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 { readable, writable } = new TransformStream()
const writer = writable.getWriter()
writer.write(new TextEncoder().encode(sameDecodedValues)).then(() => writer.close())
event.respondWith(new Response(readable, {status: 200}))
})
`
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)
await server.close()
cb()
})
})
3 changes: 3 additions & 0 deletions lib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Request, Response, fetch, Headers, freezeHeaders } = require('./runtime/
const { URL } = require('./runtime/url')
const { ReadableStream, WritableStream, TransformStream } = require('./runtime/stream')
const { FetchEvent } = require('./runtime/fetch-event')
const { TextDecoder, TextEncoder } = require('./runtime/text-encoder')
const { atob, btoa } = require('./runtime/base64')

class Context {
Expand All @@ -18,6 +19,8 @@ class Context {
this.TransformStream = TransformStream
this.FetchEvent = FetchEvent
this.caches = cacheFactory
this.TextDecoder = TextDecoder
this.TextEncoder = TextEncoder
this.atob = atob
this.btoa = btoa
Object.assign(this, bindings)
Expand Down
28 changes: 28 additions & 0 deletions lib/runtime/__tests__/textencoder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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)
})
})
40 changes: 40 additions & 0 deletions lib/runtime/text-encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const textEncoding = require('text-encoding')

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
}

CustomTextDecoder.prototype.decode = function decode (input, options) {
hankjacobs marked this conversation as resolved.
Show resolved Hide resolved
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)
}

return this.decoder.decode(input, options)
}

Object.defineProperty(CustomTextDecoder.prototype, 'encoding', {
/** @this {CustomTextDecoder} */
get: function () { return this.decoder.encoding },
})

Object.defineProperty(CustomTextDecoder.prototype, 'fatal', {
/** @this {CustomTextDecoder} */
get: function () { return this.decoder.fatal },
})

Object.defineProperty(CustomTextDecoder.prototype, 'ignoreBOM', {
/** @this {CustomTextDecoder} */
get: function () { return this.decoder.ignoreBOM },
})

module.exports.TextDecoder = CustomTextDecoder
module.exports.TextEncoder = textEncoding.TextEncoder