@semiont/content 0.5.30 → 0.5.31
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/index.d.ts +134 -151
- package/dist/index.js +126 -110
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,19 +1,87 @@
|
|
|
1
1
|
// src/working-tree-store.ts
|
|
2
2
|
import { promises as fs, createReadStream, createWriteStream } from "fs";
|
|
3
|
-
import { execFileSync } from "child_process";
|
|
4
3
|
import { createHash, randomUUID } from "crypto";
|
|
5
4
|
import { Readable } from "stream";
|
|
6
5
|
import { pipeline } from "stream/promises";
|
|
7
6
|
import path from "path";
|
|
7
|
+
|
|
8
|
+
// src/git-staging.ts
|
|
9
|
+
import { execFile } from "child_process";
|
|
10
|
+
import { promisify } from "util";
|
|
8
11
|
import { recordGitCommand } from "@semiont/observability";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
var run = promisify(execFile);
|
|
13
|
+
var DEFAULT_FLUSH_MS = 250;
|
|
14
|
+
var DEFAULT_MAX_WAIT_MS = 2e3;
|
|
15
|
+
function createStager(cwd, options = {}) {
|
|
16
|
+
const flushMs = options.flushMs ?? DEFAULT_FLUSH_MS;
|
|
17
|
+
const maxWaitMs = options.maxWaitMs ?? DEFAULT_MAX_WAIT_MS;
|
|
18
|
+
const queued = /* @__PURE__ */ new Set();
|
|
19
|
+
let timer;
|
|
20
|
+
let oldestAt;
|
|
21
|
+
let inFlight = Promise.resolve();
|
|
22
|
+
let disposed = false;
|
|
23
|
+
const git = async (args) => {
|
|
24
|
+
const started = performance.now();
|
|
25
|
+
try {
|
|
26
|
+
await run("git", args, { cwd });
|
|
27
|
+
} finally {
|
|
28
|
+
recordGitCommand(args[0] ?? "git", performance.now() - started);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const serialize = (work) => {
|
|
32
|
+
inFlight = inFlight.then(work, work);
|
|
33
|
+
return inFlight;
|
|
34
|
+
};
|
|
35
|
+
const clearTimer = () => {
|
|
36
|
+
if (timer) {
|
|
37
|
+
clearTimeout(timer);
|
|
38
|
+
timer = void 0;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const drain = () => {
|
|
42
|
+
clearTimer();
|
|
43
|
+
if (queued.size === 0) return inFlight;
|
|
44
|
+
const batch = [...queued];
|
|
45
|
+
queued.clear();
|
|
46
|
+
oldestAt = void 0;
|
|
47
|
+
return serialize(() => git(["add", ...batch]));
|
|
48
|
+
};
|
|
49
|
+
const arm = () => {
|
|
50
|
+
clearTimer();
|
|
51
|
+
if (disposed || queued.size === 0) return;
|
|
52
|
+
const sinceOldest = oldestAt === void 0 ? 0 : Date.now() - oldestAt;
|
|
53
|
+
const wait = Math.max(0, Math.min(flushMs, maxWaitMs - sinceOldest));
|
|
54
|
+
timer = setTimeout(() => {
|
|
55
|
+
void drain();
|
|
56
|
+
}, wait);
|
|
57
|
+
timer.unref?.();
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
add(path4) {
|
|
61
|
+
if (disposed) return;
|
|
62
|
+
if (queued.size === 0) oldestAt = Date.now();
|
|
63
|
+
queued.add(path4);
|
|
64
|
+
arm();
|
|
65
|
+
},
|
|
66
|
+
run(args) {
|
|
67
|
+
return drain().then(() => serialize(() => git(args)));
|
|
68
|
+
},
|
|
69
|
+
flush() {
|
|
70
|
+
return drain();
|
|
71
|
+
},
|
|
72
|
+
pending() {
|
|
73
|
+
return queued.size;
|
|
74
|
+
},
|
|
75
|
+
async dispose() {
|
|
76
|
+
await drain();
|
|
77
|
+
disposed = true;
|
|
78
|
+
clearTimer();
|
|
79
|
+
await inFlight;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
16
82
|
}
|
|
83
|
+
|
|
84
|
+
// src/working-tree-store.ts
|
|
17
85
|
function hashingTap() {
|
|
18
86
|
const hash = createHash("sha256");
|
|
19
87
|
let byteSize = 0;
|
|
@@ -34,33 +102,40 @@ var WorkingTreeStore = class {
|
|
|
34
102
|
projectRoot;
|
|
35
103
|
gitSync;
|
|
36
104
|
logger;
|
|
37
|
-
|
|
105
|
+
_stager;
|
|
106
|
+
staging;
|
|
107
|
+
/** `staging` is policy — how stale the index may get is the caller's call. */
|
|
108
|
+
constructor(project, logger, staging = {}) {
|
|
38
109
|
this.projectRoot = project.root;
|
|
39
110
|
this.gitSync = project.gitSync;
|
|
40
111
|
this.logger = logger;
|
|
112
|
+
this.staging = staging;
|
|
113
|
+
}
|
|
114
|
+
/** Created on first use — importers of this package may never stage. */
|
|
115
|
+
stager() {
|
|
116
|
+
if (!this._stager) this._stager = createStager(this.projectRoot, this.staging);
|
|
117
|
+
return this._stager;
|
|
118
|
+
}
|
|
119
|
+
/** Stage everything pending now — for a caller that wants the index current. */
|
|
120
|
+
flushStaging() {
|
|
121
|
+
return this._stager ? this._stager.flush() : Promise.resolve();
|
|
122
|
+
}
|
|
123
|
+
/** Drain and stop. A stopped process must leave nothing unstaged. */
|
|
124
|
+
async dispose() {
|
|
125
|
+
if (this._stager) await this._stager.dispose();
|
|
41
126
|
}
|
|
42
127
|
shouldRunGit(noGit) {
|
|
43
128
|
return this.gitSync && !noGit;
|
|
44
129
|
}
|
|
45
130
|
/**
|
|
46
|
-
* Write
|
|
47
|
-
*
|
|
48
|
-
* API/GUI/AI path: caller provides bytes — as a Buffer it already holds, or
|
|
49
|
-
* as a stream (the Archivist's write endpoint hands the request body
|
|
50
|
-
* straight through, SINGLE-KB-MOUNT P2/D7: memory stays bounded by the
|
|
51
|
-
* chunk, never the representation).
|
|
131
|
+
* Write bytes to the path storageUri names, whole or streamed.
|
|
52
132
|
*
|
|
53
|
-
* Atomic
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* overwritten survives) and no temp file behind, so the Stower's `register`
|
|
58
|
-
* can never find partial bytes an event names.
|
|
133
|
+
* Atomic: bytes land in a temp file and are renamed into place only once
|
|
134
|
+
* complete and once `expectedChecksum`, when given, agrees. A mismatch or a
|
|
135
|
+
* torn stream leaves the target untouched, so `register` can never find
|
|
136
|
+
* partial bytes an event names.
|
|
59
137
|
*
|
|
60
|
-
* @param content - Raw bytes to write, whole or streamed
|
|
61
|
-
* @param storageUri - file:// URI (e.g. "file://docs/overview.md")
|
|
62
138
|
* @throws ChecksumMismatchError when expectedChecksum disagrees with the body
|
|
63
|
-
* @returns Stored resource metadata
|
|
64
139
|
*/
|
|
65
140
|
async store(content, storageUri, options) {
|
|
66
141
|
const filePath = this.resolveUri(storageUri);
|
|
@@ -87,7 +162,7 @@ var WorkingTreeStore = class {
|
|
|
87
162
|
}
|
|
88
163
|
await fs.rename(tempPath, filePath);
|
|
89
164
|
if (this.shouldRunGit(options?.noGit)) {
|
|
90
|
-
|
|
165
|
+
this.stager().add(filePath);
|
|
91
166
|
}
|
|
92
167
|
this.logger?.info("Resource stored", { storageUri, checksum, byteSize });
|
|
93
168
|
return {
|
|
@@ -102,17 +177,9 @@ var WorkingTreeStore = class {
|
|
|
102
177
|
}
|
|
103
178
|
}
|
|
104
179
|
/**
|
|
105
|
-
*
|
|
180
|
+
* Adopt a file already on disk: stream it to hash it, then stage it.
|
|
106
181
|
*
|
|
107
|
-
*
|
|
108
|
-
* it is, then stages it. If expectedChecksum is provided, throws
|
|
109
|
-
* ChecksumMismatchError on mismatch.
|
|
110
|
-
*
|
|
111
|
-
* @param storageUri - file:// URI (e.g. "file://docs/overview.md")
|
|
112
|
-
* @param expectedChecksum - Optional SHA-256 to verify against
|
|
113
|
-
* @returns Stored resource metadata
|
|
114
|
-
* @throws ChecksumMismatchError if expectedChecksum is provided and does not match
|
|
115
|
-
* @throws Error if file does not exist
|
|
182
|
+
* @throws ChecksumMismatchError if expectedChecksum is given and disagrees
|
|
116
183
|
*/
|
|
117
184
|
async register(storageUri, expectedChecksum, options) {
|
|
118
185
|
const filePath = this.resolveUri(storageUri);
|
|
@@ -126,7 +193,7 @@ var WorkingTreeStore = class {
|
|
|
126
193
|
throw new ChecksumMismatchError(storageUri, expectedChecksum, checksum);
|
|
127
194
|
}
|
|
128
195
|
if (this.shouldRunGit(options?.noGit)) {
|
|
129
|
-
|
|
196
|
+
this.stager().add(filePath);
|
|
130
197
|
}
|
|
131
198
|
const byteSize = tap.byteSize;
|
|
132
199
|
this.logger?.info("Resource registered", { storageUri, checksum, byteSize });
|
|
@@ -138,22 +205,9 @@ var WorkingTreeStore = class {
|
|
|
138
205
|
};
|
|
139
206
|
}
|
|
140
207
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* @returns Raw bytes
|
|
145
|
-
*/
|
|
146
|
-
/**
|
|
147
|
-
* The same bytes as `retrieve`, streamed — for the byte paths that must not
|
|
148
|
-
* hold a whole representation in memory (SINGLE-KB-MOUNT D7: the Archivist
|
|
149
|
-
* serves content for every reader now, so its memory cannot be bounded by
|
|
150
|
-
* the largest file anyone asks for).
|
|
151
|
-
*
|
|
152
|
-
* Lazy by construction: the stream is created here but nothing is read
|
|
153
|
-
* until the caller iterates, so a missing file surfaces as an `error` event
|
|
154
|
-
* on the stream rather than a rejected promise. Callers that need the
|
|
155
|
-
* distinction up front should resolve the descriptor first — which is what
|
|
156
|
-
* `resolveRepresentation` does.
|
|
208
|
+
* The same bytes as `retrieve`, streamed. Lazy: a missing file surfaces as
|
|
209
|
+
* an `error` event on the stream, not a rejected promise — callers needing
|
|
210
|
+
* that up front should resolve the descriptor first.
|
|
157
211
|
*/
|
|
158
212
|
retrieveStream(storageUri) {
|
|
159
213
|
return createReadStream(this.resolveUri(storageUri));
|
|
@@ -169,42 +223,20 @@ var WorkingTreeStore = class {
|
|
|
169
223
|
throw error;
|
|
170
224
|
}
|
|
171
225
|
}
|
|
172
|
-
/**
|
|
173
|
-
* Move a file from one URI to another.
|
|
174
|
-
*
|
|
175
|
-
* If .git/ exists in the project root and noGit is not set, runs `git mv`.
|
|
176
|
-
* Otherwise (no .git/ or noGit: true), runs fs.rename.
|
|
177
|
-
*
|
|
178
|
-
* @param fromUri - Current file:// URI
|
|
179
|
-
* @param toUri - New file:// URI
|
|
180
|
-
* @param options.noGit - Skip git mv even if .git/ is present
|
|
181
|
-
*/
|
|
226
|
+
/** `git mv` when the project syncs git, `fs.rename` otherwise. */
|
|
182
227
|
async move(fromUri, toUri, options) {
|
|
183
228
|
const fromPath = this.resolveUri(fromUri);
|
|
184
229
|
const toPath = this.resolveUri(toUri);
|
|
185
230
|
this.logger?.debug("Moving resource", { fromUri, toUri });
|
|
186
231
|
await fs.mkdir(path.dirname(toPath), { recursive: true });
|
|
187
232
|
if (this.shouldRunGit(options?.noGit)) {
|
|
188
|
-
|
|
233
|
+
await this.stager().run(["mv", fromPath, toPath]);
|
|
189
234
|
} else {
|
|
190
235
|
await fs.rename(fromPath, toPath);
|
|
191
236
|
}
|
|
192
237
|
this.logger?.info("Resource moved", { fromUri, toUri });
|
|
193
238
|
}
|
|
194
|
-
/**
|
|
195
|
-
* Remove a file from the working tree.
|
|
196
|
-
*
|
|
197
|
-
* If .git/ exists and noGit is not set:
|
|
198
|
-
* - keepFile false (default): runs `git rm` (removes from index and disk)
|
|
199
|
-
* - keepFile true: runs `git rm --cached` (removes from index only, file stays on disk)
|
|
200
|
-
* If no .git/ or noGit: true:
|
|
201
|
-
* - keepFile false: runs fs.unlink
|
|
202
|
-
* - keepFile true: no-op on filesystem
|
|
203
|
-
*
|
|
204
|
-
* @param storageUri - file:// URI
|
|
205
|
-
* @param options.noGit - Skip git rm even if .git/ is present
|
|
206
|
-
* @param options.keepFile - Remove from git index only; leave file on disk
|
|
207
|
-
*/
|
|
239
|
+
/** @param options.keepFile - Drop from the index only; leave the file on disk. */
|
|
208
240
|
async remove(storageUri, options) {
|
|
209
241
|
const filePath = this.resolveUri(storageUri);
|
|
210
242
|
const keepFile = options?.keepFile ?? false;
|
|
@@ -212,7 +244,7 @@ var WorkingTreeStore = class {
|
|
|
212
244
|
const useGit = this.shouldRunGit(options?.noGit);
|
|
213
245
|
if (useGit) {
|
|
214
246
|
const gitArgs = keepFile ? ["rm", "--cached", filePath] : ["rm", filePath];
|
|
215
|
-
|
|
247
|
+
await this.stager().run(gitArgs);
|
|
216
248
|
this.logger?.info("Resource removed", { storageUri, keepFile, git: true });
|
|
217
249
|
return;
|
|
218
250
|
}
|
|
@@ -231,14 +263,6 @@ var WorkingTreeStore = class {
|
|
|
231
263
|
throw error;
|
|
232
264
|
}
|
|
233
265
|
}
|
|
234
|
-
/**
|
|
235
|
-
* Convert a file:// URI to an absolute filesystem path.
|
|
236
|
-
*
|
|
237
|
-
* "file://docs/overview.md" → "{projectRoot}/docs/overview.md"
|
|
238
|
-
*
|
|
239
|
-
* @param storageUri - file:// URI
|
|
240
|
-
* @returns Absolute path
|
|
241
|
-
*/
|
|
242
266
|
resolveUri(storageUri) {
|
|
243
267
|
if (!storageUri.startsWith("file://")) {
|
|
244
268
|
throw new Error(`Invalid storage URI (must start with file://): ${storageUri}`);
|
|
@@ -274,8 +298,8 @@ function verifyChecksum(content, checksum) {
|
|
|
274
298
|
return calculateChecksum(content) === checksum;
|
|
275
299
|
}
|
|
276
300
|
|
|
277
|
-
// src/
|
|
278
|
-
import {
|
|
301
|
+
// src/text-extractor.ts
|
|
302
|
+
import { yieldsGeometryOf } from "@semiont/core";
|
|
279
303
|
|
|
280
304
|
// src/pdf-extractor.ts
|
|
281
305
|
import { isObject as isObject4 } from "@semiont/core";
|
|
@@ -824,18 +848,17 @@ function shapeTables(layer) {
|
|
|
824
848
|
}
|
|
825
849
|
var pdfExtractor = {
|
|
826
850
|
// Every non-declined PDF extraction carries positioned runs — native text
|
|
827
|
-
// layers and OCR both anchor by page geometry.
|
|
828
|
-
|
|
851
|
+
// layers and OCR both anchor by page geometry. That fact is declared in core
|
|
852
|
+
// ('pdf-text-layer' → true) rather than here; this comment records the
|
|
853
|
+
// behavior the census gate holds core's answer to.
|
|
829
854
|
async extract(content, _mediaType, cache) {
|
|
830
|
-
const hit = await cache
|
|
855
|
+
const hit = await cache.store.read(cache.key);
|
|
831
856
|
if (hit) return hit;
|
|
832
857
|
const outcome = await extractPdf(content);
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
} catch {
|
|
838
|
-
}
|
|
858
|
+
try {
|
|
859
|
+
if (outcome.kind === "declined") await cache.store.write(cache.key, outcome);
|
|
860
|
+
else if (outcome.items) await cache.store.write(cache.key, { ...outcome, items: outcome.items });
|
|
861
|
+
} catch {
|
|
839
862
|
}
|
|
840
863
|
return outcome;
|
|
841
864
|
}
|
|
@@ -890,18 +913,10 @@ async function extractPdf(content) {
|
|
|
890
913
|
};
|
|
891
914
|
}
|
|
892
915
|
|
|
893
|
-
// src/
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
return { kind: "extracted", text: decodeRepresentation(content, mediaType), method: "text-passthrough" };
|
|
898
|
-
}
|
|
899
|
-
};
|
|
900
|
-
var EXTRACTORS = {
|
|
901
|
-
"decode": passthroughExtractor,
|
|
902
|
-
"pdf-text-layer": pdfExtractor,
|
|
903
|
-
"none": null
|
|
904
|
-
};
|
|
916
|
+
// src/text-extractor.ts
|
|
917
|
+
function derivingExtractorFor(mediaType) {
|
|
918
|
+
return yieldsGeometryOf(mediaType) ? pdfExtractor : null;
|
|
919
|
+
}
|
|
905
920
|
|
|
906
921
|
// src/anchored-text-store.ts
|
|
907
922
|
import fs2 from "fs";
|
|
@@ -1103,13 +1118,14 @@ function archivistContentReads(config) {
|
|
|
1103
1118
|
}
|
|
1104
1119
|
export {
|
|
1105
1120
|
ChecksumMismatchError,
|
|
1106
|
-
EXTRACTORS,
|
|
1107
1121
|
MAX_PDF_BYTES,
|
|
1108
1122
|
RepresentationMissing,
|
|
1109
1123
|
WorkingTreeStore,
|
|
1110
1124
|
archivistContentReads,
|
|
1111
1125
|
calculateChecksum,
|
|
1112
1126
|
createAnchoredTextStore,
|
|
1127
|
+
createStager,
|
|
1128
|
+
derivingExtractorFor,
|
|
1113
1129
|
extractPdfTextLayer,
|
|
1114
1130
|
verifyChecksum,
|
|
1115
1131
|
withinByteBudget
|