MyGit

v23.5.0

nodejs/node

版本发布时间: 2024-12-20 02:57:09

nodejs/node最新发布版本:v23.5.0(2024-12-20 02:57:09)

Notable Changes

WebCryptoAPI Ed25519 and X25519 algorithms are now stable

Following the merge of Curve25519 into the Web Cryptography API Editor's Draft the Ed25519 and X25519 algorithm identifiers are now stable and will no longer emit an ExperimentalWarning upon use.

Contributed by Filip Skokan in #56142.

On-thread hooks are back

This release introduces module.registerHooks() for registering module loader customization hooks that are run for all modules loaded by require(), import and functions returned by createRequire() in the same thread, which makes them easier for CJS monkey-patchers to migrate to.

import assert from 'node:assert';
import { registerHooks, createRequire } from 'node:module';
import { writeFileSync } from 'node:fs';

writeFileSync('./bar.js', 'export const id = 123;', 'utf8');

registerHooks({
  resolve(specifier, context, nextResolve) {
    const replaced = specifier.replace('foo', 'bar');
    return nextResolve(replaced, context);
  },
  load(url, context, nextLoad) {
    const result = nextLoad(url, context);
    return {
      ...result,
      source: result.source.toString().replace('123', '456'),
    };
  },
});

// Checks that it works with require.
const require = createRequire(import.meta.url);
const required = require('./foo.js');  // Redirected by resolve hook to bar.js
assert.strictEqual(required.id, 456);  // Replaced by load hook to 456

// Checks that it works with import.
const imported = await import('./foo.js');  // Redirected by resolve hook to bar.js
assert.strictEqual(imported.id, 456);  // Replaced by load hook to 456

This complements the module.register() hooks - the new hooks fit better internally and cover all corners in the module graph; whereas module.register() previously could not cover require() while it was on-thread, and still cannot cover createRequire() after being moved off-thread.

They are also run in the same thread as the modules being loaded and where the hooks are registered, which means they are easier to debug (no more console.log() getting lost) and do not have the many deadlock issues haunting the module.register() hooks. The new API also takes functions directly so that it's easier for intermediate loader packages to take user options from files that the hooks can't be aware of, like many existing CJS monkey-patchers do.

Contributed by Joyee Cheung in #55698.

Other notable changes

Commits

相关地址:原始地址 下载(tar) 下载(zip)

查看:2024-12-20发行的版本