Skip to content

Commit 0f0de86

Browse files
committed
feat(adapter-koa): 🚀 添加 Koa 适配器及其示例项目
1 parent 247a527 commit 0f0de86

File tree

8 files changed

+413
-11
lines changed

8 files changed

+413
-11
lines changed

examples/koa/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cert

examples/koa/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint-disable no-console */
2+
import { KoaHttpsAdapter } from '@https-enable/adapter-koa'
3+
import { HttpsEnabler } from '@https-enable/core'
4+
import Koa from 'koa'
5+
6+
const app = new Koa()
7+
8+
const HOST = '127.0.0.1'
9+
const PORT = 2333
10+
11+
app.use(async (ctx) => {
12+
ctx.body = 'Hello World'
13+
})
14+
15+
const adapter = new KoaHttpsAdapter(app)
16+
const enabler = new HttpsEnabler({
17+
adapter,
18+
options: { host: HOST, port: PORT },
19+
certificateOptions: { validity: 1, domains: HOST, base: 'cert' },
20+
})
21+
22+
enabler.startServer().then((res) => {
23+
console.log(`Server running in http://${res.host}:${res.port}`)
24+
})
25+
26+
enabler.on('error', (err) => {
27+
console.log('error:', err)
28+
})
29+
30+
enabler.on('cert-renewed', (res) => {
31+
console.log('cert-renewed:', res)
32+
})

examples/koa/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "koa-demo",
3+
"type": "module",
4+
"private": true,
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node index.js"
8+
},
9+
"dependencies": {
10+
"@https-enable/adapter-koa": "workspace:*",
11+
"@https-enable/core": "workspace:*",
12+
"koa": "^2.16.0"
13+
},
14+
"devDependencies": {
15+
"@types/koa": "^2.15.0"
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
entries: ['src/index'],
5+
declaration: true,
6+
clean: true,
7+
rollup: {
8+
emitCJS: true,
9+
},
10+
failOnWarn: false,
11+
externals: [],
12+
})

packages/adapters/koa/package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "@https-enable/adapter-koa",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"exports": {
6+
".": {
7+
"import": "./dist/index.mjs",
8+
"require": "./dist/index.cjs"
9+
}
10+
},
11+
"main": "dist/index.cjs",
12+
"module": "dist/index.mjs",
13+
"types": "dist/index.d.ts",
14+
"files": [
15+
"dist"
16+
],
17+
"publishConfig": {
18+
"access": "public",
19+
"registry": "https://registry.npmjs.org/"
20+
},
21+
"scripts": {
22+
"build": "unbuild",
23+
"dev": "unbuild --stub",
24+
"release": "npm run build && bumpp",
25+
"prepublishOnly": "npm run build"
26+
},
27+
"peerDependencies": {
28+
"@https-enable/core": "workspace:*",
29+
"koa": "*"
30+
},
31+
"dependencies": {
32+
"@https-enable/mkcert": "workspace:*"
33+
},
34+
"devDependencies": {
35+
"@https-enable/tsconfig": "workspace:*",
36+
"@https-enable/types": "workspace:*",
37+
"@types/koa": "^2.15.0",
38+
"@types/node": "^22.13.1",
39+
"unbuild": "^3.3.1"
40+
}
41+
}

packages/adapters/koa/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { ServerOptions } from '@https-enable/core'
2+
import type { Certificate } from '@https-enable/mkcert'
3+
import type Koa from 'koa'
4+
import { HttpsAdapter } from '@https-enable/core'
5+
6+
type KoaApp = Koa<Koa.DefaultState, Koa.DefaultContext>
7+
type Callback = ReturnType<KoaApp['callback']>
8+
9+
export class KoaHttpsAdapter extends HttpsAdapter<Callback> {
10+
constructor(app: KoaApp) {
11+
super()
12+
this.app = app.callback()
13+
}
14+
15+
init?: () => Promise<Callback>
16+
createMiddleware?: (options: ServerOptions) => any
17+
onCertRenewed?: (certificate: Certificate) => any
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@https-enable/tsconfig/tsconfig.node.json",
3+
"compilerOptions": {
4+
"incremental": false,
5+
"composite": false,
6+
"module": "ESNext",
7+
"moduleResolution": "Bundler"
8+
},
9+
"include": ["src/**/*.ts", "src/**/*.d.ts"],
10+
"exclude": ["dist", "build", "node_modules"]
11+
}

0 commit comments

Comments
 (0)