diff --git a/.eslintrc b/.eslintrc index a47f2aa..159fd59 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,9 @@ ---- -extends: standard -env: - node: true +{ + "extends": "standard", + "env": { + "node": true + }, + "globals": { + "URLPattern": "readonly" + } +} diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index ca7d551..7bb93ac 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - node-version: [18.x, 20.x, 22.x] + node-version: [24.x] env: CODECOV_TOKEN: 849dde91-e5e3-438d-a75e-07745ed7948a diff --git a/README.md b/README.md index 4bb6b4e..c36d2ea 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - # Koa Path Match [![NPM version][npm-image]][npm-url] @@ -7,13 +6,16 @@ [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] -A simple routing wrapper around [path-match](https://github.com/expressjs/path-match). +A simple routing wrapper around Node.js 24's native [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern). Similar to [koa-route](https://github.com/koajs/route), except it optionally handles methods better. -All of these routers use [path-to-regexp](https://github.com/component/path-to-regexp) -underneath, which is what Express uses as well. +This package uses the native URLPattern API which provides similar functionality to Express's routing. + +> NOTE: for older versions, [`path-to-regexp`](https://www.npmjs.com/package/path-to-regexp) was used. In v5, regexp capture groups were removed, but are added back in v6 with the migration to URLPattern. + +> NOTE: koa-path-match@6 only supports node v24+. Please use an older version of koa-path-match for older versions of node. ```js -const route = require('koa-path-match')({/* options passed to path-to-regexp */}) +const route = require('koa-path-match')({/* options passed to URLPattern */}) app.use(route('/:id', (ctx, next) => { const id = ctx.params.id @@ -39,6 +41,10 @@ app.use(route('/:id') ) ``` +## Requirements + +- Node.js 24.0.0 or higher + ## Maintainer - Lead: @jonathanong [@jongleberry](https://twitter.com/jongleberry) diff --git a/lib/index.js b/lib/index.js index 68f0ee3..5d44a11 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,10 @@ -const compile = require('path-to-regexp').match const flatten = require('lodash/flattenDeep') const METHODS = require('http').METHODS const unique = require('lodash/uniq') -module.exports = function (options) { - options = options || {} - +module.exports = function () { return function (path, fn) { - const match = compile(path, options) + const pattern = new URLPattern({ pathname: path }) // app.use(route(:path, function () {})) if (Array.isArray(fn) || typeof fn === 'function') { @@ -15,26 +12,26 @@ module.exports = function (options) { if (Array.isArray(fn)) fn = compose(flatten(fn)) return function (ctx, next) { - const result = match(ctx.request.path) + const result = pattern.exec({ pathname: ctx.request.path }) if (!result) return next() - ctx.params = result.params + ctx.params = result.pathname.groups return fn(ctx, next) } } // app.use(route(:path).get(function () {}).post(function () {})) - return newRoute(match) + return newRoute(pattern) } } -function newRoute (match) { +function newRoute (pattern) { const route = function (ctx, next) { const dispatcher = route.dispatcher || (route.dispatcher = createDispatcher(route)) return dispatcher(ctx, next) } - route.match = match + route.pattern = pattern route.methods = Object.create(null) METHODS.forEach((method) => { @@ -52,16 +49,16 @@ function newRoute (match) { } function createDispatcher (route) { - const match = route.match + const pattern = route.pattern const controllers = route.methods if (!controllers.HEAD && controllers.GET) controllers.HEAD = controllers.GET const ALLOW = unique(['OPTIONS'].concat(Object.keys(route.methods))).join(',') return function (ctx, next) { - const result = match(ctx.request.path) + const result = pattern.exec({ pathname: ctx.request.path }) if (!result) return next() - ctx.params = result.params + ctx.params = result.pathname.groups ctx.allow = ALLOW const method = ctx.method diff --git a/package-lock.json b/package-lock.json index 208349c..49fdc77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,7 @@ "version": "5.0.0", "license": "MIT", "dependencies": { - "lodash": "^4.17.21", - "path-to-regexp": "^8.2.0" + "lodash": "^4.17.21" }, "devDependencies": { "codecov": "^3.8.3", @@ -23,6 +22,9 @@ "jest": "^29.7.0", "koa": "^2.13.1", "supertest": "^7.0.0" + }, + "engines": { + "node": ">=24.0.0" } }, "node_modules/@ampproject/remapping": { @@ -5788,15 +5790,6 @@ "dev": true, "license": "MIT" }, - "node_modules/path-to-regexp": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz", - "integrity": "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==", - "license": "MIT", - "engines": { - "node": ">=16" - } - }, "node_modules/picocolors": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", diff --git a/package.json b/package.json index 94398f7..c15d0ed 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,11 @@ "author": "Jonathan Ong (http://jongleberry.com)", "license": "MIT", "repository": "koajs/path-match", + "engines": { + "node": ">=24.0.0" + }, "dependencies": { - "lodash": "^4.17.21", - "path-to-regexp": "^8.2.0" + "lodash": "^4.17.21" }, "devDependencies": { "codecov": "^3.8.3", @@ -27,8 +29,7 @@ }, "keywords": [ "koa", - "route", - "router" + "route" ], "files": [ "lib"