-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
74 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { IncomingMessage, ServerResponse } from 'micri'; | ||
import { sendError } from './error'; | ||
|
||
// These methods are allowd for static resources: files and folder listings. | ||
const ALLOW_METHODS = ['GET', 'HEAD', 'OPTIONS']; | ||
const ALLOW_METHODS_STR = ALLOW_METHODS.join(', '); | ||
|
||
const ALLOW_ORIGIN = '*'; | ||
const ALLOW_REQ_HEADERS = 'Accept, Accept-Encoding, Range'; | ||
const EXPOSE_RES_HEADERS = | ||
'Accept-Ranges, Content-Range, Content-Length, Content-Type, Content-Encoding, Content-Disposition, Date, ETag, Transfer-Encoding, Server'; | ||
|
||
/** | ||
* Check that the request method is one of the allowed types for static resources. | ||
*/ | ||
export default function allowedMethod(req: IncomingMessage, res: ServerResponse): boolean { | ||
if (!ALLOW_METHODS.includes(req.method || '')) { | ||
res.setHeader('Allow', ALLOW_METHODS_STR); | ||
sendError(req, res, 405, { | ||
code: 'method_not_allowed', | ||
message: 'Method not allowed', | ||
}); | ||
|
||
return false; | ||
} | ||
|
||
if (req.method === 'OPTIONS') { | ||
res.writeHead(204, { | ||
'Access-Control-Allow-Origin': ALLOW_ORIGIN, | ||
'Access-Control-Allow-Methods': ALLOW_METHODS_STR, | ||
'Access-Control-Allow-Headers': ALLOW_REQ_HEADERS, | ||
'Access-Control-Expose-Headers': EXPOSE_RES_HEADERS, | ||
'Access-Control-Max-Age': '86400', | ||
}); | ||
res.end(); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { ServerResponse } from 'micri'; | ||
|
||
export default (res: ServerResponse) => res.setHeader('Vary', 'Accept, Accept-Encoding, Range'); | ||
export default (res: ServerResponse): void => res.setHeader('Vary', 'Accept, Accept-Encoding, Range'); |