@hublo/sentinel 1.7.0-alpha.27 → 1.7.0-alpha.28
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/bin/sentinel.js +1 -1
- package/dist/{chunk-B23ACOC4.js → chunk-CZFZRF3I.js} +421 -252
- package/dist/index.js +1 -1
- package/dist/roles/runtime-analysis/gateway.js +8 -1
- package/dist/roles/runtime-analysis/gateway.js.map +1 -1
- package/docs/runtime-analysis-adoption.md +35 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -34,7 +34,14 @@ function createGateway(options) {
|
|
|
34
34
|
port,
|
|
35
35
|
path: request.url,
|
|
36
36
|
method: request.method,
|
|
37
|
-
|
|
37
|
+
// What an ingress does: the upstream gets its own host, and the host the browser used in
|
|
38
|
+
// `x-forwarded-host`. A server that trusts only its public origin reads it there: console
|
|
39
|
+
// answered 500 ("Untrusted Console request origin") when it saw only the app's port.
|
|
40
|
+
headers: {
|
|
41
|
+
...request.headers,
|
|
42
|
+
host: `127.0.0.1:${port}`,
|
|
43
|
+
"x-forwarded-host": request.headers["x-forwarded-host"] ?? request.headers.host ?? ""
|
|
44
|
+
},
|
|
38
45
|
agent
|
|
39
46
|
},
|
|
40
47
|
(answer) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/roles/runtime-analysis/serve/gateway-main.ts","../../../src/roles/runtime-analysis/serve/gateway.ts"],"sourcesContent":["/**\n * The gateway as its own process: `node gateway.js --module <dir> --port 4101 --app-port 4111\n * --mock-port 4199 --log requests-base.jsonl`. The front's routing is read from its config.\n */\nimport { resolve } from 'node:path'\nimport { parseArgs } from 'node:util'\n\nimport { loadRuntimeConfig } from '../config/resolve.js'\n\nimport { createGateway } from './gateway.js'\n\nconst { values: args } = parseArgs({\n options: Object.fromEntries(\n ['module', 'port', 'app-port', 'mock-port', 'log'].map((key) => [\n key,\n { type: 'string' as const },\n ]),\n ),\n})\nconst loaded = await loadRuntimeConfig(resolve(String(args.module)))\nif (!('config' in loaded)) {\n console.error(`no runtime-analysis config in ${args.module}`)\n process.exit(2)\n}\ncreateGateway({\n ...(loaded.config.basePath === undefined ? {} : { basePath: loaded.config.basePath }),\n ...(loaded.config.mockPrefixes === undefined ? {} : { mockPrefixes: loaded.config.mockPrefixes }),\n appPort: Number(args['app-port']),\n mockPort: Number(args['mock-port']),\n log: resolve(String(args.log)),\n}).listen(Number(args.port), '127.0.0.1', () =>\n console.log(\n `gateway on http://127.0.0.1:${args.port}, app ${args['app-port']}, mocks ${args['mock-port']}`,\n ),\n)\n","/**\n * The gateway, one per build, like an environment's: the front's own paths go to that build's\n * server, every other path to the mock API. A plain proxy, no interception. Every request is logged\n * as one JSON line with its status and whether the mock API handled it, which is how a flow later\n * proves its screen was fed by the mocks and nothing else.\n *\n * Moved from the monorepo's harness (`engine/serve/gateway.mjs`, hublo/monorepo#23672), unchanged\n * in behaviour. It runs in its OWN process (`gateway-main.ts`): proxying from the process that runs\n * Lighthouse would delay the very responses being measured.\n */\nimport { appendFileSync, writeFileSync } from 'node:fs'\nimport http from 'node:http'\n\nexport interface GatewayOptions {\n /** The path the app is served under; the rest is mocked. Or `mockPrefixes`. */\n basePath?: string\n /** The paths mocked, for an app at the root; the rest goes to the app. */\n mockPrefixes?: string[]\n appPort: number\n mockPort: number\n /** The request log, one JSON line per request, emptied at start. */\n log: string\n}\n\nconst under = (pathname: string, prefix: string): boolean =>\n pathname === prefix || pathname.startsWith(`${prefix}/`)\n\n/** Whether a request's path is the app's, by the front's base path or its mocked prefixes. */\nexport function isAppPath(\n pathname: string,\n options: Pick<GatewayOptions, 'basePath' | 'mockPrefixes'>,\n): boolean {\n const base = (options.basePath ?? '').replace(/\\/$/, '')\n return options.mockPrefixes\n ? !options.mockPrefixes.some((prefix) => under(pathname, prefix))\n : under(pathname, base)\n}\n\nexport function createGateway(options: GatewayOptions): http.Server {\n writeFileSync(options.log, '')\n const agent = new http.Agent({ keepAlive: true })\n return http.createServer((request, response) => {\n const startedAt = Date.now()\n const { pathname } = new URL(request.url ?? '/', 'http://gateway')\n const toApp = isAppPath(pathname, options)\n const port = toApp ? options.appPort : options.mockPort\n const log = (\n status: number | undefined,\n mock: string | string[] | null,\n error?: string,\n ): void =>\n appendFileSync(\n options.log,\n `${JSON.stringify({ at: new Date(startedAt).toISOString(), method: request.method, path: request.url, to: toApp ? 'app' : 'mock', status, mock, ms: Date.now() - startedAt, ...(error ? { error } : {}) })}\\n`,\n )\n const upstream = http.request(\n {\n hostname: '127.0.0.1',\n port,\n path: request.url,\n method: request.method,\n headers: {
|
|
1
|
+
{"version":3,"sources":["../../../src/roles/runtime-analysis/serve/gateway-main.ts","../../../src/roles/runtime-analysis/serve/gateway.ts"],"sourcesContent":["/**\n * The gateway as its own process: `node gateway.js --module <dir> --port 4101 --app-port 4111\n * --mock-port 4199 --log requests-base.jsonl`. The front's routing is read from its config.\n */\nimport { resolve } from 'node:path'\nimport { parseArgs } from 'node:util'\n\nimport { loadRuntimeConfig } from '../config/resolve.js'\n\nimport { createGateway } from './gateway.js'\n\nconst { values: args } = parseArgs({\n options: Object.fromEntries(\n ['module', 'port', 'app-port', 'mock-port', 'log'].map((key) => [\n key,\n { type: 'string' as const },\n ]),\n ),\n})\nconst loaded = await loadRuntimeConfig(resolve(String(args.module)))\nif (!('config' in loaded)) {\n console.error(`no runtime-analysis config in ${args.module}`)\n process.exit(2)\n}\ncreateGateway({\n ...(loaded.config.basePath === undefined ? {} : { basePath: loaded.config.basePath }),\n ...(loaded.config.mockPrefixes === undefined ? {} : { mockPrefixes: loaded.config.mockPrefixes }),\n appPort: Number(args['app-port']),\n mockPort: Number(args['mock-port']),\n log: resolve(String(args.log)),\n}).listen(Number(args.port), '127.0.0.1', () =>\n console.log(\n `gateway on http://127.0.0.1:${args.port}, app ${args['app-port']}, mocks ${args['mock-port']}`,\n ),\n)\n","/**\n * The gateway, one per build, like an environment's: the front's own paths go to that build's\n * server, every other path to the mock API. A plain proxy, no interception. Every request is logged\n * as one JSON line with its status and whether the mock API handled it, which is how a flow later\n * proves its screen was fed by the mocks and nothing else.\n *\n * Moved from the monorepo's harness (`engine/serve/gateway.mjs`, hublo/monorepo#23672), unchanged\n * in behaviour. It runs in its OWN process (`gateway-main.ts`): proxying from the process that runs\n * Lighthouse would delay the very responses being measured.\n */\nimport { appendFileSync, writeFileSync } from 'node:fs'\nimport http from 'node:http'\n\nexport interface GatewayOptions {\n /** The path the app is served under; the rest is mocked. Or `mockPrefixes`. */\n basePath?: string\n /** The paths mocked, for an app at the root; the rest goes to the app. */\n mockPrefixes?: string[]\n appPort: number\n mockPort: number\n /** The request log, one JSON line per request, emptied at start. */\n log: string\n}\n\nconst under = (pathname: string, prefix: string): boolean =>\n pathname === prefix || pathname.startsWith(`${prefix}/`)\n\n/** Whether a request's path is the app's, by the front's base path or its mocked prefixes. */\nexport function isAppPath(\n pathname: string,\n options: Pick<GatewayOptions, 'basePath' | 'mockPrefixes'>,\n): boolean {\n const base = (options.basePath ?? '').replace(/\\/$/, '')\n return options.mockPrefixes\n ? !options.mockPrefixes.some((prefix) => under(pathname, prefix))\n : under(pathname, base)\n}\n\nexport function createGateway(options: GatewayOptions): http.Server {\n writeFileSync(options.log, '')\n const agent = new http.Agent({ keepAlive: true })\n return http.createServer((request, response) => {\n const startedAt = Date.now()\n const { pathname } = new URL(request.url ?? '/', 'http://gateway')\n const toApp = isAppPath(pathname, options)\n const port = toApp ? options.appPort : options.mockPort\n const log = (\n status: number | undefined,\n mock: string | string[] | null,\n error?: string,\n ): void =>\n appendFileSync(\n options.log,\n `${JSON.stringify({ at: new Date(startedAt).toISOString(), method: request.method, path: request.url, to: toApp ? 'app' : 'mock', status, mock, ms: Date.now() - startedAt, ...(error ? { error } : {}) })}\\n`,\n )\n const upstream = http.request(\n {\n hostname: '127.0.0.1',\n port,\n path: request.url,\n method: request.method,\n // What an ingress does: the upstream gets its own host, and the host the browser used in\n // `x-forwarded-host`. A server that trusts only its public origin reads it there: console\n // answered 500 (\"Untrusted Console request origin\") when it saw only the app's port.\n headers: {\n ...request.headers,\n host: `127.0.0.1:${port}`,\n 'x-forwarded-host': request.headers['x-forwarded-host'] ?? request.headers.host ?? '',\n },\n agent,\n },\n (answer) => {\n response.writeHead(answer.statusCode ?? 502, answer.headers)\n answer.pipe(response)\n answer.on('end', () => log(answer.statusCode, answer.headers['x-cwv-mock'] ?? null))\n },\n )\n upstream.on('error', (error: NodeJS.ErrnoException) => {\n log(502, null, error.code ?? String(error))\n response.writeHead(502, { 'content-type': 'text/plain' })\n response.end('upstream unreachable')\n })\n request.pipe(upstream)\n })\n}\n"],"mappings":";;;;;;AAIA,SAAS,eAAe;AACxB,SAAS,iBAAiB;;;ACK1B,SAAS,gBAAgB,qBAAqB;AAC9C,OAAO,UAAU;AAajB,IAAM,QAAQ,CAAC,UAAkB,WAC/B,aAAa,UAAU,SAAS,WAAW,GAAG,MAAM,GAAG;AAGlD,SAAS,UACd,UACA,SACS;AACT,QAAM,QAAQ,QAAQ,YAAY,IAAI,QAAQ,OAAO,EAAE;AACvD,SAAO,QAAQ,eACX,CAAC,QAAQ,aAAa,KAAK,CAAC,WAAW,MAAM,UAAU,MAAM,CAAC,IAC9D,MAAM,UAAU,IAAI;AAC1B;AAEO,SAAS,cAAc,SAAsC;AAClE,gBAAc,QAAQ,KAAK,EAAE;AAC7B,QAAM,QAAQ,IAAI,KAAK,MAAM,EAAE,WAAW,KAAK,CAAC;AAChD,SAAO,KAAK,aAAa,CAAC,SAAS,aAAa;AAC9C,UAAM,YAAY,KAAK,IAAI;AAC3B,UAAM,EAAE,SAAS,IAAI,IAAI,IAAI,QAAQ,OAAO,KAAK,gBAAgB;AACjE,UAAM,QAAQ,UAAU,UAAU,OAAO;AACzC,UAAM,OAAO,QAAQ,QAAQ,UAAU,QAAQ;AAC/C,UAAM,MAAM,CACV,QACA,MACA,UAEA;AAAA,MACE,QAAQ;AAAA,MACR,GAAG,KAAK,UAAU,EAAE,IAA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,QAAQ,OAAO,KAAK,MAAM,CAAC,CAAC;AACnE,IAAI,EAAE,YAAY,SAAS;AACzB,UAAQ,MAAM,iCAAiC,KAAK,MAAM,EAAE;AAC5D,UAAQ,KAAK,CAAC;AAChB;AACA,cAAc;AAAA,EACZ,GAAI,OAAO,OAAO,aAAa,SAAY,CAAC,IAAI,EAAE,UAAU,OAAO,OAAO,SAAS;AAAA,EACnF,GAAI,OAAO,OAAO,iBAAiB,SAAY,CAAC,IAAI,EAAE,cAAc,OAAO,OAAO,aAAa;AAAA,EAC/F,SAAS,OAAO,KAAK,UAAU,CAAC;AAAA,EAChC,UAAU,OAAO,KAAK,WAAW,CAAC;AAAA,EAClC,KAAK,QAAQ,OAAO,KAAK,GAAG,CAAC;AAC/B,CAAC,EAAE;AAAA,EAAO,OAAO,KAAK,IAAI;AAAA,EAAG;AAAA,EAAa,MACxC,QAAQ;AAAA,IACN,+BAA+B,KAAK,IAAI,SAAS,KAAK,UAAU,CAAC,WAAW,KAAK,WAAW,CAAC;AAAA,EAC/F;AACF;","names":[]}
|
|
@@ -76,6 +76,40 @@ A mocked list is written once, not copied out by hand:
|
|
|
76
76
|
|
|
77
77
|
That is 25 items. `{n}` is the item's number from 1, `{nn}` the same on two digits, and `$cycle` takes its values in turn.
|
|
78
78
|
|
|
79
|
+
## A front whose server keeps its session
|
|
80
|
+
|
|
81
|
+
host-admin and career read their session through an HTTP call, so `api/session.json` answers it. A front that reads its session in its SERVER (console: better-auth, Redis, a signed cookie) needs two more files, found by name next to its mocks. Neither is a key of the config, and a front without them is measured as before.
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
runtime-analysis/
|
|
85
|
+
├── server.env the variables its server reads: test values only
|
|
86
|
+
├── session.json its test session, as data
|
|
87
|
+
└── api/ its mocks, as for any front
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`server.env` is `KEY=VALUE` per line. `{origin}` is the gateway of the side being served, and `{host}` the same without the scheme, for a server that only trusts `https` origins and compares the host:
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
DEPLOY_ENV=cwv
|
|
94
|
+
BFF_BACKOFFICE_BASE_URL={origin}
|
|
95
|
+
CONSOLE_TRUSTED_ORIGINS=https://{host}
|
|
96
|
+
CACHE_REDIS_URL=redis://127.0.0.1:6379
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`session.json` is what the server's store holds for one session, and the cookies the browser sends back. The secret that signs the cookie is a test value in `server.env`, the token is fixed and the expiry far away, so the cookie is a constant written once:
|
|
100
|
+
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"store": {
|
|
104
|
+
"url": "redis://127.0.0.1:6379",
|
|
105
|
+
"entries": [{ "key": "console-cwv:cwv-token", "value": { "session": {}, "user": {} } }]
|
|
106
|
+
},
|
|
107
|
+
"cookies": [{ "name": "__Secure-console.session_token", "value": "cwv-token.<signature>" }]
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Sentinel writes `store.entries` to the store before the servers start, and sets the cookies on every screen. It never starts the store: in CI it is a service of the workflow, on a machine whatever runs it there (in the monorepo, `pnpm redis:start`).
|
|
112
|
+
|
|
79
113
|
## Run it on your machine
|
|
80
114
|
|
|
81
115
|
```bash
|
|
@@ -153,6 +187,7 @@ The comment is sized to the 65,536 characters GitHub keeps of a comment: past th
|
|
|
153
187
|
| `could not install the measure's browser packages … ENOTFOUND on …` | The npm registry was unreachable. In CI, cache `SENTINEL_CACHE_DIR`. |
|
|
154
188
|
| A screen ❌ with `ready 200 (mocked)` failed | The `ready` call has no mock answering 200. |
|
|
155
189
|
| A screen ❌ with `no unhandled request` failed | The screen calls a path no `api` entry answers: add the entry. |
|
|
190
|
+
| `could not write the session to redis://…` | The store `session.json` names is not running. Start it (`pnpm redis:start`, or the workflow's service) and run again. |
|
|
156
191
|
| `this PR changes none of its screens` | The diff reaches none of the screen's route chain, its shell, its translations or the workspace's build inputs. A change the bundler erases (a type, a comment) does not count. |
|
|
157
192
|
|
|
158
193
|
## What the role will not do
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hublo/sentinel",
|
|
3
|
-
"version": "1.7.0-alpha.
|
|
3
|
+
"version": "1.7.0-alpha.28",
|
|
4
4
|
"description": "One CLI that guards code health across Hublo repos: shared lint/typescript/build/test presets, static & dynamic analysis, and architecture checks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|