Skip to content
Myr

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

4 files+99 added−6 removed

modified:deno_dist/middleware/timing/index.ts+7 added-3 removed
modified:src/middleware/timing/index.test.ts+85 added
53 unmodified lines
54 ).toBeTruthy()55 expect(res.headers.get('server-timing')?.includes(region)).toBeTruthy()56 expect(res.headers.get('server-timing')?.includes(regionDesc)).toBeTruthy()57 })5859 describe('Should handle crossOrigin setting', async () => {60 it('Should do nothing when crossOrigin is falsy', async () => {61 const crossOriginApp = new Hono()6263 crossOriginApp.use(64 '*',65 timing({66 crossOrigin: false,67 })68 )6970 crossOriginApp.get('/', (c) => c.text('/'))7172 const res = await crossOriginApp.request('http://localhost/')7374 expect(res).not.toBeNull()75 expect(res.headers.has('server-timing')).toBeTruthy()76 expect(res.headers.has('timing-allow-origin')).toBeFalsy()77 })7879 it('Should set Timing-Allow-Origin to * when crossOrigin is true', async () => {80 const crossOriginApp = new Hono()8182 crossOriginApp.use(83 '*',84 timing({85 crossOrigin: true,86 })87 )8889 crossOriginApp.get('/', (c) => c.text('/'))9091 const res = await crossOriginApp.request('http://localhost/')9293 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 })9899 it('Should set Timing-Allow-Origin to the value of crossOrigin when it is a string', async () => {100 const crossOriginApp = new Hono()101102 crossOriginApp.use(103 '*',104 timing({105 crossOrigin: 'https://example.com',106 })107 )108109 crossOriginApp.get('/', (c) => c.text('/'))110111 const res = await crossOriginApp.request('http://localhost/')112113 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 })118119 it('Should set Timing-Allow-Origin to the return value of crossOrigin when it is a function', async () => {120 const crossOriginApp = new Hono()121122 crossOriginApp.use(123 '*',124 timing({125 crossOrigin: (c) => c.req.header('origin') ?? '*',126 })127 )128129 crossOriginApp.get('/', (c) => c.text('/'))130131 const res = await crossOriginApp.request('http://localhost/', {132 headers: {133 origin: 'https://example.com',134 },135 })136137 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 lines
20 total: boolean21 enabled: boolean | ((c: Context) => boolean)22 totalDescription: string23 autoEnd: boolean24 crossOrigin: boolean | string24 crossOrigin: boolean | string | ((c: Context) => boolean | string)25}2627const getTime = () => {28 try {
34 unmodified lines
63 const enabled = typeof options.enabled === 'function' ? options.enabled(c) : options.enabled6465 if (enabled) {66 c.res.headers.append('Server-Timing', headers.join(','))6768 const crossOrigin =69 typeof options.crossOrigin === 'function' ? options.crossOrigin(c) : options.crossOrigin7067 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 lines
modified:bun.lockbbinary

A binary file, modified.