summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-08-22 13:19:32 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-08-22 18:39:07 -0400
commit7d7263c48f4280f8da5496273e85fb1a8fb79547 (patch)
tree709df115d6d2daf5a753a31a69bac637d65a33a7 /js
parente7cab715749e4c5000c24ffeade1ef915d15a68d (diff)
Implement writeFileSync
In collaboration with Tommy Savaria <tommy.savaria@protonmail.ch>
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts2
-rw-r--r--js/os.ts25
-rw-r--r--js/unit_tests.ts30
3 files changed, 52 insertions, 5 deletions
diff --git a/js/deno.ts b/js/deno.ts
index 060b2d526..ff9939b1b 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -1,4 +1,4 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// Public deno module.
-export { exit, readFileSync } from "./os";
+export { exit, readFileSync, writeFileSync } from "./os";
export { libdeno } from "./globals";
diff --git a/js/os.ts b/js/os.ts
index 22ef6ef4f..22bbd7748 100644
--- a/js/os.ts
+++ b/js/os.ts
@@ -121,15 +121,32 @@ export function readFileSync(filename: string): Uint8Array {
export function writeFileSync(
filename: string,
data: Uint8Array,
- perm: number
+ perm = 0o666
): void {
- util.notImplemented();
- /*
- pubInternal("os", {
+ /* Ideally we could write:
+ const res = send({
command: fbs.Command.WRITE_FILE_SYNC,
writeFileSyncFilename: filename,
writeFileSyncData: data,
writeFileSyncPerm: perm
});
*/
+ const builder = new flatbuffers.Builder();
+ const filename_ = builder.createString(filename);
+ const dataOffset = fbs.WriteFileSync.createDataVector(builder, data);
+ fbs.WriteFileSync.startWriteFileSync(builder);
+ fbs.WriteFileSync.addFilename(builder, filename_);
+ fbs.WriteFileSync.addData(builder, dataOffset);
+ fbs.WriteFileSync.addPerm(builder, perm);
+ const msg = fbs.WriteFileSync.endWriteFileSync(builder);
+ fbs.Base.startBase(builder);
+ fbs.Base.addMsg(builder, msg);
+ fbs.Base.addMsgType(builder, fbs.Any.WriteFileSync);
+ builder.finish(fbs.Base.endBase(builder));
+ const resBuf = libdeno.send(builder.asUint8Array());
+ if (resBuf != null) {
+ const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!));
+ const baseRes = fbs.Base.getRootAsBase(bb);
+ maybeThrowError(baseRes);
+ }
}
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index f203444fa..6eebf8860 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -5,6 +5,7 @@
import { test, assert, assertEqual } from "./testing/testing.ts";
import { readFileSync } from "deno";
+import * as deno from "deno";
import "./compiler_test.ts";
@@ -109,6 +110,35 @@ test(function tests_readFileSync_NotFound() {
});
*/
+/* TODO(ry) Add this once we can create a tmpDir to write the file into.
+test(function writeFileSyncSuccess() {
+ const enc = new TextEncoder();
+ const dataWritten = enc.encode("Hello");
+ const filename = "TEMPDIR/test.txt";
+ deno.writeFileSync(filename, dataWritten, 0o666);
+ const dataRead = readFileSync(filename);
+ assertEqual(dataRead, dataWritten);
+});
+*/
+
+// For this test to pass we need --allow-write permission.
+// Otherwise it will fail with deno.PermissionDenied instead of deno.NotFound.
+test(function writeFileSyncFail() {
+ const enc = new TextEncoder();
+ const data = enc.encode("Hello");
+ const filename = "/baddir/test.txt";
+ // The following should fail because /baddir doesn't exist (hopefully).
+ let caughtError = false;
+ try {
+ deno.writeFileSync(filename, data);
+ } catch (e) {
+ caughtError = true;
+ // TODO assertEqual(e, deno.NotFound);
+ assertEqual(e.name, "deno.NotFound");
+ }
+ assert(caughtError);
+});
+
test(async function tests_fetch() {
const response = await fetch("http://localhost:4545/package.json");
const json = await response.json();