-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudflare): enable native node.js compatibility (#3064)
- Loading branch information
Showing
9 changed files
with
444 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { Preset } from "unenv"; | ||
import type { Plugin } from "rollup"; | ||
|
||
import { fileURLToPath } from "mlly"; | ||
import { join } from "pathe"; | ||
|
||
export const cloudflareExternals = [ | ||
"cloudflare:email", | ||
"cloudflare:sockets", | ||
"cloudflare:workers", | ||
"cloudflare:workflows", | ||
] as const; | ||
|
||
// Built-in APIs provided by workerd with nodejs compatibility | ||
// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/preset.ts | ||
export const nodeCompatModules = [ | ||
"_stream_duplex", | ||
"_stream_passthrough", | ||
"_stream_readable", | ||
"_stream_transform", | ||
"_stream_writable", | ||
"assert", | ||
"assert/strict", | ||
"buffer", | ||
"diagnostics_channel", | ||
"dns", | ||
"dns/promises", | ||
"events", | ||
"net", | ||
"path", | ||
"path/posix", | ||
"path/win32", | ||
"querystring", | ||
"stream", | ||
"stream/consumers", | ||
"stream/promises", | ||
"stream/web", | ||
"string_decoder", | ||
"timers", | ||
"timers/promises", | ||
"url", | ||
"util/types", | ||
"zlib", | ||
]; | ||
|
||
// Modules implemented via a mix of workerd APIs and polyfills | ||
export const hybridNodeCompatModules = ["async_hooks", "crypto", "util"]; | ||
|
||
const presetRuntimeDir = fileURLToPath(new URL("runtime/", import.meta.url)); | ||
const resolvePresetRuntime = (m: string) => join(presetRuntimeDir, `${m}.mjs`); | ||
|
||
export const unenvCfPreset: Preset = { | ||
external: nodeCompatModules.map((m) => `node:${m}`), | ||
alias: { | ||
// <id> => node:<id> | ||
...Object.fromEntries(nodeCompatModules.map((m) => [m, `node:${m}`])), | ||
...Object.fromEntries(hybridNodeCompatModules.map((m) => [m, `node:${m}`])), | ||
// node:<id> => runtime/<id>.mjs (hybrid) | ||
...Object.fromEntries( | ||
hybridNodeCompatModules.map((m) => [ | ||
`node:${m}`, | ||
resolvePresetRuntime(m === "sys" ? "util" : m), | ||
]) | ||
), | ||
sys: resolvePresetRuntime("util"), | ||
"node:sys": resolvePresetRuntime("util"), | ||
}, | ||
}; | ||
|
||
export const hybridNodePlugin: Plugin = { | ||
name: "nitro:cloudflare:hybrid-node-compat", | ||
resolveId(id) { | ||
if (id.startsWith("#workerd/node:")) { | ||
return { id: id.slice("#workerd/".length), external: true }; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/async_hooks/index.ts | ||
|
||
import workerdAsyncHooks from "#workerd/node:async_hooks"; | ||
|
||
import { | ||
// asyncWrapProviders, | ||
createHook, | ||
executionAsyncId, | ||
executionAsyncResource, | ||
triggerAsyncId, | ||
} from "unenv/runtime/node/async_hooks/index"; | ||
|
||
export { | ||
// asyncWrapProviders, | ||
createHook, | ||
executionAsyncId, | ||
executionAsyncResource, | ||
triggerAsyncId, | ||
} from "unenv/runtime/node/async_hooks/index"; | ||
|
||
export const { AsyncLocalStorage, AsyncResource } = workerdAsyncHooks; | ||
|
||
export default { | ||
createHook, | ||
executionAsyncId, | ||
executionAsyncResource, | ||
triggerAsyncId, | ||
AsyncLocalStorage, | ||
AsyncResource, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/crypto/index.ts | ||
|
||
import workerdCrypto from "#workerd/node:crypto"; | ||
|
||
import { | ||
Cipher, | ||
Cipheriv, | ||
constants, | ||
createCipher, | ||
createCipheriv, | ||
createDecipher, | ||
createDecipheriv, | ||
createECDH, | ||
createSign, | ||
createVerify, | ||
Decipher, | ||
Decipheriv, | ||
diffieHellman, | ||
ECDH, | ||
getCipherInfo, | ||
// hash, | ||
privateDecrypt, | ||
privateEncrypt, | ||
pseudoRandomBytes, | ||
publicDecrypt, | ||
publicEncrypt, | ||
Sign, | ||
sign, | ||
webcrypto as unenvCryptoWebcrypto, | ||
Verify, | ||
verify, | ||
} from "unenv/runtime/node/crypto/index"; | ||
|
||
export { | ||
Cipher, | ||
Cipheriv, | ||
Decipher, | ||
Decipheriv, | ||
ECDH, | ||
Sign, | ||
Verify, | ||
constants, | ||
createCipheriv, | ||
createDecipheriv, | ||
createECDH, | ||
createSign, | ||
createVerify, | ||
diffieHellman, | ||
getCipherInfo, | ||
// hash, | ||
privateDecrypt, | ||
privateEncrypt, | ||
publicDecrypt, | ||
publicEncrypt, | ||
sign, | ||
verify, | ||
} from "unenv/runtime/node/crypto/index"; | ||
|
||
export const { | ||
Certificate, | ||
DiffieHellman, | ||
DiffieHellmanGroup, | ||
Hash, | ||
Hmac, | ||
KeyObject, | ||
X509Certificate, | ||
checkPrime, | ||
checkPrimeSync, | ||
createDiffieHellman, | ||
createDiffieHellmanGroup, | ||
createHash, | ||
createHmac, | ||
createPrivateKey, | ||
createPublicKey, | ||
createSecretKey, | ||
generateKey, | ||
generateKeyPair, | ||
generateKeyPairSync, | ||
generateKeySync, | ||
generatePrime, | ||
generatePrimeSync, | ||
getCiphers, | ||
getCurves, | ||
getDiffieHellman, | ||
getFips, | ||
getHashes, | ||
hkdf, | ||
hkdfSync, | ||
pbkdf2, | ||
pbkdf2Sync, | ||
randomBytes, | ||
randomFill, | ||
randomFillSync, | ||
randomInt, | ||
randomUUID, | ||
scrypt, | ||
scryptSync, | ||
secureHeapUsed, | ||
setEngine, | ||
setFips, | ||
subtle, | ||
timingSafeEqual, | ||
} = workerdCrypto; | ||
|
||
export const getRandomValues = workerdCrypto.getRandomValues.bind( | ||
workerdCrypto.webcrypto | ||
); | ||
|
||
export const webcrypto = { | ||
CryptoKey: unenvCryptoWebcrypto.CryptoKey, | ||
getRandomValues, | ||
randomUUID, | ||
subtle, | ||
}; | ||
|
||
const fips = workerdCrypto.fips; | ||
|
||
export default { | ||
Certificate, | ||
Cipher, | ||
Cipheriv, | ||
Decipher, | ||
Decipheriv, | ||
ECDH, | ||
Sign, | ||
Verify, | ||
X509Certificate, | ||
constants, | ||
createCipheriv, | ||
createDecipheriv, | ||
createECDH, | ||
createSign, | ||
createVerify, | ||
diffieHellman, | ||
getCipherInfo, | ||
// hash, | ||
privateDecrypt, | ||
privateEncrypt, | ||
publicDecrypt, | ||
publicEncrypt, | ||
scrypt, | ||
scryptSync, | ||
sign, | ||
verify, | ||
createCipher, | ||
createDecipher, | ||
pseudoRandomBytes, | ||
DiffieHellman, | ||
DiffieHellmanGroup, | ||
Hash, | ||
Hmac, | ||
KeyObject, | ||
checkPrime, | ||
checkPrimeSync, | ||
createDiffieHellman, | ||
createDiffieHellmanGroup, | ||
createHash, | ||
createHmac, | ||
createPrivateKey, | ||
createPublicKey, | ||
createSecretKey, | ||
generateKey, | ||
generateKeyPair, | ||
generateKeyPairSync, | ||
generateKeySync, | ||
generatePrime, | ||
generatePrimeSync, | ||
getCiphers, | ||
getCurves, | ||
getDiffieHellman, | ||
getFips, | ||
getHashes, | ||
getRandomValues, | ||
hkdf, | ||
hkdfSync, | ||
pbkdf2, | ||
pbkdf2Sync, | ||
randomBytes, | ||
randomFill, | ||
randomFillSync, | ||
randomInt, | ||
randomUUID, | ||
secureHeapUsed, | ||
setEngine, | ||
setFips, | ||
subtle, | ||
timingSafeEqual, | ||
fips, | ||
webcrypto, | ||
}; |
Oops, something went wrong.