Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/native-stream-from-web.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changeset/node-24-builtins-and-fs-constants.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changeset/node-24-dev-fs-stubs.md
Original file line number Diff line number Diff line change
@@ -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] <api> is not implemented yet!` when called, the same error as the production polyfill, instead of `(void 0) is not a function`.
6 changes: 6 additions & 0 deletions .changeset/node-24-dev-modules-stubs.md
Original file line number Diff line number Diff line change
@@ -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] <api> 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.
6 changes: 6 additions & 0 deletions .changeset/node-24-stubs-and-process-version.md
Original file line number Diff line number Diff line change
@@ -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] <api> 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.
6 changes: 6 additions & 0 deletions .changeset/stream-promises-pipeline.md
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion .github/actions/install-dependencies/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/hydrogen
lts/jod
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -42,7 +41,7 @@
}
],
"engines": {
"node": ">=18.0.0"
"node": ">=22.0.0"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
const prefixOnlyNames = new Set<string>();
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
Expand All @@ -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:', '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions packages/builder/src/polyfills/crypto/crypto.polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,6 +84,10 @@ export default {
getRandomValues,
randomUUID,
generateKeyPair,
argon2,
argon2Sync,
encapsulate,
decapsulate,
CryptoKey,
KeyObject,
subtle,
Expand Down
8 changes: 4 additions & 4 deletions packages/builder/src/polyfills/fs/context/fs.context.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const {
Dirent,
Stats,
constants,
F_OK,
O_APPEND,
O_CREAT,
O_DIRECTORY,
Expand All @@ -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;

Expand Down
16 changes: 16 additions & 0 deletions packages/builder/src/polyfills/fs/fs.polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -172,6 +184,7 @@ export {
lstat,
lstatSync,
mkdir,
mkdtempDisposableSync,
O_APPEND,
O_CREAT,
O_DIRECTORY,
Expand Down Expand Up @@ -199,6 +212,7 @@ export {
rmdir,
stat,
statSync,
Utf8Stream,
W_OK,
writeFile,
X_OK,
Expand All @@ -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;
Expand All @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
Loading
Loading