@speechly/browser-client
Advanced tools
Comparing version 1.0.11 to 1.0.12
@@ -79,2 +79,3 @@ | ||
private readonly debug; | ||
private readonly logSegments; | ||
private readonly projectId?; | ||
@@ -111,3 +112,3 @@ private readonly appId?; | ||
* Esteblish websocket connection | ||
*/ | ||
*/ | ||
private connect; | ||
@@ -226,2 +227,6 @@ /** | ||
/** | ||
* Whether to output updated segments to the console. | ||
*/ | ||
logSegments?: boolean; | ||
/** | ||
* Custom microphone implementation. | ||
@@ -228,0 +233,0 @@ * If not provided, an implementation based on getUserMedia and Web Audio API is used. |
{ | ||
"name": "@speechly/browser-client", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Browser client for Speechly API", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -38,11 +38,4 @@ <h1 align="center"> | ||
// Create a new Client. appId is configured in the dashboard. | ||
const client = new Client({ | ||
appId: 'your-app-id', | ||
}) | ||
const client = new Client({appId: 'your-app-id'}) | ||
// Also Client can be created with a projectId | ||
const client = new Client({ | ||
projectId: 'your-project-id', | ||
}) | ||
// Initialize the client - this will ask the user for microphone permissions and establish the connection to Speechly API. | ||
@@ -61,5 +54,2 @@ // Make sure you call `initlialize` from a user action handler (e.g. from a button press handler). | ||
// If Client been created with a projectId then call the function with an appId | ||
await client.startContext('your-app-id) | ||
// Stop recording after a timeout. | ||
@@ -79,5 +69,2 @@ // Ideally this should be bound to e.g. a button press. | ||
### Client states | ||
![](https://github.com/speechly/browser-client/blob/master/assets/states.jpeg) | ||
## Contributing | ||
@@ -84,0 +71,0 @@ |
@@ -15,2 +15,3 @@ import { ClientOptions, StateChangeCallback, SegmentChangeCallback, TentativeTranscriptCallback, TranscriptCallback, TentativeEntitiesCallback, EntityCallback, IntentCallback } from './types'; | ||
private readonly debug; | ||
private readonly logSegments; | ||
private readonly projectId?; | ||
@@ -47,3 +48,3 @@ private readonly appId?; | ||
* Esteblish websocket connection | ||
*/ | ||
*/ | ||
private connect; | ||
@@ -50,0 +51,0 @@ /** |
@@ -39,3 +39,3 @@ "use strict"; | ||
constructor(options) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; | ||
this.activeContexts = new Map(); | ||
@@ -115,2 +115,6 @@ this.reconnectAttemptCount = 5; | ||
this.activeContexts.set(audio_context, context); | ||
// Log segment to console | ||
if (this.logSegments) { | ||
console.info(segmentState.toString()); | ||
} | ||
// Fire segment change event. | ||
@@ -142,3 +146,3 @@ this.segmentChangeCb(segmentState.toSegment()); | ||
} | ||
catch (_m) { | ||
catch (_o) { | ||
this.nativeResamplingSupported = false; | ||
@@ -151,8 +155,9 @@ } | ||
this.debug = (_c = options.debug) !== null && _c !== void 0 ? _c : false; | ||
this.loginUrl = (_d = options.loginUrl) !== null && _d !== void 0 ? _d : defaultLoginUrl; | ||
this.appId = (_e = options.appId) !== null && _e !== void 0 ? _e : undefined; | ||
this.projectId = (_f = options.projectId) !== null && _f !== void 0 ? _f : undefined; | ||
const apiUrl = generateWsUrl((_g = options.apiUrl) !== null && _g !== void 0 ? _g : defaultApiUrl, language, (_h = options.sampleRate) !== null && _h !== void 0 ? _h : microphone_1.DefaultSampleRate); | ||
this.apiClient = (_j = options.apiClient) !== null && _j !== void 0 ? _j : new websocket_1.WebWorkerController(); | ||
this.storage = (_k = options.storage) !== null && _k !== void 0 ? _k : new storage_1.LocalStorage(); | ||
this.logSegments = (_d = options.logSegments) !== null && _d !== void 0 ? _d : false; | ||
this.loginUrl = (_e = options.loginUrl) !== null && _e !== void 0 ? _e : defaultLoginUrl; | ||
this.appId = (_f = options.appId) !== null && _f !== void 0 ? _f : undefined; | ||
this.projectId = (_g = options.projectId) !== null && _g !== void 0 ? _g : undefined; | ||
const apiUrl = generateWsUrl((_h = options.apiUrl) !== null && _h !== void 0 ? _h : defaultApiUrl, language, (_j = options.sampleRate) !== null && _j !== void 0 ? _j : microphone_1.DefaultSampleRate); | ||
this.apiClient = (_k = options.apiClient) !== null && _k !== void 0 ? _k : new websocket_1.WebWorkerController(); | ||
this.storage = (_l = options.storage) !== null && _l !== void 0 ? _l : new storage_1.LocalStorage(); | ||
this.deviceId = this.storage.getOrSet(deviceIdStorageKey, uuid_1.v4); | ||
@@ -162,3 +167,4 @@ const storedToken = this.storage.get(authTokenKey); | ||
if (storedToken == null || !token_1.validateToken(storedToken, this.projectId, this.appId, this.deviceId)) { | ||
token_1.fetchToken(this.loginUrl, this.projectId, this.appId, this.deviceId).then((token) => { | ||
token_1.fetchToken(this.loginUrl, this.projectId, this.appId, this.deviceId) | ||
.then(token => { | ||
this.authToken = token; | ||
@@ -168,3 +174,6 @@ // Cache the auth token in local storage for future use. | ||
this.connect(apiUrl); | ||
}).catch(err => { throw err; }); | ||
}) | ||
.catch(err => { | ||
throw err; | ||
}); | ||
} | ||
@@ -184,3 +193,3 @@ else { | ||
} | ||
this.microphone = (_l = options.microphone) !== null && _l !== void 0 ? _l : new microphone_1.BrowserMicrophone(this.isWebkit, this.sampleRate, this.apiClient); | ||
this.microphone = (_m = options.microphone) !== null && _m !== void 0 ? _m : new microphone_1.BrowserMicrophone(this.isWebkit, this.sampleRate, this.apiClient); | ||
this.apiClient.onResponse(this.handleWebsocketResponse); | ||
@@ -192,3 +201,3 @@ this.apiClient.onClose(this.handleWebsocketClosure); | ||
* Esteblish websocket connection | ||
*/ | ||
*/ | ||
connect(apiUrl) { | ||
@@ -370,13 +379,21 @@ this.apiClient.postMessage({ | ||
this.setState(types_1.ClientState.Stopping); | ||
this.stoppedContextIdPromise = new Promise((resolve) => { | ||
this.stoppedContextIdPromise = new Promise(resolve => { | ||
Promise.race([ | ||
new Promise((resolve) => setTimeout(resolve, this.contextStopDelay)), | ||
new Promise((resolve) => { this.resolveStopContext = resolve; }), | ||
new Promise(resolve => setTimeout(resolve, this.contextStopDelay)), | ||
new Promise(resolve => { | ||
this.resolveStopContext = resolve; | ||
}), | ||
]) | ||
.then(() => { | ||
this._stopContext() | ||
.then(id => { resolve(id); }) | ||
.catch(err => { throw err; }); | ||
.then(id => { | ||
resolve(id); | ||
}) | ||
.catch(err => { | ||
throw err; | ||
}); | ||
}) | ||
.catch(err => { throw err; }); | ||
.catch(err => { | ||
throw err; | ||
}); | ||
}); | ||
@@ -383,0 +400,0 @@ const contextId = yield this.stoppedContextIdPromise; |
@@ -11,2 +11,3 @@ import { Word, Entity, Intent, Segment } from './types'; | ||
toSegment(): Segment; | ||
toString(): string; | ||
updateTranscript(words: Word[]): SegmentState; | ||
@@ -13,0 +14,0 @@ updateEntities(entities: Entity[]): SegmentState; |
@@ -28,2 +28,8 @@ "use strict"; | ||
} | ||
toString() { | ||
const segment = this.toSegment(); | ||
const words = segment.words.filter((w) => w.value).map((w) => ({ value: w.value, index: w.index })); | ||
const cleanSegment = Object.assign(Object.assign({}, segment), { words }); | ||
return JSON.stringify(cleanSegment, null, 2); | ||
} | ||
updateTranscript(words) { | ||
@@ -30,0 +36,0 @@ words.forEach(w => { |
@@ -38,2 +38,6 @@ import { Microphone } from '../microphone'; | ||
/** | ||
* Whether to output updated segments to the console. | ||
*/ | ||
logSegments?: boolean; | ||
/** | ||
* Custom microphone implementation. | ||
@@ -40,0 +44,0 @@ * If not provided, an implementation based on getUserMedia and Web Audio API is used. |
@@ -50,3 +50,3 @@ "use strict"; | ||
} | ||
if (decoded.appId !== appId && decoded.projectId !== projectId) { | ||
if (decoded.appId !== appId || decoded.projectId !== projectId) { | ||
return false; | ||
@@ -53,0 +53,0 @@ } |
@@ -76,3 +76,3 @@ "use strict"; | ||
}); | ||
return new Promise((resolve) => { | ||
return new Promise(resolve => { | ||
this.resolveInitialization = resolve; | ||
@@ -79,0 +79,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
171570
3118
880
1
87