@freqhole/api-client 0.3.3 → 0.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freqhole/api-client",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,29 +12,10 @@ type InvokeFn = (cmd: string, args?: unknown) => Promise<unknown>;
12
12
 
13
13
  // tauri invoke is dynamically imported to avoid bundling in browser builds
14
14
  let invoke: InvokeFn | null = null;
15
- let convertFileSrc: ((path: string) => string) | null = null;
16
-
17
- // webkitgtk (linux) can't play asset:// URLs in <audio> elements.
18
- // detect once at module level so we can use blob: URLs as a workaround.
19
- //
20
- // historically there was a second workaround here — an embedded http
21
- // loopback server (`media_server_info` ipc + `server::media_server`)
22
- // that served blobs via plain http. it has been removed in favor of
23
- // the rodio backend (see `client/spume/src/music/services/audio/`),
24
- // which bypasses the html `<audio>` element entirely on linux. when
25
- // rodio is *not* enabled and we're on linux, we fall back to the
26
- // blob: object url path below.
27
- const isLinuxWebKit = typeof navigator !== "undefined" && navigator.userAgent.includes("Linux");
28
-
29
- /**
30
- * blobs that must never be buffered into memory as a blob: URL, even on
31
- * linux: a local video can easily be multi-GB, and `fetch().arrayBuffer()`
32
- * on one hangs the whole app. these stream from asset:// instead, which
33
- * also gives `<video>` real range requests for seeking.
34
- */
35
- function isStreamableMime(mime?: string | null): boolean {
36
- return (mime ?? "").startsWith("video/");
37
- }
15
+ let convertFileSrc: ((path: string, protocol?: string) => string) | null = null;
16
+ // cached target_os check (android only, for now) - see mediaSrcFor() below.
17
+ // null = not yet determined, false = determined non-android.
18
+ let isAndroidCached: boolean | null = null;
38
19
 
39
20
  /**
40
21
  * initialize tauri invoke function
@@ -46,12 +27,45 @@ async function ensureInvoke(): Promise<InvokeFn> {
46
27
  invoke = tauri.invoke as InvokeFn;
47
28
  // also grab convertFileSrc for blob URLs
48
29
  convertFileSrc = tauri.convertFileSrc;
30
+ try {
31
+ const buildInfo = (await invoke("get_build_info")) as { target_os?: string };
32
+ isAndroidCached = buildInfo?.target_os === "android";
33
+ } catch {
34
+ isAndroidCached = false;
35
+ }
49
36
  return invoke;
50
37
  } catch {
51
38
  throw new Error("@tauri-apps/api not available - not running in Tauri");
52
39
  }
53
40
  }
54
41
 
42
+ /**
43
+ * build a playable url for a local file path - tauri's built-in `asset`
44
+ * protocol everywhere except android, which gets the custom
45
+ * `freqhole-media` protocol instead (see `client/charnel/src-tauri/src/
46
+ * media_protocol.rs`: the built-in one caps every range response to
47
+ * ~1MB, which android's webview media pipeline doesn't reliably recover
48
+ * from for files larger than that - confirmed via a live network trace).
49
+ * call `ensureInvoke()` (or anything that awaits it) at least once before
50
+ * calling this, so `isAndroidCached` is resolved.
51
+ */
52
+ function mediaSrcFor(path: string): string {
53
+ if (!convertFileSrc) {
54
+ throw new Error("convertFileSrc not available");
55
+ }
56
+ return isAndroidCached ? convertFileSrc(path, "freqhole-media") : convertFileSrc(path);
57
+ }
58
+
59
+ /**
60
+ * public, self-contained version of `mediaSrcFor` for callers outside this
61
+ * file (spume's `localAudio.ts`/`localVideo.ts`) - ensures tauri's invoke/
62
+ * convertFileSrc/target_os are loaded before resolving.
63
+ */
64
+ export async function resolveCharnelMediaSrc(path: string): Promise<string> {
65
+ await ensureInvoke();
66
+ return mediaSrcFor(path);
67
+ }
68
+
55
69
  /**
56
70
  * response shape from api_call command (matches GrimoireResponse)
57
71
  */
@@ -74,8 +88,6 @@ export class CharnelLocalTransport implements Transport {
74
88
  private blobPathCache = new Map<string, { path: string; mime?: string }>();
75
89
  // cache object URLs for db-stored blobs (no local path)
76
90
  private blobObjectUrlCache = new Map<string, string>();
77
- // single audio blob URL for linux workaround (revoke-on-replace to avoid memory leak)
78
- private audioBlobUrl: { blobId: string; url: string } | null = null;
79
91
 
80
92
  constructor(_baseUrl: string) {
81
93
  // baseUrl no longer needed - all requests go through IPC.
@@ -291,7 +303,7 @@ export class CharnelLocalTransport implements Transport {
291
303
  }
292
304
 
293
305
  // convert to asset URL and fetch via browser
294
- const assetUrl = convertFileSrc(pathInfo.path);
306
+ const assetUrl = mediaSrcFor(pathInfo.path);
295
307
  const fetchResponse = await fetch(assetUrl);
296
308
  const arrayBuffer = await fetchResponse.arrayBuffer();
297
309
 
@@ -339,13 +351,7 @@ export class CharnelLocalTransport implements Transport {
339
351
  /**
340
352
  * get blob URL — preference order:
341
353
  * 1. cached object URL (db-stored blobs)
342
- * 2. tauri asset:// (via `convertFileSrc`) on macos/windows
343
- * 3. linux fallback: fetch via asset:// and wrap in a blob: object URL
344
- * (webkitgtk can't stream asset:// into `<audio>`)
345
- *
346
- * the linux fallback buffers the ENTIRE file in memory, so it is never
347
- * used for video - a multi-GB local video would exhaust memory and hang
348
- * the app (`<video>` also seeks, which a one-shot blob can't stream).
354
+ * 2. tauri asset:// (via `convertFileSrc`), for any blob with a local path
349
355
  *
350
356
  * note: when the rodio audio backend is enabled (charnel + opt-in)
351
357
  * playback bypasses html `<audio>` entirely and reads files via
@@ -359,25 +365,15 @@ export class CharnelLocalTransport implements Transport {
359
365
  return cachedObjectUrl;
360
366
  }
361
367
 
362
- // on linux without rodio, we MUST go async to wrap in a blob:
363
- // url (asset:// can't stream into <audio> on webkitgtk) - except for
364
- // video, which streams from asset:// directly (see below).
365
- const cachedPath = this.blobPathCache.get(blobId);
366
- if (isLinuxWebKit && !isStreamableMime(cachedPath?.mime)) {
367
- console.debug(`[CharnelLocalTransport] blob ${blobId}: linux fallback (async)`);
368
- return this.getBlobUrlAsync(blobId);
369
- }
370
-
371
368
  // check path cache (filesystem blobs) — direct asset:// url
372
- const cached = cachedPath;
369
+ const cached = this.blobPathCache.get(blobId);
373
370
  if (cached && convertFileSrc) {
374
- const url = convertFileSrc(cached.path);
371
+ const url = mediaSrcFor(cached.path);
375
372
  console.debug(`[CharnelLocalTransport] blob ${blobId}: asset:// (cached) -> ${url}`);
376
373
  return url;
377
374
  }
378
375
 
379
376
  // need to fetch path (or data) first
380
- // console.debug(`[CharnelLocalTransport] blob ${blobId}: async path lookup`);
381
377
  return this.getBlobUrlAsync(blobId);
382
378
  }
383
379
 
@@ -400,22 +396,10 @@ export class CharnelLocalTransport implements Transport {
400
396
  throw new Error("convertFileSrc not available");
401
397
  }
402
398
 
403
- // on linux without media server: fall back to blob: workaround.
404
- // routes audio + image differently:
405
- // - audio: single-slot cache (revoke-on-replace) since audio
406
- // blobs are large and we only play one at a time
407
- // - other (images / waveforms / cover art): per-blob cache so
408
- // multiple `<img>` and css `background-image` urls coexist
409
- // across the playerbar, queue sidebar, etc.
410
- // video is excluded - buffering it would hang the app.
411
- if (isLinuxWebKit && !isStreamableMime(parsed.data.mime)) {
412
- return this.createBlobObjectUrl(blobId, parsed.data.path, parsed.data.mime);
413
- }
414
-
415
399
  console.debug(
416
400
  `[CharnelLocalTransport] blob ${blobId}: asset:// stream (mime=${parsed.data.mime ?? "?"})`,
417
401
  );
418
- return convertFileSrc(parsed.data.path);
402
+ return mediaSrcFor(parsed.data.path);
419
403
  }
420
404
  }
421
405
 
@@ -437,54 +421,6 @@ export class CharnelLocalTransport implements Transport {
437
421
  throw new Error(`failed to get blob path: ${response.body}`);
438
422
  }
439
423
 
440
- /**
441
- * create a blob: object URL by fetching via asset:// protocol.
442
- * used on linux where webkitgtk can't play asset:// in `<audio>`
443
- * elements (and historically also where the embedded http loopback
444
- * server stood in for the same workaround).
445
- *
446
- * mime-aware caching:
447
- * - `audio/*`: single-slot cache, revoke-on-replace. audio blobs
448
- * are large (often tens of MB) and only one ever plays at a
449
- * time, so leaking the rest is wasteful.
450
- * - everything else (images, waveforms, cover art): stored in
451
- * `blobObjectUrlCache` keyed by blob id so multiple `<img>` /
452
- * css `background-image` references coexist without one
453
- * revoking another.
454
- */
455
- private async createBlobObjectUrl(
456
- blobId: string,
457
- localPath: string,
458
- mime?: string,
459
- ): Promise<string> {
460
- if (!convertFileSrc) {
461
- throw new Error("convertFileSrc not available");
462
- }
463
-
464
- const isAudio = (mime ?? "").startsWith("audio/");
465
- const effectiveMime = mime ?? (isAudio ? "audio/mpeg" : "application/octet-stream");
466
-
467
- const assetUrl = convertFileSrc(localPath);
468
- const resp = await fetch(assetUrl);
469
- const arrayBuffer = await resp.arrayBuffer();
470
- const blob = new Blob([arrayBuffer], { type: effectiveMime });
471
- const objectUrl = URL.createObjectURL(blob);
472
-
473
- if (isAudio) {
474
- // revoke previous single-slot audio url (if any) so we don't
475
- // leak large buffers as the user moves between tracks.
476
- if (this.audioBlobUrl) {
477
- URL.revokeObjectURL(this.audioBlobUrl.url);
478
- }
479
- this.audioBlobUrl = { blobId, url: objectUrl };
480
- } else {
481
- // per-blob cache so e.g. the playerbar's waveform and the queue
482
- // sidebar's matching waveform share a single object url.
483
- this.blobObjectUrlCache.set(blobId, objectUrl);
484
- }
485
- return objectUrl;
486
- }
487
-
488
424
  // -----------------------------------------------------------------
489
425
  // job events (local ipc shortcut)
490
426
  //
package/src/index.ts CHANGED
@@ -31,6 +31,7 @@ export {
31
31
  CharnelLocalTransport,
32
32
  createCharnelLocalTransport,
33
33
  JobEventsStreamClosed,
34
+ resolveCharnelMediaSrc,
34
35
  } from "./CharnelLocalTransport.js";
35
36
  export type {
36
37
  MiddenNodeLike,