feat(timing): allow crossOrigin in TimingOptions to be a function (#2359)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: allow crossOrigin in TimingOptions to be a function * chore: denoify * chore: undo accidental change * test: add tests
authored by Jonah Snider · committed by GitHub · parent 791b969 · Browse files · 4 files·+99 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+99 added−6 removed
- tests:+85 added−0 removed
- other:+14 added−6 removed
Provenance
- Commit
78fa4ca- Tree
343eaf4- Author
Jonah Snider <jonah@jonahsnider.com>- Committer
GitHub <noreply@github.com>- Parent
791b969
4 files changed+99 added−6 removed
modified:deno_dist/middleware/timing/index.ts+7 added-3 removed
19 unmodified lines2021222324242526272834 unmodified lines63646566676869706771727370747576777861 unmodified lines19 unmodified lines total: boolean enabled: boolean | ((c: Context) => boolean) totalDescription: string autoEnd: boolean crossOrigin: boolean | string crossOrigin: boolean | string | ((c: Context) => boolean | string)}
const getTime = () => { try {34 unmodified lines const enabled = typeof options.enabled === 'function' ? options.enabled(c) : options.enabled
if (enabled) { c.res.headers.append('Server-Timing', headers.join(','))
const crossOrigin = typeof options.crossOrigin === 'function' ? options.crossOrigin(c) : options.crossOrigin
if (options.crossOrigin) { if (crossOrigin) { c.res.headers.append( 'Timing-Allow-Origin', typeof options.crossOrigin === 'string' ? options.crossOrigin : '*' typeof crossOrigin === 'string' ? crossOrigin : '*' ) } } }61 unmodified linesmodified:src/middleware/timing/index.test.ts+85 added
53 unmodified lines54 ).toBeTruthy()55 expect(res.headers.get('server-timing')?.includes(region)).toBeTruthy()56 expect(res.headers.get('server-timing')?.includes(regionDesc)).toBeTruthy()57 })58+59+ describe('Should handle crossOrigin setting', async () => {60+ it('Should do nothing when crossOrigin is falsy', async () => {61+ const crossOriginApp = new Hono()62+63+ crossOriginApp.use(64+ '*',65+ timing({66+ crossOrigin: false,67+ })68+ )69+70+ crossOriginApp.get('/', (c) => c.text('/'))71+72+ const res = await crossOriginApp.request('http://localhost/')73+74+ expect(res).not.toBeNull()75+ expect(res.headers.has('server-timing')).toBeTruthy()76+ expect(res.headers.has('timing-allow-origin')).toBeFalsy()77+ })78+79+ it('Should set Timing-Allow-Origin to * when crossOrigin is true', async () => {80+ const crossOriginApp = new Hono()81+82+ crossOriginApp.use(83+ '*',84+ timing({85+ crossOrigin: true,86+ })87+ )88+89+ crossOriginApp.get('/', (c) => c.text('/'))90+91+ const res = await crossOriginApp.request('http://localhost/')92+93+ expect(res).not.toBeNull()94+ expect(res.headers.has('server-timing')).toBeTruthy()95+ expect(res.headers.has('timing-allow-origin')).toBeTruthy()96+ expect(res.headers.get('timing-allow-origin')).toBe('*')97+ })98+99+ it('Should set Timing-Allow-Origin to the value of crossOrigin when it is a string', async () => {100+ const crossOriginApp = new Hono()101+102+ crossOriginApp.use(103+ '*',104+ timing({105+ crossOrigin: 'https://example.com',106+ })107+ )108+109+ crossOriginApp.get('/', (c) => c.text('/'))110+111+ const res = await crossOriginApp.request('http://localhost/')112+113+ expect(res).not.toBeNull()114+ expect(res.headers.has('server-timing')).toBeTruthy()115+ expect(res.headers.has('timing-allow-origin')).toBeTruthy()116+ expect(res.headers.get('timing-allow-origin')).toBe('https://example.com')117+ })118+119+ it('Should set Timing-Allow-Origin to the return value of crossOrigin when it is a function', async () => {120+ const crossOriginApp = new Hono()121+122+ crossOriginApp.use(123+ '*',124+ timing({125+ crossOrigin: (c) => c.req.header('origin') ?? '*',126+ })127+ )128+129+ crossOriginApp.get('/', (c) => c.text('/'))130+131+ const res = await crossOriginApp.request('http://localhost/', {132+ headers: {133+ origin: 'https://example.com',134+ },135+ })136+137+ expect(res).not.toBeNull()138+ expect(res.headers.has('server-timing')).toBeTruthy()139+ expect(res.headers.has('timing-allow-origin')).toBeTruthy()140+ expect(res.headers.get('timing-allow-origin')).toBe('https://example.com')141+ })142+ })143 })modified:src/middleware/timing/index.ts+7 added-3 removed
19 unmodified lines20 total: boolean21 enabled: boolean | ((c: Context) => boolean)22 totalDescription: string23 autoEnd: boolean24- crossOrigin: boolean | string24+ crossOrigin: boolean | string | ((c: Context) => boolean | string)25 }26 27 const getTime = () => {28 try {34 unmodified lines63 const enabled = typeof options.enabled === 'function' ? options.enabled(c) : options.enabled64 65 if (enabled) {66 c.res.headers.append('Server-Timing', headers.join(','))67+68+ const crossOrigin =69+ typeof options.crossOrigin === 'function' ? options.crossOrigin(c) : options.crossOrigin70+67- if (options.crossOrigin) {71+ if (crossOrigin) {72 c.res.headers.append(73 'Timing-Allow-Origin',70- typeof options.crossOrigin === 'string' ? options.crossOrigin : '*'74+ typeof crossOrigin === 'string' ? crossOrigin : '*'75 )76 }77 }78 }61 unmodified linesmodified:bun.lockbbinary
A binary file, modified.