@twikoojs/edgeone-makers 2.0.14 → 2.0.15

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.
@@ -43,7 +43,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
43
43
  var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
44
44
  //#endregion
45
45
  //#region ../shared/dist/index.mjs
46
- /*! @twikoojs/shared v2.0.14 */
46
+ /*! @twikoojs/shared v2.0.15 */
47
47
  /**
48
48
  * Twikoo 统一版本号常量(构建期占位符)。
49
49
  *
@@ -56,7 +56,7 @@ var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
56
56
  * 占位符字面量由 `@twikoojs/tsdown-config` 的 `VERSION_PLACEHOLDER` 常量定义,
57
57
  * 客户端与服务端统一读取本常量,避免版本号在多包之间手工同步。
58
58
  */
59
- const VERSION = "2.0.14";
59
+ const VERSION = "2.0.15";
60
60
  /**
61
61
  * 后端事件名常量(Scope E:24 个客户端事件 + 1 个服务端内部事件)。
62
62
  *
@@ -27597,6 +27597,34 @@ function allowCors(request, config) {
27597
27597
  "Access-Control-Max-Age": "600"
27598
27598
  };
27599
27599
  }
27600
+ /** 错误路径共用进行中的配置读取,避免并发拒绝请求堆积查询。 */
27601
+ const pendingCorsConfig = /* @__PURE__ */ new WeakMap();
27602
+ /** 错误响应只读取已有数据库的配置,最多等待 100ms;失败或超时不放行跨域,也不改变原错误。 */
27603
+ async function allowCorsForError(request, database) {
27604
+ if (!request.headers.origin) return {};
27605
+ let pending = pendingCorsConfig.get(database);
27606
+ if (!pending) {
27607
+ pending = (async () => {
27608
+ try {
27609
+ return await database.getConfig() ?? {};
27610
+ } catch (e) {
27611
+ console.error("Twikoo CORS configuration failed:", e);
27612
+ return null;
27613
+ }
27614
+ })();
27615
+ pendingCorsConfig.set(database, pending);
27616
+ pending.then(() => pendingCorsConfig.delete(database));
27617
+ }
27618
+ let timer;
27619
+ try {
27620
+ const config = await Promise.race([pending, new Promise((resolve) => {
27621
+ timer = setTimeout(() => resolve(null), 100);
27622
+ })]);
27623
+ return config === null ? {} : allowCors(request, config);
27624
+ } finally {
27625
+ clearTimeout(timer);
27626
+ }
27627
+ }
27600
27628
  /**
27601
27629
  * 计算放行的 Origin(1.x getAllowedOrigin 逐行对齐)。
27602
27630
  * @param origin 请求 Origin
@@ -27666,13 +27694,15 @@ function createPipeline(adapters) {
27666
27694
  let status = 200;
27667
27695
  /** 步骤 3 产出,步骤 9 回填判断用 */
27668
27696
  let accessToken = "";
27697
+ let corsAttempted = false;
27669
27698
  try {
27670
27699
  protect(request, logger);
27671
27700
  validateClientFields(request.body);
27672
27701
  accessToken = anonymousSignIn(request);
27702
+ corsAttempted = true;
27673
27703
  await adapters.database.init();
27674
27704
  const { config, failed: configReadFailed } = await readConfig(adapters, logger);
27675
- Object.assign(headers, allowCors(request, config));
27705
+ if (!configReadFailed) Object.assign(headers, allowCors(request, config));
27676
27706
  if (request.method === "OPTIONS") return {
27677
27707
  status: 204,
27678
27708
  body: {},
@@ -27698,6 +27728,7 @@ function createPipeline(adapters) {
27698
27728
  message: getErrorMessage(e)
27699
27729
  };
27700
27730
  if (e instanceof RateLimitError) status = 429;
27731
+ if (!corsAttempted) Object.assign(headers, await allowCorsForError(request, adapters.database));
27701
27732
  }
27702
27733
  if (!body.code && !request.body.accessToken) body.accessToken = accessToken;
27703
27734
  logger.verbose("请求返回:", body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twikoojs/edgeone-makers",
3
- "version": "2.0.14",
3
+ "version": "2.0.15",
4
4
  "description": "Twikoo comment system backend for Tencent EdgeOne Makers",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -21,14 +21,14 @@
21
21
  "dependencies": {
22
22
  "@cap.js/server": "^4.0.5",
23
23
  "@edgeone/pages-blob": "latest",
24
- "@twikoojs/common": "2.0.14",
25
- "@twikoojs/shared": "2.0.14",
24
+ "@twikoojs/common": "2.0.15",
25
+ "@twikoojs/shared": "2.0.15",
26
26
  "bowser": "^2.14.1",
27
27
  "form-data": "^4.0.6",
28
28
  "html-to-text": "^10.0.1",
29
29
  "marked": "^18.0.13",
30
30
  "pako": "^3.0.2",
31
- "pushoo": "^2.0.14",
31
+ "pushoo": "^2.0.15",
32
32
  "xml2js": "^0.6.2"
33
33
  },
34
34
  "devDependencies": {