Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/on-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/[email protected]
- name: Read .nvmrc
id: nvm
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
node-version-file: '.nvmrc'
- name: prepare dependencies
run: npm install
- name: test
run: npm run precommit
run: npm run precommit
6 changes: 2 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/[email protected]
- name: Read .nvmrc
id: nvm
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
node-version-file: '.nvmrc'
- name: prepare dependencies
run: npm install
- name: test
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v12.12.0
v16.14.0
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@bessonovs/node-http-router",
"version": "0.0.7",
"description": "Extensible http router for node and micro",
"keywords": [
"router",
Expand All @@ -16,7 +17,6 @@
"http",
"server"
],
"version": "0.0.6",
"author": "Anton Bessonov",
"license": "MIT",
"repository": "bessonov/node-http-router",
Expand All @@ -33,26 +33,26 @@
"precommit": "$_ run test && $_ run lint && $_ run build"
},
"dependencies": {
"fast-url-parser": "1.1.3"
"urlite": "3.0.0"
},
"devDependencies": {
"@bessonovs/eslint-config": "0.0.7",
"@types/express": "4.17.12",
"@types/jest": "25.2.2",
"@types/node": "14.0.1",
"@typescript-eslint/eslint-plugin": "4.28.1",
"@typescript-eslint/parser": "4.28.1",
"eslint": "7.29.0",
"eslint-config-airbnb": "18.2.1",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-react": "7.24.0",
"jest": "27.0.6",
"@types/express": "4.17.13",
"@types/jest": "27.4.1",
"@types/node": "16.11.7",
"@typescript-eslint/eslint-plugin": "5.13.0",
"@typescript-eslint/parser": "5.13.0",
"eslint": "8.10.0",
"eslint-config-airbnb": "19.0.4",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-jsx-a11y": "6.5.1",
"eslint-plugin-react": "7.29.3",
"jest": "27.5.1",
"micro": "9.3.5-canary.3",
"node-mocks-http": "1.10.1",
"node-mocks-http": "1.11.0",
"path-to-regexp": "6.2.0",
"ts-jest": "27.0.3",
"typescript": "4.3.4"
"ts-jest": "27.1.3",
"typescript": "4.6.2"
},
"publishConfig": {
"access": "public"
Expand Down
8 changes: 0 additions & 8 deletions src/@types/fast-url-parser/index.d.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/@types/urlite/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module 'urlite' {
const Url: {
parse: (url: string) => {
pathname: string | undefined
search: string | undefined
}
}
// eslint-disable-next-line import/no-default-export
export default Url
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './matchers'
export { Route, Router, MatchedHandler } from './router'
export type { Route, Router, MatchedHandler } from './router'
11 changes: 6 additions & 5 deletions src/matchers/ExactQueryMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IncomingMessage } from 'http'
import Url from 'fast-url-parser'
import Url from 'urlite'
import { Matcher } from './Matcher'
import { MatchResult } from './MatchResult'

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

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

// parse query string into dict
let params = {} as QueryResult<U>
if (query !== null) {
params = query.split(/&/).reduce((acc, parts) => {
if (search !== '') {
params = search.substring(1).split(/&/).reduce((acc, parts) => {
const part = parts.split(/=/)
const [key, value] = part
// @ts-ignore
Expand Down
7 changes: 4 additions & 3 deletions src/matchers/ExactUrlPathnameMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IncomingMessage } from 'http'
import Url from 'fast-url-parser'
import Url from 'urlite'
import { Matcher } from './Matcher'
import { MatchResult } from './MatchResult'

Expand All @@ -18,8 +18,9 @@ implements Matcher<ExactUrlPathnameMatchResult<U>> {
match(req: IncomingMessage): ExactUrlPathnameMatchResult<U> {
/* istanbul ignore else */
if (req.url !== undefined) {
const { pathname } = Url.parse(req.url)
if (pathname !== null && this.urls.indexOf(pathname) >= 0) {
// original URL returns '/' if pathname is empty
const pathname = Url.parse(req.url).pathname ?? '/'
if (this.urls.indexOf(pathname) >= 0) {
return {
matched: true,
pathname,
Expand Down
19 changes: 16 additions & 3 deletions src/matchers/__tests__/ExactUrlPathnameMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import * as httpMocks from 'node-mocks-http'
import { ExactUrlPathnameMatcher } from '..'

it('not match', () => {
it('not match empty', () => {
const result = new ExactUrlPathnameMatcher(['/test'])
.match(httpMocks.createRequest())
expect(result).toStrictEqual({
matched: false,
})
})
it('not match', () => {

it('not match with postfix', () => {
const result = new ExactUrlPathnameMatcher(['/test'])
.match(httpMocks.createRequest())
.match(httpMocks.createRequest({
url: '/test2',
}))
expect(result).toStrictEqual({
matched: false,
})
})

it('not match prefix', () => {
const result = new ExactUrlPathnameMatcher(['/test'])
.match(httpMocks.createRequest({
url: '/tes',
}))
expect(result).toStrictEqual({
matched: false,
})
Expand Down