1
0

7 Commits

Author SHA1 Message Date
76d7040a1b 3.1.0 2023-09-29 12:20:50 +02:00
0037fb64ea update readme 2023-09-29 12:20:23 +02:00
Toby
a5ccf0fc01 support Access-Control-Allow-Credentials and vary CORS headers, thanks @akreiling 2023-09-29 10:14:07 +00:00
Toby
e006d0a3b1 use a new buffer for every invocation of handle, thanks @akreiling 2023-09-29 10:12:15 +00:00
Andrew Kreiling
c7d7a642e6 support Access-Control-Allow-Credentials and vary CORS headers 2023-09-28 07:52:34 -05:00
Andrew Kreiling
49c7897bd0 use a new buffer for every invocation of handle 2023-09-28 07:52:14 -05:00
e28976a4b6 update migration 2023-08-22 21:04:44 +02:00
5 changed files with 29 additions and 25 deletions

View File

@@ -7,7 +7,6 @@ From `v2.x.x` to `v3.x.x`.
- [Update Router](#update-router) - [Update Router](#update-router)
- [Handlers](#handlers) - [Handlers](#handlers)
- [Fetch](#fetch--routerhandle)
## Update Router ## Update Router

View File

@@ -211,10 +211,12 @@ If enabled will overwrite other `OPTIONS` requests.
#### `config` (object, optional) #### `config` (object, optional)
Key | Type | Default Value Key | Type | Default Value
---------------------- | --------- | ------------- -------------------------- | ---------- | -------------
`allowOrigin` | `string` | `*` `allowOrigin` | `string` | `*`
`allowMethods` | `string` | `*` `allowMethods` | `string` | `*`
`allowHeaders` | `string` | `*` `allowHeaders` | `string` | `*`
`allowCredentials` | `boolean` | `undefined`
`vary` | `string` | `undefined`
`maxAge` | `integer` | `86400` `maxAge` | `integer` | `86400`
`optionsSuccessStatus` | `integer` | `204` `optionsSuccessStatus` | `integer` | `204`

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "@tsndr/cloudflare-worker-router", "name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0", "version": "3.1.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@tsndr/cloudflare-worker-router", "name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0", "version": "3.1.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@cloudflare/workers-types": "^4.20230814.0", "@cloudflare/workers-types": "^4.20230814.0",

View File

@@ -1,6 +1,6 @@
{ {
"name": "@tsndr/cloudflare-worker-router", "name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0", "version": "3.1.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",

View File

@@ -91,6 +91,8 @@ export type RouterHandler<TEnv = any, TCtx = any, TReq = any> = {
* @property {string} [allowOrigin="*"] Access-Control-Allow-Origin (default: `*`) * @property {string} [allowOrigin="*"] Access-Control-Allow-Origin (default: `*`)
* @property {string} [allowMethods="*"] Access-Control-Allow-Methods (default: `*`) * @property {string} [allowMethods="*"] Access-Control-Allow-Methods (default: `*`)
* @property {string} [allowHeaders="*"] Access-Control-Allow-Headers (default: `*`) * @property {string} [allowHeaders="*"] Access-Control-Allow-Headers (default: `*`)
* @property {boolean} [allowCredentials="true"] Access-Control-Allow-Credentials (default: undefined)
* @property {string} [vary="origin"] vary (default: undefined)
* @property {number} [maxAge=86400] Access-Control-Max-Age (default: `86400`) * @property {number} [maxAge=86400] Access-Control-Max-Age (default: `86400`)
* @property {number} [optionsSuccessStatus=204] Return status code for OPTIONS request (default: `204`) * @property {number} [optionsSuccessStatus=204] Return status code for OPTIONS request (default: `204`)
*/ */
@@ -98,6 +100,8 @@ export type RouterCorsConfig = {
allowOrigin?: string allowOrigin?: string
allowMethods?: string allowMethods?: string
allowHeaders?: string allowHeaders?: string
allowCredentials?: boolean
vary?: string
maxAge?: number maxAge?: number
optionsSuccessStatus?: number optionsSuccessStatus?: number
} }
@@ -150,14 +154,6 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
*/ */
protected corsConfig: RouterCorsConfig = {} protected corsConfig: RouterCorsConfig = {}
/**
* Buffer
*
* @protected
* @type {RouterBuffer}
*/
protected buffer: RouterBuffer = {}
/** /**
* CORS enabled * CORS enabled
* *
@@ -312,6 +308,8 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
allowOrigin: config?.allowOrigin ?? '*', allowOrigin: config?.allowOrigin ?? '*',
allowMethods: config?.allowMethods ?? '*', allowMethods: config?.allowMethods ?? '*',
allowHeaders: config?.allowHeaders ?? '*', allowHeaders: config?.allowHeaders ?? '*',
allowCredentials: config?.allowCredentials ?? undefined,
vary: config?.vary ?? undefined,
maxAge: config?.maxAge ?? 86400, maxAge: config?.maxAge ?? 86400,
optionsSuccessStatus: config?.optionsSuccessStatus ?? 204 optionsSuccessStatus: config?.optionsSuccessStatus ?? 204
} }
@@ -325,6 +323,10 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods) headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers')) if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers'))
headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders) headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
if (this.corsConfig.allowCredentials && !headers.has('Access-Control-Allow-Credentials'))
headers.set('Access-Control-Allow-Credentials', this.corsConfig.allowCredentials.toString())
if (this.corsConfig.vary && !headers.has('vary'))
headers.set('vary', this.corsConfig.vary.toString())
if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age')) if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age'))
headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString()) headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
return headers return headers
@@ -400,6 +402,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
* @returns {Promise<Response>} * @returns {Promise<Response>}
*/ */
public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, extCtx?: TCtx, extReq?: TReq): Promise<Response> { public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, extCtx?: TCtx, extReq?: TReq): Promise<Response> {
const buffer: RouterBuffer = {};
const req = { const req = {
...(extReq ?? {}), ...(extReq ?? {}),
method: request.method, method: request.method,
@@ -409,11 +412,11 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
raw: request, raw: request,
params: {}, params: {},
query: {}, query: {},
arrayBuffer: async (): Promise<ArrayBuffer> => this.buffer.arrayBuffer ? this.buffer.arrayBuffer : this.buffer.arrayBuffer = await request.arrayBuffer(), arrayBuffer: async (): Promise<ArrayBuffer> => buffer.arrayBuffer ? buffer.arrayBuffer : buffer.arrayBuffer = await request.arrayBuffer(),
text: async (): Promise<string> => this.buffer.text ? this.buffer.text : this.buffer.text = await request.text(), text: async (): Promise<string> => buffer.text ? buffer.text : buffer.text = await request.text(),
json: async <T>(): Promise<T> => this.buffer.json ? this.buffer.json : this.buffer.json = await request.json<T>(), json: async <T>(): Promise<T> => buffer.json ? buffer.json : buffer.json = await request.json<T>(),
formData: async (): Promise<FormData> => this.buffer.formData ? this.buffer.formData : this.buffer.formData = await request.formData(), formData: async (): Promise<FormData> => buffer.formData ? buffer.formData : buffer.formData = await request.formData(),
blob: async (): Promise<Blob> => this.buffer.blob ? this.buffer.blob : this.buffer.blob = await request.blob(), blob: async (): Promise<Blob> => buffer.blob ? buffer.blob : buffer.blob = await request.blob(),
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim() bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
} as RouterRequest<TReq> } as RouterRequest<TReq>