@klnap/next-proxy-chain 1.1.1 → 2.0.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/CHANGELOG.md +20 -0
- package/README.md +195 -79
- package/dist/index.cjs +956 -258
- package/dist/index.d.cts +164 -82
- package/dist/index.d.ts +164 -82
- package/dist/index.js +954 -254
- package/package.json +35 -22
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,129 @@
|
|
|
1
1
|
import { NextRequest, NextFetchEvent, NextResponse } from 'next/server';
|
|
2
|
+
import { NextRequest as NextRequest$1, NextResponse as NextResponse$1 } from 'next/server.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Unique symbol identifying an explicit continue action in the proxy chain pipeline.
|
|
5
6
|
*/
|
|
6
7
|
declare const NEXT: unique symbol;
|
|
8
|
+
/** Marks handlers created by defineHandler(). */
|
|
9
|
+
declare const DEFINED_HANDLER: unique symbol;
|
|
10
|
+
|
|
11
|
+
type RequestMetadata = {
|
|
12
|
+
readonly requestId: string;
|
|
13
|
+
readonly startedAt: number;
|
|
14
|
+
readonly elapsedMs: number;
|
|
15
|
+
readonly originalUrl: string;
|
|
16
|
+
readonly originalPathname: string;
|
|
17
|
+
readonly originalHostname: string;
|
|
18
|
+
};
|
|
19
|
+
type ApplicationContext<TContext extends object> = string extends keyof TContext ? never : Omit<Partial<TContext>, keyof RequestMetadata | "signal">;
|
|
20
|
+
type HandlerContext<TContext extends object = object> = RequestMetadata & ApplicationContext<TContext> & {
|
|
21
|
+
readonly signal: AbortSignal;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type DebugLevel = false | true | "compact" | "detailed" | "verbose";
|
|
25
|
+
type SkipReason = "include-mismatch" | "exclude-match" | "when-false";
|
|
26
|
+
type HandlerDiagnosticDetails = {
|
|
27
|
+
include?: readonly string[];
|
|
28
|
+
includeResult?: "matched" | "mismatch";
|
|
29
|
+
exclude?: readonly string[];
|
|
30
|
+
excludeResult?: "not-matched" | "matched";
|
|
31
|
+
when?: "matched" | "false" | "not-evaluated";
|
|
32
|
+
timeoutMs?: number;
|
|
33
|
+
timeoutSource?: "handler" | "chain";
|
|
34
|
+
};
|
|
35
|
+
type NamedHandler = {
|
|
36
|
+
name: string;
|
|
37
|
+
details?: HandlerDiagnosticDetails;
|
|
38
|
+
};
|
|
39
|
+
type HandlerTrace = (NamedHandler & {
|
|
40
|
+
outcome: "continue";
|
|
41
|
+
source: "undefined" | "NextResponse.next()" | "next()";
|
|
42
|
+
durationMs: number;
|
|
43
|
+
}) | (NamedHandler & {
|
|
44
|
+
outcome: "rewrite";
|
|
45
|
+
durationMs: number;
|
|
46
|
+
destination: string;
|
|
47
|
+
final: boolean;
|
|
48
|
+
}) | (NamedHandler & {
|
|
49
|
+
outcome: "redirect";
|
|
50
|
+
durationMs: number;
|
|
51
|
+
status: number;
|
|
52
|
+
destination?: string;
|
|
53
|
+
}) | (NamedHandler & {
|
|
54
|
+
outcome: "response";
|
|
55
|
+
durationMs: number;
|
|
56
|
+
status: number;
|
|
57
|
+
}) | (NamedHandler & {
|
|
58
|
+
outcome: "skipped";
|
|
59
|
+
reason: SkipReason;
|
|
60
|
+
}) | (NamedHandler & {
|
|
61
|
+
outcome: "not-reached";
|
|
62
|
+
}) | (NamedHandler & {
|
|
63
|
+
outcome: "recovered";
|
|
64
|
+
durationMs: number;
|
|
65
|
+
recovery: "continue" | "rewrite" | "redirect" | "response";
|
|
66
|
+
policy: "handler" | "chain";
|
|
67
|
+
}) | (NamedHandler & {
|
|
68
|
+
outcome: "failed" | "timeout";
|
|
69
|
+
durationMs: number;
|
|
70
|
+
policy: "none" | "handler" | "chain";
|
|
71
|
+
phase?: "when" | "handler" | "result" | "recovery";
|
|
72
|
+
recovery?: "response";
|
|
73
|
+
timeoutMs?: number;
|
|
74
|
+
}) | (NamedHandler & {
|
|
75
|
+
outcome: "request-cancelled";
|
|
76
|
+
durationMs: number;
|
|
77
|
+
phase: "before-handler" | "when" | "handler";
|
|
78
|
+
policy: "none" | "handler" | "chain";
|
|
79
|
+
recovery?: "response";
|
|
80
|
+
});
|
|
81
|
+
type ChainOutcome = {
|
|
82
|
+
type: "next";
|
|
83
|
+
} | {
|
|
84
|
+
type: "rewrite";
|
|
85
|
+
destination: string;
|
|
86
|
+
} | {
|
|
87
|
+
type: "redirect";
|
|
88
|
+
status: number;
|
|
89
|
+
destination?: string;
|
|
90
|
+
} | {
|
|
91
|
+
type: "response";
|
|
92
|
+
status: number;
|
|
93
|
+
} | {
|
|
94
|
+
type: "error";
|
|
95
|
+
status: number;
|
|
96
|
+
classification: "handler" | "engine";
|
|
97
|
+
} | {
|
|
98
|
+
type: "timeout";
|
|
99
|
+
status: number;
|
|
100
|
+
timeoutMs: number;
|
|
101
|
+
} | {
|
|
102
|
+
type: "request-cancelled";
|
|
103
|
+
};
|
|
104
|
+
type ChainTrace = {
|
|
105
|
+
requestId: string;
|
|
106
|
+
startedAt: number;
|
|
107
|
+
method: string;
|
|
108
|
+
pathname?: string;
|
|
109
|
+
durationMs: number;
|
|
110
|
+
outcome: ChainOutcome;
|
|
111
|
+
handlers: readonly HandlerTrace[];
|
|
112
|
+
};
|
|
113
|
+
type TraceCallback = (trace: ChainTrace) => void | Promise<void>;
|
|
114
|
+
type HandlerWhen<TContext extends object = object> = (request: NextRequest, event: NextFetchEvent, context: HandlerContext<TContext>) => boolean | Promise<boolean>;
|
|
115
|
+
type HandlerDebugOptions<TContext extends object = object> = {
|
|
116
|
+
name?: string;
|
|
117
|
+
debug?: DebugLevel;
|
|
118
|
+
timeout?: number;
|
|
119
|
+
onError?: OnErrorHandler<TContext>;
|
|
120
|
+
rules?: {
|
|
121
|
+
include?: RouteScopeList;
|
|
122
|
+
exclude?: RouteScopeList;
|
|
123
|
+
when?: HandlerWhen<TContext>;
|
|
124
|
+
};
|
|
125
|
+
handler: (request: NextRequest, event: NextFetchEvent, context: HandlerContext<TContext>) => Response | NextResult | undefined | void | Promise<Response | NextResult | undefined | void>;
|
|
126
|
+
};
|
|
7
127
|
|
|
8
128
|
/**
|
|
9
129
|
* Configuration options for chain continuation.
|
|
@@ -20,11 +140,6 @@ type NextOptions = {
|
|
|
20
140
|
type NextResult = NextOptions & {
|
|
21
141
|
readonly [NEXT]: true;
|
|
22
142
|
};
|
|
23
|
-
/**
|
|
24
|
-
* Shared state for an individual request pipeline execution.
|
|
25
|
-
* Backed by Map to eliminate prototype-pollution risks.
|
|
26
|
-
*/
|
|
27
|
-
type ProxyContext = Map<string, unknown>;
|
|
28
143
|
type PathMatcher = RegExp | string;
|
|
29
144
|
type HostMatcher = string | RegExp | readonly (string | RegExp)[];
|
|
30
145
|
/**
|
|
@@ -43,40 +158,37 @@ type CompiledScope = {
|
|
|
43
158
|
host?: RegExp;
|
|
44
159
|
path?: RegExp;
|
|
45
160
|
};
|
|
46
|
-
interface
|
|
161
|
+
interface BaseHandlerRule<TFn, TContext extends object = object> {
|
|
47
162
|
run: TFn;
|
|
48
163
|
include?: CompiledScope[];
|
|
49
164
|
exclude?: CompiledScope[];
|
|
50
165
|
name?: string;
|
|
166
|
+
debug?: DebugLevel;
|
|
167
|
+
timeout?: number;
|
|
168
|
+
when?: HandlerWhen<TContext>;
|
|
169
|
+
onError?: OnErrorHandler<TContext>;
|
|
170
|
+
diagnosticRules?: {
|
|
171
|
+
include?: RouteScopeList;
|
|
172
|
+
exclude?: RouteScopeList;
|
|
173
|
+
};
|
|
51
174
|
}
|
|
52
|
-
type
|
|
175
|
+
type HandlerFilter = {
|
|
53
176
|
include?: RouteScopeList;
|
|
54
177
|
exclude?: RouteScopeList;
|
|
55
178
|
name?: string;
|
|
56
179
|
};
|
|
57
|
-
/**
|
|
58
|
-
* Type-only brand carried by defineProxy() rules. It records the context type a rule expects.
|
|
59
|
-
* Chain entries match on it as an object, which keeps handler parameters contextually typed.
|
|
60
|
-
*/
|
|
61
|
-
declare const CONTEXT_BRAND: unique symbol;
|
|
62
|
-
/** Entry type for a defineProxy() rule that expects context TContext. */
|
|
63
|
-
type ContextRuleEntry<TContext = ProxyContext> = {
|
|
64
|
-
readonly [CONTEXT_BRAND]: TContext;
|
|
65
|
-
};
|
|
66
180
|
/**
|
|
67
181
|
* A native Next.js proxy handler: `(request, event)`. Use it in chain() without any wrapper.
|
|
68
182
|
*/
|
|
69
|
-
type NativeProxy<TContext =
|
|
70
|
-
/** Request context
|
|
71
|
-
context: TContext) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
|
|
183
|
+
type NativeProxy<TContext extends object = object> = (req: NextRequest, event: NextFetchEvent,
|
|
184
|
+
/** Request context added by chain(); Next.js continues to pass its native event unchanged. */
|
|
185
|
+
context: HandlerContext<TContext>) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
|
|
72
186
|
/**
|
|
73
187
|
* A single proxy execution function.
|
|
74
188
|
*/
|
|
75
|
-
type
|
|
76
|
-
type
|
|
77
|
-
type
|
|
78
|
-
type Entry<TContext = ProxyContext> = ProxyFn<TContext> | ProxyRule<TContext>;
|
|
79
|
-
type MergeStrategy = 'last-write-wins' | 'first-write-wins' | ((key: string, existing: string, incoming: string) => string | null);
|
|
189
|
+
type HandlerFn<TContext extends object = object> = (req: NextRequest, ctx: HandlerContext<TContext>, event: NextFetchEvent) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
|
|
190
|
+
type HandlerRule<TContext extends object = object> = BaseHandlerRule<HandlerFn<TContext>, TContext>;
|
|
191
|
+
type MergeStrategy = "last-write-wins" | "first-write-wins" | ((key: string, existing: string, incoming: string) => string | null);
|
|
80
192
|
interface ChainLogger {
|
|
81
193
|
debug: (metaOrMsg: unknown, maybeMsg?: string) => void;
|
|
82
194
|
info: (metaOrMsg: unknown, maybeMsg?: string) => void;
|
|
@@ -91,19 +203,18 @@ interface RequestHeaderPolicy {
|
|
|
91
203
|
* Value returned from `onError`.
|
|
92
204
|
*
|
|
93
205
|
* - `Response` — stop the pipeline and return it
|
|
94
|
-
* - `NextResult` (`next()`) — continue
|
|
206
|
+
* - `NextResult` (`next()`) — continue after an ordinary rule error only
|
|
95
207
|
* - `undefined` / `void` / `null` — fail closed (`500`)
|
|
96
208
|
*/
|
|
97
209
|
type OnErrorResult = NextResponse | Response | NextResult | undefined | void | null;
|
|
98
|
-
type OnErrorHandler<TContext =
|
|
210
|
+
type OnErrorHandler<TContext extends object = object> = (err: unknown,
|
|
99
211
|
/**
|
|
100
212
|
* 0-based index of the rule that threw.
|
|
101
|
-
* `-1` means the error originated in the pipeline engine
|
|
213
|
+
* `-1` means the error originated in the pipeline engine.
|
|
102
214
|
* Engine-level errors cannot continue; only a `Response` is honored.
|
|
103
215
|
*/
|
|
104
|
-
ruleIndex: number, req: NextRequest, ctx: TContext) => OnErrorResult | Promise<OnErrorResult>;
|
|
105
|
-
interface ChainConfig<TContext =
|
|
106
|
-
createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
|
|
216
|
+
ruleIndex: number, req: NextRequest, ctx: HandlerContext<TContext>) => OnErrorResult | Promise<OnErrorResult>;
|
|
217
|
+
interface ChainConfig<TContext extends object = object> {
|
|
107
218
|
cookieMergeStrategy: MergeStrategy;
|
|
108
219
|
/**
|
|
109
220
|
* How conflicting **response** headers from different rules are resolved.
|
|
@@ -111,20 +222,17 @@ interface ChainConfig<TContext = ProxyContext> {
|
|
|
111
222
|
*/
|
|
112
223
|
headerMergeStrategy: MergeStrategy;
|
|
113
224
|
onError?: OnErrorHandler<TContext>;
|
|
114
|
-
debug:
|
|
225
|
+
debug: DebugLevel;
|
|
115
226
|
logger: ChainLogger;
|
|
116
|
-
|
|
117
|
-
|
|
227
|
+
onTrace?: TraceCallback;
|
|
228
|
+
tracePath?: boolean;
|
|
229
|
+
/** Maximum execution time per handler, including its predicate, in milliseconds. 0 disables timeout. */
|
|
230
|
+
timeout: number;
|
|
118
231
|
requestHeaderPolicy: RequestHeaderPolicy;
|
|
119
232
|
/** Throw an error on startup if the runtime platform does not support request header overrides. */
|
|
120
233
|
strictOverrideCheck?: boolean;
|
|
121
|
-
/**
|
|
122
|
-
* Request header name that prefixes rewrites with a physical host folder
|
|
123
|
-
* (multi-tenant `app/(sites)/shop` layouts). Unset = disabled.
|
|
124
|
-
*/
|
|
125
|
-
hostFolderHeader?: string;
|
|
126
234
|
}
|
|
127
|
-
type
|
|
235
|
+
type ChainOptions<TContext extends object = object> = Partial<ChainConfig<TContext>>;
|
|
128
236
|
|
|
129
237
|
/**
|
|
130
238
|
* Creates a continuation action instructing the proxy chain to proceed to subsequent rules.
|
|
@@ -141,59 +249,33 @@ declare function next(options?: NextOptions): NextResult;
|
|
|
141
249
|
*/
|
|
142
250
|
declare function isNextResult(value: unknown): value is NextResult;
|
|
143
251
|
|
|
252
|
+
type ProxyHandler = (req: NextRequest$1, event: NextFetchEvent) => Promise<NextResponse$1 | Response>;
|
|
253
|
+
/** Internal executor for the public chain() composition helper. */
|
|
254
|
+
declare function executeChain<TContext extends object = object>(options: ChainOptions<TContext>, entries: HandlerRule<TContext>[]): ProxyHandler;
|
|
255
|
+
|
|
256
|
+
type DefinedHandler<TContext extends object = object> = HandlerRule<TContext> & {
|
|
257
|
+
readonly [DEFINED_HANDLER]: true;
|
|
258
|
+
};
|
|
144
259
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* @example
|
|
148
|
-
* const authRule = defineProxy("auth", async (req, ctx) => next())
|
|
149
|
-
* const scopedAuthRule = authRule({ include: path("/admin/*") })
|
|
150
|
-
*/
|
|
151
|
-
declare function defineProxy<TContext = ProxyContext>(run: ProxyFn<TContext>): DefinedProxy<TContext>;
|
|
152
|
-
declare function defineProxy<TContext = ProxyContext>(name: string, run: ProxyFn<TContext>): DefinedProxy<TContext>;
|
|
153
|
-
type ProxyHandler = (req: NextRequest, event: NextFetchEvent) => Promise<NextResponse | Response>;
|
|
154
|
-
/**
|
|
155
|
-
* Composes independent proxy rules into a single pipeline for Next.js 15 (middleware.ts) and Next.js 16 (proxy.ts).
|
|
156
|
-
*
|
|
157
|
-
* Custom `TContext` requires `createContext`. Without it, context is `ProxyContext` (`Map`).
|
|
158
|
-
*
|
|
159
|
-
* @example
|
|
160
|
-
* export default proxyChain({ debug: true }, [
|
|
161
|
-
* requestIdRule,
|
|
162
|
-
* authRule({ include: path("/dashboard/*") }),
|
|
163
|
-
* i18nRule,
|
|
164
|
-
* ])
|
|
260
|
+
* Defines a native-signature handler with optional chain rules and local settings.
|
|
165
261
|
*/
|
|
166
|
-
|
|
167
|
-
declare function proxyChain(entries: Entry<ProxyContext>[]): ProxyHandler;
|
|
168
|
-
declare function proxyChain(options: ProxyChainOptions<ProxyContext>, entries: Entry<ProxyContext>[]): ProxyHandler;
|
|
169
|
-
declare function proxyChain<TContext>(options: ProxyChainOptions<TContext> & {
|
|
170
|
-
createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
|
|
171
|
-
}, entries: Entry<TContext>[]): ProxyHandler;
|
|
262
|
+
declare function defineHandler<TContext extends object = object>(options: HandlerDebugOptions<TContext>): DefinedHandler<TContext>;
|
|
172
263
|
|
|
173
264
|
/** Scope for a tuple entry: `[handler, scope]`. A string, RegExp, or array is shorthand for `include`. */
|
|
174
|
-
type ChainScope = Omit<
|
|
175
|
-
/** One entry of chain(): a native handler, a
|
|
176
|
-
type ChainEntry<TContext =
|
|
265
|
+
type ChainScope = Omit<HandlerFilter, "name"> | PathMatcher | readonly PathMatcher[];
|
|
266
|
+
/** One entry of chain(): a native handler, a defineHandler() result, or a scoped tuple. */
|
|
267
|
+
type ChainEntry<TContext extends object = object> = NativeProxy<TContext> | DefinedHandler<TContext> | readonly [NativeProxy<TContext> | DefinedHandler<TContext>, ChainScope];
|
|
177
268
|
/**
|
|
178
|
-
* Composes native Next.js proxy handlers and
|
|
269
|
+
* Composes native Next.js proxy handlers and defineHandler() results into one proxy.
|
|
179
270
|
*
|
|
180
|
-
* - A plain function receives `(request, event)
|
|
181
|
-
* - A `
|
|
271
|
+
* - A plain function receives native `(request, event)` arguments plus context as a third argument.
|
|
272
|
+
* - A `defineHandler()` handler receives `(request, event, context)`.
|
|
182
273
|
* - `[handler, scope]` runs the handler only for matching paths or hosts.
|
|
183
274
|
*
|
|
184
275
|
* @example
|
|
185
276
|
* export default chain([intlProxy, [authProxy, { include: '/dashboard/**' }]])
|
|
186
277
|
*/
|
|
187
|
-
declare function chain(entries: readonly ChainEntry<
|
|
188
|
-
declare function chain<TContext>(entries: readonly ChainEntry<TContext>[], options: ProxyChainOptions<TContext> & {
|
|
189
|
-
createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
|
|
190
|
-
}): ReturnType<typeof proxyChain>;
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Creates an isolated per-request context instance.
|
|
194
|
-
* Backed by Map to eliminate prototype pollution risks.
|
|
195
|
-
*/
|
|
196
|
-
declare function createProxyContext(): ProxyContext;
|
|
278
|
+
declare function chain<TContext extends object = object>(entries: readonly ChainEntry<TContext>[], options?: ChainOptions<TContext>): ReturnType<typeof executeChain>;
|
|
197
279
|
|
|
198
280
|
/**
|
|
199
281
|
* Builds a compiled Host matcher RegExp from domain names, `*.example.com` wildcards, or regex patterns.
|
|
@@ -213,4 +295,4 @@ declare class RuleTimeoutError extends Error {
|
|
|
213
295
|
constructor(ruleName: string, timeoutMs: number);
|
|
214
296
|
}
|
|
215
297
|
|
|
216
|
-
export { type ChainEntry, type ChainLogger, type ChainScope, type
|
|
298
|
+
export { type ChainEntry, type ChainLogger, type ChainOptions, type ChainOutcome, type ChainScope, type ChainTrace, type DebugLevel, type DefinedHandler, type HandlerContext, type HandlerDebugOptions, type HandlerDiagnosticDetails, type HandlerTrace, type HandlerWhen, type HostMatcher, type MergeStrategy, type NativeProxy, type NextOptions, type NextResult, type OnErrorHandler, type OnErrorResult, type PathMatcher, type RequestHeaderPolicy, type RequestMetadata, RuleTimeoutError, type SkipReason, type TraceCallback, chain, defineHandler, host, isNextResult, next, path };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,129 @@
|
|
|
1
1
|
import { NextRequest, NextFetchEvent, NextResponse } from 'next/server';
|
|
2
|
+
import { NextRequest as NextRequest$1, NextResponse as NextResponse$1 } from 'next/server.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Unique symbol identifying an explicit continue action in the proxy chain pipeline.
|
|
5
6
|
*/
|
|
6
7
|
declare const NEXT: unique symbol;
|
|
8
|
+
/** Marks handlers created by defineHandler(). */
|
|
9
|
+
declare const DEFINED_HANDLER: unique symbol;
|
|
10
|
+
|
|
11
|
+
type RequestMetadata = {
|
|
12
|
+
readonly requestId: string;
|
|
13
|
+
readonly startedAt: number;
|
|
14
|
+
readonly elapsedMs: number;
|
|
15
|
+
readonly originalUrl: string;
|
|
16
|
+
readonly originalPathname: string;
|
|
17
|
+
readonly originalHostname: string;
|
|
18
|
+
};
|
|
19
|
+
type ApplicationContext<TContext extends object> = string extends keyof TContext ? never : Omit<Partial<TContext>, keyof RequestMetadata | "signal">;
|
|
20
|
+
type HandlerContext<TContext extends object = object> = RequestMetadata & ApplicationContext<TContext> & {
|
|
21
|
+
readonly signal: AbortSignal;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type DebugLevel = false | true | "compact" | "detailed" | "verbose";
|
|
25
|
+
type SkipReason = "include-mismatch" | "exclude-match" | "when-false";
|
|
26
|
+
type HandlerDiagnosticDetails = {
|
|
27
|
+
include?: readonly string[];
|
|
28
|
+
includeResult?: "matched" | "mismatch";
|
|
29
|
+
exclude?: readonly string[];
|
|
30
|
+
excludeResult?: "not-matched" | "matched";
|
|
31
|
+
when?: "matched" | "false" | "not-evaluated";
|
|
32
|
+
timeoutMs?: number;
|
|
33
|
+
timeoutSource?: "handler" | "chain";
|
|
34
|
+
};
|
|
35
|
+
type NamedHandler = {
|
|
36
|
+
name: string;
|
|
37
|
+
details?: HandlerDiagnosticDetails;
|
|
38
|
+
};
|
|
39
|
+
type HandlerTrace = (NamedHandler & {
|
|
40
|
+
outcome: "continue";
|
|
41
|
+
source: "undefined" | "NextResponse.next()" | "next()";
|
|
42
|
+
durationMs: number;
|
|
43
|
+
}) | (NamedHandler & {
|
|
44
|
+
outcome: "rewrite";
|
|
45
|
+
durationMs: number;
|
|
46
|
+
destination: string;
|
|
47
|
+
final: boolean;
|
|
48
|
+
}) | (NamedHandler & {
|
|
49
|
+
outcome: "redirect";
|
|
50
|
+
durationMs: number;
|
|
51
|
+
status: number;
|
|
52
|
+
destination?: string;
|
|
53
|
+
}) | (NamedHandler & {
|
|
54
|
+
outcome: "response";
|
|
55
|
+
durationMs: number;
|
|
56
|
+
status: number;
|
|
57
|
+
}) | (NamedHandler & {
|
|
58
|
+
outcome: "skipped";
|
|
59
|
+
reason: SkipReason;
|
|
60
|
+
}) | (NamedHandler & {
|
|
61
|
+
outcome: "not-reached";
|
|
62
|
+
}) | (NamedHandler & {
|
|
63
|
+
outcome: "recovered";
|
|
64
|
+
durationMs: number;
|
|
65
|
+
recovery: "continue" | "rewrite" | "redirect" | "response";
|
|
66
|
+
policy: "handler" | "chain";
|
|
67
|
+
}) | (NamedHandler & {
|
|
68
|
+
outcome: "failed" | "timeout";
|
|
69
|
+
durationMs: number;
|
|
70
|
+
policy: "none" | "handler" | "chain";
|
|
71
|
+
phase?: "when" | "handler" | "result" | "recovery";
|
|
72
|
+
recovery?: "response";
|
|
73
|
+
timeoutMs?: number;
|
|
74
|
+
}) | (NamedHandler & {
|
|
75
|
+
outcome: "request-cancelled";
|
|
76
|
+
durationMs: number;
|
|
77
|
+
phase: "before-handler" | "when" | "handler";
|
|
78
|
+
policy: "none" | "handler" | "chain";
|
|
79
|
+
recovery?: "response";
|
|
80
|
+
});
|
|
81
|
+
type ChainOutcome = {
|
|
82
|
+
type: "next";
|
|
83
|
+
} | {
|
|
84
|
+
type: "rewrite";
|
|
85
|
+
destination: string;
|
|
86
|
+
} | {
|
|
87
|
+
type: "redirect";
|
|
88
|
+
status: number;
|
|
89
|
+
destination?: string;
|
|
90
|
+
} | {
|
|
91
|
+
type: "response";
|
|
92
|
+
status: number;
|
|
93
|
+
} | {
|
|
94
|
+
type: "error";
|
|
95
|
+
status: number;
|
|
96
|
+
classification: "handler" | "engine";
|
|
97
|
+
} | {
|
|
98
|
+
type: "timeout";
|
|
99
|
+
status: number;
|
|
100
|
+
timeoutMs: number;
|
|
101
|
+
} | {
|
|
102
|
+
type: "request-cancelled";
|
|
103
|
+
};
|
|
104
|
+
type ChainTrace = {
|
|
105
|
+
requestId: string;
|
|
106
|
+
startedAt: number;
|
|
107
|
+
method: string;
|
|
108
|
+
pathname?: string;
|
|
109
|
+
durationMs: number;
|
|
110
|
+
outcome: ChainOutcome;
|
|
111
|
+
handlers: readonly HandlerTrace[];
|
|
112
|
+
};
|
|
113
|
+
type TraceCallback = (trace: ChainTrace) => void | Promise<void>;
|
|
114
|
+
type HandlerWhen<TContext extends object = object> = (request: NextRequest, event: NextFetchEvent, context: HandlerContext<TContext>) => boolean | Promise<boolean>;
|
|
115
|
+
type HandlerDebugOptions<TContext extends object = object> = {
|
|
116
|
+
name?: string;
|
|
117
|
+
debug?: DebugLevel;
|
|
118
|
+
timeout?: number;
|
|
119
|
+
onError?: OnErrorHandler<TContext>;
|
|
120
|
+
rules?: {
|
|
121
|
+
include?: RouteScopeList;
|
|
122
|
+
exclude?: RouteScopeList;
|
|
123
|
+
when?: HandlerWhen<TContext>;
|
|
124
|
+
};
|
|
125
|
+
handler: (request: NextRequest, event: NextFetchEvent, context: HandlerContext<TContext>) => Response | NextResult | undefined | void | Promise<Response | NextResult | undefined | void>;
|
|
126
|
+
};
|
|
7
127
|
|
|
8
128
|
/**
|
|
9
129
|
* Configuration options for chain continuation.
|
|
@@ -20,11 +140,6 @@ type NextOptions = {
|
|
|
20
140
|
type NextResult = NextOptions & {
|
|
21
141
|
readonly [NEXT]: true;
|
|
22
142
|
};
|
|
23
|
-
/**
|
|
24
|
-
* Shared state for an individual request pipeline execution.
|
|
25
|
-
* Backed by Map to eliminate prototype-pollution risks.
|
|
26
|
-
*/
|
|
27
|
-
type ProxyContext = Map<string, unknown>;
|
|
28
143
|
type PathMatcher = RegExp | string;
|
|
29
144
|
type HostMatcher = string | RegExp | readonly (string | RegExp)[];
|
|
30
145
|
/**
|
|
@@ -43,40 +158,37 @@ type CompiledScope = {
|
|
|
43
158
|
host?: RegExp;
|
|
44
159
|
path?: RegExp;
|
|
45
160
|
};
|
|
46
|
-
interface
|
|
161
|
+
interface BaseHandlerRule<TFn, TContext extends object = object> {
|
|
47
162
|
run: TFn;
|
|
48
163
|
include?: CompiledScope[];
|
|
49
164
|
exclude?: CompiledScope[];
|
|
50
165
|
name?: string;
|
|
166
|
+
debug?: DebugLevel;
|
|
167
|
+
timeout?: number;
|
|
168
|
+
when?: HandlerWhen<TContext>;
|
|
169
|
+
onError?: OnErrorHandler<TContext>;
|
|
170
|
+
diagnosticRules?: {
|
|
171
|
+
include?: RouteScopeList;
|
|
172
|
+
exclude?: RouteScopeList;
|
|
173
|
+
};
|
|
51
174
|
}
|
|
52
|
-
type
|
|
175
|
+
type HandlerFilter = {
|
|
53
176
|
include?: RouteScopeList;
|
|
54
177
|
exclude?: RouteScopeList;
|
|
55
178
|
name?: string;
|
|
56
179
|
};
|
|
57
|
-
/**
|
|
58
|
-
* Type-only brand carried by defineProxy() rules. It records the context type a rule expects.
|
|
59
|
-
* Chain entries match on it as an object, which keeps handler parameters contextually typed.
|
|
60
|
-
*/
|
|
61
|
-
declare const CONTEXT_BRAND: unique symbol;
|
|
62
|
-
/** Entry type for a defineProxy() rule that expects context TContext. */
|
|
63
|
-
type ContextRuleEntry<TContext = ProxyContext> = {
|
|
64
|
-
readonly [CONTEXT_BRAND]: TContext;
|
|
65
|
-
};
|
|
66
180
|
/**
|
|
67
181
|
* A native Next.js proxy handler: `(request, event)`. Use it in chain() without any wrapper.
|
|
68
182
|
*/
|
|
69
|
-
type NativeProxy<TContext =
|
|
70
|
-
/** Request context
|
|
71
|
-
context: TContext) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
|
|
183
|
+
type NativeProxy<TContext extends object = object> = (req: NextRequest, event: NextFetchEvent,
|
|
184
|
+
/** Request context added by chain(); Next.js continues to pass its native event unchanged. */
|
|
185
|
+
context: HandlerContext<TContext>) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
|
|
72
186
|
/**
|
|
73
187
|
* A single proxy execution function.
|
|
74
188
|
*/
|
|
75
|
-
type
|
|
76
|
-
type
|
|
77
|
-
type
|
|
78
|
-
type Entry<TContext = ProxyContext> = ProxyFn<TContext> | ProxyRule<TContext>;
|
|
79
|
-
type MergeStrategy = 'last-write-wins' | 'first-write-wins' | ((key: string, existing: string, incoming: string) => string | null);
|
|
189
|
+
type HandlerFn<TContext extends object = object> = (req: NextRequest, ctx: HandlerContext<TContext>, event: NextFetchEvent) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
|
|
190
|
+
type HandlerRule<TContext extends object = object> = BaseHandlerRule<HandlerFn<TContext>, TContext>;
|
|
191
|
+
type MergeStrategy = "last-write-wins" | "first-write-wins" | ((key: string, existing: string, incoming: string) => string | null);
|
|
80
192
|
interface ChainLogger {
|
|
81
193
|
debug: (metaOrMsg: unknown, maybeMsg?: string) => void;
|
|
82
194
|
info: (metaOrMsg: unknown, maybeMsg?: string) => void;
|
|
@@ -91,19 +203,18 @@ interface RequestHeaderPolicy {
|
|
|
91
203
|
* Value returned from `onError`.
|
|
92
204
|
*
|
|
93
205
|
* - `Response` — stop the pipeline and return it
|
|
94
|
-
* - `NextResult` (`next()`) — continue
|
|
206
|
+
* - `NextResult` (`next()`) — continue after an ordinary rule error only
|
|
95
207
|
* - `undefined` / `void` / `null` — fail closed (`500`)
|
|
96
208
|
*/
|
|
97
209
|
type OnErrorResult = NextResponse | Response | NextResult | undefined | void | null;
|
|
98
|
-
type OnErrorHandler<TContext =
|
|
210
|
+
type OnErrorHandler<TContext extends object = object> = (err: unknown,
|
|
99
211
|
/**
|
|
100
212
|
* 0-based index of the rule that threw.
|
|
101
|
-
* `-1` means the error originated in the pipeline engine
|
|
213
|
+
* `-1` means the error originated in the pipeline engine.
|
|
102
214
|
* Engine-level errors cannot continue; only a `Response` is honored.
|
|
103
215
|
*/
|
|
104
|
-
ruleIndex: number, req: NextRequest, ctx: TContext) => OnErrorResult | Promise<OnErrorResult>;
|
|
105
|
-
interface ChainConfig<TContext =
|
|
106
|
-
createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
|
|
216
|
+
ruleIndex: number, req: NextRequest, ctx: HandlerContext<TContext>) => OnErrorResult | Promise<OnErrorResult>;
|
|
217
|
+
interface ChainConfig<TContext extends object = object> {
|
|
107
218
|
cookieMergeStrategy: MergeStrategy;
|
|
108
219
|
/**
|
|
109
220
|
* How conflicting **response** headers from different rules are resolved.
|
|
@@ -111,20 +222,17 @@ interface ChainConfig<TContext = ProxyContext> {
|
|
|
111
222
|
*/
|
|
112
223
|
headerMergeStrategy: MergeStrategy;
|
|
113
224
|
onError?: OnErrorHandler<TContext>;
|
|
114
|
-
debug:
|
|
225
|
+
debug: DebugLevel;
|
|
115
226
|
logger: ChainLogger;
|
|
116
|
-
|
|
117
|
-
|
|
227
|
+
onTrace?: TraceCallback;
|
|
228
|
+
tracePath?: boolean;
|
|
229
|
+
/** Maximum execution time per handler, including its predicate, in milliseconds. 0 disables timeout. */
|
|
230
|
+
timeout: number;
|
|
118
231
|
requestHeaderPolicy: RequestHeaderPolicy;
|
|
119
232
|
/** Throw an error on startup if the runtime platform does not support request header overrides. */
|
|
120
233
|
strictOverrideCheck?: boolean;
|
|
121
|
-
/**
|
|
122
|
-
* Request header name that prefixes rewrites with a physical host folder
|
|
123
|
-
* (multi-tenant `app/(sites)/shop` layouts). Unset = disabled.
|
|
124
|
-
*/
|
|
125
|
-
hostFolderHeader?: string;
|
|
126
234
|
}
|
|
127
|
-
type
|
|
235
|
+
type ChainOptions<TContext extends object = object> = Partial<ChainConfig<TContext>>;
|
|
128
236
|
|
|
129
237
|
/**
|
|
130
238
|
* Creates a continuation action instructing the proxy chain to proceed to subsequent rules.
|
|
@@ -141,59 +249,33 @@ declare function next(options?: NextOptions): NextResult;
|
|
|
141
249
|
*/
|
|
142
250
|
declare function isNextResult(value: unknown): value is NextResult;
|
|
143
251
|
|
|
252
|
+
type ProxyHandler = (req: NextRequest$1, event: NextFetchEvent) => Promise<NextResponse$1 | Response>;
|
|
253
|
+
/** Internal executor for the public chain() composition helper. */
|
|
254
|
+
declare function executeChain<TContext extends object = object>(options: ChainOptions<TContext>, entries: HandlerRule<TContext>[]): ProxyHandler;
|
|
255
|
+
|
|
256
|
+
type DefinedHandler<TContext extends object = object> = HandlerRule<TContext> & {
|
|
257
|
+
readonly [DEFINED_HANDLER]: true;
|
|
258
|
+
};
|
|
144
259
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* @example
|
|
148
|
-
* const authRule = defineProxy("auth", async (req, ctx) => next())
|
|
149
|
-
* const scopedAuthRule = authRule({ include: path("/admin/*") })
|
|
150
|
-
*/
|
|
151
|
-
declare function defineProxy<TContext = ProxyContext>(run: ProxyFn<TContext>): DefinedProxy<TContext>;
|
|
152
|
-
declare function defineProxy<TContext = ProxyContext>(name: string, run: ProxyFn<TContext>): DefinedProxy<TContext>;
|
|
153
|
-
type ProxyHandler = (req: NextRequest, event: NextFetchEvent) => Promise<NextResponse | Response>;
|
|
154
|
-
/**
|
|
155
|
-
* Composes independent proxy rules into a single pipeline for Next.js 15 (middleware.ts) and Next.js 16 (proxy.ts).
|
|
156
|
-
*
|
|
157
|
-
* Custom `TContext` requires `createContext`. Without it, context is `ProxyContext` (`Map`).
|
|
158
|
-
*
|
|
159
|
-
* @example
|
|
160
|
-
* export default proxyChain({ debug: true }, [
|
|
161
|
-
* requestIdRule,
|
|
162
|
-
* authRule({ include: path("/dashboard/*") }),
|
|
163
|
-
* i18nRule,
|
|
164
|
-
* ])
|
|
260
|
+
* Defines a native-signature handler with optional chain rules and local settings.
|
|
165
261
|
*/
|
|
166
|
-
|
|
167
|
-
declare function proxyChain(entries: Entry<ProxyContext>[]): ProxyHandler;
|
|
168
|
-
declare function proxyChain(options: ProxyChainOptions<ProxyContext>, entries: Entry<ProxyContext>[]): ProxyHandler;
|
|
169
|
-
declare function proxyChain<TContext>(options: ProxyChainOptions<TContext> & {
|
|
170
|
-
createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
|
|
171
|
-
}, entries: Entry<TContext>[]): ProxyHandler;
|
|
262
|
+
declare function defineHandler<TContext extends object = object>(options: HandlerDebugOptions<TContext>): DefinedHandler<TContext>;
|
|
172
263
|
|
|
173
264
|
/** Scope for a tuple entry: `[handler, scope]`. A string, RegExp, or array is shorthand for `include`. */
|
|
174
|
-
type ChainScope = Omit<
|
|
175
|
-
/** One entry of chain(): a native handler, a
|
|
176
|
-
type ChainEntry<TContext =
|
|
265
|
+
type ChainScope = Omit<HandlerFilter, "name"> | PathMatcher | readonly PathMatcher[];
|
|
266
|
+
/** One entry of chain(): a native handler, a defineHandler() result, or a scoped tuple. */
|
|
267
|
+
type ChainEntry<TContext extends object = object> = NativeProxy<TContext> | DefinedHandler<TContext> | readonly [NativeProxy<TContext> | DefinedHandler<TContext>, ChainScope];
|
|
177
268
|
/**
|
|
178
|
-
* Composes native Next.js proxy handlers and
|
|
269
|
+
* Composes native Next.js proxy handlers and defineHandler() results into one proxy.
|
|
179
270
|
*
|
|
180
|
-
* - A plain function receives `(request, event)
|
|
181
|
-
* - A `
|
|
271
|
+
* - A plain function receives native `(request, event)` arguments plus context as a third argument.
|
|
272
|
+
* - A `defineHandler()` handler receives `(request, event, context)`.
|
|
182
273
|
* - `[handler, scope]` runs the handler only for matching paths or hosts.
|
|
183
274
|
*
|
|
184
275
|
* @example
|
|
185
276
|
* export default chain([intlProxy, [authProxy, { include: '/dashboard/**' }]])
|
|
186
277
|
*/
|
|
187
|
-
declare function chain(entries: readonly ChainEntry<
|
|
188
|
-
declare function chain<TContext>(entries: readonly ChainEntry<TContext>[], options: ProxyChainOptions<TContext> & {
|
|
189
|
-
createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
|
|
190
|
-
}): ReturnType<typeof proxyChain>;
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Creates an isolated per-request context instance.
|
|
194
|
-
* Backed by Map to eliminate prototype pollution risks.
|
|
195
|
-
*/
|
|
196
|
-
declare function createProxyContext(): ProxyContext;
|
|
278
|
+
declare function chain<TContext extends object = object>(entries: readonly ChainEntry<TContext>[], options?: ChainOptions<TContext>): ReturnType<typeof executeChain>;
|
|
197
279
|
|
|
198
280
|
/**
|
|
199
281
|
* Builds a compiled Host matcher RegExp from domain names, `*.example.com` wildcards, or regex patterns.
|
|
@@ -213,4 +295,4 @@ declare class RuleTimeoutError extends Error {
|
|
|
213
295
|
constructor(ruleName: string, timeoutMs: number);
|
|
214
296
|
}
|
|
215
297
|
|
|
216
|
-
export { type ChainEntry, type ChainLogger, type ChainScope, type
|
|
298
|
+
export { type ChainEntry, type ChainLogger, type ChainOptions, type ChainOutcome, type ChainScope, type ChainTrace, type DebugLevel, type DefinedHandler, type HandlerContext, type HandlerDebugOptions, type HandlerDiagnosticDetails, type HandlerTrace, type HandlerWhen, type HostMatcher, type MergeStrategy, type NativeProxy, type NextOptions, type NextResult, type OnErrorHandler, type OnErrorResult, type PathMatcher, type RequestHeaderPolicy, type RequestMetadata, RuleTimeoutError, type SkipReason, type TraceCallback, chain, defineHandler, host, isNextResult, next, path };
|