Skip to content

Commit

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

This commit increases exe size
out/release/deno  48M -> 52M
  • Loading branch information
ry committed Aug 8, 2018
1 parent ccbc18b commit da2e8a2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 14 deletions.
24 changes: 18 additions & 6 deletions js/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.

// tslint:disable-next-line:no-reference
/// <reference path="./text_encoding.d.ts" />
import * as te from "text-encoding";
import { Console } from "./console";

declare global {
Expand All @@ -18,6 +20,18 @@ declare global {
const console: Console;
const deno: Readonly<Deno>;
const window: Window;

// tslint:disable-next-line:variable-name
let TextDecoder: {
prototype: TextDecoder;
new (label?: string, options?: TextDecoderOptions): TextDecoder;
};

// tslint:disable-next-line:variable-name
let TextEncoder: {
prototype: TextEncoder;
new (): TextEncoder;
};
}

// If you use the eval function indirectly, by invoking it via a reference
Expand Down Expand Up @@ -50,11 +64,9 @@ window["window"] = window; // Create a window object.
// window["clearTimeout"] = timer.clearTimer;
// window["clearInterval"] = timer.clearTimer;

window["console"] = new Console(libdeno.print);
window.console = new Console(libdeno.print);
window.TextEncoder = te.TextEncoder;
window.TextDecoder = te.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.

69 changes: 69 additions & 0 deletions js/text_encoding.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// In order to avoid importing all of lib.dom.d.ts we include type defintiions
// for text-encoding polyfill below. Ideally we could use @types/text-encoding
// but it depends on lib.dom.d.ts.
// https://github.com/DefinitelyTyped/DefinitelyTyped/commit/f9a3f3c2da18980f7c5b089eb6d298dc0fd3797b#r30007625

type BufferSource = ArrayBufferView | ArrayBuffer;

interface TextDecodeOptions {
stream?: boolean;
}

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

interface TextDecoder {
/**
* Returns encoding's name, lowercased.
*/
readonly encoding: string;
/**
* Returns true if error mode is "fatal", and false
* otherwise.
*/
readonly fatal: boolean;
/**
* Returns true if ignore BOM flag is set, and false otherwise.
*/
readonly ignoreBOM: boolean;
/**
* Returns the result of running encoding's decoder. The
* method can be invoked zero or more times with options's stream set to
* true, and then once without options's stream (or set to false), to process
* a fragmented stream. If the invocation without options's stream (or set to
* false) has no input, it's clearest to omit both arguments.
* var string = "", decoder = new TextDecoder(encoding), buffer;
* while(buffer = next_chunk()) {
* string += decoder.decode(buffer, {stream:true});
* }
* string += decoder.decode(); // end-of-stream
* If the error mode is "fatal" and encoding's decoder returns error, throws a TypeError.
*/
decode(input?: BufferSource, options?: TextDecodeOptions): string;
}

interface TextEncoder {
/**
* Returns "utf-8".
*/
readonly encoding: string;
/**
* Returns the result of running UTF-8's encoder.
*/
encode(input?: string): Uint8Array;
}

declare module "text-encoding" {
export let TextDecoder: {
prototype: TextDecoder;
new (label?: string, options?: TextDecoderOptions): TextDecoder;
};

export let TextEncoder: {
prototype: TextEncoder;
new (): TextEncoder;
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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
4 changes: 3 additions & 1 deletion tests/read_file_sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ if (!data.byteLength) {
`Expected positive value for data.byteLength ${data.byteLength}`
);
}
const pkg = JSON.parse(String.fromCharCode(...data));
const decoder = new TextDecoder("utf-8");
const json = decoder.decode(data);
const pkg = JSON.parse(json);
if (pkg['devDependencies'] == null) {
throw Error("Expected a positive number of devDependencies");
}
Expand Down

0 comments on commit da2e8a2

Please sign in to comment.