這個輔助模組使用 ES modules。請在 package.json 裡設定 "type": "module":
npm pkg set type=module
// cabby.js
import{createHmac}from"node:crypto"constSECRET=process.env.CABBY_SECRET// same value as --secret
constBROADCAST_KEY=process.env.CABBY_BROADCAST_KEY// same value as --broadcast_key
constBROADCAST_URL=process.env.CABBY_BROADCAST_URL??"http://localhost:8080/_broadcast"exportfunctionsignStream(name){constencoded=Buffer.from(JSON.stringify(name)).toString("base64")constdigest=createHmac("sha256",SECRET).update(encoded).digest("hex")return`${encoded}--${digest}`}exportasyncfunctionbroadcast(stream,data){constresponse=awaitfetch(BROADCAST_URL,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${BROADCAST_KEY}`},body:JSON.stringify({stream,data:JSON.stringify(data)}),})if(!response.ok)thrownewError(`Cabby broadcast failed: ${response.status}`)}