@g9tm/app-diagnostic 0.0.0-stage → 0.2.1
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/package.json +20 -6
- package/src/bytes.ts +48 -0
- package/src/host.ts +124 -0
- package/src/index.ts +30 -0
- package/src/live/bundle.ts +77 -0
- package/src/live/channel.ts +496 -0
- package/src/live/handlers.ts +426 -0
- package/src/live/models.ts +366 -0
- package/src/live/upload.ts +175 -0
- package/src/recording.ts +176 -0
- package/src/store.ts +39 -0
- package/src/strings.generated.ts +242 -0
- package/src/strings.ts +37 -0
- package/src/tester/archive.ts +83 -0
- package/src/tester/compose.ts +99 -0
- package/src/tester/engine.ts +852 -0
- package/src/tester/feed.ts +182 -0
- package/src/tester/gesture.ts +61 -0
- package/src/tester/model.ts +125 -0
- package/src/tester/outbox.ts +131 -0
- package/src/tester/screen.ts +117 -0
- package/src/tester/store.ts +145 -0
- package/src/tester/timeline.ts +98 -0
- package/src/upload.ts +236 -0
- package/src/wire.ts +133 -0
- package/README.md +0 -3
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
// The live channel's payloads as the wire carries them (shared/schema/live-channel.schema.json $defs; the .NET twin is
|
|
2
|
+
// G9AppDiagnostic.Shared/Models). camelCase names, enums as numbers, dates as ISO strings, byte[] as base64. A field the
|
|
3
|
+
// gateway adds later is simply ignored by an older client; a field is added, never renamed.
|
|
4
|
+
|
|
5
|
+
export interface PresencePayload {
|
|
6
|
+
clientId: string;
|
|
7
|
+
username: string;
|
|
8
|
+
displayName: string;
|
|
9
|
+
firstName: string;
|
|
10
|
+
lastName: string;
|
|
11
|
+
fullName: string;
|
|
12
|
+
firstNameEn: string;
|
|
13
|
+
firstNameFa: string;
|
|
14
|
+
lastNameEn: string;
|
|
15
|
+
lastNameFa: string;
|
|
16
|
+
fullNameEn: string;
|
|
17
|
+
fullNameFa: string;
|
|
18
|
+
avatarBase64?: string | null;
|
|
19
|
+
gender: number;
|
|
20
|
+
deviceModel: string;
|
|
21
|
+
deviceManufacturer: string;
|
|
22
|
+
deviceIdiom: string;
|
|
23
|
+
platform: string;
|
|
24
|
+
platformVersion: string;
|
|
25
|
+
appVersion: string;
|
|
26
|
+
/** The branch and commit the app was built from (an older app sends none). */
|
|
27
|
+
buildBranch?: string | null;
|
|
28
|
+
buildCommit?: string | null;
|
|
29
|
+
appBuild: string;
|
|
30
|
+
environment: string;
|
|
31
|
+
culture: string;
|
|
32
|
+
capabilities: string[];
|
|
33
|
+
currentPageName: string;
|
|
34
|
+
connectivityOnline: boolean;
|
|
35
|
+
batteryPercent: number;
|
|
36
|
+
powerSource: string;
|
|
37
|
+
freeDiskBytes: number;
|
|
38
|
+
totalDiskBytes: number;
|
|
39
|
+
managedMemoryBytes: number;
|
|
40
|
+
sqliteDbBytes: number;
|
|
41
|
+
geoDbBytes: number;
|
|
42
|
+
attachmentDbBytes: number;
|
|
43
|
+
lastSeenUtc: string;
|
|
44
|
+
foregroundConsented: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface HelloRequest {
|
|
48
|
+
clientId: string;
|
|
49
|
+
protocolTag: string;
|
|
50
|
+
presence: PresencePayload;
|
|
51
|
+
deviceAuthToken?: string | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface DeviceSettingItem {
|
|
55
|
+
key: string;
|
|
56
|
+
kind: string;
|
|
57
|
+
value: string;
|
|
58
|
+
category: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface HelloResponse {
|
|
62
|
+
accepted: boolean;
|
|
63
|
+
rejectionReason?: string | null;
|
|
64
|
+
serverProtocolMajor?: number;
|
|
65
|
+
deviceAuthToken?: string | null;
|
|
66
|
+
deviceSettings?: DeviceSettingItem[] | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface ForegroundAnnounce {
|
|
70
|
+
consented: boolean;
|
|
71
|
+
consentedAtUtc: string;
|
|
72
|
+
overlayPhase?: 'open' | 'minimized' | 'closed' | null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface PeerInfo {
|
|
76
|
+
peerId: string;
|
|
77
|
+
displayLabel: string;
|
|
78
|
+
roleKind: string;
|
|
79
|
+
scopes: string[];
|
|
80
|
+
connectedAtUtc: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface LogEntryPayload {
|
|
84
|
+
timestampUtc: string;
|
|
85
|
+
level: string;
|
|
86
|
+
category: string;
|
|
87
|
+
message: string;
|
|
88
|
+
exception?: string | null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface LogsBatchPayload {
|
|
92
|
+
entries: LogEntryPayload[];
|
|
93
|
+
isTail: boolean;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface ScreenshotFramePayload {
|
|
97
|
+
widthPx: number;
|
|
98
|
+
heightPx: number;
|
|
99
|
+
mimeType: string;
|
|
100
|
+
dataBase64: string;
|
|
101
|
+
capturedAtUtc: string;
|
|
102
|
+
pageName?: string | null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface ScreenshotErrorPayload {
|
|
106
|
+
requestId: string;
|
|
107
|
+
errorCode: string;
|
|
108
|
+
message?: string | null;
|
|
109
|
+
reportedAtUtc: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface CpuSamplePayload {
|
|
113
|
+
cpuPercent: number;
|
|
114
|
+
processorCount: number;
|
|
115
|
+
sampledAtUtc: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface SqlExecRequestPayload {
|
|
119
|
+
sql: string;
|
|
120
|
+
readOnly: boolean;
|
|
121
|
+
timeoutSeconds: number;
|
|
122
|
+
maxRows: number;
|
|
123
|
+
databaseKey: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface SqlResultPayload {
|
|
127
|
+
columnNames: string[];
|
|
128
|
+
columnTypes: string[];
|
|
129
|
+
rows: Array<Array<string | null>>;
|
|
130
|
+
rowsAffected: number;
|
|
131
|
+
rowsReturned: number;
|
|
132
|
+
durationMs: number;
|
|
133
|
+
errorMessage?: string | null;
|
|
134
|
+
truncated: boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface LogFileDescriptor {
|
|
138
|
+
name: string;
|
|
139
|
+
sizeBytes: number;
|
|
140
|
+
lastWriteUtc: string;
|
|
141
|
+
isActive: boolean;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface LogFileListPayload {
|
|
145
|
+
files: LogFileDescriptor[];
|
|
146
|
+
fileLoggingEnabled: boolean;
|
|
147
|
+
retainedFileLimit: number;
|
|
148
|
+
errorMessage?: string | null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface LogFileReadRequestPayload {
|
|
152
|
+
name: string;
|
|
153
|
+
offset: number;
|
|
154
|
+
maxBytes: number;
|
|
155
|
+
fromEnd: boolean;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface LogFileContentPayload {
|
|
159
|
+
name: string;
|
|
160
|
+
content: string;
|
|
161
|
+
offset: number;
|
|
162
|
+
nextOffset: number;
|
|
163
|
+
totalBytes: number;
|
|
164
|
+
endOfFile: boolean;
|
|
165
|
+
errorMessage?: string | null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface BackupBuildRequestPayload {
|
|
169
|
+
includeSqlite: boolean;
|
|
170
|
+
includeGeoDatabase: boolean;
|
|
171
|
+
includeAttachments: boolean;
|
|
172
|
+
includeLogs: boolean;
|
|
173
|
+
includePreferences: boolean;
|
|
174
|
+
includeSecureStorage: boolean;
|
|
175
|
+
includeErrors: boolean;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** BackupSource (a number on the wire). */
|
|
179
|
+
export const BackupSource = { liveDiagnostic: 0, userReport: 1 } as const;
|
|
180
|
+
|
|
181
|
+
export interface BackupUploadInitRequest {
|
|
182
|
+
clientId: string;
|
|
183
|
+
sha256Hex: string;
|
|
184
|
+
sizeBytes: number;
|
|
185
|
+
isPasswordProtected: boolean;
|
|
186
|
+
resumeToken?: string | null;
|
|
187
|
+
filename: string;
|
|
188
|
+
isSensitive: boolean;
|
|
189
|
+
note?: string | null;
|
|
190
|
+
includeSqlite: boolean;
|
|
191
|
+
includeGeoDatabase: boolean;
|
|
192
|
+
includeAttachments: boolean;
|
|
193
|
+
includeLogs: boolean;
|
|
194
|
+
includePreferences: boolean;
|
|
195
|
+
includeSecureStorage: boolean;
|
|
196
|
+
source: number;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export interface BackupUploadInitResponse {
|
|
200
|
+
accepted: boolean;
|
|
201
|
+
rejectionReason?: string | null;
|
|
202
|
+
token: string;
|
|
203
|
+
resumeFromOffset: number;
|
|
204
|
+
expiresAtUtc?: string;
|
|
205
|
+
cancelledByOperator?: boolean;
|
|
206
|
+
unknownResumeToken?: boolean;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface BackupUploadChunksAck {
|
|
210
|
+
accepted: boolean;
|
|
211
|
+
rejectionReason?: string | null;
|
|
212
|
+
receivedOffset: number;
|
|
213
|
+
cancelledByOperator?: boolean;
|
|
214
|
+
streamBusy?: boolean;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface BackupUploadCompleteRequest {
|
|
218
|
+
token: string;
|
|
219
|
+
sha256Hex: string;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export interface BackupUploadCompleteResponse {
|
|
223
|
+
accepted: boolean;
|
|
224
|
+
rejectionReason?: string | null;
|
|
225
|
+
finalSizeBytes?: number;
|
|
226
|
+
restartFromZero?: boolean;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface RestoreResultPayload {
|
|
230
|
+
transferId: string;
|
|
231
|
+
ok: boolean;
|
|
232
|
+
error?: string | null;
|
|
233
|
+
receivedBytes: number;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface RemoteInputCommand {
|
|
237
|
+
kind: 'tap' | 'down' | 'move' | 'up' | 'text' | string;
|
|
238
|
+
normX: number;
|
|
239
|
+
normY: number;
|
|
240
|
+
text?: string | null;
|
|
241
|
+
seqMs: number;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export interface QaCommandPayload {
|
|
245
|
+
command: string;
|
|
246
|
+
argsJson: string;
|
|
247
|
+
timeoutMs: number;
|
|
248
|
+
leaseId?: string | null;
|
|
249
|
+
/** G9AppPilot: the exact POST /qa body the controller signed; when present the device verifies and runs THIS body. */
|
|
250
|
+
signedBody?: string | null;
|
|
251
|
+
signatureKid?: string | null;
|
|
252
|
+
signatureTs?: string | null;
|
|
253
|
+
signatureNonce?: string | null;
|
|
254
|
+
signature?: string | null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export interface QaResultPayload {
|
|
258
|
+
ok: boolean;
|
|
259
|
+
resultJson: string;
|
|
260
|
+
error?: string | null;
|
|
261
|
+
durationMs: number;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface NfcWriteRequestPayload {
|
|
265
|
+
envelopeB64: string;
|
|
266
|
+
profile: number;
|
|
267
|
+
chipName: string;
|
|
268
|
+
persianLabel?: string | null;
|
|
269
|
+
englishLabel?: string | null;
|
|
270
|
+
makeReadOnly: boolean;
|
|
271
|
+
shortCode?: string | null;
|
|
272
|
+
tagPresentTimeoutSeconds: number;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export interface NfcWriteResultPayload {
|
|
276
|
+
ok: boolean;
|
|
277
|
+
outcome: string;
|
|
278
|
+
error?: string | null;
|
|
279
|
+
chipUid?: string | null;
|
|
280
|
+
totalBytes: number;
|
|
281
|
+
preservedForeignRecords: number;
|
|
282
|
+
ownershipTrusted: boolean;
|
|
283
|
+
backupCreated: boolean;
|
|
284
|
+
restoreAttempted: boolean;
|
|
285
|
+
restoreSucceeded: boolean;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export interface SyncScopeInfoPayload {
|
|
289
|
+
scopeName: string;
|
|
290
|
+
parametersLabel: string;
|
|
291
|
+
parametersJson: string;
|
|
292
|
+
lastSyncUtc?: string | null;
|
|
293
|
+
hasErrors: boolean;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export interface SyncTablePendingPayload {
|
|
297
|
+
tableName: string;
|
|
298
|
+
upserts: number;
|
|
299
|
+
deletes: number;
|
|
300
|
+
totalChanges: number;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export interface SyncScopePendingPayload {
|
|
304
|
+
scopeName: string;
|
|
305
|
+
parametersLabel: string;
|
|
306
|
+
parametersJson: string;
|
|
307
|
+
totalChanges: number;
|
|
308
|
+
tables: SyncTablePendingPayload[];
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export interface SyncRetryScopePayload {
|
|
312
|
+
scopeName: string;
|
|
313
|
+
parametersLabel: string;
|
|
314
|
+
errorBlobBytes: number;
|
|
315
|
+
lastSyncUtc?: string | null;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export interface SyncHealthPayload {
|
|
319
|
+
pendingScopes: SyncScopePendingPayload[];
|
|
320
|
+
retryScopes: SyncRetryScopePayload[];
|
|
321
|
+
pendingTotal: number;
|
|
322
|
+
batchFileCount: number;
|
|
323
|
+
batchTotalBytes: number;
|
|
324
|
+
batchDirectoryPath?: string | null;
|
|
325
|
+
allScopes: SyncScopeInfoPayload[];
|
|
326
|
+
severity: 'healthy' | 'warning' | 'error' | string;
|
|
327
|
+
error?: string | null;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** SyncReinitializeMode (a number on the wire). */
|
|
331
|
+
export const SyncReinitializeMode = { normal: 0, reinitialize: 1, reinitializeWithUpload: 2 } as const;
|
|
332
|
+
|
|
333
|
+
export interface SyncScopeSelector {
|
|
334
|
+
scopeName: string;
|
|
335
|
+
parametersJson?: string | null;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export interface SyncRunRequestPayload {
|
|
339
|
+
scope?: string | null;
|
|
340
|
+
parametersJson?: string | null;
|
|
341
|
+
mode: number;
|
|
342
|
+
scopes?: SyncScopeSelector[] | null;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export interface SyncRunScopeResultPayload {
|
|
346
|
+
scopeName: string;
|
|
347
|
+
parametersLabel: string;
|
|
348
|
+
uploaded: number;
|
|
349
|
+
downloaded: number;
|
|
350
|
+
ok: boolean;
|
|
351
|
+
error?: string | null;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export interface SyncRunResultPayload {
|
|
355
|
+
ok: boolean;
|
|
356
|
+
error?: string | null;
|
|
357
|
+
totalUploaded: number;
|
|
358
|
+
totalDownloaded: number;
|
|
359
|
+
scopes: SyncRunScopeResultPayload[];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export interface SyncClearResultPayload {
|
|
363
|
+
scopesCleared: number;
|
|
364
|
+
batchFilesDeleted: number;
|
|
365
|
+
error?: string | null;
|
|
366
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// The resumable bundle upload over the device hub (G9AppDiagnostic.Maui DiagnosticBundleUploader): BackupUploadInit →
|
|
2
|
+
// one or more BackupUploadStream calls → BackupUploadComplete (the gateway re-hashes the blob against the SHA-256 the
|
|
3
|
+
// device announced).
|
|
4
|
+
//
|
|
5
|
+
// SEGMENTED on purpose: the gateway sends no acknowledgement while a stream runs, and a native WebSocket under SignalR
|
|
6
|
+
// (OkHttp on Android) CLOSES the socket once 16 MiB are queued. So every stream call carries at most 4 MiB and waits for
|
|
7
|
+
// its ack before the next one — in-flight data stays bounded whatever the bundle's size. The gateway starts every stream
|
|
8
|
+
// at the bytes it already holds, so a segment boundary, a dropped connection and an app restart all resume the same way:
|
|
9
|
+
// from `receivedOffset`.
|
|
10
|
+
import type { DiagnosticFiles } from '../host.ts';
|
|
11
|
+
import { DeviceHubMethods, TRANSFER } from '../wire.ts';
|
|
12
|
+
import type { BackupUploadChunksAck, BackupUploadCompleteResponse, BackupUploadInitRequest, BackupUploadInitResponse } from './models.ts';
|
|
13
|
+
|
|
14
|
+
/** What the uploader needs of the hub client. */
|
|
15
|
+
export interface HubInvoker {
|
|
16
|
+
invoke<T = unknown>(method: string, ...args: unknown[]): Promise<T>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A client-to-server stream SignalR accepts as an argument: anything with `subscribe(observer)` (the JS client checks
|
|
21
|
+
* for that, not for its own class), so the package needs no SignalR import.
|
|
22
|
+
*/
|
|
23
|
+
export class StreamSubject<T> {
|
|
24
|
+
private observer: { next(v: T): void; complete(): void; error(e: unknown): void } | null = null;
|
|
25
|
+
private readonly buffer: T[] = [];
|
|
26
|
+
private done = false;
|
|
27
|
+
|
|
28
|
+
subscribe(observer: { next(v: T): void; complete(): void; error(e: unknown): void }): { dispose(): void } {
|
|
29
|
+
this.observer = observer;
|
|
30
|
+
for (const v of this.buffer.splice(0)) observer.next(v);
|
|
31
|
+
if (this.done) observer.complete();
|
|
32
|
+
return { dispose: () => (this.observer = null) };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
next(value: T): void {
|
|
36
|
+
if (this.observer) this.observer.next(value);
|
|
37
|
+
else this.buffer.push(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
complete(): void {
|
|
41
|
+
this.done = true;
|
|
42
|
+
this.observer?.complete();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
error(e: unknown): void {
|
|
46
|
+
this.done = true;
|
|
47
|
+
this.observer?.error(e);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface BundleUploadRequest {
|
|
52
|
+
readonly clientId: string;
|
|
53
|
+
readonly path: string;
|
|
54
|
+
readonly filename: string;
|
|
55
|
+
/** BackupSource: 0 an operator's backup, 1 the user's full report. */
|
|
56
|
+
readonly source: number;
|
|
57
|
+
readonly note?: string | null;
|
|
58
|
+
readonly isPasswordProtected: boolean;
|
|
59
|
+
readonly isSensitive: boolean;
|
|
60
|
+
readonly include: {
|
|
61
|
+
readonly sqlite: boolean;
|
|
62
|
+
readonly logs: boolean;
|
|
63
|
+
readonly attachments: boolean;
|
|
64
|
+
readonly geoDatabase?: boolean;
|
|
65
|
+
readonly preferences?: boolean;
|
|
66
|
+
readonly secureStorage?: boolean;
|
|
67
|
+
};
|
|
68
|
+
/** A token from an earlier, interrupted run of the SAME file. */
|
|
69
|
+
readonly resumeToken?: string | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type BundleUploadOutcome =
|
|
73
|
+
| { readonly ok: true; readonly token: string; readonly bytes: number }
|
|
74
|
+
| { readonly ok: false; readonly error: string; readonly token: string | null; readonly cancelledByOperator: boolean; readonly retryable: boolean };
|
|
75
|
+
|
|
76
|
+
export interface BundleUploadHooks {
|
|
77
|
+
readonly onProgress?: (sent: number, total: number) => void;
|
|
78
|
+
/** The token is known (persist it: a later run resumes with it). */
|
|
79
|
+
readonly onToken?: (token: string) => void;
|
|
80
|
+
readonly signal?: { readonly aborted: boolean };
|
|
81
|
+
readonly sleep?: (ms: number) => Promise<void>;
|
|
82
|
+
/** Bytes per stream call (tests); default 4 MiB. */
|
|
83
|
+
readonly segmentBytes?: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const defaultSleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
|
|
87
|
+
|
|
88
|
+
export async function uploadBundle(hub: HubInvoker, files: DiagnosticFiles, request: BundleUploadRequest, hooks: BundleUploadHooks = {}): Promise<BundleUploadOutcome> {
|
|
89
|
+
const sleep = hooks.sleep ?? defaultSleep;
|
|
90
|
+
const segment = hooks.segmentBytes ?? TRANSFER.segmentBytes;
|
|
91
|
+
const total = await files.size(request.path);
|
|
92
|
+
const sha256Hex = await files.sha256(request.path);
|
|
93
|
+
const fail = (error: string, token: string | null, extra: { cancelledByOperator?: boolean; retryable?: boolean } = {}): BundleUploadOutcome => ({
|
|
94
|
+
ok: false,
|
|
95
|
+
error,
|
|
96
|
+
token,
|
|
97
|
+
cancelledByOperator: extra.cancelledByOperator ?? false,
|
|
98
|
+
retryable: extra.retryable ?? false,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
let resumeToken = request.resumeToken ?? null;
|
|
102
|
+
// Two independent one-time recoveries: a resume token the gateway forgot, and a copy it could not verify.
|
|
103
|
+
let forgotToken = false;
|
|
104
|
+
let restarted = false;
|
|
105
|
+
for (;;) {
|
|
106
|
+
const init: BackupUploadInitRequest = {
|
|
107
|
+
clientId: request.clientId,
|
|
108
|
+
sha256Hex,
|
|
109
|
+
sizeBytes: total,
|
|
110
|
+
isPasswordProtected: request.isPasswordProtected,
|
|
111
|
+
resumeToken,
|
|
112
|
+
filename: request.filename,
|
|
113
|
+
isSensitive: request.isSensitive,
|
|
114
|
+
note: request.note ?? null,
|
|
115
|
+
includeSqlite: request.include.sqlite,
|
|
116
|
+
includeGeoDatabase: request.include.geoDatabase ?? false,
|
|
117
|
+
includeAttachments: request.include.attachments,
|
|
118
|
+
includeLogs: request.include.logs,
|
|
119
|
+
includePreferences: request.include.preferences ?? false,
|
|
120
|
+
includeSecureStorage: request.include.secureStorage ?? false,
|
|
121
|
+
source: request.source,
|
|
122
|
+
};
|
|
123
|
+
const answer = await hub.invoke<BackupUploadInitResponse>(DeviceHubMethods.backupUploadInit, init);
|
|
124
|
+
if (answer.unknownResumeToken && resumeToken && !forgotToken) {
|
|
125
|
+
forgotToken = true;
|
|
126
|
+
resumeToken = null; // the gateway forgot it (expired): start over once
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (!answer.accepted) return fail(answer.rejectionReason ?? 'rejected', null, { cancelledByOperator: answer.cancelledByOperator === true });
|
|
130
|
+
const token = answer.token;
|
|
131
|
+
hooks.onToken?.(token);
|
|
132
|
+
|
|
133
|
+
let offset = Math.max(0, Math.min(answer.resumeFromOffset, total));
|
|
134
|
+
hooks.onProgress?.(offset, total);
|
|
135
|
+
let busy = 0;
|
|
136
|
+
while (offset < total) {
|
|
137
|
+
if (hooks.signal?.aborted) return fail('cancelled', token);
|
|
138
|
+
const end = Math.min(total, offset + segment);
|
|
139
|
+
const subject = new StreamSubject<string>();
|
|
140
|
+
const ackPromise = hub.invoke<BackupUploadChunksAck>(DeviceHubMethods.backupUploadStream, token, subject);
|
|
141
|
+
try {
|
|
142
|
+
for (let pos = offset; pos < end; pos += TRANSFER.chunkBytes) {
|
|
143
|
+
if (hooks.signal?.aborted) break;
|
|
144
|
+
subject.next(await files.readChunk(request.path, pos, Math.min(TRANSFER.chunkBytes, end - pos)));
|
|
145
|
+
hooks.onProgress?.(Math.min(end, pos + TRANSFER.chunkBytes), total);
|
|
146
|
+
}
|
|
147
|
+
} finally {
|
|
148
|
+
subject.complete();
|
|
149
|
+
}
|
|
150
|
+
const ack = await ackPromise;
|
|
151
|
+
if (!ack.accepted) {
|
|
152
|
+
if (ack.cancelledByOperator) return fail(ack.rejectionReason ?? 'cancelled by the operator', token, { cancelledByOperator: true });
|
|
153
|
+
if (ack.streamBusy && busy++ < TRANSFER.busyRetries) {
|
|
154
|
+
await sleep(TRANSFER.busyDelayMs);
|
|
155
|
+
offset = ack.receivedOffset;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
return fail(ack.rejectionReason ?? 'stream rejected', token, { retryable: true });
|
|
159
|
+
}
|
|
160
|
+
busy = 0;
|
|
161
|
+
if (ack.receivedOffset <= offset && end > offset) return fail('the gateway did not take the segment', token, { retryable: true });
|
|
162
|
+
offset = ack.receivedOffset;
|
|
163
|
+
hooks.onProgress?.(offset, total);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const complete = await hub.invoke<BackupUploadCompleteResponse>(DeviceHubMethods.backupUploadComplete, { token, sha256Hex });
|
|
167
|
+
if (complete.accepted) return { ok: true, token, bytes: complete.finalSizeBytes || total };
|
|
168
|
+
if (complete.restartFromZero && !restarted) {
|
|
169
|
+
restarted = true;
|
|
170
|
+
resumeToken = null; // the gateway's copy is unusable: one fresh run
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
return fail(complete.rejectionReason ?? 'the gateway refused the archive', token, { retryable: true });
|
|
174
|
+
}
|
|
175
|
+
}
|
package/src/recording.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// One recorder's life as the tester sees it — the TypeScript twin of G9AppDiagnostic.Client RecordingSession
|
|
2
|
+
// (diagnostic/AiGuides/02-Tester-Mode.md «The stop button»): a single button that starts and stops it and never swallows
|
|
3
|
+
// a tap. A Stop used to be lost three ways: a tap while the start was still running was dropped by a busy guard; a tap
|
|
4
|
+
// within a throttle window after the start was dropped too; and a recorder whose stop never returned left the button on
|
|
5
|
+
// Stop for good. Here:
|
|
6
|
+
// - a stop asked for while Starting is remembered and done the moment the start finishes;
|
|
7
|
+
// - a stop while Stopping is answered by the one in flight (never a second recording);
|
|
8
|
+
// - a stop has a time limit: past it the session is Idle again, the late result is dropped, `onStopTimedOut` says so;
|
|
9
|
+
// - a start or stop that throws ends Idle, never stuck.
|
|
10
|
+
// Every method may be called at any time; none of them rejects.
|
|
11
|
+
|
|
12
|
+
export type RecordingState = 'idle' | 'starting' | 'recording' | 'stopping';
|
|
13
|
+
|
|
14
|
+
export interface RecordingSessionOptions<T> {
|
|
15
|
+
/** Starts the recorder; false = it could not (the session goes back to Idle). */
|
|
16
|
+
readonly start: () => Promise<boolean>;
|
|
17
|
+
/** Stops it and returns what it made (null = nothing usable). */
|
|
18
|
+
readonly stop: () => Promise<T | null>;
|
|
19
|
+
/** What happens with the result (attach it); `timedOut` when the stop did not return in time. */
|
|
20
|
+
readonly finished: (result: T | null, timedOut: boolean) => Promise<void> | void;
|
|
21
|
+
/** How long a stop may take before the session gives up on it (MAUI: 30 s screen, 10 s voice). */
|
|
22
|
+
readonly stopTimeoutMs: number;
|
|
23
|
+
readonly onStateChanged?: (state: RecordingState) => void;
|
|
24
|
+
readonly onStopTimedOut?: () => void;
|
|
25
|
+
readonly onFaulted?: (error: unknown) => void;
|
|
26
|
+
readonly now?: () => number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class RecordingSession<T> {
|
|
30
|
+
private readonly o: RecordingSessionOptions<T>;
|
|
31
|
+
private stateValue: RecordingState = 'idle';
|
|
32
|
+
private stopRequested = false;
|
|
33
|
+
private startedAt: number | null = null;
|
|
34
|
+
private inFlight: Promise<void> = Promise.resolve();
|
|
35
|
+
|
|
36
|
+
constructor(options: RecordingSessionOptions<T>) {
|
|
37
|
+
this.o = options;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get state(): RecordingState {
|
|
41
|
+
return this.stateValue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Starting, recording or stopping: the button is the way to end it. */
|
|
45
|
+
get isActive(): boolean {
|
|
46
|
+
return this.stateValue !== 'idle';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** When the recorder actually started (ms), or null while it is not recording. */
|
|
50
|
+
get startedAtMs(): number | null {
|
|
51
|
+
return this.startedAt;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The button: starts when idle, stops when recording, stops as soon as it can while starting. */
|
|
55
|
+
toggle(): Promise<void> {
|
|
56
|
+
switch (this.stateValue) {
|
|
57
|
+
case 'idle':
|
|
58
|
+
this.stopRequested = false;
|
|
59
|
+
this.set('starting');
|
|
60
|
+
return this.track(this.startCore());
|
|
61
|
+
case 'starting':
|
|
62
|
+
this.stopRequested = true;
|
|
63
|
+
return Promise.resolve();
|
|
64
|
+
case 'recording':
|
|
65
|
+
this.set('stopping');
|
|
66
|
+
return this.track(this.stopCore());
|
|
67
|
+
default:
|
|
68
|
+
return Promise.resolve(); // already stopping: the tap is answered by the stop in flight
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Stop if anything runs (the recorder ended by itself, the panel goes away). Idle: nothing. */
|
|
73
|
+
stop(): Promise<void> {
|
|
74
|
+
switch (this.stateValue) {
|
|
75
|
+
case 'starting':
|
|
76
|
+
this.stopRequested = true;
|
|
77
|
+
return Promise.resolve();
|
|
78
|
+
case 'recording':
|
|
79
|
+
this.set('stopping');
|
|
80
|
+
return this.track(this.stopCore());
|
|
81
|
+
default:
|
|
82
|
+
return Promise.resolve();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A recorder that is already running (it outlived a reload of the app's script): the session takes it over as
|
|
88
|
+
* Recording, so the next tap stops it and keeps its file. Only from Idle.
|
|
89
|
+
*/
|
|
90
|
+
adopt(startedAtMs: number = (this.o.now ?? Date.now)()): boolean {
|
|
91
|
+
if (this.stateValue !== 'idle') return false;
|
|
92
|
+
this.startedAt = startedAtMs;
|
|
93
|
+
this.set('recording');
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Resolves when whatever the session is doing now has finished (tests, a send that must wait for the file). */
|
|
98
|
+
settled(): Promise<void> {
|
|
99
|
+
return this.inFlight;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private track(p: Promise<void>): Promise<void> {
|
|
103
|
+
this.inFlight = p;
|
|
104
|
+
return p;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private async startCore(): Promise<void> {
|
|
108
|
+
let started = false;
|
|
109
|
+
try {
|
|
110
|
+
started = await this.o.start();
|
|
111
|
+
} catch (error) {
|
|
112
|
+
started = false;
|
|
113
|
+
this.fault(error);
|
|
114
|
+
}
|
|
115
|
+
if (!started) {
|
|
116
|
+
this.stopRequested = false;
|
|
117
|
+
this.set('idle');
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
this.startedAt = (this.o.now ?? Date.now)();
|
|
121
|
+
const stopNow = this.stopRequested;
|
|
122
|
+
this.stopRequested = false;
|
|
123
|
+
this.set(stopNow ? 'stopping' : 'recording');
|
|
124
|
+
if (stopNow) await this.stopCore();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private async stopCore(): Promise<void> {
|
|
128
|
+
let result: T | null = null;
|
|
129
|
+
let timedOut = false;
|
|
130
|
+
const clock: { timer?: ReturnType<typeof setTimeout> } = {};
|
|
131
|
+
try {
|
|
132
|
+
const stopping = this.o.stop();
|
|
133
|
+
const limit = new Promise<'timeout'>((resolve) => {
|
|
134
|
+
clock.timer = setTimeout(() => resolve('timeout'), this.o.stopTimeoutMs);
|
|
135
|
+
});
|
|
136
|
+
const first = await Promise.race([stopping.then((r) => ({ r })), limit]);
|
|
137
|
+
if (first === 'timeout') {
|
|
138
|
+
timedOut = true;
|
|
139
|
+
// The late result is dropped, its failure still observed (no unhandled rejection).
|
|
140
|
+
stopping.catch(() => {});
|
|
141
|
+
} else {
|
|
142
|
+
result = first.r;
|
|
143
|
+
}
|
|
144
|
+
} catch (error) {
|
|
145
|
+
this.fault(error);
|
|
146
|
+
} finally {
|
|
147
|
+
if (clock.timer !== undefined) clearTimeout(clock.timer);
|
|
148
|
+
}
|
|
149
|
+
this.startedAt = null;
|
|
150
|
+
this.set('idle');
|
|
151
|
+
if (timedOut) this.safe(() => this.o.onStopTimedOut?.());
|
|
152
|
+
try {
|
|
153
|
+
await this.o.finished(result, timedOut);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
this.fault(error);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private set(state: RecordingState): void {
|
|
160
|
+
if (this.stateValue === state) return;
|
|
161
|
+
this.stateValue = state;
|
|
162
|
+
this.safe(() => this.o.onStateChanged?.(state));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private fault(error: unknown): void {
|
|
166
|
+
this.safe(() => this.o.onFaulted?.(error));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private safe(fn: () => void): void {
|
|
170
|
+
try {
|
|
171
|
+
fn();
|
|
172
|
+
} catch {
|
|
173
|
+
// a listener's throw never breaks the button
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|