@wastech-mdlint/viewer 0.0.0-stage → 0.4.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.
- package/LICENSE +21 -0
- package/README.md +86 -2
- package/assets/favicon.ico +0 -0
- package/assets/viewer.css +4184 -0
- package/dist/asset-routes.d.ts +4 -0
- package/dist/asset-routes.js +107 -0
- package/dist/blame.d.ts +8 -0
- package/dist/blame.js +54 -0
- package/dist/branch-changes.d.ts +8 -0
- package/dist/branch-changes.js +49 -0
- package/dist/candidate-lint-worker.d.ts +1 -0
- package/dist/candidate-lint-worker.js +67 -0
- package/dist/candidate-lint.d.ts +39 -0
- package/dist/candidate-lint.js +115 -0
- package/dist/change-banner.d.ts +38 -0
- package/dist/change-banner.js +108 -0
- package/dist/change-diff.d.ts +23 -0
- package/dist/change-diff.js +140 -0
- package/dist/change-marks.d.ts +32 -0
- package/dist/change-marks.js +363 -0
- package/dist/changes-article.d.ts +15 -0
- package/dist/changes-article.js +244 -0
- package/dist/changes-routes.d.ts +12 -0
- package/dist/changes-routes.js +271 -0
- package/dist/changes.d.ts +113 -0
- package/dist/changes.js +324 -0
- package/dist/cli-args.d.ts +21 -0
- package/dist/cli-args.js +93 -0
- package/dist/client/banner-height.d.ts +1 -0
- package/dist/client/banner-height.js +33 -0
- package/dist/client/change-marks.d.ts +1 -0
- package/dist/client/change-marks.js +56 -0
- package/dist/client/changes.d.ts +1 -0
- package/dist/client/changes.js +183 -0
- package/dist/client/chart-option.d.ts +3 -0
- package/dist/client/chart-option.js +76 -0
- package/dist/client/charts.d.ts +1 -0
- package/dist/client/charts.js +54 -0
- package/dist/client/clipboard.d.ts +1 -0
- package/dist/client/clipboard.js +43 -0
- package/dist/client/code-copy.d.ts +1 -0
- package/dist/client/code-copy.js +29 -0
- package/dist/client/column-resize.d.ts +1 -0
- package/dist/client/column-resize.js +120 -0
- package/dist/client/config-dom.d.ts +13 -0
- package/dist/client/config-dom.js +47 -0
- package/dist/client/config-draft.d.ts +73 -0
- package/dist/client/config-draft.js +218 -0
- package/dist/client/config-editor.d.ts +3 -0
- package/dist/client/config-editor.js +677 -0
- package/dist/client/config-fields.d.ts +35 -0
- package/dist/client/config-fields.js +339 -0
- package/dist/client/config-json.d.ts +18 -0
- package/dist/client/config-json.js +96 -0
- package/dist/client/config-preview.d.ts +9 -0
- package/dist/client/config-preview.js +91 -0
- package/dist/client/config-rules.d.ts +14 -0
- package/dist/client/config-rules.js +202 -0
- package/dist/client/config-types.d.ts +91 -0
- package/dist/client/config-types.js +4 -0
- package/dist/client/copy-path.d.ts +1 -0
- package/dist/client/copy-path.js +32 -0
- package/dist/client/dashboard-requests.d.ts +11 -0
- package/dist/client/dashboard-requests.js +28 -0
- package/dist/client/dashboard.d.ts +1 -0
- package/dist/client/dashboard.js +119 -0
- package/dist/client/diagnostics.d.ts +1 -0
- package/dist/client/diagnostics.js +164 -0
- package/dist/client/diagrams.d.ts +1 -0
- package/dist/client/diagrams.js +81 -0
- package/dist/client/diff-expand.d.ts +1 -0
- package/dist/client/diff-expand.js +42 -0
- package/dist/client/display-settings-model.d.ts +74 -0
- package/dist/client/display-settings-model.js +229 -0
- package/dist/client/display-settings.d.ts +1 -0
- package/dist/client/display-settings.js +411 -0
- package/dist/client/echarts.d.ts +30 -0
- package/dist/client/echarts.js +14 -0
- package/dist/client/graph-layout.d.ts +88 -0
- package/dist/client/graph-layout.js +198 -0
- package/dist/client/graph.d.ts +2 -0
- package/dist/client/graph.js +566 -0
- package/dist/client/history.d.ts +21 -0
- package/dist/client/history.js +86 -0
- package/dist/client/live-reload-model.d.ts +28 -0
- package/dist/client/live-reload-model.js +52 -0
- package/dist/client/live-reload.d.ts +9 -0
- package/dist/client/live-reload.js +138 -0
- package/dist/client/menu.d.ts +14 -0
- package/dist/client/menu.js +176 -0
- package/dist/client/navigation-feedback.d.ts +1 -0
- package/dist/client/navigation-feedback.js +141 -0
- package/dist/client/open-config.d.ts +1 -0
- package/dist/client/open-config.js +48 -0
- package/dist/client/outline-spy.d.ts +1 -0
- package/dist/client/outline-spy.js +60 -0
- package/dist/client/prompts.d.ts +7 -0
- package/dist/client/prompts.js +65 -0
- package/dist/client/review.d.ts +1 -0
- package/dist/client/review.js +379 -0
- package/dist/client/search.d.ts +1 -0
- package/dist/client/search.js +199 -0
- package/dist/client/segmented.d.ts +13 -0
- package/dist/client/segmented.js +70 -0
- package/dist/client/source-lines.d.ts +16 -0
- package/dist/client/source-lines.js +47 -0
- package/dist/client/theme-toggle.d.ts +10 -0
- package/dist/client/theme-toggle.js +100 -0
- package/dist/client/theme.d.ts +0 -0
- package/dist/client/theme.js +50 -0
- package/dist/client/tree-marks.d.ts +31 -0
- package/dist/client/tree-marks.js +102 -0
- package/dist/client/tree-state.d.ts +0 -0
- package/dist/client/tree-state.js +179 -0
- package/dist/client/viewer.d.ts +1 -0
- package/dist/client/viewer.js +53 -0
- package/dist/config-editor.d.ts +166 -0
- package/dist/config-editor.js +378 -0
- package/dist/config-routes.d.ts +18 -0
- package/dist/config-routes.js +213 -0
- package/dist/context-view.d.ts +41 -0
- package/dist/context-view.js +295 -0
- package/dist/corpus-routes.d.ts +31 -0
- package/dist/corpus-routes.js +258 -0
- package/dist/corpus.d.ts +134 -0
- package/dist/corpus.js +653 -0
- package/dist/dashboard-article.d.ts +14 -0
- package/dist/dashboard-article.js +290 -0
- package/dist/dashboard.d.ts +94 -0
- package/dist/dashboard.js +468 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.js +44 -0
- package/dist/document-routes.d.ts +39 -0
- package/dist/document-routes.js +396 -0
- package/dist/event-hub.d.ts +39 -0
- package/dist/event-hub.js +120 -0
- package/dist/findings-panel.d.ts +4 -0
- package/dist/findings-panel.js +74 -0
- package/dist/folder-page.d.ts +8 -0
- package/dist/folder-page.js +74 -0
- package/dist/graph-view.d.ts +66 -0
- package/dist/graph-view.js +236 -0
- package/dist/highlight.d.ts +24 -0
- package/dist/highlight.js +100 -0
- package/dist/http-send.d.ts +9 -0
- package/dist/http-send.js +53 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +72 -0
- package/dist/open-file.d.ts +36 -0
- package/dist/open-file.js +87 -0
- package/dist/page-tree.d.ts +3 -0
- package/dist/page-tree.js +105 -0
- package/dist/page.d.ts +73 -0
- package/dist/page.js +478 -0
- package/dist/prompts.d.ts +25 -0
- package/dist/prompts.js +82 -0
- package/dist/render-cache.d.ts +23 -0
- package/dist/render-cache.js +38 -0
- package/dist/render-document.d.ts +60 -0
- package/dist/render-document.js +440 -0
- package/dist/request-path.d.ts +17 -0
- package/dist/request-path.js +51 -0
- package/dist/review-context.d.ts +24 -0
- package/dist/review-context.js +109 -0
- package/dist/review-queries.d.ts +26 -0
- package/dist/review-queries.js +69 -0
- package/dist/review-state.d.ts +106 -0
- package/dist/review-state.js +251 -0
- package/dist/review-store.d.ts +145 -0
- package/dist/review-store.js +530 -0
- package/dist/router.d.ts +28 -0
- package/dist/router.js +294 -0
- package/dist/search.d.ts +22 -0
- package/dist/search.js +164 -0
- package/dist/security.d.ts +95 -0
- package/dist/security.js +196 -0
- package/dist/server.d.ts +21 -0
- package/dist/server.js +224 -0
- package/dist/speculation-rules.d.ts +4 -0
- package/dist/speculation-rules.js +49 -0
- package/dist/state-dir.d.ts +6 -0
- package/dist/state-dir.js +31 -0
- package/dist/tree-marks.d.ts +49 -0
- package/dist/tree-marks.js +108 -0
- package/dist/viewer-context.d.ts +37 -0
- package/dist/viewer-context.js +74 -0
- package/dist/watch.d.ts +41 -0
- package/dist/watch.js +135 -0
- package/package.json +57 -4
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/** Where a changed document stands against the reader's marks. Computed on every read, never stored. */
|
|
2
|
+
export type ReviewStatus = "unreviewed" | "reviewed" | "changed";
|
|
3
|
+
/**
|
|
4
|
+
* The hash a mark is kept under: SHA-256 of the text as core normalizes it for a diff, not git's
|
|
5
|
+
* blob id. Uncommitted text has no blob, and with `core.autocrlf` the blob and the file on disk differ
|
|
6
|
+
* by every line ending although a reader sees the same document.
|
|
7
|
+
*/
|
|
8
|
+
export declare function contentHash(text: string): string;
|
|
9
|
+
/** One section of a document as a page shows it: its key and the hash of its own text. */
|
|
10
|
+
export type SectionHash = {
|
|
11
|
+
key: string;
|
|
12
|
+
hash: string;
|
|
13
|
+
};
|
|
14
|
+
/** Per path, per section key, the hashes of the section texts reviewed there, newest first. */
|
|
15
|
+
export type SectionMarks = Map<string, Map<string, string[]>>;
|
|
16
|
+
/** The marks as one read of the index found them, with the questions the pages ask of them. */
|
|
17
|
+
export declare class ReviewMarks {
|
|
18
|
+
readonly documents: ReadonlyMap<string, readonly string[]>;
|
|
19
|
+
readonly sections: SectionMarks;
|
|
20
|
+
private readonly keyOf;
|
|
21
|
+
private readonly hashes;
|
|
22
|
+
constructor(documents: ReadonlyMap<string, readonly string[]>, sections: SectionMarks, keyOf: (relPath: string) => string);
|
|
23
|
+
/**
|
|
24
|
+
* Where one section stands: reviewed while its own text is a text reviewed under its key, whatever
|
|
25
|
+
* happened to the rest of the document since; changed since review when its key has marks for
|
|
26
|
+
* other texts. A renamed heading has a new key, and starts unreviewed.
|
|
27
|
+
*/
|
|
28
|
+
sectionStatus(entry: {
|
|
29
|
+
path: string;
|
|
30
|
+
oldPath?: string;
|
|
31
|
+
key: string;
|
|
32
|
+
hash: string;
|
|
33
|
+
}): ReviewStatus;
|
|
34
|
+
/**
|
|
35
|
+
* Where a document stands. Reviewed when its text was marked under its path, under the path a
|
|
36
|
+
* rename took it from, or under any path at all — so a document moved without an edit, or found
|
|
37
|
+
* on another branch under another name, is not reviewed twice. Changed since review when its path
|
|
38
|
+
* (or the path it was renamed from) has marks for other texts; `since` is the newest of them, the
|
|
39
|
+
* version to compare with.
|
|
40
|
+
*/
|
|
41
|
+
status(entry: {
|
|
42
|
+
path: string;
|
|
43
|
+
oldPath?: string;
|
|
44
|
+
hash: string;
|
|
45
|
+
sections?: readonly SectionHash[];
|
|
46
|
+
}): {
|
|
47
|
+
status: ReviewStatus;
|
|
48
|
+
since?: string;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** A change to the marks, in the host root's paths; the store restates them from the top level. */
|
|
52
|
+
export type MarkChange = {
|
|
53
|
+
kind: "mark";
|
|
54
|
+
path: string;
|
|
55
|
+
hash: string;
|
|
56
|
+
text: string;
|
|
57
|
+
sections?: readonly SectionHash[];
|
|
58
|
+
} | {
|
|
59
|
+
kind: "unmark";
|
|
60
|
+
path: string;
|
|
61
|
+
oldPath?: string;
|
|
62
|
+
hash: string;
|
|
63
|
+
sections?: readonly SectionHash[];
|
|
64
|
+
} | {
|
|
65
|
+
kind: "section";
|
|
66
|
+
path: string;
|
|
67
|
+
oldPath?: string;
|
|
68
|
+
key: string;
|
|
69
|
+
hash: string;
|
|
70
|
+
reviewed: boolean;
|
|
71
|
+
} | {
|
|
72
|
+
kind: "reset";
|
|
73
|
+
documents: readonly {
|
|
74
|
+
path: string;
|
|
75
|
+
oldPath?: string;
|
|
76
|
+
hash: string;
|
|
77
|
+
}[];
|
|
78
|
+
};
|
|
79
|
+
export type StoreRead = {
|
|
80
|
+
ok: true;
|
|
81
|
+
marks: ReviewMarks;
|
|
82
|
+
} | {
|
|
83
|
+
ok: false;
|
|
84
|
+
message: string;
|
|
85
|
+
};
|
|
86
|
+
export type WriteOutcome = {
|
|
87
|
+
ok: true;
|
|
88
|
+
marks: ReviewMarks;
|
|
89
|
+
} | {
|
|
90
|
+
ok: false;
|
|
91
|
+
status: "failed" | "busy" | "refused";
|
|
92
|
+
message: string;
|
|
93
|
+
};
|
|
94
|
+
export type ReviewStoreOptions = {
|
|
95
|
+
stateDir: string;
|
|
96
|
+
commonDir: string;
|
|
97
|
+
keyOf: (relPath: string) => string;
|
|
98
|
+
lockWaitMs?: number;
|
|
99
|
+
};
|
|
100
|
+
export declare class ReviewStore {
|
|
101
|
+
/** The clone's own folder. Absolute, so it is never shown on a page or in a payload. */
|
|
102
|
+
readonly directory: string;
|
|
103
|
+
private readonly indexPath;
|
|
104
|
+
private readonly lockPath;
|
|
105
|
+
private readonly textsDir;
|
|
106
|
+
private readonly commonDir;
|
|
107
|
+
private readonly keyOf;
|
|
108
|
+
private readonly lockWaitMs;
|
|
109
|
+
private queue;
|
|
110
|
+
constructor(options: ReviewStoreOptions);
|
|
111
|
+
/**
|
|
112
|
+
* The marks as they are now. Creates nothing: a clone never marked, or a state directory that does
|
|
113
|
+
* not exist, simply has no marks. Read without the lock, since every write replaces the index in
|
|
114
|
+
* one rename and a reader sees either the old version or the new one, whole.
|
|
115
|
+
*/
|
|
116
|
+
read(): Promise<StoreRead>;
|
|
117
|
+
/** The text a mark was made on, or `undefined` when it is no longer kept. */
|
|
118
|
+
snapshot(hash: string): Promise<string | undefined>;
|
|
119
|
+
/**
|
|
120
|
+
* Apply a change and answer the marks it left.
|
|
121
|
+
*
|
|
122
|
+
* Under the lock every viewer of the clone takes, the index is read again, so two viewers merge
|
|
123
|
+
* their marks rather than the later write dropping the other's: marks for different documents
|
|
124
|
+
* both stay, and for the same document the later one wins, both being the same reader's. A text
|
|
125
|
+
* goes in before the index that refers to it, so a failure in between leaves an unreferenced text
|
|
126
|
+
* at worst, which is removed again. The index is replaced in one rename: a failed write leaves the
|
|
127
|
+
* previous marks whole, and is reported rather than shown as done.
|
|
128
|
+
*/
|
|
129
|
+
update(change: MarkChange): Promise<WriteOutcome>;
|
|
130
|
+
private locked;
|
|
131
|
+
/**
|
|
132
|
+
* Take the clone's lock, or say why not. Creating the file exclusively is atomic on every local
|
|
133
|
+
* filesystem the viewer runs on, so of two viewers only one gets it.
|
|
134
|
+
*/
|
|
135
|
+
private acquire;
|
|
136
|
+
private lockHeld;
|
|
137
|
+
private readIndex;
|
|
138
|
+
private dropUnreferencedTexts;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The clone's folder name: a hash of its common git directory, so every worktree of one clone shares
|
|
142
|
+
* it and a fresh clone starts empty, after the clone's own folder name, so a person looking at the
|
|
143
|
+
* state directory can tell which folder to delete. Only characters every filesystem accepts.
|
|
144
|
+
*/
|
|
145
|
+
export declare function cloneFolder(commonDir: string): string;
|
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdir, open, readdir, readFile, stat, unlink } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
|
+
import { compareStrings, normalizeText, writeFileAtomic, } from "@wastech-mdlint/core";
|
|
6
|
+
/**
|
|
7
|
+
* The hash a mark is kept under: SHA-256 of the text as core normalizes it for a diff, not git's
|
|
8
|
+
* blob id. Uncommitted text has no blob, and with `core.autocrlf` the blob and the file on disk differ
|
|
9
|
+
* by every line ending although a reader sees the same document.
|
|
10
|
+
*/
|
|
11
|
+
export function contentHash(text) {
|
|
12
|
+
return createHash("sha256").update(normalizeText(text), "utf8").digest("hex");
|
|
13
|
+
}
|
|
14
|
+
// A deleted document's text. Its mark says "this deletion was reviewed" about one path only: looked
|
|
15
|
+
// up by content like any other, one reviewed deletion would mark every deleted document reviewed.
|
|
16
|
+
const EMPTY_HASH = contentHash("");
|
|
17
|
+
const HASH = /^[0-9a-f]{64}$/;
|
|
18
|
+
const TEXT_FILE = /^([0-9a-f]{64})\.md$/;
|
|
19
|
+
// Format 2 added per-section marks. A file of format 1 has none and is read as it is; a viewer that
|
|
20
|
+
// knew only format 1 refuses a file of format 2 rather than writing it back without its sections.
|
|
21
|
+
const FORMAT = 2;
|
|
22
|
+
// How many reviewed versions a path remembers. More than one, so switching back to a branch whose
|
|
23
|
+
// text was reviewed before finds it reviewed although another version was reviewed in between; a
|
|
24
|
+
// few, so a document reviewed on many branches over months does not keep every text it ever had.
|
|
25
|
+
const VERSIONS_PER_PATH = 10;
|
|
26
|
+
// A write holds the lock for milliseconds. One older than this was left by a viewer that died while
|
|
27
|
+
// writing, and is taken over rather than blocking every mark until someone deletes it by hand.
|
|
28
|
+
const LOCK_STALE_MS = 10_000;
|
|
29
|
+
const LOCK_RETRY_MS = 25;
|
|
30
|
+
const LOCK_WAIT_MS = 3_000;
|
|
31
|
+
/** The marks as one read of the index found them, with the questions the pages ask of them. */
|
|
32
|
+
export class ReviewMarks {
|
|
33
|
+
documents;
|
|
34
|
+
sections;
|
|
35
|
+
keyOf;
|
|
36
|
+
// Every reviewed text but the empty one, for finding a document reviewed under another path.
|
|
37
|
+
hashes;
|
|
38
|
+
constructor(
|
|
39
|
+
// Keyed by path from the work tree's top level, as the index stores them.
|
|
40
|
+
documents, sections, keyOf) {
|
|
41
|
+
this.documents = documents;
|
|
42
|
+
this.sections = sections;
|
|
43
|
+
this.keyOf = keyOf;
|
|
44
|
+
this.hashes = new Set([...documents.values()].flat().filter((hash) => hash !== EMPTY_HASH));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Where one section stands: reviewed while its own text is a text reviewed under its key, whatever
|
|
48
|
+
* happened to the rest of the document since; changed since review when its key has marks for
|
|
49
|
+
* other texts. A renamed heading has a new key, and starts unreviewed.
|
|
50
|
+
*/
|
|
51
|
+
sectionStatus(entry) {
|
|
52
|
+
const marked = [entry.path, entry.oldPath]
|
|
53
|
+
.filter((relPath) => relPath !== undefined)
|
|
54
|
+
.flatMap((relPath) => this.sections.get(this.keyOf(relPath))?.get(entry.key) ?? []);
|
|
55
|
+
if (marked.includes(entry.hash)) {
|
|
56
|
+
return "reviewed";
|
|
57
|
+
}
|
|
58
|
+
return marked.length > 0 ? "changed" : "unreviewed";
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Where a document stands. Reviewed when its text was marked under its path, under the path a
|
|
62
|
+
* rename took it from, or under any path at all — so a document moved without an edit, or found
|
|
63
|
+
* on another branch under another name, is not reviewed twice. Changed since review when its path
|
|
64
|
+
* (or the path it was renamed from) has marks for other texts; `since` is the newest of them, the
|
|
65
|
+
* version to compare with.
|
|
66
|
+
*/
|
|
67
|
+
status(entry) {
|
|
68
|
+
const own = this.documents.get(this.keyOf(entry.path)) ?? [];
|
|
69
|
+
const old = entry.oldPath === undefined
|
|
70
|
+
? []
|
|
71
|
+
: (this.documents.get(this.keyOf(entry.oldPath)) ?? []);
|
|
72
|
+
const sections = entry.sections ?? [];
|
|
73
|
+
const sectionStatuses = sections.map((section) => this.sectionStatus({
|
|
74
|
+
path: entry.path,
|
|
75
|
+
...(entry.oldPath === undefined ? {} : { oldPath: entry.oldPath }),
|
|
76
|
+
...section,
|
|
77
|
+
}));
|
|
78
|
+
if (own.includes(entry.hash) ||
|
|
79
|
+
old.includes(entry.hash) ||
|
|
80
|
+
(entry.hash !== EMPTY_HASH && this.hashes.has(entry.hash)) ||
|
|
81
|
+
(sections.length > 0 &&
|
|
82
|
+
sectionStatuses.every((status) => status === "reviewed"))) {
|
|
83
|
+
return { status: "reviewed" };
|
|
84
|
+
}
|
|
85
|
+
const since = own[0] ?? old[0];
|
|
86
|
+
if (since !== undefined) {
|
|
87
|
+
return { status: "changed", since };
|
|
88
|
+
}
|
|
89
|
+
// Sections reviewed one at a time, the document never as a whole: started, not finished.
|
|
90
|
+
return sectionStatuses.some((status) => status !== "unreviewed")
|
|
91
|
+
? { status: "changed" }
|
|
92
|
+
: { status: "unreviewed" };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export class ReviewStore {
|
|
96
|
+
/** The clone's own folder. Absolute, so it is never shown on a page or in a payload. */
|
|
97
|
+
directory;
|
|
98
|
+
indexPath;
|
|
99
|
+
lockPath;
|
|
100
|
+
textsDir;
|
|
101
|
+
commonDir;
|
|
102
|
+
keyOf;
|
|
103
|
+
lockWaitMs;
|
|
104
|
+
// Writes from this process run one at a time, so they never wait on each other's lock.
|
|
105
|
+
queue = Promise.resolve();
|
|
106
|
+
constructor(options) {
|
|
107
|
+
this.commonDir = options.commonDir;
|
|
108
|
+
this.keyOf = options.keyOf;
|
|
109
|
+
this.lockWaitMs = options.lockWaitMs ?? LOCK_WAIT_MS;
|
|
110
|
+
this.directory = path.join(options.stateDir, cloneFolder(options.commonDir));
|
|
111
|
+
this.indexPath = path.join(this.directory, "review.json");
|
|
112
|
+
this.lockPath = path.join(this.directory, "review.lock");
|
|
113
|
+
this.textsDir = path.join(this.directory, "texts");
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The marks as they are now. Creates nothing: a clone never marked, or a state directory that does
|
|
117
|
+
* not exist, simply has no marks. Read without the lock, since every write replaces the index in
|
|
118
|
+
* one rename and a reader sees either the old version or the new one, whole.
|
|
119
|
+
*/
|
|
120
|
+
async read() {
|
|
121
|
+
const index = await this.readIndex();
|
|
122
|
+
return index.ok
|
|
123
|
+
? {
|
|
124
|
+
ok: true,
|
|
125
|
+
marks: new ReviewMarks(index.documents, index.sections, this.keyOf),
|
|
126
|
+
}
|
|
127
|
+
: index;
|
|
128
|
+
}
|
|
129
|
+
/** The text a mark was made on, or `undefined` when it is no longer kept. */
|
|
130
|
+
async snapshot(hash) {
|
|
131
|
+
if (!HASH.test(hash)) {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
return readFile(path.join(this.textsDir, `${hash}.md`), "utf8").catch(() => undefined);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Apply a change and answer the marks it left.
|
|
138
|
+
*
|
|
139
|
+
* Under the lock every viewer of the clone takes, the index is read again, so two viewers merge
|
|
140
|
+
* their marks rather than the later write dropping the other's: marks for different documents
|
|
141
|
+
* both stay, and for the same document the later one wins, both being the same reader's. A text
|
|
142
|
+
* goes in before the index that refers to it, so a failure in between leaves an unreferenced text
|
|
143
|
+
* at worst, which is removed again. The index is replaced in one rename: a failed write leaves the
|
|
144
|
+
* previous marks whole, and is reported rather than shown as done.
|
|
145
|
+
*/
|
|
146
|
+
update(change) {
|
|
147
|
+
const run = this.queue.then(() => this.locked(change));
|
|
148
|
+
this.queue = run.catch(() => undefined);
|
|
149
|
+
return run;
|
|
150
|
+
}
|
|
151
|
+
async locked(change) {
|
|
152
|
+
try {
|
|
153
|
+
// Owner-only on POSIX: the folder holds copies of the repository's documents, and a home
|
|
154
|
+
// directory that others can list would otherwise let them read these too.
|
|
155
|
+
await mkdir(this.textsDir, { recursive: true, mode: 0o700 });
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
return failed(error);
|
|
159
|
+
}
|
|
160
|
+
const lock = await this.acquire();
|
|
161
|
+
if (lock !== undefined) {
|
|
162
|
+
return lock;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const index = await this.readIndex();
|
|
166
|
+
if (!index.ok) {
|
|
167
|
+
return { ok: false, status: "refused", message: index.message };
|
|
168
|
+
}
|
|
169
|
+
const documents = new Map([...index.documents].map(([key, hashes]) => [key, [...hashes]]));
|
|
170
|
+
const sections = new Map([...index.sections].map(([key, marks]) => [
|
|
171
|
+
key,
|
|
172
|
+
new Map([...marks].map(([section, hashes]) => [section, [...hashes]])),
|
|
173
|
+
]));
|
|
174
|
+
applyChange({ documents, sections }, change, this.keyOf);
|
|
175
|
+
const created = [];
|
|
176
|
+
if (change.kind === "mark") {
|
|
177
|
+
const target = path.join(this.textsDir, `${change.hash}.md`);
|
|
178
|
+
const exists = await stat(target).then(() => true, () => false);
|
|
179
|
+
if (!exists) {
|
|
180
|
+
try {
|
|
181
|
+
await writeFileAtomic(target, normalizeText(change.text));
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
return failed(error);
|
|
185
|
+
}
|
|
186
|
+
created.push(target);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
await writeFileAtomic(this.indexPath, serializeIndex(this.commonDir, documents, sections));
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
await Promise.all(created.map((target) => unlink(target).catch(() => undefined)));
|
|
194
|
+
return failed(error);
|
|
195
|
+
}
|
|
196
|
+
await this.dropUnreferencedTexts(documents);
|
|
197
|
+
return {
|
|
198
|
+
ok: true,
|
|
199
|
+
marks: new ReviewMarks(documents, sections, this.keyOf),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
await unlink(this.lockPath).catch(() => undefined);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Take the clone's lock, or say why not. Creating the file exclusively is atomic on every local
|
|
208
|
+
* filesystem the viewer runs on, so of two viewers only one gets it.
|
|
209
|
+
*/
|
|
210
|
+
async acquire() {
|
|
211
|
+
const deadline = Date.now() + this.lockWaitMs;
|
|
212
|
+
for (;;) {
|
|
213
|
+
let handle;
|
|
214
|
+
try {
|
|
215
|
+
handle = await open(this.lockPath, "wx", 0o600);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
if (!(await this.lockHeld(error))) {
|
|
219
|
+
return failed(error);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (handle !== undefined) {
|
|
223
|
+
const held = handle;
|
|
224
|
+
// The process id, for a person who finds a lock left behind; nothing reads it back, so a
|
|
225
|
+
// failure to write it leaves the lock just as held.
|
|
226
|
+
await held
|
|
227
|
+
.writeFile(`${String(process.pid)}\n`)
|
|
228
|
+
.catch(() => undefined)
|
|
229
|
+
.finally(() => held.close().catch(() => undefined));
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
const age = await stat(this.lockPath).then((stats) => Date.now() - stats.mtimeMs, () => 0);
|
|
233
|
+
if (age > LOCK_STALE_MS) {
|
|
234
|
+
// Two viewers can both find the same stale lock and both remove it; then each tries to
|
|
235
|
+
// create it again and only one succeeds, so the cost of that race is one extra retry.
|
|
236
|
+
await unlink(this.lockPath).catch(() => undefined);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (Date.now() >= deadline) {
|
|
240
|
+
return {
|
|
241
|
+
ok: false,
|
|
242
|
+
status: "busy",
|
|
243
|
+
message: "Another viewer of this repository is saving review marks. Try again in a moment.",
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
await delay(LOCK_RETRY_MS);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// Whether creating the lock failed because another viewer holds it, rather than because the folder
|
|
250
|
+
// cannot be written. Windows refuses to create a file that is still being deleted with an access
|
|
251
|
+
// error rather than "exists", so an access error counts as held while the lock is still there.
|
|
252
|
+
async lockHeld(error) {
|
|
253
|
+
const code = errnoCode(error);
|
|
254
|
+
if (code === "EEXIST") {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
if (code !== "EPERM" && code !== "EACCES") {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
return stat(this.lockPath).then(() => true, (statError) => {
|
|
261
|
+
const statCode = errnoCode(statError);
|
|
262
|
+
return statCode === "EPERM" || statCode === "EACCES";
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
async readIndex() {
|
|
266
|
+
let text;
|
|
267
|
+
try {
|
|
268
|
+
text = await readFile(this.indexPath, "utf8");
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
const code = errnoCode(error);
|
|
272
|
+
// Nothing stored yet — or a state directory that is not a directory, where nothing can be.
|
|
273
|
+
if (code === "ENOENT" || code === "ENOTDIR") {
|
|
274
|
+
return { ok: true, documents: new Map(), sections: new Map() };
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
ok: false,
|
|
278
|
+
message: `The review marks could not be read (${code ?? "read error"}).`,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return parseIndex(text);
|
|
282
|
+
}
|
|
283
|
+
async dropUnreferencedTexts(documents) {
|
|
284
|
+
const referenced = new Set([...documents.values()].flat());
|
|
285
|
+
const names = await readdir(this.textsDir).catch(() => []);
|
|
286
|
+
const unreferenced = names.filter((name) => {
|
|
287
|
+
const hash = TEXT_FILE.exec(name)?.[1];
|
|
288
|
+
return hash !== undefined && !referenced.has(hash);
|
|
289
|
+
});
|
|
290
|
+
await Promise.all(unreferenced.map((name) => unlink(path.join(this.textsDir, name)).catch(() => undefined)));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* The clone's folder name: a hash of its common git directory, so every worktree of one clone shares
|
|
295
|
+
* it and a fresh clone starts empty, after the clone's own folder name, so a person looking at the
|
|
296
|
+
* state directory can tell which folder to delete. Only characters every filesystem accepts.
|
|
297
|
+
*/
|
|
298
|
+
export function cloneFolder(commonDir) {
|
|
299
|
+
const digest = createHash("sha256")
|
|
300
|
+
.update(commonDir, "utf8")
|
|
301
|
+
.digest("hex")
|
|
302
|
+
.slice(0, 16);
|
|
303
|
+
// `.git` inside a work tree: the work tree's folder names the clone. A bare clone's own folder
|
|
304
|
+
// (`name.git`) names it.
|
|
305
|
+
const base = path.basename(commonDir);
|
|
306
|
+
const name = base.toLowerCase() === ".git"
|
|
307
|
+
? path.basename(path.dirname(commonDir))
|
|
308
|
+
: base.replace(/\.git$/i, "");
|
|
309
|
+
const label = name
|
|
310
|
+
.replace(/[^A-Za-z0-9._-]+/g, "-")
|
|
311
|
+
.replace(/^[-.]+|[-.]+$/g, "")
|
|
312
|
+
.slice(0, 40);
|
|
313
|
+
return `${label === "" ? "repository" : label}-${digest}`;
|
|
314
|
+
}
|
|
315
|
+
function applyChange(state, change, keyOf) {
|
|
316
|
+
const editor = new MarkEditor(state, keyOf);
|
|
317
|
+
switch (change.kind) {
|
|
318
|
+
case "mark":
|
|
319
|
+
editor.mark(change);
|
|
320
|
+
break;
|
|
321
|
+
case "unmark":
|
|
322
|
+
editor.unmark(change);
|
|
323
|
+
break;
|
|
324
|
+
case "section":
|
|
325
|
+
editor.section(change);
|
|
326
|
+
break;
|
|
327
|
+
case "reset":
|
|
328
|
+
editor.reset(change);
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
dropEmpty(state);
|
|
332
|
+
}
|
|
333
|
+
// The edits one change makes to the marks read from the index, before they are written back.
|
|
334
|
+
class MarkEditor {
|
|
335
|
+
state;
|
|
336
|
+
keyOf;
|
|
337
|
+
constructor(state, keyOf) {
|
|
338
|
+
this.state = state;
|
|
339
|
+
this.keyOf = keyOf;
|
|
340
|
+
}
|
|
341
|
+
mark(change) {
|
|
342
|
+
const { documents } = this.state;
|
|
343
|
+
const key = this.keyOf(change.path);
|
|
344
|
+
const hashes = documents.get(key) ?? [];
|
|
345
|
+
documents.set(key, [change.hash, ...hashes.filter((hash) => hash !== change.hash)].slice(0, VERSIONS_PER_PATH));
|
|
346
|
+
for (const section of change.sections ?? []) {
|
|
347
|
+
this.markSection(key, section);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
unmark(change) {
|
|
351
|
+
const keys = this.keysOf(change);
|
|
352
|
+
this.removeEverywhere(change.hash, keys);
|
|
353
|
+
for (const section of change.sections ?? []) {
|
|
354
|
+
this.unmarkSection(keys, section);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
section(change) {
|
|
358
|
+
if (change.reviewed) {
|
|
359
|
+
this.markSection(this.keyOf(change.path), change);
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
this.unmarkSection(this.keysOf(change), change);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
// Starting over: the documents forget every version reviewed under their paths, not only the
|
|
366
|
+
// current one, or an older mark would leave them "changed since review" rather than unreviewed.
|
|
367
|
+
reset(change) {
|
|
368
|
+
const { documents, sections } = this.state;
|
|
369
|
+
for (const entry of change.documents) {
|
|
370
|
+
const keys = this.keysOf(entry);
|
|
371
|
+
for (const key of keys) {
|
|
372
|
+
documents.delete(key);
|
|
373
|
+
sections.delete(key);
|
|
374
|
+
}
|
|
375
|
+
this.removeEverywhere(entry.hash, keys);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
keysOf(entry) {
|
|
379
|
+
return [entry.path, entry.oldPath]
|
|
380
|
+
.filter((relPath) => relPath !== undefined)
|
|
381
|
+
.map(this.keyOf);
|
|
382
|
+
}
|
|
383
|
+
markSection(documentKey, section) {
|
|
384
|
+
const { sections } = this.state;
|
|
385
|
+
let marks = sections.get(documentKey);
|
|
386
|
+
if (marks === undefined) {
|
|
387
|
+
marks = new Map();
|
|
388
|
+
sections.set(documentKey, marks);
|
|
389
|
+
}
|
|
390
|
+
const hashes = marks.get(section.key) ?? [];
|
|
391
|
+
marks.set(section.key, [section.hash, ...hashes.filter((hash) => hash !== section.hash)].slice(0, VERSIONS_PER_PATH));
|
|
392
|
+
}
|
|
393
|
+
unmarkSection(documentKeys, section) {
|
|
394
|
+
for (const documentKey of documentKeys) {
|
|
395
|
+
const marks = this.state.sections.get(documentKey);
|
|
396
|
+
const hashes = marks?.get(section.key);
|
|
397
|
+
if (marks !== undefined && hashes !== undefined) {
|
|
398
|
+
marks.set(section.key, hashes.filter((hash) => hash !== section.hash));
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
// Removing a text removes it under every path, since a text marked under one path counts under
|
|
403
|
+
// all of them; the empty text of a deletion is the exception, counted under its own path only.
|
|
404
|
+
removeEverywhere(hash, keys) {
|
|
405
|
+
const { documents } = this.state;
|
|
406
|
+
for (const [key, hashes] of documents) {
|
|
407
|
+
if (hash !== EMPTY_HASH || keys.includes(key)) {
|
|
408
|
+
documents.set(key, hashes.filter((entry) => entry !== hash));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
// A path or a section left with no reviewed text is dropped, so the index holds no empty entries.
|
|
414
|
+
function dropEmpty({ documents, sections }) {
|
|
415
|
+
for (const [key, hashes] of documents) {
|
|
416
|
+
if (hashes.length === 0) {
|
|
417
|
+
documents.delete(key);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
for (const [key, marks] of sections) {
|
|
421
|
+
for (const [section, hashes] of marks) {
|
|
422
|
+
if (hashes.length === 0) {
|
|
423
|
+
marks.delete(section);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (marks.size === 0) {
|
|
427
|
+
sections.delete(key);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
// Paths in code-point order and pretty-printed, so the same marks always make the same file and a
|
|
432
|
+
// person reading it finds a path where they expect it.
|
|
433
|
+
function serializeIndex(repository, documents, sections) {
|
|
434
|
+
const sorted = {};
|
|
435
|
+
for (const key of [...documents.keys()].sort(compareStrings)) {
|
|
436
|
+
sorted[key] = documents.get(key);
|
|
437
|
+
}
|
|
438
|
+
const sortedSections = {};
|
|
439
|
+
for (const key of [...sections.keys()].sort(compareStrings)) {
|
|
440
|
+
const marks = sections.get(key);
|
|
441
|
+
const entry = {};
|
|
442
|
+
for (const section of [...marks.keys()].sort(compareStrings)) {
|
|
443
|
+
entry[section] = marks.get(section);
|
|
444
|
+
}
|
|
445
|
+
sortedSections[key] = entry;
|
|
446
|
+
}
|
|
447
|
+
return `${JSON.stringify({ format: FORMAT, repository, documents: sorted, sections: sortedSections }, null, 2)}\n`;
|
|
448
|
+
}
|
|
449
|
+
const UNREADABLE = "The review marks file is not one this viewer can read, so it is left as it is. Fix it or delete it to mark documents again.";
|
|
450
|
+
const isHashList = (value) => Array.isArray(value) &&
|
|
451
|
+
value.every((hash) => typeof hash === "string" && HASH.test(hash));
|
|
452
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
453
|
+
function parseIndex(text) {
|
|
454
|
+
let json;
|
|
455
|
+
try {
|
|
456
|
+
json = JSON.parse(text);
|
|
457
|
+
}
|
|
458
|
+
catch {
|
|
459
|
+
return { ok: false, message: UNREADABLE };
|
|
460
|
+
}
|
|
461
|
+
if (!isRecord(json)) {
|
|
462
|
+
return { ok: false, message: UNREADABLE };
|
|
463
|
+
}
|
|
464
|
+
const { format, documents, sections } = json;
|
|
465
|
+
if (typeof format === "number" && format > FORMAT) {
|
|
466
|
+
return {
|
|
467
|
+
ok: false,
|
|
468
|
+
message: "The review marks were saved by a newer version of the viewer, so this one leaves them as they are. Update the viewer to mark documents again.",
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
if ((format !== 1 && format !== FORMAT) ||
|
|
472
|
+
!isRecord(documents) ||
|
|
473
|
+
(sections !== undefined && !isRecord(sections))) {
|
|
474
|
+
return { ok: false, message: UNREADABLE };
|
|
475
|
+
}
|
|
476
|
+
const parsed = documentMarksOf(documents);
|
|
477
|
+
const parsedSections = sectionMarksOf(sections ?? {});
|
|
478
|
+
if (parsed === undefined || parsedSections === undefined) {
|
|
479
|
+
return { ok: false, message: UNREADABLE };
|
|
480
|
+
}
|
|
481
|
+
return { ok: true, documents: parsed, sections: parsedSections };
|
|
482
|
+
}
|
|
483
|
+
// Each path's reviewed texts, or `undefined` when one entry is not a list of hashes. A path with no
|
|
484
|
+
// texts left is not kept.
|
|
485
|
+
function documentMarksOf(documents) {
|
|
486
|
+
const parsed = new Map();
|
|
487
|
+
for (const [key, hashes] of Object.entries(documents)) {
|
|
488
|
+
if (!isHashList(hashes)) {
|
|
489
|
+
return undefined;
|
|
490
|
+
}
|
|
491
|
+
if (hashes.length > 0) {
|
|
492
|
+
parsed.set(key, hashes);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return parsed;
|
|
496
|
+
}
|
|
497
|
+
// Each path's sections and their reviewed texts, or `undefined` when one entry is malformed. Empty
|
|
498
|
+
// sections, and paths left with none, are not kept.
|
|
499
|
+
function sectionMarksOf(sections) {
|
|
500
|
+
const parsed = new Map();
|
|
501
|
+
for (const [key, marks] of Object.entries(sections)) {
|
|
502
|
+
if (!isRecord(marks)) {
|
|
503
|
+
return undefined;
|
|
504
|
+
}
|
|
505
|
+
const entry = new Map();
|
|
506
|
+
for (const [section, hashes] of Object.entries(marks)) {
|
|
507
|
+
if (!isHashList(hashes)) {
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
if (hashes.length > 0) {
|
|
511
|
+
entry.set(section, hashes);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (entry.size > 0) {
|
|
515
|
+
parsed.set(key, entry);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return parsed;
|
|
519
|
+
}
|
|
520
|
+
function failed(error) {
|
|
521
|
+
return {
|
|
522
|
+
ok: false,
|
|
523
|
+
status: "failed",
|
|
524
|
+
message: `The review mark could not be saved (${errnoCode(error) ?? "write error"}); nothing was changed.`,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function errnoCode(error) {
|
|
528
|
+
const code = error?.code;
|
|
529
|
+
return typeof code === "string" ? code : undefined;
|
|
530
|
+
}
|