@hatk/hatk 0.0.1-alpha.7 → 0.0.1-alpha.71

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 (172) 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 +21 -2
  5. package/dist/backfill.d.ts.map +1 -1
  6. package/dist/backfill.js +116 -43
  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 +46 -1
  12. package/dist/config.d.ts.map +1 -1
  13. package/dist/config.js +42 -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} +755 -552
  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 +119 -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 +163 -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 +109 -3
  60. package/dist/indexer.d.ts.map +1 -1
  61. package/dist/indexer.js +338 -66
  62. package/dist/jetstream.d.ts +82 -0
  63. package/dist/jetstream.d.ts.map +1 -0
  64. package/dist/jetstream.js +161 -0
  65. package/dist/labels.d.ts +36 -0
  66. package/dist/labels.d.ts.map +1 -1
  67. package/dist/labels.js +71 -6
  68. package/dist/lexicon-resolve.d.ts.map +1 -1
  69. package/dist/lexicon-resolve.js +27 -112
  70. package/dist/lexicons/com/atproto/label/defs.json +75 -0
  71. package/dist/lexicons/com/atproto/moderation/defs.json +30 -0
  72. package/dist/lexicons/com/atproto/repo/strongRef.json +21 -0
  73. package/dist/lexicons/dev/hatk/applyWrites.json +87 -0
  74. package/dist/lexicons/dev/hatk/createRecord.json +40 -0
  75. package/dist/lexicons/dev/hatk/createReport.json +48 -0
  76. package/dist/lexicons/dev/hatk/deleteRecord.json +25 -0
  77. package/dist/lexicons/dev/hatk/describeCollections.json +41 -0
  78. package/dist/lexicons/dev/hatk/describeFeeds.json +29 -0
  79. package/dist/lexicons/dev/hatk/describeLabels.json +45 -0
  80. package/dist/lexicons/dev/hatk/getFeed.json +30 -0
  81. package/dist/lexicons/dev/hatk/getPreferences.json +19 -0
  82. package/dist/lexicons/dev/hatk/getRecord.json +26 -0
  83. package/dist/lexicons/dev/hatk/getRecords.json +32 -0
  84. package/dist/lexicons/dev/hatk/putPreference.json +28 -0
  85. package/dist/lexicons/dev/hatk/putRecord.json +41 -0
  86. package/dist/lexicons/dev/hatk/searchRecords.json +32 -0
  87. package/dist/lexicons/dev/hatk/uploadBlob.json +23 -0
  88. package/dist/logger.d.ts +29 -0
  89. package/dist/logger.d.ts.map +1 -1
  90. package/dist/logger.js +29 -0
  91. package/dist/main.js +161 -72
  92. package/dist/mst.d.ts +18 -1
  93. package/dist/mst.d.ts.map +1 -1
  94. package/dist/mst.js +19 -8
  95. package/dist/oauth/db.d.ts +3 -1
  96. package/dist/oauth/db.d.ts.map +1 -1
  97. package/dist/oauth/db.js +56 -19
  98. package/dist/oauth/discovery.d.ts.map +1 -1
  99. package/dist/oauth/discovery.js +13 -1
  100. package/dist/oauth/server.d.ts +38 -0
  101. package/dist/oauth/server.d.ts.map +1 -1
  102. package/dist/oauth/server.js +228 -31
  103. package/dist/oauth/session.d.ts +11 -0
  104. package/dist/oauth/session.d.ts.map +1 -0
  105. package/dist/oauth/session.js +65 -0
  106. package/dist/opengraph.d.ts +10 -0
  107. package/dist/opengraph.d.ts.map +1 -1
  108. package/dist/opengraph.js +80 -40
  109. package/dist/pds-proxy.d.ts +60 -0
  110. package/dist/pds-proxy.d.ts.map +1 -0
  111. package/dist/pds-proxy.js +277 -0
  112. package/dist/private-collections.d.ts +8 -0
  113. package/dist/private-collections.d.ts.map +1 -0
  114. package/dist/private-collections.js +22 -0
  115. package/dist/push.d.ts +34 -0
  116. package/dist/push.d.ts.map +1 -0
  117. package/dist/push.js +186 -0
  118. package/dist/renderer.d.ts +27 -0
  119. package/dist/renderer.d.ts.map +1 -0
  120. package/dist/renderer.js +46 -0
  121. package/dist/resolve-hatk.d.ts +6 -0
  122. package/dist/resolve-hatk.d.ts.map +1 -0
  123. package/dist/resolve-hatk.js +20 -0
  124. package/dist/response.d.ts +16 -0
  125. package/dist/response.d.ts.map +1 -0
  126. package/dist/response.js +69 -0
  127. package/dist/scanner.d.ts +21 -0
  128. package/dist/scanner.d.ts.map +1 -0
  129. package/dist/scanner.js +88 -0
  130. package/dist/seed.d.ts +19 -0
  131. package/dist/seed.d.ts.map +1 -1
  132. package/dist/seed.js +43 -4
  133. package/dist/server-init.d.ts +8 -0
  134. package/dist/server-init.d.ts.map +1 -0
  135. package/dist/server-init.js +62 -0
  136. package/dist/server.d.ts +26 -3
  137. package/dist/server.d.ts.map +1 -1
  138. package/dist/server.js +729 -638
  139. package/dist/setup.d.ts +28 -1
  140. package/dist/setup.d.ts.map +1 -1
  141. package/dist/setup.js +50 -3
  142. package/dist/templates/feed.tpl +14 -0
  143. package/dist/templates/hook.tpl +5 -0
  144. package/dist/templates/label.tpl +15 -0
  145. package/dist/templates/og.tpl +17 -0
  146. package/dist/templates/seed.tpl +11 -0
  147. package/dist/templates/setup.tpl +5 -0
  148. package/dist/templates/test-feed.tpl +19 -0
  149. package/dist/templates/test-xrpc.tpl +19 -0
  150. package/dist/templates/xrpc.tpl +41 -0
  151. package/dist/test.d.ts +1 -1
  152. package/dist/test.d.ts.map +1 -1
  153. package/dist/test.js +41 -32
  154. package/dist/views.js +1 -1
  155. package/dist/vite-plugin.d.ts +1 -1
  156. package/dist/vite-plugin.d.ts.map +1 -1
  157. package/dist/vite-plugin.js +254 -66
  158. package/dist/xrpc.d.ts +75 -11
  159. package/dist/xrpc.d.ts.map +1 -1
  160. package/dist/xrpc.js +189 -39
  161. package/package.json +17 -9
  162. package/public/admin-auth.js +271 -306
  163. package/public/admin.html +177 -61
  164. package/dist/db.d.ts.map +0 -1
  165. package/dist/fts.d.ts.map +0 -1
  166. package/dist/oauth/hooks.d.ts +0 -10
  167. package/dist/oauth/hooks.d.ts.map +0 -1
  168. package/dist/oauth/hooks.js +0 -40
  169. package/dist/schema.d.ts.map +0 -1
  170. package/dist/test-browser.d.ts +0 -14
  171. package/dist/test-browser.d.ts.map +0 -1
  172. package/dist/test-browser.js +0 -26
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAmCA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAK9C,OAAO,EAAoB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAA;AACjE,OAAO,EAAqC,KAAK,aAAa,EAAE,MAAM,WAAW,CAAA;AAEjF,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,GAAG;IACjD,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,gDAAgD;IAChD,EAAE,EAAE;QACF,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC,CAAA;QAC9D,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACxD,CAAA;IACD,gEAAgE;IAChE,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1C,0DAA0D;IAC1D,YAAY,EAAE,CACZ,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,IAAI,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,KACrB,OAAO,CAAC;QAAE,GAAG,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,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 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
144
+ .handler({
145
+ action: item.action,
146
+ collection: item.collection,
147
+ record: item.record,
148
+ repo: item.authorDid,
149
+ uri: item.uri,
150
+ db: { query: base.db.query, run: runSQL },
151
+ lookup: base.lookup,
152
+ push,
153
+ })
154
+ .catch((err) => {
155
+ emit('hooks', 'on_commit_error', {
156
+ collection: item.collection,
157
+ uri: item.uri,
158
+ error: err.message,
159
+ });
160
+ });
161
+ }
162
+ }
163
+ }
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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CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAoC5E;AAID,yCAAyC;AACzC,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,GAAG,WAAW,CAkB7F"}
package/dist/hydrate.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,14 +1,120 @@
1
+ /** A single normalized repo operation, independent of the wire it arrived on. */
2
+ export interface CommitOp {
3
+ action: 'create' | 'update' | 'delete';
4
+ collection: string;
5
+ rkey: string;
6
+ /** Absent on deletes. */
7
+ cid?: string;
8
+ /** Absent on deletes. */
9
+ record?: Record<string, any>;
10
+ }
11
+ /** Record the latest firehose sequence seen. Source of truth for cursor persistence and reconnect resume. */
12
+ export declare function noteSeq(seq: number): void;
13
+ /** The highest seq this process has seen, or null before the first event. */
14
+ export declare function getLastSeq(): number | null;
15
+ /**
16
+ * Persist the latest firehose sequence when it has advanced past the stored
17
+ * cursor. Runs on a timer independent of the write path: an app whose
18
+ * collections rarely appear on the firehose never flushes a batch, so the
19
+ * flush-path cursor write alone leaves the stored cursor frozen — and a frozen
20
+ * cursor makes the relay replay its whole retention window at line rate
21
+ * (~135 GB/hour observed against bsky.network) on every boot and reconnect.
22
+ */
23
+ export declare function checkpointCursor(): Promise<void>;
24
+ /** Point cursor persistence at a stream's own `_cursor` row. See {@link cursorKey}. */
25
+ export declare function setCursorKey(key: 'relay' | 'jetstream'): void;
26
+ /**
27
+ * The cursor a reconnect should resume from: the latest seq this process has
28
+ * seen, falling back to the boot-time cursor before any message has arrived.
29
+ * Reusing the boot-time cursor on every reconnect would replay everything
30
+ * received since the process started.
31
+ */
32
+ export declare function resumeCursor(liveSeq: number | null, bootCursor: string | null | undefined): string | null | undefined;
33
+ /** Reset module cursor state between tests. */
34
+ export declare function _resetCursorStateForTests(): void;
35
+ /**
36
+ * Drain the write buffer synchronously instead of waiting out
37
+ * FLUSH_INTERVAL_MS. Lets end-to-end tests assert on rows immediately after
38
+ * feeding a frame.
39
+ */
40
+ export declare function _flushForTests(): Promise<void>;
41
+ /**
42
+ * Auto-backfill a DID's repo when first seen on the firehose.
43
+ *
44
+ * Fetches the full repo via CAR export, inserts all records, then replays any
45
+ * firehose events that arrived during the backfill. Concurrency is capped at
46
+ * `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
47
+ * to `maxRetries`.
48
+ */
49
+ /** Wait for a DID's backfill to complete if one is in flight. */
50
+ export declare function awaitBackfill(did: string): Promise<void>;
1
51
  export declare function triggerAutoBackfill(did: string, attempt?: number): Promise<void>;
2
- interface IndexerOpts {
3
- relayUrl: string;
52
+ /**
53
+ * Indexing behaviour shared by every stream source. Both the relay firehose
54
+ * and the Jetstream live tail feed the same buffers, backfill signalling, and
55
+ * cursor machinery — only the wire differs.
56
+ */
57
+ export interface IndexerCoreOpts {
58
+ plcUrl: string;
4
59
  collections: Set<string>;
5
60
  signalCollections?: Set<string>;
6
61
  pinnedRepos?: Set<string>;
7
- cursor?: string | null;
8
62
  fetchTimeout: number;
9
63
  maxRetries: number;
64
+ parallelism?: number;
10
65
  ftsRebuildInterval?: number;
11
66
  }
67
+ /** Configuration for the relay firehose indexer. */
68
+ interface IndexerOpts extends IndexerCoreOpts {
69
+ relayUrl: string;
70
+ cursor?: string | null;
71
+ }
72
+ /**
73
+ * Connect to the AT Protocol relay firehose and begin indexing.
74
+ *
75
+ * Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
76
+ * on the event loop to minimize backpressure, and batches writes through
77
+ * {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
78
+ * Reconnects automatically on disconnect after a 3s delay.
79
+ *
80
+ * @returns The WebSocket connection (for shutdown coordination)
81
+ */
82
+ export declare function configureIndexer(opts: IndexerCoreOpts): Promise<void>;
12
83
  export declare function startIndexer(opts: IndexerOpts): Promise<WebSocket>;
84
+ /**
85
+ * Handle a `#identity` firehose event for a DID. The `handle` field on the
86
+ * event is optional per the lexicon, and some emitters omit it (signalling
87
+ * "re-resolve"). When absent, we re-resolve from the PLC directory so handle
88
+ * renames propagate even when the relay payload is sparse.
89
+ *
90
+ * Only updates DIDs we already track (present in repoStatusCache) to avoid
91
+ * writing rows for the entire network.
92
+ */
93
+ export declare function handleIdentityEvent(did: string, payloadHandle: string | undefined): Promise<void>;
94
+ /**
95
+ * Whether a collection's records may be written from network data.
96
+ *
97
+ * Private collections are AppView-authoritative: nothing on the network may
98
+ * write or delete their rows, so stream ops naming them are spoofed by
99
+ * definition and must be dropped regardless of which source delivered them.
100
+ */
101
+ export declare function isIndexableCollection(collection: string, collections: Set<string>): boolean;
102
+ /**
103
+ * Apply a repo's commit ops to the index.
104
+ *
105
+ * Wire-agnostic: the relay path reaches here after CBOR/CAR decoding, the
106
+ * Jetstream path after parsing JSON. Callers must have already filtered `ops`
107
+ * through {@link isIndexableCollection}.
108
+ *
109
+ * Handles auto-backfill signalling, buffering for DIDs mid-backfill, lexicon
110
+ * validation, and delete/put routing.
111
+ */
112
+ export declare function applyCommit(did: string, ops: CommitOp[]): void;
113
+ /**
114
+ * Process a single firehose message. Decodes the CBOR header/body, filters
115
+ * for relevant collections, validates records against lexicons, and routes
116
+ * writes to the buffer (or pending buffer if the DID is mid-backfill).
117
+ */
118
+ export declare function processMessage(bytes: Uint8Array, collections: Set<string>): void;
13
119
  export {};
14
120
  //# 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"],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