From 9a5573fb7fd724d52217745c839d81bacb3f90f9 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Sun, 21 Feb 2021 12:11:20 +0300 Subject: [PATCH] feat: get rid of deprecated url.parse in favor of a custom func --- __tests__/core/app.test.ts | 40 ++++++++++++++++++++++++++++++++ __tests__/modules/router.test.ts | 14 +++++++++++ build/deps.js | 3 ++- packages/app/src/app.ts | 6 ++--- packages/app/src/request.ts | 2 +- packages/url/src/index.ts | 16 ++++++++----- 6 files changed, 70 insertions(+), 11 deletions(-) diff --git a/__tests__/core/app.test.ts b/__tests__/core/app.test.ts index 46a48a95..5e8870ca 100644 --- a/__tests__/core/app.test.ts +++ b/__tests__/core/app.test.ts @@ -523,6 +523,46 @@ describe('Route handlers', () => { await fetch('/').expect(200, 'hello world') }) + /* it('router accepts array of wares', async () => { + const app = new App({ + onError: (err, _, res) => { + console.log(err) + + res.sendStatus(500) + } + }) + + app.get( + '/', + [ + (req, _, n) => { + req.body = 'hello' + n() + } + ], + [ + (req, _, n) => { + req.body += ' ' + n() + } + ], + [ + (req, _, n) => { + req.body += 'world' + n() + }, + (req, res) => { + res.send(req.body) + } + ] + ) + + const server = app.listen() + + const fetch = makeFetch(server) + + await fetch('/').expect(200, 'hello world') + }) */ it('router methods do not match loosely', async () => { const app = new App() diff --git a/__tests__/modules/router.test.ts b/__tests__/modules/router.test.ts index 81bda771..879f77c5 100644 --- a/__tests__/modules/router.test.ts +++ b/__tests__/modules/router.test.ts @@ -128,6 +128,20 @@ describe('Testing Router', () => { }) describe('Testing HTTP methods', () => { + /* it('should accept arrays for first argument', () => { + const router = new Router() + + router.get('/', [() => 1, () => 2]) + + expect(router.middleware.length).toBe(2) + }) + it('should accept arrays for first and second argument', () => { + const router = new Router() + + router.get('/', [() => 1, () => 2], [() => 3]) + + expect(router.middleware.length).toBe(3) + }) */ it('app.get should set GET as HTTP method', () => { const router = new Router() diff --git a/build/deps.js b/build/deps.js index 7d6179f7..2ae4a9f1 100644 --- a/build/deps.js +++ b/build/deps.js @@ -7,5 +7,6 @@ export default (deps) => [ 'fs', 'fs/promises', 'net', - 'events' + 'events', + 'querystring' ] diff --git a/packages/app/src/app.ts b/packages/app/src/app.ts index 72095ae9..ea1f8aea 100644 --- a/packages/app/src/app.ts +++ b/packages/app/src/app.ts @@ -1,6 +1,5 @@ import { createServer, Server } from 'http' import path from 'path' -import { parse } from 'url' import { getURLParams } from './request' import type { Request } from './request' import type { Response } from './response' @@ -9,6 +8,7 @@ import { onErrorHandler } from './onError' import { Middleware, Handler, NextFunction, Router, UseMethodParams } from '@tinyhttp/router' import { extendMiddleware } from './extend' import rg from 'regexparam' +import { getPathname } from '@tinyhttp/req' /** * Add leading slash if not present (e.g. path -> /path, /path -> /path) @@ -241,7 +241,7 @@ export class App< req.originalUrl = req.url || req.originalUrl - const { pathname } = parse(req.originalUrl) + const pathname = getPathname(req.originalUrl) const mw: Middleware[] = [ { @@ -262,7 +262,7 @@ export class App< req.url = lead(req.url.substring(path.length)) || '/' - req.path = parse(req.url).pathname + req.path = getPathname(req.url) if (type === 'route') req.params = getURLParams(regex, pathname) diff --git a/packages/app/src/request.ts b/packages/app/src/request.ts index 7560fbe7..ce3d6509 100644 --- a/packages/app/src/request.ts +++ b/packages/app/src/request.ts @@ -28,7 +28,7 @@ const trustRemoteAddress = ({ connection }: Pick) => - middleware.find(({ handler }) => handler.name === h.name) + middleware.find(({ handler }) => typeof handler === 'function' && handler.name === h.name) export const getProtocol = (req: Request): Protocol => { const proto = req.connection.encrypted ? 'https' : 'http' diff --git a/packages/url/src/index.ts b/packages/url/src/index.ts index aac98371..af09d9df 100644 --- a/packages/url/src/index.ts +++ b/packages/url/src/index.ts @@ -1,5 +1,5 @@ import rg from 'regexparam' -import { parse } from 'url' +import * as qs from 'querystring' import { ParsedUrlQuery } from 'querystring' type Regex = { @@ -12,15 +12,19 @@ export const getURLParams = ({ pattern, keys }: Regex, reqUrl = '/'): URLParams const params = {} - if (matches) { - for (let i = 0; i < keys.length; i++) params[keys[i]] = matches[i + 1] - } + if (matches) for (let i = 0; i < keys.length; i++) params[keys[i]] = matches[i + 1] return params } -export const getQueryParams = (url = '/'): ParsedUrlQuery => parse(url, true).query - export type URLParams = { [key: string]: string } + +export const getPathname = (u: string) => { + const end = u.indexOf('?') + + return u.slice(0, end === -1 ? u.length - 1 : end) +} + +export const getQueryParams = (url = '/'): ParsedUrlQuery => qs.parse(url.slice(url.indexOf('?') + 1))