diff --git a/src/assistantSdk/assistant.ts b/src/assistantSdk/assistant.ts index de3993cce9..efa318c2b6 100644 --- a/src/assistantSdk/assistant.ts +++ b/src/assistantSdk/assistant.ts @@ -70,7 +70,7 @@ const promiseTimeout = (promise: Promise, timeout: number): Promise => return Promise.race([ promise.then((v) => { if (timeoutId) { - window.clearTimeout(timeoutId); + clearTimeout(timeoutId); } return v; }), @@ -405,7 +405,7 @@ export const createAssistant = ({ const { command } = items[i]; if (typeof command !== 'undefined') { - window.setTimeout(() => emit('command', command)); + setTimeout(() => emit('command', command)); if (command.type === 'start_music_recognition') { voice.shazam(); diff --git a/src/assistantSdk/client/protocol.ts b/src/assistantSdk/client/protocol.ts index 0eea4494c5..188b7e398c 100644 --- a/src/assistantSdk/client/protocol.ts +++ b/src/assistantSdk/client/protocol.ts @@ -275,7 +275,7 @@ export const createProtocol = ( status = 'connected'; - window.clearTimeout(clearReadyTimer); + clearTimeout(clearReadyTimer); /// считаем коннект = ready, если по истечении таймаута сокет не был разорван /// т.к бек может разрывать сокет, если с settings что-то не так @@ -343,7 +343,10 @@ export const createProtocol = ( }, init: () => { // в отличии от reconnect не обрывает коннект если он в порядке - if (status === 'ready' && window.navigator.onLine) { + if ( + (status === 'ready' && typeof window !== 'undefined' && window.navigator.onLine) || + typeof window === 'undefined' + ) { return Promise.resolve(); } diff --git a/src/assistantSdk/client/transport.ts b/src/assistantSdk/client/transport.ts index 7d545dcbae..95a626b33b 100644 --- a/src/assistantSdk/client/transport.ts +++ b/src/assistantSdk/client/transport.ts @@ -16,19 +16,24 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C const { on, emit } = createNanoEvents(); let hasCert = !checkCertUrl; - let retryTimeoutId = -1; + let retryTimeoutId: unknown = -1; let retries = 0; let status: 'closed' | 'closing' | 'connecting' | 'open' = 'closed'; let webSocket: WebSocket; let stopped = true; - const checkCert = (checkUrl: string) => - new Promise((resolve) => { + const checkCert = (checkUrl: string) => { + if (typeof window === 'undefined') { + return Promise.resolve(true); + } + + return new Promise((resolve) => { window .fetch(checkUrl) .then(() => resolve(true)) .catch(() => resolve(false)); }); + }; const close = () => { stopped = true; @@ -46,7 +51,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C status = 'connecting'; emit('connecting'); - if (!hasCert && window.navigator.onLine) { + if ((!hasCert && typeof window !== 'undefined' && window.navigator.onLine) || typeof window === 'undefined') { const okay = await checkCert(checkCertUrl!); if (!okay) { @@ -70,7 +75,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C return; } - window.clearTimeout(retryTimeoutId); + clearTimeout(retryTimeoutId as number); retries = 0; @@ -90,10 +95,10 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C // пробуем переподключаться, если возникла ошибка при коннекте if (!webSocket || (webSocket.readyState === 3 && !stopped)) { - window.clearTimeout(retryTimeoutId); + clearTimeout(retryTimeoutId as number); if (retries < 2) { - retryTimeoutId = window.setTimeout(() => { + retryTimeoutId = setTimeout(() => { // eslint-disable-next-line @typescript-eslint/no-use-before-define open(url); @@ -129,7 +134,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C return; } - window.setTimeout(() => reconnect(url)); + setTimeout(() => reconnect(url)); close(); }; @@ -141,7 +146,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C return { close, get isOnline() { - return window.navigator.onLine; + return (typeof window !== 'undefined' && window.navigator.onLine) || typeof window === 'undefined'; }, on, open, diff --git a/src/assistantSdk/voice/audioContext.ts b/src/assistantSdk/voice/audioContext.ts index 0c6fb93c1d..13f326e93c 100644 --- a/src/assistantSdk/voice/audioContext.ts +++ b/src/assistantSdk/voice/audioContext.ts @@ -8,16 +8,17 @@ export const isAudioSupported = typeof window !== 'undefined' && (window.AudioCo * @returns AudioContext */ export const createAudioContext = (options?: AudioContextOptions): AudioContext => { - if (window.AudioContext) { - return new AudioContext(options); + if (!isAudioSupported) { + throw new Error('Audio not supported'); } - if (window.webkitAudioContext) { - // eslint-disable-next-line new-cap - return new window.webkitAudioContext(); + if (window.AudioContext) { + return new AudioContext(options); } - throw new Error('Audio not supported'); + // @ts-ignore + // eslint-disable-next-line new-cap + return new window.webkitAudioContext(); }; interface ContextEvents { diff --git a/src/assistantSdk/voice/listener/navigatorAudioProvider.ts b/src/assistantSdk/voice/listener/navigatorAudioProvider.ts index c2c08303ea..a825fe5ad1 100644 --- a/src/assistantSdk/voice/listener/navigatorAudioProvider.ts +++ b/src/assistantSdk/voice/listener/navigatorAudioProvider.ts @@ -132,7 +132,7 @@ export const createNavigatorAudioProvider = (cb: (buffer: ArrayBuffer, last: boo return createAudioRecorder(stream, cb); }) .catch((err) => { - if (window.location.protocol === 'http:') { + if (typeof window !== 'undefined' && window.location.protocol === 'http:') { throw new Error('Audio is supported only on a secure connection'); }