@speedkit/cli 4.44.0-beta.4 → 4.45.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 (196) hide show
  1. package/CHANGELOG.md +40 -11
  2. package/README.md +77 -47
  3. package/dist/base/sk-command.d.ts +7 -0
  4. package/dist/base/sk-command.js +19 -0
  5. package/dist/commands/deploy.d.ts +1 -0
  6. package/dist/commands/deploy.js +5 -1
  7. package/dist/commands/diff.d.ts +47 -0
  8. package/dist/commands/diff.js +67 -0
  9. package/dist/commands/generate-pop-config.js +3 -7
  10. package/dist/commands/origin-request.d.ts +2 -1
  11. package/dist/commands/origin-request.js +1 -0
  12. package/dist/helpers/race.js +4 -2
  13. package/dist/helpers/spec/race.spec.js +31 -0
  14. package/dist/models/files.d.ts +0 -1
  15. package/dist/models/files.js +0 -1
  16. package/dist/services/browser-fetch/browser-origin-fetch.js +1 -1
  17. package/dist/services/browser-fetch/browser-origin-fetcher.d.ts +1 -7
  18. package/dist/services/browser-fetch/browser-origin-fetcher.js +3 -26
  19. package/dist/services/bundler/bundle-service-factory.d.ts +0 -3
  20. package/dist/services/bundler/bundle-service-factory.js +1 -5
  21. package/dist/services/bundler/bundle-service.d.ts +1 -10
  22. package/dist/services/bundler/bundle-service.js +1 -16
  23. package/dist/services/bundler/index.d.ts +0 -1
  24. package/dist/services/bundler/index.js +0 -1
  25. package/dist/services/cli/cli-environment.d.ts +11 -0
  26. package/dist/services/cli/cli-environment.js +37 -0
  27. package/dist/services/cli/cli-service-model.d.ts +3 -1
  28. package/dist/services/cli/cli-service.d.ts +1 -1
  29. package/dist/services/cli/cli-service.js +4 -1
  30. package/dist/services/cli/spec/cli-environment.spec.js +30 -1
  31. package/dist/services/deploy/checks/activation/customer-approval-check.js +1 -1
  32. package/dist/services/deploy/checks/post-deploy/refresh-deployment-job-check.d.ts +2 -2
  33. package/dist/services/deploy/checks/post-deploy/refresh-deployment-job-check.js +8 -7
  34. package/dist/services/deploy/checks/post-deploy/revalidated-module-check.d.ts +2 -2
  35. package/dist/services/deploy/checks/post-deploy/revalidated-module-check.js +11 -7
  36. package/dist/services/deploy/checks/post-deploy/save-files-check.d.ts +2 -2
  37. package/dist/services/deploy/checks/post-deploy/save-files-check.js +3 -2
  38. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +3 -2
  39. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +3 -2
  40. package/dist/services/deploy/deploy-service-build-context.d.ts +2 -0
  41. package/dist/services/deploy/deploy-service-build-context.js +13 -2
  42. package/dist/services/deploy/deploy-service-factory.js +2 -9
  43. package/dist/services/deploy/deploy-service-model.d.ts +11 -1
  44. package/dist/services/deploy/deploy-service.d.ts +2 -0
  45. package/dist/services/deploy/deploy-service.js +26 -1
  46. package/dist/services/deploy/handler/customer-config-handler.d.ts +2 -2
  47. package/dist/services/deploy/handler/install-resource-handler.d.ts +2 -2
  48. package/dist/services/deploy/handler/install-resource-handler.js +6 -3
  49. package/dist/services/deploy/handler/module-handler.d.ts +2 -2
  50. package/dist/services/deploy/handler/module-handler.js +2 -2
  51. package/dist/services/deploy/handler/server-config-handler.d.ts +2 -2
  52. package/dist/services/deploy/handler/server-config-handler.js +2 -2
  53. package/dist/services/deploy/mr/live-state-check.d.ts +2 -2
  54. package/dist/services/deploy/mr/merge-request-deploy-service-factory.js +2 -7
  55. package/dist/services/deploy/policy/deploy-policy-model.d.ts +1 -5
  56. package/dist/services/deploy/policy/deploy-policy-model.js +0 -4
  57. package/dist/services/deploy/policy/index.d.ts +1 -0
  58. package/dist/services/deploy/policy/index.js +1 -0
  59. package/dist/services/deploy/policy/testing-deploy-policy.d.ts +16 -0
  60. package/dist/services/deploy/policy/testing-deploy-policy.js +42 -0
  61. package/dist/services/deploy/spec/checks/post-deploy/refresh-deployment-job-check.spec.js +5 -12
  62. package/dist/services/deploy/spec/deploy-service.spec.js +64 -0
  63. package/dist/services/deploy/spec/handler/install-resource-handler.spec.js +58 -0
  64. package/dist/services/deploy/spec/mr/live-state-check.spec.js +2 -2
  65. package/dist/services/deploy/spec/policy/auto-deploy-policy.spec.js +0 -2
  66. package/dist/services/deploy/spec/policy/testing-deploy-policy.spec.d.ts +1 -0
  67. package/dist/services/deploy/spec/policy/testing-deploy-policy.spec.js +46 -0
  68. package/dist/services/diff/create-diff-engine.d.ts +14 -0
  69. package/dist/services/diff/create-diff-engine.js +42 -0
  70. package/dist/services/diff/customer-origin.d.ts +9 -0
  71. package/dist/services/diff/customer-origin.js +19 -0
  72. package/dist/services/diff/diff-engine.d.ts +30 -0
  73. package/dist/services/diff/diff-engine.js +49 -0
  74. package/dist/services/diff/diff-log.d.ts +9 -0
  75. package/dist/services/diff/diff-log.js +18 -0
  76. package/dist/services/diff/diff-model.d.ts +68 -0
  77. package/dist/services/diff/diff-model.js +1 -0
  78. package/dist/services/diff/diff-service-build-context.d.ts +99 -9
  79. package/dist/services/diff/diff-service-build-context.js +299 -1
  80. package/dist/services/diff/diff-service-factory.d.ts +24 -2
  81. package/dist/services/diff/diff-service-factory.js +165 -3
  82. package/dist/services/diff/diff-service.d.ts +93 -33
  83. package/dist/services/diff/diff-service.js +274 -140
  84. package/dist/services/diff/index.d.ts +12 -1
  85. package/dist/services/diff/index.js +12 -1
  86. package/dist/services/diff/noise-baseline.d.ts +28 -0
  87. package/dist/services/diff/noise-baseline.js +105 -0
  88. package/dist/services/diff/page-source.d.ts +42 -0
  89. package/dist/services/diff/page-source.js +93 -0
  90. package/dist/services/diff/side-failure.d.ts +28 -0
  91. package/dist/services/diff/side-failure.js +100 -0
  92. package/dist/services/diff/sinks/json-diff-sink.d.ts +32 -0
  93. package/dist/services/diff/sinks/json-diff-sink.js +44 -0
  94. package/dist/services/diff/sinks/no-diff-sink.d.ts +5 -0
  95. package/dist/services/diff/sinks/no-diff-sink.js +6 -0
  96. package/dist/services/diff/sinks/patch-diff-sink.d.ts +19 -0
  97. package/dist/services/diff/sinks/patch-diff-sink.js +29 -0
  98. package/dist/services/diff/sinks/tool-diff-sink.d.ts +26 -0
  99. package/dist/services/diff/sinks/tool-diff-sink.js +88 -0
  100. package/dist/services/diff/sinks/unified-patch.d.ts +7 -0
  101. package/dist/services/diff/sinks/unified-patch.js +12 -0
  102. package/dist/services/diff/spec/customer-origin.spec.d.ts +1 -0
  103. package/dist/services/diff/spec/customer-origin.spec.js +27 -0
  104. package/dist/services/diff/spec/diff-engine.spec.d.ts +1 -0
  105. package/dist/services/diff/spec/diff-engine.spec.js +47 -0
  106. package/dist/services/diff/spec/diff-service-build-context.spec.d.ts +1 -0
  107. package/dist/services/diff/spec/diff-service-build-context.spec.js +185 -0
  108. package/dist/services/diff/spec/diff-service.spec.d.ts +1 -0
  109. package/dist/services/diff/spec/diff-service.spec.js +393 -0
  110. package/dist/services/diff/spec/noise-baseline.spec.d.ts +1 -0
  111. package/dist/services/diff/spec/noise-baseline.spec.js +95 -0
  112. package/dist/services/diff/spec/page-source.spec.d.ts +1 -0
  113. package/dist/services/diff/spec/page-source.spec.js +140 -0
  114. package/dist/services/diff/spec/sinks/json-diff-sink.spec.d.ts +1 -0
  115. package/dist/services/diff/spec/sinks/json-diff-sink.spec.js +72 -0
  116. package/dist/services/diff/spec/sinks/patch-diff-sink.spec.d.ts +1 -0
  117. package/dist/services/diff/spec/sinks/patch-diff-sink.spec.js +47 -0
  118. package/dist/services/diff/spec/sinks/tool-diff-sink.spec.d.ts +1 -0
  119. package/dist/services/diff/spec/sinks/tool-diff-sink.spec.js +61 -0
  120. package/dist/services/document-handler-runtime/document-handler-abort.d.ts +2 -0
  121. package/dist/services/document-handler-runtime/document-handler-abort.js +2 -0
  122. package/dist/services/document-handler-runtime/document-handler-server.d.ts +0 -17
  123. package/dist/services/document-handler-runtime/document-handler-server.js +19 -81
  124. package/dist/services/document-handler-runtime/runtime-files.d.ts +9 -0
  125. package/dist/services/document-handler-runtime/runtime-files.js +17 -0
  126. package/dist/services/integration-api/integration-api-model.d.ts +1 -2
  127. package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +8 -68
  128. package/dist/services/integration-api/struct/customer-config.d.ts +1 -32
  129. package/dist/services/integration-api/struct/customer-config.js +3 -33
  130. package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +4 -9
  131. package/dist/services/integration-api/struct/speed-kit-config-file.js +10 -27
  132. package/dist/services/onboarding/config-renderer/speed-kit-install-context.js +8 -25
  133. package/dist/services/onboarding/dashboard/diff-against-current-page.d.ts +29 -7
  134. package/dist/services/onboarding/dashboard/diff-against-current-page.js +24 -37
  135. package/dist/services/onboarding/dashboard/index.js +13 -6
  136. package/dist/services/onboarding/dashboard/request-diff-service.d.ts +21 -8
  137. package/dist/services/onboarding/dashboard/request-diff-service.js +21 -49
  138. package/dist/services/onboarding/onboarding-model.d.ts +0 -1
  139. package/dist/services/onboarding/onboarding-model.js +0 -1
  140. package/dist/services/onboarding/onboarding-service-factory.d.ts +0 -1
  141. package/dist/services/onboarding/onboarding-service-factory.js +24 -30
  142. package/dist/services/onboarding/spec/dashboard/request-diff-service.spec.d.ts +1 -0
  143. package/dist/services/onboarding/spec/dashboard/request-diff-service.spec.js +57 -0
  144. package/dist/services/onboarding/templates/install-speed-kit-js-template.js +0 -5
  145. package/dist/services/onboarding/virtual-orestes-app/index.d.ts +3 -3
  146. package/dist/services/onboarding/virtual-orestes-app/index.js +5 -5
  147. package/dist/services/origin-request/index.d.ts +3 -0
  148. package/dist/services/origin-request/index.js +3 -0
  149. package/dist/services/origin-request/origin-fetcher.d.ts +52 -0
  150. package/dist/services/origin-request/origin-fetcher.js +93 -0
  151. package/dist/services/origin-request/origin-request-service-build-context.d.ts +6 -3
  152. package/dist/services/origin-request/origin-request-service-build-context.js +30 -4
  153. package/dist/services/origin-request/origin-request-service-factory.d.ts +5 -0
  154. package/dist/services/origin-request/origin-request-service-factory.js +40 -6
  155. package/dist/services/origin-request/origin-request-service.d.ts +6 -10
  156. package/dist/services/origin-request/origin-request-service.js +28 -70
  157. package/dist/services/origin-request/runtime-config-source.d.ts +36 -0
  158. package/dist/services/origin-request/runtime-config-source.js +59 -0
  159. package/dist/services/origin-request/spec/origin-error-cause.spec.d.ts +1 -0
  160. package/dist/services/{onboarding/spec/virtual-orestes-app → origin-request/spec}/origin-error-cause.spec.js +1 -1
  161. package/dist/services/origin-request/spec/origin-fetcher.spec.d.ts +1 -0
  162. package/dist/services/{onboarding/spec/virtual-orestes-app/crawler.spec.js → origin-request/spec/origin-fetcher.spec.js} +46 -15
  163. package/dist/services/origin-request/spec/origin-request-service-build-context.spec.d.ts +1 -0
  164. package/dist/services/origin-request/spec/origin-request-service-build-context.spec.js +32 -0
  165. package/dist/services/prewarm/assets/asset-api-client.d.ts +6 -0
  166. package/dist/services/prewarm/assets/asset-api-client.js +6 -1
  167. package/dist/services/prewarm/pre-warm-service.d.ts +10 -0
  168. package/dist/services/prewarm/pre-warm-service.js +17 -0
  169. package/dist/services/prewarm/spec/pre-warm-service.spec.d.ts +1 -0
  170. package/dist/services/prewarm/spec/pre-warm-service.spec.js +72 -0
  171. package/dist/services/query-builder/query-builder-factory.js +3 -3
  172. package/dist/services/setup/os/tool-registry.d.ts +1 -1
  173. package/oclif.manifest.json +162 -23
  174. package/package.json +1 -2
  175. package/dist/commands/diff-url-parameters.d.ts +0 -19
  176. package/dist/commands/diff-url-parameters.js +0 -121
  177. package/dist/services/bundler/error/packjockel-error.d.ts +0 -3
  178. package/dist/services/bundler/error/packjockel-error.js +0 -3
  179. package/dist/services/bundler/packjockel-service.d.ts +0 -72
  180. package/dist/services/bundler/packjockel-service.js +0 -156
  181. package/dist/services/bundler/spec/packjockel-service.spec.js +0 -143
  182. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.d.ts +0 -15
  183. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.js +0 -70
  184. package/dist/services/deploy/error/check-module-file-error.d.ts +0 -4
  185. package/dist/services/deploy/error/check-module-file-error.js +0 -4
  186. package/dist/services/deploy/error/post-deploy-deployment-detection-error.d.ts +0 -4
  187. package/dist/services/deploy/error/post-deploy-deployment-detection-error.js +0 -4
  188. package/dist/services/onboarding/server-config/index.d.ts +0 -10
  189. package/dist/services/onboarding/server-config/index.js +0 -27
  190. package/dist/services/onboarding/virtual-orestes-app/crawler.d.ts +0 -31
  191. package/dist/services/onboarding/virtual-orestes-app/crawler.js +0 -98
  192. /package/dist/{services/bundler/spec/packjockel-service.spec.d.ts → helpers/spec/race.spec.d.ts} +0 -0
  193. /package/dist/services/{onboarding/spec/virtual-orestes-app/crawler.spec.d.ts → deploy/spec/deploy-service.spec.d.ts} +0 -0
  194. /package/dist/services/{onboarding/spec/virtual-orestes-app/origin-error-cause.spec.d.ts → deploy/spec/handler/install-resource-handler.spec.d.ts} +0 -0
  195. /package/dist/services/{onboarding/virtual-orestes-app → origin-request}/origin-error-cause.d.ts +0 -0
  196. /package/dist/services/{onboarding/virtual-orestes-app → origin-request}/origin-error-cause.js +0 -0
@@ -6,6 +6,11 @@ export declare class OriginRequestServiceFactory {
6
6
  constructor(context: OriginRequestServiceBuildContext);
7
7
  buildService(): Promise<OriginRequestService>;
8
8
  private build;
9
+ /**
10
+ * The runtime config: a local file when one is named, else the app's. Without a customer there is
11
+ * no app, and the request carries Speed Kit's device defaults only.
12
+ */
13
+ private loadRuntimeConfig;
9
14
  private getIntegrationFiles;
10
15
  private getConfigApi;
11
16
  }
@@ -2,6 +2,8 @@ import { ConfigApiContext, ConfigApiServiceFactory, } from "../config-api/index.
2
2
  import { IntegrationApiServiceBuildContext, IntegrationApiFactory, } from "../integration-api/index.js";
3
3
  import { applyOriginProxy } from "../../helpers/origin-proxy.js";
4
4
  import { OriginRequestService } from "./origin-request-service.js";
5
+ import { OriginFetcher } from "./origin-fetcher.js";
6
+ import { RuntimeConfigSource, } from "./runtime-config-source.js";
5
7
  export class OriginRequestServiceFactory {
6
8
  context;
7
9
  service = undefined;
@@ -16,16 +18,48 @@ export class OriginRequestServiceFactory {
16
18
  }
17
19
  async build() {
18
20
  const cli = this.context.environment.cli;
19
- const files = await this.getIntegrationFiles();
20
- const customer = files.getCustomerConfig().config;
21
+ const customer = this.context.customerPath
22
+ ? (await this.getIntegrationFiles(this.context.customerPath)).getCustomerConfig().config
23
+ : undefined;
21
24
  // `--json` owns stdout, so the proxy line stays out of a machine-readable run.
22
- applyOriginProxy(customer.chromeFlags, this.context.json ? undefined : cli);
23
- this.service = new OriginRequestService(this.context, cli, this.getConfigApi(customer.app));
25
+ applyOriginProxy(customer?.chromeFlags, this.context.json ? undefined : cli);
26
+ const config = await this.loadRuntimeConfig(customer?.app);
27
+ // Without a customer, an empty set is the point: only the device defaults resolve.
28
+ const usable = config && (!customer || Object.keys(config.variations).length > 0);
29
+ if (config && !usable) {
30
+ cli.writeError("the config carries no speedKit.variations — nothing to simulate");
31
+ }
32
+ this.service = new OriginRequestService(this.context, cli, usable
33
+ ? new OriginFetcher({
34
+ cli,
35
+ config,
36
+ host: this.context.host,
37
+ strict: true,
38
+ })
39
+ : undefined);
40
+ }
41
+ /**
42
+ * The runtime config: a local file when one is named, else the app's. Without a customer there is
43
+ * no app, and the request carries Speed Kit's device defaults only.
44
+ */
45
+ async loadRuntimeConfig(app) {
46
+ const cli = this.context.environment.cli;
47
+ if (!app && !this.context.variationsFile) {
48
+ if (!this.context.json) {
49
+ cli.writeWarning("no customer: the request carries Speed Kit's device defaults only, no variation of an app");
50
+ }
51
+ return { variations: {}, authentications: [] };
52
+ }
53
+ return new RuntimeConfigSource({
54
+ cli,
55
+ configApi: app ? this.getConfigApi(app) : undefined,
56
+ fileFlag: "--variations",
57
+ }).load(this.context.variationsFile);
24
58
  }
25
- async getIntegrationFiles() {
59
+ async getIntegrationFiles(customerPath) {
26
60
  const integrationApiContext = new IntegrationApiServiceBuildContext({
27
61
  environment: this.context.environment,
28
- basePath: this.context.customerPath,
62
+ basePath: customerPath,
29
63
  configName: this.context.configName,
30
64
  whitelist: [],
31
65
  });
@@ -1,6 +1,6 @@
1
1
  import { CliServiceInterface } from "../cli/index.js";
2
- import { ConfigApiService } from "../config-api/index.js";
3
2
  import { OriginRequestOptions } from "./origin-request-service-build-context.js";
3
+ import { OriginFetcher } from "./origin-fetcher.js";
4
4
  /**
5
5
  * Fetches a page from origin with exactly the headers Speed Kit's server would send for a cache
6
6
  * variation, then reports what the origin answered.
@@ -19,15 +19,11 @@ import { OriginRequestOptions } from "./origin-request-service-build-context.js"
19
19
  export declare class OriginRequestService {
20
20
  private readonly context;
21
21
  private readonly cli;
22
- private readonly configApi;
23
- constructor(context: OriginRequestOptions, cli: CliServiceInterface, configApi: ConfigApiService);
22
+ /** Absent when the runtime config could not be used; the reason is reported. */
23
+ private readonly fetcher;
24
+ constructor(context: OriginRequestOptions, cli: CliServiceInterface,
25
+ /** Absent when the runtime config could not be used; the reason is reported. */
26
+ fetcher: OriginFetcher | undefined);
24
27
  run(): Promise<void>;
25
- /**
26
- * Reads `speedKit.variations` from the app's deployed runtime config, or from a local file when
27
- * `--variations` is given. Returns undefined when neither source is usable.
28
- */
29
- private loadVariations;
30
- private fetchRuntimeConfig;
31
- private readVariationsFile;
32
28
  private report;
33
29
  }
@@ -1,7 +1,15 @@
1
- import { readFile, writeFile } from "node:fs/promises";
2
- import { safe } from "../../helpers/safe.js";
1
+ import { writeFile } from "node:fs/promises";
3
2
  import { originFetch } from "../../helpers/origin-proxy.js";
4
- import { DEFAULT_TYPE, resolveRequest, toCurl, UndefinedVariationError, } from "./asset-variations.js";
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;
12
+ }
5
13
  /**
6
14
  * Fetches a page from origin with exactly the headers Speed Kit's server would send for a cache
7
15
  * variation, then reports what the origin answered.
@@ -20,34 +28,23 @@ import { DEFAULT_TYPE, resolveRequest, toCurl, UndefinedVariationError, } from "
20
28
  export class OriginRequestService {
21
29
  context;
22
30
  cli;
23
- configApi;
24
- constructor(context, cli, configApi) {
31
+ fetcher;
32
+ constructor(context, cli,
33
+ /** Absent when the runtime config could not be used; the reason is reported. */
34
+ fetcher) {
25
35
  this.context = context;
26
36
  this.cli = cli;
27
- this.configApi = configApi;
37
+ this.fetcher = fetcher;
28
38
  }
29
39
  async run() {
30
- const variations = await this.loadVariations();
31
- // Without variations every request would resolve to the bare defaults, so the report would
32
- // describe a request the server never sends. Stop instead of simulating something wrong.
33
- if (!variations)
40
+ // Without a runtime config every request would resolve to the bare defaults, so the report
41
+ // would describe a request the server never sends. Stop instead of simulating something wrong.
42
+ if (!this.fetcher)
34
43
  return;
35
44
  const variation = this.context.variation ?? DEFAULT_TYPE;
36
- let resolved;
37
- try {
38
- resolved = resolveRequest(variations, this.context.pageUrl, this.context.variation, this.context.host);
39
- }
40
- catch (error) {
41
- if (error instanceof UndefinedVariationError) {
42
- this.cli.writeError(error.message);
43
- return;
44
- }
45
- throw error;
46
- }
47
- const response = await originFetch(resolved.url, {
48
- headers: resolved.headers,
49
- redirect: "manual",
50
- });
45
+ const { url, init, resolved } = this.fetcher.prepare(this.context.pageUrl, this.context.variation);
46
+ const headers = printable(init.headers);
47
+ const response = await originFetch(url, { ...init, redirect: "manual" });
51
48
  const body = await response.text();
52
49
  const setCookies = response.headers.getSetCookie?.() ?? [];
53
50
  if (this.context.bodyFile)
@@ -57,8 +54,8 @@ export class OriginRequestService {
57
54
  variation,
58
55
  resolutionPath: resolved.resolutionPath,
59
56
  appliedConfigs: resolved.appliedKeys,
60
- requestUrl: resolved.url,
61
- requestHeaders: resolved.headers,
57
+ requestUrl: url,
58
+ requestHeaders: headers,
62
59
  status: response.status,
63
60
  responseHeaders: Object.fromEntries(response.headers),
64
61
  setCookie: setCookies,
@@ -66,53 +63,14 @@ export class OriginRequestService {
66
63
  }, null, 2));
67
64
  return;
68
65
  }
69
- this.report(variation, resolved, response, setCookies, body);
70
- }
71
- /**
72
- * Reads `speedKit.variations` from the app's deployed runtime config, or from a local file when
73
- * `--variations` is given. Returns undefined when neither source is usable.
74
- */
75
- async loadVariations() {
76
- const raw = this.context.variationsFile
77
- ? await this.readVariationsFile(this.context.variationsFile)
78
- : await this.fetchRuntimeConfig();
79
- if (!raw)
80
- return undefined;
81
- const parsed = JSON.parse(raw);
82
- const variations = parsed.speedKit?.variations ?? parsed.variations;
83
- if (!variations) {
84
- this.cli.writeError("the config carries no speedKit.variations — nothing to simulate");
85
- return undefined;
86
- }
87
- this.cli.writeSuccess(`variations: ${Object.keys(variations).join(", ")}`);
88
- return variations;
89
- }
90
- async fetchRuntimeConfig() {
91
- this.cli.startAction("ORIGIN_REQUEST:FETCH:RUNTIME_CONFIG", "load variations from the app");
92
- const serverConfig = await safe(this.configApi.getServerConfig());
93
- if (serverConfig.success === false) {
94
- this.cli.failAction("ORIGIN_REQUEST:FETCH:RUNTIME_CONFIG");
95
- this.cli.writeError(`unable to load the runtime config: ${serverConfig.error}. Run "sk login", or pass --variations <file> to simulate against a config you already have.`);
96
- return undefined;
97
- }
98
- this.cli.successAction("ORIGIN_REQUEST:FETCH:RUNTIME_CONFIG");
99
- return serverConfig.data;
100
- }
101
- async readVariationsFile(file) {
102
- const content = await safe(readFile(file, "utf8"));
103
- if (content.success === false) {
104
- this.cli.writeError(`unable to read ${file}: ${content.error}`);
105
- return undefined;
106
- }
107
- this.cli.writeSuccess(`variations from ${file}`);
108
- return content.data;
66
+ this.report(variation, resolved, headers, response, setCookies, body);
109
67
  }
110
- report(variation, resolved, response, setCookies, body) {
68
+ report(variation, resolved, headers, response, setCookies, body) {
111
69
  this.cli.write(`variation ${variation}`);
112
70
  this.cli.write(`resolution ${resolved.resolutionPath} → applied: ${resolved.appliedKeys.join(" then ")}`);
113
71
  this.cli.write(`request url ${resolved.url}`);
114
72
  this.cli.write("request headers");
115
- for (const [key, value] of Object.entries(resolved.headers))
73
+ for (const [key, value] of Object.entries(headers))
116
74
  this.cli.write(` ${key}: ${value}`);
117
75
  this.cli.write(`\nstatus ${response.status} ${response.statusText}`);
118
76
  const location = response.headers.get("location");
@@ -124,7 +82,7 @@ export class OriginRequestService {
124
82
  this.cli.write(` ${cookie}`);
125
83
  }
126
84
  this.cli.write(`body ${body.length} bytes${this.context.bodyFile ? ` → ${this.context.bodyFile}` : ""}`);
127
- this.cli.write(`\nequivalent curl:\n${toCurl(resolved.url, resolved.headers)}`);
85
+ this.cli.write(`\nequivalent curl:\n${toCurl(resolved.url, headers)}`);
128
86
  if (this.context.grep) {
129
87
  const pattern = new RegExp(this.context.grep, "i");
130
88
  const hits = body.split("\n").filter((line) => pattern.test(line));
@@ -0,0 +1,36 @@
1
+ import { CliServiceInterface } from "../cli/index.js";
2
+ import { ConfigApiService } from "../config-api/index.js";
3
+ import { AssetVariationMap } from "./asset-variations.js";
4
+ /** Origin basic auth of the runtime config, `speedKit.authentications`. */
5
+ export interface OriginAuthentication {
6
+ readonly type: string;
7
+ readonly uri: string;
8
+ readonly user: string;
9
+ readonly password: string;
10
+ }
11
+ /** What of the runtime config decides how Speed Kit's server requests a page from origin. */
12
+ export interface OriginRequestConfig {
13
+ readonly variations: AssetVariationMap;
14
+ readonly authentications: readonly OriginAuthentication[];
15
+ }
16
+ /** What loading the runtime config reports through. */
17
+ export type RuntimeConfigLog = Pick<CliServiceInterface, "writeError" | "writeSuccess" | "startAction" | "successAction" | "failAction">;
18
+ export interface RuntimeConfigSourceDeps {
19
+ readonly cli: RuntimeConfigLog;
20
+ /** The app's config API; absent when there is no app, and only a file can be read. */
21
+ readonly configApi?: ConfigApiService;
22
+ /** The command's flag that names a local file, for the hint when the app cannot be read. */
23
+ readonly fileFlag?: string;
24
+ }
25
+ /**
26
+ * The runtime config a request to origin is built from: the app's deployed one, so a request
27
+ * follows production, or a local file the command names.
28
+ */
29
+ export declare class RuntimeConfigSource {
30
+ private readonly deps;
31
+ constructor(deps: RuntimeConfigSourceDeps);
32
+ /** The config, or undefined when neither source is usable; the reason is reported. */
33
+ load(file?: string): Promise<OriginRequestConfig | undefined>;
34
+ private fetchRuntimeConfig;
35
+ private readFile;
36
+ }
@@ -0,0 +1,59 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { safe } from "../../helpers/safe.js";
3
+ /**
4
+ * The runtime config a request to origin is built from: the app's deployed one, so a request
5
+ * follows production, or a local file the command names.
6
+ */
7
+ export class RuntimeConfigSource {
8
+ deps;
9
+ constructor(deps) {
10
+ this.deps = deps;
11
+ }
12
+ /** The config, or undefined when neither source is usable; the reason is reported. */
13
+ async load(file) {
14
+ const raw = file
15
+ ? await this.readFile(file)
16
+ : await this.fetchRuntimeConfig();
17
+ if (!raw)
18
+ return undefined;
19
+ const parsed = JSON.parse(raw);
20
+ const speedKit = parsed.speedKit ?? parsed;
21
+ const config = {
22
+ variations: speedKit.variations ?? {},
23
+ authentications: speedKit.authentications ?? [],
24
+ };
25
+ const names = Object.keys(config.variations);
26
+ if (names.length > 0) {
27
+ this.deps.cli.writeSuccess(`variations: ${names.join(", ")}`);
28
+ }
29
+ return config;
30
+ }
31
+ async fetchRuntimeConfig() {
32
+ const { cli, configApi, fileFlag } = this.deps;
33
+ if (!configApi) {
34
+ cli.writeError(`there is no app to load the runtime config from${fileFlag ? `; pass ${fileFlag} <file>` : ""}.`);
35
+ return undefined;
36
+ }
37
+ cli.startAction("RUNTIME_CONFIG:FETCH", "get server config from app");
38
+ const serverConfig = await safe(configApi.getServerConfig());
39
+ if (serverConfig.success === false) {
40
+ cli.failAction("RUNTIME_CONFIG:FETCH");
41
+ const alternative = fileFlag
42
+ ? `, or pass ${fileFlag} <file> to use a config you already have`
43
+ : "";
44
+ cli.writeError(`unable to load the runtime config: ${serverConfig.error}. Run "sk login"${alternative}.`);
45
+ return undefined;
46
+ }
47
+ cli.successAction("RUNTIME_CONFIG:FETCH");
48
+ return serverConfig.data;
49
+ }
50
+ async readFile(file) {
51
+ const content = await safe(readFile(file, "utf8"));
52
+ if (content.success === false) {
53
+ this.deps.cli.writeError(`unable to read ${file}: ${content.error}`);
54
+ return undefined;
55
+ }
56
+ this.deps.cli.writeSuccess(`runtime config from ${file}`);
57
+ return content.data;
58
+ }
59
+ }
@@ -1,6 +1,6 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
- import { BLOCKED_ORIGIN_STATUSES, isBlockedOriginStatus, ORIGIN_ERROR_CODE, ORIGIN_ERROR_STATUS_CODE, originErrorCauseName, originErrorCode, originErrorServerTiming, } from "../../virtual-orestes-app/origin-error-cause.js";
3
+ import { BLOCKED_ORIGIN_STATUSES, isBlockedOriginStatus, ORIGIN_ERROR_CODE, ORIGIN_ERROR_STATUS_CODE, originErrorCauseName, originErrorCode, originErrorServerTiming, } from "../origin-error-cause.js";
4
4
  /**
5
5
  * Mirrors the service worker's `parseServerTimingHeader`, so the header this module emits is
6
6
  * asserted against the parser that has to read it.
@@ -1,8 +1,8 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, beforeEach, afterEach, it } from "mocha";
3
3
  import http from "node:http";
4
- import { Crawler } from "../../virtual-orestes-app/crawler.js";
5
- /** Records the lines the crawler reports, so a spec can assert on them. */
4
+ import { OriginFetcher } from "../origin-fetcher.js";
5
+ /** Records the lines the fetcher reports, so a spec can assert on them. */
6
6
  function stubCli() {
7
7
  const warnings = [];
8
8
  const errors = [];
@@ -20,7 +20,17 @@ function stubCli() {
20
20
  },
21
21
  };
22
22
  }
23
- describe("Crawler.fetchRemote", () => {
23
+ /** A fetcher over a runtime config as the config API answers it, or over none. */
24
+ function fetcher(cli, runtime) {
25
+ return new OriginFetcher({
26
+ cli,
27
+ config: runtime && {
28
+ variations: runtime.speedKit.variations ?? {},
29
+ authentications: runtime.speedKit.authentications ?? [],
30
+ },
31
+ });
32
+ }
33
+ describe("OriginFetcher.fetch", () => {
24
34
  let server;
25
35
  let url;
26
36
  /** Raw headers of the last request, exactly as they arrived on the wire. */
@@ -37,11 +47,11 @@ describe("Crawler.fetchRemote", () => {
37
47
  it("fetches an origin whose runtime config carries no authentications", async () => {
38
48
  const { cli } = stubCli();
39
49
  // The config API omits the key for an app without origin auth. Comparing `undefined < 1`
40
- // yielded false, so the crawler used to iterate undefined and throw on every fetch.
50
+ // yielded false, so the fetcher used to iterate undefined and throw on every fetch.
41
51
  const serverConfig = {
42
52
  speedKit: { variations: { "*": { headers: {} } } },
43
53
  };
44
- const result = await new Crawler(cli, serverConfig).fetchRemote(url, "DEFAULT");
54
+ const result = await fetcher(cli, serverConfig).fetch(url, "DEFAULT");
45
55
  expect(result).to.not.equal(undefined);
46
56
  expect(await result.response.text()).to.equal("origin");
47
57
  });
@@ -50,25 +60,24 @@ describe("Crawler.fetchRemote", () => {
50
60
  // default, mobile and tablet fall back to their built-in defaults, so the fetch still
51
61
  // resolves. Any override the runtime config would carry for them is lost, but the failed
52
62
  // config load reports that itself.
53
- const result = await new Crawler(cli, undefined).fetchRemote(url, "DEFAULT");
63
+ const result = await fetcher(cli, undefined).fetch(url, "DEFAULT");
54
64
  expect(await result.response.text()).to.equal("origin");
55
65
  expect(warnings).to.deep.equal([]);
56
66
  });
57
67
  it("names the defined variations when a custom one is missing", async () => {
58
68
  const { cli, warnings } = stubCli();
59
- const serverConfig = {
69
+ const originFetcher = fetcher(cli, {
60
70
  speedKit: { variations: { "*": { headers: {} }, "store-de": {} } },
61
- };
62
- const crawler = new Crawler(cli, serverConfig);
63
- await crawler.fetchRemote(url, "store-ch");
64
- await crawler.fetchRemote(url, "store-ch");
71
+ });
72
+ await originFetcher.fetch(url, "store-ch");
73
+ await originFetcher.fetch(url, "store-ch");
65
74
  expect(warnings).to.have.lengthOf(1);
66
75
  expect(warnings[0]).to.contain("variation store-ch is not defined");
67
76
  expect(warnings[0]).to.contain("defined are: *, store-de");
68
77
  });
69
78
  it("says the runtime config defines no variations at all", async () => {
70
79
  const { cli, warnings } = stubCli();
71
- await new Crawler(cli, undefined).fetchRemote(url, "store-ch");
80
+ await fetcher(cli, undefined).fetch(url, "store-ch");
72
81
  expect(warnings.join(" ")).to.contain("it defines no variations at all");
73
82
  });
74
83
  describe("a runtime config that sets Accept-Language to null", () => {
@@ -78,7 +87,7 @@ describe("Crawler.fetchRemote", () => {
78
87
  const serverConfig = withVariations({
79
88
  "*": { headers: { "Accept-Language": null } },
80
89
  });
81
- await new Crawler(cli, serverConfig).fetchRemote(url, "mobile");
90
+ await fetcher(cli, serverConfig).fetch(url, "mobile");
82
91
  expect(received).to.not.have.property("accept-language");
83
92
  expect(received).to.not.have.property("sec-fetch-mode");
84
93
  expect(received["user-agent"]).to.contain("SpeedKit/1.0");
@@ -89,14 +98,36 @@ describe("Crawler.fetchRemote", () => {
89
98
  "*": { headers: { "Accept-Language": null } },
90
99
  mobile: { headers: { "Accept-Language": "en-GB" } },
91
100
  });
92
- await new Crawler(cli, serverConfig).fetchRemote(url, "mobile");
101
+ await fetcher(cli, serverConfig).fetch(url, "mobile");
93
102
  expect(received["accept-language"]).to.equal("en-GB");
94
103
  });
95
104
  it("keeps the server's base value when no config touches the header", async () => {
96
105
  const { cli } = stubCli();
97
106
  const serverConfig = withVariations({ "*": { headers: {} } });
98
- await new Crawler(cli, serverConfig).fetchRemote(url, "DEFAULT");
107
+ await fetcher(cli, serverConfig).fetch(url, "DEFAULT");
99
108
  expect(received["accept-language"]).to.equal("*");
100
109
  });
101
110
  });
111
+ it("sends origin basic auth for a request that matched no specific variation", async () => {
112
+ const { cli } = stubCli();
113
+ const runtime = {
114
+ speedKit: {
115
+ variations: {},
116
+ authentications: [
117
+ { type: "basic", uri: url, user: "shop", password: "secret" },
118
+ ],
119
+ },
120
+ };
121
+ await fetcher(cli, runtime).fetch(url, "default");
122
+ expect(received.authorization).to.equal(`Basic ${Buffer.from("shop:secret").toString("base64")}`);
123
+ });
124
+ it("refuses an undefined variation when strict, as the server does", () => {
125
+ const { cli } = stubCli();
126
+ const strict = new OriginFetcher({
127
+ cli,
128
+ config: { variations: {}, authentications: [] },
129
+ strict: true,
130
+ });
131
+ expect(() => strict.prepare(url, "b2b")).to.throw(/b2b not found/);
132
+ });
102
133
  });
@@ -0,0 +1,32 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { OriginRequestServiceBuildContext } from "../origin-request-service-build-context.js";
4
+ const environment = {};
5
+ function create(args, flags = {}) {
6
+ return OriginRequestServiceBuildContext.create(args, { configName: "production", json: false, raw: false, ...flags }, environment);
7
+ }
8
+ describe("OriginRequestServiceBuildContext", () => {
9
+ it("takes the customer and the page", () => {
10
+ const context = create({
11
+ customerPath: "customers/shop",
12
+ pageUrl: "https://shop.test/",
13
+ });
14
+ expect(context.customerPath).to.equal("customers/shop");
15
+ expect(context.pageUrl).to.equal("https://shop.test/");
16
+ });
17
+ it("takes a lone url as the page with --raw", () => {
18
+ const context = create({ customerPath: "https://shop.test/" }, { raw: true });
19
+ expect(context.customerPath).to.equal(undefined);
20
+ expect(context.pageUrl).to.equal("https://shop.test/");
21
+ });
22
+ it("keeps the customer with --raw when one is named", () => {
23
+ const context = create({ customerPath: "customers/shop", pageUrl: "https://shop.test/" }, { raw: true });
24
+ expect(context.customerPath).to.equal("customers/shop");
25
+ });
26
+ it("refuses a lone url without --raw", () => {
27
+ expect(() => create({ customerPath: "https://shop.test/" })).to.throw(/no runtime config/);
28
+ });
29
+ it("refuses a run without a page", () => {
30
+ expect(() => create({ customerPath: "customers/shop" })).to.throw(/needs the page/);
31
+ });
32
+ });
@@ -17,6 +17,12 @@ export declare class AssetApiClient {
17
17
  constructor(authenticationToken: string, app: string, sortParameters: boolean, cli: CliServiceInterface);
18
18
  fetchURL(url: string, variation: string, verbose: boolean): Promise<SimpleResponse>;
19
19
  private makeHttp2Request;
20
+ /**
21
+ * Hands every open http2 session back.
22
+ *
23
+ * Must be called when the pre-warming is over: a session node still holds is
24
+ * an active handle, and the process cannot exit while one is open.
25
+ */
20
26
  destroy(): void;
21
27
  setIsRetry(): void;
22
28
  getRateLimit(): number;
@@ -78,7 +78,12 @@ export class AssetApiClient {
78
78
  request.end();
79
79
  });
80
80
  }
81
- // Add a method to close sessions when done
81
+ /**
82
+ * Hands every open http2 session back.
83
+ *
84
+ * Must be called when the pre-warming is over: a session node still holds is
85
+ * an active handle, and the process cannot exit while one is open.
86
+ */
82
87
  destroy() {
83
88
  for (const sessions of this.sessions.values()) {
84
89
  for (const session of sessions) {
@@ -9,7 +9,17 @@ export declare class PreWarmService {
9
9
  private requestsSend;
10
10
  constructor(assetClient: AssetApiClient, cli: CliServiceInterface, targets: readonly PrewarmTarget[], verbose: boolean);
11
11
  handleAsset(assetClient: AssetApiClient, entry: AssetItemInterface): Promise<void>;
12
+ /**
13
+ * Warms the list, then hands the sockets back.
14
+ *
15
+ * The `finally` is not tidiness. Every origin the pre-warmer talks to is held
16
+ * open as an http2 session, and an open session is an active handle: node
17
+ * keeps running while one exists, however finished the work is. Without this
18
+ * the command printed its report and then sat there — a CI job idling until
19
+ * its timeout rather than passing in the second it had actually finished in.
20
+ */
12
21
  run(): Promise<void>;
22
+ private warm;
13
23
  private fetchAssetList;
14
24
  private handleProgress;
15
25
  private fetchUrls;
@@ -46,7 +46,24 @@ export class PreWarmService {
46
46
  entry.failed();
47
47
  entry.setError(new Error(`status: ${response.status}, sk-error: ${serverTimingErrorCode}`));
48
48
  }
49
+ /**
50
+ * Warms the list, then hands the sockets back.
51
+ *
52
+ * The `finally` is not tidiness. Every origin the pre-warmer talks to is held
53
+ * open as an http2 session, and an open session is an active handle: node
54
+ * keeps running while one exists, however finished the work is. Without this
55
+ * the command printed its report and then sat there — a CI job idling until
56
+ * its timeout rather than passing in the second it had actually finished in.
57
+ */
49
58
  async run() {
59
+ try {
60
+ await this.warm();
61
+ }
62
+ finally {
63
+ this.assetClient.destroy();
64
+ }
65
+ }
66
+ async warm() {
50
67
  const assetList = this.generateAssetRequestList(this.targets);
51
68
  await this.fetchUrls(assetList);
52
69
  await this.printSummary(assetList);