Skip to content

Commit 247a527

Browse files
committed
feat(adapter-fastify): 🚀 添加 Fastify 适配器及其示例项目
1 parent f55b6ee commit 247a527

8 files changed

Lines changed: 170 additions & 0 deletions

File tree

examples/fastify/.gitignore

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

examples/fastify/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-disable unused-imports/no-unused-vars */
2+
/* eslint-disable no-console */
3+
import { FastifyHttpsAdapter } from '@https-enable/adapter-fastify'
4+
import { HttpsEnabler } from '@https-enable/core'
5+
import Fastify from 'fastify'
6+
7+
const fastify = Fastify({
8+
logger: true,
9+
})
10+
11+
const HOST = '127.0.0.1'
12+
const PORT = 2333
13+
14+
fastify.get('/', async (request, reply) => {
15+
return { hello: 'world' }
16+
})
17+
18+
const adapter = new FastifyHttpsAdapter(fastify)
19+
const enabler = new HttpsEnabler({
20+
adapter,
21+
options: { host: HOST, port: PORT },
22+
certificateOptions: { validity: 1, domains: HOST, base: 'cert' },
23+
})
24+
25+
enabler.startServer().then((res) => {
26+
console.log(`Server running in http://${res.host}:${res.port}`)
27+
})
28+
29+
enabler.on('error', (err) => {
30+
console.log('error:', err)
31+
})
32+
33+
enabler.on('cert-renewed', (res) => {
34+
console.log('cert-renewed:', res)
35+
})

examples/fastify/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "fastify-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-fastify": "workspace:*",
11+
"@https-enable/core": "workspace:*",
12+
"fastify": "^5.2.1"
13+
}
14+
}
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+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@https-enable/adapter-fastify",
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+
"fastify": "*"
30+
},
31+
"dependencies": {
32+
"@https-enable/mkcert": "workspace:*"
33+
},
34+
"devDependencies": {
35+
"@https-enable/tsconfig": "workspace:*",
36+
"@https-enable/types": "workspace:*",
37+
"@types/node": "^22.13.1",
38+
"unbuild": "^3.3.1"
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ServerOptions } from '@https-enable/core'
2+
import type { Certificate } from '@https-enable/mkcert'
3+
import type { FastifyInstance } from 'fastify'
4+
import { HttpsAdapter } from '@https-enable/core'
5+
6+
type FastifyApp = FastifyInstance['routing']
7+
8+
export class FastifyHttpsAdapter extends HttpsAdapter<FastifyApp> {
9+
constructor(public fastifyInstance: FastifyInstance) {
10+
super()
11+
}
12+
13+
init = async () => {
14+
await this.fastifyInstance.ready()
15+
return this.fastifyInstance.routing
16+
}
17+
18+
createMiddleware?: ((options: ServerOptions) => any) | undefined
19+
onCertRenewed?: ((certificate: Certificate) => any) | undefined
20+
}
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+
}

pnpm-lock.yaml

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)