alien-middleware 0.10.3 → 0.11.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/dist/chunk-ENIXF4Y2.js +195 -0
- package/dist/{index-Bzfu9JE9.d.ts → index-Dj_pJd-o.d.ts} +27 -17
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/router.d.ts +2 -2
- package/dist/router.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-2YXTWP7H.js +0 -182
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// node_modules/.pnpm/radashi@12.5.0-beta.6d5c035/node_modules/radashi/dist/radashi.js
|
|
2
|
+
function noop() {
|
|
3
|
+
}
|
|
4
|
+
var isArray = /* @__PURE__ */ (() => Array.isArray)();
|
|
5
|
+
var asyncIteratorSymbol = (
|
|
6
|
+
/* c8 ignore next */
|
|
7
|
+
Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator")
|
|
8
|
+
);
|
|
9
|
+
function isFunction(value) {
|
|
10
|
+
return typeof value === "function";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/url.ts
|
|
14
|
+
var urlDescriptor = {
|
|
15
|
+
configurable: true,
|
|
16
|
+
get() {
|
|
17
|
+
const url = new URL(this.request.url);
|
|
18
|
+
Object.defineProperty(this, "url", { value: url });
|
|
19
|
+
return url;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
function defineParsedURL(context) {
|
|
23
|
+
if (!("url" in context)) {
|
|
24
|
+
Object.defineProperty(context, "url", urlDescriptor);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/middleware/filterPlatform.ts
|
|
29
|
+
function filterPlatform(name) {
|
|
30
|
+
return function(ctx) {
|
|
31
|
+
if (ctx.platform.name !== name) {
|
|
32
|
+
return ctx.passThrough();
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/index.ts
|
|
39
|
+
var kRequestChain = Symbol("requestChain");
|
|
40
|
+
var kIgnoreNotFound = Symbol("ignoreNotFound");
|
|
41
|
+
var kMiddlewareCache = Symbol("middlewareCache");
|
|
42
|
+
var kResponseHeaders = Symbol("responseHeaders");
|
|
43
|
+
var MiddlewareChain = class _MiddlewareChain {
|
|
44
|
+
// Any properties added here must also be set in the `createHandler` function.
|
|
45
|
+
[kRequestChain] = [];
|
|
46
|
+
/**
|
|
47
|
+
* Add a request middleware to the end of the chain.
|
|
48
|
+
*
|
|
49
|
+
* If a middleware chain is given, its middlewares will be executed after any
|
|
50
|
+
* existing middlewares in this chain.
|
|
51
|
+
*
|
|
52
|
+
* @returns a new `MiddlewareChain` instance
|
|
53
|
+
*/
|
|
54
|
+
use(middleware) {
|
|
55
|
+
return createHandler(
|
|
56
|
+
middleware instanceof _MiddlewareChain ? [...this[kRequestChain], ...middleware[kRequestChain]] : [...this[kRequestChain], middleware]
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create a middleware function that encapsulates this middleware chain, so
|
|
61
|
+
* any modifications it makes to the request context are not leaked.
|
|
62
|
+
*/
|
|
63
|
+
isolate() {
|
|
64
|
+
return isFunction(this) ? (ctx) => this(ctx) : noop;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* @internal You should not need to call this method, unless you want a
|
|
68
|
+
* `RequestHandler` type from an empty middleware chain. If your middleware
|
|
69
|
+
* chain is **not** empty, you won't need this.
|
|
70
|
+
*/
|
|
71
|
+
toHandler() {
|
|
72
|
+
return createHandler(this[kRequestChain]);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
function createExtendedEnv(context) {
|
|
76
|
+
const env = /* @__PURE__ */ Object.create(null);
|
|
77
|
+
const superEnv = context.env;
|
|
78
|
+
context.env = (key) => env[key] ?? superEnv(key);
|
|
79
|
+
return env;
|
|
80
|
+
}
|
|
81
|
+
async function runMiddlewareChain(requestChain, parentContext) {
|
|
82
|
+
const context = Object.create(parentContext);
|
|
83
|
+
context[kIgnoreNotFound] = true;
|
|
84
|
+
defineParsedURL(context);
|
|
85
|
+
const { passThrough } = context;
|
|
86
|
+
let shouldPassThrough = false;
|
|
87
|
+
context.passThrough = () => {
|
|
88
|
+
shouldPassThrough = true;
|
|
89
|
+
};
|
|
90
|
+
context.setHeader = (name, value) => {
|
|
91
|
+
if (context[kResponseHeaders] === parentContext[kResponseHeaders]) {
|
|
92
|
+
context[kResponseHeaders] = new Headers(parentContext[kResponseHeaders]);
|
|
93
|
+
}
|
|
94
|
+
context[kResponseHeaders].set(name, value);
|
|
95
|
+
};
|
|
96
|
+
const responseChain = [];
|
|
97
|
+
context.onResponse = (callback) => {
|
|
98
|
+
responseChain.push(callback);
|
|
99
|
+
};
|
|
100
|
+
const cache = context[kMiddlewareCache] = new Set(
|
|
101
|
+
parentContext[kMiddlewareCache]
|
|
102
|
+
);
|
|
103
|
+
let response;
|
|
104
|
+
let env;
|
|
105
|
+
for (const middleware of requestChain) {
|
|
106
|
+
if (cache.has(middleware)) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
cache.add(middleware);
|
|
110
|
+
let result = middleware(context);
|
|
111
|
+
if (result instanceof Promise) {
|
|
112
|
+
result = await result;
|
|
113
|
+
}
|
|
114
|
+
if (shouldPassThrough) {
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
if (result) {
|
|
118
|
+
if (result instanceof Response) {
|
|
119
|
+
response = result;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
for (const key in result) {
|
|
123
|
+
if (key === "env") {
|
|
124
|
+
if (result.env) {
|
|
125
|
+
env ||= createExtendedEnv(context);
|
|
126
|
+
Object.defineProperties(
|
|
127
|
+
env,
|
|
128
|
+
Object.getOwnPropertyDescriptors(result.env)
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
} else if (key === "onResponse") {
|
|
132
|
+
if (result.onResponse) {
|
|
133
|
+
responseChain.push(result.onResponse);
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
const descriptor = Object.getOwnPropertyDescriptor(result, key);
|
|
137
|
+
descriptor.configurable = false;
|
|
138
|
+
Object.defineProperty(context, key, descriptor);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (!response) {
|
|
144
|
+
if (parentContext[kIgnoreNotFound]) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
response = new Response("Not Found", { status: 404 });
|
|
148
|
+
if (shouldPassThrough) {
|
|
149
|
+
passThrough();
|
|
150
|
+
return response;
|
|
151
|
+
}
|
|
152
|
+
} else if (response.type !== "default") {
|
|
153
|
+
response = new Response(response.body, response);
|
|
154
|
+
}
|
|
155
|
+
context[kResponseHeaders]?.forEach((value, name) => {
|
|
156
|
+
response.headers.set(name, value);
|
|
157
|
+
});
|
|
158
|
+
context.setHeader = null;
|
|
159
|
+
for (const plugin of responseChain) {
|
|
160
|
+
let result = plugin(response);
|
|
161
|
+
if (result instanceof Promise) {
|
|
162
|
+
result = await result;
|
|
163
|
+
}
|
|
164
|
+
if (result) {
|
|
165
|
+
response = result;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return response;
|
|
170
|
+
}
|
|
171
|
+
function createHandler(requestChain) {
|
|
172
|
+
const handler = runMiddlewareChain.bind(
|
|
173
|
+
null,
|
|
174
|
+
requestChain
|
|
175
|
+
);
|
|
176
|
+
Object.setPrototypeOf(handler, MiddlewareChain.prototype);
|
|
177
|
+
handler[kRequestChain] = requestChain;
|
|
178
|
+
return handler;
|
|
179
|
+
}
|
|
180
|
+
function chain(middleware) {
|
|
181
|
+
if (middleware instanceof MiddlewareChain) {
|
|
182
|
+
return middleware;
|
|
183
|
+
}
|
|
184
|
+
const empty = new MiddlewareChain();
|
|
185
|
+
return middleware ? empty.use(middleware) : empty;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export {
|
|
189
|
+
isArray,
|
|
190
|
+
isFunction,
|
|
191
|
+
defineParsedURL,
|
|
192
|
+
filterPlatform,
|
|
193
|
+
MiddlewareChain,
|
|
194
|
+
chain
|
|
195
|
+
};
|
|
@@ -115,7 +115,7 @@ type RequestContext<TEnv extends object = any, TProperties extends object = neve
|
|
|
115
115
|
*/
|
|
116
116
|
type MiddlewareContext<T extends MiddlewareChain | Middleware[]> = [
|
|
117
117
|
T
|
|
118
|
-
] extends [never] ? RequestContext<{}, never, unknown> : T extends MiddlewareChain ? RequestContext<Env<T>, Properties<T>, Platform<T>> : T extends Middleware[] ? MiddlewareContext<ApplyMiddlewares<T
|
|
118
|
+
] extends [never] ? RequestContext<{}, never, unknown> : T extends MiddlewareChain ? RequestContext<Env<T>, Properties<T>, Platform<T>> : T extends Middleware[] ? MiddlewareContext<MiddlewareChain<ApplyMiddlewares<T>>> : never;
|
|
119
119
|
type IsolatedContext<T extends MiddlewareChain> = RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>;
|
|
120
120
|
type RequestMiddleware<T extends MiddlewareChain = MiddlewareChain> = (context: RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>) => Awaitable<Response | RequestPlugin | void>;
|
|
121
121
|
type ResponseCallback = (response: Response) => Awaitable<Response | void>;
|
|
@@ -161,25 +161,18 @@ type MiddlewareInputs<T extends Middleware> = T extends Middleware<infer TEnv, i
|
|
|
161
161
|
} : never;
|
|
162
162
|
type MiddlewarePlatform<T extends Middleware> = T extends Middleware<any, any, infer TPlatform> ? TPlatform : never;
|
|
163
163
|
/**
|
|
164
|
-
* This applies a middleware to a chain. If the type `
|
|
164
|
+
* This applies a middleware to a chain. If the type `TSecond` is itself a
|
|
165
165
|
* chain, it's treated as a nested chain, which won't leak its plugins into the
|
|
166
166
|
* parent chain.
|
|
167
167
|
*
|
|
168
168
|
* The `TFirst` type is allowed to be `never`, which results in the middleware's
|
|
169
169
|
* output types being used as the request handler's input types.
|
|
170
170
|
*/
|
|
171
|
-
type ApplyMiddleware<TFirst extends MiddlewareChain, TSecond extends Middleware> = ApplyMiddlewareOutputs<TFirst, TSecond> extends infer TCurrent extends MiddlewareTypes['current'] ?
|
|
171
|
+
type ApplyMiddleware<TFirst extends MiddlewareChain, TSecond extends Middleware> = ApplyMiddlewareOutputs<TFirst, TSecond> extends infer TCurrent extends MiddlewareTypes['current'] ? {
|
|
172
172
|
initial: CastNever<Inputs<TFirst>, MiddlewareInputs<TSecond>>;
|
|
173
173
|
current: TCurrent;
|
|
174
174
|
platform: CastNever<Platform<TFirst>, MiddlewarePlatform<TSecond>>;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Apply a list of middlewares to an empty middleware chain.
|
|
178
|
-
*/
|
|
179
|
-
type ApplyMiddlewares<T extends Middleware[]> = T extends [
|
|
180
|
-
...infer TRest extends Middleware[],
|
|
181
|
-
infer TLast extends Middleware
|
|
182
|
-
] ? ApplyMiddleware<ApplyMiddlewares<TRest>, TLast> : ApplyFirstMiddleware<T[0]>;
|
|
175
|
+
} : never;
|
|
183
176
|
type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
|
|
184
177
|
initial: {
|
|
185
178
|
env: {};
|
|
@@ -191,12 +184,23 @@ type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
|
|
|
191
184
|
};
|
|
192
185
|
platform: TPlatform;
|
|
193
186
|
}>;
|
|
194
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Convert a `Middleware` type into a `MiddlewareTypes` type.
|
|
189
|
+
* @internal For similar behavior with public APIs, use `ApplyMiddlewares<[T]>`.
|
|
190
|
+
*/
|
|
191
|
+
type ApplyFirstMiddleware<T extends Middleware> = T extends MiddlewareChain<infer TInternal> ? TInternal : ApplyMiddleware<EmptyMiddlewareChain<MiddlewarePlatform<T>>, T>;
|
|
192
|
+
/**
|
|
193
|
+
* Flatten a list of middlewares into a `MiddlewareTypes` type.
|
|
194
|
+
*/
|
|
195
|
+
type ApplyMiddlewares<T extends Middleware[]> = T extends [
|
|
196
|
+
...infer TRest extends Middleware[],
|
|
197
|
+
infer TLast extends Middleware
|
|
198
|
+
] ? ApplyMiddleware<MiddlewareChain<ApplyMiddlewares<TRest>>, TLast> : ApplyFirstMiddleware<T[0]>;
|
|
195
199
|
type RouteMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | (string & {});
|
|
196
|
-
type RouteContext<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = MiddlewareContext<ApplyMiddleware<MiddlewareChain<T['$Router']>, () => {
|
|
200
|
+
type RouteContext<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = MiddlewareContext<MiddlewareChain<ApplyMiddleware<MiddlewareChain<T['$Router']>, () => {
|
|
197
201
|
params: TPathParams;
|
|
198
202
|
method: TMethod;
|
|
199
|
-
}
|
|
203
|
+
}>>>;
|
|
200
204
|
type RouteHandler<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = (context: RouteContext<T, TPathParams, TMethod>) => Awaitable<Response | void>;
|
|
201
205
|
declare class RouterTypes<T extends MiddlewareChain = any> extends Function {
|
|
202
206
|
/** This property won't exist at runtime. It contains type information for inference purposes. */
|
|
@@ -229,12 +233,18 @@ declare class MiddlewareChain<T extends MiddlewareTypes = any> {
|
|
|
229
233
|
*
|
|
230
234
|
* @returns a new `MiddlewareChain` instance
|
|
231
235
|
*/
|
|
232
|
-
use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): ApplyMiddleware<this, TMiddleware
|
|
236
|
+
use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): RequestHandler<ApplyMiddleware<this, TMiddleware>>;
|
|
233
237
|
/**
|
|
234
238
|
* Create a middleware function that encapsulates this middleware chain, so
|
|
235
239
|
* any modifications it makes to the request context are not leaked.
|
|
236
240
|
*/
|
|
237
241
|
isolate(): (ctx: IsolatedContext<this>) => Awaitable<Response | void>;
|
|
242
|
+
/**
|
|
243
|
+
* @internal You should not need to call this method, unless you want a
|
|
244
|
+
* `RequestHandler` type from an empty middleware chain. If your middleware
|
|
245
|
+
* chain is **not** empty, you won't need this.
|
|
246
|
+
*/
|
|
247
|
+
toHandler(): RequestHandler<T>;
|
|
238
248
|
}
|
|
239
249
|
declare function chain<TEnv extends object = {}, TProperties extends object = {}, TPlatform = unknown>(): MiddlewareChain<{
|
|
240
250
|
initial: {
|
|
@@ -247,6 +257,6 @@ declare function chain<TEnv extends object = {}, TProperties extends object = {}
|
|
|
247
257
|
};
|
|
248
258
|
platform: TPlatform;
|
|
249
259
|
}>;
|
|
250
|
-
declare function chain<
|
|
260
|
+
declare function chain<T extends Middleware | MiddlewareChain>(middleware: T): T extends Middleware ? RequestHandler<ApplyFirstMiddleware<T>> : T;
|
|
251
261
|
|
|
252
|
-
export { type ApplyMiddleware as A, type EmptyMiddlewareChain as E, type HattipContext as H, type MiddlewareContext as M, type Router as R, MiddlewareChain as a, type RouteContext as b, type RouteHandler as c, chain as d, type ApplyMiddlewares as e, filterPlatform as f, type EnvAccessor as g, type ExtractMiddleware as h, type Middleware as i, type
|
|
262
|
+
export { type ApplyMiddleware as A, type EmptyMiddlewareChain as E, type HattipContext as H, type MiddlewareContext as M, type Router as R, MiddlewareChain as a, type RouteContext as b, type RouteHandler as c, chain as d, type ApplyMiddlewares as e, filterPlatform as f, type EnvAccessor as g, type ExtractMiddleware as h, type Middleware as i, type MiddlewareTypes as j, type RequestContext as k, type RequestHandler as l, type RequestMiddleware as m, type RequestPlugin as n, type ResponseCallback as o };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as ApplyMiddleware, e as ApplyMiddlewares, E as EmptyMiddlewareChain, g as EnvAccessor, h as ExtractMiddleware, H as HattipContext, i as Middleware, a as MiddlewareChain, M as MiddlewareContext, j as
|
|
1
|
+
export { A as ApplyMiddleware, e as ApplyMiddlewares, E as EmptyMiddlewareChain, g as EnvAccessor, h as ExtractMiddleware, H as HattipContext, i as Middleware, a as MiddlewareChain, M as MiddlewareContext, j as MiddlewareTypes, k as RequestContext, l as RequestHandler, m as RequestMiddleware, n as RequestPlugin, o as ResponseCallback, d as chain, f as filterPlatform } from './index-Dj_pJd-o.js';
|
|
2
2
|
import '@hattip/core';
|
|
3
3
|
import 'pathic';
|
package/dist/index.js
CHANGED
package/dist/router.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { R as Router, M as MiddlewareContext, a as MiddlewareChain, E as EmptyMiddlewareChain } from './index-
|
|
2
|
-
export { b as RouteContext, c as RouteHandler } from './index-
|
|
1
|
+
import { R as Router, M as MiddlewareContext, a as MiddlewareChain, E as EmptyMiddlewareChain } from './index-Dj_pJd-o.js';
|
|
2
|
+
export { b as RouteContext, c as RouteHandler } from './index-Dj_pJd-o.js';
|
|
3
3
|
import '@hattip/core';
|
|
4
4
|
import 'pathic';
|
|
5
5
|
|
package/dist/router.js
CHANGED
package/package.json
CHANGED
package/dist/chunk-2YXTWP7H.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
// node_modules/.pnpm/radashi@12.5.0-beta.6d5c035/node_modules/radashi/dist/radashi.js
|
|
2
|
-
function noop() {
|
|
3
|
-
}
|
|
4
|
-
var isArray = /* @__PURE__ */ (() => Array.isArray)();
|
|
5
|
-
var asyncIteratorSymbol = (
|
|
6
|
-
/* c8 ignore next */
|
|
7
|
-
Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator")
|
|
8
|
-
);
|
|
9
|
-
function isFunction(value) {
|
|
10
|
-
return typeof value === "function";
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// src/url.ts
|
|
14
|
-
var urlDescriptor = {
|
|
15
|
-
configurable: true,
|
|
16
|
-
get() {
|
|
17
|
-
const url = new URL(this.request.url);
|
|
18
|
-
Object.defineProperty(this, "url", { value: url });
|
|
19
|
-
return url;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
function defineParsedURL(context) {
|
|
23
|
-
if (!("url" in context)) {
|
|
24
|
-
Object.defineProperty(context, "url", urlDescriptor);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// src/middleware/filterPlatform.ts
|
|
29
|
-
function filterPlatform(name) {
|
|
30
|
-
return function(ctx) {
|
|
31
|
-
if (ctx.platform.name !== name) {
|
|
32
|
-
return ctx.passThrough();
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// src/index.ts
|
|
39
|
-
var kRequestChain = Symbol("requestChain");
|
|
40
|
-
var kIgnoreNotFound = Symbol("ignoreNotFound");
|
|
41
|
-
var kMiddlewareCache = Symbol("middlewareCache");
|
|
42
|
-
var kResponseHeaders = Symbol("responseHeaders");
|
|
43
|
-
var MiddlewareChain = class _MiddlewareChain {
|
|
44
|
-
[kRequestChain] = [];
|
|
45
|
-
/**
|
|
46
|
-
* Add a request middleware to the end of the chain.
|
|
47
|
-
*
|
|
48
|
-
* If a middleware chain is given, its middlewares will be executed after any
|
|
49
|
-
* existing middlewares in this chain.
|
|
50
|
-
*
|
|
51
|
-
* @returns a new `MiddlewareChain` instance
|
|
52
|
-
*/
|
|
53
|
-
use(middleware) {
|
|
54
|
-
return createHandler(
|
|
55
|
-
middleware instanceof _MiddlewareChain ? [...this[kRequestChain], ...middleware[kRequestChain]] : [...this[kRequestChain], middleware]
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Create a middleware function that encapsulates this middleware chain, so
|
|
60
|
-
* any modifications it makes to the request context are not leaked.
|
|
61
|
-
*/
|
|
62
|
-
isolate() {
|
|
63
|
-
return isFunction(this) ? (ctx) => this(ctx) : noop;
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
function createHandler(requestChain) {
|
|
67
|
-
async function handler(parentContext) {
|
|
68
|
-
const context = Object.create(parentContext);
|
|
69
|
-
context[kIgnoreNotFound] = true;
|
|
70
|
-
defineParsedURL(context);
|
|
71
|
-
const { passThrough } = context;
|
|
72
|
-
let shouldPassThrough = false;
|
|
73
|
-
context.passThrough = () => {
|
|
74
|
-
shouldPassThrough = true;
|
|
75
|
-
};
|
|
76
|
-
context.setHeader = (name, value) => {
|
|
77
|
-
if (context[kResponseHeaders] === parentContext[kResponseHeaders]) {
|
|
78
|
-
context[kResponseHeaders] = new Headers(parentContext[kResponseHeaders]);
|
|
79
|
-
}
|
|
80
|
-
context[kResponseHeaders].set(name, value);
|
|
81
|
-
};
|
|
82
|
-
const responseChain = [];
|
|
83
|
-
context.onResponse = (callback) => {
|
|
84
|
-
responseChain.push(callback);
|
|
85
|
-
};
|
|
86
|
-
const cache = context[kMiddlewareCache] = new Set(
|
|
87
|
-
parentContext[kMiddlewareCache]
|
|
88
|
-
);
|
|
89
|
-
let response;
|
|
90
|
-
let env;
|
|
91
|
-
for (const middleware of requestChain) {
|
|
92
|
-
if (cache.has(middleware)) {
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
cache.add(middleware);
|
|
96
|
-
let result = middleware(context);
|
|
97
|
-
if (result instanceof Promise) {
|
|
98
|
-
result = await result;
|
|
99
|
-
}
|
|
100
|
-
if (shouldPassThrough) {
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
if (result) {
|
|
104
|
-
if (result instanceof Response) {
|
|
105
|
-
response = result;
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
for (const key in result) {
|
|
109
|
-
if (key === "env") {
|
|
110
|
-
if (result.env) {
|
|
111
|
-
env ||= createExtendedEnv(context);
|
|
112
|
-
Object.defineProperties(
|
|
113
|
-
env,
|
|
114
|
-
Object.getOwnPropertyDescriptors(result.env)
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
} else if (key === "onResponse") {
|
|
118
|
-
if (result.onResponse) {
|
|
119
|
-
responseChain.push(result.onResponse);
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
const descriptor = Object.getOwnPropertyDescriptor(result, key);
|
|
123
|
-
descriptor.configurable = false;
|
|
124
|
-
Object.defineProperty(context, key, descriptor);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (!response) {
|
|
130
|
-
if (parentContext[kIgnoreNotFound]) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
response = new Response("Not Found", { status: 404 });
|
|
134
|
-
if (shouldPassThrough) {
|
|
135
|
-
passThrough();
|
|
136
|
-
return response;
|
|
137
|
-
}
|
|
138
|
-
} else if (response.type !== "default") {
|
|
139
|
-
response = new Response(response.body, response);
|
|
140
|
-
}
|
|
141
|
-
context[kResponseHeaders]?.forEach((value, name) => {
|
|
142
|
-
response.headers.set(name, value);
|
|
143
|
-
});
|
|
144
|
-
context.setHeader = null;
|
|
145
|
-
for (const plugin of responseChain) {
|
|
146
|
-
let result = plugin(response);
|
|
147
|
-
if (result instanceof Promise) {
|
|
148
|
-
result = await result;
|
|
149
|
-
}
|
|
150
|
-
if (result) {
|
|
151
|
-
response = result;
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return response;
|
|
156
|
-
}
|
|
157
|
-
Object.setPrototypeOf(handler, MiddlewareChain.prototype);
|
|
158
|
-
handler[kRequestChain] = requestChain;
|
|
159
|
-
return handler;
|
|
160
|
-
}
|
|
161
|
-
function createExtendedEnv(context) {
|
|
162
|
-
const env = /* @__PURE__ */ Object.create(null);
|
|
163
|
-
const superEnv = context.env;
|
|
164
|
-
context.env = (key) => env[key] ?? superEnv(key);
|
|
165
|
-
return env;
|
|
166
|
-
}
|
|
167
|
-
function chain(middleware) {
|
|
168
|
-
if (middleware instanceof MiddlewareChain) {
|
|
169
|
-
return middleware;
|
|
170
|
-
}
|
|
171
|
-
const empty = new MiddlewareChain();
|
|
172
|
-
return middleware ? empty.use(middleware) : empty;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export {
|
|
176
|
-
isArray,
|
|
177
|
-
isFunction,
|
|
178
|
-
defineParsedURL,
|
|
179
|
-
filterPlatform,
|
|
180
|
-
MiddlewareChain,
|
|
181
|
-
chain
|
|
182
|
-
};
|