Skip to content
Myr

refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Minghe · committed by GitHub · parent 280c148 · Browse files · 11 files+195 added−187 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+195 added−187 removed

  • tests:+112 added−101 removed
  • other:+83 added−86 removed

11 files+195 added−187 removed

modified:src/middleware/basic-auth/basic-auth.ts+1 added-1 removed
modified:src/middleware/logger/logger.ts+1 added-1 removed
added:src/utils/buffer.test.ts+55 added
added:src/utils/buffer.ts+38 added
added:src/utils/url.test.ts+57 added
1import { splitPath, getPattern, getPathFromURL, isAbsoluteURL } from './url'23describe('url', () => {4 it('splitPath', () => {5 let ps = splitPath('/')6 expect(ps[0]).toBe('')7 ps = splitPath('/hello')8 expect(ps[0]).toBe('hello')9 ps = splitPath('*')10 expect(ps[0]).toBe('*')11 ps = splitPath('/wildcard-abc/*/wildcard-efg')12 expect(ps[0]).toBe('wildcard-abc')13 expect(ps[1]).toBe('*')14 expect(ps[2]).toBe('wildcard-efg')15 ps = splitPath('/map/:location/events')16 expect(ps[0]).toBe('map')17 expect(ps[1]).toBe(':location')18 expect(ps[2]).toBe('events')19 })2021 it('getPattern', () => {22 let res = getPattern(':id')23 expect(res).not.toBeNull()24 expect(res[0]).toBe('id')25 expect(res[1]).toBe('(.+)')26 res = getPattern(':id{[0-9]+}')27 expect(res[0]).toBe('id')28 expect(res[1]).toBe('([0-9]+)')29 })3031 it('getPathFromURL', () => {32 let path = getPathFromURL('https://example.com/')33 expect(path).toBe('/')34 path = getPathFromURL('https://example.com/hello')35 expect(path).toBe('/hello')36 path = getPathFromURL('https://example.com/hello/hey')37 expect(path).toBe('/hello/hey')38 path = getPathFromURL('https://example.com/hello?name=foo')39 expect(path).toBe('/hello')40 path = getPathFromURL('https://example.com/hello/hey?name=foo&name=bar')41 expect(path).toBe('/hello/hey')42 path = getPathFromURL('https://example.com/hello/hey#fragment')43 expect(path).toBe('/hello/hey')44 })4546 describe('isAbsoluteURL', () => {47 it('absolute url', () => {48 expect(isAbsoluteURL('https://example.com')).toBeTruthy()49 expect(isAbsoluteURL('https://example.com/xxx')).toBeTruthy()50 })51 it('relative url', () => {52 expect(isAbsoluteURL('/')).not.toBeTruthy()53 expect(isAbsoluteURL('/location')).not.toBeTruthy()54 expect(isAbsoluteURL('')).not.toBeTruthy()55 })56 })57})
added:src/utils/url.ts+40 added
1const URL_REGEXP = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/23export const splitPath = (path: string): string[] => {4 const paths = path.split(/\//) // faster than path.split('/')5 if (paths[0] === '') {6 paths.shift()7 }8 return paths9}1011export const getPattern = (label: string): string[] | null => {12 // :id{[0-9]+} => ([0-9]+)13 // :id => (.+)14 //const name = ''15 const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/)16 if (match) {17 if (match[2]) {18 return [match[1], '(' + match[2] + ')']19 } else {20 return [match[1], '(.+)']21 }22 }23 return null24}2526export const getPathFromURL = (url: string): string => {27 const match = url.match(URL_REGEXP)28 if (match) {29 return match[5]30 }31 return ''32}3334export const isAbsoluteURL = (url: string): boolean => {35 const match = url.match(URL_REGEXP)36 if (match && match[1]) {37 return true38 }39 return false40}
modified:src/context.ts+1 added-2 removed
1import { maxHeaderSize } from 'http'2import { isAbsoluteURL } from './util'1import { isAbsoluteURL } from './utils/url'23type Headers = { [key: string]: string }45export interface Env {}
77 unmodified lines
modified:src/hono.ts+1 added-1 removed
1import type { Result } from './node'2import { Node } from './node'3import { compose } from './compose'4import { getPathFromURL } from './util'4import { getPathFromURL } from './utils/url'5import { Middleware } from './middleware'6import { Context } from './context'7import type { Env } from './context'8
180 unmodified lines
modified:src/node.ts+1 added-1 removed
1import { splitPath, getPattern } from './util'1import { splitPath, getPattern } from './utils/url'23const METHOD_NAME_OF_ALL = 'ALL'45export class Result<T> {
121 unmodified lines
deleted:src/util.ts-80 removed
1const URL_REGEXP = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/23export const splitPath = (path: string): string[] => {4 const paths = path.split(/\//) // faster than path.split('/')5 if (paths[0] === '') {6 paths.shift()7 }8 return paths9}1011export const getPattern = (label: string): string[] | null => {12 // :id{[0-9]+} => ([0-9]+)13 // :id => (.+)14 //const name = ''15 const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/)16 if (match) {17 if (match[2]) {18 return [match[1], '(' + match[2] + ')']19 } else {20 return [match[1], '(.+)']21 }22 }23 return null24}2526export const getPathFromURL = (url: string): string => {27 // XXX28 const match = url.match(URL_REGEXP)29 if (match) {30 return match[5]31 }32 return ''33}3435export const isAbsoluteURL = (url: string): boolean => {36 const match = url.match(URL_REGEXP)37 if (match && match[1]) {38 return true39 }40 return false41}4243const bufferEqual = (a: ArrayBuffer, b: ArrayBuffer) => {44 if (a === b) {45 return true46 }47 if (a.byteLength !== b.byteLength) {48 return false49 }5051 const va = new DataView(a)52 const vb = new DataView(b)5354 let i = va.byteLength55 while (i--) {56 if (va.getUint8(i) !== vb.getUint8(i)) {57 return false58 }59 }6061 return true62}6364export const timingSafeEqual = async (a: any, b: any) => {65 const sa = await crypto.subtle.digest(66 {67 name: 'SHA-256',68 },69 new TextEncoder().encode(String(a))70 )7172 const sb = await crypto.subtle.digest(73 {74 name: 'SHA-256',75 },76 new TextEncoder().encode(String(b))77 )7879 return bufferEqual(sa, sb) && a === b80}
deleted:test/util.test.ts-101 removed
1import { splitPath, getPattern, getPathFromURL, timingSafeEqual, isAbsoluteURL } from '../src/util'23describe('Utility methods', () => {4 it('splitPath', () => {5 let ps = splitPath('/')6 expect(ps[0]).toBe('')7 ps = splitPath('/hello')8 expect(ps[0]).toBe('hello')9 ps = splitPath('*')10 expect(ps[0]).toBe('*')11 ps = splitPath('/wildcard-abc/*/wildcard-efg')12 expect(ps[0]).toBe('wildcard-abc')13 expect(ps[1]).toBe('*')14 expect(ps[2]).toBe('wildcard-efg')15 ps = splitPath('/map/:location/events')16 expect(ps[0]).toBe('map')17 expect(ps[1]).toBe(':location')18 expect(ps[2]).toBe('events')19 })2021 it('getPattern', () => {22 let res = getPattern(':id')23 expect(res).not.toBeNull()24 expect(res[0]).toBe('id')25 expect(res[1]).toBe('(.+)')26 res = getPattern(':id{[0-9]+}')27 expect(res[0]).toBe('id')28 expect(res[1]).toBe('([0-9]+)')29 })3031 it('getPathFromURL', () => {32 let path = getPathFromURL('https://example.com/')33 expect(path).toBe('/')34 path = getPathFromURL('https://example.com/hello')35 expect(path).toBe('/hello')36 path = getPathFromURL('https://example.com/hello/hey')37 expect(path).toBe('/hello/hey')38 path = getPathFromURL('https://example.com/hello?name=foo')39 expect(path).toBe('/hello')40 path = getPathFromURL('https://example.com/hello/hey?name=foo&name=bar')41 expect(path).toBe('/hello/hey')42 path = getPathFromURL('https://example.com/hello/hey#fragment')43 expect(path).toBe('/hello/hey')44 })4546 describe('isAbsoluteURL', () => {47 it('absolute url', () => {48 expect(isAbsoluteURL('https://example.com')).toBeTruthy()49 expect(isAbsoluteURL('https://example.com/xxx')).toBeTruthy()50 })51 it('relative url', () => {52 expect(isAbsoluteURL('/')).not.toBeTruthy()53 expect(isAbsoluteURL('/location')).not.toBeTruthy()54 expect(isAbsoluteURL('')).not.toBeTruthy()55 })56 })5758 describe('timingSafeEqual', () => {59 const crypto = global.crypto60 beforeAll(() => {61 global.crypto = require('crypto').webcrypto62 })63 afterAll(() => {64 global.crypto = crypto65 })6667 it('positive', async () => {68 expect(69 await timingSafeEqual(70 '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935',71 '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935'72 )73 ).toBe(true)74 expect(await timingSafeEqual('a', 'a')).toBe(true)75 expect(await timingSafeEqual('', '')).toBe(true)76 expect(await timingSafeEqual(undefined, undefined)).toBe(true)77 expect(await timingSafeEqual(true, true)).toBe(true)78 expect(await timingSafeEqual(false, false)).toBe(true)79 })8081 it('negative', async () => {82 expect(await timingSafeEqual('a', 'b')).toBe(false)83 expect(await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toBe(false)84 expect(await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')).toBe(false)85 expect(await timingSafeEqual('alpha', 'beta')).toBe(false)86 expect(await timingSafeEqual(false, true)).toBe(false)87 expect(await timingSafeEqual(false, undefined)).toBe(false)88 expect(89 await timingSafeEqual(90 () => {},91 () => {}92 )93 ).toBe(false)94 expect(await timingSafeEqual({}, {})).toBe(false)95 expect(await timingSafeEqual({ a: 1 }, { a: 1 })).toBe(false)96 expect(await timingSafeEqual({ a: 1 }, { a: 2 })).toBe(false)97 expect(await timingSafeEqual([1, 2], [1, 2])).toBe(false)98 expect(await timingSafeEqual([1, 2], [1, 2, 3])).toBe(false)99 })100 })101})