@writedocs/generator 0.10.3 → 0.10.5
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/package.json +1 -1
- package/src/cli/build-auth.js +95 -38
- package/src/lib/config.ts +14 -7
- package/src/lib/item-pages.js +18 -0
package/package.json
CHANGED
package/src/cli/build-auth.js
CHANGED
|
@@ -15,64 +15,121 @@
|
|
|
15
15
|
* See docs/dev/docs/deploy.mdx for how to deploy writedocs-key-server and
|
|
16
16
|
* issue keys, and that repo's own README for the service's endpoints.
|
|
17
17
|
*/
|
|
18
|
+
import https from 'node:https';
|
|
18
19
|
import { log } from './output.js';
|
|
19
20
|
|
|
20
21
|
export const EX_TEMPFAIL = 75;
|
|
21
22
|
|
|
22
|
-
/** The authorization
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
23
|
+
/** The authorization servers every `build` asks, in this order. Two only
|
|
24
|
+
* while key validation moves to the WriteDocs platform (the first entry);
|
|
25
|
+
* the second, writedocs-key-server, goes once every key comes from the
|
|
26
|
+
* platform.
|
|
27
|
+
*
|
|
28
|
+
* Fixed in the code, with no environment override: when the URL came from
|
|
29
|
+
* WRITEDOCS_KEY_SERVER_URL, anyone could point it at a 3-line server that
|
|
30
|
+
* answers `{ "valid": true }` and build without a key. Tests and CI use a
|
|
31
|
+
* real key instead (the WRITEDOCS_API_KEY secret - see
|
|
27
32
|
* scripts/smoke-installed-cli.sh). Honest limit: the source ships with the
|
|
28
33
|
* package, so someone willing to edit it can still remove this check - it
|
|
29
34
|
* raises the bar, it isn't a lock. Real protection is what the platform
|
|
30
35
|
* does with the output. */
|
|
31
|
-
export const
|
|
36
|
+
export const KEY_SERVER_URLS = [
|
|
37
|
+
'https://api.writedocs.io',
|
|
38
|
+
'https://proxy.writechoice.io:8787',
|
|
39
|
+
];
|
|
32
40
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
/** The default transport: POSTs `body` (a JSON string) to `url` and resolves
|
|
42
|
+
* with `{ status, text }`, or rejects on a network error or once 10s have
|
|
43
|
+
* passed. node:https rather than fetch: on Windows (seen on Node 24.11),
|
|
44
|
+
* two fetch calls followed by process.exit abort Node with a libuv
|
|
45
|
+
* assertion (src\win\async.c) and exit 127 instead of 1 or 75. `agent:
|
|
46
|
+
* false` keeps no connection alive past the answer. */
|
|
47
|
+
export function httpsPostJson(url, body) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const req = https.request(
|
|
50
|
+
url,
|
|
51
|
+
{
|
|
52
|
+
method: 'POST',
|
|
53
|
+
agent: false,
|
|
54
|
+
headers: { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) },
|
|
55
|
+
signal: AbortSignal.timeout(10_000),
|
|
56
|
+
},
|
|
57
|
+
(res) => {
|
|
58
|
+
let text = '';
|
|
59
|
+
res.setEncoding('utf8');
|
|
60
|
+
res.on('data', (chunk) => (text += chunk));
|
|
61
|
+
res.on('end', () => resolve({ status: res.statusCode, text }));
|
|
62
|
+
res.on('error', reject);
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
req.on('error', reject);
|
|
66
|
+
req.end(body);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
39
69
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
70
|
+
/** Asks one server about the key. Never throws; the answer is one of
|
|
71
|
+
* 'valid' - a 2xx with `valid: true`.
|
|
72
|
+
* 'invalid' - any other 2xx, or a 4xx other than 404: the server
|
|
73
|
+
* looked at the key and said no.
|
|
74
|
+
* 'unavailable' - network error, timeout, 5xx or 404 (a server that
|
|
75
|
+
* doesn't have /v1/validate yet can't judge the key),
|
|
76
|
+
* or any other status. `reason` says which.
|
|
77
|
+
* `transport` is httpsPostJson unless a test passes a stand-in. */
|
|
78
|
+
export async function askKeyServer(serverUrl, key, transport = httpsPostJson) {
|
|
47
79
|
let res;
|
|
48
80
|
try {
|
|
49
|
-
res = await
|
|
50
|
-
method: 'POST',
|
|
51
|
-
headers: { 'content-type': 'application/json' },
|
|
52
|
-
body: JSON.stringify({ key: providedKey }),
|
|
53
|
-
signal: AbortSignal.timeout(10_000),
|
|
54
|
-
});
|
|
81
|
+
res = await transport(new URL('/v1/validate', serverUrl), JSON.stringify({ key }));
|
|
55
82
|
} catch (err) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
83
|
+
return { result: 'unavailable', reason: err.message };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (res.status >= 200 && res.status < 300) {
|
|
87
|
+
let body = null;
|
|
88
|
+
try {
|
|
89
|
+
body = JSON.parse(res.text);
|
|
90
|
+
} catch {}
|
|
91
|
+
return body?.valid === true ? { result: 'valid' } : { result: 'invalid', reason: `HTTP ${res.status}` };
|
|
92
|
+
}
|
|
93
|
+
if (res.status >= 400 && res.status < 500 && res.status !== 404) {
|
|
94
|
+
return { result: 'invalid', reason: `HTTP ${res.status}` };
|
|
61
95
|
}
|
|
96
|
+
return { result: 'unavailable', reason: `HTTP ${res.status}` };
|
|
97
|
+
}
|
|
62
98
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
99
|
+
/** `transport` is passed on to askKeyServer - tests only. */
|
|
100
|
+
export async function requireBuildKey(providedKey, transport = httpsPostJson) {
|
|
101
|
+
if (!providedKey) {
|
|
102
|
+
log.error('build requires a valid --key (or WRITEDOCS_API_KEY).');
|
|
103
|
+
process.exit(1);
|
|
66
104
|
}
|
|
67
105
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
106
|
+
// The exit code is a contract with whoever runs `build` - the WriteDocs
|
|
107
|
+
// platform retries on it, never on the message text:
|
|
108
|
+
// 1 - the key was not accepted: missing key, or no server said `valid`
|
|
109
|
+
// and at least one said no. Trying again with the same key gives
|
|
110
|
+
// the same answer.
|
|
111
|
+
// 75 - EX_TEMPFAIL (sysexits.h): no server could be asked - each one
|
|
112
|
+
// gave a network error, timeout, 5xx or 404. Still fails closed (no
|
|
113
|
+
// build happens); it only tells the caller that trying later may
|
|
114
|
+
// work.
|
|
115
|
+
// The first `valid` wins; a server that says no doesn't stop the next one
|
|
116
|
+
// from being asked, since a key may be known to only one of them.
|
|
117
|
+
const answers = [];
|
|
118
|
+
for (const serverUrl of KEY_SERVER_URLS) {
|
|
119
|
+
const answer = await askKeyServer(serverUrl, providedKey, transport);
|
|
120
|
+
if (answer.result === 'valid') return;
|
|
121
|
+
answers.push({ serverUrl, ...answer });
|
|
72
122
|
}
|
|
73
123
|
|
|
74
|
-
if (
|
|
124
|
+
if (answers.some((a) => a.result === 'invalid')) {
|
|
75
125
|
log.error('build requires a valid --key (or WRITEDOCS_API_KEY) - the server rejected this one.');
|
|
76
126
|
process.exit(1);
|
|
77
127
|
}
|
|
128
|
+
|
|
129
|
+
// Every server unavailable. Fails closed, same as a rejection - an
|
|
130
|
+
// unreachable authorization server is not treated as "no opinion, let it
|
|
131
|
+
// through".
|
|
132
|
+
log.error('Could not reach any build authorization server - try again later:');
|
|
133
|
+
for (const a of answers) log.line(` ${a.serverUrl}: ${a.reason}`);
|
|
134
|
+
process.exit(EX_TEMPFAIL);
|
|
78
135
|
}
|
package/src/lib/config.ts
CHANGED
|
@@ -2,7 +2,7 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { EXCLUDED_TOP_LEVEL_DIRS, navPageId } from './pages.js';
|
|
4
4
|
import { isHidden } from './hidden-sections.js';
|
|
5
|
-
import { itemPage, childLevel, resolveLandings as resolveLandingsJs } from './item-pages.js';
|
|
5
|
+
import { itemPage, childLevel, equivalentItem, resolveLandings as resolveLandingsJs } from './item-pages.js';
|
|
6
6
|
import matter from 'gray-matter';
|
|
7
7
|
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
8
8
|
import { readableTextOn } from './color.js';
|
|
@@ -785,7 +785,7 @@ export function resolveLandings(navigation: NavigationConfig): Landing[] {
|
|
|
785
785
|
* chosen - the switcher shows the item's name until the reader picks one
|
|
786
786
|
* (`placeholder`, currentOption() in lib/selector-placement.js). */
|
|
787
787
|
export function landingSelectors(landing: Landing, hrefForSlug: (slug: string) => string): Selector[] {
|
|
788
|
-
const selectors = buildSelectors(landing.path, hrefForSlug);
|
|
788
|
+
const selectors = buildSelectors(landing.path, hrefForSlug, -1, true);
|
|
789
789
|
const level = childLevel(landing.item) as { kind: PathSegment['kind']; items: NamedContainer[] } | null;
|
|
790
790
|
if (!level) return selectors;
|
|
791
791
|
selectors.push({
|
|
@@ -1066,11 +1066,15 @@ function dropdownMenuOf(
|
|
|
1066
1066
|
export function buildSelectors(
|
|
1067
1067
|
path: PathSegment[],
|
|
1068
1068
|
hrefForSlug: (slug: string) => string,
|
|
1069
|
-
activePagePosition: number = -1
|
|
1069
|
+
activePagePosition: number = -1,
|
|
1070
|
+
// The page is an item's own page (landingSelectors()): a version or
|
|
1071
|
+
// language switch goes to the same item in the other option - where
|
|
1072
|
+
// choosing that item goes, its own page or its first page.
|
|
1073
|
+
landing = false
|
|
1070
1074
|
): Selector[] {
|
|
1071
1075
|
return path.map((segment, segIndex): Selector => {
|
|
1072
1076
|
const preservesPosition =
|
|
1073
|
-
(segment.kind === 'version' || segment.kind === 'language') && activePagePosition >= 0;
|
|
1077
|
+
(segment.kind === 'version' || segment.kind === 'language') && (activePagePosition >= 0 || landing);
|
|
1074
1078
|
const remainingPath = path.slice(segIndex + 1);
|
|
1075
1079
|
return {
|
|
1076
1080
|
kind: segment.kind,
|
|
@@ -1079,9 +1083,12 @@ export function buildSelectors(
|
|
|
1079
1083
|
// A hidden item is never offered - not even to its own pages.
|
|
1080
1084
|
if (isHidden(item)) return [];
|
|
1081
1085
|
const isActive = i === segment.index;
|
|
1082
|
-
const
|
|
1083
|
-
|
|
1084
|
-
|
|
1086
|
+
const sameItem = preservesPosition && landing ? equivalentItem(item, remainingPath) : null;
|
|
1087
|
+
const equivalentSlug = !preservesPosition
|
|
1088
|
+
? null
|
|
1089
|
+
: landing
|
|
1090
|
+
? sameItem && firstSlugOf(sameItem as Container)
|
|
1091
|
+
: equivalentPageIn(item as Container, remainingPath, activePagePosition);
|
|
1085
1092
|
const targetSlug = equivalentSlug ?? firstSlugOf(item as Container) ?? 'index';
|
|
1086
1093
|
return {
|
|
1087
1094
|
label: labelOf(item),
|
package/src/lib/item-pages.js
CHANGED
|
@@ -69,3 +69,21 @@ export function resolveLandings(navigation) {
|
|
|
69
69
|
if (navigation && !Array.isArray(navigation)) walk(navigation, []);
|
|
70
70
|
return out;
|
|
71
71
|
}
|
|
72
|
+
|
|
73
|
+
/** The item in `from` at the same place as the item an item's own page
|
|
74
|
+
* belongs to - for a version or language switcher on that page.
|
|
75
|
+
* `remainingPath` is the levels below the one being switched, down to the
|
|
76
|
+
* page's item (`{ kind, index }` segments); empty when the item is the
|
|
77
|
+
* version or language itself. Null when `from` isn't built the same way
|
|
78
|
+
* that far down - the switcher then falls back to the option's first page,
|
|
79
|
+
* as it does for any page. */
|
|
80
|
+
export function equivalentItem(from, remainingPath) {
|
|
81
|
+
let node = from;
|
|
82
|
+
for (const segment of remainingPath) {
|
|
83
|
+
const level = childLevel(node);
|
|
84
|
+
if (!level || level.kind !== segment.kind) return null;
|
|
85
|
+
node = level.items[segment.index];
|
|
86
|
+
if (!node || typeof node !== 'object' || 'href' in node) return null;
|
|
87
|
+
}
|
|
88
|
+
return node;
|
|
89
|
+
}
|