2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-07-06 11:20:35 -04:00
|
|
|
import { ModuleInfo } from "./types";
|
2018-07-25 23:07:50 -04:00
|
|
|
import { deno as fbs } from "gen/msg_generated";
|
2018-07-13 03:24:07 -04:00
|
|
|
import { assert } from "./util";
|
|
|
|
import * as util from "./util";
|
2018-07-06 11:27:36 -04:00
|
|
|
import { flatbuffers } from "flatbuffers";
|
2018-08-30 15:35:51 -04:00
|
|
|
import { send } from "./fbs_util";
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2018-08-01 15:56:27 -04:00
|
|
|
export function exit(exitCode = 0): never {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.Exit.startExit(builder);
|
|
|
|
fbs.Exit.addCode(builder, exitCode);
|
|
|
|
const msg = fbs.Exit.endExit(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
send(builder, fbs.Any.Exit, msg);
|
2018-08-19 15:04:27 -04:00
|
|
|
return util.unreachable();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function codeFetch(
|
|
|
|
moduleSpecifier: string,
|
|
|
|
containingFile: string
|
|
|
|
): ModuleInfo {
|
2018-07-13 03:24:07 -04:00
|
|
|
util.log("os.ts codeFetch", moduleSpecifier, containingFile);
|
2018-07-06 11:27:36 -04:00
|
|
|
// Send CodeFetch message
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const moduleSpecifier_ = builder.createString(moduleSpecifier);
|
|
|
|
const containingFile_ = builder.createString(containingFile);
|
|
|
|
fbs.CodeFetch.startCodeFetch(builder);
|
|
|
|
fbs.CodeFetch.addModuleSpecifier(builder, moduleSpecifier_);
|
|
|
|
fbs.CodeFetch.addContainingFile(builder, containingFile_);
|
|
|
|
const msg = fbs.CodeFetch.endCodeFetch(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
const baseRes = send(builder, fbs.Any.CodeFetch, msg);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(fbs.Any.CodeFetchRes === baseRes!.msgType());
|
2018-07-06 11:27:36 -04:00
|
|
|
const codeFetchRes = new fbs.CodeFetchRes();
|
2018-08-30 15:35:51 -04:00
|
|
|
assert(baseRes!.msg(codeFetchRes) != null);
|
2018-09-01 18:49:47 -04:00
|
|
|
return {
|
2018-07-06 11:27:36 -04:00
|
|
|
moduleName: codeFetchRes.moduleName(),
|
|
|
|
filename: codeFetchRes.filename(),
|
|
|
|
sourceCode: codeFetchRes.sourceCode(),
|
2018-07-09 01:09:56 -04:00
|
|
|
outputCode: codeFetchRes.outputCode()
|
2018-07-06 11:20:35 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function codeCache(
|
|
|
|
filename: string,
|
|
|
|
sourceCode: string,
|
|
|
|
outputCode: string
|
|
|
|
): void {
|
2018-07-13 03:24:07 -04:00
|
|
|
util.log("os.ts codeCache", filename, sourceCode, outputCode);
|
2018-07-06 11:27:36 -04:00
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const filename_ = builder.createString(filename);
|
|
|
|
const sourceCode_ = builder.createString(sourceCode);
|
|
|
|
const outputCode_ = builder.createString(outputCode);
|
|
|
|
fbs.CodeCache.startCodeCache(builder);
|
|
|
|
fbs.CodeCache.addFilename(builder, filename_);
|
|
|
|
fbs.CodeCache.addSourceCode(builder, sourceCode_);
|
|
|
|
fbs.CodeCache.addOutputCode(builder, outputCode_);
|
|
|
|
const msg = fbs.CodeCache.endCodeCache(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
const baseRes = send(builder, fbs.Any.CodeCache, msg);
|
|
|
|
assert(baseRes == null); // Expect null or error.
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:36:45 -04:00
|
|
|
/**
|
|
|
|
* makeTempDirSync creates a new temporary directory in the directory `dir`, its
|
|
|
|
* name beginning with `prefix` and ending with `suffix`.
|
|
|
|
* It returns the full path to the newly created directory.
|
|
|
|
* If `dir` is unspecified, tempDir uses the default directory for temporary
|
|
|
|
* files. Multiple programs calling tempDir simultaneously will not choose the
|
|
|
|
* same directory. It is the caller's responsibility to remove the directory
|
|
|
|
* when no longer needed.
|
|
|
|
*/
|
|
|
|
export interface MakeTempDirOptions {
|
|
|
|
dir?: string;
|
|
|
|
prefix?: string;
|
|
|
|
suffix?: string;
|
|
|
|
}
|
|
|
|
export function makeTempDirSync({
|
|
|
|
dir,
|
|
|
|
prefix,
|
|
|
|
suffix
|
|
|
|
}: MakeTempDirOptions = {}): string {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const fbDir = dir == null ? -1 : builder.createString(dir);
|
|
|
|
const fbPrefix = prefix == null ? -1 : builder.createString(prefix);
|
|
|
|
const fbSuffix = suffix == null ? -1 : builder.createString(suffix);
|
|
|
|
fbs.MakeTempDir.startMakeTempDir(builder);
|
|
|
|
if (dir != null) {
|
|
|
|
fbs.MakeTempDir.addDir(builder, fbDir);
|
|
|
|
}
|
|
|
|
if (prefix != null) {
|
|
|
|
fbs.MakeTempDir.addPrefix(builder, fbPrefix);
|
|
|
|
}
|
|
|
|
if (suffix != null) {
|
|
|
|
fbs.MakeTempDir.addSuffix(builder, fbSuffix);
|
|
|
|
}
|
|
|
|
const msg = fbs.MakeTempDir.endMakeTempDir(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
const baseRes = send(builder, fbs.Any.MakeTempDir, msg);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(fbs.Any.MakeTempDirRes === baseRes!.msgType());
|
2018-08-23 18:36:45 -04:00
|
|
|
const res = new fbs.MakeTempDirRes();
|
2018-08-30 15:35:51 -04:00
|
|
|
assert(baseRes!.msg(res) != null);
|
2018-08-23 18:36:45 -04:00
|
|
|
const path = res.path();
|
|
|
|
assert(path != null);
|
|
|
|
return path!;
|
|
|
|
}
|
|
|
|
|
2018-07-06 11:20:35 -04:00
|
|
|
export function readFileSync(filename: string): Uint8Array {
|
2018-08-09 13:24:30 -04:00
|
|
|
/* Ideally we could write
|
|
|
|
const res = send({
|
2018-07-06 11:27:36 -04:00
|
|
|
command: fbs.Command.READ_FILE_SYNC,
|
2018-07-06 11:20:35 -04:00
|
|
|
readFileSyncFilename: filename
|
|
|
|
});
|
|
|
|
return res.readFileSyncData;
|
2018-08-09 13:24:30 -04:00
|
|
|
*/
|
|
|
|
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);
|
2018-08-30 15:35:51 -04:00
|
|
|
const baseRes = send(builder, fbs.Any.ReadFileSync, msg);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(fbs.Any.ReadFileSyncRes === baseRes!.msgType());
|
2018-08-09 13:24:30 -04:00
|
|
|
const res = new fbs.ReadFileSyncRes();
|
2018-08-30 15:35:51 -04:00
|
|
|
assert(baseRes!.msg(res) != null);
|
2018-08-15 12:40:30 -04:00
|
|
|
const dataArray = res.dataArray();
|
|
|
|
assert(dataArray != null);
|
|
|
|
return new Uint8Array(dataArray!);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-09-01 10:45:26 -04:00
|
|
|
function createEnv(_msg: fbs.EnvironRes): { [index: string]: string } {
|
|
|
|
const env: { [index: string]: string } = {};
|
2018-08-31 07:51:12 -04:00
|
|
|
|
|
|
|
for (let i = 0; i < _msg.mapLength(); i++) {
|
|
|
|
const item = _msg.map(i)!;
|
|
|
|
|
|
|
|
env[item.key()!] = item.value()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Proxy(env, {
|
|
|
|
set(obj, prop: string, value: string | number) {
|
|
|
|
setEnv(prop, value.toString());
|
|
|
|
return Reflect.set(obj, prop, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEnv(key: string, value: string): void {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const _key = builder.createString(key);
|
|
|
|
const _value = builder.createString(value);
|
|
|
|
fbs.SetEnv.startSetEnv(builder);
|
|
|
|
fbs.SetEnv.addKey(builder, _key);
|
|
|
|
fbs.SetEnv.addValue(builder, _value);
|
|
|
|
const msg = fbs.SetEnv.endSetEnv(builder);
|
|
|
|
send(builder, fbs.Any.SetEnv, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a snapshot of the environment variables at invocation. Mutating a
|
|
|
|
* property in the object will set that variable in the environment for
|
|
|
|
* the process. The environment object will only accept `string`s or `number`s
|
|
|
|
* as values.
|
|
|
|
* import { env } from "deno";
|
|
|
|
* const env = deno.env();
|
|
|
|
* console.log(env.SHELL)
|
|
|
|
* env.TEST_VAR = "HELLO";
|
|
|
|
*
|
|
|
|
* const newEnv = deno.env();
|
|
|
|
* console.log(env.TEST_VAR == newEnv.TEST_VAR);
|
|
|
|
*/
|
2018-09-01 10:45:26 -04:00
|
|
|
export function env(): { [index: string]: string } {
|
2018-08-31 07:51:12 -04:00
|
|
|
/* Ideally we could write
|
|
|
|
const res = send({
|
|
|
|
command: fbs.Command.ENV,
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.Environ.startEnviron(builder);
|
|
|
|
const msg = fbs.Environ.endEnviron(builder);
|
|
|
|
const baseRes = send(builder, fbs.Any.Environ, msg)!;
|
|
|
|
assert(fbs.Any.EnvironRes === baseRes.msgType());
|
|
|
|
const res = new fbs.EnvironRes();
|
|
|
|
assert(baseRes.msg(res) != null);
|
|
|
|
// TypeScript cannot track assertion above, therefore not null assertion
|
|
|
|
return createEnv(res);
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:15:22 -04:00
|
|
|
/**
|
|
|
|
* A FileInfo describes a file and is returned by `stat`, `lstat`,
|
|
|
|
* `statSync`, `lstatSync`.
|
|
|
|
*/
|
2018-08-29 09:22:25 -04:00
|
|
|
export class FileInfo {
|
2018-09-01 18:49:47 -04:00
|
|
|
private readonly _isFile: boolean;
|
|
|
|
private readonly _isSymlink: boolean;
|
2018-08-31 15:15:22 -04:00
|
|
|
/** The size of the file, in bytes. */
|
2018-08-29 09:22:25 -04:00
|
|
|
len: number;
|
2018-08-31 15:15:22 -04:00
|
|
|
/**
|
|
|
|
* The last modification time of the file. This corresponds to the `mtime`
|
|
|
|
* field from `stat` on Unix and `ftLastWriteTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
modified: number | null;
|
|
|
|
/**
|
|
|
|
* The last access time of the file. This corresponds to the `atime`
|
|
|
|
* field from `stat` on Unix and `ftLastAccessTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
accessed: number | null;
|
|
|
|
/**
|
|
|
|
* The last access time of the file. This corresponds to the `birthtime`
|
|
|
|
* field from `stat` on Unix and `ftCreationTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
2018-08-29 09:22:25 -04:00
|
|
|
created: number | null;
|
|
|
|
|
|
|
|
/* @internal */
|
|
|
|
constructor(private _msg: fbs.StatSyncRes) {
|
2018-08-31 15:15:22 -04:00
|
|
|
const modified = this._msg.modified().toFloat64();
|
|
|
|
const accessed = this._msg.accessed().toFloat64();
|
2018-08-29 09:22:25 -04:00
|
|
|
const created = this._msg.created().toFloat64();
|
|
|
|
|
|
|
|
this._isFile = this._msg.isFile();
|
|
|
|
this._isSymlink = this._msg.isSymlink();
|
|
|
|
this.len = this._msg.len().toFloat64();
|
2018-08-31 15:15:22 -04:00
|
|
|
this.modified = modified ? modified : null;
|
|
|
|
this.accessed = accessed ? accessed : null;
|
2018-08-30 13:54:56 -04:00
|
|
|
this.created = created ? created : null;
|
2018-08-29 09:22:25 -04:00
|
|
|
}
|
|
|
|
|
2018-08-31 15:15:22 -04:00
|
|
|
/**
|
|
|
|
* Returns whether this is info for a regular file. This result is mutually
|
|
|
|
* exclusive to `FileInfo.isDirectory` and `FileInfo.isSymlink`.
|
|
|
|
*/
|
2018-08-29 09:22:25 -04:00
|
|
|
isFile() {
|
|
|
|
return this._isFile;
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:15:22 -04:00
|
|
|
/**
|
|
|
|
* Returns whether this is info for a regular directory. This result is
|
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isSymlink`.
|
|
|
|
*/
|
2018-08-29 09:22:25 -04:00
|
|
|
isDirectory() {
|
|
|
|
return !this._isFile && !this._isSymlink;
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:15:22 -04:00
|
|
|
/**
|
|
|
|
* Returns whether this is info for a symlink. This result is
|
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isDirectory`.
|
|
|
|
*/
|
2018-08-29 09:22:25 -04:00
|
|
|
isSymlink() {
|
|
|
|
return this._isSymlink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:15:22 -04:00
|
|
|
/**
|
|
|
|
* Queries the file system for information on the path provided.
|
|
|
|
* If the given path is a symlink information about the symlink will
|
|
|
|
* be returned.
|
|
|
|
* @returns FileInfo
|
|
|
|
*/
|
2018-08-29 09:22:25 -04:00
|
|
|
export function lStatSync(filename: string): FileInfo {
|
|
|
|
return statSyncInner(filename, true);
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:15:22 -04:00
|
|
|
/**
|
|
|
|
* Queries the file system for information on the path provided.
|
|
|
|
* `statSync` Will always follow symlinks.
|
|
|
|
* @returns FileInfo
|
|
|
|
*/
|
2018-08-29 09:22:25 -04:00
|
|
|
export function statSync(filename: string): FileInfo {
|
|
|
|
return statSyncInner(filename, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function statSyncInner(filename: string, lstat: boolean): FileInfo {
|
|
|
|
/* Ideally we could write
|
|
|
|
const res = send({
|
|
|
|
command: fbs.Command.STAT_FILE_SYNC,
|
|
|
|
StatFilename: filename,
|
|
|
|
StatLStat: lstat,
|
|
|
|
});
|
|
|
|
return new FileInfo(res);
|
|
|
|
*/
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const filename_ = builder.createString(filename);
|
|
|
|
fbs.StatSync.startStatSync(builder);
|
|
|
|
fbs.StatSync.addFilename(builder, filename_);
|
|
|
|
fbs.StatSync.addLstat(builder, lstat);
|
|
|
|
const msg = fbs.StatSync.endStatSync(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
const baseRes = send(builder, fbs.Any.StatSync, msg);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(fbs.Any.StatSyncRes === baseRes!.msgType());
|
2018-08-29 09:22:25 -04:00
|
|
|
const res = new fbs.StatSyncRes();
|
2018-08-30 15:35:51 -04:00
|
|
|
assert(baseRes!.msg(res) != null);
|
|
|
|
return new FileInfo(res);
|
2018-08-29 09:22:25 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 11:20:35 -04:00
|
|
|
export function writeFileSync(
|
|
|
|
filename: string,
|
|
|
|
data: Uint8Array,
|
2018-08-22 13:19:32 -04:00
|
|
|
perm = 0o666
|
2018-07-06 11:20:35 -04:00
|
|
|
): void {
|
2018-08-22 13:19:32 -04:00
|
|
|
/* Ideally we could write:
|
|
|
|
const res = send({
|
2018-07-06 11:27:36 -04:00
|
|
|
command: fbs.Command.WRITE_FILE_SYNC,
|
2018-07-06 11:20:35 -04:00
|
|
|
writeFileSyncFilename: filename,
|
|
|
|
writeFileSyncData: data,
|
|
|
|
writeFileSyncPerm: perm
|
|
|
|
});
|
2018-07-06 11:27:36 -04:00
|
|
|
*/
|
2018-08-22 13:19:32 -04:00
|
|
|
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);
|
2018-08-30 15:35:51 -04:00
|
|
|
send(builder, fbs.Any.WriteFileSync, msg);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|