@faapi/faapi 1.2.0 → 1.2.1

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/cli/index.js CHANGED
@@ -545,9 +545,14 @@ function getVitestImportActual() {
545
545
  if (typeof vi?.importActual !== "function") return void 0;
546
546
  return vi.importActual.bind(vi);
547
547
  }
548
- async function importWithCacheBust(filePath) {
548
+ async function importWithCacheBust(filePath, bustViteCache = false) {
549
549
  const importActual = getVitestImportActual();
550
550
  if (importActual) {
551
+ if (bustViteCache) {
552
+ let url2 = pathToFileURL(filePath).href;
553
+ url2 += `?t=${Date.now()}`;
554
+ return await import(url2);
555
+ }
551
556
  return await importActual(filePath);
552
557
  }
553
558
  let url = pathToFileURL(filePath).href;
@@ -4324,31 +4329,28 @@ var init_compileOnDemand = __esm({
4324
4329
  });
4325
4330
 
4326
4331
  // src/loader/loadRouteModule.ts
4332
+ import fs11 from "fs";
4327
4333
  async function loadRouteModule(filePath, method, rootDir) {
4328
- let module;
4329
- try {
4330
- module = await importWithCacheBust(filePath);
4331
- } catch (err) {
4332
- if (isDevOnDemandEnabled() && rootDir) {
4333
- const dist = getDevDist();
4334
- if (dist) {
4335
- const sourcePath = prodPathToSourcePath(filePath, rootDir, dist);
4334
+ if (isDevOnDemandEnabled() && rootDir) {
4335
+ const dist = getDevDist();
4336
+ if (dist) {
4337
+ const sourcePath = prodPathToSourcePath(filePath, rootDir, dist);
4338
+ if (sourcePath && fs11.existsSync(sourcePath)) {
4336
4339
  try {
4337
- const compiled = await ensureCompiled(sourcePath, rootDir, dist);
4338
- if (compiled) {
4339
- module = await importWithCacheBust(filePath);
4340
- const handler2 = resolveExport(module, method);
4341
- validateRouteModule(handler2, method, filePath);
4342
- return { handler: handler2, method };
4343
- }
4340
+ await ensureCompiled(sourcePath, rootDir, dist);
4344
4341
  } catch (compileErr) {
4345
- const compileReason = compileErr instanceof Error ? compileErr.message : String(compileErr);
4346
- throw new Error(`Failed to compile route module "${sourcePath}": ${compileReason}`, {
4342
+ const reason = compileErr instanceof Error ? compileErr.message : String(compileErr);
4343
+ throw new Error(`Failed to compile route module "${sourcePath}": ${reason}`, {
4347
4344
  cause: compileErr
4348
4345
  });
4349
4346
  }
4350
4347
  }
4351
4348
  }
4349
+ }
4350
+ let module;
4351
+ try {
4352
+ module = await importWithCacheBust(filePath, isDevOnDemandEnabled());
4353
+ } catch (err) {
4352
4354
  const reason = err instanceof Error ? err.message : String(err);
4353
4355
  throw new Error(`Failed to load route module "${filePath}": ${reason}`, { cause: err });
4354
4356
  }
@@ -5063,7 +5065,7 @@ function invalidateSchemaCache() {
5063
5065
  async function loadSchemaModule(schemaPath) {
5064
5066
  let mod = moduleCache.get(schemaPath);
5065
5067
  if (!mod) {
5066
- mod = await importWithCacheBust(schemaPath);
5068
+ mod = await importWithCacheBust(schemaPath, isDevOnDemandEnabled());
5067
5069
  moduleCache.set(schemaPath, mod);
5068
5070
  }
5069
5071
  return mod;
@@ -5148,6 +5150,7 @@ var init_validateInput = __esm({
5148
5150
  init_schemaName();
5149
5151
  init_httpErrors();
5150
5152
  init_importWithCacheBust();
5153
+ init_compileOnDemand();
5151
5154
  moduleCache = /* @__PURE__ */ new Map();
5152
5155
  }
5153
5156
  });
@@ -5459,6 +5462,7 @@ var init_wsHandler = __esm({
5459
5462
  });
5460
5463
 
5461
5464
  // src/server/handleWsUpgrade.ts
5465
+ import fs12 from "fs";
5462
5466
  import { WebSocketServer, WebSocket } from "ws";
5463
5467
  import path13 from "path";
5464
5468
  function getPathname(req) {
@@ -5467,24 +5471,16 @@ function getPathname(req) {
5467
5471
  return idx >= 0 ? url.slice(0, idx) : url;
5468
5472
  }
5469
5473
  async function loadWsHandler(filePath, ctx, rootDir) {
5470
- let module;
5471
- try {
5472
- module = await importWithCacheBust(filePath);
5473
- } catch (err) {
5474
- if (isDevOnDemandEnabled() && rootDir) {
5475
- const dist = getDevDist();
5476
- if (dist) {
5477
- const sourcePath = prodPathToSourcePath(filePath, rootDir, dist);
5478
- const compiled = await ensureCompiled(sourcePath, rootDir, dist);
5479
- if (compiled) {
5480
- module = await importWithCacheBust(filePath);
5481
- }
5474
+ if (isDevOnDemandEnabled() && rootDir) {
5475
+ const dist = getDevDist();
5476
+ if (dist) {
5477
+ const sourcePath = prodPathToSourcePath(filePath, rootDir, dist);
5478
+ if (sourcePath && fs12.existsSync(sourcePath)) {
5479
+ await ensureCompiled(sourcePath, rootDir, dist);
5482
5480
  }
5483
5481
  }
5484
- if (!module) {
5485
- throw err;
5486
- }
5487
5482
  }
5483
+ const module = await importWithCacheBust(filePath, isDevOnDemandEnabled());
5488
5484
  const handler = module["WS"];
5489
5485
  if (typeof handler !== "function") {
5490
5486
  throw new Error(`WS export not found in ${filePath}`);
@@ -5955,7 +5951,7 @@ var init_loadPlugins = __esm({
5955
5951
  });
5956
5952
 
5957
5953
  // src/cli/createAppCore.ts
5958
- import fs11 from "fs";
5954
+ import fs13 from "fs";
5959
5955
  import path15 from "path";
5960
5956
  import { PassThrough } from "stream";
5961
5957
  function isFaapiConfigKey(key) {
@@ -5965,7 +5961,7 @@ async function createAppBase(options) {
5965
5961
  const rootDir = options?.rootDir ?? process.cwd();
5966
5962
  const dist = options?.dist ?? process.env.FAAPI_DIST ?? DEFAULT_DIST;
5967
5963
  const routesPath = path15.resolve(rootDir, dist, ROUTES_FILE);
5968
- if (!fs11.existsSync(routesPath)) {
5964
+ if (!fs13.existsSync(routesPath)) {
5969
5965
  throw new Error(
5970
5966
  `[faapi] ${dist}/${ROUTES_FILE} \u4E0D\u5B58\u5728\uFF0C\u8BF7\u5148\u6267\u884C \`faapi build\`\uFF08\u6216 \`faapi dev\`\uFF09\u751F\u6210\u4EA7\u7269\u3002`
5971
5967
  );
@@ -6138,6 +6134,10 @@ async function createAppBase(options) {
6138
6134
  if (config?.lifecycle?.onClose) {
6139
6135
  await config.lifecycle.onClose({ rootDir, routes: sorted, server });
6140
6136
  }
6137
+ if (!server.listening) {
6138
+ app.server = null;
6139
+ return;
6140
+ }
6141
6141
  return new Promise((resolve3) => {
6142
6142
  server.close((err) => {
6143
6143
  if (err) console.error("Error closing server:", err);
@@ -6287,7 +6287,7 @@ var init_devCommand = __esm({
6287
6287
 
6288
6288
  // src/cli/compileBuildRoutes.ts
6289
6289
  import path17 from "path";
6290
- import fs12 from "fs";
6290
+ import fs14 from "fs";
6291
6291
  import fg3 from "fast-glob";
6292
6292
  async function compileBuildRoutes(options) {
6293
6293
  const { rootDir, dist, files, logLevel = "silent" } = options;
@@ -6301,7 +6301,7 @@ async function compileBuildRoutes(options) {
6301
6301
  return { compiledFiles: [] };
6302
6302
  }
6303
6303
  const absDist = path17.resolve(rootDir, dist);
6304
- await fs12.promises.mkdir(absDist, { recursive: true });
6304
+ await fs14.promises.mkdir(absDist, { recursive: true });
6305
6305
  const plugins = buildAliasPlugins(rootDir);
6306
6306
  const esbuild = await import("esbuild");
6307
6307
  const outbase = path17.resolve(rootDir, APP_DIR3);
@@ -6336,7 +6336,7 @@ __export(buildCommand_exports, {
6336
6336
  buildCommand: () => buildCommand
6337
6337
  });
6338
6338
  import path18 from "path";
6339
- import fs13 from "fs";
6339
+ import fs15 from "fs";
6340
6340
  async function buildCommand(options) {
6341
6341
  const rootDir = options?.rootDir ?? process.cwd();
6342
6342
  const outdir = options?.dist ?? DEFAULT_DIST2;
@@ -6399,7 +6399,7 @@ loadEnv(process.cwd());
6399
6399
  const app = await createProdApp(${createProdAppArgs});
6400
6400
  await app.listen();
6401
6401
  `;
6402
- await fs13.promises.writeFile(mainPath, mainContent, "utf-8");
6402
+ await fs15.promises.writeFile(mainPath, mainContent, "utf-8");
6403
6403
  console.log(` Written to ${mainPath}`);
6404
6404
  console.log("\nfaapi build completed");
6405
6405
  }