@foliwake/cli 0.0.0-stage → 0.7.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/CHANGELOG.md +56 -0
- package/LICENSE +202 -0
- package/README.md +654 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/command-words.d.ts +13 -0
- package/dist/command-words.js +53 -0
- package/dist/commands/account/keys/create.d.ts +12 -0
- package/dist/commands/account/keys/create.js +36 -0
- package/dist/commands/account/keys/list.d.ts +9 -0
- package/dist/commands/account/keys/list.js +23 -0
- package/dist/commands/account/keys/revoke.d.ts +12 -0
- package/dist/commands/account/keys/revoke.js +15 -0
- package/dist/commands/branch/bootstrap.d.ts +11 -0
- package/dist/commands/branch/bootstrap.js +23 -0
- package/dist/commands/branch/delete.d.ts +17 -0
- package/dist/commands/branch/delete.js +38 -0
- package/dist/commands/branch/history.d.ts +14 -0
- package/dist/commands/branch/history.js +24 -0
- package/dist/commands/branch/list.d.ts +11 -0
- package/dist/commands/branch/list.js +15 -0
- package/dist/commands/branch/update.d.ts +19 -0
- package/dist/commands/branch/update.js +49 -0
- package/dist/commands/code/deploy.d.ts +15 -0
- package/dist/commands/code/deploy.js +78 -0
- package/dist/commands/code/prepare.d.ts +13 -0
- package/dist/commands/code/prepare.js +54 -0
- package/dist/commands/code/release.d.ts +19 -0
- package/dist/commands/code/release.js +51 -0
- package/dist/commands/code/rollback.d.ts +17 -0
- package/dist/commands/code/rollback.js +38 -0
- package/dist/commands/connect.d.ts +19 -0
- package/dist/commands/connect.js +100 -0
- package/dist/commands/create.d.ts +32 -0
- package/dist/commands/create.js +171 -0
- package/dist/commands/credential/list.d.ts +10 -0
- package/dist/commands/credential/list.js +21 -0
- package/dist/commands/credential/rm.d.ts +12 -0
- package/dist/commands/credential/rm.js +18 -0
- package/dist/commands/credential/set.d.ts +15 -0
- package/dist/commands/credential/set.js +58 -0
- package/dist/commands/dev.d.ts +30 -0
- package/dist/commands/dev.js +144 -0
- package/dist/commands/login.d.ts +20 -0
- package/dist/commands/login.js +94 -0
- package/dist/commands/logout.d.ts +14 -0
- package/dist/commands/logout.js +61 -0
- package/dist/commands/migration/commit.d.ts +13 -0
- package/dist/commands/migration/commit.js +42 -0
- package/dist/commands/migration/prepare.d.ts +18 -0
- package/dist/commands/migration/prepare.js +53 -0
- package/dist/commands/operation/cancel.d.ts +14 -0
- package/dist/commands/operation/cancel.js +21 -0
- package/dist/commands/operation/get.d.ts +14 -0
- package/dist/commands/operation/get.js +26 -0
- package/dist/commands/operation/list.d.ts +12 -0
- package/dist/commands/operation/list.js +25 -0
- package/dist/commands/operation/recover.d.ts +20 -0
- package/dist/commands/operation/recover.js +61 -0
- package/dist/commands/page/changes.d.ts +13 -0
- package/dist/commands/page/changes.js +26 -0
- package/dist/commands/page/check.d.ts +12 -0
- package/dist/commands/page/check.js +25 -0
- package/dist/commands/page/replace.d.ts +13 -0
- package/dist/commands/page/replace.js +59 -0
- package/dist/commands/page/unpreview.d.ts +17 -0
- package/dist/commands/page/unpreview.js +54 -0
- package/dist/commands/page/unpublish.d.ts +18 -0
- package/dist/commands/page/unpublish.js +36 -0
- package/dist/commands/project/preview.d.ts +8 -0
- package/dist/commands/project/preview.js +20 -0
- package/dist/commands/project/publish.d.ts +16 -0
- package/dist/commands/project/publish.js +40 -0
- package/dist/commands/project/unpublish.d.ts +16 -0
- package/dist/commands/project/unpublish.js +33 -0
- package/dist/commands/release/rollback.d.ts +17 -0
- package/dist/commands/release/rollback.js +38 -0
- package/dist/commands/servers/index.d.ts +20 -0
- package/dist/commands/servers/index.js +36 -0
- package/dist/commands/servers/use.d.ts +11 -0
- package/dist/commands/servers/use.js +30 -0
- package/dist/commands/site/adopt.d.ts +11 -0
- package/dist/commands/site/adopt.js +21 -0
- package/dist/commands/site/create.d.ts +18 -0
- package/dist/commands/site/create.js +61 -0
- package/dist/commands/site/edit.d.ts +11 -0
- package/dist/commands/site/edit.js +56 -0
- package/dist/commands/site/history.d.ts +11 -0
- package/dist/commands/site/history.js +25 -0
- package/dist/commands/site/link.d.ts +15 -0
- package/dist/commands/site/link.js +35 -0
- package/dist/commands/site/list.d.ts +9 -0
- package/dist/commands/site/list.js +29 -0
- package/dist/commands/site/reset-storage.d.ts +15 -0
- package/dist/commands/site/reset-storage.js +61 -0
- package/dist/commands/site/set.d.ts +16 -0
- package/dist/commands/site/set.js +45 -0
- package/dist/commands/site/show.d.ts +10 -0
- package/dist/commands/site/show.js +19 -0
- package/dist/commands/site/status.d.ts +11 -0
- package/dist/commands/site/status.js +15 -0
- package/dist/commands/site/unlink.d.ts +11 -0
- package/dist/commands/site/unlink.js +28 -0
- package/dist/commands/source/add.d.ts +32 -0
- package/dist/commands/source/add.js +103 -0
- package/dist/commands/source/adopt-pages.d.ts +14 -0
- package/dist/commands/source/adopt-pages.js +30 -0
- package/dist/commands/source/check.d.ts +17 -0
- package/dist/commands/source/check.js +52 -0
- package/dist/commands/source/list.d.ts +11 -0
- package/dist/commands/source/list.js +15 -0
- package/dist/commands/source/mappings/check.d.ts +13 -0
- package/dist/commands/source/mappings/check.js +69 -0
- package/dist/commands/source/pending.d.ts +12 -0
- package/dist/commands/source/pending.js +25 -0
- package/dist/commands/source/plugins.d.ts +6 -0
- package/dist/commands/source/plugins.js +17 -0
- package/dist/commands/source/sync.d.ts +16 -0
- package/dist/commands/source/sync.js +58 -0
- package/dist/commands/whoami.d.ts +6 -0
- package/dist/commands/whoami.js +22 -0
- package/dist/connect-prompts.d.ts +16 -0
- package/dist/connect-prompts.js +24 -0
- package/dist/connect.d.ts +133 -0
- package/dist/connect.js +416 -0
- package/dist/core-command.d.ts +99 -0
- package/dist/core-command.js +332 -0
- package/dist/core-types.d.ts +21 -0
- package/dist/core-types.js +1 -0
- package/dist/create-connect.d.ts +26 -0
- package/dist/create-connect.js +93 -0
- package/dist/dev/code-watcher.d.ts +66 -0
- package/dist/dev/code-watcher.js +254 -0
- package/dist/dev/dev-checks.d.ts +14 -0
- package/dist/dev/dev-checks.js +138 -0
- package/dist/dev/dev-reload.d.ts +51 -0
- package/dist/dev/dev-reload.js +345 -0
- package/dist/dev/flags.d.ts +19 -0
- package/dist/dev/flags.js +54 -0
- package/dist/dev/html.d.ts +30 -0
- package/dist/dev/html.js +106 -0
- package/dist/dev/kit-files.d.ts +7 -0
- package/dist/dev/kit-files.js +33 -0
- package/dist/dev/local-code.d.ts +22 -0
- package/dist/dev/local-code.js +101 -0
- package/dist/dev/remote-config.d.ts +57 -0
- package/dist/dev/remote-config.js +100 -0
- package/dist/dev/remote-dev.d.ts +41 -0
- package/dist/dev/remote-dev.js +527 -0
- package/dist/dev/remote-fetch.d.ts +5 -0
- package/dist/dev/remote-fetch.js +36 -0
- package/dist/dev/remote-html.d.ts +10 -0
- package/dist/dev/remote-html.js +60 -0
- package/dist/dev/remote-page.d.ts +34 -0
- package/dist/dev/remote-page.js +183 -0
- package/dist/dev/remote-routes.d.ts +63 -0
- package/dist/dev/remote-routes.js +114 -0
- package/dist/dev/signal-stop.d.ts +17 -0
- package/dist/dev/signal-stop.js +46 -0
- package/dist/dev/start.d.ts +18 -0
- package/dist/dev/start.js +80 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +8 -0
- package/dist/lib/diagnostics.d.ts +15 -0
- package/dist/lib/diagnostics.js +12 -0
- package/dist/lib/front-matter.d.ts +6 -0
- package/dist/lib/front-matter.js +59 -0
- package/dist/lib/operation-result.d.ts +3 -0
- package/dist/lib/operation-result.js +16 -0
- package/dist/lib/problem-groups.d.ts +27 -0
- package/dist/lib/problem-groups.js +49 -0
- package/dist/lib/problems.d.ts +50 -0
- package/dist/lib/problems.js +94 -0
- package/dist/lib/prompts.d.ts +38 -0
- package/dist/lib/prompts.js +77 -0
- package/dist/lib/result-types.d.ts +198 -0
- package/dist/lib/result-types.js +3 -0
- package/dist/lib/rules.d.ts +15 -0
- package/dist/lib/rules.js +26 -0
- package/dist/lib/summaries.d.ts +19 -0
- package/dist/lib/summaries.js +22 -0
- package/dist/lib/sync-problems.d.ts +21 -0
- package/dist/lib/sync-problems.js +19 -0
- package/dist/local-runtime.d.ts +78 -0
- package/dist/local-runtime.js +11 -0
- package/dist/local-target.d.ts +6 -0
- package/dist/local-target.js +23 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +40 -0
- package/dist/project-preview.d.ts +35 -0
- package/dist/project-preview.js +156 -0
- package/dist/project-publish.d.ts +28 -0
- package/dist/project-publish.js +173 -0
- package/dist/remote/connection.d.ts +8 -0
- package/dist/remote/connection.js +19 -0
- package/dist/remote/core-failure.d.ts +8 -0
- package/dist/remote/core-failure.js +15 -0
- package/dist/remote/credentials.d.ts +15 -0
- package/dist/remote/credentials.js +112 -0
- package/dist/remote/device-login.d.ts +33 -0
- package/dist/remote/device-login.js +161 -0
- package/dist/remote/remote-caller.d.ts +14 -0
- package/dist/remote/remote-caller.js +52 -0
- package/dist/remote/rpc-wire.d.ts +18 -0
- package/dist/remote/rpc-wire.js +57 -0
- package/dist/remote/servers.d.ts +80 -0
- package/dist/remote/servers.js +162 -0
- package/dist/remote/session.d.ts +53 -0
- package/dist/remote/session.js +132 -0
- package/dist/remote/sign-in-here.d.ts +5 -0
- package/dist/remote/sign-in-here.js +33 -0
- package/dist/remote/sign-in.d.ts +25 -0
- package/dist/remote/sign-in.js +55 -0
- package/dist/remote/wait-slices.d.ts +16 -0
- package/dist/remote/wait-slices.js +21 -0
- package/dist/render/add.d.ts +3 -0
- package/dist/render/add.js +9 -0
- package/dist/render/branch-write.d.ts +9 -0
- package/dist/render/branch-write.js +28 -0
- package/dist/render/branches.d.ts +5 -0
- package/dist/render/branches.js +28 -0
- package/dist/render/credentials.d.ts +5 -0
- package/dist/render/credentials.js +39 -0
- package/dist/render/operation-result.d.ts +16 -0
- package/dist/render/operation-result.js +65 -0
- package/dist/render/operation.d.ts +7 -0
- package/dist/render/operation.js +15 -0
- package/dist/render/page-changes.d.ts +3 -0
- package/dist/render/page-changes.js +36 -0
- package/dist/render/pending.d.ts +3 -0
- package/dist/render/pending.js +46 -0
- package/dist/render/plugin-list.d.ts +3 -0
- package/dist/render/plugin-list.js +30 -0
- package/dist/render/probe.d.ts +3 -0
- package/dist/render/probe.js +61 -0
- package/dist/render/problems.d.ts +35 -0
- package/dist/render/problems.js +64 -0
- package/dist/render/recovery.d.ts +2 -0
- package/dist/render/recovery.js +36 -0
- package/dist/render/reset-storage.d.ts +4 -0
- package/dist/render/reset-storage.js +70 -0
- package/dist/render/site-status.d.ts +2 -0
- package/dist/render/site-status.js +81 -0
- package/dist/render/source-list.d.ts +2 -0
- package/dist/render/source-list.js +37 -0
- package/dist/render/sync.d.ts +5 -0
- package/dist/render/sync.js +66 -0
- package/dist/settings.d.ts +10 -0
- package/dist/settings.js +45 -0
- package/dist/site-entry-edit.d.ts +5 -0
- package/dist/site-entry-edit.js +44 -0
- package/dist/source-entry.d.ts +32 -0
- package/dist/source-entry.js +89 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.js +10 -0
- package/package.json +112 -3
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
// Browser auto-reload for `foliwake dev --watch` (no hot module replacement).
|
|
2
|
+
//
|
|
3
|
+
// The pieces do not depend on where page HTML comes from, so any dev page
|
|
4
|
+
// source can reuse them:
|
|
5
|
+
// - createReloadHub(): a debounced change notifier. notify(files) any number
|
|
6
|
+
// of times; after `debounceMs` without further changes it runs `check()`
|
|
7
|
+
// once (the current problems) and sends ONE `reload` event to every open
|
|
8
|
+
// event stream.
|
|
9
|
+
// - hub.handleEvents(): the Server-Sent Events endpoint (DEV_EVENTS_PATH).
|
|
10
|
+
// A new stream first gets `hello` (server id, change generation, current
|
|
11
|
+
// problems). Every flush increments the generation. The page carries the
|
|
12
|
+
// generation it was rendered at (data-generation on the client script tag),
|
|
13
|
+
// so a client reloads when `hello` shows another server id OR another
|
|
14
|
+
// generation: changes made while the stream was disconnected, or between
|
|
15
|
+
// rendering the page and connecting, are never missed.
|
|
16
|
+
// - injectDevClient(): the single injection point; adds the client script
|
|
17
|
+
// (DEV_CLIENT_PATH, DEV_CLIENT_SCRIPT) to an HTML document.
|
|
18
|
+
// - hub.stop(): sends `bye`, ends every stream and clears the timer, so
|
|
19
|
+
// `server.close()` is not held open by event streams.
|
|
20
|
+
import { randomUUID } from 'node:crypto';
|
|
21
|
+
export const DEV_EVENTS_PATH = '/_dev/events';
|
|
22
|
+
export const DEV_CLIENT_PATH = '/_dev/reload.js';
|
|
23
|
+
export const DEFAULT_RELOAD_DEBOUNCE_MS = 200;
|
|
24
|
+
export function formatProblem(p) {
|
|
25
|
+
const where = p.line == null ? p.file : `${p.file}:${p.line}${p.column == null ? '' : `:${p.column}`}`;
|
|
26
|
+
return ` ${where} ${p.code} ${p.message}${p.fix ? `\n Fix: ${p.fix}` : ''}`;
|
|
27
|
+
}
|
|
28
|
+
export function createReloadHub({ debounceMs = DEFAULT_RELOAD_DEBOUNCE_MS, check, log = () => { }, }) {
|
|
29
|
+
const serverId = randomUUID();
|
|
30
|
+
const clients = new Set();
|
|
31
|
+
const listeners = new Set();
|
|
32
|
+
const pending = new Set();
|
|
33
|
+
let timer = null;
|
|
34
|
+
let flushing = null;
|
|
35
|
+
let current = [];
|
|
36
|
+
let ready = null;
|
|
37
|
+
let stopped = false;
|
|
38
|
+
let generation = 0;
|
|
39
|
+
function write(res, event, data) {
|
|
40
|
+
res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
|
41
|
+
}
|
|
42
|
+
async function refresh() {
|
|
43
|
+
const run = (async () => {
|
|
44
|
+
try {
|
|
45
|
+
current = await check();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
current = [
|
|
49
|
+
{
|
|
50
|
+
file: '(dev server)',
|
|
51
|
+
line: null,
|
|
52
|
+
column: null,
|
|
53
|
+
code: 'DEV_CHECK_FAILED',
|
|
54
|
+
message: `The change check failed: ${error?.message ?? String(error)}`,
|
|
55
|
+
fix: 'See the terminal; save the file again after fixing it.',
|
|
56
|
+
},
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
return current;
|
|
60
|
+
})();
|
|
61
|
+
ready = run;
|
|
62
|
+
return run;
|
|
63
|
+
}
|
|
64
|
+
async function flush() {
|
|
65
|
+
const files = [...pending].sort();
|
|
66
|
+
pending.clear();
|
|
67
|
+
const before = current.length;
|
|
68
|
+
const problems = await refresh();
|
|
69
|
+
if (stopped)
|
|
70
|
+
return;
|
|
71
|
+
generation += 1;
|
|
72
|
+
const event = { files, problems, generation };
|
|
73
|
+
for (const res of clients)
|
|
74
|
+
write(res, 'reload', event);
|
|
75
|
+
const changed = files.length > 5 ? `${files.slice(0, 5).join(', ')} and ${files.length - 5} more` : files.join(', ');
|
|
76
|
+
if (problems.length)
|
|
77
|
+
log(`Changed: ${changed}. ${problems.length} problem(s); the browser shows ` +
|
|
78
|
+
`them:\n${problems.map(formatProblem).join('\n')}`);
|
|
79
|
+
else
|
|
80
|
+
log(`Changed: ${changed}. ${before ? 'Problems fixed; ' : ''}reloading ${clients.size} browser page(s).`);
|
|
81
|
+
for (const listener of listeners)
|
|
82
|
+
listener(event);
|
|
83
|
+
}
|
|
84
|
+
function schedule() {
|
|
85
|
+
if (timer)
|
|
86
|
+
clearTimeout(timer);
|
|
87
|
+
timer = setTimeout(() => {
|
|
88
|
+
timer = null;
|
|
89
|
+
// A change arriving during a flush is sent by the next flush.
|
|
90
|
+
const run = (flushing ?? Promise.resolve()).then(flush);
|
|
91
|
+
flushing = run.finally(() => {
|
|
92
|
+
if (flushing === run)
|
|
93
|
+
flushing = null;
|
|
94
|
+
});
|
|
95
|
+
}, debounceMs);
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
serverId,
|
|
99
|
+
generation: () => generation,
|
|
100
|
+
notify(files) {
|
|
101
|
+
if (stopped)
|
|
102
|
+
return;
|
|
103
|
+
for (const file of files)
|
|
104
|
+
pending.add(file);
|
|
105
|
+
schedule();
|
|
106
|
+
},
|
|
107
|
+
refresh,
|
|
108
|
+
problems: () => current,
|
|
109
|
+
handleEvents(req, res) {
|
|
110
|
+
if (stopped) {
|
|
111
|
+
res.writeHead(503, { 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'no-store' });
|
|
112
|
+
res.end('The dev server is stopping.\n');
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
// Same origin only: no Access-Control-* headers. `Connection: close`
|
|
116
|
+
// makes ending the stream also close its socket (prompt server.close()).
|
|
117
|
+
res.writeHead(200, {
|
|
118
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
119
|
+
'Cache-Control': 'no-store',
|
|
120
|
+
'X-Content-Type-Options': 'nosniff',
|
|
121
|
+
Connection: 'close',
|
|
122
|
+
});
|
|
123
|
+
res.write('retry: 1000\n\n');
|
|
124
|
+
clients.add(res);
|
|
125
|
+
const drop = () => {
|
|
126
|
+
clients.delete(res);
|
|
127
|
+
};
|
|
128
|
+
req.on('close', drop);
|
|
129
|
+
res.on('close', drop);
|
|
130
|
+
void (ready ?? refresh()).then(() => {
|
|
131
|
+
if (clients.has(res))
|
|
132
|
+
write(res, 'hello', { serverId, generation, problems: current });
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
clientCount: () => clients.size,
|
|
136
|
+
onReload(listener) {
|
|
137
|
+
listeners.add(listener);
|
|
138
|
+
return () => listeners.delete(listener);
|
|
139
|
+
},
|
|
140
|
+
stop() {
|
|
141
|
+
if (stopped)
|
|
142
|
+
return;
|
|
143
|
+
stopped = true;
|
|
144
|
+
if (timer)
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
timer = null;
|
|
147
|
+
pending.clear();
|
|
148
|
+
for (const res of clients) {
|
|
149
|
+
try {
|
|
150
|
+
write(res, 'bye', { serverId });
|
|
151
|
+
res.end();
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
res.destroy();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
clients.clear();
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The one place a dev client is added to an HTML document (any dev page source).
|
|
163
|
+
* `generation` is the hub's change generation when the document was produced;
|
|
164
|
+
* the client reloads when the server's generation differs after (re)connecting.
|
|
165
|
+
*/
|
|
166
|
+
export function injectDevClient(html, generation = null) {
|
|
167
|
+
const attr = Number.isSafeInteger(generation) && generation >= 0 ? ` data-generation="${generation}"` : '';
|
|
168
|
+
const tag = `<script src="${DEV_CLIENT_PATH}"${attr} defer></script>`;
|
|
169
|
+
const head = html.search(/<\/head>/i);
|
|
170
|
+
if (head >= 0)
|
|
171
|
+
return `${html.slice(0, head)} ${tag}\n${html.slice(head)}`;
|
|
172
|
+
return `${tag}\n${html}`;
|
|
173
|
+
}
|
|
174
|
+
// The browser client: reloads on `reload`, shows server problems (descriptor,
|
|
175
|
+
// module syntax, content errors), problems embedded in the page by the server
|
|
176
|
+
// (<script type="application/json" data-foliwake-dev-problems>) and browser problems (failed script or
|
|
177
|
+
// stylesheet loads, uncaught errors, components the loader marked as failed)
|
|
178
|
+
// in an overlay. Plain ES2017, no dependencies; its only globals are a guard and a
|
|
179
|
+
// connected flag (tests wait for it).
|
|
180
|
+
export const DEV_CLIENT_SCRIPT = `// foliwake dev --watch: reload after changes, show problems.
|
|
181
|
+
(function () {
|
|
182
|
+
'use strict';
|
|
183
|
+
if (window.__foliwakeDevClient) return;
|
|
184
|
+
window.__foliwakeDevClient = true;
|
|
185
|
+
var serverId = null;
|
|
186
|
+
// The change generation this page was rendered at (null: unknown, e.g. an
|
|
187
|
+
// older tag); compared with every hello so changes made while disconnected
|
|
188
|
+
// are not missed.
|
|
189
|
+
var pageGeneration = (function () {
|
|
190
|
+
var s = document.currentScript;
|
|
191
|
+
var g = s && s.getAttribute('data-generation');
|
|
192
|
+
return g != null && /^\\d+$/.test(g) ? Number(g) : null;
|
|
193
|
+
})();
|
|
194
|
+
var source = null;
|
|
195
|
+
var attempts = 0;
|
|
196
|
+
var serverProblems = [];
|
|
197
|
+
var browserProblems = [];
|
|
198
|
+
var dismissed = false;
|
|
199
|
+
|
|
200
|
+
function rel(url) {
|
|
201
|
+
try {
|
|
202
|
+
var u = new URL(url, location.href);
|
|
203
|
+
return u.origin === location.origin ? u.pathname.replace(/^\\//, '') : u.href;
|
|
204
|
+
} catch (e) { return String(url); }
|
|
205
|
+
}
|
|
206
|
+
function addBrowserProblem(p) {
|
|
207
|
+
var key = p.file + '|' + p.message;
|
|
208
|
+
for (var i = 0; i < browserProblems.length; i++) if (browserProblems[i].key === key) return;
|
|
209
|
+
p.key = key;
|
|
210
|
+
browserProblems.push(p);
|
|
211
|
+
render();
|
|
212
|
+
}
|
|
213
|
+
function el(tag, text, style) {
|
|
214
|
+
var e = document.createElement(tag);
|
|
215
|
+
if (text != null) e.textContent = text;
|
|
216
|
+
if (style) e.setAttribute('style', style);
|
|
217
|
+
return e;
|
|
218
|
+
}
|
|
219
|
+
function render() {
|
|
220
|
+
var all = serverProblems.concat(browserProblems);
|
|
221
|
+
var old = document.querySelector('[data-foliwake-dev-overlay]');
|
|
222
|
+
if (old) old.remove();
|
|
223
|
+
if (!all.length || dismissed || !document.body) return;
|
|
224
|
+
var box = el('div', null, 'position:fixed;left:0;right:0;bottom:0;z-index:2147483647;max-height:50vh;' +
|
|
225
|
+
'overflow:auto;background:#fff5f5;color:#1b1b1b;border-top:4px solid #b00020;' +
|
|
226
|
+
'font:14px/1.45 system-ui,sans-serif;padding:12px 16px;box-shadow:0 -4px 16px rgba(0,0,0,.2)');
|
|
227
|
+
box.setAttribute('data-foliwake-dev-overlay', '');
|
|
228
|
+
box.setAttribute('role', 'alert');
|
|
229
|
+
var close = el('button', 'Dismiss', 'float:right;font:inherit');
|
|
230
|
+
close.type = 'button';
|
|
231
|
+
close.addEventListener('click', function () { dismissed = true; render(); });
|
|
232
|
+
box.appendChild(close);
|
|
233
|
+
box.appendChild(el('strong', 'foliwake dev: ' + all.length + ' problem(s)'));
|
|
234
|
+
var list = el('ol', null, 'margin:6px 0;padding-left:20px');
|
|
235
|
+
all.forEach(function (p) {
|
|
236
|
+
var li = el('li', null, 'margin:0 0 6px');
|
|
237
|
+
li.setAttribute('data-file', p.file);
|
|
238
|
+
var where = p.file + (p.line != null ? ':' + p.line + (p.column != null ? ':' + p.column : '') : '');
|
|
239
|
+
li.appendChild(el('code', where, 'font:13px ui-monospace,Menlo,monospace;font-weight:600'));
|
|
240
|
+
li.appendChild(document.createTextNode(' ' + (p.code ? p.code + ': ' : '') + p.message));
|
|
241
|
+
if (p.fix) li.appendChild(el('div', 'Fix: ' + p.fix));
|
|
242
|
+
list.appendChild(li);
|
|
243
|
+
});
|
|
244
|
+
box.appendChild(list);
|
|
245
|
+
var after = 'Fix the file and save it: the page reloads by itself. All diagnostics: /_dev/diagnostics';
|
|
246
|
+
box.appendChild(el('div', after, 'color:#555'));
|
|
247
|
+
document.body.appendChild(box);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
window.addEventListener('error', function (event) {
|
|
251
|
+
var t = event.target;
|
|
252
|
+
if (t && t !== window && (t.tagName === 'SCRIPT' || t.tagName === 'LINK')) {
|
|
253
|
+
var url = t.src || t.href;
|
|
254
|
+
addBrowserProblem({
|
|
255
|
+
file: rel(url), line: null, column: null, code: 'DEV_LOAD_FAILED',
|
|
256
|
+
message: (t.tagName === 'SCRIPT' ? 'Script' : 'Stylesheet') + ' failed to load.',
|
|
257
|
+
fix: 'Check that the file exists and is served (see the terminal).'
|
|
258
|
+
});
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
addBrowserProblem({
|
|
262
|
+
file: event.filename ? rel(event.filename) : '(page)', line: event.lineno || null,
|
|
263
|
+
column: event.colno || null, code: 'DEV_SCRIPT_ERROR', message: String(event.message || 'Uncaught error'),
|
|
264
|
+
fix: null
|
|
265
|
+
});
|
|
266
|
+
}, true);
|
|
267
|
+
window.addEventListener('unhandledrejection', function (event) {
|
|
268
|
+
var r = event.reason;
|
|
269
|
+
addBrowserProblem({
|
|
270
|
+
file: '(page)', line: null, column: null, code: 'DEV_UNHANDLED_REJECTION',
|
|
271
|
+
message: String(r && r.message ? r.name + ': ' + r.message : r), fix: null
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
function scanComponents(root) {
|
|
275
|
+
var scope = root || document;
|
|
276
|
+
var failed = scope.querySelectorAll ? scope.querySelectorAll('[data-component-status="error"]') : [];
|
|
277
|
+
for (var i = 0; i < failed.length; i++) {
|
|
278
|
+
var name = failed[i].getAttribute('data-component') || '?';
|
|
279
|
+
addBrowserProblem({
|
|
280
|
+
file: 'components/' + name, line: null, column: null, code: 'DEV_COMPONENT_FAILED',
|
|
281
|
+
message: 'Component "' + name + '" failed in the browser; the console has the loader error.',
|
|
282
|
+
fix: 'Fix the component files (or components/components.json) and save.'
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Problems the server found in this page itself (for example a remote page
|
|
287
|
+
// that the local components cannot render with foliwake dev --remote).
|
|
288
|
+
function pageProblems() {
|
|
289
|
+
var blocks = document.querySelectorAll('script[type="application/json"][data-foliwake-dev-problems]');
|
|
290
|
+
for (var i = 0; i < blocks.length; i++) {
|
|
291
|
+
var list;
|
|
292
|
+
try { list = JSON.parse(blocks[i].textContent || '[]'); } catch (e) { continue; }
|
|
293
|
+
if (!Array.isArray(list)) continue;
|
|
294
|
+
list.forEach(function (p) {
|
|
295
|
+
if (!p || typeof p.message !== 'string') return;
|
|
296
|
+
addBrowserProblem({
|
|
297
|
+
file: String(p.file || '(page)'), line: p.line == null ? null : p.line,
|
|
298
|
+
column: p.column == null ? null : p.column, code: String(p.code || ''), message: p.message,
|
|
299
|
+
fix: p.fix == null ? null : String(p.fix)
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function observe() {
|
|
305
|
+
pageProblems();
|
|
306
|
+
scanComponents(document);
|
|
307
|
+
var watched = { subtree: true, attributes: true, attributeFilter: ['data-component-status'] };
|
|
308
|
+
new MutationObserver(function () { scanComponents(document); }).observe(document.documentElement, watched);
|
|
309
|
+
render();
|
|
310
|
+
}
|
|
311
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', observe); else observe();
|
|
312
|
+
|
|
313
|
+
function reconnectLater() {
|
|
314
|
+
window.__foliwakeDevConnected = false;
|
|
315
|
+
if (source) source.close();
|
|
316
|
+
source = null;
|
|
317
|
+
attempts++;
|
|
318
|
+
if (attempts > 60) return; // about 10 minutes, then stop; refresh by hand
|
|
319
|
+
setTimeout(connect, Math.min(1000 * Math.pow(2, attempts - 1), 10000));
|
|
320
|
+
}
|
|
321
|
+
function connect() {
|
|
322
|
+
source = new EventSource('${DEV_EVENTS_PATH}');
|
|
323
|
+
source.addEventListener('hello', function (e) {
|
|
324
|
+
var data = JSON.parse(e.data);
|
|
325
|
+
if (serverId && data.serverId !== serverId) { location.reload(); return; }
|
|
326
|
+
if (typeof data.generation === 'number') {
|
|
327
|
+
if (pageGeneration !== null && data.generation !== pageGeneration) { location.reload(); return; }
|
|
328
|
+
// An established connection without a page generation adopts the server's.
|
|
329
|
+
if (pageGeneration === null && serverId) { location.reload(); return; }
|
|
330
|
+
pageGeneration = data.generation;
|
|
331
|
+
}
|
|
332
|
+
serverId = data.serverId;
|
|
333
|
+
attempts = 0;
|
|
334
|
+
window.__foliwakeDevConnected = true;
|
|
335
|
+
serverProblems = data.problems || [];
|
|
336
|
+
render();
|
|
337
|
+
});
|
|
338
|
+
source.addEventListener('reload', function () { location.reload(); });
|
|
339
|
+
source.addEventListener('bye', function () { reconnectLater(); });
|
|
340
|
+
// Our own bounded backoff instead of EventSource's endless 1 s retries.
|
|
341
|
+
source.onerror = function () { reconnectLater(); };
|
|
342
|
+
}
|
|
343
|
+
connect();
|
|
344
|
+
})();
|
|
345
|
+
`;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The flags of `foliwake dev` as given (`remote`: 'preview' | 'live' or anything else the user typed). */
|
|
2
|
+
export interface DevFlags {
|
|
3
|
+
remote?: string | undefined;
|
|
4
|
+
'remote-url'?: string | undefined;
|
|
5
|
+
'remote-config'?: string | undefined;
|
|
6
|
+
'remote-auth-env'?: string | undefined;
|
|
7
|
+
'remote-auth-user'?: string | undefined;
|
|
8
|
+
/** Local mode only (the engine's): the configuration, site and content folder it serves. */
|
|
9
|
+
content?: string | undefined;
|
|
10
|
+
config?: string | undefined;
|
|
11
|
+
site?: string | undefined;
|
|
12
|
+
watch?: boolean | undefined;
|
|
13
|
+
'watch-polling'?: boolean | undefined;
|
|
14
|
+
}
|
|
15
|
+
export declare const isRemote: (flags: DevFlags) => boolean;
|
|
16
|
+
/** What the command line of `dev` may not combine; the message, or null. */
|
|
17
|
+
export declare function devProblem(flags: DevFlags): string | null;
|
|
18
|
+
/** `--remote` takes an optional value: alone (or before another option) it means preview. */
|
|
19
|
+
export declare function withRemoteValue(argv: readonly string[]): string[];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// What `foliwake dev` checks of its command line; no server imports, so `--help` stays light.
|
|
2
|
+
export const isRemote = (flags) => flags.remote !== undefined || flags['remote-url'] !== undefined;
|
|
3
|
+
function localOnlyProblem(flags) {
|
|
4
|
+
if (flags['remote-auth-env'] !== undefined)
|
|
5
|
+
return '--remote-auth-env applies with --remote-url.';
|
|
6
|
+
if (flags['remote-auth-user'] !== undefined)
|
|
7
|
+
return '--remote-auth-user applies with --remote-url.';
|
|
8
|
+
if (flags['remote-config'] !== undefined)
|
|
9
|
+
return '--remote-config applies with --remote.';
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
function authProblem(flags) {
|
|
13
|
+
const env = flags['remote-auth-env'] !== undefined;
|
|
14
|
+
const user = flags['remote-auth-user'] !== undefined;
|
|
15
|
+
if ((env || user) && flags['remote-url'] === undefined) {
|
|
16
|
+
return ('--remote-auth-env and --remote-auth-user apply with --remote-url; ' +
|
|
17
|
+
'with foliwake.remote.json the file names the variable.');
|
|
18
|
+
}
|
|
19
|
+
if (env !== user)
|
|
20
|
+
return '--remote-auth-env and --remote-auth-user must be given together.';
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
/** Remote mode reads no site configuration and no content folder. */
|
|
24
|
+
function localProblem(flags) {
|
|
25
|
+
const option = ['content', 'config', 'site'].find((name) => flags[name] !== undefined);
|
|
26
|
+
if (!option)
|
|
27
|
+
return null;
|
|
28
|
+
return (`--${option} does not apply with --remote: remote mode reads no site configuration and no content folder; ` +
|
|
29
|
+
'it uses ./foliwake.remote.json (or --remote-url) in the frontend checkout.');
|
|
30
|
+
}
|
|
31
|
+
/** What the command line of `dev` may not combine; the message, or null. */
|
|
32
|
+
export function devProblem(flags) {
|
|
33
|
+
const polling = flags['watch-polling'];
|
|
34
|
+
if (polling !== undefined && !flags.watch) {
|
|
35
|
+
return `--${polling ? 'watch-polling' : 'no-watch-polling'} applies with --watch.`;
|
|
36
|
+
}
|
|
37
|
+
if (!isRemote(flags))
|
|
38
|
+
return localOnlyProblem(flags);
|
|
39
|
+
if (flags.remote !== undefined && flags.remote !== 'preview' && flags.remote !== 'live') {
|
|
40
|
+
return `Unknown remote target "${flags.remote}"; use --remote preview (the default) or --remote live.`;
|
|
41
|
+
}
|
|
42
|
+
const local = localProblem(flags);
|
|
43
|
+
if (local)
|
|
44
|
+
return local;
|
|
45
|
+
if (flags['remote-config'] !== undefined && flags['remote-url'] !== undefined) {
|
|
46
|
+
return 'Use either --remote-config or --remote-url, not both.';
|
|
47
|
+
}
|
|
48
|
+
return authProblem(flags);
|
|
49
|
+
}
|
|
50
|
+
/** `--remote` takes an optional value: alone (or before another option) it means preview. */
|
|
51
|
+
export function withRemoteValue(argv) {
|
|
52
|
+
const bare = (i) => i + 1 >= argv.length || argv[i + 1].startsWith('-');
|
|
53
|
+
return argv.map((arg, i) => (arg === '--remote' && bare(i) ? '--remote=preview' : arg));
|
|
54
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Diagnostic } from '@foliwake/site-check/formats/diagnostics';
|
|
2
|
+
export declare function escapeHtml(value: unknown): string;
|
|
3
|
+
/**
|
|
4
|
+
* The import map of a code release at `codeBase`: blocks import the kit as `@foliwake/block-kit`, which
|
|
5
|
+
* resolves to the kit of the release (`<codeBase>_foliwake/block-kit.js`). `<` is escaped.
|
|
6
|
+
*/
|
|
7
|
+
export declare function importMapScript(codeBase: string): string;
|
|
8
|
+
/** The page fields the local shell renders. */
|
|
9
|
+
export interface ShellPage {
|
|
10
|
+
title: string;
|
|
11
|
+
description: string | null;
|
|
12
|
+
body: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function renderPageShell({ page, lang, htmlContract, devBanner, componentCss, template, }: {
|
|
15
|
+
page: ShellPage;
|
|
16
|
+
lang?: string;
|
|
17
|
+
htmlContract: number;
|
|
18
|
+
devBanner?: string;
|
|
19
|
+
componentCss?: readonly string[];
|
|
20
|
+
template?: {
|
|
21
|
+
name: string;
|
|
22
|
+
css: boolean;
|
|
23
|
+
js: boolean;
|
|
24
|
+
} | null;
|
|
25
|
+
}): string;
|
|
26
|
+
export declare function renderDiagnosticsPage({ title, intro, diagnostics, }: {
|
|
27
|
+
title: string;
|
|
28
|
+
intro: string;
|
|
29
|
+
diagnostics: readonly Diagnostic[] | null;
|
|
30
|
+
}): string;
|
package/dist/dev/html.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// HTML produced by the dev server itself: the page shell and the diagnostics pages. Every author-controlled
|
|
2
|
+
// value is escaped here.
|
|
3
|
+
import { KIT_PATH, KIT_SPECIFIER } from '@foliwake/blocks-contract/import-map';
|
|
4
|
+
/** The language of the shell when the page names none. */
|
|
5
|
+
const SHELL_LANG = 'en';
|
|
6
|
+
const ESCAPES = {
|
|
7
|
+
'&': '&',
|
|
8
|
+
'<': '<',
|
|
9
|
+
'>': '>',
|
|
10
|
+
'"': '"',
|
|
11
|
+
"'": ''',
|
|
12
|
+
};
|
|
13
|
+
export function escapeHtml(value) {
|
|
14
|
+
return String(value).replace(/[&<>"']/g, (ch) => ESCAPES[ch]);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The import map of a code release at `codeBase`: blocks import the kit as `@foliwake/block-kit`, which
|
|
18
|
+
* resolves to the kit of the release (`<codeBase>_foliwake/block-kit.js`). `<` is escaped.
|
|
19
|
+
*/
|
|
20
|
+
export function importMapScript(codeBase) {
|
|
21
|
+
const map = JSON.stringify({ imports: { [KIT_SPECIFIER]: `${codeBase}${KIT_PATH}` } });
|
|
22
|
+
return `<script type="importmap">${map.replaceAll('<', '\\u003c')}</script>`;
|
|
23
|
+
}
|
|
24
|
+
/** The link of one component's stylesheet in the shell. */
|
|
25
|
+
function componentStylesheet(name) {
|
|
26
|
+
const href = escapeHtml(`/components/${name}/${name}.css`);
|
|
27
|
+
return `\n <link rel="stylesheet" href="${href}" data-component-css="${escapeHtml(name)}">`;
|
|
28
|
+
}
|
|
29
|
+
// `devBanner` (dev server HTML, already escaped) is placed before <main>, so
|
|
30
|
+
// the content inside <main> is exactly the page body; local Markdown pages
|
|
31
|
+
// have none.
|
|
32
|
+
export function renderPageShell({ page, lang = SHELL_LANG, htmlContract, devBanner = '', componentCss = [], template = null, }) {
|
|
33
|
+
const templateBase = template ? `/templates/${template.name}/${template.name}` : '';
|
|
34
|
+
const templateAttribute = template ? ` data-template="${escapeHtml(template.name)}"` : '';
|
|
35
|
+
const templateStyle = template?.css
|
|
36
|
+
? `\n <link rel="stylesheet" href="${escapeHtml(`${templateBase}.css`)}" data-template-css>`
|
|
37
|
+
: '';
|
|
38
|
+
const templateScript = template?.js
|
|
39
|
+
? `\n <script type="module" src="${escapeHtml(`${templateBase}.js`)}"></script>`
|
|
40
|
+
: '';
|
|
41
|
+
const componentStyles = componentCss.map(componentStylesheet).join('');
|
|
42
|
+
const description = page.description == null ? '' : `\n <meta name="description" content="${escapeHtml(page.description)}">`;
|
|
43
|
+
return `<!doctype html>
|
|
44
|
+
<html lang="${escapeHtml(lang)}" data-env="local" data-html-contract="${htmlContract}"${templateAttribute}>
|
|
45
|
+
<head>
|
|
46
|
+
<meta charset="utf-8">
|
|
47
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
48
|
+
${importMapScript('/')}
|
|
49
|
+
<title>${escapeHtml(page.title)}</title>${description}
|
|
50
|
+
<link rel="stylesheet" href="/styles/styles.css">${templateStyle}${componentStyles}
|
|
51
|
+
<script type="module" src="/scripts/components.js"></script>${templateScript}
|
|
52
|
+
</head>
|
|
53
|
+
<body>${devBanner ? `\n${devBanner}` : ''}
|
|
54
|
+
<main>
|
|
55
|
+
${page.body}
|
|
56
|
+
</main>
|
|
57
|
+
</body>
|
|
58
|
+
</html>
|
|
59
|
+
`;
|
|
60
|
+
}
|
|
61
|
+
const ALL_DIAGNOSTICS = 'All diagnostics: <a href="/_dev/diagnostics">/_dev/diagnostics</a>';
|
|
62
|
+
const DIAGNOSTICS_STYLE = `
|
|
63
|
+
body { font: 15px/1.5 system-ui, sans-serif; margin: 2rem auto; max-width: 60rem; padding: 0 1rem; color: #1b1b1b; }
|
|
64
|
+
h1 { font-size: 1.4rem; }
|
|
65
|
+
ol { padding-left: 1.2rem; }
|
|
66
|
+
li { margin: 0 0 1rem; padding: .6rem .8rem; border-left: 4px solid #b00020; background: #fff5f5; }
|
|
67
|
+
li.warning { border-color: #a15c00; background: #fffaf0; }
|
|
68
|
+
code { font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
69
|
+
.where { font-weight: 600; }
|
|
70
|
+
.fix { margin-top: .3rem; }
|
|
71
|
+
.muted { color: #555; }
|
|
72
|
+
`;
|
|
73
|
+
function renderDiagnosticItem(d) {
|
|
74
|
+
const where = `${d.file}:${d.line}:${d.column}`;
|
|
75
|
+
const scope = [d.component, d.field].filter((v) => v != null).join(' ');
|
|
76
|
+
const scoped = scope ? ` <code>${escapeHtml(scope)}</code>` : '';
|
|
77
|
+
const head = `<code class="where">${escapeHtml(where)}</code> <strong>${escapeHtml(d.severity)}</strong>`;
|
|
78
|
+
const fix = d.fix ? `\n <div class="fix">Fix: ${escapeHtml(d.fix)}</div>` : '';
|
|
79
|
+
return ` <li class="${escapeHtml(d.severity)}">
|
|
80
|
+
<div>${head} <code>${escapeHtml(d.code)}</code>${scoped}</div>
|
|
81
|
+
<div>${escapeHtml(d.message)}</div>${fix}
|
|
82
|
+
</li>`;
|
|
83
|
+
}
|
|
84
|
+
export function renderDiagnosticsPage({ title, intro, diagnostics, }) {
|
|
85
|
+
let items = '';
|
|
86
|
+
if (diagnostics) {
|
|
87
|
+
items = diagnostics.length
|
|
88
|
+
? ` <ol>\n${diagnostics.map(renderDiagnosticItem).join('\n')}\n </ol>`
|
|
89
|
+
: ' <p>No diagnostics.</p>';
|
|
90
|
+
}
|
|
91
|
+
return `<!doctype html>
|
|
92
|
+
<html lang="en">
|
|
93
|
+
<head>
|
|
94
|
+
<meta charset="utf-8">
|
|
95
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
96
|
+
<title>${escapeHtml(title)}</title>
|
|
97
|
+
<style>${DIAGNOSTICS_STYLE}</style>
|
|
98
|
+
</head>
|
|
99
|
+
<body>
|
|
100
|
+
<h1>${escapeHtml(title)}</h1>
|
|
101
|
+
<p class="muted">${escapeHtml(intro)} Fix the source file and refresh. ${ALL_DIAGNOSTICS}.</p>
|
|
102
|
+
${items}
|
|
103
|
+
</body>
|
|
104
|
+
</html>
|
|
105
|
+
`;
|
|
106
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const KIT_PREFIX = "_foliwake";
|
|
2
|
+
/** File name under /_foliwake/ → its content type. */
|
|
3
|
+
export declare const KIT_FILES: Readonly<Record<string, string>>;
|
|
4
|
+
/** Whether decoded request segments name a kit file (`/_foliwake/<file>`). */
|
|
5
|
+
export declare function isKitFile(segments: readonly string[]): boolean;
|
|
6
|
+
/** The bytes of a kit file, read once. */
|
|
7
|
+
export declare function kitFileBytes(name: string): Buffer;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The files the dev server serves under /_foliwake/: the block kit and lit-html from the installed
|
|
2
|
+
// @foliwake/block-kit (the same files every code release gets), and the block definition schema of
|
|
3
|
+
// @foliwake/site-check for editors.
|
|
4
|
+
import { readFileSync } from 'node:fs';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { BLOCK_SCHEMA_FILE, blockSchemaText } from '@foliwake/site-check/blocks/schema';
|
|
7
|
+
export const KIT_PREFIX = '_foliwake';
|
|
8
|
+
const JS = 'text/javascript; charset=utf-8';
|
|
9
|
+
/** File name under /_foliwake/ → its content type. */
|
|
10
|
+
export const KIT_FILES = Object.freeze({
|
|
11
|
+
'block-kit.js': JS,
|
|
12
|
+
'lit-html.js': JS,
|
|
13
|
+
[BLOCK_SCHEMA_FILE]: 'application/schema+json',
|
|
14
|
+
});
|
|
15
|
+
/** Whether decoded request segments name a kit file (`/_foliwake/<file>`). */
|
|
16
|
+
export function isKitFile(segments) {
|
|
17
|
+
return segments.length === 2 && segments[0] === KIT_PREFIX && Object.hasOwn(KIT_FILES, segments[1]);
|
|
18
|
+
}
|
|
19
|
+
function packageFile(name) {
|
|
20
|
+
return readFileSync(fileURLToPath(import.meta.resolve(`@foliwake/block-kit/${name}`)));
|
|
21
|
+
}
|
|
22
|
+
const cache = new Map();
|
|
23
|
+
/** The bytes of a kit file, read once. */
|
|
24
|
+
export function kitFileBytes(name) {
|
|
25
|
+
if (!Object.hasOwn(KIT_FILES, name))
|
|
26
|
+
throw new Error(`not a kit file: ${name}`);
|
|
27
|
+
let bytes = cache.get(name);
|
|
28
|
+
if (!bytes) {
|
|
29
|
+
bytes = name === BLOCK_SCHEMA_FILE ? Buffer.from(blockSchemaText()) : packageFile(name);
|
|
30
|
+
cache.set(name, bytes);
|
|
31
|
+
}
|
|
32
|
+
return bytes;
|
|
33
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ServerResponse } from 'node:http';
|
|
2
|
+
import { isKitFile } from './kit-files.js';
|
|
3
|
+
export { isKitFile };
|
|
4
|
+
/** Code directories served from the working directory. */
|
|
5
|
+
export declare const CODE_PREFIXES: readonly string[];
|
|
6
|
+
/** Headers of every dev response: never cached, never sniffed. */
|
|
7
|
+
export declare const BASE_HEADERS: {
|
|
8
|
+
readonly 'Cache-Control': "no-store";
|
|
9
|
+
readonly 'X-Content-Type-Options': "nosniff";
|
|
10
|
+
};
|
|
11
|
+
/** Thrown for anything that is answered with the dev 404 page. */
|
|
12
|
+
export declare class NotFound extends Error {
|
|
13
|
+
}
|
|
14
|
+
type Method = string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Splits the raw request path into decoded segments, rejecting anything that
|
|
17
|
+
* could address a file outside the intended root (backslashes, NUL, encoded
|
|
18
|
+
* slashes, `.` and `..` segments).
|
|
19
|
+
*/
|
|
20
|
+
export declare function decodeSegments(rawPath: string): string[];
|
|
21
|
+
/** Serves `<cwd>/<prefix>/<rest…>` (a code file of an allowed type inside that directory) or throws NotFound. */
|
|
22
|
+
export declare function serveCodeFile(res: ServerResponse, cwd: string, prefix: string, rest: readonly string[], method: Method): Promise<void>;
|