@hyperframes/share 0.9.106 → 0.10.0

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.
@@ -4,11 +4,13 @@ export function referencedHiddenDigest(paths: readonly string[]): string;
4
4
  export function contentKey(html: string): string;
5
5
  /** @typedef {Map<string, unknown>} Snapshot */
6
6
  /** @typedef {{ lenientIgnore?: boolean }} IgnoreOptions */
7
- /** @param {string} dir @param {{ byContent?: boolean, followSymlinks?: boolean } & IgnoreOptions} [options]
7
+ /** @param {string} dir
8
+ * @param {{ byContent?: boolean, followSymlinks?: boolean, progress?: () => void } & IgnoreOptions} [options]
8
9
  * @returns {Promise<Snapshot>} */
9
- export function snapshotProject(dir: string, { byContent, followSymlinks, lenientIgnore }?: {
10
+ export function snapshotProject(dir: string, { byContent, followSymlinks, lenientIgnore, progress }?: {
10
11
  byContent?: boolean;
11
12
  followSymlinks?: boolean;
13
+ progress?: () => void;
12
14
  } & IgnoreOptions): Promise<Snapshot>;
13
15
  /** What an export's sidecar records of the project, from a snapshot already taken.
14
16
  * @param {Snapshot} snapshot */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/share",
3
- "version": "0.9.106",
3
+ "version": "0.10.0",
4
4
  "description": "Shared review identity, version upload and sharing state for HyperFrames hosts",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -113,7 +113,7 @@
113
113
  "css-tree": "^3.2.1",
114
114
  "ignore": "^5.3.2",
115
115
  "linkedom": "^0.18.12",
116
- "@hyperframes/parsers": "0.9.106"
116
+ "@hyperframes/parsers": "0.10.0"
117
117
  },
118
118
  "devDependencies": {
119
119
  "@types/css-tree": "^3.2.0",
@@ -29,32 +29,65 @@ export function contentKey(html) {
29
29
  return sha256(html.replace(HF_ID_ATTRIBUTE, ""));
30
30
  }
31
31
 
32
+ const noTick = () => {};
33
+
34
+ /** A throw from the caller's `progress`, carried past the catches that skip one unreadable entry. */
35
+ class ProgressThrew {
36
+ /** @param {unknown} cause */
37
+ constructor(cause) {
38
+ this.cause = cause;
39
+ }
40
+ }
41
+
32
42
  /** A file renamed away between readdir and stat (atomic writes), or one the app may not read, is simply not there
33
- * (undefined): one unreadable entry must not blind the whole scan. @param {string} full @param {boolean} byContent */
34
- async function snapshotKey(full, byContent) {
43
+ * (undefined): one unreadable entry must not blind the whole scan. @param {string} full @param {boolean} byContent
44
+ * @param {() => void} [progress] */
45
+ async function snapshotKey(full, byContent, progress) {
46
+ const tick = progress ?? noTick;
35
47
  const resolved = await fs.realpath(full).catch(() => null);
36
48
  if (!resolved) return undefined;
49
+ tick();
37
50
  const handle = await fs
38
51
  .open(resolved, constants.O_RDONLY | constants.O_NOFOLLOW)
39
52
  .catch(() => null);
40
53
  if (!handle) return undefined;
41
54
  try {
42
55
  const stat = await handle.stat();
56
+ tick();
43
57
  if (!stat.isFile()) return undefined;
44
- if (full.endsWith(".html")) return contentKey(await handle.readFile("utf8"));
45
- if (byContent) return await hashFile(handle);
58
+ if (full.endsWith(".html")) return contentKey(await readText(handle, progress));
59
+ if (byContent) return await hashFile(handle, progress);
46
60
  return `${stat.mtimeMs}:${stat.size}`;
47
- } catch {
61
+ } catch (error) {
62
+ if (error instanceof ProgressThrew) throw error;
48
63
  return undefined;
49
64
  } finally {
50
65
  await handle.close();
51
66
  }
52
67
  }
53
68
 
54
- /** @param {import("node:fs/promises").FileHandle} handle */
55
- async function hashFile(handle) {
69
+ // Measured when the read starts and bounded as readFile bounds it, so both paths key the same bytes.
70
+ /** @param {import("node:fs/promises").FileHandle} handle @param {() => void} [progress] */
71
+ async function readText(handle, progress) {
72
+ if (!progress) return handle.readFile("utf8");
73
+ const { size } = await handle.stat();
74
+ const extent = size > 0 ? { end: size - 1 } : {};
75
+ const chunks = [];
76
+ const stream = handle.createReadStream({ encoding: "utf8", autoClose: false, ...extent });
77
+ for await (const chunk of stream) {
78
+ chunks.push(chunk);
79
+ progress();
80
+ }
81
+ return chunks.join("");
82
+ }
83
+
84
+ /** @param {import("node:fs/promises").FileHandle} handle @param {() => void} [progress] */
85
+ async function hashFile(handle, progress) {
56
86
  const hash = createHash("sha256");
57
- for await (const chunk of handle.createReadStream({ autoClose: false })) hash.update(chunk);
87
+ for await (const chunk of handle.createReadStream({ autoClose: false })) {
88
+ hash.update(chunk);
89
+ progress?.();
90
+ }
58
91
  return hash.digest("hex");
59
92
  }
60
93
 
@@ -70,19 +103,24 @@ async function entryKind(entry, full, followSymlinks) {
70
103
  // A `.hyperframesignore` this package can't read makes the snapshot throw, so a caller that picks files to send never
71
104
  // sends one the file meant to exclude. Change detection passes `lenientIgnore`: it then sees more files, never fewer.
72
105
  /** @typedef {{ lenientIgnore?: boolean }} IgnoreOptions */
73
- /** @param {string} dir @param {{ byContent?: boolean, followSymlinks?: boolean } & IgnoreOptions} [options]
106
+ // `progress` is called as the read moves, so a caller timing it out can tell a slow disk from a stuck one.
107
+ /** @param {string} dir
108
+ * @param {{ byContent?: boolean, followSymlinks?: boolean, progress?: () => void } & IgnoreOptions} [options]
74
109
  * @returns {Promise<Snapshot>} */
75
110
  export async function snapshotProject(
76
111
  dir,
77
- { byContent = false, followSymlinks = false, lenientIgnore = false } = {},
112
+ { byContent = false, followSymlinks = false, lenientIgnore = false, progress } = {},
78
113
  ) {
79
114
  /** @type {Snapshot} */
80
115
  const result = new Map();
81
116
  const ignored = await ignoreFor(dir, lenientIgnore);
117
+ const tick = progress ? () => callProgress(progress) : noTick;
118
+ const fileTick = progress && tick;
82
119
  /** @param {string} current @param {Set<string>} ancestors */
83
120
  async function walk(current, ancestors) {
84
121
  const real = await fs.realpath(current).catch(() => null);
85
122
  if (!real || ancestors.has(real)) return;
123
+ tick();
86
124
  const parents = new Set([...ancestors, real]);
87
125
  /** @type {import("node:fs").Dirent[]} */
88
126
  let entries;
@@ -91,21 +129,35 @@ export async function snapshotProject(
91
129
  } catch {
92
130
  return;
93
131
  }
132
+ tick();
94
133
  for (const entry of entries) {
95
134
  const full = path.join(current, entry.name);
96
135
  if (excludedFromSnapshot(dir, full, ignored)) continue;
97
136
  const kind = await entryKind(entry, full, followSymlinks);
98
137
  if (kind?.isDirectory()) await walk(full, parents);
99
138
  else if (kind?.isFile()) {
100
- const key = await snapshotKey(full, byContent);
139
+ const key = await snapshotKey(full, byContent, fileTick);
101
140
  if (key !== undefined) result.set(path.relative(dir, full), key);
102
141
  }
103
142
  }
104
143
  }
105
- await walk(dir, new Set());
144
+ try {
145
+ await walk(dir, new Set());
146
+ } catch (error) {
147
+ throw error instanceof ProgressThrew ? error.cause : error;
148
+ }
106
149
  return result;
107
150
  }
108
151
 
152
+ /** @param {() => void} progress */
153
+ function callProgress(progress) {
154
+ try {
155
+ progress();
156
+ } catch (cause) {
157
+ throw new ProgressThrew(cause);
158
+ }
159
+ }
160
+
109
161
  /** What an export's sidecar records of the project, from a snapshot already taken.
110
162
  * @param {Snapshot} snapshot */
111
163
  export function snapshotFingerprint(snapshot) {