@picovoice/eagle-web 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,208 +1,246 @@
1
- /*
2
- Copyright 2023-2025 Picovoice Inc.
3
- You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
4
- file accompanying this source.
5
- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
6
- an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
7
- specific language governing permissions and limitations under the License.
8
- */
9
-
10
- /// <reference no-default-lib="false"/>
11
- /// <reference lib="webworker" />
12
-
13
- import { EagleProfiler } from './eagle';
14
- import {
15
- EagleProfilerWorkerEnrollRequest,
16
- EagleProfilerWorkerInitRequest,
17
- EagleProfilerWorkerRequest,
18
- PvStatus,
19
- } from './types';
20
- import { EagleError } from "./eagle_errors";
21
-
22
- let profiler: EagleProfiler | null = null;
23
-
24
- const initRequest = async (
25
- request: EagleProfilerWorkerInitRequest
26
- ): Promise<any> => {
27
- if (profiler !== null) {
28
- return {
29
- command: 'error',
30
- status: PvStatus.INVALID_STATE,
31
- shortMessage: 'Eagle profiler already initialized',
32
- };
33
- }
34
- try {
35
- EagleProfiler.setWasmSimd(request.wasmSimd);
36
- EagleProfiler.setWasmSimdLib(request.wasmSimdLib);
37
- EagleProfiler.setWasmPThread(request.wasmPThread);
38
- EagleProfiler.setWasmPThreadLib(request.wasmPThreadLib);
39
- EagleProfiler.setSdk(request.sdk);
40
- profiler = await EagleProfiler._init(
41
- request.accessKey,
42
- request.modelPath,
43
- request.options
44
- );
45
- return {
46
- command: 'ok',
47
- minEnrollSamples: profiler.minEnrollSamples,
48
- sampleRate: profiler.sampleRate,
49
- version: profiler.version,
50
- };
51
- } catch (e: any) {
52
- if (e instanceof EagleError) {
53
- return {
54
- command: 'error',
55
- status: e.status,
56
- shortMessage: e.shortMessage,
57
- messageStack: e.messageStack
58
- };
59
- } else {
60
- return {
61
- command: 'error',
62
- status: PvStatus.RUNTIME_ERROR,
63
- shortMessage: e.message
64
- };
65
- }
66
- }
67
- };
68
-
69
- const enrollRequest = async (
70
- request: EagleProfilerWorkerEnrollRequest
71
- ): Promise<any> => {
72
- if (profiler === null) {
73
- return {
74
- command: 'error',
75
- status: PvStatus.INVALID_STATE,
76
- shortMessage: 'Eagle profiler not initialized',
77
- };
78
- }
79
- try {
80
- const result = await profiler.enroll(request.inputFrame);
81
- return {
82
- command: 'ok',
83
- result,
84
- };
85
- } catch (e: any) {
86
- if (e instanceof EagleError) {
87
- return {
88
- command: 'error',
89
- status: e.status,
90
- shortMessage: e.shortMessage,
91
- messageStack: e.messageStack
92
- };
93
- } else {
94
- return {
95
- command: 'error',
96
- status: PvStatus.RUNTIME_ERROR,
97
- shortMessage: e.message
98
- };
99
- }
100
- }
101
- };
102
-
103
- const exportRequest = async (): Promise<any> => {
104
- if (profiler === null) {
105
- return {
106
- command: 'error',
107
- status: PvStatus.INVALID_STATE,
108
- shortMessage: 'Eagle profiler not initialized',
109
- };
110
- }
111
- try {
112
- const profile = await profiler.export();
113
- return {
114
- command: 'ok',
115
- profile,
116
- };
117
- } catch (e: any) {
118
- if (e instanceof EagleError) {
119
- return {
120
- command: 'error',
121
- status: e.status,
122
- shortMessage: e.shortMessage,
123
- messageStack: e.messageStack
124
- };
125
- } else {
126
- return {
127
- command: 'error',
128
- status: PvStatus.RUNTIME_ERROR,
129
- shortMessage: e.message
130
- };
131
- }
132
- }
133
- };
134
-
135
- const resetRequest = async (): Promise<any> => {
136
- if (profiler === null) {
137
- return {
138
- command: 'error',
139
- status: PvStatus.INVALID_STATE,
140
- shortMessage: 'Eagle profiler not initialized',
141
- };
142
- }
143
- try {
144
- await profiler.reset();
145
- return {
146
- command: 'ok',
147
- };
148
- } catch (e: any) {
149
- if (e instanceof EagleError) {
150
- return {
151
- command: 'error',
152
- status: e.status,
153
- shortMessage: e.shortMessage,
154
- messageStack: e.messageStack
155
- };
156
- } else {
157
- return {
158
- command: 'error',
159
- status: PvStatus.RUNTIME_ERROR,
160
- shortMessage: e.message
161
- };
162
- }
163
- }
164
- };
165
-
166
- const releaseRequest = async (): Promise<any> => {
167
- if (profiler !== null) {
168
- await profiler.release();
169
- profiler = null;
170
- close();
171
- }
172
- return {
173
- command: 'ok',
174
- };
175
- };
176
-
177
- /**
178
- * Eagle profiler worker handler.
179
- */
180
- self.onmessage = async function (
181
- event: MessageEvent<EagleProfilerWorkerRequest>
182
- ): Promise<void> {
183
- switch (event.data.command) {
184
- case 'init':
185
- self.postMessage(await initRequest(event.data));
186
- break;
187
- case 'enroll':
188
- self.postMessage(await enrollRequest(event.data));
189
- break;
190
- case 'export': {
191
- const exportMsg = await exportRequest();
192
- self.postMessage(exportMsg, [exportMsg.profile.bytes.buffer]);
193
- break;
194
- }
195
- case 'reset':
196
- self.postMessage(await resetRequest());
197
- break;
198
- case 'release':
199
- self.postMessage(await releaseRequest());
200
- break;
201
- default:
202
- self.postMessage({
203
- command: 'failed',
204
- // @ts-ignore
205
- message: `Unrecognized command: ${event.data.command}`,
206
- });
207
- }
208
- };
1
+ /*
2
+ Copyright 2023-2026 Picovoice Inc.
3
+ You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
4
+ file accompanying this source.
5
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
6
+ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
7
+ specific language governing permissions and limitations under the License.
8
+ */
9
+
10
+ /// <reference no-default-lib="false"/>
11
+ /// <reference lib="webworker" />
12
+
13
+ import { EagleProfiler } from './eagle';
14
+ import {
15
+ EagleProfilerWorkerEnrollRequest,
16
+ EagleProfilerWorkerFlushRequest,
17
+ EagleProfilerWorkerInitRequest,
18
+ EagleProfilerWorkerRequest,
19
+ PvStatus,
20
+ } from './types';
21
+ import { EagleError } from "./eagle_errors";
22
+
23
+ let profiler: EagleProfiler | null = null;
24
+
25
+ const initRequest = async (
26
+ request: EagleProfilerWorkerInitRequest
27
+ ): Promise<any> => {
28
+ if (profiler !== null) {
29
+ return {
30
+ command: 'error',
31
+ status: PvStatus.INVALID_STATE,
32
+ shortMessage: 'Eagle profiler already initialized',
33
+ };
34
+ }
35
+ try {
36
+ EagleProfiler.setWasmSimd(request.wasmSimd);
37
+ EagleProfiler.setWasmSimdLib(request.wasmSimdLib);
38
+ EagleProfiler.setWasmPThread(request.wasmPThread);
39
+ EagleProfiler.setWasmPThreadLib(request.wasmPThreadLib);
40
+ EagleProfiler.setSdk(request.sdk);
41
+ profiler = await EagleProfiler._init(
42
+ request.accessKey,
43
+ request.modelPath,
44
+ request.options
45
+ );
46
+ return {
47
+ command: 'ok',
48
+ frameLength: profiler.frameLength,
49
+ sampleRate: profiler.sampleRate,
50
+ version: profiler.version,
51
+ };
52
+ } catch (e: any) {
53
+ if (e instanceof EagleError) {
54
+ return {
55
+ command: 'error',
56
+ status: e.status,
57
+ shortMessage: e.shortMessage,
58
+ messageStack: e.messageStack
59
+ };
60
+ } else {
61
+ return {
62
+ command: 'error',
63
+ status: PvStatus.RUNTIME_ERROR,
64
+ shortMessage: e.message
65
+ };
66
+ }
67
+ }
68
+ };
69
+
70
+ const enrollRequest = async (
71
+ request: EagleProfilerWorkerEnrollRequest
72
+ ): Promise<any> => {
73
+ if (profiler === null) {
74
+ return {
75
+ command: 'error',
76
+ status: PvStatus.INVALID_STATE,
77
+ shortMessage: 'Eagle profiler not initialized',
78
+ };
79
+ }
80
+ try {
81
+ const result = await profiler.enroll(request.inputFrame);
82
+ return {
83
+ command: 'ok',
84
+ result,
85
+ };
86
+ } catch (e: any) {
87
+ if (e instanceof EagleError) {
88
+ return {
89
+ command: 'error',
90
+ status: e.status,
91
+ shortMessage: e.shortMessage,
92
+ messageStack: e.messageStack
93
+ };
94
+ } else {
95
+ return {
96
+ command: 'error',
97
+ status: PvStatus.RUNTIME_ERROR,
98
+ shortMessage: e.message
99
+ };
100
+ }
101
+ }
102
+ };
103
+
104
+ const flushRequest = async (
105
+ request: EagleProfilerWorkerFlushRequest
106
+ ): Promise<any> => {
107
+ if (profiler === null) {
108
+ return {
109
+ command: 'error',
110
+ status: PvStatus.INVALID_STATE,
111
+ shortMessage: 'Eagle profiler not initialized',
112
+ };
113
+ }
114
+ try {
115
+ const result = await profiler.flush();
116
+ return {
117
+ command: 'ok',
118
+ result,
119
+ };
120
+ } catch (e: any) {
121
+ if (e instanceof EagleError) {
122
+ return {
123
+ command: 'error',
124
+ status: e.status,
125
+ shortMessage: e.shortMessage,
126
+ messageStack: e.messageStack
127
+ };
128
+ } else {
129
+ return {
130
+ command: 'error',
131
+ status: PvStatus.RUNTIME_ERROR,
132
+ shortMessage: e.message
133
+ };
134
+ }
135
+ }
136
+ };
137
+
138
+ const exportRequest = async (): Promise<any> => {
139
+ if (profiler === null) {
140
+ return {
141
+ command: 'error',
142
+ status: PvStatus.INVALID_STATE,
143
+ shortMessage: 'Eagle profiler not initialized',
144
+ };
145
+ }
146
+ try {
147
+ const profile = await profiler.export();
148
+ return {
149
+ command: 'ok',
150
+ profile,
151
+ };
152
+ } catch (e: any) {
153
+ if (e instanceof EagleError) {
154
+ return {
155
+ command: 'error',
156
+ status: e.status,
157
+ shortMessage: e.shortMessage,
158
+ messageStack: e.messageStack
159
+ };
160
+ } else {
161
+ return {
162
+ command: 'error',
163
+ status: PvStatus.RUNTIME_ERROR,
164
+ shortMessage: e.message
165
+ };
166
+ }
167
+ }
168
+ };
169
+
170
+ const resetRequest = async (): Promise<any> => {
171
+ if (profiler === null) {
172
+ return {
173
+ command: 'error',
174
+ status: PvStatus.INVALID_STATE,
175
+ shortMessage: 'Eagle profiler not initialized',
176
+ };
177
+ }
178
+ try {
179
+ await profiler.reset();
180
+ return {
181
+ command: 'ok',
182
+ };
183
+ } catch (e: any) {
184
+ if (e instanceof EagleError) {
185
+ return {
186
+ command: 'error',
187
+ status: e.status,
188
+ shortMessage: e.shortMessage,
189
+ messageStack: e.messageStack
190
+ };
191
+ } else {
192
+ return {
193
+ command: 'error',
194
+ status: PvStatus.RUNTIME_ERROR,
195
+ shortMessage: e.message
196
+ };
197
+ }
198
+ }
199
+ };
200
+
201
+ const releaseRequest = async (): Promise<any> => {
202
+ if (profiler !== null) {
203
+ await profiler.release();
204
+ profiler = null;
205
+ close();
206
+ }
207
+ return {
208
+ command: 'ok',
209
+ };
210
+ };
211
+
212
+ /**
213
+ * Eagle profiler worker handler.
214
+ */
215
+ self.onmessage = async function (
216
+ event: MessageEvent<EagleProfilerWorkerRequest>
217
+ ): Promise<void> {
218
+ switch (event.data.command) {
219
+ case 'init':
220
+ self.postMessage(await initRequest(event.data));
221
+ break;
222
+ case 'enroll':
223
+ self.postMessage(await enrollRequest(event.data));
224
+ break;
225
+ case 'flush':
226
+ self.postMessage(await flushRequest(event.data));
227
+ break;
228
+ case 'export': {
229
+ const exportMsg = await exportRequest();
230
+ self.postMessage(exportMsg, [exportMsg.profile.bytes.buffer]);
231
+ break;
232
+ }
233
+ case 'reset':
234
+ self.postMessage(await resetRequest());
235
+ break;
236
+ case 'release':
237
+ self.postMessage(await releaseRequest());
238
+ break;
239
+ default:
240
+ self.postMessage({
241
+ command: 'failed',
242
+ // @ts-ignore
243
+ message: `Unrecognized command: ${event.data.command}`,
244
+ });
245
+ }
246
+ };