From d737a20a5806ce899595e09c9b8f8b42e36fe443 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 11 Jan 2025 09:32:00 +0800 Subject: [PATCH] feat: remove is-type-of and debug deps (#17) force path-to-regexp to >=1.9.0 ## Summary by CodeRabbit ## Release Notes - **New Features** - Added `resources` method to router for enhanced RESTful routing capabilities - Introduced new utility functions for function type checking - **Dependency Updates** - Updated `path-to-regexp` dependency - Removed `debug` and `is-type-of` dependencies - **Performance Improvements** - Replaced external debugging library with Node.js native `util.debuglog` - Streamlined type checking with native JavaScript methods - **Maintenance** - Refactored type checking across multiple files - Updated GitHub Actions workflows for 2.x branch --- .github/workflows/nodejs.yml | 2 +- .github/workflows/release.yml | 2 +- lib/egg_router.js | 10 +++++----- lib/layer.js | 2 +- lib/router.js | 2 +- lib/utils.js | 24 ++++++++++++++++++++---- package.json | 4 +--- test/lib/egg_router.test.js | 14 +++++++------- test/lib/utils.test.js | 17 ++++++++--------- 9 files changed, 45 insertions(+), 32 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index e0eaa23..236e349 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -1,4 +1,4 @@ -name: CI +name: CI for 2.x on: push: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5871bf7..de9df75 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: Release +name: Release for 2.x on: push: diff --git a/lib/egg_router.js b/lib/egg_router.js index fef7c9f..f9eaf40 100644 --- a/lib/egg_router.js +++ b/lib/egg_router.js @@ -1,11 +1,10 @@ 'use strict'; -const is = require('is-type-of'); -const Router = require('./router'); const utility = require('utility'); const inflection = require('inflection'); const assert = require('assert'); const utils = require('./utils'); +const Router = require('./router'); const METHODS = [ 'head', 'options', 'get', 'put', 'patch', 'post', 'delete' ]; @@ -138,6 +137,7 @@ class EggRouter extends Router { * @return {Router} return route object. * @since 1.0.0 */ + resources(...args) { const splited = spliteAndResolveRouterParams({ args, app: this.app }); const middlewares = splited.middlewares; @@ -198,7 +198,7 @@ class EggRouter extends Router { const args = params; let url = route.path; - assert(!is.regExp(url), `Can't get the url for regExp ${url} for by name '${name}'`); + assert(!(url instanceof RegExp), `Can't get the url for regExp ${url} for by name '${name}'`); const queries = []; if (typeof args === 'object' && args !== null) { @@ -263,7 +263,7 @@ class EggRouter extends Router { function spliteAndResolveRouterParams({ args, app }) { let prefix; let middlewares; - if (args.length >= 3 && (is.string(args[1]) || is.regExp(args[1]))) { + if (args.length >= 3 && (typeof args[1] === 'string' || args[1] instanceof RegExp)) { // app.get(name, url, [...middleware], controller) prefix = args.slice(0, 2); middlewares = args.slice(2); @@ -285,7 +285,7 @@ function spliteAndResolveRouterParams({ args, app }) { * @return {Function} controller function */ function resolveController(controller, app) { - if (is.string(controller)) { + if (typeof controller === 'string') { const actions = controller.split('.'); let obj = app.controller; actions.forEach(key => { diff --git a/lib/layer.js b/lib/layer.js index 90cf992..4fe675b 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -1,6 +1,6 @@ 'use strict'; -const debug = require('debug')('egg-router:layer'); +const debug = require('util').debuglog('egg-router:layer'); const pathToRegExp = require('path-to-regexp'); const uri = require('urijs'); const utility = require('utility'); diff --git a/lib/router.js b/lib/router.js index e101a8a..2597ae4 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4,7 +4,7 @@ * RESTful resource routing middleware for eggjs. */ -const debug = require('debug')('egg-router'); +const debug = require('util').debuglog('egg-router'); const compose = require('koa-compose'); const HttpError = require('http-errors'); const methods = require('methods'); diff --git a/lib/utils.js b/lib/utils.js index f28c546..0dd5049 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,18 +1,34 @@ 'use strict'; const convert = require('koa-convert'); -const is = require('is-type-of'); const co = require('co'); +function isGeneratorFunction(obj) { + return obj + && obj.constructor + && obj.constructor.name === 'GeneratorFunction'; +} + module.exports = { async callFn(fn, args, ctx) { args = args || []; - if (!is.function(fn)) return; - if (is.generatorFunction(fn)) fn = co.wrap(fn); + if (typeof fn !== 'function') return; + if (isGeneratorFunction(fn)) fn = co.wrap(fn); return ctx ? fn.call(ctx, ...args) : fn(...args); }, middleware(fn) { - return is.generatorFunction(fn) ? convert(fn) : fn; + return isGeneratorFunction(fn) ? convert(fn) : fn; + }, + + isGeneratorFunction, + isAsyncFunction(obj) { + return obj + && obj.constructor + && obj.constructor.name === 'AsyncFunction'; + }, + isPromise(obj) { + return obj + && typeof obj.then === 'function'; }, }; diff --git a/package.json b/package.json index aa183e9..646e62d 100644 --- a/package.json +++ b/package.json @@ -29,14 +29,12 @@ ], "dependencies": { "co": "^4.6.0", - "debug": "^3.1.0", "http-errors": "^1.3.1", "inflection": "^1.12.0", - "is-type-of": "^1.2.1", "koa-compose": "^3.0.0", "koa-convert": "^1.2.0", "methods": "^1.0.1", - "path-to-regexp": "^1.1.1", + "path-to-regexp": "^1.9.0", "urijs": "^1.19.0", "utility": "^1.15.0" }, diff --git a/test/lib/egg_router.test.js b/test/lib/egg_router.test.js index 381691a..b2e1e5a 100644 --- a/test/lib/egg_router.test.js +++ b/test/lib/egg_router.test.js @@ -2,7 +2,7 @@ const EggRouter = require('../../').EggRouter; const assert = require('assert'); -const is = require('is-type-of'); +const { isGeneratorFunction, isAsyncFunction } = require('../../lib/utils'); describe('test/lib/egg_router.test.js', () => { it('creates new router with egg app', function() { @@ -126,16 +126,16 @@ describe('test/lib/egg_router.test.js', () => { assert(router.stack[0].path === '/foo'); assert.deepEqual(router.stack[0].methods, [ 'HEAD', 'GET' ]); assert(router.stack[0].stack.length === 4); - assert(!is.generatorFunction(router.stack[0].stack[0])); - assert(is.asyncFunction(router.stack[0].stack[1])); - assert(!is.generatorFunction(router.stack[0].stack[3])); + assert(!isGeneratorFunction(router.stack[0].stack[0])); + assert(isAsyncFunction(router.stack[0].stack[1])); + assert(!isGeneratorFunction(router.stack[0].stack[3])); assert(router.stack[1].name === 'hello'); assert(router.stack[1].path === '/hello/world'); assert.deepEqual(router.stack[1].methods, [ 'POST' ]); assert(router.stack[1].stack.length === 4); - assert(!is.generatorFunction(router.stack[1].stack[0])); - assert(is.asyncFunction(router.stack[1].stack[1])); - assert(!is.generatorFunction(router.stack[1].stack[3])); + assert(!isGeneratorFunction(router.stack[1].stack[0])); + assert(isAsyncFunction(router.stack[1].stack[1])); + assert(!isGeneratorFunction(router.stack[1].stack[3])); }); it('should app.resource() work', () => { diff --git a/test/lib/utils.test.js b/test/lib/utils.test.js index a6c6f4c..170eb8f 100644 --- a/test/lib/utils.test.js +++ b/test/lib/utils.test.js @@ -1,14 +1,13 @@ 'use strict'; -const utils = require('../../lib/utils'); -const is = require('is-type-of'); const assert = require('assert'); +const utils = require('../../lib/utils'); describe('test/lib/utils.test.js', () => { describe('callFn', () => { it('should not function return same', () => { const res = utils.callFn('foo'); - assert(is.promise(res)); + assert(utils.isPromise(res)); return res.then(result => assert(result === undefined)); }); @@ -16,7 +15,7 @@ describe('test/lib/utils.test.js', () => { const res = utils.callFn(async (foo, bar) => { return foo + bar; }, [ 1, 2 ]); - assert(is.promise(res)); + assert(utils.isPromise(res)); return res.then(result => assert(result === 3)); }); @@ -24,7 +23,7 @@ describe('test/lib/utils.test.js', () => { const res = utils.callFn(function* (foo, bar) { return foo + bar; }, [ 1, 2 ]); - assert(is.promise(res)); + assert(utils.isPromise(res)); return res.then(result => assert(result === 3)); }); @@ -32,7 +31,7 @@ describe('test/lib/utils.test.js', () => { const res = utils.callFn((foo, bar) => { return foo + bar; }, [ 1, 2 ]); - assert(is.promise(res)); + assert(utils.isPromise(res)); return res.then(result => assert(result === 3)); }); @@ -40,7 +39,7 @@ describe('test/lib/utils.test.js', () => { const res = utils.callFn(async function(bar) { return this.foo + bar; }, [ 2 ], { foo: 1 }); - assert(is.promise(res)); + assert(utils.isPromise(res)); return res.then(result => assert(result === 3)); }); }); @@ -48,14 +47,14 @@ describe('test/lib/utils.test.js', () => { describe('middleware', () => { it('should work with async function', () => { const res = utils.middleware(async () => {}); - assert(is.asyncFunction(res)); + assert(utils.isAsyncFunction(res)); }); it('should work with generator function', () => { const res = utils.middleware(function* () { return; }); - assert(!is.generatorFunction(res)); + assert(!utils.isGeneratorFunction(res)); }); }); });