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
Provenance
- Commit
c522636- Tree
50184a6- Author
Minghe <h.minghe@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
280c148
11 files changed+195 added−187 removed
modified:src/middleware/basic-auth/basic-auth.ts+1 added-1 removed
122345659 unmodified linesimport type { Context } from '../../context'import { timingSafeEqual } from '../../util'import { timingSafeEqual } from '../../utils/buffer'
const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/const USER_PASS_REGEXP = /^([^:]*):(.*)$/59 unmodified linesmodified:src/middleware/logger/logger.ts+1 added-1 removed
11234571 unmodified linesimport { getPathFromURL } from '../../util'import { getPathFromURL } from '../../utils/url'import type { Context } from '../../context'
const humanize = ( n: string[],71 unmodified linesadded:src/utils/buffer.test.ts+55 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import { timingSafeEqual } from './buffer'
describe('buffer', () => { const crypto = global.crypto beforeAll(() => { global.crypto = require('crypto').webcrypto }) afterAll(() => { global.crypto = crypto })
it('positive', async () => { expect( await timingSafeEqual( '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935', '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935' ) ).toBe(true) expect(await timingSafeEqual('a', 'a')).toBe(true) expect(await timingSafeEqual('', '')).toBe(true) expect(await timingSafeEqual(undefined, undefined)).toBe(true) expect(await timingSafeEqual(true, true)).toBe(true) expect(await timingSafeEqual(false, false)).toBe(true) })
it('negative', async () => { expect(await timingSafeEqual('a', 'b')).toBe(false) expect( await timingSafeEqual( 'a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) ).toBe(false) expect( await timingSafeEqual( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a' ) ).toBe(false) expect(await timingSafeEqual('alpha', 'beta')).toBe(false) expect(await timingSafeEqual(false, true)).toBe(false) expect(await timingSafeEqual(false, undefined)).toBe(false) expect( await timingSafeEqual( () => {}, () => {} ) ).toBe(false) expect(await timingSafeEqual({}, {})).toBe(false) expect(await timingSafeEqual({ a: 1 }, { a: 1 })).toBe(false) expect(await timingSafeEqual({ a: 1 }, { a: 2 })).toBe(false) expect(await timingSafeEqual([1, 2], [1, 2])).toBe(false) expect(await timingSafeEqual([1, 2], [1, 2, 3])).toBe(false) })})added:src/utils/buffer.ts+38 added
1234567891011121314151617181920212223242526272829303132333435363738export const equal = (a: ArrayBuffer, b: ArrayBuffer) => { if (a === b) { return true } if (a.byteLength !== b.byteLength) { return false }
const va = new DataView(a) const vb = new DataView(b)
let i = va.byteLength while (i--) { if (va.getUint8(i) !== vb.getUint8(i)) { return false } }
return true}
export const timingSafeEqual = async (a: any, b: any) => { const sa = await crypto.subtle.digest( { name: 'SHA-256', }, new TextEncoder().encode(String(a)) )
const sb = await crypto.subtle.digest( { name: 'SHA-256', }, new TextEncoder().encode(String(b)) )
return equal(sa, sb) && a === b}added:src/utils/url.test.ts+57 added
1+import { splitPath, getPattern, getPathFromURL, isAbsoluteURL } from './url'2+3+describe('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+ })20+21+ 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+ })30+31+ 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+ })45+46+ 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
1+const URL_REGEXP = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/2+3+export 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+}10+11+export 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+}25+26+export const getPathFromURL = (url: string): string => {27+ const match = url.match(URL_REGEXP)28+ if (match) {29+ return match[5]30+ }31+ return ''32+}33+34+export 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
1-import { maxHeaderSize } from 'http'2-import { isAbsoluteURL } from './util'1+import { isAbsoluteURL } from './utils/url'2 3 type Headers = { [key: string]: string }4 5 export interface Env {}77 unmodified linesmodified:src/hono.ts+1 added-1 removed
1 import type { Result } from './node'2 import { Node } from './node'3 import { compose } from './compose'4-import { getPathFromURL } from './util'4+import { getPathFromURL } from './utils/url'5 import { Middleware } from './middleware'6 import { Context } from './context'7 import type { Env } from './context'8 180 unmodified linesmodified:src/node.ts+1 added-1 removed
1-import { splitPath, getPattern } from './util'1+import { splitPath, getPattern } from './utils/url'2 3 const METHOD_NAME_OF_ALL = 'ALL'4 5 export class Result<T> {121 unmodified linesdeleted:src/util.ts-80 removed
1-const URL_REGEXP = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/2-3-export 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-}10-11-export 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-}25-26-export const getPathFromURL = (url: string): string => {27- // XXX28- const match = url.match(URL_REGEXP)29- if (match) {30- return match[5]31- }32- return ''33-}34-35-export const isAbsoluteURL = (url: string): boolean => {36- const match = url.match(URL_REGEXP)37- if (match && match[1]) {38- return true39- }40- return false41-}42-43-const bufferEqual = (a: ArrayBuffer, b: ArrayBuffer) => {44- if (a === b) {45- return true46- }47- if (a.byteLength !== b.byteLength) {48- return false49- }50-51- const va = new DataView(a)52- const vb = new DataView(b)53-54- let i = va.byteLength55- while (i--) {56- if (va.getUint8(i) !== vb.getUint8(i)) {57- return false58- }59- }60-61- return true62-}63-64-export 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- )71-72- const sb = await crypto.subtle.digest(73- {74- name: 'SHA-256',75- },76- new TextEncoder().encode(String(b))77- )78-79- return bufferEqual(sa, sb) && a === b80-}deleted:test/util.test.ts-101 removed
1-import { splitPath, getPattern, getPathFromURL, timingSafeEqual, isAbsoluteURL } from '../src/util'2-3-describe('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- })20-21- 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- })30-31- 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- })45-46- 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-58- describe('timingSafeEqual', () => {59- const crypto = global.crypto60- beforeAll(() => {61- global.crypto = require('crypto').webcrypto62- })63- afterAll(() => {64- global.crypto = crypto65- })66-67- 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- })80-81- 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-})