dsh-locator 0.8.2 → 0.10.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README-es.md +30 -5
  3. package/README-hi.md +31 -5
  4. package/README-pt.md +31 -5
  5. package/README.en.md +31 -5
  6. package/README.md +31 -5
  7. package/bin/darwin-arm64/indexd +0 -0
  8. package/bin/darwin-x64/indexd +0 -0
  9. package/bin/linux-arm64/indexd +0 -0
  10. package/bin/linux-x64/indexd +0 -0
  11. package/bin/win32-arm64/indexd.exe +0 -0
  12. package/bin/win32-x64/indexd.exe +0 -0
  13. package/client.js +1198 -117
  14. package/lib/combined.d.ts +20 -3
  15. package/lib/combined.d.ts.map +1 -1
  16. package/lib/combined.js +85 -15
  17. package/lib/combined.js.map +1 -1
  18. package/lib/content-search-worker.d.ts +8 -3
  19. package/lib/content-search-worker.d.ts.map +1 -1
  20. package/lib/content-search-worker.js +54 -27
  21. package/lib/content-search-worker.js.map +1 -1
  22. package/lib/content-search.d.ts +127 -7
  23. package/lib/content-search.d.ts.map +1 -1
  24. package/lib/content-search.js +571 -36
  25. package/lib/content-search.js.map +1 -1
  26. package/lib/content.d.ts +83 -5
  27. package/lib/content.d.ts.map +1 -1
  28. package/lib/content.js +351 -87
  29. package/lib/content.js.map +1 -1
  30. package/lib/export.d.ts +61 -0
  31. package/lib/export.d.ts.map +1 -1
  32. package/lib/export.js +217 -52
  33. package/lib/export.js.map +1 -1
  34. package/lib/find.d.ts.map +1 -1
  35. package/lib/find.js +73 -16
  36. package/lib/find.js.map +1 -1
  37. package/lib/http.d.ts +38 -2
  38. package/lib/http.d.ts.map +1 -1
  39. package/lib/http.js +279 -82
  40. package/lib/http.js.map +1 -1
  41. package/lib/reveal.d.ts +69 -3
  42. package/lib/reveal.d.ts.map +1 -1
  43. package/lib/reveal.js +140 -11
  44. package/lib/reveal.js.map +1 -1
  45. package/lib/tools.d.ts +30 -0
  46. package/lib/tools.d.ts.map +1 -1
  47. package/lib/tools.js +65 -3
  48. package/lib/tools.js.map +1 -1
  49. package/package.json +1 -1
  50. package/src/combined.ts +107 -17
  51. package/src/content-search-worker.ts +52 -24
  52. package/src/content-search.ts +710 -36
  53. package/src/content.ts +349 -73
  54. package/src/export.ts +240 -50
  55. package/src/find.ts +83 -16
  56. package/src/http.ts +271 -83
  57. package/src/reveal.ts +148 -10
  58. package/src/tools.ts +83 -6
@@ -1,14 +1,18 @@
1
1
  /**
2
- * Search worker: the FTS5 query runs here so a slow content query can never
3
- * stall the DSH host's event loop. A read-only connection makes the build's
4
- * writes in the main thread safe to observe concurrently (WAL).
2
+ * Search worker: content queries run here so a slow query can never stall the
3
+ * DSH host's event loop. A read-only connection makes the build's writes in the
4
+ * main thread safe to observe concurrently (WAL).
5
+ *
6
+ * Every request is answered with a complete {@link ContentSearchResult}; the
7
+ * worker never throws into the channel, because the host treats a timeout or a
8
+ * malformed reply as a hard error rather than re-running the query on the main
9
+ * thread.
5
10
  * @module dsh-locator/content-search-worker
6
11
  */
7
12
 
8
13
  import { parentPort, workerData } from 'node:worker_threads'
9
14
  import { DatabaseSync } from 'node:sqlite'
10
- import { searchContent } from './content-search.ts'
11
- import type { ContentHit } from './content.ts'
15
+ import { searchContent, type ContentSearchOptions, type ContentSearchResult } from './content-search.ts'
12
16
 
13
17
  interface Request {
14
18
  id: number
@@ -16,6 +20,12 @@ interface Request {
16
20
  close?: boolean
17
21
  query?: string
18
22
  limit?: number
23
+ options?: ContentSearchOptions
24
+ }
25
+
26
+ /** A result shape every failure path can share. */
27
+ function failure(error: string, warnings: string[] = [error]): ContentSearchResult {
28
+ return { hits: [], count: { value: 0, kind: 'unknown' }, warnings, error }
19
29
  }
20
30
 
21
31
  let db: DatabaseSync | null = null
@@ -26,27 +36,45 @@ function open(path: string): void {
26
36
  try { db = new DatabaseSync(path, { readOnly: true }) } catch { db = null }
27
37
  }
28
38
 
39
+ function post(message: Record<string, unknown>): void {
40
+ try { parentPort?.postMessage(message) } catch { /* the host is gone; nothing to do */ }
41
+ }
42
+
43
+ /** Handle one request; never throws. */
44
+ function handle(request: Request): void {
45
+ try {
46
+ if (request.open !== undefined) {
47
+ open(request.open)
48
+ post({ id: request.id, ok: true })
49
+ return
50
+ }
51
+ if (request.close === true) {
52
+ try { db?.close() } catch { /* ignore */ }
53
+ db = null
54
+ post({ id: request.id, ok: true })
55
+ return
56
+ }
57
+ if (typeof request.query !== 'string') {
58
+ post({ id: request.id, result: failure('content-search-worker-protocol-error') })
59
+ return
60
+ }
61
+ if (db === null) {
62
+ post({ id: request.id, result: failure('content-index-unavailable') })
63
+ return
64
+ }
65
+ const result = searchContent(db, request.query, request.limit ?? 30, request.options ?? {})
66
+ post({ id: request.id, result })
67
+ } catch {
68
+ // A thrown SQLite/JS error is reported as a result so the host can show a
69
+ // clear error code instead of silently re-running the query in-process.
70
+ post({ id: request.id, result: failure('content-search-worker-error') })
71
+ }
72
+ }
73
+
29
74
  const initial = (workerData as { dbPath?: string } | undefined)?.dbPath
30
75
  if (initial !== undefined) open(initial)
31
76
 
32
77
  parentPort?.on('message', (request: Request) => {
33
- if (request.open !== undefined) {
34
- open(request.open)
35
- parentPort?.postMessage({ id: request.id, ok: true })
36
- return
37
- }
38
- if (request.close === true) {
39
- try { db?.close() } catch { /* ignore */ }
40
- db = null
41
- parentPort?.postMessage({ id: request.id, ok: true })
42
- return
43
- }
44
- let hits: ContentHit[] = []
45
- let error = ''
46
- if (db !== null) {
47
- try { hits = searchContent(db, request.query ?? '', request.limit ?? 30) } catch (cause) {
48
- error = cause instanceof Error ? cause.message : String(cause)
49
- }
50
- }
51
- parentPort?.postMessage({ id: request.id, hits, error })
78
+ if (typeof request !== 'object' || request === null || typeof request.id !== 'number') return
79
+ handle(request)
52
80
  })