@faapi/faapi 2.0.0 → 2.0.1-canary.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/index.d.ts CHANGED
@@ -765,6 +765,9 @@ interface InjectResponse {
765
765
  *
766
766
  * 用于在无法直接拿到 app 引用的场景(如 Next.js Server Component)中访问 app。
767
767
  *
768
+ * 通过 `globalThis` 共享单例,确保 Next.js Turbopack runtime 加载的 `@faapi/faapi`
769
+ * 模块实例与主进程 `faapi dev`/`node dist/main` 设置单例的实例能读到同一个 app 引用。
770
+ *
768
771
  * @returns 当前 app 实例
769
772
  * @throws 未初始化时抛错(需先调 `createProdApp()` / `createDevApp()`,或 `faapi dev` / `node dist/main` 启动)
770
773
  *
package/dist/index.js CHANGED
@@ -3552,14 +3552,25 @@ var DEFAULT_DIST = "dist";
3552
3552
  var DEFAULT_PORT = 3e3;
3553
3553
  var ROUTES_FILE = "faapi-routes.js";
3554
3554
  var PATTERNS = ["src/api/**/*.ts"];
3555
- var currentApp = null;
3555
+ var APP_INSTANCE_KEY = /* @__PURE__ */ Symbol.for("faapi.app.instance");
3556
+ function getCurrentApp() {
3557
+ return globalThis[APP_INSTANCE_KEY] ?? null;
3558
+ }
3559
+ function setCurrentApp(app) {
3560
+ if (app === null) {
3561
+ delete globalThis[APP_INSTANCE_KEY];
3562
+ } else {
3563
+ globalThis[APP_INSTANCE_KEY] = app;
3564
+ }
3565
+ }
3556
3566
  function getApp() {
3557
- if (!currentApp) {
3567
+ const app = getCurrentApp();
3568
+ if (!app) {
3558
3569
  throw new Error(
3559
3570
  "[faapi] No app instance. Call createProdApp() / createDevApp() first, or run `faapi dev` / `node dist/main`."
3560
3571
  );
3561
3572
  }
3562
- return currentApp;
3573
+ return app;
3563
3574
  }
3564
3575
  var FAAPI_CONFIG_KEYS = /* @__PURE__ */ new Set([
3565
3576
  "cors",
@@ -3748,20 +3759,20 @@ async function createAppBase(options) {
3748
3759
  }
3749
3760
  if (!server.listening) {
3750
3761
  app.server = null;
3751
- if (currentApp === app) currentApp = null;
3762
+ if (getCurrentApp() === app) setCurrentApp(null);
3752
3763
  return;
3753
3764
  }
3754
3765
  return new Promise((resolve) => {
3755
3766
  server.close((err) => {
3756
3767
  if (err) console.error("Error closing server:", err);
3757
3768
  app.server = null;
3758
- if (currentApp === app) currentApp = null;
3769
+ if (getCurrentApp() === app) setCurrentApp(null);
3759
3770
  resolve();
3760
3771
  });
3761
3772
  });
3762
3773
  }
3763
3774
  };
3764
- currentApp = app;
3775
+ setCurrentApp(app);
3765
3776
  const ctx = {
3766
3777
  rootDir,
3767
3778
  dist,