fix: use non deprecated custom inspect (#1198)

Custom inspection with the `inspect` property is deprecated and will
not work in Node.js 11 anymore. This fixes it by using the custom
inspect symbol where existent and falls back to the old style in case
it does not exist.
This commit is contained in:
Ruben Bridgewater 2018-06-25 04:34:15 +02:00 committed by Yiyu He
parent 77a4cfb829
commit 8f047ddb84
8 changed files with 66 additions and 11 deletions

View file

@ -46,6 +46,9 @@ module.exports = class Application extends Emitter {
this.context = Object.create(context); this.context = Object.create(context);
this.request = Object.create(request); this.request = Object.create(request);
this.response = Object.create(response); this.response = Object.create(response);
if (util.inspect.custom) {
this[util.inspect.custom] = this.inspect;
}
} }
/** /**

View file

@ -153,6 +153,16 @@ const proto = module.exports = {
} }
}; };
/**
* Custom inspection implementation for newer Node.js versions.
*
* @return {Object}
* @api public
*/
if (util.inspect.custom) {
module.exports[util.inspect.custom] = module.exports.inspect;
}
/** /**
* Response delegation. * Response delegation.
*/ */

View file

@ -14,6 +14,7 @@ const qs = require('querystring');
const typeis = require('type-is'); const typeis = require('type-is');
const fresh = require('fresh'); const fresh = require('fresh');
const only = require('only'); const only = require('only');
const util = require('util');
/** /**
* Prototype. * Prototype.
@ -664,3 +665,13 @@ module.exports = {
]); ]);
} }
}; };
/**
* Custom inspection implementation for newer Node.js versions.
*
* @return {Object}
* @api public
*/
if (util.inspect.custom) {
module.exports[util.inspect.custom] = module.exports.inspect;
}

View file

@ -18,6 +18,7 @@ const assert = require('assert');
const extname = require('path').extname; const extname = require('path').extname;
const vary = require('vary'); const vary = require('vary');
const only = require('only'); const only = require('only');
const util = require('util');
/** /**
* Prototype. * Prototype.
@ -545,3 +546,13 @@ module.exports = {
this.res.flushHeaders(); this.res.flushHeaders();
} }
}; };
/**
* Custom inspection implementation for newer Node.js versions.
*
* @return {Object}
* @api public
*/
if (util.inspect.custom) {
module.exports[util.inspect.custom] = module.exports.inspect;
}

View file

@ -2,13 +2,20 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('assert');
const util = require('util');
const Koa = require('../..'); const Koa = require('../..');
const app = new Koa();
describe('app.inspect()', () => { describe('app.inspect()', () => {
it('should work', () => { it('should work', () => {
const app = new Koa();
const util = require('util');
const str = util.inspect(app); const str = util.inspect(app);
assert.equal("{ subdomainOffset: 2, proxy: false, env: 'test' }", str); assert.equal("{ subdomainOffset: 2, proxy: false, env: 'test' }", str);
}); });
it('should return a json representation', () => {
assert.deepEqual(
{ subdomainOffset: 2, proxy: false, env: 'test' },
app.inspect()
);
});
}); });

View file

@ -3,6 +3,7 @@
const prototype = require('../../lib/context'); const prototype = require('../../lib/context');
const assert = require('assert'); const assert = require('assert');
const util = require('util');
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.inspect()', () => { describe('ctx.inspect()', () => {
@ -11,10 +12,12 @@ describe('ctx.inspect()', () => {
const toJSON = ctx.toJSON(ctx); const toJSON = ctx.toJSON(ctx);
assert.deepEqual(toJSON, ctx.inspect()); assert.deepEqual(toJSON, ctx.inspect());
assert.deepEqual(util.inspect(toJSON), util.inspect(ctx));
}); });
// console.log(require.cache) will call prototype.inspect() // console.log(require.cache) will call prototype.inspect()
it('should not crash when called on the prototype', () => { it('should not crash when called on the prototype', () => {
assert.deepEqual(prototype, prototype.inspect()); assert.deepEqual(prototype, prototype.inspect());
assert.deepEqual(util.inspect(prototype.inspect()), util.inspect(prototype));
}); });
}); });

View file

@ -3,6 +3,7 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
const assert = require('assert'); const assert = require('assert');
const util = require('util');
describe('req.inspect()', () => { describe('req.inspect()', () => {
describe('with no request.req present', () => { describe('with no request.req present', () => {
@ -10,7 +11,8 @@ describe('req.inspect()', () => {
const req = request(); const req = request();
req.method = 'GET'; req.method = 'GET';
delete req.req; delete req.req;
assert(null == req.inspect()); assert(undefined === req.inspect());
assert('undefined' === util.inspect(req));
}); });
}); });
@ -20,12 +22,15 @@ describe('req.inspect()', () => {
req.url = 'example.com'; req.url = 'example.com';
req.header.host = 'example.com'; req.header.host = 'example.com';
assert.deepEqual({ const expected = {
method: 'GET', method: 'GET',
url: 'example.com', url: 'example.com',
header: { header: {
host: 'example.com' host: 'example.com'
} }
}, req.inspect()); };
assert.deepEqual(req.inspect(), expected);
assert.deepEqual(util.inspect(req), util.inspect(expected));
}); });
}); });

View file

@ -3,6 +3,7 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
const assert = require('assert'); const assert = require('assert');
const util = require('util');
describe('res.inspect()', () => { describe('res.inspect()', () => {
describe('with no response.res present', () => { describe('with no response.res present', () => {
@ -11,6 +12,7 @@ describe('res.inspect()', () => {
res.body = 'hello'; res.body = 'hello';
delete res.res; delete res.res;
assert.equal(res.inspect(), null); assert.equal(res.inspect(), null);
assert.equal(util.inspect(res), 'undefined');
}); });
}); });
@ -18,14 +20,17 @@ describe('res.inspect()', () => {
const res = response(); const res = response();
res.body = 'hello'; res.body = 'hello';
assert.deepEqual({ const expected = {
body: 'hello',
status: 200, status: 200,
message: 'OK', message: 'OK',
header: { header: {
'content-length': '5', 'content-type': 'text/plain; charset=utf-8',
'content-type': 'text/plain; charset=utf-8' 'content-length': '5'
} },
}, res.inspect()); body: 'hello'
};
assert.deepEqual(res.inspect(), expected);
assert.deepEqual(util.inspect(res), util.inspect(expected));
}); });
}); });