@nhtio/middleware 1.20251013.0 → 1.20251110.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/index.cjs CHANGED
@@ -1,2 +1,301 @@
1
- "use strict";var H=t=>{throw TypeError(t)};var M=(t,r,e)=>r.has(t)||H("Cannot "+e);var n=(t,r,e)=>(M(t,r,"read from private field"),e?e.call(t):r.get(t)),a=(t,r,e)=>r.has(t)?H("Cannot add the same private member more than once"):r instanceof WeakSet?r.add(t):r.set(t,e),s=(t,r,e,i)=>(M(t,r,"write to private field"),i?i.call(t,e):r.set(t,e),e),f=(t,r,e)=>(M(t,r,"access private method"),e);var z=(t,r,e,i)=>({set _(l){s(t,r,l,e)},get _(){return n(t,r,i)}});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function y(t,r){function e(){if(!e.called)return e.called=!0,r(t)}return e.called=!1,e}const C=()=>Promise.resolve();var u,v,w,m,c,h,k,g;class A{constructor(r){a(this,h);a(this,u);a(this,v,0);a(this,w);a(this,m,C);a(this,c);s(this,u,r)}finalHandler(r){return s(this,m,r),this}errorHandler(r){return s(this,c,r),this}async run(r){return s(this,w,r),n(this,c)?f(this,h,g).call(this,this):f(this,h,k).call(this,this)}}u=new WeakMap,v=new WeakMap,w=new WeakMap,m=new WeakMap,c=new WeakMap,h=new WeakSet,k=function(r){var i,l;const e=n(r,u)[z(r,v)._++];return e?n(l=r,w).call(l,e,y(r,f(r,h,k))):n(i=r,m).call(i)},g=function(r){var i,l;const e=n(r,u)[z(r,v)._++];return e?n(l=r,w).call(l,e,y(r,f(r,h,g))).catch(n(r,c)):n(i=r,m).call(i).catch(n(r,c))};var o,E,d;class x{constructor(){a(this,o,new Set);a(this,E);a(this,d,!1)}all(){return n(this,o)}has(r){return n(this,o).has(r)}add(r){if(n(this,d))throw new Error("Middleware stack is frozen. Cannot add new middleware");return n(this,o).add(r),this}remove(r){if(n(this,d))throw new Error("Middleware stack is frozen. Cannot remove middleware");return n(this,o).delete(r)}clear(){if(n(this,d))throw new Error("Middleware stack is frozen. Cannot clear middleware");n(this,o).clear()}merge(r){if(n(this,d))throw new Error("Middleware stack is frozen. Cannot merge middleware");r.all().forEach(e=>{this.add(e)})}freeze(){n(this,d)||(s(this,d,!0),s(this,E,[...this.all()]))}runner(){return this.freeze(),new A(n(this,E))}}o=new WeakMap,E=new WeakMap,d=new WeakMap;const F="1.20251013.0";exports.Middleware=x;exports.Runner=A;exports.version=F;
1
+ "use strict";
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
5
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
6
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
7
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
8
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
9
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
10
+ var __privateWrapper = (obj, member, setter, getter) => ({
11
+ set _(value) {
12
+ __privateSet(obj, member, value, setter);
13
+ },
14
+ get _() {
15
+ return __privateGet(obj, member, getter);
16
+ }
17
+ });
18
+ var _middleware, _currentIndex, _executor, _finalHandler, _errorHandler, _Runner_instances, invoke_fn, invokeWithErrorManagement_fn, _middleware2, _middlewareArray, _isFrozen;
19
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
20
+ function once(scope, callback) {
21
+ function next() {
22
+ if (next.called) {
23
+ return;
24
+ }
25
+ next.called = true;
26
+ return callback(scope);
27
+ }
28
+ next.called = false;
29
+ return next;
30
+ }
31
+ const DEFAULT_FINAL_HANDLER = () => Promise.resolve();
32
+ class Runner {
33
+ constructor(middleware) {
34
+ __privateAdd(this, _Runner_instances);
35
+ /**
36
+ * The array of middleware handlers to execute in sequence.
37
+ */
38
+ __privateAdd(this, _middleware);
39
+ /**
40
+ * The current index position in the middleware pipeline.
41
+ */
42
+ __privateAdd(this, _currentIndex, 0);
43
+ /**
44
+ * The executor function responsible for invoking each middleware handler.
45
+ */
46
+ __privateAdd(this, _executor);
47
+ /**
48
+ * The final handler to execute when the middleware chain completes successfully.
49
+ */
50
+ __privateAdd(this, _finalHandler, DEFAULT_FINAL_HANDLER);
51
+ /**
52
+ * Optional error handler to catch and handle exceptions in the middleware pipeline.
53
+ */
54
+ __privateAdd(this, _errorHandler);
55
+ __privateSet(this, _middleware, middleware);
56
+ }
57
+ /**
58
+ * Sets a custom final handler to execute when the middleware chain completes successfully.
59
+ *
60
+ * The final handler is called when the entire middleware pipeline reaches the end
61
+ * by calling `next` through all handlers. This makes it easier to execute custom
62
+ * logic that is not part of the chain but must run when the chain ends.
63
+ *
64
+ * @param finalHandler - The function to execute when the chain completes
65
+ * @returns The Runner instance for method chaining
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * await middleware
70
+ * .runner()
71
+ * .finalHandler(() => {
72
+ * console.log('All middleware completed')
73
+ * })
74
+ * .run((fn, next) => fn(context, next))
75
+ * ```
76
+ */
77
+ finalHandler(finalHandler) {
78
+ __privateSet(this, _finalHandler, finalHandler);
79
+ return this;
80
+ }
81
+ /**
82
+ * Sets a custom error handler to catch exceptions in the middleware pipeline.
83
+ *
84
+ * By default, exceptions raised in the middleware pipeline bubble up to the `run`
85
+ * method and can be captured using a try/catch block. When an exception is raised,
86
+ * the middleware downstream logic will not run.
87
+ *
88
+ * Defining an error handler changes this behavior:
89
+ * - The `run` method will not throw exceptions
90
+ * - Errors are caught and passed to the error handler
91
+ * - Middleware upstream logic (after `next`) can still execute
92
+ *
93
+ * @param errorHandler - The function to handle errors
94
+ * @returns The Runner instance for method chaining
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * await middleware
99
+ * .runner()
100
+ * .errorHandler((error) => {
101
+ * console.error('Middleware error:', error)
102
+ * })
103
+ * .run((fn, next) => fn(context, next))
104
+ * ```
105
+ */
106
+ errorHandler(errorHandler) {
107
+ __privateSet(this, _errorHandler, errorHandler);
108
+ return this;
109
+ }
110
+ /**
111
+ * Executes the middleware pipeline using the provided executor function.
112
+ *
113
+ * The executor function is responsible for invoking each middleware handler with
114
+ * the appropriate context and the `next` callback. Since you control the executor,
115
+ * you can pass any data you want to the middleware.
116
+ *
117
+ * @param cb - The executor function that invokes each middleware handler
118
+ * @returns A Promise that resolves when the middleware pipeline completes
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * const context = { user: null }
123
+ * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>
124
+ *
125
+ * const middleware = new Middleware<MiddlewareFn>()
126
+ * middleware.add(async (ctx, next) => {
127
+ * ctx.user = await authenticate()
128
+ * await next()
129
+ * })
130
+ *
131
+ * await middleware
132
+ * .runner()
133
+ * .run((fn, next) => fn(context, next))
134
+ * ```
135
+ */
136
+ async run(cb) {
137
+ __privateSet(this, _executor, cb);
138
+ if (__privateGet(this, _errorHandler)) {
139
+ return __privateMethod(this, _Runner_instances, invokeWithErrorManagement_fn).call(this, this);
140
+ }
141
+ return __privateMethod(this, _Runner_instances, invoke_fn).call(this, this);
142
+ }
143
+ }
144
+ _middleware = new WeakMap();
145
+ _currentIndex = new WeakMap();
146
+ _executor = new WeakMap();
147
+ _finalHandler = new WeakMap();
148
+ _errorHandler = new WeakMap();
149
+ _Runner_instances = new WeakSet();
150
+ /**
151
+ * Invokes middleware handlers one at a time.
152
+ *
153
+ * Middleware functions are executed recursively until `next` is called.
154
+ * If a middleware doesn't call `next`, the chain will finish automatically
155
+ * without executing remaining handlers.
156
+ *
157
+ * @param self - The Runner instance scope
158
+ * @returns A Promise that resolves when the middleware chain completes
159
+ * @internal
160
+ */
161
+ invoke_fn = function(self) {
162
+ var _a, _b;
163
+ const middleware = __privateGet(self, _middleware)[__privateWrapper(self, _currentIndex)._++];
164
+ if (!middleware) {
165
+ return __privateGet(_a = self, _finalHandler).call(_a);
166
+ }
167
+ return __privateGet(_b = self, _executor).call(_b, middleware, once(self, __privateMethod(self, _Runner_instances, invoke_fn)));
168
+ };
169
+ /**
170
+ * Invokes middleware handlers with error handling.
171
+ *
172
+ * Similar to `#invoke`, but catches exceptions and passes them to the error handler.
173
+ * When an exception is raised, the middleware downstream logic will not run unless
174
+ * the error handler allows the chain to continue.
175
+ *
176
+ * @param self - The Runner instance scope
177
+ * @returns A Promise that resolves when the middleware chain completes
178
+ * @internal
179
+ */
180
+ invokeWithErrorManagement_fn = function(self) {
181
+ var _a, _b;
182
+ const middleware = __privateGet(self, _middleware)[__privateWrapper(self, _currentIndex)._++];
183
+ if (!middleware) {
184
+ return __privateGet(_a = self, _finalHandler).call(_a).catch(__privateGet(self, _errorHandler));
185
+ }
186
+ return __privateGet(_b = self, _executor).call(_b, middleware, once(self, __privateMethod(self, _Runner_instances, invokeWithErrorManagement_fn))).catch(__privateGet(self, _errorHandler));
187
+ };
188
+ class Middleware {
189
+ constructor() {
190
+ __privateAdd(this, _middleware2, /* @__PURE__ */ new Set());
191
+ __privateAdd(this, _middlewareArray);
192
+ __privateAdd(this, _isFrozen, false);
193
+ }
194
+ /**
195
+ * Returns all registered middleware handlers.
196
+ *
197
+ * @returns A Set containing all registered middleware handlers
198
+ */
199
+ all() {
200
+ return __privateGet(this, _middleware2);
201
+ }
202
+ /**
203
+ * Checks if a specific handler has already been registered as middleware.
204
+ *
205
+ * @param handler - The middleware handler to check for
206
+ * @returns `true` if the handler is registered, `false` otherwise
207
+ */
208
+ has(handler) {
209
+ return __privateGet(this, _middleware2).has(handler);
210
+ }
211
+ /**
212
+ * Registers a new middleware handler to the pipeline.
213
+ *
214
+ * Adding the same middleware handler multiple times will result in a no-op,
215
+ * as handlers are stored in a Set to prevent duplicates.
216
+ *
217
+ * @param handler - The middleware handler to register
218
+ * @returns The Middleware instance for method chaining
219
+ * @throws {Error} If the middleware stack is frozen
220
+ */
221
+ add(handler) {
222
+ if (__privateGet(this, _isFrozen)) {
223
+ throw new Error("Middleware stack is frozen. Cannot add new middleware");
224
+ }
225
+ __privateGet(this, _middleware2).add(handler);
226
+ return this;
227
+ }
228
+ /**
229
+ * Removes a specific middleware handler from the pipeline.
230
+ *
231
+ * @param handler - The middleware handler to remove
232
+ * @returns `true` if the handler was removed, `false` if it was not found
233
+ * @throws {Error} If the middleware stack is frozen
234
+ */
235
+ remove(handler) {
236
+ if (__privateGet(this, _isFrozen)) {
237
+ throw new Error("Middleware stack is frozen. Cannot remove middleware");
238
+ }
239
+ return __privateGet(this, _middleware2).delete(handler);
240
+ }
241
+ /**
242
+ * Removes all registered middleware handlers from the pipeline.
243
+ *
244
+ * @throws {Error} If the middleware stack is frozen
245
+ */
246
+ clear() {
247
+ if (__privateGet(this, _isFrozen)) {
248
+ throw new Error("Middleware stack is frozen. Cannot clear middleware");
249
+ }
250
+ __privateGet(this, _middleware2).clear();
251
+ }
252
+ /**
253
+ * Merges middleware handlers from another Middleware instance.
254
+ *
255
+ * The middleware from the source instance are appended to the current instance.
256
+ *
257
+ * @param hooks - The source Middleware instance to merge from
258
+ * @throws {Error} If the middleware stack is frozen
259
+ */
260
+ merge(hooks) {
261
+ if (__privateGet(this, _isFrozen)) {
262
+ throw new Error("Middleware stack is frozen. Cannot merge middleware");
263
+ }
264
+ hooks.all().forEach((handler) => {
265
+ this.add(handler);
266
+ });
267
+ }
268
+ /**
269
+ * Freezes the middleware stack to prevent further modifications.
270
+ *
271
+ * Once frozen, the middleware array is cached and no new handlers can be added,
272
+ * removed, or modified. This method is automatically called when creating a runner.
273
+ */
274
+ freeze() {
275
+ if (__privateGet(this, _isFrozen)) {
276
+ return;
277
+ }
278
+ __privateSet(this, _isFrozen, true);
279
+ __privateSet(this, _middlewareArray, [...this.all()]);
280
+ }
281
+ /**
282
+ * Creates and returns a Runner instance to execute the middleware pipeline.
283
+ *
284
+ * This method automatically freezes the middleware stack to prevent modifications
285
+ * during execution.
286
+ *
287
+ * @returns A new Runner instance configured with the current middleware handlers
288
+ */
289
+ runner() {
290
+ this.freeze();
291
+ return new Runner(__privateGet(this, _middlewareArray));
292
+ }
293
+ }
294
+ _middleware2 = new WeakMap();
295
+ _middlewareArray = new WeakMap();
296
+ _isFrozen = new WeakMap();
297
+ const version = "1.20251110.0";
298
+ exports.Middleware = Middleware;
299
+ exports.Runner = Runner;
300
+ exports.version = version;
2
301
  //# sourceMappingURL=index.cjs.map
package/index.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/private/runner.ts","../src/private/middleware.ts","../src/index.ts"],"sourcesContent":["import type { ErrorHandler, Executor, FinalHandler } from './types'\n\n/**\n * Ensures a function is executed only once by maintaining a called state.\n *\n * This utility function is tightly coupled with the Runner class and is used\n * to ensure the `next` callback is not invoked multiple times in the middleware chain.\n *\n * @param scope - The Runner instance scope\n * @param callback - The function to execute only once\n * @returns A wrapped function that tracks its execution state\n * @internal\n */\nfunction once(scope: Runner<any>, callback: (scope: Runner<any>) => Promise<void> | void) {\n function next(): Promise<void> | void {\n if (next.called) {\n return\n }\n\n next.called = true\n return callback(scope)\n }\n next.called = false\n\n return next\n}\n\n/**\n * Default final handler that resolves immediately when the middleware chain completes.\n *\n * This handler is used when no custom final handler is specified via `finalHandler()`.\n *\n * @internal\n */\nconst DEFAULT_FINAL_HANDLER = () => Promise.resolve()\n\n/**\n * Runner executes an array of middleware functions in sequence.\n *\n * The middleware pipeline advances only when the current function calls `next`,\n * implementing the chain of responsibility pattern. You control how to execute\n * the underlying middleware functions by providing an executor function.\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = { stack: [] }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add((ctx, next) => {\n * ctx.stack.push('fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * context.stack.push('final handler')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Runner<MiddlewareFn extends any> {\n /**\n * The array of middleware handlers to execute in sequence.\n */\n #middleware: MiddlewareFn[]\n\n /**\n * The current index position in the middleware pipeline.\n */\n #currentIndex = 0\n\n /**\n * The executor function responsible for invoking each middleware handler.\n */\n #executor!: Executor<MiddlewareFn>\n\n /**\n * The final handler to execute when the middleware chain completes successfully.\n */\n #finalHandler: FinalHandler = DEFAULT_FINAL_HANDLER\n\n /**\n * Optional error handler to catch and handle exceptions in the middleware pipeline.\n */\n #errorHandler?: ErrorHandler\n\n constructor(middleware: MiddlewareFn[]) {\n this.#middleware = middleware\n }\n\n /**\n * Invokes middleware handlers one at a time.\n *\n * Middleware functions are executed recursively until `next` is called.\n * If a middleware doesn't call `next`, the chain will finish automatically\n * without executing remaining handlers.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invoke(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler()\n }\n\n return self.#executor(middleware, once(self, self.#invoke))\n }\n\n /**\n * Invokes middleware handlers with error handling.\n *\n * Similar to `#invoke`, but catches exceptions and passes them to the error handler.\n * When an exception is raised, the middleware downstream logic will not run unless\n * the error handler allows the chain to continue.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invokeWithErrorManagement(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler().catch(self.#errorHandler)\n }\n\n return self\n .#executor(middleware, once(self, self.#invokeWithErrorManagement))\n .catch(self.#errorHandler)\n }\n\n /**\n * Sets a custom final handler to execute when the middleware chain completes successfully.\n *\n * The final handler is called when the entire middleware pipeline reaches the end\n * by calling `next` through all handlers. This makes it easier to execute custom\n * logic that is not part of the chain but must run when the chain ends.\n *\n * @param finalHandler - The function to execute when the chain completes\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * console.log('All middleware completed')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n finalHandler(finalHandler: FinalHandler): this {\n this.#finalHandler = finalHandler\n return this\n }\n\n /**\n * Sets a custom error handler to catch exceptions in the middleware pipeline.\n *\n * By default, exceptions raised in the middleware pipeline bubble up to the `run`\n * method and can be captured using a try/catch block. When an exception is raised,\n * the middleware downstream logic will not run.\n *\n * Defining an error handler changes this behavior:\n * - The `run` method will not throw exceptions\n * - Errors are caught and passed to the error handler\n * - Middleware upstream logic (after `next`) can still execute\n *\n * @param errorHandler - The function to handle errors\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .errorHandler((error) => {\n * console.error('Middleware error:', error)\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n errorHandler(errorHandler: ErrorHandler): this {\n this.#errorHandler = errorHandler\n return this\n }\n\n /**\n * Executes the middleware pipeline using the provided executor function.\n *\n * The executor function is responsible for invoking each middleware handler with\n * the appropriate context and the `next` callback. Since you control the executor,\n * you can pass any data you want to the middleware.\n *\n * @param cb - The executor function that invokes each middleware handler\n * @returns A Promise that resolves when the middleware pipeline completes\n *\n * @example\n * ```ts\n * const context = { user: null }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add(async (ctx, next) => {\n * ctx.user = await authenticate()\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n async run(cb: Executor<MiddlewareFn>): Promise<void> {\n this.#executor = cb\n\n if (this.#errorHandler) {\n return this.#invokeWithErrorManagement(this)\n }\n\n return this.#invoke(this)\n }\n}\n","import { Runner } from './runner'\n\n/**\n * The Middleware class implements the chain of responsibility design pattern\n * and allows executing handlers in series.\n *\n * The middleware handlers can be represented as any value you wish, such as:\n * - A function: `middleware.add(function () { console.log('called') })`\n * - An object with a `handle` method: `middleware.add({ name: 'auth', handle: authenticate })`\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing middleware')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Middleware<MiddlewareFn extends any> {\n #middleware: Set<MiddlewareFn> = new Set()\n #middlewareArray?: MiddlewareFn[]\n #isFrozen: boolean = false\n\n /**\n * Returns all registered middleware handlers.\n *\n * @returns A Set containing all registered middleware handlers\n */\n all() {\n return this.#middleware\n }\n\n /**\n * Checks if a specific handler has already been registered as middleware.\n *\n * @param handler - The middleware handler to check for\n * @returns `true` if the handler is registered, `false` otherwise\n */\n has(handler: MiddlewareFn): boolean {\n return this.#middleware.has(handler)\n }\n\n /**\n * Registers a new middleware handler to the pipeline.\n *\n * Adding the same middleware handler multiple times will result in a no-op,\n * as handlers are stored in a Set to prevent duplicates.\n *\n * @param handler - The middleware handler to register\n * @returns The Middleware instance for method chaining\n * @throws {Error} If the middleware stack is frozen\n */\n add(handler: MiddlewareFn): this {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot add new middleware')\n }\n\n this.#middleware.add(handler)\n return this\n }\n\n /**\n * Removes a specific middleware handler from the pipeline.\n *\n * @param handler - The middleware handler to remove\n * @returns `true` if the handler was removed, `false` if it was not found\n * @throws {Error} If the middleware stack is frozen\n */\n remove(handler: MiddlewareFn): boolean {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot remove middleware')\n }\n\n return this.#middleware.delete(handler)\n }\n\n /**\n * Removes all registered middleware handlers from the pipeline.\n *\n * @throws {Error} If the middleware stack is frozen\n */\n clear(): void {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot clear middleware')\n }\n\n this.#middleware.clear()\n }\n\n /**\n * Merges middleware handlers from another Middleware instance.\n *\n * The middleware from the source instance are appended to the current instance.\n *\n * @param hooks - The source Middleware instance to merge from\n * @throws {Error} If the middleware stack is frozen\n */\n merge(hooks: Middleware<MiddlewareFn>) {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot merge middleware')\n }\n\n hooks.all().forEach((handler) => {\n this.add(handler)\n })\n }\n\n /**\n * Freezes the middleware stack to prevent further modifications.\n *\n * Once frozen, the middleware array is cached and no new handlers can be added,\n * removed, or modified. This method is automatically called when creating a runner.\n */\n freeze() {\n if (this.#isFrozen) {\n return\n }\n\n this.#isFrozen = true\n this.#middlewareArray = [...this.all()]\n }\n\n /**\n * Creates and returns a Runner instance to execute the middleware pipeline.\n *\n * This method automatically freezes the middleware stack to prevent modifications\n * during execution.\n *\n * @returns A new Runner instance configured with the current middleware handlers\n */\n runner(): Runner<MiddlewareFn> {\n this.freeze()\n return new Runner(this.#middlewareArray!)\n }\n}\n","/**\n * @module @nhtio/middleware\n *\n * A cross-environment (browser/node) implementation of the chain of responsibility design pattern,\n * also known as the middleware pipeline. This package is based on\n * [@poppinss/middleware](https://github.com/poppinss/middleware) and provides a zero-dependency\n * implementation for executing handlers in series.\n *\n * @example\n * ```ts\n * import { Middleware } from '@nhtio/middleware'\n * import type { NextFn } from '@nhtio/middleware'\n *\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n\n/**\n * The current version of the package.\n *\n * This constant is replaced during the build process with the actual version from package.json.\n */\nexport const version = __VERSION__\n\nexport { Middleware } from './private/middleware'\nexport { Runner } from './private/runner'\nexport * from './private/types'\n"],"names":["once","scope","callback","next","DEFAULT_FINAL_HANDLER","Runner","middleware","__privateAdd","_Runner_instances","_middleware","_currentIndex","_executor","_finalHandler","_errorHandler","__privateSet","finalHandler","errorHandler","cb","__privateGet","__privateMethod","invokeWithErrorManagement_fn","invoke_fn","self","__privateWrapper","_b","_a","Middleware","_middlewareArray","_isFrozen","handler","hooks","version"],"mappings":"4gBAaA,SAASA,EAAKC,EAAoBC,EAAwD,CACxF,SAASC,GAA6B,CACpC,GAAI,CAAAA,EAAK,OAIT,OAAAA,EAAK,OAAS,GACPD,EAASD,CAAK,CACvB,CACA,OAAAE,EAAK,OAAS,GAEPA,CACT,CASA,MAAMC,EAAwB,IAAM,QAAQ,QAAA,sBA8BrC,MAAMC,CAAiC,CA0B5C,YAAYC,EAA4B,CA1BnCC,EAAA,KAAAC,GAILD,EAAA,KAAAE,GAKAF,EAAA,KAAAG,EAAgB,GAKhBH,EAAA,KAAAI,GAKAJ,EAAA,KAAAK,EAA8BR,GAK9BG,EAAA,KAAAM,GAGEC,EAAA,KAAKL,EAAcH,EACrB,CAwEA,aAAaS,EAAkC,CAC7C,OAAAD,EAAA,KAAKF,EAAgBG,GACd,IACT,CA2BA,aAAaC,EAAkC,CAC7C,OAAAF,EAAA,KAAKD,EAAgBG,GACd,IACT,CA4BA,MAAM,IAAIC,EAA2C,CAGnD,OAFAH,EAAA,KAAKH,EAAYM,GAEbC,EAAA,KAAKL,GACAM,EAAA,KAAKX,EAAAY,GAAL,UAAgC,MAGlCD,EAAA,KAAKX,EAAAa,GAAL,UAAa,KACtB,CACF,CAtKEZ,EAAA,YAKAC,EAAA,YAKAC,EAAA,YAKAC,EAAA,YAKAC,EAAA,YAxBKL,EAAA,YAyCLa,WAAQC,EAAkD,SACxD,MAAMhB,EAAaY,EAAAI,EAAKb,GAAYc,EAAAD,EAAKZ,GAAL,GAAoB,EAKxD,OAAKJ,EAIEY,EAAAM,EAAAF,EAAKX,GAAL,KAAAa,EAAelB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,EAAA,KAAKc,EAAY,IACjBd,EAAA,KAAKa,EAAmB,CAAC,GAAG,KAAK,KAAK,GACxC,CAUA,QAA+B,CAC7B,YAAK,OAAA,EACE,IAAItB,EAAOa,EAAA,KAAKS,EAAiB,CAC1C,CACF,CAnHElB,EAAA,YACAkB,EAAA,YACAC,EAAA,YCEK,MAAMG,EAAU"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/private/runner.ts","../src/private/middleware.ts","../src/index.ts"],"sourcesContent":["import type { ErrorHandler, Executor, FinalHandler } from './types'\n\n/**\n * Ensures a function is executed only once by maintaining a called state.\n *\n * This utility function is tightly coupled with the Runner class and is used\n * to ensure the `next` callback is not invoked multiple times in the middleware chain.\n *\n * @param scope - The Runner instance scope\n * @param callback - The function to execute only once\n * @returns A wrapped function that tracks its execution state\n * @internal\n */\nfunction once(scope: Runner<any>, callback: (scope: Runner<any>) => Promise<void> | void) {\n function next(): Promise<void> | void {\n if (next.called) {\n return\n }\n\n next.called = true\n return callback(scope)\n }\n next.called = false\n\n return next\n}\n\n/**\n * Default final handler that resolves immediately when the middleware chain completes.\n *\n * This handler is used when no custom final handler is specified via `finalHandler()`.\n *\n * @internal\n */\nconst DEFAULT_FINAL_HANDLER = () => Promise.resolve()\n\n/**\n * Runner executes an array of middleware functions in sequence.\n *\n * The middleware pipeline advances only when the current function calls `next`,\n * implementing the chain of responsibility pattern. You control how to execute\n * the underlying middleware functions by providing an executor function.\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = { stack: [] }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add((ctx, next) => {\n * ctx.stack.push('fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * context.stack.push('final handler')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Runner<MiddlewareFn extends any> {\n /**\n * The array of middleware handlers to execute in sequence.\n */\n #middleware: MiddlewareFn[]\n\n /**\n * The current index position in the middleware pipeline.\n */\n #currentIndex = 0\n\n /**\n * The executor function responsible for invoking each middleware handler.\n */\n #executor!: Executor<MiddlewareFn>\n\n /**\n * The final handler to execute when the middleware chain completes successfully.\n */\n #finalHandler: FinalHandler = DEFAULT_FINAL_HANDLER\n\n /**\n * Optional error handler to catch and handle exceptions in the middleware pipeline.\n */\n #errorHandler?: ErrorHandler\n\n constructor(middleware: MiddlewareFn[]) {\n this.#middleware = middleware\n }\n\n /**\n * Invokes middleware handlers one at a time.\n *\n * Middleware functions are executed recursively until `next` is called.\n * If a middleware doesn't call `next`, the chain will finish automatically\n * without executing remaining handlers.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invoke(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler()\n }\n\n return self.#executor(middleware, once(self, self.#invoke))\n }\n\n /**\n * Invokes middleware handlers with error handling.\n *\n * Similar to `#invoke`, but catches exceptions and passes them to the error handler.\n * When an exception is raised, the middleware downstream logic will not run unless\n * the error handler allows the chain to continue.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invokeWithErrorManagement(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler().catch(self.#errorHandler)\n }\n\n return self\n .#executor(middleware, once(self, self.#invokeWithErrorManagement))\n .catch(self.#errorHandler)\n }\n\n /**\n * Sets a custom final handler to execute when the middleware chain completes successfully.\n *\n * The final handler is called when the entire middleware pipeline reaches the end\n * by calling `next` through all handlers. This makes it easier to execute custom\n * logic that is not part of the chain but must run when the chain ends.\n *\n * @param finalHandler - The function to execute when the chain completes\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * console.log('All middleware completed')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n finalHandler(finalHandler: FinalHandler): this {\n this.#finalHandler = finalHandler\n return this\n }\n\n /**\n * Sets a custom error handler to catch exceptions in the middleware pipeline.\n *\n * By default, exceptions raised in the middleware pipeline bubble up to the `run`\n * method and can be captured using a try/catch block. When an exception is raised,\n * the middleware downstream logic will not run.\n *\n * Defining an error handler changes this behavior:\n * - The `run` method will not throw exceptions\n * - Errors are caught and passed to the error handler\n * - Middleware upstream logic (after `next`) can still execute\n *\n * @param errorHandler - The function to handle errors\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .errorHandler((error) => {\n * console.error('Middleware error:', error)\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n errorHandler(errorHandler: ErrorHandler): this {\n this.#errorHandler = errorHandler\n return this\n }\n\n /**\n * Executes the middleware pipeline using the provided executor function.\n *\n * The executor function is responsible for invoking each middleware handler with\n * the appropriate context and the `next` callback. Since you control the executor,\n * you can pass any data you want to the middleware.\n *\n * @param cb - The executor function that invokes each middleware handler\n * @returns A Promise that resolves when the middleware pipeline completes\n *\n * @example\n * ```ts\n * const context = { user: null }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add(async (ctx, next) => {\n * ctx.user = await authenticate()\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n async run(cb: Executor<MiddlewareFn>): Promise<void> {\n this.#executor = cb\n\n if (this.#errorHandler) {\n return this.#invokeWithErrorManagement(this)\n }\n\n return this.#invoke(this)\n }\n}\n","import { Runner } from './runner'\n\n/**\n * The Middleware class implements the chain of responsibility design pattern\n * and allows executing handlers in series.\n *\n * The middleware handlers can be represented as any value you wish, such as:\n * - A function: `middleware.add(function () { console.log('called') })`\n * - An object with a `handle` method: `middleware.add({ name: 'auth', handle: authenticate })`\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing middleware')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Middleware<MiddlewareFn extends any> {\n #middleware: Set<MiddlewareFn> = new Set()\n #middlewareArray?: MiddlewareFn[]\n #isFrozen: boolean = false\n\n /**\n * Returns all registered middleware handlers.\n *\n * @returns A Set containing all registered middleware handlers\n */\n all() {\n return this.#middleware\n }\n\n /**\n * Checks if a specific handler has already been registered as middleware.\n *\n * @param handler - The middleware handler to check for\n * @returns `true` if the handler is registered, `false` otherwise\n */\n has(handler: MiddlewareFn): boolean {\n return this.#middleware.has(handler)\n }\n\n /**\n * Registers a new middleware handler to the pipeline.\n *\n * Adding the same middleware handler multiple times will result in a no-op,\n * as handlers are stored in a Set to prevent duplicates.\n *\n * @param handler - The middleware handler to register\n * @returns The Middleware instance for method chaining\n * @throws {Error} If the middleware stack is frozen\n */\n add(handler: MiddlewareFn): this {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot add new middleware')\n }\n\n this.#middleware.add(handler)\n return this\n }\n\n /**\n * Removes a specific middleware handler from the pipeline.\n *\n * @param handler - The middleware handler to remove\n * @returns `true` if the handler was removed, `false` if it was not found\n * @throws {Error} If the middleware stack is frozen\n */\n remove(handler: MiddlewareFn): boolean {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot remove middleware')\n }\n\n return this.#middleware.delete(handler)\n }\n\n /**\n * Removes all registered middleware handlers from the pipeline.\n *\n * @throws {Error} If the middleware stack is frozen\n */\n clear(): void {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot clear middleware')\n }\n\n this.#middleware.clear()\n }\n\n /**\n * Merges middleware handlers from another Middleware instance.\n *\n * The middleware from the source instance are appended to the current instance.\n *\n * @param hooks - The source Middleware instance to merge from\n * @throws {Error} If the middleware stack is frozen\n */\n merge(hooks: Middleware<MiddlewareFn>) {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot merge middleware')\n }\n\n hooks.all().forEach((handler) => {\n this.add(handler)\n })\n }\n\n /**\n * Freezes the middleware stack to prevent further modifications.\n *\n * Once frozen, the middleware array is cached and no new handlers can be added,\n * removed, or modified. This method is automatically called when creating a runner.\n */\n freeze() {\n if (this.#isFrozen) {\n return\n }\n\n this.#isFrozen = true\n this.#middlewareArray = [...this.all()]\n }\n\n /**\n * Creates and returns a Runner instance to execute the middleware pipeline.\n *\n * This method automatically freezes the middleware stack to prevent modifications\n * during execution.\n *\n * @returns A new Runner instance configured with the current middleware handlers\n */\n runner(): Runner<MiddlewareFn> {\n this.freeze()\n return new Runner(this.#middlewareArray!)\n }\n}\n","/**\n * @module @nhtio/middleware\n *\n * A cross-environment (browser/node) implementation of the chain of responsibility design pattern,\n * also known as the middleware pipeline. This package is based on\n * [@poppinss/middleware](https://github.com/poppinss/middleware) and provides a zero-dependency\n * implementation for executing handlers in series.\n *\n * @example\n * ```ts\n * import { Middleware } from '@nhtio/middleware'\n * import type { NextFn } from '@nhtio/middleware'\n *\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n\n/**\n * The current version of the package.\n *\n * This constant is replaced during the build process with the actual version from package.json.\n */\nexport const version = __VERSION__\n\nexport { Middleware } from './private/middleware'\nexport { Runner } from './private/runner'\nexport * from './private/types'\n"],"names":["_middleware"],"mappings":";;;;;;;;;;;;;;;;;;;AAaA,SAAS,KAAK,OAAoB,UAAwD;AAC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EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,QAAc;AACZ,QAAI,mBAAK,YAAW;AAClB,YAAM,IAAI,MAAM,qDAAqD;AAAA,IACvE;AAEA,uBAAKA,cAAY,MAAA;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,OAAiC;AACrC,QAAI,mBAAK,YAAW;AAClB,YAAM,IAAI,MAAM,qDAAqD;AAAA,IACvE;AAEA,UAAM,IAAA,EAAM,QAAQ,CAAC,YAAY;AAC/B,WAAK,IAAI,OAAO;AAAA,IAClB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,SAAS;AACP,QAAI,mBAAK,YAAW;AAClB;AAAA,IACF;AAEA,uBAAK,WAAY;AACjB,uBAAK,kBAAmB,CAAC,GAAG,KAAK,KAAK;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,SAA+B;AAC7B,SAAK,OAAA;AACL,WAAO,IAAI,OAAO,mBAAK,iBAAiB;AAAA,EAC1C;AACF;AAnHEA,eAAA;AACA;AACA;ACEK,MAAM,UAAU;;;;"}
package/index.mjs CHANGED
@@ -1,49 +1,56 @@
1
- var x = (t) => {
2
- throw TypeError(t);
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
3
  };
4
- var k = (t, r, e) => r.has(t) || x("Cannot " + e);
5
- var n = (t, r, e) => (k(t, r, "read from private field"), e ? e.call(t) : r.get(t)), a = (t, r, e) => r.has(t) ? x("Cannot add the same private member more than once") : r instanceof WeakSet ? r.add(t) : r.set(t, e), s = (t, r, e, i) => (k(t, r, "write to private field"), i ? i.call(t, e) : r.set(t, e), e), f = (t, r, e) => (k(t, r, "access private method"), e);
6
- var v = (t, r, e, i) => ({
7
- set _(c) {
8
- s(t, r, c, e);
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
+ var __privateWrapper = (obj, member, setter, getter) => ({
10
+ set _(value) {
11
+ __privateSet(obj, member, value, setter);
9
12
  },
10
13
  get _() {
11
- return n(t, r, i);
14
+ return __privateGet(obj, member, getter);
12
15
  }
13
16
  });
14
- function A(t, r) {
15
- function e() {
16
- if (!e.called)
17
- return e.called = !0, r(t);
17
+ var _middleware, _currentIndex, _executor, _finalHandler, _errorHandler, _Runner_instances, invoke_fn, invokeWithErrorManagement_fn, _middleware2, _middlewareArray, _isFrozen;
18
+ function once(scope, callback) {
19
+ function next() {
20
+ if (next.called) {
21
+ return;
22
+ }
23
+ next.called = true;
24
+ return callback(scope);
18
25
  }
19
- return e.called = !1, e;
26
+ next.called = false;
27
+ return next;
20
28
  }
21
- const C = () => Promise.resolve();
22
- var u, E, w, m, l, h, M, H;
23
- class g {
24
- constructor(r) {
25
- a(this, h);
29
+ const DEFAULT_FINAL_HANDLER = () => Promise.resolve();
30
+ class Runner {
31
+ constructor(middleware) {
32
+ __privateAdd(this, _Runner_instances);
26
33
  /**
27
34
  * The array of middleware handlers to execute in sequence.
28
35
  */
29
- a(this, u);
36
+ __privateAdd(this, _middleware);
30
37
  /**
31
38
  * The current index position in the middleware pipeline.
32
39
  */
33
- a(this, E, 0);
40
+ __privateAdd(this, _currentIndex, 0);
34
41
  /**
35
42
  * The executor function responsible for invoking each middleware handler.
36
43
  */
37
- a(this, w);
44
+ __privateAdd(this, _executor);
38
45
  /**
39
46
  * The final handler to execute when the middleware chain completes successfully.
40
47
  */
41
- a(this, m, C);
48
+ __privateAdd(this, _finalHandler, DEFAULT_FINAL_HANDLER);
42
49
  /**
43
50
  * Optional error handler to catch and handle exceptions in the middleware pipeline.
44
51
  */
45
- a(this, l);
46
- s(this, u, r);
52
+ __privateAdd(this, _errorHandler);
53
+ __privateSet(this, _middleware, middleware);
47
54
  }
48
55
  /**
49
56
  * Sets a custom final handler to execute when the middleware chain completes successfully.
@@ -65,8 +72,9 @@ class g {
65
72
  * .run((fn, next) => fn(context, next))
66
73
  * ```
67
74
  */
68
- finalHandler(r) {
69
- return s(this, m, r), this;
75
+ finalHandler(finalHandler) {
76
+ __privateSet(this, _finalHandler, finalHandler);
77
+ return this;
70
78
  }
71
79
  /**
72
80
  * Sets a custom error handler to catch exceptions in the middleware pipeline.
@@ -93,8 +101,9 @@ class g {
93
101
  * .run((fn, next) => fn(context, next))
94
102
  * ```
95
103
  */
96
- errorHandler(r) {
97
- return s(this, l, r), this;
104
+ errorHandler(errorHandler) {
105
+ __privateSet(this, _errorHandler, errorHandler);
106
+ return this;
98
107
  }
99
108
  /**
100
109
  * Executes the middleware pipeline using the provided executor function.
@@ -122,11 +131,21 @@ class g {
122
131
  * .run((fn, next) => fn(context, next))
123
132
  * ```
124
133
  */
125
- async run(r) {
126
- return s(this, w, r), n(this, l) ? f(this, h, H).call(this, this) : f(this, h, M).call(this, this);
134
+ async run(cb) {
135
+ __privateSet(this, _executor, cb);
136
+ if (__privateGet(this, _errorHandler)) {
137
+ return __privateMethod(this, _Runner_instances, invokeWithErrorManagement_fn).call(this, this);
138
+ }
139
+ return __privateMethod(this, _Runner_instances, invoke_fn).call(this, this);
127
140
  }
128
141
  }
129
- u = new WeakMap(), E = new WeakMap(), w = new WeakMap(), m = new WeakMap(), l = new WeakMap(), h = new WeakSet(), /**
142
+ _middleware = new WeakMap();
143
+ _currentIndex = new WeakMap();
144
+ _executor = new WeakMap();
145
+ _finalHandler = new WeakMap();
146
+ _errorHandler = new WeakMap();
147
+ _Runner_instances = new WeakSet();
148
+ /**
130
149
  * Invokes middleware handlers one at a time.
131
150
  *
132
151
  * Middleware functions are executed recursively until `next` is called.
@@ -137,11 +156,15 @@ u = new WeakMap(), E = new WeakMap(), w = new WeakMap(), m = new WeakMap(), l =
137
156
  * @returns A Promise that resolves when the middleware chain completes
138
157
  * @internal
139
158
  */
140
- M = function(r) {
141
- var i, c;
142
- const e = n(r, u)[v(r, E)._++];
143
- return e ? n(c = r, w).call(c, e, A(r, f(r, h, M))) : n(i = r, m).call(i);
144
- }, /**
159
+ invoke_fn = function(self) {
160
+ var _a, _b;
161
+ const middleware = __privateGet(self, _middleware)[__privateWrapper(self, _currentIndex)._++];
162
+ if (!middleware) {
163
+ return __privateGet(_a = self, _finalHandler).call(_a);
164
+ }
165
+ return __privateGet(_b = self, _executor).call(_b, middleware, once(self, __privateMethod(self, _Runner_instances, invoke_fn)));
166
+ };
167
+ /**
145
168
  * Invokes middleware handlers with error handling.
146
169
  *
147
170
  * Similar to `#invoke`, but catches exceptions and passes them to the error handler.
@@ -152,17 +175,19 @@ M = function(r) {
152
175
  * @returns A Promise that resolves when the middleware chain completes
153
176
  * @internal
154
177
  */
155
- H = function(r) {
156
- var i, c;
157
- const e = n(r, u)[v(r, E)._++];
158
- return e ? n(c = r, w).call(c, e, A(r, f(r, h, H))).catch(n(r, l)) : n(i = r, m).call(i).catch(n(r, l));
178
+ invokeWithErrorManagement_fn = function(self) {
179
+ var _a, _b;
180
+ const middleware = __privateGet(self, _middleware)[__privateWrapper(self, _currentIndex)._++];
181
+ if (!middleware) {
182
+ return __privateGet(_a = self, _finalHandler).call(_a).catch(__privateGet(self, _errorHandler));
183
+ }
184
+ return __privateGet(_b = self, _executor).call(_b, middleware, once(self, __privateMethod(self, _Runner_instances, invokeWithErrorManagement_fn))).catch(__privateGet(self, _errorHandler));
159
185
  };
160
- var o, z, d;
161
- class L {
186
+ class Middleware {
162
187
  constructor() {
163
- a(this, o, /* @__PURE__ */ new Set());
164
- a(this, z);
165
- a(this, d, !1);
188
+ __privateAdd(this, _middleware2, /* @__PURE__ */ new Set());
189
+ __privateAdd(this, _middlewareArray);
190
+ __privateAdd(this, _isFrozen, false);
166
191
  }
167
192
  /**
168
193
  * Returns all registered middleware handlers.
@@ -170,7 +195,7 @@ class L {
170
195
  * @returns A Set containing all registered middleware handlers
171
196
  */
172
197
  all() {
173
- return n(this, o);
198
+ return __privateGet(this, _middleware2);
174
199
  }
175
200
  /**
176
201
  * Checks if a specific handler has already been registered as middleware.
@@ -178,8 +203,8 @@ class L {
178
203
  * @param handler - The middleware handler to check for
179
204
  * @returns `true` if the handler is registered, `false` otherwise
180
205
  */
181
- has(r) {
182
- return n(this, o).has(r);
206
+ has(handler) {
207
+ return __privateGet(this, _middleware2).has(handler);
183
208
  }
184
209
  /**
185
210
  * Registers a new middleware handler to the pipeline.
@@ -191,10 +216,12 @@ class L {
191
216
  * @returns The Middleware instance for method chaining
192
217
  * @throws {Error} If the middleware stack is frozen
193
218
  */
194
- add(r) {
195
- if (n(this, d))
219
+ add(handler) {
220
+ if (__privateGet(this, _isFrozen)) {
196
221
  throw new Error("Middleware stack is frozen. Cannot add new middleware");
197
- return n(this, o).add(r), this;
222
+ }
223
+ __privateGet(this, _middleware2).add(handler);
224
+ return this;
198
225
  }
199
226
  /**
200
227
  * Removes a specific middleware handler from the pipeline.
@@ -203,10 +230,11 @@ class L {
203
230
  * @returns `true` if the handler was removed, `false` if it was not found
204
231
  * @throws {Error} If the middleware stack is frozen
205
232
  */
206
- remove(r) {
207
- if (n(this, d))
233
+ remove(handler) {
234
+ if (__privateGet(this, _isFrozen)) {
208
235
  throw new Error("Middleware stack is frozen. Cannot remove middleware");
209
- return n(this, o).delete(r);
236
+ }
237
+ return __privateGet(this, _middleware2).delete(handler);
210
238
  }
211
239
  /**
212
240
  * Removes all registered middleware handlers from the pipeline.
@@ -214,9 +242,10 @@ class L {
214
242
  * @throws {Error} If the middleware stack is frozen
215
243
  */
216
244
  clear() {
217
- if (n(this, d))
245
+ if (__privateGet(this, _isFrozen)) {
218
246
  throw new Error("Middleware stack is frozen. Cannot clear middleware");
219
- n(this, o).clear();
247
+ }
248
+ __privateGet(this, _middleware2).clear();
220
249
  }
221
250
  /**
222
251
  * Merges middleware handlers from another Middleware instance.
@@ -226,11 +255,12 @@ class L {
226
255
  * @param hooks - The source Middleware instance to merge from
227
256
  * @throws {Error} If the middleware stack is frozen
228
257
  */
229
- merge(r) {
230
- if (n(this, d))
258
+ merge(hooks) {
259
+ if (__privateGet(this, _isFrozen)) {
231
260
  throw new Error("Middleware stack is frozen. Cannot merge middleware");
232
- r.all().forEach((e) => {
233
- this.add(e);
261
+ }
262
+ hooks.all().forEach((handler) => {
263
+ this.add(handler);
234
264
  });
235
265
  }
236
266
  /**
@@ -240,7 +270,11 @@ class L {
240
270
  * removed, or modified. This method is automatically called when creating a runner.
241
271
  */
242
272
  freeze() {
243
- n(this, d) || (s(this, d, !0), s(this, z, [...this.all()]));
273
+ if (__privateGet(this, _isFrozen)) {
274
+ return;
275
+ }
276
+ __privateSet(this, _isFrozen, true);
277
+ __privateSet(this, _middlewareArray, [...this.all()]);
244
278
  }
245
279
  /**
246
280
  * Creates and returns a Runner instance to execute the middleware pipeline.
@@ -251,14 +285,17 @@ class L {
251
285
  * @returns A new Runner instance configured with the current middleware handlers
252
286
  */
253
287
  runner() {
254
- return this.freeze(), new g(n(this, z));
288
+ this.freeze();
289
+ return new Runner(__privateGet(this, _middlewareArray));
255
290
  }
256
291
  }
257
- o = new WeakMap(), z = new WeakMap(), d = new WeakMap();
258
- const y = "1.20251013.0";
292
+ _middleware2 = new WeakMap();
293
+ _middlewareArray = new WeakMap();
294
+ _isFrozen = new WeakMap();
295
+ const version = "1.20251110.0";
259
296
  export {
260
- L as Middleware,
261
- g as Runner,
262
- y as version
297
+ Middleware,
298
+ Runner,
299
+ version
263
300
  };
264
301
  //# sourceMappingURL=index.mjs.map
package/index.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/private/runner.ts","../src/private/middleware.ts","../src/index.ts"],"sourcesContent":["import type { ErrorHandler, Executor, FinalHandler } from './types'\n\n/**\n * Ensures a function is executed only once by maintaining a called state.\n *\n * This utility function is tightly coupled with the Runner class and is used\n * to ensure the `next` callback is not invoked multiple times in the middleware chain.\n *\n * @param scope - The Runner instance scope\n * @param callback - The function to execute only once\n * @returns A wrapped function that tracks its execution state\n * @internal\n */\nfunction once(scope: Runner<any>, callback: (scope: Runner<any>) => Promise<void> | void) {\n function next(): Promise<void> | void {\n if (next.called) {\n return\n }\n\n next.called = true\n return callback(scope)\n }\n next.called = false\n\n return next\n}\n\n/**\n * Default final handler that resolves immediately when the middleware chain completes.\n *\n * This handler is used when no custom final handler is specified via `finalHandler()`.\n *\n * @internal\n */\nconst DEFAULT_FINAL_HANDLER = () => Promise.resolve()\n\n/**\n * Runner executes an array of middleware functions in sequence.\n *\n * The middleware pipeline advances only when the current function calls `next`,\n * implementing the chain of responsibility pattern. You control how to execute\n * the underlying middleware functions by providing an executor function.\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = { stack: [] }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add((ctx, next) => {\n * ctx.stack.push('fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * context.stack.push('final handler')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Runner<MiddlewareFn extends any> {\n /**\n * The array of middleware handlers to execute in sequence.\n */\n #middleware: MiddlewareFn[]\n\n /**\n * The current index position in the middleware pipeline.\n */\n #currentIndex = 0\n\n /**\n * The executor function responsible for invoking each middleware handler.\n */\n #executor!: Executor<MiddlewareFn>\n\n /**\n * The final handler to execute when the middleware chain completes successfully.\n */\n #finalHandler: FinalHandler = DEFAULT_FINAL_HANDLER\n\n /**\n * Optional error handler to catch and handle exceptions in the middleware pipeline.\n */\n #errorHandler?: ErrorHandler\n\n constructor(middleware: MiddlewareFn[]) {\n this.#middleware = middleware\n }\n\n /**\n * Invokes middleware handlers one at a time.\n *\n * Middleware functions are executed recursively until `next` is called.\n * If a middleware doesn't call `next`, the chain will finish automatically\n * without executing remaining handlers.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invoke(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler()\n }\n\n return self.#executor(middleware, once(self, self.#invoke))\n }\n\n /**\n * Invokes middleware handlers with error handling.\n *\n * Similar to `#invoke`, but catches exceptions and passes them to the error handler.\n * When an exception is raised, the middleware downstream logic will not run unless\n * the error handler allows the chain to continue.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invokeWithErrorManagement(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler().catch(self.#errorHandler)\n }\n\n return self\n .#executor(middleware, once(self, self.#invokeWithErrorManagement))\n .catch(self.#errorHandler)\n }\n\n /**\n * Sets a custom final handler to execute when the middleware chain completes successfully.\n *\n * The final handler is called when the entire middleware pipeline reaches the end\n * by calling `next` through all handlers. This makes it easier to execute custom\n * logic that is not part of the chain but must run when the chain ends.\n *\n * @param finalHandler - The function to execute when the chain completes\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * console.log('All middleware completed')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n finalHandler(finalHandler: FinalHandler): this {\n this.#finalHandler = finalHandler\n return this\n }\n\n /**\n * Sets a custom error handler to catch exceptions in the middleware pipeline.\n *\n * By default, exceptions raised in the middleware pipeline bubble up to the `run`\n * method and can be captured using a try/catch block. When an exception is raised,\n * the middleware downstream logic will not run.\n *\n * Defining an error handler changes this behavior:\n * - The `run` method will not throw exceptions\n * - Errors are caught and passed to the error handler\n * - Middleware upstream logic (after `next`) can still execute\n *\n * @param errorHandler - The function to handle errors\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .errorHandler((error) => {\n * console.error('Middleware error:', error)\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n errorHandler(errorHandler: ErrorHandler): this {\n this.#errorHandler = errorHandler\n return this\n }\n\n /**\n * Executes the middleware pipeline using the provided executor function.\n *\n * The executor function is responsible for invoking each middleware handler with\n * the appropriate context and the `next` callback. Since you control the executor,\n * you can pass any data you want to the middleware.\n *\n * @param cb - The executor function that invokes each middleware handler\n * @returns A Promise that resolves when the middleware pipeline completes\n *\n * @example\n * ```ts\n * const context = { user: null }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add(async (ctx, next) => {\n * ctx.user = await authenticate()\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n async run(cb: Executor<MiddlewareFn>): Promise<void> {\n this.#executor = cb\n\n if (this.#errorHandler) {\n return this.#invokeWithErrorManagement(this)\n }\n\n return this.#invoke(this)\n }\n}\n","import { Runner } from './runner'\n\n/**\n * The Middleware class implements the chain of responsibility design pattern\n * and allows executing handlers in series.\n *\n * The middleware handlers can be represented as any value you wish, such as:\n * - A function: `middleware.add(function () { console.log('called') })`\n * - An object with a `handle` method: `middleware.add({ name: 'auth', handle: authenticate })`\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing middleware')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Middleware<MiddlewareFn extends any> {\n #middleware: Set<MiddlewareFn> = new Set()\n #middlewareArray?: MiddlewareFn[]\n #isFrozen: boolean = false\n\n /**\n * Returns all registered middleware handlers.\n *\n * @returns A Set containing all registered middleware handlers\n */\n all() {\n return this.#middleware\n }\n\n /**\n * Checks if a specific handler has already been registered as middleware.\n *\n * @param handler - The middleware handler to check for\n * @returns `true` if the handler is registered, `false` otherwise\n */\n has(handler: MiddlewareFn): boolean {\n return this.#middleware.has(handler)\n }\n\n /**\n * Registers a new middleware handler to the pipeline.\n *\n * Adding the same middleware handler multiple times will result in a no-op,\n * as handlers are stored in a Set to prevent duplicates.\n *\n * @param handler - The middleware handler to register\n * @returns The Middleware instance for method chaining\n * @throws {Error} If the middleware stack is frozen\n */\n add(handler: MiddlewareFn): this {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot add new middleware')\n }\n\n this.#middleware.add(handler)\n return this\n }\n\n /**\n * Removes a specific middleware handler from the pipeline.\n *\n * @param handler - The middleware handler to remove\n * @returns `true` if the handler was removed, `false` if it was not found\n * @throws {Error} If the middleware stack is frozen\n */\n remove(handler: MiddlewareFn): boolean {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot remove middleware')\n }\n\n return this.#middleware.delete(handler)\n }\n\n /**\n * Removes all registered middleware handlers from the pipeline.\n *\n * @throws {Error} If the middleware stack is frozen\n */\n clear(): void {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot clear middleware')\n }\n\n this.#middleware.clear()\n }\n\n /**\n * Merges middleware handlers from another Middleware instance.\n *\n * The middleware from the source instance are appended to the current instance.\n *\n * @param hooks - The source Middleware instance to merge from\n * @throws {Error} If the middleware stack is frozen\n */\n merge(hooks: Middleware<MiddlewareFn>) {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot merge middleware')\n }\n\n hooks.all().forEach((handler) => {\n this.add(handler)\n })\n }\n\n /**\n * Freezes the middleware stack to prevent further modifications.\n *\n * Once frozen, the middleware array is cached and no new handlers can be added,\n * removed, or modified. This method is automatically called when creating a runner.\n */\n freeze() {\n if (this.#isFrozen) {\n return\n }\n\n this.#isFrozen = true\n this.#middlewareArray = [...this.all()]\n }\n\n /**\n * Creates and returns a Runner instance to execute the middleware pipeline.\n *\n * This method automatically freezes the middleware stack to prevent modifications\n * during execution.\n *\n * @returns A new Runner instance configured with the current middleware handlers\n */\n runner(): Runner<MiddlewareFn> {\n this.freeze()\n return new Runner(this.#middlewareArray!)\n }\n}\n","/**\n * @module @nhtio/middleware\n *\n * A cross-environment (browser/node) implementation of the chain of responsibility design pattern,\n * also known as the middleware pipeline. This package is based on\n * [@poppinss/middleware](https://github.com/poppinss/middleware) and provides a zero-dependency\n * implementation for executing handlers in series.\n *\n * @example\n * ```ts\n * import { Middleware } from '@nhtio/middleware'\n * import type { NextFn } from '@nhtio/middleware'\n *\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n\n/**\n * The current version of the package.\n *\n * This constant is replaced during the build process with the actual version from package.json.\n */\nexport const version = __VERSION__\n\nexport { Middleware } from './private/middleware'\nexport { Runner } from './private/runner'\nexport * from './private/types'\n"],"names":["once","scope","callback","next","DEFAULT_FINAL_HANDLER","_middleware","_currentIndex","_executor","_finalHandler","_errorHandler","_Runner_instances","invoke_fn","invokeWithErrorManagement_fn","Runner","middleware","__privateAdd","__privateSet","finalHandler","errorHandler","cb","__privateGet","__privateMethod","self","_a","_b","__privateWrapper","_middlewareArray","_isFrozen","Middleware","handler","hooks","version"],"mappings":";;;;;;;;;;;;;AAaA,SAASA,EAAKC,GAAoBC,GAAwD;AACxF,WAASC,IAA6B;AACpC,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AAA,eACAqB,IAAA,eACAC,IAAA;ACEK,MAAMI,IAAU;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/private/runner.ts","../src/private/middleware.ts","../src/index.ts"],"sourcesContent":["import type { ErrorHandler, Executor, FinalHandler } from './types'\n\n/**\n * Ensures a function is executed only once by maintaining a called state.\n *\n * This utility function is tightly coupled with the Runner class and is used\n * to ensure the `next` callback is not invoked multiple times in the middleware chain.\n *\n * @param scope - The Runner instance scope\n * @param callback - The function to execute only once\n * @returns A wrapped function that tracks its execution state\n * @internal\n */\nfunction once(scope: Runner<any>, callback: (scope: Runner<any>) => Promise<void> | void) {\n function next(): Promise<void> | void {\n if (next.called) {\n return\n }\n\n next.called = true\n return callback(scope)\n }\n next.called = false\n\n return next\n}\n\n/**\n * Default final handler that resolves immediately when the middleware chain completes.\n *\n * This handler is used when no custom final handler is specified via `finalHandler()`.\n *\n * @internal\n */\nconst DEFAULT_FINAL_HANDLER = () => Promise.resolve()\n\n/**\n * Runner executes an array of middleware functions in sequence.\n *\n * The middleware pipeline advances only when the current function calls `next`,\n * implementing the chain of responsibility pattern. You control how to execute\n * the underlying middleware functions by providing an executor function.\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = { stack: [] }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add((ctx, next) => {\n * ctx.stack.push('fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * context.stack.push('final handler')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Runner<MiddlewareFn extends any> {\n /**\n * The array of middleware handlers to execute in sequence.\n */\n #middleware: MiddlewareFn[]\n\n /**\n * The current index position in the middleware pipeline.\n */\n #currentIndex = 0\n\n /**\n * The executor function responsible for invoking each middleware handler.\n */\n #executor!: Executor<MiddlewareFn>\n\n /**\n * The final handler to execute when the middleware chain completes successfully.\n */\n #finalHandler: FinalHandler = DEFAULT_FINAL_HANDLER\n\n /**\n * Optional error handler to catch and handle exceptions in the middleware pipeline.\n */\n #errorHandler?: ErrorHandler\n\n constructor(middleware: MiddlewareFn[]) {\n this.#middleware = middleware\n }\n\n /**\n * Invokes middleware handlers one at a time.\n *\n * Middleware functions are executed recursively until `next` is called.\n * If a middleware doesn't call `next`, the chain will finish automatically\n * without executing remaining handlers.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invoke(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler()\n }\n\n return self.#executor(middleware, once(self, self.#invoke))\n }\n\n /**\n * Invokes middleware handlers with error handling.\n *\n * Similar to `#invoke`, but catches exceptions and passes them to the error handler.\n * When an exception is raised, the middleware downstream logic will not run unless\n * the error handler allows the chain to continue.\n *\n * @param self - The Runner instance scope\n * @returns A Promise that resolves when the middleware chain completes\n * @internal\n */\n #invokeWithErrorManagement(self: Runner<MiddlewareFn>): Promise<void> | void {\n const middleware = self.#middleware[self.#currentIndex++]\n\n /**\n * Empty stack\n */\n if (!middleware) {\n return self.#finalHandler().catch(self.#errorHandler)\n }\n\n return self\n .#executor(middleware, once(self, self.#invokeWithErrorManagement))\n .catch(self.#errorHandler)\n }\n\n /**\n * Sets a custom final handler to execute when the middleware chain completes successfully.\n *\n * The final handler is called when the entire middleware pipeline reaches the end\n * by calling `next` through all handlers. This makes it easier to execute custom\n * logic that is not part of the chain but must run when the chain ends.\n *\n * @param finalHandler - The function to execute when the chain completes\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .finalHandler(() => {\n * console.log('All middleware completed')\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n finalHandler(finalHandler: FinalHandler): this {\n this.#finalHandler = finalHandler\n return this\n }\n\n /**\n * Sets a custom error handler to catch exceptions in the middleware pipeline.\n *\n * By default, exceptions raised in the middleware pipeline bubble up to the `run`\n * method and can be captured using a try/catch block. When an exception is raised,\n * the middleware downstream logic will not run.\n *\n * Defining an error handler changes this behavior:\n * - The `run` method will not throw exceptions\n * - Errors are caught and passed to the error handler\n * - Middleware upstream logic (after `next`) can still execute\n *\n * @param errorHandler - The function to handle errors\n * @returns The Runner instance for method chaining\n *\n * @example\n * ```ts\n * await middleware\n * .runner()\n * .errorHandler((error) => {\n * console.error('Middleware error:', error)\n * })\n * .run((fn, next) => fn(context, next))\n * ```\n */\n errorHandler(errorHandler: ErrorHandler): this {\n this.#errorHandler = errorHandler\n return this\n }\n\n /**\n * Executes the middleware pipeline using the provided executor function.\n *\n * The executor function is responsible for invoking each middleware handler with\n * the appropriate context and the `next` callback. Since you control the executor,\n * you can pass any data you want to the middleware.\n *\n * @param cb - The executor function that invokes each middleware handler\n * @returns A Promise that resolves when the middleware pipeline completes\n *\n * @example\n * ```ts\n * const context = { user: null }\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n * middleware.add(async (ctx, next) => {\n * ctx.user = await authenticate()\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n async run(cb: Executor<MiddlewareFn>): Promise<void> {\n this.#executor = cb\n\n if (this.#errorHandler) {\n return this.#invokeWithErrorManagement(this)\n }\n\n return this.#invoke(this)\n }\n}\n","import { Runner } from './runner'\n\n/**\n * The Middleware class implements the chain of responsibility design pattern\n * and allows executing handlers in series.\n *\n * The middleware handlers can be represented as any value you wish, such as:\n * - A function: `middleware.add(function () { console.log('called') })`\n * - An object with a `handle` method: `middleware.add({ name: 'auth', handle: authenticate })`\n *\n * @template MiddlewareFn - The type of the middleware function/handler\n *\n * @example\n * ```ts\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing middleware')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\nexport class Middleware<MiddlewareFn extends any> {\n #middleware: Set<MiddlewareFn> = new Set()\n #middlewareArray?: MiddlewareFn[]\n #isFrozen: boolean = false\n\n /**\n * Returns all registered middleware handlers.\n *\n * @returns A Set containing all registered middleware handlers\n */\n all() {\n return this.#middleware\n }\n\n /**\n * Checks if a specific handler has already been registered as middleware.\n *\n * @param handler - The middleware handler to check for\n * @returns `true` if the handler is registered, `false` otherwise\n */\n has(handler: MiddlewareFn): boolean {\n return this.#middleware.has(handler)\n }\n\n /**\n * Registers a new middleware handler to the pipeline.\n *\n * Adding the same middleware handler multiple times will result in a no-op,\n * as handlers are stored in a Set to prevent duplicates.\n *\n * @param handler - The middleware handler to register\n * @returns The Middleware instance for method chaining\n * @throws {Error} If the middleware stack is frozen\n */\n add(handler: MiddlewareFn): this {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot add new middleware')\n }\n\n this.#middleware.add(handler)\n return this\n }\n\n /**\n * Removes a specific middleware handler from the pipeline.\n *\n * @param handler - The middleware handler to remove\n * @returns `true` if the handler was removed, `false` if it was not found\n * @throws {Error} If the middleware stack is frozen\n */\n remove(handler: MiddlewareFn): boolean {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot remove middleware')\n }\n\n return this.#middleware.delete(handler)\n }\n\n /**\n * Removes all registered middleware handlers from the pipeline.\n *\n * @throws {Error} If the middleware stack is frozen\n */\n clear(): void {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot clear middleware')\n }\n\n this.#middleware.clear()\n }\n\n /**\n * Merges middleware handlers from another Middleware instance.\n *\n * The middleware from the source instance are appended to the current instance.\n *\n * @param hooks - The source Middleware instance to merge from\n * @throws {Error} If the middleware stack is frozen\n */\n merge(hooks: Middleware<MiddlewareFn>) {\n if (this.#isFrozen) {\n throw new Error('Middleware stack is frozen. Cannot merge middleware')\n }\n\n hooks.all().forEach((handler) => {\n this.add(handler)\n })\n }\n\n /**\n * Freezes the middleware stack to prevent further modifications.\n *\n * Once frozen, the middleware array is cached and no new handlers can be added,\n * removed, or modified. This method is automatically called when creating a runner.\n */\n freeze() {\n if (this.#isFrozen) {\n return\n }\n\n this.#isFrozen = true\n this.#middlewareArray = [...this.all()]\n }\n\n /**\n * Creates and returns a Runner instance to execute the middleware pipeline.\n *\n * This method automatically freezes the middleware stack to prevent modifications\n * during execution.\n *\n * @returns A new Runner instance configured with the current middleware handlers\n */\n runner(): Runner<MiddlewareFn> {\n this.freeze()\n return new Runner(this.#middlewareArray!)\n }\n}\n","/**\n * @module @nhtio/middleware\n *\n * A cross-environment (browser/node) implementation of the chain of responsibility design pattern,\n * also known as the middleware pipeline. This package is based on\n * [@poppinss/middleware](https://github.com/poppinss/middleware) and provides a zero-dependency\n * implementation for executing handlers in series.\n *\n * @example\n * ```ts\n * import { Middleware } from '@nhtio/middleware'\n * import type { NextFn } from '@nhtio/middleware'\n *\n * const context = {}\n * type MiddlewareFn = (ctx: typeof context, next: NextFn) => void | Promise<void>\n *\n * const middleware = new Middleware<MiddlewareFn>()\n *\n * middleware.add((ctx, next) => {\n * console.log('executing fn1')\n * await next()\n * })\n *\n * await middleware\n * .runner()\n * .run((fn, next) => fn(context, next))\n * ```\n */\n\n/**\n * The current version of the package.\n *\n * This constant is replaced during the build process with the actual version from package.json.\n */\nexport const version = __VERSION__\n\nexport { Middleware } from './private/middleware'\nexport { Runner } from './private/runner'\nexport * from './private/types'\n"],"names":["_middleware"],"mappings":";;;;;;;;;;;;;;;;AAaA,qIAAAA,cAAA;AAAA,SAAS,KAAK,OAAoB,UAAwD;AACxF,WAAS,OAA6B;AACpC,QAAI,KAAK,QAAQ;AACf;AAAA,IACF;AAEA,SAAK,SAAS;AACd,WAAO,SAAS,KAAK;AAAA,EACvB;AACA,OAAK,SAAS;AAEd,SAAO;AACT;AASA,MAAM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AAY,MAAA;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,OAAiC;AACrC,QAAI,mBAAK,YAAW;AAClB,YAAM,IAAI,MAAM,qDAAqD;AAAA,IACvE;AAEA,UAAM,IAAA,EAAM,QAAQ,CAAC,YAAY;AAC/B,WAAK,IAAI,OAAO;AAAA,IAClB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,SAAS;AACP,QAAI,mBAAK,YAAW;AAClB;AAAA,IACF;AAEA,uBAAK,WAAY;AACjB,uBAAK,kBAAmB,CAAC,GAAG,KAAK,KAAK;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,SAA+B;AAC7B,SAAK,OAAA;AACL,WAAO,IAAI,OAAO,mBAAK,iBAAiB;AAAA,EAC1C;AACF;AAnHEA,eAAA;AACA;AACA;ACEK,MAAM,UAAU;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nhtio/middleware",
3
- "version": "1.20251013.0",
3
+ "version": "1.20251110.0",
4
4
  "description": "A cross-environment (browser/node) implementation of the chain of responsibility design pattern based on @poppinss/middleware",
5
5
  "keywords": [],
6
6
  "author": "Jak Giveon <jak@nht.io>",