Skip to content

Commit

Permalink
Add TextEncoder/TextDecoder support.
Browse files Browse the repository at this point in the history
Fixes denoland#470

This commit increases size:
out/release/gen/bundle/main.js      7.3M -> 7.9M
out/release/gen/bundle/main.js.map   11M -> 12M
out/release/gen/snapshot_deno.bin    34M -> 37M
out/release/deno                     49M -> 53M

Note the amount in the JS code added is quite small:
4.0K    node_modules/text-encoding/index.js
4.0K    node_modules/@types/text-encoding/index.d.ts
4.0K    js/text_encoding.ts

Unclear to me what is causing the jump in snapshot size.
  • Loading branch information
ry committed Aug 9, 2018
1 parent fb87cb3 commit 040a042
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ run_node("bundle") {
"js/os.ts",
"js/plugins.d.ts",
"js/runtime.ts",
"js/text_encoding.ts",
"js/timers.ts",
"js/types.d.ts",
"js/util.ts",
Expand Down
12 changes: 8 additions & 4 deletions js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Console } from "./console";
import { RawSourceMap } from "./types";
import * as timers from "./timers";
import { TextEncoder, TextDecoder } from "./text_encoding";

declare global {
interface Window {
Expand All @@ -16,6 +17,11 @@ declare global {

const console: Console;
const window: Window;

// tslint:disable:variable-name
let TextEncoder: TextEncoder;
let TextDecoder: TextDecoder;
// tslint:enable:variable-name
}

// If you use the eval function indirectly, by invoking it via a reference
Expand Down Expand Up @@ -49,10 +55,8 @@ window.clearTimeout = timers.clearTimer;
window.clearInterval = timers.clearTimer;

window.console = new Console(libdeno.print);
window.TextEncoder = TextEncoder;
window.TextDecoder = TextDecoder;

// import { fetch } from "./fetch";
// window["fetch"] = fetch;

// import { TextEncoder, TextDecoder } from "text-encoding";
// window["TextEncoder"] = TextEncoder;
// window["TextDecoder"] = TextDecoder;
6 changes: 0 additions & 6 deletions js/text-encoding.d.ts

This file was deleted.

28 changes: 28 additions & 0 deletions js/text_encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.

// @types/text-encoding relies on lib.dom.d.ts for some interfaces. We do not
// want to include lib.dom.d.ts (due to size) into deno's global type scope.
// Therefore this hack: add a few of the missing interfaces in
// @types/text-encoding to the global scope before importing.

declare global {
type BufferSource = ArrayBufferView | ArrayBuffer;

interface TextDecodeOptions {
stream?: boolean;
}

interface TextDecoderOptions {
fatal?: boolean;
ignoreBOM?: boolean;
}

interface TextDecoder {
readonly encoding: string;
readonly fatal: boolean;
readonly ignoreBOM: boolean;
decode(input?: BufferSource, options?: TextDecodeOptions): string;
}
}

export { TextEncoder, TextDecoder } from "text-encoding";
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"@types/base64-js": "^1.2.5",
"@types/flatbuffers": "^1.9.0",
"@types/source-map-support": "^0.4.1",
"@types/text-encoding": "0.0.33",
"base64-js": "^1.3.0",
"flatbuffers": "^1.9.0",
"prettier": "^1.14.0",
Expand All @@ -16,6 +17,7 @@
"rollup-plugin-typescript2": "^0.16.1",
"rollup-pluginutils": "^2.3.0",
"source-map-support": "^0.5.6",
"text-encoding": "0.6.4",
"tslint": "^5.10.0",
"tslint-eslint-rules": "^5.3.1",
"tslint-no-circular-imports": "^0.5.0",
Expand Down

0 comments on commit 040a042

Please sign in to comment.