summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts2
-rw-r--r--js/os.ts38
-rw-r--r--js/os_test.ts28
-rw-r--r--js/read_file.ts50
-rw-r--r--js/read_file_test.ts34
-rw-r--r--js/unit_tests.ts3
6 files changed, 87 insertions, 68 deletions
diff --git a/js/deno.ts b/js/deno.ts
index ba4a5151e..ce01c5dad 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -7,12 +7,12 @@ export {
FileInfo,
makeTempDirSync,
mkdirSync,
- readFileSync,
renameSync,
statSync,
lstatSync,
writeFileSync
} from "./os";
+export { readFileSync, readFile } from "./read_file";
export { ErrorKind, DenoError } from "./errors";
export { libdeno } from "./libdeno";
export const argv: string[] = [];
diff --git a/js/os.ts b/js/os.ts
index 3adfb5c1e..d29d0d74a 100644
--- a/js/os.ts
+++ b/js/os.ts
@@ -126,37 +126,6 @@ export function mkdirSync(path: string, mode = 0o777): void {
sendSync(builder, fbs.Any.MkdirSync, msg);
}
-/**
- * Read the file.
- * import { readFileSync } from "deno";
- *
- * const decoder = new TextDecoder("utf-8");
- * const data = readFileSync("hello.txt");
- * console.log(decoder.decode(data));
- */
-export function readFileSync(filename: string): Uint8Array {
- /* Ideally we could write
- const res = sendSync({
- command: fbs.Command.READ_FILE_SYNC,
- readFileSyncFilename: filename
- });
- return res.readFileSyncData;
- */
- const builder = new flatbuffers.Builder();
- const filename_ = builder.createString(filename);
- fbs.ReadFileSync.startReadFileSync(builder);
- fbs.ReadFileSync.addFilename(builder, filename_);
- const msg = fbs.ReadFileSync.endReadFileSync(builder);
- const baseRes = sendSync(builder, fbs.Any.ReadFileSync, msg);
- assert(baseRes != null);
- assert(fbs.Any.ReadFileSyncRes === baseRes!.msgType());
- const res = new fbs.ReadFileSyncRes();
- assert(baseRes!.msg(res) != null);
- const dataArray = res.dataArray();
- assert(dataArray != null);
- return new Uint8Array(dataArray!);
-}
-
function createEnv(_msg: fbs.EnvironRes): { [index: string]: string } {
const env: { [index: string]: string } = {};
@@ -365,13 +334,6 @@ export function writeFileSync(
* renameSync(oldpath, newpath);
*/
export function renameSync(oldpath: string, newpath: string): void {
- /* Ideally we could write:
- const res = sendSync({
- command: fbs.Command.RENAME_SYNC,
- renameOldPath: oldpath,
- renameNewPath: newpath
- });
- */
const builder = new flatbuffers.Builder();
const _oldpath = builder.createString(oldpath);
const _newpath = builder.createString(newpath);
diff --git a/js/os_test.ts b/js/os_test.ts
index 7a252cb73..544d7d49f 100644
--- a/js/os_test.ts
+++ b/js/os_test.ts
@@ -85,34 +85,6 @@ test(async function lstatSyncNotFound() {
assertEqual(badInfo, undefined);
});
-test(async function readFileSyncSuccess() {
- const data = deno.readFileSync("package.json");
- if (!data.byteLength) {
- throw Error(
- `Expected positive value for data.byteLength ${data.byteLength}`
- );
- }
- const decoder = new TextDecoder("utf-8");
- const json = decoder.decode(data);
- const pkg = JSON.parse(json);
- assertEqual(pkg.name, "deno");
-});
-
-/* TODO We should be able to catch specific types.
-test(function tests_readFileSync_NotFound() {
- let caughtError = false;
- let data;
- try {
- data = deno.readFileSync("bad_filename");
- } catch (e) {
- caughtError = true;
- assert(e instanceof deno.NotFound);
- }
- assert(caughtError);
- assert(data === undefined);
-});
-*/
-
testPerm({ write: true }, function writeFileSyncSuccess() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
diff --git a/js/read_file.ts b/js/read_file.ts
new file mode 100644
index 000000000..2afea42f1
--- /dev/null
+++ b/js/read_file.ts
@@ -0,0 +1,50 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as fbs from "gen/msg_generated";
+import { flatbuffers } from "flatbuffers";
+import { assert } from "./util";
+import * as dispatch from "./dispatch";
+
+/**
+ * Read the entire contents of a file synchronously.
+ *
+ * import { readFileSync } from "deno";
+ * const decoder = new TextDecoder("utf-8");
+ * const data = readFileSync("hello.txt");
+ * console.log(decoder.decode(data));
+ */
+export function readFileSync(filename: string): Uint8Array {
+ return res(dispatch.sendSync(...req(filename)));
+}
+
+/**
+ * Read the entire contents of a file.
+ *
+ * import { readFile } from "deno";
+ * const decoder = new TextDecoder("utf-8");
+ * const data = await readFile("hello.txt");
+ * console.log(decoder.decode(data));
+ */
+export async function readFile(filename: string): Promise<Uint8Array> {
+ return res(await dispatch.sendAsync(...req(filename)));
+}
+
+function req(
+ filename: string
+): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
+ const builder = new flatbuffers.Builder();
+ const filename_ = builder.createString(filename);
+ fbs.ReadFile.startReadFile(builder);
+ fbs.ReadFile.addFilename(builder, filename_);
+ const msg = fbs.ReadFile.endReadFile(builder);
+ return [builder, fbs.Any.ReadFile, msg];
+}
+
+function res(baseRes: null | fbs.Base): Uint8Array {
+ assert(baseRes != null);
+ assert(fbs.Any.ReadFileRes === baseRes!.msgType());
+ const msg = new fbs.ReadFileRes();
+ assert(baseRes!.msg(msg) != null);
+ const dataArray = msg.dataArray();
+ assert(dataArray != null);
+ return new Uint8Array(dataArray!);
+}
diff --git a/js/read_file_test.ts b/js/read_file_test.ts
new file mode 100644
index 000000000..6d4f71b62
--- /dev/null
+++ b/js/read_file_test.ts
@@ -0,0 +1,34 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import { test, assert, assertEqual } from "./test_util.ts";
+import * as deno from "deno";
+
+test(function readFileSyncSuccess() {
+ const data = deno.readFileSync("package.json");
+ assert(data.byteLength > 0);
+ const decoder = new TextDecoder("utf-8");
+ const json = decoder.decode(data);
+ const pkg = JSON.parse(json);
+ assertEqual(pkg.name, "deno");
+});
+
+test(function readFileSyncNotFound() {
+ let caughtError = false;
+ let data;
+ try {
+ data = deno.readFileSync("bad_filename");
+ } catch (e) {
+ caughtError = true;
+ assertEqual(e.kind, deno.ErrorKind.NotFound);
+ }
+ assert(caughtError);
+ assert(data === undefined);
+});
+
+test(async function readFileSuccess() {
+ const data = await deno.readFile("package.json");
+ assert(data.byteLength > 0);
+ const decoder = new TextDecoder("utf-8");
+ const json = decoder.decode(data);
+ const pkg = JSON.parse(json);
+ assertEqual(pkg.name, "deno");
+});
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index 0e877dea6..efbc7383c 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -3,5 +3,6 @@
// But it can also be run manually: ./out/debug/deno js/unit_tests.ts
import "./compiler_test.ts";
import "./console_test.ts";
-import "./os_test.ts";
import "./fetch_test.ts";
+import "./os_test.ts";
+import "./read_file_test.ts";