@hatk/hatk 0.0.1-alpha.21 → 0.0.1-alpha.23

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 (84) hide show
  1. package/dist/backfill.d.ts.map +1 -1
  2. package/dist/backfill.js +4 -4
  3. package/dist/car.js +1 -1
  4. package/dist/cli.js +111 -54
  5. package/dist/config.d.ts +1 -0
  6. package/dist/config.d.ts.map +1 -1
  7. package/dist/config.js +1 -0
  8. package/dist/database/adapter-factory.d.ts +6 -0
  9. package/dist/database/adapter-factory.d.ts.map +1 -0
  10. package/dist/database/adapter-factory.js +20 -0
  11. package/dist/database/adapters/duckdb-search.d.ts +12 -0
  12. package/dist/database/adapters/duckdb-search.d.ts.map +1 -0
  13. package/dist/database/adapters/duckdb-search.js +27 -0
  14. package/dist/database/adapters/duckdb.d.ts +25 -0
  15. package/dist/database/adapters/duckdb.d.ts.map +1 -0
  16. package/dist/database/adapters/duckdb.js +161 -0
  17. package/dist/database/adapters/sqlite-search.d.ts +18 -0
  18. package/dist/database/adapters/sqlite-search.d.ts.map +1 -0
  19. package/dist/database/adapters/sqlite-search.js +38 -0
  20. package/dist/database/adapters/sqlite.d.ts +18 -0
  21. package/dist/database/adapters/sqlite.d.ts.map +1 -0
  22. package/dist/database/adapters/sqlite.js +87 -0
  23. package/dist/database/db.d.ts +149 -0
  24. package/dist/database/db.d.ts.map +1 -0
  25. package/dist/database/db.js +1456 -0
  26. package/dist/database/dialect.d.ts +45 -0
  27. package/dist/database/dialect.d.ts.map +1 -0
  28. package/dist/database/dialect.js +72 -0
  29. package/dist/database/fts.d.ts +24 -0
  30. package/dist/database/fts.d.ts.map +1 -0
  31. package/dist/database/fts.js +777 -0
  32. package/dist/database/index.d.ts +7 -0
  33. package/dist/database/index.d.ts.map +1 -0
  34. package/dist/database/index.js +6 -0
  35. package/dist/database/ports.d.ts +44 -0
  36. package/dist/database/ports.d.ts.map +1 -0
  37. package/dist/database/ports.js +1 -0
  38. package/dist/database/schema.d.ts +60 -0
  39. package/dist/database/schema.d.ts.map +1 -0
  40. package/dist/database/schema.js +388 -0
  41. package/dist/feeds.js +1 -1
  42. package/dist/hooks.d.ts +15 -0
  43. package/dist/hooks.d.ts.map +1 -0
  44. package/dist/hooks.js +65 -0
  45. package/dist/hydrate.js +1 -1
  46. package/dist/indexer.d.ts +19 -0
  47. package/dist/indexer.d.ts.map +1 -1
  48. package/dist/indexer.js +34 -4
  49. package/dist/labels.d.ts +20 -0
  50. package/dist/labels.d.ts.map +1 -1
  51. package/dist/labels.js +50 -2
  52. package/dist/logger.d.ts +29 -0
  53. package/dist/logger.d.ts.map +1 -1
  54. package/dist/logger.js +29 -0
  55. package/dist/main.js +37 -32
  56. package/dist/mst.d.ts +15 -0
  57. package/dist/mst.d.ts.map +1 -1
  58. package/dist/mst.js +13 -0
  59. package/dist/oauth/db.d.ts.map +1 -1
  60. package/dist/oauth/db.js +41 -15
  61. package/dist/oauth/server.d.ts.map +1 -1
  62. package/dist/oauth/server.js +11 -6
  63. package/dist/opengraph.js +1 -1
  64. package/dist/schema.d.ts +8 -0
  65. package/dist/schema.d.ts.map +1 -1
  66. package/dist/schema.js +29 -0
  67. package/dist/seed.d.ts +19 -0
  68. package/dist/seed.d.ts.map +1 -1
  69. package/dist/seed.js +43 -4
  70. package/dist/server.d.ts.map +1 -1
  71. package/dist/server.js +12 -6
  72. package/dist/setup.d.ts +21 -1
  73. package/dist/setup.d.ts.map +1 -1
  74. package/dist/setup.js +37 -2
  75. package/dist/test.d.ts +1 -1
  76. package/dist/test.d.ts.map +1 -1
  77. package/dist/test.js +23 -9
  78. package/dist/views.js +1 -1
  79. package/dist/vite-plugin.d.ts.map +1 -1
  80. package/dist/vite-plugin.js +10 -0
  81. package/dist/xrpc.d.ts +23 -0
  82. package/dist/xrpc.d.ts.map +1 -1
  83. package/dist/xrpc.js +37 -2
  84. package/package.json +3 -1
package/dist/indexer.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import { cborDecode } from "./cbor.js";
2
2
  import { parseCarFrame } from "./car.js";
3
- import { insertRecord, deleteRecord, setCursor, setRepoStatus, getRepoRetryInfo, listAllRepoStatuses } from "./db.js";
3
+ import { insertRecord, deleteRecord, setCursor, setRepoStatus, getRepoRetryInfo, listAllRepoStatuses, } from "./database/db.js";
4
4
  import { backfillRepo } from "./backfill.js";
5
- import { rebuildAllIndexes } from "./fts.js";
5
+ import { rebuildAllIndexes } from "./database/fts.js";
6
6
  import { log, emit, timer } from "./logger.js";
7
7
  import { runLabelRules } from "./labels.js";
8
- import { getLexiconArray } from "./schema.js";
8
+ import { getLexiconArray } from "./database/schema.js";
9
9
  import { validateRecord } from '@bigmoves/lexicon';
10
10
  let buffer = [];
11
11
  let flushTimer = null;
@@ -28,6 +28,11 @@ let indexerPinnedRepos = null;
28
28
  let indexerFetchTimeout;
29
29
  let indexerMaxRetries;
30
30
  let maxConcurrentBackfills = 3;
31
+ /**
32
+ * Flush the write buffer — insert all buffered records, update the relay cursor,
33
+ * run label rules on inserted records, and trigger FTS rebuilds when the write
34
+ * threshold is reached. Emits a wide event with batch stats.
35
+ */
31
36
  async function flushBuffer() {
32
37
  if (buffer.length === 0)
33
38
  return;
@@ -90,6 +95,7 @@ async function flushBuffer() {
90
95
  rebuildAllIndexes([...indexerCollections]).catch(() => { });
91
96
  }
92
97
  }
98
+ /** Schedule a flush after FLUSH_INTERVAL_MS if one isn't already pending. */
93
99
  function scheduleFlush() {
94
100
  if (flushTimer)
95
101
  return;
@@ -98,6 +104,7 @@ function scheduleFlush() {
98
104
  await flushBuffer();
99
105
  }, FLUSH_INTERVAL_MS);
100
106
  }
107
+ /** Add a record to the write buffer. Flushes immediately if BATCH_SIZE is reached. */
101
108
  function bufferWrite(item) {
102
109
  buffer.push(item);
103
110
  if (buffer.length >= BATCH_SIZE) {
@@ -111,6 +118,14 @@ function bufferWrite(item) {
111
118
  scheduleFlush();
112
119
  }
113
120
  }
121
+ /**
122
+ * Auto-backfill a DID's repo when first seen on the firehose.
123
+ *
124
+ * Fetches the full repo via CAR export, inserts all records, then replays any
125
+ * firehose events that arrived during the backfill. Concurrency is capped at
126
+ * `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
127
+ * to `maxRetries`.
128
+ */
114
129
  export async function triggerAutoBackfill(did, attempt = 0) {
115
130
  if (backfillInFlight.has(did))
116
131
  return;
@@ -173,7 +188,7 @@ export async function triggerAutoBackfill(did, attempt = 0) {
173
188
  }, delayMs);
174
189
  }
175
190
  }
176
- // Periodic memory diagnostics
191
+ /** Emit a memory diagnostics wide event every 30s for observability. */
177
192
  function startMemoryDiagnostics() {
178
193
  setInterval(() => {
179
194
  const mem = process.memoryUsage();
@@ -195,6 +210,16 @@ function startMemoryDiagnostics() {
195
210
  });
196
211
  }, 30_000);
197
212
  }
213
+ /**
214
+ * Connect to the AT Protocol relay firehose and begin indexing.
215
+ *
216
+ * Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
217
+ * on the event loop to minimize backpressure, and batches writes through
218
+ * {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
219
+ * Reconnects automatically on disconnect after a 3s delay.
220
+ *
221
+ * @returns The WebSocket connection (for shutdown coordination)
222
+ */
198
223
  export async function startIndexer(opts) {
199
224
  const { relayUrl, collections, cursor, fetchTimeout } = opts;
200
225
  if (opts.ftsRebuildInterval != null)
@@ -243,6 +268,11 @@ export async function startIndexer(opts) {
243
268
  });
244
269
  return ws;
245
270
  }
271
+ /**
272
+ * Process a single firehose message. Decodes the CBOR header/body, filters
273
+ * for relevant collections, validates records against lexicons, and routes
274
+ * writes to the buffer (or pending buffer if the DID is mid-backfill).
275
+ */
246
276
  function processMessage(bytes, collections) {
247
277
  const header = cborDecode(bytes, 0);
248
278
  const body = cborDecode(bytes, header.offset);
package/dist/labels.d.ts CHANGED
@@ -13,7 +13,20 @@ export interface LabelRuleContext {
13
13
  value: Record<string, any>;
14
14
  };
15
15
  }
16
+ /**
17
+ * Discover and load label rule modules from the `labels/` directory.
18
+ *
19
+ * Each module should default-export an object with an optional `definition`
20
+ * (label metadata like severity and blur behavior) and an optional `evaluate`
21
+ * function that returns label values to apply to a record.
22
+ *
23
+ * @param labelsDir - Absolute path to the `labels/` directory
24
+ */
16
25
  export declare function initLabels(labelsDir: string): Promise<void>;
26
+ /**
27
+ * Evaluate all loaded label rules against a record and persist any resulting labels.
28
+ * Called after each record is indexed. Rule errors are logged but never block indexing.
29
+ */
17
30
  export declare function runLabelRules(record: {
18
31
  uri: string;
19
32
  cid: string;
@@ -21,9 +34,16 @@ export declare function runLabelRules(record: {
21
34
  collection: string;
22
35
  value: Record<string, any>;
23
36
  }): Promise<void>;
37
+ /**
38
+ * Re-evaluate all label rules against every existing record in the given collections.
39
+ * Used by `/admin/rescan-labels` to apply new or updated rules retroactively.
40
+ *
41
+ * @returns Count of records scanned and new labels applied
42
+ */
24
43
  export declare function rescanLabels(collections: string[]): Promise<{
25
44
  scanned: number;
26
45
  labeled: number;
27
46
  }>;
47
+ /** Return all label definitions discovered during {@link initLabels}. */
28
48
  export declare function getLabelDefinitions(): LabelDefinition[];
29
49
  //# sourceMappingURL=labels.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"labels.d.ts","sourceRoot":"","sources":["../src/labels.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAIlD,wDAAwD;AACxD,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE;QACF,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAA;QACtD,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACtD,CAAA;IACD,MAAM,EAAE;QACN,GAAG,EAAE,MAAM,CAAA;QACX,GAAG,EAAE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U,EAAE,MAAM,CAAA;QAClB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAC3B,CAAA;CACF;AAYD;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAmCjE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC3B,GAAG,OAAO,CAAC,IAAI,CAAC,CAyBhB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAuCvG;AAED,yEAAyE;AACzE,wBAAgB,mBAAmB,IAAI,eAAe,EAAE,CAEvD"}
package/dist/labels.js CHANGED
@@ -6,13 +6,50 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
6
6
  }
7
7
  return path;
8
8
  };
9
+ /**
10
+ * Label system for applying moderation labels to records as they are indexed.
11
+ *
12
+ * Place label modules in the `labels/` directory. Each module default-exports
13
+ * an object with a `definition` (label metadata) and/or an `evaluate` function
14
+ * (rule that returns label values for a given record).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * // labels/nsfw.ts
19
+ * import type { LabelRuleContext } from '@hatk/hatk/labels'
20
+ *
21
+ * export default {
22
+ * definition: {
23
+ * identifier: 'nsfw',
24
+ * severity: 'alert',
25
+ * blurs: 'media',
26
+ * defaultSetting: 'warn',
27
+ * locales: [{ lang: 'en', name: 'NSFW', description: 'Not safe for work' }],
28
+ * },
29
+ *
30
+ * async evaluate(ctx: LabelRuleContext): Promise<string[]> {
31
+ * if (ctx.record.value.nsfw === true) return ['nsfw']
32
+ * return []
33
+ * },
34
+ * }
35
+ * ```
36
+ */
9
37
  import { resolve } from 'node:path';
10
38
  import { readdirSync } from 'node:fs';
11
- import { querySQL, runSQL, insertLabels, getSchema } from "./db.js";
39
+ import { querySQL, runSQL, insertLabels, getSchema } from "./database/db.js";
12
40
  import { log, emit } from "./logger.js";
13
41
  const rules = [];
14
42
  let labelDefs = [];
15
43
  let labelSrc = 'self';
44
+ /**
45
+ * Discover and load label rule modules from the `labels/` directory.
46
+ *
47
+ * Each module should default-export an object with an optional `definition`
48
+ * (label metadata like severity and blur behavior) and an optional `evaluate`
49
+ * function that returns label values to apply to a record.
50
+ *
51
+ * @param labelsDir - Absolute path to the `labels/` directory
52
+ */
16
53
  export async function initLabels(labelsDir) {
17
54
  let files;
18
55
  try {
@@ -45,6 +82,10 @@ export async function initLabels(labelsDir) {
45
82
  log(`[labels] ${labelDefs.length} label definitions loaded`);
46
83
  }
47
84
  }
85
+ /**
86
+ * Evaluate all loaded label rules against a record and persist any resulting labels.
87
+ * Called after each record is indexed. Rule errors are logged but never block indexing.
88
+ */
48
89
  export async function runLabelRules(record) {
49
90
  if (rules.length === 0)
50
91
  return;
@@ -69,6 +110,12 @@ export async function runLabelRules(record) {
69
110
  emit('labels', 'applied', { count: allLabels.length, uri: record.uri, vals: allLabels.map((l) => l.val) });
70
111
  }
71
112
  }
113
+ /**
114
+ * Re-evaluate all label rules against every existing record in the given collections.
115
+ * Used by `/admin/rescan-labels` to apply new or updated rules retroactively.
116
+ *
117
+ * @returns Count of records scanned and new labels applied
118
+ */
72
119
  export async function rescanLabels(collections) {
73
120
  const beforeRows = await querySQL(`SELECT COUNT(*) as count FROM _labels`);
74
121
  const beforeCount = Number(beforeRows[0]?.count || 0);
@@ -85,7 +132,7 @@ export async function rescanLabels(collections) {
85
132
  let v = row[col.name];
86
133
  if (v === null || v === undefined)
87
134
  continue;
88
- if (col.duckdbType === 'JSON' && typeof v === 'string') {
135
+ if (col.sqlType === 'JSON' && typeof v === 'string') {
89
136
  try {
90
137
  v = JSON.parse(v);
91
138
  }
@@ -106,6 +153,7 @@ export async function rescanLabels(collections) {
106
153
  const afterCount = Number(afterRows[0]?.count || 0);
107
154
  return { scanned, labeled: afterCount - beforeCount };
108
155
  }
156
+ /** Return all label definitions discovered during {@link initLabels}. */
109
157
  export function getLabelDefinitions() {
110
158
  return labelDefs;
111
159
  }
package/dist/logger.d.ts CHANGED
@@ -1,4 +1,33 @@
1
+ /**
2
+ * Unstructured debug log — use sparingly for human-readable dev output.
3
+ * Prefer {@link emit} for anything that should be queryable in production.
4
+ * Disabled when `DEBUG=0`.
5
+ */
1
6
  export declare function log(...args: unknown[]): void;
7
+ /**
8
+ * Emit a structured wide event as a single JSON line to stdout.
9
+ *
10
+ * Each call produces one canonical log line with a timestamp, module, operation,
11
+ * and arbitrary key-value fields — designed for columnar search and aggregation,
12
+ * not string grep. Pack as much context as possible into `fields` (request IDs,
13
+ * durations, status codes, user DIDs, counts) so a single event tells the full
14
+ * story. See https://loggingsucks.com for the philosophy behind this approach.
15
+ *
16
+ * Disabled when `DEBUG=0`.
17
+ *
18
+ * @param module - Subsystem emitting the event (e.g. "server", "indexer", "backfill")
19
+ * @param op - Operation name (e.g. "request", "commit", "memory")
20
+ * @param fields - High-cardinality key-value context — include everything relevant
21
+ */
2
22
  export declare function emit(module: string, op: string, fields: Record<string, unknown>): void;
23
+ /**
24
+ * Start a millisecond timer. Call the returned function to get elapsed ms.
25
+ * Use with {@link emit} to add `duration_ms` to wide events.
26
+ *
27
+ * @example
28
+ * const elapsed = timer()
29
+ * await doWork()
30
+ * emit('server', 'request', { path, status_code, duration_ms: elapsed() })
31
+ */
3
32
  export declare function timer(): () => number;
4
33
  //# sourceMappingURL=logger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,wBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAG5C;AAED,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAWtF;AAED,wBAAgB,KAAK,IAAI,MAAM,MAAM,CAGpC"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAG5C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAWtF;AAED;;;;;;;;GAQG;AACH,wBAAgB,KAAK,IAAI,MAAM,MAAM,CAGpC"}
package/dist/logger.js CHANGED
@@ -1,8 +1,28 @@
1
+ /**
2
+ * Unstructured debug log — use sparingly for human-readable dev output.
3
+ * Prefer {@link emit} for anything that should be queryable in production.
4
+ * Disabled when `DEBUG=0`.
5
+ */
1
6
  export function log(...args) {
2
7
  if (process.env.DEBUG === '0')
3
8
  return;
4
9
  console.log(...args);
5
10
  }
11
+ /**
12
+ * Emit a structured wide event as a single JSON line to stdout.
13
+ *
14
+ * Each call produces one canonical log line with a timestamp, module, operation,
15
+ * and arbitrary key-value fields — designed for columnar search and aggregation,
16
+ * not string grep. Pack as much context as possible into `fields` (request IDs,
17
+ * durations, status codes, user DIDs, counts) so a single event tells the full
18
+ * story. See https://loggingsucks.com for the philosophy behind this approach.
19
+ *
20
+ * Disabled when `DEBUG=0`.
21
+ *
22
+ * @param module - Subsystem emitting the event (e.g. "server", "indexer", "backfill")
23
+ * @param op - Operation name (e.g. "request", "commit", "memory")
24
+ * @param fields - High-cardinality key-value context — include everything relevant
25
+ */
6
26
  export function emit(module, op, fields) {
7
27
  if (process.env.DEBUG === '0')
8
28
  return;
@@ -17,6 +37,15 @@ export function emit(module, op, fields) {
17
37
  }
18
38
  process.stdout.write(JSON.stringify(entry) + '\n');
19
39
  }
40
+ /**
41
+ * Start a millisecond timer. Call the returned function to get elapsed ms.
42
+ * Use with {@link emit} to add `duration_ms` to wide events.
43
+ *
44
+ * @example
45
+ * const elapsed = timer()
46
+ * await doWork()
47
+ * emit('server', 'request', { path, status_code, duration_ms: elapsed() })
48
+ */
20
49
  export function timer() {
21
50
  const start = performance.now();
22
51
  return () => Math.round(performance.now() - start);
package/dist/main.js CHANGED
@@ -1,23 +1,26 @@
1
1
  #!/usr/bin/env node
2
- import { mkdirSync } from 'node:fs';
2
+ import { mkdirSync, writeFileSync } from 'node:fs';
3
3
  import { dirname, resolve } from 'node:path';
4
4
  import { log } from "./logger.js";
5
5
  import { loadConfig } from "./config.js";
6
- import { loadLexicons, storeLexicons, discoverCollections, generateTableSchema, generateCreateTableSQL, } from "./schema.js";
6
+ import { loadLexicons, storeLexicons, discoverCollections, buildSchemas } from "./database/schema.js";
7
7
  import { discoverViews } from "./views.js";
8
- import { initDatabase, getCursor, querySQL } from "./db.js";
8
+ import { initDatabase, getCursor, querySQL, getSqlDialect, getSchemaDump, migrateSchema } from "./database/db.js";
9
+ import { createAdapter } from "./database/adapter-factory.js";
10
+ import { getDialect } from "./database/dialect.js";
11
+ import { setSearchPort } from "./database/fts.js";
9
12
  import { initFeeds, listFeeds } from "./feeds.js";
10
13
  import { initXrpc, listXrpc, configureRelay } from "./xrpc.js";
11
14
  import { initOpengraph } from "./opengraph.js";
12
15
  import { initLabels, getLabelDefinitions } from "./labels.js";
13
16
  import { startIndexer } from "./indexer.js";
14
- import { rebuildAllIndexes } from "./fts.js";
17
+ import { rebuildAllIndexes } from "./database/fts.js";
15
18
  import { startServer } from "./server.js";
16
19
  import { validateLexicons } from '@bigmoves/lexicon';
17
20
  import { relayHttpUrl } from "./config.js";
18
21
  import { runBackfill } from "./backfill.js";
19
22
  import { initOAuth } from "./oauth/server.js";
20
- import { loadOnLoginHook } from "./oauth/hooks.js";
23
+ import { loadOnLoginHook } from "./hooks.js";
21
24
  import { initSetup } from "./setup.js";
22
25
  function logMemory(phase) {
23
26
  const mem = process.memoryUsage();
@@ -50,42 +53,44 @@ log(`[main] Loaded config: ${collections.length} collections`);
50
53
  // Discover view defs from lexicons
51
54
  discoverViews();
52
55
  await loadOnLoginHook(resolve(configDir, 'hooks'));
53
- const schemas = [];
54
- const ddlStatements = [];
55
- for (const nsid of collections) {
56
- const lexicon = lexicons.get(nsid);
57
- if (!lexicon) {
58
- log(`[main] No lexicon found for ${nsid}, using generic JSON storage`);
59
- const genericDDL = `CREATE TABLE IF NOT EXISTS "${nsid}" (
60
- uri TEXT PRIMARY KEY,
61
- cid TEXT,
62
- did TEXT NOT NULL,
63
- indexed_at TIMESTAMP NOT NULL,
64
- data JSON
65
- );
66
- CREATE INDEX IF NOT EXISTS idx_${nsid.replace(/\./g, '_')}_indexed ON "${nsid}"(indexed_at DESC);
67
- CREATE INDEX IF NOT EXISTS idx_${nsid.replace(/\./g, '_')}_author ON "${nsid}"(did);`;
68
- schemas.push({ collection: nsid, tableName: `"${nsid}"`, columns: [], refColumns: [], children: [], unions: [] });
69
- ddlStatements.push(genericDDL);
70
- continue;
56
+ const engineDialect = getDialect(config.databaseEngine);
57
+ const { schemas, ddlStatements } = buildSchemas(lexicons, collections, engineDialect);
58
+ for (const s of schemas) {
59
+ if (s.columns.length === 0) {
60
+ log(`[main] No lexicon found for ${s.collection}, using generic JSON storage`);
61
+ }
62
+ else {
63
+ log(`[main] Schema for ${s.collection}: ${s.columns.length} columns, ${s.unions.length} unions`);
71
64
  }
72
- const schema = generateTableSchema(nsid, lexicon, lexicons);
73
- schemas.push(schema);
74
- ddlStatements.push(generateCreateTableSQL(schema));
75
- log(`[main] Schema for ${nsid}: ${schema.columns.length} columns, ${schema.unions.length} unions`);
76
65
  }
77
- // 3. Ensure data directory exists and initialize DuckDB
66
+ // 3. Ensure data directory exists and initialize database
78
67
  if (config.database !== ':memory:') {
79
68
  mkdirSync(dirname(config.database), { recursive: true });
80
69
  }
81
- await initDatabase(config.database, schemas, ddlStatements);
70
+ const { adapter, searchPort } = await createAdapter(config.databaseEngine);
71
+ setSearchPort(searchPort);
72
+ await initDatabase(adapter, config.database, schemas, ddlStatements);
82
73
  logMemory('after-db-init');
83
- log(`[main] DuckDB initialized (${config.database === ':memory:' ? 'in-memory' : config.database})`);
74
+ log(`[main] Database initialized (${config.databaseEngine}, ${config.database === ':memory:' ? 'in-memory' : config.database})`);
75
+ // Auto-migrate schema if lexicons changed
76
+ const migrationChanges = await migrateSchema(schemas);
77
+ if (migrationChanges.length > 0) {
78
+ log(`[main] Applied ${migrationChanges.length} schema migration(s)`);
79
+ }
84
80
  // 3b. Run setup hooks (after DB init, before server)
85
81
  await initSetup(resolve(configDir, 'setup'));
82
+ // Write db/schema.sql (after setup, so setup-created tables are included)
83
+ try {
84
+ const schemaDir = resolve(configDir, 'db');
85
+ mkdirSync(schemaDir, { recursive: true });
86
+ const schemaDump = await getSchemaDump();
87
+ writeFileSync(resolve(schemaDir, 'schema.sql'), `-- This file is auto-generated by hatk on startup. Do not edit.\n-- Database engine: ${config.databaseEngine}\n\n${schemaDump}\n`);
88
+ log(`[main] Schema written to db/schema.sql`);
89
+ }
90
+ catch { }
86
91
  // Detect orphaned tables
87
92
  try {
88
- const existingTables = await querySQL(`SELECT table_name FROM information_schema.tables WHERE table_schema = 'main' AND table_name NOT LIKE '\\_%' ESCAPE '\\'`);
93
+ const existingTables = await querySQL(getSqlDialect().listTablesQuery);
89
94
  for (const row of existingTables) {
90
95
  const tableName = row.table_name;
91
96
  const isChildTable = collections.some((c) => tableName.startsWith(c + '__'));
@@ -127,7 +132,7 @@ function runBackfillAndRestart() {
127
132
  })
128
133
  .then((recordCount) => {
129
134
  log('[main] FTS indexes ready');
130
- if (recordCount > 0) {
135
+ if (recordCount > 0 && !process.env.DEV_MODE) {
131
136
  logMemory('after-backfill');
132
137
  log('[main] Restarting to reclaim memory...');
133
138
  process.exit(1);
package/dist/mst.d.ts CHANGED
@@ -1,7 +1,22 @@
1
+ /** A single entry from a Merkle Search Tree — a record path paired with its content CID. */
1
2
  export interface MstEntry {
3
+ /** Record path, e.g. "xyz.marketplace.listing/3mfniulnr7c2g" */
2
4
  path: string;
5
+ /** CID of the record's CBOR block */
3
6
  cid: string;
4
7
  }
8
+ /**
9
+ * Walk an AT Protocol Merkle Search Tree (MST) in key order, yielding every record entry.
10
+ *
11
+ * The MST is a prefix-compressed B+ tree used by AT Protocol repositories to map
12
+ * record paths to CIDs. Each node contains a left subtree pointer, an array of entries
13
+ * (each with a prefix length, key suffix, value CID, and right subtree pointer), and
14
+ * keys are reconstructed by combining the prefix of the previous key with the suffix.
15
+ *
16
+ * @param blocks - Block store that resolves CIDs to raw CBOR bytes
17
+ * @param rootCid - CID of the MST root node
18
+ * @yields {MstEntry} Record entries in lexicographic key order
19
+ */
5
20
  export declare function walkMst(blocks: {
6
21
  get(cid: string): Uint8Array | undefined;
7
22
  }, rootCid: string): Generator<MstEntry>;
package/dist/mst.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mst.d.ts","sourceRoot":"","sources":["../src/mst.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,wBAAiB,OAAO,CAAC,MAAM,EAAE;IAAE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAAA;CAAE,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CA8BnH"}
1
+ {"version":3,"file":"mst.d.ts","sourceRoot":"","sources":["../src/mst.ts"],"names":[],"mappings":"AAEA,4FAA4F;AAC5F,MAAM,WAAW,QAAQ;IACvB,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAA;IACZ,qCAAqC;IACrC,GAAG,EAAE,MAAM,CAAA;CACZ;AAED;;;;;;;;;;;GAWG;AACH,wBAAiB,OAAO,CAAC,MAAM,EAAE;IAAE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAAA;CAAE,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CA+BnH"}
package/dist/mst.js CHANGED
@@ -1,5 +1,18 @@
1
1
  import { cborDecode } from "./cbor.js";
2
+ /**
3
+ * Walk an AT Protocol Merkle Search Tree (MST) in key order, yielding every record entry.
4
+ *
5
+ * The MST is a prefix-compressed B+ tree used by AT Protocol repositories to map
6
+ * record paths to CIDs. Each node contains a left subtree pointer, an array of entries
7
+ * (each with a prefix length, key suffix, value CID, and right subtree pointer), and
8
+ * keys are reconstructed by combining the prefix of the previous key with the suffix.
9
+ *
10
+ * @param blocks - Block store that resolves CIDs to raw CBOR bytes
11
+ * @param rootCid - CID of the MST root node
12
+ * @yields {MstEntry} Record entries in lexicographic key order
13
+ */
2
14
  export function* walkMst(blocks, rootCid) {
15
+ /** Recursively visit an MST node, reconstructing full keys from prefix-compressed entries. */
3
16
  function* visit(cid, prefix) {
4
17
  const data = blocks.get(cid);
5
18
  if (!data)
@@ -1 +1 @@
1
- {"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/oauth/db.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,SAAS,87CA0DrB,CAAA;AAID,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAIzG;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAO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,CAGjE;AAED,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE9D;AAID,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,MAAM,EACb,IAAI,EAAE;IACJ,QAAQ,EAAE,MAAM,CAAA;IAChB,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,GACA,OAAO,CAAC,IAAI,CAAC,CAQf;AAED,wBAAsB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAGxE;AAED,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAErE;AAID,wBAAsB,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAK3F;AAED,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAQzD"}
package/dist/oauth/db.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // packages/hatk/src/oauth/db.ts
2
- import { querySQL, runSQL } from "../db.js";
2
+ import { querySQL, runSQL } from "../database/db.js";
3
3
  // --- DDL ---
4
4
  export const OAUTH_DDL = `
5
5
  CREATE TABLE IF NOT EXISTS _oauth_keys (
@@ -68,12 +68,32 @@ export async function getServerKey(kid) {
68
68
  return { privateKey: rows[0].private_key, publicKey: rows[0].public_key };
69
69
  }
70
70
  export async function storeServerKey(kid, privateKey, publicKey) {
71
- await runSQL('INSERT OR REPLACE INTO _oauth_keys (kid, private_key, public_key) VALUES ($1, $2, $3)', kid, privateKey, publicKey);
71
+ await runSQL('INSERT OR REPLACE INTO _oauth_keys (kid, private_key, public_key) VALUES ($1, $2, $3)', [
72
+ kid,
73
+ privateKey,
74
+ publicKey,
75
+ ]);
72
76
  }
73
77
  // --- OAuth Request Storage ---
74
78
  export async function storeOAuthRequest(requestUri, data) {
75
79
  await runSQL(`INSERT INTO _oauth_requests (request_uri, client_id, redirect_uri, scope, state, code_challenge, code_challenge_method, dpop_jkt, pds_request_uri, pds_auth_server, pds_code_verifier, pds_state, did, login_hint, expires_at)
76
- VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15)`, requestUri, data.clientId, data.redirectUri, data.scope || null, data.state || null, data.codeChallenge, data.codeChallengeMethod || 'S256', data.dpopJkt, data.pdsRequestUri || null, data.pdsAuthServer || null, data.pdsCodeVerifier || null, data.pdsState || null, data.did || null, data.loginHint || null, data.expiresAt);
80
+ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15)`, [
81
+ requestUri,
82
+ data.clientId,
83
+ data.redirectUri,
84
+ data.scope || null,
85
+ data.state || null,
86
+ data.codeChallenge,
87
+ data.codeChallengeMethod || 'S256',
88
+ data.dpopJkt,
89
+ data.pdsRequestUri || null,
90
+ data.pdsAuthServer || null,
91
+ data.pdsCodeVerifier || null,
92
+ data.pdsState || null,
93
+ data.did || null,
94
+ data.loginHint || null,
95
+ data.expiresAt,
96
+ ]);
77
97
  }
78
98
  export async function getOAuthRequest(requestUri) {
79
99
  const rows = await querySQL('SELECT * FROM _oauth_requests WHERE request_uri = $1 AND expires_at > $2', [
@@ -83,57 +103,63 @@ export async function getOAuthRequest(requestUri) {
83
103
  return rows.length > 0 ? rows[0] : null;
84
104
  }
85
105
  export async function deleteOAuthRequest(requestUri) {
86
- await runSQL('DELETE FROM _oauth_requests WHERE request_uri = $1', requestUri);
106
+ await runSQL('DELETE FROM _oauth_requests WHERE request_uri = $1', [requestUri]);
87
107
  }
88
108
  // --- Authorization Codes ---
89
109
  export async function storeAuthCode(code, requestUri) {
90
- await runSQL('INSERT INTO _oauth_codes (code, request_uri, created_at) VALUES ($1, $2, $3)', code, requestUri, Math.floor(Date.now() / 1000));
110
+ await runSQL('INSERT INTO _oauth_codes (code, request_uri, created_at) VALUES ($1, $2, $3)', [
111
+ code,
112
+ requestUri,
113
+ Math.floor(Date.now() / 1000),
114
+ ]);
91
115
  }
92
116
  export async function consumeAuthCode(code) {
93
117
  const rows = await querySQL('SELECT request_uri FROM _oauth_codes WHERE code = $1', [code]);
94
118
  if (rows.length === 0)
95
119
  return null;
96
- await runSQL('DELETE FROM _oauth_codes WHERE code = $1', code);
120
+ await runSQL('DELETE FROM _oauth_codes WHERE code = $1', [code]);
97
121
  return rows[0].request_uri;
98
122
  }
99
123
  // --- Sessions ---
100
124
  export async function storeSession(did, data) {
101
125
  await runSQL(`INSERT OR REPLACE INTO _oauth_sessions (did, pds_endpoint, access_token, refresh_token, dpop_jkt, token_expires_at, updated_at)
102
- VALUES ($1,$2,$3,$4,$5,$6,CURRENT_TIMESTAMP)`, did, data.pdsEndpoint, data.accessToken, data.refreshToken || null, data.dpopJkt, data.tokenExpiresAt || null);
126
+ VALUES ($1,$2,$3,$4,$5,$6,CURRENT_TIMESTAMP)`, [did, data.pdsEndpoint, data.accessToken, data.refreshToken || null, data.dpopJkt, data.tokenExpiresAt || null]);
103
127
  }
104
128
  export async function getSession(did) {
105
129
  const rows = await querySQL('SELECT * FROM _oauth_sessions WHERE did = $1', [did]);
106
130
  return rows.length > 0 ? rows[0] : null;
107
131
  }
108
132
  export async function deleteSession(did) {
109
- await runSQL('DELETE FROM _oauth_sessions WHERE did = $1', did);
133
+ await runSQL('DELETE FROM _oauth_sessions WHERE did = $1', [did]);
110
134
  }
111
135
  // --- Refresh Tokens ---
112
136
  export async function storeRefreshToken(token, data) {
113
137
  const now = Math.floor(Date.now() / 1000);
114
138
  const expiresAt = data.expiresAt ?? now + 14 * 86400; // 14 days default
115
139
  await runSQL(`INSERT INTO _oauth_refresh_tokens (token, client_id, did, dpop_jkt, scope, created_at, expires_at)
116
- VALUES ($1,$2,$3,$4,$5,$6,$7)`, token, data.clientId, data.did, data.dpopJkt, data.scope || null, now, expiresAt);
140
+ VALUES ($1,$2,$3,$4,$5,$6,$7)`, [token, data.clientId, data.did, data.dpopJkt, data.scope || null, now, expiresAt]);
117
141
  }
118
142
  export async function getRefreshToken(token) {
119
143
  const rows = await querySQL('SELECT * FROM _oauth_refresh_tokens WHERE token = $1', [token]);
120
144
  return rows.length > 0 ? rows[0] : null;
121
145
  }
122
146
  export async function revokeRefreshToken(token) {
123
- await runSQL('UPDATE _oauth_refresh_tokens SET revoked = 1 WHERE token = $1', token);
147
+ await runSQL('UPDATE _oauth_refresh_tokens SET revoked = 1 WHERE token = $1', [token]);
124
148
  }
125
149
  // --- DPoP JTI Replay Protection ---
126
150
  export async function checkAndStoreDpopJti(jti, expiresAt) {
127
151
  const rows = await querySQL('SELECT 1 FROM _oauth_dpop_jtis WHERE jti = $1', [jti]);
128
152
  if (rows.length > 0)
129
153
  return false;
130
- await runSQL('INSERT INTO _oauth_dpop_jtis (jti, expires_at) VALUES ($1, $2)', jti, expiresAt);
154
+ await runSQL('INSERT INTO _oauth_dpop_jtis (jti, expires_at) VALUES ($1, $2)', [jti, expiresAt]);
131
155
  return true;
132
156
  }
133
157
  export async function cleanupExpiredOAuth() {
134
158
  const now = Math.floor(Date.now() / 1000);
135
- await runSQL('DELETE FROM _oauth_dpop_jtis WHERE expires_at < $1', now);
136
- await runSQL('DELETE FROM _oauth_requests WHERE expires_at < $1', now);
137
- await runSQL('DELETE FROM _oauth_codes WHERE created_at < $1', now - 600);
138
- await runSQL('DELETE FROM _oauth_refresh_tokens WHERE revoked = 1 OR (expires_at IS NOT NULL AND expires_at < $1)', now);
159
+ await runSQL('DELETE FROM _oauth_dpop_jtis WHERE expires_at < $1', [now]);
160
+ await runSQL('DELETE FROM _oauth_requests WHERE expires_at < $1', [now]);
161
+ await runSQL('DELETE FROM _oauth_codes WHERE created_at < $1', [now - 600]);
162
+ await runSQL('DELETE FROM _oauth_refresh_tokens WHERE revoked = 1 OR (expires_at IS NOT NULL AND expires_at < $1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