@speedkit/cli 4.48.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +8 -1
  3. package/dist/commands/onboarding.d.ts +2 -0
  4. package/dist/commands/onboarding.js +1 -0
  5. package/dist/services/browser-fetch/device-emulation.d.ts +16 -3
  6. package/dist/services/browser-fetch/device-emulation.js +32 -17
  7. package/dist/services/browser-fetch/spec/device-emulation.spec.js +20 -0
  8. package/dist/services/diff/diff-service-factory.js +1 -1
  9. package/dist/services/diff/spec/page-source.spec.js +1 -1
  10. package/dist/services/onboarding/browser/emulate-device.d.ts +13 -0
  11. package/dist/services/onboarding/browser/emulate-device.js +50 -0
  12. package/dist/services/onboarding/browser/launch-browser.js +7 -2
  13. package/dist/services/onboarding/onboarding-model.d.ts +27 -6
  14. package/dist/services/onboarding/onboarding-model.js +88 -15
  15. package/dist/services/onboarding/onboarding-service-build-context.d.ts +7 -0
  16. package/dist/services/onboarding/onboarding-service-build-context.js +12 -0
  17. package/dist/services/onboarding/onboarding-service-factory.d.ts +7 -0
  18. package/dist/services/onboarding/onboarding-service-factory.js +34 -1
  19. package/dist/services/onboarding/spec/onboarding-model.spec.js +93 -2
  20. package/dist/services/onboarding/spec/onboarding-service-build-context.spec.js +16 -1
  21. package/dist/services/origin-request/asset-variations.d.ts +24 -3
  22. package/dist/services/origin-request/asset-variations.js +51 -17
  23. package/dist/services/origin-request/origin-fetcher.d.ts +2 -2
  24. package/dist/services/origin-request/origin-fetcher.js +10 -4
  25. package/dist/services/origin-request/origin-request-service-factory.js +1 -1
  26. package/dist/services/origin-request/origin-request-service.d.ts +6 -2
  27. package/dist/services/origin-request/origin-request-service.js +32 -11
  28. package/dist/services/origin-request/runtime-config-source.d.ts +3 -1
  29. package/dist/services/origin-request/runtime-config-source.js +1 -0
  30. package/dist/services/origin-request/spec/asset-variations.spec.js +58 -0
  31. package/dist/services/origin-request/spec/origin-fetcher.spec.js +16 -1
  32. package/dist/services/origin-request/spec/origin-request-service.spec.js +27 -1
  33. package/dist/services/origin-request/spec/runtime-config-source.spec.d.ts +1 -0
  34. package/dist/services/origin-request/spec/runtime-config-source.spec.js +47 -0
  35. package/oclif.manifest.json +24 -1
  36. package/package.json +1 -1
@@ -50,6 +50,13 @@ export declare class OnboardingServiceFactory {
50
50
  customerChromeFlags?: string[];
51
51
  }): BrowserContext;
52
52
  private getOnboardingService;
53
+ /**
54
+ * `--device` only changes what Speed Kit does if the config derives the device from the user
55
+ * agent. `RequestContext.getDevice` short-circuits on a pinned `customDevice` and returns nothing
56
+ * at all when `userAgentDetection` is off. In both cases no `bqvariation` is appended and the
57
+ * emulated device silently gets desktop assets. Say so up front.
58
+ */
59
+ private warnAboutDeviceDetection;
53
60
  private prepareMessagehandler;
54
61
  /**
55
62
  * The app token for the devtools extension, or `null` when nobody is logged
@@ -201,11 +201,15 @@ export class OnboardingServiceFactory {
201
201
  if (this.context.headless) {
202
202
  cli.writeWarning("Running in headless mode.");
203
203
  }
204
+ if (browserContext.emulation) {
205
+ const { device, userAgent, viewport } = browserContext.emulation;
206
+ cli.writeWarning(`Emulating ${device} (${viewport.width}x${viewport.height} @${viewport.deviceScaleFactor}x) with ${userAgent}`);
207
+ }
204
208
  return { browserContext, browserExtensions };
205
209
  }
206
210
  /** The launch for `domain`. It reads no mode of the build context, so browse mode cannot drift from onboarding. */
207
211
  buildBrowserContext({ domain, browserVersionString, customerChromeFlags, }) {
208
- return new BrowserContext(domain, browserVersionString, withOriginProxyFlag(customerChromeFlags), this.context.environment, undefined, this.context.debuggingPort, this.context.headless, this.context.debugPort);
212
+ return new BrowserContext(domain, browserVersionString, withOriginProxyFlag(customerChromeFlags), this.context.environment, undefined, this.context.debuggingPort, this.context.headless, this.context.debugPort, this.context.device);
209
213
  }
210
214
  async getOnboardingService() {
211
215
  const cli = this.context.environment.cli;
@@ -236,6 +240,9 @@ export class OnboardingServiceFactory {
236
240
  const speedKitInstallContext = new SpeedKitInstallContext(customerConfig, this.context.configName, files, bundler, skTemplate);
237
241
  await this.buildInstallResource(speedKitInstallContext, files, cli);
238
242
  const { browserContext, browserExtensions } = await this.prepareBrowser(domainToStart, customerConfig.chromeFlags);
243
+ if (this.context.device) {
244
+ await this.warnAboutDeviceDetection(files, customerConfig, domainToStart);
245
+ }
239
246
  const fileWatcher = new FileWatcher(cli, customerConfig, files, documentHandler, browserContext, cache);
240
247
  const athenaClient = (await this.getAthenaClient(customerConfig.app)) || null;
241
248
  const messageHandler = await this.prepareMessagehandler(athenaClient, files, customerConfig);
@@ -244,6 +251,32 @@ export class OnboardingServiceFactory {
244
251
  const extensionBridge = new ExtensionBridge(cli, browserExtensions.hasDevtoolsExtension);
245
252
  return new OnboardingService(browserContext, customerConfig, fetchHandler, fileWatcher, cli, messageHandler, extensionBridge, speedKitServiceWorkerJs, browserFetch);
246
253
  }
254
+ /**
255
+ * `--device` only changes what Speed Kit does if the config derives the device from the user
256
+ * agent. `RequestContext.getDevice` short-circuits on a pinned `customDevice` and returns nothing
257
+ * at all when `userAgentDetection` is off. In both cases no `bqvariation` is appended and the
258
+ * emulated device silently gets desktop assets. Say so up front.
259
+ */
260
+ async warnAboutDeviceDetection(files, customerConfig, domain) {
261
+ const cli = this.context.environment.cli;
262
+ const speedKitConfig = files.getSpeedKitConfig();
263
+ if (!speedKitConfig) {
264
+ return;
265
+ }
266
+ const config = await safe(speedKitConfig.evaluate(customerConfig.app, new URL(domain).origin));
267
+ if (config.success !== true) {
268
+ cli.writeWarning(`Could not check the Speed Kit config for device detection: ${config.error}`);
269
+ return;
270
+ }
271
+ // `raw` for the pin: `SSRPlugin` wraps `detectDevice` in `effective` for every config.
272
+ const { raw, effective } = config.data;
273
+ if (!effective.userAgentDetection && this.context.device !== "desktop") {
274
+ cli.writeWarning(`userAgentDetection is off in the Speed Kit config — Speed Kit resolves the desktop variation regardless of --device ${this.context.device}.`);
275
+ }
276
+ if (raw.customDevice || raw.detectDevice) {
277
+ cli.writeWarning("The Speed Kit config pins the device via customDevice/detectDevice, which takes precedence over the emulated user agent.");
278
+ }
279
+ }
247
280
  async prepareMessagehandler(athenaClient, files, customerConfig) {
248
281
  const messageHandler = new DevtoolsExtensionApi();
249
282
  messageHandler.addMessage({
@@ -1,6 +1,7 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
3
  import { BrowserContext, HEADLESS_DEFAULT_ARGS_TO_DROP, } from "../onboarding-model.js";
4
+ import { DEVICE_VIEWPORTS, deviceEmulation, } from "../../browser-fetch/device-emulation.js";
4
5
  import { buildHeadedUserAgent } from "../browser/executable/headed-user-agent.js";
5
6
  // Providing chromeUserProfilePath keeps BrowserContext off the filesystem
6
7
  // (it skips the temp-profile branch in the constructor).
@@ -10,8 +11,8 @@ const environment = {
10
11
  chromeUserProfilePath: "/tmp/sk-test-profile",
11
12
  },
12
13
  };
13
- function newContext(headless, { chromeFlags = [], userAgent, browserVersionString = "Google Chrome for Testing 146.0.7680.165", } = {}) {
14
- return new BrowserContext("example.com", browserVersionString, chromeFlags, environment, userAgent, false, headless, undefined);
14
+ function newContext(headless, { chromeFlags = [], userAgent, browserVersionString = "Google Chrome for Testing 146.0.7680.165", device, } = {}) {
15
+ return new BrowserContext("example.com", browserVersionString, chromeFlags, environment, userAgent, false, headless, undefined, device);
15
16
  }
16
17
  function launchArgs(headless, overrides = {}) {
17
18
  return newContext(headless, overrides).getPuppeteerLaunchOptions()
@@ -98,6 +99,96 @@ describe("BrowserContext headless/headed parity", () => {
98
99
  expect(launchArgs(true).length).to.equal(first);
99
100
  });
100
101
  });
102
+ describe("BrowserContext device emulation", () => {
103
+ const VERSION = "Google Chrome for Testing 146.0.7680.165";
104
+ const argOf = (args, name) => args.find((arg) => arg.startsWith(`--${name}=`));
105
+ for (const device of ["mobile", "tablet", "desktop"]) {
106
+ // A Service Worker resolves the device from its own navigator.userAgent, which only a launch
107
+ // flag reaches, so headed mode carries the flag too.
108
+ it(`sets the ${device} user agent as a launch flag in headed mode`, () => {
109
+ expect(argOf(launchArgs(false, { device }), "user-agent")).to.equal(`--user-agent=${deviceEmulation(VERSION, device, "win32").userAgent}`);
110
+ });
111
+ it(`sizes the headless window and screen to the ${device}`, () => {
112
+ const { width, height } = newContext(true, { device }).emulation.viewport;
113
+ expect(launchArgs(true, { device })).to.include.members([
114
+ `--window-size=${width},${height}`,
115
+ `--screen-info={${width}x${height}}`,
116
+ ]);
117
+ });
118
+ }
119
+ it("asks for a coarse pointer, no hover and touch events on a touch device", () => {
120
+ for (const device of ["mobile", "tablet"]) {
121
+ const args = launchArgs(true, { device });
122
+ expect(argOf(args, "blink-settings")).to.equal("--blink-settings=primaryHoverType=1,availableHoverTypes=1,primaryPointerType=2,availablePointerTypes=2");
123
+ expect(args).to.include("--touch-events=enabled");
124
+ }
125
+ });
126
+ it("asks for a fine pointer and no touch events on the desktop", () => {
127
+ const args = launchArgs(false, { device: "desktop" });
128
+ expect(argOf(args, "blink-settings")).to.equal("--blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4");
129
+ expect(args).to.include("--touch-events=disabled");
130
+ });
131
+ it("gives the desktop an explicit viewport", () => {
132
+ expect(newContext(false, { device: "desktop" }).emulation.viewport).to.equal(DEVICE_VIEWPORTS.desktop);
133
+ });
134
+ it("presents the desktop as Chrome on Windows, whatever the host", () => {
135
+ const { userAgent, metadata } = newContext(false, {
136
+ device: "desktop",
137
+ }).emulation;
138
+ expect(userAgent).to.equal("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36");
139
+ expect(metadata).to.include({ platform: "Windows", mobile: false });
140
+ });
141
+ it("wins over the headless user agent", () => {
142
+ expect(argOf(launchArgs(true, { device: "tablet" }), "user-agent")).to.include("Android");
143
+ });
144
+ it("still prefers an explicitly provided user agent", () => {
145
+ expect(argOf(launchArgs(false, { device: "mobile", userAgent: "custom" }), "user-agent")).to.equal("--user-agent=custom");
146
+ });
147
+ it("leaves room for docked devtools in headed mode", () => {
148
+ const [width] = argOf(launchArgs(false, { device: "mobile" }), "window-size")
149
+ .split("=")[1]
150
+ .split(",")
151
+ .map(Number);
152
+ expect(width).to.be.greaterThan(newContext(false, { device: "mobile" }).emulation.viewport.width);
153
+ });
154
+ // A phone has overlay scrollbars, so Puppeteer's headless default is right there.
155
+ it("keeps --hide-scrollbars only for a headless touch device", () => {
156
+ const ignored = (device) => newContext(true, { device }).getPuppeteerLaunchOptions()
157
+ .ignoreDefaultArgs;
158
+ expect(ignored("mobile")).to.not.include("--hide-scrollbars");
159
+ expect(ignored("desktop")).to.include("--hide-scrollbars");
160
+ });
161
+ it("lets customer chromeFlags win over the device defaults", () => {
162
+ const args = launchArgs(false, {
163
+ device: "mobile",
164
+ chromeFlags: ["--user-agent=pinned-agent"],
165
+ });
166
+ expect(args.lastIndexOf("--user-agent=pinned-agent")).to.be.greaterThan(args.findIndex((arg) => arg.startsWith("--user-agent=Mozilla/5.0")));
167
+ });
168
+ it("emulates nothing without a device", () => {
169
+ const context = newContext(false);
170
+ expect(context.emulation).to.equal(undefined);
171
+ expect(context.getDeviceMetricsOverride()).to.equal(null);
172
+ expect(context.getTouchEmulation()).to.equal(null);
173
+ });
174
+ // screenWidth/screenHeight matter beyond the viewport: the snippet persists window.screen.width and
175
+ // devicePixelRatio into the SW cache, where they drive Asset API image downscaling.
176
+ it("overrides the screen and orientation along with the viewport", () => {
177
+ expect(newContext(false, { device: "tablet" }).getDeviceMetricsOverride()).to.deep.equal({
178
+ width: 1280,
179
+ height: 800,
180
+ screenWidth: 1280,
181
+ screenHeight: 800,
182
+ deviceScaleFactor: 1.5,
183
+ mobile: true,
184
+ screenOrientation: { type: "landscapePrimary", angle: 90 },
185
+ });
186
+ });
187
+ it("enables touch for a touch device and disables it for the desktop", () => {
188
+ expect(newContext(false, { device: "mobile" }).getTouchEmulation()).to.deep.equal({ enabled: true, maxTouchPoints: 5 });
189
+ expect(newContext(false, { device: "desktop" }).getTouchEmulation()).to.deep.equal({ enabled: false });
190
+ });
191
+ });
101
192
  describe("BrowserContext system chromeFlags", () => {
102
193
  function systemContext(chromeFlags, systemFlags, debuggingPort = false) {
103
194
  return new BrowserContext("example.com", "Chrome/120", chromeFlags, {
@@ -1,6 +1,6 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
- import { withInjectDefault } from "../onboarding-service-build-context.js";
3
+ import { OnboardingServiceBuildContext, withInjectDefault, } from "../onboarding-service-build-context.js";
4
4
  describe("withInjectDefault", () => {
5
5
  it("gives --inject at the end the latest installation", () => {
6
6
  expect(withInjectDefault(["customers/shop", "--inject"])).to.deep.equal([
@@ -20,3 +20,18 @@ describe("withInjectDefault", () => {
20
20
  expect(withInjectDefault(argv)).to.deep.equal(argv);
21
21
  });
22
22
  });
23
+ describe("OnboardingServiceBuildContext.create device", () => {
24
+ const environment = { oclif: { root: "/cli" } };
25
+ function deviceOf(flags) {
26
+ return OnboardingServiceBuildContext.create({ customerPath: undefined }, flags, environment).device;
27
+ }
28
+ it("takes the device of --device", () => {
29
+ expect(deviceOf({ device: "tablet" })).to.equal("tablet");
30
+ });
31
+ it("reads --mobile as --device mobile", () => {
32
+ expect(deviceOf({ mobile: true })).to.equal("mobile");
33
+ });
34
+ it("emulates no device without either flag", () => {
35
+ expect(deviceOf({ mobile: false })).to.equal(undefined);
36
+ });
37
+ });
@@ -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()` — base headers, last-writer-wins merging,
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?: string): ResolvedRequest;
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()` — base headers, last-writer-wins merging,
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
- * Base headers from AssetClient.newRequest, then each config's headers with the last writer
125
- * winning. A `null` value removes the header, which is how a config drops Accept-Language.
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 buildHeaders(chain, hostOverride) {
128
- const headers = new Map([
129
- [
130
- "accept",
131
- "text/html, application/xhtml+xml, application/xml;q=0.9, image/avif, image/webp, */*;q=0.8",
132
- ],
133
- ["accept-encoding", "gzip"],
134
- ["accept-language", "*"],
135
- ["user-agent", DEFAULT_USER_AGENT],
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
- url: buildUrl(url, chain),
159
- headers: buildHeaders(chain, hostOverride),
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, and the query params. On top of it
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, and the query params. On top of it
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, this.deps.host);
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, this.deps.host);
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
- /** The headers as reported: origin basic auth is sent, but its credentials are not printed. */
5
- function printable(headers) {
6
- return headers["Authorization"]
7
- ? {
8
- ...headers,
9
- Authorization: "Basic <credentials from the runtime config>",
10
- }
11
- : headers;
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
  });