summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--deno.d.ts2
-rw-r--r--msg.proto8
-rw-r--r--os.go22
-rw-r--r--os.ts8
-rw-r--r--runtime.ts6
-rw-r--r--testdata/read_file_sync.ts14
-rw-r--r--testdata/read_file_sync.ts.out1
8 files changed, 61 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index e1df60d43..0e9e3a5f7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
TS_FILES = \
+ deno.d.ts \
dispatch.ts \
fetch.ts \
globals.ts \
diff --git a/deno.d.ts b/deno.d.ts
index cf6dcbcec..5113cf412 100644
--- a/deno.d.ts
+++ b/deno.d.ts
@@ -2,4 +2,6 @@ declare module "deno" {
type MessageCallback = (msg: Uint8Array) => void;
function sub(channel: string, cb: MessageCallback): void;
function pub(channel: string, payload: Uint8Array): null | ArrayBuffer;
+
+ function readFileSync(filename: string): Uint8Array;
}
diff --git a/msg.proto b/msg.proto
index 1c18c8018..ecd636618 100644
--- a/msg.proto
+++ b/msg.proto
@@ -19,6 +19,8 @@ message Msg {
TIMER_CLEAR = 8;
FETCH_REQ = 9;
FETCH_RES = 10;
+ READ_FILE_SYNC = 11;
+ READ_FILE_SYNC_RES = 12;
}
Command command = 1;
@@ -81,4 +83,10 @@ message Msg {
int32 fetch_res_status = 101;
repeated string fetch_res_header_line = 102;
bytes fetch_res_body = 103;
+
+ // READ_FILE_SYNC
+ string read_file_sync_filename = 110;
+
+ // READ_FILE_SYNC_RES
+ bytes read_file_sync_data = 120;
}
diff --git a/os.go b/os.go
index 4f4f26d71..479cefb83 100644
--- a/os.go
+++ b/os.go
@@ -2,6 +2,7 @@ package main
import (
"github.com/golang/protobuf/proto"
+ "github.com/spf13/afero"
"io/ioutil"
"net/url"
"os"
@@ -11,7 +12,11 @@ import (
const assetPrefix string = "/$asset$/"
+var fs afero.Fs
+
func InitOS() {
+ fs = afero.NewOsFs()
+
Sub("os", func(buf []byte) []byte {
msg := &Msg{}
check(proto.Unmarshal(buf, msg))
@@ -27,6 +32,8 @@ func InitOS() {
msg.CodeCacheOutputCode)
case Msg_EXIT:
os.Exit(int(msg.ExitCode))
+ case Msg_READ_FILE_SYNC:
+ return ReadFileSync(msg.ReadFileSyncFilename)
default:
panic("[os] Unexpected message " + string(buf))
}
@@ -128,3 +135,18 @@ func HandleCodeCache(filename string, sourceCode string,
check(err)
return out
}
+
+func ReadFileSync(filename string) []byte {
+ data, err := afero.ReadFile(fs, filename)
+ res := &Msg{
+ Command: Msg_READ_FILE_SYNC_RES,
+ }
+ if err != nil {
+ res.Error = err.Error()
+ } else {
+ res.ReadFileSyncData = data
+ }
+ out, err := proto.Marshal(res)
+ check(err)
+ return out
+}
diff --git a/os.ts b/os.ts
index 32988e224..8628edd2a 100644
--- a/os.ts
+++ b/os.ts
@@ -40,3 +40,11 @@ export function codeCache(
codeCacheOutputCode: outputCode
});
}
+
+export function readFileSync(filename: string): Uint8Array {
+ const res = sendMsg("os", {
+ command: pb.Msg.Command.READ_FILE_SYNC,
+ readFileSyncFilename: filename
+ });
+ return res.readFileSyncData;
+}
diff --git a/runtime.ts b/runtime.ts
index c34f7faac..ca2e40ba0 100644
--- a/runtime.ts
+++ b/runtime.ts
@@ -17,7 +17,11 @@ import { _global, globalEval } from "./globals";
const EOL = "\n";
// Public deno module.
-const deno = { pub, sub };
+const deno = {
+ pub,
+ sub,
+ readFileSync: os.readFileSync
+};
// tslint:disable-next-line:no-any
type AmdFactory = (...args: any[]) => undefined | object;
diff --git a/testdata/read_file_sync.ts b/testdata/read_file_sync.ts
new file mode 100644
index 000000000..cb0904d35
--- /dev/null
+++ b/testdata/read_file_sync.ts
@@ -0,0 +1,14 @@
+import { readFileSync } from "deno";
+
+let data = 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);
+if (pkg.name !== "deno") {
+ throw Error(`Expected "deno" but got "${pkg.name}"`)
+}
+console.log("package.name ", pkg.name);
diff --git a/testdata/read_file_sync.ts.out b/testdata/read_file_sync.ts.out
new file mode 100644
index 000000000..53d818f44
--- /dev/null
+++ b/testdata/read_file_sync.ts.out
@@ -0,0 +1 @@
+package.name deno