1 line
3.5 KiB
JavaScript
1 line
3.5 KiB
JavaScript
|
|
"use strict";function e(e,t,n){var s={token:e,timestamp:t,status:n,moreinfo:[]};return s}function t(e){var t=.2,s=[{name:"/pau/",start:0,end:t}];return e.split(" ").forEach(function(e){e=e.trim(),n(e[e.length-1])&&(e=e.substr(0,e.length-1)),s.push({name:e,start:t,end:t+.3}),t+=.3}),s.push({name:"/pau/",start:t,end:t+.2}),s}function n(e){return"."===e||","===e||"?"===e||"!"===e||";"===e||":"===e}var s=function(e){return e&&e.__esModule?e["default"]:e},r=function(){function e(e,t){for(var n in t){var s=t[n];s.configurable=!0,s.value&&(s.writable=!0)}Object.defineProperties(e,t)}return function(t,n,s){return n&&e(t.prototype,n),s&&e(t,s),t}}(),o=function f(e,t,n){var s=Object.getOwnPropertyDescriptor(e,t);if(void 0===s){var r=Object.getPrototypeOf(e);return null===r?void 0:f(r,t,n)}if("value"in s&&s.writable)return s.value;var o=s.get;if(void 0!==o)return o.call(n)},i=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(e.__proto__=t)},a=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},u=s(require("http")),c=require("events").EventEmitter,p=require("websocket").server,l=function(n){function s(){a(this,s),o(Object.getPrototypeOf(s.prototype),"constructor",this).call(this),this.port=11275,this.messages=[],this.connectionHandlers={"/tts_token_times":this.onTokenTimes.bind(this),"/tts_speak":this.onSpeak.bind(this),"/tts_stop":this.onStop.bind(this)}}return i(s,n),r(s,{init:{value:function(e){var t=this;this.httpServer=u.createServer(),this.httpServer.on("request",function(e,n){"POST"===e.method&&(e.setEncoding("utf8"),e.on("data",function(s){var r=void 0;try{for(r=JSON.parse(s),t.messages.push({url:e.url,data:r,response:n});t.messages.length;){var o=t.messages.shift();t.connectionHandlers[o.url](o.data,o.response)}}catch(i){return console.error(i),t.messages.push({url:e.url,data:"ERROR Invalid request. Request body is not valid JSON",response:n}),n.writeHead(200,{"Content-Type":"text/html"}),n.write(JSON.stringify({Status:"ERROR",Message:"Inavlid request. Request body is not valid JSON"})),t.socket&&t.socket.send(JSON.stringify({status:"error",message:"ERROR Invalid request. Request body is not valid JSON"})),void n.end()}t.socket&&t.processMessages()}))}),this.httpServer.on("listening",function(){t.wsServer=new p({httpServer:t.httpServer,autoAcceptconnections:!1}),t.wsServer.on("request",function(e){var n=e.accept(null,e.orgin);"/tts_tokens"===e.resource?(t.tokenSocket=n,t.processMessages()):"/tts_phones"===e.resource&&(t.phoneSocket=n,t.processMessages())})}),e(),this.httpServer.listen(this.port)}},processMessages:{value:function(){for(;this.messages.length;){var e=this.messages.shift();this.connectionHandlers[e.url](e.data,e.response)}}},onSpeak:{value:function(n,s){var r=this;s.writeHead(204,{"Content-Type":"text/html"}),s.write(JSON.stringify({Status:"OK",Message:"Speaking TTS"})),s.end();var o=t(n.prompt);setTimeout(function(){r.emit("speech",n.prompt),r.tokenSocket.send(JSON.stringify(e("",0,"STOP")))},1e3*o[o.length-1].end)}},onTokenTimes:{value:function(e,n){var s={tokentimes:{tokens:t(e.prompt)}};n.writeHead(200,{"Content-Type":"text/html"}),n.write(JSON.stringify(s)),n.end()}},onStop:{value:function(e,t){t.writeHead(200,{"Content-Type":"text/html"}),t.write(JSON.stringify({Status:"OK",Message:"Stopping TTS"})),t.end(),this.emit("stop")}}}),s}(c);module.exports=new l;
|