Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
08c329ff20
|
|||
|
e9a0a2436d
|
|||
|
76d7040a1b
|
|||
|
0037fb64ea
|
|||
|
|
a5ccf0fc01 | ||
|
|
e006d0a3b1 | ||
|
|
c7d7a642e6 | ||
|
|
49c7897bd0 | ||
|
e28976a4b6
|
|||
|
eea9e580f6
|
|||
|
68e614b3fa
|
|||
|
7a20454f79
|
@@ -1,4 +1,4 @@
|
|||||||
name: Publish (pre)
|
name: Publish (main)
|
||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
@@ -11,7 +11,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v3
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 'latest'
|
node-version: latest
|
||||||
registry-url: https://registry.npmjs.org/
|
registry-url: https://registry.npmjs.org/
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
@@ -20,12 +20,12 @@ jobs:
|
|||||||
- name: Publish to npmjs
|
- name: Publish to npmjs
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
||||||
run: npm publish --tag pre --access public
|
run: npm publish --tag latest --access public
|
||||||
- uses: actions/setup-node@v3
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 'latest'
|
node-version: latest
|
||||||
registry-url: https://npm.pkg.github.com/
|
registry-url: https://npm.pkg.github.com/
|
||||||
- name: Publish to GPR
|
- name: Publish to GPR
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
run: npm publish --tag pre --access public
|
run: npm publish --tag latest --access public
|
||||||
@@ -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
|
||||||
|
|||||||
16
README.md
16
README.md
@@ -210,13 +210,15 @@ 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` | `*`
|
||||||
`maxAge` | `integer` | `86400`
|
`allowCredentials` | `boolean` | `undefined`
|
||||||
`optionsSuccessStatus` | `integer` | `204`
|
`vary` | `string` | `undefined`
|
||||||
|
`maxAge` | `integer` | `86400`
|
||||||
|
`optionsSuccessStatus` | `integer` | `204`
|
||||||
|
|
||||||
|
|
||||||
### Supported Methods
|
### Supported Methods
|
||||||
|
|||||||
22
package-lock.json
generated
22
package-lock.json
generated
@@ -1,35 +1,35 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.0.0-16",
|
"version": "3.1.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.0.0-16",
|
"version": "3.1.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20230518.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
"typescript": "^5.0.4"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@cloudflare/workers-types": {
|
"node_modules/@cloudflare/workers-types": {
|
||||||
"version": "4.20230518.0",
|
"version": "4.20231025.0",
|
||||||
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230518.0.tgz",
|
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20231025.0.tgz",
|
||||||
"integrity": "sha512-A0w1V+5SUawGaaPRlhFhSC/SCDT9oQG8TMoWOKFLA4qbqagELqEAFD4KySBIkeVOvCBLT1DZSYBMCxbXddl0kw==",
|
"integrity": "sha512-TkcZkntUTOcvJ4vgmwpNfLTclpMbmbClZCe62B25/VTukmyv91joRa4eKzSjzCZUXTbFHNmVdOpmGaaJU2U3+A==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/typescript": {
|
"node_modules/typescript": {
|
||||||
"version": "5.0.4",
|
"version": "5.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz",
|
||||||
"integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==",
|
"integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"bin": {
|
"bin": {
|
||||||
"tsc": "bin/tsc",
|
"tsc": "bin/tsc",
|
||||||
"tsserver": "bin/tsserver"
|
"tsserver": "bin/tsserver"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=12.20"
|
"node": ">=14.17"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.0.0-16",
|
"version": "3.1.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20230518.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
"typescript": "^5.0.4"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
src/index.ts
31
src/index.ts
@@ -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
|
||||||
@@ -390,7 +392,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
}) || this.routes.find(r => r.url === '*' && [request.method, '*'].includes(r.method))
|
}) || this.routes.find(r => r.url === '*' && [request.method, '*'].includes(r.method))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle requests
|
* Handle requests
|
||||||
*
|
*
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
@@ -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.clone().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.clone().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.clone().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.clone().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.clone().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>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user