mcard-js 2.1.49 → 2.1.51
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/CardCollection-EMSBVZP3.js +10 -0
- package/dist/CardCollection-KQWR4PCV.js +10 -0
- package/dist/CardCollection-ORGE2XBG.js +10 -0
- package/dist/EngineRegistry-ABZXHZWO.js +17 -0
- package/dist/EngineRegistry-EIOT4MUZ.js +17 -0
- package/dist/EngineRegistry-IQ6EVO72.js +17 -0
- package/dist/EngineRegistry-PHRFXEOE.js +17 -0
- package/dist/IndexedDBEngine-EWA3SLAO.js +12 -0
- package/dist/IndexedDBEngine-FXAD42F3.js +12 -0
- package/dist/IndexedDBEngine-RD4447IS.js +12 -0
- package/dist/LLMRuntime-ARUWOX52.js +17 -0
- package/dist/LLMRuntime-C3XCO7WF.js +17 -0
- package/dist/LLMRuntime-CQ7X43QR.js +17 -0
- package/dist/LLMRuntime-PD45COKE.js +17 -0
- package/dist/LLMRuntime-QOUMLT33.js +17 -0
- package/dist/LLMRuntime-SZNLTHD7.js +17 -0
- package/dist/LLMRuntime-TVJGK2BG.js +17 -0
- package/dist/LambdaRuntime-25GMEJCU.js +19 -0
- package/dist/LambdaRuntime-7KQUMHPI.js +19 -0
- package/dist/LambdaRuntime-DRT7ODPC.js +19 -0
- package/dist/LambdaRuntime-HSREEYQG.js +19 -0
- package/dist/LambdaRuntime-IH7NVG6Z.js +19 -0
- package/dist/LambdaRuntime-MPG27FM2.js +19 -0
- package/dist/LambdaRuntime-ODSWIMNM.js +19 -0
- package/dist/LambdaRuntime-PHGRZYAW.js +19 -0
- package/dist/LambdaRuntime-QOEYR37L.js +19 -0
- package/dist/LambdaRuntime-RT33TFN2.js +19 -0
- package/dist/LambdaRuntime-W6TQBP5O.js +19 -0
- package/dist/Loader-35WSUC53.js +14 -0
- package/dist/Loader-STS3G4OQ.js +16 -0
- package/dist/Loader-W22AEM6F.js +12 -0
- package/dist/Loader-YBPWP43S.js +12 -0
- package/dist/Loader-ZYSS7B4D.js +12 -0
- package/dist/NetworkRuntime-KR2QITXV.js +987 -0
- package/dist/NetworkRuntime-S6V2CMZV.js +1575 -0
- package/dist/OllamaProvider-2ANW6EB2.js +9 -0
- package/dist/OllamaProvider-5QFJKYAC.js +9 -0
- package/dist/OllamaProvider-6QXJGR7V.js +9 -0
- package/dist/OllamaProvider-ABEEFX7M.js +9 -0
- package/dist/OllamaProvider-Z2CGY5LY.js +9 -0
- package/dist/VCard-225X42W7.js +25 -0
- package/dist/chunk-2APJYBH4.js +368 -0
- package/dist/chunk-4DFTWDRB.js +497 -0
- package/dist/chunk-4PBRTFSY.js +112 -0
- package/dist/chunk-4T3H25AP.js +299 -0
- package/dist/chunk-5DFXPIRL.js +42 -0
- package/dist/chunk-5HRZV4R3.js +217 -0
- package/dist/chunk-6ZRJXVJ3.js +529 -0
- package/dist/chunk-7N7JYGN2.js +364 -0
- package/dist/chunk-7QTJUGYQ.js +74 -0
- package/dist/chunk-7TXIPJI2.js +2360 -0
- package/dist/chunk-BFJUD527.js +2369 -0
- package/dist/chunk-CHXIVTQV.js +364 -0
- package/dist/chunk-DM2ABCA4.js +497 -0
- package/dist/chunk-DTPHGTBQ.js +275 -0
- package/dist/chunk-EDAJ5FO6.js +405 -0
- package/dist/chunk-ETJWXHKZ.js +246 -0
- package/dist/chunk-FLYGNPUC.js +2369 -0
- package/dist/chunk-FSDRDWOP.js +34 -0
- package/dist/chunk-GIKMCG4D.js +497 -0
- package/dist/chunk-IJKS3LGK.js +428 -0
- package/dist/chunk-JUQ2VQZA.js +428 -0
- package/dist/chunk-JVW4J7BY.js +2369 -0
- package/dist/chunk-JWTRVEC3.js +2369 -0
- package/dist/chunk-KJM4C65U.js +299 -0
- package/dist/chunk-KMC566CN.js +591 -0
- package/dist/chunk-KMNP6DBL.js +455 -0
- package/dist/chunk-LVU7O5IY.js +597 -0
- package/dist/chunk-M4C6RWLA.js +373 -0
- package/dist/chunk-NAAAKSEO.js +541 -0
- package/dist/chunk-NKIXLPHL.js +373 -0
- package/dist/chunk-NOEDMK7I.js +428 -0
- package/dist/chunk-NOPYSBOQ.js +2360 -0
- package/dist/chunk-P4G42QCY.js +2369 -0
- package/dist/chunk-PKLONZCF.js +253 -0
- package/dist/chunk-PNGECWPN.js +597 -0
- package/dist/chunk-PYP6T64W.js +217 -0
- package/dist/chunk-QFT3COE2.js +217 -0
- package/dist/chunk-QFZFXMNX.js +275 -0
- package/dist/chunk-QZGRQRJP.js +2369 -0
- package/dist/chunk-R3XRBAM7.js +253 -0
- package/dist/chunk-RYP66UMH.js +74 -0
- package/dist/chunk-RZIZYRLF.js +112 -0
- package/dist/chunk-T43V44RS.js +2369 -0
- package/dist/chunk-UCNVX5BZ.js +74 -0
- package/dist/chunk-UDF7HS4V.js +368 -0
- package/dist/chunk-VJPXJVEH.js +299 -0
- package/dist/chunk-VW3KBDK5.js +74 -0
- package/dist/chunk-X72XIYSN.js +364 -0
- package/dist/chunk-XETU7TV4.js +112 -0
- package/dist/chunk-Y4BT6LHA.js +368 -0
- package/dist/chunk-YQGB6BIA.js +2369 -0
- package/dist/chunk-ZEQPO3XV.js +217 -0
- package/dist/chunk-ZKRKWXEQ.js +2369 -0
- package/dist/chunk-ZMK2HTZ5.js +275 -0
- package/dist/constants-CLB7B6MN.js +101 -0
- package/dist/constants-O343SMHL.js +103 -0
- package/dist/constants-YPGDEX5X.js +103 -0
- package/dist/index.browser.cjs +11 -5
- package/dist/index.browser.js +12 -12
- package/dist/index.cjs +2358 -1896
- package/dist/index.d.cts +934 -776
- package/dist/index.d.ts +934 -776
- package/dist/index.js +1353 -1271
- package/dist/storage/SqliteNodeEngine.cjs +12 -6
- package/dist/storage/SqliteNodeEngine.js +4 -4
- package/dist/storage/SqliteWasmEngine.cjs +11 -5
- package/dist/storage/SqliteWasmEngine.js +4 -4
- package/package.json +5 -3
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ContentTypeInterpreter,
|
|
3
|
+
MCard
|
|
4
|
+
} from "./chunk-GGQCF7ZK.js";
|
|
5
|
+
import {
|
|
6
|
+
MAX_FILE_SIZE,
|
|
7
|
+
READ_TIMEOUT_MS
|
|
8
|
+
} from "./chunk-R3XRBAM7.js";
|
|
9
|
+
import {
|
|
10
|
+
__export
|
|
11
|
+
} from "./chunk-PNKVD2UK.js";
|
|
12
|
+
|
|
13
|
+
// src/Loader.ts
|
|
14
|
+
var Loader_exports = {};
|
|
15
|
+
__export(Loader_exports, {
|
|
16
|
+
exportCollectionToDirectory: () => exportCollectionToDirectory,
|
|
17
|
+
loadFileToCollection: () => loadFileToCollection,
|
|
18
|
+
loadFileToCollectionStream: () => loadFileToCollectionStream,
|
|
19
|
+
processAndStoreFile: () => processAndStoreFile
|
|
20
|
+
});
|
|
21
|
+
import * as fs2 from "fs/promises";
|
|
22
|
+
import * as path2 from "path";
|
|
23
|
+
|
|
24
|
+
// src/FileIO.ts
|
|
25
|
+
var FileIO_exports = {};
|
|
26
|
+
__export(FileIO_exports, {
|
|
27
|
+
getTreeHashes: () => getTreeHashes,
|
|
28
|
+
isProblematicFile: () => isProblematicFile,
|
|
29
|
+
listFiles: () => listFiles,
|
|
30
|
+
processFileContent: () => processFileContent,
|
|
31
|
+
readFileSafely: () => readFileSafely,
|
|
32
|
+
streamReadNormalizedText: () => streamReadNormalizedText
|
|
33
|
+
});
|
|
34
|
+
import * as crypto from "crypto";
|
|
35
|
+
import * as fs from "fs/promises";
|
|
36
|
+
import * as path from "path";
|
|
37
|
+
async function streamReadNormalizedText(filePath, options) {
|
|
38
|
+
const { byteCap, wrapWidth } = options;
|
|
39
|
+
const sha = crypto.createHash("sha256");
|
|
40
|
+
let totalSize = 0;
|
|
41
|
+
let producedText = "";
|
|
42
|
+
let currentLen = 0;
|
|
43
|
+
const handle = await fs.open(filePath, "r");
|
|
44
|
+
try {
|
|
45
|
+
const buffer = new Uint8Array(8192);
|
|
46
|
+
let remaining = byteCap;
|
|
47
|
+
const decoder = new TextDecoder("utf-8", { fatal: false });
|
|
48
|
+
let position = 0;
|
|
49
|
+
while (remaining > 0) {
|
|
50
|
+
const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, remaining), position);
|
|
51
|
+
if (bytesRead === 0) break;
|
|
52
|
+
position += bytesRead;
|
|
53
|
+
const chunk = buffer.subarray(0, bytesRead);
|
|
54
|
+
sha.update(chunk);
|
|
55
|
+
totalSize += bytesRead;
|
|
56
|
+
remaining -= bytesRead;
|
|
57
|
+
const s2 = decoder.decode(chunk, { stream: true });
|
|
58
|
+
for (const ch of s2) {
|
|
59
|
+
if (ch === "\r") continue;
|
|
60
|
+
producedText += ch;
|
|
61
|
+
if (ch === "\n") {
|
|
62
|
+
currentLen = 0;
|
|
63
|
+
} else {
|
|
64
|
+
currentLen++;
|
|
65
|
+
if (wrapWidth > 0 && currentLen >= wrapWidth) {
|
|
66
|
+
producedText += "\n";
|
|
67
|
+
currentLen = 0;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const s = decoder.decode();
|
|
73
|
+
for (const ch of s) {
|
|
74
|
+
if (ch === "\r") continue;
|
|
75
|
+
producedText += ch;
|
|
76
|
+
if (ch === "\n") {
|
|
77
|
+
currentLen = 0;
|
|
78
|
+
} else {
|
|
79
|
+
currentLen++;
|
|
80
|
+
if (wrapWidth > 0 && currentLen >= wrapWidth) {
|
|
81
|
+
producedText += "\n";
|
|
82
|
+
currentLen = 0;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
await handle.close();
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
text: producedText,
|
|
91
|
+
originalSize: totalSize,
|
|
92
|
+
originalSha256Prefix: sha.digest("hex").substring(0, 16)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
var MAX_FILE_SIZE2 = MAX_FILE_SIZE;
|
|
96
|
+
var READ_TIMEOUT_MS2 = READ_TIMEOUT_MS;
|
|
97
|
+
async function isProblematicFile(filePath) {
|
|
98
|
+
try {
|
|
99
|
+
const stats = await fs.stat(filePath);
|
|
100
|
+
if (stats.size === 0) return false;
|
|
101
|
+
if (path.basename(filePath).startsWith(".")) return true;
|
|
102
|
+
if (stats.size > MAX_FILE_SIZE2) return true;
|
|
103
|
+
const ext = path.extname(filePath);
|
|
104
|
+
const isKnownType = ContentTypeInterpreter.isKnownLongLineExtension(ext);
|
|
105
|
+
if (isKnownType && stats.size > 1024 * 1024) return true;
|
|
106
|
+
const handle = await fs.open(filePath, "r");
|
|
107
|
+
try {
|
|
108
|
+
const buffer = new Uint8Array(32 * 1024);
|
|
109
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
110
|
+
const sample = buffer.subarray(0, bytesRead);
|
|
111
|
+
if (ContentTypeInterpreter.isUnstructuredBinary(sample)) return true;
|
|
112
|
+
if (ContentTypeInterpreter.hasPathologicalLines(sample, isKnownType)) return true;
|
|
113
|
+
} finally {
|
|
114
|
+
await handle.close();
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
} catch {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async function readFileSafely(filePath, options = {}) {
|
|
122
|
+
const stats = await fs.stat(filePath);
|
|
123
|
+
if (stats.size > MAX_FILE_SIZE2) throw new Error(`File too large: ${stats.size}`);
|
|
124
|
+
const controller = new AbortController();
|
|
125
|
+
const timeout = setTimeout(() => controller.abort(), READ_TIMEOUT_MS2);
|
|
126
|
+
try {
|
|
127
|
+
const handle = await fs.open(filePath, "r");
|
|
128
|
+
try {
|
|
129
|
+
const buffer = new Uint8Array(stats.size);
|
|
130
|
+
await handle.read(buffer, 0, stats.size, 0);
|
|
131
|
+
return buffer;
|
|
132
|
+
} finally {
|
|
133
|
+
await handle.close();
|
|
134
|
+
}
|
|
135
|
+
} catch (e) {
|
|
136
|
+
const error = e;
|
|
137
|
+
if (error.name === "AbortError") throw new Error(`Read timeout for ${filePath}`);
|
|
138
|
+
throw e;
|
|
139
|
+
} finally {
|
|
140
|
+
clearTimeout(timeout);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function listFiles(dirPath, recursive = false) {
|
|
144
|
+
let files = [];
|
|
145
|
+
try {
|
|
146
|
+
const entries = await fs.readdir(dirPath, { withFileTypes: true });
|
|
147
|
+
for (const entry of entries) {
|
|
148
|
+
const fullPath = path.join(dirPath, entry.name);
|
|
149
|
+
if (entry.name.startsWith(".")) continue;
|
|
150
|
+
if (entry.isDirectory()) {
|
|
151
|
+
if (recursive) {
|
|
152
|
+
files = files.concat(await listFiles(fullPath, true));
|
|
153
|
+
}
|
|
154
|
+
} else if (entry.isFile()) {
|
|
155
|
+
if (!await isProblematicFile(fullPath)) {
|
|
156
|
+
files.push(fullPath);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} catch (e) {
|
|
161
|
+
console.warn(`Error listing directory ${dirPath}:`, e);
|
|
162
|
+
}
|
|
163
|
+
return files;
|
|
164
|
+
}
|
|
165
|
+
async function processFileContent(filePath, options = {}) {
|
|
166
|
+
const rawContent = await readFileSafely(filePath, { allowPathological: options.allowPathological, maxBytes: options.maxBytes });
|
|
167
|
+
const sample = rawContent.subarray(0, 1024 * 1024);
|
|
168
|
+
const detection = ContentTypeInterpreter.detectContentType(sample, path.extname(filePath));
|
|
169
|
+
let isBinary = ContentTypeInterpreter.isBinaryContent(sample, detection.mimeType);
|
|
170
|
+
if (options.forceBinary) isBinary = true;
|
|
171
|
+
let content = rawContent;
|
|
172
|
+
if (!isBinary) {
|
|
173
|
+
try {
|
|
174
|
+
content = new TextDecoder("utf-8", { fatal: true }).decode(rawContent);
|
|
175
|
+
} catch {
|
|
176
|
+
content = new TextDecoder("utf-8", { fatal: false }).decode(rawContent);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
content,
|
|
181
|
+
filename: path.basename(filePath),
|
|
182
|
+
mimeType: detection.mimeType,
|
|
183
|
+
extension: detection.extension,
|
|
184
|
+
isBinary,
|
|
185
|
+
size: rawContent.length
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
async function getTreeHashes(dirPath) {
|
|
189
|
+
const hashes = {};
|
|
190
|
+
const files = await listFiles(dirPath, true);
|
|
191
|
+
for (const filePath of files) {
|
|
192
|
+
const relPath = path.relative(dirPath, filePath);
|
|
193
|
+
try {
|
|
194
|
+
const result = await processFileContent(filePath);
|
|
195
|
+
const sha = crypto.createHash("sha256");
|
|
196
|
+
if (typeof result.content === "string") {
|
|
197
|
+
sha.update(result.content, "utf-8");
|
|
198
|
+
} else {
|
|
199
|
+
sha.update(result.content);
|
|
200
|
+
}
|
|
201
|
+
hashes[relPath] = sha.digest("hex");
|
|
202
|
+
} catch (e) {
|
|
203
|
+
console.warn(`Failed to hash ${filePath}:`, e);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return hashes;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/Loader.ts
|
|
210
|
+
var DEFAULT_MAX_PROBLEM_BYTES = 2 * 1024 * 1024;
|
|
211
|
+
var WRAP_WIDTH_KNOWN = 1e3;
|
|
212
|
+
var WRAP_WIDTH_DEFAULT = 80;
|
|
213
|
+
var Logger = {
|
|
214
|
+
info: (...args) => console.log("[Loader]", ...args),
|
|
215
|
+
warn: (...args) => console.warn("[Loader]", ...args),
|
|
216
|
+
error: (...args) => console.error("[Loader]", ...args),
|
|
217
|
+
debug: (...args) => {
|
|
218
|
+
if (process.env.DEBUG) console.log("[Loader]", ...args);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
async function processAndStoreFile(filePath, collection, options = {}) {
|
|
222
|
+
const {
|
|
223
|
+
allowProblematic = false,
|
|
224
|
+
maxBytesOnProblem = DEFAULT_MAX_PROBLEM_BYTES,
|
|
225
|
+
metadataOnly = false,
|
|
226
|
+
rootPath
|
|
227
|
+
} = options;
|
|
228
|
+
try {
|
|
229
|
+
let fileInfo;
|
|
230
|
+
if (await isProblematicFile(filePath)) {
|
|
231
|
+
if (!allowProblematic) {
|
|
232
|
+
Logger.warn(`Skipping problematic file: ${filePath}`);
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
const extension = path2.extname(filePath).toLowerCase();
|
|
236
|
+
const isKnownType = ContentTypeInterpreter.isKnownLongLineExtension(extension);
|
|
237
|
+
const wrapWidth = isKnownType ? WRAP_WIDTH_KNOWN : WRAP_WIDTH_DEFAULT;
|
|
238
|
+
Logger.warn(`Problematic file detected, processing as safe text: ${filePath}`);
|
|
239
|
+
try {
|
|
240
|
+
const streamed = await streamReadNormalizedText(filePath, {
|
|
241
|
+
byteCap: maxBytesOnProblem,
|
|
242
|
+
wrapWidth
|
|
243
|
+
});
|
|
244
|
+
fileInfo = {
|
|
245
|
+
content: streamed.text,
|
|
246
|
+
filename: path2.basename(filePath),
|
|
247
|
+
mimeType: "text/plain",
|
|
248
|
+
extension,
|
|
249
|
+
isBinary: false,
|
|
250
|
+
size: streamed.text.length,
|
|
251
|
+
originalSize: streamed.originalSize,
|
|
252
|
+
originalSha256Prefix: streamed.originalSha256Prefix,
|
|
253
|
+
normalized: true,
|
|
254
|
+
wrapWidth
|
|
255
|
+
};
|
|
256
|
+
} catch (e) {
|
|
257
|
+
Logger.warn(`Safe text processing failed, falling back to capped binary: ${filePath}`, e);
|
|
258
|
+
fileInfo = await processFileContent(filePath, {
|
|
259
|
+
forceBinary: true,
|
|
260
|
+
allowPathological: true,
|
|
261
|
+
maxBytes: maxBytesOnProblem
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
Logger.info(`Processing file: ${filePath}`);
|
|
266
|
+
fileInfo = await processFileContent(filePath);
|
|
267
|
+
}
|
|
268
|
+
if (!fileInfo) return null;
|
|
269
|
+
const content = fileInfo.content;
|
|
270
|
+
if (!content || typeof content === "string" && content.length === 0 || content instanceof Uint8Array && content.length === 0) {
|
|
271
|
+
Logger.debug(`Skipping empty file: ${filePath} (empty files cannot be stored as MCards)`);
|
|
272
|
+
return {
|
|
273
|
+
hash: "",
|
|
274
|
+
contentType: fileInfo.mimeType,
|
|
275
|
+
isBinary: fileInfo.isBinary,
|
|
276
|
+
filename: fileInfo.filename,
|
|
277
|
+
size: 0,
|
|
278
|
+
filePath
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
let mcard = null;
|
|
282
|
+
const isProblematic = await isProblematicFile(filePath);
|
|
283
|
+
if (metadataOnly && isProblematic) {
|
|
284
|
+
mcard = null;
|
|
285
|
+
} else {
|
|
286
|
+
mcard = await MCard.create(fileInfo.content);
|
|
287
|
+
const handle = path2.basename(filePath);
|
|
288
|
+
try {
|
|
289
|
+
await collection.addWithHandle(mcard, handle);
|
|
290
|
+
} catch (e) {
|
|
291
|
+
let registered = false;
|
|
292
|
+
if (rootPath) {
|
|
293
|
+
const relPath = path2.relative(rootPath, filePath);
|
|
294
|
+
if (relPath !== handle) {
|
|
295
|
+
try {
|
|
296
|
+
await collection.addWithHandle(mcard, relPath);
|
|
297
|
+
registered = true;
|
|
298
|
+
} catch (e2) {
|
|
299
|
+
Logger.debug(
|
|
300
|
+
`Handle name '${handle}' already in use (common for files like README.md, LICENSE). MCard stored successfully with hash ${mcard.hash.slice(0, 8)}... (accessible by hash, not by handle)`
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (!registered) {
|
|
306
|
+
try {
|
|
307
|
+
await collection.add(mcard);
|
|
308
|
+
} catch (e3) {
|
|
309
|
+
Logger.warn(`Hash fallback also failed for ${handle}:`, e3);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const result = {
|
|
315
|
+
hash: mcard ? mcard.hash : "METADATA_ONLY",
|
|
316
|
+
contentType: fileInfo.mimeType,
|
|
317
|
+
isBinary: fileInfo.isBinary,
|
|
318
|
+
filename: fileInfo.filename,
|
|
319
|
+
size: fileInfo.size,
|
|
320
|
+
filePath
|
|
321
|
+
};
|
|
322
|
+
if (fileInfo.originalSize !== void 0) result.originalSize = fileInfo.originalSize;
|
|
323
|
+
if (fileInfo.originalSha256Prefix) result.originalSha256Prefix = fileInfo.originalSha256Prefix;
|
|
324
|
+
if (metadataOnly && isProblematic) result.metadataOnly = true;
|
|
325
|
+
return result;
|
|
326
|
+
} catch (e) {
|
|
327
|
+
Logger.error(`Error processing ${filePath}:`, e);
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
async function loadFileToCollection(targetPath, collection, options = {}) {
|
|
332
|
+
const {
|
|
333
|
+
recursive = false,
|
|
334
|
+
includeProblematic = false,
|
|
335
|
+
maxBytesOnProblem = DEFAULT_MAX_PROBLEM_BYTES,
|
|
336
|
+
metadataOnly = false
|
|
337
|
+
} = options;
|
|
338
|
+
const resolvedPath = path2.resolve(targetPath);
|
|
339
|
+
const stats = await fs2.stat(resolvedPath);
|
|
340
|
+
const results = [];
|
|
341
|
+
let files = [];
|
|
342
|
+
let rootPath = resolvedPath;
|
|
343
|
+
if (stats.isFile()) {
|
|
344
|
+
files = [resolvedPath];
|
|
345
|
+
rootPath = path2.dirname(resolvedPath);
|
|
346
|
+
} else if (stats.isDirectory()) {
|
|
347
|
+
files = await listFiles(resolvedPath, recursive);
|
|
348
|
+
rootPath = resolvedPath;
|
|
349
|
+
} else {
|
|
350
|
+
throw new Error(`Path ${targetPath} is not a file or directory`);
|
|
351
|
+
}
|
|
352
|
+
const uniqueDirs = /* @__PURE__ */ new Set();
|
|
353
|
+
let maxDepth = 0;
|
|
354
|
+
for (const file of files) {
|
|
355
|
+
const dir = path2.dirname(file);
|
|
356
|
+
if (dir.startsWith(rootPath)) {
|
|
357
|
+
uniqueDirs.add(dir);
|
|
358
|
+
const rel = path2.relative(rootPath, file);
|
|
359
|
+
const parts = rel.split(path2.sep);
|
|
360
|
+
const depth = parts.length - 1;
|
|
361
|
+
if (depth > maxDepth) maxDepth = depth;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const metrics = {
|
|
365
|
+
filesCount: files.length,
|
|
366
|
+
directoriesCount: uniqueDirs.size,
|
|
367
|
+
directoryLevels: maxDepth
|
|
368
|
+
};
|
|
369
|
+
Logger.info(`About to process ${files.length} files`);
|
|
370
|
+
for (const file of files) {
|
|
371
|
+
const result = await processAndStoreFile(file, collection, {
|
|
372
|
+
allowProblematic: includeProblematic,
|
|
373
|
+
maxBytesOnProblem,
|
|
374
|
+
metadataOnly,
|
|
375
|
+
rootPath
|
|
376
|
+
});
|
|
377
|
+
if (result) results.push(result);
|
|
378
|
+
}
|
|
379
|
+
return { metrics, results };
|
|
380
|
+
}
|
|
381
|
+
async function* loadFileToCollectionStream(targetPath, collection, options = {}) {
|
|
382
|
+
const {
|
|
383
|
+
recursive = false,
|
|
384
|
+
includeProblematic = false,
|
|
385
|
+
maxBytesOnProblem = DEFAULT_MAX_PROBLEM_BYTES,
|
|
386
|
+
metadataOnly = false
|
|
387
|
+
} = options;
|
|
388
|
+
const resolvedPath = path2.resolve(targetPath);
|
|
389
|
+
const stats = await fs2.stat(resolvedPath);
|
|
390
|
+
let files = [];
|
|
391
|
+
let rootPath = resolvedPath;
|
|
392
|
+
if (stats.isFile()) {
|
|
393
|
+
files = [resolvedPath];
|
|
394
|
+
rootPath = path2.dirname(resolvedPath);
|
|
395
|
+
} else if (stats.isDirectory()) {
|
|
396
|
+
files = await listFiles(resolvedPath, recursive);
|
|
397
|
+
rootPath = resolvedPath;
|
|
398
|
+
}
|
|
399
|
+
const uniqueDirs = /* @__PURE__ */ new Set();
|
|
400
|
+
let maxDepth = 0;
|
|
401
|
+
for (const file of files) {
|
|
402
|
+
const dir = path2.dirname(file);
|
|
403
|
+
if (dir.startsWith(rootPath)) {
|
|
404
|
+
uniqueDirs.add(dir);
|
|
405
|
+
const depth = path2.relative(rootPath, file).split(path2.sep).length - 1;
|
|
406
|
+
if (depth > maxDepth) maxDepth = depth;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const metrics = {
|
|
410
|
+
filesCount: files.length,
|
|
411
|
+
directoriesCount: uniqueDirs.size,
|
|
412
|
+
directoryLevels: maxDepth
|
|
413
|
+
};
|
|
414
|
+
let processedCount = 0;
|
|
415
|
+
for (const file of files) {
|
|
416
|
+
const result = await processAndStoreFile(file, collection, {
|
|
417
|
+
allowProblematic: includeProblematic,
|
|
418
|
+
maxBytesOnProblem,
|
|
419
|
+
metadataOnly,
|
|
420
|
+
rootPath
|
|
421
|
+
});
|
|
422
|
+
processedCount++;
|
|
423
|
+
if (result) {
|
|
424
|
+
yield { progress: processedCount, total: files.length, result };
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return metrics;
|
|
428
|
+
}
|
|
429
|
+
async function exportCollectionToDirectory(collection, targetDir) {
|
|
430
|
+
let exportedCount = 0;
|
|
431
|
+
let bytesWritten = 0;
|
|
432
|
+
const handles = await collection.getAllHandles();
|
|
433
|
+
for (const h of handles) {
|
|
434
|
+
const handlePath = h.handle;
|
|
435
|
+
const card = await collection.get(h.hash);
|
|
436
|
+
if (card) {
|
|
437
|
+
const absolutePath = path2.join(targetDir, handlePath);
|
|
438
|
+
await fs2.mkdir(path2.dirname(absolutePath), { recursive: true });
|
|
439
|
+
await fs2.writeFile(absolutePath, card.content);
|
|
440
|
+
bytesWritten += card.content.length;
|
|
441
|
+
exportedCount++;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return { exportedCount, bytesWritten };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export {
|
|
448
|
+
getTreeHashes,
|
|
449
|
+
FileIO_exports,
|
|
450
|
+
processAndStoreFile,
|
|
451
|
+
loadFileToCollection,
|
|
452
|
+
loadFileToCollectionStream,
|
|
453
|
+
exportCollectionToDirectory,
|
|
454
|
+
Loader_exports
|
|
455
|
+
};
|