@basmilius/apple-companion-link 0.6.6 → 0.7.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/dist/frame.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  export declare const FrameType: {
2
2
  readonly Unknown: 0;
3
- readonly Noop: 1;
4
- readonly PS_Start: 3;
5
- readonly PS_Next: 4;
6
- readonly PV_Start: 5;
7
- readonly PV_Next: 6;
8
- readonly U_OPACK: 7;
9
- readonly E_OPACK: 8;
10
- readonly P_OPACK: 9;
11
- readonly PA_Request: 10;
12
- readonly PA_Response: 11;
3
+ readonly NoOp: 1;
4
+ readonly PairSetupStart: 3;
5
+ readonly PairSetupNext: 4;
6
+ readonly PairVerifyStart: 5;
7
+ readonly PairVerifyNext: 6;
8
+ readonly OPackUnencrypted: 7;
9
+ readonly OPackEncrypted: 8;
10
+ readonly OPackPacked: 9;
11
+ readonly PairingRequest: 10;
12
+ readonly PairingResponse: 11;
13
13
  readonly SessionStartRequest: 16;
14
14
  readonly SessionStartResponse: 17;
15
15
  readonly SessionData: 18;
@@ -23,4 +23,4 @@ export declare const MessageType: {
23
23
  readonly Response: 3;
24
24
  };
25
25
  export declare const OPackFrameTypes: number[];
26
- export declare const PairFrameTypes: number[];
26
+ export declare const PairingFrameTypes: number[];
package/dist/index.js CHANGED
@@ -44,16 +44,16 @@ var MediaControlCommand = {
44
44
  // src/frame.ts
45
45
  var FrameType = {
46
46
  Unknown: 0,
47
- Noop: 1,
48
- PS_Start: 3,
49
- PS_Next: 4,
50
- PV_Start: 5,
51
- PV_Next: 6,
52
- U_OPACK: 7,
53
- E_OPACK: 8,
54
- P_OPACK: 9,
55
- PA_Request: 10,
56
- PA_Response: 11,
47
+ NoOp: 1,
48
+ PairSetupStart: 3,
49
+ PairSetupNext: 4,
50
+ PairVerifyStart: 5,
51
+ PairVerifyNext: 6,
52
+ OPackUnencrypted: 7,
53
+ OPackEncrypted: 8,
54
+ OPackPacked: 9,
55
+ PairingRequest: 10,
56
+ PairingResponse: 11,
57
57
  SessionStartRequest: 16,
58
58
  SessionStartResponse: 17,
59
59
  SessionData: 18,
@@ -67,19 +67,19 @@ var MessageType = {
67
67
  Response: 3
68
68
  };
69
69
  var OPackFrameTypes = [
70
- FrameType.PS_Start,
71
- FrameType.PS_Next,
72
- FrameType.PV_Start,
73
- FrameType.PV_Next,
74
- FrameType.U_OPACK,
75
- FrameType.E_OPACK,
76
- FrameType.P_OPACK
70
+ FrameType.PairSetupStart,
71
+ FrameType.PairSetupNext,
72
+ FrameType.PairVerifyStart,
73
+ FrameType.PairVerifyNext,
74
+ FrameType.OPackUnencrypted,
75
+ FrameType.OPackEncrypted,
76
+ FrameType.OPackPacked
77
77
  ];
78
- var PairFrameTypes = [
79
- FrameType.PS_Start,
80
- FrameType.PS_Next,
81
- FrameType.PV_Start,
82
- FrameType.PV_Next
78
+ var PairingFrameTypes = [
79
+ FrameType.PairSetupStart,
80
+ FrameType.PairSetupNext,
81
+ FrameType.PairVerifyStart,
82
+ FrameType.PairVerifyNext
83
83
  ];
84
84
 
85
85
  // src/pairing.ts
@@ -108,7 +108,7 @@ class Pairing {
108
108
  return this.#internal.transient();
109
109
  }
110
110
  async#request(step, data) {
111
- const frameType = step === "m1" ? FrameType.PS_Start : FrameType.PS_Next;
111
+ const frameType = step === "m1" ? FrameType.PairSetupStart : FrameType.PairSetupNext;
112
112
  const [, response] = await this.#stream.exchange(frameType, {
113
113
  _pd: data,
114
114
  _pwTy: 1
@@ -157,7 +157,7 @@ class Verify {
157
157
  };
158
158
  }
159
159
  async#request(step, data) {
160
- const frameType = step === "m1" ? FrameType.PV_Start : FrameType.PV_Next;
160
+ const frameType = step === "m1" ? FrameType.PairVerifyStart : FrameType.PairVerifyNext;
161
161
  const [, response] = await this.#stream.exchange(frameType, {
162
162
  _pd: data,
163
163
  _auTy: 4
@@ -202,6 +202,7 @@ class Stream extends EncryptionAwareConnection {
202
202
  #xid;
203
203
  constructor(context, address, port) {
204
204
  super(context, address, port);
205
+ this.debug(true);
205
206
  this.#xid = randomInt(0, 2 ** 16);
206
207
  this.on("close", this.#onClose.bind(this));
207
208
  this.on("data", this.#onData.bind(this));
@@ -214,18 +215,15 @@ class Stream extends EncryptionAwareConnection {
214
215
  async exchange(type, obj) {
215
216
  const _x = this.#xid;
216
217
  return new Promise((resolve, reject) => {
217
- if (PairFrameTypes.includes(type)) {
218
+ if (PairingFrameTypes.includes(type)) {
218
219
  this.#queue.set(PAIRING_QUEUE_IDENTIFIER, [resolve, reject]);
219
220
  } else {
220
221
  this.#queue.set(_x, [resolve, reject]);
221
222
  }
222
- this.send(type, obj).catch(reject);
223
+ this.sendOPack(type, obj);
223
224
  });
224
225
  }
225
- async send(type, obj) {
226
- const _x = this.#xid++;
227
- obj._x ??= OPack.sizedInteger(_x, 8);
228
- let payload = Buffer.from(OPack.encode(obj));
226
+ send(type, payload) {
229
227
  let payloadLength = payload.byteLength;
230
228
  if (this.isEncrypted && payloadLength > 0) {
231
229
  payloadLength += Chacha20.CHACHA20_AUTH_TAG_LENGTH;
@@ -242,13 +240,14 @@ class Stream extends EncryptionAwareConnection {
242
240
  } else {
243
241
  data = Buffer.concat([header, payload]);
244
242
  }
245
- this.context.logger.raw("[companion-link]", "Sending data frame", this.isEncrypted, Buffer.from(data).toString("hex"), obj);
246
- try {
247
- return await this.write(data);
248
- } catch (err) {
249
- this.context.logger.error("[companion-link]", "Error in Companion Link send()", err);
250
- this.emit("error", err);
251
- }
243
+ this.context.logger.raw("[companion-link]", "Sending data frame", this.isEncrypted, type);
244
+ this.write(data);
245
+ }
246
+ sendOPack(type, obj) {
247
+ const _x = this.#xid++;
248
+ obj._x ??= OPack.sizedInteger(_x, 8);
249
+ this.context.logger.raw("[companion-link]", "Sending opack frame", type, this.isEncrypted, obj);
250
+ this.send(type, Buffer.from(OPack.encode(obj)));
252
251
  }
253
252
  #cleanup() {
254
253
  this.#buffer = Buffer.alloc(0);
@@ -381,28 +380,28 @@ class Protocol {
381
380
  await this.#stream.disconnect();
382
381
  }
383
382
  async fetchMediaControlStatus() {
384
- await this.#stream.exchange(FrameType.E_OPACK, {
383
+ await this.#stream.exchange(FrameType.OPackEncrypted, {
385
384
  _i: "FetchMediaControlStatus",
386
385
  _t: MessageType.Request,
387
386
  _c: {}
388
387
  });
389
388
  }
390
389
  async fetchNowPlayingInfo() {
391
- await this.#stream.exchange(FrameType.E_OPACK, {
390
+ await this.#stream.exchange(FrameType.OPackEncrypted, {
392
391
  _i: "FetchCurrentNowPlayingInfoEvent",
393
392
  _t: MessageType.Request,
394
393
  _c: {}
395
394
  });
396
395
  }
397
396
  async fetchSupportedActions() {
398
- await this.#stream.exchange(FrameType.E_OPACK, {
397
+ await this.#stream.exchange(FrameType.OPackEncrypted, {
399
398
  _i: "FetchSupportedActionsEvent",
400
399
  _t: MessageType.Request,
401
400
  _c: {}
402
401
  });
403
402
  }
404
403
  async getAttentionState() {
405
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
404
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
406
405
  _i: "FetchAttentionState",
407
406
  _t: MessageType.Request,
408
407
  _c: {}
@@ -411,7 +410,7 @@ class Protocol {
411
410
  return convertAttentionState(_c.state);
412
411
  }
413
412
  async getLaunchableApps() {
414
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
413
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
415
414
  _i: "FetchLaunchableApplicationsEvent",
416
415
  _t: MessageType.Request,
417
416
  _c: {}
@@ -423,7 +422,7 @@ class Protocol {
423
422
  }));
424
423
  }
425
424
  async getSiriRemoteInfo() {
426
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
425
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
427
426
  _i: "FetchSiriRemoteInfo",
428
427
  _t: MessageType.Request,
429
428
  _c: {}
@@ -431,7 +430,7 @@ class Protocol {
431
430
  return Plist.parse(Buffer.from(payload["_c"]["SiriRemoteInfoKey"]).buffer);
432
431
  }
433
432
  async getUserAccounts() {
434
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
433
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
435
434
  _i: "FetchUserAccountsEvent",
436
435
  _t: MessageType.Request,
437
436
  _c: {}
@@ -443,7 +442,7 @@ class Protocol {
443
442
  }));
444
443
  }
445
444
  async hidCommand(command, down = false) {
446
- await this.#stream.exchange(FrameType.E_OPACK, {
445
+ await this.#stream.exchange(FrameType.OPackEncrypted, {
447
446
  _i: "_hidC",
448
447
  _t: MessageType.Request,
449
448
  _c: {
@@ -453,7 +452,7 @@ class Protocol {
453
452
  });
454
453
  }
455
454
  async launchApp(bundleId) {
456
- await this.#stream.exchange(FrameType.E_OPACK, {
455
+ await this.#stream.exchange(FrameType.OPackEncrypted, {
457
456
  _i: "_launchApp",
458
457
  _t: MessageType.Request,
459
458
  _c: {
@@ -462,7 +461,7 @@ class Protocol {
462
461
  });
463
462
  }
464
463
  async launchUrl(url) {
465
- await this.#stream.exchange(FrameType.E_OPACK, {
464
+ await this.#stream.exchange(FrameType.OPackEncrypted, {
466
465
  _i: "_launchApp",
467
466
  _t: MessageType.Request,
468
467
  _c: {
@@ -471,7 +470,7 @@ class Protocol {
471
470
  });
472
471
  }
473
472
  async mediaControlCommand(command, content) {
474
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
473
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
475
474
  _i: "_mcc",
476
475
  _t: MessageType.Request,
477
476
  _c: {
@@ -501,7 +500,7 @@ class Protocol {
501
500
  }
502
501
  }
503
502
  async switchUserAccount(accountId) {
504
- await this.#stream.exchange(FrameType.E_OPACK, {
503
+ await this.#stream.exchange(FrameType.OPackEncrypted, {
505
504
  _i: "SwitchUserAccountEvent",
506
505
  _t: MessageType.Request,
507
506
  _c: {
@@ -509,9 +508,9 @@ class Protocol {
509
508
  }
510
509
  });
511
510
  }
512
- async _subscribe(event, fn) {
511
+ async subscribe(event, fn) {
513
512
  this.#stream.on(event, fn);
514
- await this.#stream.send(FrameType.E_OPACK, {
513
+ this.#stream.sendOPack(FrameType.OPackEncrypted, {
515
514
  _i: "_interest",
516
515
  _t: MessageType.Event,
517
516
  _c: {
@@ -519,14 +518,14 @@ class Protocol {
519
518
  }
520
519
  });
521
520
  }
522
- async _unsubscribe(event, fn) {
521
+ async unsubscribe(event, fn) {
523
522
  if (!this.#stream.isConnected) {
524
523
  return;
525
524
  }
526
525
  if (fn) {
527
526
  this.#stream.off(event, fn);
528
527
  }
529
- await this.#stream.send(FrameType.E_OPACK, {
528
+ this.#stream.sendOPack(FrameType.OPackEncrypted, {
530
529
  _i: "_interest",
531
530
  _t: MessageType.Event,
532
531
  _c: {
@@ -534,8 +533,8 @@ class Protocol {
534
533
  }
535
534
  });
536
535
  }
537
- async _sessionStart() {
538
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
536
+ async sessionStart() {
537
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
539
538
  _i: "_sessionStart",
540
539
  _t: MessageType.Request,
541
540
  _btHP: false,
@@ -547,8 +546,8 @@ class Protocol {
547
546
  });
548
547
  return objectOrFail(payload);
549
548
  }
550
- async _systemInfo(pairingId) {
551
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
549
+ async systemInfo(pairingId) {
550
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
552
551
  _i: "_systemInfo",
553
552
  _t: MessageType.Request,
554
553
  _btHP: false,
@@ -597,8 +596,8 @@ class Protocol {
597
596
  });
598
597
  return objectOrFail(payload);
599
598
  }
600
- async _tiStart() {
601
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
599
+ async tiStart() {
600
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
602
601
  _i: "_tiStart",
603
602
  _t: MessageType.Request,
604
603
  _btHP: false,
@@ -606,8 +605,8 @@ class Protocol {
606
605
  });
607
606
  return objectOrFail(payload);
608
607
  }
609
- async _touchStart() {
610
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
608
+ async touchStart() {
609
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
611
610
  _i: "_touchStart",
612
611
  _t: MessageType.Request,
613
612
  _btHP: false,
@@ -619,8 +618,8 @@ class Protocol {
619
618
  });
620
619
  return objectOrFail(payload);
621
620
  }
622
- async _tvrcSessionStart() {
623
- const [, payload] = await this.#stream.exchange(FrameType.E_OPACK, {
621
+ async tvrcSessionStart() {
622
+ const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
624
623
  _i: "TVRCSessionStart",
625
624
  _t: MessageType.Request,
626
625
  _btHP: false,
@@ -629,6 +628,10 @@ class Protocol {
629
628
  });
630
629
  return objectOrFail(payload);
631
630
  }
631
+ async noOp() {
632
+ this.#context.logger.debug("Sending no-op operation.");
633
+ await this.#stream.send(FrameType.NoOp, Buffer.allocUnsafe(0));
634
+ }
632
635
  }
633
636
  function objectOrFail(obj) {
634
637
  if (obj !== null && typeof obj === "object") {
@@ -27,11 +27,12 @@ export default class Protocol {
27
27
  mediaControlCommand(command: MediaControlCommandKey, content?: object): Promise<object>;
28
28
  pressButton(command: HidCommandKey, type?: ButtonPressType, holdDelayMs?: number): Promise<void>;
29
29
  switchUserAccount(accountId: string): Promise<void>;
30
- _subscribe(event: string, fn: (data: unknown) => void): Promise<void>;
31
- _unsubscribe(event: string, fn?: (data: unknown) => void): Promise<void>;
32
- _sessionStart(): Promise<object>;
33
- _systemInfo(pairingId: Buffer): Promise<object>;
34
- _tiStart(): Promise<object>;
35
- _touchStart(): Promise<object>;
36
- _tvrcSessionStart(): Promise<object>;
30
+ subscribe(event: string, fn: (data: unknown) => void): Promise<void>;
31
+ unsubscribe(event: string, fn?: (data: unknown) => void): Promise<void>;
32
+ sessionStart(): Promise<object>;
33
+ systemInfo(pairingId: Buffer): Promise<object>;
34
+ tiStart(): Promise<object>;
35
+ touchStart(): Promise<object>;
36
+ tvrcSessionStart(): Promise<object>;
37
+ noOp(): Promise<void>;
37
38
  }
package/dist/stream.d.ts CHANGED
@@ -4,5 +4,6 @@ export default class Stream extends EncryptionAwareConnection<Record<string, [un
4
4
  constructor(context: Context, address: string, port: number);
5
5
  disconnect(): Promise<void>;
6
6
  exchange(type: number, obj: Record<string, unknown>): Promise<[number, unknown]>;
7
- send(type: number, obj: Record<string, unknown>): Promise<void>;
7
+ send(type: number, payload: Buffer): void;
8
+ sendOPack(type: number, obj: Record<string, unknown>): void;
8
9
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@basmilius/apple-companion-link",
3
3
  "description": "Implementation of Apple's Companion Link in Node.js.",
4
- "version": "0.6.6",
4
+ "version": "0.7.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {
@@ -22,7 +22,7 @@
22
22
  ],
23
23
  "publishConfig": {
24
24
  "access": "public",
25
- "provenance": true
25
+ "provenance": false
26
26
  },
27
27
  "scripts": {
28
28
  "build": "tsgo && bun -b build.ts",
@@ -40,12 +40,12 @@
40
40
  }
41
41
  },
42
42
  "dependencies": {
43
- "@basmilius/apple-common": "0.6.6",
44
- "@basmilius/apple-encoding": "0.6.6",
45
- "@basmilius/apple-encryption": "0.6.6"
43
+ "@basmilius/apple-common": "0.7.0",
44
+ "@basmilius/apple-encoding": "0.7.0",
45
+ "@basmilius/apple-encryption": "0.7.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@basmilius/tools": "^2.24.0",
49
- "@types/bun": "^1.3.8"
48
+ "@basmilius/tools": "^2.25.0",
49
+ "@types/bun": "^1.3.9"
50
50
  }
51
51
  }