Skip to content
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

add process.platform #2924

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
anonrig marked this conversation as resolved.
Show resolved Hide resolved
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_LAZY_READONLY_INSTANCE_PROPERTY(processPlatform, getProcessPlatform);
}
};

Expand Down