1
0

17 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
eea9e580f6 3.0.0 2023-08-19 18:10:11 +02:00
68e614b3fa prepare workflow for release 2023-08-19 18:00:13 +02:00
7a20454f79 update dependencies 2023-08-19 17:55:53 +02:00
101de4f5f6 3.0.0-16 2023-05-25 20:38:48 +02:00
9ee1182fa9 update node for dev 2023-05-25 20:37:41 +02:00
0309b0131f update migration guide 2023-05-25 20:34:55 +02:00
afd7ea662f 3.0.0-15 2023-05-25 20:01:45 +02:00
a742753cb7 update workflow 2023-05-25 20:01:36 +02:00
d631645b99 3.0.0-14 2023-05-25 19:55:37 +02:00
cb1bf98053 update types 2023-05-25 19:55:25 +02:00
7 changed files with 48 additions and 58 deletions

View File

@@ -1,4 +1,4 @@
name: Publish (pre)
name: Publish (main)
on:
release:
@@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: latest
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: npm ci
@@ -20,11 +20,12 @@ jobs:
- name: Publish to npmjs
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: npm publish --tag pre --access public
- uses: actions/setup-node@v1
run: npm publish --tag latest --access public
- uses: actions/setup-node@v3
with:
node-version: latest
registry-url: https://npm.pkg.github.com/
- name: Publish to GPR
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: npm publish --tag pre --access public
run: npm publish --tag latest --access public

1
.nvmrc
View File

@@ -1 +0,0 @@
v16

View File

@@ -7,7 +7,6 @@ From `v2.x.x` to `v3.x.x`.
- [Update Router](#update-router)
- [Handlers](#handlers)
- [Fetch](#fetch--routerhandle)
## Update Router
@@ -15,7 +14,7 @@ From `v2.x.x` to `v3.x.x`.
Update to the latest version version of the router.
```bash
npm i -D @tsndr/cloudflare-worker-router
npm i -D @tsndr/cloudflare-worker-router@^3
```

View File

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

38
package-lock.json generated
View File

@@ -1,50 +1,36 @@
{
"name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0-13",
"lockfileVersion": 2,
"version": "3.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0-13",
"version": "3.1.0",
"license": "MIT",
"devDependencies": {
"@cloudflare/workers-types": "^4.20230115.0",
"typescript": "^4.9.5"
"@cloudflare/workers-types": "^4.20230814.0",
"typescript": "^5.1.6"
}
},
"node_modules/@cloudflare/workers-types": {
"version": "4.20230115.0",
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230115.0.tgz",
"integrity": "sha512-GPJEiO8AFN+jUpA+DHJ1qdVmk4s/hq8JYKjOV/+U7avGquQbVnj905+Kg6uAEfrq16muwmRKl+XJGqsvlBlDNg==",
"version": "4.20230814.0",
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230814.0.tgz",
"integrity": "sha512-+jHiGjZg2UpULZSSHmHLqUG45TLg1s+uppSMlGvMn0u/xyFsRX9HX6b8Ydg/oHSp3jfSuPtX05GSvtgRAmrWTg==",
"dev": true
},
"node_modules/typescript": {
"version": "4.9.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz",
"integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==",
"version": "5.1.6",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz",
"integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==",
"dev": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=4.2.0"
"node": ">=14.17"
}
}
},
"dependencies": {
"@cloudflare/workers-types": {
"version": "4.20230115.0",
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230115.0.tgz",
"integrity": "sha512-GPJEiO8AFN+jUpA+DHJ1qdVmk4s/hq8JYKjOV/+U7avGquQbVnj905+Kg6uAEfrq16muwmRKl+XJGqsvlBlDNg==",
"dev": true
},
"typescript": {
"version": "4.9.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz",
"integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==",
"dev": true
}
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0-13",
"version": "3.1.0",
"description": "",
"main": "index.js",
"types": "index.d.ts",
@@ -31,7 +31,7 @@
},
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
"devDependencies": {
"@cloudflare/workers-types": "^4.20230307.0",
"typescript": "^4.9.5"
"@cloudflare/workers-types": "^4.20230814.0",
"typescript": "^5.1.6"
}
}

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