localgate 0.3.0 → 0.5.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/README.md +36 -24
- package/logic/cli/localgateBanner.ts +8 -6
- package/logic/cli/localgateCli.ts +141 -20
- package/logic/client/localgateProxyClient.ts +5 -2
- package/logic/config/localgateAliasStore.ts +147 -0
- package/logic/config/localgateConfigReport.ts +45 -0
- package/logic/config/localgateMachineConfig.ts +25 -3
- package/logic/config/localgateNames.ts +59 -0
- package/logic/proxy/localgateAliasRoute.ts +46 -0
- package/logic/proxy/localgateControlApi.ts +18 -0
- package/logic/proxy/localgateHeaderRewrite.ts +27 -11
- package/logic/proxy/localgateHealth.ts +46 -15
- package/logic/proxy/localgateProxy.ts +11 -3
- package/logic/proxy/localgateProxyHost.ts +13 -2
- package/logic/proxy/localgateRegistry.ts +16 -2
- package/logic/run/localgateEnvRewrite.ts +16 -6
- package/logic/run/localgateRunner.ts +58 -21
- package/package.json +1 -1
|
@@ -6,13 +6,21 @@ import { join } from "node:path";
|
|
|
6
6
|
import { createInterface } from "node:readline/promises";
|
|
7
7
|
import { LocalgateBanner } from "../cli/localgateBanner.ts";
|
|
8
8
|
import { LocalgateProxyClient } from "../client/localgateProxyClient.ts";
|
|
9
|
-
import { LocalgateMachineConfig } from "../config/localgateMachineConfig.ts";
|
|
9
|
+
import { LocalgateMachineConfig, type LocalgateMachineSettings } from "../config/localgateMachineConfig.ts";
|
|
10
|
+
import { LocalgateNames } from "../config/localgateNames.ts";
|
|
10
11
|
import { LocalgateProjectConfig, type LocalgateProjectSettings } from "../config/localgateProjectConfig.ts";
|
|
11
12
|
import type { LocalgateRoute } from "../proxy/localgateRegistry.ts";
|
|
12
13
|
import { LocalgateUrl } from "../proxy/localgateUrl.ts";
|
|
13
14
|
import { LocalgateEnvRewrite } from "./localgateEnvRewrite.ts";
|
|
14
15
|
import { LocalgateProcessTree } from "./localgateProcessTree.ts";
|
|
15
16
|
|
|
17
|
+
type LocalgateRunnerRuntime =
|
|
18
|
+
{
|
|
19
|
+
project: LocalgateProjectSettings;
|
|
20
|
+
machine: LocalgateMachineSettings | null;
|
|
21
|
+
names: string[];
|
|
22
|
+
};
|
|
23
|
+
|
|
16
24
|
// Runs inside the editor's debug terminal and owns the dev process, which is the part an editor cannot
|
|
17
25
|
// give away: the terminal keeps the debugger attached, while `localgate restart` swaps the child
|
|
18
26
|
// underneath without touching the terminal, the route or the port.
|
|
@@ -25,6 +33,7 @@ export class LocalgateRunner
|
|
|
25
33
|
private child: ChildProcess | null = null;
|
|
26
34
|
private controlServer: Server | null = null;
|
|
27
35
|
private route: LocalgateRoute | null = null;
|
|
36
|
+
private runtime: LocalgateRunnerRuntime | null = null;
|
|
28
37
|
private readonly logs: string[] = [];
|
|
29
38
|
private stopping = false;
|
|
30
39
|
private restarting = false;
|
|
@@ -97,6 +106,11 @@ export class LocalgateRunner
|
|
|
97
106
|
return command.map(part => /[\s"]/.test(part) ? `"${part.replace(/"/g, '\\"')}"` : part).join(" ");
|
|
98
107
|
}
|
|
99
108
|
|
|
109
|
+
static routeRegistrationMatches(route: LocalgateRoute, registered: LocalgateRoute | null, runnerPid: number): boolean
|
|
110
|
+
{
|
|
111
|
+
return registered?.runnerPid == runnerPid && registered.controlUrl == route.controlUrl;
|
|
112
|
+
}
|
|
113
|
+
|
|
100
114
|
static async freePort(): Promise<number>
|
|
101
115
|
{
|
|
102
116
|
return new Promise<number>((resolve, reject) =>
|
|
@@ -122,27 +136,21 @@ export class LocalgateRunner
|
|
|
122
136
|
{
|
|
123
137
|
if (this.command.length == 0) throw new Error("localgate run needs a command, for example: localgate run npm run dev");
|
|
124
138
|
|
|
125
|
-
const
|
|
139
|
+
const runtime = this.loadRuntime();
|
|
126
140
|
|
|
127
|
-
const blocked = await this.clearPredecessor(project);
|
|
141
|
+
const blocked = await this.clearPredecessor(runtime.project);
|
|
128
142
|
if (blocked != 0) return blocked;
|
|
129
143
|
|
|
130
|
-
const machine = LocalgateMachineConfig.load();
|
|
131
|
-
const external = machine && project.mode != "local" ? LocalgateMachineConfig.externalSuffix(machine) : null;
|
|
132
|
-
|
|
133
|
-
const names = [`${project.name}.localhost`];
|
|
134
|
-
if (external) names.push(`${project.name}${external}`);
|
|
135
|
-
|
|
136
144
|
const port = await LocalgateRunner.freePort();
|
|
137
145
|
const controlUrl = await this.startControlServer();
|
|
138
146
|
|
|
139
147
|
await LocalgateProxyClient.ensureRunning();
|
|
140
148
|
const route = await LocalgateProxyClient.register({
|
|
141
|
-
names,
|
|
149
|
+
names: runtime.names,
|
|
142
150
|
port,
|
|
143
151
|
kind: "app",
|
|
144
|
-
mode: project.mode,
|
|
145
|
-
cwd: project.packageDirectory,
|
|
152
|
+
mode: runtime.project.mode,
|
|
153
|
+
cwd: runtime.project.packageDirectory,
|
|
146
154
|
command: this.command.join(" "),
|
|
147
155
|
controlUrl,
|
|
148
156
|
runnerPid: process.pid,
|
|
@@ -150,13 +158,14 @@ export class LocalgateRunner
|
|
|
150
158
|
debuggerAttached: LocalgateRunner.debuggerAttached()
|
|
151
159
|
});
|
|
152
160
|
this.route = route;
|
|
161
|
+
this.runtime = runtime;
|
|
153
162
|
|
|
154
163
|
process.stdout.write(LocalgateBanner.render(route, LocalgateProxyClient.proxyPort()));
|
|
155
164
|
|
|
156
165
|
this.installSignalHandlers();
|
|
157
166
|
const heartbeat = this.startHeartbeat();
|
|
158
167
|
|
|
159
|
-
const finished = this.spawnChild(port
|
|
168
|
+
const finished = this.spawnChild(port);
|
|
160
169
|
await finished;
|
|
161
170
|
|
|
162
171
|
clearInterval(heartbeat);
|
|
@@ -295,12 +304,16 @@ export class LocalgateRunner
|
|
|
295
304
|
throw new Error(`${route.names[0]} still holds 127.0.0.1:${route.port} - stop it by hand`);
|
|
296
305
|
}
|
|
297
306
|
|
|
298
|
-
private spawnChild(port: number
|
|
307
|
+
private spawnChild(port: number): Promise<void>
|
|
299
308
|
{
|
|
300
|
-
const
|
|
309
|
+
const runtime = this.runtime;
|
|
310
|
+
if (!runtime) throw new Error("localgate runtime is not configured");
|
|
311
|
+
|
|
312
|
+
const scripts = LocalgateRunner.readScripts(runtime.project.packageDirectory);
|
|
301
313
|
const command = LocalgateRunner.withPortFlag(this.command, scripts, port);
|
|
302
314
|
|
|
303
|
-
const env = LocalgateEnvRewrite.apply(process.env,
|
|
315
|
+
const env = LocalgateEnvRewrite.apply(process.env, runtime.project.mode, runtime.machine,
|
|
316
|
+
LocalgateProxyClient.proxyPort());
|
|
304
317
|
env.PORT = String(port);
|
|
305
318
|
|
|
306
319
|
// One command string rather than a command plus an args array: `npm run dev` on Windows is a .cmd,
|
|
@@ -320,7 +333,10 @@ export class LocalgateRunner
|
|
|
320
333
|
});
|
|
321
334
|
|
|
322
335
|
this.child = child;
|
|
323
|
-
if (this.route)
|
|
336
|
+
if (this.route)
|
|
337
|
+
void LocalgateProxyClient.patch(this.route.id, { childPid: child.pid ?? null })
|
|
338
|
+
.then(route => { this.route = route; })
|
|
339
|
+
.catch(() => {});
|
|
324
340
|
|
|
325
341
|
child.stdout?.on("data", chunk => this.absorb(chunk as Buffer, process.stdout));
|
|
326
342
|
child.stderr?.on("data", chunk => this.absorb(chunk as Buffer, process.stderr));
|
|
@@ -334,7 +350,7 @@ export class LocalgateRunner
|
|
|
334
350
|
if (this.restarting)
|
|
335
351
|
{
|
|
336
352
|
this.restarting = false;
|
|
337
|
-
resolve(this.spawnChild(port
|
|
353
|
+
resolve(this.spawnChild(port));
|
|
338
354
|
return;
|
|
339
355
|
}
|
|
340
356
|
|
|
@@ -349,6 +365,12 @@ export class LocalgateRunner
|
|
|
349
365
|
const child = this.child;
|
|
350
366
|
if (!child?.pid || !this.route) throw new Error("nothing to restart");
|
|
351
367
|
|
|
368
|
+
const runtime = this.loadRuntime();
|
|
369
|
+
this.route = await LocalgateProxyClient.patch(this.route.id, {
|
|
370
|
+
names: runtime.names,
|
|
371
|
+
mode: runtime.project.mode
|
|
372
|
+
});
|
|
373
|
+
this.runtime = runtime;
|
|
352
374
|
this.restarting = true;
|
|
353
375
|
await LocalgateProcessTree.killTree(child.pid, this.route.port);
|
|
354
376
|
}
|
|
@@ -422,8 +444,9 @@ export class LocalgateRunner
|
|
|
422
444
|
});
|
|
423
445
|
}
|
|
424
446
|
|
|
425
|
-
// The proxy holds the route table in memory, so if it dies the route dies with it.
|
|
426
|
-
//
|
|
447
|
+
// The proxy holds the route table in memory, so if it dies the route dies with it. Another runner may
|
|
448
|
+
// start the proxy before this heartbeat, so a successful ping is not enough: this runner verifies that
|
|
449
|
+
// its own route is present and re-registers when it is missing.
|
|
427
450
|
private startHeartbeat(): NodeJS.Timeout
|
|
428
451
|
{
|
|
429
452
|
const timer = setInterval(() =>
|
|
@@ -431,7 +454,13 @@ export class LocalgateRunner
|
|
|
431
454
|
void (async () =>
|
|
432
455
|
{
|
|
433
456
|
if (this.stopping || !this.route) return;
|
|
434
|
-
|
|
457
|
+
|
|
458
|
+
const registered = await LocalgateProxyClient.resolve({ name: this.route.names[0] }).catch(() => null);
|
|
459
|
+
if (LocalgateRunner.routeRegistrationMatches(this.route, registered, process.pid))
|
|
460
|
+
{
|
|
461
|
+
this.route = registered;
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
435
464
|
|
|
436
465
|
try
|
|
437
466
|
{
|
|
@@ -486,6 +515,7 @@ export class LocalgateRunner
|
|
|
486
515
|
{
|
|
487
516
|
await LocalgateProxyClient.deregister(this.route.id).catch(() => {});
|
|
488
517
|
this.route = null;
|
|
518
|
+
this.runtime = null;
|
|
489
519
|
}
|
|
490
520
|
|
|
491
521
|
if (this.controlServer)
|
|
@@ -508,4 +538,11 @@ export class LocalgateRunner
|
|
|
508
538
|
return {};
|
|
509
539
|
}
|
|
510
540
|
}
|
|
541
|
+
|
|
542
|
+
private loadRuntime(): LocalgateRunnerRuntime
|
|
543
|
+
{
|
|
544
|
+
const project = LocalgateProjectConfig.load(this.directory);
|
|
545
|
+
const machine = LocalgateMachineConfig.load();
|
|
546
|
+
return { project, machine, names: LocalgateNames.routeNames(project.name, project.mode, machine) };
|
|
547
|
+
}
|
|
511
548
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "localgate",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Stable names for local dev servers: one port 80 multiplexed by Host, plus an API that can restart a dev server owned by your editor.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|