@hatk/hatk 0.0.1-alpha.5 → 0.0.1-alpha.50
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 +243 -996
- 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/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 +88 -0
- package/dist/{db.d.ts → database/db.d.ts} +56 -6
- package/dist/database/db.d.ts.map +1 -0
- package/dist/{db.js → database/db.js} +719 -549
- 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/{fts.d.ts → database/fts.d.ts} +7 -0
- package/dist/database/fts.d.ts.map +1 -0
- package/dist/{fts.js → database/fts.js} +116 -32
- 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/{schema.d.ts → database/schema.d.ts} +14 -3
- package/dist/database/schema.d.ts.map +1 -0
- package/dist/{schema.js → database/schema.js} +81 -41
- package/dist/dev-entry.d.ts +8 -0
- package/dist/dev-entry.d.ts.map +1 -0
- package/dist/dev-entry.js +111 -0
- package/dist/feeds.d.ts +12 -8
- package/dist/feeds.d.ts.map +1 -1
- package/dist/feeds.js +45 -6
- package/dist/hooks.d.ts +43 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +102 -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 +22 -0
- package/dist/indexer.d.ts.map +1 -1
- package/dist/indexer.js +80 -8
- package/dist/labels.d.ts +36 -0
- package/dist/labels.d.ts.map +1 -1
- package/dist/labels.js +71 -6
- package/dist/lexicon-resolve.d.ts.map +1 -1
- package/dist/lexicon-resolve.js +27 -112
- package/dist/lexicons/com/atproto/label/defs.json +75 -0
- package/dist/lexicons/com/atproto/moderation/defs.json +30 -0
- package/dist/lexicons/com/atproto/repo/strongRef.json +24 -0
- package/dist/lexicons/dev/hatk/createRecord.json +40 -0
- package/dist/lexicons/dev/hatk/createReport.json +48 -0
- package/dist/lexicons/dev/hatk/deleteRecord.json +25 -0
- package/dist/lexicons/dev/hatk/describeCollections.json +41 -0
- package/dist/lexicons/dev/hatk/describeFeeds.json +29 -0
- package/dist/lexicons/dev/hatk/describeLabels.json +45 -0
- package/dist/lexicons/dev/hatk/getFeed.json +30 -0
- package/dist/lexicons/dev/hatk/getPreferences.json +19 -0
- package/dist/lexicons/dev/hatk/getRecord.json +26 -0
- package/dist/lexicons/dev/hatk/getRecords.json +32 -0
- package/dist/lexicons/dev/hatk/putPreference.json +28 -0
- package/dist/lexicons/dev/hatk/putRecord.json +41 -0
- package/dist/lexicons/dev/hatk/searchRecords.json +32 -0
- package/dist/lexicons/dev/hatk/uploadBlob.json +23 -0
- 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 +126 -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 +3 -1
- package/dist/oauth/db.d.ts.map +1 -1
- package/dist/oauth/db.js +48 -19
- package/dist/oauth/server.d.ts +24 -0
- package/dist/oauth/server.d.ts.map +1 -1
- package/dist/oauth/server.js +198 -22
- 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 +207 -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/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 +62 -0
- package/dist/server.d.ts +26 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +601 -635
- package/dist/setup.d.ts +28 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +50 -3
- package/dist/templates/feed.tpl +14 -0
- package/dist/templates/hook.tpl +5 -0
- package/dist/templates/label.tpl +15 -0
- package/dist/templates/og.tpl +17 -0
- package/dist/templates/seed.tpl +11 -0
- package/dist/templates/setup.tpl +5 -0
- package/dist/templates/test-feed.tpl +19 -0
- package/dist/templates/test-xrpc.tpl +19 -0
- package/dist/templates/xrpc.tpl +41 -0
- 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 +60 -10
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +155 -39
- package/package.json +15 -7
- package/public/admin.html +133 -54
- package/dist/db.d.ts.map +0 -1
- package/dist/fts.d.ts.map +0 -1
- package/dist/oauth/hooks.d.ts +0 -10
- package/dist/oauth/hooks.d.ts.map +0 -1
- package/dist/oauth/hooks.js +0 -40
- package/dist/schema.d.ts.map +0 -1
- package/dist/test-browser.d.ts +0 -14
- package/dist/test-browser.d.ts.map +0 -1
- package/dist/test-browser.js +0 -26
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
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 { pdsCreateRecord, pdsPutRecord, pdsDeleteRecord } from "./pds-proxy.js";
|
|
33
|
+
import { log, emit } from "./logger.js";
|
|
34
|
+
import { setRepoStatus, runSQL } from "./database/db.js";
|
|
35
|
+
import { triggerAutoBackfill, awaitBackfill } from "./indexer.js";
|
|
36
|
+
import { buildBaseContext } from "./hydrate.js";
|
|
37
|
+
export function defineHook(event, handler) {
|
|
38
|
+
return { __type: 'hook', event, handler };
|
|
39
|
+
}
|
|
40
|
+
let onLoginHook = null;
|
|
41
|
+
/**
|
|
42
|
+
* Discover and load the on-login hook from the project's `hooks/` directory.
|
|
43
|
+
* Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
|
|
44
|
+
*/
|
|
45
|
+
export async function loadOnLoginHook(hooksDir) {
|
|
46
|
+
const tsPath = resolve(hooksDir, 'on-login.ts');
|
|
47
|
+
const jsPath = resolve(hooksDir, 'on-login.js');
|
|
48
|
+
const path = existsSync(tsPath) ? tsPath : existsSync(jsPath) ? jsPath : null;
|
|
49
|
+
if (!path)
|
|
50
|
+
return;
|
|
51
|
+
const mod = await import(__rewriteRelativeImportExtension(/* @vite-ignore */ `${path}?t=${Date.now()}`));
|
|
52
|
+
onLoginHook = mod.default;
|
|
53
|
+
log('[hooks] on-login hook loaded');
|
|
54
|
+
}
|
|
55
|
+
/** Mark a DID as pending, trigger auto-backfill, and wait for completion. */
|
|
56
|
+
async function ensureRepo(did) {
|
|
57
|
+
await setRepoStatus(did, 'pending');
|
|
58
|
+
triggerAutoBackfill(did);
|
|
59
|
+
await awaitBackfill(did);
|
|
60
|
+
}
|
|
61
|
+
/** Register a hook from a scanned server/ module. */
|
|
62
|
+
export function registerHook(event, handler) {
|
|
63
|
+
if (event === 'on-login') {
|
|
64
|
+
onLoginHook = handler;
|
|
65
|
+
log('[hooks] on-login hook registered');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** Fire the on-login hook if loaded. Errors are logged but never block login. */
|
|
69
|
+
export async function fireOnLoginHook(did, oauthConfig) {
|
|
70
|
+
if (!onLoginHook)
|
|
71
|
+
return;
|
|
72
|
+
try {
|
|
73
|
+
const base = buildBaseContext({ did });
|
|
74
|
+
const viewer = { did };
|
|
75
|
+
const hookPromise = onLoginHook({
|
|
76
|
+
...base,
|
|
77
|
+
did,
|
|
78
|
+
db: { query: base.db.query, run: runSQL },
|
|
79
|
+
ensureRepo,
|
|
80
|
+
createRecord: async (collection, record, opts) => {
|
|
81
|
+
if (!oauthConfig)
|
|
82
|
+
throw new Error('No OAuth config — cannot write to PDS');
|
|
83
|
+
return pdsCreateRecord(oauthConfig, viewer, { collection, record, rkey: opts?.rkey });
|
|
84
|
+
},
|
|
85
|
+
putRecord: async (collection, rkey, record) => {
|
|
86
|
+
if (!oauthConfig)
|
|
87
|
+
throw new Error('No OAuth config — cannot write to PDS');
|
|
88
|
+
return pdsPutRecord(oauthConfig, viewer, { collection, rkey, record });
|
|
89
|
+
},
|
|
90
|
+
deleteRecord: async (collection, rkey) => {
|
|
91
|
+
if (!oauthConfig)
|
|
92
|
+
throw new Error('No OAuth config — cannot write to PDS');
|
|
93
|
+
await pdsDeleteRecord(oauthConfig, viewer, { collection, rkey });
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error('on-login hook timed out after 30s')), 30_000));
|
|
97
|
+
await Promise.race([hookPromise, timeout]);
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
emit('hooks', 'on_login_error', { did, error: err.message });
|
|
101
|
+
}
|
|
102
|
+
}
|
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,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-backfill a DID's repo when first seen on the firehose.
|
|
3
|
+
*
|
|
4
|
+
* Fetches the full repo via CAR export, inserts all records, then replays any
|
|
5
|
+
* firehose events that arrived during the backfill. Concurrency is capped at
|
|
6
|
+
* `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
|
|
7
|
+
* to `maxRetries`.
|
|
8
|
+
*/
|
|
9
|
+
/** Wait for a DID's backfill to complete if one is in flight. */
|
|
10
|
+
export declare function awaitBackfill(did: string): Promise<void>;
|
|
1
11
|
export declare function triggerAutoBackfill(did: string, attempt?: number): Promise<void>;
|
|
12
|
+
/** Configuration for the firehose indexer. */
|
|
2
13
|
interface IndexerOpts {
|
|
3
14
|
relayUrl: string;
|
|
4
15
|
collections: Set<string>;
|
|
@@ -7,8 +18,19 @@ interface IndexerOpts {
|
|
|
7
18
|
cursor?: string | null;
|
|
8
19
|
fetchTimeout: number;
|
|
9
20
|
maxRetries: number;
|
|
21
|
+
parallelism?: number;
|
|
10
22
|
ftsRebuildInterval?: number;
|
|
11
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Connect to the AT Protocol relay firehose and begin indexing.
|
|
26
|
+
*
|
|
27
|
+
* Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
|
|
28
|
+
* on the event loop to minimize backpressure, and batches writes through
|
|
29
|
+
* {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
|
|
30
|
+
* Reconnects automatically on disconnect after a 3s delay.
|
|
31
|
+
*
|
|
32
|
+
* @returns The WebSocket connection (for shutdown coordination)
|
|
33
|
+
*/
|
|
12
34
|
export declare function startIndexer(opts: IndexerOpts): Promise<WebSocket>;
|
|
13
35
|
export {};
|
|
14
36
|
//# sourceMappingURL=indexer.d.ts.map
|
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":"AAyJA;;;;;;;GAOG;AACH,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGxD;AAED,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,SAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CA4EjF;AAED,8CAA8C;AAC9C,UAAU,WAAW;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC/B,WAAW,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACzB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAyBD;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAmDxE"}
|
package/dist/indexer.js
CHANGED
|
@@ -1,11 +1,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, updateRepoHandle, } from "./database/db.js";
|
|
4
4
|
import { backfillRepo } from "./backfill.js";
|
|
5
|
-
import { rebuildAllIndexes } from "./fts.js";
|
|
5
|
+
import { rebuildAllIndexes } from "./database/fts.js";
|
|
6
6
|
import { log, emit, timer } from "./logger.js";
|
|
7
7
|
import { runLabelRules } from "./labels.js";
|
|
8
|
-
import { getLexiconArray } from "./schema.js";
|
|
8
|
+
import { getLexiconArray } from "./database/schema.js";
|
|
9
9
|
import { validateRecord } from '@bigmoves/lexicon';
|
|
10
10
|
let buffer = [];
|
|
11
11
|
let flushTimer = null;
|
|
@@ -18,7 +18,8 @@ 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 backfillPromises = new Map();
|
|
22
|
+
const pendingReschedule = new Set();
|
|
22
23
|
// In-memory cache of repo status to avoid flooding the DB read queue
|
|
23
24
|
const repoStatusCache = new Map();
|
|
24
25
|
// Set by startIndexer
|
|
@@ -27,6 +28,12 @@ let indexerSignalCollections;
|
|
|
27
28
|
let indexerPinnedRepos = null;
|
|
28
29
|
let indexerFetchTimeout;
|
|
29
30
|
let indexerMaxRetries;
|
|
31
|
+
let maxConcurrentBackfills = 3;
|
|
32
|
+
/**
|
|
33
|
+
* Flush the write buffer — insert all buffered records, update the relay cursor,
|
|
34
|
+
* run label rules on inserted records, and trigger FTS rebuilds when the write
|
|
35
|
+
* threshold is reached. Emits a wide event with batch stats.
|
|
36
|
+
*/
|
|
30
37
|
async function flushBuffer() {
|
|
31
38
|
if (buffer.length === 0)
|
|
32
39
|
return;
|
|
@@ -86,9 +93,14 @@ async function flushBuffer() {
|
|
|
86
93
|
writesSinceRebuild += batch.length;
|
|
87
94
|
if (writesSinceRebuild >= ftsRebuildInterval) {
|
|
88
95
|
writesSinceRebuild = 0;
|
|
89
|
-
|
|
96
|
+
// Skip periodic full rebuild for SQLite — it uses incremental FTS updates
|
|
97
|
+
const port = getDatabasePort();
|
|
98
|
+
if (port.dialect !== 'sqlite') {
|
|
99
|
+
rebuildAllIndexes([...indexerCollections]).catch(() => { });
|
|
100
|
+
}
|
|
90
101
|
}
|
|
91
102
|
}
|
|
103
|
+
/** Schedule a flush after FLUSH_INTERVAL_MS if one isn't already pending. */
|
|
92
104
|
function scheduleFlush() {
|
|
93
105
|
if (flushTimer)
|
|
94
106
|
return;
|
|
@@ -97,6 +109,7 @@ function scheduleFlush() {
|
|
|
97
109
|
await flushBuffer();
|
|
98
110
|
}, FLUSH_INTERVAL_MS);
|
|
99
111
|
}
|
|
112
|
+
/** Add a record to the write buffer. Flushes immediately if BATCH_SIZE is reached. */
|
|
100
113
|
function bufferWrite(item) {
|
|
101
114
|
buffer.push(item);
|
|
102
115
|
if (buffer.length >= BATCH_SIZE) {
|
|
@@ -110,11 +123,39 @@ function bufferWrite(item) {
|
|
|
110
123
|
scheduleFlush();
|
|
111
124
|
}
|
|
112
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Auto-backfill a DID's repo when first seen on the firehose.
|
|
128
|
+
*
|
|
129
|
+
* Fetches the full repo via CAR export, inserts all records, then replays any
|
|
130
|
+
* firehose events that arrived during the backfill. Concurrency is capped at
|
|
131
|
+
* `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
|
|
132
|
+
* to `maxRetries`.
|
|
133
|
+
*/
|
|
134
|
+
/** Wait for a DID's backfill to complete if one is in flight. */
|
|
135
|
+
export function awaitBackfill(did) {
|
|
136
|
+
const entry = backfillPromises.get(did);
|
|
137
|
+
return entry ? entry.promise : Promise.resolve();
|
|
138
|
+
}
|
|
113
139
|
export async function triggerAutoBackfill(did, attempt = 0) {
|
|
114
140
|
if (backfillInFlight.has(did))
|
|
115
141
|
return;
|
|
142
|
+
if (backfillInFlight.size >= maxConcurrentBackfills) {
|
|
143
|
+
if (!pendingReschedule.has(did)) {
|
|
144
|
+
pendingReschedule.add(did);
|
|
145
|
+
setTimeout(() => {
|
|
146
|
+
pendingReschedule.delete(did);
|
|
147
|
+
triggerAutoBackfill(did, attempt);
|
|
148
|
+
}, 10_000);
|
|
149
|
+
}
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
116
152
|
backfillInFlight.add(did);
|
|
117
153
|
pendingBuffers.set(did, []);
|
|
154
|
+
if (!backfillPromises.has(did)) {
|
|
155
|
+
let resolveBackfill;
|
|
156
|
+
const promise = new Promise((r) => { resolveBackfill = r; });
|
|
157
|
+
backfillPromises.set(did, { promise, resolve: resolveBackfill });
|
|
158
|
+
}
|
|
118
159
|
if (attempt === 0)
|
|
119
160
|
await setRepoStatus(did, 'pending');
|
|
120
161
|
const elapsed = timer();
|
|
@@ -154,6 +195,12 @@ export async function triggerAutoBackfill(did, attempt = 0) {
|
|
|
154
195
|
error,
|
|
155
196
|
retry_count: currentRetryCount,
|
|
156
197
|
});
|
|
198
|
+
// Resolve awaiting callers (e.g. on-login hooks)
|
|
199
|
+
const entry = backfillPromises.get(did);
|
|
200
|
+
if (entry) {
|
|
201
|
+
entry.resolve();
|
|
202
|
+
backfillPromises.delete(did);
|
|
203
|
+
}
|
|
157
204
|
if (status === 'error' && currentRetryCount < indexerMaxRetries) {
|
|
158
205
|
const delaySecs = Math.min(currentRetryCount * 60, 3600);
|
|
159
206
|
const delayMs = Math.max(delaySecs, 60) * 1000;
|
|
@@ -162,7 +209,7 @@ export async function triggerAutoBackfill(did, attempt = 0) {
|
|
|
162
209
|
}, delayMs);
|
|
163
210
|
}
|
|
164
211
|
}
|
|
165
|
-
|
|
212
|
+
/** Emit a memory diagnostics wide event every 30s for observability. */
|
|
166
213
|
function startMemoryDiagnostics() {
|
|
167
214
|
setInterval(() => {
|
|
168
215
|
const mem = process.memoryUsage();
|
|
@@ -184,6 +231,16 @@ function startMemoryDiagnostics() {
|
|
|
184
231
|
});
|
|
185
232
|
}, 30_000);
|
|
186
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Connect to the AT Protocol relay firehose and begin indexing.
|
|
236
|
+
*
|
|
237
|
+
* Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
|
|
238
|
+
* on the event loop to minimize backpressure, and batches writes through
|
|
239
|
+
* {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
|
|
240
|
+
* Reconnects automatically on disconnect after a 3s delay.
|
|
241
|
+
*
|
|
242
|
+
* @returns The WebSocket connection (for shutdown coordination)
|
|
243
|
+
*/
|
|
187
244
|
export async function startIndexer(opts) {
|
|
188
245
|
const { relayUrl, collections, cursor, fetchTimeout } = opts;
|
|
189
246
|
if (opts.ftsRebuildInterval != null)
|
|
@@ -193,6 +250,7 @@ export async function startIndexer(opts) {
|
|
|
193
250
|
indexerPinnedRepos = opts.pinnedRepos || null;
|
|
194
251
|
indexerFetchTimeout = fetchTimeout;
|
|
195
252
|
indexerMaxRetries = opts.maxRetries;
|
|
253
|
+
maxConcurrentBackfills = opts.parallelism ?? 3;
|
|
196
254
|
// Pre-populate repo status cache from DB so non-signal updates
|
|
197
255
|
// (e.g. profile changes) are processed for already-tracked DIDs
|
|
198
256
|
if (repoStatusCache.size === 0) {
|
|
@@ -202,7 +260,7 @@ export async function startIndexer(opts) {
|
|
|
202
260
|
}
|
|
203
261
|
log(`[indexer] Warmed repo status cache with ${statuses.length} entries`);
|
|
204
262
|
}
|
|
205
|
-
startMemoryDiagnostics()
|
|
263
|
+
// startMemoryDiagnostics()
|
|
206
264
|
let wsUrl = `${relayUrl}/xrpc/com.atproto.sync.subscribeRepos`;
|
|
207
265
|
if (cursor) {
|
|
208
266
|
wsUrl += `?cursor=${cursor}`;
|
|
@@ -231,9 +289,23 @@ export async function startIndexer(opts) {
|
|
|
231
289
|
});
|
|
232
290
|
return ws;
|
|
233
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Process a single firehose message. Decodes the CBOR header/body, filters
|
|
294
|
+
* for relevant collections, validates records against lexicons, and routes
|
|
295
|
+
* writes to the buffer (or pending buffer if the DID is mid-backfill).
|
|
296
|
+
*/
|
|
234
297
|
function processMessage(bytes, collections) {
|
|
235
298
|
const header = cborDecode(bytes, 0);
|
|
236
299
|
const body = cborDecode(bytes, header.offset);
|
|
300
|
+
// Handle identity events (handle changes)
|
|
301
|
+
if (header.value.t === '#identity') {
|
|
302
|
+
const did = body.value.did;
|
|
303
|
+
const handle = body.value.handle;
|
|
304
|
+
if (did && handle && repoStatusCache.has(did)) {
|
|
305
|
+
updateRepoHandle(did, handle).catch(() => { });
|
|
306
|
+
}
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
237
309
|
if (header.value.op !== 1 || header.value.t !== '#commit')
|
|
238
310
|
return;
|
|
239
311
|
if (!body.value.blocks || !body.value.ops)
|
|
@@ -264,7 +336,7 @@ function processMessage(bytes, collections) {
|
|
|
264
336
|
repoStatusCache.set(did, 'unknown');
|
|
265
337
|
}
|
|
266
338
|
if (hasSignalOp && (!indexerPinnedRepos || indexerPinnedRepos.has(did))) {
|
|
267
|
-
if (repoStatus === null && backfillInFlight.size <
|
|
339
|
+
if (repoStatus === null && backfillInFlight.size < maxConcurrentBackfills) {
|
|
268
340
|
repoStatusCache.set(did, 'pending');
|
|
269
341
|
triggerAutoBackfill(did);
|
|
270
342
|
}
|
package/dist/labels.d.ts
CHANGED
|
@@ -13,7 +13,36 @@ export interface LabelRuleContext {
|
|
|
13
13
|
value: Record<string, any>;
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
+
export interface LabelModule {
|
|
17
|
+
definition?: LabelDefinition;
|
|
18
|
+
evaluate?: (ctx: LabelRuleContext) => Promise<string[]>;
|
|
19
|
+
}
|
|
20
|
+
export declare function defineLabel(module: LabelModule): {
|
|
21
|
+
definition?: LabelDefinition;
|
|
22
|
+
evaluate?: (ctx: LabelRuleContext) => Promise<string[]>;
|
|
23
|
+
__type: "labels";
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Discover and load label rule modules from the `labels/` directory.
|
|
27
|
+
*
|
|
28
|
+
* Each module should default-export an object with an optional `definition`
|
|
29
|
+
* (label metadata like severity and blur behavior) and an optional `evaluate`
|
|
30
|
+
* function that returns label values to apply to a record.
|
|
31
|
+
*
|
|
32
|
+
* @param labelsDir - Absolute path to the `labels/` directory
|
|
33
|
+
*/
|
|
16
34
|
export declare function initLabels(labelsDir: string): Promise<void>;
|
|
35
|
+
/** Clear all registered label definitions and rules (for hot-reload). */
|
|
36
|
+
export declare function clearLabels(): void;
|
|
37
|
+
/** Register a single label module from a scanned server/ module. */
|
|
38
|
+
export declare function registerLabelModule(name: string, labelMod: {
|
|
39
|
+
definition?: LabelDefinition;
|
|
40
|
+
evaluate?: (ctx: LabelRuleContext) => Promise<string[]>;
|
|
41
|
+
}): void;
|
|
42
|
+
/**
|
|
43
|
+
* Evaluate all loaded label rules against a record and persist any resulting labels.
|
|
44
|
+
* Called after each record is indexed. Rule errors are logged but never block indexing.
|
|
45
|
+
*/
|
|
17
46
|
export declare function runLabelRules(record: {
|
|
18
47
|
uri: string;
|
|
19
48
|
cid: string;
|
|
@@ -21,9 +50,16 @@ export declare function runLabelRules(record: {
|
|
|
21
50
|
collection: string;
|
|
22
51
|
value: Record<string, any>;
|
|
23
52
|
}): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Re-evaluate all label rules against every existing record in the given collections.
|
|
55
|
+
* Used by `/admin/rescan-labels` to apply new or updated rules retroactively.
|
|
56
|
+
*
|
|
57
|
+
* @returns Count of records scanned and new labels applied
|
|
58
|
+
*/
|
|
24
59
|
export declare function rescanLabels(collections: string[]): Promise<{
|
|
25
60
|
scanned: number;
|
|
26
61
|
labeled: number;
|
|
27
62
|
}>;
|
|
63
|
+
/** Return all label definitions discovered during {@link initLabels}. */
|
|
28
64
|
export declare function getLabelDefinitions(): LabelDefinition[];
|
|
29
65
|
//# sourceMappingURL=labels.d.ts.map
|
package/dist/labels.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"labels.d.ts","sourceRoot":"","sources":["../src/labels.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"labels.d.ts","sourceRoot":"","sources":["../src/labels.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAIlD,wDAAwD;AACxD,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE;QACF,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAA;QACtD,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACtD,CAAA;IACD,MAAM,EAAE;QACN,GAAG,EAAE,MAAM,CAAA;QACX,GAAG,EAAE,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;AAED,MAAM,WAAW,WAAW;IAC1B,UAAU,CAAC,EAAE,eAAe,CAAA;IAC5B,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;CACxD;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW;iBAJhC,eAAe;eACjB,CAAC,GAAG,EAAE,gBAAgB,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC;;EAKxD;AAYD;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAmCjE;AAED,yEAAyE;AACzE,wBAAgB,WAAW,IAAI,IAAI,CAGlC;AAED,oEAAoE;AACpE,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE;IAAE,UAAU,CAAC,EAAE,eAAe,CAAC;IAAC,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;CAAE,GAClG,IAAI,CAON;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC3B,GAAG,OAAO,CAAC,IAAI,CAAC,CAyBhB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAuCvG;AAED,yEAAyE;AACzE,wBAAgB,mBAAmB,IAAI,eAAe,EAAE,CAEvD"}
|
package/dist/labels.js
CHANGED
|
@@ -6,13 +6,53 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
6
6
|
}
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
|
+
/**
|
|
10
|
+
* Label system for applying moderation labels to records as they are indexed.
|
|
11
|
+
*
|
|
12
|
+
* Place label modules in the `labels/` directory. Each module default-exports
|
|
13
|
+
* an object with a `definition` (label metadata) and/or an `evaluate` function
|
|
14
|
+
* (rule that returns label values for a given record).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // labels/nsfw.ts
|
|
19
|
+
* import type { LabelRuleContext } from '@hatk/hatk/labels'
|
|
20
|
+
*
|
|
21
|
+
* export default {
|
|
22
|
+
* definition: {
|
|
23
|
+
* identifier: 'nsfw',
|
|
24
|
+
* severity: 'alert',
|
|
25
|
+
* blurs: 'media',
|
|
26
|
+
* defaultSetting: 'warn',
|
|
27
|
+
* locales: [{ lang: 'en', name: 'NSFW', description: 'Not safe for work' }],
|
|
28
|
+
* },
|
|
29
|
+
*
|
|
30
|
+
* async evaluate(ctx: LabelRuleContext): Promise<string[]> {
|
|
31
|
+
* if (ctx.record.value.nsfw === true) return ['nsfw']
|
|
32
|
+
* return []
|
|
33
|
+
* },
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
9
37
|
import { resolve } from 'node:path';
|
|
10
38
|
import { readdirSync } from 'node:fs';
|
|
11
|
-
import { querySQL, runSQL, insertLabels, getSchema } from "./db.js";
|
|
39
|
+
import { querySQL, runSQL, insertLabels, getSchema } from "./database/db.js";
|
|
12
40
|
import { log, emit } from "./logger.js";
|
|
41
|
+
export function defineLabel(module) {
|
|
42
|
+
return { __type: 'labels', ...module };
|
|
43
|
+
}
|
|
13
44
|
const rules = [];
|
|
14
45
|
let labelDefs = [];
|
|
15
46
|
let labelSrc = 'self';
|
|
47
|
+
/**
|
|
48
|
+
* Discover and load label rule modules from the `labels/` directory.
|
|
49
|
+
*
|
|
50
|
+
* Each module should default-export an object with an optional `definition`
|
|
51
|
+
* (label metadata like severity and blur behavior) and an optional `evaluate`
|
|
52
|
+
* function that returns label values to apply to a record.
|
|
53
|
+
*
|
|
54
|
+
* @param labelsDir - Absolute path to the `labels/` directory
|
|
55
|
+
*/
|
|
16
56
|
export async function initLabels(labelsDir) {
|
|
17
57
|
let files;
|
|
18
58
|
try {
|
|
@@ -26,7 +66,7 @@ export async function initLabels(labelsDir) {
|
|
|
26
66
|
for (const file of files) {
|
|
27
67
|
const name = file.replace(/\.(ts|js)$/, '');
|
|
28
68
|
const scriptPath = resolve(labelsDir, file);
|
|
29
|
-
const mod = await import(__rewriteRelativeImportExtension(scriptPath));
|
|
69
|
+
const mod = await import(__rewriteRelativeImportExtension(/* @vite-ignore */ `${scriptPath}?t=${Date.now()}`));
|
|
30
70
|
const handler = mod.default;
|
|
31
71
|
if (handler.definition) {
|
|
32
72
|
labelDefs.push(handler.definition);
|
|
@@ -45,6 +85,24 @@ export async function initLabels(labelsDir) {
|
|
|
45
85
|
log(`[labels] ${labelDefs.length} label definitions loaded`);
|
|
46
86
|
}
|
|
47
87
|
}
|
|
88
|
+
/** Clear all registered label definitions and rules (for hot-reload). */
|
|
89
|
+
export function clearLabels() {
|
|
90
|
+
labelDefs.length = 0;
|
|
91
|
+
rules.length = 0;
|
|
92
|
+
}
|
|
93
|
+
/** Register a single label module from a scanned server/ module. */
|
|
94
|
+
export function registerLabelModule(name, labelMod) {
|
|
95
|
+
if (labelMod.definition) {
|
|
96
|
+
labelDefs.push(labelMod.definition);
|
|
97
|
+
}
|
|
98
|
+
if (labelMod.evaluate) {
|
|
99
|
+
rules.push({ name, evaluate: labelMod.evaluate });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Evaluate all loaded label rules against a record and persist any resulting labels.
|
|
104
|
+
* Called after each record is indexed. Rule errors are logged but never block indexing.
|
|
105
|
+
*/
|
|
48
106
|
export async function runLabelRules(record) {
|
|
49
107
|
if (rules.length === 0)
|
|
50
108
|
return;
|
|
@@ -69,15 +127,21 @@ export async function runLabelRules(record) {
|
|
|
69
127
|
emit('labels', 'applied', { count: allLabels.length, uri: record.uri, vals: allLabels.map((l) => l.val) });
|
|
70
128
|
}
|
|
71
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Re-evaluate all label rules against every existing record in the given collections.
|
|
132
|
+
* Used by `/admin/rescan-labels` to apply new or updated rules retroactively.
|
|
133
|
+
*
|
|
134
|
+
* @returns Count of records scanned and new labels applied
|
|
135
|
+
*/
|
|
72
136
|
export async function rescanLabels(collections) {
|
|
73
|
-
const beforeRows = await querySQL(`SELECT COUNT(*) as count FROM _labels`);
|
|
137
|
+
const beforeRows = (await querySQL(`SELECT COUNT(*) as count FROM _labels`));
|
|
74
138
|
const beforeCount = Number(beforeRows[0]?.count || 0);
|
|
75
139
|
let scanned = 0;
|
|
76
140
|
for (const collection of collections) {
|
|
77
141
|
const schema = getSchema(collection);
|
|
78
142
|
if (!schema)
|
|
79
143
|
continue;
|
|
80
|
-
const rows = await querySQL(`SELECT * FROM ${schema.tableName}`);
|
|
144
|
+
const rows = (await querySQL(`SELECT * FROM ${schema.tableName}`));
|
|
81
145
|
for (const row of rows) {
|
|
82
146
|
scanned++;
|
|
83
147
|
const value = {};
|
|
@@ -85,7 +149,7 @@ export async function rescanLabels(collections) {
|
|
|
85
149
|
let v = row[col.name];
|
|
86
150
|
if (v === null || v === undefined)
|
|
87
151
|
continue;
|
|
88
|
-
if (col.
|
|
152
|
+
if (col.isJson && typeof v === 'string') {
|
|
89
153
|
try {
|
|
90
154
|
v = JSON.parse(v);
|
|
91
155
|
}
|
|
@@ -102,10 +166,11 @@ export async function rescanLabels(collections) {
|
|
|
102
166
|
});
|
|
103
167
|
}
|
|
104
168
|
}
|
|
105
|
-
const afterRows = await querySQL(`SELECT COUNT(*) as count FROM _labels`);
|
|
169
|
+
const afterRows = (await querySQL(`SELECT COUNT(*) as count FROM _labels`));
|
|
106
170
|
const afterCount = Number(afterRows[0]?.count || 0);
|
|
107
171
|
return { scanned, labeled: afterCount - beforeCount };
|
|
108
172
|
}
|
|
173
|
+
/** Return all label definitions discovered during {@link initLabels}. */
|
|
109
174
|
export function getLabelDefinitions() {
|
|
110
175
|
return labelDefs;
|
|
111
176
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lexicon-resolve.d.ts","sourceRoot":"","sources":["../src/lexicon-resolve.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"lexicon-resolve.d.ts","sourceRoot":"","sources":["../src/lexicon-resolve.ts"],"names":[],"mappings":"AAoFA,UAAU,OAAO;IACf,OAAO,EAAE,MAAM,CAAA;IACf,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACnB;AAyFD;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAgChF"}
|