@incodetech/core 2.0.0-alpha.1 → 2.0.0-alpha.11
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.
- package/dist/Manager-Cy9-TMC9.d.ts +19 -0
- package/dist/OpenViduLogger-20ZYS-mT.esm.js +3 -0
- package/dist/OpenViduLogger-5b7KqNTo.esm.js +803 -0
- package/dist/StateMachine-BqPpBhOz.d.ts +2 -0
- package/dist/addEvent-s2Za-pK3.esm.js +16 -0
- package/dist/chunk-C_Yo44FK.esm.js +49 -0
- package/dist/deepsightLoader-Bn2D0REl.esm.js +25 -0
- package/dist/email.d.ts +264 -0
- package/dist/email.esm.js +479 -0
- package/dist/endpoints-B0ltwtb5.esm.js +2335 -0
- package/dist/flow.d.ts +578 -0
- package/dist/flow.esm.js +628 -0
- package/dist/getBrowser-CLEzz0Hi.esm.js +8 -0
- package/dist/index.d.ts +226 -0
- package/dist/index.esm.js +156 -0
- package/dist/lib-CykGFCEr.esm.js +11700 -0
- package/dist/permissionServices-BhD0KxsO.esm.js +66 -0
- package/dist/phone.d.ts +292 -0
- package/dist/phone.esm.js +550 -0
- package/dist/recordingsRepository-CTjaf-ER.esm.js +256 -0
- package/dist/selfie.d.ts +821 -0
- package/dist/selfie.esm.js +1087 -0
- package/dist/types-Dif6MQmX.d.ts +5 -0
- package/dist/warmup-Cijuyvoc.d.ts +55 -0
- package/dist/xstate.esm-2T5fOCTq.esm.js +3261 -0
- package/package.json +14 -11
- package/src/camera/cameraActor.ts +0 -21
- package/src/camera/cameraService.test.ts +0 -437
- package/src/camera/cameraService.ts +0 -165
- package/src/camera/cameraServices.test.ts +0 -66
- package/src/camera/cameraServices.ts +0 -26
- package/src/camera/cameraStateMachine.test.ts +0 -602
- package/src/camera/cameraStateMachine.ts +0 -264
- package/src/camera/index.ts +0 -5
- package/src/camera/types.ts +0 -17
- package/src/device/getBrowser.ts +0 -31
- package/src/device/getDeviceClass.ts +0 -29
- package/src/device/index.ts +0 -2
- package/src/email/__mocks__/emailMocks.ts +0 -59
- package/src/email/emailActor.ts +0 -15
- package/src/email/emailManager.test.ts +0 -573
- package/src/email/emailManager.ts +0 -427
- package/src/email/emailServices.ts +0 -66
- package/src/email/emailStateMachine.test.ts +0 -741
- package/src/email/emailStateMachine.ts +0 -367
- package/src/email/index.ts +0 -39
- package/src/email/types.ts +0 -60
- package/src/events/addEvent.ts +0 -20
- package/src/events/types.ts +0 -7
- package/src/flow/__mocks__/flowMocks.ts +0 -84
- package/src/flow/flowActor.ts +0 -13
- package/src/flow/flowAnalyzer.test.ts +0 -266
- package/src/flow/flowAnalyzer.ts +0 -37
- package/src/flow/flowCompletionService.ts +0 -21
- package/src/flow/flowManager.test.ts +0 -560
- package/src/flow/flowManager.ts +0 -235
- package/src/flow/flowServices.test.ts +0 -109
- package/src/flow/flowServices.ts +0 -13
- package/src/flow/flowStateMachine.test.ts +0 -334
- package/src/flow/flowStateMachine.ts +0 -182
- package/src/flow/index.ts +0 -21
- package/src/flow/moduleLoader.test.ts +0 -136
- package/src/flow/moduleLoader.ts +0 -73
- package/src/flow/orchestratedFlowManager.test.ts +0 -240
- package/src/flow/orchestratedFlowManager.ts +0 -231
- package/src/flow/orchestratedFlowStateMachine.test.ts +0 -199
- package/src/flow/orchestratedFlowStateMachine.ts +0 -325
- package/src/flow/types.ts +0 -434
- package/src/http/__mocks__/api.ts +0 -88
- package/src/http/api.test.ts +0 -231
- package/src/http/api.ts +0 -90
- package/src/http/endpoints.ts +0 -17
- package/src/index.ts +0 -33
- package/src/permissions/index.ts +0 -2
- package/src/permissions/permissionServices.ts +0 -31
- package/src/permissions/types.ts +0 -3
- package/src/phone/__mocks__/phoneMocks.ts +0 -71
- package/src/phone/index.ts +0 -39
- package/src/phone/phoneActor.ts +0 -15
- package/src/phone/phoneManager.test.ts +0 -393
- package/src/phone/phoneManager.ts +0 -458
- package/src/phone/phoneServices.ts +0 -98
- package/src/phone/phoneStateMachine.test.ts +0 -918
- package/src/phone/phoneStateMachine.ts +0 -422
- package/src/phone/types.ts +0 -83
- package/src/recordings/recordingsRepository.test.ts +0 -87
- package/src/recordings/recordingsRepository.ts +0 -48
- package/src/recordings/streamingEvents.ts +0 -10
- package/src/selfie/__mocks__/selfieMocks.ts +0 -26
- package/src/selfie/index.ts +0 -14
- package/src/selfie/selfieActor.ts +0 -17
- package/src/selfie/selfieErrorUtils.test.ts +0 -116
- package/src/selfie/selfieErrorUtils.ts +0 -66
- package/src/selfie/selfieManager.test.ts +0 -297
- package/src/selfie/selfieManager.ts +0 -301
- package/src/selfie/selfieServices.ts +0 -362
- package/src/selfie/selfieStateMachine.test.ts +0 -283
- package/src/selfie/selfieStateMachine.ts +0 -804
- package/src/selfie/selfieUploadService.test.ts +0 -90
- package/src/selfie/selfieUploadService.ts +0 -81
- package/src/selfie/types.ts +0 -103
- package/src/session/index.ts +0 -5
- package/src/session/sessionService.ts +0 -78
- package/src/setup.test.ts +0 -61
- package/src/setup.ts +0 -171
- package/tsconfig.json +0 -13
- package/tsdown.config.ts +0 -22
- package/vitest.config.ts +0 -37
- package/vitest.setup.ts +0 -135
|
@@ -0,0 +1,1087 @@
|
|
|
1
|
+
import { b as createManager, c as WasmUtilProvider, f as OpenViduRecordingProvider, g as StreamCanvasCapture, h as StreamCanvasProcessingSession, i as getToken, l as FaceDetectionProvider, n as api, t as endpoints, v as requestCameraAccess, y as stopCameraStream } from "./endpoints-B0ltwtb5.esm.js";
|
|
2
|
+
import "./getBrowser-CLEzz0Hi.esm.js";
|
|
3
|
+
import { n as requestPermission, r as getDeviceClass, t as checkPermission } from "./permissionServices-BhD0KxsO.esm.js";
|
|
4
|
+
import { a as createActor, i as fromPromise, n as assign, r as fromCallback, t as setup } from "./xstate.esm-2T5fOCTq.esm.js";
|
|
5
|
+
import { t as addEvent } from "./addEvent-s2Za-pK3.esm.js";
|
|
6
|
+
import { n as startRecording, r as stopRecording$1, t as createRecordingSession } from "./recordingsRepository-CTjaf-ER.esm.js";
|
|
7
|
+
|
|
8
|
+
//#region src/modules/selfie/types.ts
|
|
9
|
+
const FACE_ERROR_CODES = {
|
|
10
|
+
FACE_OCCLUDED: "FACE_OCCLUDED",
|
|
11
|
+
LIVENESS: "LIVENESS_ERROR",
|
|
12
|
+
BRIGHTNESS: "BRIGHTNESS_ERROR",
|
|
13
|
+
LENSES: "LENSES_ERROR",
|
|
14
|
+
MASK: "MASK_ERROR",
|
|
15
|
+
CLOSED_EYES: "CLOSED_EYES_ERROR",
|
|
16
|
+
HEAD_COVER: "HEAD_COVER_ERROR",
|
|
17
|
+
SERVER: "SERVER_ERROR",
|
|
18
|
+
FACE_NOT_FOUND: "FACE_NOT_FOUND",
|
|
19
|
+
MULTIPLE_FACES: "MULTIPLE_FACES",
|
|
20
|
+
TOO_BLURRY: "TOO_BLURRY_ERROR",
|
|
21
|
+
TOO_DARK: "TOO_DARK_ERROR",
|
|
22
|
+
USER_IS_NOT_RECOGNIZED: "USER_IS_NOT_RECOGNIZED",
|
|
23
|
+
SPOOF_ATTEMPT_DETECTED: "SPOOF_ATTEMPT_DETECTED",
|
|
24
|
+
FACE_TOO_DARK: "FACE_TOO_DARK",
|
|
25
|
+
LENSES_DETECTED: "LENSES_DETECTED",
|
|
26
|
+
FACE_MASK_DETECTED: "FACE_MASK_DETECTED",
|
|
27
|
+
CLOSED_EYES_DETECTED: "CLOSED_EYES_DETECTED",
|
|
28
|
+
HEAD_COVER_DETECTED: "HEAD_COVER_DETECTED",
|
|
29
|
+
FACE_CROPPING_FAILED: "FACE_CROPPING_FAILED",
|
|
30
|
+
FACE_TOO_SMALL: "FACE_TOO_SMALL",
|
|
31
|
+
FACE_TOO_BLURRY: "FACE_TOO_BLURRY",
|
|
32
|
+
BAD_PHOTO_QUALITY: "BAD_PHOTO_QUALITY",
|
|
33
|
+
PROCESSING_ERROR: "PROCESSING_ERROR",
|
|
34
|
+
BAD_REQUEST: "BAD_REQUEST",
|
|
35
|
+
NONEXISTENT_CUSTOMER: "NONEXISTENT_CUSTOMER",
|
|
36
|
+
HINT_NOT_PROVIDED: "HINT_NOT_PROVIDED",
|
|
37
|
+
SELFIE_IMAGE_LOW_QUALITY: "SELFIE_IMAGE_LOW_QUALITY"
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/modules/selfie/selfieErrorUtils.ts
|
|
42
|
+
const FACE_ERROR_CODE_VALUES = Object.values(FACE_ERROR_CODES);
|
|
43
|
+
const isFaceErrorCode = (value) => {
|
|
44
|
+
return FACE_ERROR_CODE_VALUES.includes(value);
|
|
45
|
+
};
|
|
46
|
+
const getFaceErrorCodeFromUnknown = (value) => {
|
|
47
|
+
if (value instanceof Error && typeof value.message === "string") return isFaceErrorCode(value.message) ? value.message : void 0;
|
|
48
|
+
if (typeof value === "string") return isFaceErrorCode(value) ? value : void 0;
|
|
49
|
+
};
|
|
50
|
+
const validateUploadResponse = ({ config, response }) => {
|
|
51
|
+
if (!response) return FACE_ERROR_CODES.SERVER;
|
|
52
|
+
if (response.confidence !== 0) return FACE_ERROR_CODES.LIVENESS;
|
|
53
|
+
if (config.validateBrightness && !response.isBright) return FACE_ERROR_CODES.BRIGHTNESS;
|
|
54
|
+
if (config.validateLenses && response.hasLenses) return FACE_ERROR_CODES.LENSES;
|
|
55
|
+
if (config.validateFaceMask && response.hasFaceMask) return FACE_ERROR_CODES.MASK;
|
|
56
|
+
if (config.validateClosedEyes && response.hasClosedEyes) return FACE_ERROR_CODES.CLOSED_EYES;
|
|
57
|
+
if (config.validateHeadCover && response.hasHeadCover) return FACE_ERROR_CODES.HEAD_COVER;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/internal/recordings/streamingEvents.ts
|
|
62
|
+
const streamingEvents = {
|
|
63
|
+
strSessionDidConnect: "strSessionDidConnect",
|
|
64
|
+
strSessionDidDisconnect: "strSessionDidDisconnect",
|
|
65
|
+
strSessionDidFailWithError: "strSessionDidFailWithError",
|
|
66
|
+
strStreamVideoCaptureStart: "strStreamVideoCaptureStart",
|
|
67
|
+
strStreamVideoCaptureStop: "strStreamVideoCaptureStop",
|
|
68
|
+
strStreamPublisherCreated: "strStreamPublisherCreated",
|
|
69
|
+
strStreamPublisherDestroyed: "strStreamPublisherDestroyed",
|
|
70
|
+
strStreamPublisherDidFailWithError: "strStreamPublisherDidFailWithError"
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/modules/selfie/selfieUploadService.ts
|
|
75
|
+
/**
|
|
76
|
+
* Uploads a selfie image to the backend.
|
|
77
|
+
*/
|
|
78
|
+
async function uploadSelfie({ encryptedBase64Image, faceCoordinates, signal, metadata, recordingId }) {
|
|
79
|
+
try {
|
|
80
|
+
const payload = {
|
|
81
|
+
base64Image: encryptedBase64Image,
|
|
82
|
+
faceCoordinates: faceCoordinates ?? void 0,
|
|
83
|
+
encrypted: true,
|
|
84
|
+
clientInfo: { deviceClass: getDeviceClass() },
|
|
85
|
+
metadata: metadata ?? void 0
|
|
86
|
+
};
|
|
87
|
+
const query = { imageType: "selfie" };
|
|
88
|
+
if (recordingId) query.recordingId = recordingId;
|
|
89
|
+
const res = await api.post(endpoints.selfie, payload, {
|
|
90
|
+
signal,
|
|
91
|
+
query
|
|
92
|
+
});
|
|
93
|
+
if (!res.ok) throw new Error(`POST ${endpoints.selfie} failed: ${res.status} ${res.statusText}`);
|
|
94
|
+
return res.data;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
const errorCode = getFaceErrorCodeFromHttpError(error);
|
|
97
|
+
if (errorCode) throw new Error(errorCode);
|
|
98
|
+
throw new Error(FACE_ERROR_CODES.SERVER);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const getFaceErrorCodeFromHttpError = (error) => {
|
|
102
|
+
const err = error;
|
|
103
|
+
if (err.ok !== false || typeof err.status !== "number") return;
|
|
104
|
+
if (err.status !== 400) return FACE_ERROR_CODES.SERVER;
|
|
105
|
+
if (typeof err.data?.status !== "number") return FACE_ERROR_CODES.BAD_REQUEST;
|
|
106
|
+
return {
|
|
107
|
+
3004: FACE_ERROR_CODES.FACE_NOT_FOUND,
|
|
108
|
+
3005: FACE_ERROR_CODES.FACE_NOT_FOUND,
|
|
109
|
+
3006: FACE_ERROR_CODES.TOO_BLURRY,
|
|
110
|
+
3007: FACE_ERROR_CODES.TOO_DARK,
|
|
111
|
+
4010: FACE_ERROR_CODES.MULTIPLE_FACES,
|
|
112
|
+
4019: FACE_ERROR_CODES.FACE_NOT_FOUND,
|
|
113
|
+
4077: FACE_ERROR_CODES.BAD_PHOTO_QUALITY,
|
|
114
|
+
4078: FACE_ERROR_CODES.FACE_OCCLUDED
|
|
115
|
+
}[err.data.status] ?? FACE_ERROR_CODES.BAD_REQUEST;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/modules/selfie/selfieServices.ts
|
|
120
|
+
const CAMERA_CONSTRAINTS = {
|
|
121
|
+
video: {
|
|
122
|
+
facingMode: "user",
|
|
123
|
+
height: { ideal: 480 },
|
|
124
|
+
width: { ideal: 640 }
|
|
125
|
+
},
|
|
126
|
+
audio: false
|
|
127
|
+
};
|
|
128
|
+
function stopStream(stream) {
|
|
129
|
+
stopCameraStream(stream);
|
|
130
|
+
}
|
|
131
|
+
async function initializeCamera(config) {
|
|
132
|
+
const provider = new FaceDetectionProvider();
|
|
133
|
+
await provider.initialize({ autocaptureInterval: config.autoCaptureTimeout * 1e3 });
|
|
134
|
+
provider.setChecksEnabled({
|
|
135
|
+
lenses: config.validateLenses,
|
|
136
|
+
mask: config.validateFaceMask,
|
|
137
|
+
closedEyes: config.validateClosedEyes,
|
|
138
|
+
headWear: config.validateHeadCover,
|
|
139
|
+
occlusion: false
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
stream: await requestCameraAccess({
|
|
143
|
+
video: CAMERA_CONSTRAINTS.video,
|
|
144
|
+
audio: false
|
|
145
|
+
}),
|
|
146
|
+
provider
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Encrypts the provided selfie image using WASM.
|
|
151
|
+
*/
|
|
152
|
+
async function encryptSelfieImage({ canvas }) {
|
|
153
|
+
const base64Image = canvas.getBase64Image();
|
|
154
|
+
if (!base64Image) throw new Error("Canvas image is empty or null");
|
|
155
|
+
return (await WasmUtilProvider.getInstance()).encryptImage(base64Image);
|
|
156
|
+
}
|
|
157
|
+
function startDetection(params) {
|
|
158
|
+
let lastStatus;
|
|
159
|
+
let session;
|
|
160
|
+
const { provider } = params;
|
|
161
|
+
const setStatus = (status) => {
|
|
162
|
+
if (session?.isDisposed() === true) return;
|
|
163
|
+
if (lastStatus === status) return;
|
|
164
|
+
lastStatus = status;
|
|
165
|
+
params.onUpdate(status);
|
|
166
|
+
};
|
|
167
|
+
const stopDetectionLoop = () => {
|
|
168
|
+
session?.dispose();
|
|
169
|
+
};
|
|
170
|
+
const reset = () => {
|
|
171
|
+
provider.reset();
|
|
172
|
+
};
|
|
173
|
+
const cleanup = () => {
|
|
174
|
+
stopDetectionLoop();
|
|
175
|
+
};
|
|
176
|
+
(async () => {
|
|
177
|
+
try {
|
|
178
|
+
provider.setCallbacks({
|
|
179
|
+
onFarAway: () => setStatus("tooFar"),
|
|
180
|
+
onTooClose: () => setStatus("tooClose"),
|
|
181
|
+
onTooManyFaces: () => setStatus("tooManyFaces"),
|
|
182
|
+
onNoFace: () => setStatus("idle"),
|
|
183
|
+
onCenterFace: () => setStatus("centerFace"),
|
|
184
|
+
onGetReady: () => setStatus("getReady"),
|
|
185
|
+
onGetReadyFinished: () => setStatus("getReadyFinished"),
|
|
186
|
+
onDark: () => {
|
|
187
|
+
if (params.config.validateBrightness) setStatus("dark");
|
|
188
|
+
},
|
|
189
|
+
onBlur: () => setStatus("blur"),
|
|
190
|
+
onFaceAngle: () => setStatus("faceAngle"),
|
|
191
|
+
onLenses: () => {
|
|
192
|
+
if (params.config.validateLenses) setStatus("lenses");
|
|
193
|
+
},
|
|
194
|
+
onMask: () => {
|
|
195
|
+
if (params.config.validateFaceMask) setStatus("faceMask");
|
|
196
|
+
},
|
|
197
|
+
onEyesClosed: () => {
|
|
198
|
+
if (params.config.validateClosedEyes) setStatus("eyesClosed");
|
|
199
|
+
},
|
|
200
|
+
onHeadWear: () => {
|
|
201
|
+
if (params.config.validateHeadCover) setStatus("headWear");
|
|
202
|
+
},
|
|
203
|
+
onFaceOccluded: () => {},
|
|
204
|
+
onSwitchToManualCapture: () => {
|
|
205
|
+
setStatus("manualCapture");
|
|
206
|
+
stopDetectionLoop();
|
|
207
|
+
},
|
|
208
|
+
onCapture: (canvas, faceCoordinates) => {
|
|
209
|
+
setStatus("success");
|
|
210
|
+
params.onSuccess(canvas, faceCoordinates);
|
|
211
|
+
cleanup();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
setStatus("detecting");
|
|
215
|
+
session = new StreamCanvasProcessingSession({
|
|
216
|
+
capturer: params.capturer,
|
|
217
|
+
provider,
|
|
218
|
+
onFrame: params.onFrame
|
|
219
|
+
});
|
|
220
|
+
} catch {
|
|
221
|
+
setStatus("error");
|
|
222
|
+
cleanup();
|
|
223
|
+
}
|
|
224
|
+
})();
|
|
225
|
+
return {
|
|
226
|
+
cleanup,
|
|
227
|
+
reset
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function buildResolutionFromStream(stream) {
|
|
231
|
+
const track = stream.getVideoTracks()[0];
|
|
232
|
+
if (!track) return;
|
|
233
|
+
const settings = track.getSettings();
|
|
234
|
+
const width = settings.width;
|
|
235
|
+
const height = settings.height;
|
|
236
|
+
if (typeof width === "number" && typeof height === "number") return `${width}x${height}`;
|
|
237
|
+
}
|
|
238
|
+
async function startRecordingSession(params) {
|
|
239
|
+
if (params.config.deepsightLiveness === "VIDEOLIVENESS") return;
|
|
240
|
+
if (params.config.enableFaceRecording !== true) return;
|
|
241
|
+
if (params.existing) return params.existing;
|
|
242
|
+
const provider = params.config.recording?.capability ?? new OpenViduRecordingProvider();
|
|
243
|
+
const clonedStream = params.clonedStream;
|
|
244
|
+
const hasAudio = clonedStream.getAudioTracks().length > 0;
|
|
245
|
+
const resolution = buildResolutionFromStream(clonedStream);
|
|
246
|
+
const session = await createRecordingSession("selfie");
|
|
247
|
+
const connection = await provider.connect({
|
|
248
|
+
sessionToken: session.token,
|
|
249
|
+
stream: clonedStream,
|
|
250
|
+
events: {
|
|
251
|
+
onSessionConnected: (sessionId) => {
|
|
252
|
+
addEvent({
|
|
253
|
+
code: streamingEvents.strSessionDidConnect,
|
|
254
|
+
payload: {
|
|
255
|
+
message: "Recording session connected",
|
|
256
|
+
sessionId
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
onSessionDisconnected: (sessionId) => {
|
|
261
|
+
addEvent({
|
|
262
|
+
code: streamingEvents.strSessionDidDisconnect,
|
|
263
|
+
payload: {
|
|
264
|
+
message: "Recording session disconnected",
|
|
265
|
+
sessionId
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
onSessionException: (event) => {
|
|
270
|
+
addEvent({
|
|
271
|
+
code: streamingEvents.strSessionDidFailWithError,
|
|
272
|
+
payload: {
|
|
273
|
+
message: "Recording session failed due to an error",
|
|
274
|
+
eventName: event.name,
|
|
275
|
+
type: "OpenViduException",
|
|
276
|
+
errorMessage: event.message,
|
|
277
|
+
sessionId: event.sessionId
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
},
|
|
281
|
+
onPublisherCreated: (p) => {
|
|
282
|
+
addEvent({
|
|
283
|
+
code: streamingEvents.strStreamPublisherCreated,
|
|
284
|
+
payload: {
|
|
285
|
+
message: "Recording publisher created",
|
|
286
|
+
sessionId: p.sessionId,
|
|
287
|
+
streamId: p.streamId
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
onPublisherError: (p) => {
|
|
292
|
+
addEvent({
|
|
293
|
+
code: streamingEvents.strStreamPublisherDidFailWithError,
|
|
294
|
+
payload: {
|
|
295
|
+
message: "Recording publisher failed due to an error",
|
|
296
|
+
sessionId: p.sessionId,
|
|
297
|
+
streamId: p.streamId,
|
|
298
|
+
error: { message: p.message ?? "Unknown error" }
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
await startRecording({
|
|
305
|
+
videoRecordingId: session.videoRecordingId,
|
|
306
|
+
type: "selfie",
|
|
307
|
+
resolution,
|
|
308
|
+
hasAudio
|
|
309
|
+
});
|
|
310
|
+
addEvent({
|
|
311
|
+
code: streamingEvents.strStreamVideoCaptureStart,
|
|
312
|
+
payload: {
|
|
313
|
+
message: "Recording capture started",
|
|
314
|
+
resolution,
|
|
315
|
+
videoRecordingId: session.videoRecordingId,
|
|
316
|
+
sessionId: session.sessionId,
|
|
317
|
+
streamId: connection.publisher.getStreamId()
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
return {
|
|
321
|
+
token: session.token,
|
|
322
|
+
sessionId: session.sessionId,
|
|
323
|
+
videoRecordingId: session.videoRecordingId,
|
|
324
|
+
connection,
|
|
325
|
+
resolution,
|
|
326
|
+
hasAudio
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function stopRecording(session) {
|
|
330
|
+
(async () => {
|
|
331
|
+
try {
|
|
332
|
+
addEvent({
|
|
333
|
+
code: streamingEvents.strStreamVideoCaptureStop,
|
|
334
|
+
payload: {
|
|
335
|
+
message: "Recording capture stopped",
|
|
336
|
+
videoRecordingId: session.videoRecordingId,
|
|
337
|
+
sessionId: session.sessionId,
|
|
338
|
+
streamId: session.connection.publisher.getStreamId()
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
await stopRecording$1(session.videoRecordingId);
|
|
342
|
+
addEvent({
|
|
343
|
+
code: streamingEvents.strStreamPublisherDestroyed,
|
|
344
|
+
payload: {
|
|
345
|
+
message: "Recording publisher destroyed",
|
|
346
|
+
sessionId: session.sessionId,
|
|
347
|
+
streamId: session.connection.publisher.getStreamId()
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
} finally {
|
|
351
|
+
await session.connection.disconnect();
|
|
352
|
+
addEvent({
|
|
353
|
+
code: streamingEvents.strSessionDidDisconnect,
|
|
354
|
+
payload: {
|
|
355
|
+
message: "Recording session disconnected",
|
|
356
|
+
sessionId: session.sessionId
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
})();
|
|
361
|
+
}
|
|
362
|
+
async function initializeDeepsightSession(config) {
|
|
363
|
+
if (config.deepsightLiveness !== "VIDEOLIVENESS") return;
|
|
364
|
+
const { loadDeepsightSession } = await import("./deepsightLoader-Bn2D0REl.esm.js");
|
|
365
|
+
return loadDeepsightSession();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
//#endregion
|
|
369
|
+
//#region src/modules/selfie/selfieStateMachine.ts
|
|
370
|
+
const _selfieMachine = setup({
|
|
371
|
+
types: {
|
|
372
|
+
context: {},
|
|
373
|
+
events: {},
|
|
374
|
+
input: {}
|
|
375
|
+
},
|
|
376
|
+
actors: {
|
|
377
|
+
checkPermission: fromPromise(async () => {
|
|
378
|
+
return checkPermission();
|
|
379
|
+
}),
|
|
380
|
+
requestPermission: fromPromise(async () => {
|
|
381
|
+
return requestPermission();
|
|
382
|
+
}),
|
|
383
|
+
initializeCamera: fromPromise(async ({ input }) => {
|
|
384
|
+
return initializeCamera(input);
|
|
385
|
+
}),
|
|
386
|
+
runDetection: fromCallback(({ input, sendBack }) => {
|
|
387
|
+
if (!input.frameCapturer || !input.provider) {
|
|
388
|
+
sendBack({
|
|
389
|
+
type: "DETECTION_UPDATE",
|
|
390
|
+
status: "error"
|
|
391
|
+
});
|
|
392
|
+
return () => {};
|
|
393
|
+
}
|
|
394
|
+
const { cleanup, reset } = startDetection({
|
|
395
|
+
config: input.config,
|
|
396
|
+
capturer: input.frameCapturer,
|
|
397
|
+
onUpdate: (status) => sendBack({
|
|
398
|
+
type: "DETECTION_UPDATE",
|
|
399
|
+
status
|
|
400
|
+
}),
|
|
401
|
+
onFrame: (frame) => sendBack({
|
|
402
|
+
type: "DETECTION_FRAME",
|
|
403
|
+
frame
|
|
404
|
+
}),
|
|
405
|
+
onSuccess: (canvas, faceCoordinates) => sendBack({
|
|
406
|
+
type: "DETECTION_SUCCESS",
|
|
407
|
+
canvas,
|
|
408
|
+
faceCoordinates
|
|
409
|
+
}),
|
|
410
|
+
provider: input.provider
|
|
411
|
+
});
|
|
412
|
+
sendBack({
|
|
413
|
+
type: "DETECTION_RESET_READY",
|
|
414
|
+
reset
|
|
415
|
+
});
|
|
416
|
+
return cleanup;
|
|
417
|
+
}),
|
|
418
|
+
initializeDeepsightSession: fromPromise(async ({ input }) => {
|
|
419
|
+
return await initializeDeepsightSession(input.config);
|
|
420
|
+
}),
|
|
421
|
+
uploadSelfie: fromPromise(async ({ input, signal }) => {
|
|
422
|
+
let metadata;
|
|
423
|
+
let recordingId = null;
|
|
424
|
+
if (input.deepsightService) {
|
|
425
|
+
const imageData = input.canvas.getImageData();
|
|
426
|
+
const sessionToken = getToken();
|
|
427
|
+
if (imageData && sessionToken) {
|
|
428
|
+
const result = await input.deepsightService.performCapture(sessionToken, imageData);
|
|
429
|
+
metadata = result.metadata;
|
|
430
|
+
recordingId = result.recordingId;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return uploadSelfie({
|
|
434
|
+
encryptedBase64Image: await encryptSelfieImage({ canvas: input.canvas }),
|
|
435
|
+
faceCoordinates: input.faceCoordinates,
|
|
436
|
+
metadata,
|
|
437
|
+
recordingId,
|
|
438
|
+
signal
|
|
439
|
+
});
|
|
440
|
+
}),
|
|
441
|
+
startRecording: fromPromise(async ({ input }) => {
|
|
442
|
+
if (!input.clonedStream) return;
|
|
443
|
+
return startRecordingSession({
|
|
444
|
+
config: input.config,
|
|
445
|
+
clonedStream: input.clonedStream,
|
|
446
|
+
existing: input.existing
|
|
447
|
+
});
|
|
448
|
+
})
|
|
449
|
+
},
|
|
450
|
+
actions: {
|
|
451
|
+
stopMediaStream: assign(({ context }) => {
|
|
452
|
+
context.frameCapturer?.dispose();
|
|
453
|
+
if (context.stream) stopStream(context.stream);
|
|
454
|
+
context.provider?.dispose();
|
|
455
|
+
return {
|
|
456
|
+
stream: void 0,
|
|
457
|
+
provider: void 0,
|
|
458
|
+
frameCapturer: void 0
|
|
459
|
+
};
|
|
460
|
+
}),
|
|
461
|
+
setStreamAndCapturer: assign({
|
|
462
|
+
stream: ({ event }) => {
|
|
463
|
+
if ("output" in event) return event.output.stream;
|
|
464
|
+
},
|
|
465
|
+
provider: ({ event }) => {
|
|
466
|
+
if ("output" in event) return event.output.provider;
|
|
467
|
+
},
|
|
468
|
+
frameCapturer: ({ event }) => {
|
|
469
|
+
if ("output" in event) return new StreamCanvasCapture(event.output.stream);
|
|
470
|
+
}
|
|
471
|
+
}),
|
|
472
|
+
trackTutorialSelfie: () => {
|
|
473
|
+
addEvent({
|
|
474
|
+
code: "tutorialSelfie",
|
|
475
|
+
payload: { tutorialSelfie: true }
|
|
476
|
+
});
|
|
477
|
+
},
|
|
478
|
+
trackContinue: () => {
|
|
479
|
+
addEvent({ code: "continue" });
|
|
480
|
+
},
|
|
481
|
+
resetContext: assign(({ context }) => ({
|
|
482
|
+
stream: void 0,
|
|
483
|
+
provider: void 0,
|
|
484
|
+
frameCapturer: void 0,
|
|
485
|
+
error: void 0,
|
|
486
|
+
detectionStatus: "idle",
|
|
487
|
+
debugFrame: void 0,
|
|
488
|
+
capturedImage: void 0,
|
|
489
|
+
faceCoordinates: void 0,
|
|
490
|
+
uploadResponse: void 0,
|
|
491
|
+
recordingSession: void 0,
|
|
492
|
+
recordingStream: void 0,
|
|
493
|
+
attemptsRemaining: context.config.captureAttempts,
|
|
494
|
+
uploadError: void 0,
|
|
495
|
+
permissionResult: void 0,
|
|
496
|
+
resetDetection: void 0,
|
|
497
|
+
deepsightService: void 0
|
|
498
|
+
})),
|
|
499
|
+
resetDetection: ({ context }) => {
|
|
500
|
+
context.resetDetection?.();
|
|
501
|
+
},
|
|
502
|
+
captureImage: assign({ capturedImage: ({ context }) => {
|
|
503
|
+
if (context.capturedImage) return context.capturedImage;
|
|
504
|
+
return context.frameCapturer?.getLatestCanvas() ?? void 0;
|
|
505
|
+
} }),
|
|
506
|
+
captureLatestFrame: assign({ capturedImage: ({ context }) => {
|
|
507
|
+
return context.frameCapturer?.getLatestCanvas() ?? void 0;
|
|
508
|
+
} }),
|
|
509
|
+
clearUploadFailure: assign({
|
|
510
|
+
uploadError: () => void 0,
|
|
511
|
+
detectionStatus: () => "idle",
|
|
512
|
+
capturedImage: () => void 0
|
|
513
|
+
}),
|
|
514
|
+
decrementAttemptsRemaining: assign(({ context }) => ({ attemptsRemaining: context.attemptsRemaining - 1 })),
|
|
515
|
+
setUploadErrorFromUploadValidation: assign({ uploadError: ({ context }) => validateUploadResponse({
|
|
516
|
+
config: context.config,
|
|
517
|
+
response: context.uploadResponse
|
|
518
|
+
}) ?? FACE_ERROR_CODES.SERVER }),
|
|
519
|
+
stopMediaRecording: ({ context }) => {
|
|
520
|
+
if (context.recordingSession) stopRecording(context.recordingSession);
|
|
521
|
+
},
|
|
522
|
+
clearRecordingSession: assign({
|
|
523
|
+
recordingSession: () => void 0,
|
|
524
|
+
recordingStream: () => void 0
|
|
525
|
+
}),
|
|
526
|
+
createRecordingStream: assign({ recordingStream: ({ context }) => {
|
|
527
|
+
if (!context.config.enableFaceRecording) return;
|
|
528
|
+
if (context.recordingStream) return context.recordingStream;
|
|
529
|
+
return context.stream?.clone();
|
|
530
|
+
} }),
|
|
531
|
+
stopRecordingStream: ({ context }) => {
|
|
532
|
+
if (context.recordingStream) stopStream(context.recordingStream);
|
|
533
|
+
},
|
|
534
|
+
startDeepsightRecording: ({ context }) => {
|
|
535
|
+
if (context.deepsightService && context.stream) context.deepsightService.startRecording(context.stream);
|
|
536
|
+
},
|
|
537
|
+
checkVirtualCamera: ({ context }) => {
|
|
538
|
+
(async () => {
|
|
539
|
+
if (context.deepsightService && context.stream) {
|
|
540
|
+
const videoTrack = context.stream.getVideoTracks()[0];
|
|
541
|
+
if (videoTrack) await context.deepsightService.checkVirtualCamera(videoTrack);
|
|
542
|
+
}
|
|
543
|
+
})();
|
|
544
|
+
},
|
|
545
|
+
cleanupDeepsight: ({ context }) => {
|
|
546
|
+
context.deepsightService?.cleanup();
|
|
547
|
+
}
|
|
548
|
+
},
|
|
549
|
+
guards: {
|
|
550
|
+
hasShowTutorial: ({ context }) => context.config.showTutorial,
|
|
551
|
+
isPermissionGranted: ({ event }) => {
|
|
552
|
+
if ("output" in event) return event.output === "granted";
|
|
553
|
+
return false;
|
|
554
|
+
},
|
|
555
|
+
isPermissionDeniedError: ({ event }) => {
|
|
556
|
+
if ("error" in event) {
|
|
557
|
+
const error = event.error;
|
|
558
|
+
return error?.name === "NotAllowedError" || error?.name === "PermissionDeniedError";
|
|
559
|
+
}
|
|
560
|
+
return false;
|
|
561
|
+
},
|
|
562
|
+
hasStream: ({ context }) => context.stream !== void 0,
|
|
563
|
+
isCameraAndDeepsightReady: ({ context }) => context.stream !== void 0 && (context.config.deepsightLiveness !== "VIDEOLIVENESS" || context.deepsightService !== void 0),
|
|
564
|
+
hasAttemptsRemaining: ({ context }) => context.attemptsRemaining > 0,
|
|
565
|
+
hasCapturedImage: ({ context }) => context.capturedImage !== void 0,
|
|
566
|
+
hasUploadValidationError: ({ context }) => validateUploadResponse({
|
|
567
|
+
config: context.config,
|
|
568
|
+
response: context.uploadResponse
|
|
569
|
+
}) !== void 0
|
|
570
|
+
}
|
|
571
|
+
}).createMachine({
|
|
572
|
+
id: "selfie",
|
|
573
|
+
initial: "idle",
|
|
574
|
+
context: ({ input }) => ({
|
|
575
|
+
config: input.config,
|
|
576
|
+
stream: void 0,
|
|
577
|
+
provider: void 0,
|
|
578
|
+
frameCapturer: void 0,
|
|
579
|
+
error: void 0,
|
|
580
|
+
detectionStatus: "idle",
|
|
581
|
+
debugFrame: void 0,
|
|
582
|
+
capturedImage: void 0,
|
|
583
|
+
faceCoordinates: void 0,
|
|
584
|
+
uploadResponse: void 0,
|
|
585
|
+
recordingSession: void 0,
|
|
586
|
+
recordingStream: void 0,
|
|
587
|
+
attemptsRemaining: input.config.captureAttempts,
|
|
588
|
+
uploadError: void 0,
|
|
589
|
+
permissionResult: void 0,
|
|
590
|
+
resetDetection: void 0,
|
|
591
|
+
deepsightService: void 0
|
|
592
|
+
}),
|
|
593
|
+
on: { QUIT: { target: "#selfie.closed" } },
|
|
594
|
+
states: {
|
|
595
|
+
idle: { on: { LOAD: [{
|
|
596
|
+
target: "tutorial",
|
|
597
|
+
guard: "hasShowTutorial"
|
|
598
|
+
}, { target: "loading" }] } },
|
|
599
|
+
loading: { invoke: [{
|
|
600
|
+
id: "checkPermissionLoading",
|
|
601
|
+
src: "checkPermission",
|
|
602
|
+
onDone: [{
|
|
603
|
+
target: "capture",
|
|
604
|
+
guard: "isPermissionGranted",
|
|
605
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
606
|
+
}, {
|
|
607
|
+
target: "permissions",
|
|
608
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
609
|
+
}]
|
|
610
|
+
}, {
|
|
611
|
+
id: "loadingInitDeepsight",
|
|
612
|
+
src: "initializeDeepsightSession",
|
|
613
|
+
input: ({ context }) => ({ config: context.config }),
|
|
614
|
+
onDone: { actions: assign({ deepsightService: ({ event }) => event.output }) },
|
|
615
|
+
onError: { actions: () => void 0 }
|
|
616
|
+
}] },
|
|
617
|
+
tutorial: {
|
|
618
|
+
initial: "checkingPermission",
|
|
619
|
+
entry: "trackTutorialSelfie",
|
|
620
|
+
states: {
|
|
621
|
+
checkingPermission: { invoke: {
|
|
622
|
+
id: "checkPermissionTutorial",
|
|
623
|
+
src: "checkPermission",
|
|
624
|
+
onDone: [{
|
|
625
|
+
target: "initializingCamera",
|
|
626
|
+
guard: "isPermissionGranted",
|
|
627
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
628
|
+
}, {
|
|
629
|
+
target: "ready",
|
|
630
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
631
|
+
}]
|
|
632
|
+
} },
|
|
633
|
+
initializingCamera: {
|
|
634
|
+
initial: "booting",
|
|
635
|
+
invoke: [{
|
|
636
|
+
id: "tutorialInitCamera",
|
|
637
|
+
src: "initializeCamera",
|
|
638
|
+
input: ({ context }) => context.config,
|
|
639
|
+
onDone: { actions: "setStreamAndCapturer" },
|
|
640
|
+
onError: [{
|
|
641
|
+
target: "ready",
|
|
642
|
+
guard: "isPermissionDeniedError",
|
|
643
|
+
actions: assign({ permissionResult: () => "denied" })
|
|
644
|
+
}, {
|
|
645
|
+
target: "ready",
|
|
646
|
+
actions: assign({ error: ({ event }) => String(event.error) })
|
|
647
|
+
}]
|
|
648
|
+
}, {
|
|
649
|
+
id: "tutorialInitDeepsight",
|
|
650
|
+
src: "initializeDeepsightSession",
|
|
651
|
+
input: ({ context }) => ({ config: context.config }),
|
|
652
|
+
onDone: { actions: assign({ deepsightService: ({ event }) => event.output }) },
|
|
653
|
+
onError: { actions: () => void 0 }
|
|
654
|
+
}],
|
|
655
|
+
states: {
|
|
656
|
+
booting: {
|
|
657
|
+
always: [{
|
|
658
|
+
target: "#tutorialCameraReady",
|
|
659
|
+
guard: "isCameraAndDeepsightReady"
|
|
660
|
+
}],
|
|
661
|
+
on: { NEXT_STEP: {
|
|
662
|
+
target: "waitingForStream",
|
|
663
|
+
actions: "trackContinue"
|
|
664
|
+
} }
|
|
665
|
+
},
|
|
666
|
+
waitingForStream: { always: [{
|
|
667
|
+
target: "#selfie.capture",
|
|
668
|
+
guard: "isCameraAndDeepsightReady"
|
|
669
|
+
}] }
|
|
670
|
+
}
|
|
671
|
+
},
|
|
672
|
+
cameraReady: {
|
|
673
|
+
id: "tutorialCameraReady",
|
|
674
|
+
on: { NEXT_STEP: {
|
|
675
|
+
target: "#selfie.capture",
|
|
676
|
+
actions: "trackContinue"
|
|
677
|
+
} }
|
|
678
|
+
},
|
|
679
|
+
ready: {
|
|
680
|
+
initial: "idle",
|
|
681
|
+
states: {
|
|
682
|
+
idle: { always: [{
|
|
683
|
+
target: "initializingDeepsight",
|
|
684
|
+
guard: ({ context }) => context.config.deepsightLiveness === "VIDEOLIVENESS"
|
|
685
|
+
}, { target: "readyForNext" }] },
|
|
686
|
+
initializingDeepsight: { invoke: {
|
|
687
|
+
id: "initializeDeepsightTutorial",
|
|
688
|
+
src: "initializeDeepsightSession",
|
|
689
|
+
input: ({ context }) => ({ config: context.config }),
|
|
690
|
+
onDone: {
|
|
691
|
+
target: "readyForNext",
|
|
692
|
+
actions: assign({ deepsightService: ({ event }) => event.output })
|
|
693
|
+
},
|
|
694
|
+
onError: { target: "readyForNext" }
|
|
695
|
+
} },
|
|
696
|
+
readyForNext: { on: { NEXT_STEP: {
|
|
697
|
+
target: "#selfie.tutorial.waitingForPermission",
|
|
698
|
+
actions: "trackContinue"
|
|
699
|
+
} } }
|
|
700
|
+
}
|
|
701
|
+
},
|
|
702
|
+
waitingForPermission: { invoke: {
|
|
703
|
+
id: "checkPermissionWaiting",
|
|
704
|
+
src: "checkPermission",
|
|
705
|
+
onDone: [{
|
|
706
|
+
target: "#selfie.capture",
|
|
707
|
+
guard: "isPermissionGranted",
|
|
708
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
709
|
+
}, {
|
|
710
|
+
target: "#selfie.permissions",
|
|
711
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
712
|
+
}]
|
|
713
|
+
} }
|
|
714
|
+
}
|
|
715
|
+
},
|
|
716
|
+
permissions: {
|
|
717
|
+
initial: "checkingDeepsight",
|
|
718
|
+
states: {
|
|
719
|
+
checkingDeepsight: { always: [{
|
|
720
|
+
target: "initializingDeepsight",
|
|
721
|
+
guard: ({ context }) => context.config.deepsightLiveness === "VIDEOLIVENESS" && context.deepsightService === void 0
|
|
722
|
+
}, { target: "idle" }] },
|
|
723
|
+
initializingDeepsight: { invoke: {
|
|
724
|
+
id: "initializeDeepsightPerms",
|
|
725
|
+
src: "initializeDeepsightSession",
|
|
726
|
+
input: ({ context }) => ({ config: context.config }),
|
|
727
|
+
onDone: {
|
|
728
|
+
target: "idle",
|
|
729
|
+
actions: assign({ deepsightService: ({ event }) => event.output })
|
|
730
|
+
},
|
|
731
|
+
onError: { target: "idle" }
|
|
732
|
+
} },
|
|
733
|
+
idle: {
|
|
734
|
+
invoke: {
|
|
735
|
+
id: "checkPermissionIdle",
|
|
736
|
+
src: "checkPermission",
|
|
737
|
+
onDone: [{
|
|
738
|
+
target: "#selfie.capture",
|
|
739
|
+
guard: "isPermissionGranted",
|
|
740
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
741
|
+
}, {
|
|
742
|
+
target: "denied",
|
|
743
|
+
guard: ({ event }) => event.output === "denied",
|
|
744
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
745
|
+
}]
|
|
746
|
+
},
|
|
747
|
+
on: {
|
|
748
|
+
REQUEST_PERMISSION: "requesting",
|
|
749
|
+
GO_TO_LEARN_MORE: "learnMore"
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
learnMore: { on: {
|
|
753
|
+
BACK: "idle",
|
|
754
|
+
REQUEST_PERMISSION: "requesting"
|
|
755
|
+
} },
|
|
756
|
+
requesting: { invoke: {
|
|
757
|
+
id: "requestPermission",
|
|
758
|
+
src: "requestPermission",
|
|
759
|
+
onDone: [
|
|
760
|
+
{
|
|
761
|
+
target: "#selfie.capture",
|
|
762
|
+
guard: "isPermissionGranted",
|
|
763
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
764
|
+
},
|
|
765
|
+
{
|
|
766
|
+
target: "denied",
|
|
767
|
+
guard: ({ event }) => event.output === "denied",
|
|
768
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
769
|
+
},
|
|
770
|
+
{
|
|
771
|
+
target: "idle",
|
|
772
|
+
actions: assign({ permissionResult: ({ event }) => event.output })
|
|
773
|
+
}
|
|
774
|
+
],
|
|
775
|
+
onError: { target: "denied" }
|
|
776
|
+
} },
|
|
777
|
+
denied: { entry: assign({ permissionResult: () => "refresh" }) }
|
|
778
|
+
}
|
|
779
|
+
},
|
|
780
|
+
capture: {
|
|
781
|
+
initial: "checkingDeepsight",
|
|
782
|
+
exit: [
|
|
783
|
+
"stopMediaRecording",
|
|
784
|
+
"stopRecordingStream",
|
|
785
|
+
"clearRecordingSession",
|
|
786
|
+
"stopMediaStream",
|
|
787
|
+
"cleanupDeepsight"
|
|
788
|
+
],
|
|
789
|
+
states: {
|
|
790
|
+
checkingDeepsight: { always: [{
|
|
791
|
+
target: "initializingDeepsight",
|
|
792
|
+
guard: ({ context }) => context.config.deepsightLiveness === "VIDEOLIVENESS" && context.deepsightService === void 0
|
|
793
|
+
}, { target: "checkingStream" }] },
|
|
794
|
+
initializingDeepsight: { invoke: {
|
|
795
|
+
id: "initializeDeepsightCapture",
|
|
796
|
+
src: "initializeDeepsightSession",
|
|
797
|
+
input: ({ context }) => ({ config: context.config }),
|
|
798
|
+
onDone: {
|
|
799
|
+
target: "checkingStream",
|
|
800
|
+
actions: [assign({ deepsightService: ({ event }) => event.output })]
|
|
801
|
+
},
|
|
802
|
+
onError: { target: "checkingStream" }
|
|
803
|
+
} },
|
|
804
|
+
checkingStream: { always: [{
|
|
805
|
+
target: "detecting",
|
|
806
|
+
guard: "hasStream"
|
|
807
|
+
}, { target: "initializing" }] },
|
|
808
|
+
initializing: { invoke: {
|
|
809
|
+
id: "initializeCamera",
|
|
810
|
+
src: "initializeCamera",
|
|
811
|
+
input: ({ context }) => context.config,
|
|
812
|
+
onDone: {
|
|
813
|
+
target: "detecting",
|
|
814
|
+
actions: ["setStreamAndCapturer"]
|
|
815
|
+
},
|
|
816
|
+
onError: [{
|
|
817
|
+
target: "#selfie.permissions",
|
|
818
|
+
guard: "isPermissionDeniedError",
|
|
819
|
+
actions: assign({ permissionResult: () => "denied" })
|
|
820
|
+
}, {
|
|
821
|
+
target: "#selfie.error",
|
|
822
|
+
actions: assign({ error: ({ event }) => String(event.error) })
|
|
823
|
+
}]
|
|
824
|
+
} },
|
|
825
|
+
detecting: {
|
|
826
|
+
entry: [
|
|
827
|
+
assign({ detectionStatus: () => "detecting" }),
|
|
828
|
+
"createRecordingStream",
|
|
829
|
+
"checkVirtualCamera",
|
|
830
|
+
"startDeepsightRecording"
|
|
831
|
+
],
|
|
832
|
+
invoke: [{
|
|
833
|
+
id: "startRecording",
|
|
834
|
+
src: "startRecording",
|
|
835
|
+
input: ({ context }) => ({
|
|
836
|
+
config: context.config,
|
|
837
|
+
clonedStream: context.recordingStream,
|
|
838
|
+
existing: context.recordingSession
|
|
839
|
+
}),
|
|
840
|
+
onDone: { actions: assign({ recordingSession: ({ context, event }) => {
|
|
841
|
+
return event.output ?? context.recordingSession;
|
|
842
|
+
} }) },
|
|
843
|
+
onError: { actions: () => void 0 }
|
|
844
|
+
}, {
|
|
845
|
+
id: "runDetection",
|
|
846
|
+
src: "runDetection",
|
|
847
|
+
input: ({ context }) => ({
|
|
848
|
+
frameCapturer: context.frameCapturer,
|
|
849
|
+
provider: context.provider,
|
|
850
|
+
config: context.config
|
|
851
|
+
})
|
|
852
|
+
}],
|
|
853
|
+
on: {
|
|
854
|
+
DETECTION_UPDATE: { actions: assign({ detectionStatus: ({ event }) => event.status }) },
|
|
855
|
+
DETECTION_FRAME: { actions: assign({ debugFrame: ({ event }) => event.frame }) },
|
|
856
|
+
DETECTION_RESET_READY: { actions: assign({ resetDetection: ({ event }) => event.reset }) },
|
|
857
|
+
DETECTION_SUCCESS: {
|
|
858
|
+
target: "capturing",
|
|
859
|
+
actions: assign({
|
|
860
|
+
capturedImage: ({ event }) => event.canvas,
|
|
861
|
+
faceCoordinates: ({ event }) => event.faceCoordinates
|
|
862
|
+
})
|
|
863
|
+
},
|
|
864
|
+
MANUAL_CAPTURE: { target: "capturingManual" }
|
|
865
|
+
}
|
|
866
|
+
},
|
|
867
|
+
capturing: {
|
|
868
|
+
entry: ["captureImage"],
|
|
869
|
+
always: [{
|
|
870
|
+
target: "uploading",
|
|
871
|
+
guard: "hasCapturedImage"
|
|
872
|
+
}, {
|
|
873
|
+
target: "uploadError",
|
|
874
|
+
actions: assign(({ context }) => ({
|
|
875
|
+
uploadError: FACE_ERROR_CODES.SERVER,
|
|
876
|
+
attemptsRemaining: context.attemptsRemaining - 1
|
|
877
|
+
}))
|
|
878
|
+
}]
|
|
879
|
+
},
|
|
880
|
+
capturingManual: {
|
|
881
|
+
entry: ["captureLatestFrame"],
|
|
882
|
+
always: [{
|
|
883
|
+
target: "uploading",
|
|
884
|
+
guard: "hasCapturedImage"
|
|
885
|
+
}, {
|
|
886
|
+
target: "uploadError",
|
|
887
|
+
actions: assign(({ context }) => ({
|
|
888
|
+
uploadError: FACE_ERROR_CODES.SERVER,
|
|
889
|
+
attemptsRemaining: context.attemptsRemaining - 1
|
|
890
|
+
}))
|
|
891
|
+
}]
|
|
892
|
+
},
|
|
893
|
+
uploading: { invoke: {
|
|
894
|
+
id: "uploadSelfie",
|
|
895
|
+
src: "uploadSelfie",
|
|
896
|
+
input: ({ context }) => {
|
|
897
|
+
const canvas = context.capturedImage;
|
|
898
|
+
if (!canvas) throw new Error(FACE_ERROR_CODES.SERVER);
|
|
899
|
+
return {
|
|
900
|
+
canvas,
|
|
901
|
+
faceCoordinates: context.faceCoordinates,
|
|
902
|
+
deepsightService: context.deepsightService
|
|
903
|
+
};
|
|
904
|
+
},
|
|
905
|
+
onDone: {
|
|
906
|
+
target: "validatingUpload",
|
|
907
|
+
actions: assign({ uploadResponse: ({ event }) => event.output })
|
|
908
|
+
},
|
|
909
|
+
onError: {
|
|
910
|
+
target: "uploadError",
|
|
911
|
+
actions: assign(({ context, event }) => ({
|
|
912
|
+
uploadError: getFaceErrorCodeFromUnknown(event.error) ?? FACE_ERROR_CODES.SERVER,
|
|
913
|
+
attemptsRemaining: context.attemptsRemaining - 1
|
|
914
|
+
}))
|
|
915
|
+
}
|
|
916
|
+
} },
|
|
917
|
+
validatingUpload: { always: [{
|
|
918
|
+
target: "uploadError",
|
|
919
|
+
guard: "hasUploadValidationError",
|
|
920
|
+
actions: ["setUploadErrorFromUploadValidation", "decrementAttemptsRemaining"]
|
|
921
|
+
}, { target: "success" }] },
|
|
922
|
+
uploadError: { on: { RETRY_CAPTURE: {
|
|
923
|
+
target: "detecting",
|
|
924
|
+
guard: "hasAttemptsRemaining",
|
|
925
|
+
actions: ["resetDetection", "clearUploadFailure"]
|
|
926
|
+
} } },
|
|
927
|
+
success: {
|
|
928
|
+
entry: "stopMediaRecording",
|
|
929
|
+
after: { 3e3: { target: "#selfie.finished" } }
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
},
|
|
933
|
+
finished: {
|
|
934
|
+
entry: "stopMediaStream",
|
|
935
|
+
on: { RESET: {
|
|
936
|
+
target: "idle",
|
|
937
|
+
actions: "resetContext"
|
|
938
|
+
} }
|
|
939
|
+
},
|
|
940
|
+
closed: {
|
|
941
|
+
entry: "stopMediaStream",
|
|
942
|
+
type: "final"
|
|
943
|
+
},
|
|
944
|
+
error: {
|
|
945
|
+
entry: "stopMediaStream",
|
|
946
|
+
on: { RESET: {
|
|
947
|
+
target: "idle",
|
|
948
|
+
actions: "resetContext"
|
|
949
|
+
} }
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
});
|
|
953
|
+
/**
|
|
954
|
+
* The selfie capture state machine.
|
|
955
|
+
*
|
|
956
|
+
* Note: Uses AnyStateMachine type for declaration file portability.
|
|
957
|
+
* Type safety is ensured via the machine configuration.
|
|
958
|
+
*/
|
|
959
|
+
const selfieMachine = _selfieMachine;
|
|
960
|
+
|
|
961
|
+
//#endregion
|
|
962
|
+
//#region src/modules/selfie/selfieActor.ts
|
|
963
|
+
function createSelfieActor(options) {
|
|
964
|
+
return createActor(selfieMachine, { input: { config: options.config } }).start();
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
//#endregion
|
|
968
|
+
//#region src/modules/selfie/selfieManager.ts
|
|
969
|
+
function getPermissionStatus(snapshot) {
|
|
970
|
+
if (!snapshot.matches("permissions")) return;
|
|
971
|
+
if (snapshot.matches({ permissions: "idle" })) return "idle";
|
|
972
|
+
if (snapshot.matches({ permissions: "learnMore" })) return "learnMore";
|
|
973
|
+
if (snapshot.matches({ permissions: "requesting" })) return "requesting";
|
|
974
|
+
if (snapshot.matches({ permissions: "denied" })) return "denied";
|
|
975
|
+
}
|
|
976
|
+
function getCaptureStatus(snapshot) {
|
|
977
|
+
if (snapshot.matches({ capture: "initializing" })) return "initializing";
|
|
978
|
+
if (snapshot.matches({ capture: "detecting" })) return "detecting";
|
|
979
|
+
if (snapshot.matches({ capture: "capturing" })) return "capturing";
|
|
980
|
+
if (snapshot.matches({ capture: "capturingManual" })) return "capturing";
|
|
981
|
+
if (snapshot.matches({ capture: "uploading" })) return "uploading";
|
|
982
|
+
if (snapshot.matches({ capture: "uploadError" })) return "uploadError";
|
|
983
|
+
if (snapshot.matches({ capture: "success" })) return "success";
|
|
984
|
+
}
|
|
985
|
+
function mapState(snapshot) {
|
|
986
|
+
const { context } = snapshot;
|
|
987
|
+
if (snapshot.matches("idle")) return { status: "idle" };
|
|
988
|
+
if (snapshot.matches("loading")) return { status: "loading" };
|
|
989
|
+
if (snapshot.matches("tutorial")) return { status: "tutorial" };
|
|
990
|
+
if (snapshot.matches("closed")) return { status: "closed" };
|
|
991
|
+
if (snapshot.matches("permissions")) {
|
|
992
|
+
const permissionStatus = getPermissionStatus(snapshot);
|
|
993
|
+
if (permissionStatus === void 0) return {
|
|
994
|
+
status: "permissions",
|
|
995
|
+
permissionStatus: "idle"
|
|
996
|
+
};
|
|
997
|
+
return {
|
|
998
|
+
status: "permissions",
|
|
999
|
+
permissionStatus
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
if (snapshot.matches("capture")) return {
|
|
1003
|
+
status: "capture",
|
|
1004
|
+
captureStatus: getCaptureStatus(snapshot) ?? "initializing",
|
|
1005
|
+
stream: context.stream,
|
|
1006
|
+
detectionStatus: context.detectionStatus,
|
|
1007
|
+
debugFrame: context.debugFrame,
|
|
1008
|
+
attemptsRemaining: context.attemptsRemaining,
|
|
1009
|
+
uploadError: context.uploadError
|
|
1010
|
+
};
|
|
1011
|
+
if (snapshot.matches("finished")) return { status: "finished" };
|
|
1012
|
+
if (snapshot.matches("error")) return {
|
|
1013
|
+
status: "error",
|
|
1014
|
+
error: context.error ?? "Unknown error"
|
|
1015
|
+
};
|
|
1016
|
+
return { status: "idle" };
|
|
1017
|
+
}
|
|
1018
|
+
function createApi({ actor }) {
|
|
1019
|
+
return {
|
|
1020
|
+
load() {
|
|
1021
|
+
actor.send({ type: "LOAD" });
|
|
1022
|
+
},
|
|
1023
|
+
nextStep() {
|
|
1024
|
+
actor.send({ type: "NEXT_STEP" });
|
|
1025
|
+
},
|
|
1026
|
+
requestPermission() {
|
|
1027
|
+
actor.send({ type: "REQUEST_PERMISSION" });
|
|
1028
|
+
},
|
|
1029
|
+
goToLearnMore() {
|
|
1030
|
+
actor.send({ type: "GO_TO_LEARN_MORE" });
|
|
1031
|
+
},
|
|
1032
|
+
back() {
|
|
1033
|
+
actor.send({ type: "BACK" });
|
|
1034
|
+
},
|
|
1035
|
+
close() {
|
|
1036
|
+
actor.send({ type: "QUIT" });
|
|
1037
|
+
},
|
|
1038
|
+
reset() {
|
|
1039
|
+
actor.send({ type: "RESET" });
|
|
1040
|
+
},
|
|
1041
|
+
retryCapture() {
|
|
1042
|
+
actor.send({ type: "RETRY_CAPTURE" });
|
|
1043
|
+
},
|
|
1044
|
+
capture() {
|
|
1045
|
+
actor.send({ type: "MANUAL_CAPTURE" });
|
|
1046
|
+
}
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Creates a selfie manager instance for handling selfie capture flow.
|
|
1051
|
+
*
|
|
1052
|
+
* The selfie manager provides:
|
|
1053
|
+
* - State management with statuses: `idle`, `loading`, `tutorial`, `permissions`, `capture`, `finished`, `closed`, `error`
|
|
1054
|
+
* - Permission handling with nested states: `idle`, `requesting`, `denied`, `learnMore`
|
|
1055
|
+
* - Capture handling with nested states: `initializing`, `startingRecorder`, `recordingActive`, `detecting`, `capturing`, `uploading`, `uploadError`, `success`
|
|
1056
|
+
* - Camera stream access when in `capture` state
|
|
1057
|
+
* - Detection status feedback during face detection
|
|
1058
|
+
* - Attempt tracking with `attemptsRemaining`
|
|
1059
|
+
*
|
|
1060
|
+
* @param options - Configuration for the selfie actor
|
|
1061
|
+
* @param options.config - The selfie module configuration from the flow
|
|
1062
|
+
* @returns A manager instance with state subscription, API methods, and lifecycle controls
|
|
1063
|
+
*
|
|
1064
|
+
* @example
|
|
1065
|
+
* ```ts
|
|
1066
|
+
* const selfieManager = createSelfieManager({ config: selfieConfig });
|
|
1067
|
+
*
|
|
1068
|
+
* selfieManager.subscribe((state) => {
|
|
1069
|
+
* if (state.status === 'capture') {
|
|
1070
|
+
* console.log('Camera ready:', state.stream);
|
|
1071
|
+
* console.log('Detection status:', state.detectionStatus);
|
|
1072
|
+
* }
|
|
1073
|
+
* });
|
|
1074
|
+
*
|
|
1075
|
+
* selfieManager.load();
|
|
1076
|
+
* ```
|
|
1077
|
+
*/
|
|
1078
|
+
function createSelfieManager(options) {
|
|
1079
|
+
return createManager({
|
|
1080
|
+
actor: createSelfieActor(options),
|
|
1081
|
+
mapState,
|
|
1082
|
+
createApi
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
//#endregion
|
|
1087
|
+
export { createSelfieManager, selfieMachine };
|