Skip to content

Commit

Permalink
add process.platform
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 15, 2024
1 parent 2160e96 commit 1816265
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/node/internal/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ export function exit(code: number) {
utilImpl.processExitImpl(code);
}

export const platform = utilImpl.processPlatform;

export default {
nextTick,
env,
exit,
getBuiltinModule,
platform,
};
1 change: 1 addition & 0 deletions src/node/internal/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,4 @@ export function isBoxedPrimitive(
export function getBuiltinModule(id: string): any;
export function getCallSite(frames: number): Record<string, string>[];
export function processExitImpl(code: number): void;
export const processPlatform: string;
6 changes: 6 additions & 0 deletions src/workerd/api/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,9 @@ wd_test(
args = ["--experimental"],
data = ["tests/process-exit-test.js"],
)

wd_test(
src = "tests/process-nodejs-test.wd-test",
args = ["--experimental"],
data = ["tests/process-nodejs-test.js"],
)
7 changes: 7 additions & 0 deletions src/workerd/api/node/tests/process-nodejs-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import assert from 'node:assert';

export const processPlatform = {
test() {
assert.strictEqual(typeof process.platform, 'string');
},
};
15 changes: 15 additions & 0 deletions src/workerd/api/node/tests/process-nodejs-test.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "nodejs-process-test",
worker = (
modules = [
(name = "worker", esModule = embed "process-nodejs-test.js")
],
compatibilityDate = "2024-10-11",
compatibilityFlags = ["nodejs_compat"],
)
),
],
);
12 changes: 12 additions & 0 deletions src/workerd/api/node/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,16 @@ void UtilModule::processExitImpl(jsg::Lock& js, int code) {
js.logWarning(kj::str(err.get(js, "stack"_kj)));
}

kj::String UtilModule::getProcessPlatform() const {
#ifdef _WIN32
return kj::str("win32");
#elif defined(__linux__)
return kj::str("linux");
#elif defined(__APPLE__)
return kj::str("darwin");
#else
JSG_FAIL_REQUIRE(Error, "Unsupported platform"_kj);
#endif
}

} // namespace workerd::api::node
2 changes: 2 additions & 0 deletions src/workerd/api/node/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class UtilModule final: public jsg::Object {
bool isBoxedPrimitive(jsg::JsValue value);

jsg::JsValue getBuiltinModule(jsg::Lock& js, kj::String specifier);
kj::String getProcessPlatform() const;

// This is used in the implementation of process.exit(...). Contrary
// to what the name suggests, it does not actually exit the process.
Expand Down Expand Up @@ -251,6 +252,7 @@ class UtilModule final: public jsg::Object {

JSG_METHOD(getBuiltinModule);
JSG_METHOD(processExitImpl);
JSG_READONLY_INSTANCE_PROPERTY(processPlatform, getProcessPlatform);
}
};

Expand Down

0 comments on commit 1816265

Please sign in to comment.