diff --git a/.changeset/native-stream-from-web.md b/.changeset/native-stream-from-web.md new file mode 100644 index 00000000..dd681e10 --- /dev/null +++ b/.changeset/native-stream-from-web.md @@ -0,0 +1,5 @@ +--- +'@aziontech/builder': patch +--- + +Stop overriding `Readable.toWeb` and `Readable.fromWeb` of the host `node:stream` in the `stream` polyfill used by `azbundler dev`. The polyfill replaced the native implementations, and its `Readable.fromWeb` never pushed any data (it returned `{ done, value }` from `read()` instead of calling `push()`), so a stream created with it never emitted `data` or `end`. Next.js 16.4+ builds the HTML with `Readable.fromWeb` and `Readable.toWeb`, so the render never finished and ISR pages were never revalidated on the local server. The polyfill versions are now installed only when the host does not provide them, and the `fromWeb` fallback now pushes the data. diff --git a/.changeset/node-24-builtins-and-fs-constants.md b/.changeset/node-24-builtins-and-fs-constants.md new file mode 100644 index 00000000..51822570 --- /dev/null +++ b/.changeset/node-24-builtins-and-fs-constants.md @@ -0,0 +1,5 @@ +--- +'@aziontech/builder': patch +--- + +Support building and running `azbundler dev` with Node.js 24. The esbuild node polyfills plugin now only handles the built-in modules that have an unenv alias, a polyfill or an external entry. Node.js 24 lists `node:test`, `node:test/reporters` and `node:sea` in `builtinModules`, they have nothing to resolve to and the build failed trying to read a file named after them. The `fs` polyfill used by `azbundler dev` now reads `F_OK`, `R_OK`, `W_OK` and `X_OK` from `fs.constants`, because Node.js 24 removed them from the top level of `fs`. The behavior on Node.js 22 does not change. diff --git a/.changeset/node-24-dev-fs-stubs.md b/.changeset/node-24-dev-fs-stubs.md new file mode 100644 index 00000000..43b38331 --- /dev/null +++ b/.changeset/node-24-dev-fs-stubs.md @@ -0,0 +1,5 @@ +--- +'@aziontech/builder': patch +--- + +The `fs` polyfill used by `azbundler dev` now exports `mkdtempDisposableSync` and `Utf8Stream`, the Node.js 24 APIs it does not provide. They throw `[azion] is not implemented yet!` when called, the same error as the production polyfill, instead of `(void 0) is not a function`. diff --git a/.changeset/node-24-dev-modules-stubs.md b/.changeset/node-24-dev-modules-stubs.md new file mode 100644 index 00000000..dc6b691c --- /dev/null +++ b/.changeset/node-24-dev-modules-stubs.md @@ -0,0 +1,6 @@ +--- +'@aziontech/unenv-preset': minor +'@aziontech/builder': patch +--- + +Do not fail the build when the code imports `node:test`, `node:test/reporters` or `node:sea`, the Node.js 24 development modules that have no polyfill. `@aziontech/unenv-preset` now has stubs for them, in the same way as the other APIs the runtime does not provide: they throw `[azion] is not implemented yet!` when called, not when imported, and `sea.isSea()` returns `false`. In `@aziontech/builder`, the built-in modules that exist only with the `node:` prefix (`node:test`, `node:sea`, `node:sqlite`...) are matched only with the prefix, so an npm package named `test`, `sea` or `sqlite` is not taken as a built-in module, and the same modules are handled on Node.js 22 and 24. In the `fs` polyfill, `mkdtempDisposable`, `mkdtempDisposableSync` and `Utf8Stream` now use the ones of the runtime when it has them, and no longer fail when the runtime defines them as getters. diff --git a/.changeset/node-24-stubs-and-process-version.md b/.changeset/node-24-stubs-and-process-version.md new file mode 100644 index 00000000..d0836302 --- /dev/null +++ b/.changeset/node-24-stubs-and-process-version.md @@ -0,0 +1,6 @@ +--- +'@aziontech/unenv-preset': minor +'@aziontech/builder': patch +--- + +Add stubs for the Node.js 24 APIs that unenv does not cover yet. In `@aziontech/unenv-preset`: `perf_hooks.timerify` (returns the function as it is) and `perf_hooks.eventLoopUtilization` (all idle), `url.URLPattern` (the runtime `URLPattern` global when it has one), `v8.startCpuProfile`, `worker_threads.locks` (`navigator.locks` when available) and, in the `fs` polyfill, `mkdtempDisposable`, `mkdtempDisposableSync` and `Utf8Stream`. The ones that cannot work throw `[azion] is not implemented yet!` when called, not when imported. `process.versions.node` in the `process` polyfill changes from `18.3.1` to `22.23.4`, so libraries that check the Node.js version no longer take the Node.js 18 path. In `@aziontech/builder`, the `crypto` polyfill used by `azbundler dev` now also exports `argon2`, `argon2Sync`, `encapsulate` and `decapsulate`, which are `undefined` on a host without them. diff --git a/.changeset/stream-promises-pipeline.md b/.changeset/stream-promises-pipeline.md new file mode 100644 index 00000000..47c39113 --- /dev/null +++ b/.changeset/stream-promises-pipeline.md @@ -0,0 +1,6 @@ +--- +'@aziontech/unenv-preset': patch +'@aziontech/builder': patch +--- + +Fix `[unenv] stream.promises.pipeline is not implemented yet!` when running a Next.js server action. Next.js reads the body of a server action with `pipeline` from `node:stream/promises`, and unenv only has stubs for it. In `@aziontech/unenv-preset`, `node:stream/promises` now uses the `promises` of the runtime `node:stream` when it has them, and otherwise builds `pipeline` and `finished` on the callback versions (the `end` option is not supported). In `@aziontech/builder`, the stream context used by `azbundler dev` now passes `pipeline`, `finished` and `promises` of the host `node:stream`, and the `node:stream` and `node:stream/promises` polyfills export them. `azbundler dev` needs a version of `@aziontech/bundler` that uses this `@aziontech/builder`. diff --git a/.github/actions/install-dependencies/action.yml b/.github/actions/install-dependencies/action.yml index 2a1d0054..2b8b38e0 100644 --- a/.github/actions/install-dependencies/action.yml +++ b/.github/actions/install-dependencies/action.yml @@ -3,7 +3,7 @@ description: 'Install dependencies, fetching from cache when possible' inputs: node-version: description: the version of Node.js to install - default: 22.21.1 + default: 24 runs: using: 'composite' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5da11845..91529acd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,15 +29,21 @@ jobs: run: pnpm compile test_unit: - name: Unit Tests + name: Unit Tests (Node ${{ matrix.node-version }}) needs: lint_and_build runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: [22, 24] steps: - name: Checkout code uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 - name: Install Dependencies uses: ./.github/actions/install-dependencies + with: + node-version: ${{ matrix.node-version }} - name: Build run: pnpm compile diff --git a/.nvmrc b/.nvmrc index c32828c2..deed13c0 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -lts/hydrogen \ No newline at end of file +lts/jod diff --git a/package.json b/package.json index 3b32e649..9c7dd4eb 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "format:check": "pnpm -r --if-present prettier:check", "test": "pnpm -r --if-present --workspace-concurrency=1 test", "test:e2e": "npx jest ./tests/e2e/*", - "download:cli": "node ./cli/scripts/download-cli.mjs", "build": "pnpm -r --if-present compile" }, "keywords": [ @@ -42,7 +41,7 @@ } ], "engines": { - "node": ">=18.0.0" + "node": ">=22.0.0" }, "devDependencies": { "@changesets/changelog-github": "^0.5.2", diff --git a/packages/builder/src/bundlers/esbuild/plugins/node-polyfills/node-polyfills.test.ts b/packages/builder/src/bundlers/esbuild/plugins/node-polyfills/node-polyfills.test.ts new file mode 100644 index 00000000..b73391be --- /dev/null +++ b/packages/builder/src/bundlers/esbuild/plugins/node-polyfills/node-polyfills.test.ts @@ -0,0 +1,70 @@ +import { describe, expect, it } from '@jest/globals'; +import { builtinModules } from 'node:module'; + +import { buildNodeModulesRegExp } from './node-polyfills'; + +describe('buildNodeModulesRegExp', () => { + it('matches the modules that have a polyfill, with and without the node: prefix', () => { + const regexp = buildNodeModulesRegExp(['fs', 'stream', 'crypto', 'async_hooks', 'util']); + + for (const name of ['fs', 'stream', 'crypto', 'async_hooks', 'util']) { + expect(regexp.test(name)).toBe(true); + expect(regexp.test(`node:${name}`)).toBe(true); + } + }); + + it('does not match the modules without alias, polyfill or external entry', () => { + const regexp = buildNodeModulesRegExp(['fs', 'node:unknown', 'unknown']); + + expect(regexp.test('node:fs')).toBe(true); + expect(regexp.test('node:unknown')).toBe(false); + expect(regexp.test('unknown')).toBe(false); + }); + + it('matches the prefix-only modules only with the node: prefix', () => { + // Node.js 24 lists these with the node: prefix, unenv resolves them to a stub + const regexp = buildNodeModulesRegExp(['fs', 'node:sqlite', 'node:test', 'node:test/reporters', 'node:sea']); + + for (const name of ['sqlite', 'test', 'test/reporters', 'sea']) { + expect(regexp.test(`node:${name}`)).toBe(true); + // could be an npm package with the same name + expect(regexp.test(name)).toBe(false); + } + expect(regexp.test('fs')).toBe(true); + }); + + it('does not match unrelated names', () => { + const regexp = buildNodeModulesRegExp(['fs']); + + expect(regexp.test('node:fs-extra')).toBe(false); + expect(regexp.test('my-fs')).toBe(false); + }); + + it('does not match anything when no module can be resolved', () => { + const regexp = buildNodeModulesRegExp(['node:unknown']); + + expect(regexp.test('node:unknown')).toBe(false); + expect(regexp.test('')).toBe(false); + }); + + it('handles the same modules whatever the Node.js version is', () => { + const regexp = buildNodeModulesRegExp(); + + for (const name of ['node:test', 'node:test/reporters', 'node:sea', 'node:sqlite']) { + expect(regexp.test(name)).toBe(true); + } + for (const name of ['test', 'sea', 'sqlite']) { + expect(regexp.test(name)).toBe(false); + } + }); + + it('matches every built-in module of the running Node.js that can be resolved', () => { + const regexp = buildNodeModulesRegExp(); + + for (const name of ['fs', 'path', 'stream', 'crypto', 'url', 'v8', 'worker_threads', 'perf_hooks']) { + expect(builtinModules).toContain(name); + expect(regexp.test(name)).toBe(true); + expect(regexp.test(`node:${name}`)).toBe(true); + } + }); +}); diff --git a/packages/builder/src/bundlers/esbuild/plugins/node-polyfills/node-polyfills.ts b/packages/builder/src/bundlers/esbuild/plugins/node-polyfills/node-polyfills.ts index ee39f20f..16e25015 100644 --- a/packages/builder/src/bundlers/esbuild/plugins/node-polyfills/node-polyfills.ts +++ b/packages/builder/src/bundlers/esbuild/plugins/node-polyfills/node-polyfills.ts @@ -96,6 +96,55 @@ function handleAliasUnenv(build: PluginBuild) { }); } +/** + * Built-in modules handled by default: the ones of the Node.js that runs the build plus the ones the + * alias map only knows with the `node:` prefix (`node:sqlite`, `node:test`, `node:sea`...). + * `builtinModules` changes between Node.js versions, the alias map does not, so the same modules are + * handled on any Node.js version. + */ +const DEFAULT_NODE_MODULES: readonly string[] = [ + ...builtinModules, + ...Object.keys(alias).filter((name) => name.startsWith('node:')), +]; + +/** + * Builds the regexp that matches the Node.js built-in modules handled by this plugin. + * + * A module is only handled when there is something to resolve it to (an unenv alias, a polyfill or an + * external entry). The others are left to esbuild instead of failing in the plugin trying to read a + * file that does not exist. + * + * Modules that exist only with the `node:` prefix (`node:test`, `node:sea`, `node:sqlite`...) only match + * with the prefix, so an npm package with the same name (`test`, `sea`, `sqlite`) is not taken as a + * built-in module. + * @param {readonly string[]} modules Built-in module names, as listed in `builtinModules` + * @returns {RegExp} Regexp that matches the modules + */ +export function buildNodeModulesRegExp(modules: readonly string[] = DEFAULT_NODE_MODULES): RegExp { + const hasResolution = (name: string): boolean => + Boolean(alias[name] ?? alias[`node:${name}`]) || + polyfill.some((p) => p.includes(`:${name}:`)) || + external.some((ext) => ext === name || ext === `node:${name}`); + + const withoutPrefix = (name: string): string => name.replace(/^node:/, ''); + const bareNames = new Set(modules.filter((name) => !name.startsWith('node:'))); + + const names = new Set(); + const prefixOnlyNames = new Set(); + for (const module of modules) { + const name = withoutPrefix(module); + if (!hasResolution(name)) continue; + (bareNames.has(name) ? names : prefixOnlyNames).add(name); + } + + const patterns = []; + if (names.size) patterns.push(`(node:)?(${[...names].join('|')})`); + if (prefixOnlyNames.size) patterns.push(`node:(${[...prefixOnlyNames].join('|')})`); + + // `(?!)` never matches, for when there is no module to handle + return new RegExp(`^(?:${patterns.join('|') || '(?!)'})$`); +} + /** * Node built in modules * @param {*} build Build object @@ -111,7 +160,7 @@ function nodeBuiltInModules( ) { const IMPORTED_NODE_BUILT_IN_NAMESPACE = 'node-built-in-modules'; - const NODEJS_MODULES_RE = new RegExp(`^(node:)?(${builtinModules.join('|')})$`); + const NODEJS_MODULES_RE = buildNodeModulesRegExp(); build.onResolve({ filter: NODEJS_MODULES_RE }, (args) => { const nameModuleWithoutNode = args.path.replace('node:', ''); diff --git a/packages/builder/src/polyfills/crypto/context/crypto.context.js b/packages/builder/src/polyfills/crypto/context/crypto.context.js index eab08697..cda75520 100644 --- a/packages/builder/src/polyfills/crypto/context/crypto.context.js +++ b/packages/builder/src/polyfills/crypto/context/crypto.context.js @@ -35,6 +35,11 @@ export var { randomFillSync } = crypto; export var { getRandomValues } = crypto; export var { randomUUID } = crypto; export var { generateKeyPair } = crypto; +// Node.js 24, undefined on a host that does not have them +export var { argon2 } = crypto; +export var { argon2Sync } = crypto; +export var { encapsulate } = crypto; +export var { decapsulate } = crypto; // Export Web Crypto API from globalThis.crypto (edge runtime) export var webcrypto = (typeof globalThis !== 'undefined' && globalThis.crypto) || crypto.webcrypto; diff --git a/packages/builder/src/polyfills/crypto/crypto.polyfills.js b/packages/builder/src/polyfills/crypto/crypto.polyfills.js index 2d48db4b..0e192e99 100644 --- a/packages/builder/src/polyfills/crypto/crypto.polyfills.js +++ b/packages/builder/src/polyfills/crypto/crypto.polyfills.js @@ -39,6 +39,11 @@ export var { randomFillSync } = CRYPTO_CONTEXT.cryptoContext; export var { getRandomValues } = CRYPTO_CONTEXT.cryptoContext; export var { randomUUID } = CRYPTO_CONTEXT.cryptoContext; export var { generateKeyPair } = CRYPTO_CONTEXT.cryptoContext; +// Node.js 24, undefined on a host that does not have them +export var { argon2 } = CRYPTO_CONTEXT.cryptoContext; +export var { argon2Sync } = CRYPTO_CONTEXT.cryptoContext; +export var { encapsulate } = CRYPTO_CONTEXT.cryptoContext; +export var { decapsulate } = CRYPTO_CONTEXT.cryptoContext; export var { CryptoKey } = CRYPTO_CONTEXT.cryptoContext; export var { KeyObject } = CRYPTO_CONTEXT.cryptoContext; export var { webcrypto } = CRYPTO_CONTEXT.cryptoContext; @@ -79,6 +84,10 @@ export default { getRandomValues, randomUUID, generateKeyPair, + argon2, + argon2Sync, + encapsulate, + decapsulate, CryptoKey, KeyObject, subtle, diff --git a/packages/builder/src/polyfills/fs/context/fs.context.js b/packages/builder/src/polyfills/fs/context/fs.context.js index ef60af53..ef32c093 100644 --- a/packages/builder/src/polyfills/fs/context/fs.context.js +++ b/packages/builder/src/polyfills/fs/context/fs.context.js @@ -27,7 +27,6 @@ export const { Dirent, Stats, constants, - F_OK, O_APPEND, O_CREAT, O_DIRECTORY, @@ -42,11 +41,12 @@ export const { O_SYNC, O_TRUNC, O_WRONLY, - R_OK, - W_OK, - X_OK, } = fs; +// Node.js 24 removed F_OK, R_OK, W_OK and X_OK from the top level of `fs` (deprecated since 20, DEP0176), they are +// only in `fs.constants`. Reading them from `fs` also logs a deprecation warning on Node.js 22. +export const { F_OK, R_OK, W_OK, X_OK } = constants; + export { promises } from 'fs'; localFs.promises = fs.promises; diff --git a/packages/builder/src/polyfills/fs/fs.polyfills.js b/packages/builder/src/polyfills/fs/fs.polyfills.js index 1f171332..71bf6962 100644 --- a/packages/builder/src/polyfills/fs/fs.polyfills.js +++ b/packages/builder/src/polyfills/fs/fs.polyfills.js @@ -161,6 +161,18 @@ const R_OK = 4; const W_OK = 2; const X_OK = 1; +// Node.js 24 APIs that the fs of azbundler dev does not provide. They fail when called, not when imported, with +// the same message the production polyfill (@aziontech/unenv-preset) uses. +function mkdtempDisposableSync() { + throw new Error('[azion] fs.mkdtempDisposableSync is not implemented yet!'); +} + +class Utf8Stream { + constructor() { + throw new Error('[azion] fs.Utf8Stream is not implemented yet!'); + } +} + export { close, closeSync, @@ -172,6 +184,7 @@ export { lstat, lstatSync, mkdir, + mkdtempDisposableSync, O_APPEND, O_CREAT, O_DIRECTORY, @@ -199,6 +212,7 @@ export { rmdir, stat, statSync, + Utf8Stream, W_OK, writeFile, X_OK, @@ -220,6 +234,7 @@ localFs.readFileSync = readFileSync; localFs.readdir = readdir; localFs.readdirSync = readdirSync; localFs.mkdir = mkdir; +localFs.mkdtempDisposableSync = mkdtempDisposableSync; localFs.rmdir = rmdir; localFs.copyFile = copyFile; localFs.cp = cp; @@ -245,5 +260,6 @@ localFs.O_WRONLY = O_WRONLY; localFs.R_OK = R_OK; localFs.W_OK = W_OK; localFs.X_OK = X_OK; +localFs.Utf8Stream = Utf8Stream; export default localFs; diff --git a/packages/builder/src/polyfills/stream/context/stream.context.js b/packages/builder/src/polyfills/stream/context/stream.context.js index 700cd5a7..a979d0ff 100644 --- a/packages/builder/src/polyfills/stream/context/stream.context.js +++ b/packages/builder/src/polyfills/stream/context/stream.context.js @@ -3,7 +3,12 @@ import stream from 'node:stream'; const localStream = {}; export const { Duplex, Writable, Readable, Transform, PassThrough, Stream } = stream; export const { prototype } = stream; +// used by the `node:stream/promises` polyfill, Next.js uses `pipeline` to read the body of a server action +export const { pipeline, finished, promises } = stream; +localStream.pipeline = pipeline; +localStream.finished = finished; +localStream.promises = promises; localStream.Duplex = Duplex; localStream.Writable = Writable; localStream.Readable = Readable; diff --git a/packages/builder/src/polyfills/stream/promises/promises.polyfills.js b/packages/builder/src/polyfills/stream/promises/promises.polyfills.js new file mode 100644 index 00000000..7528ba1b --- /dev/null +++ b/packages/builder/src/polyfills/stream/promises/promises.polyfills.js @@ -0,0 +1,15 @@ +/* eslint-disable */ +/** + * STREAM_CONTEXT is defined in runtime.env.js for use on the local server + */ + +// `node:stream/promises` of the host Node.js. A bundler that does not pass `promises` in STREAM_CONTEXT +// gets a clear error when it is called, instead of the unenv stub one. +const notAvailable = (name) => () => { + throw new Error(`[azion] stream/promises.${name} is not available, update @aziontech/bundler`); +}; + +export const pipeline = STREAM_CONTEXT.promises?.pipeline ?? notAvailable('pipeline'); +export const finished = STREAM_CONTEXT.promises?.finished ?? notAvailable('finished'); + +export default { pipeline, finished }; diff --git a/packages/builder/src/polyfills/stream/stream.polyfills.js b/packages/builder/src/polyfills/stream/stream.polyfills.js index a6e836da..63f4318c 100644 --- a/packages/builder/src/polyfills/stream/stream.polyfills.js +++ b/packages/builder/src/polyfills/stream/stream.polyfills.js @@ -5,8 +5,11 @@ const localStream = {}; const { Duplex, Writable, Readable, Transform, PassThrough, Stream } = STREAM_CONTEXT; -export const { prototype } = STREAM_CONTEXT; +export const { prototype, pipeline, finished, promises } = STREAM_CONTEXT; +localStream.pipeline = pipeline; +localStream.finished = finished; +localStream.promises = promises; localStream.Duplex = Duplex; localStream.Writable = Writable; localStream.Readable = Readable; @@ -15,7 +18,9 @@ localStream.PassThrough = PassThrough; localStream.Stream = Stream; localStream.prototype = prototype; -Readable.toWeb = function (readable) { +// STREAM_CONTEXT is the real `node:stream` of the host Node.js, which already implements `Readable.toWeb` and +// `Readable.fromWeb`. These fallbacks are only installed when the host does not have them, see below. +const toWebFallback = function (readable) { let onData, onEnd, onError; let closed = false; const stream = new ReadableStream({ @@ -59,22 +64,37 @@ Readable.toWeb = function (readable) { return stream; }; -Readable.fromWeb = function (webStream) { +const fromWebFallback = function (webStream) { const reader = webStream.getReader(); return new Readable({ - async read(size) { - const { done, value } = await reader.read(); - if (done) { - return { done: true }; - } - return { done: false, value }; + read() { + // A Node.js Readable gets its data from `push()`, the return value of `read()` is ignored + reader.read().then( + ({ done, value }) => { + if (done) { + this.push(null); + } else { + this.push(value); + } + }, + (error) => this.destroy(error), + ); }, - async destroy(error) { - await reader.cancel(error); + destroy(error, callback) { + reader.cancel(error).then( + () => callback(error), + () => callback(error), + ); }, }); }; +// Do not replace the native implementations: the previous `Readable.fromWeb` never pushed any data, so a stream +// created with it never emitted `data` or `end`. Next.js 16.4+ builds the HTML with `Readable.fromWeb`/`toWeb` +// (`stream-ops.node.js`), so ISR revalidation hung on `azbundler dev`. +if (typeof Readable.toWeb !== 'function') Readable.toWeb = toWebFallback; +if (typeof Readable.fromWeb !== 'function') Readable.fromWeb = fromWebFallback; + Writable.fromWeb = function (webStream) { const writer = webStream.getWriter(); diff --git a/packages/presets/src/presets/next/utils/vercel/vercel.test.js b/packages/presets/src/presets/next/utils/vercel/vercel.test.js index 4b7783f0..1d21d6a7 100644 --- a/packages/presets/src/presets/next/utils/vercel/vercel.test.js +++ b/packages/presets/src/presets/next/utils/vercel/vercel.test.js @@ -3,31 +3,31 @@ import { jest } from '@jest/globals'; import child_process from 'child_process'; import fs from 'fs'; import mockFs from 'mock-fs'; +import os from 'os'; +import path from 'path'; import VercelUtils from './index'; const { deleteTelemetryFiles, createVercelProjectConfig, runVercelBuild } = VercelUtils; describe('Vercel Utils', () => { it('Should delete telemtry files', () => { - mockFs({ - '.vercel': { - output: { - static: { - _next: { - __private: { - 'next-telemetry.js': 'telemetry', - }, - }, - }, - }, - }, - }); - - deleteTelemetryFiles(); - - expect(fs.existsSync('.vercel/output/static/_next/__private')).toBe(false); - - mockFs.restore(); + // A real temporary directory: mock-fs does not intercept `fs.rmSync` on Node.js 24, where it is native + const originalCwd = process.cwd(); + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vercel-utils-')); + const privateDir = path.join(tmpDir, '.vercel', 'output', 'static', '_next', '__private'); + + try { + fs.mkdirSync(privateDir, { recursive: true }); + fs.writeFileSync(path.join(privateDir, 'next-telemetry.js'), 'telemetry'); + process.chdir(tmpDir); + + deleteTelemetryFiles(); + + expect(fs.existsSync('.vercel/output/static/_next/__private')).toBe(false); + } finally { + process.chdir(originalCwd); + fs.rmSync(tmpDir, { recursive: true, force: true }); + } }); it('Should create vercel project config', () => { diff --git a/packages/unenv-preset/src/index.ts b/packages/unenv-preset/src/index.ts index cfa89e51..a539d18f 100644 --- a/packages/unenv-preset/src/index.ts +++ b/packages/unenv-preset/src/index.ts @@ -24,9 +24,18 @@ export default { buffer: `${polyfillsPath}/node/buffer.js`, https: `${polyfillsPath}/node/https.js`, module: `${polyfillsPath}/node/module.js`, + // Only available with the node: prefix + 'node:sea': `${polyfillsPath}/node/sea.js`, + 'node:test': `${polyfillsPath}/node/test.js`, + 'node:test/reporters': `${polyfillsPath}/node/test-reporters.js`, + perf_hooks: `${polyfillsPath}/node/perf_hooks.js`, + 'stream/promises': `${polyfillsPath}/node/stream.promises.js`, string_decoder: 'string_decoder/', timers: 'timers-browserify', + url: `${polyfillsPath}/node/url.js`, util: `${polyfillsPath}/node/util.js`, + v8: `${polyfillsPath}/node/v8.js`, + worker_threads: `${polyfillsPath}/node/worker_threads.js`, zlib: `${polyfillsPath}/node/zlib.js`, }, external: ['node:async_hooks', 'node:fs/promises', 'node:stream', 'node:crypto'], @@ -35,6 +44,7 @@ export default { 'aziondev:fs:/fs/fs.polyfills.js', 'aziondev:fs/promises:/fs/promises/promises.polyfills.js', 'aziondev:stream:/stream/stream.polyfills.js', + 'aziondev:stream/promises:/stream/promises/promises.polyfills.js', 'aziondev:crypto:/crypto/crypto.polyfills.js', `azionprd:fs:/fs.js`, ], diff --git a/packages/unenv-preset/src/polyfills/node/_internal/not-implemented.js b/packages/unenv-preset/src/polyfills/node/_internal/not-implemented.js new file mode 100644 index 00000000..67c3b753 --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/_internal/not-implemented.js @@ -0,0 +1,26 @@ +/** + * Stubs for Node.js APIs that the Azion runtime does not provide and unenv does not cover yet. + * They fail when called, not when imported, so a bundle that only references them still loads. + */ + +/** + * @param {string} name Name of the API, used in the error message + * @returns {Function} Function that throws when called + */ +export function notImplemented(name) { + return function () { + throw new Error(`[azion] ${name} is not implemented yet!`); + }; +} + +/** + * @param {string} name Name of the API, used in the error message + * @returns {Function} Class that throws when instantiated + */ +export function notImplementedClass(name) { + return class { + constructor() { + throw new Error(`[azion] ${name} is not implemented yet!`); + } + }; +} diff --git a/packages/unenv-preset/src/polyfills/node/fs.js b/packages/unenv-preset/src/polyfills/node/fs.js index 950bcf67..2c407f13 100644 --- a/packages/unenv-preset/src/polyfills/node/fs.js +++ b/packages/unenv-preset/src/polyfills/node/fs.js @@ -1,5 +1,6 @@ import { Buffer } from 'buffer'; import bPath from 'path'; +import { notImplemented, notImplementedClass } from './_internal/not-implemented.js'; /* eslint-disable */ @@ -403,9 +404,30 @@ fsPolyfill.readFileSync = readFileSync; fsPolyfill.readdirSync = readdirSync; fsPolyfill.existsSync = existsSync; +// Node.js 24 APIs that unenv does not cover yet. They are stubs, unless the runtime already has them. +// defineProperty because assigning fails when the runtime defines them as getters (as Node.js 24 does). +const mkdtempDisposable = SRC_NODE_FS.mkdtempDisposable ?? notImplemented('fs.mkdtempDisposable'); +const mkdtempDisposableSync = SRC_NODE_FS.mkdtempDisposableSync ?? notImplemented('fs.mkdtempDisposableSync'); +const Utf8Stream = SRC_NODE_FS.Utf8Stream ?? notImplementedClass('fs.Utf8Stream'); +for (const [name, value] of Object.entries({ mkdtempDisposable, mkdtempDisposableSync, Utf8Stream })) { + Object.defineProperty(fsPolyfill, name, { value, writable: true, enumerable: true, configurable: true }); +} + export default fsPolyfill; -export { close, closeSync, existsSync, statSync as lstatSync, openSync, readdirSync, readFileSync, statSync }; +export { + close, + closeSync, + existsSync, + statSync as lstatSync, + mkdtempDisposable, + mkdtempDisposableSync, + openSync, + readdirSync, + readFileSync, + statSync, + Utf8Stream, +}; export const { access, diff --git a/packages/unenv-preset/src/polyfills/node/globals/process.cjs b/packages/unenv-preset/src/polyfills/node/globals/process.cjs index 7be5f3ed..38664d44 100644 --- a/packages/unenv-preset/src/polyfills/node/globals/process.cjs +++ b/packages/unenv-preset/src/polyfills/node/globals/process.cjs @@ -177,7 +177,7 @@ Object.defineProperty(processShim, 'env', { processShim.argv = []; processShim.version = ''; // empty string to avoid regexp issues -processShim.versions = { node: '18.3.1' }; +processShim.versions = { node: '22.23.4' }; function noop() {} diff --git a/packages/unenv-preset/src/polyfills/node/perf_hooks.js b/packages/unenv-preset/src/polyfills/node/perf_hooks.js new file mode 100644 index 00000000..8503fb28 --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/perf_hooks.js @@ -0,0 +1,20 @@ +import * as originalPerfHooks from 'unenv/node/perf_hooks'; + +export * from 'unenv/node/perf_hooks'; + +// Node.js 24: `timerify` wraps a function to measure it. There is no event loop to measure, so the function is +// returned as it is and nothing is recorded. +export function timerify(fn) { + return fn; +} + +// Node.js 24: with no event loop activity to measure, everything is idle. +export function eventLoopUtilization() { + return { idle: 0, active: 0, utilization: 0 }; +} + +export default { + ...originalPerfHooks.default, + timerify, + eventLoopUtilization, +}; diff --git a/packages/unenv-preset/src/polyfills/node/sea.js b/packages/unenv-preset/src/polyfills/node/sea.js new file mode 100644 index 00000000..777e1def --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/sea.js @@ -0,0 +1,13 @@ +import { notImplemented } from './_internal/not-implemented.js'; + +/** + * `node:sea` is for Node.js single executable applications, which the Azion runtime is not. + * `isSea()` answers `false`, the other APIs throw when called. + */ + +export const isSea = () => false; +export const getAsset = notImplemented('sea.getAsset'); +export const getAssetAsBlob = notImplemented('sea.getAssetAsBlob'); +export const getRawAsset = notImplemented('sea.getRawAsset'); + +export default { isSea, getAsset, getAssetAsBlob, getRawAsset }; diff --git a/packages/unenv-preset/src/polyfills/node/stream.promises.js b/packages/unenv-preset/src/polyfills/node/stream.promises.js new file mode 100644 index 00000000..63c771da --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/stream.promises.js @@ -0,0 +1,85 @@ +import * as nodeStream from 'node:stream'; + +/** + * `node:stream/promises`. unenv only has stubs for it (`pipeline` and `finished` throw), but Next.js uses + * `pipeline` to read the body of a server action. + * + * `node:stream` is provided by the runtime, so its own `promises` is used when it has one. Otherwise + * `pipeline` and `finished` are built on the callback versions of `node:stream`. + */ +const nativePromises = nodeStream.promises ?? nodeStream.default?.promises; + +/** + * The last argument of `pipeline` can be an options object (`{ signal, end }`) instead of a stream. + * @param {unknown} arg Last argument of `pipeline` + * @returns {boolean} Whether it is the options object + */ +function isOptions(arg) { + return ( + arg !== null && + typeof arg === 'object' && + !Array.isArray(arg) && + typeof arg.pipe !== 'function' && + typeof arg.read !== 'function' && + typeof arg.write !== 'function' && + typeof arg.getReader !== 'function' && + !(Symbol.asyncIterator in arg) && + !(Symbol.iterator in arg) + ); +} + +/** + * Fallback for runtimes where `node:stream` has no `promises`. The `end` option is not supported. + * @param {...unknown} args Streams, with an optional options object at the end + * @returns {Promise} Resolves when the pipeline is done, rejects with the first error + */ +function pipelineFallback(...args) { + return new Promise((resolve, reject) => { + let streams = args; + let options; + if (streams.length > 1 && isOptions(streams[streams.length - 1])) { + options = streams[streams.length - 1]; + streams = streams.slice(0, -1); + } + if (streams.length === 1 && Array.isArray(streams[0])) { + streams = streams[0]; + } + + const signal = options?.signal; + const onAbort = () => { + const error = new Error('The operation was aborted'); + error.name = 'AbortError'; + error.code = 'ABORT_ERR'; + streams.forEach((stream) => stream?.destroy?.(error)); + }; + + nodeStream.pipeline(...streams, (error, value) => { + signal?.removeEventListener('abort', onAbort); + if (error) reject(error); + else resolve(value); + }); + + if (signal?.aborted) onAbort(); + else signal?.addEventListener('abort', onAbort, { once: true }); + }); +} + +/** + * Fallback for runtimes where `node:stream` has no `promises`. + * @param {unknown} stream Stream to wait for + * @param {object} [options] Options of `stream.finished` + * @returns {Promise} Resolves when the stream is finished, rejects on error + */ +function finishedFallback(stream, options) { + return new Promise((resolve, reject) => { + nodeStream.finished(stream, options ?? {}, (error) => { + if (error) reject(error); + else resolve(); + }); + }); +} + +export const pipeline = nativePromises?.pipeline ?? pipelineFallback; +export const finished = nativePromises?.finished ?? finishedFallback; + +export default { finished, pipeline }; diff --git a/packages/unenv-preset/src/polyfills/node/test-reporters.js b/packages/unenv-preset/src/polyfills/node/test-reporters.js new file mode 100644 index 00000000..7a93948c --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/test-reporters.js @@ -0,0 +1,14 @@ +import { notImplemented } from './_internal/not-implemented.js'; + +/** + * `node:test/reporters` belongs to the Node.js test runner, a development tool that does not run in + * the Azion runtime. This stub only lets a bundle that imports it be built. Calling any reporter throws. + */ + +export const dot = notImplemented('test/reporters.dot'); +export const junit = notImplemented('test/reporters.junit'); +export const lcov = notImplemented('test/reporters.lcov'); +export const spec = notImplemented('test/reporters.spec'); +export const tap = notImplemented('test/reporters.tap'); + +export default { dot, junit, lcov, spec, tap }; diff --git a/packages/unenv-preset/src/polyfills/node/test.js b/packages/unenv-preset/src/polyfills/node/test.js new file mode 100644 index 00000000..e9a0eb15 --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/test.js @@ -0,0 +1,47 @@ +import { notImplemented } from './_internal/not-implemented.js'; + +/** + * `node:test` is the Node.js test runner, a development tool that does not run in the Azion runtime. + * This stub only lets a bundle that imports it be built. Calling any API throws. + */ + +export const describe = notImplemented('test.describe'); +export const it = notImplemented('test.it'); +export const suite = notImplemented('test.suite'); +export const before = notImplemented('test.before'); +export const after = notImplemented('test.after'); +export const beforeEach = notImplemented('test.beforeEach'); +export const afterEach = notImplemented('test.afterEach'); +export const run = notImplemented('test.run'); + +export const mock = { + fn: notImplemented('test.mock.fn'), + method: notImplemented('test.mock.method'), + getter: notImplemented('test.mock.getter'), + setter: notImplemented('test.mock.setter'), + module: notImplemented('test.mock.module'), + reset: notImplemented('test.mock.reset'), + restoreAll: notImplemented('test.mock.restoreAll'), +}; + +const test = notImplemented('test'); + +Object.assign(test, { + test, + describe, + it, + suite, + before, + after, + beforeEach, + afterEach, + run, + mock, + skip: notImplemented('test.skip'), + todo: notImplemented('test.todo'), + only: notImplemented('test.only'), +}); + +export { test }; + +export default test; diff --git a/packages/unenv-preset/src/polyfills/node/url.js b/packages/unenv-preset/src/polyfills/node/url.js new file mode 100644 index 00000000..c74d1629 --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/url.js @@ -0,0 +1,13 @@ +import * as originalUrl from 'unenv/node/url'; +import { notImplementedClass } from './_internal/not-implemented.js'; + +export * from 'unenv/node/url'; + +// Node.js 24: `url.URLPattern` is the same class as the `URLPattern` global, use the one of the runtime when it has it +export const URLPattern = + typeof globalThis.URLPattern === 'function' ? globalThis.URLPattern : notImplementedClass('url.URLPattern'); + +export default { + ...originalUrl.default, + URLPattern, +}; diff --git a/packages/unenv-preset/src/polyfills/node/v8.js b/packages/unenv-preset/src/polyfills/node/v8.js new file mode 100644 index 00000000..3ee308c2 --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/v8.js @@ -0,0 +1,12 @@ +import * as originalV8 from 'unenv/node/v8'; +import { notImplemented } from './_internal/not-implemented.js'; + +export * from 'unenv/node/v8'; + +// Node.js 24 +export const startCpuProfile = notImplemented('v8.startCpuProfile'); + +export default { + ...originalV8.default, + startCpuProfile, +}; diff --git a/packages/unenv-preset/src/polyfills/node/worker_threads.js b/packages/unenv-preset/src/polyfills/node/worker_threads.js new file mode 100644 index 00000000..98b999d2 --- /dev/null +++ b/packages/unenv-preset/src/polyfills/node/worker_threads.js @@ -0,0 +1,15 @@ +import * as originalWorkerThreads from 'unenv/node/worker_threads'; +import { notImplemented } from './_internal/not-implemented.js'; + +export * from 'unenv/node/worker_threads'; + +// Node.js 24: `worker_threads.locks` is the same LockManager as `navigator.locks` +export const locks = globalThis.navigator?.locks ?? { + request: notImplemented('worker_threads.locks.request'), + query: notImplemented('worker_threads.locks.query'), +}; + +export default { + ...originalWorkerThreads.default, + locks, +}; diff --git a/scripts/code-review.mjs b/scripts/code-review.mjs deleted file mode 100644 index 06261601..00000000 --- a/scripts/code-review.mjs +++ /dev/null @@ -1,93 +0,0 @@ -import * as github from '@actions/github'; -import { chat } from '../packages/ai/dist/index.mjs'; - -async function runCodeReview() { - const octokit = github.getOctokit(process.env.GITHUB_TOKEN); - const context = github.context; - - try { - const { data: pullRequest } = await octokit.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: context.payload.pull_request.number, - }); - - const { data: files } = await octokit.rest.pulls.listFiles({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: context.payload.pull_request.number, - }); - - let fullContent = ''; - for (const file of files) { - if (file.status !== 'removed') { - const { data: content } = await octokit.rest.repos.getContent({ - owner: context.repo.owner, - repo: context.repo.repo, - path: file.filename, - ref: pullRequest.head.sha, - }); - - const fileContent = Buffer.from(content.content, 'base64').toString('utf-8'); - fullContent += `File: ${file.filename}\n\n${fileContent}\n\n`; - } - } - - const prompt = `Analyze the following pull request and provide a summary of what it implements, including good practices, possible problems, and suggestions for improvement: - -${fullContent} - -Provide your analysis in Markdown format, starting with a general summary of the pull request.`; - - const { data: response, error } = await chat( - { - messages: [{ role: 'user', content: prompt }], - }, - { debug: true }, - ); - - if (response) { - console.log('AI response received'); - - const logoUrl = 'https://avatars.githubusercontent.com/u/6660972?s=200&v=4'; - const logoSize = 14; // Logo size reduced to 14 pixels - const footer = ` -
- - Powered by - Azion Logo - Azion AI - -
`; - - const commentBody = response.choices[0].message.content + '\n\n---\n' + footer; - - try { - await octokit.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.payload.pull_request.number, - body: commentBody, - }); - console.log('Comment successfully created on the pull request'); - } catch (commentError) { - console.error('Error creating comment:', commentError.message); - console.error('Error details:', JSON.stringify(commentError, null, 2)); - } - } else { - console.error('Code review failed:', error); - } - } catch (error) { - console.error('Error during code review execution:', error.message); - console.error('Error details:', JSON.stringify(error, null, 2)); - } -} - -(async () => { - try { - await runCodeReview(); - } catch (error) { - console.error(error); - process.exit(1); - } -})();