2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg from "gen/msg_generated";
|
2019-01-28 20:41:28 -05:00
|
|
|
import { handleAsyncMsgFromRust, sendSync } from "./dispatch";
|
|
|
|
import * as flatbuffers from "./flatbuffers";
|
|
|
|
import { libdeno } from "./libdeno";
|
2018-07-13 03:24:07 -04:00
|
|
|
import { assert } from "./util";
|
|
|
|
import * as util from "./util";
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2019-01-06 14:16:42 -05:00
|
|
|
/** process id */
|
|
|
|
export let pid: number;
|
|
|
|
|
|
|
|
export function setPid(pid_: number): void {
|
|
|
|
assert(!pid);
|
|
|
|
pid = pid_;
|
|
|
|
}
|
|
|
|
|
2018-10-21 22:14:27 -04:00
|
|
|
interface CodeInfo {
|
|
|
|
moduleName: string | undefined;
|
|
|
|
filename: string | undefined;
|
|
|
|
mediaType: msg.MediaType;
|
|
|
|
sourceCode: string | undefined;
|
|
|
|
outputCode: string | undefined;
|
2018-10-28 19:41:10 -04:00
|
|
|
sourceMap: string | undefined;
|
2018-10-21 22:14:27 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Exit the Deno process with optional exit code. */
|
2018-08-01 15:56:27 -04:00
|
|
|
export function exit(exitCode = 0): never {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Exit.startExit(builder);
|
|
|
|
msg.Exit.addCode(builder, exitCode);
|
|
|
|
const inner = msg.Exit.endExit(builder);
|
|
|
|
sendSync(builder, msg.Any.Exit, inner);
|
2018-08-19 15:04:27 -04:00
|
|
|
return util.unreachable();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 19:33:30 -04:00
|
|
|
// @internal
|
2018-12-11 11:23:59 -05:00
|
|
|
export function codeFetch(specifier: string, referrer: string): CodeInfo {
|
|
|
|
util.log("os.ts codeFetch", specifier, referrer);
|
2018-07-06 11:27:36 -04:00
|
|
|
// Send CodeFetch message
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-12-11 11:23:59 -05:00
|
|
|
const specifier_ = builder.createString(specifier);
|
|
|
|
const referrer_ = builder.createString(referrer);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.CodeFetch.startCodeFetch(builder);
|
2018-12-11 11:23:59 -05:00
|
|
|
msg.CodeFetch.addSpecifier(builder, specifier_);
|
|
|
|
msg.CodeFetch.addReferrer(builder, referrer_);
|
2018-10-03 21:18:23 -04:00
|
|
|
const inner = msg.CodeFetch.endCodeFetch(builder);
|
|
|
|
const baseRes = sendSync(builder, msg.Any.CodeFetch, inner);
|
2018-08-30 15:35:51 -04:00
|
|
|
assert(baseRes != null);
|
2018-09-08 16:37:49 -04:00
|
|
|
assert(
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Any.CodeFetchRes === baseRes!.innerType(),
|
2018-10-03 21:12:23 -04:00
|
|
|
`base.innerType() unexpectedly is ${baseRes!.innerType()}`
|
2018-09-08 16:37:49 -04:00
|
|
|
);
|
2018-10-03 21:18:23 -04:00
|
|
|
const codeFetchRes = new msg.CodeFetchRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes!.inner(codeFetchRes) != null);
|
2018-10-21 22:14:27 -04:00
|
|
|
// flatbuffers returns `null` for an empty value, this does not fit well with
|
|
|
|
// idiomatic TypeScript under strict null checks, so converting to `undefined`
|
2018-09-01 18:49:47 -04:00
|
|
|
return {
|
2018-10-21 22:14:27 -04:00
|
|
|
moduleName: codeFetchRes.moduleName() || undefined,
|
|
|
|
filename: codeFetchRes.filename() || undefined,
|
|
|
|
mediaType: codeFetchRes.mediaType(),
|
2019-01-03 22:11:01 -05:00
|
|
|
sourceCode: codeFetchRes.sourceCode() || undefined,
|
|
|
|
outputCode: codeFetchRes.outputCode() || undefined,
|
|
|
|
sourceMap: codeFetchRes.sourceMap() || undefined
|
2018-07-06 11:20:35 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-07 19:33:30 -04:00
|
|
|
// @internal
|
2018-07-06 11:20:35 -04:00
|
|
|
export function codeCache(
|
|
|
|
filename: string,
|
|
|
|
sourceCode: string,
|
2018-10-28 19:41:10 -04:00
|
|
|
outputCode: string,
|
|
|
|
sourceMap: string
|
2018-07-06 11:20:35 -04:00
|
|
|
): void {
|
2018-07-13 03:24:07 -04:00
|
|
|
util.log("os.ts codeCache", filename, sourceCode, outputCode);
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-07-06 11:27:36 -04:00
|
|
|
const filename_ = builder.createString(filename);
|
|
|
|
const sourceCode_ = builder.createString(sourceCode);
|
|
|
|
const outputCode_ = builder.createString(outputCode);
|
2018-10-28 19:41:10 -04:00
|
|
|
const sourceMap_ = builder.createString(sourceMap);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.CodeCache.startCodeCache(builder);
|
|
|
|
msg.CodeCache.addFilename(builder, filename_);
|
|
|
|
msg.CodeCache.addSourceCode(builder, sourceCode_);
|
|
|
|
msg.CodeCache.addOutputCode(builder, outputCode_);
|
2018-10-28 19:41:10 -04:00
|
|
|
msg.CodeCache.addSourceMap(builder, sourceMap_);
|
2018-10-03 21:18:23 -04:00
|
|
|
const inner = msg.CodeCache.endCodeCache(builder);
|
|
|
|
const baseRes = sendSync(builder, msg.Any.CodeCache, inner);
|
2018-08-30 15:35:51 -04:00
|
|
|
assert(baseRes == null); // Expect null or error.
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-11-02 20:09:10 -04:00
|
|
|
function createEnv(inner: msg.EnvironRes): { [index: string]: string } {
|
2018-09-01 10:45:26 -04:00
|
|
|
const env: { [index: string]: string } = {};
|
2018-08-31 07:51:12 -04:00
|
|
|
|
2018-11-02 20:09:10 -04:00
|
|
|
for (let i = 0; i < inner.mapLength(); i++) {
|
|
|
|
const item = inner.map(i)!;
|
2018-08-31 07:51:12 -04:00
|
|
|
env[item.key()!] = item.value()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Proxy(env, {
|
2019-01-23 20:29:18 -05:00
|
|
|
set(obj, prop: string, value: string) {
|
|
|
|
setEnv(prop, value);
|
2018-08-31 07:51:12 -04:00
|
|
|
return Reflect.set(obj, prop, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEnv(key: string, value: string): void {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-08-31 07:51:12 -04:00
|
|
|
const _key = builder.createString(key);
|
|
|
|
const _value = builder.createString(value);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.SetEnv.startSetEnv(builder);
|
|
|
|
msg.SetEnv.addKey(builder, _key);
|
|
|
|
msg.SetEnv.addValue(builder, _value);
|
|
|
|
const inner = msg.SetEnv.endSetEnv(builder);
|
|
|
|
sendSync(builder, msg.Any.SetEnv, inner);
|
2018-08-31 07:51:12 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns a snapshot of the environment variables at invocation. Mutating a
|
2018-08-31 07:51:12 -04:00
|
|
|
* property in the object will set that variable in the environment for
|
2019-01-23 20:29:18 -05:00
|
|
|
* the process. The environment object will only accept `string`s
|
2018-08-31 07:51:12 -04:00
|
|
|
* as values.
|
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* import { env } from "deno";
|
|
|
|
*
|
|
|
|
* const myEnv = env();
|
|
|
|
* console.log(myEnv.SHELL);
|
|
|
|
* myEnv.TEST_VAR = "HELLO";
|
|
|
|
* const newEnv = env();
|
|
|
|
* console.log(myEnv.TEST_VAR == newEnv.TEST_VAR);
|
2018-08-31 07:51:12 -04:00
|
|
|
*/
|
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
|
2018-09-09 18:54:42 -04:00
|
|
|
const res = sendSync({
|
2018-10-03 21:18:23 -04:00
|
|
|
command: msg.Command.ENV,
|
2018-08-31 07:51:12 -04:00
|
|
|
});
|
|
|
|
*/
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Environ.startEnviron(builder);
|
|
|
|
const inner = msg.Environ.endEnviron(builder);
|
|
|
|
const baseRes = sendSync(builder, msg.Any.Environ, inner)!;
|
|
|
|
assert(msg.Any.EnvironRes === baseRes.innerType());
|
|
|
|
const res = new msg.EnvironRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes.inner(res) != null);
|
2018-08-31 07:51:12 -04:00
|
|
|
// TypeScript cannot track assertion above, therefore not null assertion
|
|
|
|
return createEnv(res);
|
|
|
|
}
|
2019-01-28 20:41:28 -05:00
|
|
|
|
|
|
|
/** Send to the privileged side that we have setup and are ready. */
|
|
|
|
function sendStart(): msg.StartRes {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
msg.Start.startStart(builder);
|
|
|
|
const startOffset = msg.Start.endStart(builder);
|
|
|
|
const baseRes = sendSync(builder, msg.Any.Start, startOffset);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.StartRes === baseRes!.innerType());
|
|
|
|
const startResMsg = new msg.StartRes();
|
|
|
|
assert(baseRes!.inner(startResMsg) != null);
|
|
|
|
return startResMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function bootstraps an environment within Deno, it is shared both by
|
|
|
|
// the runtime and the compiler environments.
|
|
|
|
// @internal
|
|
|
|
export function start(): msg.StartRes {
|
|
|
|
libdeno.recv(handleAsyncMsgFromRust);
|
|
|
|
|
|
|
|
// First we send an empty `Start` message to let the privileged side know we
|
|
|
|
// are ready. The response should be a `StartRes` message containing the CLI
|
|
|
|
// args and other info.
|
|
|
|
const startResMsg = sendStart();
|
|
|
|
|
|
|
|
util.setLogDebug(startResMsg.debugFlag());
|
|
|
|
|
|
|
|
return startResMsg;
|
|
|
|
}
|