Skip to content

Commit 2f6e814

Browse files
authored
feat: replace debug module with pure node:util::debuglog (#1885)
## Checklist - [x] I have ensured my pull request is not behind the main or master branch of the original repository. - [x] I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first. - [x] I have written a commit message that passes commitlint linting. - [x] I have ensured that my code changes pass linting tests. - [x] I have ensured that my code changes pass unit tests. - [x] I have described my pull request and the reasons for code changes along with context if necessary.
1 parent 8620ced commit 2f6e814

File tree

4 files changed

+60
-61
lines changed

4 files changed

+60
-61
lines changed

docs/guide.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ app.use(async function (ctx, next) {
213213

214214
## Debugging Koa
215215

216-
Koa along with many of the libraries it's built with support the __DEBUG__ environment variable from [debug](https://github.com/visionmedia/debug) which provides simple conditional logging.
216+
Koa, along with many of the libraries it's built, can be debugged using Node.js's built-in util.debuglog, which provides simple conditional logging via the __NODE_DEBUG__ environment variable.
217217

218218
For example
219-
to see all Koa-specific debugging information just pass `DEBUG=koa*` and upon boot you'll see the list of middleware used, among other things.
219+
to see all Koa-specific debugging information just pass `NODE_DEBUG=koa*` and upon boot you'll see the list of middleware used, among other things.
220220

221221
```
222-
$ DEBUG=koa* node --harmony examples/simple
222+
$ NODE_DEBUG=koa* node --harmony examples/simple
223223
koa:application use responseTime +0ms
224224
koa:application use logger +4ms
225225
koa:application use contentLength +0ms
@@ -234,7 +234,7 @@ $ DEBUG=koa* node --harmony examples/simple
234234
For example:
235235

236236
```js
237-
const path = require('path');
237+
const path = require('node:path');
238238
const serve = require('koa-static');
239239

240240
const publicFiles = serve(path.join(__dirname, 'public'));
@@ -249,6 +249,8 @@ app.use(publicFiles);
249249
koa:application use static /public +0ms
250250
```
251251

252+
This lets you use Koa’s internal debug logs without any third-party dependencies by relying on node:util's built-in debuglog.
253+
252254
## HTTP2
253255

254256
Example of setting up an HTTP2 server with Koa using the HTTP compatibility layer:

lib/application.js

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
/**
44
* Module dependencies.
55
*/
6+
const util = require('node:util')
7+
const debug = util.debuglog('koa:application')
8+
const Emitter = require('node:events')
9+
const Stream = require('node:stream')
10+
const http = require('node:http')
11+
const { AsyncLocalStorage } = require('node:async_hooks')
612

7-
const debug = require('debug')('koa:application')
813
const onFinished = require('on-finished')
9-
const response = require('./response')
1014
const compose = require('koa-compose')
11-
const context = require('./context')
12-
const request = require('./request')
1315
const statuses = require('statuses')
14-
const Emitter = require('events')
15-
const util = require('util')
16-
const Stream = require('stream')
17-
const http = require('http')
16+
const { HttpError } = require('http-errors')
17+
18+
const request = require('./request')
19+
const response = require('./response')
20+
const context = require('./context')
1821
const isStream = require('./is-stream.js')
1922
const only = require('./only.js')
20-
const { HttpError } = require('http-errors')
21-
const { AsyncLocalStorage } = require('async_hooks')
2223

2324
/** @typedef {typeof import ('./context') & {
2425
* app: Application
@@ -47,18 +48,18 @@ module.exports = class Application extends Emitter {
4748
*/
4849

4950
/**
50-
*
51-
* @param {object} [options] Application options
52-
* @param {string} [options.env='development'] Environment
53-
* @param {string[]} [options.keys] Signed cookie keys
54-
* @param {boolean} [options.proxy] Trust proxy headers
55-
* @param {number} [options.subdomainOffset] Subdomain offset
56-
* @param {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For
57-
* @param {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity)
58-
* @param {function} [options.compose] Function to handle middleware composition
59-
* @param {boolean} [options.asyncLocalStorage] Enable AsyncLocalStorage, default to false
60-
*
61-
*/
51+
*
52+
* @param {object} [options] Application options
53+
* @param {string} [options.env='development'] Environment
54+
* @param {string[]} [options.keys] Signed cookie keys
55+
* @param {boolean} [options.proxy] Trust proxy headers
56+
* @param {number} [options.subdomainOffset] Subdomain offset
57+
* @param {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For
58+
* @param {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity)
59+
* @param {function} [options.compose] Function to handle middleware composition
60+
* @param {boolean} [options.asyncLocalStorage] Enable AsyncLocalStorage, default to false
61+
*
62+
*/
6263

6364
constructor (options) {
6465
super()
@@ -113,11 +114,7 @@ module.exports = class Application extends Emitter {
113114
*/
114115

115116
toJSON () {
116-
return only(this, [
117-
'subdomainOffset',
118-
'proxy',
119-
'env'
120-
])
117+
return only(this, ['subdomainOffset', 'proxy', 'env'])
121118
}
122119

123120
/**
@@ -142,7 +139,7 @@ module.exports = class Application extends Emitter {
142139
*/
143140

144141
use (fn) {
145-
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!')
142+
if (typeof fn !== 'function') { throw new TypeError('middleware must be a function!') }
146143
debug('use %s', fn._name || fn.name || '-')
147144
this.middleware.push(fn)
148145
return this
@@ -190,7 +187,7 @@ module.exports = class Application extends Emitter {
190187
handleRequest (ctx, fnMiddleware) {
191188
const res = ctx.res
192189
res.statusCode = 404
193-
const onerror = err => ctx.onerror(err)
190+
const onerror = (err) => ctx.onerror(err)
194191
const handleResponse = () => respond(ctx)
195192
onFinished(res, onerror)
196193
return fnMiddleware(ctx).then(handleResponse).catch(onerror)
@@ -206,9 +203,9 @@ module.exports = class Application extends Emitter {
206203
/** @type {Context} */
207204
const context = Object.create(this.context)
208205
/** @type {KoaRequest} */
209-
const request = context.request = Object.create(this.request)
206+
const request = (context.request = Object.create(this.request))
210207
/** @type {KoaResponse} */
211-
const response = context.response = Object.create(this.response)
208+
const response = (context.response = Object.create(this.response))
212209
context.app = request.app = response.app = this
213210
context.req = request.req = response.req = req
214211
context.res = request.res = response.res = res
@@ -234,7 +231,7 @@ module.exports = class Application extends Emitter {
234231
const isNativeError =
235232
Object.prototype.toString.call(err) === '[object Error]' ||
236233
err instanceof Error
237-
if (!isNativeError) throw new TypeError(util.format('non-error thrown: %j', err))
234+
if (!isNativeError) { throw new TypeError(util.format('non-error thrown: %j', err)) }
238235

239236
if (err.status === 404 || err.expose) return
240237
if (this.silent) return
@@ -307,9 +304,9 @@ function respond (ctx) {
307304

308305
if (Buffer.isBuffer(body)) return res.end(body)
309306
if (typeof body === 'string') return res.end(body)
310-
if (body instanceof Blob) return Stream.Readable.from(body.stream()).pipe(res)
311-
if (body instanceof ReadableStream) return Stream.Readable.from(body).pipe(res)
312-
if (body instanceof Response) return Stream.Readable.from(body?.body || '').pipe(res)
307+
if (body instanceof Blob) { return Stream.Readable.from(body.stream()).pipe(res) }
308+
if (body instanceof ReadableStream) { return Stream.Readable.from(body).pipe(res) }
309+
if (body instanceof Response) { return Stream.Readable.from(body?.body || '').pipe(res) }
313310
if (isStream(body)) return body.pipe(res)
314311

315312
// body: json

package-lock.json

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,31 @@
3535
],
3636
"license": "MIT",
3737
"dependencies": {
38-
"accepts": "^1.3.5",
38+
"accepts": "^1.3.8",
3939
"cache-content-type": "^1.0.0",
40-
"content-disposition": "~0.5.2",
41-
"content-type": "^1.0.4",
40+
"content-disposition": "~0.5.4",
41+
"content-type": "^1.0.5",
4242
"cookies": "~0.9.1",
43-
"debug": "^4.3.2",
4443
"delegates": "^1.0.0",
45-
"destroy": "^1.0.4",
44+
"destroy": "^1.2.0",
4645
"encodeurl": "^2.0.0",
4746
"escape-html": "^1.0.3",
4847
"fresh": "~0.5.2",
49-
"http-assert": "^1.3.0",
48+
"http-assert": "^1.5.0",
5049
"http-errors": "^2.0.0",
5150
"koa-compose": "^4.1.0",
52-
"on-finished": "^2.3.0",
53-
"parseurl": "^1.3.2",
51+
"on-finished": "^2.4.1",
52+
"parseurl": "^1.3.3",
5453
"statuses": "^2.0.1",
5554
"type-is": "^2.0.1",
5655
"vary": "^1.1.2"
5756
},
5857
"devDependencies": {
5958
"c8": "^10.1.3",
60-
"gen-esm-wrapper": "^1.0.6",
59+
"gen-esm-wrapper": "^1.1.3",
6160
"snazzy": "^9.0.0",
62-
"standard": "^17.1.0",
63-
"supertest": "^7.0.0"
61+
"standard": "^17.1.2",
62+
"supertest": "^7.1.1"
6463
},
6564
"engines": {
6665
"node": ">= 18"

0 commit comments

Comments
 (0)