File
Blob: src/workerd/api/node/process.h
| 1 | // Copyright (c) 2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | #pragma once |
| 5 | |
| 6 | #include <workerd/api/node/i18n.h> |
| 7 | #include <workerd/api/node/node-version.h> |
| 8 | #include <workerd/io/worker-fs.h> |
| 9 | #include <workerd/jsg/jsg.h> |
| 10 | |
| 11 | namespace workerd::api::node { |
| 12 | |
| 13 | class ProcessModule final: public jsg::Object { |
| 14 | public: |
| 15 | ProcessModule() = default; |
| 16 | ProcessModule(jsg::Lock&, const jsg::Url&) {} |
| 17 | |
| 18 | jsg::JsValue getBuiltinModule(jsg::Lock& js, kj::String specifier); |
| 19 | |
| 20 | // This process.platform implementation is always gated behind the |
| 21 | // 'enable_deprecated_process_actual_platform' compatibility flag |
| 22 | // which is ONLY used in testing environments, and requires 'experimental'. |
| 23 | #ifdef _WIN32 |
| 24 | static constexpr kj::StringPtr platform = "win32"_kj; |
| 25 | #elifdef __linux__ |
| 26 | static constexpr kj::StringPtr platform = "linux"_kj; |
| 27 | #elifdef __APPLE__ |
| 28 | static constexpr kj::StringPtr platform = "darwin"_kj; |
| 29 | #else |
| 30 | static constexpr kj::StringPtr platform = "unsupported-platform"_kj; |
| 31 | #endif |
| 32 | |
| 33 | // This is used in the implementation of process.exit(...). Contrary |
| 34 | // to what the name suggests, it does not actually exit the process. |
| 35 | // Instead, it will cause the IoContext, if any, and will stop javascript |
| 36 | // from further executing in that request. If there is no active IoContext, |
| 37 | // then it becomes a non-op. |
| 38 | void exitImpl(jsg::Lock& js, int code); |
| 39 | |
| 40 | // IMPORTANT: This function will always return "linux" on production unless |
| 41 | // the unsupported_process_actual_platform compat flag is enabled. |
| 42 | // This is only added for Node.js compatibility and running OS specific tests |
| 43 | kj::StringPtr getPlatform(jsg::Lock& js) const; |
| 44 | |
| 45 | jsg::JsObject getEnvObject(jsg::Lock& js); |
| 46 | |
| 47 | jsg::JsObject getVersions(jsg::Lock& js) const; |
| 48 | |
| 49 | kj::String getCwd(jsg::Lock& js); |
| 50 | |
| 51 | void setCwd(jsg::Lock& js, kj::String path); |
| 52 | |
| 53 | JSG_RESOURCE_TYPE(ProcessModule) { |
| 54 | JSG_METHOD(getEnvObject); |
| 55 | JSG_METHOD(getBuiltinModule); |
| 56 | JSG_METHOD(exitImpl); |
| 57 | JSG_METHOD(getCwd); |
| 58 | JSG_METHOD(setCwd); |
| 59 | JSG_LAZY_READONLY_INSTANCE_PROPERTY(versions, getVersions); |
| 60 | JSG_LAZY_READONLY_INSTANCE_PROPERTY(platform, getPlatform); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | #define EW_NODE_PROCESS_ISOLATE_TYPES api::node::ProcessModule |
| 65 | |
| 66 | } // namespace workerd::api::node |