Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/Transcriber.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'core-js';
import querystring from 'querystring';
import EventEmitter from 'events';
import uuid from 'uuid/v4';
import Microphone from '@gkt/microphone';
Expand All @@ -10,8 +11,9 @@ class Transcriber extends EventEmitter {
webSocket = window.WebSocket,
audioContext = window.AudioContext || window.webkitAudioContext,
microphone = Microphone,
interpreter = 'auto',
maxFinalWait = 5000,
middlewareConfig = {},
memo = null,
} = {}) {
super();
this.gkUrl = gkUrl;
Expand All @@ -21,9 +23,10 @@ class Transcriber extends EventEmitter {
this.WebSocket = webSocket;
this.AudioContext = audioContext;
this.Microphone = microphone;
this.interpreter = interpreter;
this.maxFinalWait = maxFinalWait;
this.state = UNINITIALIZED;
this.middlewareConfig = middlewareConfig;
this.memo = memo;
}

init() {
Expand Down Expand Up @@ -56,11 +59,6 @@ class Transcriber extends EventEmitter {
return this._initPromise;
}

setInterpreter(interpreter) {
this.interpreter = interpreter;
return this;
}

start() {
if (this.state !== READY) {
throw new Error('Transcriber#start: not ready');
Expand Down Expand Up @@ -150,7 +148,15 @@ class Transcriber extends EventEmitter {

_openAudioSocket(sessionId) {
return new Promise((resolve, reject) => {
const path = `${this.gkUrl}/audio/${sessionId}?interpreters=${this.interpreter}`;
const params = {
middleware_config: encodeURIComponent(JSON.stringify(this.middlewareConfig))
};

if (this.memo) {
params.memo = encodeURIComponent(this.memo);
}

const path = `${this.gkUrl}/audio/${sessionId}?${querystring.stringify(params)}`;
const socket = new this.WebSocket(this._webSocketUrl(path));
socket.onopen = () => resolve(socket);
socket.onerror = e => reject(e);
Expand Down
34 changes: 26 additions & 8 deletions src/Transcriber.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ let defaultOpts;
let transcriber;

beforeEach(() => {
MockWebSocket.clearConnections();

defaultOpts = {
getUserMedia,
audioContext: MockAudioContext,
webSocket: MockWebSocket,
microphone: MockMicrophone,
maxFinalWait: 0,
middlewareConfig: {discovery: {foo: 1}},
memo: 'hello world',
};

transcriber = new Transcriber(defaultOpts);
Expand Down Expand Up @@ -81,14 +85,6 @@ describe('#init', () => {
});
});

describe('#setInterpreter', () => {
it('sets the interpreter prop', () => {
expect(transcriber.interpreter).toBe('auto');
transcriber.setInterpreter('foo');
expect(transcriber.interpreter).toBe('foo');
});
});

describe('#start', () => {
describe('when not READY', () => {
it('throws an exception', () => {
Expand All @@ -106,6 +102,28 @@ describe('#start', () => {
await p;
});

it('opens an audio websocket connection', async () => {
await transcriber.init();

const p = transcriber.start();
expect(MockWebSocket.connections.length).toBe(2);
const url = new URL(MockWebSocket.connections[0]);
expect(url.origin).toEqual('ws://audio');
expect(url.searchParams.get('middleware_config')).toEqual(encodeURIComponent(JSON.stringify({discovery: {foo: 1}})));
expect(url.searchParams.get('memo')).toEqual('hello%20world');
await p;
});

it('opens a relay websocket connection', async () => {
await transcriber.init();

const p = transcriber.start();
expect(MockWebSocket.connections.length).toBe(2);
const url = new URL(MockWebSocket.connections[1]);
expect(url.origin).toEqual('ws://relay');
await p;
});

describe('when websocket connection succeeds', () => {
it('goes to the RECORDING state', async () => {
await transcriber.init();
Expand Down
9 changes: 8 additions & 1 deletion src/transcriberMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ export class MockAudioContext {
}

export class MockWebSocket {
constructor() {
static clearConnections() {
this.connections = [];
}

constructor(url) {
MockWebSocket.connections = MockWebSocket.connections || [];
MockWebSocket.connections.push(url);

setTimeout(() => {
this.onopen(this);
});
Expand Down