Skip to content

Commit

Permalink
feat(timing): allow crossOrigin in TimingOptions to be a function (#2359
Browse files Browse the repository at this point in the history
)

* feat: allow crossOrigin in TimingOptions to be a function

* chore: denoify

* chore: undo accidental change

* test: add tests
  • Loading branch information
jonahsnider authored May 3, 2024
1 parent 791b969 commit 78fa4ca
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 7 additions & 3 deletions deno_dist/middleware/timing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface TimingOptions {
enabled: boolean | ((c: Context) => boolean)
totalDescription: string
autoEnd: boolean
crossOrigin: boolean | string
crossOrigin: boolean | string | ((c: Context) => boolean | string)
}

const getTime = () => {
Expand Down Expand Up @@ -64,10 +64,14 @@ export const timing = (config?: Partial<TimingOptions>): MiddlewareHandler => {

if (enabled) {
c.res.headers.append('Server-Timing', headers.join(','))
if (options.crossOrigin) {

const crossOrigin =
typeof options.crossOrigin === 'function' ? options.crossOrigin(c) : options.crossOrigin

if (crossOrigin) {
c.res.headers.append(
'Timing-Allow-Origin',
typeof options.crossOrigin === 'string' ? options.crossOrigin : '*'
typeof crossOrigin === 'string' ? crossOrigin : '*'
)
}
}
Expand Down
85 changes: 85 additions & 0 deletions src/middleware/timing/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,89 @@ describe('Server-Timing API', () => {
expect(res.headers.get('server-timing')?.includes(region)).toBeTruthy()
expect(res.headers.get('server-timing')?.includes(regionDesc)).toBeTruthy()
})

describe('Should handle crossOrigin setting', async () => {
it('Should do nothing when crossOrigin is falsy', async () => {
const crossOriginApp = new Hono()

crossOriginApp.use(
'*',
timing({
crossOrigin: false,
})
)

crossOriginApp.get('/', (c) => c.text('/'))

const res = await crossOriginApp.request('http://localhost/')

expect(res).not.toBeNull()
expect(res.headers.has('server-timing')).toBeTruthy()
expect(res.headers.has('timing-allow-origin')).toBeFalsy()
})

it('Should set Timing-Allow-Origin to * when crossOrigin is true', async () => {
const crossOriginApp = new Hono()

crossOriginApp.use(
'*',
timing({
crossOrigin: true,
})
)

crossOriginApp.get('/', (c) => c.text('/'))

const res = await crossOriginApp.request('http://localhost/')

expect(res).not.toBeNull()
expect(res.headers.has('server-timing')).toBeTruthy()
expect(res.headers.has('timing-allow-origin')).toBeTruthy()
expect(res.headers.get('timing-allow-origin')).toBe('*')
})

it('Should set Timing-Allow-Origin to the value of crossOrigin when it is a string', async () => {
const crossOriginApp = new Hono()

crossOriginApp.use(
'*',
timing({
crossOrigin: 'https://example.com',
})
)

crossOriginApp.get('/', (c) => c.text('/'))

const res = await crossOriginApp.request('http://localhost/')

expect(res).not.toBeNull()
expect(res.headers.has('server-timing')).toBeTruthy()
expect(res.headers.has('timing-allow-origin')).toBeTruthy()
expect(res.headers.get('timing-allow-origin')).toBe('https://example.com')
})

it('Should set Timing-Allow-Origin to the return value of crossOrigin when it is a function', async () => {
const crossOriginApp = new Hono()

crossOriginApp.use(
'*',
timing({
crossOrigin: (c) => c.req.header('origin') ?? '*',
})
)

crossOriginApp.get('/', (c) => c.text('/'))

const res = await crossOriginApp.request('http://localhost/', {
headers: {
origin: 'https://example.com',
},
})

expect(res).not.toBeNull()
expect(res.headers.has('server-timing')).toBeTruthy()
expect(res.headers.has('timing-allow-origin')).toBeTruthy()
expect(res.headers.get('timing-allow-origin')).toBe('https://example.com')
})
})
})
10 changes: 7 additions & 3 deletions src/middleware/timing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface TimingOptions {
enabled: boolean | ((c: Context) => boolean)
totalDescription: string
autoEnd: boolean
crossOrigin: boolean | string
crossOrigin: boolean | string | ((c: Context) => boolean | string)
}

const getTime = () => {
Expand Down Expand Up @@ -64,10 +64,14 @@ export const timing = (config?: Partial<TimingOptions>): MiddlewareHandler => {

if (enabled) {
c.res.headers.append('Server-Timing', headers.join(','))
if (options.crossOrigin) {

const crossOrigin =
typeof options.crossOrigin === 'function' ? options.crossOrigin(c) : options.crossOrigin

if (crossOrigin) {
c.res.headers.append(
'Timing-Allow-Origin',
typeof options.crossOrigin === 'string' ? options.crossOrigin : '*'
typeof crossOrigin === 'string' ? crossOrigin : '*'
)
}
}
Expand Down

0 comments on commit 78fa4ca

Please sign in to comment.