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 16, 2024
1 parent 2160e96 commit 2b5249c
Show file tree
Hide file tree
Showing 6 changed files with 50 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"],
)
8 changes: 8 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,8 @@
import assert from 'node:assert';

export const processPlatform = {
test() {
assert.strictEqual(typeof process.platform, 'string');
assert.ok(['darwin', 'win32', 'linux'].includes(process.platform));
},
};
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"],
)
),
],
);
17 changes: 17 additions & 0 deletions src/workerd/api/node/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ class UtilModule final: public jsg::Object {

jsg::Name getResourceTypeInspect(jsg::Lock& js);

#ifdef _WIN32
static constexpr kj::StringPtr processPlatform = "win32"_kj;
#elif defined(__linux__)
static constexpr kj::StringPtr processPlatform = "linux"_kj;
#elif defined(__APPLE__)
static constexpr kj::StringPtr processPlatform = "darwin"_kj;
#else
static constexpr kj::StringPtr processPlatform = "unsupported-platform"_kj;
#endif

// `getOwnNonIndexProperties()` `filter`s
static constexpr int ALL_PROPERTIES = jsg::PropertyFilter::ALL_PROPERTIES;
static constexpr int ONLY_ENUMERABLE = jsg::PropertyFilter::ONLY_ENUMERABLE;
Expand Down Expand Up @@ -223,6 +233,12 @@ class UtilModule final: public jsg::Object {
// then it becomes a non-op.
void processExitImpl(jsg::Lock& js, int code);

// IMPORTANT: This function will always return "linux" on production.
// This is only added for Node.js compatibility and running OS specific tests
kj::StringPtr getProcessPlatform() const {
return processPlatform;
}

JSG_RESOURCE_TYPE(UtilModule) {
JSG_NESTED_TYPE(MIMEType);
JSG_NESTED_TYPE(MIMEParams);
Expand Down Expand Up @@ -251,6 +267,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 2b5249c

Please sign in to comment.