@histoire/dev-proxy 1.0.0-beta.2 → 1.0.0-beta.3
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/dist/config/watcher.js +6 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +38 -7
- package/dist/process/child.d.ts +3 -1
- package/dist/process/child.js +6 -0
- package/dist/protocol.d.ts +17 -1
- package/dist/protocol.js +2 -0
- package/dist/server/http.d.ts +3 -1
- package/dist/server/http.js +15 -6
- package/dist/server/listener.d.ts +2 -0
- package/dist/server/listener.js +24 -5
- package/package.json +1 -1
package/dist/config/watcher.js
CHANGED
|
@@ -42,7 +42,12 @@ export async function watchConfiguration(inputs, changed, onError = console.erro
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
if (added.size) {
|
|
45
|
-
const watcher = chokidar.watch([...added], {
|
|
45
|
+
const watcher = chokidar.watch([...added], {
|
|
46
|
+
ignoreInitial: true,
|
|
47
|
+
depth: 0,
|
|
48
|
+
// Chokidar's default change throttle can drop a save just after a notification.
|
|
49
|
+
awaitWriteFinish: { stabilityThreshold: 50, pollInterval: 10 },
|
|
50
|
+
});
|
|
46
51
|
watchers.set(watcher, added);
|
|
47
52
|
watcher.on('add', inspect).on('change', inspect).on('unlink', inspect).on('error', onError);
|
|
48
53
|
// Every new dependency directory gets a scan barrier before reconciliation;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,11 +7,11 @@ export interface DevProxyOptions {
|
|
|
7
7
|
root?: string;
|
|
8
8
|
/** Explicit config path relative to root. */
|
|
9
9
|
config?: string;
|
|
10
|
-
/** Public port; zero asks OS for an ephemeral port. */
|
|
10
|
+
/** Public port; zero asks OS for an ephemeral port. Overrides the port project config asks for. */
|
|
11
11
|
port?: number;
|
|
12
|
-
/** Public interface, default localhost. */
|
|
12
|
+
/** Public interface, default localhost. Overrides the host project config asks for. */
|
|
13
13
|
host?: string | boolean;
|
|
14
|
-
/** Open public URL once
|
|
14
|
+
/** Open public URL once its address is settled. */
|
|
15
15
|
open?: boolean;
|
|
16
16
|
/** Framework runtime executable owned by proxy. */
|
|
17
17
|
runtime: URL;
|
|
@@ -21,7 +21,7 @@ export interface DevProxyOptions {
|
|
|
21
21
|
token?: string;
|
|
22
22
|
/** Graceful child winddown budget; default 10000ms. */
|
|
23
23
|
shutdownTimeout?: number;
|
|
24
|
-
/** Observe public listener before project work begins. */
|
|
24
|
+
/** Observe public listener before project work begins, and again whenever project config moves it. */
|
|
25
25
|
onListening?: (url: string) => void;
|
|
26
26
|
/** Optional terminal/application observation; receives detached browser-safe state. */
|
|
27
27
|
onStatus?: (state: Readonly<PublicState>) => void;
|
|
@@ -48,8 +48,8 @@ export interface DevProxyOptions {
|
|
|
48
48
|
}
|
|
49
49
|
/** Bind first; child startup and project imports happen after handle is available. */
|
|
50
50
|
export declare function startDevProxy(options: DevProxyOptions): Promise<{
|
|
51
|
-
/** Actual public address, including OS-selected or fallback port. */
|
|
52
|
-
url: string;
|
|
51
|
+
/** Actual public address, including OS-selected or fallback port; project config can move it. */
|
|
52
|
+
readonly url: string;
|
|
53
53
|
/** Initial runtime acquisition; watcher remains live after rejection. */
|
|
54
54
|
ready: Promise<void>;
|
|
55
55
|
/** Latest public availability, independent of any runtime process. */
|
package/dist/index.js
CHANGED
|
@@ -4,20 +4,48 @@ import { inspect } from 'node:util';
|
|
|
4
4
|
import { createRestartCoordinator } from './lifecycle/index.js';
|
|
5
5
|
import { startRuntimeChild } from './process/child.js';
|
|
6
6
|
import { createProxyHttp } from './server/http.js';
|
|
7
|
-
import { listen } from './server/listener.js';
|
|
7
|
+
import { assertValidPort, listen } from './server/listener.js';
|
|
8
|
+
/** Public port used when neither the command line nor project config names one. */
|
|
9
|
+
const DEFAULT_PORT = 6006;
|
|
8
10
|
/** Bind first; child startup and project imports happen after handle is available. */
|
|
9
11
|
export async function startDevProxy(options) {
|
|
10
12
|
const root = resolve(options.root ?? process.cwd());
|
|
11
13
|
let state = { status: 'starting', generation: randomUUID(), base: '/' };
|
|
12
|
-
const http = createProxyHttp(() => state, () => restart({ cause: 'browser' }), options.onRequest);
|
|
13
14
|
let url;
|
|
15
|
+
let bound = { port: options.port ?? DEFAULT_PORT, host: options.host };
|
|
16
|
+
const http = createProxyHttp(() => state, () => restart({ cause: 'browser' }), options.onRequest, () => url ? new URL(url).hostname : undefined);
|
|
14
17
|
try {
|
|
15
|
-
url = `${await listen(http.server,
|
|
18
|
+
url = `${await listen(http.server, bound.port, bound.host)}/`;
|
|
16
19
|
}
|
|
17
20
|
catch (error) {
|
|
18
21
|
await http.close();
|
|
19
22
|
throw error;
|
|
20
23
|
}
|
|
24
|
+
/** Moves the public listener to the address project config asks for. */
|
|
25
|
+
async function relocate(configured) {
|
|
26
|
+
const port = options.port ?? (configured.port === undefined ? DEFAULT_PORT : configured.port);
|
|
27
|
+
const host = options.host ?? configured.host;
|
|
28
|
+
assertValidPort(port);
|
|
29
|
+
if (host !== undefined && typeof host !== 'boolean' && (typeof host !== 'string' || !host.length || host.length > 255)) {
|
|
30
|
+
throw new Error('Host must be a boolean or a non-empty hostname of at most 255 characters');
|
|
31
|
+
}
|
|
32
|
+
const next = { port, host };
|
|
33
|
+
if (next.port !== bound.port || next.host !== bound.host) {
|
|
34
|
+
await http.unlisten();
|
|
35
|
+
try {
|
|
36
|
+
url = `${await listen(http.server, next.port, next.host)}/`;
|
|
37
|
+
bound = next;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
url = `${await listen(http.server, bound.port, bound.host)}/`;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
options.onListening?.(url);
|
|
44
|
+
}
|
|
45
|
+
if (options.open)
|
|
46
|
+
void open().catch(diagnostic);
|
|
47
|
+
return new URL(url).origin;
|
|
48
|
+
}
|
|
21
49
|
/** Proxy notices reach the observer when present, otherwise the console. */
|
|
22
50
|
function diagnostic(error) {
|
|
23
51
|
const text = typeof error === 'string' ? error : inspect(error);
|
|
@@ -112,6 +140,7 @@ export async function startDevProxy(options) {
|
|
|
112
140
|
onOutput: options.onOutput,
|
|
113
141
|
onRuntimeMessage: options.onRuntimeMessage,
|
|
114
142
|
onRetire: options.onRuntimeRetired && (() => options.onRuntimeRetired(generation)),
|
|
143
|
+
onAddress: async (address) => closed || !isActive() ? new URL(url).origin : relocate(address),
|
|
115
144
|
onDiagnostic: options.onDiagnostic && (message => diagnostic(message)),
|
|
116
145
|
onWatch: async (inputs, complete) => {
|
|
117
146
|
if (complete)
|
|
@@ -172,12 +201,12 @@ export async function startDevProxy(options) {
|
|
|
172
201
|
if (runtime.backend.open)
|
|
173
202
|
void open(typeof runtime.backend.open === 'string' ? runtime.backend.open : runtime.backend.base).catch(diagnostic);
|
|
174
203
|
}
|
|
204
|
+
if (options.open && (status === 'ready' || status === 'failed'))
|
|
205
|
+
void open().catch(diagnostic);
|
|
175
206
|
publish(status);
|
|
176
207
|
});
|
|
177
208
|
const prepared = (async () => {
|
|
178
209
|
options.onListening?.(url);
|
|
179
|
-
if (options.open)
|
|
180
|
-
void open().catch(diagnostic);
|
|
181
210
|
const config = await import('./config/index.js');
|
|
182
211
|
if (closed)
|
|
183
212
|
return;
|
|
@@ -227,8 +256,10 @@ export async function startDevProxy(options) {
|
|
|
227
256
|
});
|
|
228
257
|
}
|
|
229
258
|
return {
|
|
230
|
-
/** Actual public address, including OS-selected or fallback port. */
|
|
231
|
-
url
|
|
259
|
+
/** Actual public address, including OS-selected or fallback port; project config can move it. */
|
|
260
|
+
get url() {
|
|
261
|
+
return url;
|
|
262
|
+
},
|
|
232
263
|
/** Initial runtime acquisition; watcher remains live after rejection. */
|
|
233
264
|
ready,
|
|
234
265
|
/** Latest public availability, independent of any runtime process. */
|
package/dist/process/child.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RuntimeCommand, RuntimeEvent, RuntimeOutput, RuntimeReply, RuntimeSendResult, WatchInput } from '../protocol.js';
|
|
1
|
+
import type { ConfiguredAddress, RuntimeCommand, RuntimeEvent, RuntimeOutput, RuntimeReply, RuntimeSendResult, WatchInput } from '../protocol.js';
|
|
2
2
|
/** Starts one IPC runtime and joins its entire lifetime before ownership transfer. */
|
|
3
3
|
export declare function startRuntimeChild(options: {
|
|
4
4
|
/** Runtime entry supplied by Histoire, never imported into proxy process. */
|
|
@@ -15,6 +15,8 @@ export declare function startRuntimeChild(options: {
|
|
|
15
15
|
token?: string;
|
|
16
16
|
/** Evaluation snapshots must reconcile before readiness publication. */
|
|
17
17
|
onWatch: (inputs: WatchInput[], complete: boolean) => Promise<void>;
|
|
18
|
+
/** Applies the configured public address and returns the origin the runtime must use. */
|
|
19
|
+
onAddress?: (address: ConfiguredAddress) => Promise<string>;
|
|
18
20
|
/** Commit current dependency set before public readiness, if runtime reported one. */
|
|
19
21
|
onReady?: () => Promise<void>;
|
|
20
22
|
/** Reports unexpected exits after readiness. */
|
package/dist/process/child.js
CHANGED
|
@@ -81,6 +81,12 @@ export function startRuntimeChild(options) {
|
|
|
81
81
|
return;
|
|
82
82
|
if (raw.type === 'watch')
|
|
83
83
|
await options.onWatch(raw.inputs, raw.complete === true);
|
|
84
|
+
if (raw.type === 'address') {
|
|
85
|
+
const publicOrigin = await options.onAddress?.({ port: raw.port, host: raw.host }) ?? options.publicOrigin;
|
|
86
|
+
if (closing || !child.connected)
|
|
87
|
+
return;
|
|
88
|
+
child.send({ type: 'address', generation: options.generation, publicOrigin }, error => error && reject(error));
|
|
89
|
+
}
|
|
84
90
|
if (raw.type === 'failed')
|
|
85
91
|
reject(new Error(raw.error));
|
|
86
92
|
if (raw.type === 'ready') {
|
package/dist/protocol.d.ts
CHANGED
|
@@ -117,16 +117,32 @@ export type ParentMessage = {
|
|
|
117
117
|
publicOrigin: string;
|
|
118
118
|
options: Record<string, unknown>;
|
|
119
119
|
token?: string;
|
|
120
|
+
} | {
|
|
121
|
+
type: 'address';
|
|
122
|
+
generation: string;
|
|
123
|
+
publicOrigin: string;
|
|
120
124
|
} | {
|
|
121
125
|
type: 'shutdown';
|
|
122
126
|
} | RuntimeCommand;
|
|
127
|
+
/** Public address asked by project configuration; command-line values win. */
|
|
128
|
+
export interface ConfiguredAddress {
|
|
129
|
+
/** Unvalidated project port, checked only if the CLI did not override it. */
|
|
130
|
+
port?: unknown;
|
|
131
|
+
/** Unvalidated project host, checked only if the CLI did not override it. */
|
|
132
|
+
host?: unknown;
|
|
133
|
+
}
|
|
123
134
|
/** Runtime reports observations; only parent decides public availability/restarts. */
|
|
124
135
|
export type ChildMessage = {
|
|
125
136
|
type: 'watch';
|
|
126
137
|
generation: string;
|
|
127
138
|
inputs: WatchInput[];
|
|
128
139
|
complete?: boolean;
|
|
129
|
-
}
|
|
140
|
+
}
|
|
141
|
+
/** Configured public address, sent before the runtime pins its origin; parent answers with the final one. */
|
|
142
|
+
| ({
|
|
143
|
+
type: 'address';
|
|
144
|
+
generation: string;
|
|
145
|
+
} & ConfiguredAddress) | {
|
|
130
146
|
type: 'ready';
|
|
131
147
|
generation: string;
|
|
132
148
|
target: string;
|
package/dist/protocol.js
CHANGED
|
@@ -65,6 +65,8 @@ export function isChildMessage(value) {
|
|
|
65
65
|
}
|
|
66
66
|
if (message.type === 'runtime-event')
|
|
67
67
|
return isIdentifier(message.event) && isBoundedFrame(value);
|
|
68
|
+
if (message.type === 'address')
|
|
69
|
+
return isBoundedFrame(value);
|
|
68
70
|
if (message.type === 'failed')
|
|
69
71
|
return typeof message.error === 'string' && message.error.length <= 4096;
|
|
70
72
|
if (message.type === 'restart')
|
package/dist/server/http.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { PublicState } from '../protocol.js';
|
|
2
2
|
import type { ProxyRequestObserver } from './observe.js';
|
|
3
3
|
/** Stable HTTP listener delegates only after coherent generation readiness. */
|
|
4
|
-
export declare function createProxyHttp(read: () => PublicState, restart: () => Promise<void>, observer?: ProxyRequestObserver): {
|
|
4
|
+
export declare function createProxyHttp(read: () => PublicState, restart: () => Promise<void>, observer?: ProxyRequestObserver, readPublicHostname?: () => string | undefined): {
|
|
5
5
|
server: import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>;
|
|
6
6
|
events: {
|
|
7
7
|
connect(_request: import("http").IncomingMessage, response: import("http").ServerResponse): void;
|
|
@@ -12,6 +12,8 @@ export declare function createProxyHttp(read: () => PublicState, restart: () =>
|
|
|
12
12
|
prepare(): Promise<void>;
|
|
13
13
|
/** Withdraw target synchronously and retire old runtime WebSockets. */
|
|
14
14
|
setTarget(next?: string): void;
|
|
15
|
+
/** Release the bound address while keeping the server reusable for another bind. */
|
|
16
|
+
unlisten(): Promise<void>;
|
|
15
17
|
/** Join listener close after terminating owned streams and idle connections. */
|
|
16
18
|
close(): Promise<void>;
|
|
17
19
|
};
|
package/dist/server/http.js
CHANGED
|
@@ -5,15 +5,15 @@ import { renderLoadingPage } from '../client/page.js';
|
|
|
5
5
|
import { designPlaygroundPath, renderDesignPlayground } from '../client/playground.js';
|
|
6
6
|
import { createStatusChannel } from './events.js';
|
|
7
7
|
import { observeRequest } from './observe.js';
|
|
8
|
-
/**
|
|
9
|
-
function isTrustedHost(host) {
|
|
8
|
+
/** Trust the published hostname and Vite's default local hosts for proxy-owned responses. */
|
|
9
|
+
function isTrustedHost(host, publicHostname) {
|
|
10
10
|
if (!host)
|
|
11
11
|
return false;
|
|
12
|
-
const hostname = host.startsWith('[') ? host.slice(1, host.indexOf(']')) : host.replace(/:\d+$/, '');
|
|
13
|
-
return hostname === 'localhost' || hostname.endsWith('.localhost') || isIP(hostname) !== 0;
|
|
12
|
+
const hostname = (host.startsWith('[') ? host.slice(1, host.indexOf(']')) : host.replace(/:\d+$/, '')).toLowerCase();
|
|
13
|
+
return hostname === publicHostname || hostname === 'localhost' || hostname.endsWith('.localhost') || isIP(hostname) !== 0;
|
|
14
14
|
}
|
|
15
15
|
/** Stable HTTP listener delegates only after coherent generation readiness. */
|
|
16
|
-
export function createProxyHttp(read, restart, observer) {
|
|
16
|
+
export function createProxyHttp(read, restart, observer, readPublicHostname) {
|
|
17
17
|
/** Exchange counter; only used when an observer exists. */
|
|
18
18
|
let exchanges = 0;
|
|
19
19
|
let target;
|
|
@@ -28,7 +28,7 @@ export function createProxyHttp(read, restart, observer) {
|
|
|
28
28
|
const observation = observer && !pathname.startsWith('/__histoire/dev/') ? observeRequest(request, response, ++exchanges, observer) : undefined;
|
|
29
29
|
try {
|
|
30
30
|
// Proxied runtime requests keep Vite's own host policy; everything answered here is checked.
|
|
31
|
-
if (!(target && proxy && !pathname.startsWith('/__histoire/dev/')) && !isTrustedHost(request.headers.host)) {
|
|
31
|
+
if (!(target && proxy && !pathname.startsWith('/__histoire/dev/')) && !isTrustedHost(request.headers.host, readPublicHostname?.())) {
|
|
32
32
|
response.writeHead(403, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' });
|
|
33
33
|
response.end('Blocked host');
|
|
34
34
|
return;
|
|
@@ -117,6 +117,15 @@ export function createProxyHttp(read, restart, observer) {
|
|
|
117
117
|
upgraded.clear();
|
|
118
118
|
}
|
|
119
119
|
},
|
|
120
|
+
/** Release the bound address while keeping the server reusable for another bind. */
|
|
121
|
+
async unlisten() {
|
|
122
|
+
if (!server.listening)
|
|
123
|
+
return;
|
|
124
|
+
const closing = new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve()));
|
|
125
|
+
for (const socket of sockets)
|
|
126
|
+
socket.destroy();
|
|
127
|
+
await closing;
|
|
128
|
+
},
|
|
120
129
|
/** Join listener close after terminating owned streams and idle connections. */
|
|
121
130
|
async close() {
|
|
122
131
|
target = undefined;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { Server } from 'node:http';
|
|
2
|
+
/** Check the effective port before opening or moving a listener. */
|
|
3
|
+
export declare function assertValidPort(port: unknown): asserts port is number;
|
|
2
4
|
/** Binds public address before any project modules load, retaining occupied-port fallback. */
|
|
3
5
|
export declare function listen(server: Server, port: number, host?: string | boolean): Promise<string>;
|
package/dist/server/listener.js
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
|
+
import { isIP } from 'node:net';
|
|
2
|
+
/** Check the effective port before opening or moving a listener. */
|
|
3
|
+
export function assertValidPort(port) {
|
|
4
|
+
if (typeof port !== 'number' || !Number.isInteger(port) || port < 0 || port > 65535)
|
|
5
|
+
throw new Error('Port must be an integer from 0 to 65535');
|
|
6
|
+
}
|
|
7
|
+
/** Host handed to `server.listen`; undefined binds every interface. */
|
|
8
|
+
function getBindHost(host) {
|
|
9
|
+
if (typeof host === 'string')
|
|
10
|
+
return host;
|
|
11
|
+
return host ? undefined : 'localhost';
|
|
12
|
+
}
|
|
13
|
+
/** Host shown in the public origin; a name is kept so `localhost` never becomes `[::1]`, which Vite mangles in asset URLs. */
|
|
14
|
+
function getPublicHost(bindHost, address) {
|
|
15
|
+
if (bindHost && !isIP(bindHost))
|
|
16
|
+
return bindHost;
|
|
17
|
+
if (['::', '0.0.0.0'].includes(address))
|
|
18
|
+
return 'localhost';
|
|
19
|
+
return address.includes(':') ? `[${address}]` : address;
|
|
20
|
+
}
|
|
1
21
|
/** Binds public address before any project modules load, retaining occupied-port fallback. */
|
|
2
22
|
export async function listen(server, port, host = 'localhost') {
|
|
3
|
-
|
|
4
|
-
|
|
23
|
+
assertValidPort(port);
|
|
24
|
+
const bindHost = getBindHost(host);
|
|
5
25
|
for (;;) {
|
|
6
26
|
try {
|
|
7
27
|
await new Promise((resolve, reject) => {
|
|
@@ -16,7 +36,7 @@ export async function listen(server, port, host = 'localhost') {
|
|
|
16
36
|
resolve();
|
|
17
37
|
}
|
|
18
38
|
server.once('error', failed).once('listening', ready);
|
|
19
|
-
server.listen(port,
|
|
39
|
+
server.listen(port, bindHost);
|
|
20
40
|
});
|
|
21
41
|
break;
|
|
22
42
|
}
|
|
@@ -29,6 +49,5 @@ export async function listen(server, port, host = 'localhost') {
|
|
|
29
49
|
const address = server.address();
|
|
30
50
|
if (!address || typeof address === 'string')
|
|
31
51
|
throw new Error('Unable to resolve Histoire proxy address');
|
|
32
|
-
|
|
33
|
-
return `http://${hostname.includes(':') ? `[${hostname}]` : hostname}:${address.port}`;
|
|
52
|
+
return `http://${getPublicHost(bindHost, address.address)}:${address.port}`;
|
|
34
53
|
}
|