@hatk/hatk 0.0.1-alpha.4 → 0.0.1-alpha.40
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.
- package/dist/adapter.d.ts +19 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +107 -0
- package/dist/backfill.d.ts +60 -1
- package/dist/backfill.d.ts.map +1 -1
- package/dist/backfill.js +167 -33
- package/dist/car.d.ts +59 -1
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +179 -7
- package/dist/cbor.d.ts +37 -0
- package/dist/cbor.d.ts.map +1 -1
- package/dist/cbor.js +36 -3
- package/dist/cid.d.ts +37 -0
- package/dist/cid.d.ts.map +1 -1
- package/dist/cid.js +38 -3
- package/dist/cli.js +417 -133
- package/dist/cloudflare/container.d.ts +73 -0
- package/dist/cloudflare/container.d.ts.map +1 -0
- package/dist/cloudflare/container.js +232 -0
- package/dist/cloudflare/hooks.d.ts +33 -0
- package/dist/cloudflare/hooks.d.ts.map +1 -0
- package/dist/cloudflare/hooks.js +40 -0
- package/dist/cloudflare/init.d.ts +27 -0
- package/dist/cloudflare/init.d.ts.map +1 -0
- package/dist/cloudflare/init.js +103 -0
- package/dist/cloudflare/worker.d.ts +27 -0
- package/dist/cloudflare/worker.d.ts.map +1 -0
- package/dist/cloudflare/worker.js +54 -0
- package/dist/config.d.ts +12 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +36 -9
- package/dist/database/adapter-factory.d.ts +6 -0
- package/dist/database/adapter-factory.d.ts.map +1 -0
- package/dist/database/adapter-factory.js +20 -0
- package/dist/database/adapters/d1.d.ts +56 -0
- package/dist/database/adapters/d1.d.ts.map +1 -0
- package/dist/database/adapters/d1.js +108 -0
- package/dist/database/adapters/duckdb-search.d.ts +12 -0
- package/dist/database/adapters/duckdb-search.d.ts.map +1 -0
- package/dist/database/adapters/duckdb-search.js +27 -0
- package/dist/database/adapters/duckdb.d.ts +25 -0
- package/dist/database/adapters/duckdb.d.ts.map +1 -0
- package/dist/database/adapters/duckdb.js +161 -0
- package/dist/database/adapters/sqlite-search.d.ts +23 -0
- package/dist/database/adapters/sqlite-search.d.ts.map +1 -0
- package/dist/database/adapters/sqlite-search.js +74 -0
- package/dist/database/adapters/sqlite.d.ts +18 -0
- package/dist/database/adapters/sqlite.d.ts.map +1 -0
- package/dist/database/adapters/sqlite.js +87 -0
- package/dist/database/db.d.ts +159 -0
- package/dist/database/db.d.ts.map +1 -0
- package/dist/database/db.js +1445 -0
- package/dist/database/dialect.d.ts +45 -0
- package/dist/database/dialect.d.ts.map +1 -0
- package/dist/database/dialect.js +72 -0
- package/dist/database/fts.d.ts +27 -0
- package/dist/database/fts.d.ts.map +1 -0
- package/dist/database/fts.js +846 -0
- package/dist/database/index.d.ts +7 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +6 -0
- package/dist/database/ports.d.ts +50 -0
- package/dist/database/ports.d.ts.map +1 -0
- package/dist/database/ports.js +1 -0
- package/dist/database/schema.d.ts +61 -0
- package/dist/database/schema.d.ts.map +1 -0
- package/dist/database/schema.js +394 -0
- package/dist/db.d.ts +1 -1
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +4 -38
- package/dist/dev-entry.d.ts +8 -0
- package/dist/dev-entry.d.ts.map +1 -0
- package/dist/dev-entry.js +110 -0
- package/dist/feeds.d.ts +12 -8
- package/dist/feeds.d.ts.map +1 -1
- package/dist/feeds.js +45 -6
- package/dist/fts.d.ts.map +1 -1
- package/dist/fts.js +5 -0
- package/dist/hooks.d.ts +22 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +75 -0
- package/dist/hydrate.d.ts +6 -5
- package/dist/hydrate.d.ts.map +1 -1
- package/dist/hydrate.js +4 -16
- package/dist/indexer.d.ts +20 -0
- package/dist/indexer.d.ts.map +1 -1
- package/dist/indexer.js +53 -7
- package/dist/labels.d.ts +34 -0
- package/dist/labels.d.ts.map +1 -1
- package/dist/labels.js +66 -6
- package/dist/logger.d.ts +29 -0
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +29 -0
- package/dist/main.js +134 -67
- package/dist/mst.d.ts +18 -1
- package/dist/mst.d.ts.map +1 -1
- package/dist/mst.js +19 -8
- package/dist/oauth/db.d.ts.map +1 -1
- package/dist/oauth/db.js +43 -17
- package/dist/oauth/server.d.ts +2 -0
- package/dist/oauth/server.d.ts.map +1 -1
- package/dist/oauth/server.js +102 -7
- package/dist/oauth/session.d.ts +11 -0
- package/dist/oauth/session.d.ts.map +1 -0
- package/dist/oauth/session.js +65 -0
- package/dist/opengraph.d.ts +10 -0
- package/dist/opengraph.d.ts.map +1 -1
- package/dist/opengraph.js +73 -39
- package/dist/pds-proxy.d.ts +42 -0
- package/dist/pds-proxy.d.ts.map +1 -0
- package/dist/pds-proxy.js +189 -0
- package/dist/renderer.d.ts +27 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +46 -0
- package/dist/resolve-hatk.d.ts +6 -0
- package/dist/resolve-hatk.d.ts.map +1 -0
- package/dist/resolve-hatk.js +20 -0
- package/dist/response.d.ts +16 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +69 -0
- package/dist/scanner.d.ts +21 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +88 -0
- package/dist/schema.d.ts +8 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +29 -0
- package/dist/seed.d.ts +19 -0
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +43 -4
- package/dist/server-init.d.ts +8 -0
- package/dist/server-init.d.ts.map +1 -0
- package/dist/server-init.js +61 -0
- package/dist/server.d.ts +26 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +528 -635
- package/dist/setup.d.ts +28 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +50 -3
- package/dist/test.d.ts +1 -1
- package/dist/test.d.ts.map +1 -1
- package/dist/test.js +38 -32
- package/dist/views.js +1 -1
- package/dist/vite-plugin.d.ts +1 -1
- package/dist/vite-plugin.d.ts.map +1 -1
- package/dist/vite-plugin.js +254 -66
- package/dist/xrpc.d.ts +46 -10
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +128 -39
- package/package.json +13 -6
- package/public/admin.html +0 -54
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev mode entry point — loaded through Vite's module runner.
|
|
3
|
+
* Boots hatk infrastructure and exports the fetch handler.
|
|
4
|
+
*/
|
|
5
|
+
import { loadConfig } from "./config.js";
|
|
6
|
+
import { loadLexicons, storeLexicons, discoverCollections, buildSchemas } from "./database/schema.js";
|
|
7
|
+
import { discoverViews } from "./views.js";
|
|
8
|
+
import { initDatabase, migrateSchema, getSchemaDump } from "./database/db.js";
|
|
9
|
+
import { createAdapter } from "./database/adapter-factory.js";
|
|
10
|
+
import { getDialect } from "./database/dialect.js";
|
|
11
|
+
import { setSearchPort } from "./database/fts.js";
|
|
12
|
+
import { configureRelay } from "./xrpc.js";
|
|
13
|
+
import { initOAuth } from "./oauth/server.js";
|
|
14
|
+
import { initServer } from "./server-init.js";
|
|
15
|
+
import { createHandler, registerCoreHandlers } from "./server.js";
|
|
16
|
+
import { startIndexer } from "./indexer.js";
|
|
17
|
+
import { getCursor } from "./database/db.js";
|
|
18
|
+
import { runBackfill } from "./backfill.js";
|
|
19
|
+
import { rebuildAllIndexes } from "./database/fts.js";
|
|
20
|
+
import { relayHttpUrl } from "./config.js";
|
|
21
|
+
import { validateLexicons } from '@bigmoves/lexicon';
|
|
22
|
+
import { log } from "./logger.js";
|
|
23
|
+
import { mkdirSync } from 'node:fs';
|
|
24
|
+
import { dirname, resolve } from 'node:path';
|
|
25
|
+
process.env.DEV_MODE = '1';
|
|
26
|
+
// Boot sequence (mirrors main.ts but exports handler instead of starting server)
|
|
27
|
+
const configPath = 'hatk.config.ts';
|
|
28
|
+
const configDir = dirname(resolve(configPath));
|
|
29
|
+
const config = await loadConfig(configPath);
|
|
30
|
+
configureRelay(config.relay);
|
|
31
|
+
const lexicons = loadLexicons(resolve(configDir, 'lexicons'));
|
|
32
|
+
const lexiconErrors = validateLexicons([...lexicons.values()]);
|
|
33
|
+
if (lexiconErrors) {
|
|
34
|
+
for (const [nsid, errors] of Object.entries(lexiconErrors)) {
|
|
35
|
+
for (const err of errors)
|
|
36
|
+
console.error(`Invalid lexicon ${nsid}: ${err}`);
|
|
37
|
+
}
|
|
38
|
+
throw new Error('Invalid lexicons');
|
|
39
|
+
}
|
|
40
|
+
storeLexicons(lexicons);
|
|
41
|
+
const collections = config.collections.length > 0 ? config.collections : discoverCollections(lexicons);
|
|
42
|
+
discoverViews();
|
|
43
|
+
const engineDialect = getDialect(config.databaseEngine);
|
|
44
|
+
const { schemas, ddlStatements } = buildSchemas(lexicons, collections, engineDialect);
|
|
45
|
+
if (config.database !== ':memory:') {
|
|
46
|
+
mkdirSync(dirname(config.database), { recursive: true });
|
|
47
|
+
}
|
|
48
|
+
const { adapter, searchPort } = await createAdapter(config.databaseEngine);
|
|
49
|
+
setSearchPort(searchPort);
|
|
50
|
+
await initDatabase(adapter, config.database, schemas, ddlStatements);
|
|
51
|
+
await migrateSchema(schemas);
|
|
52
|
+
// Write db/schema.sql
|
|
53
|
+
try {
|
|
54
|
+
const { mkdirSync, writeFileSync } = await import('node:fs');
|
|
55
|
+
const schemaDir = resolve(configDir, 'db');
|
|
56
|
+
mkdirSync(schemaDir, { recursive: true });
|
|
57
|
+
const schemaDump = await getSchemaDump();
|
|
58
|
+
writeFileSync(resolve(schemaDir, 'schema.sql'), `-- This file is auto-generated by hatk on startup. Do not edit.\n-- Database engine: ${config.databaseEngine}\n\n${schemaDump}\n`);
|
|
59
|
+
log(`[hatk] Schema written to db/schema.sql`);
|
|
60
|
+
}
|
|
61
|
+
catch { }
|
|
62
|
+
// Initialize handlers from server/ directory
|
|
63
|
+
await initServer(resolve(configDir, 'server'));
|
|
64
|
+
// Register built-in dev.hatk.* handlers so callXrpc() can find them
|
|
65
|
+
registerCoreHandlers(collections, config.oauth);
|
|
66
|
+
if (config.oauth) {
|
|
67
|
+
await initOAuth(config.oauth, config.plc, config.relay);
|
|
68
|
+
}
|
|
69
|
+
// Start indexer
|
|
70
|
+
const collectionSet = new Set(collections);
|
|
71
|
+
const cursor = await getCursor('relay');
|
|
72
|
+
startIndexer({
|
|
73
|
+
relayUrl: config.relay,
|
|
74
|
+
collections: collectionSet,
|
|
75
|
+
signalCollections: config.backfill.signalCollections ? new Set(config.backfill.signalCollections) : undefined,
|
|
76
|
+
pinnedRepos: config.backfill.repos ? new Set(config.backfill.repos) : undefined,
|
|
77
|
+
cursor,
|
|
78
|
+
fetchTimeout: config.backfill.fetchTimeout,
|
|
79
|
+
maxRetries: config.backfill.maxRetries,
|
|
80
|
+
parallelism: config.backfill.parallelism,
|
|
81
|
+
ftsRebuildInterval: config.ftsRebuildInterval,
|
|
82
|
+
});
|
|
83
|
+
// Run backfill in background (no restart in dev mode)
|
|
84
|
+
runBackfill({
|
|
85
|
+
pdsUrl: relayHttpUrl(config.relay),
|
|
86
|
+
plcUrl: config.plc,
|
|
87
|
+
collections: collectionSet,
|
|
88
|
+
config: config.backfill,
|
|
89
|
+
})
|
|
90
|
+
.then(() => rebuildAllIndexes(Array.from(collectionSet)))
|
|
91
|
+
.catch((err) => console.error('[backfill]', err.message));
|
|
92
|
+
// Export the handler for Vite middleware
|
|
93
|
+
export const handler = createHandler({
|
|
94
|
+
collections: Array.from(collectionSet),
|
|
95
|
+
publicDir: null, // Vite serves static assets in dev
|
|
96
|
+
oauth: config.oauth,
|
|
97
|
+
admins: config.admins,
|
|
98
|
+
});
|
|
99
|
+
/** Re-scan server/ directory to pick up changed handlers in dev mode. */
|
|
100
|
+
export async function reloadServer() {
|
|
101
|
+
await initServer(resolve(configDir, 'server'));
|
|
102
|
+
}
|
|
103
|
+
export { renderPage } from "./renderer.js";
|
|
104
|
+
export { getRenderer } from "./renderer.js";
|
|
105
|
+
export { callXrpc } from "./xrpc.js";
|
|
106
|
+
export { parseSessionCookie, getSessionCookieName } from "./oauth/session.js";
|
|
107
|
+
log(`[hatk] Dev server ready`);
|
|
108
|
+
log(` Relay: ${config.relay}`);
|
|
109
|
+
log(` Database: ${config.database}`);
|
|
110
|
+
log(` Collections: ${collections.join(', ')}`);
|
package/dist/feeds.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseContext, Row } from './hydrate.ts';
|
|
2
2
|
import type { Checked } from './lex-types.ts';
|
|
3
|
-
export type {
|
|
3
|
+
export type { BaseContext, Row };
|
|
4
4
|
export interface FeedResult {
|
|
5
5
|
uris: string[];
|
|
6
6
|
cursor?: string;
|
|
@@ -16,7 +16,7 @@ export interface PaginateResult<T> {
|
|
|
16
16
|
}
|
|
17
17
|
export interface FeedContext {
|
|
18
18
|
db: {
|
|
19
|
-
query: (sql: string, params?:
|
|
19
|
+
query: (sql: string, params?: unknown[]) => Promise<unknown[]>;
|
|
20
20
|
};
|
|
21
21
|
params: Record<string, string>;
|
|
22
22
|
cursor?: string;
|
|
@@ -43,17 +43,17 @@ type FeedOpts = {
|
|
|
43
43
|
view?: string;
|
|
44
44
|
label: string;
|
|
45
45
|
generate: FeedGenerate;
|
|
46
|
-
hydrate?: (ctx:
|
|
46
|
+
hydrate?: (ctx: BaseContext, items: Row<any>[]) => Promise<unknown[]>;
|
|
47
47
|
} | {
|
|
48
48
|
collection?: never;
|
|
49
49
|
view?: never;
|
|
50
50
|
label: string;
|
|
51
51
|
generate: FeedGenerate;
|
|
52
|
-
hydrate: (ctx:
|
|
52
|
+
hydrate: (ctx: BaseContext, items: Row<any>[]) => Promise<unknown[]>;
|
|
53
53
|
};
|
|
54
54
|
export declare function createPaginate(deps: {
|
|
55
55
|
db: {
|
|
56
|
-
query: (sql: string, params?:
|
|
56
|
+
query: (sql: string, params?: unknown[]) => Promise<unknown[]>;
|
|
57
57
|
};
|
|
58
58
|
cursor?: string;
|
|
59
59
|
limit: number;
|
|
@@ -70,14 +70,18 @@ export declare function defineFeed(opts: FeedOpts): {
|
|
|
70
70
|
collection: string;
|
|
71
71
|
view?: string;
|
|
72
72
|
label: string;
|
|
73
|
-
hydrate?: (ctx:
|
|
73
|
+
hydrate?: (ctx: BaseContext, items: Row<any>[]) => Promise<unknown[]>;
|
|
74
|
+
__type: "feed";
|
|
74
75
|
} | {
|
|
75
76
|
generate: (ctx: any) => Promise<Checked<FeedResult>>;
|
|
76
77
|
collection?: never;
|
|
77
78
|
view?: never;
|
|
78
79
|
label: string;
|
|
79
|
-
hydrate: (ctx:
|
|
80
|
+
hydrate: (ctx: BaseContext, items: Row<any>[]) => Promise<unknown[]>;
|
|
81
|
+
__type: "feed";
|
|
80
82
|
};
|
|
83
|
+
/** Register a single feed from a scanned server/ module. */
|
|
84
|
+
export declare function registerFeed(name: string, generator: ReturnType<typeof defineFeed>): void;
|
|
81
85
|
export declare function initFeeds(feedsDir: string): Promise<void>;
|
|
82
86
|
/** Execute a feed and run its hydrate pipeline if present. */
|
|
83
87
|
export declare function executeFeed(name: string, params: Record<string, string>, cursor: string | undefined, limit: number, viewer?: {
|
package/dist/feeds.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feeds.d.ts","sourceRoot":"","sources":["../src/feeds.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"feeds.d.ts","sourceRoot":"","sources":["../src/feeds.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAE7C,YAAY,EAAE,WAAW,EAAE,GAAG,EAAE,CAAA;AAEhC,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,MA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AAgB,SAAS,IAAI;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,CAE7D;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEpD"}
|
package/dist/feeds.js
CHANGED
|
@@ -9,8 +9,8 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
9
9
|
import { resolve } from 'node:path';
|
|
10
10
|
import { readdirSync } from 'node:fs';
|
|
11
11
|
import { log } from "./logger.js";
|
|
12
|
-
import { querySQL, packCursor, unpackCursor, isTakendownDid, filterTakendownDids } from "./db.js";
|
|
13
|
-
import { resolveRecords,
|
|
12
|
+
import { querySQL, packCursor, unpackCursor, isTakendownDid, filterTakendownDids } from "./database/db.js";
|
|
13
|
+
import { resolveRecords, buildBaseContext } from "./hydrate.js";
|
|
14
14
|
export function createPaginate(deps) {
|
|
15
15
|
return async (sql, opts) => {
|
|
16
16
|
const { db, cursor, limit, packCursor: pack, unpackCursor: unpack } = deps;
|
|
@@ -59,7 +59,46 @@ export function createPaginate(deps) {
|
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
61
|
export function defineFeed(opts) {
|
|
62
|
-
return { ...opts, generate: (ctx) => opts.generate({ ...ctx, ok: (v) => v }) };
|
|
62
|
+
return { __type: 'feed', ...opts, generate: (ctx) => opts.generate({ ...ctx, ok: (v) => v }) };
|
|
63
|
+
}
|
|
64
|
+
/** Register a single feed from a scanned server/ module. */
|
|
65
|
+
export function registerFeed(name, generator) {
|
|
66
|
+
const handler = {
|
|
67
|
+
name,
|
|
68
|
+
label: generator.label || name,
|
|
69
|
+
collection: generator.collection,
|
|
70
|
+
view: generator.view,
|
|
71
|
+
generate: async (params, cursor, limit, viewer) => {
|
|
72
|
+
const paginateDeps = {
|
|
73
|
+
db: { query: querySQL },
|
|
74
|
+
cursor,
|
|
75
|
+
limit,
|
|
76
|
+
packCursor,
|
|
77
|
+
unpackCursor,
|
|
78
|
+
};
|
|
79
|
+
const ctx = {
|
|
80
|
+
db: { query: querySQL },
|
|
81
|
+
params,
|
|
82
|
+
cursor,
|
|
83
|
+
limit,
|
|
84
|
+
viewer,
|
|
85
|
+
packCursor,
|
|
86
|
+
unpackCursor,
|
|
87
|
+
isTakendown: isTakendownDid,
|
|
88
|
+
filterTakendownDids,
|
|
89
|
+
paginate: createPaginate(paginateDeps),
|
|
90
|
+
};
|
|
91
|
+
const result = await generator.generate(ctx);
|
|
92
|
+
if (Array.isArray(result)) {
|
|
93
|
+
return { uris: result.map((r) => r.uri || r) };
|
|
94
|
+
}
|
|
95
|
+
return { uris: result.uris, cursor: result.cursor };
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
if (typeof generator.hydrate === 'function') {
|
|
99
|
+
handler.hydrate = generator.hydrate;
|
|
100
|
+
}
|
|
101
|
+
feeds.set(name, handler);
|
|
63
102
|
}
|
|
64
103
|
const feeds = new Map();
|
|
65
104
|
export async function initFeeds(feedsDir) {
|
|
@@ -75,7 +114,7 @@ export async function initFeeds(feedsDir) {
|
|
|
75
114
|
for (const file of files) {
|
|
76
115
|
const name = file.replace(/\.(ts|js)$/, '');
|
|
77
116
|
const scriptPath = resolve(feedsDir, file);
|
|
78
|
-
const mod = await import(__rewriteRelativeImportExtension(scriptPath));
|
|
117
|
+
const mod = await import(__rewriteRelativeImportExtension(/* @vite-ignore */ `${scriptPath}?t=${Date.now()}`));
|
|
79
118
|
const generator = mod.default;
|
|
80
119
|
const handler = {
|
|
81
120
|
name,
|
|
@@ -130,8 +169,8 @@ export async function executeFeed(name, params, cursor, limit, viewer) {
|
|
|
130
169
|
const result = await handler.generate(params, cursor, limit, viewer || null);
|
|
131
170
|
if (handler.hydrate) {
|
|
132
171
|
const items = await resolveRecords(result.uris);
|
|
133
|
-
const ctx =
|
|
134
|
-
const hydrated = await handler.hydrate(ctx);
|
|
172
|
+
const ctx = buildBaseContext(viewer || null);
|
|
173
|
+
const hydrated = await handler.hydrate(ctx, items);
|
|
135
174
|
return { items: hydrated, cursor: result.cursor };
|
|
136
175
|
}
|
|
137
176
|
return { uris: result.uris, cursor: result.cursor };
|
package/dist/fts.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fts.d.ts","sourceRoot":"","sources":["../src/fts.ts"],"names":[],"mappings":"AAwEA,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAE7D;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAElE;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA0FrE;AAokBD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIpD;AAED,wBAAsB,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"fts.d.ts","sourceRoot":"","sources":["../src/fts.ts"],"names":[],"mappings":"AAwEA,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAE7D;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAElE;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA0FrE;AAokBD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIpD;AAED,wBAAsB,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA0B5E"}
|
package/dist/fts.js
CHANGED
|
@@ -752,6 +752,11 @@ export async function rebuildAllIndexes(collections) {
|
|
|
752
752
|
errors.push(`${collection}: ${err.message}`);
|
|
753
753
|
}
|
|
754
754
|
}
|
|
755
|
+
// Compact WAL to free DuckDB memory after heavy FTS operations
|
|
756
|
+
try {
|
|
757
|
+
await runSQL('CHECKPOINT');
|
|
758
|
+
}
|
|
759
|
+
catch { }
|
|
755
760
|
emit('fts', 'rebuild', {
|
|
756
761
|
collections_total: collections.length,
|
|
757
762
|
collections_rebuilt: rebuilt,
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
export declare function defineHook(event: 'on-login', handler: (ctx: OnLoginCtx) => Promise<void>): {
|
|
9
|
+
__type: "hook";
|
|
10
|
+
event: "on-login";
|
|
11
|
+
handler: (ctx: OnLoginCtx) => Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Discover and load the on-login hook from the project's `hooks/` directory.
|
|
15
|
+
* Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadOnLoginHook(hooksDir: string): Promise<void>;
|
|
18
|
+
/** Register a hook from a scanned server/ module. */
|
|
19
|
+
export declare function registerHook(event: string, handler: Function): void;
|
|
20
|
+
/** Fire the on-login hook if loaded. Errors are logged but never block login. */
|
|
21
|
+
export declare function fireOnLoginHook(did: string): Promise<void>;
|
|
22
|
+
//# 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;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC;;;mBAA5B,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC;EAExF;AAMD;;;GAGG;AACH,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQrE;AAQD,qDAAqD;AACrD,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,GAAG,IAAI,CAKnE;AAED,iFAAiF;AACjF,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAOhE"}
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
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 "./database/db.js";
|
|
34
|
+
import { triggerAutoBackfill } from "./indexer.js";
|
|
35
|
+
export function defineHook(event, handler) {
|
|
36
|
+
return { __type: 'hook', event, handler };
|
|
37
|
+
}
|
|
38
|
+
let onLoginHook = null;
|
|
39
|
+
/**
|
|
40
|
+
* Discover and load the on-login hook from the project's `hooks/` directory.
|
|
41
|
+
* Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
|
|
42
|
+
*/
|
|
43
|
+
export async function loadOnLoginHook(hooksDir) {
|
|
44
|
+
const tsPath = resolve(hooksDir, 'on-login.ts');
|
|
45
|
+
const jsPath = resolve(hooksDir, 'on-login.js');
|
|
46
|
+
const path = existsSync(tsPath) ? tsPath : existsSync(jsPath) ? jsPath : null;
|
|
47
|
+
if (!path)
|
|
48
|
+
return;
|
|
49
|
+
const mod = await import(__rewriteRelativeImportExtension(/* @vite-ignore */ `${path}?t=${Date.now()}`));
|
|
50
|
+
onLoginHook = mod.default;
|
|
51
|
+
log('[hooks] on-login hook loaded');
|
|
52
|
+
}
|
|
53
|
+
/** Mark a DID as pending and trigger auto-backfill. */
|
|
54
|
+
async function ensureRepo(did) {
|
|
55
|
+
await setRepoStatus(did, 'pending');
|
|
56
|
+
triggerAutoBackfill(did);
|
|
57
|
+
}
|
|
58
|
+
/** Register a hook from a scanned server/ module. */
|
|
59
|
+
export function registerHook(event, handler) {
|
|
60
|
+
if (event === 'on-login') {
|
|
61
|
+
onLoginHook = handler;
|
|
62
|
+
log('[hooks] on-login hook registered');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Fire the on-login hook if loaded. Errors are logged but never block login. */
|
|
66
|
+
export async function fireOnLoginHook(did) {
|
|
67
|
+
if (!onLoginHook)
|
|
68
|
+
return;
|
|
69
|
+
try {
|
|
70
|
+
await onLoginHook({ did, ensureRepo });
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
console.error('[hooks] onLogin hook error:', err.message);
|
|
74
|
+
}
|
|
75
|
+
}
|
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
|
|
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[]>;
|
|
@@ -16,8 +16,9 @@ export interface HydrateContext<T = unknown> {
|
|
|
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
|
|
20
|
-
export declare function
|
|
19
|
+
/** Build a BaseContext for hydration. */
|
|
20
|
+
export declare function buildBaseContext(viewer: {
|
|
21
21
|
did: string;
|
|
22
|
-
|
|
22
|
+
handle?: string;
|
|
23
|
+
} | null): BaseContext;
|
|
23
24
|
//# sourceMappingURL=hydrate.d.ts.map
|
package/dist/hydrate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hydrate.d.ts","sourceRoot":"","sources":["../src/hydrate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hydrate.d.ts","sourceRoot":"","sources":["../src/hydrate.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AAEzC,YAAY,EAAE,GAAG,EAAE,CAAA;AAInB,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAA;IAC/C,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,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1G,KAAK,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IAC5F,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,CAAA;IAC3D,OAAO,EAAE,CACP,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,OAAO,EACZ,MAAM,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,gBAAgB,GAAG,eAAe,KAC9D,MAAM,GAAG,SAAS,CAAA;CACxB;AAID,4EAA4E;AAC5E,wBAAsB,cAAc,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 {
|
|
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
|
|
43
|
-
export function
|
|
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:
|
|
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,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>;
|
|
@@ -7,8 +16,19 @@ interface IndexerOpts {
|
|
|
7
16
|
cursor?: string | null;
|
|
8
17
|
fetchTimeout: number;
|
|
9
18
|
maxRetries: number;
|
|
19
|
+
parallelism?: number;
|
|
10
20
|
ftsRebuildInterval?: number;
|
|
11
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
|
+
*/
|
|
12
32
|
export declare function startIndexer(opts: IndexerOpts): Promise<WebSocket>;
|
|
13
33
|
export {};
|
|
14
34
|
//# sourceMappingURL=indexer.d.ts.map
|
package/dist/indexer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"AAuJA;;;;;;;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
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { cborDecode } from "./cbor.js";
|
|
2
2
|
import { parseCarFrame } from "./car.js";
|
|
3
|
-
import { insertRecord, deleteRecord, setCursor, setRepoStatus, getRepoRetryInfo, listAllRepoStatuses } from "./db.js";
|
|
3
|
+
import { insertRecord, deleteRecord, setCursor, setRepoStatus, getRepoRetryInfo, listAllRepoStatuses, getDatabasePort, } from "./database/db.js";
|
|
4
4
|
import { backfillRepo } from "./backfill.js";
|
|
5
|
-
import { rebuildAllIndexes } from "./fts.js";
|
|
5
|
+
import { rebuildAllIndexes } from "./database/fts.js";
|
|
6
6
|
import { log, emit, timer } from "./logger.js";
|
|
7
7
|
import { runLabelRules } from "./labels.js";
|
|
8
|
-
import { getLexiconArray } from "./schema.js";
|
|
8
|
+
import { getLexiconArray } from "./database/schema.js";
|
|
9
9
|
import { validateRecord } from '@bigmoves/lexicon';
|
|
10
10
|
let buffer = [];
|
|
11
11
|
let flushTimer = null;
|
|
@@ -18,7 +18,7 @@ let ftsRebuildInterval = 500;
|
|
|
18
18
|
const pendingBuffers = new Map();
|
|
19
19
|
// Track in-flight backfills to avoid duplicates
|
|
20
20
|
const backfillInFlight = new Set();
|
|
21
|
-
const
|
|
21
|
+
const pendingReschedule = new Set();
|
|
22
22
|
// In-memory cache of repo status to avoid flooding the DB read queue
|
|
23
23
|
const repoStatusCache = new Map();
|
|
24
24
|
// Set by startIndexer
|
|
@@ -27,6 +27,12 @@ let indexerSignalCollections;
|
|
|
27
27
|
let indexerPinnedRepos = null;
|
|
28
28
|
let indexerFetchTimeout;
|
|
29
29
|
let indexerMaxRetries;
|
|
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
|
+
*/
|
|
30
36
|
async function flushBuffer() {
|
|
31
37
|
if (buffer.length === 0)
|
|
32
38
|
return;
|
|
@@ -86,9 +92,14 @@ async function flushBuffer() {
|
|
|
86
92
|
writesSinceRebuild += batch.length;
|
|
87
93
|
if (writesSinceRebuild >= ftsRebuildInterval) {
|
|
88
94
|
writesSinceRebuild = 0;
|
|
89
|
-
|
|
95
|
+
// Skip periodic full rebuild for SQLite — it uses incremental FTS updates
|
|
96
|
+
const port = getDatabasePort();
|
|
97
|
+
if (port.dialect !== 'sqlite') {
|
|
98
|
+
rebuildAllIndexes([...indexerCollections]).catch(() => { });
|
|
99
|
+
}
|
|
90
100
|
}
|
|
91
101
|
}
|
|
102
|
+
/** Schedule a flush after FLUSH_INTERVAL_MS if one isn't already pending. */
|
|
92
103
|
function scheduleFlush() {
|
|
93
104
|
if (flushTimer)
|
|
94
105
|
return;
|
|
@@ -97,6 +108,7 @@ function scheduleFlush() {
|
|
|
97
108
|
await flushBuffer();
|
|
98
109
|
}, FLUSH_INTERVAL_MS);
|
|
99
110
|
}
|
|
111
|
+
/** Add a record to the write buffer. Flushes immediately if BATCH_SIZE is reached. */
|
|
100
112
|
function bufferWrite(item) {
|
|
101
113
|
buffer.push(item);
|
|
102
114
|
if (buffer.length >= BATCH_SIZE) {
|
|
@@ -110,9 +122,27 @@ function bufferWrite(item) {
|
|
|
110
122
|
scheduleFlush();
|
|
111
123
|
}
|
|
112
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Auto-backfill a DID's repo when first seen on the firehose.
|
|
127
|
+
*
|
|
128
|
+
* Fetches the full repo via CAR export, inserts all records, then replays any
|
|
129
|
+
* firehose events that arrived during the backfill. Concurrency is capped at
|
|
130
|
+
* `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
|
|
131
|
+
* to `maxRetries`.
|
|
132
|
+
*/
|
|
113
133
|
export async function triggerAutoBackfill(did, attempt = 0) {
|
|
114
134
|
if (backfillInFlight.has(did))
|
|
115
135
|
return;
|
|
136
|
+
if (backfillInFlight.size >= maxConcurrentBackfills) {
|
|
137
|
+
if (!pendingReschedule.has(did)) {
|
|
138
|
+
pendingReschedule.add(did);
|
|
139
|
+
setTimeout(() => {
|
|
140
|
+
pendingReschedule.delete(did);
|
|
141
|
+
triggerAutoBackfill(did, attempt);
|
|
142
|
+
}, 10_000);
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
116
146
|
backfillInFlight.add(did);
|
|
117
147
|
pendingBuffers.set(did, []);
|
|
118
148
|
if (attempt === 0)
|
|
@@ -162,7 +192,7 @@ export async function triggerAutoBackfill(did, attempt = 0) {
|
|
|
162
192
|
}, delayMs);
|
|
163
193
|
}
|
|
164
194
|
}
|
|
165
|
-
|
|
195
|
+
/** Emit a memory diagnostics wide event every 30s for observability. */
|
|
166
196
|
function startMemoryDiagnostics() {
|
|
167
197
|
setInterval(() => {
|
|
168
198
|
const mem = process.memoryUsage();
|
|
@@ -184,6 +214,16 @@ function startMemoryDiagnostics() {
|
|
|
184
214
|
});
|
|
185
215
|
}, 30_000);
|
|
186
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Connect to the AT Protocol relay firehose and begin indexing.
|
|
219
|
+
*
|
|
220
|
+
* Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
|
|
221
|
+
* on the event loop to minimize backpressure, and batches writes through
|
|
222
|
+
* {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
|
|
223
|
+
* Reconnects automatically on disconnect after a 3s delay.
|
|
224
|
+
*
|
|
225
|
+
* @returns The WebSocket connection (for shutdown coordination)
|
|
226
|
+
*/
|
|
187
227
|
export async function startIndexer(opts) {
|
|
188
228
|
const { relayUrl, collections, cursor, fetchTimeout } = opts;
|
|
189
229
|
if (opts.ftsRebuildInterval != null)
|
|
@@ -193,6 +233,7 @@ export async function startIndexer(opts) {
|
|
|
193
233
|
indexerPinnedRepos = opts.pinnedRepos || null;
|
|
194
234
|
indexerFetchTimeout = fetchTimeout;
|
|
195
235
|
indexerMaxRetries = opts.maxRetries;
|
|
236
|
+
maxConcurrentBackfills = opts.parallelism ?? 3;
|
|
196
237
|
// Pre-populate repo status cache from DB so non-signal updates
|
|
197
238
|
// (e.g. profile changes) are processed for already-tracked DIDs
|
|
198
239
|
if (repoStatusCache.size === 0) {
|
|
@@ -231,6 +272,11 @@ export async function startIndexer(opts) {
|
|
|
231
272
|
});
|
|
232
273
|
return ws;
|
|
233
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Process a single firehose message. Decodes the CBOR header/body, filters
|
|
277
|
+
* for relevant collections, validates records against lexicons, and routes
|
|
278
|
+
* writes to the buffer (or pending buffer if the DID is mid-backfill).
|
|
279
|
+
*/
|
|
234
280
|
function processMessage(bytes, collections) {
|
|
235
281
|
const header = cborDecode(bytes, 0);
|
|
236
282
|
const body = cborDecode(bytes, header.offset);
|
|
@@ -264,7 +310,7 @@ function processMessage(bytes, collections) {
|
|
|
264
310
|
repoStatusCache.set(did, 'unknown');
|
|
265
311
|
}
|
|
266
312
|
if (hasSignalOp && (!indexerPinnedRepos || indexerPinnedRepos.has(did))) {
|
|
267
|
-
if (repoStatus === null && backfillInFlight.size <
|
|
313
|
+
if (repoStatus === null && backfillInFlight.size < maxConcurrentBackfills) {
|
|
268
314
|
repoStatusCache.set(did, 'pending');
|
|
269
315
|
triggerAutoBackfill(did);
|
|
270
316
|
}
|