Skip to content

Commit

Permalink
refactor: add debug calls
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 15, 2023
1 parent 029372e commit 7fa89b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"format": "prettier --write .",
"lint": "eslint . --ext=.ts",
"sync-labels": "github-label-sync --labels .github/labels.json poppinss/middleware",
"quick:test": "node --loader=ts-node/esm bin/test.ts"
"quick:test": "cross-env NODE_DEBUG=poppinss:middleware node --loader=ts-node/esm bin/test.ts"
},
"keywords": [
"koa",
Expand All @@ -52,6 +52,7 @@
"@types/node": "^20.10.4",
"benchmark": "^2.1.4",
"c8": "^8.0.1",
"cross-env": "^7.0.3",
"del-cli": "^5.1.0",
"eslint": "^8.55.0",
"fastseries": "^2.0.0",
Expand Down
7 changes: 7 additions & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
* file that was distributed with this source code.
*/

import { debuglog } from 'node:util'
import type { ErrorHandler, Executor, FinalHandler } from './types.js'

const debug = debuglog('poppinss:middleware')

/**
* Run a function only once. Tightly coupled with the Runner class
*/
Expand All @@ -19,6 +22,7 @@ function once(scope: Runner<any>, callback: (scope: Runner<any>) => Promise<void
}

next.called = true
debug('next invoked')
return callback(scope)
}
next.called = false
Expand Down Expand Up @@ -79,6 +83,7 @@ export class Runner<MiddlewareFn extends any> {
*/
#invoke(self: Runner<MiddlewareFn>): Promise<void> | void {
const middleware = self.#middleware[self.#currentIndex++]
debug('running middleware at index', self.#currentIndex)

/**
* Empty stack
Expand All @@ -95,6 +100,7 @@ export class Runner<MiddlewareFn extends any> {
*/
#invokeWithErrorManagement(self: Runner<MiddlewareFn>): Promise<void> | void {
const middleware = self.#middleware[self.#currentIndex++]
debug('running middleware at index', self.#currentIndex)

/**
* Empty stack
Expand Down Expand Up @@ -132,6 +138,7 @@ export class Runner<MiddlewareFn extends any> {
*/
async run(cb: Executor<MiddlewareFn>): Promise<void> {
this.#executor = cb
debug('starting middleware chain with %d middleware', this.#middleware.length)

if (this.#errorHandler) {
return this.#invokeWithErrorManagement(this)
Expand Down

0 comments on commit 7fa89b8

Please sign in to comment.