@faapi/faapi 1.0.0-canary.d7438c8 → 1.0.1-canary.218585b
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 +177 -1
- package/dist/index.js +1340 -1140
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import * as node_http from 'node:http';
|
|
|
2
2
|
import { Server, IncomingMessage, ServerResponse } from 'node:http';
|
|
3
3
|
import { Socket } from 'node:net';
|
|
4
4
|
import ts from 'typescript';
|
|
5
|
+
import { WebSocket } from 'ws';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* SSE(Server-Sent Events)支持
|
|
@@ -1122,6 +1123,181 @@ declare function createContext(request: Request, params: Record<string, string>,
|
|
|
1122
1123
|
*/
|
|
1123
1124
|
declare function invokeHandler(handler: (...args: unknown[]) => unknown, ctx: FaapiContext, body?: unknown, middlewares?: FaapiMiddleware[], injectors?: InjectorMap): Promise<Response>;
|
|
1124
1125
|
|
|
1126
|
+
/**
|
|
1127
|
+
* createTestServer 入参
|
|
1128
|
+
*
|
|
1129
|
+
* 业务方一行代码启动带 schema 校验的 E2E 测试服务器。
|
|
1130
|
+
* 详见 src/testServer.md。
|
|
1131
|
+
*/
|
|
1132
|
+
interface TestServerOptions {
|
|
1133
|
+
/** 项目根目录(路由源码所在,必填) */
|
|
1134
|
+
rootDir: string;
|
|
1135
|
+
patterns?: string[];
|
|
1136
|
+
/**
|
|
1137
|
+
* schema 产物输出目录(绝对路径或相对 rootDir)。
|
|
1138
|
+
* 不传时自动 mkdtemp 生成临时目录,close() 时清理。
|
|
1139
|
+
* 传值时 close() 仍会清理该目录。
|
|
1140
|
+
*/
|
|
1141
|
+
dist?: string;
|
|
1142
|
+
/** CORS 中间件配置,默认 false(禁用,避免污染断言) */
|
|
1143
|
+
cors?: CorsOptions | boolean;
|
|
1144
|
+
/** 安全头配置,默认 false */
|
|
1145
|
+
helmet?: HelmetOptions | boolean;
|
|
1146
|
+
/** 请求日志配置,默认 false(避免污染测试输出) */
|
|
1147
|
+
logger?: LoggerOptions | boolean;
|
|
1148
|
+
/** 全局中间件(外层洋葱) */
|
|
1149
|
+
middlewares?: FaapiMiddleware[];
|
|
1150
|
+
/** 全局注入器 */
|
|
1151
|
+
injectors?: InjectorMap;
|
|
1152
|
+
/** 请求错误钩子(在错误响应生成后调用,用于副作用) */
|
|
1153
|
+
onError?: (error: unknown, ctx: FaapiContext) => Promise<void> | void;
|
|
1154
|
+
/** 业务配置(注入到 ctx.config) */
|
|
1155
|
+
config?: Record<string, unknown>;
|
|
1156
|
+
/** 请求体大小限制(字节),默认 10MB */
|
|
1157
|
+
bodyLimit?: number;
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* createTestServer 返回值
|
|
1161
|
+
*
|
|
1162
|
+
* 业务方通过 baseUrl 发 fetch 请求,close() 一行完成 teardown。
|
|
1163
|
+
*/
|
|
1164
|
+
interface TestServer {
|
|
1165
|
+
/** Node.js HTTP Server 实例(已 listen) */
|
|
1166
|
+
server: Server;
|
|
1167
|
+
/** 形如 http://localhost:<随机端口> */
|
|
1168
|
+
baseUrl: string;
|
|
1169
|
+
/** 排序后的路由清单 */
|
|
1170
|
+
routes: RouteManifest;
|
|
1171
|
+
/** WebSocket 路由清单 */
|
|
1172
|
+
wsRoutes: WsRouteManifest;
|
|
1173
|
+
/** schema 临时目录绝对路径(业务方调试时可查看生成的 zod.js) */
|
|
1174
|
+
schemaDist: string;
|
|
1175
|
+
/**
|
|
1176
|
+
* 关闭 server + 清理 schema 目录 + 清空 schema 模块缓存
|
|
1177
|
+
*
|
|
1178
|
+
* 内部顺序:
|
|
1179
|
+
* 1. server.closeAllConnections?.()(Node 18+,强制断开 WS / 长连接)
|
|
1180
|
+
* 2. server.close()
|
|
1181
|
+
* 3. fs.rm(schemaDist, { recursive, force })
|
|
1182
|
+
* 4. invalidateSchemaCache()
|
|
1183
|
+
*
|
|
1184
|
+
* 幂等:重复调用不会重复清理。
|
|
1185
|
+
*/
|
|
1186
|
+
close(): Promise<void>;
|
|
1187
|
+
}
|
|
1188
|
+
/**
|
|
1189
|
+
* 一键启动带 schema 校验的 E2E 测试服务器
|
|
1190
|
+
*
|
|
1191
|
+
* 内部流程:
|
|
1192
|
+
* 1. scanRoutes 扫描路由
|
|
1193
|
+
* 2. sortRoutes 排序
|
|
1194
|
+
* 3. mkdtemp 创建临时 schema 目录(或用传入的 dist)
|
|
1195
|
+
* 4. generateSchemaFiles 生成 zod.js
|
|
1196
|
+
* 5. createServer 创建 server(默认禁用 CORS/Helmet/Logger,避免污染断言)
|
|
1197
|
+
* 6. server.listen(0) 随机端口
|
|
1198
|
+
* 7. 返回 TestServer
|
|
1199
|
+
*
|
|
1200
|
+
* 详见 src/testServer.md。
|
|
1201
|
+
*
|
|
1202
|
+
* @param options rootDir 必填,其余可选
|
|
1203
|
+
* @returns TestServer 实例
|
|
1204
|
+
*/
|
|
1205
|
+
declare function createTestServer(options: TestServerOptions): Promise<TestServer>;
|
|
1206
|
+
|
|
1207
|
+
/**
|
|
1208
|
+
* WebSocket 测试客户端
|
|
1209
|
+
*
|
|
1210
|
+
* 公开导出 connectWs + MessageQueue + waitForWsOpen,业务方测试 WS 路由时
|
|
1211
|
+
* 免去手写"消息竞态防护 + 三事件监听 + 端口拼接"样板代码。
|
|
1212
|
+
*
|
|
1213
|
+
* 详见 src/wsTestClient.md。
|
|
1214
|
+
*/
|
|
1215
|
+
/**
|
|
1216
|
+
* connectWs 入参
|
|
1217
|
+
*/
|
|
1218
|
+
interface WsTestClientOptions {
|
|
1219
|
+
/** 等待 open 的超时(ms),默认 2000 */
|
|
1220
|
+
timeout?: number;
|
|
1221
|
+
/** 握手请求头(如 authorization) */
|
|
1222
|
+
headers?: Record<string, string>;
|
|
1223
|
+
/** WS 子协议 */
|
|
1224
|
+
protocols?: string | string[];
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* connectWs 返回值
|
|
1228
|
+
*
|
|
1229
|
+
* 业务方通过 ws.send() 发消息,queue.next() 取消息,close() 关闭。
|
|
1230
|
+
*/
|
|
1231
|
+
interface WsTestClient {
|
|
1232
|
+
/** ws 库原生实例,业务方可直接 ws.send() / ws.close() */
|
|
1233
|
+
ws: WebSocket;
|
|
1234
|
+
/** 已开始缓冲的消息队列,调 next(timeout?) 取下一条 */
|
|
1235
|
+
queue: MessageQueue;
|
|
1236
|
+
/**
|
|
1237
|
+
* 关闭 ws 并等待 'close' 事件
|
|
1238
|
+
*
|
|
1239
|
+
* 内部:
|
|
1240
|
+
* 1. 若 ws 仍 OPEN/CLOSING,调 ws.close()
|
|
1241
|
+
* 2. 等待 'close' 事件(超时 1000ms 强制 resolve)
|
|
1242
|
+
*
|
|
1243
|
+
* 幂等:重复调用不抛错。
|
|
1244
|
+
*/
|
|
1245
|
+
close(): Promise<void>;
|
|
1246
|
+
}
|
|
1247
|
+
/**
|
|
1248
|
+
* 消息队列:避免 once('message') 与服务端 onOpen 推送的竞态
|
|
1249
|
+
*
|
|
1250
|
+
* 服务端在 handleUpgrade 回调里同步触发 onOpen 并 send('connected'),
|
|
1251
|
+
* 客户端 'open' 事件触发后到注册 once('message') 之间存在窗口,
|
|
1252
|
+
* 若 'connected' 在此窗口内到达,once 会错过。
|
|
1253
|
+
*
|
|
1254
|
+
* 队列在创建 ws 时立即监听 'message',按 FIFO 顺序消费。
|
|
1255
|
+
*/
|
|
1256
|
+
declare class MessageQueue {
|
|
1257
|
+
private queue;
|
|
1258
|
+
private waiters;
|
|
1259
|
+
private listener;
|
|
1260
|
+
constructor(ws: WebSocket);
|
|
1261
|
+
/**
|
|
1262
|
+
* 取下一条消息
|
|
1263
|
+
*
|
|
1264
|
+
* 队列有则立即 resolve,无则注册 waiter 等待下一条 'message' 事件。
|
|
1265
|
+
* 超时未到 → reject('WebSocket message timeout'),waiter 被清理。
|
|
1266
|
+
*
|
|
1267
|
+
* @param timeout 超时毫秒,默认 2000
|
|
1268
|
+
*/
|
|
1269
|
+
next(timeout?: number): Promise<string>;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* Promise 化等待 ws 'open' 事件
|
|
1273
|
+
*
|
|
1274
|
+
* 同时监听 'open' / 'error' / 'close' 三事件,任一触发都清理 timer,
|
|
1275
|
+
* 避免 timer 泄漏。
|
|
1276
|
+
*
|
|
1277
|
+
* @param ws WebSocket 实例
|
|
1278
|
+
* @param timeout 超时毫秒,默认 2000
|
|
1279
|
+
* @returns 'open' → resolve;'error' → reject(err);'close' → reject;超时 → reject
|
|
1280
|
+
*/
|
|
1281
|
+
declare function waitForWsOpen(ws: WebSocket, timeout?: number): Promise<void>;
|
|
1282
|
+
/**
|
|
1283
|
+
* 一键连接 WS server
|
|
1284
|
+
*
|
|
1285
|
+
* 内部流程:
|
|
1286
|
+
* 1. baseUrl 协议转换(http → ws,https → wss)
|
|
1287
|
+
* 2. new WebSocket(url, protocols, { headers })
|
|
1288
|
+
* 3. 立即创建 MessageQueue(开始缓冲消息,避免竞态)
|
|
1289
|
+
* 4. waitForWsOpen 等待连接建立(三事件监听 + 超时清理)
|
|
1290
|
+
* 5. 返回 WsTestClient
|
|
1291
|
+
*
|
|
1292
|
+
* 连接失败(中间件拦截 / 路径未匹配 / 超时)→ reject。
|
|
1293
|
+
*
|
|
1294
|
+
* @param baseUrl createTestServer().baseUrl(http://...)
|
|
1295
|
+
* @param pathname WS 路径,如 '/api/chat',可含 query
|
|
1296
|
+
* @param options timeout / headers / protocols
|
|
1297
|
+
* @returns WsTestClient 实例
|
|
1298
|
+
*/
|
|
1299
|
+
declare function connectWs(baseUrl: string, pathname: string, options?: WsTestClientOptions): Promise<WsTestClient>;
|
|
1300
|
+
|
|
1125
1301
|
interface InjectOptions {
|
|
1126
1302
|
method?: string;
|
|
1127
1303
|
path?: string;
|
|
@@ -1215,4 +1391,4 @@ type ProdApp = AppBase;
|
|
|
1215
1391
|
*/
|
|
1216
1392
|
declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
|
|
1217
1393
|
|
|
1218
|
-
export { type ProdApp as App, type CorsOptions, type CreateAppOptions, type DevApp, type FaapiConfig, type FaapiContext, type FaapiContextConfig, FaapiError, type FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, type HelmetOptions, type InjectOptions, type InjectResponse, type Injector, type InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LoggerOptions, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type RouteInfo, type RouteInputSchema, type RouteManifest, RouteNotFoundError, type RouteOutputSchema, type RouteParamSchema, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, type SseEvent, type SseWriter, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, collectRouteSchemaSources, cors, createProdApp as createApp, createContext, createDevApp, createProdApp, createProgram, extractTypeInfo, getInputTypeForMethod, helmet, invalidateProgramCache, invokeHandler, loadConfig, loadEnv, logger, resolveTypeNode };
|
|
1394
|
+
export { type ProdApp as App, type CorsOptions, type CreateAppOptions, type DevApp, type FaapiConfig, type FaapiContext, type FaapiContextConfig, FaapiError, type FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, type HelmetOptions, type InjectOptions, type InjectResponse, type Injector, type InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LoggerOptions, MessageQueue, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type RouteInfo, type RouteInputSchema, type RouteManifest, RouteNotFoundError, type RouteOutputSchema, type RouteParamSchema, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, type SseEvent, type SseWriter, type TestServer, type TestServerOptions, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, type WsTestClient, type WsTestClientOptions, collectRouteSchemaSources, connectWs, cors, createProdApp as createApp, createContext, createDevApp, createProdApp, createProgram, createTestServer, extractTypeInfo, getInputTypeForMethod, helmet, invalidateProgramCache, invokeHandler, loadConfig, loadEnv, logger, resolveTypeNode, waitForWsOpen };
|