Compare commits
25 Commits
v3.2.2
...
ba67b34a0d
| Author | SHA1 | Date | |
|---|---|---|---|
|
ba67b34a0d
|
|||
|
2d5bdfe40e
|
|||
|
7bb935fa53
|
|||
|
49d5806d89
|
|||
|
73a0d4a4f9
|
|||
|
24a85e9814
|
|||
|
dfdc50ba8a
|
|||
|
67bba2a339
|
|||
|
c21e91bcb6
|
|||
|
fe2f8ec227
|
|||
|
0e78ec4787
|
|||
|
fd7dce7256
|
|||
|
1d72a15ca6
|
|||
|
a740f1b103
|
|||
|
21dc38b676
|
|||
|
9df19a50cd
|
|||
|
7dce99214d
|
|||
|
5aedc0248d
|
|||
|
3aecc4ca5f
|
|||
|
e04bcfb395
|
|||
|
f2ba5f5bc5
|
|||
|
306c046adb
|
|||
|
2dab64be19
|
|||
|
175deddc9c
|
|||
|
ff638ad04f
|
@@ -11,3 +11,7 @@ indent_style = tab
|
|||||||
[README.md]
|
[README.md]
|
||||||
indent_style = space
|
indent_style = space
|
||||||
indent_size = 4
|
indent_size = 4
|
||||||
|
|
||||||
|
[{package,package-lock}.json]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
name: Publish (main)
|
name: Publish
|
||||||
|
run-name: Publish ${{ github.ref_name }}
|
||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
@@ -13,19 +14,27 @@ jobs:
|
|||||||
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
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: npm test
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: npm run build
|
run: npm run build
|
||||||
|
|
||||||
- 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 latest --access public
|
run: npm publish --tag ${{ contains(github.ref_name, '-') && 'pre' || '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 latest --access public
|
run: npm publish --tag ${{ contains(github.ref_name, '-') && 'pre' || 'latest' }} --access public
|
||||||
31
.github/workflows/release.yml
vendored
Normal file
31
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: Release
|
||||||
|
run-name: Release ${{ github.ref_name }}
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: latest
|
||||||
|
registry-url: https://registry.npmjs.org/
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: npm test
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: npm run build
|
||||||
|
|
||||||
|
- name: Create release
|
||||||
|
run: gh release create ${{ github.ref_name }} --draft --generate-notes ${{ contains(github.ref_name, '-') && '--prerelease --latest=false' || '--latest' }}
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
26
.github/workflows/test.yml
vendored
Normal file
26
.github/workflows/test.yml
vendored
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
name: Test
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
run:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: latest
|
||||||
|
registry-url: https://registry.npmjs.org/
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: npm test
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: npm run build
|
||||||
15
.npmignore
15
.npmignore
@@ -1,7 +1,10 @@
|
|||||||
.github/
|
|
||||||
src/
|
|
||||||
test/
|
|
||||||
.nvmrc
|
|
||||||
MIGRATION.md
|
|
||||||
tsconfig.json
|
|
||||||
.editorconfig
|
.editorconfig
|
||||||
|
.github/
|
||||||
|
.gitignore
|
||||||
|
.nvmrc
|
||||||
|
coverage/
|
||||||
|
jest.config.ts
|
||||||
|
MIGRATION.md
|
||||||
|
src/
|
||||||
|
tests/
|
||||||
|
tsconfig.json
|
||||||
@@ -4,8 +4,6 @@ Cloudflare Workers Router is a super lightweight router (1.0K gzipped) with midd
|
|||||||
|
|
||||||
When I was trying out Cloudflare Workers I almost immediately noticed how fast it was compared to other serverless offerings. So I wanted to build a full-fledged API to see how it performs doing real work, but since I wasn't able to find a router that suited my needs I created my own.
|
When I was trying out Cloudflare Workers I almost immediately noticed how fast it was compared to other serverless offerings. So I wanted to build a full-fledged API to see how it performs doing real work, but since I wasn't able to find a router that suited my needs I created my own.
|
||||||
|
|
||||||
I worked a lot with [Express.js](https://expressjs.com/) in the past and really enjoyed their middleware approach, but since none of the available Cloudflare Worker routers offered middleware support at the time, I felt the need to create this router.
|
|
||||||
|
|
||||||
|
|
||||||
## Contents
|
## Contents
|
||||||
|
|
||||||
@@ -20,8 +18,9 @@ I worked a lot with [Express.js](https://expressjs.com/) in the past and really
|
|||||||
- ZERO dependencies
|
- ZERO dependencies
|
||||||
- Lightweight (1.0K gzipped)
|
- Lightweight (1.0K gzipped)
|
||||||
- Fully written in TypeScript
|
- Fully written in TypeScript
|
||||||
- Integrated Debug-Mode & CORS helper
|
|
||||||
- Built specifically around Middlewares
|
- Built specifically around Middlewares
|
||||||
|
- Debug-Mode, CORS and Bearer helpers
|
||||||
|
- Allows accessing request body multiple times
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -294,7 +293,7 @@ wrangler init <name>
|
|||||||
Use of TypeScript is strongly encouraged :)
|
Use of TypeScript is strongly encouraged :)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm i -D @tsndr/cloudflare-worker-router
|
npm i @tsndr/cloudflare-worker-router
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import type { Config } from 'jest'
|
|
||||||
|
|
||||||
const config: Config = {
|
|
||||||
preset: 'ts-jest',
|
|
||||||
testEnvironment: 'node',
|
|
||||||
verbose: true
|
|
||||||
}
|
|
||||||
|
|
||||||
export default config
|
|
||||||
4744
package-lock.json
generated
4744
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
21
package.json
21
package.json
@@ -1,23 +1,23 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.2.2",
|
"version": "3.2.9",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"type": "module",
|
||||||
|
"exports": "./index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"test": "jest"
|
"test": "vitest"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/tsndr/cloudflare-worker-router.git"
|
"url": "git+https://github.com/tsndr/cloudflare-worker-router.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
"api",
|
||||||
"cloudflare",
|
"cloudflare",
|
||||||
"cloudflare-worker",
|
"cloudflare-worker",
|
||||||
"cloudflare-workers",
|
"cloudflare-workers",
|
||||||
"express",
|
|
||||||
"expressjs",
|
|
||||||
"framework",
|
"framework",
|
||||||
"middleware",
|
"middleware",
|
||||||
"router",
|
"router",
|
||||||
@@ -32,12 +32,9 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20231025.0",
|
"@cloudflare/workers-types": "^4.20241022.0",
|
||||||
"@jest/globals": "^29.7.0",
|
"@edge-runtime/vm": "^4.0.3",
|
||||||
"@types/jest": "^29.5.11",
|
"typescript": "^5.6.3",
|
||||||
"jest": "^29.7.0",
|
"vitest": "^2.1.3"
|
||||||
"ts-jest": "^29.1.1",
|
|
||||||
"ts-node": "^10.9.2",
|
|
||||||
"typescript": "^5.2.2"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/index.ts
17
src/index.ts
@@ -406,21 +406,23 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const route = this.getRoute(req)
|
const route = this.getRoute(req)
|
||||||
|
let response: Response | undefined
|
||||||
|
|
||||||
if (!route)
|
if (!route)
|
||||||
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
|
response = new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
|
||||||
|
|
||||||
const handlers = [...this.globalHandlers, ...route.handlers]
|
if (!response) {
|
||||||
const dbg = this.debugMode
|
const handlers = [
|
||||||
|
...this.globalHandlers,
|
||||||
let response: Response | undefined
|
...(route?.handlers ?? [])
|
||||||
|
]
|
||||||
|
|
||||||
for (const handler of handlers) {
|
for (const handler of handlers) {
|
||||||
const context = {
|
const context = {
|
||||||
...(ctxExt ?? {}),
|
...(ctxExt ?? {}),
|
||||||
env,
|
env,
|
||||||
req,
|
req,
|
||||||
dbg,
|
dbg: this.debugMode,
|
||||||
ctx
|
ctx
|
||||||
} as RouterContext<Env, CtxExt, ReqExt>
|
} as RouterContext<Env, CtxExt, ReqExt>
|
||||||
|
|
||||||
@@ -431,9 +433,10 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!response)
|
if (!response)
|
||||||
return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
|
response = new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
|
||||||
|
|
||||||
if (this.corsEnabled) {
|
if (this.corsEnabled) {
|
||||||
response = new Response(response.body, response)
|
response = new Response(response.body, response)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, test } from '@jest/globals'
|
import { describe, expect, test } from 'vitest'
|
||||||
import { Router } from '..'
|
import { Router } from '../src/index'
|
||||||
|
|
||||||
describe('Router', () => {
|
describe('Router', () => {
|
||||||
test('use', async () => {
|
test('use', async () => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": ".",
|
"outDir": ".",
|
||||||
"module": "commonjs",
|
"module": "esnext",
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"lib": ["esnext"],
|
"lib": ["esnext"],
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
|||||||
9
vite.config.ts
Normal file
9
vite.config.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { defineConfig } from 'vitest/config'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
environment: 'edge-runtime',
|
||||||
|
reporters: ['verbose'],
|
||||||
|
watch: false
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user