weifuwu 0.28.2 → 0.30.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/README.md +394 -0
- package/dist/core/cookie.d.ts +36 -0
- package/dist/core/env.d.ts +69 -0
- package/dist/core/html.d.ts +34 -0
- package/dist/core/logger.d.ts +16 -0
- package/dist/core/router.d.ts +95 -0
- package/dist/core/serve.d.ts +36 -0
- package/dist/core/sse.d.ts +47 -0
- package/dist/core/trace.d.ts +95 -0
- package/dist/graphql.d.ts +16 -0
- package/dist/hub.d.ts +36 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +3387 -0
- package/dist/middleware/compress.d.ts +20 -0
- package/dist/middleware/cors.d.ts +25 -0
- package/dist/middleware/csrf.d.ts +31 -0
- package/dist/middleware/flash.d.ts +44 -0
- package/dist/middleware/health.d.ts +24 -0
- package/dist/middleware/helmet.d.ts +33 -0
- package/dist/middleware/i18n.d.ts +39 -0
- package/dist/middleware/rate-limit.d.ts +44 -0
- package/dist/middleware/request-id.d.ts +40 -0
- package/dist/middleware/static.d.ts +23 -0
- package/dist/middleware/theme.d.ts +31 -0
- package/dist/middleware/upload.d.ts +55 -0
- package/dist/middleware/validate.d.ts +32 -0
- package/dist/postgres/client.d.ts +4 -0
- package/dist/postgres/index.d.ts +3 -0
- package/dist/postgres/module.d.ts +12 -0
- package/dist/postgres/types.d.ts +42 -0
- package/dist/queue/cron.d.ts +9 -0
- package/dist/queue/index.d.ts +2 -0
- package/dist/queue/types.d.ts +61 -0
- package/dist/redis/client.d.ts +2 -0
- package/dist/redis/index.d.ts +2 -0
- package/dist/redis/types.d.ts +17 -0
- package/dist/test/test-utils.d.ts +193 -0
- package/dist/types.d.ts +82 -0
- package/package.json +31 -9
- package/index.d.ts +0 -8
- package/index.js +0 -26
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format an SSE message string with a named event type.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* formatSSE('ping', { ts: Date.now() })
|
|
6
|
+
* // "event: ping\ndata: {"ts":...}\n\n"
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatSSE(event: string, data: unknown): string;
|
|
10
|
+
/**
|
|
11
|
+
* Format an SSE message string with only a data line (no event type).
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* formatSSEData({ message: 'hello' })
|
|
15
|
+
* // "data: {"message":"hello"}\n\n"
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function formatSSEData(data: unknown): string;
|
|
19
|
+
/** An SSE event to be sent via {@link createSSEStream}. */
|
|
20
|
+
export interface SSEEvent {
|
|
21
|
+
/** Event type (maps to `event:` field). */
|
|
22
|
+
event: string;
|
|
23
|
+
/** Event payload (serialized as JSON in `data:` field). */
|
|
24
|
+
data: unknown;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a Server-Sent Events (SSE) `Response` from an async iterable.
|
|
28
|
+
*
|
|
29
|
+
* Each item in the iterable is serialized as an SSE message:
|
|
30
|
+
* - If the item has a `.type` property → `event: {type}` + `data: {item}`
|
|
31
|
+
* - Otherwise → `data: {item}`
|
|
32
|
+
*
|
|
33
|
+
* Errors are sent as `event: error` messages. `AbortError` is silently ignored.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* app.get('/events', () => {
|
|
37
|
+
* async function* generate() {
|
|
38
|
+
* yield { type: 'ping', data: { ts: Date.now() } }
|
|
39
|
+
* }
|
|
40
|
+
* return createSSEStream(generate())
|
|
41
|
+
* })
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function createSSEStream(iterable: AsyncIterable<any>, opts?: {
|
|
45
|
+
headers?: Record<string, string>;
|
|
46
|
+
status?: number;
|
|
47
|
+
}): Response;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { Context, Middleware } from '../types.ts';
|
|
2
|
+
declare module '../types.ts' {
|
|
3
|
+
interface Context {
|
|
4
|
+
trace: TraceInjected;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export interface TraceInjected {
|
|
8
|
+
/** Unique request identifier (from X-Request-ID header or auto-generated). */
|
|
9
|
+
requestId: string;
|
|
10
|
+
/** Unique trace identifier for the request. */
|
|
11
|
+
traceId: string;
|
|
12
|
+
/** Milliseconds elapsed since the trace started. */
|
|
13
|
+
elapsed: () => number;
|
|
14
|
+
/** Timestamp (ms) when the trace started. */
|
|
15
|
+
startTime: number;
|
|
16
|
+
}
|
|
17
|
+
export interface TraceContext {
|
|
18
|
+
/** Unique identifier for the current request trace. */
|
|
19
|
+
traceId: string;
|
|
20
|
+
/** Timestamp (ms since epoch) when the trace started. */
|
|
21
|
+
startTime: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get the current request's trace ID.
|
|
25
|
+
* Returns `undefined` when called outside a request context (e.g. at startup).
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* const traceId = currentTraceId()
|
|
29
|
+
* log.info({ traceId }, 'request started')
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function currentTraceId(): string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Get the full current trace context ({ traceId, startTime }).
|
|
35
|
+
* Returns `undefined` outside a request.
|
|
36
|
+
*/
|
|
37
|
+
export declare function currentTrace(): TraceContext | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Run a function inside a trace context.
|
|
40
|
+
* Used internally by `serve()` for every incoming request.
|
|
41
|
+
* If `incomingTraceId` is provided (e.g. from an `X-Trace-Id` header) it is reused;
|
|
42
|
+
* otherwise a new UUID is generated.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const result = runWithTrace(req.headers.get('x-trace-id'), () => {
|
|
46
|
+
* return handleRequest(req)
|
|
47
|
+
* })
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @param incomingTraceId - Optional trace ID from upstream. Pass `null` to auto-generate.
|
|
51
|
+
* @param fn - Function to execute within the trace scope.
|
|
52
|
+
* @returns The return value of `fn`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function runWithTrace<T>(incomingTraceId: string | null, fn: () => T): T;
|
|
55
|
+
/**
|
|
56
|
+
* Milliseconds elapsed since the current trace started.
|
|
57
|
+
* Returns `0` if called outside a request context.
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* app.use(async (req, ctx, next) => {
|
|
61
|
+
* const res = await next(req, ctx)
|
|
62
|
+
* console.log('handled in', traceElapsed(), 'ms')
|
|
63
|
+
* return res
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare function traceElapsed(): number;
|
|
68
|
+
/** Options for {@link trace}. */
|
|
69
|
+
export interface TraceOptions {
|
|
70
|
+
/** Header name for request ID (default: `'X-Request-ID'`). */
|
|
71
|
+
header?: string;
|
|
72
|
+
/** Custom ID generator (default: `crypto.randomUUID`). */
|
|
73
|
+
generator?: () => string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Request tracing middleware.
|
|
77
|
+
*
|
|
78
|
+
* Injects `ctx.trace = { requestId, traceId, elapsed, startTime }`.
|
|
79
|
+
* Reads/writes `X-Request-ID` header. Combines the functionality of `requestId()`
|
|
80
|
+
* with the per-request tracing from `AsyncLocalStorage`.
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { trace } from 'weifuwu'
|
|
84
|
+
* app.use(trace())
|
|
85
|
+
*
|
|
86
|
+
* app.get('/', (req, ctx) => {
|
|
87
|
+
* console.log(ctx.trace.requestId) // 550e8400-e29b-...
|
|
88
|
+
* console.log(ctx.trace.traceId) // same as currentTraceId()
|
|
89
|
+
* console.log(ctx.trace.elapsed()) // ms since request start
|
|
90
|
+
* })
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function trace(options?: TraceOptions): Middleware<Context, Context & {
|
|
94
|
+
trace: TraceInjected;
|
|
95
|
+
}>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type GraphQLSchema } from 'graphql';
|
|
2
|
+
import type { Context } from './types.ts';
|
|
3
|
+
import { Router } from './core/router.ts';
|
|
4
|
+
export interface GraphQLOptions {
|
|
5
|
+
schema: string | GraphQLSchema;
|
|
6
|
+
rootValue?: any;
|
|
7
|
+
resolvers?: any;
|
|
8
|
+
context?: (req: Request, ctx: Context) => Record<string, any> | Promise<Record<string, any>>;
|
|
9
|
+
graphiql?: boolean;
|
|
10
|
+
/** Max query depth (nesting). Default: 10. Set 0 to disable. */
|
|
11
|
+
maxDepth?: number;
|
|
12
|
+
/** Execution timeout in ms. Default: 30_000. */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
}
|
|
15
|
+
export type GraphQLHandler = (req: Request, ctx: Context) => GraphQLOptions | Promise<GraphQLOptions>;
|
|
16
|
+
export declare function graphql(handler: GraphQLHandler): Router;
|
package/dist/hub.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Redis, WebSocket, Closeable } from './types.ts';
|
|
2
|
+
/** Options for {@link createHub}. */
|
|
3
|
+
export interface HubOptions {
|
|
4
|
+
/** Optional Redis client for cross-process pub/sub broadcast. */
|
|
5
|
+
redis?: Redis;
|
|
6
|
+
/** Key prefix for Redis channels (default: `'hub:'`). */
|
|
7
|
+
prefix?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* In-memory (and optionally Redis-backed) pub/sub hub for WebSocket rooms.
|
|
11
|
+
*
|
|
12
|
+
* Used internally by the WebSocket handler to implement `ctx.ws.join()` / `ctx.ws.sendRoom()`. */
|
|
13
|
+
export interface Hub extends Closeable {
|
|
14
|
+
/** Subscribe a WebSocket to a room/group. */
|
|
15
|
+
join(key: string, ws: WebSocket): void;
|
|
16
|
+
/** Unsubscribe a WebSocket from all rooms. */
|
|
17
|
+
leave(ws: WebSocket): void;
|
|
18
|
+
/** Send a JSON message to all members of a room. */
|
|
19
|
+
broadcast(key: string, data: unknown): void;
|
|
20
|
+
/** Close the hub, disconnect Redis subscribers, clear all rooms. */
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create a pub/sub hub for WebSocket room management.
|
|
25
|
+
*
|
|
26
|
+
* In-memory by default. Pass `redis` to enable cross-process broadcasting.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { createHub } from 'weifuwu'
|
|
30
|
+
*
|
|
31
|
+
* const hub = createHub()
|
|
32
|
+
* hub.join('room:general', ws)
|
|
33
|
+
* hub.broadcast('room:general', { type: 'chat', text: 'Hello' })
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function createHub(opts?: HubOptions): Hub;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type { Context, Handler, Middleware, ErrorHandler, WsContext, WebSocket } from './types.ts';
|
|
2
|
+
export { HttpError } from './types.ts';
|
|
3
|
+
export { currentTraceId, currentTrace, runWithTrace, traceElapsed, trace } from './core/trace.ts';
|
|
4
|
+
export type { TraceContext, TraceInjected, TraceOptions } from './core/trace.ts';
|
|
5
|
+
export { loadEnv, isDev, isProd, isBundled, getPublicEnv, env } from './core/env.ts';
|
|
6
|
+
export { serve, createTestServer, DEFAULT_MAX_BODY } from './core/serve.ts';
|
|
7
|
+
export type { ServeOptions, Server } from './core/serve.ts';
|
|
8
|
+
export { Router } from './core/router.ts';
|
|
9
|
+
export type { WebSocketHandler } from './core/router.ts';
|
|
10
|
+
export { logger } from './core/logger.ts';
|
|
11
|
+
export type { LoggerOptions } from './core/logger.ts';
|
|
12
|
+
export { cors } from './middleware/cors.ts';
|
|
13
|
+
export type { CORSOptions } from './middleware/cors.ts';
|
|
14
|
+
export { serveStatic } from './middleware/static.ts';
|
|
15
|
+
export type { ServeStaticOptions } from './middleware/static.ts';
|
|
16
|
+
export { validate } from './middleware/validate.ts';
|
|
17
|
+
export type { ValidationSchemas, ValidateModule } from './middleware/validate.ts';
|
|
18
|
+
export { getCookies, setCookie, deleteCookie } from './core/cookie.ts';
|
|
19
|
+
export type { CookieOptions } from './core/cookie.ts';
|
|
20
|
+
export { upload } from './middleware/upload.ts';
|
|
21
|
+
export type { UploadOptions, UploadedFile, UploadModule } from './middleware/upload.ts';
|
|
22
|
+
export { rateLimit } from './middleware/rate-limit.ts';
|
|
23
|
+
export type { RateLimitOptions } from './middleware/rate-limit.ts';
|
|
24
|
+
export { compress } from './middleware/compress.ts';
|
|
25
|
+
export type { CompressOptions } from './middleware/compress.ts';
|
|
26
|
+
export { helmet } from './middleware/helmet.ts';
|
|
27
|
+
export type { HelmetOptions } from './middleware/helmet.ts';
|
|
28
|
+
export { requestId } from './middleware/request-id.ts';
|
|
29
|
+
export type { RequestIdOptions, RequestIdModule } from './middleware/request-id.ts';
|
|
30
|
+
export { createSSEStream, formatSSE, formatSSEData } from './core/sse.ts';
|
|
31
|
+
export type { SSEEvent } from './core/sse.ts';
|
|
32
|
+
export { testApp, TestApp, TestRequest, createTestDb, withTestDb } from './test/test-utils.ts';
|
|
33
|
+
export type { TestResponse, TestDb } from './test/test-utils.ts';
|
|
34
|
+
export { graphql } from './graphql.ts';
|
|
35
|
+
export type { GraphQLOptions, GraphQLHandler } from './graphql.ts';
|
|
36
|
+
export { postgres, MIGRATIONS_TABLE } from './postgres/index.ts';
|
|
37
|
+
export type { PostgresOptions, PostgresClient, PostgresInjected } from './postgres/types.ts';
|
|
38
|
+
export { redis } from './redis/index.ts';
|
|
39
|
+
export type { RedisOptions, RedisClient, RedisInjected } from './redis/types.ts';
|
|
40
|
+
export { createHub } from './hub.ts';
|
|
41
|
+
export type { Hub, HubOptions } from './hub.ts';
|
|
42
|
+
export { queue } from './queue/index.ts';
|
|
43
|
+
export type { QueueOptions, QueueJob, Queue, QueueInjected } from './queue/types.ts';
|
|
44
|
+
export { health } from './middleware/health.ts';
|
|
45
|
+
export type { HealthOptions } from './middleware/health.ts';
|
|
46
|
+
export { html, raw } from './core/html.ts';
|
|
47
|
+
export { theme } from './middleware/theme.ts';
|
|
48
|
+
export type { ThemeOptions, ThemeInjected, ThemeModule } from './middleware/theme.ts';
|
|
49
|
+
export { i18n } from './middleware/i18n.ts';
|
|
50
|
+
export type { I18nOptions, I18nInjected, I18nModule } from './middleware/i18n.ts';
|
|
51
|
+
export { flash } from './middleware/flash.ts';
|
|
52
|
+
export type { FlashOptions, FlashInjected, FlashModule } from './middleware/flash.ts';
|
|
53
|
+
export { csrf } from './middleware/csrf.ts';
|
|
54
|
+
export type { CsrfOptions, CsrfInjected, CsrfModule } from './middleware/csrf.ts';
|