@vue-skuilder/db 0.2.35 → 0.2.36
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/CLAUDE.md +15 -4
- package/build.entries.mjs +12 -0
- package/dist/_virtual/_rolldown/runtime.mjs +8 -0
- package/dist/{contentSource-BteiDpxH.d.ts → contentSource-D_E7JaxR.d.ts} +1 -1
- package/dist/core/UserDBDebugger.mjs +285 -0
- package/dist/core/UserDBDebugger.mjs.map +1 -0
- package/dist/core/bulkImport/cardProcessor.mjs +106 -0
- package/dist/core/bulkImport/cardProcessor.mjs.map +1 -0
- package/dist/core/{index.js → index.cjs} +6438 -7784
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.ts +5 -5
- package/dist/core/index.mjs +20 -11368
- package/dist/core/interfaces/contentSource.mjs +20 -0
- package/dist/core/interfaces/contentSource.mjs.map +1 -0
- package/dist/core/navigators/Pipeline.mjs +723 -0
- package/dist/core/navigators/Pipeline.mjs.map +1 -0
- package/dist/core/navigators/PipelineAssembler.mjs +86 -0
- package/dist/core/navigators/PipelineAssembler.mjs.map +1 -0
- package/dist/core/navigators/PipelineDebugger.mjs +771 -0
- package/dist/core/navigators/PipelineDebugger.mjs.map +1 -0
- package/dist/core/navigators/SrsDebugger.mjs +34 -0
- package/dist/core/navigators/SrsDebugger.mjs.map +1 -0
- package/dist/core/navigators/StrategyPressureDebugger.mjs +19 -0
- package/dist/core/navigators/StrategyPressureDebugger.mjs.map +1 -0
- package/dist/core/navigators/defaults.mjs +96 -0
- package/dist/core/navigators/defaults.mjs.map +1 -0
- package/dist/core/navigators/diversityRerank.mjs +85 -0
- package/dist/core/navigators/diversityRerank.mjs.map +1 -0
- package/dist/core/navigators/filters/WeightedFilter.mjs +81 -0
- package/dist/core/navigators/filters/WeightedFilter.mjs.map +1 -0
- package/dist/core/navigators/filters/eloDistance.mjs +244 -0
- package/dist/core/navigators/filters/eloDistance.mjs.map +1 -0
- package/dist/core/navigators/filters/hierarchyDefinition.mjs +235 -0
- package/dist/core/navigators/filters/hierarchyDefinition.mjs.map +1 -0
- package/dist/core/navigators/filters/interferenceMitigator.mjs +201 -0
- package/dist/core/navigators/filters/interferenceMitigator.mjs.map +1 -0
- package/dist/core/navigators/filters/relativePriority.mjs +135 -0
- package/dist/core/navigators/filters/relativePriority.mjs.map +1 -0
- package/dist/core/navigators/filters/userTagPreference.mjs +101 -0
- package/dist/core/navigators/filters/userTagPreference.mjs.map +1 -0
- package/dist/core/navigators/generators/CompositeGenerator.mjs +203 -0
- package/dist/core/navigators/generators/CompositeGenerator.mjs.map +1 -0
- package/dist/core/navigators/generators/elo.mjs +77 -0
- package/dist/core/navigators/generators/elo.mjs.map +1 -0
- package/dist/core/navigators/generators/prescribed.mjs +676 -0
- package/dist/core/navigators/generators/prescribed.mjs.map +1 -0
- package/dist/core/navigators/generators/srs.mjs +158 -0
- package/dist/core/navigators/generators/srs.mjs.map +1 -0
- package/dist/core/navigators/index.mjs +345 -0
- package/dist/core/navigators/index.mjs.map +1 -0
- package/dist/core/navigators/pipelinePlan.mjs +109 -0
- package/dist/core/navigators/pipelinePlan.mjs.map +1 -0
- package/dist/core/navigators/regulators.mjs +128 -0
- package/dist/core/navigators/regulators.mjs.map +1 -0
- package/dist/core/orchestration/gradient.mjs +101 -0
- package/dist/core/orchestration/gradient.mjs.map +1 -0
- package/dist/core/orchestration/index.mjs +115 -0
- package/dist/core/orchestration/index.mjs.map +1 -0
- package/dist/core/orchestration/learning.mjs +140 -0
- package/dist/core/orchestration/learning.mjs.map +1 -0
- package/dist/core/orchestration/recording.mjs +64 -0
- package/dist/core/orchestration/recording.mjs.map +1 -0
- package/dist/core/orchestration/signal.mjs +40 -0
- package/dist/core/orchestration/signal.mjs.map +1 -0
- package/dist/core/types/contentNavigationStrategy.mjs +10 -0
- package/dist/core/types/contentNavigationStrategy.mjs.map +1 -0
- package/dist/core/types/hydration.mjs +13 -0
- package/dist/core/types/hydration.mjs.map +1 -0
- package/dist/core/types/strategyState.mjs +16 -0
- package/dist/core/types/strategyState.mjs.map +1 -0
- package/dist/core/types/studySession.mjs +14 -0
- package/dist/core/types/studySession.mjs.map +1 -0
- package/dist/core/types/types-legacy.mjs +44 -0
- package/dist/core/types/types-legacy.mjs.map +1 -0
- package/dist/core/util/index.mjs +31 -0
- package/dist/core/util/index.mjs.map +1 -0
- package/dist/core/versionStamps.mjs +46 -0
- package/dist/core/versionStamps.mjs.map +1 -0
- package/dist/courseConfigRegistration.mjs +306 -0
- package/dist/courseConfigRegistration.mjs.map +1 -0
- package/dist/{dataLayerProvider-C_QWzHf-.d.ts → dataLayerProvider-BDshhSp_.d.ts} +1 -1
- package/dist/diagnostics/dataset.mjs +118 -0
- package/dist/diagnostics/dataset.mjs.map +1 -0
- package/dist/diagnostics/derive.mjs +318 -0
- package/dist/diagnostics/derive.mjs.map +1 -0
- package/dist/diagnostics/{index.js → index.cjs} +1 -1
- package/dist/diagnostics/index.d.ts +3 -3
- package/dist/diagnostics/index.mjs +7 -1042
- package/dist/diagnostics/loader.mjs +151 -0
- package/dist/diagnostics/loader.mjs.map +1 -0
- package/dist/diagnostics/pipeline.mjs +161 -0
- package/dist/diagnostics/pipeline.mjs.map +1 -0
- package/dist/diagnostics/tagElo.mjs +177 -0
- package/dist/diagnostics/tagElo.mjs.map +1 -0
- package/dist/diagnostics/text.mjs +174 -0
- package/dist/diagnostics/text.mjs.map +1 -0
- package/dist/factory.mjs +70 -0
- package/dist/factory.mjs.map +1 -0
- package/dist/impl/common/BaseUserDB.mjs +1103 -0
- package/dist/impl/common/BaseUserDB.mjs.map +1 -0
- package/dist/impl/common/userDBHelpers.mjs +49 -0
- package/dist/impl/common/userDBHelpers.mjs.map +1 -0
- package/dist/impl/common/userDBKeys.mjs +45 -0
- package/dist/impl/common/userDBKeys.mjs.map +1 -0
- package/dist/impl/couch/CouchDBSyncStrategy.mjs +272 -0
- package/dist/impl/couch/CouchDBSyncStrategy.mjs.map +1 -0
- package/dist/impl/couch/CourseSyncService.mjs +242 -0
- package/dist/impl/couch/CourseSyncService.mjs.map +1 -0
- package/dist/impl/couch/PouchDataLayerProvider.mjs +74 -0
- package/dist/impl/couch/PouchDataLayerProvider.mjs.map +1 -0
- package/dist/impl/couch/adminDB.mjs +61 -0
- package/dist/impl/couch/adminDB.mjs.map +1 -0
- package/dist/impl/couch/auth.mjs +33 -0
- package/dist/impl/couch/auth.mjs.map +1 -0
- package/dist/impl/couch/classroomDB.mjs +227 -0
- package/dist/impl/couch/classroomDB.mjs.map +1 -0
- package/dist/impl/couch/clientCache.mjs +15 -0
- package/dist/impl/couch/clientCache.mjs.map +1 -0
- package/dist/impl/couch/courseAPI.mjs +183 -0
- package/dist/impl/couch/courseAPI.mjs.map +1 -0
- package/dist/impl/couch/courseDB.mjs +729 -0
- package/dist/impl/couch/courseDB.mjs.map +1 -0
- package/dist/impl/couch/courseLookupDB.mjs +94 -0
- package/dist/impl/couch/courseLookupDB.mjs.map +1 -0
- package/dist/impl/couch/{index.js → index.cjs} +6911 -10367
- package/dist/impl/couch/index.cjs.map +1 -0
- package/dist/impl/couch/index.d.ts +12 -5
- package/dist/impl/couch/index.mjs +134 -12146
- package/dist/impl/couch/index.mjs.map +1 -1
- package/dist/impl/couch/pouchdb-setup.mjs +15 -0
- package/dist/impl/couch/pouchdb-setup.mjs.map +1 -0
- package/dist/impl/couch/updateQueue.mjs +101 -0
- package/dist/impl/couch/updateQueue.mjs.map +1 -0
- package/dist/impl/couch/user-course-relDB.mjs +55 -0
- package/dist/impl/couch/user-course-relDB.mjs.map +1 -0
- package/dist/impl/static/NoOpSyncStrategy.mjs +49 -0
- package/dist/impl/static/NoOpSyncStrategy.mjs.map +1 -0
- package/dist/impl/static/StaticDataLayerProvider.mjs +94 -0
- package/dist/impl/static/StaticDataLayerProvider.mjs.map +1 -0
- package/dist/impl/static/StaticDataUnpacker.mjs +311 -0
- package/dist/impl/static/StaticDataUnpacker.mjs.map +1 -0
- package/dist/impl/static/courseDB.mjs +368 -0
- package/dist/impl/static/courseDB.mjs.map +1 -0
- package/dist/impl/static/coursesDB.mjs +38 -0
- package/dist/impl/static/coursesDB.mjs.map +1 -0
- package/dist/impl/static/{index.js → index.cjs} +2461 -5992
- package/dist/impl/static/index.cjs.map +1 -0
- package/dist/impl/static/index.d.ts +4 -4
- package/dist/impl/static/index.mjs +6 -10741
- package/dist/{index-C745YB_6.d.ts → index-BLDpFYG3.d.ts} +1 -1
- package/dist/{index.js → index.cjs} +14070 -14239
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +12 -23
- package/dist/index.mjs +38 -16937
- package/dist/pouch/{index.js → index.cjs} +5 -1
- package/dist/pouch/index.cjs.map +1 -0
- package/dist/pouch/index.mjs +2 -19
- package/dist/study/ItemQueue.mjs +95 -0
- package/dist/study/ItemQueue.mjs.map +1 -0
- package/dist/study/MixerDebugger.mjs +303 -0
- package/dist/study/MixerDebugger.mjs.map +1 -0
- package/dist/study/SessionController.mjs +1187 -0
- package/dist/study/SessionController.mjs.map +1 -0
- package/dist/study/SessionDebugger.mjs +271 -0
- package/dist/study/SessionDebugger.mjs.map +1 -0
- package/dist/study/SessionOverlay.mjs +407 -0
- package/dist/study/SessionOverlay.mjs.map +1 -0
- package/dist/study/SourceMixer.mjs +45 -0
- package/dist/study/SourceMixer.mjs.map +1 -0
- package/dist/study/SpacedRepetition.mjs +108 -0
- package/dist/study/SpacedRepetition.mjs.map +1 -0
- package/dist/study/TagFilteredContentSource.mjs +136 -0
- package/dist/study/TagFilteredContentSource.mjs.map +1 -0
- package/dist/study/services/CardHydrationService.mjs +172 -0
- package/dist/study/services/CardHydrationService.mjs.map +1 -0
- package/dist/study/services/EloService.mjs +217 -0
- package/dist/study/services/EloService.mjs.map +1 -0
- package/dist/study/services/ResponseProcessor.mjs +190 -0
- package/dist/study/services/ResponseProcessor.mjs.map +1 -0
- package/dist/study/services/SrsService.mjs +49 -0
- package/dist/study/services/SrsService.mjs.map +1 -0
- package/dist/study/services/tagRecent.mjs +65 -0
- package/dist/study/services/tagRecent.mjs.map +1 -0
- package/dist/{types-DF0JuPbs.d.ts → types-DChCz22D.d.ts} +1 -1
- package/dist/{types-legacy-DdVjg81H.d.cts → types-legacy-fT5Y1Wuy.d.ts} +1 -1
- package/dist/userDBKeys-ne1g4Xuq.d.ts +16 -0
- package/dist/util/Loggable.mjs +14 -0
- package/dist/util/Loggable.mjs.map +1 -0
- package/dist/util/dataDirectory.mjs +47 -0
- package/dist/util/dataDirectory.mjs.map +1 -0
- package/dist/util/logger.mjs +86 -0
- package/dist/util/logger.mjs.map +1 -0
- package/dist/util/migrator/FileSystemAdapter.mjs +18 -0
- package/dist/util/migrator/FileSystemAdapter.mjs.map +1 -0
- package/dist/util/migrator/StaticToCouchDBMigrator.mjs +428 -0
- package/dist/util/migrator/StaticToCouchDBMigrator.mjs.map +1 -0
- package/dist/util/migrator/types.mjs +11 -0
- package/dist/util/migrator/types.mjs.map +1 -0
- package/dist/util/migrator/validation.mjs +334 -0
- package/dist/util/migrator/validation.mjs.map +1 -0
- package/dist/util/packer/CouchDBToStaticPacker.mjs +438 -0
- package/dist/util/packer/CouchDBToStaticPacker.mjs.map +1 -0
- package/dist/util/packer/{index.js → index.cjs} +73 -9
- package/dist/util/packer/index.cjs.map +1 -0
- package/dist/util/packer/index.d.ts +3 -3
- package/dist/util/packer/index.mjs +2 -537
- package/eslint.config.mjs +9 -1
- package/package.json +25 -13
- package/rolldown.config.mjs +44 -0
- package/src/core/UserDBDebugger.ts +47 -46
- package/src/core/navigators/Pipeline.ts +10 -10
- package/src/core/navigators/PipelineDebugger.ts +55 -55
- package/src/core/navigators/filters/eloDistance.ts +1 -1
- package/src/core/navigators/generators/elo.ts +5 -1
- package/src/core/navigators/index.ts +9 -3
- package/src/factory.ts +10 -1
- package/src/impl/common/BaseUserDB.ts +1 -3
- package/src/impl/common/index.ts +2 -0
- package/src/impl/common/userDBHelpers.ts +4 -58
- package/src/impl/common/userDBKeys.ts +64 -0
- package/src/impl/couch/courseDB.ts +15 -3
- package/src/impl/couch/index.ts +2 -18
- package/src/impl/couch/pouchdb-setup.ts +8 -0
- package/src/index.ts +1 -1
- package/src/study/MixerDebugger.ts +46 -46
- package/src/study/SessionDebugger.ts +20 -20
- package/src/study/SessionOverlay.ts +4 -4
- package/src/util/logger.ts +112 -13
- package/tests/core/navigators/eloGenerator.test.ts +36 -0
- package/tsup.config.ts +24 -21
- package/dist/SyncStrategy-BXliog1m.d.cts +0 -116
- package/dist/classroomDB-DLTtLZct.d.cts +0 -86
- package/dist/contentSource-FQZcZopC.d.cts +0 -2007
- package/dist/core/index.d.cts +0 -521
- package/dist/core/index.js.map +0 -1
- package/dist/core/index.mjs.map +0 -1
- package/dist/dataLayerProvider-3y21KtF-.d.cts +0 -61
- package/dist/diagnostics/index.d.cts +0 -561
- package/dist/diagnostics/index.mjs.map +0 -1
- package/dist/impl/couch/index.d.cts +0 -620
- package/dist/impl/couch/index.js.map +0 -1
- package/dist/impl/static/index.d.cts +0 -232
- package/dist/impl/static/index.js.map +0 -1
- package/dist/impl/static/index.mjs.map +0 -1
- package/dist/index-BIum9Ue2.d.cts +0 -137
- package/dist/index.d.cts +0 -1663
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/pouch/index.d.cts +0 -1
- package/dist/pouch/index.js.map +0 -1
- package/dist/pouch/index.mjs.map +0 -1
- package/dist/types-BcWVH4ww.d.cts +0 -58
- package/dist/types-legacy-DdVjg81H.d.ts +0 -168
- package/dist/util/packer/index.d.cts +0 -5
- package/dist/util/packer/index.js.map +0 -1
- package/dist/util/packer/index.mjs.map +0 -1
- package/src/study/getCardDataShape.ts +0 -34
- /package/dist/diagnostics/{index.js.map → index.cjs.map} +0 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { __name } from "../../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { logger } from "../../util/logger.mjs";
|
|
3
|
+
import { DocTypePrefixes } from "../../core/types/types-legacy.mjs";
|
|
4
|
+
//#region src/impl/static/StaticDataUnpacker.ts
|
|
5
|
+
const pathUtils = { isAbsolute: (path) => {
|
|
6
|
+
if (/^[a-zA-Z]:[\\/]/.test(path) || /^\\\\/.test(path)) return true;
|
|
7
|
+
if (path.startsWith("/")) return true;
|
|
8
|
+
return false;
|
|
9
|
+
} };
|
|
10
|
+
let nodeFS = null;
|
|
11
|
+
try {
|
|
12
|
+
if (typeof window === "undefined" && typeof process !== "undefined" && process.versions?.node) nodeFS = eval("require")("fs");
|
|
13
|
+
} catch {}
|
|
14
|
+
var StaticDataUnpacker = class {
|
|
15
|
+
manifest;
|
|
16
|
+
basePath;
|
|
17
|
+
documentCache = /* @__PURE__ */ new Map();
|
|
18
|
+
chunkCache = /* @__PURE__ */ new Map();
|
|
19
|
+
indexCache = /* @__PURE__ */ new Map();
|
|
20
|
+
constructor(manifest, basePath) {
|
|
21
|
+
this.manifest = manifest;
|
|
22
|
+
this.basePath = basePath;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get a document by ID, loading from appropriate chunk if needed
|
|
26
|
+
*/
|
|
27
|
+
async getDocument(id) {
|
|
28
|
+
if (this.documentCache.has(id)) {
|
|
29
|
+
const doc = this.documentCache.get(id);
|
|
30
|
+
return await this.hydrateAttachments(doc);
|
|
31
|
+
}
|
|
32
|
+
const chunk = await this.findChunkForDocument(id);
|
|
33
|
+
if (!chunk) {
|
|
34
|
+
logger.error(`Document ${id} not found in any chunk. Available chunks:`, this.manifest.chunks.map((c) => `${c.id} (${c.docType}): ${c.startKey} - ${c.endKey}`));
|
|
35
|
+
throw new Error(`Document ${id} not found in any chunk`);
|
|
36
|
+
}
|
|
37
|
+
await this.loadChunk(chunk.id);
|
|
38
|
+
if (this.documentCache.has(id)) {
|
|
39
|
+
const doc = this.documentCache.get(id);
|
|
40
|
+
return await this.hydrateAttachments(doc);
|
|
41
|
+
}
|
|
42
|
+
logger.error(`Document ${id} not found in chunk ${chunk.id}`);
|
|
43
|
+
throw new Error(`Document ${id} not found in chunk ${chunk.id}`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get all documents with IDs starting with a specific prefix.
|
|
47
|
+
*
|
|
48
|
+
* This method loads the relevant chunk(s) and returns all matching documents.
|
|
49
|
+
* Useful for querying documents by type (e.g., all NAVIGATION_STRATEGY documents).
|
|
50
|
+
*
|
|
51
|
+
* @param prefix - Document ID prefix to match (e.g., "NAVIGATION_STRATEGY")
|
|
52
|
+
* @returns Array of all documents with IDs starting with the prefix
|
|
53
|
+
*/
|
|
54
|
+
async getAllDocumentsByPrefix(prefix) {
|
|
55
|
+
const relevantChunks = this.manifest.chunks.filter((chunk) => {
|
|
56
|
+
const prefixEnd = prefix + "";
|
|
57
|
+
return chunk.startKey <= prefixEnd && chunk.endKey >= prefix;
|
|
58
|
+
});
|
|
59
|
+
if (relevantChunks.length === 0) {
|
|
60
|
+
logger.debug(`[StaticDataUnpacker] No chunks found for prefix: ${prefix}`);
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
await Promise.all(relevantChunks.map((chunk) => this.loadChunk(chunk.id)));
|
|
64
|
+
const matchingDocs = [];
|
|
65
|
+
for (const [docId, doc] of this.documentCache.entries()) if (docId.startsWith(prefix)) matchingDocs.push(await this.hydrateAttachments(doc));
|
|
66
|
+
logger.debug(`[StaticDataUnpacker] Found ${matchingDocs.length} documents with prefix: ${prefix}`);
|
|
67
|
+
return matchingDocs;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Query cards by ELO score, returning card IDs sorted by ELO
|
|
71
|
+
*/
|
|
72
|
+
async queryByElo(targetElo, limit = 25) {
|
|
73
|
+
const eloIndex = await this.loadIndex("elo");
|
|
74
|
+
if (!eloIndex || !eloIndex.sorted) {
|
|
75
|
+
logger.warn("ELO index not found or malformed, returning empty results");
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
const sorted = eloIndex.sorted;
|
|
79
|
+
let startIndex = 0;
|
|
80
|
+
let left = 0;
|
|
81
|
+
let right = sorted.length - 1;
|
|
82
|
+
while (left <= right) {
|
|
83
|
+
const mid = Math.floor((left + right) / 2);
|
|
84
|
+
if (sorted[mid].elo < targetElo) left = mid + 1;
|
|
85
|
+
else right = mid - 1;
|
|
86
|
+
}
|
|
87
|
+
startIndex = left;
|
|
88
|
+
const result = [];
|
|
89
|
+
const halfLimit = Math.floor(limit / 2);
|
|
90
|
+
for (let i = Math.max(0, startIndex - halfLimit); i < startIndex && result.length < limit; i++) result.push(sorted[i].cardId);
|
|
91
|
+
for (let i = startIndex; i < sorted.length && result.length < limit; i++) result.push(sorted[i].cardId);
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get all tag names mapped to their card arrays
|
|
96
|
+
*/
|
|
97
|
+
async getTagsIndex() {
|
|
98
|
+
try {
|
|
99
|
+
return await this.loadIndex("tags");
|
|
100
|
+
} catch {
|
|
101
|
+
return {
|
|
102
|
+
byCard: {},
|
|
103
|
+
byTag: {}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
getDocTypeFromId(id) {
|
|
108
|
+
for (const docTypeKey in DocTypePrefixes) {
|
|
109
|
+
const prefix = DocTypePrefixes[docTypeKey];
|
|
110
|
+
if (id.startsWith(`${prefix}-`)) return docTypeKey;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Find which chunk contains a specific document ID
|
|
115
|
+
*/
|
|
116
|
+
async findChunkForDocument(docId) {
|
|
117
|
+
const expectedDocType = this.getDocTypeFromId(docId);
|
|
118
|
+
if (expectedDocType) {
|
|
119
|
+
const typeChunks = this.manifest.chunks.filter((c) => c.docType === expectedDocType);
|
|
120
|
+
for (const chunk of typeChunks) if (docId >= chunk.startKey && docId <= chunk.endKey) {
|
|
121
|
+
if (await this.verifyDocumentInChunk(docId, chunk)) return chunk;
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
for (const chunk of this.manifest.chunks) if (docId >= chunk.startKey && docId <= chunk.endKey) {
|
|
125
|
+
if (await this.verifyDocumentInChunk(docId, chunk)) return chunk;
|
|
126
|
+
}
|
|
127
|
+
const otherChunks = this.manifest.chunks.filter((c) => c.docType !== "CARD" && c.docType !== "DISPLAYABLE_DATA" && c.docType !== "TAG");
|
|
128
|
+
for (const chunk of otherChunks) if (docId >= chunk.startKey && docId <= chunk.endKey) {
|
|
129
|
+
if (await this.verifyDocumentInChunk(docId, chunk)) return chunk;
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Verify that a document actually exists in a specific chunk by loading and checking it
|
|
136
|
+
*/
|
|
137
|
+
async verifyDocumentInChunk(docId, chunk) {
|
|
138
|
+
try {
|
|
139
|
+
await this.loadChunk(chunk.id);
|
|
140
|
+
return this.documentCache.has(docId);
|
|
141
|
+
} catch {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Load a chunk file and cache its documents
|
|
147
|
+
*/
|
|
148
|
+
async loadChunk(chunkId) {
|
|
149
|
+
if (this.chunkCache.has(chunkId)) return;
|
|
150
|
+
const chunk = this.manifest.chunks.find((c) => c.id === chunkId);
|
|
151
|
+
if (!chunk) throw new Error(`Chunk ${chunkId} not found in manifest`);
|
|
152
|
+
try {
|
|
153
|
+
const chunkPath = `${this.basePath}/${chunk.path}`;
|
|
154
|
+
logger.debug(`Loading chunk from ${chunkPath}`);
|
|
155
|
+
let documents;
|
|
156
|
+
if (this.isLocalPath(chunkPath) && nodeFS) {
|
|
157
|
+
const fileContent = await nodeFS.promises.readFile(chunkPath, "utf8");
|
|
158
|
+
documents = JSON.parse(fileContent);
|
|
159
|
+
} else {
|
|
160
|
+
const response = await fetch(chunkPath);
|
|
161
|
+
if (!response.ok) throw new Error(`Failed to fetch chunk ${chunkId}: ${response.status} ${response.statusText}`);
|
|
162
|
+
documents = await response.json();
|
|
163
|
+
}
|
|
164
|
+
this.chunkCache.set(chunkId, documents);
|
|
165
|
+
for (const doc of documents) if (doc._id) this.documentCache.set(doc._id, doc);
|
|
166
|
+
logger.debug(`Loaded ${documents.length} documents from chunk ${chunkId}`);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
logger.error(`Failed to load chunk ${chunkId}:`, error);
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Load an index file and cache it
|
|
174
|
+
*/
|
|
175
|
+
async loadIndex(indexName) {
|
|
176
|
+
if (this.indexCache.has(indexName)) return this.indexCache.get(indexName);
|
|
177
|
+
const indexMeta = this.manifest.indices.find((idx) => idx.name === indexName);
|
|
178
|
+
if (!indexMeta) throw new Error(`Index ${indexName} not found in manifest`);
|
|
179
|
+
try {
|
|
180
|
+
const indexPath = `${this.basePath}/${indexMeta.path}`;
|
|
181
|
+
logger.debug(`Loading index from ${indexPath}`);
|
|
182
|
+
let indexData;
|
|
183
|
+
if (this.isLocalPath(indexPath) && nodeFS) {
|
|
184
|
+
const fileContent = await nodeFS.promises.readFile(indexPath, "utf8");
|
|
185
|
+
indexData = JSON.parse(fileContent);
|
|
186
|
+
} else {
|
|
187
|
+
const response = await fetch(indexPath);
|
|
188
|
+
if (!response.ok) throw new Error(`Failed to fetch index ${indexName}: ${response.status} ${response.statusText}`);
|
|
189
|
+
indexData = await response.json();
|
|
190
|
+
}
|
|
191
|
+
this.indexCache.set(indexName, indexData);
|
|
192
|
+
logger.debug(`Loaded index ${indexName}`);
|
|
193
|
+
return indexData;
|
|
194
|
+
} catch (error) {
|
|
195
|
+
logger.error(`Failed to load index ${indexName}:`, error);
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get a document by ID without hydration (raw document access)
|
|
201
|
+
* Used internally to avoid recursion during attachment hydration
|
|
202
|
+
*/
|
|
203
|
+
async getRawDocument(id) {
|
|
204
|
+
if (this.documentCache.has(id)) return this.documentCache.get(id);
|
|
205
|
+
const chunk = await this.findChunkForDocument(id);
|
|
206
|
+
if (!chunk) {
|
|
207
|
+
logger.error(`Document ${id} not found in any chunk. Available chunks:`, this.manifest.chunks.map((c) => `${c.id} (${c.docType}): ${c.startKey} - ${c.endKey}`));
|
|
208
|
+
throw new Error(`Document ${id} not found in any chunk`);
|
|
209
|
+
}
|
|
210
|
+
await this.loadChunk(chunk.id);
|
|
211
|
+
if (this.documentCache.has(id)) return this.documentCache.get(id);
|
|
212
|
+
logger.error(`Document ${id} not found in chunk ${chunk.id}`);
|
|
213
|
+
throw new Error(`Document ${id} not found in chunk ${chunk.id}`);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Get attachment URL for a given document and attachment name
|
|
217
|
+
*/
|
|
218
|
+
getAttachmentUrl(docId, attachmentName) {
|
|
219
|
+
return `${this.basePath}/attachments/${docId}/${attachmentName}`;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Load attachment data from the document and get the correct file path
|
|
223
|
+
* Uses raw document access to avoid recursion with hydration
|
|
224
|
+
*/
|
|
225
|
+
async getAttachmentPath(docId, attachmentName) {
|
|
226
|
+
try {
|
|
227
|
+
const doc = await this.getRawDocument(docId);
|
|
228
|
+
if (doc._attachments && doc._attachments[attachmentName]) {
|
|
229
|
+
const attachment = doc._attachments[attachmentName];
|
|
230
|
+
if (attachment.path) return `${this.basePath}/${attachment.path}`;
|
|
231
|
+
}
|
|
232
|
+
return null;
|
|
233
|
+
} catch {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Load attachment as a blob (for browser) or buffer (for Node.js)
|
|
239
|
+
*/
|
|
240
|
+
async getAttachmentBlob(docId, attachmentName) {
|
|
241
|
+
const attachmentPath = await this.getAttachmentPath(docId, attachmentName);
|
|
242
|
+
if (!attachmentPath) return null;
|
|
243
|
+
try {
|
|
244
|
+
if (this.isLocalPath(attachmentPath) && nodeFS) return await nodeFS.promises.readFile(attachmentPath);
|
|
245
|
+
else {
|
|
246
|
+
const response = await fetch(attachmentPath);
|
|
247
|
+
if (!response.ok) throw new Error(`Failed to fetch attachment ${docId}/${attachmentName}: ${response.status} ${response.statusText}`);
|
|
248
|
+
return await response.blob();
|
|
249
|
+
}
|
|
250
|
+
} catch (error) {
|
|
251
|
+
logger.error(`Failed to load attachment ${docId}/${attachmentName}:`, error);
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Hydrate document attachments by converting file paths to blob URLs
|
|
257
|
+
*/
|
|
258
|
+
async hydrateAttachments(doc) {
|
|
259
|
+
const typedDoc = doc;
|
|
260
|
+
if (!typedDoc._attachments) return doc;
|
|
261
|
+
const hydratedDoc = JSON.parse(JSON.stringify(doc));
|
|
262
|
+
for (const [attachmentName, attachment] of Object.entries(typedDoc._attachments)) {
|
|
263
|
+
const attachmentData = attachment;
|
|
264
|
+
if (attachmentData.path) try {
|
|
265
|
+
const blob = await this.getAttachmentBlob(typedDoc._id, attachmentName);
|
|
266
|
+
if (blob) if (typeof window !== "undefined" && window.URL) hydratedDoc._attachments[attachmentName] = {
|
|
267
|
+
...attachmentData,
|
|
268
|
+
data: blob,
|
|
269
|
+
stub: false
|
|
270
|
+
};
|
|
271
|
+
else hydratedDoc._attachments[attachmentName] = {
|
|
272
|
+
...attachmentData,
|
|
273
|
+
buffer: blob
|
|
274
|
+
};
|
|
275
|
+
else logger.warn(`[hydrateAttachments] getAttachmentBlob returned null for ${typedDoc._id}/${attachmentName}. Skipping hydration for this attachment.`);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
logger.warn(`[hydrateAttachments] Failed to hydrate attachment ${typedDoc._id}/${attachmentName}:`, error);
|
|
278
|
+
}
|
|
279
|
+
else logger.debug(`[hydrateAttachments] Attachment ${attachmentName} for doc ${typedDoc._id} has no path. Skipping blob conversion.`);
|
|
280
|
+
}
|
|
281
|
+
return hydratedDoc;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Clear all caches (useful for testing or memory management)
|
|
285
|
+
*/
|
|
286
|
+
clearCaches() {
|
|
287
|
+
this.documentCache.clear();
|
|
288
|
+
this.chunkCache.clear();
|
|
289
|
+
this.indexCache.clear();
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Get cache statistics for debugging
|
|
293
|
+
*/
|
|
294
|
+
getCacheStats() {
|
|
295
|
+
return {
|
|
296
|
+
documents: this.documentCache.size,
|
|
297
|
+
chunks: this.chunkCache.size,
|
|
298
|
+
indices: this.indexCache.size
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Check if a path is a local file path (vs URL)
|
|
303
|
+
*/
|
|
304
|
+
isLocalPath(filePath) {
|
|
305
|
+
return !filePath.startsWith("http://") && !filePath.startsWith("https://") && (pathUtils.isAbsolute(filePath) || filePath.startsWith("./") || filePath.startsWith("../"));
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
//#endregion
|
|
309
|
+
export { StaticDataUnpacker };
|
|
310
|
+
|
|
311
|
+
//# sourceMappingURL=StaticDataUnpacker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StaticDataUnpacker.mjs","names":[],"sources":["../../../src/impl/static/StaticDataUnpacker.ts"],"sourcesContent":["// packages/db/src/impl/static/StaticDataUnpacker.ts\n\nimport { StaticCourseManifest, ChunkMetadata } from '../../util/packer/types';\nimport { logger } from '../../util/logger';\nimport { DocType, DocTypePrefixes } from '@db/core';\n\n// Browser-compatible path utilities\nconst pathUtils = {\n isAbsolute: (path: string): boolean => {\n // Check for Windows absolute paths (C:\\ or \\\\server\\)\n if (/^[a-zA-Z]:[\\\\/]/.test(path) || /^\\\\\\\\/.test(path)) {\n return true;\n }\n // Check for Unix absolute paths (/)\n if (path.startsWith('/')) {\n return true;\n }\n return false;\n },\n};\n\n// Check if we're in Node.js environment and fs is available\nlet nodeFS: any = null;\ntry {\n // Use eval to prevent bundlers from including fs in browser builds\n if (typeof window === 'undefined' && typeof process !== 'undefined' && process.versions?.node) {\n nodeFS = eval('require')('fs');\n }\n} catch {\n // fs not available, will use fetch\n}\n\ninterface EloIndexEntry {\n elo: number;\n cardId: string;\n}\n\ninterface EloIndex {\n sorted: EloIndexEntry[];\n buckets: Record<string, EloIndexEntry[]>;\n stats: {\n min: number;\n max: number;\n count: number;\n };\n}\n\ninterface TagsIndex {\n byTag: Record<string, string[]>; // tagName -> cardIds\n byCard: Record<string, string[]>; // cardId -> tagNames\n}\n\nexport class StaticDataUnpacker {\n private manifest: StaticCourseManifest;\n private basePath: string;\n private documentCache: Map<string, any> = new Map();\n private chunkCache: Map<string, any[]> = new Map();\n private indexCache: Map<string, any> = new Map();\n\n constructor(manifest: StaticCourseManifest, basePath: string) {\n this.manifest = manifest;\n this.basePath = basePath;\n }\n\n /**\n * Get a document by ID, loading from appropriate chunk if needed\n */\n async getDocument<T = any>(id: string): Promise<T> {\n // Check document cache first\n if (this.documentCache.has(id)) {\n const doc = this.documentCache.get(id);\n return await this.hydrateAttachments(doc);\n }\n\n // Find which chunk contains this document\n const chunk = await this.findChunkForDocument(id);\n if (!chunk) {\n logger.error(\n `Document ${id} not found in any chunk. Available chunks:`,\n this.manifest.chunks.map((c) => `${c.id} (${c.docType}): ${c.startKey} - ${c.endKey}`)\n );\n throw new Error(`Document ${id} not found in any chunk`);\n }\n\n // Load the chunk if not cached\n await this.loadChunk(chunk.id);\n\n // Try to get the document from cache again\n if (this.documentCache.has(id)) {\n const doc = this.documentCache.get(id);\n return await this.hydrateAttachments(doc);\n }\n\n logger.error(`Document ${id} not found in chunk ${chunk.id}`);\n throw new Error(`Document ${id} not found in chunk ${chunk.id}`);\n }\n\n /**\n * Get all documents with IDs starting with a specific prefix.\n *\n * This method loads the relevant chunk(s) and returns all matching documents.\n * Useful for querying documents by type (e.g., all NAVIGATION_STRATEGY documents).\n *\n * @param prefix - Document ID prefix to match (e.g., \"NAVIGATION_STRATEGY\")\n * @returns Array of all documents with IDs starting with the prefix\n */\n async getAllDocumentsByPrefix(prefix: string): Promise<any[]> {\n // Find all chunks that could contain documents with this prefix\n // A chunk contains documents if the prefix falls within its startKey/endKey range\n const relevantChunks = this.manifest.chunks.filter((chunk) => {\n // Check if prefix could be in this chunk's range\n // Prefix matches if it's >= startKey and <= endKey (for lexicographic ordering)\n const prefixEnd = prefix + '\\ufff0'; // High unicode character for range end\n return chunk.startKey <= prefixEnd && chunk.endKey >= prefix;\n });\n\n if (relevantChunks.length === 0) {\n logger.debug(`[StaticDataUnpacker] No chunks found for prefix: ${prefix}`);\n return [];\n }\n\n // Load all relevant chunks\n await Promise.all(relevantChunks.map((chunk) => this.loadChunk(chunk.id)));\n\n // Filter documents from cache that match the prefix\n const matchingDocs: any[] = [];\n for (const [docId, doc] of this.documentCache.entries()) {\n if (docId.startsWith(prefix)) {\n matchingDocs.push(await this.hydrateAttachments(doc));\n }\n }\n\n logger.debug(\n `[StaticDataUnpacker] Found ${matchingDocs.length} documents with prefix: ${prefix}`\n );\n return matchingDocs;\n }\n\n /**\n * Query cards by ELO score, returning card IDs sorted by ELO\n */\n async queryByElo(targetElo: number, limit: number = 25): Promise<string[]> {\n const eloIndex = (await this.loadIndex('elo')) as EloIndex;\n\n if (!eloIndex || !eloIndex.sorted) {\n logger.warn('ELO index not found or malformed, returning empty results');\n return [];\n }\n\n // Find cards near the target ELO\n const sorted = eloIndex.sorted;\n let startIndex = 0;\n\n // Binary search to find insertion point for target ELO\n let left = 0;\n let right = sorted.length - 1;\n while (left <= right) {\n const mid = Math.floor((left + right) / 2);\n if (sorted[mid].elo < targetElo) {\n left = mid + 1;\n } else {\n right = mid - 1;\n }\n }\n startIndex = left;\n\n // Collect cards around the target ELO\n const result: string[] = [];\n const halfLimit = Math.floor(limit / 2);\n\n // Get cards below target ELO\n for (\n let i = Math.max(0, startIndex - halfLimit);\n i < startIndex && result.length < limit;\n i++\n ) {\n result.push(sorted[i].cardId);\n }\n\n // Get cards at or above target ELO\n for (let i = startIndex; i < sorted.length && result.length < limit; i++) {\n result.push(sorted[i].cardId);\n }\n\n return result;\n }\n\n /**\n * Get all tag names mapped to their card arrays\n */\n async getTagsIndex(): Promise<TagsIndex> {\n try {\n return (await this.loadIndex('tags')) as TagsIndex;\n } catch {\n // If no tags index exists, presume \"no tagged cards\"\n return {\n byCard: {},\n byTag: {},\n }\n }\n }\n\n private getDocTypeFromId(id: string): DocType | undefined {\n for (const docTypeKey in DocTypePrefixes) {\n const prefix = DocTypePrefixes[docTypeKey as DocType];\n if (id.startsWith(`${prefix}-`)) {\n return docTypeKey as DocType;\n }\n }\n return undefined;\n }\n\n /**\n * Find which chunk contains a specific document ID\n */\n private async findChunkForDocument(docId: string): Promise<ChunkMetadata | undefined> {\n const expectedDocType = this.getDocTypeFromId(docId);\n\n if (expectedDocType) {\n const typeChunks = this.manifest.chunks.filter((c) => c.docType === expectedDocType);\n\n for (const chunk of typeChunks) {\n if (docId >= chunk.startKey && docId <= chunk.endKey) {\n const exists = await this.verifyDocumentInChunk(docId, chunk);\n if (exists) {\n return chunk;\n }\n }\n }\n } else {\n // Fallback for documents without recognized prefixes (e.g., CourseConfig, or old documents)\n // This part remains for backward compatibility and non-prefixed documents.\n // It's less efficient but necessary if not all document types are prefixed.\n for (const chunk of this.manifest.chunks) {\n if (docId >= chunk.startKey && docId <= chunk.endKey) {\n // Verify document actually exists in chunk\n const exists = await this.verifyDocumentInChunk(docId, chunk);\n if (exists) {\n return chunk;\n }\n }\n }\n\n // Finally try any other chunk types\n const otherChunks = this.manifest.chunks.filter(\n (c) => c.docType !== 'CARD' && c.docType !== 'DISPLAYABLE_DATA' && c.docType !== 'TAG'\n );\n for (const chunk of otherChunks) {\n if (docId >= chunk.startKey && docId <= chunk.endKey) {\n // Verify document actually exists in chunk\n const exists = await this.verifyDocumentInChunk(docId, chunk);\n if (exists) {\n return chunk;\n }\n }\n }\n\n return undefined;\n }\n return undefined;\n }\n\n /**\n * Verify that a document actually exists in a specific chunk by loading and checking it\n */\n private async verifyDocumentInChunk(docId: string, chunk: ChunkMetadata): Promise<boolean> {\n try {\n // Load the chunk if not already cached\n await this.loadChunk(chunk.id);\n\n // Check if the document is now in our document cache\n return this.documentCache.has(docId);\n } catch {\n return false;\n }\n }\n\n /**\n * Load a chunk file and cache its documents\n */\n private async loadChunk(chunkId: string): Promise<void> {\n if (this.chunkCache.has(chunkId)) {\n return; // Already loaded\n }\n\n const chunk = this.manifest.chunks.find((c) => c.id === chunkId);\n if (!chunk) {\n throw new Error(`Chunk ${chunkId} not found in manifest`);\n }\n\n try {\n const chunkPath = `${this.basePath}/${chunk.path}`;\n logger.debug(`Loading chunk from ${chunkPath}`);\n\n let documents: any[];\n\n // Check if we're in a Node.js environment with local files\n if (this.isLocalPath(chunkPath) && nodeFS) {\n // Use fs for local file access (e.g., in tests)\n const fileContent = await nodeFS.promises.readFile(chunkPath, 'utf8');\n documents = JSON.parse(fileContent);\n } else {\n // Use fetch for URL-based access (e.g., in browser)\n const response = await fetch(chunkPath);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch chunk ${chunkId}: ${response.status} ${response.statusText}`\n );\n }\n documents = await response.json();\n }\n\n this.chunkCache.set(chunkId, documents);\n\n // Cache individual documents for quick lookup\n for (const doc of documents) {\n if (doc._id) {\n this.documentCache.set(doc._id, doc);\n }\n }\n\n logger.debug(`Loaded ${documents.length} documents from chunk ${chunkId}`);\n } catch (error) {\n logger.error(`Failed to load chunk ${chunkId}:`, error);\n throw error;\n }\n }\n\n /**\n * Load an index file and cache it\n */\n private async loadIndex(indexName: string): Promise<any> {\n if (this.indexCache.has(indexName)) {\n return this.indexCache.get(indexName);\n }\n\n const indexMeta = this.manifest.indices.find((idx) => idx.name === indexName);\n if (!indexMeta) {\n throw new Error(`Index ${indexName} not found in manifest`);\n }\n\n try {\n const indexPath = `${this.basePath}/${indexMeta.path}`;\n logger.debug(`Loading index from ${indexPath}`);\n\n let indexData: any;\n\n // Check if we're in a Node.js environment with local files\n if (this.isLocalPath(indexPath) && nodeFS) {\n // Use fs for local file access (e.g., in tests)\n const fileContent = await nodeFS.promises.readFile(indexPath, 'utf8');\n indexData = JSON.parse(fileContent);\n } else {\n // Use fetch for URL-based access (e.g., in browser)\n const response = await fetch(indexPath);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch index ${indexName}: ${response.status} ${response.statusText}`\n );\n }\n indexData = await response.json();\n }\n\n this.indexCache.set(indexName, indexData);\n\n logger.debug(`Loaded index ${indexName}`);\n return indexData;\n } catch (error) {\n logger.error(`Failed to load index ${indexName}:`, error);\n throw error;\n }\n }\n\n /**\n * Get a document by ID without hydration (raw document access)\n * Used internally to avoid recursion during attachment hydration\n */\n private async getRawDocument<T = any>(id: string): Promise<T> {\n // Check document cache first\n if (this.documentCache.has(id)) {\n return this.documentCache.get(id);\n }\n\n // Find which chunk contains this document\n const chunk = await this.findChunkForDocument(id);\n if (!chunk) {\n logger.error(\n `Document ${id} not found in any chunk. Available chunks:`,\n this.manifest.chunks.map((c) => `${c.id} (${c.docType}): ${c.startKey} - ${c.endKey}`)\n );\n throw new Error(`Document ${id} not found in any chunk`);\n }\n\n // Load the chunk if not cached\n await this.loadChunk(chunk.id);\n\n // Try to get the document from cache again\n if (this.documentCache.has(id)) {\n return this.documentCache.get(id);\n }\n\n logger.error(`Document ${id} not found in chunk ${chunk.id}`);\n throw new Error(`Document ${id} not found in chunk ${chunk.id}`);\n }\n\n /**\n * Get attachment URL for a given document and attachment name\n */\n getAttachmentUrl(docId: string, attachmentName: string): string {\n // Construct attachment path: attachments/{docId}/{attachmentName}.{ext}\n // The exact filename will be resolved from the document's _attachments metadata\n return `${this.basePath}/attachments/${docId}/${attachmentName}`;\n }\n\n /**\n * Load attachment data from the document and get the correct file path\n * Uses raw document access to avoid recursion with hydration\n */\n async getAttachmentPath(docId: string, attachmentName: string): Promise<string | null> {\n try {\n const doc = await this.getRawDocument(docId);\n if (doc._attachments && doc._attachments[attachmentName]) {\n const attachment = doc._attachments[attachmentName];\n if (attachment.path) {\n // Return the full path as stored in the document\n return `${this.basePath}/${attachment.path}`;\n }\n }\n return null;\n } catch {\n return null;\n }\n }\n\n /**\n * Load attachment as a blob (for browser) or buffer (for Node.js)\n */\n async getAttachmentBlob(docId: string, attachmentName: string): Promise<Blob | Buffer | null> {\n const attachmentPath = await this.getAttachmentPath(docId, attachmentName);\n if (!attachmentPath) {\n return null;\n }\n\n try {\n // Check if we're in a Node.js environment with local files\n if (this.isLocalPath(attachmentPath) && nodeFS) {\n // Use fs for local file access (e.g., in tests)\n const buffer = await nodeFS.promises.readFile(attachmentPath);\n return buffer;\n } else {\n // Use fetch for URL-based access (e.g., in browser)\n const response = await fetch(attachmentPath);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch attachment ${docId}/${attachmentName}: ${response.status} ${response.statusText}`\n );\n }\n return await response.blob();\n }\n } catch (error) {\n logger.error(`Failed to load attachment ${docId}/${attachmentName}:`, error);\n return null;\n }\n }\n\n /**\n * Hydrate document attachments by converting file paths to blob URLs\n */\n private async hydrateAttachments<T = any>(doc: T): Promise<T> {\n // logger.debug(`[hydrateAttachments] Starting hydration for doc: ${JSON.stringify(doc)}`);\n const typedDoc = doc as any;\n\n // If no attachments, return document as-is\n if (!typedDoc._attachments) {\n return doc;\n }\n\n // Clone the document to avoid mutating the cached version\n const hydratedDoc = JSON.parse(JSON.stringify(doc));\n\n // Process each attachment\n for (const [attachmentName, attachment] of Object.entries(typedDoc._attachments)) {\n // logger.debug(\n // `[hydrateAttachments] Processing attachment: ${attachmentName} for doc ${typedDoc?._id}`\n // );\n const attachmentData = attachment as any;\n\n // If attachment has a path, convert it to a blob URL\n if (attachmentData.path) {\n // logger.debug(\n // `[hydrateAttachments] Attachment ${attachmentName} has path: ${attachmentData.path}. Attempting to get blob.`\n // );\n try {\n const blob = await this.getAttachmentBlob(typedDoc._id, attachmentName);\n if (blob) {\n // logger.debug(\n // `[hydrateAttachments] Successfully retrieved blob for ${typedDoc._id}/${attachmentName}. Size: ${blob instanceof Blob ? blob.size : (blob as Buffer).length}`\n // );\n // Create blob URL for browser rendering\n if (typeof window !== 'undefined' && window.URL) {\n // Store attachment data in PouchDB-compatible format\n hydratedDoc._attachments[attachmentName] = {\n ...attachmentData,\n data: blob,\n stub: false, // Indicates this contains actual data, not just metadata\n };\n // logger.debug(\n // `[hydrateAttachments] Added blobUrl and blob to attachment ${attachmentName} for doc ${typedDoc._id}.`\n // );\n } else {\n // In Node.js environment, just attach the buffer\n hydratedDoc._attachments[attachmentName] = {\n ...attachmentData,\n buffer: blob, // Attach buffer for Node.js use\n };\n // logger.debug(\n // `[hydrateAttachments] Added buffer to attachment ${attachmentName} for doc ${typedDoc._id}.`\n // );\n }\n } else {\n logger.warn(\n `[hydrateAttachments] getAttachmentBlob returned null for ${typedDoc._id}/${attachmentName}. Skipping hydration for this attachment.`\n );\n }\n } catch (error) {\n logger.warn(\n `[hydrateAttachments] Failed to hydrate attachment ${typedDoc._id}/${attachmentName}:`,\n error\n );\n // Keep original attachment data if hydration fails\n }\n } else {\n logger.debug(\n `[hydrateAttachments] Attachment ${attachmentName} for doc ${typedDoc._id} has no path. Skipping blob conversion.`\n );\n }\n }\n\n // logger.debug(\n // `[hydrateAttachments] Finished hydration for doc ${typedDoc?._id}. Returning hydrated doc: ${JSON.stringify(hydratedDoc)}`\n // );\n return hydratedDoc;\n }\n\n /**\n * Clear all caches (useful for testing or memory management)\n */\n clearCaches(): void {\n this.documentCache.clear();\n this.chunkCache.clear();\n this.indexCache.clear();\n }\n\n /**\n * Get cache statistics for debugging\n */\n getCacheStats(): {\n documents: number;\n chunks: number;\n indices: number;\n } {\n return {\n documents: this.documentCache.size,\n chunks: this.chunkCache.size,\n indices: this.indexCache.size,\n };\n }\n\n /**\n * Check if a path is a local file path (vs URL)\n */\n private isLocalPath(filePath: string): boolean {\n // Check if it's an absolute path or doesn't start with http/https\n return (\n !filePath.startsWith('http://') &&\n !filePath.startsWith('https://') &&\n (pathUtils.isAbsolute(filePath) || filePath.startsWith('./') || filePath.startsWith('../'))\n );\n }\n}\n"],"mappings":";;;;AAOA,MAAM,YAAY,EAChB,aAAa,SAA0B;AAErC,KAAI,kBAAkB,KAAK,KAAK,IAAI,QAAQ,KAAK,KAAK,CACpD,QAAO;AAGT,KAAI,KAAK,WAAW,IAAI,CACtB,QAAO;AAET,QAAO;GAEV;AAGD,IAAI,SAAc;AAClB,IAAI;AAEF,KAAI,OAAO,WAAW,eAAe,OAAO,YAAY,eAAe,QAAQ,UAAU,KACvF,UAAS,KAAK,UAAU,CAAC,KAAK;QAE1B;AAwBR,IAAa,qBAAb,MAAgC;CAC9B;CACA;CACA,gCAA0C,IAAI,KAAK;CACnD,6BAAyC,IAAI,KAAK;CAClD,6BAAuC,IAAI,KAAK;CAEhD,YAAY,UAAgC,UAAkB;AAC5D,OAAK,WAAW;AAChB,OAAK,WAAW;;;;;CAMlB,MAAM,YAAqB,IAAwB;AAEjD,MAAI,KAAK,cAAc,IAAI,GAAG,EAAE;GAC9B,MAAM,MAAM,KAAK,cAAc,IAAI,GAAG;AACtC,UAAO,MAAM,KAAK,mBAAmB,IAAI;;EAI3C,MAAM,QAAQ,MAAM,KAAK,qBAAqB,GAAG;AACjD,MAAI,CAAC,OAAO;AACV,UAAO,MACL,YAAY,GAAG,6CACf,KAAK,SAAS,OAAO,KAAK,MAAM,GAAG,EAAE,GAAG,IAAI,EAAE,QAAQ,KAAK,EAAE,SAAS,KAAK,EAAE,SAAS,CACvF;AACD,SAAM,IAAI,MAAM,YAAY,GAAG,yBAAyB;;AAI1D,QAAM,KAAK,UAAU,MAAM,GAAG;AAG9B,MAAI,KAAK,cAAc,IAAI,GAAG,EAAE;GAC9B,MAAM,MAAM,KAAK,cAAc,IAAI,GAAG;AACtC,UAAO,MAAM,KAAK,mBAAmB,IAAI;;AAG3C,SAAO,MAAM,YAAY,GAAG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