summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--deno.d.ts5
-rw-r--r--msg.proto7
-rw-r--r--os.go14
-rw-r--r--os.ts13
-rw-r--r--runtime.ts3
-rw-r--r--tests.ts16
6 files changed, 55 insertions, 3 deletions
diff --git a/deno.d.ts b/deno.d.ts
index 5113cf412..59b1f2dfd 100644
--- a/deno.d.ts
+++ b/deno.d.ts
@@ -4,4 +4,9 @@ declare module "deno" {
function pub(channel: string, payload: Uint8Array): null | ArrayBuffer;
function readFileSync(filename: string): Uint8Array;
+ function writeFileSync(
+ filename: string,
+ data: Uint8Array,
+ perm: number
+ ): void;
}
diff --git a/msg.proto b/msg.proto
index ecd636618..1a2abcbbf 100644
--- a/msg.proto
+++ b/msg.proto
@@ -21,6 +21,7 @@ message Msg {
FETCH_RES = 10;
READ_FILE_SYNC = 11;
READ_FILE_SYNC_RES = 12;
+ WRITE_FILE_SYNC = 13;
}
Command command = 1;
@@ -89,4 +90,10 @@ message Msg {
// READ_FILE_SYNC_RES
bytes read_file_sync_data = 120;
+
+ // WRITE_FILE_SYNC
+ string write_file_sync_filename = 130;
+ bytes write_file_sync_data = 131;
+ uint32 write_file_sync_perm = 132;
+ // write_file_sync_perm specified by https://godoc.org/os#FileMode
}
diff --git a/os.go b/os.go
index 2fbbd95a5..52c70eddc 100644
--- a/os.go
+++ b/os.go
@@ -34,6 +34,9 @@ func InitOS() {
os.Exit(int(msg.ExitCode))
case Msg_READ_FILE_SYNC:
return ReadFileSync(msg.ReadFileSyncFilename)
+ case Msg_WRITE_FILE_SYNC:
+ return WriteFileSync(msg.WriteFileSyncFilename, msg.WriteFileSyncData,
+ msg.WriteFileSyncPerm)
default:
panic("[os] Unexpected message " + string(buf))
}
@@ -176,3 +179,14 @@ func ReadFileSync(filename string) []byte {
check(err)
return out
}
+
+func WriteFileSync(filename string, data []byte, perm uint32) []byte {
+ err := afero.WriteFile(fs, filename, data, os.FileMode(perm))
+ res := &Msg{}
+ if err != nil {
+ res.Error = err.Error()
+ }
+ out, err := proto.Marshal(res)
+ check(err)
+ return out
+}
diff --git a/os.ts b/os.ts
index 8628edd2a..18ccb5bb5 100644
--- a/os.ts
+++ b/os.ts
@@ -48,3 +48,16 @@ export function readFileSync(filename: string): Uint8Array {
});
return res.readFileSyncData;
}
+
+export function writeFileSync(
+ filename: string,
+ data: Uint8Array,
+ perm: number
+): void {
+ sendMsg("os", {
+ command: pb.Msg.Command.WRITE_FILE_SYNC,
+ writeFileSyncFilename: filename,
+ writeFileSyncData: data,
+ writeFileSyncPerm: perm
+ });
+}
diff --git a/runtime.ts b/runtime.ts
index 7d91484ab..e46c73c61 100644
--- a/runtime.ts
+++ b/runtime.ts
@@ -20,7 +20,8 @@ const EOL = "\n";
const deno = {
pub,
sub,
- readFileSync: os.readFileSync
+ readFileSync: os.readFileSync,
+ writeFileSync: os.writeFileSync
};
// tslint:disable-next-line:no-any
diff --git a/tests.ts b/tests.ts
index 702f6406d..b2c7ee513 100644
--- a/tests.ts
+++ b/tests.ts
@@ -2,7 +2,7 @@
// But it can also be run manually:
// ./deno tests.ts
import { test, assert, assertEqual } from "./deno_testing/testing.ts";
-import { readFileSync } from "deno";
+import { readFileSync, writeFileSync } from "deno";
test(async function tests_test() {
assert(true);
@@ -21,8 +21,20 @@ test(async function tests_readFileSync() {
assertEqual(pkg.name, "deno");
});
+test(async function tests_writeFileSync() {
+ const enc = new TextEncoder();
+ const data = enc.encode("Hello");
+ // TODO need ability to get tmp dir.
+ const fn = "/tmp/test.txt";
+ writeFileSync("/tmp/test.txt", data, 0o666);
+ const dataRead = readFileSync("/tmp/test.txt");
+ const dec = new TextDecoder("utf-8");
+ const actual = dec.decode(dataRead);
+ assertEqual("Hello", actual);
+});
+
test(async function tests_fetch() {
- const response = await fetch('http://localhost:4545/package.json');
+ const response = await fetch("http://localhost:4545/package.json");
const json = await response.json();
assertEqual(json.name, "deno");
});