alien-middleware 0.10.2 → 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.
@@ -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<const T extends Middleware = Middleware<{}, {}, unknown>>(middleware: T): ApplyFirstMiddleware<T>;
256
+ declare function chain<T extends Middleware | MiddlewareChain>(middleware: T): T extends Middleware ? ApplyFirstMiddleware<T> : T;
251
257
 
252
- export { type ApplyMiddleware as A, type EmptyMiddlewareChain as E, 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 };
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, g as EnvAccessor, h as ExtractMiddleware, 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-CqtSRV4s.js';
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
@@ -2,7 +2,7 @@ import {
2
2
  MiddlewareChain,
3
3
  chain,
4
4
  filterPlatform
5
- } from "./chunk-BBMRIHZ5.js";
5
+ } from "./chunk-FHGDNFTY.js";
6
6
  export {
7
7
  MiddlewareChain,
8
8
  chain,
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-CqtSRV4s.js';
2
- export { b as RouteContext, c as RouteHandler } from './index-CqtSRV4s.js';
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
@@ -3,7 +3,7 @@ import {
3
3
  defineParsedURL,
4
4
  isArray,
5
5
  isFunction
6
- } from "./chunk-BBMRIHZ5.js";
6
+ } from "./chunk-FHGDNFTY.js";
7
7
 
8
8
  // src/router.ts
9
9
  import { compilePaths } from "pathic";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "alien-middleware",
3
3
  "type": "module",
4
- "version": "0.10.2",
4
+ "version": "0.10.4",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",
@@ -1,178 +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
- 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
- // src/middleware/filterPlatform.ts
25
- function filterPlatform(name) {
26
- return function(ctx) {
27
- if (ctx.platform.name !== name) {
28
- return ctx.passThrough();
29
- }
30
- return null;
31
- };
32
- }
33
-
34
- // src/index.ts
35
- var kRequestChain = Symbol("requestChain");
36
- var kIgnoreNotFound = Symbol("ignoreNotFound");
37
- var kMiddlewareCache = Symbol("middlewareCache");
38
- var kResponseHeaders = Symbol("responseHeaders");
39
- var MiddlewareChain = class _MiddlewareChain {
40
- [kRequestChain] = [];
41
- /**
42
- * Add a request middleware to the end of the chain.
43
- *
44
- * If a middleware chain is given, its middlewares will be executed after any
45
- * existing middlewares in this chain.
46
- *
47
- * @returns a new `MiddlewareChain` instance
48
- */
49
- use(middleware) {
50
- return createHandler(
51
- middleware instanceof _MiddlewareChain ? [...this[kRequestChain], ...middleware[kRequestChain]] : [...this[kRequestChain], middleware]
52
- );
53
- }
54
- /**
55
- * Create a middleware function that encapsulates this middleware chain, so
56
- * any modifications it makes to the request context are not leaked.
57
- */
58
- isolate() {
59
- return isFunction(this) ? (ctx) => this(ctx) : noop;
60
- }
61
- };
62
- function createHandler(requestChain) {
63
- async function handler(parentContext) {
64
- const context = Object.create(parentContext);
65
- context[kIgnoreNotFound] = true;
66
- defineParsedURL(context);
67
- const { passThrough } = context;
68
- let shouldPassThrough = false;
69
- context.passThrough = () => {
70
- shouldPassThrough = true;
71
- };
72
- context.setHeader = (name, value) => {
73
- if (context[kResponseHeaders] === parentContext[kResponseHeaders]) {
74
- context[kResponseHeaders] = new Headers(parentContext[kResponseHeaders]);
75
- }
76
- context[kResponseHeaders].set(name, value);
77
- };
78
- const responseChain = [];
79
- context.onResponse = (callback) => {
80
- responseChain.push(callback);
81
- };
82
- const cache = context[kMiddlewareCache] = new Set(
83
- parentContext[kMiddlewareCache]
84
- );
85
- let response;
86
- let env;
87
- for (const middleware of requestChain) {
88
- if (cache.has(middleware)) {
89
- continue;
90
- }
91
- cache.add(middleware);
92
- let result = middleware(context);
93
- if (result instanceof Promise) {
94
- result = await result;
95
- }
96
- if (shouldPassThrough) {
97
- break;
98
- }
99
- if (result) {
100
- if (result instanceof Response) {
101
- response = result;
102
- break;
103
- }
104
- for (const key in result) {
105
- if (key === "env") {
106
- if (result.env) {
107
- env ||= createExtendedEnv(context);
108
- Object.defineProperties(
109
- env,
110
- Object.getOwnPropertyDescriptors(result.env)
111
- );
112
- }
113
- } else if (key === "onResponse") {
114
- if (result.onResponse) {
115
- responseChain.push(result.onResponse);
116
- }
117
- } else {
118
- const descriptor = Object.getOwnPropertyDescriptor(result, key);
119
- descriptor.configurable = false;
120
- Object.defineProperty(context, key, descriptor);
121
- }
122
- }
123
- }
124
- }
125
- if (!response) {
126
- if (parentContext[kIgnoreNotFound]) {
127
- return;
128
- }
129
- response = new Response("Not Found", { status: 404 });
130
- if (shouldPassThrough) {
131
- passThrough();
132
- return response;
133
- }
134
- } else if (response.type !== "default") {
135
- response = new Response(response.body, response);
136
- }
137
- context[kResponseHeaders]?.forEach((value, name) => {
138
- response.headers.set(name, value);
139
- });
140
- context.setHeader = null;
141
- for (const plugin of responseChain) {
142
- let result = plugin(response);
143
- if (result instanceof Promise) {
144
- result = await result;
145
- }
146
- if (result) {
147
- response = result;
148
- continue;
149
- }
150
- }
151
- return response;
152
- }
153
- Object.setPrototypeOf(handler, MiddlewareChain.prototype);
154
- handler[kRequestChain] = requestChain;
155
- return handler;
156
- }
157
- function createExtendedEnv(context) {
158
- const env = /* @__PURE__ */ Object.create(null);
159
- const superEnv = context.env;
160
- context.env = (key) => env[key] ?? superEnv(key);
161
- return env;
162
- }
163
- function chain(middleware) {
164
- if (middleware instanceof MiddlewareChain) {
165
- return middleware;
166
- }
167
- const empty = new MiddlewareChain();
168
- return middleware ? empty.use(middleware) : empty;
169
- }
170
-
171
- export {
172
- isArray,
173
- isFunction,
174
- defineParsedURL,
175
- filterPlatform,
176
- MiddlewareChain,
177
- chain
178
- };