Skip to content

Commit a092108

Browse files
authored
feat: support cjs and esm both (#8)
BREAKING CHANGE: drop Node.js < 18.19.0 support eggjs/egg#5257
1 parent a2cb6a3 commit a092108

File tree

9 files changed

+128
-69
lines changed

9 files changed

+128
-69
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "eslint-config-egg"
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
36
}

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ jobs:
1212
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1313
with:
1414
os: 'ubuntu-latest'
15-
version: '14, 16, 18, 20, 22'
15+
version: '18.19.0, 18, 20, 22'
1616
secrets:
1717
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ dump.rdb
1616
.DS_Store
1717

1818
test.js
19+
.tshy*
20+
dist

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ npm install egg-path-matching
2121

2222
## Usage
2323

24-
```js
25-
const pathMatching = require('egg-path-matching');
24+
```ts
25+
import { pathMatching } from 'egg-path-matching';
26+
2627
const options = {
2728
ignore: '/api', // string will use parsed by path-to-regexp
2829
// support regexp
@@ -36,17 +37,18 @@ const options = {
3637
};
3738

3839
const match = pathMatching(options);
39-
assert(match({ path: '/api' }) === true);
40-
assert(match({ path: '/api/hello' }) === true);
41-
assert(match({ path: '/api' }) === true);
40+
assert.equal(match({ path: '/api' }), true);
41+
assert.equal(match({ path: '/api/hello' }), true);
42+
assert.equal(match({ path: '/api' }), true);
4243
```
4344

4445
### options
4546

46-
- `match` {String | RegExp | Function | Array} - if request path hit `options.match`, will return true, otherwise will return false.
47-
- `ignore` {String | RegExp | Function | Array} - if request path hit `options.ignore`, will return false, otherwise will return true.
47+
- `match` {String | RegExp | Function | Array} - if request path hit `options.match`, will return `true`, otherwise will return `false`.
48+
- `ignore` {String | RegExp | Function | Array} - if request path hit `options.ignore`, will return `false`, otherwise will return `true`.
4849

49-
`ignore` and `match` can not both be presented. and if neither `ignore` nor `match` presented, the new function will always return true.
50+
`ignore` and `match` can not both be presented.
51+
and if neither `ignore` nor `match` presented, the new function will always return `true`.
5052

5153
### License
5254

index.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

package.json

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
"name": "egg-path-matching",
33
"version": "1.1.0",
44
"engine": {
5-
"node": ">= 4.0.0"
5+
"node": ">= 18.19.0"
66
},
77
"description": "match or ignore url path",
8-
"main": "index.js",
9-
"files": [
10-
"index.js"
11-
],
128
"scripts": {
13-
"test": "egg-bin test",
14-
"cov": "egg-bin cov",
15-
"lint": "eslint *.js test",
16-
"ci": "npm run lint && npm run cov",
17-
"contributor": "git-contributor"
9+
"lint": "eslint src test",
10+
"test": "npm run lint -- --fix && npm run test-local",
11+
"test-local": "egg-bin test",
12+
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
13+
"contributor": "git-contributor",
14+
"prepublishOnly": "tshy && tshy-after"
1815
},
1916
"keywords": [
2017
"url",
@@ -32,12 +29,46 @@
3229
},
3330
"license": "MIT",
3431
"dependencies": {
35-
"path-to-regexp": "^1.7.0"
32+
"path-to-regexp": "^6.2.2"
3633
},
3734
"devDependencies": {
38-
"egg-bin": "^6.5.2",
39-
"eslint": "^8.55.0",
40-
"eslint-config-egg": "12",
41-
"git-contributor": "^2.1.5"
42-
}
35+
"@eggjs/tsconfig": "1",
36+
"@types/mocha": "10",
37+
"@types/node": "20",
38+
"egg-bin": "6",
39+
"eslint": "8",
40+
"eslint-config-egg": "13",
41+
"git-contributor": "2",
42+
"tshy": "1",
43+
"tshy-after": "1",
44+
"typescript": "5"
45+
},
46+
"files": [
47+
"dist",
48+
"src"
49+
],
50+
"type": "module",
51+
"tshy": {
52+
"exports": {
53+
"./package.json": "./package.json",
54+
".": "./src/index.ts"
55+
}
56+
},
57+
"exports": {
58+
"./package.json": "./package.json",
59+
".": {
60+
"import": {
61+
"source": "./src/index.ts",
62+
"types": "./dist/esm/index.d.ts",
63+
"default": "./dist/esm/index.js"
64+
},
65+
"require": {
66+
"source": "./src/index.ts",
67+
"types": "./dist/commonjs/index.d.ts",
68+
"default": "./dist/commonjs/index.js"
69+
}
70+
}
71+
},
72+
"main": "./dist/commonjs/index.js",
73+
"types": "./dist/commonjs/index.d.ts"
4374
}

src/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { pathToRegexp } from 'path-to-regexp';
2+
3+
export type PathMatchingFun = (ctx: any) => boolean;
4+
5+
export type PathMatchingPattern = string | RegExp | PathMatchingFun | (string | RegExp | PathMatchingFun)[];
6+
7+
export interface PathMatchingOptions {
8+
ignore?: PathMatchingPattern;
9+
match?: PathMatchingPattern;
10+
}
11+
12+
export function pathMatching(options: PathMatchingOptions): PathMatchingFun {
13+
options = options || {};
14+
if (options.match && options.ignore) {
15+
throw new Error('options.match and options.ignore can not both present');
16+
}
17+
if (!options.match && !options.ignore) {
18+
return () => true;
19+
}
20+
21+
const matchFn = options.match ? toPathMatch(options.match) : toPathMatch(options.ignore!);
22+
23+
return function pathMatch(ctx: any) {
24+
const matched = matchFn(ctx);
25+
return options.match ? matched : !matched;
26+
};
27+
}
28+
29+
function toPathMatch(pattern: PathMatchingPattern): PathMatchingFun {
30+
if (typeof pattern === 'string') {
31+
const reg = pathToRegexp(pattern, [], { end: false });
32+
if (reg.global) reg.lastIndex = 0;
33+
return ctx => reg.test(ctx.path);
34+
}
35+
if (pattern instanceof RegExp) {
36+
return ctx => {
37+
if (pattern.global) {
38+
pattern.lastIndex = 0;
39+
}
40+
return pattern.test(ctx.path);
41+
};
42+
}
43+
if (typeof pattern === 'function') return pattern;
44+
if (Array.isArray(pattern)) {
45+
const matchFns = pattern.map(item => toPathMatch(item));
46+
return ctx => matchFns.some(matchFn => matchFn(ctx));
47+
}
48+
throw new Error(`match/ignore pattern must be RegExp, Array or String, but got ${pattern}`);
49+
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
'use strict';
2-
3-
const assert = require('assert');
4-
const match = require('..');
1+
import { strict as assert } from 'node:assert';
2+
import { pathMatching as match } from '../src/index.js';
53

64
describe('egg-path-matching', () => {
75
it('options.match and options.ignore both present should throw', () => {
86
try {
97
match({ ignore: '/api', match: '/dashboard' });
108
throw new Error('should not exec');
11-
} catch (e) {
12-
assert(e.message === 'options.match and options.ignore can not both present');
9+
} catch (e: any) {
10+
assert.equal(e.message, 'options.match and options.ignore can not both present');
1311
}
1412
});
1513

1614
it('options.match and options.ignore both not present should always return true', () => {
1715
const fn = match({});
18-
assert(fn() === true);
16+
assert(fn({}) === true);
1917
});
2018

2119
describe('match', () => {

tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@eggjs/tsconfig",
3+
"compilerOptions": {
4+
"strict": true,
5+
"noImplicitAny": true,
6+
"target": "ES2022",
7+
"module": "NodeNext",
8+
"moduleResolution": "NodeNext"
9+
}
10+
}

0 commit comments

Comments
 (0)