@speedkit/cli 4.44.0-beta.3 → 4.44.1

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 (189) hide show
  1. package/CHANGELOG.md +38 -8
  2. package/README.md +68 -42
  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/helpers/race.js +4 -2
  11. package/dist/helpers/spec/race.spec.js +31 -0
  12. package/dist/models/files.d.ts +0 -1
  13. package/dist/models/files.js +0 -1
  14. package/dist/services/browser-fetch/browser-origin-fetch.js +1 -1
  15. package/dist/services/browser-fetch/browser-origin-fetcher.d.ts +1 -7
  16. package/dist/services/browser-fetch/browser-origin-fetcher.js +3 -26
  17. package/dist/services/bundler/bundle-service-factory.d.ts +0 -3
  18. package/dist/services/bundler/bundle-service-factory.js +1 -5
  19. package/dist/services/bundler/bundle-service.d.ts +1 -10
  20. package/dist/services/bundler/bundle-service.js +1 -16
  21. package/dist/services/bundler/index.d.ts +0 -1
  22. package/dist/services/bundler/index.js +0 -1
  23. package/dist/services/cli/cli-environment.d.ts +11 -0
  24. package/dist/services/cli/cli-environment.js +37 -0
  25. package/dist/services/cli/cli-service-model.d.ts +3 -1
  26. package/dist/services/cli/cli-service.d.ts +1 -1
  27. package/dist/services/cli/cli-service.js +4 -1
  28. package/dist/services/cli/spec/cli-environment.spec.js +30 -1
  29. package/dist/services/deploy/checks/activation/customer-approval-check.js +1 -1
  30. package/dist/services/deploy/checks/post-deploy/refresh-deployment-job-check.d.ts +2 -2
  31. package/dist/services/deploy/checks/post-deploy/refresh-deployment-job-check.js +8 -7
  32. package/dist/services/deploy/checks/post-deploy/revalidated-module-check.d.ts +2 -2
  33. package/dist/services/deploy/checks/post-deploy/revalidated-module-check.js +11 -7
  34. package/dist/services/deploy/checks/post-deploy/save-files-check.d.ts +2 -2
  35. package/dist/services/deploy/checks/post-deploy/save-files-check.js +3 -2
  36. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +3 -2
  37. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +3 -2
  38. package/dist/services/deploy/deploy-service-build-context.d.ts +2 -0
  39. package/dist/services/deploy/deploy-service-build-context.js +13 -2
  40. package/dist/services/deploy/deploy-service-factory.js +2 -9
  41. package/dist/services/deploy/deploy-service-model.d.ts +11 -1
  42. package/dist/services/deploy/deploy-service.d.ts +2 -0
  43. package/dist/services/deploy/deploy-service.js +26 -1
  44. package/dist/services/deploy/handler/customer-config-handler.d.ts +2 -2
  45. package/dist/services/deploy/handler/install-resource-handler.d.ts +2 -2
  46. package/dist/services/deploy/handler/install-resource-handler.js +6 -3
  47. package/dist/services/deploy/handler/module-handler.d.ts +2 -2
  48. package/dist/services/deploy/handler/module-handler.js +2 -2
  49. package/dist/services/deploy/handler/server-config-handler.d.ts +2 -2
  50. package/dist/services/deploy/handler/server-config-handler.js +2 -2
  51. package/dist/services/deploy/mr/live-state-check.d.ts +2 -2
  52. package/dist/services/deploy/mr/merge-request-deploy-service-factory.js +2 -7
  53. package/dist/services/deploy/policy/deploy-policy-model.d.ts +1 -5
  54. package/dist/services/deploy/policy/deploy-policy-model.js +0 -4
  55. package/dist/services/deploy/policy/index.d.ts +1 -0
  56. package/dist/services/deploy/policy/index.js +1 -0
  57. package/dist/services/deploy/policy/testing-deploy-policy.d.ts +16 -0
  58. package/dist/services/deploy/policy/testing-deploy-policy.js +42 -0
  59. package/dist/services/deploy/spec/checks/post-deploy/refresh-deployment-job-check.spec.js +5 -12
  60. package/dist/services/deploy/spec/deploy-service.spec.js +64 -0
  61. package/dist/services/deploy/spec/handler/install-resource-handler.spec.js +58 -0
  62. package/dist/services/deploy/spec/mr/live-state-check.spec.js +2 -2
  63. package/dist/services/deploy/spec/policy/auto-deploy-policy.spec.js +0 -2
  64. package/dist/services/deploy/spec/policy/testing-deploy-policy.spec.d.ts +1 -0
  65. package/dist/services/deploy/spec/policy/testing-deploy-policy.spec.js +46 -0
  66. package/dist/services/diff/create-diff-engine.d.ts +14 -0
  67. package/dist/services/diff/create-diff-engine.js +42 -0
  68. package/dist/services/diff/customer-origin.d.ts +9 -0
  69. package/dist/services/diff/customer-origin.js +19 -0
  70. package/dist/services/diff/diff-engine.d.ts +30 -0
  71. package/dist/services/diff/diff-engine.js +49 -0
  72. package/dist/services/diff/diff-log.d.ts +9 -0
  73. package/dist/services/diff/diff-log.js +18 -0
  74. package/dist/services/diff/diff-model.d.ts +68 -0
  75. package/dist/services/diff/diff-model.js +1 -0
  76. package/dist/services/diff/diff-service-build-context.d.ts +99 -9
  77. package/dist/services/diff/diff-service-build-context.js +299 -1
  78. package/dist/services/diff/diff-service-factory.d.ts +24 -2
  79. package/dist/services/diff/diff-service-factory.js +165 -3
  80. package/dist/services/diff/diff-service.d.ts +93 -33
  81. package/dist/services/diff/diff-service.js +274 -140
  82. package/dist/services/diff/index.d.ts +12 -1
  83. package/dist/services/diff/index.js +12 -1
  84. package/dist/services/diff/noise-baseline.d.ts +28 -0
  85. package/dist/services/diff/noise-baseline.js +105 -0
  86. package/dist/services/diff/page-source.d.ts +42 -0
  87. package/dist/services/diff/page-source.js +93 -0
  88. package/dist/services/diff/side-failure.d.ts +28 -0
  89. package/dist/services/diff/side-failure.js +100 -0
  90. package/dist/services/diff/sinks/json-diff-sink.d.ts +32 -0
  91. package/dist/services/diff/sinks/json-diff-sink.js +44 -0
  92. package/dist/services/diff/sinks/no-diff-sink.d.ts +5 -0
  93. package/dist/services/diff/sinks/no-diff-sink.js +6 -0
  94. package/dist/services/diff/sinks/patch-diff-sink.d.ts +19 -0
  95. package/dist/services/diff/sinks/patch-diff-sink.js +29 -0
  96. package/dist/services/diff/sinks/tool-diff-sink.d.ts +26 -0
  97. package/dist/services/diff/sinks/tool-diff-sink.js +88 -0
  98. package/dist/services/diff/sinks/unified-patch.d.ts +7 -0
  99. package/dist/services/diff/sinks/unified-patch.js +12 -0
  100. package/dist/services/diff/spec/customer-origin.spec.d.ts +1 -0
  101. package/dist/services/diff/spec/customer-origin.spec.js +27 -0
  102. package/dist/services/diff/spec/diff-engine.spec.d.ts +1 -0
  103. package/dist/services/diff/spec/diff-engine.spec.js +47 -0
  104. package/dist/services/diff/spec/diff-service-build-context.spec.d.ts +1 -0
  105. package/dist/services/diff/spec/diff-service-build-context.spec.js +185 -0
  106. package/dist/services/diff/spec/diff-service.spec.d.ts +1 -0
  107. package/dist/services/diff/spec/diff-service.spec.js +393 -0
  108. package/dist/services/diff/spec/noise-baseline.spec.d.ts +1 -0
  109. package/dist/services/diff/spec/noise-baseline.spec.js +95 -0
  110. package/dist/services/diff/spec/page-source.spec.d.ts +1 -0
  111. package/dist/services/diff/spec/page-source.spec.js +140 -0
  112. package/dist/services/diff/spec/sinks/json-diff-sink.spec.d.ts +1 -0
  113. package/dist/services/diff/spec/sinks/json-diff-sink.spec.js +72 -0
  114. package/dist/services/diff/spec/sinks/patch-diff-sink.spec.d.ts +1 -0
  115. package/dist/services/diff/spec/sinks/patch-diff-sink.spec.js +47 -0
  116. package/dist/services/diff/spec/sinks/tool-diff-sink.spec.d.ts +1 -0
  117. package/dist/services/diff/spec/sinks/tool-diff-sink.spec.js +61 -0
  118. package/dist/services/document-handler-runtime/document-handler-abort.d.ts +2 -0
  119. package/dist/services/document-handler-runtime/document-handler-abort.js +2 -0
  120. package/dist/services/document-handler-runtime/document-handler-server.d.ts +0 -17
  121. package/dist/services/document-handler-runtime/document-handler-server.js +19 -81
  122. package/dist/services/document-handler-runtime/runtime-files.d.ts +9 -0
  123. package/dist/services/document-handler-runtime/runtime-files.js +17 -0
  124. package/dist/services/integration-api/integration-api-model.d.ts +1 -2
  125. package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +8 -68
  126. package/dist/services/integration-api/struct/customer-config.d.ts +1 -32
  127. package/dist/services/integration-api/struct/customer-config.js +3 -33
  128. package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +4 -9
  129. package/dist/services/integration-api/struct/speed-kit-config-file.js +10 -27
  130. package/dist/services/onboarding/config-renderer/speed-kit-install-context.js +8 -20
  131. package/dist/services/onboarding/dashboard/diff-against-current-page.d.ts +29 -7
  132. package/dist/services/onboarding/dashboard/diff-against-current-page.js +24 -37
  133. package/dist/services/onboarding/dashboard/index.js +13 -6
  134. package/dist/services/onboarding/dashboard/request-diff-service.d.ts +21 -8
  135. package/dist/services/onboarding/dashboard/request-diff-service.js +21 -49
  136. package/dist/services/onboarding/onboarding-model.d.ts +0 -1
  137. package/dist/services/onboarding/onboarding-model.js +0 -1
  138. package/dist/services/onboarding/onboarding-service-factory.d.ts +0 -1
  139. package/dist/services/onboarding/onboarding-service-factory.js +24 -30
  140. package/dist/services/onboarding/spec/dashboard/request-diff-service.spec.d.ts +1 -0
  141. package/dist/services/onboarding/spec/dashboard/request-diff-service.spec.js +57 -0
  142. package/dist/services/onboarding/templates/install-speed-kit-js-template.js +0 -5
  143. package/dist/services/onboarding/virtual-orestes-app/index.d.ts +3 -3
  144. package/dist/services/onboarding/virtual-orestes-app/index.js +5 -5
  145. package/dist/services/origin-request/index.d.ts +3 -0
  146. package/dist/services/origin-request/index.js +3 -0
  147. package/dist/services/origin-request/origin-fetcher.d.ts +52 -0
  148. package/dist/services/origin-request/origin-fetcher.js +93 -0
  149. package/dist/services/origin-request/origin-request-service-factory.js +19 -1
  150. package/dist/services/origin-request/origin-request-service.d.ts +6 -10
  151. package/dist/services/origin-request/origin-request-service.js +28 -70
  152. package/dist/services/origin-request/runtime-config-source.d.ts +36 -0
  153. package/dist/services/origin-request/runtime-config-source.js +59 -0
  154. package/dist/services/origin-request/spec/origin-error-cause.spec.d.ts +1 -0
  155. package/dist/services/{onboarding/spec/virtual-orestes-app → origin-request/spec}/origin-error-cause.spec.js +1 -1
  156. package/dist/services/origin-request/spec/origin-fetcher.spec.d.ts +1 -0
  157. package/dist/services/{onboarding/spec/virtual-orestes-app/crawler.spec.js → origin-request/spec/origin-fetcher.spec.js} +46 -15
  158. package/dist/services/prewarm/assets/asset-api-client.d.ts +6 -0
  159. package/dist/services/prewarm/assets/asset-api-client.js +6 -1
  160. package/dist/services/prewarm/pre-warm-service.d.ts +10 -0
  161. package/dist/services/prewarm/pre-warm-service.js +17 -0
  162. package/dist/services/prewarm/spec/pre-warm-service.spec.d.ts +1 -0
  163. package/dist/services/prewarm/spec/pre-warm-service.spec.js +72 -0
  164. package/dist/services/query-builder/query-builder-factory.js +3 -3
  165. package/dist/services/setup/os/tool-registry.d.ts +1 -1
  166. package/oclif.manifest.json +294 -162
  167. package/package.json +1 -2
  168. package/dist/commands/diff-url-parameters.d.ts +0 -19
  169. package/dist/commands/diff-url-parameters.js +0 -121
  170. package/dist/services/bundler/error/packjockel-error.d.ts +0 -3
  171. package/dist/services/bundler/error/packjockel-error.js +0 -3
  172. package/dist/services/bundler/packjockel-service.d.ts +0 -72
  173. package/dist/services/bundler/packjockel-service.js +0 -156
  174. package/dist/services/bundler/spec/packjockel-service.spec.js +0 -143
  175. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.d.ts +0 -15
  176. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.js +0 -70
  177. package/dist/services/deploy/error/check-module-file-error.d.ts +0 -4
  178. package/dist/services/deploy/error/check-module-file-error.js +0 -4
  179. package/dist/services/deploy/error/post-deploy-deployment-detection-error.d.ts +0 -4
  180. package/dist/services/deploy/error/post-deploy-deployment-detection-error.js +0 -4
  181. package/dist/services/onboarding/server-config/index.d.ts +0 -10
  182. package/dist/services/onboarding/server-config/index.js +0 -27
  183. package/dist/services/onboarding/virtual-orestes-app/crawler.d.ts +0 -31
  184. package/dist/services/onboarding/virtual-orestes-app/crawler.js +0 -98
  185. /package/dist/{services/bundler/spec/packjockel-service.spec.d.ts → helpers/spec/race.spec.d.ts} +0 -0
  186. /package/dist/services/{onboarding/spec/virtual-orestes-app/crawler.spec.d.ts → deploy/spec/deploy-service.spec.d.ts} +0 -0
  187. /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
  188. /package/dist/services/{onboarding/virtual-orestes-app → origin-request}/origin-error-cause.d.ts +0 -0
  189. /package/dist/services/{onboarding/virtual-orestes-app → origin-request}/origin-error-cause.js +0 -0
@@ -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
  });
@@ -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);
@@ -0,0 +1,72 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { PreWarmService } from "../pre-warm-service.js";
4
+ /**
5
+ * A cli that swallows everything. A proxy rather than a list of stubs: the
6
+ * service reaches for a different writer as the run progresses, and a missing
7
+ * one would fail the test for a reason that has nothing to do with sockets.
8
+ */
9
+ function silentCli() {
10
+ const identity = (text) => text;
11
+ return new Proxy({}, {
12
+ get: (_target, property) => {
13
+ if (property === "quiet") {
14
+ return true;
15
+ }
16
+ if (property === "style") {
17
+ return new Proxy({}, { get: () => identity });
18
+ }
19
+ return () => undefined;
20
+ },
21
+ });
22
+ }
23
+ /** Counts the handover, and can be told to fail the requests. */
24
+ function recordingClient(fetchRejects = false) {
25
+ const state = { destroyed: 0, fetched: 0 };
26
+ const client = {
27
+ destroy: () => {
28
+ state.destroyed += 1;
29
+ },
30
+ fetchURL: () => {
31
+ state.fetched += 1;
32
+ return fetchRejects
33
+ ? Promise.reject(new Error("origin unreachable"))
34
+ : Promise.resolve({ headers: new Headers(), ok: true, status: 200 });
35
+ },
36
+ getRateLimit: () => 10,
37
+ increaseMaxRequestsPerSecond: () => undefined,
38
+ reduceMaxRequestsPerSecond: () => undefined,
39
+ setIsRetry: () => undefined,
40
+ };
41
+ return { client, state };
42
+ }
43
+ const TARGETS = [
44
+ { url: "https://www.obi.de/p/1", variation: "desktop" },
45
+ { url: "https://www.obi.de/p/2", variation: "desktop" },
46
+ ];
47
+ describe("PreWarmService — handing the sockets back", () => {
48
+ // Every origin is held open as an http2 session, and an open session is an
49
+ // active handle: node will not exit while one exists. Leaving them open made
50
+ // the command print its report and then idle until the CI job timed out.
51
+ it("releases the http2 sessions when the run is over", async () => {
52
+ const { client, state } = recordingClient();
53
+ await new PreWarmService(client, silentCli(), TARGETS, false).run();
54
+ expect(state.fetched).to.equal(2);
55
+ expect(state.destroyed).to.equal(1);
56
+ });
57
+ it("releases them even when the run threw", async () => {
58
+ const { client, state } = recordingClient();
59
+ const service = new PreWarmService(client, silentCli(), TARGETS, false);
60
+ // Break the run itself, not a single request: a thrown run must not strand
61
+ // the sockets either.
62
+ service.printSummary =
63
+ () => Promise.reject(new Error("boom"));
64
+ await service.run().catch(() => undefined);
65
+ expect(state.destroyed).to.equal(1);
66
+ });
67
+ it("releases them when every request failed", async () => {
68
+ const { client, state } = recordingClient(true);
69
+ await new PreWarmService(client, silentCli(), TARGETS, false).run();
70
+ expect(state.destroyed).to.equal(1);
71
+ });
72
+ });
@@ -22,7 +22,7 @@ export class QueryBuilderFactory {
22
22
  const customerConfig = files.getCustomerConfig().config;
23
23
  const app = customerConfig.app;
24
24
  const origins = customerConfig.origins ?? [];
25
- const speedKitConfig = await this.getSpeedKitConfig(customerConfig, origins[0], files.getSpeedKitConfig());
25
+ const speedKitConfig = await this.getSpeedKitConfig(app, origins[0], files.getSpeedKitConfig());
26
26
  const serviceContext = {
27
27
  app,
28
28
  bySet: this.context.bySet,
@@ -72,8 +72,8 @@ export class QueryBuilderFactory {
72
72
  const integrationApi = new IntegrationApiFactory(integrationApiContext).buildService();
73
73
  return await integrationApi.run();
74
74
  }
75
- async getSpeedKitConfig(customer, origin, speedKitConfigFile) {
76
- const result = await safe(speedKitConfigFile.evaluate(customer, origin));
75
+ async getSpeedKitConfig(app, origin, speedKitConfigFile) {
76
+ const result = await safe(speedKitConfigFile.evaluate(app, origin));
77
77
  if (result.success !== true) {
78
78
  throw new ParseConfigSpeedKitError(result.error);
79
79
  }
@@ -20,7 +20,7 @@ export interface DetectableTool {
20
20
  export interface DetectableEditor extends DetectableTool {
21
21
  /**
22
22
  * diff-service template for this tool, using the `$exec`/`$file1`/`$file2`
23
- * placeholders understood by DiffService.
23
+ * placeholders understood by ToolDiffSink.
24
24
  */
25
25
  diffExecTemplate: string;
26
26
  }