Skip to content

Commit

Permalink
feat(std/node): add os.type() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jbergstroem committed Mar 30, 2020
1 parent 6ae4744 commit b7b690b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ termcolor = "1.1.0"
tokio = { version = "0.2.13", features = ["rt-core", "tcp", "udp", "uds", "process", "fs", "blocking", "sync", "io-std", "macros", "time"] }
tokio-rustls = "0.13.0"
url = "2.1.1"
uname = "0.1.1"
utime = "0.2.1"
webpki = "0.21.2"
webpki-roots = "0.19.0"
Expand Down
1 change: 1 addition & 0 deletions cli/js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export {
execPath,
hostname,
loadavg,
type,
osRelease,
} from "./ops/os.ts";
export {
Expand Down
6 changes: 6 additions & 0 deletions cli/js/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,12 @@ declare namespace Deno {
/** Read `r` until `Deno.EOF` and returns the content as `Uint8Array`. */
export function readAllSync(r: SyncReader): Uint8Array;

/** Returns the operating system name as returned by uname(3).
*
* console.log(Deno.type());
*/
export function type(): string;

/** Write all the content of `arr` to `w`. */
export function writeAll(w: Writer, arr: Uint8Array): Promise<void>;

Expand Down
4 changes: 4 additions & 0 deletions cli/js/ops/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function osRelease(): string {
return sendSync("op_os_release");
}

export function type(): string {
return sendSync("op_type");
}

export function exit(code = 0): never {
sendSync("op_exit", { code });
throw new Error("Code not reachable");
Expand Down
12 changes: 12 additions & 0 deletions cli/js/tests/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,15 @@ unitTest({ perms: { env: false } }, function releasePerm(): void {
}
assert(caughtError);
});

unitTest({ perms: { env: true } }, function osType(): void {
const typeMatrix = {
'linux': 'Linux',
'mac': 'Darwin',
'win': 'Windows_NT',
};

assertNotEquals(Deno.type(), "");
assertEquals(Deno.type(), typeMatrix[Deno.build.os]);
}
);
12 changes: 12 additions & 0 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::collections::HashMap;
use std::env;
use std::io::{Error, ErrorKind};
use sys_info;
use uname::uname;
use url::Url;

pub fn init(i: &mut Isolate, s: &State) {
Expand All @@ -18,6 +19,7 @@ pub fn init(i: &mut Isolate, s: &State) {
i.register_op("op_get_dir", s.stateful_json_op(op_get_dir));
i.register_op("op_hostname", s.stateful_json_op(op_hostname));
i.register_op("op_loadavg", s.stateful_json_op(op_loadavg));
i.register_op("op_type", s.stateful_json_op(op_type));
i.register_op("op_os_release", s.stateful_json_op(op_os_release));
}

Expand Down Expand Up @@ -176,6 +178,16 @@ fn op_hostname(
Ok(JsonOp::Sync(json!(hostname)))
}

fn op_type(
state: &State,
_args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
state.check_env()?;
let info = uname().unwrap().sysname;
Ok(JsonOp::Sync(json!(info)))
}

fn op_os_release(
state: &State,
_args: Value,
Expand Down
4 changes: 2 additions & 2 deletions std/node/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ export function totalmem(): number {
notImplemented(SEE_GITHUB_ISSUE);
}

/** Not yet implemented */
/** Returns the operating system name as returned by uname(3). */
export function type(): string {
notImplemented(SEE_GITHUB_ISSUE);
return Deno.type();
}

/** Not yet implemented */
Expand Down

0 comments on commit b7b690b

Please sign in to comment.