@histoire/dev-proxy 0.0.0-stage → 1.0.0-beta.2
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/LICENSE +21 -0
- package/README.md +16 -2
- package/dist/appearance.d.ts +11 -0
- package/dist/appearance.js +11 -0
- package/dist/browser/chunks/transitions-3JvGwb-W.js +71 -0
- package/dist/browser/demo.js +50 -0
- package/dist/browser/lifecycle.js +55 -0
- package/dist/client/demo.d.ts +1 -0
- package/dist/client/demo.js +46 -0
- package/dist/client/lifecycle.d.ts +1 -0
- package/dist/client/lifecycle.js +73 -0
- package/dist/client/mark.d.ts +2 -0
- package/dist/client/mark.js +8 -0
- package/dist/client/page.d.ts +14 -0
- package/dist/client/page.js +18 -0
- package/dist/client/playground.d.ts +5 -0
- package/dist/client/playground.js +32 -0
- package/dist/client/status.d.ts +3 -0
- package/dist/client/status.js +9 -0
- package/dist/client/styles.d.ts +2 -0
- package/dist/client/styles.js +139 -0
- package/dist/client/transitions.d.ts +8 -0
- package/dist/client/transitions.js +45 -0
- package/dist/config/discovery.d.ts +12 -0
- package/dist/config/discovery.js +57 -0
- package/dist/config/index.d.ts +4 -0
- package/dist/config/index.js +4 -0
- package/dist/config/inputs.d.ts +18 -0
- package/dist/config/inputs.js +29 -0
- package/dist/config/snapshots.d.ts +16 -0
- package/dist/config/snapshots.js +25 -0
- package/dist/config/watcher.d.ts +12 -0
- package/dist/config/watcher.js +103 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +272 -0
- package/dist/lifecycle/coordinator.d.ts +51 -0
- package/dist/lifecycle/coordinator.js +244 -0
- package/dist/lifecycle/index.d.ts +3 -0
- package/dist/lifecycle/index.js +2 -0
- package/dist/lifecycle/wait.d.ts +2 -0
- package/dist/lifecycle/wait.js +22 -0
- package/dist/process/child.d.ts +53 -0
- package/dist/process/child.js +185 -0
- package/dist/protocol.d.ts +166 -0
- package/dist/protocol.js +75 -0
- package/dist/server/events.d.ts +11 -0
- package/dist/server/events.js +37 -0
- package/dist/server/http.d.ts +17 -0
- package/dist/server/http.js +130 -0
- package/dist/server/listener.d.ts +3 -0
- package/dist/server/listener.js +34 -0
- package/dist/server/observe.d.ts +47 -0
- package/dist/server/observe.js +59 -0
- package/package.json +58 -3
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
/** How a public HTTP exchange ended. */
|
|
3
|
+
export type ProxyRequestOutcome = 'completed' | 'failed' | 'aborted';
|
|
4
|
+
/**
|
|
5
|
+
* Framework-neutral observation of one public HTTP exchange. Carries no query, header,
|
|
6
|
+
* cookie or body: only what is needed to list real traffic. `key` is unique per proxy
|
|
7
|
+
* and joins the two phases of one exchange.
|
|
8
|
+
*/
|
|
9
|
+
export type ProxyRequestEvent = {
|
|
10
|
+
phase: 'start';
|
|
11
|
+
key: number;
|
|
12
|
+
method: string;
|
|
13
|
+
pathname: string;
|
|
14
|
+
} | {
|
|
15
|
+
phase: 'end';
|
|
16
|
+
key: number;
|
|
17
|
+
method: string;
|
|
18
|
+
pathname: string;
|
|
19
|
+
status?: number;
|
|
20
|
+
outcome: ProxyRequestOutcome;
|
|
21
|
+
durationMs: number;
|
|
22
|
+
};
|
|
23
|
+
/** Receives request events; failures of the observer never reach the HTTP exchange. */
|
|
24
|
+
export type ProxyRequestObserver = (event: ProxyRequestEvent) => void;
|
|
25
|
+
/** Handle returned for an observed exchange. */
|
|
26
|
+
export interface RequestObservation {
|
|
27
|
+
/** Marks the exchange as failed by the proxy itself (e.g. it destroyed a half-sent response). */
|
|
28
|
+
fail: () => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Pathname of a request target without query or fragment. Handles origin-form and
|
|
32
|
+
* absolute-form targets; anything unparseable becomes `/`.
|
|
33
|
+
* @param target Raw `request.url`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function pathnameOf(target: string | undefined): string;
|
|
36
|
+
/**
|
|
37
|
+
* Observes one exchange. Must be called before the request is dispatched, so every
|
|
38
|
+
* outcome is seen. Registers a single `close` listener on the response, which Node emits
|
|
39
|
+
* after a normal finish and also when the client connection ends early; `finished` guards
|
|
40
|
+
* against a second emission. An exchange whose response was not completely written when
|
|
41
|
+
* `close` fired is `aborted`, even if backend work finishes later.
|
|
42
|
+
* @param request Incoming request (only method and target are read).
|
|
43
|
+
* @param response Outgoing response (never wrapped or modified).
|
|
44
|
+
* @param key Unique exchange key.
|
|
45
|
+
* @param observer Consumer of the events.
|
|
46
|
+
*/
|
|
47
|
+
export declare function observeRequest(request: IncomingMessage, response: ServerResponse, key: number, observer: ProxyRequestObserver): RequestObservation;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { performance } from 'node:perf_hooks';
|
|
2
|
+
/** Longest pathname handed to observers; a longer one is cut (retention is bounded again by consumers). */
|
|
3
|
+
const PATHNAME_MAX = 2048;
|
|
4
|
+
/**
|
|
5
|
+
* Pathname of a request target without query or fragment. Handles origin-form and
|
|
6
|
+
* absolute-form targets; anything unparseable becomes `/`.
|
|
7
|
+
* @param target Raw `request.url`.
|
|
8
|
+
*/
|
|
9
|
+
export function pathnameOf(target) {
|
|
10
|
+
let path = target ?? '/';
|
|
11
|
+
if (!path.startsWith('/')) {
|
|
12
|
+
try {
|
|
13
|
+
path = new URL(path).pathname;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return '/';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const cut = path.search(/[?#]/);
|
|
20
|
+
return (cut === -1 ? path : path.slice(0, cut)).slice(0, PATHNAME_MAX);
|
|
21
|
+
}
|
|
22
|
+
/** Observer faults (including rejected promises) must not alter forwarding, nor produce diagnostics of their own. */
|
|
23
|
+
function emit(observer, event) {
|
|
24
|
+
try {
|
|
25
|
+
const result = observer(event);
|
|
26
|
+
if (result && typeof result.catch === 'function')
|
|
27
|
+
result.catch(() => { });
|
|
28
|
+
}
|
|
29
|
+
catch { }
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Observes one exchange. Must be called before the request is dispatched, so every
|
|
33
|
+
* outcome is seen. Registers a single `close` listener on the response, which Node emits
|
|
34
|
+
* after a normal finish and also when the client connection ends early; `finished` guards
|
|
35
|
+
* against a second emission. An exchange whose response was not completely written when
|
|
36
|
+
* `close` fired is `aborted`, even if backend work finishes later.
|
|
37
|
+
* @param request Incoming request (only method and target are read).
|
|
38
|
+
* @param response Outgoing response (never wrapped or modified).
|
|
39
|
+
* @param key Unique exchange key.
|
|
40
|
+
* @param observer Consumer of the events.
|
|
41
|
+
*/
|
|
42
|
+
export function observeRequest(request, response, key, observer) {
|
|
43
|
+
const startedAt = performance.now();
|
|
44
|
+
const method = (request.method ?? 'GET').slice(0, 32);
|
|
45
|
+
const pathname = pathnameOf(request.url);
|
|
46
|
+
let finished = false;
|
|
47
|
+
let failed = false;
|
|
48
|
+
emit(observer, { phase: 'start', key, method, pathname });
|
|
49
|
+
response.once('close', () => {
|
|
50
|
+
if (finished)
|
|
51
|
+
return;
|
|
52
|
+
finished = true;
|
|
53
|
+
const complete = response.writableFinished;
|
|
54
|
+
const status = response.headersSent ? response.statusCode : undefined;
|
|
55
|
+
const outcome = !complete && !failed ? 'aborted' : failed || (status !== undefined && status >= 500) ? 'failed' : 'completed';
|
|
56
|
+
emit(observer, { phase: 'end', key, method, pathname, ...(status === undefined ? {} : { status }), outcome, durationMs: Math.round((performance.now() - startedAt) * 10) / 10 });
|
|
57
|
+
});
|
|
58
|
+
return { fail: () => void (failed = true) };
|
|
59
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@histoire/dev-proxy",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0-beta.2",
|
|
5
|
+
"description": "Early HTTP hosting and process lifecycle for Histoire development",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/Akryum/histoire.git",
|
|
10
|
+
"directory": "packages/histoire-dev-proxy"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./lifecycle": {
|
|
21
|
+
"types": "./dist/lifecycle/index.d.ts",
|
|
22
|
+
"default": "./dist/lifecycle/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./config": {
|
|
25
|
+
"types": "./dist/config/index.d.ts",
|
|
26
|
+
"default": "./dist/config/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./protocol": {
|
|
29
|
+
"types": "./dist/protocol.d.ts",
|
|
30
|
+
"default": "./dist/protocol.js"
|
|
31
|
+
},
|
|
32
|
+
"./appearance": {
|
|
33
|
+
"types": "./dist/appearance.d.ts",
|
|
34
|
+
"default": "./dist/appearance.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22.12.0"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"chokidar": "^4.0.1",
|
|
45
|
+
"httpxy": "^0.1.7",
|
|
46
|
+
"open": "^10.2.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^22.10.1",
|
|
50
|
+
"@typescript/native": "npm:typescript@7.0.2",
|
|
51
|
+
"concurrently": "^9.1.0",
|
|
52
|
+
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
53
|
+
"vite": "^8.3.5",
|
|
54
|
+
"vitest": "5.1.1"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "rimraf dist && tsc && vite build",
|
|
58
|
+
"watch": "concurrently \"tsc -w\" \"vite build --watch\"",
|
|
59
|
+
"test": "vitest run"
|
|
60
|
+
}
|
|
6
61
|
}
|