-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ba4474f
Add partial Node-compatible process implementation
rsp 205c80b
Add tests for process implementation
rsp 22780bc
Fix linter errors
rsp 8405762
Fix formatting
rsp 37bb133
Update tests for process
rsp cfdcc6c
Remove assumptions about the ci environment
rsp 2a82f40
Add unimplemented exception
rsp dfef99a
Add a test message to examine the argv that fails on ci
rsp 0e220da
Fix argv tests
rsp 7923317
Refactor to use getters for properties that require --allow-env
rsp d71576d
Fix test on Windows caused by different error message
rsp 7a29331
Use util for consistency
rsp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { notImplemented } from "./_utils.ts"; | ||
|
||
const version = `v${Deno.version.deno}`; | ||
|
||
const versions = { | ||
node: Deno.version.deno, | ||
...Deno.version | ||
}; | ||
|
||
const osToPlatform = (os: Deno.OperatingSystem): string => | ||
os === "win" ? "win32" : os === "mac" ? "darwin" : os; | ||
|
||
const platform = osToPlatform(Deno.build.os); | ||
|
||
const { arch } = Deno.build; | ||
|
||
const { pid, cwd, chdir, exit } = Deno; | ||
|
||
function on(_event: string, _callback: Function): void { | ||
// TODO(rsp): to be implemented | ||
notImplemented(); | ||
} | ||
|
||
export const process = { | ||
version, | ||
versions, | ||
platform, | ||
arch, | ||
pid, | ||
cwd, | ||
chdir, | ||
exit, | ||
on, | ||
get env(): { [index: string]: string } { | ||
// using getter to avoid --allow-env unless it's used | ||
return Deno.env(); | ||
}, | ||
get argv(): string[] { | ||
// Deno.execPath() also requires --allow-env | ||
return [Deno.execPath(), ...Deno.args]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { test } from "../testing/mod.ts"; | ||
import { assert, assertThrows, assertEquals } from "../testing/asserts.ts"; | ||
import { process } from "./process.ts"; | ||
|
||
// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env | ||
// (Also Deno.env() (and process.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, | ||
"file" | ||
// On every OS Deno returns: "No such file" except for Windows, where it's: | ||
// "The system cannot find the file specified. (os error 2)" so "file" is | ||
// the only common string here. | ||
// TODO(rsp): Crazy idea: 404 for things like this? | ||
// It would be nice to have error codes like 404 or 403 in addition to strings. | ||
); | ||
} | ||
}); | ||
|
||
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"); | ||
assertThrows( | ||
() => { | ||
process.on("uncaughtException", (_err: Error) => {}); | ||
}, | ||
Error, | ||
"implemented" | ||
); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.argv", | ||
fn() { | ||
assert(Array.isArray(process.argv)); | ||
assert( | ||
process.argv[0].match(/[^/\\]*deno[^/\\]*$/), | ||
"deno included in the file name of argv[0]" | ||
); | ||
// we cannot test for anything else (we see test runner arguments here) | ||
} | ||
}); | ||
|
||
test({ | ||
name: "process.env", | ||
fn() { | ||
assertEquals(typeof process.env.PATH, "string"); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
please add
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.
Is it ok if I throw Error("unimplemented") for everything except "uncaughtException"? The idea is that Deno crashes on all uncaught exceptions anyway, and I want to prevent code that makes Node crash on uncaught exceptions to crash during adding a listener for uncaughtException.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.
But it you think it's not a good idea then I can throw on everything and take care of it in a third party module, just let me know.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, 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.