mirror of
https://github.com/denoland/deno.git
synced 2024-12-12 02:27:46 -05:00
fix(std/node/process): env, argv exports (#6455)
The promise approach still required permissions to be specified
at initialisation, rather than at request.
Using a Proxy instance solves this permission issue.
The Proxy instance approach also eliminates the need for the
await. Achieving direct compatibility with Node.js.
/ref pr #6392
/ref commit d16337cc9c
This commit is contained in:
parent
7ea3071db5
commit
16038b8f82
2 changed files with 32 additions and 27 deletions
|
@ -28,7 +28,7 @@ export const versions = {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** https://nodejs.org/api/process.html#process_process */
|
/** https://nodejs.org/api/process.html#process_process */
|
||||||
// @deprecated exported only for backwards compatibility with old deno versions
|
// @deprecated `import { process } from 'process'` for backwards compatibility with old deno versions
|
||||||
export const process = {
|
export const process = {
|
||||||
arch,
|
arch,
|
||||||
chdir,
|
chdir,
|
||||||
|
@ -40,43 +40,48 @@ export const process = {
|
||||||
versions,
|
versions,
|
||||||
|
|
||||||
/** https://nodejs.org/api/process.html#process_process_events */
|
/** https://nodejs.org/api/process.html#process_process_events */
|
||||||
// node --input-type=module -e "import {on} from 'process'; console.log(on)"
|
// on is not exported by node, it is only available within process:
|
||||||
// on is not exported by node, it is only available within process
|
// node --input-type=module -e "import { on } from 'process'; console.log(on)"
|
||||||
on(_event: string, _callback: Function): void {
|
on(_event: string, _callback: Function): void {
|
||||||
// TODO(rsp): to be implemented
|
// TODO(rsp): to be implemented
|
||||||
notImplemented();
|
notImplemented();
|
||||||
},
|
},
|
||||||
|
|
||||||
/** https://nodejs.org/api/process.html#process_process_env */
|
|
||||||
get env(): { [index: string]: string } {
|
|
||||||
// using getter to avoid --allow-env unless it's used
|
|
||||||
return Deno.env.toObject();
|
|
||||||
},
|
|
||||||
|
|
||||||
/** https://nodejs.org/api/process.html#process_process_argv */
|
/** https://nodejs.org/api/process.html#process_process_argv */
|
||||||
get argv(): string[] {
|
get argv(): string[] {
|
||||||
// Deno.execPath() also requires --allow-env
|
// Getter delegates --allow-env and --allow-read until request
|
||||||
|
// Getter also allows the export Proxy instance to function as intended
|
||||||
return [Deno.execPath(), ...Deno.args];
|
return [Deno.execPath(), ...Deno.args];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/** https://nodejs.org/api/process.html#process_process_env */
|
||||||
|
get env(): { [index: string]: string } {
|
||||||
|
// Getter delegates --allow-env and --allow-read until request
|
||||||
|
// Getter also allows the export Proxy instance to function as intended
|
||||||
|
return Deno.env.toObject();
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// define the type for configuring the env and argv promises
|
/**
|
||||||
// as well as for the global.process declaration
|
* https://nodejs.org/api/process.html#process_process_argv
|
||||||
type Process = typeof process;
|
* @example `import { argv } from './std/node/process.ts'; console.log(argv)`
|
||||||
|
*/
|
||||||
|
// Proxy delegates --allow-env and --allow-read to request time, even for exports
|
||||||
|
export const argv = new Proxy(process.argv, {});
|
||||||
|
|
||||||
/** requires the use of await for compatibility with deno */
|
/**
|
||||||
export const env = new Promise<Process["env"]>((resolve) =>
|
* https://nodejs.org/api/process.html#process_process_env
|
||||||
resolve(process.env)
|
* @example `import { env } from './std/node/process.ts'; console.log(env)`
|
||||||
);
|
*/
|
||||||
|
// Proxy delegates --allow-env and --allow-read to request time, even for exports
|
||||||
|
export const env = new Proxy(process.env, {});
|
||||||
|
|
||||||
/** requires the use of await for compatibility with deno */
|
// import process from './std/node/process.ts'
|
||||||
export const argv = new Promise<Process["argv"]>((resolve) =>
|
|
||||||
resolve(process.argv)
|
|
||||||
);
|
|
||||||
|
|
||||||
/** use this for access to `process.env` and `process.argv` without the need for await */
|
|
||||||
export default process;
|
export default process;
|
||||||
|
|
||||||
|
// Define the type for the global declration
|
||||||
|
type Process = typeof process;
|
||||||
|
|
||||||
Object.defineProperty(process, Symbol.toStringTag, {
|
Object.defineProperty(process, Symbol.toStringTag, {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true,
|
writable: true,
|
||||||
|
|
|
@ -102,9 +102,9 @@ Deno.test({
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "process.argv",
|
name: "process.argv",
|
||||||
async fn() {
|
fn() {
|
||||||
assert(Array.isArray(process.argv));
|
assert(Array.isArray(process.argv));
|
||||||
assert(Array.isArray(await argv));
|
assert(Array.isArray(argv));
|
||||||
assert(
|
assert(
|
||||||
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
|
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
|
||||||
"deno included in the file name of argv[0]"
|
"deno included in the file name of argv[0]"
|
||||||
|
@ -115,8 +115,8 @@ Deno.test({
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "process.env",
|
name: "process.env",
|
||||||
async fn() {
|
fn() {
|
||||||
assertEquals(typeof process.env.PATH, "string");
|
assertEquals(typeof process.env.PATH, "string");
|
||||||
assertEquals(typeof (await env).PATH, "string");
|
assertEquals(typeof env.PATH, "string");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue