Skip to content

Commit

Permalink
feat: allow set CORS allow credentials header
Browse files Browse the repository at this point in the history
  • Loading branch information
idanto committed Jul 25, 2024
1 parent a09bb31 commit 0865e9d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/server/src/handlers/BaseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class BaseHandler extends EventEmitter {
}

write(res: http.ServerResponse, status: number, headers = {}, body = '') {
if(this.options.allowedCredentials) {

Check failure on line 30 in packages/server/src/handlers/BaseHandler.ts

View workflow job for this annotation

GitHub Actions / lts/hydrogen

Insert `·`
res.setHeader('Access-Control-Allow-Credentials', 'true');

Check failure on line 31 in packages/server/src/handlers/BaseHandler.ts

View workflow job for this annotation

GitHub Actions / lts/hydrogen

Delete `;`
}
if (status !== 204) {
// @ts-expect-error not explicitly typed but possible
headers['Content-Length'] = Buffer.byteLength(body, 'utf8')
Expand Down
5 changes: 5 additions & 0 deletions packages/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export type ServerOptions = {
*/
allowedHeaders?: string[]

/**
* set `Access-Control-Allow-Credentials` to true.
*/
allowedCredentials?: boolean;

Check failure on line 40 in packages/server/src/types.ts

View workflow job for this annotation

GitHub Actions / lts/hydrogen

Delete `;`

/**
* Interval in milliseconds for sending progress of an upload over `EVENTS.POST_RECEIVE_V2`
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/server/test/BaseHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ describe('BaseHandler', () => {
done()
})

it('write() should set Access-Control-Allow-Credentials', (done) => {
handler.options.allowedCredentials = true;
const header = 'Access-Control-Allow-Credentials'
const headers = {[header]: 'GET, OPTIONS'}
handler.write(res, 200, headers)
assert.equal(res.getHeader(header), 'true'])
handler.options.allowedCredentials = false;

done()
})

it('write() should write the body', (done) => {
const body = 'Hello tus!'
handler.write(res, 200, {}, body)
Expand Down

0 comments on commit 0865e9d

Please sign in to comment.