@lenne.tech/nest-server 12.8.2 → 12.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. package/.claude/rules/architecture.md +14 -4
  2. package/.claude/rules/configurable-features.md +1 -0
  3. package/.claude/rules/package-management.md +9 -7
  4. package/.claude/rules/testing.md +3 -3
  5. package/FRAMEWORK-API.md +2 -1
  6. package/dist/core/common/interfaces/server-options.interface.d.ts +1 -0
  7. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.d.ts +1 -1
  8. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js +7 -0
  9. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js.map +1 -1
  10. package/dist/core/modules/migrate/helpers/ts-compiler-hooks.mjs +4 -55
  11. package/dist/core/modules/migrate/helpers/ts-compiler-hooks.mjs.map +1 -1
  12. package/dist/core/modules/migrate/helpers/ts-compiler.cjs +100 -5
  13. package/dist/core/modules/migrate/helpers/ts-compiler.cjs.map +1 -1
  14. package/dist/core/modules/migrate/helpers/ts-compiler.d.cts +12 -1
  15. package/docs/security-overrides.md +6 -5
  16. package/migration-guides/12.8.2-to-12.8.3.md +111 -0
  17. package/migration-guides/12.8.3-to-12.8.4.md +109 -0
  18. package/package.json +36 -36
  19. package/src/core/common/interfaces/server-options.interface.ts +21 -0
  20. package/src/core/modules/better-auth/README.md +4 -1
  21. package/src/core/modules/better-auth/core-better-auth-email-verification.service.ts +18 -3
  22. package/src/core/modules/migrate/README.md +5 -3
  23. package/src/core/modules/migrate/helpers/ts-compiler-hooks.mjs +14 -65
  24. package/src/core/modules/migrate/helpers/ts-compiler.cjs +157 -12
@@ -2,8 +2,10 @@
2
2
  * TypeScript loader for running sources without a build — the migrate CLI, the dev server and scripts.
3
3
  *
4
4
  * Registers a CommonJS `require` hook that compiles `.ts` / `.cts` files with SWC (`@swc/core`) on load, and — for
5
- * ESM projects (`"type": "module"`, what `nest new` creates since NestJS 12) — the matching ESM hooks
6
- * (`ts-compiler-hooks.mjs`, through `module.register()`), so `import` reaches TypeScript sources the same way.
5
+ * ESM projects (`"type": "module"`, what `nest new` creates since NestJS 12) — the matching module hooks, so `import`
6
+ * reaches TypeScript sources the same way: in-thread through `module.registerHooks()` where Node supports what the
7
+ * loader needs (24.12+, 26+), otherwise through `module.register()` and `ts-compiler-hooks.mjs` (see
8
+ * `registerHooksHandsBackCommonJs()`).
7
9
  * It is the framework's replacement for ts-node (12.0.0): ts-node is unmaintained, needed TypeScript-6
8
10
  * workarounds (TS5011 / TS5107 / TS5110 from inherited compiler options), type-checked what it
9
11
  * loaded on every start, and — like `@swc-node/register`, whose peer range ends below TypeScript 7 —
@@ -27,7 +29,7 @@
27
29
  const fs = require('node:fs');
28
30
  const Module = require('node:module');
29
31
  const path = require('node:path');
30
- const { pathToFileURL } = require('node:url');
32
+ const { fileURLToPath, pathToFileURL } = require('node:url');
31
33
  const { isMainThread } = require('node:worker_threads');
32
34
 
33
35
  // The loader table Node's CommonJS `require` dispatches on by extension — what every require hook patches.
@@ -114,6 +116,9 @@ function patchResolver() {
114
116
  Module._resolveFilename = resolveTypeScriptSource;
115
117
  }
116
118
 
119
+ /** `@swc/core`, loaded once per thread by `register()` or by the first ESM file compiled. */
120
+ let swc;
121
+
117
122
  /**
118
123
  * Load `@swc/core`, or fail with the one instruction that fixes it.
119
124
  *
@@ -141,7 +146,7 @@ function register() {
141
146
  if (extensionHandlers['.ts']?.[MARKER]) {
142
147
  return;
143
148
  }
144
- const swc = loadSwc();
149
+ swc ??= loadSwc();
145
150
  // Stack traces point at the TypeScript lines (the code carries inline source maps).
146
151
  process.setSourceMapsEnabled(true);
147
152
 
@@ -154,25 +159,162 @@ function register() {
154
159
  for (const extension of EXTENSIONS) {
155
160
  extensionHandlers[extension] = compileTypeScript;
156
161
  }
162
+ // Node 24.22+ and 26 pick the handler of a module whose load hook named its format BY THAT FORMAT, not by the file's
163
+ // extension: `commonjs` means `.js`. That happens when the ESM loader hands a CommonJS-scoped `.ts` / `.cts` file
164
+ // back (`loadTypeScript()`), which it does for a CommonJS entry as soon as a second, asynchronous loader runs next to
165
+ // this one (an instrumentation preload through `--import`), and for an ESM project's `.cts` import. Without this,
166
+ // the file would be compiled as plain JavaScript. Any other file keeps Node's own handler.
167
+ const javaScriptHandler = extensionHandlers['.js'];
168
+ function compileTypeScriptReachingJavaScriptHandler(module, filename) {
169
+ return /\.c?ts$/.test(filename)
170
+ ? compileTypeScript(module, filename)
171
+ : javaScriptHandler.call(this, module, filename);
172
+ }
173
+ extensionHandlers['.js'] = compileTypeScriptReachingJavaScriptHandler;
157
174
  patchResolver();
158
175
  registerEsmHooks();
159
176
  }
160
177
 
161
- /** Marks the process once the ESM hooks are registered. */
178
+ /** Marks the thread once the module hooks are registered. */
162
179
  const ESM_MARKER = Symbol.for('lenne.tech/nest-server/ts-compiler/esm');
163
180
 
181
+ const TYPESCRIPT = /\.(c|m)?ts$/;
182
+ const packageTypes = new Map();
183
+
184
+ /** `"module"` or `"commonjs"`, from the nearest package.json — how Node itself decides a `.ts` file's format. */
185
+ function packageType(directory) {
186
+ if (packageTypes.has(directory)) {
187
+ return packageTypes.get(directory);
188
+ }
189
+ let type = 'commonjs';
190
+ try {
191
+ type =
192
+ JSON.parse(fs.readFileSync(path.join(directory, 'package.json'), 'utf8')).type === 'module'
193
+ ? 'module'
194
+ : 'commonjs';
195
+ } catch {
196
+ const parent = path.dirname(directory);
197
+ type = parent === directory ? 'commonjs' : packageType(parent);
198
+ }
199
+ packageTypes.set(directory, type);
200
+ return type;
201
+ }
202
+
203
+ /** The module format Node gives a TypeScript file: `.mts` / `.cts` by extension, `.ts` by the nearest package.json. */
204
+ function formatOf(filename) {
205
+ if (filename.endsWith('.mts')) {
206
+ return 'module';
207
+ }
208
+ if (filename.endsWith('.cts')) {
209
+ return 'commonjs';
210
+ }
211
+ return packageType(path.dirname(filename));
212
+ }
213
+
214
+ /**
215
+ * The specifiers to try after an `import` of `./x.js` from a TypeScript file failed: `./x.ts`, `./x.cts` (`./x.mjs`:
216
+ * `./x.mts`). `undefined` when the failure is not of that kind — a real `.js` file always wins, because this is asked
217
+ * only after Node's own resolution failed, and package specifiers are never touched.
218
+ */
219
+ function typeScriptSpecifiers(specifier, context, error) {
220
+ const extension = specifier.match(/\.(c|m)?js$/)?.[0];
221
+ const fromTypeScript = TYPESCRIPT.test(context.parentURL ?? '');
222
+ const pathLike = /^\.{0,2}\//.test(specifier) || specifier.startsWith('file:');
223
+ if (error?.code !== 'ERR_MODULE_NOT_FOUND' || !extension || !fromTypeScript || !pathLike) {
224
+ return undefined;
225
+ }
226
+ const sources = { ...SOURCE_EXTENSIONS, '.mjs': ['.mts'] }[extension] ?? [];
227
+ return sources.map((sourceExtension) => specifier.slice(0, -extension.length) + sourceExtension);
228
+ }
229
+
230
+ /**
231
+ * What the `load` hook answers for a URL, or `undefined` for anything that is not a TypeScript file (Node loads it).
232
+ *
233
+ * A TypeScript file in ESM scope (`.mts`, or `.ts` under `"type": "module"`) is compiled with SWC to ESM, with the
234
+ * options of the CommonJS hook. One in CommonJS scope is handed back to the CommonJS loader WITHOUT a source, where the
235
+ * `require` hook compiles it: the same file must not be compiled two different ways, and only the CommonJS loader gives
236
+ * it a real `require` (`require.cache`, `require.extensions`) — a CommonJS source returned from here crashes Node
237
+ * 22's CommonJS translator and runs with the reduced `require` Node builds for it on 24.13 and earlier.
238
+ */
239
+ function loadTypeScript(url) {
240
+ if (!url.startsWith('file:') || !TYPESCRIPT.test(new URL(url).pathname)) {
241
+ return undefined;
242
+ }
243
+ const filename = fileURLToPath(url);
244
+ if (formatOf(filename) === 'commonjs') {
245
+ return { format: 'commonjs', shortCircuit: true };
246
+ }
247
+ swc ??= loadSwc();
248
+ const { code } = swc.transformSync(fs.readFileSync(filename, 'utf8'), {
249
+ ...SWC_OPTIONS,
250
+ filename,
251
+ // SWC drops import attributes unless told to keep them, and Node refuses a JSON import without
252
+ // `with { type: 'json' }` (ERR_IMPORT_ATTRIBUTE_MISSING) — `import pkg from '../package.json' with { type: 'json' }`.
253
+ jsc: { ...SWC_OPTIONS.jsc, experimental: { keepImportAssertions: true } },
254
+ module: { type: 'es6' },
255
+ });
256
+ return { format: 'module', shortCircuit: true, source: code };
257
+ }
258
+
259
+ /** The synchronous `resolve` hook for `module.registerHooks()`; `ts-compiler-hooks.mjs` is its asynchronous twin. */
260
+ function resolveHook(specifier, context, nextResolve) {
261
+ try {
262
+ return nextResolve(specifier, context);
263
+ } catch (error) {
264
+ for (const candidate of typeScriptSpecifiers(specifier, context, error) ?? []) {
265
+ try {
266
+ return nextResolve(candidate, context);
267
+ } catch {
268
+ // try the next one; the original error is the one worth reporting
269
+ }
270
+ }
271
+ throw error;
272
+ }
273
+ }
274
+
275
+ /** The synchronous `load` hook for `module.registerHooks()`; `ts-compiler-hooks.mjs` is its asynchronous twin. */
276
+ function loadHook(url, context, nextLoad) {
277
+ return loadTypeScript(url) ?? nextLoad(url, context);
278
+ }
279
+
280
+ /**
281
+ * Whether this Node's `module.registerHooks()` takes a `load` result for a CommonJS module without `source`, i.e. hands
282
+ * it back to the CommonJS loader — what `loadTypeScript()` answers for CommonJS-scoped TypeScript imported from ESM.
283
+ * Measured (tests/unit/ts-compiler.spec.ts): 24.12.0 and 26.0.0 take it; 22.x, 23.x, 24.0–24.11 and 25.0 refuse it
284
+ * with ERR_INVALID_RETURN_PROPERTY_VALUE, and a source compiled for them crashes Node 22 and runs on 24.0–24.11
285
+ * without `require.cache`. Those lines keep `module.register()`, which no Node before 26 deprecates at runtime. 25.x is
286
+ * end-of-life and stays on the older path.
287
+ *
288
+ * @param {string} [version] - A Node version, `process.versions.node` by default.
289
+ */
290
+ function registerHooksHandsBackCommonJs(version = process.versions.node) {
291
+ const [major, minor] = version.split('.').map(Number);
292
+ return major >= 26 || (major === 24 && minor >= 12);
293
+ }
294
+
164
295
  /**
165
- * Register the ESM hooks (`ts-compiler-hooks.mjs`) for `import`ed TypeScript: an ESM project's entry and everything it
166
- * imports. Without them Node would hand a `.ts` entry to its own type stripping, which cannot lower decorators or emit
167
- * their metadata — NestJS needs both. Only on the main thread: the hooks run on a thread of their own, which loads this
168
- * file for its SWC options and must not register itself again.
296
+ * Register the module hooks for `import`ed TypeScript: an ESM project's entry and everything it imports. Without them
297
+ * Node would hand a `.ts` entry to its own type stripping, which cannot lower decorators or emit their metadata —
298
+ * NestJS needs both.
299
+ *
300
+ * `module.registerHooks()` (in-thread, synchronous) where it hands CommonJS back; there it replaces `module.register()`,
301
+ * which Node 26 deprecates at runtime (DEP0205, printed on every start). The CommonJS path needs neither: `require()`
302
+ * of a `.ts` file reaches the extension handler above, which does not consult module hooks.
303
+ *
304
+ * Only on the main thread: `module.register()` runs `ts-compiler-hooks.mjs` on a thread of its own, which loads this
305
+ * file for its helpers and must not register itself again.
169
306
  */
170
307
  function registerEsmHooks() {
171
- if (!isMainThread || typeof Module.register !== 'function' || globalThis[ESM_MARKER]) {
308
+ if (!isMainThread || globalThis[ESM_MARKER]) {
172
309
  return;
173
310
  }
174
- globalThis[ESM_MARKER] = true;
175
- Module.register(pathToFileURL(path.join(__dirname, 'ts-compiler-hooks.mjs')).href);
311
+ if (typeof Module.registerHooks === 'function' && registerHooksHandsBackCommonJs()) {
312
+ globalThis[ESM_MARKER] = true;
313
+ Module.registerHooks({ load: loadHook, resolve: resolveHook });
314
+ } else if (typeof Module.register === 'function') {
315
+ globalThis[ESM_MARKER] = true;
316
+ Module.register(pathToFileURL(path.join(__dirname, 'ts-compiler-hooks.mjs')).href);
317
+ }
176
318
  }
177
319
 
178
320
  register();
@@ -181,3 +323,6 @@ module.exports = register;
181
323
  module.exports.loadSwc = loadSwc;
182
324
  module.exports.SWC_OPTIONS = SWC_OPTIONS;
183
325
  module.exports.SOURCE_EXTENSIONS = SOURCE_EXTENSIONS;
326
+ module.exports.loadTypeScript = loadTypeScript;
327
+ module.exports.registerHooksHandsBackCommonJs = registerHooksHandsBackCommonJs;
328
+ module.exports.typeScriptSpecifiers = typeScriptSpecifiers;