@speedkit/cli 4.49.0 → 4.50.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/CHANGELOG.md +8 -0
- package/dist/services/diff/diff-service-factory.js +1 -1
- package/dist/services/diff/spec/page-source.spec.js +1 -1
- package/dist/services/origin-request/asset-variations.d.ts +24 -3
- package/dist/services/origin-request/asset-variations.js +51 -17
- package/dist/services/origin-request/origin-fetcher.d.ts +2 -2
- package/dist/services/origin-request/origin-fetcher.js +10 -4
- package/dist/services/origin-request/origin-request-service-factory.js +1 -1
- package/dist/services/origin-request/origin-request-service.d.ts +6 -2
- package/dist/services/origin-request/origin-request-service.js +32 -11
- package/dist/services/origin-request/runtime-config-source.d.ts +3 -1
- package/dist/services/origin-request/runtime-config-source.js +1 -0
- package/dist/services/origin-request/spec/asset-variations.spec.js +58 -0
- package/dist/services/origin-request/spec/origin-fetcher.spec.js +16 -1
- package/dist/services/origin-request/spec/origin-request-service.spec.js +27 -1
- package/dist/services/origin-request/spec/runtime-config-source.spec.d.ts +1 -0
- package/dist/services/origin-request/spec/runtime-config-source.spec.js +47 -0
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# [4.50.0](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.49.0...v4.50.0) (2026-10-27)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **origin-request:** apply the runtime config's origin map to origin fetches ([dfc6f63](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/dfc6f6340033cce1525e7c25a159e8b7654d492b))
|
|
7
|
+
* **origin-request:** hide origin map header values in the report ([65c161a](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/65c161a973d95e3559e46ae7b1c42b8dbd697a5e))
|
|
8
|
+
|
|
1
9
|
# [4.49.0](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.48.0...v4.49.0) (2026-10-26)
|
|
2
10
|
|
|
3
11
|
|
|
@@ -145,7 +145,7 @@ export class DiffServiceFactory {
|
|
|
145
145
|
async loadRuntimeConfig(log, app) {
|
|
146
146
|
if (!app && !this.context.variationsFile) {
|
|
147
147
|
log.writeWarning("no customer: the requests carry Speed Kit's device defaults only, no variations of an app");
|
|
148
|
-
return { variations: {}, authentications: [] };
|
|
148
|
+
return { variations: {}, authentications: [], originMap: {} };
|
|
149
149
|
}
|
|
150
150
|
return new RuntimeConfigSource({
|
|
151
151
|
cli: log,
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* Mirrors two pieces of server behaviour:
|
|
6
6
|
* - `AssetVariations.getVariations()` — which variation configs apply and in which order
|
|
7
7
|
* (orestes: `orestes-common/.../typesystem/AssetVariations.kt`)
|
|
8
|
-
* - `AssetClient.newRequest()` / `createRequest()` —
|
|
9
|
-
* urlPrefix and queryParams (orestes: `orestes-server/.../asset/AssetClient.kt`)
|
|
8
|
+
* - `AssetClient.newRequest()` / `createRequest()` — the origin map, base headers,
|
|
9
|
+
* last-writer-wins merging, urlPrefix and queryParams (orestes: `orestes-server/.../asset/AssetClient.kt`)
|
|
10
10
|
*
|
|
11
11
|
* Keep this file free of I/O so the resolution stays unit-testable.
|
|
12
12
|
*/
|
|
@@ -21,11 +21,25 @@ export interface AssetVariation {
|
|
|
21
21
|
matchedKey?: string;
|
|
22
22
|
}
|
|
23
23
|
export type AssetVariationMap = Record<string, AssetVariation>;
|
|
24
|
+
/** An entry of the runtime config's `speedKit.originMap`, keyed by origin or by host. */
|
|
25
|
+
export interface OriginMapEntry {
|
|
26
|
+
/** Replaces the host, or the scheme and the host when it carries a scheme. */
|
|
27
|
+
origin?: string | null;
|
|
28
|
+
/** Keeps the original Host header after `origin` rewrote the url. */
|
|
29
|
+
overrideHostHeader?: boolean;
|
|
30
|
+
/** Sent with every request to this origin; the base and variation headers win on a conflict. */
|
|
31
|
+
headers?: Record<string, string>;
|
|
32
|
+
}
|
|
33
|
+
export type OriginMap = Record<string, OriginMapEntry>;
|
|
24
34
|
export interface ResolvedRequest {
|
|
25
35
|
/** Keys of the applied configs, in the order the server applies them. */
|
|
26
36
|
appliedKeys: string[];
|
|
27
37
|
/** `template` when a regex entry matched, otherwise `named`. */
|
|
28
38
|
resolutionPath: "template" | "named";
|
|
39
|
+
/** The origin map key that applied, if any. */
|
|
40
|
+
originMapKey?: string;
|
|
41
|
+
/** Lower-case names of the headers whose sent value comes from the origin map. */
|
|
42
|
+
originMapHeaders?: string[];
|
|
29
43
|
url: string;
|
|
30
44
|
headers: Record<string, string>;
|
|
31
45
|
}
|
|
@@ -43,7 +57,14 @@ export declare class UndefinedVariationError extends Error {
|
|
|
43
57
|
* `[wildcard, template]` and skips the device defaults; otherwise `[default, wildcard, named]`.
|
|
44
58
|
*/
|
|
45
59
|
export declare function getVariations(custom: AssetVariationMap, variationType?: string): AssetVariation[];
|
|
60
|
+
/** What a request is resolved against besides the variations. */
|
|
61
|
+
export interface ResolveOptions {
|
|
62
|
+
/** A Host header for every request, as `--host` sends it. */
|
|
63
|
+
hostOverride?: string;
|
|
64
|
+
/** The runtime config's `speedKit.originMap`. */
|
|
65
|
+
originMap?: OriginMap;
|
|
66
|
+
}
|
|
46
67
|
/** Resolves a variation into the concrete request the server would send. */
|
|
47
|
-
export declare function resolveRequest(custom: AssetVariationMap, url: string, variationType?: string, hostOverride?:
|
|
68
|
+
export declare function resolveRequest(custom: AssetVariationMap, url: string, variationType?: string, { hostOverride, originMap }?: ResolveOptions): ResolvedRequest;
|
|
48
69
|
/** Renders the resolved request as a curl command, for pasting into a bug report. */
|
|
49
70
|
export declare function toCurl(url: string, headers: Record<string, string>): string;
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* Mirrors two pieces of server behaviour:
|
|
6
6
|
* - `AssetVariations.getVariations()` — which variation configs apply and in which order
|
|
7
7
|
* (orestes: `orestes-common/.../typesystem/AssetVariations.kt`)
|
|
8
|
-
* - `AssetClient.newRequest()` / `createRequest()` —
|
|
9
|
-
* urlPrefix and queryParams (orestes: `orestes-server/.../asset/AssetClient.kt`)
|
|
8
|
+
* - `AssetClient.newRequest()` / `createRequest()` — the origin map, base headers,
|
|
9
|
+
* last-writer-wins merging, urlPrefix and queryParams (orestes: `orestes-server/.../asset/AssetClient.kt`)
|
|
10
10
|
*
|
|
11
11
|
* Keep this file free of I/O so the resolution stays unit-testable.
|
|
12
12
|
*/
|
|
@@ -121,19 +121,40 @@ function buildUrl(url, chain) {
|
|
|
121
121
|
return result;
|
|
122
122
|
}
|
|
123
123
|
/**
|
|
124
|
-
*
|
|
125
|
-
*
|
|
124
|
+
* The origin map entry for a url. Like AssetClient.createRequest, it tries `scheme://host` first
|
|
125
|
+
* and the host alone second.
|
|
126
126
|
*/
|
|
127
|
-
function
|
|
128
|
-
const
|
|
129
|
-
[
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
127
|
+
function findOriginMapEntry(originMap, url) {
|
|
128
|
+
for (const key of [url.origin, url.host]) {
|
|
129
|
+
if (originMap[key])
|
|
130
|
+
return [key, originMap[key]];
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
/** URIs.cloneWithOrigin: `origin` replaces the host, plus the scheme when it names one. */
|
|
135
|
+
function withOrigin(url, origin) {
|
|
136
|
+
const target = new URL(origin.includes("://") ? origin : `${url.protocol}//${origin}`);
|
|
137
|
+
const result = new URL(url);
|
|
138
|
+
result.protocol = target.protocol;
|
|
139
|
+
result.host = target.host;
|
|
140
|
+
return result.toString();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The origin map's headers and Host first, because createRequest sets them before newRequest
|
|
144
|
+
* adds the rest. Then the base headers, then each config's headers, the last writer winning. A
|
|
145
|
+
* `null` value removes the header, which is how a config drops Accept-Language.
|
|
146
|
+
*/
|
|
147
|
+
function buildHeaders(chain, hostOverride, originEntry, originalHost) {
|
|
148
|
+
const headers = new Map();
|
|
149
|
+
for (const [key, value] of Object.entries(originEntry?.headers ?? {})) {
|
|
150
|
+
headers.set(key.toLowerCase(), String(value));
|
|
151
|
+
}
|
|
152
|
+
if (originalHost)
|
|
153
|
+
headers.set("host", originalHost);
|
|
154
|
+
headers.set("accept", "text/html, application/xhtml+xml, application/xml;q=0.9, image/avif, image/webp, */*;q=0.8");
|
|
155
|
+
headers.set("accept-encoding", "gzip");
|
|
156
|
+
headers.set("accept-language", "*");
|
|
157
|
+
headers.set("user-agent", DEFAULT_USER_AGENT);
|
|
137
158
|
for (const config of chain) {
|
|
138
159
|
for (const [key, value] of Object.entries(config.headers ?? {})) {
|
|
139
160
|
if (value === null)
|
|
@@ -147,16 +168,29 @@ function buildHeaders(chain, hostOverride) {
|
|
|
147
168
|
return Object.fromEntries(headers);
|
|
148
169
|
}
|
|
149
170
|
/** Resolves a variation into the concrete request the server would send. */
|
|
150
|
-
export function resolveRequest(custom, url, variationType, hostOverride) {
|
|
171
|
+
export function resolveRequest(custom, url, variationType, { hostOverride, originMap = {} } = {}) {
|
|
151
172
|
const chain = getVariations(custom, variationType);
|
|
173
|
+
const parsed = new URL(url);
|
|
174
|
+
const [originMapKey, originEntry] = findOriginMapEntry(originMap, parsed) ?? [];
|
|
175
|
+
const requestUrl = originEntry?.origin
|
|
176
|
+
? withOrigin(parsed, originEntry.origin)
|
|
177
|
+
: url;
|
|
178
|
+
const originalHost = originEntry?.origin && originEntry.overrideHostHeader
|
|
179
|
+
? parsed.hostname
|
|
180
|
+
: undefined;
|
|
152
181
|
const isTemplate = chain.some((config) => config.matchedKey !== WILD_CARD &&
|
|
153
182
|
!DEVICE_DEFAULTS[config.matchedKey ?? ""]) &&
|
|
154
183
|
!chain.some((config) => config.matchedKey?.startsWith("device-default:"));
|
|
184
|
+
const headers = buildHeaders(chain, hostOverride, originEntry, originalHost);
|
|
185
|
+
const originMapHeaders = Object.entries(originEntry?.headers ?? {})
|
|
186
|
+
.filter(([key, value]) => headers[key.toLowerCase()] === String(value))
|
|
187
|
+
.map(([key]) => key.toLowerCase());
|
|
155
188
|
return {
|
|
156
189
|
appliedKeys: chain.map((config) => config.matchedKey ?? "?"),
|
|
157
190
|
resolutionPath: isTemplate ? "template" : "named",
|
|
158
|
-
|
|
159
|
-
|
|
191
|
+
...(originMapKey ? { originMapKey, originMapHeaders } : {}),
|
|
192
|
+
url: buildUrl(requestUrl, chain),
|
|
193
|
+
headers,
|
|
160
194
|
};
|
|
161
195
|
}
|
|
162
196
|
/** Renders the resolved request as a curl command, for pasting into a bug report. */
|
|
@@ -27,8 +27,8 @@ export interface PreparedOriginRequest {
|
|
|
27
27
|
* dashboard diffs, `sk origin-request` and `sk diff` all fetch through it.
|
|
28
28
|
*
|
|
29
29
|
* The resolution itself is `resolveRequest`: the base headers, the wildcard-then-variation order,
|
|
30
|
-
* the regex templates with their `${group}` substitution,
|
|
31
|
-
* comes origin basic auth, which no variation carries.
|
|
30
|
+
* the regex templates with their `${group}` substitution, the query params and the origin map.
|
|
31
|
+
* On top of it comes origin basic auth, which no variation carries.
|
|
32
32
|
*/
|
|
33
33
|
export declare class OriginFetcher {
|
|
34
34
|
private readonly deps;
|
|
@@ -8,8 +8,8 @@ import { resolveRequest, UndefinedVariationError, } from "./asset-variations.js"
|
|
|
8
8
|
* dashboard diffs, `sk origin-request` and `sk diff` all fetch through it.
|
|
9
9
|
*
|
|
10
10
|
* The resolution itself is `resolveRequest`: the base headers, the wildcard-then-variation order,
|
|
11
|
-
* the regex templates with their `${group}` substitution,
|
|
12
|
-
* comes origin basic auth, which no variation carries.
|
|
11
|
+
* the regex templates with their `${group}` substitution, the query params and the origin map.
|
|
12
|
+
* On top of it comes origin basic auth, which no variation carries.
|
|
13
13
|
*/
|
|
14
14
|
export class OriginFetcher {
|
|
15
15
|
deps;
|
|
@@ -56,7 +56,10 @@ export class OriginFetcher {
|
|
|
56
56
|
}
|
|
57
57
|
resolve(variations, originUrl, variation) {
|
|
58
58
|
try {
|
|
59
|
-
return resolveRequest(variations, originUrl, variation,
|
|
59
|
+
return resolveRequest(variations, originUrl, variation, {
|
|
60
|
+
hostOverride: this.deps.host,
|
|
61
|
+
originMap: this.deps.config?.originMap,
|
|
62
|
+
});
|
|
60
63
|
}
|
|
61
64
|
catch (error) {
|
|
62
65
|
// Only a custom variation reaches this branch: a device variation (default, mobile, tablet)
|
|
@@ -72,7 +75,10 @@ export class OriginFetcher {
|
|
|
72
75
|
? `defined are: ${defined.join(", ")}. `
|
|
73
76
|
: "it defines no variations at all. ") +
|
|
74
77
|
"Falling back to the base headers.");
|
|
75
|
-
return resolveRequest({}, originUrl, undefined,
|
|
78
|
+
return resolveRequest({}, originUrl, undefined, {
|
|
79
|
+
hostOverride: this.deps.host,
|
|
80
|
+
originMap: this.deps.config?.originMap,
|
|
81
|
+
});
|
|
76
82
|
}
|
|
77
83
|
}
|
|
78
84
|
authorization(originUrl) {
|
|
@@ -48,7 +48,7 @@ export class OriginRequestServiceFactory {
|
|
|
48
48
|
if (!this.context.json) {
|
|
49
49
|
cli.writeWarning("no customer: the request carries Speed Kit's device defaults only, no variation of an app");
|
|
50
50
|
}
|
|
51
|
-
return { variations: {}, authentications: [] };
|
|
51
|
+
return { variations: {}, authentications: [], originMap: {} };
|
|
52
52
|
}
|
|
53
53
|
return new RuntimeConfigSource({
|
|
54
54
|
cli,
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { CliServiceInterface } from "../cli/index.js";
|
|
2
2
|
import { OriginRequestOptions } from "./origin-request-service-build-context.js";
|
|
3
3
|
import { OriginFetcher } from "./origin-fetcher.js";
|
|
4
|
+
import { ResolvedRequest } from "./asset-variations.js";
|
|
5
|
+
/**
|
|
6
|
+
* The headers as reported. Origin basic auth and the origin map's headers are sent, but their
|
|
7
|
+
* values are not printed: the runtime config stores credentials there, such as a bypass token.
|
|
8
|
+
*/
|
|
9
|
+
export declare function printable(headers: Record<string, string>, { originMapHeaders }: ResolvedRequest): Record<string, string>;
|
|
4
10
|
/**
|
|
5
11
|
* Fetches a page from origin with exactly the headers Speed Kit's server would send for a cache
|
|
6
12
|
* variation, then reports what the origin answered.
|
|
@@ -10,8 +16,6 @@ import { OriginFetcher } from "./origin-fetcher.js";
|
|
|
10
16
|
* deployed runtime config, so the simulation follows production rather than a local file.
|
|
11
17
|
*
|
|
12
18
|
* Known deviations from the server, all deliberate:
|
|
13
|
-
* - The origin map (origin rewriting plus its extra headers) lives outside the runtime config;
|
|
14
|
-
* use `--host` for the Host part.
|
|
15
19
|
* - Revalidation headers (If-None-Match, If-Modified-Since) apply only when refreshing a cached
|
|
16
20
|
* entry, so they are out of scope.
|
|
17
21
|
* - Header order and the HTTP version come from undici, not Jetty.
|
|
@@ -1,14 +1,34 @@
|
|
|
1
1
|
import { writeFile } from "node:fs/promises";
|
|
2
2
|
import { originFetch } from "../../helpers/origin-proxy.js";
|
|
3
3
|
import { DEFAULT_TYPE, toCurl } from "./asset-variations.js";
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
/** Standard request headers that carry no credentials, so an origin map value stays visible. */
|
|
5
|
+
const PLAIN_HEADERS = new Set([
|
|
6
|
+
"accept",
|
|
7
|
+
"accept-encoding",
|
|
8
|
+
"accept-language",
|
|
9
|
+
"cache-control",
|
|
10
|
+
"host",
|
|
11
|
+
"origin",
|
|
12
|
+
"pragma",
|
|
13
|
+
"referer",
|
|
14
|
+
"user-agent",
|
|
15
|
+
"x-forwarded-for",
|
|
16
|
+
"x-forwarded-host",
|
|
17
|
+
"x-forwarded-proto",
|
|
18
|
+
]);
|
|
19
|
+
/**
|
|
20
|
+
* The headers as reported. Origin basic auth and the origin map's headers are sent, but their
|
|
21
|
+
* values are not printed: the runtime config stores credentials there, such as a bypass token.
|
|
22
|
+
*/
|
|
23
|
+
export function printable(headers, { originMapHeaders = [] }) {
|
|
24
|
+
const result = { ...headers };
|
|
25
|
+
if (result["Authorization"])
|
|
26
|
+
result["Authorization"] = "Basic <credentials from the runtime config>";
|
|
27
|
+
for (const key of originMapHeaders) {
|
|
28
|
+
if (!PLAIN_HEADERS.has(key))
|
|
29
|
+
result[key] = "<from the origin map>";
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
12
32
|
}
|
|
13
33
|
/**
|
|
14
34
|
* Fetches a page from origin with exactly the headers Speed Kit's server would send for a cache
|
|
@@ -19,8 +39,6 @@ function printable(headers) {
|
|
|
19
39
|
* deployed runtime config, so the simulation follows production rather than a local file.
|
|
20
40
|
*
|
|
21
41
|
* Known deviations from the server, all deliberate:
|
|
22
|
-
* - The origin map (origin rewriting plus its extra headers) lives outside the runtime config;
|
|
23
|
-
* use `--host` for the Host part.
|
|
24
42
|
* - Revalidation headers (If-None-Match, If-Modified-Since) apply only when refreshing a cached
|
|
25
43
|
* entry, so they are out of scope.
|
|
26
44
|
* - Header order and the HTTP version come from undici, not Jetty.
|
|
@@ -44,7 +62,7 @@ export class OriginRequestService {
|
|
|
44
62
|
return false;
|
|
45
63
|
const variation = this.context.variation ?? DEFAULT_TYPE;
|
|
46
64
|
const { url, init, resolved } = this.fetcher.prepare(this.context.pageUrl, this.context.variation);
|
|
47
|
-
const headers = printable(init.headers);
|
|
65
|
+
const headers = printable(init.headers, resolved);
|
|
48
66
|
const response = await originFetch(url, { ...init, redirect: "manual" });
|
|
49
67
|
const body = await response.text();
|
|
50
68
|
const setCookies = response.headers.getSetCookie?.() ?? [];
|
|
@@ -55,6 +73,7 @@ export class OriginRequestService {
|
|
|
55
73
|
variation,
|
|
56
74
|
resolutionPath: resolved.resolutionPath,
|
|
57
75
|
appliedConfigs: resolved.appliedKeys,
|
|
76
|
+
originMapKey: resolved.originMapKey ?? null,
|
|
58
77
|
requestUrl: url,
|
|
59
78
|
requestHeaders: headers,
|
|
60
79
|
status: response.status,
|
|
@@ -70,6 +89,8 @@ export class OriginRequestService {
|
|
|
70
89
|
report(variation, resolved, headers, response, setCookies, body) {
|
|
71
90
|
this.cli.write(`variation ${variation}`);
|
|
72
91
|
this.cli.write(`resolution ${resolved.resolutionPath} → applied: ${resolved.appliedKeys.join(" then ")}`);
|
|
92
|
+
if (resolved.originMapKey)
|
|
93
|
+
this.cli.write(`origin map ${resolved.originMapKey}`);
|
|
73
94
|
this.cli.write(`request url ${resolved.url}`);
|
|
74
95
|
this.cli.write("request headers");
|
|
75
96
|
for (const [key, value] of Object.entries(headers))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CliServiceInterface } from "../cli/index.js";
|
|
2
2
|
import { ConfigApiService } from "../config-api/index.js";
|
|
3
|
-
import { AssetVariationMap } from "./asset-variations.js";
|
|
3
|
+
import { AssetVariationMap, OriginMap } from "./asset-variations.js";
|
|
4
4
|
/** Origin basic auth of the runtime config, `speedKit.authentications`. */
|
|
5
5
|
export interface OriginAuthentication {
|
|
6
6
|
readonly type: string;
|
|
@@ -12,6 +12,8 @@ export interface OriginAuthentication {
|
|
|
12
12
|
export interface OriginRequestConfig {
|
|
13
13
|
readonly variations: AssetVariationMap;
|
|
14
14
|
readonly authentications: readonly OriginAuthentication[];
|
|
15
|
+
/** `speedKit.originMap`: per origin, a rewrite target and extra headers. */
|
|
16
|
+
readonly originMap: OriginMap;
|
|
15
17
|
}
|
|
16
18
|
/** What loading the runtime config reports through. */
|
|
17
19
|
export type RuntimeConfigLog = Pick<CliServiceInterface, "writeError" | "writeSuccess" | "startAction" | "successAction" | "failAction">;
|
|
@@ -21,6 +21,7 @@ export class RuntimeConfigSource {
|
|
|
21
21
|
const config = {
|
|
22
22
|
variations: speedKit.variations ?? {},
|
|
23
23
|
authentications: speedKit.authentications ?? [],
|
|
24
|
+
originMap: speedKit.originMap ?? {},
|
|
24
25
|
};
|
|
25
26
|
const names = Object.keys(config.variations);
|
|
26
27
|
if (names.length > 0) {
|
|
@@ -81,4 +81,62 @@ describe("asset variations", () => {
|
|
|
81
81
|
it("reports an unknown variation instead of guessing", () => {
|
|
82
82
|
expect(() => getVariations({}, "b2b-unknown")).to.throw(UndefinedVariationError);
|
|
83
83
|
});
|
|
84
|
+
describe("origin map", () => {
|
|
85
|
+
it("sends an entry's headers, but lets the base and variation headers win", () => {
|
|
86
|
+
const { headers, originMapKey, originMapHeaders } = resolveRequest({ "*": { headers: { cookie: "from-variation" } } }, "https://www.example.com/plp", undefined, {
|
|
87
|
+
originMap: {
|
|
88
|
+
"www.example.com": {
|
|
89
|
+
headers: {
|
|
90
|
+
"X-Bypass": "token",
|
|
91
|
+
cookie: "from-origin-map",
|
|
92
|
+
"accept-language": "de",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
expect(originMapKey).to.equal("www.example.com");
|
|
98
|
+
expect(headers["x-bypass"]).to.equal("token");
|
|
99
|
+
expect(headers.cookie).to.equal("from-variation");
|
|
100
|
+
expect(headers["accept-language"]).to.equal("*");
|
|
101
|
+
expect(originMapHeaders).to.deep.equal(["x-bypass"]);
|
|
102
|
+
});
|
|
103
|
+
it("prefers the entry for scheme and host over the one for the host alone", () => {
|
|
104
|
+
const { headers, originMapKey } = resolveRequest({}, "https://www.example.com/plp", undefined, {
|
|
105
|
+
originMap: {
|
|
106
|
+
"www.example.com": { headers: { "x-entry": "host" } },
|
|
107
|
+
"https://www.example.com": { headers: { "x-entry": "origin" } },
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
expect(originMapKey).to.equal("https://www.example.com");
|
|
111
|
+
expect(headers["x-entry"]).to.equal("origin");
|
|
112
|
+
});
|
|
113
|
+
it("rewrites the host, keeps path and query, and keeps the Host header on request", () => {
|
|
114
|
+
const { url, headers } = resolveRequest({ "*": { queryParams: ["sk=1"] } }, "https://www.example.com/plp?page=2", undefined, {
|
|
115
|
+
originMap: {
|
|
116
|
+
"www.example.com": {
|
|
117
|
+
origin: "backend.example.net:8443",
|
|
118
|
+
overrideHostHeader: true,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
expect(url).to.equal("https://backend.example.net:8443/plp?page=2&sk=1");
|
|
123
|
+
expect(headers.host).to.equal("www.example.com");
|
|
124
|
+
});
|
|
125
|
+
it("takes the scheme from an origin that names one, and sends its own Host otherwise", () => {
|
|
126
|
+
const { url, headers } = resolveRequest({}, "https://www.example.com/plp", undefined, {
|
|
127
|
+
originMap: {
|
|
128
|
+
"www.example.com": { origin: "http://backend.example.net" },
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
expect(url).to.equal("http://backend.example.net/plp");
|
|
132
|
+
expect(headers.host).to.equal(undefined);
|
|
133
|
+
});
|
|
134
|
+
it("leaves a url without an entry untouched", () => {
|
|
135
|
+
const { url, originMapKey } = resolveRequest({}, "https://www.example.com/plp", undefined, {
|
|
136
|
+
originMap: { "shop.example.org": { origin: "backend.example.net" } },
|
|
137
|
+
});
|
|
138
|
+
expect(url).to.equal("https://www.example.com/plp");
|
|
139
|
+
expect(originMapKey).to.equal(undefined);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
84
142
|
});
|
|
@@ -27,6 +27,7 @@ function fetcher(cli, runtime) {
|
|
|
27
27
|
config: runtime && {
|
|
28
28
|
variations: runtime.speedKit.variations ?? {},
|
|
29
29
|
authentications: runtime.speedKit.authentications ?? [],
|
|
30
|
+
originMap: runtime.speedKit.originMap ?? {},
|
|
30
31
|
},
|
|
31
32
|
});
|
|
32
33
|
}
|
|
@@ -121,11 +122,25 @@ describe("OriginFetcher.fetch", () => {
|
|
|
121
122
|
await fetcher(cli, runtime).fetch(url, "default");
|
|
122
123
|
expect(received.authorization).to.equal(`Basic ${Buffer.from("shop:secret").toString("base64")}`);
|
|
123
124
|
});
|
|
125
|
+
it("sends the origin map's headers for a device variation, which carries none of its own", async () => {
|
|
126
|
+
const { cli } = stubCli();
|
|
127
|
+
const runtime = {
|
|
128
|
+
speedKit: {
|
|
129
|
+
originMap: {
|
|
130
|
+
[new URL(url).origin]: {
|
|
131
|
+
headers: { "x-vercel-protection-bypass": "token" },
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
await fetcher(cli, runtime).fetch(url, "default");
|
|
137
|
+
expect(received["x-vercel-protection-bypass"]).to.equal("token");
|
|
138
|
+
});
|
|
124
139
|
it("refuses an undefined variation when strict, as the server does", () => {
|
|
125
140
|
const { cli } = stubCli();
|
|
126
141
|
const strict = new OriginFetcher({
|
|
127
142
|
cli,
|
|
128
|
-
config: { variations: {}, authentications: [] },
|
|
143
|
+
config: { variations: {}, authentications: [], originMap: {} },
|
|
129
144
|
strict: true,
|
|
130
145
|
});
|
|
131
146
|
expect(() => strict.prepare(url, "b2b")).to.throw(/b2b not found/);
|
|
@@ -1,9 +1,35 @@
|
|
|
1
1
|
import { expect } from "chai";
|
|
2
2
|
import { describe, it } from "mocha";
|
|
3
|
-
import { OriginRequestService } from "../origin-request-service.js";
|
|
3
|
+
import { OriginRequestService, printable } from "../origin-request-service.js";
|
|
4
4
|
describe("OriginRequestService", () => {
|
|
5
5
|
it("reports that it sent nothing when the runtime config was not usable", async () => {
|
|
6
6
|
const service = new OriginRequestService({ pageUrl: "https://shop.test/" }, {}, undefined);
|
|
7
7
|
expect(await service.run()).to.equal(false);
|
|
8
8
|
});
|
|
9
|
+
it("prints neither basic auth nor an origin map credential, but keeps plain headers", () => {
|
|
10
|
+
const headers = printable({
|
|
11
|
+
Authorization: "Basic dXNlcjpwYXNz",
|
|
12
|
+
"x-vercel-protection-bypass": "secret",
|
|
13
|
+
cookie: "session=1",
|
|
14
|
+
"cache-control": "no-cache",
|
|
15
|
+
"x-variant": "b2b",
|
|
16
|
+
}, {
|
|
17
|
+
appliedKeys: [],
|
|
18
|
+
resolutionPath: "named",
|
|
19
|
+
originMapHeaders: [
|
|
20
|
+
"x-vercel-protection-bypass",
|
|
21
|
+
"cookie",
|
|
22
|
+
"cache-control",
|
|
23
|
+
],
|
|
24
|
+
url: "https://shop.test/",
|
|
25
|
+
headers: {},
|
|
26
|
+
});
|
|
27
|
+
expect(headers).to.deep.equal({
|
|
28
|
+
Authorization: "Basic <credentials from the runtime config>",
|
|
29
|
+
"x-vercel-protection-bypass": "<from the origin map>",
|
|
30
|
+
cookie: "<from the origin map>",
|
|
31
|
+
"cache-control": "no-cache",
|
|
32
|
+
"x-variant": "b2b",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
9
35
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { afterEach, beforeEach, describe, it } from "mocha";
|
|
6
|
+
import { RuntimeConfigSource } from "../runtime-config-source.js";
|
|
7
|
+
const quietCli = {
|
|
8
|
+
writeError: () => undefined,
|
|
9
|
+
writeSuccess: () => undefined,
|
|
10
|
+
startAction: () => undefined,
|
|
11
|
+
successAction: () => undefined,
|
|
12
|
+
failAction: () => undefined,
|
|
13
|
+
};
|
|
14
|
+
// RuntimeConfigSource reads with node:fs/promises, so these run against a temp dir rather than memfs.
|
|
15
|
+
describe("RuntimeConfigSource", () => {
|
|
16
|
+
let dir;
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
dir = await mkdtemp(join(tmpdir(), "runtime-config-"));
|
|
19
|
+
});
|
|
20
|
+
afterEach(() => rm(dir, { recursive: true, force: true }));
|
|
21
|
+
it("loads the origin map", async () => {
|
|
22
|
+
const file = join(dir, "runtime.json");
|
|
23
|
+
await writeFile(file, JSON.stringify({
|
|
24
|
+
speedKit: {
|
|
25
|
+
originMap: {
|
|
26
|
+
"www.example.com": {
|
|
27
|
+
origin: "backend.example.net",
|
|
28
|
+
overrideHostHeader: true,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
}));
|
|
33
|
+
const config = await new RuntimeConfigSource({ cli: quietCli }).load(file);
|
|
34
|
+
expect(config.originMap).to.deep.equal({
|
|
35
|
+
"www.example.com": {
|
|
36
|
+
origin: "backend.example.net",
|
|
37
|
+
overrideHostHeader: true,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
it("loads a config without an origin map as an empty one", async () => {
|
|
42
|
+
const file = join(dir, "runtime.json");
|
|
43
|
+
await writeFile(file, JSON.stringify({ speedKit: { variations: {} } }));
|
|
44
|
+
const config = await new RuntimeConfigSource({ cli: quietCli }).load(file);
|
|
45
|
+
expect(config.originMap).to.deep.equal({});
|
|
46
|
+
});
|
|
47
|
+
});
|
package/oclif.manifest.json
CHANGED