Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x | /** * Represents a function that can be called to proceed to the next middleware in the chain. * Should return a Promise that resolves when the next middleware has completed. * * @returns A Promise that resolves when the next middleware completes */ export type Next = () => Promise<void>; /** * A no-op implementation of the Next function that immediately resolves. * Used as a default when no next function is provided. * * @returns A Promise that resolves immediately */ export function NextNoop() { return Promise.resolve(); } /** * Represents a handler function that processes a context and optionally calls next. * Can be either a two-parameter function (context and next) or a one-parameter function (context only). * One-parameter functions automatically call next after completion. * * @template Ctx - The type of the context object * @param ctx - The context object to process * @param next - Optional function to call the next middleware * @returns A Promise that resolves when processing is complete */ export type Handler<Ctx> = (ctx: Ctx, next?:Next) => Promise<void>; /** * Represents metadata that can be attached to middleware for debugging, logging, or other purposes. * Can contain any key-value pairs. */ export type Meta = Record<string, any>; /** * Represents a hierarchical structure of middleware metadata. * Used for debugging and visualization of middleware chains. */ export type MetaHierarchy = { meta?: Meta, children?: MetaHierarchy[] } /** * Represents a middleware object that can process contexts and call the next middleware. * * @template Ctx - The type of the context object */ export type Middleware<Ctx> = { /** Optional metadata associated with this middleware */ meta?: Meta; /** Optional array of child middleware */ middleware?: Middleware<Ctx>[]; /** * Processes the context and calls the next middleware * @param ctx - The context to process * @param next - Function to call the next middleware * @returns A Promise that resolves when processing is complete */ process(ctx: Ctx, next: Next): Promise<void>; } /** * Union type representing either a Handler function or a Middleware object. * * @template Ctx - The type of the context object */ export type Processor<Ctx> = Handler<Ctx> | Middleware<Ctx> /** * Interface defining the contract for Rowan middleware containers. * Extends Middleware to provide additional functionality for managing middleware chains. * * @template Ctx - The type of the context object */ export interface IRowan<Ctx> extends Middleware<Ctx> { /** * Adds a processor to the middleware chain * @param h - The processor to add * @param meta - Optional metadata to associate with the processor * @returns This instance for method chaining */ use(h: Processor<Ctx>, meta?: Meta): IRowan<Ctx>; /** Read-only array of middleware in this container */ readonly middleware: Middleware<Ctx>[]; } /** * Main class for creating and managing middleware chains. * Provides functionality to add middleware, process contexts, and manage metadata. * * @template Ctx - The type of the context object that will be processed * * @example * ```typescript * const app = new Rowan(); * app.use(async (ctx) => { * console.log('Processing:', ctx); * }); * await app.process({ data: 'example' }); * ``` */ export class Rowan<Ctx = any> implements IRowan<Ctx>{ /** Array of middleware processors in this container */ middleware: Middleware<Ctx>[]; /** * Creates a new Rowan middleware container. * * @param middleware - Initial array of processors to add to the container * @param meta - Metadata to associate with this container * * @example * ```typescript * const rowan = new Rowan([ * async (ctx) => console.log('First'), * async (ctx) => console.log('Second') * ], { name: 'MyApp' }); * ``` */ constructor(middleware: Processor<Ctx>[] = [], public meta: Meta = {}) { this.middleware = middleware.map(function(x) { return Rowan.convertToMiddleware(x); }); } /** * Adds a processor to the middleware chain. * * @param input - The processor (handler function or middleware object) to add * @param meta - Optional metadata to associate with the processor * @returns This instance for method chaining * * @example * ```typescript * rowan * .use(async (ctx) => console.log('First')) * .use(async (ctx, next) => { * console.log('Before'); * await next(); * console.log('After'); * }); * ``` */ use(input: Processor<Ctx>, meta?: any): this { this.middleware.push(Rowan.convertToMiddleware(input, meta)); return this; } /** * Processes a context through the middleware chain. * * @param ctx - The context object to process * @param next - Optional function to call after all middleware has been processed * @returns A Promise that resolves when all middleware has completed * * @example * ```typescript * await rowan.process({ userId: 123, data: 'example' }); * ``` */ process(ctx: Ctx, next: Next = NextNoop): Promise<void> { return Rowan.process(this.middleware, ctx, next); } /** * Static method to process a middleware array with a context. * Sets up the middleware chain and executes it in reverse order. * * @param middleware - Array of middleware to process * @param ctx - The context object to process * @param next - Optional function to call after all middleware has been processed * @returns A Promise that resolves when all middleware has completed * * @example * ```typescript * const middleware = [ * { process: async (ctx, next) => { console.log('1'); await next(); } }, * { process: async (ctx, next) => { console.log('2'); await next(); } } * ]; * await Rowan.process(middleware, { data: 'test' }); * ``` */ static process<Ctx>(middleware: Middleware<Ctx>[], ctx: Ctx, next: Next = NextNoop): Promise<void> { for (let index = middleware.length - 1; index >= 0; index -= 1) { const item = middleware[index]; next = item.process.bind(item, ctx, next); } return next(); } /** * Converts a processor (handler function or middleware object) into a standardized middleware object. * Handles different types of input and normalizes them to the Middleware interface. * * @param input - The processor to convert * @param meta - Optional metadata to associate with the middleware * @returns A standardized middleware object * * @example * ```typescript * // Convert a handler function * const middleware = Rowan.convertToMiddleware(async (ctx) => console.log(ctx)); * * // Convert with metadata * const middleware2 = Rowan.convertToMiddleware( * async (ctx, next) => { await next(); }, * { name: 'Logger' } * ); * ``` */ static convertToMiddleware<Ctx>(input: Processor<Ctx>, meta?: Meta) { if (isMiddleware(input)) { input.meta = input.meta || meta; return input; } else { return { meta: (input as any)["meta"] || meta, process: isAutoHandler(input) ? function (ctx, next) { return input(ctx, undefined).then(function(_) { return next(); }); } : input } as Middleware<Ctx>; } } /** * Returns the metadata hierarchy of middleware for debugging and visualization. * Recursively traverses the middleware tree to build a hierarchical structure. * * @param input - The middleware to analyze * @returns A hierarchical representation of the middleware metadata * * @example * ```typescript * const hierarchy = Rowan.hierarchy(rowan); * console.log(JSON.stringify(hierarchy, null, 2)); * // { * // "meta": { "name": "MyApp" }, * // "children": [ * // { "meta": { "name": "Logger" } }, * // { "meta": { "name": "Auth" } } * // ] * // } * ``` */ static hierarchy<Ctx>(input: Middleware<Ctx>): MetaHierarchy { return { meta: input.meta, children: input.middleware ? input.middleware.map(function(item) { return Rowan.hierarchy(item); }) : undefined } } } /** * Type guard to determine if an object is a middleware object. * Checks if the object has a 'process' method and is not null. * * @param obj - The object to check * @returns True if the object is a middleware, false otherwise * * @example * ```typescript * const middleware = { process: async (ctx, next) => next() }; * const handler = async (ctx) => {}; * * console.log(isMiddleware(middleware)); // true * console.log(isMiddleware(handler)); // false * ``` */ export function isMiddleware(obj: any): obj is Middleware<any> { return typeof (obj) === "object" && obj !== null && typeof (obj["process"]) === "function"; } /** * Type guard to determine if a function is an auto-handler. * Auto-handlers are functions with 0 or 1 parameters that automatically call next() after completion. * Functions with 2 or more parameters are considered manual handlers that must call next() explicitly. * * @param obj - The object to check * @returns True if the object is an auto-handler function, false otherwise * * @example * ```typescript * const autoHandler = async (ctx) => console.log(ctx); * const manualHandler = async (ctx, next) => { await next(); }; * * console.log(isAutoHandler(autoHandler)); // true * console.log(isAutoHandler(manualHandler)); // false * ``` */ export function isAutoHandler(obj: any): boolean { return typeof (obj) === "function" && obj.length <= 1; } |