EAS 托管工作器运行时

了解 EAS Hosting 工作器运行时和 Node.js 兼容性。


EAS Hosting 构建在 Cloudflare Workers 上,这是一种现代且强大的无服务器 API 平台,旨在实现无缝扩展、高可靠性和卓越的全球性能。

🌐 EAS Hosting is built on Cloudflare Workers, a modern and powerful platform for serverless APIs that's been built for seamless scalability, high reliability, and exceptional performance globally.

Cloudflare Workers 运行时运行在 V8 JavaScript 引擎上,该引擎同样驱动着 Node.js 和 Chromium 中的 JavaScript。然而,它的运行时与传统的无服务器 Node.js 部署相比,有一些关键的区别。

🌐 The Cloudflare Workers runtime runs on the V8 JavaScript engine, the same powering JavaScript in Node.js and Chromium. However, its runtime has a few key differences from what you might be used to in traditional serverless Node.js deployments.

与每个请求在完整的 JavaScript 进程中运行不同,Workers 旨在在小型 V8 隔离环境中运行它们,这是 V8 运行时的一个特性。可以将它们视为单个 JavaScript 进程中的微型容器。

🌐 Instead of each request running in a full JavaScript process, Workers are designed to run them in small V8 isolates, a feature of the V8 runtime. Think of them as micro-containers in a single JavaScript process.

有关 Workers 如何工作的更多信息,请参阅 Cloudflare Workers 文档。

🌐 For more information on how Workers work, see Cloudflare Workers documentation.

Node.js 兼容性

🌐 Node.js compatibility

Cloudflare 是 Winter TC 的一部分,更类似于浏览器和服务工作线程中的 JavaScript 环境,而不是 Node.js。这样的限制提供了比 Node.js 更精简的运行时,同时仍然很熟悉。这种通用运行时是目前许多 JavaScript 运行时所支持的最小标准。

🌐 Cloudflare is part of Winter TC, is more similar to the JavaScript environments in browsers and service workers rather than in Node.js. Restrictions like these provide a leaner runtime than Node.js, which is still familiar. This common runtime is a minimal standard supported by many JavaScript runtime these days.

这意味着,许多你可能习惯使用的 Node.js API 或你使用的一些依赖,在 EAS Hosting 运行时中并不可直接使用。为了简化这一过渡过程,因为并非所有依赖都已经对 Web API 提供一流支持,所以存在 Node.js 兼容模块,可以在你的 API 路由中使用。

🌐 This means, many Node.js APIs that you might be used to or some dependencies you utilize, aren't directly available in the EAS Hosting runtime. To ease this transition, as not all dependencies will have first-class support for Web APIs yet, Node.js compatibility modules exist and can be used in your API routes.

Node.js built-in moduleSupportedImplementation notes
node:assert
node:async_hooks
node:buffer
node:cryptoSelect deprecated algorithms are not available
node:consoleProvided as partially functional JS shims
node:constants
node:diagnostics_channelSelect deprecated algorithms are not implemented
node:dnsResolver is unimplemented, all DNS requests are sent to Cloudflare
node:events
node:fsSupported, with in-memory filesystem
node:httpSupported, except for server functionality
node:http2Partially supported. Server functionality unsupported
node:httpsSupported, except for server functionality
node:moduleSourceMap is unimplemented, partially supported otherwise
node:netServer and BlockList are unimplemented, client sockets are partially supported
node:osProvided as JS stubs that provide mock values matching Node.js on Linux
node:path
node:path/posix
node:path/win32
node:processProvided as JS stubs
node:punycode
node:querystring
node:readlineProvided as non-functional JS stubs, since workers have no stdin
node:stream
node:stream/consumers
node:stream/web
node:string_decoder
node:test
node:timers
node:tlsSupported, except for server functionality
node:trace_eventsProvided as non-functional JS stubs
node:ttyProvided as JS shims redirecting output to the Console API
node:url
node:util
node:util/types
node:worker_threadsProvided as non-functional JS stubs, since workers don't support threading
node:zlib

这些模块通常提供比其 Node.js 对应模块精度更低的填补功能或近似实现。例如,fshttphttps 模块有额外的限制,并且是 Node.js 兼容层,并不等同于在 Node.js 进程中运行它们。

🌐 These modules generally provide a lower-accuracy polyfill or approximation of their Node.js counterparts. For example, the fs, http, and https modules have additional restrictions in place and are Node.js compatibility layers, which aren't equivalent to running them in a Node.js process.

上述列出的任何 Node.js 模块都可以像平常一样在 API 路由或 API 路由的依赖中使用,并将使用适当的兼容性模块。但是,其中一些模块可能不提供任何实际功能,只是存在于对 API 进行填充以防止运行时崩溃。

🌐 Any of the above listed Node.js modules can be used in API routes or dependencies of your API routes as usual and will use appropriate compatibility modules. However, some of these modules may not provide any practical functionality and only exist to shim APIs to prevent runtime crashes.

此处未提及的任何模块均不可用或不受支持,你的代码和任何依赖都不应依赖于这些模块。

🌐 Any modules that aren't mentioned here are unavailable or unsupported, and your code and none of your dependencies should rely on them being provided.

未来可能会添加更多 Node.js 兼容性填充,但所有未在此非详尽列表中记录的 Node.js API 都不被期望能够正常工作。

全局变量

🌐 Globals

JavaScript runtime globalsSupportedImplementation notes
originWill always be the same as the incoming request's Origin header
process
process.envPopulated with EAS Hosting environment variables
process.stdoutWill redirect output to the Console API (console.log) for logging
process.stderrWill redirect output to the Console API (console.error) for logging
setImmediate
clearImmediate
BufferSet to Buffer from node:buffer
EventEmitterSet to EventEmitter from node:events
globalSet to globalThis
WeakRef
FinalizationRegistry
requireExternal requires are supported but limited to deployed JS files and built-in modules. Node module resolution is unsupported.
require.cache