@taicode/common-server 4.0.0-beta.2 → 4.0.0-beta.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 (276) hide show
  1. package/output/app/index.d.ts +13 -15
  2. package/output/app/index.d.ts.map +1 -1
  3. package/output/app/index.js +256 -18
  4. package/output/auth/index.d.ts +72 -0
  5. package/output/auth/index.d.ts.map +1 -0
  6. package/output/auth/index.js +176 -0
  7. package/output/core/assemble.d.ts +64 -0
  8. package/output/core/assemble.d.ts.map +1 -0
  9. package/output/core/assemble.js +88 -0
  10. package/output/core/boot/context.d.ts +89 -0
  11. package/output/core/boot/context.d.ts.map +1 -0
  12. package/output/core/boot/context.js +106 -0
  13. package/output/core/boot/lifecycle.d.ts +94 -0
  14. package/output/core/boot/lifecycle.d.ts.map +1 -0
  15. package/output/{app → core/boot}/lifecycle.js +45 -14
  16. package/output/core/boot/shutdown.d.ts +30 -0
  17. package/output/core/boot/shutdown.d.ts.map +1 -0
  18. package/output/core/boot/shutdown.js +45 -0
  19. package/output/core/boot/signal.d.ts +13 -0
  20. package/output/core/boot/signal.d.ts.map +1 -0
  21. package/output/{app → core/boot}/signal.js +3 -2
  22. package/output/core/boot/steps.d.ts +107 -0
  23. package/output/core/boot/steps.d.ts.map +1 -0
  24. package/output/core/boot/steps.js +200 -0
  25. package/output/core/entries.d.ts +123 -0
  26. package/output/core/entries.d.ts.map +1 -0
  27. package/output/core/entries.js +193 -0
  28. package/output/core/entries.test.d.ts +2 -0
  29. package/output/core/entries.test.d.ts.map +1 -0
  30. package/output/core/entries.test.js +217 -0
  31. package/output/core/env.d.ts +62 -0
  32. package/output/core/env.d.ts.map +1 -0
  33. package/output/core/env.js +96 -0
  34. package/output/{app → core}/env.test.d.ts.map +1 -1
  35. package/output/core/env.test.js +105 -0
  36. package/output/core/modules/auth-provider/index.d.ts +10 -0
  37. package/output/core/modules/auth-provider/index.d.ts.map +1 -0
  38. package/output/core/modules/auth-provider/index.js +9 -0
  39. package/output/core/modules/auth-provider/types.d.ts +71 -0
  40. package/output/core/modules/auth-provider/types.d.ts.map +1 -0
  41. package/output/core/modules/context/index.d.ts +18 -0
  42. package/output/core/modules/context/index.d.ts.map +1 -0
  43. package/output/core/modules/context/index.js +23 -0
  44. package/output/core/modules/context/index.test.d.ts.map +1 -0
  45. package/output/core/modules/cookie/index.d.ts +21 -0
  46. package/output/core/modules/cookie/index.d.ts.map +1 -0
  47. package/output/{app/core/cookie.js → core/modules/cookie/index.js} +1 -6
  48. package/output/core/modules/identity/index.d.ts +4 -0
  49. package/output/core/modules/identity/index.d.ts.map +1 -0
  50. package/output/{app/core/identity.js → core/modules/identity/index.js} +1 -1
  51. package/output/core/modules/logging/index.d.ts +5 -0
  52. package/output/core/modules/logging/index.d.ts.map +1 -0
  53. package/output/{app/core/logging.js → core/modules/logging/index.js} +2 -2
  54. package/output/core/modules/openapi/index.d.ts +7 -0
  55. package/output/core/modules/openapi/index.d.ts.map +1 -0
  56. package/output/{app/core/openapi.js → core/modules/openapi/index.js} +11 -12
  57. package/output/core/modules/permission-operations/index.d.ts +26 -0
  58. package/output/core/modules/permission-operations/index.d.ts.map +1 -0
  59. package/output/core/modules/permission-operations/index.js +36 -0
  60. package/output/core/modules/permission-operations/index.test.d.ts +2 -0
  61. package/output/core/modules/permission-operations/index.test.d.ts.map +1 -0
  62. package/output/core/modules/permission-operations/index.test.js +45 -0
  63. package/output/{app/core/request-id.d.ts → core/modules/request-id/index.d.ts} +1 -4
  64. package/output/core/modules/request-id/index.d.ts.map +1 -0
  65. package/output/{app/core/request-id.js → core/modules/request-id/index.js} +2 -2
  66. package/output/core/modules/response/index.d.ts +22 -0
  67. package/output/core/modules/response/index.d.ts.map +1 -0
  68. package/output/{app/core/response.js → core/modules/response/index.js} +22 -11
  69. package/output/{schema/index.d.ts → core/modules/schema/builders.d.ts} +8 -1
  70. package/output/core/modules/schema/builders.d.ts.map +1 -0
  71. package/output/{schema/index.js → core/modules/schema/builders.js} +7 -4
  72. package/output/core/modules/schema/index.d.ts +34 -0
  73. package/output/core/modules/schema/index.d.ts.map +1 -0
  74. package/output/core/modules/schema/index.js +62 -0
  75. package/output/core/modules/schema/index.test.d.ts +2 -0
  76. package/output/core/modules/schema/index.test.d.ts.map +1 -0
  77. package/output/core/modules/schema/index.test.js +55 -0
  78. package/output/core/modules/system-routes/index.d.ts +15 -0
  79. package/output/core/modules/system-routes/index.d.ts.map +1 -0
  80. package/output/core/modules/system-routes/index.js +171 -0
  81. package/output/core/test-support/database.d.ts +96 -0
  82. package/output/core/test-support/database.d.ts.map +1 -0
  83. package/output/core/test-support/database.js +144 -0
  84. package/output/core/test-support/database.test.d.ts +2 -0
  85. package/output/core/test-support/database.test.d.ts.map +1 -0
  86. package/output/core/test-support/database.test.js +110 -0
  87. package/output/core/test-support/environment.d.ts +33 -0
  88. package/output/core/test-support/environment.d.ts.map +1 -0
  89. package/output/core/test-support/environment.js +42 -0
  90. package/output/core/test-support/test-server.d.ts +94 -0
  91. package/output/core/test-support/test-server.d.ts.map +1 -0
  92. package/output/core/test-support/test-server.js +60 -0
  93. package/output/core/test-support/test-server.test.d.ts +2 -0
  94. package/output/core/test-support/test-server.test.d.ts.map +1 -0
  95. package/output/core/test-support/test-server.test.js +162 -0
  96. package/output/{app → core}/type-augment.d.ts +2 -2
  97. package/output/core/type-augment.d.ts.map +1 -0
  98. package/output/core/types.d.ts +338 -0
  99. package/output/core/types.d.ts.map +1 -0
  100. package/output/cors/index.d.ts +3 -0
  101. package/output/cors/index.d.ts.map +1 -0
  102. package/output/cors/index.js +27 -0
  103. package/output/index.d.ts +8 -4
  104. package/output/index.d.ts.map +1 -1
  105. package/output/index.js +24 -5
  106. package/output/rate-limit/index.d.ts +23 -0
  107. package/output/rate-limit/index.d.ts.map +1 -0
  108. package/output/rate-limit/index.js +42 -0
  109. package/output/{rate-limiter/rate-limiter.d.ts → rate-limit/limiter.d.ts} +1 -1
  110. package/output/rate-limit/limiter.d.ts.map +1 -0
  111. package/output/rate-limit/memory-store.d.ts.map +1 -0
  112. package/output/rate-limit/redis-store.d.ts.map +1 -0
  113. package/output/rate-limit/redis-store.test.d.ts.map +1 -0
  114. package/output/{rate-limiter → rate-limit}/redis-store.test.js +2 -2
  115. package/output/{rate-limiter → rate-limit}/types.d.ts +0 -1
  116. package/output/rate-limit/types.d.ts.map +1 -0
  117. package/output/rate-limit/types.js +1 -0
  118. package/output/{redis-cache/redis-cache.d.ts → redis/cache.d.ts} +1 -1
  119. package/output/redis/cache.d.ts.map +1 -0
  120. package/output/redis/cache.test.d.ts +2 -0
  121. package/output/redis/cache.test.d.ts.map +1 -0
  122. package/output/{redis-cache/redis-cache.test.js → redis/cache.test.js} +1 -1
  123. package/output/{app/redis.d.ts → redis/index.d.ts} +12 -7
  124. package/output/redis/index.d.ts.map +1 -0
  125. package/output/{app/redis.js → redis/index.js} +15 -5
  126. package/output/redis/index.test.d.ts +2 -0
  127. package/output/{context → redis}/index.test.d.ts.map +1 -1
  128. package/output/redis/index.test.js +55 -0
  129. package/output/redis-queue/index.js +1 -1
  130. package/output/redis-queue/test-helpers.d.ts +1 -1
  131. package/output/redis-queue/test-helpers.js +1 -1
  132. package/output/redis-queue/types.d.ts +3 -12
  133. package/output/redis-queue/types.d.ts.map +1 -1
  134. package/package.json +16 -15
  135. package/LICENSE +0 -21
  136. package/output/app/auth.test.d.ts +0 -2
  137. package/output/app/auth.test.d.ts.map +0 -1
  138. package/output/app/auth.test.js +0 -148
  139. package/output/app/boot.d.ts +0 -39
  140. package/output/app/boot.d.ts.map +0 -1
  141. package/output/app/boot.js +0 -140
  142. package/output/app/config.test.d.ts +0 -2
  143. package/output/app/config.test.d.ts.map +0 -1
  144. package/output/app/config.test.js +0 -132
  145. package/output/app/context.d.ts +0 -70
  146. package/output/app/context.d.ts.map +0 -1
  147. package/output/app/context.js +0 -73
  148. package/output/app/core/cookie.d.ts +0 -25
  149. package/output/app/core/cookie.d.ts.map +0 -1
  150. package/output/app/core/identity.d.ts +0 -12
  151. package/output/app/core/identity.d.ts.map +0 -1
  152. package/output/app/core/index.d.ts +0 -44
  153. package/output/app/core/index.d.ts.map +0 -1
  154. package/output/app/core/index.js +0 -57
  155. package/output/app/core/logging.d.ts +0 -13
  156. package/output/app/core/logging.d.ts.map +0 -1
  157. package/output/app/core/openapi.d.ts +0 -17
  158. package/output/app/core/openapi.d.ts.map +0 -1
  159. package/output/app/core/request-id.d.ts.map +0 -1
  160. package/output/app/core/response.d.ts +0 -41
  161. package/output/app/core/response.d.ts.map +0 -1
  162. package/output/app/define-server.negative-cases.d.ts +0 -40
  163. package/output/app/define-server.negative-cases.d.ts.map +0 -1
  164. package/output/app/define-server.negative-cases.js +0 -268
  165. package/output/app/define-server.test.d.ts +0 -2
  166. package/output/app/define-server.test.d.ts.map +0 -1
  167. package/output/app/define-server.test.js +0 -97
  168. package/output/app/entries.d.ts +0 -29
  169. package/output/app/entries.d.ts.map +0 -1
  170. package/output/app/entries.js +0 -64
  171. package/output/app/env.d.ts +0 -59
  172. package/output/app/env.d.ts.map +0 -1
  173. package/output/app/env.js +0 -66
  174. package/output/app/env.test.js +0 -124
  175. package/output/app/error-contract.test.d.ts +0 -2
  176. package/output/app/error-contract.test.d.ts.map +0 -1
  177. package/output/app/error-contract.test.js +0 -227
  178. package/output/app/errors.d.ts +0 -20
  179. package/output/app/errors.d.ts.map +0 -1
  180. package/output/app/errors.js +0 -51
  181. package/output/app/generate-openapi.test.d.ts +0 -2
  182. package/output/app/generate-openapi.test.d.ts.map +0 -1
  183. package/output/app/generate-openapi.test.js +0 -123
  184. package/output/app/lifecycle-logging.test.d.ts +0 -2
  185. package/output/app/lifecycle-logging.test.d.ts.map +0 -1
  186. package/output/app/lifecycle-logging.test.js +0 -155
  187. package/output/app/lifecycle.d.ts +0 -70
  188. package/output/app/lifecycle.d.ts.map +0 -1
  189. package/output/app/lifecycle.test.d.ts +0 -2
  190. package/output/app/lifecycle.test.d.ts.map +0 -1
  191. package/output/app/lifecycle.test.js +0 -152
  192. package/output/app/middleware/auth.d.ts +0 -71
  193. package/output/app/middleware/auth.d.ts.map +0 -1
  194. package/output/app/middleware/auth.js +0 -190
  195. package/output/app/middleware/context.d.ts +0 -13
  196. package/output/app/middleware/context.d.ts.map +0 -1
  197. package/output/app/middleware/context.js +0 -15
  198. package/output/app/middleware/cors.d.ts +0 -14
  199. package/output/app/middleware/cors.d.ts.map +0 -1
  200. package/output/app/middleware/cors.js +0 -30
  201. package/output/app/middleware/index.d.ts +0 -17
  202. package/output/app/middleware/index.d.ts.map +0 -1
  203. package/output/app/middleware/index.js +0 -16
  204. package/output/app/middleware/rate-limit.d.ts +0 -24
  205. package/output/app/middleware/rate-limit.d.ts.map +0 -1
  206. package/output/app/middleware/rate-limit.js +0 -46
  207. package/output/app/middleware-order.test.d.ts +0 -2
  208. package/output/app/middleware-order.test.d.ts.map +0 -1
  209. package/output/app/middleware-order.test.js +0 -148
  210. package/output/app/permission-operations.d.ts +0 -48
  211. package/output/app/permission-operations.d.ts.map +0 -1
  212. package/output/app/permission-operations.js +0 -77
  213. package/output/app/permission-operations.test.d.ts +0 -2
  214. package/output/app/permission-operations.test.d.ts.map +0 -1
  215. package/output/app/permission-operations.test.js +0 -72
  216. package/output/app/permission-registration.test.d.ts +0 -2
  217. package/output/app/permission-registration.test.d.ts.map +0 -1
  218. package/output/app/permission-registration.test.js +0 -138
  219. package/output/app/redis.d.ts.map +0 -1
  220. package/output/app/registered-operations.test.d.ts +0 -2
  221. package/output/app/registered-operations.test.d.ts.map +0 -1
  222. package/output/app/registered-operations.test.js +0 -57
  223. package/output/app/request-id.test.d.ts +0 -2
  224. package/output/app/request-id.test.d.ts.map +0 -1
  225. package/output/app/request-id.test.js +0 -130
  226. package/output/app/resources.test.d.ts +0 -2
  227. package/output/app/resources.test.d.ts.map +0 -1
  228. package/output/app/resources.test.js +0 -199
  229. package/output/app/shutdown.d.ts +0 -22
  230. package/output/app/shutdown.d.ts.map +0 -1
  231. package/output/app/shutdown.js +0 -30
  232. package/output/app/shutdown.test.d.ts +0 -2
  233. package/output/app/shutdown.test.d.ts.map +0 -1
  234. package/output/app/shutdown.test.js +0 -103
  235. package/output/app/signal.d.ts +0 -12
  236. package/output/app/signal.d.ts.map +0 -1
  237. package/output/app/smoke.test.d.ts +0 -2
  238. package/output/app/smoke.test.d.ts.map +0 -1
  239. package/output/app/smoke.test.js +0 -210
  240. package/output/app/test-support.d.ts +0 -21
  241. package/output/app/test-support.d.ts.map +0 -1
  242. package/output/app/test-support.js +0 -24
  243. package/output/app/type-augment.d.ts.map +0 -1
  244. package/output/app/types.d.ts +0 -228
  245. package/output/app/types.d.ts.map +0 -1
  246. package/output/context/index.d.ts +0 -6
  247. package/output/context/index.d.ts.map +0 -1
  248. package/output/context/index.js +0 -14
  249. package/output/rate-limiter/index.d.ts +0 -5
  250. package/output/rate-limiter/index.d.ts.map +0 -1
  251. package/output/rate-limiter/index.js +0 -4
  252. package/output/rate-limiter/memory-store.d.ts.map +0 -1
  253. package/output/rate-limiter/rate-limiter.d.ts.map +0 -1
  254. package/output/rate-limiter/redis-store.d.ts.map +0 -1
  255. package/output/rate-limiter/redis-store.test.d.ts.map +0 -1
  256. package/output/rate-limiter/types.d.ts.map +0 -1
  257. package/output/redis-cache/index.d.ts +0 -4
  258. package/output/redis-cache/index.d.ts.map +0 -1
  259. package/output/redis-cache/index.js +0 -1
  260. package/output/redis-cache/redis-cache.d.ts.map +0 -1
  261. package/output/redis-cache/redis-cache.test.d.ts +0 -2
  262. package/output/redis-cache/redis-cache.test.d.ts.map +0 -1
  263. package/output/schema/index.d.ts.map +0 -1
  264. /package/output/{app → core}/env.test.d.ts +0 -0
  265. /package/output/{rate-limiter → core/modules/auth-provider}/types.js +0 -0
  266. /package/output/{context → core/modules/context}/index.test.d.ts +0 -0
  267. /package/output/{context → core/modules/context}/index.test.js +0 -0
  268. /package/output/{app → core}/type-augment.js +0 -0
  269. /package/output/{app → core}/types.js +0 -0
  270. /package/output/{rate-limiter/rate-limiter.js → rate-limit/limiter.js} +0 -0
  271. /package/output/{rate-limiter → rate-limit}/memory-store.d.ts +0 -0
  272. /package/output/{rate-limiter → rate-limit}/memory-store.js +0 -0
  273. /package/output/{rate-limiter → rate-limit}/redis-store.d.ts +0 -0
  274. /package/output/{rate-limiter → rate-limit}/redis-store.js +0 -0
  275. /package/output/{rate-limiter → rate-limit}/redis-store.test.d.ts +0 -0
  276. /package/output/{redis-cache/redis-cache.js → redis/cache.js} +0 -0
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../source/app/middleware/context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD;;;;;GAKG;AAEH,MAAM,WAAW,wBAAwB;IACvC,iBAAiB;IACjB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,wBAAgB,uBAAuB,CAAC,OAAO,GAAE,wBAA6B,GAAG,cAAc,CAa9F"}
@@ -1,15 +0,0 @@
1
- import { createContext } from '../../context/index.js';
2
- export function createContextMiddleware(options = {}) {
3
- const decorateAs = options.decorateAs ?? 'ctx';
4
- return {
5
- name: 'context',
6
- register: (app) => {
7
- app.decorateRequest(decorateAs, null);
8
- app.addHook('onRequest', (request, _reply, done) => {
9
- ;
10
- request[decorateAs] = createContext(request);
11
- done();
12
- });
13
- },
14
- };
15
- }
@@ -1,14 +0,0 @@
1
- import type { MiddlewareContext, MiddlewareUnit } from '../types.js';
2
- /**
3
- * 标准中间件单元 `cors`(可替换)。
4
- *
5
- * 不传 `origins` → **每次请求现读**配置的 `cors` 键(配置加载后即生效,不得缓存);
6
- * **未配置即放行任意源 + 首次请求 warn 一次**(对齐四家现状)。构造期**不读配置**,
7
- * 故出 spec 路径(配置不可用)不受影响。
8
- */
9
- export interface CorsMiddlewareOptions {
10
- origins?: string[] | '*';
11
- credentials?: boolean;
12
- }
13
- export declare function createCorsMiddleware<C = unknown, S = unknown>(ctx: MiddlewareContext<C, S>, options?: CorsMiddlewareOptions): MiddlewareUnit;
14
- //# sourceMappingURL=cors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cors.d.ts","sourceRoot":"","sources":["../../../source/app/middleware/cors.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEpE;;;;;;GAMG;AAEH,MAAM,WAAW,qBAAqB;IACpC,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,GAAG,CAAA;IACxB,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB;AAOD,wBAAgB,oBAAoB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,GAAE,qBAA0B,GAAG,cAAc,CA6BhJ"}
@@ -1,30 +0,0 @@
1
- import cors from '@fastify/cors';
2
- import { readConfigKey } from '../context.js';
3
- export function createCorsMiddleware(ctx, options = {}) {
4
- return {
5
- name: 'cors',
6
- register: async (app) => {
7
- let warned = false;
8
- // 请求期现读:`cors` 配置变化立即生效;构造期不读
9
- const resolveOrigins = () => {
10
- const configured = readConfigKey(ctx.config, 'cors');
11
- return options.origins ?? (configured?.enabled ? configured.origins : undefined);
12
- };
13
- await app.register(cors, {
14
- origin: (origin, callback) => {
15
- const origins = resolveOrigins();
16
- if (origins === undefined || origins === '*') {
17
- if (!warned) {
18
- warned = true;
19
- ctx.log.warn({}, '[middleware] cors origins not configured; allowing any origin');
20
- }
21
- callback(null, true);
22
- return;
23
- }
24
- callback(null, origins.includes(origin ?? ''));
25
- },
26
- credentials: options.credentials ?? true,
27
- });
28
- },
29
- };
30
- }
@@ -1,17 +0,0 @@
1
- import type { MiddlewareContext, MiddlewareUnit } from '../types.js';
2
- export { createCorsMiddleware } from './cors.js';
3
- export { createContextMiddleware } from './context.js';
4
- export { createAuthMiddleware, defaultAuthSkip } from './auth.js';
5
- export { createRateLimitMiddleware } from './rate-limit.js';
6
- export type { CorsMiddlewareOptions } from './cors.js';
7
- export type { ContextMiddlewareOptions } from './context.js';
8
- export type { AccountIntegrationConfig, AuthMiddlewareOptions, AuthSkip, AuthVerifyInput, AuthVerifyMetadata, AuthVerifyResult, } from './auth.js';
9
- export type { RateLimitMiddlewareOptions } from './rate-limit.js';
10
- /**
11
- * 默认中间件预设:`[cors, context, auth]`。
12
- *
13
- * 顺序 `cors → context → auth` 只是**预设内容**,不是框架常量:可 `...defaultMiddlewares(ctx)`
14
- * 后替换 / 插入 / 删除任意项。`rateLimit` **不在**预设内(唯一一个默认不装的项)。
15
- */
16
- export declare function defaultMiddlewares<C = unknown, S = unknown>(ctx: MiddlewareContext<C, S>): MiddlewareUnit[];
17
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/app/middleware/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AACtD,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AACjE,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAA;AAE3D,YAAY,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAA;AACtD,YAAY,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAA;AAC5D,YAAY,EACV,wBAAwB,EACxB,qBAAqB,EACrB,QAAQ,EACR,eAAe,EACf,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,WAAW,CAAA;AAClB,YAAY,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAA;AAEjE;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EACzD,GAAG,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAC3B,cAAc,EAAE,CAElB"}
@@ -1,16 +0,0 @@
1
- import { createAuthMiddleware } from './auth.js';
2
- import { createContextMiddleware } from './context.js';
3
- import { createCorsMiddleware } from './cors.js';
4
- export { createCorsMiddleware } from './cors.js';
5
- export { createContextMiddleware } from './context.js';
6
- export { createAuthMiddleware, defaultAuthSkip } from './auth.js';
7
- export { createRateLimitMiddleware } from './rate-limit.js';
8
- /**
9
- * 默认中间件预设:`[cors, context, auth]`。
10
- *
11
- * 顺序 `cors → context → auth` 只是**预设内容**,不是框架常量:可 `...defaultMiddlewares(ctx)`
12
- * 后替换 / 插入 / 删除任意项。`rateLimit` **不在**预设内(唯一一个默认不装的项)。
13
- */
14
- export function defaultMiddlewares(ctx) {
15
- return [createCorsMiddleware(ctx), createContextMiddleware(), createAuthMiddleware(ctx)];
16
- }
@@ -1,24 +0,0 @@
1
- import type { MiddlewareContext, MiddlewareUnit } from '../types.js';
2
- import type { RateLimitStore } from '../../rate-limiter/types.js';
3
- /**
4
- * 标准中间件单元 `rateLimit`(可替换;**不在默认预设内**)。
5
- *
6
- * 路由级限流;存储后端由调用方注入(缺省进程内 memory)。`TOO_MANY_REQUESTS` 经 `UserError`
7
- * 仍映射 **400**。路由未声明 `config.rateLimit` 且未给 `defaultLimit` 时该项空转。
8
- *
9
- * 注:中间件的 ctx 看不到 `resources`(中间件不得直连数据层),因此 Redis store 需由调用方
10
- * 在 `services` 侧构造后注入(`store`),框架不再自行探测 Redis。
11
- */
12
- export interface RateLimitMiddlewareOptions {
13
- /** 存储后端(缺省进程内 memory) */
14
- store?: RateLimitStore;
15
- /** 限流键前缀(缺省 `<service>:`) */
16
- keyPrefix?: string;
17
- /** 缺省规则:路由未声明 `config.rateLimit` 时使用 */
18
- defaultLimit?: {
19
- max: number;
20
- windowMs: number;
21
- };
22
- }
23
- export declare function createRateLimitMiddleware<C = unknown, S = unknown>(ctx: MiddlewareContext<C, S>, options?: RateLimitMiddlewareOptions): MiddlewareUnit;
24
- //# sourceMappingURL=rate-limit.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../../../source/app/middleware/rate-limit.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AACpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAGjE;;;;;;;;GAQG;AAEH,MAAM,WAAW,0BAA0B;IACzC,yBAAyB;IACzB,KAAK,CAAC,EAAE,cAAc,CAAA;IACtB,6BAA6B;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,wCAAwC;IACxC,YAAY,CAAC,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;CACjD;AAsBD,wBAAgB,yBAAyB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,GAAE,0BAA+B,GAAG,cAAc,CA0B1J"}
@@ -1,46 +0,0 @@
1
- import { createRateLimitKey, RateLimiter } from '../../rate-limiter/rate-limiter.js';
2
- import { MemoryRateLimitStore } from '../../rate-limiter/memory-store.js';
3
- import { readServerIdentity } from '../core/identity.js';
4
- import { UserError } from '../errors.js';
5
- function resolveRule(request, defaultRule) {
6
- const configured = request.routeOptions?.config?.rateLimit;
7
- return configured ?? defaultRule;
8
- }
9
- function buildKeys(request, keys) {
10
- return keys.map((key) => {
11
- if (key === 'ip') {
12
- return `ip:${request.ip}`;
13
- }
14
- if (key === 'user') {
15
- return `user:${String(request.user?.id ?? 'anonymous')}`;
16
- }
17
- if (key === 'path') {
18
- return `path:${request.routeOptions?.url ?? request.url}`;
19
- }
20
- return `${key}:${String(request[key] ?? '')}`;
21
- });
22
- }
23
- export function createRateLimitMiddleware(ctx, options = {}) {
24
- void ctx;
25
- return {
26
- name: 'rateLimit',
27
- register: (app) => {
28
- const limiter = new RateLimiter({ store: options.store ?? new MemoryRateLimitStore() });
29
- const prefix = options.keyPrefix ?? `${readServerIdentity(app)?.service ?? ''}:`;
30
- const defaultRule = options.defaultLimit
31
- ? { limit: options.defaultLimit.max, window: options.defaultLimit.windowMs / 1000 }
32
- : undefined;
33
- app.addHook('preValidation', async (request) => {
34
- const rule = resolveRule(request, defaultRule);
35
- if (!rule) {
36
- return;
37
- }
38
- const keys = buildKeys(request, rule.keys ?? ['ip']).map((key) => createRateLimitKey(prefix, key));
39
- const result = await limiter.checkMultiLimit(keys, { limit: rule.limit, window: rule.window });
40
- if (!result.allowed) {
41
- throw new UserError('TOO_MANY_REQUESTS', undefined, '请求过于频繁');
42
- }
43
- });
44
- },
45
- };
46
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=middleware-order.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"middleware-order.test.d.ts","sourceRoot":"","sources":["../../source/app/middleware-order.test.ts"],"names":[],"mappings":""}
@@ -1,148 +0,0 @@
1
- import { describe, expect, expectTypeOf, it } from 'vitest';
2
- import { defineServer, startServer } from './entries.js';
3
- import { defaultMiddlewares } from './middleware/index.js';
4
- /**
5
- * 中间件数组序(N1 §1 / §4)。
6
- *
7
- * `middlewares(ctx)` 返回**有序数组**,框架**严格按数组序**注册(无键、无枚举、无内置相对顺序魔法);
8
- * `rateLimit` **不在**默认预设内。中间件单元是 `{ name?, register(app) }`:**不参与生命周期**。
9
- */
10
- const fakeMiddlewareContext = {
11
- app: {},
12
- log: { debug: () => { }, info: () => { }, warn: () => { }, error: () => { } },
13
- services: {},
14
- config: {},
15
- };
16
- describe('ctx 可见性(N1 §2)', () => {
17
- it('MiddlewareContext 有 app / log / services / config;没有 resources / env / disposer', () => {
18
- expectTypeOf().toHaveProperty('app');
19
- expectTypeOf().toHaveProperty('services');
20
- expectTypeOf().toHaveProperty('config');
21
- // @ts-expect-error 中间件看不到 resources(禁直连数据层)
22
- void 0..resources;
23
- // @ts-expect-error 中间件看不到 env
24
- void 0..env;
25
- // @ts-expect-error 中间件看不到 disposer(disposer 只属 services)
26
- void 0..disposer;
27
- });
28
- });
29
- describe('默认预设', () => {
30
- it('defaultMiddlewares 为 [cors, context, auth],且不含 rateLimit', () => {
31
- const units = defaultMiddlewares(fakeMiddlewareContext);
32
- expect(units.map((unit) => unit.name)).toEqual(['cors', 'context', 'auth']);
33
- expect(units.map((unit) => unit.name)).not.toContain('rateLimit');
34
- });
35
- });
36
- describe('数组序即注册序', () => {
37
- it('严格按数组序执行每一项注册的钩子', async () => {
38
- const calls = [];
39
- const makeUnit = (name) => ({
40
- name,
41
- register: (app) => {
42
- app.addHook('preValidation', (_request, _reply, done) => {
43
- calls.push(name);
44
- done();
45
- });
46
- },
47
- });
48
- const server = defineServer({
49
- app: { service: 'middleware-order', title: 'Middleware Order', version: '1.0.0' },
50
- resources: () => ({}),
51
- config: () => ({}),
52
- services: () => ({}),
53
- middlewares: () => [makeUnit('third'), makeUnit('first'), makeUnit('second')],
54
- routers: (ctx) => {
55
- ctx.app.get('/ping', () => ({ pong: true }));
56
- },
57
- });
58
- const app = await startServer(server, { listen: false });
59
- const response = await app.inject({ method: 'GET', url: '/ping' });
60
- expect(response.statusCode).toBe(200);
61
- expect(calls).toEqual(['third', 'first', 'second']);
62
- await app.close();
63
- });
64
- it('ctx.services 与 services 槽位产物是同一个实例(M5 强类型)', async () => {
65
- const servicesValue = { user: { find: (id) => `user:${id}` } };
66
- let seen;
67
- let seenFind;
68
- const server = defineServer({
69
- app: { service: 'middleware-services', title: 'Middleware Services', version: '1.0.0' },
70
- resources: () => ({}),
71
- config: () => ({}),
72
- services: () => servicesValue,
73
- middlewares: (ctx) => {
74
- seen = ctx.services;
75
- seenFind = ctx.services.user.find;
76
- return [];
77
- },
78
- routers: (ctx) => {
79
- ctx.app.get('/ping', () => ({ pong: true }));
80
- },
81
- });
82
- const app = await startServer(server, { listen: false });
83
- expect(seen).toBe(servicesValue);
84
- expect(seenFind).toBe(servicesValue.user.find);
85
- await app.close();
86
- });
87
- it('注册的钩子返回 reply → 终止链(后续单元不执行)', async () => {
88
- const calls = [];
89
- const server = defineServer({
90
- app: { service: 'middleware-terminate', title: 'Middleware Terminate', version: '1.0.0' },
91
- resources: () => ({}),
92
- config: () => ({}),
93
- services: () => ({}),
94
- middlewares: () => [
95
- {
96
- name: 'short',
97
- register: (app) => {
98
- app.addHook('preValidation', (_request, reply, done) => {
99
- calls.push('short');
100
- void reply.status(418).send({ stopped: true });
101
- done();
102
- });
103
- },
104
- },
105
- {
106
- name: 'after',
107
- register: (app) => {
108
- app.addHook('preValidation', (_request, _reply, done) => {
109
- calls.push('after');
110
- done();
111
- });
112
- },
113
- },
114
- ],
115
- routers: (ctx) => {
116
- ctx.app.get('/ping', () => ({ pong: true }));
117
- },
118
- });
119
- const app = await startServer(server, { listen: false });
120
- const response = await app.inject({ method: 'GET', url: '/ping' });
121
- expect(response.statusCode).toBe(418);
122
- expect(calls).toEqual(['short']);
123
- await app.close();
124
- });
125
- it('出 spec 路径也注册中间件(保证 schema 一致)', async () => {
126
- const { generateOpenApi } = await import('./entries.js');
127
- const calls = [];
128
- const server = defineServer({
129
- app: { service: 'middleware-openapi', title: 'Middleware OpenApi', version: '1.0.0' },
130
- resources: () => ({}),
131
- config: () => ({}),
132
- services: () => ({}),
133
- middlewares: () => [
134
- {
135
- name: 'probe',
136
- register: () => {
137
- calls.push('register:probe');
138
- },
139
- },
140
- ],
141
- routers: (ctx) => {
142
- ctx.app.get('/ping', () => ({ pong: true }));
143
- },
144
- });
145
- await generateOpenApi(server);
146
- expect(calls).toEqual(['register:probe']);
147
- });
148
- });
@@ -1,48 +0,0 @@
1
- import type { FastifyInstance } from 'fastify';
2
- /**
3
- * 权限操作提取(设计 §5.2)——**框架内置**。
4
- *
5
- * 两条来源:
6
- *
7
- * 1. **路由注册表**(`installRegisteredOperations` / `readRegisteredOperations`):core 装配期
8
- * 在 root 挂 `onRoute` 钩子,采集每条路由的 `{ method, url, operationId, summary, description, tags }`。
9
- * `auth` 的权限自注册只读这张表,**不依赖 `app.swagger()`**(swagger 只负责出文档)。
10
- * 2. **OpenAPI 文档提取**(`extractPermissionOperations`):从 `paths` 提取。
11
- * 各仓的 `onReady` 自注册(如 account 作为签发方走进程内调用)用它与 `app.swagger()` 配合,
12
- * **替代各仓自写实现**(已从公共面导出)。
13
- *
14
- * 目录元素形状(`{ operation, summary?, description?, tags? }`)与各仓原自写实现一致:
15
- *
16
- * - 缺 `operationId` 的条目(如 `parameters` / vendor 扩展 / 文档路由)**跳过**;
17
- * - 缺 `paths` / 空文档 → 返回 `[]`;
18
- * - `summary` / `description` / `tags` 缺省分别归一为 `''` / `''` / `[]`。
19
- */
20
- /** 单条权限操作(对齐 account `POST /permission/operation/register` 的 `operations` 元素) */
21
- export interface PermissionOperation {
22
- operation: string;
23
- summary: string;
24
- description: string;
25
- tags: string[];
26
- }
27
- /** 单条已注册路由的采集项(`onRoute` 期收集;`operationId` 缺省表示该路由未声明) */
28
- export interface RegisteredOperation {
29
- method: string;
30
- url: string;
31
- operationId?: string;
32
- summary?: string;
33
- description?: string;
34
- tags?: string[];
35
- }
36
- /**
37
- * 在 root 挂 `onRoute` 钩子,收集框架自身可见的**路由注册表**(设计 §5.2)。
38
- *
39
- * 必须在**路由注册之前**(core 装配期)调用:钩子挂在 root,随后 `routers` 阶段注册的路由逐条被采集。
40
- * 该表与 swagger 解耦:swagger 只负责出文档,`auth` 的权限自注册只读这张表。
41
- */
42
- export declare function installRegisteredOperations(app: FastifyInstance): void;
43
- /** 读取 root `onRoute` 采集的路由注册表;未安装(未走框架装配)时返回空数组。 */
44
- export declare function readRegisteredOperations(app: FastifyInstance): RegisteredOperation[];
45
- /** 从路由注册表筛出声明了 `operationId` 的项,归一为权限操作目录元素 */
46
- export declare function toPermissionOperations(operations: readonly RegisteredOperation[]): PermissionOperation[];
47
- export declare function extractPermissionOperations(openApiSpec: unknown): PermissionOperation[];
48
- //# sourceMappingURL=permission-operations.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"permission-operations.d.ts","sourceRoot":"","sources":["../../source/app/permission-operations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAe,MAAM,SAAS,CAAA;AAE3D;;;;;;;;;;;;;;;;;GAiBG;AAEH,iFAAiF;AACjF,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;IACnB,IAAI,EAAE,MAAM,EAAE,CAAA;CACf;AAeD,0DAA0D;AAC1D,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;CAChB;AAaD;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,GAAG,EAAE,eAAe,GAAG,IAAI,CA+BtE;AAED,oDAAoD;AACpD,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,eAAe,GAAG,mBAAmB,EAAE,CAIpF;AAED,+CAA+C;AAC/C,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,SAAS,mBAAmB,EAAE,GAAG,mBAAmB,EAAE,CAcxG;AAED,wBAAgB,2BAA2B,CAAC,WAAW,EAAE,OAAO,GAAG,mBAAmB,EAAE,CAoBvF"}
@@ -1,77 +0,0 @@
1
- /** 路由注册表在 `app` 上的存放键(Symbol,不进公共契约) */
2
- const REGISTERED_OPERATIONS = Symbol.for('@taicode/common-server/registered-operations');
3
- /**
4
- * 在 root 挂 `onRoute` 钩子,收集框架自身可见的**路由注册表**(设计 §5.2)。
5
- *
6
- * 必须在**路由注册之前**(core 装配期)调用:钩子挂在 root,随后 `routers` 阶段注册的路由逐条被采集。
7
- * 该表与 swagger 解耦:swagger 只负责出文档,`auth` 的权限自注册只读这张表。
8
- */
9
- export function installRegisteredOperations(app) {
10
- const collected = [];
11
- Object.defineProperty(app, REGISTERED_OPERATIONS, {
12
- value: collected,
13
- enumerable: false,
14
- configurable: true,
15
- });
16
- app.addHook('onRoute', (routeOptions) => {
17
- const routeConfig = (routeOptions.config ?? {});
18
- // 与 `@fastify/swagger` 同口径:Fastify 默认为 GET 自动生成 HEAD 路由;未显式
19
- // `config.swagger.exposeHeadRoute = true` 时跳过,避免与 GET 重复(权限目录保持同源)。
20
- if (routeOptions.method === 'HEAD' && routeConfig.swagger?.exposeHeadRoute !== true) {
21
- return;
22
- }
23
- const schema = (routeOptions.schema ?? {});
24
- const methods = Array.isArray(routeOptions.method)
25
- ? routeOptions.method
26
- : [routeOptions.method];
27
- for (const method of methods) {
28
- collected.push({
29
- method,
30
- url: routeOptions.url,
31
- operationId: schema.operationId,
32
- summary: schema.summary,
33
- description: schema.description,
34
- tags: schema.tags,
35
- });
36
- }
37
- });
38
- }
39
- /** 读取 root `onRoute` 采集的路由注册表;未安装(未走框架装配)时返回空数组。 */
40
- export function readRegisteredOperations(app) {
41
- return (app[REGISTERED_OPERATIONS] ?? []);
42
- }
43
- /** 从路由注册表筛出声明了 `operationId` 的项,归一为权限操作目录元素 */
44
- export function toPermissionOperations(operations) {
45
- const result = [];
46
- for (const entry of operations) {
47
- if (!entry.operationId) {
48
- continue;
49
- }
50
- result.push({
51
- operation: entry.operationId,
52
- summary: entry.summary ?? '',
53
- description: entry.description ?? '',
54
- tags: entry.tags ?? [],
55
- });
56
- }
57
- return result;
58
- }
59
- export function extractPermissionOperations(openApiSpec) {
60
- // 出 spec 路径拿到的文档是未知形态;只按结构取字段,缺什么就跳过什么
61
- const spec = (openApiSpec ?? {});
62
- const operations = [];
63
- for (const pathItem of Object.values(spec.paths ?? {})) {
64
- for (const operation of Object.values(pathItem ?? {})) {
65
- if (!operation?.operationId) {
66
- continue;
67
- }
68
- operations.push({
69
- operation: operation.operationId,
70
- summary: operation.summary ?? '',
71
- description: operation.description ?? '',
72
- tags: operation.tags ?? [],
73
- });
74
- }
75
- }
76
- return operations;
77
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=permission-operations.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"permission-operations.test.d.ts","sourceRoot":"","sources":["../../source/app/permission-operations.test.ts"],"names":[],"mappings":""}
@@ -1,72 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { extractPermissionOperations } from './permission-operations.js';
3
- /**
4
- * `extractPermissionOperations`(设计 §5.2,框架内置)契约。
5
- *
6
- * 口径与各仓原自写实现一致:逐 path → 逐 method,缺 `operationId` 跳过;缺 `paths` / 空文档 → `[]`;
7
- * `summary` / `description` / `tags` 缺省归一为 `''` / `''` / `[]`。
8
- */
9
- describe('extractPermissionOperations(设计 §5.2)', () => {
10
- it('提取 operationId 与可选元信息(summary / description / tags)', () => {
11
- expect(extractPermissionOperations({
12
- paths: {
13
- '/release/list': {
14
- post: {
15
- operationId: 'listRelease',
16
- summary: 'List releases',
17
- description: 'Returns available releases.',
18
- tags: ['Release'],
19
- },
20
- },
21
- },
22
- })).toEqual([
23
- {
24
- operation: 'listRelease',
25
- summary: 'List releases',
26
- description: 'Returns available releases.',
27
- tags: ['Release'],
28
- },
29
- ]);
30
- });
31
- it('可选元信息缺省归一,且跳过无 operationId 的条目', () => {
32
- expect(extractPermissionOperations({
33
- paths: {
34
- '/release/create': {
35
- post: { operationId: 'createRelease' },
36
- parameters: { description: 'Shared parameters' },
37
- },
38
- },
39
- })).toEqual([
40
- {
41
- operation: 'createRelease',
42
- summary: '',
43
- description: '',
44
- tags: [],
45
- },
46
- ]);
47
- });
48
- it('缺 paths 时返回空数组', () => {
49
- expect(extractPermissionOperations({})).toEqual([]);
50
- });
51
- it('空文档 / 非对象入参返回空数组', () => {
52
- expect(extractPermissionOperations({ paths: {} })).toEqual([]);
53
- expect(extractPermissionOperations(undefined)).toEqual([]);
54
- });
55
- it('多路径 + 多方法:按 paths → methods 的遍历序全量提取', () => {
56
- expect(extractPermissionOperations({
57
- paths: {
58
- '/widget/list': {
59
- get: { operationId: 'listWidget', tags: ['Widget'] },
60
- post: { operationId: 'queryWidget' },
61
- },
62
- '/widget/create': {
63
- post: { operationId: 'createWidget', summary: 'Create widget' },
64
- },
65
- },
66
- })).toEqual([
67
- { operation: 'listWidget', summary: '', description: '', tags: ['Widget'] },
68
- { operation: 'queryWidget', summary: '', description: '', tags: [] },
69
- { operation: 'createWidget', summary: 'Create widget', description: '', tags: [] },
70
- ]);
71
- });
72
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=permission-registration.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"permission-registration.test.d.ts","sourceRoot":"","sources":["../../source/app/permission-registration.test.ts"],"names":[],"mappings":""}