@limrun/api 0.79.0 → 0.79.2
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/CHANGELOG.md +26 -0
- package/destination-tunnel-dialer.d.mts.map +1 -1
- package/destination-tunnel-dialer.d.ts.map +1 -1
- package/destination-tunnel-dialer.js +23 -14
- package/destination-tunnel-dialer.js.map +1 -1
- package/destination-tunnel-dialer.mjs +23 -14
- package/destination-tunnel-dialer.mjs.map +1 -1
- package/internal/app-log-events.d.mts +21 -0
- package/internal/app-log-events.d.mts.map +1 -0
- package/internal/app-log-events.d.ts +21 -0
- package/internal/app-log-events.d.ts.map +1 -0
- package/internal/app-log-events.js +73 -0
- package/internal/app-log-events.js.map +1 -0
- package/internal/app-log-events.mjs +69 -0
- package/internal/app-log-events.mjs.map +1 -0
- package/internal/signaling-transport.d.mts +79 -0
- package/internal/signaling-transport.d.mts.map +1 -0
- package/internal/signaling-transport.d.ts +79 -0
- package/internal/signaling-transport.d.ts.map +1 -0
- package/internal/signaling-transport.js +320 -0
- package/internal/signaling-transport.js.map +1 -0
- package/internal/signaling-transport.mjs +316 -0
- package/internal/signaling-transport.mjs.map +1 -0
- package/ios-client.d.mts +4 -9
- package/ios-client.d.mts.map +1 -1
- package/ios-client.d.ts +4 -9
- package/ios-client.d.ts.map +1 -1
- package/ios-client.js +172 -412
- package/ios-client.js.map +1 -1
- package/ios-client.mjs +173 -413
- package/ios-client.mjs.map +1 -1
- package/log-stream.d.mts +11 -4
- package/log-stream.d.mts.map +1 -1
- package/log-stream.d.ts +11 -4
- package/log-stream.d.ts.map +1 -1
- package/log-stream.js +45 -8
- package/log-stream.js.map +1 -1
- package/log-stream.mjs +45 -8
- package/log-stream.mjs.map +1 -1
- package/package.json +1 -1
- package/resources/macos-instances-helpers.d.mts +12 -0
- package/resources/macos-instances-helpers.d.mts.map +1 -1
- package/resources/macos-instances-helpers.d.ts +12 -0
- package/resources/macos-instances-helpers.d.ts.map +1 -1
- package/resources/macos-instances-helpers.js +118 -141
- package/resources/macos-instances-helpers.js.map +1 -1
- package/resources/macos-instances-helpers.mjs +118 -141
- package/resources/macos-instances-helpers.mjs.map +1 -1
- package/src/destination-tunnel-dialer.ts +23 -14
- package/src/internal/app-log-events.ts +76 -0
- package/src/internal/signaling-transport.ts +454 -0
- package/src/ios-client.ts +193 -471
- package/src/log-stream.ts +54 -11
- package/src/resources/macos-instances-helpers.ts +153 -136
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/ios-client.mjs
CHANGED
|
@@ -2,9 +2,8 @@ import os from 'os';
|
|
|
2
2
|
import crypto from 'crypto';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import fs from 'fs';
|
|
5
|
-
import { WebSocket } from 'ws';
|
|
6
5
|
import { EventEmitter } from 'events';
|
|
7
|
-
import { assertPort,
|
|
6
|
+
import { assertPort, startReverseTcpTunnel } from "./tunnel.mjs";
|
|
8
7
|
import { destinationTunnelDialOptions, startDestinationTcpTunnel, } from "./destination-tunnel-dialer.mjs";
|
|
9
8
|
import { syncFolder } from "./folder-sync.mjs";
|
|
10
9
|
import { createIgnoreFn } from "./folder-sync-ignore.mjs";
|
|
@@ -22,6 +21,7 @@ import { deriveTargetHttpPortUrlPrefix } from "./target-http-port.mjs";
|
|
|
22
21
|
import { persistFields, ttlFields } from "./internal/persist-option.mjs";
|
|
23
22
|
import { streamSessionEntries } from "./internal/session-stream.mjs";
|
|
24
23
|
import { LogStream } from "./log-stream.mjs";
|
|
24
|
+
import { openSignalingTransport, } from "./internal/signaling-transport.mjs";
|
|
25
25
|
export { LogStream } from "./log-stream.mjs";
|
|
26
26
|
const ACTIVE_RECORDING_FILENAME = 'recording.mp4';
|
|
27
27
|
export const REVERSE_TUNNEL_REMOTE_PORT_MIN = 57090;
|
|
@@ -307,28 +307,14 @@ export function streamPerformance(options) {
|
|
|
307
307
|
* @returns An InstanceClient for controlling the instance
|
|
308
308
|
*/
|
|
309
309
|
export async function createInstanceClient(options) {
|
|
310
|
-
const endpointWebSocketUrl = `${options.apiUrl
|
|
311
|
-
.replace('https://', 'wss://')
|
|
312
|
-
.replace('http://', 'ws://')}/signaling?token=${options.token}`;
|
|
313
310
|
const logLevel = options.logLevel ?? 'info';
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
const maxReconnectDelay = options.maxReconnectDelay ?? 30000;
|
|
317
|
-
let ws = undefined;
|
|
318
|
-
let connectionState = 'connecting';
|
|
319
|
-
let reconnectAttempts = 0;
|
|
320
|
-
let reconnectTimeout;
|
|
321
|
-
let handshakeDeadline;
|
|
322
|
-
let intentionalDisconnect = false;
|
|
323
|
-
let lastError;
|
|
324
|
-
// Centralized pending requests map - handles all request/response patterns
|
|
325
|
-
const pendingRequests = new Map();
|
|
311
|
+
// Set once the signaling socket opens, before any method can run.
|
|
312
|
+
let transport;
|
|
326
313
|
// Simctl uses streaming, so it needs separate handling
|
|
327
314
|
const simctlExecutions = new Map();
|
|
328
315
|
const serverNotifications = new Map();
|
|
329
316
|
const xcrunShimCleanups = [];
|
|
330
317
|
const httpProxyCleanups = [];
|
|
331
|
-
const stateChangeCallbacks = new Set();
|
|
332
318
|
// Logger functions
|
|
333
319
|
const logger = {
|
|
334
320
|
debug: (...args) => {
|
|
@@ -348,49 +334,10 @@ export async function createInstanceClient(options) {
|
|
|
348
334
|
console.error(...args);
|
|
349
335
|
},
|
|
350
336
|
};
|
|
351
|
-
const
|
|
352
|
-
if (connectionState !== newState) {
|
|
353
|
-
connectionState = newState;
|
|
354
|
-
logger.debug(`Connection state changed to: ${newState}`);
|
|
355
|
-
stateChangeCallbacks.forEach((callback) => {
|
|
356
|
-
try {
|
|
357
|
-
callback(newState);
|
|
358
|
-
}
|
|
359
|
-
catch (err) {
|
|
360
|
-
logger.error('Error in connection state callback:', err);
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
const failPendingRequests = (reason) => {
|
|
366
|
-
pendingRequests.forEach((request) => {
|
|
367
|
-
clearTimeout(request.timeout);
|
|
368
|
-
request.reject(new Error(reason));
|
|
369
|
-
});
|
|
370
|
-
pendingRequests.clear();
|
|
337
|
+
const failExecutions = (reason) => {
|
|
371
338
|
simctlExecutions.forEach((execution) => execution._handleError(new Error(reason)));
|
|
372
339
|
simctlExecutions.clear();
|
|
373
340
|
};
|
|
374
|
-
const cleanupConnection = () => {
|
|
375
|
-
clearTimeout(handshakeDeadline);
|
|
376
|
-
if (reconnectTimeout) {
|
|
377
|
-
clearTimeout(reconnectTimeout);
|
|
378
|
-
reconnectTimeout = undefined;
|
|
379
|
-
}
|
|
380
|
-
if (pingInterval) {
|
|
381
|
-
clearInterval(pingInterval);
|
|
382
|
-
pingInterval = undefined;
|
|
383
|
-
}
|
|
384
|
-
if (ws) {
|
|
385
|
-
ws.removeAllListeners();
|
|
386
|
-
// A retired socket can emit an error when an unfinished handshake is cancelled.
|
|
387
|
-
ws.on('error', () => { });
|
|
388
|
-
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
|
|
389
|
-
ws.close();
|
|
390
|
-
}
|
|
391
|
-
ws = undefined;
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
341
|
const cleanupClientResources = () => {
|
|
395
342
|
const shimCleanups = xcrunShimCleanups.splice(0);
|
|
396
343
|
for (const closeShim of shimCleanups) {
|
|
@@ -401,10 +348,6 @@ export async function createInstanceClient(options) {
|
|
|
401
348
|
closeProxy().catch((error) => logger.warn('Failed to close HTTP proxy:', error));
|
|
402
349
|
}
|
|
403
350
|
};
|
|
404
|
-
const cleanup = () => {
|
|
405
|
-
cleanupConnection();
|
|
406
|
-
cleanupClientResources();
|
|
407
|
-
};
|
|
408
351
|
const registerServerNotification = (type, notificationId, handler) => {
|
|
409
352
|
const handlers = serverNotifications.get(type) ?? new Map();
|
|
410
353
|
handlers.set(notificationId, handler);
|
|
@@ -439,38 +382,7 @@ export async function createInstanceClient(options) {
|
|
|
439
382
|
const clearServerNotifications = () => {
|
|
440
383
|
serverNotifications.clear();
|
|
441
384
|
};
|
|
442
|
-
|
|
443
|
-
const keepAliveSessionId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
444
|
-
return new Promise((resolveConnection, rejectConnection) => {
|
|
445
|
-
let hasResolved = false;
|
|
446
|
-
let retriedHandshake = false;
|
|
447
|
-
// Reconnection logic with exponential backoff
|
|
448
|
-
const scheduleReconnect = () => {
|
|
449
|
-
if (intentionalDisconnect) {
|
|
450
|
-
logger.debug('Skipping reconnection (intentional disconnect)');
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (isNonRetryableError(lastError ?? '')) {
|
|
454
|
-
logger.error(`Skipping reconnection (non-retryable error): ${lastError}`);
|
|
455
|
-
updateConnectionState('disconnected');
|
|
456
|
-
clearServerNotifications();
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
if (reconnectAttempts >= maxReconnectAttempts) {
|
|
460
|
-
logger.error(`Max reconnection attempts (${maxReconnectAttempts}) reached. Giving up.`);
|
|
461
|
-
updateConnectionState('disconnected');
|
|
462
|
-
clearServerNotifications();
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
const currentDelay = Math.min(reconnectDelay * Math.pow(2, reconnectAttempts), maxReconnectDelay);
|
|
466
|
-
reconnectAttempts++;
|
|
467
|
-
logger.debug(`Scheduling reconnection attempt ${reconnectAttempts} in ${currentDelay}ms...`);
|
|
468
|
-
updateConnectionState('reconnecting');
|
|
469
|
-
reconnectTimeout = setTimeout(() => {
|
|
470
|
-
logger.debug(`Attempting to reconnect (attempt ${reconnectAttempts})...`);
|
|
471
|
-
setupWebSocket();
|
|
472
|
-
}, currentDelay);
|
|
473
|
-
};
|
|
385
|
+
{
|
|
474
386
|
// Generate unique request ID
|
|
475
387
|
const generateId = () => {
|
|
476
388
|
return `ts-client-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
@@ -495,31 +407,7 @@ export async function createInstanceClient(options) {
|
|
|
495
407
|
const source = failedAction ?? message;
|
|
496
408
|
return new CommandError(message.error, source.code, source.written);
|
|
497
409
|
};
|
|
498
|
-
|
|
499
|
-
const sendRequest = (type, params = {}, transform, timeoutMs = 30000) => {
|
|
500
|
-
return new Promise((resolve, reject) => {
|
|
501
|
-
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
|
502
|
-
reject(new Error('WebSocket is not connected or connection is not open.'));
|
|
503
|
-
return;
|
|
504
|
-
}
|
|
505
|
-
const id = generateId();
|
|
506
|
-
const timeout = setTimeout(() => {
|
|
507
|
-
pendingRequests.delete(id);
|
|
508
|
-
reject(new Error(`Request ${type} timed out`));
|
|
509
|
-
}, timeoutMs);
|
|
510
|
-
pendingRequests.set(id, transform ? { resolve, reject, timeout, transform } : { resolve, reject, timeout });
|
|
511
|
-
const request = { type, id, ...params };
|
|
512
|
-
logger.debug('Sending request:', redactRequestForDebug(request));
|
|
513
|
-
ws.send(JSON.stringify(request), (err) => {
|
|
514
|
-
if (err) {
|
|
515
|
-
clearTimeout(timeout);
|
|
516
|
-
pendingRequests.delete(id);
|
|
517
|
-
logger.error(`Failed to send ${type} request:`, err);
|
|
518
|
-
reject(err);
|
|
519
|
-
}
|
|
520
|
-
});
|
|
521
|
-
});
|
|
522
|
-
};
|
|
410
|
+
const sendRequest = (type, params = {}, transform, timeoutMs = 30000) => transport.request(type, params, transform, timeoutMs);
|
|
523
411
|
const splitAppLogTail = (logs) => {
|
|
524
412
|
if (logs.length === 0) {
|
|
525
413
|
return [];
|
|
@@ -650,259 +538,6 @@ export async function createInstanceClient(options) {
|
|
|
650
538
|
exitCode: msg.exitCode ?? -1,
|
|
651
539
|
}),
|
|
652
540
|
};
|
|
653
|
-
const setupWebSocket = () => {
|
|
654
|
-
cleanupConnection();
|
|
655
|
-
updateConnectionState('connecting');
|
|
656
|
-
const proxyAgent = nodeProxyTransport.getWebSocketAgent(endpointWebSocketUrl);
|
|
657
|
-
let opened = false;
|
|
658
|
-
let handshakeError;
|
|
659
|
-
const socket = new WebSocket(endpointWebSocketUrl, proxyAgent ? { agent: proxyAgent } : {});
|
|
660
|
-
ws = socket;
|
|
661
|
-
// A socket inactivity timeout allows a slow TLS peer to keep the first command waiting.
|
|
662
|
-
handshakeDeadline = setTimeout(() => {
|
|
663
|
-
handshakeError = Object.assign(new Error('Opening handshake has timed out'), { code: 'ETIMEDOUT' });
|
|
664
|
-
socket.terminate();
|
|
665
|
-
}, 5000);
|
|
666
|
-
ws.on('message', (data) => {
|
|
667
|
-
let message;
|
|
668
|
-
try {
|
|
669
|
-
message = JSON.parse(data.toString());
|
|
670
|
-
}
|
|
671
|
-
catch (e) {
|
|
672
|
-
logger.error({ data, error: e }, 'Failed to parse JSON message');
|
|
673
|
-
return;
|
|
674
|
-
}
|
|
675
|
-
if (handleAppExit(message) || handleAppHang(message)) {
|
|
676
|
-
return;
|
|
677
|
-
}
|
|
678
|
-
// Handle simctl streaming separately (it uses multiple messages per request)
|
|
679
|
-
if (message.type === 'simctlStream') {
|
|
680
|
-
const execution = simctlExecutions.get(message.id);
|
|
681
|
-
if (!execution) {
|
|
682
|
-
logger.debug(`Received simctl stream for unknown execution: ${message.id}`);
|
|
683
|
-
return;
|
|
684
|
-
}
|
|
685
|
-
if (execution instanceof LldbExecution) {
|
|
686
|
-
execution._handleStarted();
|
|
687
|
-
}
|
|
688
|
-
if (message.stdout) {
|
|
689
|
-
try {
|
|
690
|
-
execution._handleStdout(Buffer.from(message.stdout, 'base64'));
|
|
691
|
-
}
|
|
692
|
-
catch (err) {
|
|
693
|
-
logger.error('Failed to decode stdout data:', err);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
if (message.stderr) {
|
|
697
|
-
try {
|
|
698
|
-
execution._handleStderr(Buffer.from(message.stderr, 'base64'));
|
|
699
|
-
}
|
|
700
|
-
catch (err) {
|
|
701
|
-
logger.error('Failed to decode stderr data:', err);
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
if (message.exitCode !== undefined) {
|
|
705
|
-
logger.debug(`Simctl execution ${message.id} completed with exit code ${message.exitCode}`);
|
|
706
|
-
// Forget the execution first: an exit listener may disconnect, and disconnecting
|
|
707
|
-
// fails every execution still tracked, including this one.
|
|
708
|
-
simctlExecutions.delete(message.id);
|
|
709
|
-
execution._handleExit(message.exitCode);
|
|
710
|
-
}
|
|
711
|
-
return;
|
|
712
|
-
}
|
|
713
|
-
// Handle all other request/response patterns generically
|
|
714
|
-
const request = pendingRequests.get(message.id);
|
|
715
|
-
if (!request) {
|
|
716
|
-
logger.debug(`Received response for unknown or already handled request: ${message.id}`);
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
clearTimeout(request.timeout);
|
|
720
|
-
pendingRequests.delete(message.id);
|
|
721
|
-
if (message.error) {
|
|
722
|
-
logger.debug(`Server error for ${message.type}: ${message.error}`);
|
|
723
|
-
request.reject(commandError(message));
|
|
724
|
-
return;
|
|
725
|
-
}
|
|
726
|
-
// Use handler to transform response, or resolve with raw message
|
|
727
|
-
const handler = request.transform ?? responseHandlers[message.type];
|
|
728
|
-
if (handler) {
|
|
729
|
-
try {
|
|
730
|
-
request.resolve(handler(message));
|
|
731
|
-
}
|
|
732
|
-
catch (err) {
|
|
733
|
-
request.reject(err);
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
else {
|
|
737
|
-
logger.warn('Received unexpected message type:', message.type);
|
|
738
|
-
request.resolve(message);
|
|
739
|
-
}
|
|
740
|
-
});
|
|
741
|
-
ws.on('error', (err) => {
|
|
742
|
-
lastError = err.message;
|
|
743
|
-
logger.error('WebSocket error:', err.message);
|
|
744
|
-
if (!opened)
|
|
745
|
-
handshakeError ?? (handshakeError = err);
|
|
746
|
-
});
|
|
747
|
-
ws.on('close', () => {
|
|
748
|
-
clearTimeout(handshakeDeadline);
|
|
749
|
-
if (pingInterval) {
|
|
750
|
-
clearInterval(pingInterval);
|
|
751
|
-
pingInterval = undefined;
|
|
752
|
-
}
|
|
753
|
-
const shouldReconnect = !intentionalDisconnect &&
|
|
754
|
-
!isNonRetryableError(lastError ?? '') &&
|
|
755
|
-
connectionState !== 'disconnected';
|
|
756
|
-
updateConnectionState('disconnected');
|
|
757
|
-
logger.debug('Disconnected from server.');
|
|
758
|
-
failPendingRequests('Connection closed');
|
|
759
|
-
if (!hasResolved) {
|
|
760
|
-
const err = handshakeError ?? new Error('Connection closed before the client was ready');
|
|
761
|
-
// Retry only the opening handshake. Once open, deviceInfo owns its failure.
|
|
762
|
-
const code = err.code;
|
|
763
|
-
const transient = ['ETIMEDOUT', 'ECONNRESET', 'ECONNREFUSED', 'EHOSTUNREACH', 'ENETUNREACH', 'EAI_AGAIN'].includes(code ?? '') ||
|
|
764
|
-
/^(Opening handshake has timed out|socket hang up|Unexpected server response: 50[234])$/.test(err.message);
|
|
765
|
-
if (!opened && !retriedHandshake && maxReconnectAttempts > 0 && transient) {
|
|
766
|
-
retriedHandshake = true;
|
|
767
|
-
updateConnectionState('reconnecting');
|
|
768
|
-
reconnectTimeout = setTimeout(setupWebSocket, 100 + Math.floor(Math.random() * 151));
|
|
769
|
-
}
|
|
770
|
-
else {
|
|
771
|
-
cleanupConnection();
|
|
772
|
-
rejectConnection(err);
|
|
773
|
-
}
|
|
774
|
-
return;
|
|
775
|
-
}
|
|
776
|
-
if (shouldReconnect) {
|
|
777
|
-
scheduleReconnect();
|
|
778
|
-
}
|
|
779
|
-
else if (isNonRetryableError(lastError ?? '')) {
|
|
780
|
-
logger.error(`Closing connection due to non-retryable error: ${lastError}`);
|
|
781
|
-
cleanup();
|
|
782
|
-
updateConnectionState('disconnected');
|
|
783
|
-
failPendingRequests('Non-retryable error');
|
|
784
|
-
clearServerNotifications();
|
|
785
|
-
logger.debug('Non-retryable error. Closing connection.');
|
|
786
|
-
}
|
|
787
|
-
});
|
|
788
|
-
ws.on('open', async () => {
|
|
789
|
-
clearTimeout(handshakeDeadline);
|
|
790
|
-
opened = true;
|
|
791
|
-
logger.debug('WebSocket connected');
|
|
792
|
-
reconnectAttempts = 0;
|
|
793
|
-
lastError = undefined;
|
|
794
|
-
updateConnectionState('connected');
|
|
795
|
-
pingInterval = setInterval(() => {
|
|
796
|
-
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
797
|
-
ws.ping();
|
|
798
|
-
}
|
|
799
|
-
}, 30000);
|
|
800
|
-
if (!hasResolved) {
|
|
801
|
-
try {
|
|
802
|
-
// Fetch device info before resolving connection
|
|
803
|
-
cachedDeviceInfo = await fetchDeviceInfo();
|
|
804
|
-
logger.debug('Device info fetched:', cachedDeviceInfo);
|
|
805
|
-
}
|
|
806
|
-
catch (err) {
|
|
807
|
-
logger.error('Failed to fetch device info:', err);
|
|
808
|
-
cleanupConnection();
|
|
809
|
-
updateConnectionState('disconnected');
|
|
810
|
-
rejectConnection(err);
|
|
811
|
-
return;
|
|
812
|
-
}
|
|
813
|
-
hasResolved = true;
|
|
814
|
-
resolveConnection({
|
|
815
|
-
screenshot,
|
|
816
|
-
elementTree,
|
|
817
|
-
elementTreeRaw,
|
|
818
|
-
readElementTree,
|
|
819
|
-
focusedElement,
|
|
820
|
-
tap,
|
|
821
|
-
tapWithScreenSize,
|
|
822
|
-
tapElement,
|
|
823
|
-
incrementElement,
|
|
824
|
-
decrementElement,
|
|
825
|
-
setElementValue,
|
|
826
|
-
typeText,
|
|
827
|
-
pressKey,
|
|
828
|
-
toggleKeyboard,
|
|
829
|
-
launchApp,
|
|
830
|
-
terminateApp,
|
|
831
|
-
watchApp,
|
|
832
|
-
monitorApp,
|
|
833
|
-
appLogTail,
|
|
834
|
-
streamAppLog,
|
|
835
|
-
streamSyslog,
|
|
836
|
-
listApps,
|
|
837
|
-
openUrl,
|
|
838
|
-
installApp,
|
|
839
|
-
saveKeychain,
|
|
840
|
-
restoreKeychain,
|
|
841
|
-
resetKeychain,
|
|
842
|
-
setPermission,
|
|
843
|
-
getPermission,
|
|
844
|
-
restartServices,
|
|
845
|
-
setOrientation,
|
|
846
|
-
getFoldState,
|
|
847
|
-
screenshotDisplay,
|
|
848
|
-
tapDisplay,
|
|
849
|
-
setDuoOrientation,
|
|
850
|
-
setHingeAngle,
|
|
851
|
-
scroll,
|
|
852
|
-
performActions,
|
|
853
|
-
startRecording,
|
|
854
|
-
stopRecording,
|
|
855
|
-
startRecordingSession,
|
|
856
|
-
stopRecordingSession,
|
|
857
|
-
startAppLogCapture,
|
|
858
|
-
stopAppLogCapture,
|
|
859
|
-
startEventCapture,
|
|
860
|
-
stopEventCapture,
|
|
861
|
-
streamAppLogCapture,
|
|
862
|
-
streamEventCapture,
|
|
863
|
-
startPerformanceCapture,
|
|
864
|
-
stopPerformanceCapture,
|
|
865
|
-
streamPerformance: streamClientPerformance,
|
|
866
|
-
playOnMicrophone,
|
|
867
|
-
stopMicrophonePlayback,
|
|
868
|
-
microphoneStatus,
|
|
869
|
-
keepAlive,
|
|
870
|
-
syncApp,
|
|
871
|
-
setStoreKitConfig,
|
|
872
|
-
addCaCertificate,
|
|
873
|
-
setBlockedDomains,
|
|
874
|
-
getBlockedDomains,
|
|
875
|
-
clearStoreKitConfig,
|
|
876
|
-
discoverStoreKitConfig,
|
|
877
|
-
softReset,
|
|
878
|
-
startReverseTunnel,
|
|
879
|
-
startTunnel,
|
|
880
|
-
getTunnelStatus,
|
|
881
|
-
stopTunnel,
|
|
882
|
-
targetHttpPortUrl,
|
|
883
|
-
startHttpProxy,
|
|
884
|
-
startForwardHttpProxy,
|
|
885
|
-
disconnect,
|
|
886
|
-
getConnectionState,
|
|
887
|
-
onConnectionStateChange,
|
|
888
|
-
simctl,
|
|
889
|
-
lldb,
|
|
890
|
-
xcrun,
|
|
891
|
-
startXcrunShim,
|
|
892
|
-
xcodebuild,
|
|
893
|
-
pushFile,
|
|
894
|
-
pullFile,
|
|
895
|
-
listFiles,
|
|
896
|
-
deleteFile,
|
|
897
|
-
setCameraVideo,
|
|
898
|
-
clearCameraVideo,
|
|
899
|
-
lsof,
|
|
900
|
-
deviceInfo: cachedDeviceInfo,
|
|
901
|
-
getDeviceInfo,
|
|
902
|
-
});
|
|
903
|
-
}
|
|
904
|
-
});
|
|
905
|
-
};
|
|
906
541
|
// ========================================================================
|
|
907
542
|
// Client Methods - using centralized sendRequest
|
|
908
543
|
// ========================================================================
|
|
@@ -1194,14 +829,13 @@ export async function createInstanceClient(options) {
|
|
|
1194
829
|
return sendRequest('appLogTail', { bundleId, lines });
|
|
1195
830
|
};
|
|
1196
831
|
const streamAppLog = (bundleId) => {
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
return new LogStream(endpointWebSocketUrl, subscribeMessage, 'streamAppLogTerminate', id);
|
|
832
|
+
// The instance serves app logs as server-sent events; appLogTail stays on signaling.
|
|
833
|
+
return new LogStream({ apiUrl: options.apiUrl, token: options.token, bundleId });
|
|
1200
834
|
};
|
|
1201
835
|
const streamSyslog = () => {
|
|
1202
836
|
const id = generateId();
|
|
1203
837
|
const subscribeMessage = { type: 'streamSyslog', id };
|
|
1204
|
-
return new LogStream(
|
|
838
|
+
return new LogStream(transport.url, subscribeMessage, 'streamSyslogTerminate', id);
|
|
1205
839
|
};
|
|
1206
840
|
const listApps = () => {
|
|
1207
841
|
return sendRequest('listApps');
|
|
@@ -1379,15 +1013,7 @@ export async function createInstanceClient(options) {
|
|
|
1379
1013
|
const microphoneStatus = async () => {
|
|
1380
1014
|
return sendRequest('microphoneStatus');
|
|
1381
1015
|
};
|
|
1382
|
-
const keepAlive = () =>
|
|
1383
|
-
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
|
1384
|
-
return;
|
|
1385
|
-
}
|
|
1386
|
-
ws.send(JSON.stringify({
|
|
1387
|
-
type: 'keepAlive',
|
|
1388
|
-
sessionId: keepAliveSessionId,
|
|
1389
|
-
}));
|
|
1390
|
-
};
|
|
1016
|
+
const keepAlive = () => transport.keepAlive();
|
|
1391
1017
|
const syncApp = async (localAppBundlePath, opts) => {
|
|
1392
1018
|
const preparedApp = await prepareAppBundlePath(localAppBundlePath);
|
|
1393
1019
|
const appBundlePath = preparedApp.appPath;
|
|
@@ -1498,7 +1124,7 @@ export async function createInstanceClient(options) {
|
|
|
1498
1124
|
});
|
|
1499
1125
|
}
|
|
1500
1126
|
// Send request asynchronously
|
|
1501
|
-
if (!
|
|
1127
|
+
if (!transport.isOpen()) {
|
|
1502
1128
|
// Defer error to next tick to allow caller to attach listeners
|
|
1503
1129
|
process.nextTick(() => {
|
|
1504
1130
|
simctlExecutions.delete(id);
|
|
@@ -1508,7 +1134,7 @@ export async function createInstanceClient(options) {
|
|
|
1508
1134
|
}
|
|
1509
1135
|
// stdin can carry secrets such as one-time codes, so it stays out of the log.
|
|
1510
1136
|
logger.debug(`Sending ${request.type} request:`, { ...request, stdin: undefined });
|
|
1511
|
-
|
|
1137
|
+
transport.send(request, (err) => {
|
|
1512
1138
|
if (err) {
|
|
1513
1139
|
logger.error(`Failed to send ${request.type} request:`, err);
|
|
1514
1140
|
simctlExecutions.delete(id);
|
|
@@ -1540,25 +1166,20 @@ export async function createInstanceClient(options) {
|
|
|
1540
1166
|
const lldb = (args, opts = {}) => {
|
|
1541
1167
|
const id = generateId();
|
|
1542
1168
|
const sendInput = (data, close, sequence) => {
|
|
1543
|
-
|
|
1544
|
-
return;
|
|
1545
|
-
ws.send(JSON.stringify({
|
|
1169
|
+
transport.send({
|
|
1546
1170
|
type: 'lldbInput',
|
|
1547
1171
|
id,
|
|
1548
1172
|
sequence,
|
|
1549
1173
|
...(data && { data: data.toString('base64') }),
|
|
1550
1174
|
...(close && { close }),
|
|
1551
|
-
})
|
|
1175
|
+
});
|
|
1552
1176
|
};
|
|
1553
1177
|
const execution = new LldbExecution(() => {
|
|
1554
1178
|
// The session stays tracked: the instance answers with the exit of LLDB, which is what
|
|
1555
1179
|
// ends `wait()`. A session dropped without `end` would otherwise keep the app stopped.
|
|
1556
1180
|
if (!execution.isRunning)
|
|
1557
1181
|
return;
|
|
1558
|
-
if (
|
|
1559
|
-
ws.send(JSON.stringify({ type: 'simctlTerminate', id }));
|
|
1560
|
-
}
|
|
1561
|
-
else {
|
|
1182
|
+
if (!transport.send({ type: 'simctlTerminate', id })) {
|
|
1562
1183
|
// Without a connection the instance has already ended the session and cannot say so.
|
|
1563
1184
|
simctlExecutions.delete(id);
|
|
1564
1185
|
execution._handleExit(143);
|
|
@@ -1829,24 +1450,163 @@ export async function createInstanceClient(options) {
|
|
|
1829
1450
|
httpProxyCleanups.push(proxy.close);
|
|
1830
1451
|
return proxy.port;
|
|
1831
1452
|
};
|
|
1832
|
-
const disconnect = () =>
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
logger
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1453
|
+
const disconnect = () => transport.disconnect();
|
|
1454
|
+
const getConnectionState = () => transport.getConnectionState();
|
|
1455
|
+
const onConnectionStateChange = (callback) => transport.onConnectionStateChange(callback);
|
|
1456
|
+
transport = await openSignalingTransport({
|
|
1457
|
+
apiUrl: options.apiUrl,
|
|
1458
|
+
token: options.token,
|
|
1459
|
+
logger,
|
|
1460
|
+
...(options.maxReconnectAttempts !== undefined && {
|
|
1461
|
+
maxReconnectAttempts: options.maxReconnectAttempts,
|
|
1462
|
+
}),
|
|
1463
|
+
...(options.reconnectDelay !== undefined && { reconnectDelay: options.reconnectDelay }),
|
|
1464
|
+
...(options.maxReconnectDelay !== undefined && { maxReconnectDelay: options.maxReconnectDelay }),
|
|
1465
|
+
responseHandlers: responseHandlers,
|
|
1466
|
+
replyError: (message) => commandError(message),
|
|
1467
|
+
redactRequest: redactRequestForDebug,
|
|
1468
|
+
handleMessage: (signal) => {
|
|
1469
|
+
const message = signal;
|
|
1470
|
+
if (handleAppExit(message) || handleAppHang(message)) {
|
|
1471
|
+
return true;
|
|
1472
|
+
}
|
|
1473
|
+
// Handle simctl streaming separately (it uses multiple messages per request)
|
|
1474
|
+
if (message.type === 'simctlStream') {
|
|
1475
|
+
const execution = simctlExecutions.get(message.id);
|
|
1476
|
+
if (!execution) {
|
|
1477
|
+
logger.debug(`Received simctl stream for unknown execution: ${message.id}`);
|
|
1478
|
+
return true;
|
|
1479
|
+
}
|
|
1480
|
+
if (execution instanceof LldbExecution) {
|
|
1481
|
+
execution._handleStarted();
|
|
1482
|
+
}
|
|
1483
|
+
if (message.stdout) {
|
|
1484
|
+
try {
|
|
1485
|
+
execution._handleStdout(Buffer.from(message.stdout, 'base64'));
|
|
1486
|
+
}
|
|
1487
|
+
catch (err) {
|
|
1488
|
+
logger.error('Failed to decode stdout data:', err);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
if (message.stderr) {
|
|
1492
|
+
try {
|
|
1493
|
+
execution._handleStderr(Buffer.from(message.stderr, 'base64'));
|
|
1494
|
+
}
|
|
1495
|
+
catch (err) {
|
|
1496
|
+
logger.error('Failed to decode stderr data:', err);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
if (message.exitCode !== undefined) {
|
|
1500
|
+
logger.debug(`Simctl execution ${message.id} completed with exit code ${message.exitCode}`);
|
|
1501
|
+
// Forget the execution first: an exit listener may disconnect, and disconnecting
|
|
1502
|
+
// fails every execution still tracked, including this one.
|
|
1503
|
+
simctlExecutions.delete(message.id);
|
|
1504
|
+
execution._handleExit(message.exitCode);
|
|
1505
|
+
}
|
|
1506
|
+
return true;
|
|
1507
|
+
}
|
|
1508
|
+
return false;
|
|
1509
|
+
},
|
|
1510
|
+
onConnectionLost: failExecutions,
|
|
1511
|
+
onClosed: () => {
|
|
1512
|
+
cleanupClientResources();
|
|
1513
|
+
clearServerNotifications();
|
|
1514
|
+
},
|
|
1515
|
+
onFirstOpen: async (opened) => {
|
|
1516
|
+
transport = opened;
|
|
1517
|
+
cachedDeviceInfo = await fetchDeviceInfo();
|
|
1518
|
+
logger.debug('Device info fetched:', cachedDeviceInfo);
|
|
1519
|
+
},
|
|
1520
|
+
});
|
|
1521
|
+
return {
|
|
1522
|
+
screenshot,
|
|
1523
|
+
elementTree,
|
|
1524
|
+
elementTreeRaw,
|
|
1525
|
+
focusedElement,
|
|
1526
|
+
readElementTree,
|
|
1527
|
+
tap,
|
|
1528
|
+
tapWithScreenSize,
|
|
1529
|
+
tapElement,
|
|
1530
|
+
incrementElement,
|
|
1531
|
+
decrementElement,
|
|
1532
|
+
setElementValue,
|
|
1533
|
+
typeText,
|
|
1534
|
+
pressKey,
|
|
1535
|
+
toggleKeyboard,
|
|
1536
|
+
launchApp,
|
|
1537
|
+
terminateApp,
|
|
1538
|
+
watchApp,
|
|
1539
|
+
monitorApp,
|
|
1540
|
+
appLogTail,
|
|
1541
|
+
streamAppLog,
|
|
1542
|
+
streamSyslog,
|
|
1543
|
+
listApps,
|
|
1544
|
+
openUrl,
|
|
1545
|
+
installApp,
|
|
1546
|
+
saveKeychain,
|
|
1547
|
+
restoreKeychain,
|
|
1548
|
+
resetKeychain,
|
|
1549
|
+
setPermission,
|
|
1550
|
+
getPermission,
|
|
1551
|
+
restartServices,
|
|
1552
|
+
setOrientation,
|
|
1553
|
+
getFoldState,
|
|
1554
|
+
screenshotDisplay,
|
|
1555
|
+
tapDisplay,
|
|
1556
|
+
setDuoOrientation,
|
|
1557
|
+
setHingeAngle,
|
|
1558
|
+
scroll,
|
|
1559
|
+
performActions,
|
|
1560
|
+
startRecording,
|
|
1561
|
+
stopRecording,
|
|
1562
|
+
startRecordingSession,
|
|
1563
|
+
stopRecordingSession,
|
|
1564
|
+
startAppLogCapture,
|
|
1565
|
+
stopAppLogCapture,
|
|
1566
|
+
startEventCapture,
|
|
1567
|
+
stopEventCapture,
|
|
1568
|
+
streamAppLogCapture,
|
|
1569
|
+
streamEventCapture,
|
|
1570
|
+
startPerformanceCapture,
|
|
1571
|
+
stopPerformanceCapture,
|
|
1572
|
+
streamPerformance: streamClientPerformance,
|
|
1573
|
+
playOnMicrophone,
|
|
1574
|
+
stopMicrophonePlayback,
|
|
1575
|
+
microphoneStatus,
|
|
1576
|
+
keepAlive,
|
|
1577
|
+
syncApp,
|
|
1578
|
+
setStoreKitConfig,
|
|
1579
|
+
addCaCertificate,
|
|
1580
|
+
setBlockedDomains,
|
|
1581
|
+
getBlockedDomains,
|
|
1582
|
+
clearStoreKitConfig,
|
|
1583
|
+
discoverStoreKitConfig,
|
|
1584
|
+
softReset,
|
|
1585
|
+
startReverseTunnel,
|
|
1586
|
+
startTunnel,
|
|
1587
|
+
getTunnelStatus,
|
|
1588
|
+
stopTunnel,
|
|
1589
|
+
targetHttpPortUrl,
|
|
1590
|
+
startHttpProxy,
|
|
1591
|
+
startForwardHttpProxy,
|
|
1592
|
+
disconnect,
|
|
1593
|
+
getConnectionState,
|
|
1594
|
+
onConnectionStateChange,
|
|
1595
|
+
simctl,
|
|
1596
|
+
lldb,
|
|
1597
|
+
xcrun,
|
|
1598
|
+
startXcrunShim,
|
|
1599
|
+
xcodebuild,
|
|
1600
|
+
pushFile,
|
|
1601
|
+
pullFile,
|
|
1602
|
+
listFiles,
|
|
1603
|
+
deleteFile,
|
|
1604
|
+
setCameraVideo,
|
|
1605
|
+
clearCameraVideo,
|
|
1606
|
+
lsof,
|
|
1607
|
+
deviceInfo: cachedDeviceInfo,
|
|
1608
|
+
getDeviceInfo,
|
|
1848
1609
|
};
|
|
1849
|
-
|
|
1850
|
-
});
|
|
1610
|
+
}
|
|
1851
1611
|
}
|
|
1852
1612
|
//# sourceMappingURL=ios-client.mjs.map
|