nylorun 0.6.0-beta → 0.8.0-beta
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 +61 -0
- package/README.md +77 -73
- package/dist/cli.js +7 -2
- package/dist/doctor.d.ts +2 -4
- package/dist/doctor.js +12 -19
- package/dist/sandbox/cluster-file.d.ts +47 -0
- package/dist/sandbox/cluster-file.js +55 -0
- package/dist/sandbox/commands.d.ts +41 -0
- package/dist/sandbox/commands.js +281 -0
- package/dist/sandbox/kubectl.d.ts +39 -0
- package/dist/sandbox/kubectl.js +70 -0
- package/dist/sandbox/manifests.d.ts +41 -0
- package/dist/sandbox/manifests.js +138 -0
- package/dist/sandbox/probe.d.ts +44 -0
- package/dist/sandbox/probe.js +137 -0
- package/dist/stack/commands.d.ts +56 -9
- package/dist/stack/commands.js +167 -123
- package/dist/stack/compose-file.d.ts +42 -5
- package/dist/stack/compose-file.js +208 -21
- package/dist/stack/env-file.d.ts +48 -6
- package/dist/stack/env-file.js +81 -9
- package/dist/stack/host-files.js +11 -1
- package/dist/stack/images.d.ts +8 -4
- package/dist/stack/images.js +8 -3
- package/dist/stack/index.d.ts +4 -2
- package/dist/stack/index.js +7 -3
- package/dist/stack/paths.d.ts +15 -0
- package/dist/stack/paths.js +7 -0
- package/dist/stack/ports.d.ts +0 -2
- package/dist/stack/ports.js +0 -2
- package/dist/stack/prepare.d.ts +10 -4
- package/dist/stack/prepare.js +20 -3
- package/dist/stack/sandbox.d.ts +4 -0
- package/dist/stack/sandbox.js +106 -0
- package/dist/stack/stacks.d.ts +8 -0
- package/dist/stack/stacks.js +19 -0
- package/dist/stack/studio-login.d.ts +2 -2
- package/dist/stack/studio-login.js +2 -2
- package/package.json +5 -5
- package/dist/stack/proxy.d.ts +0 -78
- package/dist/stack/proxy.js +0 -224
package/dist/stack/proxy.js
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { parseEnvLines } from "./env-file.js";
|
|
6
|
-
import { PINNED_IMAGES } from "./images.js";
|
|
7
|
-
import { choosePort, DEFAULT_PORTS } from "./ports.js";
|
|
8
|
-
/**
|
|
9
|
-
* The Studio proxy: one small Caddy container per machine that gives each local Tenant's
|
|
10
|
-
* Studio the address `http://<name>.localhost:<port>`. Browsers keep cookies per host, not
|
|
11
|
-
* per port, so separate hosts keep the Studios' sessions apart. It routes browsers to Studio
|
|
12
|
-
* only (programs keep the Runtime's `http://localhost:<port>`: Node's resolver need not
|
|
13
|
-
* resolve `*.localhost`) and holds no Tenant data: its files are `~/.nylorun/proxy/`
|
|
14
|
-
* (`compose.yaml`, `Caddyfile`, `.env`), and Caddy's own state is tmpfs.
|
|
15
|
-
*
|
|
16
|
-
* The proxy joins each Tenant's network (named after its Compose project) and reaches Studio
|
|
17
|
-
* by container name, `<project>-studio:3000`. Docker cannot remove a network the proxy is
|
|
18
|
-
* attached to, so `reset` and `delete` disconnect it first.
|
|
19
|
-
*/
|
|
20
|
-
export const PROXY_PROJECT = "nylorun-proxy";
|
|
21
|
-
export const PROXY_CONTAINER = "nylorun-proxy";
|
|
22
|
-
export function proxyPaths(base) {
|
|
23
|
-
const dir = join(base, "proxy");
|
|
24
|
-
return {
|
|
25
|
-
dir,
|
|
26
|
-
compose: join(dir, "compose.yaml"),
|
|
27
|
-
caddyfile: join(dir, "Caddyfile"),
|
|
28
|
-
env: join(dir, ".env"),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
/** `NYLORUN_PROXY_DISABLED=1`: no proxy; Studio is `http://localhost:<port>`. */
|
|
32
|
-
export function proxyDisabled(env) {
|
|
33
|
-
return /^(1|true)$/i.test(env.NYLORUN_PROXY_DISABLED?.trim() ?? "");
|
|
34
|
-
}
|
|
35
|
-
export function proxyOrigin(name, port) {
|
|
36
|
-
return `http://${name}.localhost:${port}`;
|
|
37
|
-
}
|
|
38
|
-
export async function readProxySettings(base) {
|
|
39
|
-
let text;
|
|
40
|
-
try {
|
|
41
|
-
text = await readFile(proxyPaths(base).env, "utf8");
|
|
42
|
-
}
|
|
43
|
-
catch {
|
|
44
|
-
return undefined;
|
|
45
|
-
}
|
|
46
|
-
const values = parseEnvLines(text);
|
|
47
|
-
const port = Number(values.get("NYLORUN_PROXY_PORT"));
|
|
48
|
-
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
49
|
-
return undefined;
|
|
50
|
-
return { port, ipv6: values.get("NYLORUN_PROXY_IPV6") !== "false" };
|
|
51
|
-
}
|
|
52
|
-
async function writeAtomic(path, text) {
|
|
53
|
-
const temporary = `${path}.${randomBytes(8).toString("hex")}.tmp`;
|
|
54
|
-
await writeFile(temporary, text, { mode: 0o644 });
|
|
55
|
-
await rename(temporary, path);
|
|
56
|
-
}
|
|
57
|
-
async function writeProxySettings(base, settings) {
|
|
58
|
-
const paths = proxyPaths(base);
|
|
59
|
-
await mkdir(paths.dir, { recursive: true, mode: 0o755 });
|
|
60
|
-
await writeAtomic(paths.env, [
|
|
61
|
-
"# Written by `nylorun start`: the Studio proxy's port, kept from its first start,",
|
|
62
|
-
"# and whether it is also published on [::1].",
|
|
63
|
-
`NYLORUN_PROXY_PORT=${settings.port}`,
|
|
64
|
-
`NYLORUN_PROXY_IPV6=${settings.ipv6}`,
|
|
65
|
-
"",
|
|
66
|
-
].join("\n"));
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* The proxy's settings. On first use its port is 4160 when free, else a free one outside
|
|
70
|
-
* `taken`; it is kept after that.
|
|
71
|
-
*/
|
|
72
|
-
export async function ensureProxySettings(base, ports, taken) {
|
|
73
|
-
const existing = await readProxySettings(base);
|
|
74
|
-
if (existing)
|
|
75
|
-
return existing;
|
|
76
|
-
const settings = { port: await choosePort(ports, DEFAULT_PORTS.proxy, undefined, taken), ipv6: true };
|
|
77
|
-
await writeProxySettings(base, settings);
|
|
78
|
-
return settings;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* One site per Tenant. Site addresses carry no port: Caddy listens on 80 in the container,
|
|
82
|
-
* and its host match ignores the published port in the browser's Host. The browser's Host
|
|
83
|
-
* (`<name>.localhost:<port>`) reaches Studio unchanged, which Studio checks; a Tenant that is
|
|
84
|
-
* not running answers 502, and an unknown host 404.
|
|
85
|
-
*/
|
|
86
|
-
export function renderCaddyfile(routes) {
|
|
87
|
-
const sites = routes.map(({ name, project }) => `http://${name}.localhost {
|
|
88
|
-
reverse_proxy ${project}-studio:3000
|
|
89
|
-
handle_errors {
|
|
90
|
-
respond \`Tenant ${name} is not running. Run "nylorun start --tenant ${name}".\` 502
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
`);
|
|
94
|
-
return [
|
|
95
|
-
`# Written by \`nylorun start\`: a site per Tenant under ~/.nylorun/tenants. Rewritten on every start.
|
|
96
|
-
{
|
|
97
|
-
auto_https off
|
|
98
|
-
admin localhost:2019 # inside the container only; \`caddy reload\` uses it
|
|
99
|
-
}
|
|
100
|
-
`,
|
|
101
|
-
...sites,
|
|
102
|
-
`:80 {
|
|
103
|
-
respond \`No Tenant at {host}. "nylorun ls" lists the Tenants on this machine.\` 404
|
|
104
|
-
}
|
|
105
|
-
`,
|
|
106
|
-
].join("\n");
|
|
107
|
-
}
|
|
108
|
-
export function renderProxyCompose(ipv6) {
|
|
109
|
-
return `# Written by \`nylorun start\`; rewritten on every start. The port lives in .env.
|
|
110
|
-
name: ${PROXY_PROJECT}
|
|
111
|
-
|
|
112
|
-
services:
|
|
113
|
-
proxy: # http://<tenant>.localhost:<port>: every local Tenant's Studio
|
|
114
|
-
image: ${PINNED_IMAGES.proxy}
|
|
115
|
-
container_name: ${PROXY_CONTAINER}
|
|
116
|
-
labels:
|
|
117
|
-
dev.nylorun.proxy: "true"
|
|
118
|
-
volumes:
|
|
119
|
-
- .:/etc/caddy:ro # this directory: the Caddyfile, reloaded with \`caddy reload\`
|
|
120
|
-
tmpfs: [/config, /data] # Caddy's own state; nothing to keep
|
|
121
|
-
ports:
|
|
122
|
-
- "127.0.0.1:\${NYLORUN_PROXY_PORT:?run nylorun start}:80"${ipv6
|
|
123
|
-
? `
|
|
124
|
-
- "[::1]:\${NYLORUN_PROXY_PORT:?run nylorun start}:80" # macOS resolves <name>.localhost to ::1 only`
|
|
125
|
-
: ""}
|
|
126
|
-
restart: unless-stopped
|
|
127
|
-
|
|
128
|
-
networks:
|
|
129
|
-
default:
|
|
130
|
-
name: ${PROXY_PROJECT}
|
|
131
|
-
labels:
|
|
132
|
-
dev.nylorun.proxy: "true"
|
|
133
|
-
`;
|
|
134
|
-
}
|
|
135
|
-
function proxyCompose(paths, ...args) {
|
|
136
|
-
return ["compose", "--project-name", PROXY_PROJECT, "--file", paths.compose, "--env-file", paths.env, ...args];
|
|
137
|
-
}
|
|
138
|
-
function lastLine(text) {
|
|
139
|
-
return text.trim().split(/\r?\n/).at(-1) ?? "";
|
|
140
|
-
}
|
|
141
|
-
/** Attach the proxy to a Tenant's network; "already attached" and "no such network" are fine. */
|
|
142
|
-
async function connectProxy(docker, network) {
|
|
143
|
-
const result = await docker.run(["network", "connect", network, PROXY_CONTAINER]);
|
|
144
|
-
if (result.code === 0 || /already exists|not found|no such network/i.test(result.stderr))
|
|
145
|
-
return undefined;
|
|
146
|
-
return lastLine(result.stderr) || `exit ${result.code}`;
|
|
147
|
-
}
|
|
148
|
-
/** Detach the proxy from a Tenant's network, so Compose can remove it; never fails. */
|
|
149
|
-
export async function disconnectProxy(docker, network) {
|
|
150
|
-
await docker.run(["network", "disconnect", "--force", network, PROXY_CONTAINER]);
|
|
151
|
-
}
|
|
152
|
-
async function reloadProxy(docker) {
|
|
153
|
-
return await docker.run(["exec", PROXY_CONTAINER, "caddy", "reload", "--config", "/etc/caddy/Caddyfile"]);
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Write the routes, bring the proxy up (Compose recreates it when its file changed), attach
|
|
157
|
-
* it to every routed Tenant's network and reload Caddy. Never throws: a problem is warned
|
|
158
|
-
* once and the caller shows Studio's own port. When Docker refuses `[::1]` (IPv6 off), the
|
|
159
|
-
* proxy listens on 127.0.0.1 only, and says so once.
|
|
160
|
-
*/
|
|
161
|
-
export async function startProxy(input) {
|
|
162
|
-
const { docker, base, err } = input;
|
|
163
|
-
const paths = proxyPaths(base);
|
|
164
|
-
const warn = (problem) => err(`Warning: the Studio proxy is not available (${problem}); Studio is on its own port. See "nylorun doctor".`);
|
|
165
|
-
try {
|
|
166
|
-
await writeAtomic(paths.caddyfile, renderCaddyfile(input.routes));
|
|
167
|
-
await writeAtomic(paths.compose, renderProxyCompose(input.settings.ipv6));
|
|
168
|
-
}
|
|
169
|
-
catch (error) {
|
|
170
|
-
warn(error instanceof Error ? error.message : String(error));
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
let up = await docker.run(proxyCompose(paths, "up", "--detach"));
|
|
174
|
-
if (up.code !== 0 && input.settings.ipv6) {
|
|
175
|
-
await writeAtomic(paths.compose, renderProxyCompose(false));
|
|
176
|
-
const ipv4 = await docker.run(proxyCompose(paths, "up", "--detach"));
|
|
177
|
-
if (ipv4.code === 0) {
|
|
178
|
-
await writeProxySettings(base, { ...input.settings, ipv6: false });
|
|
179
|
-
err(`Docker refused the Studio proxy on [::1]:${input.settings.port} (${lastLine(up.stderr)}); it listens on 127.0.0.1 only, so a browser that resolves *.localhost to ::1 alone does not reach it.`);
|
|
180
|
-
}
|
|
181
|
-
up = ipv4;
|
|
182
|
-
}
|
|
183
|
-
if (up.code !== 0) {
|
|
184
|
-
warn(`docker compose up: ${lastLine(up.stderr) || `exit ${up.code}`}`);
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
for (const route of input.routes) {
|
|
188
|
-
const problem = await connectProxy(docker, route.project);
|
|
189
|
-
if (problem)
|
|
190
|
-
err(`Warning: the Studio proxy could not join ${route.project} (${problem}).`);
|
|
191
|
-
}
|
|
192
|
-
// A container Compose just created may not take the reload yet.
|
|
193
|
-
for (let attempt = 1;; attempt += 1) {
|
|
194
|
-
const reload = await reloadProxy(docker);
|
|
195
|
-
if (reload.code === 0)
|
|
196
|
-
return true;
|
|
197
|
-
if (attempt === 10) {
|
|
198
|
-
warn(`caddy reload: ${lastLine(reload.stderr) || `exit ${reload.code}`}`);
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
await new Promise((resolve) => setTimeout(resolve, input.pollMs ?? 500));
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
/** Rewrite the routes of an existing proxy and reload it (`delete`); never fails. */
|
|
205
|
-
export async function refreshProxyRoutes(docker, base, routes) {
|
|
206
|
-
const paths = proxyPaths(base);
|
|
207
|
-
if (!existsSync(paths.caddyfile))
|
|
208
|
-
return;
|
|
209
|
-
await writeAtomic(paths.caddyfile, renderCaddyfile(routes));
|
|
210
|
-
await reloadProxy(docker);
|
|
211
|
-
}
|
|
212
|
-
/** Stop the proxy (`stop --all`); true when it stopped. */
|
|
213
|
-
export async function stopProxy(docker) {
|
|
214
|
-
return (await docker.run(["compose", "--project-name", PROXY_PROJECT, "stop"])).code === 0;
|
|
215
|
-
}
|
|
216
|
-
/** The proxy as `nylorun doctor` reports it. */
|
|
217
|
-
export async function readProxyStatus(docker, base, env) {
|
|
218
|
-
if (proxyDisabled(env))
|
|
219
|
-
return { state: "disabled" };
|
|
220
|
-
const settings = await readProxySettings(base);
|
|
221
|
-
const inspect = await docker.run(["inspect", "--format", "{{.State.Status}}", PROXY_CONTAINER]);
|
|
222
|
-
const state = inspect.code !== 0 ? "absent" : inspect.stdout.trim() === "running" ? "running" : "stopped";
|
|
223
|
-
return { state, ...(settings ? { port: settings.port, ipv6: settings.ipv6 } : {}) };
|
|
224
|
-
}
|