@hatk/hatk 0.0.1-alpha.20 → 0.0.1-alpha.22

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.
@@ -1 +1 @@
1
- {"version":3,"file":"backfill.d.ts","sourceRoot":"","sources":["../src/backfill.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,6CAA6C;AAC7C,UAAU,YAAY;IACpB,wFAAwF;IACxF,MAAM,EAAE,MAAM,CAAA;IACd,8FAA8F;IAC9F,MAAM,EAAE,MAAM,CAAA;IACd,yEAAyE;IACzE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,wDAAwD;IACxD,MAAM,EAAE,cAAc,CAAA;CACvB;AAuGD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsJ/G;AAgCD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAkIrE"}
1
+ {"version":3,"file":"backfill.d.ts","sourceRoot":"","sources":["../src/backfill.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,6CAA6C;AAC7C,UAAU,YAAY;IACpB,wFAAwF;IACxF,MAAM,EAAE,MAAM,CAAA;IACd,8FAA8F;IAC9F,MAAM,EAAE,MAAM,CAAA;IACd,yEAAyE;IACzE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,wDAAwD;IACxD,MAAM,EAAE,cAAc,CAAA;CACvB;AAoGD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsJ/G;AA8BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAkIrE"}
package/dist/car.js CHANGED
@@ -107,7 +107,7 @@ export async function parseCarStream(body) {
107
107
  while (len - pos < need) {
108
108
  const { done, value } = await reader.read();
109
109
  if (done)
110
- return (len - pos) >= need;
110
+ return len - pos >= need;
111
111
  byteLength += value.length;
112
112
  // Compact: shift remaining data to front when read cursor passes midpoint
113
113
  if (pos > 0 && pos > buf.length >>> 1) {
package/dist/cli.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import { mkdirSync, writeFileSync, existsSync, unlinkSync, readdirSync, readFileSync } from 'node:fs';
3
- import { resolve, join } from 'node:path';
4
- import { execSync } from 'node:child_process';
5
- import { loadLexicons } from "./schema.js";
3
+ import { resolve, join, dirname } from 'node:path';
4
+ import { execSync, spawn } from 'node:child_process';
5
+ import { loadLexicons, discoverCollections, buildSchemas } from "./schema.js";
6
6
  import { loadConfig } from "./config.js";
7
7
  const args = process.argv.slice(2);
8
8
  const command = args[0];
@@ -34,6 +34,31 @@ async function ensurePds() {
34
34
  console.error('[dev] PDS failed to start');
35
35
  process.exit(1);
36
36
  }
37
+ /** Spawn a long-running process and forward SIGINT/SIGTERM for clean shutdown. */
38
+ function spawnForward(cmd, args, env) {
39
+ return new Promise((resolve, reject) => {
40
+ const child = spawn(cmd, args, {
41
+ stdio: 'inherit',
42
+ cwd: process.cwd(),
43
+ env: { ...process.env, ...env },
44
+ });
45
+ const onSignal = (sig) => {
46
+ child.kill(sig);
47
+ };
48
+ process.on('SIGINT', onSignal);
49
+ process.on('SIGTERM', onSignal);
50
+ child.on('close', (code, signal) => {
51
+ process.removeListener('SIGINT', onSignal);
52
+ process.removeListener('SIGTERM', onSignal);
53
+ if (signal === 'SIGINT' || signal === 'SIGTERM')
54
+ process.exit(0);
55
+ if (code === 0 || code === null)
56
+ resolve();
57
+ else
58
+ reject(new Error(`Process exited with code ${code}`));
59
+ });
60
+ });
61
+ }
37
62
  function runSeed() {
38
63
  const seedFile = resolve('seeds/seed.ts');
39
64
  if (!existsSync(seedFile))
@@ -511,6 +536,14 @@ export default defineConfig({
511
536
  properties: {
512
537
  uri: { type: 'string', format: 'at-uri' },
513
538
  cid: { type: 'string', format: 'cid' },
539
+ commit: {
540
+ type: 'object',
541
+ properties: {
542
+ cid: { type: 'string', format: 'cid' },
543
+ rev: { type: 'string' },
544
+ },
545
+ },
546
+ validationStatus: { type: 'string' },
514
547
  },
515
548
  },
516
549
  },
@@ -566,6 +599,14 @@ export default defineConfig({
566
599
  properties: {
567
600
  uri: { type: 'string', format: 'at-uri' },
568
601
  cid: { type: 'string', format: 'cid' },
602
+ commit: {
603
+ type: 'object',
604
+ properties: {
605
+ cid: { type: 'string', format: 'cid' },
606
+ rev: { type: 'string' },
607
+ },
608
+ },
609
+ validationStatus: { type: 'string' },
569
610
  },
570
611
  },
571
612
  },
@@ -897,7 +938,7 @@ CMD ["node", "--experimental-strip-types", "--max-old-space-size=512", "node_mod
897
938
  allowImportingTsExtensions: true,
898
939
  resolveJsonModule: true,
899
940
  },
900
- include: ['feeds', 'xrpc', 'og', 'seeds', 'labels', 'jobs', 'setup', 'hatk.generated.ts'],
941
+ include: ['feeds', 'xrpc', 'og', 'seeds', 'labels', 'jobs', 'setup', 'hatk.generated.ts', 'hatk.config.ts'],
901
942
  }, null, 2) + '\n');
902
943
  writeFileSync(join(dir, 'playwright.config.ts'), `import { defineConfig } from '@playwright/test'
903
944
 
@@ -1082,6 +1123,43 @@ a {
1082
1123
  </div>
1083
1124
  `);
1084
1125
  }
1126
+ writeFileSync(join(dir, 'AGENTS.md'), `# hatk project
1127
+
1128
+ This is an AT Protocol application built with [hatk](https://github.com/hatk-dev/hatk).
1129
+ Read the project's lexicons in \`lexicons/\` to understand the data model.
1130
+ Types are generated from lexicons into \`hatk.generated.ts\` — never edit this file directly.
1131
+
1132
+ ## Project structure
1133
+
1134
+ | Directory | Purpose |
1135
+ |-------------|------------------------------------------------------|
1136
+ | \`lexicons/\` | AT Protocol lexicon schemas (JSON). Defines collections and XRPC methods |
1137
+ | \`feeds/\` | Feed generators — each file exports a feed via \`defineFeed\` |
1138
+ | \`xrpc/\` | XRPC method handlers — directory nesting maps to NSID segments |
1139
+ | \`labels/\` | Label definitions and rules for moderation |
1140
+ | \`setup/\` | Boot-time scripts (run before server starts). Prefix with numbers for ordering |
1141
+ | \`seeds/\` | Test data seeding scripts for local development |
1142
+ | \`hooks/\` | Lifecycle hooks (e.g. \`on-login.ts\`) |
1143
+ | \`og/\` | OpenGraph image routes |
1144
+ | \`jobs/\` | Periodic background tasks |
1145
+ | \`test/\` | Test files (vitest). Run with \`hatk test\` |
1146
+ | \`public/\` | Static files served at the root |
1147
+
1148
+ ## Key files
1149
+
1150
+ - \`hatk.config.ts\` — project configuration (see \`defineConfig\` for type info)
1151
+ - \`hatk.generated.ts\` — auto-generated types and typed helpers. Regenerate with \`hatk generate types\`
1152
+
1153
+ ## Commands
1154
+
1155
+ Run \`npx hatk --help\` for the full list of commands.
1156
+
1157
+ Use \`npx hatk generate\` to scaffold new feeds, xrpc handlers, labels, and lexicons
1158
+ rather than creating files manually. These generate files with the correct imports
1159
+ from \`hatk.generated.ts\`.
1160
+
1161
+ After modifying lexicons, always run \`npx hatk generate types\` to update the generated types.
1162
+ `);
1085
1163
  console.log(`Created ${name}/`);
1086
1164
  console.log(` hatk.config.ts`);
1087
1165
  console.log(` lexicons/ — lexicon JSON files (core + your own)`);
@@ -1486,25 +1564,14 @@ else if (command === 'destroy') {
1486
1564
  else if (command === 'dev') {
1487
1565
  await ensurePds();
1488
1566
  runSeed();
1489
- try {
1490
- if (existsSync(resolve('svelte.config.js')) && existsSync(resolve('src/app.html'))) {
1491
- // SvelteKit project — vite dev starts the hatk server via the plugin
1492
- execSync('npx vite dev', { stdio: 'inherit', cwd: process.cwd() });
1493
- }
1494
- else {
1495
- // No frontend — just run the hatk server directly
1496
- const mainPath = resolve(import.meta.dirname, 'main.js');
1497
- execSync(`npx tsx ${mainPath} hatk.config.ts`, {
1498
- stdio: 'inherit',
1499
- cwd: process.cwd(),
1500
- env: { ...process.env, DEV_MODE: '1' },
1501
- });
1502
- }
1567
+ if (existsSync(resolve('svelte.config.js')) && existsSync(resolve('src/app.html'))) {
1568
+ // SvelteKit project vite dev starts the hatk server via the plugin
1569
+ await spawnForward('npx', ['vite', 'dev']);
1503
1570
  }
1504
- catch (e) {
1505
- if (e.signal === 'SIGINT' || e.signal === 'SIGTERM')
1506
- process.exit(0);
1507
- throw e;
1571
+ else {
1572
+ // No frontend just run the hatk server directly
1573
+ const mainPath = resolve(import.meta.dirname, 'main.js');
1574
+ await spawnForward('npx', ['tsx', mainPath, 'hatk.config.ts'], { DEV_MODE: '1' });
1508
1575
  }
1509
1576
  }
1510
1577
  else if (command === 'format' || command === 'fmt') {
@@ -1689,10 +1756,19 @@ else if (command === 'schema') {
1689
1756
  console.error('No database file configured (database is :memory:)');
1690
1757
  process.exit(1);
1691
1758
  }
1759
+ // Init DB from lexicons if it doesn't exist yet
1692
1760
  if (!existsSync(config.database)) {
1693
- console.error(`Database not found: ${config.database}`);
1694
- console.error('Run "hatk dev" first to create it.');
1695
- process.exit(1);
1761
+ const configDir = resolve('.');
1762
+ const lexicons = loadLexicons(resolve(configDir, 'lexicons'));
1763
+ const collections = config.collections.length > 0 ? config.collections : discoverCollections(lexicons);
1764
+ if (collections.length === 0) {
1765
+ console.error('No record collections found. Add record lexicons to the lexicons/ directory.');
1766
+ process.exit(1);
1767
+ }
1768
+ mkdirSync(dirname(config.database), { recursive: true });
1769
+ const { initDatabase } = await import("./db.js");
1770
+ const { schemas, ddlStatements } = buildSchemas(lexicons, collections);
1771
+ await initDatabase(config.database, schemas, ddlStatements);
1696
1772
  }
1697
1773
  const { DuckDBInstance } = await import('@duckdb/node-api');
1698
1774
  const instance = await DuckDBInstance.create(config.database);
@@ -1709,15 +1785,8 @@ else if (command === 'schema') {
1709
1785
  }
1710
1786
  }
1711
1787
  else if (command === 'start') {
1712
- try {
1713
- const mainPath = resolve(import.meta.dirname, 'main.js');
1714
- execSync(`npx tsx ${mainPath} hatk.config.ts`, { stdio: 'inherit', cwd: process.cwd() });
1715
- }
1716
- catch (e) {
1717
- if (e.signal === 'SIGINT' || e.signal === 'SIGTERM')
1718
- process.exit(0);
1719
- throw e;
1720
- }
1788
+ const mainPath = resolve(import.meta.dirname, 'main.js');
1789
+ await spawnForward('npx', ['tsx', mainPath, 'hatk.config.ts']);
1721
1790
  }
1722
1791
  else {
1723
1792
  usage();
package/dist/config.d.ts CHANGED
@@ -41,8 +41,15 @@ export interface HatkConfig {
41
41
  oauth: OAuthConfig | null;
42
42
  admins: string[];
43
43
  }
44
+ /** Input type for defineConfig — fields that have defaults are optional. */
45
+ export type HatkConfigInput = Partial<Omit<HatkConfig, 'oauth' | 'backfill'>> & {
46
+ oauth?: (Partial<OAuthConfig> & {
47
+ clients: OAuthClientConfig[];
48
+ }) | null;
49
+ backfill?: Partial<BackfillConfig>;
50
+ };
44
51
  /** Identity function that provides type inference for hatk config files. */
45
- export declare function defineConfig(config: Partial<HatkConfig>): Partial<HatkConfig>;
52
+ export declare function defineConfig(config: HatkConfigInput): HatkConfigInput;
46
53
  /** Derive HTTP URL from relay WebSocket URL (ws://host → http://host) */
47
54
  export declare function relayHttpUrl(relay: string): string;
48
55
  export declare function loadConfig(configPath: string): Promise<HatkConfig>;
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,M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AAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,QAAQ,EAAE,cAAc,CAAA;IACxB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,KAAK,EAAE,WAAW,GAAG,IAAI,CAAA;IACzB,MAAM,EAAE,MAAM,EAAE,CAAA;CACjB;AAED,4EAA4E;AAC5E,MAAM,MAAM,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,OAAO,GAAG,UAAU,CAAC,CAAC,GAAG;IAC9E,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG;QAAE,OAAO,EAAE,iBAAiB,EAAE,CAAA;KAAE,CAAC,GAAG,IAAI,CAAA;IACxE,QAAQ,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;CACnC,CAAA;AAED,4EAA4E;AAC5E,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAErE;AAED,yEAAyE;AACzE,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAsB,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAuDxE"}
@@ -0,0 +1,15 @@
1
+ /** Context passed to the on-login hook after a successful OAuth login. */
2
+ export type OnLoginCtx = {
3
+ /** DID of the user who just logged in. */
4
+ did: string;
5
+ /** Trigger a backfill for a DID if it hasn't been indexed yet. */
6
+ ensureRepo: (did: string) => Promise<void>;
7
+ };
8
+ /**
9
+ * Discover and load the on-login hook from the project's `hooks/` directory.
10
+ * Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
11
+ */
12
+ export declare function loadOnLoginHook(hooksDir: string): Promise<void>;
13
+ /** Fire the on-login hook if loaded. Errors are logged but never block login. */
14
+ export declare function fireOnLoginHook(did: string): Promise<void>;
15
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AA2BA,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG;IACvB,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,kEAAkE;IAClE,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC3C,CAAA;AAMD;;;GAGG;AACH,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQrE;AAQD,iFAAiF;AACjF,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAOhE"}
package/dist/hooks.js ADDED
@@ -0,0 +1,65 @@
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
+ *
16
+ * Each hook default-exports an async function that receives an event-specific
17
+ * context object.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * // hooks/on-login.ts
22
+ * import type { OnLoginCtx } from '@hatk/hatk/hooks'
23
+ *
24
+ * export default async function (ctx: OnLoginCtx) {
25
+ * // Ensure the user's repo is backfilled on first login
26
+ * await ctx.ensureRepo(ctx.did)
27
+ * }
28
+ * ```
29
+ */
30
+ import { existsSync } from 'node:fs';
31
+ import { resolve } from 'node:path';
32
+ import { log } from "./logger.js";
33
+ import { setRepoStatus } from "./db.js";
34
+ import { triggerAutoBackfill } from "./indexer.js";
35
+ let onLoginHook = null;
36
+ /**
37
+ * Discover and load the on-login hook from the project's `hooks/` directory.
38
+ * Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
39
+ */
40
+ export async function loadOnLoginHook(hooksDir) {
41
+ const tsPath = resolve(hooksDir, 'on-login.ts');
42
+ const jsPath = resolve(hooksDir, 'on-login.js');
43
+ const path = existsSync(tsPath) ? tsPath : existsSync(jsPath) ? jsPath : null;
44
+ if (!path)
45
+ return;
46
+ const mod = await import(__rewriteRelativeImportExtension(path));
47
+ onLoginHook = mod.default;
48
+ log('[hooks] on-login hook loaded');
49
+ }
50
+ /** Mark a DID as pending and trigger auto-backfill. */
51
+ async function ensureRepo(did) {
52
+ await setRepoStatus(did, 'pending');
53
+ triggerAutoBackfill(did);
54
+ }
55
+ /** Fire the on-login hook if loaded. Errors are logged but never block login. */
56
+ export async function fireOnLoginHook(did) {
57
+ if (!onLoginHook)
58
+ return;
59
+ try {
60
+ await onLoginHook({ did, ensureRepo });
61
+ }
62
+ catch (err) {
63
+ console.error('[hooks] onLogin hook error:', err.message);
64
+ }
65
+ }
package/dist/indexer.d.ts CHANGED
@@ -1,4 +1,13 @@
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
+ */
1
9
  export declare function triggerAutoBackfill(did: string, attempt?: number): Promise<void>;
10
+ /** Configuration for the firehose indexer. */
2
11
  interface IndexerOpts {
3
12
  relayUrl: string;
4
13
  collections: Set<string>;
@@ -10,6 +19,16 @@ interface IndexerOpts {
10
19
  parallelism?: number;
11
20
  ftsRebuildInterval?: number;
12
21
  }
22
+ /**
23
+ * Connect to the AT Protocol relay firehose and begin indexing.
24
+ *
25
+ * Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
26
+ * on the event loop to minimize backpressure, and batches writes through
27
+ * {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
28
+ * Reconnects automatically on disconnect after a 3s delay.
29
+ *
30
+ * @returns The WebSocket connection (for shutdown coordination)
31
+ */
13
32
  export declare function startIndexer(opts: IndexerOpts): Promise<WebSocket>;
14
33
  export {};
15
34
  //# sourceMappingURL=indexer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"AAmIA,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,SAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAgEjF;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,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAyBD,wBAAsB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAmDxE"}
1
+ {"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"AA2IA;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,SAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAgEjF;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
@@ -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,E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E,GAAG,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACtD,CAAA;IACD,MAAM,EAAE;QACN,GAAG,EAAE,MAAM,CAAA;QACX,GAAG,EAAE,MAAM,CAAA;QACX,GAAG,EAAE,MAAM,CAAA;QACX,UAAU,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,6 +6,34 @@ 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
39
  import { querySQL, runSQL, insertLabels, getSchema } from "./db.js";
@@ -13,6 +41,15 @@ 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);
@@ -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);