@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
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { ViewerContext } from "./viewer-context.js";
|
|
3
|
+
/** The address the server is bound to, which the `Host` check compares a request's header with. */
|
|
4
|
+
export type BoundAddress = {
|
|
5
|
+
host: string;
|
|
6
|
+
port: number;
|
|
7
|
+
};
|
|
8
|
+
export declare class ViewerRouter {
|
|
9
|
+
private readonly ctx;
|
|
10
|
+
private readonly bound;
|
|
11
|
+
private readonly documents;
|
|
12
|
+
private readonly review;
|
|
13
|
+
private readonly config;
|
|
14
|
+
private readonly pages;
|
|
15
|
+
private readonly exact;
|
|
16
|
+
private readonly prefixes;
|
|
17
|
+
constructor(ctx: ViewerContext, bound: BoundAddress);
|
|
18
|
+
/** Stops what a route started that outlives a request: the config preview's lint thread. */
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
private exactRoutes;
|
|
21
|
+
private pageRoutes;
|
|
22
|
+
private dataRoutes;
|
|
23
|
+
private prefixRoutes;
|
|
24
|
+
handle(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
25
|
+
private answer;
|
|
26
|
+
private handleChange;
|
|
27
|
+
private readChangeBody;
|
|
28
|
+
}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { sendEcharts, sendMermaid, sendPackageAsset } from "./asset-routes.js";
|
|
2
|
+
import { diffViewOf } from "./change-banner.js";
|
|
3
|
+
import { ChangesRoutes } from "./changes-routes.js";
|
|
4
|
+
import { ConfigRoutes } from "./config-routes.js";
|
|
5
|
+
import { CorpusRoutes } from "./corpus-routes.js";
|
|
6
|
+
import { DocumentRoutes } from "./document-routes.js";
|
|
7
|
+
import { readBody, sendJson, sendText } from "./http-send.js";
|
|
8
|
+
import { decodeRequestPath } from "./request-path.js";
|
|
9
|
+
import { sendSpeculationRules, SPECULATION_RULES_PATH, } from "./speculation-rules.js";
|
|
10
|
+
import { baselineOf } from "./review-state.js";
|
|
11
|
+
import { CHANGES_OFF_REMOTELY, changeRequestProblem, isAllowedHostHeader, isForeignRequest, } from "./security.js";
|
|
12
|
+
// The one place every request passes through: the checks that apply to all of them, then the route
|
|
13
|
+
// that answers. What each route does lives with its group — a document's page, the Changes page and
|
|
14
|
+
// review marks, the config page, the corpus-wide pages, the shipped assets — over one shared
|
|
15
|
+
// `ViewerContext`, so every route reads the same corpus and review.
|
|
16
|
+
// A config change or a review mark is at most a few kilobytes of JSON; anything far beyond that is
|
|
17
|
+
// not one, and reading it into memory would cost the server for nothing.
|
|
18
|
+
const MAX_CHANGE_BODY_BYTES = 256 * 1024;
|
|
19
|
+
// The only requests with a body the server acts on. Each computes from the config, writes it, opens
|
|
20
|
+
// it in another program or records a review mark, so each passes the checks in
|
|
21
|
+
// `changeRequestProblem` before its body is read.
|
|
22
|
+
const CHANGE_ROUTES = new Set([
|
|
23
|
+
"/api/config/preview",
|
|
24
|
+
"/api/config/save",
|
|
25
|
+
"/api/config/open",
|
|
26
|
+
"/api/review/mark",
|
|
27
|
+
"/api/review/section",
|
|
28
|
+
"/api/review/reset",
|
|
29
|
+
]);
|
|
30
|
+
export class ViewerRouter {
|
|
31
|
+
ctx;
|
|
32
|
+
bound;
|
|
33
|
+
documents;
|
|
34
|
+
review;
|
|
35
|
+
config;
|
|
36
|
+
pages;
|
|
37
|
+
// Addresses answered by exactly one route. A lookup rather than a chain of comparisons: no exact
|
|
38
|
+
// address starts with one of the prefixes below, so which is asked first cannot change the answer.
|
|
39
|
+
exact;
|
|
40
|
+
// Asked in this order, after the exact addresses.
|
|
41
|
+
prefixes;
|
|
42
|
+
constructor(ctx, bound) {
|
|
43
|
+
this.ctx = ctx;
|
|
44
|
+
this.bound = bound;
|
|
45
|
+
this.documents = new DocumentRoutes(ctx);
|
|
46
|
+
this.review = new ChangesRoutes(ctx);
|
|
47
|
+
this.config = new ConfigRoutes(ctx);
|
|
48
|
+
this.pages = new CorpusRoutes(ctx);
|
|
49
|
+
this.exact = this.exactRoutes();
|
|
50
|
+
this.prefixes = this.prefixRoutes();
|
|
51
|
+
}
|
|
52
|
+
/** Stops what a route started that outlives a request: the config preview's lint thread. */
|
|
53
|
+
close() {
|
|
54
|
+
return this.config.close();
|
|
55
|
+
}
|
|
56
|
+
// Every exact address, the pages and the data they ask for, as one lookup.
|
|
57
|
+
exactRoutes() {
|
|
58
|
+
return new Map([...this.pageRoutes(), ...this.dataRoutes()]);
|
|
59
|
+
}
|
|
60
|
+
// The pages a reader opens, the live-reload stream and the shipped files with fixed addresses.
|
|
61
|
+
pageRoutes() {
|
|
62
|
+
const { documents, review, config, pages, ctx } = this;
|
|
63
|
+
return [
|
|
64
|
+
["/", (_request, response) => documents.redirectToIndex(response)],
|
|
65
|
+
[
|
|
66
|
+
"/search",
|
|
67
|
+
(request, response, params) => pages.sendSearchPage(request, response, params.get("q") ?? ""),
|
|
68
|
+
],
|
|
69
|
+
[
|
|
70
|
+
"/graph",
|
|
71
|
+
(request, response, params) => pages.sendGraphPage(request, response, params),
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
"/changes",
|
|
75
|
+
(request, response, params) => review.sendChangesPage(request, response, params),
|
|
76
|
+
],
|
|
77
|
+
[
|
|
78
|
+
"/dashboard",
|
|
79
|
+
(request, response, params) => pages.sendDashboardPage(request, response, params),
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
"/config",
|
|
83
|
+
(request, response) => config.sendConfigPage(request, response),
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
"/events",
|
|
87
|
+
(request, response) => {
|
|
88
|
+
ctx.events.open(request, response);
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
[
|
|
92
|
+
SPECULATION_RULES_PATH,
|
|
93
|
+
(request, response) => {
|
|
94
|
+
sendSpeculationRules(request, response);
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
"/favicon.ico",
|
|
99
|
+
(request, response) => sendPackageAsset(request, response, "favicon.ico"),
|
|
100
|
+
],
|
|
101
|
+
[
|
|
102
|
+
"/vendor/echarts/echarts.esm.min.mjs",
|
|
103
|
+
(request, response) => sendEcharts(request, response),
|
|
104
|
+
],
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
// The JSON the pages ask for.
|
|
108
|
+
dataRoutes() {
|
|
109
|
+
const { documents, review, config, pages } = this;
|
|
110
|
+
return [
|
|
111
|
+
[
|
|
112
|
+
"/api/search",
|
|
113
|
+
(_request, response, params) => {
|
|
114
|
+
pages.sendSearchData(response, params.get("q") ?? "");
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
[
|
|
118
|
+
"/api/graph",
|
|
119
|
+
(_request, response, params) => pages.sendGraphData(response, params),
|
|
120
|
+
],
|
|
121
|
+
[
|
|
122
|
+
"/api/neighbors",
|
|
123
|
+
(_request, response, params) => pages.sendNeighbors(response, params),
|
|
124
|
+
],
|
|
125
|
+
[
|
|
126
|
+
"/api/changes",
|
|
127
|
+
(_request, response, params) => review.sendChangesData(response, params),
|
|
128
|
+
],
|
|
129
|
+
[
|
|
130
|
+
"/api/history",
|
|
131
|
+
(_request, response, params) => documents.sendHistory(response, params),
|
|
132
|
+
],
|
|
133
|
+
[
|
|
134
|
+
"/api/changes/diff",
|
|
135
|
+
(_request, response, params) => review.sendChangeDiff(response, params),
|
|
136
|
+
],
|
|
137
|
+
[
|
|
138
|
+
"/api/dashboard",
|
|
139
|
+
(_request, response, params) => pages.sendDashboardData(response, params),
|
|
140
|
+
],
|
|
141
|
+
["/api/config", (_request, response) => config.sendConfigData(response)],
|
|
142
|
+
[
|
|
143
|
+
"/api/diagnostics",
|
|
144
|
+
(_request, response, params) => {
|
|
145
|
+
pages.sendDiagnostics(response, params);
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
[
|
|
149
|
+
"/api/diagnostics/panel",
|
|
150
|
+
(_request, response, params) => {
|
|
151
|
+
pages.sendDiagnosticsPanel(response, params);
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
[
|
|
155
|
+
"/api/prompts",
|
|
156
|
+
(_request, response, params) => pages.sendPrompts(response, params),
|
|
157
|
+
],
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
prefixRoutes() {
|
|
161
|
+
const { documents, pages } = this;
|
|
162
|
+
return [
|
|
163
|
+
[
|
|
164
|
+
"/assets/",
|
|
165
|
+
(request, response, rest) => sendPackageAsset(request, response, rest),
|
|
166
|
+
],
|
|
167
|
+
[
|
|
168
|
+
"/vendor/mermaid/",
|
|
169
|
+
(request, response, rest) => sendMermaid(request, response, rest),
|
|
170
|
+
],
|
|
171
|
+
[
|
|
172
|
+
"/doc/",
|
|
173
|
+
(request, response, rest, params) => documents.sendDocument(request, response, {
|
|
174
|
+
relPath: decodeRequestPath(rest),
|
|
175
|
+
view: diffViewOf(params),
|
|
176
|
+
baseline: baselineOf(params),
|
|
177
|
+
blame: params.get("blame") === "1",
|
|
178
|
+
}),
|
|
179
|
+
],
|
|
180
|
+
[
|
|
181
|
+
"/folder/",
|
|
182
|
+
(request, response, rest) => pages.sendFolderPage(request, response, folderOf(rest)),
|
|
183
|
+
],
|
|
184
|
+
[
|
|
185
|
+
"/file/",
|
|
186
|
+
(request, response, rest) => documents.sendLinkedFile(request, response, decodeRequestPath(rest)),
|
|
187
|
+
],
|
|
188
|
+
];
|
|
189
|
+
}
|
|
190
|
+
async handle(request, response) {
|
|
191
|
+
if (!isAllowedHostHeader(request.headers.host, this.bound)) {
|
|
192
|
+
sendText(response, 403, "Forbidden: this server answers only to localhost.");
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (isForeignRequest(request.headers)) {
|
|
196
|
+
sendText(response, 403, "Forbidden: cross-site request.");
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const [target = "", queryString = ""] = (request.url ?? "")
|
|
200
|
+
.split("#", 1)[0]
|
|
201
|
+
.split("?", 2);
|
|
202
|
+
if (CHANGE_ROUTES.has(target)) {
|
|
203
|
+
if (request.method !== "POST") {
|
|
204
|
+
response.setHeader("Allow", "POST");
|
|
205
|
+
sendText(response, 405, "Method not allowed: send the change as a POST.");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
await this.handleChange(request, response, target);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
212
|
+
response.setHeader("Allow", "GET, HEAD");
|
|
213
|
+
sendText(response, 405, "Method not allowed: only the config page's and the review marks' requests take a body.");
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
await this.answer(request, response, target, new URLSearchParams(queryString));
|
|
217
|
+
}
|
|
218
|
+
// The page or data a read request's address names, or the not-found page when it names nothing.
|
|
219
|
+
async answer(request, response, target, params) {
|
|
220
|
+
const route = this.exact.get(target);
|
|
221
|
+
if (route !== undefined) {
|
|
222
|
+
await route(request, response, params);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
for (const [prefix, answer] of this.prefixes) {
|
|
226
|
+
if (target.startsWith(prefix)) {
|
|
227
|
+
await answer(request, response, target.slice(prefix.length), params);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
await this.ctx.sendNotFound(request, response, "There is no page at this address.");
|
|
232
|
+
}
|
|
233
|
+
// A request with a body: refused before its body is read unless it came from a page this server
|
|
234
|
+
// rendered, then parsed once here, so every route that takes a body is guarded the same way.
|
|
235
|
+
async handleChange(request, response, target) {
|
|
236
|
+
const json = await this.readChangeBody(request, response);
|
|
237
|
+
if (json === undefined) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (target === "/api/config/open") {
|
|
241
|
+
await this.config.openConfig(response, json.value);
|
|
242
|
+
}
|
|
243
|
+
else if (target === "/api/review/section") {
|
|
244
|
+
await this.review.changeSectionMark(response, json.value);
|
|
245
|
+
}
|
|
246
|
+
else if (target === "/api/review/mark" ||
|
|
247
|
+
target === "/api/review/reset") {
|
|
248
|
+
await this.review.changeMarks(response, target, json.value);
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
await this.config.changeConfig(response, target, json.value);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// The parsed body of a change request, or `undefined` after answering why it is refused. Wrapped,
|
|
255
|
+
// so a body that is valid JSON for `null` is still told apart from a refused one.
|
|
256
|
+
async readChangeBody(request, response) {
|
|
257
|
+
const token = this.ctx.changeToken;
|
|
258
|
+
if (token === undefined) {
|
|
259
|
+
request.resume();
|
|
260
|
+
sendJson(response, { status: "refused", error: CHANGES_OFF_REMOTELY }, 403);
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
const problem = changeRequestProblem(request.headers, token);
|
|
264
|
+
if (problem !== undefined) {
|
|
265
|
+
request.resume();
|
|
266
|
+
sendJson(response, { status: "refused", error: problem.message }, problem.status);
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
const body = await readBody(request, MAX_CHANGE_BODY_BYTES);
|
|
270
|
+
if (body === undefined) {
|
|
271
|
+
sendJson(response, {
|
|
272
|
+
status: "refused",
|
|
273
|
+
error: "The request body is too large to be a config change or a review mark.",
|
|
274
|
+
}, 413);
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
return { value: JSON.parse(body) };
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
sendJson(response, { status: "refused", error: "The request body is not valid JSON." }, 400);
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// The folder a `/folder/` address names. Its links end in `/`, as a folder's address does on GitHub,
|
|
287
|
+
// and the bare prefix is the root; `decodeRequestPath` refuses an empty segment, so that one slash is
|
|
288
|
+
// taken off before it judges the rest.
|
|
289
|
+
function folderOf(rest) {
|
|
290
|
+
if (rest === "") {
|
|
291
|
+
return "";
|
|
292
|
+
}
|
|
293
|
+
return decodeRequestPath(rest.endsWith("/") ? rest.slice(0, -1) : rest);
|
|
294
|
+
}
|
package/dist/search.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ParsedDocument } from "@wastech-mdlint/core";
|
|
2
|
+
export type SearchHit = {
|
|
3
|
+
path: string;
|
|
4
|
+
title: string;
|
|
5
|
+
href: string;
|
|
6
|
+
snippet: string;
|
|
7
|
+
terms: string[];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Full-text search over the corpus.
|
|
11
|
+
*
|
|
12
|
+
* The index is built on the first query rather than at startup, so a reader who never searches pays
|
|
13
|
+
* nothing, and it is kept in step with the corpus by object identity: the corpus replaces a
|
|
14
|
+
* document's object when its file changes, so each query re-indexes exactly the documents that
|
|
15
|
+
* changed since the last one.
|
|
16
|
+
*/
|
|
17
|
+
export declare class DocumentSearch {
|
|
18
|
+
private readonly index;
|
|
19
|
+
private readonly indexed;
|
|
20
|
+
search(documents: ReadonlyMap<string, ParsedDocument>, query: string): SearchHit[];
|
|
21
|
+
private sync;
|
|
22
|
+
}
|
package/dist/search.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import MiniSearch from "minisearch";
|
|
2
|
+
import { documentHref, documentTitle, headingAbove, } from "./render-document.js";
|
|
3
|
+
// Queries are capped because fuzzy matching cost grows with every term; nobody types a paragraph
|
|
4
|
+
// into a search box, and a request that does should not stall the server.
|
|
5
|
+
const MAX_QUERY_LENGTH = 200;
|
|
6
|
+
const RESULT_LIMIT = 20;
|
|
7
|
+
const SNIPPET_BEFORE = 60;
|
|
8
|
+
const SNIPPET_AFTER = 140;
|
|
9
|
+
const SEARCH_OPTIONS = {
|
|
10
|
+
prefix: true,
|
|
11
|
+
// Typos are forgiven only in longer words; on short ones a fuzzy match is mostly noise.
|
|
12
|
+
fuzzy: (term) => (term.length > 4 ? 0.2 : false),
|
|
13
|
+
boost: { title: 4, headings: 2, path: 2 },
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Full-text search over the corpus.
|
|
17
|
+
*
|
|
18
|
+
* The index is built on the first query rather than at startup, so a reader who never searches pays
|
|
19
|
+
* nothing, and it is kept in step with the corpus by object identity: the corpus replaces a
|
|
20
|
+
* document's object when its file changes, so each query re-indexes exactly the documents that
|
|
21
|
+
* changed since the last one.
|
|
22
|
+
*/
|
|
23
|
+
export class DocumentSearch {
|
|
24
|
+
index = new MiniSearch({
|
|
25
|
+
idField: "path",
|
|
26
|
+
fields: ["title", "headings", "path", "text"],
|
|
27
|
+
storeFields: [],
|
|
28
|
+
});
|
|
29
|
+
indexed = new Map();
|
|
30
|
+
search(documents, query) {
|
|
31
|
+
const text = query.trim().slice(0, MAX_QUERY_LENGTH);
|
|
32
|
+
if (text.length === 0) {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
this.sync(documents);
|
|
36
|
+
// Every word must match when that finds anything; otherwise any word may, so a query with one
|
|
37
|
+
// word the corpus lacks still finds the documents that have the rest.
|
|
38
|
+
let results = this.index.search(text, {
|
|
39
|
+
...SEARCH_OPTIONS,
|
|
40
|
+
combineWith: "AND",
|
|
41
|
+
});
|
|
42
|
+
if (results.length === 0) {
|
|
43
|
+
results = this.index.search(text, {
|
|
44
|
+
...SEARCH_OPTIONS,
|
|
45
|
+
combineWith: "OR",
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return results.slice(0, RESULT_LIMIT).flatMap((result) => {
|
|
49
|
+
const document = documents.get(String(result.id));
|
|
50
|
+
return document === undefined ? [] : [toHit(document, result.terms)];
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
sync(documents) {
|
|
54
|
+
for (const relPath of [...this.indexed.keys()]) {
|
|
55
|
+
if (!documents.has(relPath)) {
|
|
56
|
+
this.index.discard(relPath);
|
|
57
|
+
this.indexed.delete(relPath);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const [relPath, document] of documents) {
|
|
61
|
+
const previous = this.indexed.get(relPath);
|
|
62
|
+
if (previous === document) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (previous === undefined) {
|
|
66
|
+
this.index.add(toIndexed(document));
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.index.replace(toIndexed(document));
|
|
70
|
+
}
|
|
71
|
+
this.indexed.set(relPath, document);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function toIndexed(document) {
|
|
76
|
+
return {
|
|
77
|
+
path: document.path,
|
|
78
|
+
title: documentTitle(document),
|
|
79
|
+
headings: document.headings.map((heading) => heading.text).join("\n"),
|
|
80
|
+
text: document.content.slice(bodyOffset(document)),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function toHit(document, terms) {
|
|
84
|
+
const content = document.content;
|
|
85
|
+
const bodyStart = bodyOffset(document);
|
|
86
|
+
// The title is already the hit's headline. A match inside it shows the reader nothing new, and its
|
|
87
|
+
// anchor is just the top of the page, so the snippet and the landing place come from the text
|
|
88
|
+
// under the title whenever that has a match too.
|
|
89
|
+
const title = document.headings.find((heading) => heading.depth === 1);
|
|
90
|
+
const belowTitle = title === undefined
|
|
91
|
+
? bodyStart
|
|
92
|
+
: Math.max(bodyStart, lineOffset(content, title.line + 1));
|
|
93
|
+
const below = firstMatch(content, terms, belowTitle);
|
|
94
|
+
const at = below ?? firstMatch(content, terms, bodyStart);
|
|
95
|
+
const floor = below === undefined ? bodyStart : belowTitle;
|
|
96
|
+
// A heading that itself contains a matched word is the best place to land; failing that, the
|
|
97
|
+
// section the first match sits in.
|
|
98
|
+
const landing = document.headings.find((heading) => heading !== title &&
|
|
99
|
+
terms.some((term) => heading.text.toLowerCase().includes(term))) ??
|
|
100
|
+
(at === undefined
|
|
101
|
+
? undefined
|
|
102
|
+
: headingAbove(document, lineAt(content, at)));
|
|
103
|
+
const anchor = landing === title ? undefined : landing?.slug;
|
|
104
|
+
const from = at === undefined ? floor : Math.max(floor, at - SNIPPET_BEFORE);
|
|
105
|
+
const to = at === undefined
|
|
106
|
+
? floor + SNIPPET_BEFORE + SNIPPET_AFTER
|
|
107
|
+
: at + SNIPPET_AFTER;
|
|
108
|
+
const snippet = plainText(content.slice(from, to));
|
|
109
|
+
return {
|
|
110
|
+
path: document.path,
|
|
111
|
+
title: documentTitle(document),
|
|
112
|
+
href: `${documentHref(document.path)}${anchor === undefined ? "" : `#${anchor}`}`,
|
|
113
|
+
snippet: `${from > floor ? "…" : ""}${snippet}${to < content.length ? "…" : ""}`,
|
|
114
|
+
terms: [...terms],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
// Offset of the earliest occurrence of any term at or after `from`, case-insensitively.
|
|
118
|
+
function firstMatch(content, terms, from) {
|
|
119
|
+
const lower = content.toLowerCase();
|
|
120
|
+
let at;
|
|
121
|
+
for (const term of terms) {
|
|
122
|
+
const index = lower.indexOf(term, from);
|
|
123
|
+
if (index !== -1 && (at === undefined || index < at)) {
|
|
124
|
+
at = index;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return at;
|
|
128
|
+
}
|
|
129
|
+
// Offset of the start of a 1-based line; the end of the text when the line is past it.
|
|
130
|
+
function lineOffset(content, line) {
|
|
131
|
+
let offset = 0;
|
|
132
|
+
for (let current = 1; current < line; current += 1) {
|
|
133
|
+
const next = content.indexOf("\n", offset);
|
|
134
|
+
if (next === -1) {
|
|
135
|
+
return content.length;
|
|
136
|
+
}
|
|
137
|
+
offset = next + 1;
|
|
138
|
+
}
|
|
139
|
+
return offset;
|
|
140
|
+
}
|
|
141
|
+
function lineAt(content, offset) {
|
|
142
|
+
let line = 1;
|
|
143
|
+
for (let index = content.indexOf("\n"); index !== -1 && index < offset;) {
|
|
144
|
+
line += 1;
|
|
145
|
+
index = content.indexOf("\n", index + 1);
|
|
146
|
+
}
|
|
147
|
+
return line;
|
|
148
|
+
}
|
|
149
|
+
// Front matter is metadata, shown as its own card; matching inside it would send readers to the top
|
|
150
|
+
// of a page for a word that is not in its text.
|
|
151
|
+
function bodyOffset(document) {
|
|
152
|
+
return document.frontmatter === null
|
|
153
|
+
? 0
|
|
154
|
+
: lineOffset(document.content, document.frontmatter.endLine + 1);
|
|
155
|
+
}
|
|
156
|
+
// A snippet is read as prose, so the Markdown punctuation around a match goes: link targets,
|
|
157
|
+
// emphasis and heading markers, table pipes and backticks.
|
|
158
|
+
function plainText(markdown) {
|
|
159
|
+
return markdown
|
|
160
|
+
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
161
|
+
.replace(/[#*_`>|~]+/g, " ")
|
|
162
|
+
.replace(/\s+/g, " ")
|
|
163
|
+
.trim();
|
|
164
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export declare const DEFAULT_HOST = "127.0.0.1";
|
|
2
|
+
export declare function isLoopbackHost(host: string): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Whether pages may carry a file's absolute path, for the button that copies it.
|
|
5
|
+
*
|
|
6
|
+
* Absolute paths stay out of what the viewer shows by default: they say where the checkout lives and
|
|
7
|
+
* under which user account. A reader of a viewer bound to a loopback address is on this machine and
|
|
8
|
+
* already knows; one bound with `--host` to another address is read from other machines, which do
|
|
9
|
+
* not, so there the path is withheld and the copy menu says why.
|
|
10
|
+
*/
|
|
11
|
+
export declare function exposesAbsolutePaths(host: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether a request's `Host` header names this server.
|
|
14
|
+
*
|
|
15
|
+
* DNS rebinding works by pointing an attacker-controlled name at 127.0.0.1 after the page has loaded,
|
|
16
|
+
* so the browser treats our responses as same-origin with the attacker's page. The request still
|
|
17
|
+
* carries the attacker's name in `Host`, which is what this rejects. IP literals are accepted for any
|
|
18
|
+
* address because a rebinding attack needs a *name* to re-point, and a server bound with `--host` to
|
|
19
|
+
* a LAN address is reached by its IP; `localhost` and the configured host are the only names.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isAllowedHostHeader(hostHeader: string | undefined, bound: {
|
|
22
|
+
host: string;
|
|
23
|
+
port: number;
|
|
24
|
+
}): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Whether a request was made by another site's page rather than by navigating to us.
|
|
27
|
+
*
|
|
28
|
+
* Navigations are allowed from anywhere: following a link to a document is the point of the tool,
|
|
29
|
+
* and the response goes to the user, not to the linking page. Everything else from another origin —
|
|
30
|
+
* a `fetch`, an `EventSource`, an `<img>` or `<script>` tag — is refused. `same-site` counts as
|
|
31
|
+
* foreign too, because every other port on localhost is the same site, and another dev server the
|
|
32
|
+
* user runs is exactly the kind of page that should not reach this one.
|
|
33
|
+
*/
|
|
34
|
+
export declare function isForeignRequest(headers: {
|
|
35
|
+
origin?: string;
|
|
36
|
+
host?: string;
|
|
37
|
+
"sec-fetch-site"?: string;
|
|
38
|
+
"sec-fetch-mode"?: string;
|
|
39
|
+
}): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Headers for a rendered page.
|
|
42
|
+
*
|
|
43
|
+
* Scripts only from this origin, never inline: the rendered Markdown is sanitized, and this is the
|
|
44
|
+
* second line if sanitizing ever misses something. Styles do allow `'unsafe-inline'`, because both
|
|
45
|
+
* Shiki (a `style` attribute per token) and Mermaid (a `<style>` inside each SVG) emit them, and the
|
|
46
|
+
* sanitizer already strips every `style` an author wrote. `no-store` because the page reflects the
|
|
47
|
+
* file on disk right now, and a cached copy after an edit is the wrong answer.
|
|
48
|
+
*/
|
|
49
|
+
export declare function pageHeaders(): Record<string, string>;
|
|
50
|
+
/**
|
|
51
|
+
* Headers for a repository file served as-is (an image a document embeds).
|
|
52
|
+
*
|
|
53
|
+
* `sandbox` matters for SVG: opened directly, an SVG is a document that can run script in our
|
|
54
|
+
* origin, and an SVG in a repository is just as untrusted as the Markdown around it.
|
|
55
|
+
*/
|
|
56
|
+
export declare function rawFileHeaders(): Record<string, string>;
|
|
57
|
+
/** Headers for the viewer's own scripts and styles, which change only with the package. */
|
|
58
|
+
export declare function assetHeaders(): Record<string, string>;
|
|
59
|
+
/**
|
|
60
|
+
* The per-process change token for a viewer bound to `host`, or `undefined` where the viewer takes no
|
|
61
|
+
* request with a body at all.
|
|
62
|
+
*
|
|
63
|
+
* Every such request makes the server act for whoever sent it: a preview lints the repository under a
|
|
64
|
+
* config the request supplies — a scope widened past `.gitignore`, a custom rule whose finding quotes
|
|
65
|
+
* the text it matched, which together read any file in the checkout — a save rewrites the config, an
|
|
66
|
+
* open starts a program, a mark records a review in this user's name. On a loopback binding the
|
|
67
|
+
* sender is on this machine. With `--host` it is anyone who reaches the port, and a token carried in a
|
|
68
|
+
* page every one of them can load proves nothing about who they are. So off loopback there is no
|
|
69
|
+
* token for a page to carry, and the router refuses those requests before reading their bodies.
|
|
70
|
+
*/
|
|
71
|
+
export declare function changeTokenFor(host: string): string | undefined;
|
|
72
|
+
/** Why a request with a body is refused on a binding other machines reach (`changeTokenFor`). */
|
|
73
|
+
export declare const CHANGES_OFF_REMOTELY = "Forbidden: the viewer is reachable from other machines (--host), so it previews, saves, opens and marks nothing. Start it on a loopback address to make changes.";
|
|
74
|
+
export declare const TOKEN_HEADER = "x-wastech-viewer-token";
|
|
75
|
+
/**
|
|
76
|
+
* Why a request that changes or computes from the config is refused, or `undefined` to let it in.
|
|
77
|
+
*
|
|
78
|
+
* Checked on top of the `Host` and cross-site checks every request passes, because these are the
|
|
79
|
+
* only requests with a body the server acts on. A browser always sends `Origin` on a POST, so a
|
|
80
|
+
* missing one is a request no page of ours made. Fetch metadata, when present, must say
|
|
81
|
+
* same-origin. The body must be declared JSON: an HTML form cannot send that type, and a page that
|
|
82
|
+
* sets it on a cross-origin `fetch` triggers a preflight. Last, the token: it is random per process
|
|
83
|
+
* and appears only in the config page, so a request that carries it was made by a page this server
|
|
84
|
+
* rendered.
|
|
85
|
+
*/
|
|
86
|
+
export declare function changeRequestProblem(headers: {
|
|
87
|
+
origin?: string;
|
|
88
|
+
host?: string;
|
|
89
|
+
"sec-fetch-site"?: string;
|
|
90
|
+
"content-type"?: string;
|
|
91
|
+
[TOKEN_HEADER]?: string;
|
|
92
|
+
}, token: string): {
|
|
93
|
+
status: 403 | 415;
|
|
94
|
+
message: string;
|
|
95
|
+
} | undefined;
|