-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmusickit-bridge.js
More file actions
562 lines (495 loc) · 17.6 KB
/
musickit-bridge.js
File metadata and controls
562 lines (495 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/**
* MusicKit Bridge - Electron side
*
* Communicates with the native Swift MusicKit helper via stdin/stdout JSON
*/
const { spawn, execFileSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const EventEmitter = require('events');
class MusicKitBridge extends EventEmitter {
constructor() {
super();
this.process = null;
this.pendingRequests = new Map();
this.requestId = 0;
this.isReady = false;
this.buffer = '';
// Authorization caching
this._authStatus = null; // { authorized: boolean, status: string }
this._authCheckedAt = 0; // Timestamp of last check
this._authCacheTTL = 60000; // Cache valid for 60 seconds
// Last error from helper (captured from id:"error" responses before crash)
this._lastHelperError = null;
}
/**
* Get cached authorization status (synchronous)
* Returns null if not cached or cache expired
*/
getCachedAuthStatus() {
if (!this._authStatus) return null;
if (Date.now() - this._authCheckedAt > this._authCacheTTL) return null;
return this._authStatus;
}
/**
* Check if cached auth shows authorized
*/
isCachedAuthorized() {
const cached = this.getCachedAuthStatus();
return cached?.authorized === true;
}
/**
* Invalidate auth cache (call after failures)
*/
invalidateAuthCache() {
this._authStatus = null;
this._authCheckedAt = 0;
}
/**
* Clear auth state: invalidate cache and stop the helper process.
* Cannot programmatically revoke macOS system keychain auth, but this
* ensures the app won't treat the user as connected on next launch.
*/
unauthorize() {
this.invalidateAuthCache();
this.stop();
}
/**
* Reject all pending requests (e.g. when the helper crashes or is stopped)
*/
rejectAllPending(reason) {
if (this.pendingRequests.size === 0) return;
console.log(`[MusicKit] Rejecting ${this.pendingRequests.size} pending request(s): ${reason}`);
for (const [id, pending] of this.pendingRequests) {
pending.reject(new Error(reason));
}
this.pendingRequests.clear();
}
/**
* Get the path to the MusicKit helper executable (inside .app bundle)
*/
getHelperPath() {
// Look for .app bundle first (required for MusicKit entitlements)
// The executable is inside: MusicKitHelper.app/Contents/MacOS/MusicKitHelper
const appBundlePaths = [
// Development: use Swift build output .app bundle
path.join(__dirname, 'native', 'musickit-helper', '.build', 'release', 'MusicKitHelper.app'),
// Resources directory
path.join(__dirname, 'resources', 'bin', 'darwin', 'MusicKitHelper.app'),
// Production path (inside Electron app bundle)
path.join(process.resourcesPath || __dirname, 'bin', 'darwin', 'MusicKitHelper.app'),
];
for (const appPath of appBundlePaths) {
const execPath = path.join(appPath, 'Contents', 'MacOS', 'MusicKitHelper');
if (fs.existsSync(execPath)) {
console.log('[MusicKit] Found app bundle at:', appPath);
return execPath;
}
}
// Fallback to old CLI binary (won't have MusicKit playback, but might work for search)
const fallbackPaths = [
path.join(__dirname, 'native', 'musickit-helper', '.build', 'release', 'musickit-helper'),
path.join(__dirname, 'resources', 'bin', 'darwin', 'musickit-helper'),
path.join(process.resourcesPath || __dirname, 'bin', 'darwin', 'musickit-helper'),
];
for (const p of fallbackPaths) {
if (fs.existsSync(p)) {
console.log('[MusicKit] Warning: Using fallback CLI binary (no playback support):', p);
return p;
}
}
return null;
}
/**
* Check if MusicKit helper is available (macOS only)
*/
isAvailable() {
if (process.platform !== 'darwin') {
return false;
}
return this.getHelperPath() !== null;
}
/**
* Start the MusicKit helper process
*/
async start() {
if (this.process) {
console.log('[MusicKit] Already running');
return true;
}
if (process.platform !== 'darwin') {
console.log('[MusicKit] Only available on macOS');
return false;
}
const helperPath = this.getHelperPath();
if (!helperPath) {
console.log('[MusicKit] Helper binary not found');
console.log('[MusicKit] Build it with: cd native/musickit-helper && ./build.sh');
return false;
}
console.log('[MusicKit] Starting helper:', helperPath);
console.log('[MusicKit] Helper exists:', fs.existsSync(helperPath));
console.log('[MusicKit] Helper stats:', fs.statSync(helperPath).mode.toString(8));
// Remove macOS quarantine xattr from the helper app bundle.
// When the user downloads the DMG, macOS sets com.apple.quarantine on
// everything inside. The main Parachord.app passes Gatekeeper (signed +
// notarized), but the quarantine xattr propagates to nested binaries.
// Removing it from the bundled helper is safe — the parent app was already
// verified by Gatekeeper.
const appBundlePath = path.dirname(path.dirname(path.dirname(helperPath))); // up from Contents/MacOS/MusicKitHelper
try {
execFileSync('/usr/bin/xattr', ['-dr', 'com.apple.quarantine', appBundlePath], { timeout: 5000 });
console.log('[MusicKit] Cleared quarantine xattr from:', appBundlePath);
} catch (e) {
// Ignore — xattr may not exist (no quarantine) or we lack permission
console.log('[MusicKit] Could not clear quarantine (may not be set):', e.message);
}
return new Promise((resolve) => {
try {
this.process = spawn(helperPath, [], {
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env }
});
console.log('[MusicKit] Process spawned, PID:', this.process.pid);
} catch (spawnError) {
console.error('[MusicKit] Spawn error:', spawnError);
resolve(false);
return;
}
this.process.stdout.on('data', (data) => {
console.log('[MusicKit] stdout raw:', data.toString());
this.handleData(data);
});
this.process.stderr.on('data', (data) => {
console.error('[MusicKit] stderr:', data.toString());
});
this.process.on('error', (error) => {
console.error('[MusicKit] Process error:', error);
this.process = null;
this.isReady = false;
resolve(false);
});
this.process.on('close', (code, signal) => {
console.log('[MusicKit] Process closed with code:', code, 'signal:', signal);
this.process = null;
this.isReady = false;
// If the helper's uncaught exception handler already sent an error
// (id:"error" response handled in handleData), pending requests are
// already rejected with the real error. Only reject stragglers here.
if (this.pendingRequests.size > 0) {
const reason = this._lastHelperError
|| `MusicKit helper exited (code: ${code}, signal: ${signal})`;
this.rejectAllPending(reason);
}
this._lastHelperError = null;
this.emit('close', code);
});
this.process.on('exit', (code, signal) => {
console.log('[MusicKit] Process exit with code:', code, 'signal:', signal);
});
// Wait for ready signal, or resolve early if the process dies
let settled = false;
const settle = (value) => {
if (settled) return;
settled = true;
clearTimeout(readyTimeout);
resolve(value);
};
const readyTimeout = setTimeout(() => {
if (!this.isReady) {
console.log('[MusicKit] Timeout waiting for ready signal');
console.log('[MusicKit] Buffer contents:', this.buffer);
console.log('[MusicKit] Process still alive:', this.process && !this.process.killed);
this.stop();
settle(false);
}
}, 10000);
this.once('ready', () => {
console.log('[MusicKit] Helper ready');
settle(true);
});
// If the process crashes during startup (before "ready"), resolve
// immediately instead of waiting for the 10-second timeout.
this.once('close', () => {
if (!this.isReady) {
console.log('[MusicKit] Helper died during startup');
settle(false);
}
});
});
}
/**
* Handle incoming data from the helper
*/
handleData(data) {
this.buffer += data.toString();
console.log('[MusicKit] handleData, buffer now:', this.buffer.substring(0, 200));
// Process complete JSON lines
const lines = this.buffer.split('\n');
this.buffer = lines.pop(); // Keep incomplete line in buffer
console.log('[MusicKit] Lines to process:', lines.length);
for (const line of lines) {
if (!line.trim()) continue;
console.log('[MusicKit] Processing line:', line.substring(0, 100));
try {
const response = JSON.parse(line);
console.log('[MusicKit] Parsed response id:', response.id);
// Check for ready signal
if (response.id === 'ready') {
console.log('[MusicKit] Got ready signal!');
this.isReady = true;
this.emit('ready', response.data);
continue;
}
// Handle error responses from the helper's uncaught exception handler.
// These have id "error" and don't match any pending request, but they
// contain the real error message (e.g. MusicKit SIGABRT reason).
// The helper is about to crash, so reject all pending requests with the
// actual error instead of the generic "MusicKit helper exited" message.
if (response.id === 'error' && !response.success) {
const errorMsg = response.error || 'MusicKit internal error';
console.error('[MusicKit] Helper reported fatal error:', errorMsg);
this._lastHelperError = errorMsg;
this.rejectAllPending(errorMsg);
continue;
}
// Resolve pending request
const pending = this.pendingRequests.get(response.id);
if (pending) {
this.pendingRequests.delete(response.id);
if (response.success) {
pending.resolve(response.data);
} else {
pending.reject(new Error(response.error || 'Unknown error'));
}
}
} catch (error) {
console.error('[MusicKit] Parse error:', error, 'Line:', line);
}
}
}
/**
* Send a command to the helper.
* If the helper crashes mid-request, waits briefly then retries once.
*/
async send(action, params = {}, timeoutMs = 30000) {
return this._sendOnce(action, params, timeoutMs).catch(async (error) => {
// Retry once if the helper died mid-request (not for timeouts or app errors)
if (error.message && (
error.message.includes('MusicKit helper exited') ||
error.message.includes('MusicKit internal error')
)) {
console.log(`[MusicKit] Retrying ${action} after helper crash`);
// Small delay before retry — gives macOS subsystems (TCC, XPC) time to settle
await new Promise(r => setTimeout(r, 1500));
return this._sendOnce(action, params, timeoutMs);
}
throw error;
});
}
async _sendOnce(action, params = {}, timeoutMs = 30000) {
if (!this.process || !this.isReady) {
// Try to start if not running
const started = await this.start();
if (!started) {
throw new Error('MusicKit helper not available');
}
}
return new Promise((resolve, reject) => {
const id = `req_${++this.requestId}`;
const request = {
id,
action,
params
};
// Store pending request
const timeout = setTimeout(() => {
this.pendingRequests.delete(id);
reject(new Error(`Request timeout: ${action}`));
}, timeoutMs);
this.pendingRequests.set(id, {
resolve: (data) => {
clearTimeout(timeout);
resolve(data);
},
reject: (error) => {
clearTimeout(timeout);
reject(error);
}
});
// Send request
const json = JSON.stringify(request) + '\n';
this.process.stdin.write(json);
});
}
/**
* Stop the helper process
*/
stop() {
if (this.process) {
console.log('[MusicKit] Stopping helper');
try {
this.process.stdin.write(JSON.stringify({ id: 'quit', action: 'quit', params: {} }) + '\n');
} catch (e) {
// Ignore write errors
}
setTimeout(() => {
if (this.process) {
this.process.kill();
this.process = null;
}
}, 500);
}
this.isReady = false;
this.rejectAllPending('MusicKit helper stopped');
}
// High-level API methods
/**
* Check authorization status (updates cache)
*/
async checkAuthStatus() {
const result = await this.send('checkAuthStatus');
// Update cache
this._authStatus = result;
this._authCheckedAt = Date.now();
this.emit('authStatusChanged', result);
return result;
}
/**
* Get authorization status, using cache if valid
* @param {boolean} forceRefresh - Force a fresh check even if cached
*/
async getAuthStatus(forceRefresh = false) {
// Return cached if valid and not forcing refresh
if (!forceRefresh) {
const cached = this.getCachedAuthStatus();
if (cached) {
console.log('[MusicKit] Using cached auth status:', cached);
return cached;
}
}
// Otherwise fetch fresh
return this.checkAuthStatus();
}
/**
* Request authorization (updates cache).
* Authorization is the most crash-prone MusicKit operation (the system
* dialog can trigger AppKit/XPC assertions in the headless helper), so
* we add an extra retry on top of send()'s built-in single retry.
*/
async authorize() {
// Use a longer timeout for authorize since it requires user interaction
// (Apple ID sign-in dialog with potential 2FA)
const attempt = async () => {
const result = await this.send('authorize', {}, 300000);
this._authStatus = result;
this._authCheckedAt = Date.now();
this.emit('authStatusChanged', result);
return result;
};
try {
return await attempt();
} catch (firstError) {
// send() already retried once. For authorize specifically, try one
// more time with a longer settle delay — the activation-policy change
// that triggers the crash is transient and often works on a second
// cold start of the helper.
if (firstError.message && (
firstError.message.includes('MusicKit helper exited') ||
firstError.message.includes('MusicKit internal error') ||
firstError.message.includes('MusicKit helper not available')
)) {
console.log('[MusicKit] Authorization crashed twice, final retry after longer delay');
await new Promise(r => setTimeout(r, 3000));
try {
return await attempt();
} catch (secondError) {
// Wrap with a user-friendly message while preserving the original
const detail = secondError.message || 'unknown error';
throw new Error(
`Apple Music authorization failed after multiple attempts. ` +
`You can enable access manually: open System Settings \u2192 ` +
`Privacy & Security \u2192 Media & Apple Music, toggle Parachord on, ` +
`then click Connect again. (${detail})`
);
}
}
throw firstError;
}
}
async fetchUserToken(developerToken) {
// User token fetch may require authorization, use longer timeout
return this.send('fetchUserToken', { developerToken }, 300000);
}
async search(query, limit = 25) {
return this.send('search', { query, limit });
}
async resolve(artist, title, album = null) {
return this.send('resolve', { artist, title, album });
}
async play(songId) {
try {
const result = await this.send('play', { songId });
return result;
} catch (error) {
// If play fails, invalidate auth cache so next call re-checks
// This handles cases where auth was revoked mid-session
console.log('[MusicKit] Play failed, invalidating auth cache:', error.message);
this.invalidateAuthCache();
throw error;
}
}
async pause() {
return this.send('pause');
}
async resume() {
return this.send('resume');
}
async stop_playback() {
return this.send('stop');
}
async skipToNext() {
return this.send('skipToNext');
}
async skipToPrevious() {
return this.send('skipToPrevious');
}
async seek(position) {
// Ensure position serializes as a JSON float, not an integer.
// Swift's AnyCodable decodes "30" as Int, and `as? Double` fails,
// so whole-number positions silently fail. Adding a tiny epsilon
// forces JSON.stringify to emit a decimal (e.g. "30.0000001").
const safePosition = Number.isInteger(position) ? position + 1e-7 : position;
return this.send('seek', { position: safePosition });
}
async getPlaybackState() {
return this.send('getPlaybackState');
}
async getNowPlaying() {
return this.send('getNowPlaying');
}
async addToQueue(songId) {
try {
return await this.send('addToQueue', { songId });
} catch (error) {
console.log('[MusicKit] addToQueue failed, invalidating auth cache:', error.message);
this.invalidateAuthCache();
throw error;
}
}
async setVolume(volume) {
return this.send('setVolume', { volume });
}
async ping() {
return this.send('ping');
}
}
// Singleton instance
let instance = null;
function getMusicKitBridge() {
if (!instance) {
instance = new MusicKitBridge();
}
return instance;
}
module.exports = { MusicKitBridge, getMusicKitBridge };