Skip to content

Commit 622e185

Browse files
authored
Merge pull request #3 from Bessonov/switch-to-urlite
switch to urlite
2 parents ca88895 + 5b8a088 commit 622e185

File tree

10 files changed

+59
-46
lines changed

10 files changed

+59
-46
lines changed

.github/workflows/on-commit.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ jobs:
88
runs-on: ubuntu-20.04
99
steps:
1010
- uses: actions/[email protected]
11-
- name: Read .nvmrc
12-
id: nvm
13-
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
1411
- name: Setup node
1512
uses: actions/setup-node@v2
1613
with:
17-
node-version: '${{ steps.nvm.outputs.NVMRC }}'
14+
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
15+
node-version-file: '.nvmrc'
1816
- name: prepare dependencies
1917
run: npm install
2018
- name: test
21-
run: npm run precommit
19+
run: npm run precommit

.github/workflows/publish.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ jobs:
1111
runs-on: ubuntu-20.04
1212
steps:
1313
- uses: actions/[email protected]
14-
- name: Read .nvmrc
15-
id: nvm
16-
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
1714
- name: Setup node
1815
uses: actions/setup-node@v2
1916
with:
20-
node-version: '${{ steps.nvm.outputs.NVMRC }}'
17+
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
18+
node-version-file: '.nvmrc'
2119
- name: prepare dependencies
2220
run: npm install
2321
- name: test

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v12.12.0
1+
v16.14.0

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@bessonovs/node-http-router",
3+
"version": "0.0.7",
34
"description": "Extensible http router for node and micro",
45
"keywords": [
56
"router",
@@ -16,7 +17,6 @@
1617
"http",
1718
"server"
1819
],
19-
"version": "0.0.6",
2020
"author": "Anton Bessonov",
2121
"license": "MIT",
2222
"repository": "bessonov/node-http-router",
@@ -33,26 +33,26 @@
3333
"precommit": "$_ run test && $_ run lint && $_ run build"
3434
},
3535
"dependencies": {
36-
"fast-url-parser": "1.1.3"
36+
"urlite": "3.0.0"
3737
},
3838
"devDependencies": {
3939
"@bessonovs/eslint-config": "0.0.7",
40-
"@types/express": "4.17.12",
41-
"@types/jest": "25.2.2",
42-
"@types/node": "14.0.1",
43-
"@typescript-eslint/eslint-plugin": "4.28.1",
44-
"@typescript-eslint/parser": "4.28.1",
45-
"eslint": "7.29.0",
46-
"eslint-config-airbnb": "18.2.1",
47-
"eslint-plugin-import": "2.23.4",
48-
"eslint-plugin-jsx-a11y": "6.4.1",
49-
"eslint-plugin-react": "7.24.0",
50-
"jest": "27.0.6",
40+
"@types/express": "4.17.13",
41+
"@types/jest": "27.4.1",
42+
"@types/node": "16.11.7",
43+
"@typescript-eslint/eslint-plugin": "5.13.0",
44+
"@typescript-eslint/parser": "5.13.0",
45+
"eslint": "8.10.0",
46+
"eslint-config-airbnb": "19.0.4",
47+
"eslint-plugin-import": "2.25.4",
48+
"eslint-plugin-jsx-a11y": "6.5.1",
49+
"eslint-plugin-react": "7.29.3",
50+
"jest": "27.5.1",
5151
"micro": "9.3.5-canary.3",
52-
"node-mocks-http": "1.10.1",
52+
"node-mocks-http": "1.11.0",
5353
"path-to-regexp": "6.2.0",
54-
"ts-jest": "27.0.3",
55-
"typescript": "4.3.4"
54+
"ts-jest": "27.1.3",
55+
"typescript": "4.6.2"
5656
},
5757
"publishConfig": {
5858
"access": "public"

src/@types/fast-url-parser/index.d.ts

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

src/@types/urlite/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module 'urlite' {
2+
const Url: {
3+
parse: (url: string) => {
4+
pathname: string | undefined
5+
search: string | undefined
6+
}
7+
}
8+
// eslint-disable-next-line import/no-default-export
9+
export default Url
10+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from './matchers'
2-
export { Route, Router, MatchedHandler } from './router'
2+
export type { Route, Router, MatchedHandler } from './router'

src/matchers/ExactQueryMatcher.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IncomingMessage } from 'http'
2-
import Url from 'fast-url-parser'
2+
import Url from 'urlite'
33
import { Matcher } from './Matcher'
44
import { MatchResult } from './MatchResult'
55

@@ -29,19 +29,20 @@ export type ExactQueryMatchResult<U extends QueryMatch> = MatchResult<{
2929
export class ExactQueryMatcher<U extends QueryMatch>
3030
implements Matcher<ExactQueryMatchResult<U>> {
3131
private readonly listConfig: [string, string | true | false | undefined][]
32-
constructor(private readonly config: U) {
32+
constructor(config: U) {
3333
this.listConfig = Object.entries(config)
3434
}
3535

3636
match(req: IncomingMessage): ExactQueryMatchResult<U> {
3737
/* istanbul ignore else */
3838
if (req.url !== undefined) {
39-
const { query } = Url.parse(req.url)
39+
// original URL returns '' if search is empty
40+
const search = Url.parse(req.url).search ?? ''
4041

4142
// parse query string into dict
4243
let params = {} as QueryResult<U>
43-
if (query !== null) {
44-
params = query.split(/&/).reduce((acc, parts) => {
44+
if (search !== '') {
45+
params = search.substring(1).split(/&/).reduce((acc, parts) => {
4546
const part = parts.split(/=/)
4647
const [key, value] = part
4748
// @ts-ignore

src/matchers/ExactUrlPathnameMatcher.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IncomingMessage } from 'http'
2-
import Url from 'fast-url-parser'
2+
import Url from 'urlite'
33
import { Matcher } from './Matcher'
44
import { MatchResult } from './MatchResult'
55

@@ -18,8 +18,9 @@ implements Matcher<ExactUrlPathnameMatchResult<U>> {
1818
match(req: IncomingMessage): ExactUrlPathnameMatchResult<U> {
1919
/* istanbul ignore else */
2020
if (req.url !== undefined) {
21-
const { pathname } = Url.parse(req.url)
22-
if (pathname !== null && this.urls.indexOf(pathname) >= 0) {
21+
// original URL returns '/' if pathname is empty
22+
const pathname = Url.parse(req.url).pathname ?? '/'
23+
if (this.urls.indexOf(pathname) >= 0) {
2324
return {
2425
matched: true,
2526
pathname,

src/matchers/__tests__/ExactUrlPathnameMatcher.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
import * as httpMocks from 'node-mocks-http'
22
import { ExactUrlPathnameMatcher } from '..'
33

4-
it('not match', () => {
4+
it('not match empty', () => {
55
const result = new ExactUrlPathnameMatcher(['/test'])
66
.match(httpMocks.createRequest())
77
expect(result).toStrictEqual({
88
matched: false,
99
})
1010
})
11-
it('not match', () => {
11+
12+
it('not match with postfix', () => {
1213
const result = new ExactUrlPathnameMatcher(['/test'])
13-
.match(httpMocks.createRequest())
14+
.match(httpMocks.createRequest({
15+
url: '/test2',
16+
}))
17+
expect(result).toStrictEqual({
18+
matched: false,
19+
})
20+
})
21+
22+
it('not match prefix', () => {
23+
const result = new ExactUrlPathnameMatcher(['/test'])
24+
.match(httpMocks.createRequest({
25+
url: '/tes',
26+
}))
1427
expect(result).toStrictEqual({
1528
matched: false,
1629
})

0 commit comments

Comments
 (0)