-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node process
object compatibility
#3368
Changes from 6 commits
ba4474f
205c80b
22780bc
8405762
37bb133
cfdcc6c
2a82f40
dfef99a
0e220da
7923317
d71576d
7a29331
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export const version = `v${Deno.version.deno}`; | ||
|
||
export const versions = { | ||
node: Deno.version.deno, | ||
...Deno.version | ||
}; | ||
|
||
const osToPlatform = (os: Deno.OperatingSystem): string => | ||
os === "win" ? "win32" : os === "mac" ? "darwin" : os; | ||
|
||
export const platform = osToPlatform(Deno.build.os); | ||
|
||
export const { arch } = Deno.build; | ||
|
||
export const argv = [Deno.execPath(), ...Deno.args]; | ||
|
||
// TODO(rsp): currently setting env seems to be working by modifying the object | ||
// that is returnd by Deno.env(). Need to make sure that this is the final API | ||
// or Deno.env('key', 'value') is to be used in the future. | ||
export const env = Deno.env(); | ||
|
||
export const { pid, cwd, chdir, exit } = Deno; | ||
|
||
export function on(_event: string, _callback: Function): void { | ||
// TODO(rsp): to be implemented | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'm just throwing Error("unimplemented") for everything that is not actually implemented. I don't want to add any exceptions. Sorry for the confusion. |
||
// This is needed and empty func is actually sufficient for code that do things like: | ||
// process.on("uncaughtException", (err) => { | ||
// if (!(err instanceof ExitStatus)) throw err; | ||
// }); | ||
// Deno dies on uncaught exceptions anyway, but without it it will also die on | ||
// registering the callback itself. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { test } from "../testing/mod.ts"; | ||
import { assert, assertThrows, assertEquals } from "../testing/asserts.ts"; | ||
import * as process from "./process.ts"; | ||
|
||
// NOTE: Deno.cwd() and Deno.chdir() currently require --allow-env | ||
// (Also Deno.env() requires --allow-env but it's more obvious) | ||
|
||
test({ | ||
name: "process.cwd and process.chdir success", | ||
fn() { | ||
// this should be run like other tests from directory up | ||
assert(process.cwd().match(/\Wstd$/)); | ||
process.chdir("node"); | ||
assert(process.cwd().match(/\Wnode$/)); | ||
process.chdir(".."); | ||
assert(process.cwd().match(/\Wstd$/)); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.chdir failure", | ||
fn() { | ||
assertThrows( | ||
() => { | ||
process.chdir("non-existent-directory-name"); | ||
}, | ||
Deno.DenoError, | ||
"No such file" | ||
); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.version", | ||
fn() { | ||
assertEquals(typeof process, "object"); | ||
assertEquals(typeof process.version, "string"); | ||
assertEquals(typeof process.versions, "object"); | ||
assertEquals(typeof process.versions.node, "string"); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.platform", | ||
fn() { | ||
assertEquals(typeof process.platform, "string"); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.arch", | ||
fn() { | ||
assertEquals(typeof process.arch, "string"); | ||
// TODO(rsp): make sure that the arch strings should be the same in Node and Deno: | ||
assertEquals(process.arch, Deno.build.arch); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.pid", | ||
fn() { | ||
assertEquals(typeof process.pid, "number"); | ||
assertEquals(process.pid, Deno.pid); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.on", | ||
fn() { | ||
assertEquals(typeof process.on, "function"); | ||
process.on("uncaughtException", (_err: Error) => {}); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.argv", | ||
fn() { | ||
assert(Array.isArray(process.argv)); | ||
assert(process.argv.filter(x => x.match(/process_test[.]ts$/)).length > 0); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.env", | ||
fn() { | ||
assertEquals(typeof process.env.PATH, "string"); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use a getter here? Otherwise everyone who imports this module will need to have --allow-env
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so you mean to give only a read access to the env? (I thought that--allow-env
was needed to read the env as well, wasn't it?)If so then I can either return a copy of the env as it was in the moment of reading, or use a Proxy to handle getting properties and throw on setting them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I think I know what you mean :) Never mind what I said, I apparently had too little sleep today.