@soffits/dsh-media-hub 0.7.1 → 0.8.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.8.0
4
+
5
+ - The page no longer polls. The plugin tells open pages when something they show changes (a watch-state write, a refreshed copy of a show's details, a download landing or progressing, a room opening, closing or changing, a book on the shelf), over a Server-Sent Events stream, and the page re-reads then, and once whenever the stream connects or reconnects. Before, the library and rooms were re-read every 4 seconds and the shelf every 2 seconds while a book was fetching.
6
+
3
7
  ## 0.7.1
4
8
 
5
9
  - Opening the page no longer waits on the sources. Each show's details (title, blurb, score, related works, episode titles, cover) come from the copy saved with it, at once, as before 0.7.0. A copy that is missing (a show just added) or about a day old is refreshed from its source in the background, a couple of requests at a time and never for a source that just failed, and the new details show on the next look. A copy over a week old says when it was saved.
package/dist/index.js CHANGED
@@ -12671,7 +12671,8 @@ function runConverter(command, input, output) {
12671
12671
  });
12672
12672
  });
12673
12673
  }
12674
- function createBooks({ dir, sources, capBytes = 0, converter, covers, logger, now = Date.now }) {
12674
+ function createBooks({ dir, sources, capBytes = 0, converter, covers, logger, changed = () => {
12675
+ }, now = Date.now }) {
12675
12676
  let state = { files: {}, shelves: {} };
12676
12677
  let saving = Promise.resolve();
12677
12678
  const running = /* @__PURE__ */ new Map();
@@ -12679,6 +12680,7 @@ function createBooks({ dir, sources, capBytes = 0, converter, covers, logger, no
12679
12680
  let active = 0;
12680
12681
  let disposed = false;
12681
12682
  const save = () => {
12683
+ changed();
12682
12684
  saving = saving.then(async () => {
12683
12685
  await mkdir6(dir, { recursive: true });
12684
12686
  const temporary = join12(dir, `.${STATE_FILE}.${process.pid}.tmp`);
@@ -14096,6 +14098,7 @@ function createLibrary(options) {
14096
14098
  }
14097
14099
  }
14098
14100
  function save() {
14101
+ options.changed?.();
14099
14102
  const body2 = JSON.stringify({ shows: meta }, null, 1);
14100
14103
  saving = saving.then(async () => {
14101
14104
  await writeFile6(`${metaFile}.part`, body2);
@@ -14139,6 +14142,7 @@ function createLibrary(options) {
14139
14142
  await rm8(join15(target, name2), { force: true });
14140
14143
  for (const sub of matchSubtitles(name2, await readdir7(target).catch(() => []))) await rm8(join15(target, sub.name), { force: true });
14141
14144
  await forgetSum(target, name2);
14145
+ options.changed?.();
14142
14146
  }
14143
14147
  async function folderBytes(path) {
14144
14148
  const info = await stat9(path).catch(() => null);
@@ -14606,6 +14610,39 @@ function createFollows({ library, source, refreshed, addSeason, record, notify,
14606
14610
  };
14607
14611
  }
14608
14612
 
14613
+ // src/changes.js
14614
+ function createChanges({ intervalMs = 300 } = {}) {
14615
+ const listeners = /* @__PURE__ */ new Set();
14616
+ let pending = /* @__PURE__ */ new Set();
14617
+ let timer = null;
14618
+ let lastAt = 0;
14619
+ function flush() {
14620
+ timer = null;
14621
+ lastAt = Date.now();
14622
+ const what = [...pending];
14623
+ pending = /* @__PURE__ */ new Set();
14624
+ for (const listener of listeners) listener(what);
14625
+ }
14626
+ return {
14627
+ notify(kind) {
14628
+ pending.add(kind);
14629
+ if (timer !== null) return;
14630
+ timer = setTimeout(flush, Math.max(0, lastAt + intervalMs - Date.now()));
14631
+ timer.unref?.();
14632
+ },
14633
+ /** @returns {() => void} stop listening */
14634
+ listen(listener) {
14635
+ listeners.add(listener);
14636
+ return () => listeners.delete(listener);
14637
+ },
14638
+ close() {
14639
+ clearTimeout(timer);
14640
+ timer = null;
14641
+ listeners.clear();
14642
+ }
14643
+ };
14644
+ }
14645
+
14609
14646
  // src/work-refresh.js
14610
14647
  var DAY_MS3 = 864e5;
14611
14648
  var HOUR_MS = 36e5;
@@ -14656,7 +14693,8 @@ function createMediaHub(options) {
14656
14693
  const sources = new Map(options.sources.map((source2) => [source2.id, source2]));
14657
14694
  const covers = createCovers({ dir: join16(stateDir, "covers"), capBytes: options.coverCacheBytes, ffmpeg: options.tools.ffmpeg, logger }, (id) => sources.get(id));
14658
14695
  const log = createWatchLog({ file: join16(stateDir, LOG_NAME), now, logger });
14659
- const books = createBooks({ dir: join16(stateDir, "books"), sources, capBytes: options.books?.capBytes ?? 0, converter: options.books?.converter ?? NO_CONVERTER, covers, logger, now });
14696
+ const changes = createChanges({ intervalMs: options.changeIntervalMs });
14697
+ const books = createBooks({ changed: () => changes.notify("shelf"), dir: join16(stateDir, "books"), sources, capBytes: options.books?.capBytes ?? 0, converter: options.books?.converter ?? NO_CONVERTER, covers, logger, now });
14660
14698
  let rooms;
14661
14699
  let cache;
14662
14700
  const downloads = createDownloads({
@@ -14668,13 +14706,17 @@ function createMediaHub(options) {
14668
14706
  return disk === null || disk.bavail * disk.bsize >= MIN_FREE_BYTES;
14669
14707
  },
14670
14708
  onFiles,
14671
- onProgress: (job, number, progress) => rooms.downloadProgress(job.dir, number, progress),
14709
+ onProgress: (job, number, progress) => {
14710
+ rooms.downloadProgress(job.dir, number, progress);
14711
+ changes.notify("library");
14712
+ },
14672
14713
  onPartial: (job, number, partial) => rooms.streamEpisode(job.dir, number, partial),
14673
14714
  logger,
14674
14715
  now
14675
14716
  });
14676
14717
  const refreshWork = createWorkRefresh({ source: (id) => sources.get(id), keep: (path, work) => library.keepWork(path, work), maxAgeMs: options.workMaxAgeMs, logger, now });
14677
14718
  const library = createLibrary({
14719
+ changed: () => changes.notify("library"),
14678
14720
  roots: libraryRoots,
14679
14721
  stateDir,
14680
14722
  autoDeleteDays: options.autoDeleteDays,
@@ -14686,6 +14728,7 @@ function createMediaHub(options) {
14686
14728
  logger,
14687
14729
  now
14688
14730
  });
14731
+ const journal = createJournal({ log, library, now });
14689
14732
  rooms = createRoomService({
14690
14733
  ...options,
14691
14734
  watchOf: (item, show) => library.watchOfItem(show, item),
@@ -14694,7 +14737,10 @@ function createMediaHub(options) {
14694
14737
  cache.refresh(show, change).catch(() => {
14695
14738
  });
14696
14739
  },
14697
- observe: createJournal({ log, library, now })
14740
+ observe: (type, detail) => {
14741
+ journal(type, detail);
14742
+ if (type !== "progress") changes.notify("rooms");
14743
+ }
14698
14744
  });
14699
14745
  cache = createCache({ library, downloads, rooms, window: options.cache, logger });
14700
14746
  library.onChange((show) => {
@@ -14718,6 +14764,7 @@ function createMediaHub(options) {
14718
14764
  });
14719
14765
  }, HOUR_MS2);
14720
14766
  async function onFiles(job) {
14767
+ changes.notify("library");
14721
14768
  await rooms.syncShow(job.dir);
14722
14769
  if (job.status === "failed") rooms.failWaiting(job.dir, job.error ?? "the download failed", job.reason ?? null);
14723
14770
  await cache.enforce();
@@ -14838,6 +14885,7 @@ ${item.id}`).digest("hex").slice(0, 8);
14838
14885
  },
14839
14886
  async rate(input, person) {
14840
14887
  await log.append("rating", ratingLine(input, person));
14888
+ changes.notify("library");
14841
14889
  return { ok: true };
14842
14890
  },
14843
14891
  picks: (person) => picks.list(person),
@@ -14973,7 +15021,10 @@ ${item.id}`).digest("hex").slice(0, 8);
14973
15021
  // ---------------------------------------------------------- watch log
14974
15022
  history: (opts) => log.history(opts),
14975
15023
  logFile: log.file,
15024
+ /** Be told (with the kinds of what changed: `library`, `rooms`, `shelf`) when something a page shows changes; returns a way to stop. */
15025
+ listenChanges: (listener) => changes.listen(listener),
14976
15026
  async dispose() {
15027
+ changes.close();
14977
15028
  clearInterval(sweepTimer);
14978
15029
  follows.stop();
14979
15030
  await books.dispose();
@@ -15069,6 +15120,55 @@ function createPersonRoutes({ hub, identity }) {
15069
15120
  ];
15070
15121
  }
15071
15122
 
15123
+ // src/events-route.js
15124
+ var EVENTS_PATH = "/api/media-hub/events";
15125
+ var HEARTBEAT_MS = 25e3;
15126
+ function createEventsRoute({ hub }) {
15127
+ return {
15128
+ path: EVENTS_PATH,
15129
+ methods: ["GET"],
15130
+ requestBody: "buffered",
15131
+ fetch: async (request) => {
15132
+ const current = hub();
15133
+ if (current === void 0) return json(503, { error: { code: "unavailable", message: "The media hub is starting." } });
15134
+ const encoder = new TextEncoder();
15135
+ let stop = () => {
15136
+ };
15137
+ const stream = new ReadableStream({
15138
+ start(controller) {
15139
+ const write2 = (chunk) => {
15140
+ try {
15141
+ controller.enqueue(encoder.encode(chunk));
15142
+ } catch {
15143
+ stop();
15144
+ }
15145
+ };
15146
+ write2("retry: 5000\n: open\n\n");
15147
+ const unlisten = current.listenChanges((what) => write2(`data: ${JSON.stringify({ type: "change", what })}
15148
+
15149
+ `));
15150
+ const beat = setInterval(() => write2(": beat\n\n"), HEARTBEAT_MS);
15151
+ stop = () => {
15152
+ unlisten();
15153
+ clearInterval(beat);
15154
+ };
15155
+ request.signal?.addEventListener("abort", () => {
15156
+ stop();
15157
+ try {
15158
+ controller.close();
15159
+ } catch {
15160
+ }
15161
+ }, { once: true });
15162
+ },
15163
+ cancel() {
15164
+ stop();
15165
+ }
15166
+ });
15167
+ return new Response(stream, { status: 200, headers: { ...HEADERS, "content-type": "text/event-stream; charset=utf-8", "cache-control": "private, no-cache, no-transform", "x-accel-buffering": "no" } });
15168
+ }
15169
+ };
15170
+ }
15171
+
15072
15172
  // src/shelf-routes.js
15073
15173
  import { createReadStream as createReadStream3 } from "node:fs";
15074
15174
  import { stat as stat11 } from "node:fs/promises";
@@ -15166,7 +15266,8 @@ function apply(ctx, config) {
15166
15266
  const identity = () => ctx.get("requestIdentity");
15167
15267
  const fetchRoutes = [
15168
15268
  ...createShelfRoutes({ books: () => current?.books, identity }),
15169
- ...createPersonRoutes({ hub: () => current, identity })
15269
+ ...createPersonRoutes({ hub: () => current, identity }),
15270
+ createEventsRoute({ hub: () => current })
15170
15271
  ].map((route) => ctx.connection.fetch.register(route));
15171
15272
  const sockets = new import_websocket_server.default({ noServer: true, maxPayload: 16 * 1024, perMessageDeflate: false });
15172
15273
  const disposeHttp = ctx.webServer.register({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soffits/dsh-media-hub",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "A media hub for DeepSeek Harness: search shows, novels, ebooks and papers across sources, watch together in sync, and download books in the format you want onto your own shelf.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/web/books-view.js CHANGED
@@ -12,9 +12,8 @@
12
12
  */
13
13
  import { Cover } from './details.js'
14
14
  import { offeredFormats } from './formats.js'
15
- import { Confirm, Icon, Spinner, bytes, canChat, chatAbout, fill, h, languageName, React, shelf, shelfUrl } from './ui.js'
15
+ import { Changes, Confirm, Icon, Spinner, bytes, canChat, chatAbout, fill, h, languageName, React, shelf, shelfUrl } from './ui.js'
16
16
 
17
- const SHELF_POLL_MS = 2000
18
17
 
19
18
  export const FORMATS = ['', 'epub', 'pdf', 'txt', 'azw3', 'mobi']
20
19
  export const SIZES = ['', '5', '20', '50', '200']
@@ -67,22 +66,21 @@ export function ChatButton({ t, onClick }) {
67
66
 
68
67
  /** Waits until the shelf entry is no longer being fetched. */
69
68
  function useEntry(entryId) {
69
+ const changes = React.useContext(Changes)
70
70
  const [entry, setEntry] = React.useState(null)
71
71
  React.useEffect(() => {
72
72
  if (entryId === null) return undefined
73
73
  let live = true
74
- let timer
75
- const tick = async () => {
74
+ const read = async () => {
76
75
  const answer = await shelf()
77
76
  if (!live) return
78
77
  const found = answer.entries?.find(one => one.entry === entryId) ?? null
79
78
  setEntry(found)
80
- if (found?.state === 'fetching') timer = setTimeout(tick, SHELF_POLL_MS)
81
- else forgetShelf()
79
+ if (found?.state !== 'fetching') forgetShelf()
82
80
  }
83
- tick()
84
- return () => { live = false; clearTimeout(timer) }
85
- }, [entryId])
81
+ read()
82
+ return () => { live = false }
83
+ }, [entryId, changes.shelf])
86
84
  return entry
87
85
  }
88
86
 
@@ -138,6 +136,7 @@ export function BookCard({ t, source, result }) {
138
136
 
139
137
  /** My books: what the signed-in person downloaded, to save again, fetch again or delete. */
140
138
  export function ShelfSection({ t }) {
139
+ const changes = React.useContext(Changes)
141
140
  const [state, setState] = React.useState(null)
142
141
  const [confirm, setConfirm] = React.useState(null)
143
142
  const load = React.useCallback(async () => {
@@ -145,23 +144,13 @@ export function ShelfSection({ t }) {
145
144
  setState(answer)
146
145
  return answer
147
146
  }, [])
148
- React.useEffect(() => {
149
- let live = true
150
- let timer
151
- const tick = async () => {
152
- const answer = await load()
153
- if (live && answer.entries?.some(entry => entry.state === 'fetching')) timer = setTimeout(tick, SHELF_POLL_MS)
154
- }
155
- tick()
156
- return () => { live = false; clearTimeout(timer) }
157
- }, [load])
147
+ React.useEffect(() => { load() }, [load, changes.shelf])
158
148
 
159
149
  const act = async (path, entry) => {
160
150
  setConfirm(null)
161
151
  await shelf(path, { entry: entry.entry })
162
152
  forgetShelf()
163
- const answer = await load()
164
- if (answer.entries?.some(one => one.state === 'fetching')) setTimeout(load, SHELF_POLL_MS)
153
+ await load()
165
154
  }
166
155
 
167
156
  if (state === null) return null
package/web/page.js CHANGED
@@ -9,7 +9,7 @@ import { LibrarySection } from './library-view.js'
9
9
  import { clearNav, readNav, TABS, writeNav } from './nav.js'
10
10
  import { RoomView } from './room-view.js'
11
11
  import { WatchingTab, withNews } from './watching-view.js'
12
- import { Known, P, POLL_MS, api, h, React, setWords } from './ui.js'
12
+ import { Changes, K, Known, P, api, h, React, setWords } from './ui.js'
13
13
 
14
14
  /** Marks the history entries this page added, so a back control knows going back stays on the page. */
15
15
  const PUSHED = 'vsyncPushed'
@@ -89,16 +89,18 @@ export function Page({ t, initialRoom }) {
89
89
  if (lib.shows) setLibrary(lib)
90
90
  }, [])
91
91
 
92
- React.useEffect(() => {
93
- let live = true
94
- const tick = () => { if (live) load() }
95
- tick()
96
- const timer = setInterval(tick, POLL_MS)
97
- return () => { live = false; clearInterval(timer) }
98
- }, [load])
92
+ // The plugin announces every change of what the page shows (src/changes.js); the page re-reads then, and once when the stream (re)connects. No timer.
93
+ const [shelf, setShelf] = React.useState(0)
94
+ const stream = K.useEventStream('api/media-hub/events', (message) => {
95
+ if (message.what?.includes('shelf')) setShelf(count => count + 1)
96
+ if (message.what?.some(kind => kind === 'library' || kind === 'rooms')) load()
97
+ })
98
+ React.useEffect(() => { if (stream === 'live') { load(); setShelf(count => count + 1) } }, [stream, load])
99
+ React.useEffect(() => { load() }, [load])
99
100
 
100
101
  const known = React.useMemo(() => ({ library, rooms, reload: load }), [library, rooms, load])
101
- return h(Known.Provider, { value: known }, roomId === null
102
+ const changes = React.useMemo(() => ({ shelf }), [shelf])
103
+ return h(Changes.Provider, { value: changes }, h(Known.Provider, { value: known }, roomId === null
102
104
  ? h(Home, { t, nav, go, onOpen: setRoomId, rooms, library, load })
103
- : h(RoomView, { key: roomId, t, roomId, onLeave: () => setRoomId(null), onOpen: setRoomId }))
105
+ : h(RoomView, { key: roomId, t, roomId, onLeave: () => setRoomId(null), onOpen: setRoomId })))
104
106
  }
package/web/ui.js CHANGED
@@ -9,6 +9,8 @@ import { pick } from './i18n.js'
9
9
 
10
10
  export let React
11
11
  export let h
12
+ /** How many times each kind of change (`shelf`) has been announced by the plugin's change stream: a view re-reads when its count moves. */
13
+ export let Changes
12
14
  /** What the page knows of the library and the open rooms (web/known.js): set by Page, read anywhere a work is shown. */
13
15
  export let Known
14
16
  /** DSH's own components (@deepseek-ai/dsh-client-ui-primitives): Button, Tag, StateDot, SegmentedTabs, icons. */
@@ -22,6 +24,7 @@ export function init(react, { primitives, kit }) {
22
24
  h = react.createElement
23
25
  P = primitives
24
26
  K = kit
27
+ Changes = react.createContext({ shelf: 0 })
25
28
  Known = react.createContext({ library: null, rooms: undefined, reload: () => {} })
26
29
  }
27
30
 
@@ -30,7 +33,6 @@ export const Btn = ({ variant = 'outline', size = 'sm', className, ...rest }) =>
30
33
 
31
34
  export const base = () => new URL('media-hub/', document.baseURI)
32
35
  export const LINK_PARAM = 'media-room'
33
- export const POLL_MS = 4000
34
36
 
35
37
  /** The page's words for failures, set by the page whenever its language changes (`setWords`). */
36
38
  let words = null