capdebug 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/android/build/.transforms/105231053f5053e70f17f15178bed793/results.bin +1 -0
- package/android/build/.transforms/7c2b02c40854250ff97be5e9e723091e/results.bin +1 -0
- package/android/build/.transforms/ca1c0f6bb67b5238fd83a40c3a1c0ddd/results.bin +1 -0
- package/android/build/.transforms/ca1c0f6bb67b5238fd83a40c3a1c0ddd/transformed/classes/classes_dex/classes.dex +0 -0
- package/android/build/intermediates/aapt_friendly_merged_manifests/debug/aapt/AndroidManifest.xml +9 -0
- package/android/build/intermediates/aapt_friendly_merged_manifests/debug/aapt/output-metadata.json +18 -0
- package/android/build/intermediates/aar_metadata/debug/aar-metadata.properties +6 -0
- package/android/build/intermediates/annotation_processor_list/debug/annotationProcessors.json +1 -0
- package/android/build/intermediates/compile_library_classes_jar/debug/classes.jar +0 -0
- package/android/build/intermediates/compile_r_class_jar/debug/R.jar +0 -0
- package/android/build/intermediates/compile_symbol_list/debug/R.txt +0 -0
- package/android/build/intermediates/incremental/debug/packageDebugResources/compile-file-map.properties +1 -0
- package/android/build/intermediates/incremental/debug/packageDebugResources/merger.xml +2 -0
- package/android/build/intermediates/incremental/mergeDebugJniLibFolders/merger.xml +2 -0
- package/android/build/intermediates/incremental/mergeDebugShaders/merger.xml +2 -0
- package/android/build/intermediates/incremental/packageDebugAssets/merger.xml +2 -0
- package/android/build/intermediates/java_res/debug/out/META-INF/capdebug_debug.kotlin_module +0 -0
- package/android/build/intermediates/local_only_symbol_list/debug/R-def.txt +2 -0
- package/android/build/intermediates/manifest_merge_blame_file/debug/manifest-merger-blame-debug-report.txt +11 -0
- package/android/build/intermediates/merged_manifest/debug/AndroidManifest.xml +9 -0
- package/android/build/intermediates/navigation_json/debug/navigation.json +1 -0
- package/android/build/intermediates/runtime_library_classes_jar/debug/classes.jar +0 -0
- package/android/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt +1 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/counters.tab +2 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/last-build.bin +0 -0
- package/android/build/kotlin/compileDebugKotlin/classpath-snapshot/shrunk-classpath-snapshot.bin +0 -0
- package/android/build/kotlin/compileDebugKotlin/local-state/build-history.bin +0 -0
- package/android/build/outputs/logs/manifest-merger-debug-report.txt +20 -0
- package/android/build/tmp/kotlin-classes/debug/META-INF/capdebug_debug.kotlin_module +0 -0
- package/android/build/tmp/kotlin-classes/debug/com/capdebug/plugin/CapDebugPlugin$Companion.class +0 -0
- package/android/build/tmp/kotlin-classes/debug/com/capdebug/plugin/CapDebugPlugin.class +0 -0
- package/bin/capdebug.js +8 -0
- package/dist/capacitor/deep-link.d.ts +4 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/qr.d.ts +15 -0
- package/dist/cli/server.d.ts +34 -0
- package/dist/cli.js +1868 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/plugin.cjs.js +866 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +866 -0
- package/dist/plugin.js.map +1 -1
- package/dist/plugin.mjs +859 -1
- package/dist/plugin.mjs.map +1 -1
- package/dist/remote/buffer.d.ts +13 -0
- package/dist/remote/desktop-viewer-html.d.ts +10 -0
- package/dist/remote/pairing-page-html.d.ts +8 -0
- package/dist/remote/pairing.d.ts +34 -0
- package/dist/remote/protocol.d.ts +14 -0
- package/dist/remote/remote-manager.d.ts +19 -0
- package/dist/remote/signaling.d.ts +27 -0
- package/dist/remote/transport.d.ts +15 -0
- package/dist/remote/types.d.ts +69 -0
- package/dist/remote/webrtc-transport.d.ts +47 -0
- package/package.json +5 -1
package/dist/plugin.js
CHANGED
|
@@ -2374,10 +2374,861 @@ var capacitorCapDebug = (function (exports, core) {
|
|
|
2374
2374
|
}
|
|
2375
2375
|
}
|
|
2376
2376
|
|
|
2377
|
+
class HttpSseSignalingClient {
|
|
2378
|
+
baseUrl;
|
|
2379
|
+
sessionId = "";
|
|
2380
|
+
secret = "";
|
|
2381
|
+
role = "peer";
|
|
2382
|
+
eventSource = null;
|
|
2383
|
+
listeners = /* @__PURE__ */ new Set();
|
|
2384
|
+
isClosed = false;
|
|
2385
|
+
pollTimer = null;
|
|
2386
|
+
constructor(baseUrl) {
|
|
2387
|
+
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
2388
|
+
}
|
|
2389
|
+
async createSession(ttlSeconds = 300) {
|
|
2390
|
+
this.role = "host";
|
|
2391
|
+
const res = await fetch(`${this.baseUrl}/api/pair/create`, {
|
|
2392
|
+
method: "POST",
|
|
2393
|
+
headers: { "Content-Type": "application/json" },
|
|
2394
|
+
body: JSON.stringify({ ttl: ttlSeconds })
|
|
2395
|
+
});
|
|
2396
|
+
if (!res.ok) {
|
|
2397
|
+
throw new Error(`Failed to create pairing session: ${res.statusText}`);
|
|
2398
|
+
}
|
|
2399
|
+
const data = await res.json();
|
|
2400
|
+
this.sessionId = data.sessionId;
|
|
2401
|
+
this.secret = data.secret;
|
|
2402
|
+
this.connectEventStream();
|
|
2403
|
+
return data;
|
|
2404
|
+
}
|
|
2405
|
+
async joinSession(sessionId, secret) {
|
|
2406
|
+
this.role = "peer";
|
|
2407
|
+
this.sessionId = sessionId;
|
|
2408
|
+
this.secret = secret;
|
|
2409
|
+
const res = await fetch(`${this.baseUrl}/api/pair/join`, {
|
|
2410
|
+
method: "POST",
|
|
2411
|
+
headers: { "Content-Type": "application/json" },
|
|
2412
|
+
body: JSON.stringify({ sessionId, secret })
|
|
2413
|
+
});
|
|
2414
|
+
if (!res.ok) {
|
|
2415
|
+
const err = await res.text().catch(() => "");
|
|
2416
|
+
throw new Error(`Failed to join pairing session: ${err || res.statusText}`);
|
|
2417
|
+
}
|
|
2418
|
+
this.connectEventStream();
|
|
2419
|
+
}
|
|
2420
|
+
async send(message) {
|
|
2421
|
+
if (this.isClosed) return;
|
|
2422
|
+
message.sessionId = this.sessionId;
|
|
2423
|
+
message.secret = this.secret;
|
|
2424
|
+
message.sender = this.role;
|
|
2425
|
+
message.timestamp = Date.now();
|
|
2426
|
+
await fetch(`${this.baseUrl}/api/pair/signal`, {
|
|
2427
|
+
method: "POST",
|
|
2428
|
+
headers: { "Content-Type": "application/json" },
|
|
2429
|
+
body: JSON.stringify(message)
|
|
2430
|
+
});
|
|
2431
|
+
}
|
|
2432
|
+
onMessage(callback) {
|
|
2433
|
+
this.listeners.add(callback);
|
|
2434
|
+
return () => {
|
|
2435
|
+
this.listeners.delete(callback);
|
|
2436
|
+
};
|
|
2437
|
+
}
|
|
2438
|
+
close() {
|
|
2439
|
+
this.isClosed = true;
|
|
2440
|
+
if (this.eventSource) {
|
|
2441
|
+
this.eventSource.close();
|
|
2442
|
+
this.eventSource = null;
|
|
2443
|
+
}
|
|
2444
|
+
if (this.pollTimer) {
|
|
2445
|
+
clearInterval(this.pollTimer);
|
|
2446
|
+
this.pollTimer = null;
|
|
2447
|
+
}
|
|
2448
|
+
this.listeners.clear();
|
|
2449
|
+
}
|
|
2450
|
+
connectEventStream() {
|
|
2451
|
+
if (typeof EventSource !== "undefined") {
|
|
2452
|
+
const sseUrl = `${this.baseUrl}/api/pair/events?session=${encodeURIComponent(this.sessionId)}&secret=${encodeURIComponent(this.secret)}&role=${this.role}`;
|
|
2453
|
+
this.eventSource = new EventSource(sseUrl);
|
|
2454
|
+
this.eventSource.onmessage = (event) => {
|
|
2455
|
+
if (!event.data) return;
|
|
2456
|
+
try {
|
|
2457
|
+
const msg = JSON.parse(event.data);
|
|
2458
|
+
this.dispatchMessage(msg);
|
|
2459
|
+
} catch {
|
|
2460
|
+
}
|
|
2461
|
+
};
|
|
2462
|
+
this.eventSource.onerror = () => {
|
|
2463
|
+
};
|
|
2464
|
+
} else {
|
|
2465
|
+
this.startPolling();
|
|
2466
|
+
}
|
|
2467
|
+
}
|
|
2468
|
+
startPolling() {
|
|
2469
|
+
if (this.pollTimer) return;
|
|
2470
|
+
this.pollTimer = setInterval(async () => {
|
|
2471
|
+
if (this.isClosed || !this.sessionId) return;
|
|
2472
|
+
try {
|
|
2473
|
+
const res = await fetch(
|
|
2474
|
+
`${this.baseUrl}/api/pair/poll?session=${encodeURIComponent(this.sessionId)}&secret=${encodeURIComponent(this.secret)}&role=${this.role}`
|
|
2475
|
+
);
|
|
2476
|
+
if (res.ok) {
|
|
2477
|
+
const messages = await res.json();
|
|
2478
|
+
if (Array.isArray(messages)) {
|
|
2479
|
+
for (const msg of messages) {
|
|
2480
|
+
this.dispatchMessage(msg);
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
}
|
|
2484
|
+
} catch {
|
|
2485
|
+
}
|
|
2486
|
+
}, 1e3);
|
|
2487
|
+
}
|
|
2488
|
+
dispatchMessage(message) {
|
|
2489
|
+
for (const listener of this.listeners) {
|
|
2490
|
+
try {
|
|
2491
|
+
listener(message);
|
|
2492
|
+
} catch (err) {
|
|
2493
|
+
console.error("[CapDebug Signaling] Listener error:", err);
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
const PROTOCOL_VERSION = 1;
|
|
2500
|
+
class RemoteProtocol {
|
|
2501
|
+
static createHello(capdebugVersion = "0.2.0", platform = "web", deviceName) {
|
|
2502
|
+
return {
|
|
2503
|
+
v: 1,
|
|
2504
|
+
type: "hello",
|
|
2505
|
+
payload: {
|
|
2506
|
+
capdebugVersion,
|
|
2507
|
+
platform,
|
|
2508
|
+
deviceName: deviceName || (typeof navigator !== "undefined" ? navigator.userAgent : "Unknown Device")
|
|
2509
|
+
},
|
|
2510
|
+
timestamp: Date.now()
|
|
2511
|
+
};
|
|
2512
|
+
}
|
|
2513
|
+
static createHelloAck() {
|
|
2514
|
+
return {
|
|
2515
|
+
v: 1,
|
|
2516
|
+
type: "hello-ack",
|
|
2517
|
+
payload: {
|
|
2518
|
+
serverTime: Date.now()
|
|
2519
|
+
},
|
|
2520
|
+
timestamp: Date.now()
|
|
2521
|
+
};
|
|
2522
|
+
}
|
|
2523
|
+
static createEvent(event) {
|
|
2524
|
+
return {
|
|
2525
|
+
v: 1,
|
|
2526
|
+
type: "event",
|
|
2527
|
+
event,
|
|
2528
|
+
timestamp: Date.now()
|
|
2529
|
+
};
|
|
2530
|
+
}
|
|
2531
|
+
static createBatch(events) {
|
|
2532
|
+
return {
|
|
2533
|
+
v: 1,
|
|
2534
|
+
type: "events-batch",
|
|
2535
|
+
events,
|
|
2536
|
+
timestamp: Date.now()
|
|
2537
|
+
};
|
|
2538
|
+
}
|
|
2539
|
+
static createControl(command) {
|
|
2540
|
+
return {
|
|
2541
|
+
v: 1,
|
|
2542
|
+
type: "control",
|
|
2543
|
+
command,
|
|
2544
|
+
timestamp: Date.now()
|
|
2545
|
+
};
|
|
2546
|
+
}
|
|
2547
|
+
static createPing(timestamp = Date.now()) {
|
|
2548
|
+
return {
|
|
2549
|
+
v: 1,
|
|
2550
|
+
type: "ping",
|
|
2551
|
+
timestamp
|
|
2552
|
+
};
|
|
2553
|
+
}
|
|
2554
|
+
static createPong(pingTimestamp) {
|
|
2555
|
+
return {
|
|
2556
|
+
v: 1,
|
|
2557
|
+
type: "pong",
|
|
2558
|
+
payload: { pingTimestamp },
|
|
2559
|
+
timestamp: Date.now()
|
|
2560
|
+
};
|
|
2561
|
+
}
|
|
2562
|
+
static serialize(message) {
|
|
2563
|
+
return JSON.stringify(message);
|
|
2564
|
+
}
|
|
2565
|
+
static parse(raw) {
|
|
2566
|
+
try {
|
|
2567
|
+
const parsed = JSON.parse(raw);
|
|
2568
|
+
if (!parsed || typeof parsed !== "object") {
|
|
2569
|
+
return null;
|
|
2570
|
+
}
|
|
2571
|
+
if (parsed.v !== PROTOCOL_VERSION) {
|
|
2572
|
+
console.warn(`[CapDebug Remote] Unsupported protocol version: ${parsed.v}`);
|
|
2573
|
+
return null;
|
|
2574
|
+
}
|
|
2575
|
+
return parsed;
|
|
2576
|
+
} catch {
|
|
2577
|
+
return null;
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
|
|
2582
|
+
class RemoteEventBuffer {
|
|
2583
|
+
buffer = [];
|
|
2584
|
+
maxCapacity;
|
|
2585
|
+
droppedCount = 0;
|
|
2586
|
+
constructor(maxCapacity = 100) {
|
|
2587
|
+
this.maxCapacity = Math.max(1, maxCapacity);
|
|
2588
|
+
}
|
|
2589
|
+
push(event) {
|
|
2590
|
+
if (this.buffer.length >= this.maxCapacity) {
|
|
2591
|
+
this.buffer.shift();
|
|
2592
|
+
}
|
|
2593
|
+
this.buffer.push(event);
|
|
2594
|
+
}
|
|
2595
|
+
flush() {
|
|
2596
|
+
const items = [...this.buffer];
|
|
2597
|
+
this.buffer = [];
|
|
2598
|
+
return items;
|
|
2599
|
+
}
|
|
2600
|
+
clear() {
|
|
2601
|
+
this.buffer = [];
|
|
2602
|
+
this.droppedCount = 0;
|
|
2603
|
+
}
|
|
2604
|
+
size() {
|
|
2605
|
+
return this.buffer.length;
|
|
2606
|
+
}
|
|
2607
|
+
getDroppedCount() {
|
|
2608
|
+
return this.droppedCount;
|
|
2609
|
+
}
|
|
2610
|
+
incrementDropped() {
|
|
2611
|
+
this.droppedCount++;
|
|
2612
|
+
}
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2615
|
+
class WebRTCTransport {
|
|
2616
|
+
id = "webrtc";
|
|
2617
|
+
pc = null;
|
|
2618
|
+
signaling = null;
|
|
2619
|
+
config = null;
|
|
2620
|
+
eventsChannel = null;
|
|
2621
|
+
controlChannel = null;
|
|
2622
|
+
status = "disconnected";
|
|
2623
|
+
buffer = new RemoteEventBuffer(100);
|
|
2624
|
+
eventListeners = /* @__PURE__ */ new Set();
|
|
2625
|
+
controlListeners = /* @__PURE__ */ new Set();
|
|
2626
|
+
statusListeners = /* @__PURE__ */ new Set();
|
|
2627
|
+
diagnosticsListeners = /* @__PURE__ */ new Set();
|
|
2628
|
+
pingTimer = null;
|
|
2629
|
+
statsTimer = null;
|
|
2630
|
+
rttMs = 0;
|
|
2631
|
+
totalEventsSent = 0;
|
|
2632
|
+
totalEventsReceived = 0;
|
|
2633
|
+
connectedAt = 0;
|
|
2634
|
+
remoteDeviceName = "Unknown Device";
|
|
2635
|
+
remotePlatform = "unknown";
|
|
2636
|
+
diagnostics = {
|
|
2637
|
+
transport: "webrtc",
|
|
2638
|
+
status: "disconnected",
|
|
2639
|
+
route: "unknown"
|
|
2640
|
+
};
|
|
2641
|
+
async connect(config) {
|
|
2642
|
+
this.config = config;
|
|
2643
|
+
this.setStatus("connecting");
|
|
2644
|
+
const signalingUrl = config.signalingUrl || (typeof window !== "undefined" ? window.location.origin : "http://localhost:4783");
|
|
2645
|
+
this.signaling = new HttpSseSignalingClient(signalingUrl);
|
|
2646
|
+
this.signaling.onMessage((msg) => this.handleSignalingMessage(msg));
|
|
2647
|
+
if (config.isHost) {
|
|
2648
|
+
this.setStatus("signaling");
|
|
2649
|
+
} else {
|
|
2650
|
+
this.setStatus("signaling");
|
|
2651
|
+
await this.signaling.joinSession(config.sessionId, config.secret);
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
send(event) {
|
|
2655
|
+
if (this.eventsChannel && this.eventsChannel.readyState === "open") {
|
|
2656
|
+
if (this.eventsChannel.bufferedAmount > 256 * 1024) {
|
|
2657
|
+
if (event.level === "debug" || event.level === "info") {
|
|
2658
|
+
this.buffer.incrementDropped();
|
|
2659
|
+
return;
|
|
2660
|
+
}
|
|
2661
|
+
}
|
|
2662
|
+
const msg = RemoteProtocol.createEvent(event);
|
|
2663
|
+
try {
|
|
2664
|
+
this.eventsChannel.send(RemoteProtocol.serialize(msg));
|
|
2665
|
+
this.totalEventsSent++;
|
|
2666
|
+
} catch (err) {
|
|
2667
|
+
console.error("[CapDebug Remote] Send error:", err);
|
|
2668
|
+
}
|
|
2669
|
+
} else {
|
|
2670
|
+
this.buffer.push(event);
|
|
2671
|
+
}
|
|
2672
|
+
}
|
|
2673
|
+
sendControl(command) {
|
|
2674
|
+
if (this.controlChannel && this.controlChannel.readyState === "open") {
|
|
2675
|
+
const msg = RemoteProtocol.createControl(command);
|
|
2676
|
+
try {
|
|
2677
|
+
this.controlChannel.send(RemoteProtocol.serialize(msg));
|
|
2678
|
+
} catch (err) {
|
|
2679
|
+
console.error("[CapDebug Remote] Control send error:", err);
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
disconnect() {
|
|
2684
|
+
this.stopTimers();
|
|
2685
|
+
if (this.controlChannel) {
|
|
2686
|
+
try {
|
|
2687
|
+
this.sendControl({ type: "disconnect" });
|
|
2688
|
+
} catch {
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
if (this.signaling) {
|
|
2692
|
+
this.signaling.close();
|
|
2693
|
+
this.signaling = null;
|
|
2694
|
+
}
|
|
2695
|
+
if (this.eventsChannel) {
|
|
2696
|
+
this.eventsChannel.close();
|
|
2697
|
+
this.eventsChannel = null;
|
|
2698
|
+
}
|
|
2699
|
+
if (this.controlChannel) {
|
|
2700
|
+
this.controlChannel.close();
|
|
2701
|
+
this.controlChannel = null;
|
|
2702
|
+
}
|
|
2703
|
+
if (this.pc) {
|
|
2704
|
+
this.pc.close();
|
|
2705
|
+
this.pc = null;
|
|
2706
|
+
}
|
|
2707
|
+
this.setStatus("disconnected");
|
|
2708
|
+
}
|
|
2709
|
+
getStatus() {
|
|
2710
|
+
return this.status;
|
|
2711
|
+
}
|
|
2712
|
+
getDiagnostics() {
|
|
2713
|
+
return {
|
|
2714
|
+
...this.diagnostics,
|
|
2715
|
+
status: this.status,
|
|
2716
|
+
rttMs: this.rttMs,
|
|
2717
|
+
bufferedAmountBytes: this.eventsChannel?.bufferedAmount || 0,
|
|
2718
|
+
droppedEventsCount: this.buffer.getDroppedCount(),
|
|
2719
|
+
totalEventsSent: this.totalEventsSent,
|
|
2720
|
+
totalEventsReceived: this.totalEventsReceived,
|
|
2721
|
+
connectedAt: this.connectedAt,
|
|
2722
|
+
deviceName: this.remoteDeviceName,
|
|
2723
|
+
platform: this.remotePlatform
|
|
2724
|
+
};
|
|
2725
|
+
}
|
|
2726
|
+
onEvent(callback) {
|
|
2727
|
+
this.eventListeners.add(callback);
|
|
2728
|
+
return () => this.eventListeners.delete(callback);
|
|
2729
|
+
}
|
|
2730
|
+
onControl(callback) {
|
|
2731
|
+
this.controlListeners.add(callback);
|
|
2732
|
+
return () => this.controlListeners.delete(callback);
|
|
2733
|
+
}
|
|
2734
|
+
onStatusChange(callback) {
|
|
2735
|
+
this.statusListeners.add(callback);
|
|
2736
|
+
callback(this.status);
|
|
2737
|
+
return () => this.statusListeners.delete(callback);
|
|
2738
|
+
}
|
|
2739
|
+
onDiagnostics(callback) {
|
|
2740
|
+
this.diagnosticsListeners.add(callback);
|
|
2741
|
+
return () => this.diagnosticsListeners.delete(callback);
|
|
2742
|
+
}
|
|
2743
|
+
async handleSignalingMessage(msg) {
|
|
2744
|
+
switch (msg.type) {
|
|
2745
|
+
case "peer-joined":
|
|
2746
|
+
if (this.config?.isHost) {
|
|
2747
|
+
await this.startHandshakeAsInitiator();
|
|
2748
|
+
}
|
|
2749
|
+
break;
|
|
2750
|
+
case "offer":
|
|
2751
|
+
if (!this.config?.isHost) {
|
|
2752
|
+
await this.handleOffer(msg.payload);
|
|
2753
|
+
}
|
|
2754
|
+
break;
|
|
2755
|
+
case "answer":
|
|
2756
|
+
if (this.config?.isHost && this.pc) {
|
|
2757
|
+
await this.pc.setRemoteDescription(new RTCSessionDescription(msg.payload));
|
|
2758
|
+
}
|
|
2759
|
+
break;
|
|
2760
|
+
case "ice-candidate":
|
|
2761
|
+
if (this.pc && msg.payload) {
|
|
2762
|
+
try {
|
|
2763
|
+
await this.pc.addIceCandidate(new RTCIceCandidate(msg.payload));
|
|
2764
|
+
} catch (err) {
|
|
2765
|
+
console.warn("[CapDebug Remote] ICE Candidate error:", err);
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
break;
|
|
2769
|
+
case "disconnect":
|
|
2770
|
+
this.disconnect();
|
|
2771
|
+
break;
|
|
2772
|
+
}
|
|
2773
|
+
}
|
|
2774
|
+
initPeerConnection() {
|
|
2775
|
+
const iceServers = this.config?.iceServers || [
|
|
2776
|
+
{ urls: "stun:stun.l.google.com:19302" },
|
|
2777
|
+
{ urls: "stun:global.stun.twilio.com:3478" }
|
|
2778
|
+
];
|
|
2779
|
+
const pc = new RTCPeerConnection({ iceServers });
|
|
2780
|
+
pc.onicecandidate = (event) => {
|
|
2781
|
+
if (event.candidate && this.signaling) {
|
|
2782
|
+
this.signaling.send({
|
|
2783
|
+
sessionId: this.config.sessionId,
|
|
2784
|
+
secret: this.config.secret,
|
|
2785
|
+
type: "ice-candidate",
|
|
2786
|
+
sender: this.config.isHost ? "host" : "peer",
|
|
2787
|
+
payload: event.candidate.toJSON()
|
|
2788
|
+
});
|
|
2789
|
+
}
|
|
2790
|
+
};
|
|
2791
|
+
pc.onconnectionstatechange = () => {
|
|
2792
|
+
this.updateDiagnostics();
|
|
2793
|
+
if (pc.connectionState === "failed") {
|
|
2794
|
+
this.setStatus("failed");
|
|
2795
|
+
} else if (pc.connectionState === "closed" || pc.connectionState === "disconnected") {
|
|
2796
|
+
this.setStatus("disconnected");
|
|
2797
|
+
}
|
|
2798
|
+
};
|
|
2799
|
+
pc.oniceconnectionstatechange = () => {
|
|
2800
|
+
this.updateDiagnostics();
|
|
2801
|
+
};
|
|
2802
|
+
this.pc = pc;
|
|
2803
|
+
return pc;
|
|
2804
|
+
}
|
|
2805
|
+
async startHandshakeAsInitiator() {
|
|
2806
|
+
const pc = this.initPeerConnection();
|
|
2807
|
+
this.eventsChannel = pc.createDataChannel("events", { ordered: true });
|
|
2808
|
+
this.controlChannel = pc.createDataChannel("control", { ordered: true });
|
|
2809
|
+
this.setupDataChannel(this.eventsChannel, "events");
|
|
2810
|
+
this.setupDataChannel(this.controlChannel, "control");
|
|
2811
|
+
const offer = await pc.createOffer();
|
|
2812
|
+
await pc.setLocalDescription(offer);
|
|
2813
|
+
if (this.signaling) {
|
|
2814
|
+
await this.signaling.send({
|
|
2815
|
+
sessionId: this.config.sessionId,
|
|
2816
|
+
secret: this.config.secret,
|
|
2817
|
+
type: "offer",
|
|
2818
|
+
sender: "host",
|
|
2819
|
+
payload: offer
|
|
2820
|
+
});
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
async handleOffer(offerSdp) {
|
|
2824
|
+
const pc = this.initPeerConnection();
|
|
2825
|
+
pc.ondatachannel = (event) => {
|
|
2826
|
+
const channel = event.channel;
|
|
2827
|
+
if (channel.label === "events") {
|
|
2828
|
+
this.eventsChannel = channel;
|
|
2829
|
+
this.setupDataChannel(channel, "events");
|
|
2830
|
+
} else if (channel.label === "control") {
|
|
2831
|
+
this.controlChannel = channel;
|
|
2832
|
+
this.setupDataChannel(channel, "control");
|
|
2833
|
+
}
|
|
2834
|
+
};
|
|
2835
|
+
await pc.setRemoteDescription(new RTCSessionDescription(offerSdp));
|
|
2836
|
+
const answer = await pc.createAnswer();
|
|
2837
|
+
await pc.setLocalDescription(answer);
|
|
2838
|
+
if (this.signaling) {
|
|
2839
|
+
await this.signaling.send({
|
|
2840
|
+
sessionId: this.config.sessionId,
|
|
2841
|
+
secret: this.config.secret,
|
|
2842
|
+
type: "answer",
|
|
2843
|
+
sender: "peer",
|
|
2844
|
+
payload: answer
|
|
2845
|
+
});
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
setupDataChannel(channel, label) {
|
|
2849
|
+
channel.onopen = () => {
|
|
2850
|
+
if (this.eventsChannel?.readyState === "open" && this.controlChannel?.readyState === "open") {
|
|
2851
|
+
this.connectedAt = Date.now();
|
|
2852
|
+
this.setStatus("connected");
|
|
2853
|
+
this.startTimers();
|
|
2854
|
+
if (!this.config?.isHost) {
|
|
2855
|
+
const hello = RemoteProtocol.createHello("0.2.0", "mobile");
|
|
2856
|
+
channel.send(RemoteProtocol.serialize(hello));
|
|
2857
|
+
const buffered = this.buffer.flush();
|
|
2858
|
+
if (buffered.length > 0 && this.eventsChannel?.readyState === "open") {
|
|
2859
|
+
const batch = RemoteProtocol.createBatch(buffered);
|
|
2860
|
+
this.eventsChannel.send(RemoteProtocol.serialize(batch));
|
|
2861
|
+
this.totalEventsSent += buffered.length;
|
|
2862
|
+
}
|
|
2863
|
+
}
|
|
2864
|
+
}
|
|
2865
|
+
};
|
|
2866
|
+
channel.onclose = () => {
|
|
2867
|
+
this.setStatus("disconnected");
|
|
2868
|
+
};
|
|
2869
|
+
channel.onmessage = (e) => {
|
|
2870
|
+
const raw = e.data;
|
|
2871
|
+
if (typeof raw !== "string") return;
|
|
2872
|
+
const message = RemoteProtocol.parse(raw);
|
|
2873
|
+
if (!message) return;
|
|
2874
|
+
if (label === "events") {
|
|
2875
|
+
if (message.type === "event" && message.event) {
|
|
2876
|
+
this.totalEventsReceived++;
|
|
2877
|
+
for (const listener of this.eventListeners) {
|
|
2878
|
+
listener(message.event);
|
|
2879
|
+
}
|
|
2880
|
+
} else if (message.type === "events-batch" && Array.isArray(message.events)) {
|
|
2881
|
+
this.totalEventsReceived += message.events.length;
|
|
2882
|
+
for (const ev of message.events) {
|
|
2883
|
+
for (const listener of this.eventListeners) {
|
|
2884
|
+
listener(ev);
|
|
2885
|
+
}
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
} else if (label === "control") {
|
|
2889
|
+
this.handleControlMessage(message);
|
|
2890
|
+
}
|
|
2891
|
+
};
|
|
2892
|
+
}
|
|
2893
|
+
handleControlMessage(message) {
|
|
2894
|
+
if (message.type === "control" && message.command) {
|
|
2895
|
+
for (const listener of this.controlListeners) {
|
|
2896
|
+
listener(message.command);
|
|
2897
|
+
}
|
|
2898
|
+
} else if (message.type === "hello") {
|
|
2899
|
+
this.remoteDeviceName = message.payload?.deviceName || "Mobile Device";
|
|
2900
|
+
this.remotePlatform = message.payload?.platform || "mobile";
|
|
2901
|
+
if (this.controlChannel?.readyState === "open") {
|
|
2902
|
+
this.controlChannel.send(RemoteProtocol.serialize(RemoteProtocol.createHelloAck()));
|
|
2903
|
+
}
|
|
2904
|
+
this.updateDiagnostics();
|
|
2905
|
+
} else if (message.type === "hello-ack") {
|
|
2906
|
+
this.updateDiagnostics();
|
|
2907
|
+
} else if (message.type === "ping") {
|
|
2908
|
+
if (this.controlChannel?.readyState === "open") {
|
|
2909
|
+
const pong = RemoteProtocol.createPong(message.timestamp || Date.now());
|
|
2910
|
+
this.controlChannel.send(RemoteProtocol.serialize(pong));
|
|
2911
|
+
}
|
|
2912
|
+
} else if (message.type === "pong") {
|
|
2913
|
+
const pingTime = message.payload?.pingTimestamp || message.timestamp;
|
|
2914
|
+
if (pingTime) {
|
|
2915
|
+
this.rttMs = Math.max(1, Date.now() - pingTime);
|
|
2916
|
+
this.updateDiagnostics();
|
|
2917
|
+
}
|
|
2918
|
+
}
|
|
2919
|
+
}
|
|
2920
|
+
startTimers() {
|
|
2921
|
+
this.stopTimers();
|
|
2922
|
+
this.pingTimer = setInterval(() => {
|
|
2923
|
+
if (this.controlChannel?.readyState === "open") {
|
|
2924
|
+
this.controlChannel.send(RemoteProtocol.serialize(RemoteProtocol.createPing(Date.now())));
|
|
2925
|
+
}
|
|
2926
|
+
}, 2e3);
|
|
2927
|
+
this.statsTimer = setInterval(() => {
|
|
2928
|
+
this.queryWebRTCStats();
|
|
2929
|
+
}, 3e3);
|
|
2930
|
+
}
|
|
2931
|
+
stopTimers() {
|
|
2932
|
+
if (this.pingTimer) {
|
|
2933
|
+
clearInterval(this.pingTimer);
|
|
2934
|
+
this.pingTimer = null;
|
|
2935
|
+
}
|
|
2936
|
+
if (this.statsTimer) {
|
|
2937
|
+
clearInterval(this.statsTimer);
|
|
2938
|
+
this.statsTimer = null;
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
async queryWebRTCStats() {
|
|
2942
|
+
if (!this.pc) return;
|
|
2943
|
+
try {
|
|
2944
|
+
const stats = await this.pc.getStats();
|
|
2945
|
+
let candidateType = "unknown";
|
|
2946
|
+
let protocol = "UDP";
|
|
2947
|
+
let route = "unknown";
|
|
2948
|
+
stats.forEach((report) => {
|
|
2949
|
+
if (report.type === "candidate-pair" && (report.selected || report.state === "succeeded")) {
|
|
2950
|
+
const localCandidate = stats.get(report.localCandidateId);
|
|
2951
|
+
if (localCandidate) {
|
|
2952
|
+
candidateType = localCandidate.candidateType || "host";
|
|
2953
|
+
protocol = localCandidate.protocol || "UDP";
|
|
2954
|
+
if (candidateType === "host") {
|
|
2955
|
+
route = "direct / LAN";
|
|
2956
|
+
} else if (candidateType === "srflx" || candidateType === "prflx") {
|
|
2957
|
+
route = "STUN / WAN";
|
|
2958
|
+
} else if (candidateType === "relay") {
|
|
2959
|
+
route = "relay / TURN";
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
if (report.currentRoundTripTime) {
|
|
2963
|
+
this.rttMs = Math.round(report.currentRoundTripTime * 1e3);
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
});
|
|
2967
|
+
this.diagnostics = {
|
|
2968
|
+
...this.diagnostics,
|
|
2969
|
+
candidateType,
|
|
2970
|
+
protocol,
|
|
2971
|
+
route
|
|
2972
|
+
};
|
|
2973
|
+
this.updateDiagnostics();
|
|
2974
|
+
} catch {
|
|
2975
|
+
}
|
|
2976
|
+
}
|
|
2977
|
+
setStatus(status) {
|
|
2978
|
+
this.status = status;
|
|
2979
|
+
this.updateDiagnostics();
|
|
2980
|
+
for (const listener of this.statusListeners) {
|
|
2981
|
+
listener(status);
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
updateDiagnostics() {
|
|
2985
|
+
if (this.pc) {
|
|
2986
|
+
this.diagnostics.connectionState = this.pc.connectionState;
|
|
2987
|
+
this.diagnostics.iceConnectionState = this.pc.iceConnectionState;
|
|
2988
|
+
this.diagnostics.iceGatheringState = this.pc.iceGatheringState;
|
|
2989
|
+
this.diagnostics.signalingState = this.pc.signalingState;
|
|
2990
|
+
}
|
|
2991
|
+
const fullDiag = this.getDiagnostics();
|
|
2992
|
+
for (const listener of this.diagnosticsListeners) {
|
|
2993
|
+
listener(fullDiag);
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
}
|
|
2997
|
+
|
|
2998
|
+
class PairingHelper {
|
|
2999
|
+
/**
|
|
3000
|
+
* Creates a new pairing session with given TTL (default 5 minutes).
|
|
3001
|
+
*/
|
|
3002
|
+
static createSession(ttlMs = 5 * 60 * 1e3) {
|
|
3003
|
+
const now = Date.now();
|
|
3004
|
+
return {
|
|
3005
|
+
id: this.generateSessionId(),
|
|
3006
|
+
createdAt: now,
|
|
3007
|
+
expiresAt: now + ttlMs,
|
|
3008
|
+
status: "waiting"
|
|
3009
|
+
};
|
|
3010
|
+
}
|
|
3011
|
+
/**
|
|
3012
|
+
* Generates a short, human-readable session ID (e.g. "K7P4-M2" or "7HQ2-9X").
|
|
3013
|
+
*/
|
|
3014
|
+
static generateSessionId() {
|
|
3015
|
+
const chars = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
3016
|
+
const bytes = this.getRandomBytes(4);
|
|
3017
|
+
let id = "";
|
|
3018
|
+
for (let i = 0; i < 4; i++) {
|
|
3019
|
+
id += chars[bytes[i] % chars.length];
|
|
3020
|
+
}
|
|
3021
|
+
const suffix = this.getRandomBytes(2);
|
|
3022
|
+
id += "-" + chars[suffix[0] % chars.length] + chars[suffix[1] % chars.length];
|
|
3023
|
+
return id;
|
|
3024
|
+
}
|
|
3025
|
+
/**
|
|
3026
|
+
* Generates a cryptographically strong 128-bit random secret string.
|
|
3027
|
+
*/
|
|
3028
|
+
static generateSecret() {
|
|
3029
|
+
const bytes = this.getRandomBytes(16);
|
|
3030
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
3031
|
+
}
|
|
3032
|
+
/**
|
|
3033
|
+
* Formats a web pairing URL where the secret is stored strictly in the URL fragment (#SECRET).
|
|
3034
|
+
*/
|
|
3035
|
+
static formatPairingUrl(baseUrl, sessionId, secret) {
|
|
3036
|
+
const cleanBase = baseUrl.replace(/\/+$/, "");
|
|
3037
|
+
return `${cleanBase}/pair?s=${encodeURIComponent(sessionId)}#${secret}`;
|
|
3038
|
+
}
|
|
3039
|
+
/**
|
|
3040
|
+
* Formats a Capacitor mobile deep link URL.
|
|
3041
|
+
*/
|
|
3042
|
+
static formatDeepLinkUrl(sessionId, secret, signalingUrl) {
|
|
3043
|
+
let url = `capdebug://pair?session=${encodeURIComponent(sessionId)}&secret=${encodeURIComponent(secret)}`;
|
|
3044
|
+
if (signalingUrl) {
|
|
3045
|
+
url += `&server=${encodeURIComponent(signalingUrl)}`;
|
|
3046
|
+
}
|
|
3047
|
+
return url;
|
|
3048
|
+
}
|
|
3049
|
+
/**
|
|
3050
|
+
* Parses a web pairing URL or Capacitor deep-link URL into session and secret components.
|
|
3051
|
+
*/
|
|
3052
|
+
static parseUrl(urlStr) {
|
|
3053
|
+
if (!urlStr || typeof urlStr !== "string") return null;
|
|
3054
|
+
try {
|
|
3055
|
+
if (urlStr.startsWith("capdebug://")) {
|
|
3056
|
+
const queryIdx = urlStr.indexOf("?");
|
|
3057
|
+
if (queryIdx === -1) return null;
|
|
3058
|
+
const search = urlStr.substring(queryIdx + 1);
|
|
3059
|
+
const params = new URLSearchParams(search);
|
|
3060
|
+
const sessionId2 = params.get("session") || params.get("s");
|
|
3061
|
+
const secret2 = params.get("secret") || params.get("sec") || "";
|
|
3062
|
+
const signalingUrl2 = params.get("server") || params.get("sig") || void 0;
|
|
3063
|
+
if (sessionId2 && secret2) {
|
|
3064
|
+
return { sessionId: sessionId2, secret: secret2, signalingUrl: signalingUrl2, rawUrl: urlStr };
|
|
3065
|
+
}
|
|
3066
|
+
return null;
|
|
3067
|
+
}
|
|
3068
|
+
const url = new URL(urlStr);
|
|
3069
|
+
let sessionId = url.searchParams.get("s") || url.searchParams.get("session");
|
|
3070
|
+
if (!sessionId && url.pathname.includes("/s/")) {
|
|
3071
|
+
const parts = url.pathname.split("/s/");
|
|
3072
|
+
if (parts[1]) {
|
|
3073
|
+
sessionId = decodeURIComponent(parts[1].split("/")[0]);
|
|
3074
|
+
}
|
|
3075
|
+
}
|
|
3076
|
+
let secret = url.hash.replace(/^#/, "");
|
|
3077
|
+
if (!secret) {
|
|
3078
|
+
secret = url.searchParams.get("secret") || url.searchParams.get("sec") || "";
|
|
3079
|
+
}
|
|
3080
|
+
const signalingUrl = url.searchParams.get("server") || `${url.protocol}//${url.host}`;
|
|
3081
|
+
if (sessionId && secret) {
|
|
3082
|
+
return {
|
|
3083
|
+
sessionId: sessionId.trim(),
|
|
3084
|
+
secret: secret.trim(),
|
|
3085
|
+
signalingUrl,
|
|
3086
|
+
rawUrl: urlStr
|
|
3087
|
+
};
|
|
3088
|
+
}
|
|
3089
|
+
return null;
|
|
3090
|
+
} catch {
|
|
3091
|
+
return null;
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3094
|
+
static getRandomBytes(length) {
|
|
3095
|
+
const bytes = new Uint8Array(length);
|
|
3096
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
3097
|
+
crypto.getRandomValues(bytes);
|
|
3098
|
+
} else {
|
|
3099
|
+
for (let i = 0; i < length; i++) {
|
|
3100
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
return bytes;
|
|
3104
|
+
}
|
|
3105
|
+
}
|
|
3106
|
+
|
|
3107
|
+
class RemoteManager {
|
|
3108
|
+
transport;
|
|
3109
|
+
store;
|
|
3110
|
+
isListening = false;
|
|
3111
|
+
eventListener = null;
|
|
3112
|
+
constructor(store) {
|
|
3113
|
+
this.store = store;
|
|
3114
|
+
this.transport = new WebRTCTransport();
|
|
3115
|
+
this.setupControlHandler();
|
|
3116
|
+
}
|
|
3117
|
+
get activeTransport() {
|
|
3118
|
+
return this.transport;
|
|
3119
|
+
}
|
|
3120
|
+
async connect(config) {
|
|
3121
|
+
this.startEventListener();
|
|
3122
|
+
await this.transport.connect(config);
|
|
3123
|
+
}
|
|
3124
|
+
disconnect() {
|
|
3125
|
+
this.transport.disconnect();
|
|
3126
|
+
this.stopEventListener();
|
|
3127
|
+
}
|
|
3128
|
+
status() {
|
|
3129
|
+
return this.transport.getStatus();
|
|
3130
|
+
}
|
|
3131
|
+
getDiagnostics() {
|
|
3132
|
+
return this.transport.getDiagnostics();
|
|
3133
|
+
}
|
|
3134
|
+
async handlePairingUrl(url) {
|
|
3135
|
+
const parsed = PairingHelper.parseUrl(url);
|
|
3136
|
+
if (!parsed) {
|
|
3137
|
+
console.warn("[CapDebug Remote] Invalid pairing URL:", url);
|
|
3138
|
+
return false;
|
|
3139
|
+
}
|
|
3140
|
+
try {
|
|
3141
|
+
await this.connect({
|
|
3142
|
+
sessionId: parsed.sessionId,
|
|
3143
|
+
secret: parsed.secret,
|
|
3144
|
+
signalingUrl: parsed.signalingUrl,
|
|
3145
|
+
isHost: false
|
|
3146
|
+
});
|
|
3147
|
+
return true;
|
|
3148
|
+
} catch (err) {
|
|
3149
|
+
console.error("[CapDebug Remote] Failed to connect via pairing URL:", err);
|
|
3150
|
+
return false;
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
startEventListener() {
|
|
3154
|
+
if (this.isListening || typeof window === "undefined") return;
|
|
3155
|
+
this.eventListener = (event) => {
|
|
3156
|
+
const customEvent = event;
|
|
3157
|
+
if (customEvent.detail) {
|
|
3158
|
+
this.transport.send(customEvent.detail);
|
|
3159
|
+
}
|
|
3160
|
+
};
|
|
3161
|
+
window.addEventListener("capdebug", this.eventListener);
|
|
3162
|
+
this.isListening = true;
|
|
3163
|
+
}
|
|
3164
|
+
stopEventListener() {
|
|
3165
|
+
if (!this.isListening || typeof window === "undefined") return;
|
|
3166
|
+
if (this.eventListener) {
|
|
3167
|
+
window.removeEventListener("capdebug", this.eventListener);
|
|
3168
|
+
this.eventListener = null;
|
|
3169
|
+
}
|
|
3170
|
+
this.isListening = false;
|
|
3171
|
+
}
|
|
3172
|
+
setupControlHandler() {
|
|
3173
|
+
this.transport.onControl(async (cmd) => {
|
|
3174
|
+
switch (cmd.type) {
|
|
3175
|
+
case "pause":
|
|
3176
|
+
this.store.pause();
|
|
3177
|
+
break;
|
|
3178
|
+
case "resume":
|
|
3179
|
+
this.store.resume();
|
|
3180
|
+
break;
|
|
3181
|
+
case "clear":
|
|
3182
|
+
this.store.clear();
|
|
3183
|
+
break;
|
|
3184
|
+
case "snapshot.request":
|
|
3185
|
+
try {
|
|
3186
|
+
const [device, memory, webView, app, network] = await Promise.allSettled([
|
|
3187
|
+
CapDebugNative.getDeviceInfo(),
|
|
3188
|
+
CapDebugNative.getMemoryInfo(),
|
|
3189
|
+
CapDebugNative.getWebViewInfo(),
|
|
3190
|
+
CapDebugNative.getAppInfo(),
|
|
3191
|
+
CapDebugNative.getNetworkInfo()
|
|
3192
|
+
]);
|
|
3193
|
+
const snapshot = {
|
|
3194
|
+
device: device.status === "fulfilled" ? device.value : null,
|
|
3195
|
+
memory: memory.status === "fulfilled" ? memory.value : null,
|
|
3196
|
+
webView: webView.status === "fulfilled" ? webView.value : null,
|
|
3197
|
+
app: app.status === "fulfilled" ? app.value : null,
|
|
3198
|
+
network: network.status === "fulfilled" ? network.value : null
|
|
3199
|
+
};
|
|
3200
|
+
this.transport.send({
|
|
3201
|
+
id: "diag_" + Date.now(),
|
|
3202
|
+
timestamp: Date.now(),
|
|
3203
|
+
source: "native",
|
|
3204
|
+
type: "device.snapshot",
|
|
3205
|
+
level: "info",
|
|
3206
|
+
message: "Live native diagnostics snapshot",
|
|
3207
|
+
data: snapshot
|
|
3208
|
+
});
|
|
3209
|
+
} catch (err) {
|
|
3210
|
+
console.error("[CapDebug Remote] Failed snapshot:", err);
|
|
3211
|
+
}
|
|
3212
|
+
break;
|
|
3213
|
+
case "disconnect":
|
|
3214
|
+
this.disconnect();
|
|
3215
|
+
break;
|
|
3216
|
+
}
|
|
3217
|
+
});
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3221
|
+
class DeepLinkHandler {
|
|
3222
|
+
static parse(url) {
|
|
3223
|
+
return PairingHelper.parseUrl(url);
|
|
3224
|
+
}
|
|
3225
|
+
}
|
|
3226
|
+
|
|
2377
3227
|
class CapDebugManager {
|
|
2378
3228
|
store;
|
|
2379
3229
|
viewerElement = null;
|
|
2380
3230
|
isMounted = false;
|
|
3231
|
+
remoteManager;
|
|
2381
3232
|
customEventCollector;
|
|
2382
3233
|
consoleCollector;
|
|
2383
3234
|
errorCollector;
|
|
@@ -2386,6 +3237,7 @@ var capacitorCapDebug = (function (exports, core) {
|
|
|
2386
3237
|
bridgeCollector;
|
|
2387
3238
|
constructor() {
|
|
2388
3239
|
this.store = new CapDebugStore();
|
|
3240
|
+
this.remoteManager = new RemoteManager(this.store);
|
|
2389
3241
|
this.customEventCollector = new CustomEventCollector(this.store);
|
|
2390
3242
|
this.consoleCollector = new ConsoleCollector();
|
|
2391
3243
|
this.errorCollector = new ErrorCollector();
|
|
@@ -2393,6 +3245,12 @@ var capacitorCapDebug = (function (exports, core) {
|
|
|
2393
3245
|
this.browserEventCollector = new BrowserEventCollector();
|
|
2394
3246
|
this.bridgeCollector = new BridgeCollector();
|
|
2395
3247
|
}
|
|
3248
|
+
get remote() {
|
|
3249
|
+
return this.remoteManager;
|
|
3250
|
+
}
|
|
3251
|
+
async handlePairingUrl(url) {
|
|
3252
|
+
return this.remoteManager.handlePairingUrl(url);
|
|
3253
|
+
}
|
|
2396
3254
|
get native() {
|
|
2397
3255
|
return CapDebugNative;
|
|
2398
3256
|
}
|
|
@@ -2634,9 +3492,17 @@ var capacitorCapDebug = (function (exports, core) {
|
|
|
2634
3492
|
exports.ConsoleCollector = ConsoleCollector;
|
|
2635
3493
|
exports.CustomEventCollector = CustomEventCollector;
|
|
2636
3494
|
exports.DataSanitizer = DataSanitizer;
|
|
3495
|
+
exports.DeepLinkHandler = DeepLinkHandler;
|
|
2637
3496
|
exports.ErrorCollector = ErrorCollector;
|
|
3497
|
+
exports.HttpSseSignalingClient = HttpSseSignalingClient;
|
|
2638
3498
|
exports.JsonViewer = JsonViewer;
|
|
2639
3499
|
exports.NetworkCollector = NetworkCollector;
|
|
3500
|
+
exports.PROTOCOL_VERSION = PROTOCOL_VERSION;
|
|
3501
|
+
exports.PairingHelper = PairingHelper;
|
|
3502
|
+
exports.RemoteEventBuffer = RemoteEventBuffer;
|
|
3503
|
+
exports.RemoteManager = RemoteManager;
|
|
3504
|
+
exports.RemoteProtocol = RemoteProtocol;
|
|
3505
|
+
exports.WebRTCTransport = WebRTCTransport;
|
|
2640
3506
|
exports.default = CapDebug;
|
|
2641
3507
|
exports.defaultSanitizer = defaultSanitizer;
|
|
2642
3508
|
exports.registerCustomElement = registerCustomElement;
|