Bun 開箱即用地支持 TLS,由 BoringSSL 提供支持。通過為 key 和 cert 傳遞值來啟用 TLS;兩者都需要啟用 TLS。
ts
Bun.serve({
tls: {
key: Bun.file("./key.pem"),
cert: Bun.file("./cert.pem"),
},
});key 和 cert 字段期望你的 TLS 密鑰和證書的_內容_,而不是路徑。這可以是字符串、BunFile、TypedArray 或 Buffer。
ts
Bun.serve({
tls: {
key: Bun.file("./key.pem"), // BunFile
key: fs.readFileSync("./key.pem"), // Buffer
key: fs.readFileSync("./key.pem", "utf8"), // 字符串
key: [Bun.file("./key1.pem"), Bun.file("./key2.pem")], // 上述數組
},
});密碼短語
如果你的私鑰使用密碼短語加密,提供 passphrase 值來解密它。
ts
Bun.serve({
tls: {
key: Bun.file("./key.pem"),
cert: Bun.file("./cert.pem"),
passphrase: "my-secret-passphrase",
},
});CA 證書
可選地,你可以通過為 ca 傳遞值來覆蓋受信任的 CA 證書。默認情況下,服務器將信任由 Mozilla 策劃的知名 CA 列表。當指定 ca 時,Mozilla 列表會被覆蓋。
ts
Bun.serve({
tls: {
key: Bun.file("./key.pem"), // TLS 密鑰路徑
cert: Bun.file("./cert.pem"), // TLS 證書路徑
ca: Bun.file("./ca.pem"), // 根 CA 證書路徑
},
});Diffie-Hellman
要覆蓋 Diffie-Hellman 參數:
ts
Bun.serve({
tls: {
dhParamsFile: "/path/to/dhparams.pem", // Diffie-Hellman 參數路徑
},
});服務器名稱指示 (SNI)
要配置服務器的服務器名稱指示 (SNI),在 tls 對象中設置 serverName 字段。
ts
Bun.serve({
tls: {
serverName: "my-server.com", // SNI
},
});要允許多個服務器名稱,傳遞對象數組給 tls,每個對象都有 serverName 字段。
ts
Bun.serve({
tls: [
{
key: Bun.file("./key1.pem"),
cert: Bun.file("./cert1.pem"),
serverName: "my-server1.com",
},
{
key: Bun.file("./key2.pem"),
cert: Bun.file("./cert2.pem"),
serverName: "my-server2.com",
},
],
});