1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

Add readFileSync

This commit is contained in:
Ryan Dahl 2018-05-27 12:49:20 -04:00
parent a831d1e239
commit 4e55928063
8 changed files with 61 additions and 1 deletions

View file

@ -1,4 +1,5 @@
TS_FILES = \ TS_FILES = \
deno.d.ts \
dispatch.ts \ dispatch.ts \
fetch.ts \ fetch.ts \
globals.ts \ globals.ts \

2
deno.d.ts vendored
View file

@ -2,4 +2,6 @@ declare module "deno" {
type MessageCallback = (msg: Uint8Array) => void; type MessageCallback = (msg: Uint8Array) => void;
function sub(channel: string, cb: MessageCallback): void; function sub(channel: string, cb: MessageCallback): void;
function pub(channel: string, payload: Uint8Array): null | ArrayBuffer; function pub(channel: string, payload: Uint8Array): null | ArrayBuffer;
function readFileSync(filename: string): Uint8Array;
} }

View file

@ -19,6 +19,8 @@ message Msg {
TIMER_CLEAR = 8; TIMER_CLEAR = 8;
FETCH_REQ = 9; FETCH_REQ = 9;
FETCH_RES = 10; FETCH_RES = 10;
READ_FILE_SYNC = 11;
READ_FILE_SYNC_RES = 12;
} }
Command command = 1; Command command = 1;
@ -81,4 +83,10 @@ message Msg {
int32 fetch_res_status = 101; int32 fetch_res_status = 101;
repeated string fetch_res_header_line = 102; repeated string fetch_res_header_line = 102;
bytes fetch_res_body = 103; bytes fetch_res_body = 103;
// READ_FILE_SYNC
string read_file_sync_filename = 110;
// READ_FILE_SYNC_RES
bytes read_file_sync_data = 120;
} }

22
os.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/spf13/afero"
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
@ -11,7 +12,11 @@ import (
const assetPrefix string = "/$asset$/" const assetPrefix string = "/$asset$/"
var fs afero.Fs
func InitOS() { func InitOS() {
fs = afero.NewOsFs()
Sub("os", func(buf []byte) []byte { Sub("os", func(buf []byte) []byte {
msg := &Msg{} msg := &Msg{}
check(proto.Unmarshal(buf, msg)) check(proto.Unmarshal(buf, msg))
@ -27,6 +32,8 @@ func InitOS() {
msg.CodeCacheOutputCode) msg.CodeCacheOutputCode)
case Msg_EXIT: case Msg_EXIT:
os.Exit(int(msg.ExitCode)) os.Exit(int(msg.ExitCode))
case Msg_READ_FILE_SYNC:
return ReadFileSync(msg.ReadFileSyncFilename)
default: default:
panic("[os] Unexpected message " + string(buf)) panic("[os] Unexpected message " + string(buf))
} }
@ -128,3 +135,18 @@ func HandleCodeCache(filename string, sourceCode string,
check(err) check(err)
return out 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
}

8
os.ts
View file

@ -40,3 +40,11 @@ export function codeCache(
codeCacheOutputCode: outputCode codeCacheOutputCode: outputCode
}); });
} }
export function readFileSync(filename: string): Uint8Array {
const res = sendMsg("os", {
command: pb.Msg.Command.READ_FILE_SYNC,
readFileSyncFilename: filename
});
return res.readFileSyncData;
}

View file

@ -17,7 +17,11 @@ import { _global, globalEval } from "./globals";
const EOL = "\n"; const EOL = "\n";
// Public deno module. // Public deno module.
const deno = { pub, sub }; const deno = {
pub,
sub,
readFileSync: os.readFileSync
};
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
type AmdFactory = (...args: any[]) => undefined | object; type AmdFactory = (...args: any[]) => undefined | object;

14
testdata/read_file_sync.ts vendored Normal file
View file

@ -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);

1
testdata/read_file_sync.ts.out vendored Normal file
View file

@ -0,0 +1 @@
package.name deno