@faapi/faapi 6.32.0 → 6.34.0

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.
package/dist/testing.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { g as FaapiContext, F as FaapiMiddleware, w as InjectorMap, R as RouteManifest, W as WsRouteManifest, r as CorsOptions, H as HelmetOptions } from './contextTypes-WKsf3cCE.js';
1
+ import { g as FaapiContext, F as FaapiMiddleware, w as InjectorMap, R as RouteManifest, W as WsRouteManifest, r as CorsOptions, H as HelmetOptions } from './contextTypes-Bsc1ljvn.js';
2
2
  import { Server } from 'node:http';
3
3
  import { WebSocket } from 'ws';
4
4
  import 'node:net';
package/dist/testing.js CHANGED
@@ -429,11 +429,7 @@ function formatEntry(entry) {
429
429
  const plain = Object.fromEntries(
430
430
  Object.entries(entry.fields).map(([k, v]) => [k, serializeFieldValue(v)])
431
431
  );
432
- try {
433
- line += ` ${JSON.stringify(plain)}`;
434
- } catch (err) {
435
- line += ` [unserializable fields: ${err instanceof Error ? err.message : String(err)}]`;
436
- }
432
+ line += ` ${stringifyJson(plain)}`;
437
433
  }
438
434
  return line;
439
435
  }
@@ -1520,38 +1516,30 @@ function setCachedMiddlewares(absPath, bundle) {
1520
1516
  middlewareCache.set(absPath, bundle);
1521
1517
  }
1522
1518
  async function loadMiddlewaresFile(filePath) {
1523
- let module;
1524
- try {
1525
- module = await importWithCacheBust(filePath);
1526
- } catch (err) {
1527
- console.error(
1528
- `[faapi] Failed to load middlewares from ${filePath}:`,
1529
- err instanceof Error ? err.stack ?? err.message : err
1530
- );
1531
- return { middlewares: [], injectors: {} };
1532
- }
1519
+ const module = await importWithCacheBust(filePath);
1533
1520
  const middlewares = module.default ?? module.middlewares ?? [];
1534
1521
  if (!Array.isArray(middlewares)) {
1535
- console.warn(`[faapi] middlewares.ts \u5E94\u5BFC\u51FA\u6570\u7EC4\uFF0C\u5DF2\u5FFD\u7565: ${filePath}`);
1536
- return { middlewares: [], injectors: {} };
1522
+ throw new TypeError(
1523
+ `[faapi] middlewares.ts must export an array (default or named "middlewares"): ${filePath}`
1524
+ );
1537
1525
  }
1538
- const validMiddlewares = middlewares.filter((m) => {
1526
+ const validMiddlewares = [];
1527
+ for (const m of middlewares) {
1539
1528
  if (typeof m !== "function") {
1540
- console.warn(`[faapi] \u65E0\u6548\u7684\u4E2D\u95F4\u4EF6\u9879\uFF08\u5E94\u4E3A\u51FD\u6570\uFF09\uFF0C\u5DF2\u5FFD\u7565: ${typeof m}`);
1541
- return false;
1529
+ throw new TypeError(
1530
+ `[faapi] every middleware must be a function, got ${typeof m}: ${filePath}`
1531
+ );
1542
1532
  }
1543
- return true;
1544
- });
1533
+ validMiddlewares.push(m);
1534
+ }
1545
1535
  const injectors = module.injectors ?? {};
1546
1536
  if (typeof injectors !== "object" || injectors === null) {
1547
- console.warn(`[faapi] injectors \u5E94\u5BFC\u51FA\u5BF9\u8C61\uFF0C\u5DF2\u5FFD\u7565: ${filePath}`);
1548
- return { middlewares: validMiddlewares, injectors: {} };
1537
+ throw new TypeError(`[faapi] "injectors" export must be an object: ${filePath}`);
1549
1538
  }
1550
1539
  const validInjectors = {};
1551
1540
  for (const [name, injector] of Object.entries(injectors)) {
1552
1541
  if (typeof injector !== "function") {
1553
- console.warn(`[faapi] \u6CE8\u5165\u5668 ${name} \u5E94\u4E3A\u51FD\u6570\uFF0C\u5DF2\u5FFD\u7565`);
1554
- continue;
1542
+ throw new TypeError(`[faapi] injector "${name}" must be a function: ${filePath}`);
1555
1543
  }
1556
1544
  validInjectors[name] = injector;
1557
1545
  }
@@ -1566,10 +1554,16 @@ async function loadMergedMiddlewares(middlewarePaths) {
1566
1554
  if (bundle === void 0) {
1567
1555
  let loading = inFlight.get(absMwPath);
1568
1556
  if (!loading) {
1569
- loading = loadMiddlewaresFile(absMwPath).then((result) => {
1570
- setCachedMiddlewares(absMwPath, result);
1571
- return result;
1572
- });
1557
+ loading = loadMiddlewaresFile(absMwPath).then(
1558
+ (result) => {
1559
+ setCachedMiddlewares(absMwPath, result);
1560
+ return result;
1561
+ },
1562
+ (err) => {
1563
+ inFlight.delete(absMwPath);
1564
+ throw err;
1565
+ }
1566
+ );
1573
1567
  inFlight.set(absMwPath, loading);
1574
1568
  }
1575
1569
  bundle = await loading;
@@ -3753,11 +3747,7 @@ async function ensureMiddlewaresCompiled(middlewarePaths, rootDir) {
3753
3747
  if (!dist) return;
3754
3748
  for (const mwPath of middlewarePaths) {
3755
3749
  const sourcePath = prodPathToSourcePath(mwPath, rootDir, dist);
3756
- try {
3757
- await ensureCompiled(sourcePath, rootDir, dist);
3758
- } catch (err) {
3759
- console.error(`[faapi] Failed to compile middleware source ${sourcePath}:`, err);
3760
- }
3750
+ await ensureCompiled(sourcePath, rootDir, dist);
3761
3751
  }
3762
3752
  }
3763
3753
  function prodSourcePathToProductPath(sourceAbsPath, rootDir, dist) {
@@ -5277,7 +5267,8 @@ async function sendErrorResponse(err, meta, res, onError, ctx) {
5277
5267
  if (onError && ctx) {
5278
5268
  try {
5279
5269
  await onError(err, ctx);
5280
- } catch {
5270
+ } catch (hookErr) {
5271
+ console.error("[faapi] lifecycle onError hook threw:", hookErr);
5281
5272
  }
5282
5273
  }
5283
5274
  }