@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,101 @@
|
|
|
1
|
+
// Local working files of `dev --remote`: path decoding and the code directories components/, scripts/,
|
|
2
|
+
// styles/ and references/, read from the working directory on every request (no cache).
|
|
3
|
+
import { createReadStream } from 'node:fs';
|
|
4
|
+
import { realpath, stat } from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { CODE_ASSET_TYPES } from '@foliwake/site-check/formats/code-files';
|
|
7
|
+
import { KIT_FILES, KIT_PREFIX, isKitFile, kitFileBytes } from './kit-files.js';
|
|
8
|
+
export { isKitFile };
|
|
9
|
+
/** An SVG is served so that it cannot run script, as a code release serves it. */
|
|
10
|
+
const SVG_HEADERS = Object.freeze({
|
|
11
|
+
'Content-Security-Policy': "default-src 'none'; style-src 'unsafe-inline'; img-src data:; sandbox",
|
|
12
|
+
});
|
|
13
|
+
const codeFileHeaders = (extension) => extension === '.svg' ? SVG_HEADERS : {};
|
|
14
|
+
/** Code directories served from the working directory. */
|
|
15
|
+
export const CODE_PREFIXES = ['components', 'scripts', 'styles', 'references'];
|
|
16
|
+
// The only file types allowed there: scripts, stylesheets, JSON, and the fonts and images they reference.
|
|
17
|
+
const CODE_TYPES = {
|
|
18
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
19
|
+
'.css': 'text/css; charset=utf-8',
|
|
20
|
+
'.json': 'application/json; charset=utf-8',
|
|
21
|
+
...CODE_ASSET_TYPES,
|
|
22
|
+
};
|
|
23
|
+
/** Headers of every dev response: never cached, never sniffed. */
|
|
24
|
+
export const BASE_HEADERS = {
|
|
25
|
+
'Cache-Control': 'no-store',
|
|
26
|
+
'X-Content-Type-Options': 'nosniff',
|
|
27
|
+
};
|
|
28
|
+
/** Thrown for anything that is answered with the dev 404 page. */
|
|
29
|
+
export class NotFound extends Error {
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Splits the raw request path into decoded segments, rejecting anything that
|
|
33
|
+
* could address a file outside the intended root (backslashes, NUL, encoded
|
|
34
|
+
* slashes, `.` and `..` segments).
|
|
35
|
+
*/
|
|
36
|
+
export function decodeSegments(rawPath) {
|
|
37
|
+
if (/[\\\0]|%00|%2f|%5c/i.test(rawPath))
|
|
38
|
+
throw new NotFound();
|
|
39
|
+
const segments = rawPath.split('/').slice(1);
|
|
40
|
+
return segments.map((segment) => {
|
|
41
|
+
let decoded;
|
|
42
|
+
try {
|
|
43
|
+
decoded = decodeURIComponent(segment);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
throw new NotFound();
|
|
47
|
+
}
|
|
48
|
+
if (decoded === '.' || decoded === '..' || /[\\/\0]/.test(decoded))
|
|
49
|
+
throw new NotFound();
|
|
50
|
+
return decoded;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async function isInside(baseDir, target) {
|
|
54
|
+
const [realBase, realTarget] = await Promise.all([realpath(baseDir), realpath(target)]);
|
|
55
|
+
return realTarget === realBase || realTarget.startsWith(realBase + path.sep);
|
|
56
|
+
}
|
|
57
|
+
async function sendFile(res, absolutePath, contentType, method, headers = {}) {
|
|
58
|
+
const info = await stat(absolutePath);
|
|
59
|
+
if (!info.isFile())
|
|
60
|
+
throw new NotFound();
|
|
61
|
+
res.writeHead(200, { ...BASE_HEADERS, ...headers, 'Content-Type': contentType, 'Content-Length': info.size });
|
|
62
|
+
if (method === 'HEAD') {
|
|
63
|
+
res.end();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
await new Promise((resolve, reject) => {
|
|
67
|
+
createReadStream(absolutePath).on('error', reject).on('end', resolve).pipe(res);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** Serves `<cwd>/<prefix>/<rest…>` (a code file of an allowed type inside that directory) or throws NotFound. */
|
|
71
|
+
export async function serveCodeFile(res, cwd, prefix, rest, method) {
|
|
72
|
+
if (prefix === KIT_PREFIX)
|
|
73
|
+
return serveKitFile(res, rest, method);
|
|
74
|
+
if (rest.length === 0 || rest.some((s) => s === '' || s.startsWith('.')))
|
|
75
|
+
throw new NotFound();
|
|
76
|
+
const extension = path.extname(rest[rest.length - 1]);
|
|
77
|
+
const contentType = CODE_TYPES[extension];
|
|
78
|
+
if (!contentType)
|
|
79
|
+
throw new NotFound();
|
|
80
|
+
const baseDir = path.join(cwd, prefix);
|
|
81
|
+
const target = path.join(baseDir, ...rest);
|
|
82
|
+
try {
|
|
83
|
+
if (!(await isInside(baseDir, target)))
|
|
84
|
+
throw new NotFound();
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
const code = error.code; // realpath's system error, or the NotFound above (no code)
|
|
88
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
89
|
+
throw new NotFound();
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
await sendFile(res, target, contentType, method, codeFileHeaders(extension));
|
|
93
|
+
}
|
|
94
|
+
// /_foliwake/<file>: a kit file (kit-files.ts), the same bytes for every site.
|
|
95
|
+
function serveKitFile(res, rest, method) {
|
|
96
|
+
if (!isKitFile([KIT_PREFIX, ...rest]))
|
|
97
|
+
throw new NotFound();
|
|
98
|
+
const bytes = kitFileBytes(rest[0]);
|
|
99
|
+
res.writeHead(200, { ...BASE_HEADERS, 'Content-Type': KIT_FILES[rest[0]], 'Content-Length': bytes.length });
|
|
100
|
+
res.end(method === 'HEAD' ? undefined : bytes);
|
|
101
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { FoliwakeConfigError, isLoopbackName, parseRemoteUrl } from '@foliwake/server/config';
|
|
2
|
+
export { isLoopbackName, parseRemoteUrl };
|
|
3
|
+
export declare const REMOTE_CONFIG_FILE = "foliwake.remote.json";
|
|
4
|
+
export declare const REMOTE_CHANNELS: readonly ["preview", "live"];
|
|
5
|
+
export type RemoteChannel = (typeof REMOTE_CHANNELS)[number];
|
|
6
|
+
/** HTTP Basic credentials: a user name and the NAME of the variable holding the password. */
|
|
7
|
+
export interface RemoteAuth {
|
|
8
|
+
type: 'basic';
|
|
9
|
+
username: string;
|
|
10
|
+
passwordEnv: string;
|
|
11
|
+
}
|
|
12
|
+
/** The deployed site one dev server reads content from. */
|
|
13
|
+
export interface RemoteTarget {
|
|
14
|
+
channel: RemoteChannel;
|
|
15
|
+
/** Origin only (scheme, host, port), e.g. https://preview.foliwake.example */
|
|
16
|
+
origin: string;
|
|
17
|
+
auth: RemoteAuth | null;
|
|
18
|
+
/** Where the endpoint came from, for messages: the file name or the option. */
|
|
19
|
+
from: string;
|
|
20
|
+
}
|
|
21
|
+
/** A configuration problem; the message is shown as is (it never contains a secret). */
|
|
22
|
+
export declare const RemoteConfigError: typeof FoliwakeConfigError;
|
|
23
|
+
export type RemoteConfigError = FoliwakeConfigError;
|
|
24
|
+
/** Validates an environment variable NAME (never a value), by the rule of @foliwake/server/config. */
|
|
25
|
+
export declare function checkEnvName(value: unknown, where: string): string;
|
|
26
|
+
/** Parses the text of foliwake.remote.json (version 2) and returns the endpoint of `channel`. */
|
|
27
|
+
export declare function parseRemoteConfig(text: string, channel: RemoteChannel, file?: string): {
|
|
28
|
+
origin: string;
|
|
29
|
+
auth: RemoteAuth | null;
|
|
30
|
+
};
|
|
31
|
+
/** The command-line choices of remote mode. */
|
|
32
|
+
export interface RemoteSelection {
|
|
33
|
+
channel: RemoteChannel;
|
|
34
|
+
/** --remote-url: no file is read. */
|
|
35
|
+
url?: string;
|
|
36
|
+
/** --remote-auth-env (with --remote-auth-user). */
|
|
37
|
+
authEnv?: string;
|
|
38
|
+
authUser?: string;
|
|
39
|
+
/** --remote-config: another endpoints file (relative to cwd). */
|
|
40
|
+
configFile?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Resolves the target from the command line and, without --remote-url, from a config file:
|
|
44
|
+
* --remote-config beats FOLIWAKE_REMOTE beats <cwd>/foliwake.remote.json.
|
|
45
|
+
*/
|
|
46
|
+
export declare function resolveRemoteTarget(selection: RemoteSelection, cwd: string, env?: NodeJS.ProcessEnv): Promise<RemoteTarget>;
|
|
47
|
+
/** The credential of a target, read from the environment: kept in memory only. */
|
|
48
|
+
export interface RemoteCredential {
|
|
49
|
+
/** The Authorization header value, or null for an unprotected target. */
|
|
50
|
+
authorization: string | null;
|
|
51
|
+
/** Strings that must never appear in output (the password and its encodings). */
|
|
52
|
+
secrets: string[];
|
|
53
|
+
}
|
|
54
|
+
/** Reads the password named by the target; refuses to start when it is missing or empty. */
|
|
55
|
+
export declare function readRemoteCredential(target: RemoteTarget, env: NodeJS.ProcessEnv): RemoteCredential;
|
|
56
|
+
/** Replaces every secret (and its encodings) in a text with [redacted]. */
|
|
57
|
+
export declare function redactor(secrets: readonly string[]): (text: string) => string;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// `foliwake dev --remote`: which deployed site supplies the content, and the
|
|
2
|
+
// credential used for it.
|
|
3
|
+
//
|
|
4
|
+
// The endpoints and the NAMES of the environment variables that hold
|
|
5
|
+
// passwords come from a committed, secret-free `foliwake.remote.json` in the
|
|
6
|
+
// frontend repository (or from --remote-url / --remote-auth-env /
|
|
7
|
+
// --remote-auth-user). The password itself is read from the environment once,
|
|
8
|
+
// at start, and stays in the dev server process: it is never printed, logged,
|
|
9
|
+
// written to a file or sent to the browser. Every message passes through
|
|
10
|
+
// redactor() as a second line of defence.
|
|
11
|
+
import { readFile } from 'node:fs/promises';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { CONFIG_ENV, FoliwakeConfigError, checkEnvName as checkEnvNameV2, checkUsername, endpointOf, isLoopbackName, parseFoliwakeConfig, parseRemoteUrl, } from '@foliwake/server/config';
|
|
14
|
+
// One parser for the CLI and the app packages (@foliwake/server/config, config version 2).
|
|
15
|
+
export { isLoopbackName, parseRemoteUrl };
|
|
16
|
+
export const REMOTE_CONFIG_FILE = 'foliwake.remote.json';
|
|
17
|
+
export const REMOTE_CHANNELS = ['preview', 'live'];
|
|
18
|
+
/** A configuration problem; the message is shown as is (it never contains a secret). */
|
|
19
|
+
export const RemoteConfigError = FoliwakeConfigError;
|
|
20
|
+
/** Validates an environment variable NAME (never a value), by the rule of @foliwake/server/config. */
|
|
21
|
+
export function checkEnvName(value, where) {
|
|
22
|
+
return checkEnvNameV2(value, where);
|
|
23
|
+
}
|
|
24
|
+
/** Why `foliwake dev --remote` cannot read with a service token, and what to use instead. */
|
|
25
|
+
function serviceTokenRefusal(file, channel) {
|
|
26
|
+
return new RemoteConfigError(`${file} "${channel}": foliwake dev --remote reads with "basic" auth only; ` +
|
|
27
|
+
'"service-token" is for an app server (@foliwake/server). ' +
|
|
28
|
+
`Point --remote-config at a file whose ${channel} endpoint uses "basic", ` +
|
|
29
|
+
'or pass --remote-url with --remote-auth-user and --remote-auth-env.');
|
|
30
|
+
}
|
|
31
|
+
/** Parses the text of foliwake.remote.json (version 2) and returns the endpoint of `channel`. */
|
|
32
|
+
export function parseRemoteConfig(text, channel, file = REMOTE_CONFIG_FILE) {
|
|
33
|
+
const config = parseFoliwakeConfig(text, { file, requireApp: false });
|
|
34
|
+
const { origin, auth } = endpointOf(config, channel);
|
|
35
|
+
if (auth?.type === 'service-token')
|
|
36
|
+
throw serviceTokenRefusal(file, channel);
|
|
37
|
+
return { origin, auth: auth ? { ...auth } : null };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Resolves the target from the command line and, without --remote-url, from a config file:
|
|
41
|
+
* --remote-config beats FOLIWAKE_REMOTE beats <cwd>/foliwake.remote.json.
|
|
42
|
+
*/
|
|
43
|
+
export async function resolveRemoteTarget(selection, cwd, env = process.env) {
|
|
44
|
+
const { channel } = selection;
|
|
45
|
+
if (selection.url !== undefined) {
|
|
46
|
+
const origin = parseRemoteUrl(selection.url, '--remote-url');
|
|
47
|
+
const auth = selection.authEnv === undefined
|
|
48
|
+
? null
|
|
49
|
+
: {
|
|
50
|
+
type: 'basic',
|
|
51
|
+
username: checkUsername(selection.authUser, '--remote-auth-user'),
|
|
52
|
+
passwordEnv: checkEnvName(selection.authEnv, '--remote-auth-env'),
|
|
53
|
+
};
|
|
54
|
+
return { channel, origin, auth, from: '--remote-url' };
|
|
55
|
+
}
|
|
56
|
+
const named = selection.configFile ?? (env[CONFIG_ENV] || undefined);
|
|
57
|
+
const option = selection.configFile !== undefined ? '--remote-config' : CONFIG_ENV;
|
|
58
|
+
const file = path.resolve(cwd, named ?? REMOTE_CONFIG_FILE);
|
|
59
|
+
const name = named ?? REMOTE_CONFIG_FILE;
|
|
60
|
+
let text;
|
|
61
|
+
try {
|
|
62
|
+
text = await readFile(file, 'utf8');
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (error.code === 'ENOENT') {
|
|
66
|
+
if (named !== undefined)
|
|
67
|
+
throw new RemoteConfigError(`${option}: ${file} does not exist.`);
|
|
68
|
+
throw new RemoteConfigError(`No ${REMOTE_CONFIG_FILE} in ${cwd}. Run the command in the frontend checkout that has one, or pass ` +
|
|
69
|
+
`--remote-config <file> or --remote-url <https url> (and --remote-auth-user <name> --remote-auth-env ` +
|
|
70
|
+
`<NAME> for a protected site).`);
|
|
71
|
+
}
|
|
72
|
+
throw new RemoteConfigError(`Cannot read ${name}: ${error.message}`);
|
|
73
|
+
}
|
|
74
|
+
const { origin, auth } = parseRemoteConfig(text, channel, name);
|
|
75
|
+
return { channel, origin, auth, from: name };
|
|
76
|
+
}
|
|
77
|
+
/** Reads the password named by the target; refuses to start when it is missing or empty. */
|
|
78
|
+
export function readRemoteCredential(target, env) {
|
|
79
|
+
if (!target.auth)
|
|
80
|
+
return { authorization: null, secrets: [] };
|
|
81
|
+
const { username, passwordEnv } = target.auth;
|
|
82
|
+
const password = env[passwordEnv];
|
|
83
|
+
if (password === undefined || password === '') {
|
|
84
|
+
throw new RemoteConfigError(`${passwordEnv} is not set. ${target.channel} at ${target.origin} needs the password of user ` +
|
|
85
|
+
`"${username}" in the environment variable ${passwordEnv} ` +
|
|
86
|
+
`(for example \`set -a; . <file outside Git>; set +a\`). Nothing was started; the value is never printed.`);
|
|
87
|
+
}
|
|
88
|
+
const encoded = Buffer.from(`${username}:${password}`, 'utf8').toString('base64');
|
|
89
|
+
return { authorization: `Basic ${encoded}`, secrets: [password, encoded, encodeURIComponent(password)] };
|
|
90
|
+
}
|
|
91
|
+
/** Replaces every secret (and its encodings) in a text with [redacted]. */
|
|
92
|
+
export function redactor(secrets) {
|
|
93
|
+
const list = [...new Set(secrets.filter((s) => s.length > 0))].sort((a, b) => b.length - a.length);
|
|
94
|
+
return (text) => {
|
|
95
|
+
let out = String(text);
|
|
96
|
+
for (const secret of list)
|
|
97
|
+
out = out.split(secret).join('[redacted]');
|
|
98
|
+
return out;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { type ReloadHub } from './dev-reload.js';
|
|
3
|
+
import type { RemoteTarget } from './remote-config.js';
|
|
4
|
+
/** `--watch` options: debounce, polling or native events, and the polling interval. */
|
|
5
|
+
export interface LiveReloadOptions {
|
|
6
|
+
debounceMs?: number;
|
|
7
|
+
polling?: boolean;
|
|
8
|
+
codePollingIntervalMs?: number;
|
|
9
|
+
}
|
|
10
|
+
type FetchLike = (input: URL, init: RequestInit) => Promise<Response>;
|
|
11
|
+
export interface RemoteDevServerOptions {
|
|
12
|
+
cwd: string;
|
|
13
|
+
target: RemoteTarget;
|
|
14
|
+
/** The Authorization header value (from the environment), or null. */
|
|
15
|
+
authorization: string | null;
|
|
16
|
+
/** Applied to every message that leaves the process (terminal and browser). */
|
|
17
|
+
redact?: (text: string) => string;
|
|
18
|
+
log?: (text: string) => void;
|
|
19
|
+
reload?: ReloadHub;
|
|
20
|
+
/** Replaces global fetch (tests). */
|
|
21
|
+
fetch?: FetchLike;
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
}
|
|
24
|
+
export type RemoteDevServer = http.Server & {
|
|
25
|
+
reload?: ReloadHub;
|
|
26
|
+
stopped?: Promise<void>;
|
|
27
|
+
};
|
|
28
|
+
export declare function createRemoteDevServer({ cwd, target, authorization, redact, log, reload, fetch: fetchImpl, timeoutMs, }: RemoteDevServerOptions): RemoteDevServer;
|
|
29
|
+
/** startRemoteDevServer() options. */
|
|
30
|
+
export interface StartRemoteDevServerOptions extends Omit<RemoteDevServerOptions, 'reload'> {
|
|
31
|
+
port: number;
|
|
32
|
+
/** `--watch`: reload open pages after changes of local code (components/, scripts/, styles/). */
|
|
33
|
+
liveReload?: boolean | LiveReloadOptions;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Starts remote mode on 127.0.0.1. With `liveReload`, only local code is
|
|
37
|
+
* watched (the remote site is never polled); a reload fetches the page again.
|
|
38
|
+
* server.close() stops the event streams at once and the watchers after it.
|
|
39
|
+
*/
|
|
40
|
+
export declare function startRemoteDevServer({ port, liveReload, ...options }: StartRemoteDevServerOptions): Promise<RemoteDevServer>;
|
|
41
|
+
export {};
|