This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+22
View File
@@ -0,0 +1,22 @@
/**
* An array of arguments + the next function
*/
export declare type MiddlewareArgs = any[];
/**
* The middleware actual function
*/
export declare type MiddlewareFn = (...params: MiddlewareArgs) => Promise<void>;
/**
* Executor job is to execute one middleware function at a
* time and pass arguments to it
*/
export declare type Executor = (fn: any, params: MiddlewareArgs) => Promise<void>;
/**
* Args received by the final handler
*/
export declare type FinalHandlerArgs = any[];
/**
* Final handler is called when the entire chain executes
* completely
*/
export declare type FinalHandler = (...params: FinalHandlerArgs) => Promise<void>;
+10
View File
@@ -0,0 +1,10 @@
"use strict";
/*
* co-compose
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
+18
View File
@@ -0,0 +1,18 @@
import { Runnable } from './Runnable';
/**
* Exposes the API to register middleware and later execute
* them using the runner.
*/
export declare class Middleware {
private list;
/**
* Register an array of middleware to executed
* later.
*/
register(list: any[]): this;
/**
* Returns an instance of runner to execute
* the middleware
*/
runner(): Runnable;
}
+40
View File
@@ -0,0 +1,40 @@
"use strict";
/*
* co-componse
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Middleware = void 0;
const Runnable_1 = require("./Runnable");
/**
* Exposes the API to register middleware and later execute
* them using the runner.
*/
class Middleware {
constructor() {
this.list = [];
}
/**
* Register an array of middleware to executed
* later.
*/
register(list) {
if (!Array.isArray(list)) {
throw new Error('middleware.register expects an array of middleware');
}
this.list = this.list.concat(list);
return this;
}
/**
* Returns an instance of runner to execute
* the middleware
*/
runner() {
return new Runnable_1.Runnable(this.list);
}
}
exports.Middleware = Middleware;
+43
View File
@@ -0,0 +1,43 @@
import { Executor, FinalHandler, FinalHandlerArgs } from './Contracts';
/**
* Runnable to execute an array of functions in sequence. The queue is
* advanced only when the current function calls `next`.
*
* @example
* ```js
* const runner = new Runnable([async function fn1 (params, next) {
* }])
* ```
*/
export declare class Runnable {
private list;
private index;
private params;
private executorFn;
private registeredFinalHandler;
constructor(list: any[]);
/**
* Invoke one middleware at a time. Middleware fns will be executed
* recursively until `next` is invoked.
*
* If one method doesn't call `next`, then the chain will be finished
* automatically.
*/
private invoke;
/**
* Final handler to be executed, when chain ends successfully
*/
finalHandler(fn: FinalHandler, args: FinalHandlerArgs): this;
/**
* Define custom resolver, which is invoked for all the middleware.
* If this method is defined, then default executor is not called
* and it's the responsibility of this method to call the
* middleware and pass params to it
*/
executor(fn: Executor): this;
/**
* Start the middleware queue and pass params to it. The `params`
* array will be passed as spread arguments.
*/
run(params: any[]): Promise<void>;
}
+85
View File
@@ -0,0 +1,85 @@
"use strict";
/*
* co-compose
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Runnable = void 0;
/**
* Default final handler resolves the middleware chain right away
*/
const DEFAULT_FINAL_HANDLER = {
fn: () => Promise.resolve(),
args: [],
};
/**
* The default executor to execute middlewares. This method assumes middleware
* as functions and calls them right away
*/
const DEFAULT_EXECUTOR = (fn, params) => fn(...params);
/**
* Runnable to execute an array of functions in sequence. The queue is
* advanced only when the current function calls `next`.
*
* @example
* ```js
* const runner = new Runnable([async function fn1 (params, next) {
* }])
* ```
*/
class Runnable {
constructor(list) {
this.list = list;
this.index = 0;
this.params = [];
this.executorFn = DEFAULT_EXECUTOR;
this.registeredFinalHandler = DEFAULT_FINAL_HANDLER;
}
/**
* Invoke one middleware at a time. Middleware fns will be executed
* recursively until `next` is invoked.
*
* If one method doesn't call `next`, then the chain will be finished
* automatically.
*/
invoke() {
const fn = this.list[this.index++];
/**
* Empty stack
*/
if (!fn) {
return this.registeredFinalHandler.fn(...this.registeredFinalHandler.args);
}
return this.executorFn(fn, this.params);
}
/**
* Final handler to be executed, when chain ends successfully
*/
finalHandler(fn, args) {
this.registeredFinalHandler = { fn, args };
return this;
}
/**
* Define custom resolver, which is invoked for all the middleware.
* If this method is defined, then default executor is not called
* and it's the responsibility of this method to call the
* middleware and pass params to it
*/
executor(fn) {
this.executorFn = fn;
return this;
}
/**
* Start the middleware queue and pass params to it. The `params`
* array will be passed as spread arguments.
*/
async run(params) {
this.params = params.concat(this.invoke.bind(this));
return this.invoke();
}
}
exports.Runnable = Runnable;