@hatk/hatk 0.0.1-alpha.6 → 0.0.1-alpha.60

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 (163) hide show
  1. package/dist/adapter.d.ts +19 -0
  2. package/dist/adapter.d.ts.map +1 -0
  3. package/dist/adapter.js +108 -0
  4. package/dist/backfill.d.ts +2 -2
  5. package/dist/backfill.d.ts.map +1 -1
  6. package/dist/backfill.js +78 -31
  7. package/dist/car.d.ts +42 -10
  8. package/dist/car.d.ts.map +1 -1
  9. package/dist/car.js +154 -14
  10. package/dist/cli.js +243 -1043
  11. package/dist/config.d.ts +31 -1
  12. package/dist/config.d.ts.map +1 -1
  13. package/dist/config.js +40 -9
  14. package/dist/database/adapter-factory.d.ts +6 -0
  15. package/dist/database/adapter-factory.d.ts.map +1 -0
  16. package/dist/database/adapter-factory.js +20 -0
  17. package/dist/database/adapters/duckdb-search.d.ts +12 -0
  18. package/dist/database/adapters/duckdb-search.d.ts.map +1 -0
  19. package/dist/database/adapters/duckdb-search.js +27 -0
  20. package/dist/database/adapters/duckdb.d.ts +25 -0
  21. package/dist/database/adapters/duckdb.d.ts.map +1 -0
  22. package/dist/database/adapters/duckdb.js +161 -0
  23. package/dist/database/adapters/sqlite-search.d.ts +23 -0
  24. package/dist/database/adapters/sqlite-search.d.ts.map +1 -0
  25. package/dist/database/adapters/sqlite-search.js +74 -0
  26. package/dist/database/adapters/sqlite.d.ts +18 -0
  27. package/dist/database/adapters/sqlite.d.ts.map +1 -0
  28. package/dist/database/adapters/sqlite.js +88 -0
  29. package/dist/{db.d.ts → database/db.d.ts} +57 -6
  30. package/dist/database/db.d.ts.map +1 -0
  31. package/dist/{db.js → database/db.js} +730 -549
  32. package/dist/database/dialect.d.ts +45 -0
  33. package/dist/database/dialect.d.ts.map +1 -0
  34. package/dist/database/dialect.js +72 -0
  35. package/dist/{fts.d.ts → database/fts.d.ts} +7 -0
  36. package/dist/database/fts.d.ts.map +1 -0
  37. package/dist/{fts.js → database/fts.js} +116 -32
  38. package/dist/database/index.d.ts +7 -0
  39. package/dist/database/index.d.ts.map +1 -0
  40. package/dist/database/index.js +6 -0
  41. package/dist/database/ports.d.ts +50 -0
  42. package/dist/database/ports.d.ts.map +1 -0
  43. package/dist/database/ports.js +1 -0
  44. package/dist/{schema.d.ts → database/schema.d.ts} +14 -3
  45. package/dist/database/schema.d.ts.map +1 -0
  46. package/dist/{schema.js → database/schema.js} +81 -41
  47. package/dist/dev-entry.d.ts +8 -0
  48. package/dist/dev-entry.d.ts.map +1 -0
  49. package/dist/dev-entry.js +112 -0
  50. package/dist/feeds.d.ts +12 -8
  51. package/dist/feeds.d.ts.map +1 -1
  52. package/dist/feeds.js +51 -6
  53. package/dist/hooks.d.ts +85 -0
  54. package/dist/hooks.d.ts.map +1 -0
  55. package/dist/hooks.js +161 -0
  56. package/dist/hydrate.d.ts +7 -6
  57. package/dist/hydrate.d.ts.map +1 -1
  58. package/dist/hydrate.js +4 -16
  59. package/dist/indexer.d.ts +22 -0
  60. package/dist/indexer.d.ts.map +1 -1
  61. package/dist/indexer.js +123 -32
  62. package/dist/labels.d.ts +36 -0
  63. package/dist/labels.d.ts.map +1 -1
  64. package/dist/labels.js +71 -6
  65. package/dist/lexicon-resolve.d.ts.map +1 -1
  66. package/dist/lexicon-resolve.js +27 -112
  67. package/dist/lexicons/com/atproto/label/defs.json +75 -0
  68. package/dist/lexicons/com/atproto/moderation/defs.json +30 -0
  69. package/dist/lexicons/com/atproto/repo/strongRef.json +24 -0
  70. package/dist/lexicons/dev/hatk/applyWrites.json +87 -0
  71. package/dist/lexicons/dev/hatk/createRecord.json +40 -0
  72. package/dist/lexicons/dev/hatk/createReport.json +48 -0
  73. package/dist/lexicons/dev/hatk/deleteRecord.json +25 -0
  74. package/dist/lexicons/dev/hatk/describeCollections.json +41 -0
  75. package/dist/lexicons/dev/hatk/describeFeeds.json +29 -0
  76. package/dist/lexicons/dev/hatk/describeLabels.json +45 -0
  77. package/dist/lexicons/dev/hatk/getFeed.json +30 -0
  78. package/dist/lexicons/dev/hatk/getPreferences.json +19 -0
  79. package/dist/lexicons/dev/hatk/getRecord.json +26 -0
  80. package/dist/lexicons/dev/hatk/getRecords.json +32 -0
  81. package/dist/lexicons/dev/hatk/putPreference.json +28 -0
  82. package/dist/lexicons/dev/hatk/putRecord.json +41 -0
  83. package/dist/lexicons/dev/hatk/searchRecords.json +32 -0
  84. package/dist/lexicons/dev/hatk/uploadBlob.json +23 -0
  85. package/dist/logger.d.ts +29 -0
  86. package/dist/logger.d.ts.map +1 -1
  87. package/dist/logger.js +29 -0
  88. package/dist/main.js +137 -67
  89. package/dist/mst.d.ts +18 -1
  90. package/dist/mst.d.ts.map +1 -1
  91. package/dist/mst.js +19 -8
  92. package/dist/oauth/db.d.ts +3 -1
  93. package/dist/oauth/db.d.ts.map +1 -1
  94. package/dist/oauth/db.js +48 -19
  95. package/dist/oauth/server.d.ts +24 -0
  96. package/dist/oauth/server.d.ts.map +1 -1
  97. package/dist/oauth/server.js +198 -22
  98. package/dist/oauth/session.d.ts +11 -0
  99. package/dist/oauth/session.d.ts.map +1 -0
  100. package/dist/oauth/session.js +65 -0
  101. package/dist/opengraph.d.ts +10 -0
  102. package/dist/opengraph.d.ts.map +1 -1
  103. package/dist/opengraph.js +80 -40
  104. package/dist/pds-proxy.d.ts +60 -0
  105. package/dist/pds-proxy.d.ts.map +1 -0
  106. package/dist/pds-proxy.js +277 -0
  107. package/dist/push.d.ts +34 -0
  108. package/dist/push.d.ts.map +1 -0
  109. package/dist/push.js +184 -0
  110. package/dist/renderer.d.ts +27 -0
  111. package/dist/renderer.d.ts.map +1 -0
  112. package/dist/renderer.js +46 -0
  113. package/dist/resolve-hatk.d.ts +6 -0
  114. package/dist/resolve-hatk.d.ts.map +1 -0
  115. package/dist/resolve-hatk.js +20 -0
  116. package/dist/response.d.ts +16 -0
  117. package/dist/response.d.ts.map +1 -0
  118. package/dist/response.js +69 -0
  119. package/dist/scanner.d.ts +21 -0
  120. package/dist/scanner.d.ts.map +1 -0
  121. package/dist/scanner.js +88 -0
  122. package/dist/seed.d.ts +19 -0
  123. package/dist/seed.d.ts.map +1 -1
  124. package/dist/seed.js +43 -4
  125. package/dist/server-init.d.ts +8 -0
  126. package/dist/server-init.d.ts.map +1 -0
  127. package/dist/server-init.js +62 -0
  128. package/dist/server.d.ts +26 -3
  129. package/dist/server.d.ts.map +1 -1
  130. package/dist/server.js +629 -635
  131. package/dist/setup.d.ts +28 -1
  132. package/dist/setup.d.ts.map +1 -1
  133. package/dist/setup.js +50 -3
  134. package/dist/templates/feed.tpl +14 -0
  135. package/dist/templates/hook.tpl +5 -0
  136. package/dist/templates/label.tpl +15 -0
  137. package/dist/templates/og.tpl +17 -0
  138. package/dist/templates/seed.tpl +11 -0
  139. package/dist/templates/setup.tpl +5 -0
  140. package/dist/templates/test-feed.tpl +19 -0
  141. package/dist/templates/test-xrpc.tpl +19 -0
  142. package/dist/templates/xrpc.tpl +41 -0
  143. package/dist/test.d.ts +1 -1
  144. package/dist/test.d.ts.map +1 -1
  145. package/dist/test.js +39 -32
  146. package/dist/views.js +1 -1
  147. package/dist/vite-plugin.d.ts +1 -1
  148. package/dist/vite-plugin.d.ts.map +1 -1
  149. package/dist/vite-plugin.js +254 -66
  150. package/dist/xrpc.d.ts +75 -11
  151. package/dist/xrpc.d.ts.map +1 -1
  152. package/dist/xrpc.js +189 -39
  153. package/package.json +14 -7
  154. package/public/admin.html +133 -54
  155. package/dist/db.d.ts.map +0 -1
  156. package/dist/fts.d.ts.map +0 -1
  157. package/dist/oauth/hooks.d.ts +0 -10
  158. package/dist/oauth/hooks.d.ts.map +0 -1
  159. package/dist/oauth/hooks.js +0 -40
  160. package/dist/schema.d.ts.map +0 -1
  161. package/dist/test-browser.d.ts +0 -14
  162. package/dist/test-browser.d.ts.map +0 -1
  163. package/dist/test-browser.js +0 -26
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot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A;CAAE,EAClC,OAAO,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,GAC3C;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,WAAW,CAAC;IAAC,WAAW,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,CAAA;AAkB9G;;;GAGG;AACH,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQrE;AASD,qDAAqD;AACrD,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,IAAI,CASlF;AAED,iFAAiF;AACjF,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CA8BjG;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,KAAK,CAAC;IAC7C,MAAM,EAAE,QAAQ,GAAG,QAAQ,CAAA;IAC3B,UAAU,EAAE,MAAM,CAAA;IAClB,GAAG,EAAE,MAAM,CAAA;IACX,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAA;CACnC,CAAC,GAAG,IAAI,CA2BR"}
package/dist/hooks.js ADDED
@@ -0,0 +1,161 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ /**
10
+ * Lifecycle hooks that run in response to server events.
11
+ *
12
+ * Place hook modules in the `hooks/` directory. Currently supported hooks:
13
+ *
14
+ * - `on-login.ts` — called after each successful OAuth login
15
+ * - `on-commit-*.ts` — called after records are indexed from the firehose
16
+ *
17
+ * Each hook default-exports the result of `defineHook()`.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * // hooks/on-login.ts
22
+ * import { defineHook } from '$hatk'
23
+ *
24
+ * export default defineHook("on-login", async (ctx) => {
25
+ * await ctx.ensureRepo(ctx.did)
26
+ * })
27
+ * ```
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * // hooks/on-commit-favorite.ts
32
+ * import { defineHook } from '$hatk'
33
+ *
34
+ * export default defineHook("on-commit", { collections: ["social.grain.favorite"] },
35
+ * async ({ action, collection, record, repo, uri, db, lookup, push }) => {
36
+ * if (action !== "create") return
37
+ * // send push notification, etc.
38
+ * }
39
+ * )
40
+ * ```
41
+ */
42
+ import { existsSync } from 'node:fs';
43
+ import { resolve } from 'node:path';
44
+ import { pdsCreateRecord, pdsPutRecord, pdsDeleteRecord } from "./pds-proxy.js";
45
+ import { log, emit } from "./logger.js";
46
+ import { setRepoStatus, runSQL } from "./database/db.js";
47
+ import { triggerAutoBackfill, awaitBackfill } from "./indexer.js";
48
+ import { buildBaseContext } from "./hydrate.js";
49
+ import { buildPushInterface, isPushEnabled } from "./push.js";
50
+ export function defineHook(event, ...args) {
51
+ if (event === 'on-login') {
52
+ return { __type: 'hook', event, handler: args[0] };
53
+ }
54
+ if (event === 'on-commit') {
55
+ const options = args[0];
56
+ const handler = args[1];
57
+ return { __type: 'hook', event, collections: options.collections, handler };
58
+ }
59
+ throw new Error(`Unknown hook event: ${event}`);
60
+ }
61
+ let onLoginHook = null;
62
+ const onCommitHooks = [];
63
+ /**
64
+ * Discover and load the on-login hook from the project's `hooks/` directory.
65
+ * Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
66
+ */
67
+ export async function loadOnLoginHook(hooksDir) {
68
+ const tsPath = resolve(hooksDir, 'on-login.ts');
69
+ const jsPath = resolve(hooksDir, 'on-login.js');
70
+ const path = existsSync(tsPath) ? tsPath : existsSync(jsPath) ? jsPath : null;
71
+ if (!path)
72
+ return;
73
+ const mod = await import(__rewriteRelativeImportExtension(/* @vite-ignore */ `${path}?t=${Date.now()}`));
74
+ onLoginHook = mod.default;
75
+ log('[hooks] on-login hook loaded');
76
+ }
77
+ /** Mark a DID as pending, trigger auto-backfill, and wait for completion. */
78
+ async function ensureRepo(did) {
79
+ await setRepoStatus(did, 'pending');
80
+ triggerAutoBackfill(did);
81
+ await awaitBackfill(did);
82
+ }
83
+ /** Register a hook from a scanned server/ module. */
84
+ export function registerHook(event, handler, options) {
85
+ if (event === 'on-login') {
86
+ onLoginHook = handler;
87
+ log('[hooks] on-login hook registered');
88
+ }
89
+ else if (event === 'on-commit') {
90
+ const collections = new Set(options?.collections || []);
91
+ onCommitHooks.push({ collections, handler: handler });
92
+ log(`[hooks] on-commit hook registered (collections: ${[...collections].join(', ')})`);
93
+ }
94
+ }
95
+ /** Fire the on-login hook if loaded. Errors are logged but never block login. */
96
+ export async function fireOnLoginHook(did, oauthConfig) {
97
+ if (!onLoginHook)
98
+ return;
99
+ try {
100
+ const base = buildBaseContext({ did });
101
+ const viewer = { did };
102
+ const hookPromise = onLoginHook({
103
+ ...base,
104
+ did,
105
+ db: { query: base.db.query, run: runSQL },
106
+ ensureRepo,
107
+ createRecord: async (collection, record, opts) => {
108
+ if (!oauthConfig)
109
+ throw new Error('No OAuth config — cannot write to PDS');
110
+ return pdsCreateRecord(oauthConfig, viewer, { collection, record, rkey: opts?.rkey });
111
+ },
112
+ putRecord: async (collection, rkey, record) => {
113
+ if (!oauthConfig)
114
+ throw new Error('No OAuth config — cannot write to PDS');
115
+ return pdsPutRecord(oauthConfig, viewer, { collection, rkey, record });
116
+ },
117
+ deleteRecord: async (collection, rkey) => {
118
+ if (!oauthConfig)
119
+ throw new Error('No OAuth config — cannot write to PDS');
120
+ await pdsDeleteRecord(oauthConfig, viewer, { collection, rkey });
121
+ },
122
+ });
123
+ const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error('on-login hook timed out after 30s')), 30_000));
124
+ await Promise.race([hookPromise, timeout]);
125
+ }
126
+ catch (err) {
127
+ emit('hooks', 'on_login_error', { did, error: err.message });
128
+ }
129
+ }
130
+ /**
131
+ * Fire on-commit hooks for a batch of indexed records.
132
+ * Runs async and non-blocking — errors are logged but never throw.
133
+ */
134
+ export function fireOnCommitHooks(items) {
135
+ if (onCommitHooks.length === 0)
136
+ return;
137
+ const base = buildBaseContext(null);
138
+ const push = isPushEnabled() ? buildPushInterface() : { send: async () => { } };
139
+ for (const item of items) {
140
+ for (const hook of onCommitHooks) {
141
+ if (hook.collections.size > 0 && !hook.collections.has(item.collection))
142
+ continue;
143
+ hook.handler({
144
+ action: item.action,
145
+ collection: item.collection,
146
+ record: item.record,
147
+ repo: item.authorDid,
148
+ uri: item.uri,
149
+ db: { query: base.db.query, run: runSQL },
150
+ lookup: base.lookup,
151
+ push,
152
+ }).catch((err) => {
153
+ emit('hooks', 'on_commit_error', {
154
+ collection: item.collection,
155
+ uri: item.uri,
156
+ error: err.message,
157
+ });
158
+ });
159
+ }
160
+ }
161
+ }
package/dist/hydrate.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { Row } from './lex-types.ts';
2
2
  export type { Row };
3
- export interface HydrateContext<T = unknown> {
4
- items: Row<T>[];
3
+ export interface BaseContext {
5
4
  viewer: {
6
5
  did: string;
6
+ handle?: string;
7
7
  } | null;
8
8
  db: {
9
9
  query: (sql: string, params?: unknown[]) => Promise<unknown[]>;
@@ -12,12 +12,13 @@ export interface HydrateContext<T = unknown> {
12
12
  lookup: <R = unknown>(collection: string, field: string, values: string[]) => Promise<Map<string, Row<R>>>;
13
13
  count: (collection: string, field: string, values: string[]) => Promise<Map<string, number>>;
14
14
  labels: (uris: string[]) => Promise<Map<string, unknown[]>>;
15
- blobUrl: (did: string, ref: unknown, preset?: 'avatar' | 'banner' | 'feed_thumbnail' | 'feed_fullsize') => string | undefined;
15
+ blobUrl: (did: string, ref: unknown, preset?: string) => string | undefined;
16
16
  }
17
17
  /** Fetch records for URIs, reshape them, and filter out taken-down DIDs. */
18
18
  export declare function resolveRecords(uris: string[]): Promise<Row<unknown>[]>;
19
- /** Build a HydrateContext for a feed's hydrate function. */
20
- export declare function buildHydrateContext(items: Row<unknown>[], viewer: {
19
+ /** Build a BaseContext for hydration. */
20
+ export declare function buildBaseContext(viewer: {
21
21
  did: string;
22
- } | null): HydrateContext;
22
+ handle?: string;
23
+ } | null): BaseContext;
23
24
  //# sourceMappingURL=hydrate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hydrate.d.ts","sourceRoot":"","sources":["../src/hydrate.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AAEzC,YAAY,EAAE,GAAG,EAAE,CAAA;AAInB,MAAM,WAAW,cAAc,CAAC,CAAC,GAAG,OAAO;IACzC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,CAAA;IACf,MAAM,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAA;IAC9B,EAAE,EAAE;QAAE,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC,CAAA;KAAE,CAAA;IACtE,UAAU,EAAE,CAAC,CAAC,GAAG,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7F,MAAM,EAAE,CAAC,CAAC,GAAG,OAAO,EAAE,UAAU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ate.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getRecordsByUris, countByFieldBatch, lookupByFieldBatch, querySQL, reshapeRow, queryLabelsForUris, filterTakendownDids, } from "./db.js";
1
+ import { getRecordsMap, countByFieldBatch, lookupByFieldBatch, querySQL, queryLabelsForUris, filterTakendownDids, getRecordsByUris, reshapeRow, } from "./database/db.js";
2
2
  import { blobUrl } from "./xrpc.js";
3
3
  // --- Record Resolution ---
4
4
  /** Fetch records for URIs, reshape them, and filter out taken-down DIDs. */
@@ -39,24 +39,12 @@ export async function resolveRecords(uris) {
39
39
  .filter((r) => r != null);
40
40
  }
41
41
  // --- Context Builder ---
42
- /** Build a HydrateContext for a feed's hydrate function. */
43
- export function buildHydrateContext(items, viewer) {
42
+ /** Build a BaseContext for hydration. */
43
+ export function buildBaseContext(viewer) {
44
44
  return {
45
- items,
46
45
  viewer,
47
46
  db: { query: querySQL },
48
- getRecords: async (collection, uris) => {
49
- if (uris.length === 0)
50
- return new Map();
51
- const records = await getRecordsByUris(collection, uris);
52
- const map = new Map();
53
- for (const r of records) {
54
- const shaped = reshapeRow(r, r?.__childData, r?.__unionData);
55
- if (shaped)
56
- map.set(shaped.uri, shaped);
57
- }
58
- return map;
59
- },
47
+ getRecords: getRecordsMap,
60
48
  lookup: async (collection, field, values) => {
61
49
  if (values.length === 0)
62
50
  return new Map();
package/dist/indexer.d.ts CHANGED
@@ -1,4 +1,15 @@
1
+ /**
2
+ * Auto-backfill a DID's repo when first seen on the firehose.
3
+ *
4
+ * Fetches the full repo via CAR export, inserts all records, then replays any
5
+ * firehose events that arrived during the backfill. Concurrency is capped at
6
+ * `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
7
+ * to `maxRetries`.
8
+ */
9
+ /** Wait for a DID's backfill to complete if one is in flight. */
10
+ export declare function awaitBackfill(did: string): Promise<void>;
1
11
  export declare function triggerAutoBackfill(did: string, attempt?: number): Promise<void>;
12
+ /** Configuration for the firehose indexer. */
2
13
  interface IndexerOpts {
3
14
  relayUrl: string;
4
15
  collections: Set<string>;
@@ -7,8 +18,19 @@ interface IndexerOpts {
7
18
  cursor?: string | null;
8
19
  fetchTimeout: number;
9
20
  maxRetries: number;
21
+ parallelism?: number;
10
22
  ftsRebuildInterval?: number;
11
23
  }
24
+ /**
25
+ * Connect to the AT Protocol relay firehose and begin indexing.
26
+ *
27
+ * Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
28
+ * on the event loop to minimize backpressure, and batches writes through
29
+ * {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
30
+ * Reconnects automatically on disconnect after a 3s delay.
31
+ *
32
+ * @returns The WebSocket connection (for shutdown coordination)
33
+ */
12
34
  export declare function startIndexer(opts: IndexerOpts): Promise<WebSocket>;
13
35
  export {};
14
36
  //# sourceMappingURL=indexer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"AAkIA,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,SAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAsDjF;AAED,UAAU,WAAW;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC/B,WAAW,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACzB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,MAAM,CAAA;IAClB,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAyBD,wBAAsB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAkDxE"}
1
+ {"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"AAmKA;;;;;;;GAOG;AACH,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGxD;AAED,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,SAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CA4EjF;AAED,8CAA8C;AAC9C,UAAU,WAAW;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC/B,WAAW,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACzB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAyBD;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAmDxE"}
package/dist/indexer.js CHANGED
@@ -1,11 +1,12 @@
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, getDatabasePort, updateRepoHandle, } 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 { fireOnCommitHooks } from "./hooks.js";
9
+ import { getLexiconArray } from "./database/schema.js";
9
10
  import { validateRecord } from '@bigmoves/lexicon';
10
11
  let buffer = [];
11
12
  let flushTimer = null;
@@ -18,7 +19,8 @@ let ftsRebuildInterval = 500;
18
19
  const pendingBuffers = new Map();
19
20
  // Track in-flight backfills to avoid duplicates
20
21
  const backfillInFlight = new Set();
21
- const MAX_CONCURRENT_BACKFILLS = 5;
22
+ const backfillPromises = new Map();
23
+ const pendingReschedule = new Set();
22
24
  // In-memory cache of repo status to avoid flooding the DB read queue
23
25
  const repoStatusCache = new Map();
24
26
  // Set by startIndexer
@@ -27,6 +29,12 @@ let indexerSignalCollections;
27
29
  let indexerPinnedRepos = null;
28
30
  let indexerFetchTimeout;
29
31
  let indexerMaxRetries;
32
+ let maxConcurrentBackfills = 3;
33
+ /**
34
+ * Flush the write buffer — insert all buffered records, update the relay cursor,
35
+ * run label rules on inserted records, and trigger FTS rebuilds when the write
36
+ * threshold is reached. Emits a wide event with batch stats.
37
+ */
30
38
  async function flushBuffer() {
31
39
  if (buffer.length === 0)
32
40
  return;
@@ -64,6 +72,14 @@ async function flushBuffer() {
64
72
  value: item.record,
65
73
  }).catch(() => { });
66
74
  }
75
+ // Fire on-commit hooks for inserted records (async, non-blocking)
76
+ fireOnCommitHooks(inserted.map((item) => ({
77
+ action: 'create',
78
+ collection: item.collection,
79
+ uri: item.uri,
80
+ authorDid: item.authorDid,
81
+ record: item.record,
82
+ })));
67
83
  // Aggregate collection counts and unique DIDs for wide event
68
84
  const collections = {};
69
85
  const dids = new Set();
@@ -86,9 +102,14 @@ async function flushBuffer() {
86
102
  writesSinceRebuild += batch.length;
87
103
  if (writesSinceRebuild >= ftsRebuildInterval) {
88
104
  writesSinceRebuild = 0;
89
- rebuildAllIndexes([...indexerCollections]).catch(() => { });
105
+ // Skip periodic full rebuild for SQLite — it uses incremental FTS updates
106
+ const port = getDatabasePort();
107
+ if (port.dialect !== 'sqlite') {
108
+ rebuildAllIndexes([...indexerCollections]).catch(() => { });
109
+ }
90
110
  }
91
111
  }
112
+ /** Schedule a flush after FLUSH_INTERVAL_MS if one isn't already pending. */
92
113
  function scheduleFlush() {
93
114
  if (flushTimer)
94
115
  return;
@@ -97,6 +118,7 @@ function scheduleFlush() {
97
118
  await flushBuffer();
98
119
  }, FLUSH_INTERVAL_MS);
99
120
  }
121
+ /** Add a record to the write buffer. Flushes immediately if BATCH_SIZE is reached. */
100
122
  function bufferWrite(item) {
101
123
  buffer.push(item);
102
124
  if (buffer.length >= BATCH_SIZE) {
@@ -110,11 +132,39 @@ function bufferWrite(item) {
110
132
  scheduleFlush();
111
133
  }
112
134
  }
135
+ /**
136
+ * Auto-backfill a DID's repo when first seen on the firehose.
137
+ *
138
+ * Fetches the full repo via CAR export, inserts all records, then replays any
139
+ * firehose events that arrived during the backfill. Concurrency is capped at
140
+ * `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
141
+ * to `maxRetries`.
142
+ */
143
+ /** Wait for a DID's backfill to complete if one is in flight. */
144
+ export function awaitBackfill(did) {
145
+ const entry = backfillPromises.get(did);
146
+ return entry ? entry.promise : Promise.resolve();
147
+ }
113
148
  export async function triggerAutoBackfill(did, attempt = 0) {
114
149
  if (backfillInFlight.has(did))
115
150
  return;
151
+ if (backfillInFlight.size >= maxConcurrentBackfills) {
152
+ if (!pendingReschedule.has(did)) {
153
+ pendingReschedule.add(did);
154
+ setTimeout(() => {
155
+ pendingReschedule.delete(did);
156
+ triggerAutoBackfill(did, attempt);
157
+ }, 10_000);
158
+ }
159
+ return;
160
+ }
116
161
  backfillInFlight.add(did);
117
162
  pendingBuffers.set(did, []);
163
+ if (!backfillPromises.has(did)) {
164
+ let resolveBackfill;
165
+ const promise = new Promise((r) => { resolveBackfill = r; });
166
+ backfillPromises.set(did, { promise, resolve: resolveBackfill });
167
+ }
118
168
  if (attempt === 0)
119
169
  await setRepoStatus(did, 'pending');
120
170
  const elapsed = timer();
@@ -154,6 +204,12 @@ export async function triggerAutoBackfill(did, attempt = 0) {
154
204
  error,
155
205
  retry_count: currentRetryCount,
156
206
  });
207
+ // Resolve awaiting callers (e.g. on-login hooks)
208
+ const entry = backfillPromises.get(did);
209
+ if (entry) {
210
+ entry.resolve();
211
+ backfillPromises.delete(did);
212
+ }
157
213
  if (status === 'error' && currentRetryCount < indexerMaxRetries) {
158
214
  const delaySecs = Math.min(currentRetryCount * 60, 3600);
159
215
  const delayMs = Math.max(delaySecs, 60) * 1000;
@@ -162,7 +218,7 @@ export async function triggerAutoBackfill(did, attempt = 0) {
162
218
  }, delayMs);
163
219
  }
164
220
  }
165
- // Periodic memory diagnostics
221
+ /** Emit a memory diagnostics wide event every 30s for observability. */
166
222
  function startMemoryDiagnostics() {
167
223
  setInterval(() => {
168
224
  const mem = process.memoryUsage();
@@ -184,6 +240,16 @@ function startMemoryDiagnostics() {
184
240
  });
185
241
  }, 30_000);
186
242
  }
243
+ /**
244
+ * Connect to the AT Protocol relay firehose and begin indexing.
245
+ *
246
+ * Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
247
+ * on the event loop to minimize backpressure, and batches writes through
248
+ * {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
249
+ * Reconnects automatically on disconnect after a 3s delay.
250
+ *
251
+ * @returns The WebSocket connection (for shutdown coordination)
252
+ */
187
253
  export async function startIndexer(opts) {
188
254
  const { relayUrl, collections, cursor, fetchTimeout } = opts;
189
255
  if (opts.ftsRebuildInterval != null)
@@ -193,6 +259,7 @@ export async function startIndexer(opts) {
193
259
  indexerPinnedRepos = opts.pinnedRepos || null;
194
260
  indexerFetchTimeout = fetchTimeout;
195
261
  indexerMaxRetries = opts.maxRetries;
262
+ maxConcurrentBackfills = opts.parallelism ?? 3;
196
263
  // Pre-populate repo status cache from DB so non-signal updates
197
264
  // (e.g. profile changes) are processed for already-tracked DIDs
198
265
  if (repoStatusCache.size === 0) {
@@ -202,7 +269,7 @@ export async function startIndexer(opts) {
202
269
  }
203
270
  log(`[indexer] Warmed repo status cache with ${statuses.length} entries`);
204
271
  }
205
- startMemoryDiagnostics();
272
+ // startMemoryDiagnostics()
206
273
  let wsUrl = `${relayUrl}/xrpc/com.atproto.sync.subscribeRepos`;
207
274
  if (cursor) {
208
275
  wsUrl += `?cursor=${cursor}`;
@@ -231,9 +298,23 @@ export async function startIndexer(opts) {
231
298
  });
232
299
  return ws;
233
300
  }
301
+ /**
302
+ * Process a single firehose message. Decodes the CBOR header/body, filters
303
+ * for relevant collections, validates records against lexicons, and routes
304
+ * writes to the buffer (or pending buffer if the DID is mid-backfill).
305
+ */
234
306
  function processMessage(bytes, collections) {
235
307
  const header = cborDecode(bytes, 0);
236
308
  const body = cborDecode(bytes, header.offset);
309
+ // Handle identity events (handle changes)
310
+ if (header.value.t === '#identity') {
311
+ const did = body.value.did;
312
+ const handle = body.value.handle;
313
+ if (did && handle && repoStatusCache.has(did)) {
314
+ updateRepoHandle(did, handle).catch(() => { });
315
+ }
316
+ return;
317
+ }
237
318
  if (header.value.op !== 1 || header.value.t !== '#commit')
238
319
  return;
239
320
  if (!body.value.blocks || !body.value.ops)
@@ -264,7 +345,7 @@ function processMessage(bytes, collections) {
264
345
  repoStatusCache.set(did, 'unknown');
265
346
  }
266
347
  if (hasSignalOp && (!indexerPinnedRepos || indexerPinnedRepos.has(did))) {
267
- if (repoStatus === null && backfillInFlight.size < MAX_CONCURRENT_BACKFILLS) {
348
+ if (repoStatus === null && backfillInFlight.size < maxConcurrentBackfills) {
268
349
  repoStatusCache.set(did, 'pending');
269
350
  triggerAutoBackfill(did);
270
351
  }
@@ -283,34 +364,44 @@ function processMessage(bytes, collections) {
283
364
  const uri = `at://${did}/${op.path}`;
284
365
  if (op.action === 'delete') {
285
366
  deleteRecord(collection, uri);
367
+ fireOnCommitHooks([{
368
+ action: 'delete',
369
+ collection,
370
+ uri,
371
+ authorDid: did,
372
+ record: null,
373
+ }]);
286
374
  continue;
287
375
  }
288
- for (const [cid, data] of blocks) {
289
- try {
290
- const { value: record } = cborDecode(data);
291
- if (record?.$type === collection) {
292
- const validationError = validateRecord(getLexiconArray(), collection, record);
293
- if (validationError) {
294
- emit('indexer', 'validation_skip', {
295
- uri,
296
- collection,
297
- path: validationError.path,
298
- error: validationError.message,
299
- });
300
- break;
301
- }
302
- const item = { collection, uri, cid, authorDid: did, record };
303
- // If DID is mid-backfill, buffer instead of writing directly
304
- if (pendingBuffers.has(did)) {
305
- pendingBuffers.get(did).push(item);
306
- }
307
- else {
308
- bufferWrite(item);
309
- }
310
- break;
376
+ const opCid = typeof op.cid === 'string' ? op.cid : op.cid?.$link;
377
+ if (!opCid)
378
+ continue;
379
+ const data = blocks.get(opCid);
380
+ if (!data)
381
+ continue;
382
+ try {
383
+ const { value: record } = cborDecode(data);
384
+ if (record?.$type === collection) {
385
+ const validationError = validateRecord(getLexiconArray(), collection, record);
386
+ if (validationError) {
387
+ emit('indexer', 'validation_skip', {
388
+ uri,
389
+ collection,
390
+ path: validationError.path,
391
+ error: validationError.message,
392
+ });
393
+ continue;
394
+ }
395
+ const item = { collection, uri, cid: opCid, authorDid: did, record };
396
+ // If DID is mid-backfill, buffer instead of writing directly
397
+ if (pendingBuffers.has(did)) {
398
+ pendingBuffers.get(did).push(item);
399
+ }
400
+ else {
401
+ bufferWrite(item);
311
402
  }
312
403
  }
313
- catch { }
314
404
  }
405
+ catch { }
315
406
  }
316
407
  }
package/dist/labels.d.ts CHANGED
@@ -13,7 +13,36 @@ export interface LabelRuleContext {
13
13
  value: Record<string, any>;
14
14
  };
15
15
  }
16
+ export interface LabelModule {
17
+ definition?: LabelDefinition;
18
+ evaluate?: (ctx: LabelRuleContext) => Promise<string[]>;
19
+ }
20
+ export declare function defineLabel(module: LabelModule): {
21
+ definition?: LabelDefinition;
22
+ evaluate?: (ctx: LabelRuleContext) => Promise<string[]>;
23
+ __type: "labels";
24
+ };
25
+ /**
26
+ * Discover and load label rule modules from the `labels/` directory.
27
+ *
28
+ * Each module should default-export an object with an optional `definition`
29
+ * (label metadata like severity and blur behavior) and an optional `evaluate`
30
+ * function that returns label values to apply to a record.
31
+ *
32
+ * @param labelsDir - Absolute path to the `labels/` directory
33
+ */
16
34
  export declare function initLabels(labelsDir: string): Promise<void>;
35
+ /** Clear all registered label definitions and rules (for hot-reload). */
36
+ export declare function clearLabels(): void;
37
+ /** Register a single label module from a scanned server/ module. */
38
+ export declare function registerLabelModule(name: string, labelMod: {
39
+ definition?: LabelDefinition;
40
+ evaluate?: (ctx: LabelRuleContext) => Promise<string[]>;
41
+ }): void;
42
+ /**
43
+ * Evaluate all loaded label rules against a record and persist any resulting labels.
44
+ * Called after each record is indexed. Rule errors are logged but never block indexing.
45
+ */
17
46
  export declare function runLabelRules(record: {
18
47
  uri: string;
19
48
  cid: string;
@@ -21,9 +50,16 @@ export declare function runLabelRules(record: {
21
50
  collection: string;
22
51
  value: Record<string, any>;
23
52
  }): Promise<void>;
53
+ /**
54
+ * Re-evaluate all label rules against every existing record in the given collections.
55
+ * Used by `/admin/rescan-labels` to apply new or updated rules retroactively.
56
+ *
57
+ * @returns Count of records scanned and new labels applied
58
+ */
24
59
  export declare function rescanLabels(collections: string[]): Promise<{
25
60
  scanned: number;
26
61
  labeled: number;
27
62
  }>;
63
+ /** Return all label definitions discovered during {@link initLabels}. */
28
64
  export declare function getLabelDefinitions(): LabelDefinition[];
29
65
  //# 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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