-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.ts
More file actions
551 lines (485 loc) · 16.3 KB
/
index.ts
File metadata and controls
551 lines (485 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import { EventEmitter } from 'events';
import * as stream from 'stream';
import * as t from 'io-ts';
import {
decode,
ParseJSON,
Peer,
TypesafeRequestDispatcher,
} from '@fitbit/jsonrpc-ts';
import {
BulkData,
FDBTypes,
maxBase64DecodedSize,
} from '@fitbit/fdb-protocol';
import * as lodash from 'lodash';
import JSZip = require('jszip');
import { version } from '../package.json';
import BulkDataReceiver from './BulkDataReceiver';
import ConfigurableEncode from './ConfigurableEncode';
import { getAppUUID, makePartialBundle } from './componentBundle';
export interface RemoteHostOptions {
/** Suffix to append to the debugger userAgent string. */
userAgentSuffix?: string;
}
export interface ConsoleMessage {
timestamp?: Date;
emittedBy: FDBTypes.AppComponent;
fromHost?: boolean;
position?: FDBTypes.Position;
kind: 'log' | 'info' | 'warn' | 'error';
message: any[];
}
export interface ConsoleTrace {
timestamp?: Date;
emittedBy: FDBTypes.AppComponent;
stack: FDBTypes.Position[];
kind: 'trace' | 'assert' | 'exception';
message: any[];
}
/**
* Fitbit OS 3.0 does not fully support REPL despite claiming support via the capability
* Fitbit developers see bug: FW-65649
*/
const FBOS3_EVAL_QUIRK = /^[a-zA-Z]+ \d+\.33\.1\.[1-3]?\d$/;
export class RemoteHost extends EventEmitter {
static readonly CAPABILITIES = {
protocol: { maxMessageSize: 1024 * 1024 },
console: { appLogging: true },
io: { write: true },
};
static readonly USER_AGENT = `fdb-debugger/${version}`;
dispatcher = new TypesafeRequestDispatcher();
/** JSON-RPC peer connection to the remote host. */
rpc = new Peer(this.dispatcher);
/** Host's initialization info. */
info!: FDBTypes.InitializeResult;
/** The local clock timestamp of the (approximate) epoch for the Host. */
epoch!: Date;
/** Milliseconds to wait before giving up on a method call. */
timeout: number;
/** Open bulk data transfer streams. */
protected bulkDataStreams = new BulkData();
private screenshotReceiver = new BulkDataReceiver(this.bulkDataStreams, 'screenshot');
private appContentsListReceiver = new BulkDataReceiver(
this.bulkDataStreams,
'app component contents list',
);
private heapSnapshotReceiver = new BulkDataReceiver(this.bulkDataStreams, 'heap snapshot');
private serializerTransform = new ConfigurableEncode();
protected constructor(timeout: number) {
super();
this.timeout = timeout;
this.dispatcher
.method('ping', t.undefined, () => {})
.notification('console.message', FDBTypes.ConsoleMessage, this.handleMessage)
.notification('console.traceMessage', FDBTypes.TraceMessage, this.handleTrace)
.notification('experimental.lifecycle.appRunning', FDBTypes.App, this.handleAppRunning)
.notification('experimental.lifecycle.appClosed', FDBTypes.App, this.handleAppClosed);
this.bulkDataStreams.register(this.dispatcher);
this.screenshotReceiver.registerCloserMethods(
this.dispatcher,
'app.screenshot.stream',
);
this.appContentsListReceiver.registerCloserMethods(
this.dispatcher,
'app.contents.stream',
);
this.heapSnapshotReceiver.registerCloserMethods(
this.dispatcher,
'app.debug.heapSnapshot',
);
}
/**
* Initialize a connection to a remote debug bridge host.
*
* @param hostStream stream for communicating with the host
* @param options connection options
*/
static async connect(
hostStream: stream.Duplex,
{
userAgentSuffix = '',
timeout = 10000,
postDeserializeTransform = new stream.PassThrough({ objectMode: true }),
preSerializeTransform = new stream.PassThrough({ objectMode: true }),
} = {},
) {
let userAgent = this.USER_AGENT;
if (userAgentSuffix) {
userAgent = `${userAgent} ${userAgentSuffix}`;
}
const host = new this(timeout);
hostStream
.pipe(new ParseJSON)
.pipe(postDeserializeTransform)
.pipe(host.rpc)
.pipe(preSerializeTransform)
.pipe(host.serializerTransform)
.pipe(hostStream);
const reqTime = Date.now();
host.info = await host.initialize({
userAgent,
capabilities: this.CAPABILITIES,
});
if (
host.hasCapability('protocol.additionalSerializations') &&
host.info.capabilities.protocol!.additionalSerializations!.includes('cbor-definite')
) {
host.changeSerialization('cbor-definite');
}
/**
* Half-decent guess of the connection epoch in the local clock
* domain, assuming that the send and receive latency is symmetric,
* and that the host responds immediately to the request.
*/
host.epoch = new Date((reqTime + Date.now()) / 2);
return host;
}
/**
* Convert a host-relative timestamp to a Date in the local clock domain.
*/
convertTimestamp(relativeTS: number) {
return new Date(this.epoch.getTime() + relativeTS * 1000);
}
handleMessage = (params: FDBTypes.ConsoleMessage) => {
if (params.timestamp) {
this.emit('consoleMessage', {
...params,
timestamp: this.convertTimestamp(params.timestamp),
});
} else {
this.emit('consoleMessage', params);
}
}
handleTrace = (params: FDBTypes.TraceMessage) => {
if (params.timestamp) {
this.emit('consoleTrace', {
...params,
timestamp: this.convertTimestamp(params.timestamp),
});
} else {
this.emit('consoleTrace', params);
}
}
handleAppRunning = (params: FDBTypes.App) => {
this.emit('appRunning', params);
}
handleAppClosed = (params: FDBTypes.App) => {
this.emit('appClosed', params);
}
/**
* Query whether the Host advertises a capability.
*
* @param path capability path, e.g. 'appHost.install.sideloadStream'
*/
hasCapability(path: string) {
return lodash.get(this.info.capabilities, path) !== undefined;
}
/**
* The max message size that the remote host will accept, in bytes.
*/
get maxMessageSize() {
const protocolDefaultSize = 8192; // Assuming WebSocket
const capabilitySize: number = lodash.get(
this.info.capabilities,
'protocol.maxMessageSize',
0,
);
return Math.max(protocolDefaultSize, capabilitySize);
}
/**
* Bind an RPC method on the remote host with type-checking of the
* method's response result. If the response result does not pass
* verification, the method call promise is rejected with a TypeError.
*
* @param method RPC method to bind
* @param paramsType io-ts type of the RPC method params
* @param resultType io-ts type of the RPC response result
*/
protected bindMethod<P extends t.Any, R extends t.Any>(
method: string,
paramsType: P,
resultType: R,
{
timeoutEnabled = true,
minTimeout = 0,
} = {},
): (params: t.TypeOf<P>) => Promise<t.TypeOf<R>> {
return (params: t.TypeOf<P>) =>
this.rpc.callMethod(method, params, {
timeout: timeoutEnabled ? Math.max(this.timeout, minTimeout) : undefined,
}).then(decode(resultType));
}
private initialize = this.bindMethod(
'initialize', FDBTypes.InitializeParams, FDBTypes.InitializeResult);
/**
* Ping the remote host.
*/
ping = (): Promise<void> => this.rpc.callMethod('ping', undefined, { timeout: this.timeout });
protected ioWrite = this.bindMethod(
'io.write',
FDBTypes.IOWriteParams,
t.any,
{ timeoutEnabled: false },
);
protected beginStreamingInstall = this.bindMethod(
'app.install.stream.begin',
FDBTypes.AppInstallStreamBeginParams,
FDBTypes.StreamOpenResponse,
);
protected finalizeStreamingInstall = this.bindMethod(
'app.install.stream.finalize',
FDBTypes.StreamCloseParams,
FDBTypes.AppInstallResult,
{ minTimeout: 300000 },
);
protected abortStreamingInstall = this.bindMethod(
'app.install.stream.abort',
FDBTypes.StreamCloseParams,
t.any,
);
/** Request that the host launch an installed app component. */
launchAppComponent = this.bindMethod(
'app.launchComponent',
FDBTypes.LaunchComponentParams,
FDBTypes.AppComponent,
);
private changeSerialization = (serialization: FDBTypes.SerializationType) => {
this.rpc.sendNotification(
'protocol.serialization.change',
{ serialization },
);
this.serializerTransform.setEncoder(serialization);
}
protected async writeToStream(
stream: FDBTypes.StreamToken,
data: Buffer,
{
onProgress = (() => {}) as (bytesWritten: number, totalBytes: number) => void,
} = {},
) {
/**
* How much data can we send in each io.write call?
* Depends on the overhead. Thanks to my brilliant idea of loosely
* coupling the JSON-RPC protocol implementation from the serializer,
* there isn't a way to find out how big a message will be. But wait,
* there's more! The request ID and stream ID fields could be of
* arbitrary length. And the outgoing JSON could be pretty-printed.
*
* Rather than come up with some rigorous method to determine exactly how
* many bytes of overhead a specific message is going to end up with so
* that each io.write can be packed to max size, let's just guesstimate
* a value for overhead and add some headroom to make it extremely unlikely
* that a write is not going to go over.
*
* > JSON.stringify({
* ... jsonrpc: '2.0', id: Number.MIN_SAFE_INTEGER,
* ... method: 'io.write', params: {
* ... stream: Number.MIN_SAFE_INTEGER,
* ... data: '',
* ... encoding: 'base64',
* ... }).length;
* 128
*
* A JSON-RPC io.write request with max-length numeric request and stream
* IDs and no pretty-printing is 128 chars long (which in this case is
* equal to 128 bytes of UTF-8). So let's double that to 256 bytes of
* potential overhead to deal with any overhead from pretty-printing the
* JSON and long string IDs and call it a day.
*/
const overheadChars = 256;
const maxDataBytes = maxBase64DecodedSize(
this.maxMessageSize - overheadChars);
if (maxDataBytes < 1) {
throw new Error('Cannot fit any data into an io.write message');
}
let expireWriteTimeout: (reason: any) => void;
const writeTimedOut = new Promise((_, reject) => {
expireWriteTimeout = () => {
reject(new Error('io.write timed out'));
};
});
let timeoutTimer = setTimeout(expireWriteTimeout!, this.timeout);
const resetWriteTimeout = () => {
clearTimeout(timeoutTimer);
timeoutTimer = setTimeout(expireWriteTimeout, this.timeout);
};
/**
* It would be much more intelligent to only have a small number of
* write requests in-flight at once so that we can fail fast and not
* have to wait for all the remaining buffered requests to be sent.
* But that's a lot more complicated to write than the naive
* machine-gun approach.
*/
const writes: Promise<any>[] = [];
for (let cursor = 0; cursor < data.length; cursor += maxDataBytes) {
const chunk: FDBTypes.IOWriteParams = this.serializerTransform.canAcceptRawBuffers() ? {
stream,
data: data.slice(cursor, cursor + maxDataBytes),
encoding: 'none',
} : {
stream,
data: data.toString('base64', cursor, cursor + maxDataBytes),
};
writes.push(
this.ioWrite(chunk).then(() => {
resetWriteTimeout();
onProgress(Math.min(cursor + maxDataBytes, data.length), data.length);
}),
);
}
await Promise.race([Promise.all(writes), writeTimedOut]);
clearTimeout(timeoutTimer);
}
async installApp(
componentBundle: 'app' | 'companion',
data: Buffer,
{
onProgress = (() => {}) as (bytesWritten: number, totalBytes: number) => void,
} = {},
) {
let bundleData = data;
if (this.supportsPartialAppInstall()) {
try {
const bundleZip = await JSZip.loadAsync(data);
const uuid = await getAppUUID(bundleZip);
const existingContents = await this.getInstalledAppContents(uuid, componentBundle);
const partialBundle = await makePartialBundle(bundleZip, existingContents);
if (partialBundle == null) {
// Nothing to do! The bundle is already installed.
return null;
}
bundleData = partialBundle;
} catch {
// Install like normal.
}
}
const { stream } = await this.beginStreamingInstall({ componentBundle });
try {
await this.writeToStream(stream, bundleData, { onProgress });
} catch (e) {
this.abortStreamingInstall({ stream });
throw e;
}
return this.finalizeStreamingInstall({ stream })
.then(result => ({
installType: 'full' as FDBTypes.InstallType,
...result,
}));
}
protected beginStreamingScreenshotCapture = this.bindMethod(
'app.screenshot.stream.capture',
FDBTypes.AppScreenshotStreamCaptureParams,
FDBTypes.AppScreenshotStreamCaptureResult,
);
canTakeScreenshot() {
return (
this.hasCapability('appHost.screenshot') &&
!!this.info.capabilities.appHost!.screenshot!.stream
);
}
screenshotFormats() {
if (!this.canTakeScreenshot()) return [];
return this.info.capabilities.appHost!.screenshot!.imageFormats;
}
takeScreenshot(format: string, onWrite?: (received: number, total?: number) => void) {
return this.screenshotReceiver.receiveFromStream(stream =>
this.beginStreamingScreenshotCapture({ stream: stream.token, imageFormat: format })
.then(({ length }) => {
if (onWrite) {
stream.onWrite = (_, received) => onWrite(received, length);
}
}));
}
private sendEvalCmd = this.bindMethod(
'app.debug.evalToString',
FDBTypes.AppDebugEvalParams,
FDBTypes.AppDebugEvalResult,
);
private sendButtonInput = this.bindMethod(
'input.button',
FDBTypes.ButtonInput,
t.null,
);
private sendTouchInput = this.bindMethod(
'input.touch',
FDBTypes.TouchInput,
t.null,
);
hasEvalSupport() {
return this.hasCapability('appHost.debug.app.evalToString.supported') &&
this.info.capabilities.appHost!.debug!.app!.evalToString!.supported &&
!FBOS3_EVAL_QUIRK.test(this.info.device);
}
hasTouchInputSupport() {
return this.hasCapability('appHost.input.touch') &&
this.info.capabilities.appHost!.input!.touch!;
}
hasButtonInputSupport() {
return this.hasCapability('appHost.input.buttons') &&
this.info.capabilities.appHost!.input!.buttons! &&
this.info.capabilities.appHost!.input!.buttons!.length > 0;
}
buttons() {
if (!this.hasButtonInputSupport) return [];
return this.info.capabilities.appHost!.input!.buttons!;
}
eval(cmd: string) {
return this.sendEvalCmd({ cmd });
}
simulateButtonPress(button: FDBTypes.Button) {
return this.sendButtonInput({ button });
}
simulateTouch(location: FDBTypes.Point, state: FDBTypes.TouchState) {
return this.sendTouchInput({
location,
state,
});
}
supportsPartialAppInstall() {
return this.hasCapability('appHost.install.partialBundle') &&
this.info.capabilities.appHost!.install!.partialBundle!;
}
protected beginStreamingAppComponentContents = this.bindMethod(
'app.contents.stream.list',
FDBTypes.AppComponentContentsRequest,
t.any,
);
getInstalledAppContents(uuid: string, componentBundle: FDBTypes.ComponentBundleKind) {
return this.appContentsListReceiver.receiveFromStream(
stream => this.beginStreamingAppComponentContents({
componentBundle,
uuid,
stream: stream.token,
}),
)
.then(buffer => JSON.parse(buffer.toString()))
.then(decode(FDBTypes.AppComponentContentsList));
}
getHeapSnapshotSupport(): {
supported: boolean,
requiresInstrumentedLaunch: boolean,
formats: string[],
} {
return {
supported: false,
requiresInstrumentedLaunch: false,
formats: [],
...(
this.hasCapability('appHost.debug.app.heapSnapshot') &&
this.info.capabilities.appHost!.debug!.app!.heapSnapshot!
),
};
}
protected beginHeapSnapshotCapture = this.bindMethod(
'app.debug.heapSnapshot.capture',
FDBTypes.AppHeapSnapshotRequest,
t.any,
);
captureHeapSnapshot(format: string) {
return this.heapSnapshotReceiver.receiveFromStream(
stream => this.beginHeapSnapshotCapture({ format, stream: stream.token }),
);
}
}