alien-middleware 0.6.0 → 0.7.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-HZDENULC.js +29 -0
- package/dist/{index-CuxbHrbf.d.ts → index-DEMeaByn.d.ts} +14 -15
- package/dist/index.d.ts +2 -2
- package/dist/index.js +41 -29
- package/dist/router.d.ts +3 -3
- package/dist/router.js +12 -15
- package/package.json +3 -2
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
function isFunction(value) {
|
|
6
|
+
return typeof value === "function";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/url.ts
|
|
10
|
+
var urlDescriptor = {
|
|
11
|
+
configurable: true,
|
|
12
|
+
get() {
|
|
13
|
+
const url = new URL(this.request.url);
|
|
14
|
+
Object.defineProperty(this, "url", { value: url });
|
|
15
|
+
return url;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function defineParsedURL(context) {
|
|
19
|
+
if (!("url" in context)) {
|
|
20
|
+
Object.defineProperty(context, "url", urlDescriptor);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
noop,
|
|
26
|
+
isArray,
|
|
27
|
+
isFunction,
|
|
28
|
+
defineParsedURL
|
|
29
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { Any, noop } from 'radashi';
|
|
1
2
|
import { AdapterRequestContext, HattipHandler } from '@hattip/core';
|
|
2
|
-
import { Any } from 'radashi';
|
|
3
3
|
|
|
4
4
|
type RequestEnvPlugin = {
|
|
5
5
|
/**
|
|
@@ -55,6 +55,7 @@ type RequestContext<TProperties extends object = never, TEnv extends object = an
|
|
|
55
55
|
* When type `T` is `never`, a default context is returned.
|
|
56
56
|
*/
|
|
57
57
|
type MiddlewareContext<T extends MiddlewareChain> = [T] extends [never] ? RequestContext<{}, {}, unknown> : RequestContext<Properties<T>, Env<T>, Platform<T>>;
|
|
58
|
+
type IsolatedContext<T extends MiddlewareChain> = RequestContext<InputProperties<T>, InputEnv<T>, Platform<T>>;
|
|
58
59
|
type Awaitable<T> = T | Promise<T>;
|
|
59
60
|
type RequestMiddleware<T extends MiddlewareChain = MiddlewareChain> = (context: RequestContext<InputProperties<T>, InputEnv<T>, Platform<T>>) => Awaitable<Response | RequestPlugin | void>;
|
|
60
61
|
type ResponseMiddleware<T extends MiddlewareChain = MiddlewareChain> = (context: RequestContext<InputProperties<T>, InputEnv<T>, Platform<T>>, response: Response) => Awaitable<Response | void>;
|
|
@@ -74,7 +75,7 @@ type ExtractMiddleware<T extends MiddlewareChain> = Middleware<Properties<T>, En
|
|
|
74
75
|
type Merge<TSource extends object, TOverrides extends object | undefined> = {} & (TOverrides extends object ? {
|
|
75
76
|
[K in keyof TSource | keyof TOverrides]: K extends keyof TOverrides ? TOverrides[K] : K extends keyof TSource ? TSource[K] : never;
|
|
76
77
|
} : TSource);
|
|
77
|
-
type ApplyRequestPlugin<TParent extends MiddlewareChain, TPlugin extends RequestPlugin> = {
|
|
78
|
+
type ApplyRequestPlugin<TParent extends MiddlewareChain, TPlugin extends RequestPlugin> = {} & {
|
|
78
79
|
properties: Merge<Properties<TParent>, Omit<TPlugin, keyof RequestEnvPlugin>>;
|
|
79
80
|
env: Merge<Env<TParent>, TPlugin['env']>;
|
|
80
81
|
};
|
|
@@ -83,7 +84,10 @@ type ApplyRequestPlugin<TParent extends MiddlewareChain, TPlugin extends Request
|
|
|
83
84
|
* chain, it's treated as a nested chain, which won't leak its plugins into the
|
|
84
85
|
* parent chain.
|
|
85
86
|
*/
|
|
86
|
-
type ApplyMiddleware<
|
|
87
|
+
type ApplyMiddleware<TFirst extends MiddlewareChain, TSecond extends Middleware<Properties<TFirst>, Env<TFirst>, Platform<TFirst>>> = RequestHandler<Inputs<TFirst>, TSecond extends MiddlewareChain ? {
|
|
88
|
+
properties: Merge<Properties<TFirst>, Properties<TSecond>>;
|
|
89
|
+
env: Merge<Env<TFirst>, Env<TSecond>>;
|
|
90
|
+
} : TSecond extends () => Awaitable<infer TResult> ? TResult extends RequestPlugin ? ApplyRequestPlugin<TFirst, TResult> : Current<TFirst> : Current<TFirst>, Platform<TFirst>>;
|
|
87
91
|
type EmptyMiddlewareChain = MiddlewareChain<{
|
|
88
92
|
properties: {};
|
|
89
93
|
env: {};
|
|
@@ -92,10 +96,6 @@ type EmptyMiddlewareChain = MiddlewareChain<{
|
|
|
92
96
|
env: {};
|
|
93
97
|
}, unknown>;
|
|
94
98
|
type ApplyFirstMiddleware<T extends Middleware> = T extends MiddlewareChain ? T : ApplyMiddleware<EmptyMiddlewareChain, T>;
|
|
95
|
-
type MergeMiddleware<TFirst extends MiddlewareChain, TSecond extends Middleware<Properties<TFirst>, Env<TFirst>, Platform<TFirst>>> = RequestHandler<Inputs<TFirst>, TSecond extends MiddlewareChain ? {
|
|
96
|
-
properties: Merge<Properties<TFirst>, Properties<TSecond>>;
|
|
97
|
-
env: Merge<Env<TFirst>, Env<TSecond>>;
|
|
98
|
-
} : TSecond extends () => Awaitable<infer TPlugin extends RequestPlugin> ? ApplyRequestPlugin<TFirst, TPlugin> : Current<TFirst>, Platform<TFirst>>;
|
|
99
99
|
type RouteMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
|
|
100
100
|
type RouterContext<T extends MiddlewareChain = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = MiddlewareContext<T> & {
|
|
101
101
|
params: TPathParams;
|
|
@@ -127,18 +127,17 @@ TPlatform = any> {
|
|
|
127
127
|
* treated as a response middleware. Otherwise, it will be treated as a
|
|
128
128
|
* request middleware.
|
|
129
129
|
*
|
|
130
|
+
* If a middleware chain is given, its middlewares will be executed after any
|
|
131
|
+
* existing middlewares in this chain.
|
|
132
|
+
*
|
|
130
133
|
* @returns a new `MiddlewareChain` instance
|
|
131
134
|
*/
|
|
132
135
|
use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): ApplyMiddleware<this, TMiddleware>;
|
|
133
136
|
/**
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* For ease of use, this method may be given a middleware function, which
|
|
138
|
-
* short-circuits to the `use` method. You should prefer using the `use`
|
|
139
|
-
* method directly, if possible.
|
|
137
|
+
* Create a middleware function that encapsulates this middleware chain, so
|
|
138
|
+
* any modifications it makes to the request context are not leaked.
|
|
140
139
|
*/
|
|
141
|
-
|
|
140
|
+
isolate(): ((ctx: IsolatedContext<this>) => Promise<Response | void>) | typeof noop;
|
|
142
141
|
}
|
|
143
142
|
declare function chain<TProperties extends object = {}, TEnv extends object = {}, TPlatform = unknown>(): MiddlewareChain<{
|
|
144
143
|
env: TEnv;
|
|
@@ -149,4 +148,4 @@ declare function chain<TProperties extends object = {}, TEnv extends object = {}
|
|
|
149
148
|
}, TPlatform>;
|
|
150
149
|
declare function chain<const T extends Middleware = Middleware>(middleware: T): ApplyFirstMiddleware<T>;
|
|
151
150
|
|
|
152
|
-
export { type ApplyMiddleware as A, type EmptyMiddlewareChain as E, MiddlewareChain as M, type RouteHandler as R, type MiddlewareContext as a, type RouteMethod as b, type RouterContext as c, chain as d, type ExtractMiddleware as e, type
|
|
151
|
+
export { type ApplyMiddleware as A, type EmptyMiddlewareChain as E, MiddlewareChain as M, type RouteHandler as R, type MiddlewareContext as a, type RouteMethod as b, type RouterContext as c, chain as d, type ExtractMiddleware as e, type Middleware as f, type RequestContext as g, type RequestHandler as h, type RequestMiddleware as i, type RequestPlugin as j, type ResponseMiddleware as k };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as ApplyMiddleware, e as ExtractMiddleware, f as MergeMiddleware, g as Middleware, M as MiddlewareChain, a as MiddlewareContext, h as RequestContext, i as RequestHandler, j as RequestMiddleware, k as RequestPlugin, l as ResponseMiddleware, d as chain } from './index-CuxbHrbf.js';
|
|
2
|
-
import '@hattip/core';
|
|
3
1
|
import 'radashi';
|
|
2
|
+
export { A as ApplyMiddleware, e as ExtractMiddleware, f as Middleware, M as MiddlewareChain, a as MiddlewareContext, g as RequestContext, h as RequestHandler, i as RequestMiddleware, j as RequestPlugin, k as ResponseMiddleware, d as chain } from './index-DEMeaByn.js';
|
|
3
|
+
import '@hattip/core';
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineParsedURL,
|
|
3
|
+
isFunction,
|
|
4
|
+
noop
|
|
5
|
+
} from "./chunk-HZDENULC.js";
|
|
6
|
+
|
|
1
7
|
// src/index.ts
|
|
2
8
|
var kRequestChain = Symbol("requestChain");
|
|
3
9
|
var kResponseChain = Symbol("responseChain");
|
|
4
10
|
var kIgnoreNotFound = Symbol("ignoreNotFound");
|
|
5
11
|
var kMiddlewareCache = Symbol("middlewareCache");
|
|
6
|
-
var urlDescriptor = {
|
|
7
|
-
configurable: true,
|
|
8
|
-
get() {
|
|
9
|
-
const url = new URL(this.request.url);
|
|
10
|
-
Object.defineProperty(this, "url", { value: url });
|
|
11
|
-
return url;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
12
|
var MiddlewareChain = class _MiddlewareChain {
|
|
15
13
|
[kRequestChain] = [];
|
|
16
14
|
[kResponseChain] = [];
|
|
@@ -19,9 +17,18 @@ var MiddlewareChain = class _MiddlewareChain {
|
|
|
19
17
|
* treated as a response middleware. Otherwise, it will be treated as a
|
|
20
18
|
* request middleware.
|
|
21
19
|
*
|
|
20
|
+
* If a middleware chain is given, its middlewares will be executed after any
|
|
21
|
+
* existing middlewares in this chain.
|
|
22
|
+
*
|
|
22
23
|
* @returns a new `MiddlewareChain` instance
|
|
23
24
|
*/
|
|
24
25
|
use(middleware) {
|
|
26
|
+
if (middleware instanceof _MiddlewareChain) {
|
|
27
|
+
return createHandler(
|
|
28
|
+
[...this[kRequestChain], ...middleware[kRequestChain]],
|
|
29
|
+
[...this[kResponseChain], ...middleware[kResponseChain]]
|
|
30
|
+
);
|
|
31
|
+
}
|
|
25
32
|
let requestChain = this[kRequestChain];
|
|
26
33
|
let responseChain = this[kResponseChain];
|
|
27
34
|
if (middleware.length < 2) {
|
|
@@ -32,30 +39,23 @@ var MiddlewareChain = class _MiddlewareChain {
|
|
|
32
39
|
return createHandler(requestChain, responseChain);
|
|
33
40
|
}
|
|
34
41
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* For ease of use, this method may be given a middleware function, which
|
|
39
|
-
* short-circuits to the `use` method. You should prefer using the `use`
|
|
40
|
-
* method directly, if possible.
|
|
42
|
+
* Create a middleware function that encapsulates this middleware chain, so
|
|
43
|
+
* any modifications it makes to the request context are not leaked.
|
|
41
44
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return createHandler(
|
|
45
|
-
[...this[kRequestChain], ...chain2[kRequestChain]],
|
|
46
|
-
[...this[kResponseChain], ...chain2[kResponseChain]]
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
return this.use(chain2);
|
|
45
|
+
isolate() {
|
|
46
|
+
return isFunction(this) ? (ctx) => this(ctx) : noop;
|
|
50
47
|
}
|
|
51
48
|
};
|
|
52
49
|
function createHandler(requestChain, responseChain) {
|
|
53
50
|
async function handler(parentContext) {
|
|
54
51
|
const context = Object.create(parentContext);
|
|
55
52
|
context[kIgnoreNotFound] = true;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
defineParsedURL(context);
|
|
54
|
+
const { passThrough } = context;
|
|
55
|
+
let shouldPassThrough = false;
|
|
56
|
+
context.passThrough = () => {
|
|
57
|
+
shouldPassThrough = true;
|
|
58
|
+
};
|
|
59
59
|
const cache = context[kMiddlewareCache] ||= /* @__PURE__ */ new Set();
|
|
60
60
|
let response;
|
|
61
61
|
let env;
|
|
@@ -68,17 +68,23 @@ function createHandler(requestChain, responseChain) {
|
|
|
68
68
|
if (result instanceof Promise) {
|
|
69
69
|
result = await result;
|
|
70
70
|
}
|
|
71
|
+
if (shouldPassThrough) {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
71
74
|
if (result) {
|
|
72
75
|
if (result instanceof Response) {
|
|
73
76
|
response = result;
|
|
74
77
|
break;
|
|
75
78
|
}
|
|
76
79
|
for (const key in result) {
|
|
77
|
-
const descriptor = Object.getOwnPropertyDescriptor(result, key);
|
|
78
80
|
if (key === "env") {
|
|
79
81
|
env ||= createExtendedEnv(context);
|
|
80
|
-
Object.
|
|
82
|
+
Object.defineProperties(
|
|
83
|
+
env,
|
|
84
|
+
Object.getOwnPropertyDescriptors(result.env)
|
|
85
|
+
);
|
|
81
86
|
} else {
|
|
87
|
+
const descriptor = Object.getOwnPropertyDescriptor(result, key);
|
|
82
88
|
descriptor.configurable = false;
|
|
83
89
|
Object.defineProperty(context, key, descriptor);
|
|
84
90
|
}
|
|
@@ -90,6 +96,12 @@ function createHandler(requestChain, responseChain) {
|
|
|
90
96
|
return;
|
|
91
97
|
}
|
|
92
98
|
response = new Response("Not Found", { status: 404 });
|
|
99
|
+
if (shouldPassThrough) {
|
|
100
|
+
passThrough();
|
|
101
|
+
return response;
|
|
102
|
+
}
|
|
103
|
+
} else if (response.type !== "default") {
|
|
104
|
+
response = new Response(response.body, response);
|
|
93
105
|
}
|
|
94
106
|
for (const middleware of responseChain) {
|
|
95
107
|
if (cache.has(middleware)) {
|
|
@@ -122,8 +134,8 @@ function chain(middleware) {
|
|
|
122
134
|
if (middleware instanceof MiddlewareChain) {
|
|
123
135
|
return middleware;
|
|
124
136
|
}
|
|
125
|
-
const
|
|
126
|
-
return middleware ?
|
|
137
|
+
const empty = new MiddlewareChain();
|
|
138
|
+
return middleware ? empty.use(middleware) : empty;
|
|
127
139
|
}
|
|
128
140
|
export {
|
|
129
141
|
MiddlewareChain,
|
package/dist/router.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { InferParams } from 'pathic';
|
|
2
|
-
import { M as MiddlewareChain, E as EmptyMiddlewareChain, a as MiddlewareContext, R as RouteHandler, b as RouteMethod } from './index-
|
|
3
|
-
export { c as RouterContext } from './index-
|
|
4
|
-
import '@hattip/core';
|
|
2
|
+
import { M as MiddlewareChain, E as EmptyMiddlewareChain, a as MiddlewareContext, R as RouteHandler, b as RouteMethod } from './index-DEMeaByn.js';
|
|
3
|
+
export { c as RouterContext } from './index-DEMeaByn.js';
|
|
5
4
|
import 'radashi';
|
|
5
|
+
import '@hattip/core';
|
|
6
6
|
|
|
7
7
|
type OneOrMany<T> = T | readonly T[];
|
|
8
8
|
type Router<T extends MiddlewareChain = any> = ReturnType<typeof routes<T>>;
|
package/dist/router.js
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var asyncIteratorSymbol = (
|
|
7
|
-
/* c8 ignore next */
|
|
8
|
-
Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator")
|
|
9
|
-
);
|
|
10
|
-
function isFunction(value) {
|
|
11
|
-
return typeof value === "function";
|
|
12
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
defineParsedURL,
|
|
3
|
+
isArray,
|
|
4
|
+
isFunction
|
|
5
|
+
} from "./chunk-HZDENULC.js";
|
|
13
6
|
|
|
14
7
|
// src/router.ts
|
|
8
|
+
import { compilePaths } from "pathic";
|
|
15
9
|
function routes(middlewares) {
|
|
16
10
|
const paths = [];
|
|
17
11
|
const filters = [];
|
|
18
12
|
const handlers = [];
|
|
13
|
+
let matcher;
|
|
19
14
|
function use(method, path, handler) {
|
|
20
15
|
if (isFunction(path)) {
|
|
21
16
|
paths.push(method);
|
|
@@ -28,13 +23,15 @@ function routes(middlewares) {
|
|
|
28
23
|
);
|
|
29
24
|
handlers.push(handler);
|
|
30
25
|
}
|
|
26
|
+
matcher = void 0;
|
|
31
27
|
return router;
|
|
32
28
|
}
|
|
33
|
-
let matcher;
|
|
34
29
|
function router(context) {
|
|
30
|
+
defineParsedURL(context);
|
|
31
|
+
const { request, url } = context;
|
|
32
|
+
const method = request.method;
|
|
35
33
|
matcher ||= compilePaths(paths);
|
|
36
|
-
|
|
37
|
-
return matcher(context.request.path, (index, params) => {
|
|
34
|
+
return matcher(url.pathname, (index, params) => {
|
|
38
35
|
if (!filters[index] || filters[index](method)) {
|
|
39
36
|
context.method = method;
|
|
40
37
|
context.params = params;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alien-middleware",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -29,13 +29,14 @@
|
|
|
29
29
|
"@alloc/prettier-config": "^1.0.0",
|
|
30
30
|
"@hattip/core": "^0.0.49",
|
|
31
31
|
"@types/node": "^22.15.3",
|
|
32
|
+
"@vitest/coverage-v8": "^3.1.3",
|
|
32
33
|
"prettier": "^3.5.3",
|
|
33
34
|
"radashi": "12.5.0-beta.6d5c035",
|
|
34
35
|
"rimraf": "^6.0.1",
|
|
35
36
|
"tsc-lint": "^0.1.9",
|
|
36
37
|
"tsup": "^8.4.0",
|
|
37
38
|
"typescript": "^5.8.3",
|
|
38
|
-
"vitest": "^3.1.
|
|
39
|
+
"vitest": "^3.1.3"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
42
|
"pathic": "^0.1.6"
|