Skip to content

Commit

Permalink
Add isBrowser check for optional agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Migushthe2nd committed Dec 7, 2023
1 parent 1225819 commit 2666a73
Show file tree
Hide file tree
Showing 3 changed files with 644 additions and 653 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"@types/node": "^15.12.1",
"@types/ws": "^8.5.5",
"@types/ws": "^8.5.10",
"ts-node": "^10.9.1",
"typedoc": "^0.22.2",
"typescript": "^4.3.2"
Expand Down
49 changes: 25 additions & 24 deletions src/MsEdgeTTS.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import * as stream from "stream";
import WebSocket from "isomorphic-ws";
import {Buffer} from 'buffer';
import {Buffer} from "buffer";
import {randomBytes} from "crypto";
import {OUTPUT_FORMAT} from "./OUTPUT_FORMAT";
import * as fs from "fs";
Expand Down Expand Up @@ -49,6 +49,7 @@ export class MsEdgeTTS {
private static BINARY_DELIM = "Path:audio\r\n";
private static VOICE_LANG_REGEX = /\w{2}-\w{2}/;
private readonly _enableLogger;
private readonly _isBrowser: boolean;
private _ws: WebSocket;
private _voice;
private _voiceLocale;
Expand All @@ -66,12 +67,13 @@ export class MsEdgeTTS {
/**
* Create a new `MsEdgeTTS` instance.
*
* @param agent (optional) Use a custom http.Agent implementation like [https-proxy-agent](https://github.com/TooTallNate/proxy-agents) or [socks-proxy-agent](https://github.com/TooTallNate/proxy-agents/tree/main/packages/socks-proxy-agent).
* @param agent (optional, **NOT SUPPORTED IN BROWSER**) Use a custom http.Agent implementation like [https-proxy-agent](https://github.com/TooTallNate/proxy-agents) or [socks-proxy-agent](https://github.com/TooTallNate/proxy-agents/tree/main/packages/socks-proxy-agent).
* @param enableLogger=false whether to enable the built-in logger. This logs connections inits, disconnects, and incoming data to the console
*/
public constructor(agent?: Agent, enableLogger: boolean = false) {
this._agent = agent;
this._enableLogger = enableLogger;
this._isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
}

private async _send(message) {
Expand All @@ -88,8 +90,11 @@ export class MsEdgeTTS {
}

private _initClient() {
this._ws = new WebSocket(MsEdgeTTS.SYNTH_URL);
this._ws.binaryType = 'arraybuffer';
this._ws = this._isBrowser
? new WebSocket(MsEdgeTTS.SYNTH_URL)
: new WebSocket(MsEdgeTTS.SYNTH_URL, {agent: this._agent});

this._ws.binaryType = "arraybuffer";
return new Promise((resolve, reject) => {
this._ws.onopen = () => {
this._log("Connected in", (Date.now() - this._startTime) / 1000, "seconds")
Expand All @@ -110,30 +115,26 @@ export class MsEdgeTTS {
`).then(resolve);
};
this._ws.onmessage = (m) => {
const buffer = Buffer.from(m.data);
const message = buffer.toString()
const requestId = /X-RequestId:(.*?)\r\n/gm.exec(message)[1];
if (message.includes("Path:turn.start")) {
// start of turn, ignore
} else if (message.includes("Path:turn.end")) {
// end of turn, close stream
this._queue[requestId].push(null);
} else if (message.includes("Path:response")) {
// context response, ignore
} else if (message.includes("Path:audio")) {
if (m.data instanceof ArrayBuffer) {
this.cacheAudioData(buffer, requestId)
} else {
this._log("UNKNOWN MESSAGE", message);
}
const buffer = Buffer.from(m.data as ArrayBuffer);
const message = buffer.toString()
const requestId = /X-RequestId:(.*?)\r\n/gm.exec(message)[1];
if (message.includes("Path:turn.start")) {
// start of turn, ignore
} else if (message.includes("Path:turn.end")) {
// end of turn, close stream
this._queue[requestId].push(null);
} else if (message.includes("Path:response")) {
// context response, ignore
} else if (message.includes("Path:audio")) {
if (m.data instanceof ArrayBuffer) {
this.cacheAudioData(buffer, requestId)
} else {
this._log("UNKNOWN MESSAGE", message);
}
} else {
this._log("UNKNOWN MESSAGE", message);
}
// "upgrade" event is not available in native browser WebSocket
// this._ws.on("upgrade", (m) => {
// this._log("upgrade", m)
// })
}
this._ws.onclose = () => {
this._log("disconnected after:", (Date.now() - this._startTime) / 1000, "seconds")
for (const requestId in this._queue) {
Expand Down
Loading

0 comments on commit 2666a73

Please sign in to comment.