/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/whisper-node-server@1.0.0/dist/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var s=Object.getOwnPropertyDescriptor(r,t);s&&!("get"in s?!r.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,s)}:function(e,r,t,o){void 0===o&&(o=t),e[o]=r[t]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,r){Object.defineProperty(e,"default",{enumerable:!0,value:r})}:function(e,r){e.default=r}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(null!=e)for(var t in e)"default"!==t&&Object.prototype.hasOwnProperty.call(e,t)&&__createBinding(r,e,t);return __setModuleDefault(r,e),r},__awaiter=this&&this.__awaiter||function(e,r,t,o){return new(t||(t=Promise))((function(s,n){function i(e){try{a(o.next(e))}catch(e){n(e)}}function l(e){try{a(o.throw(e))}catch(e){n(e)}}function a(e){var r;e.done?s(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(i,l)}a((o=o.apply(e,r||[])).next())}))},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.whisper=exports.whisperCleanup=exports.whisperInit=void 0;const path_1=__importDefault(require("path")),shell_1=__importStar(require("./shell")),whisper_1=require("./whisper"),tsToArray_1=__importDefault(require("./tsToArray")),node_fetch_1=__importDefault(require("node-fetch")),form_data_1=__importDefault(require("form-data")),fs_1=__importDefault(require("fs"));let serverState={isRunning:!1,process:null,port:8080};function setupCleanupHandlers(){const e=()=>__awaiter(this,void 0,void 0,(function*(){yield whisperCleanup(),process.exit(0)}));process.on("SIGINT",e),process.on("SIGTERM",e),process.on("SIGHUP",e),"win32"===process.platform&&process.on("SIGBREAK",e),process.on("uncaughtException",(r=>__awaiter(this,void 0,void 0,(function*(){console.error("[whisper-node-server] Uncaught exception:",r),yield e()}))))}function whisperInit(e){return __awaiter(this,void 0,void 0,(function*(){if(serverState.isRunning)console.log("[whisper-node-server] Server already running on port",serverState.port);else try{yield(0,shell_1.initializeWhisperCpp)();const r=(0,whisper_1.createCppCommand)({modelName:null==e?void 0:e.modelName,modelPath:null==e?void 0:e.modelPath,options:null==e?void 0:e.whisperOptions,isServer:!0});console.log("[whisper-node-server] Starting server with command:",r),serverState.process=(0,shell_1.default)(r,null==e?void 0:e.shellOptions),serverState.modelName=null==e?void 0:e.modelName,serverState.modelPath=null==e?void 0:e.modelPath,serverState.process&&(serverState.process.on("error",(e=>{console.error("[whisper-node-server] Server process error:",e),serverState.isRunning=!1})),serverState.process.on("exit",((e,r)=>{console.log("[whisper-node-server] Server process exited with code:",e,"signal:",r),serverState.isRunning=!1,serverState.process=null}))),yield new Promise((e=>setTimeout(e,2e3))),serverState.isRunning=!0,setupCleanupHandlers(),console.log("[whisper-node-server] Server started successfully on port",serverState.port)}catch(e){throw console.error("[whisper-node-server] Failed to start server:",e),e}}))}function whisperCleanup(){return __awaiter(this,void 0,void 0,(function*(){if(serverState.process){console.log("[whisper-node-server] Shutting down server...");try{serverState.process.kill("SIGTERM"),yield new Promise(((e,r)=>{const t=setTimeout((()=>{if(serverState.process){console.log("[whisper-node-server] Force killing server process...");try{serverState.process.kill("SIGKILL")}catch(e){console.error("[whisper-node-server] Error during force kill:",e)}}e()}),5e3);serverState.process.once("exit",(()=>{clearTimeout(t),e()})),serverState.process.once("error",(e=>{clearTimeout(t),r(e)}))}))}catch(e){console.error("[whisper-node-server] Error during server shutdown:",e)}finally{serverState.process=null,serverState.isRunning=!1,console.log("[whisper-node-server] Server shutdown complete")}}}))}exports.whisperInit=whisperInit,exports.whisperCleanup=whisperCleanup;const whisper=(e,r)=>__awaiter(void 0,void 0,void 0,(function*(){try{if(console.log("[whisper-node-server] Transcribing:",e,"\n"),serverState.isRunning){const t=(null==r?void 0:r.serverUrl)||`http://localhost:${serverState.port}/inference`,o=new form_data_1.default;o.append("file",fs_1.default.createReadStream(e)),o.append("response_format","verbose_json"),(null==r?void 0:r.whisperOptions)&&Object.entries(r.whisperOptions).forEach((([e,r])=>{const t="boolean"==typeof r?String(r).toLowerCase():String(r);o.append(e,t)})),console.log("Sending request with options:",Object.assign({response_format:"verbose_json"},null==r?void 0:r.whisperOptions));const s=yield(0,node_fetch_1.default)(t,{method:"POST",body:o});if(!s.ok){const e=yield s.text();throw console.error("Server response:",e),new Error(`Server error: ${s.statusText}`)}const n=yield s.json();return(0,tsToArray_1.default)(n)}{const t=(0,whisper_1.createCppCommand)({filePath:path_1.default.normalize(`"${e}"`),modelName:null==r?void 0:r.modelName,modelPath:(null==r?void 0:r.modelPath)?`"${null==r?void 0:r.modelPath}"`:void 0,options:null==r?void 0:r.whisperOptions}),o=yield(0,shell_1.shellExec)(t,null==r?void 0:r.shellOptions);return(0,tsToArray_1.default)(o)}}catch(e){throw console.error("[whisper-node-server] Error:",e),e}}));exports.whisper=whisper,exports.default=exports.whisper;
//# sourceMappingURL=/sm/a37dde774360298acd5faecec24d4c7022dc1c850619b264816403bb5ea759f5.map