@takazudo/zudo-doc-history-server 0.2.18 → 0.2.20

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.
@@ -113,19 +113,33 @@ function parseHashToPathMap(output) {
113
113
  if (!output) return /* @__PURE__ */ new Map();
114
114
  const map = /* @__PURE__ */ new Map();
115
115
  const lines = output.split("\n");
116
- const hashSet = /* @__PURE__ */ new Set();
116
+ let state = "expect_hash";
117
+ let currentHash = "";
117
118
  for (const line of lines) {
118
119
  const t = line.trim();
119
- if (/^[0-9a-f]{40}$/.test(t)) hashSet.add(t);
120
- }
121
- let currentHash = null;
122
- for (const line of lines) {
123
- const t = line.trim();
124
- if (!t) continue;
125
- if (hashSet.has(t)) {
126
- currentHash = t;
127
- } else if (currentHash && !map.has(currentHash)) {
128
- map.set(currentHash, t);
120
+ switch (state) {
121
+ case "expect_hash":
122
+ if (/^[0-9a-f]{40,64}$/.test(t)) {
123
+ currentHash = t;
124
+ state = "expect_blank";
125
+ }
126
+ break;
127
+ case "expect_blank":
128
+ if (t === "") {
129
+ state = "expect_path";
130
+ } else {
131
+ currentHash = "";
132
+ state = "expect_hash";
133
+ }
134
+ break;
135
+ case "expect_path":
136
+ if (t === "") break;
137
+ if (currentHash && !map.has(currentHash)) {
138
+ map.set(currentHash, t);
139
+ }
140
+ currentHash = "";
141
+ state = "expect_hash";
142
+ break;
129
143
  }
130
144
  }
131
145
  return map;
@@ -206,23 +220,13 @@ function commitMetaArgs(relPath, maxEntries) {
206
220
  }
207
221
  async function getDocHistoryAsync(filePath, slug, maxEntries = 50) {
208
222
  const relPath = filePath.startsWith("/") ? toRepoRelative(filePath) : filePath;
209
- let metaRecords = [];
210
- try {
211
- const { stdout } = await execFileAsync(
212
- "git",
213
- commitMetaArgs(relPath, maxEntries),
214
- { ...gitOpts() }
215
- );
216
- const logOutput = stdout.trim();
217
- if (logOutput) {
218
- metaRecords = parseCommitLog(logOutput);
219
- }
220
- } catch {
221
- }
223
+ const [metaRecords, hashToPath] = await Promise.all([
224
+ execFileAsync("git", commitMetaArgs(relPath, maxEntries), { ...gitOpts() }).then(({ stdout }) => stdout.trim() ? parseCommitLog(stdout.trim()) : []).catch(() => []),
225
+ buildHashToPathMapAsync(relPath, maxEntries)
226
+ ]);
222
227
  if (metaRecords.length === 0) {
223
228
  return { slug, filePath: relPath, entries: [] };
224
229
  }
225
- const hashToPath = await buildHashToPathMapAsync(relPath, maxEntries);
226
230
  const pairs = metaRecords.map((rec) => ({
227
231
  hash: rec.hash,
228
232
  path: hashToPath.get(rec.hash) ?? relPath
@@ -327,6 +331,7 @@ function getFileCommitsMeta(filePath) {
327
331
  return [];
328
332
  }
329
333
  }
334
+ var MAX_BUFFER_BYTES = 32 * 1024 * 1024;
330
335
  async function getFileCommitsMetaAsync(filePath) {
331
336
  const relPath = filePath.startsWith("/") ? toRepoRelative(filePath) : filePath;
332
337
  try {
@@ -339,16 +344,15 @@ async function getFileCommitsMetaAsync(filePath) {
339
344
  "--",
340
345
  relPath
341
346
  ],
342
- // Use the same opts as the sync variant (stdio suppressed, cwd = repo root).
343
- // maxBuffer default (1 MB) is intentionally kept — matching execFileSync's
344
- // behaviour: a file whose full history exceeds 1 MB would throw, get caught,
345
- // and return [], keeping the key absent from the manifest (same semantics
346
- // as the sync path's catch→[]).
347
- { ...gitOpts() }
347
+ { ...gitOpts(), maxBuffer: MAX_BUFFER_BYTES }
348
348
  );
349
349
  if (!stdout.trim()) return [];
350
350
  return parseCommitLog(stdout.trim());
351
- } catch {
351
+ } catch (err) {
352
+ console.warn(
353
+ `doc-history-server: getFileCommitsMetaAsync failed for "${relPath}" \u2014 file will be absent from the manifest.`,
354
+ err instanceof Error ? err.message : String(err)
355
+ );
352
356
  return [];
353
357
  }
354
358
  }
@@ -380,8 +384,10 @@ export {
380
384
  getFileCommits,
381
385
  getFirstCommit,
382
386
  getCommitInfo,
387
+ parseHashToPathMap,
383
388
  getDocHistoryAsync,
384
389
  getFileCommitsMeta,
390
+ MAX_BUFFER_BYTES,
385
391
  getFileCommitsMetaAsync,
386
392
  collectContentFiles,
387
393
  getDocHistory
@@ -86,9 +86,13 @@ function parseCommonArgs(args, extra) {
86
86
  }
87
87
  return { contentDir, locales, maxEntries };
88
88
  }
89
+ function isLoopback(host) {
90
+ return host === "127.0.0.1" || host === "::1" || host === "localhost";
91
+ }
89
92
  function parseServerArgs(args) {
90
93
  let port = 4322;
91
94
  let host = "127.0.0.1";
95
+ let allowLan = false;
92
96
  const common = parseCommonArgs(args, {
93
97
  onFlag: (flag, next) => {
94
98
  if (flag === "--port") {
@@ -104,10 +108,20 @@ function parseServerArgs(args) {
104
108
  host = next();
105
109
  return true;
106
110
  }
111
+ if (flag === "--allow-lan-history") {
112
+ allowLan = true;
113
+ return true;
114
+ }
107
115
  return false;
108
116
  }
109
117
  });
110
- return { ...common, port, host };
118
+ if (!isLoopback(host) && !allowLan) {
119
+ console.error(
120
+ `doc-history-server: binding to "${host}" exposes full git history on the LAN, including unpublished content. Pass --allow-lan-history to confirm this is intentional.`
121
+ );
122
+ process.exit(1);
123
+ }
124
+ return { ...common, port, host, allowLan };
111
125
  }
112
126
  function parseCliArgs(args) {
113
127
  let outDir = "";
package/dist/cli.js CHANGED
@@ -6,11 +6,11 @@ import {
6
6
  import {
7
7
  getContentDirEntries,
8
8
  parseCliArgs
9
- } from "./chunk-S2IER563.js";
9
+ } from "./chunk-Y26SYCW7.js";
10
10
  import {
11
11
  collectContentFiles,
12
12
  getDocHistoryAsync
13
- } from "./chunk-OGIEQWJI.js";
13
+ } from "./chunk-TP7M3HUM.js";
14
14
 
15
15
  // src/cli.ts
16
16
  import { mkdirSync, writeFileSync } from "fs";
@@ -27,11 +27,31 @@ declare function getFirstCommit(filePath: string): string | null;
27
27
  * Returns { hash, date, author, message } with full hash for unique identification.
28
28
  */
29
29
  declare function getCommitInfo(hash: string, filePath: string): Omit<DocHistoryEntry, "content">;
30
+ /**
31
+ * Pure parser for `git log --follow --format=%H --name-only` output.
32
+ *
33
+ * git emits records with the structure:
34
+ * <40-hex-hash>\n\n<filepath>\n
35
+ * where the blank line (`\n\n`) appears between the hash and the filepath —
36
+ * it is git's commit separator emitted for every --format output even when
37
+ * using the minimal %H format. Consecutive records are delimited by the
38
+ * filepath of the previous record followed immediately by the next hash line
39
+ * (single `\n` between them, not a blank line).
40
+ *
41
+ * Parsing strategy: scan line by line, using a state machine that transitions
42
+ * from EXPECT_HASH → EXPECT_BLANK → EXPECT_PATH and back. The blank-line
43
+ * state is the structural record boundary — it distinguishes the separator
44
+ * git injects after each format output from ordinary non-hash lines. This
45
+ * avoids the previous two-pass approach (collect all hashes → re-classify)
46
+ * and is robust against file paths that happen to be 40-char hex strings.
47
+ */
48
+ declare function parseHashToPathMap(output: string): Map<string, string>;
30
49
  /**
31
50
  * Get the complete history for a document file.
32
51
  *
33
- * Optimised: uses ONE `git log --follow` for commit metadata and ONE
34
- * `git log --follow --name-only` for the hash→path map, then a single
52
+ * Optimised: uses ONE `git log --follow` for commit metadata AND ONE
53
+ * `git log --follow --name-only` for the hash→path map, run in PARALLEL
54
+ * via Promise.all (they are independent git walks). Then a single
35
55
  * `git cat-file --batch` for all content fetches. Falls back to
36
56
  * per-commit logic only for batch misses (renamed-path entries where the
37
57
  * current path didn't exist at that commit).
@@ -46,11 +66,27 @@ declare function getDocHistoryAsync(filePath: string, slug: string, maxEntries?:
46
66
  * Used by pre-build meta to get both newest and oldest in one spawn.
47
67
  */
48
68
  declare function getFileCommitsMeta(filePath: string): Array<Omit<DocHistoryEntry, "content">>;
69
+ /**
70
+ * 32 MB maxBuffer for `getFileCommitsMetaAsync`. The Node default (1 MB) is
71
+ * too small for a doc corpus with long histories: an RangeError / ERR_CHILD_PROCESS_STDIO_MAXBUFFER
72
+ * from execFile's promisified form was previously silently caught and returned
73
+ * [], causing the file to disappear from the manifest without any trace (#2293).
74
+ * 32 MB covers a corpus of ~16 000 commits × 2 KB average format output before
75
+ * the guard kicks in; adjust via the exported MAX_BUFFER_BYTES constant if your
76
+ * project needs a different bound.
77
+ */
78
+ declare const MAX_BUFFER_BYTES: number;
49
79
  /**
50
80
  * Get commit metadata for a file using a single `git log --follow` walk
51
81
  * (async variant). Runs via execFile (non-blocking) so callers can issue
52
82
  * multiple git spawns concurrently. Returns an array of records newest-first
53
83
  * (no -n limit). Used by the pre-build parallelization path.
84
+ *
85
+ * maxBuffer is raised to MAX_BUFFER_BYTES (32 MB) to avoid silent data loss
86
+ * for files with very long histories — previously the default 1 MB limit
87
+ * caused the catch block to swallow the error and return [], dropping the
88
+ * file from the manifest. A warning is now logged on any catch so the
89
+ * failure is visible (#2293).
54
90
  */
55
91
  declare function getFileCommitsMetaAsync(filePath: string): Promise<Array<Omit<DocHistoryEntry, "content">>>;
56
92
  /**
@@ -69,4 +105,4 @@ declare function collectContentFiles(dir: string): Array<{
69
105
  */
70
106
  declare const getDocHistory: typeof getDocHistoryAsync;
71
107
 
72
- export { collectContentFiles, getCommitInfo, getDocHistory, getDocHistoryAsync, getFileCommits, getFileCommitsMeta, getFileCommitsMetaAsync, getFirstCommit };
108
+ export { MAX_BUFFER_BYTES, collectContentFiles, getCommitInfo, getDocHistory, getDocHistoryAsync, getFileCommits, getFileCommitsMeta, getFileCommitsMetaAsync, getFirstCommit, parseHashToPathMap };
@@ -1,4 +1,5 @@
1
1
  import {
2
+ MAX_BUFFER_BYTES,
2
3
  collectContentFiles,
3
4
  getCommitInfo,
4
5
  getDocHistory,
@@ -6,9 +7,11 @@ import {
6
7
  getFileCommits,
7
8
  getFileCommitsMeta,
8
9
  getFileCommitsMetaAsync,
9
- getFirstCommit
10
- } from "./chunk-OGIEQWJI.js";
10
+ getFirstCommit,
11
+ parseHashToPathMap
12
+ } from "./chunk-TP7M3HUM.js";
11
13
  export {
14
+ MAX_BUFFER_BYTES,
12
15
  collectContentFiles,
13
16
  getCommitInfo,
14
17
  getDocHistory,
@@ -16,5 +19,6 @@ export {
16
19
  getFileCommits,
17
20
  getFileCommitsMeta,
18
21
  getFileCommitsMetaAsync,
19
- getFirstCommit
22
+ getFirstCommit,
23
+ parseHashToPathMap
20
24
  };
package/dist/index.js CHANGED
@@ -2,16 +2,29 @@
2
2
  import {
3
3
  getContentDirEntries,
4
4
  parseServerArgs
5
- } from "./chunk-S2IER563.js";
5
+ } from "./chunk-Y26SYCW7.js";
6
6
  import {
7
7
  collectContentFiles,
8
8
  getDocHistoryAsync
9
- } from "./chunk-OGIEQWJI.js";
9
+ } from "./chunk-TP7M3HUM.js";
10
10
 
11
11
  // src/server.ts
12
12
  import { createServer } from "http";
13
13
  import { resolve } from "path";
14
+ import { statSync } from "fs";
14
15
  var FILE_INDEX_REFRESH_MS = 1e4;
16
+ function getContentDirsMtime(dirEntries) {
17
+ let latest = 0;
18
+ for (const [, dir] of dirEntries) {
19
+ try {
20
+ const ms = statSync(dir).mtimeMs;
21
+ if (ms > latest) latest = ms;
22
+ } catch {
23
+ return 0;
24
+ }
25
+ }
26
+ return latest;
27
+ }
15
28
  function setCorsHeaders(res, origin) {
16
29
  const isLocalhost = origin !== void 0 && /^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(origin);
17
30
  if (isLocalhost) {
@@ -50,9 +63,13 @@ function startServer(options2) {
50
63
  const dirEntries = getContentDirEntries(contentDir, locales);
51
64
  let fileIndex = buildFileIndex(dirEntries);
52
65
  console.log(`Indexed ${fileIndex.size} documents`);
66
+ let lastMtime = getContentDirsMtime(dirEntries);
53
67
  const refreshTimer = setInterval(() => {
54
68
  try {
69
+ const currentMtime = getContentDirsMtime(dirEntries);
70
+ if (currentMtime === lastMtime) return;
55
71
  fileIndex = buildFileIndex(dirEntries);
72
+ lastMtime = currentMtime;
56
73
  } catch {
57
74
  }
58
75
  }, FILE_INDEX_REFRESH_MS);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takazudo/zudo-doc-history-server",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "type": "module",
5
5
  "description": "Standalone doc git-history service for zudo-doc — HTTP server + CLI batch generator. Decouples expensive `git log --follow` calls from the main build pipeline.",
6
6
  "license": "MIT",