Repository navigation
Expand file tree
/
Copy pathdiscover-read.mjs
More file actions
158 lines (147 loc) · 6.42 KB
/
Copy pathdiscover-read.mjs
File metadata and controls
158 lines (147 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// The part of discover that opens files: a page's frontmatter split off and
// parsed into the page object every later phase consumes, a static file's
// size, and a picture pair's dimensions. Apart from discover.mjs so that a lane
// can run it after loading no more than this, the YAML parser and paths.mjs
// (the first stage of a lane's boot, cpu-worker.mjs): discover.mjs itself
// loads the glob library, which only the main thread needs.
//
// Everything here is synchronous. A lane has nothing else to do while it
// reads, and several lanes reading at once is what makes it parallel; the
// asynchronous calls would all queue on libuv's one pool of four threads.
import { closeSync, openSync, readFileSync, readSync, statSync } from "node:fs";
import path from "node:path";
import { parseFrontmatter, unquotedHashValues } from "../lib/frontmatter.mjs";
import { permalinkToDestPath } from "./paths.mjs";
export const PAGE_EXT = /\.(md|html)$/i;
const IMAGE_SCOPE = /(^|\/)Images\//;
export const toPosix = (p) => p.replace(/\\/g, "/");
/**
* One piece of the source tree (discover.mjs, cutPieces), read one file after
* the other. Every path is relative to `srcRoot`.
*
* @param {string} srcRoot
* @param {object} piece
* @param {string[]} piece.pageRels .md and .html files
* @param {string[]} piece.statSrcs static files to size
* @param {[string, string][]} piece.pairs a light picture's file and its dark picture's
* @returns {{ pages: object[], plain: { srcRel: string, size: number }[], warnings: string[],
* sizes: number[], pairSizes: (number[] | null)[] }}
* the pages among `pageRels`; the files among them with no frontmatter
* (`plain`, which are static files), each with its size; the warnings to
* print; the size of each of `statSrcs`; and for each of `pairs`
* [lightW, lightH, darkW, darkH], or null when either is no PNG
*/
export function readPiece(srcRoot, { pageRels, statSrcs, pairs }) {
const pages = [];
const plain = [];
const warnings = [];
for (const srcRel of pageRels) {
const file = path.join(srcRoot, srcRel);
const parsed = readFrontmatter(readFileSync(file, "utf8"), srcRel, warnings);
if (parsed) pages.push(buildPage(srcRoot, srcRel, parsed));
// .md/.html without frontmatter falls through to static treatment.
else plain.push({ srcRel: toPosix(srcRel), size: statSync(file).size });
}
const sizes = statSrcs.map((src) => statSync(path.resolve(srcRoot, src)).size);
const pairSizes = pairs.map(([light, dark]) => {
const l = pngSize(path.resolve(srcRoot, light));
const d = pngSize(path.resolve(srcRoot, dark));
return l && d ? [...l, ...d] : null;
});
return { pages, plain, warnings, sizes, pairSizes };
}
/**
* The pages' text (`rawContent`) as UTF-8 in one SharedArrayBuffer, for the
* lanes that render them (tbdocs.mjs: discover broadcasts it, and a render
* chunk's payload names where each page's text is instead of holding it). The
* lane that read the pages packs them, so the main thread never turns the
* site's text into JSON for the chunks.
*
* @param {{ rawContent: string }[]} pages
* @returns {{ sab: SharedArrayBuffer, spans: number[] }} `spans` holds each
* page's offset and length in bytes, two numbers a page, in the pages' order
*/
export function packPageText(pages) {
const spans = [];
let total = 0;
for (const page of pages) {
const length = Buffer.byteLength(page.rawContent, "utf8");
spans.push(total, length);
total += length;
}
const sab = new SharedArrayBuffer(total);
const bytes = Buffer.from(sab);
for (const [i, page] of pages.entries()) bytes.write(page.rawContent, spans[2 * i], spans[2 * i + 1], "utf8");
return { sab, spans };
}
/** One page's text, from what packPageText packed. */
export function pageText(sab, offset, length) {
return Buffer.from(sab, offset, length).toString("utf8");
}
// A PNG file's width and height in pixels, from its header; null when it is no PNG.
function pngSize(file) {
const fd = openSync(file, "r");
try {
const b = Buffer.alloc(24);
const bytesRead = readSync(fd, b, 0, 24, 0);
if (bytesRead < 24 || b.toString("latin1", 12, 16) !== "IHDR") return null;
return [b.readUInt32BE(16), b.readUInt32BE(20)];
} finally {
closeSync(fd);
}
}
// Every .md and .html passes through here, and `parseFrontmatter` drops a
// leading BOM, so a page an editor saved with one is still a page rather than
// a static asset served as raw markdown.
//
// `contentLine` is the file's 1-based line on which `content` starts, so a
// message about a line of the content can name the line of the file. The
// content is the text after the frontmatter block, so the lines before it are
// the lines of the block.
//
// A value left unquoted that ends in `#` is warned about, not refused: YAML
// drops a `#` after a space as a comment, so `title: Input #` titles the page
// `Input`, while `lang: C#` is read as written. Quoting the value keeps the `#`
// and silences the warning either way. The warning goes into `warnings`, for
// the main thread to print.
function readFrontmatter(raw, srcRel, warnings) {
let parsed;
try {
parsed = parseFrontmatter(raw);
} catch (err) {
throw new Error(`Failed to parse frontmatter in ${srcRel}: ${err.message}`);
}
if (!parsed) return null;
for (const { line, text } of unquotedHashValues(raw)) {
warnings.push(
`discover: ${srcRel}:${line}: an unquoted value ends in #, and YAML drops a # after a space as a comment; quote the value to keep it: ${text.trim()}`,
);
}
const { data, content } = parsed;
const block = raw.slice(0, raw.length - content.length);
return { data, content, contentLine: (block.match(/\r\n?|\n/g) ?? []).length + 1 };
}
function buildPage(srcRoot, srcRel, { data, content, contentLine }) {
const srcRelPosix = toPosix(srcRel);
const ext = path.extname(srcRel).toLowerCase();
const permalink = computePermalink(data.permalink, srcRelPosix);
const destPath = permalinkToDestPath(permalink);
return {
srcPath: path.join(srcRoot, srcRel),
srcRel: srcRelPosix,
ext,
frontmatter: data,
rawContent: content,
contentLine,
permalink,
destPath,
layoutDefault: data.layout === undefined || data.layout === null,
imageScope: IMAGE_SCOPE.test(srcRelPosix),
};
}
function computePermalink(fmPermalink, srcRelPosix) {
if (typeof fmPermalink === "string" && fmPermalink.length > 0) {
return fmPermalink;
}
return "/" + srcRelPosix.replace(PAGE_EXT, "") + ".html";
}