alien-middleware 0.10.3 → 0.10.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.
- package/dist/chunk-FHGDNFTY.js +192 -0
- package/dist/{index-Bzfu9JE9.d.ts → index-BK1OAKm-.d.ts} +7 -1
- 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,192 @@
|
|
|
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(null, requestChain);
|
|
173
|
+
Object.setPrototypeOf(handler, MiddlewareChain.prototype);
|
|
174
|
+
handler[kRequestChain] = requestChain;
|
|
175
|
+
return handler;
|
|
176
|
+
}
|
|
177
|
+
function chain(middleware) {
|
|
178
|
+
if (middleware instanceof MiddlewareChain) {
|
|
179
|
+
return middleware;
|
|
180
|
+
}
|
|
181
|
+
const empty = new MiddlewareChain();
|
|
182
|
+
return middleware ? empty.use(middleware) : empty;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export {
|
|
186
|
+
isArray,
|
|
187
|
+
isFunction,
|
|
188
|
+
defineParsedURL,
|
|
189
|
+
filterPlatform,
|
|
190
|
+
MiddlewareChain,
|
|
191
|
+
chain
|
|
192
|
+
};
|
|
@@ -235,6 +235,12 @@ declare class MiddlewareChain<T extends MiddlewareTypes = any> {
|
|
|
235
235
|
* any modifications it makes to the request context are not leaked.
|
|
236
236
|
*/
|
|
237
237
|
isolate(): (ctx: IsolatedContext<this>) => Awaitable<Response | void>;
|
|
238
|
+
/**
|
|
239
|
+
* @internal You should not need to call this method, unless you want a
|
|
240
|
+
* `RequestHandler` type from an empty middleware chain. If your middleware
|
|
241
|
+
* chain is **not** empty, you won't need this.
|
|
242
|
+
*/
|
|
243
|
+
toHandler(): RequestHandler<T>;
|
|
238
244
|
}
|
|
239
245
|
declare function chain<TEnv extends object = {}, TProperties extends object = {}, TPlatform = unknown>(): MiddlewareChain<{
|
|
240
246
|
initial: {
|
|
@@ -247,6 +253,6 @@ declare function chain<TEnv extends object = {}, TProperties extends object = {}
|
|
|
247
253
|
};
|
|
248
254
|
platform: TPlatform;
|
|
249
255
|
}>;
|
|
250
|
-
declare function chain<
|
|
256
|
+
declare function chain<T extends Middleware | MiddlewareChain>(middleware: T): T extends Middleware ? ApplyFirstMiddleware<T> : T;
|
|
251
257
|
|
|
252
258
|
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 RequestContext as j, type RequestHandler as k, type RequestMiddleware as l, type RequestPlugin as m, type ResponseCallback as n };
|
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 RequestContext, k as RequestHandler, l as RequestMiddleware, m as RequestPlugin, n as ResponseCallback, d as chain, f as filterPlatform } from './index-
|
|
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 RequestContext, k as RequestHandler, l as RequestMiddleware, m as RequestPlugin, n as ResponseCallback, d as chain, f as filterPlatform } from './index-BK1OAKm-.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-BK1OAKm-.js';
|
|
2
|
+
export { b as RouteContext, c as RouteHandler } from './index-BK1OAKm-.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
|
-
};
|