@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
@@ -1,170 +1,304 @@
1
- import pathModule from "node:path";
2
- import fs, { readFile } from "node:fs/promises";
3
- import { mkdtempSync } from "node:fs";
4
- import { tmpdir } from "node:os";
5
- import { execSync } from "node:child_process";
6
- import { DEFAULT_INTELLIJ_IDEA_PATH_MACOS } from "../../models/os.js";
1
+ import { withoutNoise } from "./noise-baseline.js";
7
2
  import ApplicationError from "../error-handling/error/application-error.js";
3
+ import { PageSideFailure } from "./side-failure.js";
4
+ function isFailed(side) {
5
+ return side !== undefined && "failure" in side;
6
+ }
7
+ /** The failed side as a sink reports it, without the error object. */
8
+ function withoutError({ error: _error, ...side }) {
9
+ return side;
10
+ }
11
+ /** The outcome a run ends with: the worst of its comparisons, in this order. */
12
+ const OUTCOME_RANK = [
13
+ "failed",
14
+ "differs",
15
+ "uncacheable",
16
+ ];
17
+ /** The url without its query and fragment. */
18
+ function withoutQuery(url) {
19
+ return `${url.origin}${url.pathname}`;
20
+ }
21
+ /** The url without some of its query parameters, every value of each. */
22
+ function withoutParameters(url, parameters) {
23
+ const reduced = new URL(url);
24
+ for (const parameter of parameters)
25
+ reduced.searchParams.delete(parameter);
26
+ reduced.hash = "";
27
+ return reduced.toString();
28
+ }
8
29
  /**
9
- * Remove the backslashes a shell-quoted path carries before its spaces, so the
10
- * value can be handed to a child process as a single argument again.
30
+ * `sk diff`: compares one page, as Speed Kit's server would request and the
31
+ * customer's documentHandler would render it, along two axes, or against
32
+ * another page.
33
+ *
34
+ * - **Parameters:** the page against itself with its query parameters
35
+ * removed. A parameter that changes nothing can be dropped from the cache
36
+ * key; one that changes the page must stay. When removing all of them makes
37
+ * a difference, each is checked on its own, so the report names the ones
38
+ * that matter.
39
+ * - **Variations:** the page in one variation against another
40
+ * variation, which shows whether the origin serves a device or a custom
41
+ * variation different markup.
42
+ *
43
+ * With both, the parameter check runs once per variation, each against that
44
+ * variation's own page. Without variations, the parameter check runs in the
45
+ * one `variation` names, desktop by default.
46
+ *
47
+ * - **Against:** the page against another page in one variation, such as
48
+ * `?color=red` against `?color=blue`. With and without a parameter cannot
49
+ * tell a filter apart from a shop that answers a missing or unknown value
50
+ * with the unfiltered page; two values can.
51
+ *
52
+ * Parameters selected by name are checked once, all removed together while
53
+ * the others stay in the url, without the check of removing all of them.
54
+ *
55
+ * With `baseline`, every side is fetched twice, and what differs between the
56
+ * two is noise no comparison counts. Each side is fetched once per run, so a
57
+ * page that takes part in several comparisons costs its requests once.
58
+ *
59
+ * A side that fails ends its comparison, not the run. When the
60
+ * documentHandler aborts a side, Speed Kit never caches it, so that
61
+ * comparison is `uncacheable`; any other failure leaves it `failed`.
11
62
  */
12
- function unescapeSpaces(string_) {
13
- return string_.replaceAll("\\ ", " ");
14
- }
15
63
  export class DiffService {
16
- cli;
17
- diffExecutablePath;
18
- diffExecutableTemplate;
19
- opensDiffTool;
20
- tempPath = null;
21
- constructor(cli, diffExecutablePath, diffExecutableTemplate, opensDiffTool = true) {
22
- this.cli = cli;
23
- this.diffExecutablePath = diffExecutablePath;
24
- this.diffExecutableTemplate = diffExecutableTemplate;
25
- this.opensDiffTool = opensDiffTool;
26
- }
27
- async diff(localPath, fileName, temporaryContent) {
28
- const temporaryFilePath = await this.writeContentToTemporalFile(fileName, temporaryContent);
29
- return await this.executeDiff(temporaryFilePath, localPath);
30
- }
31
- async diffContent(fileName, firstContent, secondContent, diffSuffix = ["remote", "local"]) {
32
- const temporaryFirstFilePath = await this.writeContentToTemporalFile(`${fileName}-${diffSuffix[0]}`, firstContent);
33
- const temporarySecondFilePath = await this.writeContentToTemporalFile(`${fileName}-${diffSuffix[1]}`, secondContent);
34
- const diff = await this.executeDiff(temporaryFirstFilePath, temporarySecondFilePath);
35
- // return changes made in diffView
36
- // eg adding changes from remote to safe locally
37
- const changedFile = await readFile(temporarySecondFilePath, "utf-8");
38
- return { updatedFile: changedFile, diff };
64
+ deps;
65
+ outcomes = new Set();
66
+ sides = new Map();
67
+ /** The hints written, so a failure seen in several comparisons writes them once. */
68
+ hinted = new Set();
69
+ constructor(deps) {
70
+ this.deps = deps;
39
71
  }
40
72
  /**
41
- * Returns a temporal directory path.
73
+ * Runs every comparison. Resolves to the worst outcome among them: a
74
+ * comparison that failed before one that differed, before one that is not
75
+ * cacheable.
42
76
  */
43
- async getTemporalDirectoryPath() {
44
- // create temporal directory for remote installationFiles so the diff can be generated
45
- if (!this.tempPath) {
46
- this.tempPath = this.createTemporalDirectory();
77
+ async run() {
78
+ try {
79
+ await this.check();
47
80
  }
48
- if (!this.tempPath) {
49
- throw new ApplicationError("No temporary directory found and unable to create a new temporary directory");
81
+ finally {
82
+ this.deps.engine.finish();
83
+ await this.deps.browserFetch?.close();
50
84
  }
51
- return this.tempPath;
85
+ return (OUTCOME_RANK.find((outcome) => this.outcomes.has(outcome)) ?? "equal");
52
86
  }
53
- /**
54
- * The temporary directory remote installation files are written to.
55
- *
56
- * Returns null instead of throwing, because the one caller turns that into a
57
- * readable CLI error — an mkdtemp stack trace says less than "unable to
58
- * create a temporary directory" does.
59
- */
60
- createTemporalDirectory() {
61
- try {
62
- return mkdtempSync(pathModule.join(tmpdir(), "speed-kit-cli-"));
87
+ async check() {
88
+ const { against, variation, variations, params } = this.deps.options;
89
+ if (against) {
90
+ await this.checkAgainst(against, variation);
91
+ return;
63
92
  }
64
- catch (error) {
65
- this.cli.writeError(`Could not create a temporary directory: ${error}`);
66
- return null;
93
+ if (!variations) {
94
+ this.assertParameters();
95
+ await this.checkParameters(variation);
96
+ return;
67
97
  }
68
- }
69
- async writeContentToTemporalFile(temporaryFileName, content) {
70
- if (!content) {
71
- content = "";
98
+ await this.checkVariations(variations);
99
+ if (!params)
100
+ return;
101
+ for (const each of variations) {
102
+ await this.checkParameters(each);
72
103
  }
73
- const resolveFilePath = pathModule.join((await this.getTemporalDirectoryPath()) + `/${temporaryFileName}`);
74
- await fs.writeFile(resolveFilePath, content.replaceAll(/\r?\n/gm, "\n"));
75
- return resolveFilePath;
104
+ }
105
+ /** The page against another page, both in one variation. */
106
+ async checkAgainst(against, variation) {
107
+ const { pageUrl } = this.deps.options;
108
+ const page = await this.side(pageUrl, variation, `${variation}-page.html`);
109
+ const other = await this.side(against, variation, `${variation}-against.html`);
110
+ this.announce(`Checking two pages against each other (${variation})...`, [
111
+ `- page: ${pageUrl}`,
112
+ `- against: ${against}`,
113
+ ]);
114
+ await this.compare(page, other);
115
+ }
116
+ async checkVariations([left, right]) {
117
+ const { pageUrl } = this.deps.options;
118
+ const leftSide = await this.side(pageUrl, left, `${left}.html`);
119
+ const rightSide = await this.side(pageUrl, right, `${right}.html`);
120
+ this.announce(`Checking "${right}" against "${left}"...`, [
121
+ `- url: ${pageUrl}`,
122
+ ]);
123
+ await this.compare(leftSide, rightSide);
76
124
  }
77
125
  /**
78
- * @param localContent local file content.
79
- * @param remoteContent remote content.
80
- * @description removes whitespaces, newlines and compares both params content for equality.
81
- * @return returns true if both localContent and remoteContent are equal otherwise returns false.
126
+ * Without variations the parameters are all there is to compare. A url
127
+ * without any is a mistake, not a page without a diff, so it does not end
128
+ * as one.
82
129
  */
83
- isContentEqual(localContent = "", remoteContent = "") {
84
- const trimLocalContent = localContent.replaceAll(/\s/g, "");
85
- const trimRemoteContent = remoteContent.replaceAll(/\s/g, "");
86
- return trimLocalContent === trimRemoteContent;
130
+ assertParameters() {
131
+ const { pageUrl } = this.deps.options;
132
+ if (new URL(pageUrl).searchParams.size === 0) {
133
+ throw new ApplicationError(`${pageUrl} has no query parameters, so there is nothing to compare.`, [
134
+ "Diff a url with parameters, or compare variations with --variations.",
135
+ ]);
136
+ }
137
+ }
138
+ async checkParameters(variation) {
139
+ const { log, options } = this.deps;
140
+ const url = new URL(options.pageUrl);
141
+ const parameters = [...new Set(url.searchParams.keys())];
142
+ if (options.parameters) {
143
+ await this.checkEachParameter(variation, [options.parameters]);
144
+ return;
145
+ }
146
+ if (parameters.length === 0) {
147
+ log.writeWarning(`${options.pageUrl} has no query parameters, so there is nothing to compare`);
148
+ return;
149
+ }
150
+ const original = await this.side(options.pageUrl, variation, `${variation}-original.html`);
151
+ const strippedUrl = withoutQuery(url);
152
+ const stripped = await this.side(strippedUrl, variation, `${variation}-without-any-params.html`);
153
+ this.announce(`Checking any parameter (${variation})...`, [
154
+ `- with: ${options.pageUrl}`,
155
+ `- without: ${strippedUrl}`,
156
+ ]);
157
+ const all = await this.compare(original, stripped);
158
+ if (all === "equal" || parameters.length === 1 || isFailed(original)) {
159
+ return;
160
+ }
161
+ await this.checkEachParameter(variation, parameters.map((parameter) => [parameter]), isFailed(stripped) ? undefined : stripped);
87
162
  }
88
163
  /**
89
- *
90
- * check if content of two files are equal
91
- *
92
- * @param firstPath
93
- * @param secondPath
94
- * @param skipDiffExecution
164
+ * The page against itself without each group of parameters. With
165
+ * `stripped`, each comparison also says whether its difference is the one
166
+ * removing every parameter makes.
95
167
  */
96
- async executeDiff(firstPath, secondPath, skipDiffExecution = false) {
97
- let firstContent;
98
- let secondContent;
99
- try {
100
- firstContent = await fs.readFile(firstPath, {
101
- encoding: "utf8",
102
- flag: "r",
103
- });
168
+ async checkEachParameter(variation, groups, stripped) {
169
+ const { log, options } = this.deps;
170
+ const url = new URL(options.pageUrl);
171
+ const original = await this.side(options.pageUrl, variation, `${variation}-original.html`);
172
+ let allEqual = true;
173
+ for (const group of groups) {
174
+ const reducedUrl = withoutParameters(url, group);
175
+ const plural = group.length === 1 ? "" : "s";
176
+ const reduced = await this.side(reducedUrl, variation, `${variation}-without-${group.join("+")}-param${plural}.html`);
177
+ const names = group.map((parameter) => `"${parameter}"`).join(", ");
178
+ this.announce(`Checking ${names} parameter${plural} (${variation})...`, [
179
+ `- with: ${options.pageUrl}`,
180
+ `- without: ${reducedUrl}`,
181
+ ]);
182
+ const outcome = await this.compare(original, reduced, stripped);
183
+ allEqual &&= outcome === "equal";
184
+ // Without the page itself, every other check fails the same way.
185
+ if (isFailed(original))
186
+ return;
104
187
  }
105
- catch {
106
- return false;
188
+ if (stripped && allEqual) {
189
+ log.spacer();
190
+ log.writeWarning("⚠️ No parameter makes the difference on its own: only removing them together does");
107
191
  }
108
- try {
109
- secondContent = await fs.readFile(secondPath, {
110
- encoding: "utf8",
111
- flag: "r",
112
- });
113
- }
114
- catch (error) {
115
- this.cli.writeError(`Error reading remote file ${secondContent}: ${error.message}`);
116
- return false;
117
- }
118
- // Returning if no difference
119
- if (this.isContentEqual(firstContent, secondContent)) {
120
- return true;
121
- }
122
- if (skipDiffExecution || !this.opensDiffTool) {
123
- return false;
124
- }
125
- // try custom diff tool
126
- if (this.diffExecutablePath !== null &&
127
- this.diffExecutablePath.length > 0 &&
128
- this.diffExecutableTemplate !== null) {
129
- try {
130
- const command = this.diffExecutableTemplate
131
- .replace("$exec", this.diffExecutablePath)
132
- .replace("$file1", firstPath)
133
- .replace("$file2", secondPath);
134
- execSync(command, { stdio: "pipe" });
135
- }
136
- catch (error) {
137
- this.cli.code(error.stdout.toString());
138
- }
139
- return false;
192
+ }
193
+ /**
194
+ * Compares two sides and reports the verdict. With `stripped`, the
195
+ * verdict also says whether the difference is the one removing every
196
+ * parameter makes: when it is not, the parameter matters together with
197
+ * the others too. The right side carries the answer as
198
+ * `sameAsWithoutAnyParam`, for a sink that reports it.
199
+ */
200
+ async compare(left, right, stripped) {
201
+ if (isFailed(left) || isFailed(right)) {
202
+ return this.reportFailure(left, right);
140
203
  }
141
- let ideaCommandAvailable;
142
- try {
143
- execSync(`idea diff "${firstPath}" "${secondPath}"`, {
144
- stdio: ["pipe", "ignore", "pipe"], // prevent output of "/bin/sh: idea: command not found"
204
+ const { log, engine } = this.deps;
205
+ const sameAsStripped = stripped
206
+ ? engine.isContentEqual(stripped.content, withoutNoise(stripped, right).content)
207
+ : undefined;
208
+ const compared = withoutNoise(left, right);
209
+ const result = await engine.compare(left, sameAsStripped === undefined
210
+ ? compared
211
+ : {
212
+ ...compared,
213
+ details: {
214
+ ...compared.details,
215
+ sameAsWithoutAnyParam: sameAsStripped,
216
+ },
145
217
  });
146
- ideaCommandAvailable = true;
218
+ const ignored = Number(result.right.details?.ignoredLines ?? 0);
219
+ if (ignored > 0) {
220
+ log.write(`ignored ${ignored} lines that change between identical requests`);
147
221
  }
148
- catch {
149
- ideaCommandAvailable = false;
222
+ if (result.equal) {
223
+ log.writeSuccess(stripped
224
+ ? "✅ No diff found: this parameter does not make the difference"
225
+ : "✅ No diff found");
226
+ return this.settle("equal");
150
227
  }
151
- try {
152
- if (!ideaCommandAvailable) {
153
- const isWindows = process.platform.startsWith("win");
154
- if (isWindows) {
155
- execSync(`"%IDEA_INITIAL_DIRECTORY%\\idea" diff ${firstPath} ${secondPath}`, { stdio: "pipe" });
156
- }
157
- else {
158
- const ideaInitialDirectory = unescapeSpaces(process.env.IDEA_INITIAL_DIRECTORY ||
159
- DEFAULT_INTELLIJ_IDEA_PATH_MACOS);
160
- execSync(`"${ideaInitialDirectory}/idea" diff ${firstPath} ${secondPath}`, { stdio: "pipe" });
161
- }
228
+ log.writeWarning(this.diffVerdict(sameAsStripped));
229
+ return this.settle("differs");
230
+ }
231
+ /**
232
+ * Reports a comparison a side of failed. It is `uncacheable` when every
233
+ * failed side is a documentHandler abort: Speed Kit never caches such a
234
+ * page, which is a verdict in itself. Any other failure leaves the
235
+ * comparison `failed`.
236
+ */
237
+ reportFailure(left, right) {
238
+ const { log, engine } = this.deps;
239
+ const failed = [left, right].filter(isFailed);
240
+ const outcome = failed.every((side) => side.error.isAbort)
241
+ ? "uncacheable"
242
+ : "failed";
243
+ for (const { error } of failed) {
244
+ log.writeError(error.message);
245
+ for (const hint of error.suggestions ?? []) {
246
+ if (this.hinted.has(hint))
247
+ continue;
248
+ this.hinted.add(hint);
249
+ log.comment(hint);
162
250
  }
163
- return false;
164
251
  }
165
- catch (error) {
166
- this.cli.writeError(`Error executing diff command: ${error.message} (diff/diff-service)`);
167
- return false;
252
+ log.writeWarning(outcome === "uncacheable"
253
+ ? "🚫 Not cacheable, so there is nothing to compare"
254
+ : "❌ Could not compare");
255
+ engine.fail({
256
+ outcome,
257
+ left: isFailed(left) ? withoutError(left) : left,
258
+ right: isFailed(right) ? withoutError(right) : right,
259
+ });
260
+ return this.settle(outcome);
261
+ }
262
+ settle(outcome) {
263
+ this.outcomes.add(outcome);
264
+ return outcome;
265
+ }
266
+ diffVerdict(sameAsStripped) {
267
+ if (sameAsStripped === undefined)
268
+ return "⚠️ Found diff";
269
+ return sameAsStripped
270
+ ? "⚠️ Found diff, the same one removing every parameter makes"
271
+ : "⚠️ Found diff, not the one removing every parameter makes";
272
+ }
273
+ /**
274
+ * The side of a url in a variation, fetched once per run. A side Speed
275
+ * Kit's server would fail on resolves to that failure; any other error
276
+ * ends the run.
277
+ */
278
+ side(url, variation, label) {
279
+ const key = `${variation} ${url}`;
280
+ let side = this.sides.get(key);
281
+ if (!side) {
282
+ side = this.deps.pages
283
+ .fetch(url, variation, label, { baseline: this.deps.options.baseline })
284
+ .catch((error) => {
285
+ if (!(error instanceof PageSideFailure))
286
+ throw error;
287
+ return {
288
+ label,
289
+ failure: error.failure,
290
+ details: { url, variation },
291
+ error,
292
+ };
293
+ });
294
+ this.sides.set(key, side);
168
295
  }
296
+ return side;
297
+ }
298
+ announce(title, lines) {
299
+ this.deps.log.spacer();
300
+ this.deps.log.write(title);
301
+ for (const line of lines)
302
+ this.deps.log.write(line);
169
303
  }
170
304
  }
@@ -1,4 +1,15 @@
1
- export * from "./diff-service.js";
1
+ export * from "./diff-model.js";
2
+ export * from "./diff-engine.js";
3
+ export * from "./create-diff-engine.js";
2
4
  export * from "./html-format.js";
5
+ export * from "./sinks/no-diff-sink.js";
6
+ export * from "./sinks/tool-diff-sink.js";
7
+ export * from "./sinks/json-diff-sink.js";
8
+ export * from "./sinks/patch-diff-sink.js";
9
+ export * from "./diff-log.js";
10
+ export * from "./page-source.js";
11
+ export * from "./side-failure.js";
12
+ export * from "./diff-service.js";
3
13
  export * from "./diff-service-factory.js";
4
14
  export * from "./diff-service-build-context.js";
15
+ export * from "./noise-baseline.js";
@@ -1,4 +1,15 @@
1
- export * from "./diff-service.js";
1
+ export * from "./diff-model.js";
2
+ export * from "./diff-engine.js";
3
+ export * from "./create-diff-engine.js";
2
4
  export * from "./html-format.js";
5
+ export * from "./sinks/no-diff-sink.js";
6
+ export * from "./sinks/tool-diff-sink.js";
7
+ export * from "./sinks/json-diff-sink.js";
8
+ export * from "./sinks/patch-diff-sink.js";
9
+ export * from "./diff-log.js";
10
+ export * from "./page-source.js";
11
+ export * from "./side-failure.js";
12
+ export * from "./diff-service.js";
3
13
  export * from "./diff-service-factory.js";
4
14
  export * from "./diff-service-build-context.js";
15
+ export * from "./noise-baseline.js";
@@ -0,0 +1,28 @@
1
+ import { DiffSide } from "./diff-model.js";
2
+ /** What differs between two responses to the same request: the page's own noise. */
3
+ export interface Baseline {
4
+ /** The lines that changed, as {@link noiseKey} writes them. */
5
+ readonly lines: ReadonlySet<string>;
6
+ /** Whether a line only moved, so the page does not keep its order. */
7
+ readonly reorders: boolean;
8
+ }
9
+ /**
10
+ * The baseline of a page from two responses to the same request: nonces,
11
+ * timestamps, serialized state, and whether the page reorders its lines, as
12
+ * a page does that emits its preloads in no fixed order.
13
+ */
14
+ export declare function baselineOf(first: string, second: string): Baseline;
15
+ /** A side with the noise its baseline found. */
16
+ export interface BaselinedSide extends DiffSide {
17
+ readonly baseline?: Baseline;
18
+ }
19
+ /**
20
+ * The right side with every change that is noise on either side taken back
21
+ * to the left side's content, so only a real difference is left to compare
22
+ * and show. A changed line is noise when the baseline of either side saw it
23
+ * change, and a line that only moved is noise when either baseline saw the
24
+ * page reorder. A hunk with one real line in it stays a change as a whole.
25
+ *
26
+ * Without a baseline on either side, the right side is returned as it is.
27
+ */
28
+ export declare function withoutNoise(left: BaselinedSide, right: BaselinedSide): BaselinedSide;
@@ -0,0 +1,105 @@
1
+ import { diffArrays } from "diff";
2
+ /**
3
+ * A run of hex digits with at least one decimal digit in it: a number, an
4
+ * offset, or a random id such as `6abfae220721d`. Words without a digit stay.
5
+ */
6
+ const NUMBER_OR_HEX_ID = /[\da-f]*\d[\da-f]*/gi;
7
+ /**
8
+ * A line as noise is recognised: trimmed, with every number and hex id
9
+ * masked, so a line whose offsets or ids change from one request to the next
10
+ * is the same line each time.
11
+ */
12
+ function noiseKey(line) {
13
+ return line.trim().replaceAll(NUMBER_OR_HEX_ID, "0");
14
+ }
15
+ /**
16
+ * The lines that moved: removed in one place and added unchanged in another.
17
+ * Compared as they are, not as {@link noiseKey} writes them, or a number that
18
+ * changed would look like a line that moved.
19
+ */
20
+ function movedKeys(changes) {
21
+ const removed = new Set();
22
+ const added = new Set();
23
+ for (const change of changes) {
24
+ const into = change.added ? added : change.removed ? removed : undefined;
25
+ for (const line of into ? change.value : [])
26
+ into.add(line.trim());
27
+ }
28
+ return new Set([...removed].filter((key) => added.has(key)));
29
+ }
30
+ /**
31
+ * The baseline of a page from two responses to the same request: nonces,
32
+ * timestamps, serialized state, and whether the page reorders its lines, as
33
+ * a page does that emits its preloads in no fixed order.
34
+ */
35
+ export function baselineOf(first, second) {
36
+ const changes = diffArrays(first.split("\n"), second.split("\n"));
37
+ const lines = new Set();
38
+ for (const change of changes) {
39
+ if (!change.added && !change.removed)
40
+ continue;
41
+ for (const line of change.value)
42
+ lines.add(noiseKey(line));
43
+ }
44
+ return { lines, reorders: movedKeys(changes).size > 0 };
45
+ }
46
+ /**
47
+ * The right side with every change that is noise on either side taken back
48
+ * to the left side's content, so only a real difference is left to compare
49
+ * and show. A changed line is noise when the baseline of either side saw it
50
+ * change, and a line that only moved is noise when either baseline saw the
51
+ * page reorder. A hunk with one real line in it stays a change as a whole.
52
+ *
53
+ * Without a baseline on either side, the right side is returned as it is.
54
+ */
55
+ export function withoutNoise(left, right) {
56
+ if (!left.baseline && !right.baseline) {
57
+ return right;
58
+ }
59
+ const changes = diffArrays(left.content.split("\n"), right.content.split("\n"));
60
+ const reorders = Boolean(left.baseline?.reorders || right.baseline?.reorders);
61
+ const moved = reorders ? movedKeys(changes) : new Set();
62
+ const isNoise = (line) => {
63
+ const key = noiseKey(line);
64
+ return Boolean(moved.has(line.trim()) ||
65
+ left.baseline?.lines.has(key) ||
66
+ right.baseline?.lines.has(key));
67
+ };
68
+ // A hunk is a run of removed and added lines between two unchanged ones.
69
+ // Only a hunk that is noise through and through goes back to the left
70
+ // side; one real line makes the whole hunk a change, shown as it is.
71
+ const lines = [];
72
+ let ignored = 0;
73
+ let removed = [];
74
+ let added = [];
75
+ const closeHunk = () => {
76
+ const hunk = [...removed, ...added];
77
+ if (hunk.length > 0 && hunk.every((line) => isNoise(line))) {
78
+ lines.push(...removed);
79
+ ignored += hunk.length;
80
+ }
81
+ else {
82
+ lines.push(...added);
83
+ }
84
+ removed = [];
85
+ added = [];
86
+ };
87
+ for (const change of changes) {
88
+ if (change.removed) {
89
+ removed.push(...change.value);
90
+ }
91
+ else if (change.added) {
92
+ added.push(...change.value);
93
+ }
94
+ else {
95
+ closeHunk();
96
+ lines.push(...change.value);
97
+ }
98
+ }
99
+ closeHunk();
100
+ return {
101
+ ...right,
102
+ content: lines.join("\n"),
103
+ details: { ...right.details, ignoredLines: ignored },
104
+ };
105
+ }
@@ -0,0 +1,42 @@
1
+ import { DocumentHandlerServer } from "../document-handler-runtime/document-handler-server.js";
2
+ import { OriginFetcher } from "../origin-request/index.js";
3
+ import { DiffSide } from "./diff-model.js";
4
+ import { BaselinedSide } from "./noise-baseline.js";
5
+ export interface PageSourceDeps {
6
+ /** Requests the page as Speed Kit's server does. */
7
+ readonly fetcher: OriginFetcher;
8
+ /** Renders the page as Speed Kit would; absent with `--raw`. */
9
+ readonly documentHandler?: DocumentHandlerServer;
10
+ /** Removed from the page before it is compared. */
11
+ readonly ignore: readonly RegExp[];
12
+ }
13
+ export interface PageSourceOptions {
14
+ /** Compare the origin's HTML without running the documentHandler. */
15
+ readonly raw?: boolean;
16
+ /**
17
+ * Fetch the page a second time and record what differs between the two,
18
+ * the noise a comparison must not count.
19
+ */
20
+ readonly baseline?: boolean;
21
+ }
22
+ /**
23
+ * One side of a page diff: the page fetched from origin as Speed Kit's server
24
+ * requests it for a variation, rendered by the customer's local
25
+ * documentHandler, and reduced to what should count as a difference.
26
+ */
27
+ export declare class PageSource {
28
+ private readonly deps;
29
+ constructor(deps: PageSourceDeps);
30
+ fetch(url: string, variation: string, label: string, options?: PageSourceOptions): Promise<BaselinedSide>;
31
+ private fetchOnce;
32
+ /**
33
+ * A side from HTML the caller already has, such as the page a browser
34
+ * shows: rendered and reduced the same way a fetched page is.
35
+ */
36
+ fromHtml(html: string, url: string, variation: string, options: PageSourceOptions & {
37
+ label: string;
38
+ contentType?: string;
39
+ }): Promise<DiffSide>;
40
+ private render;
41
+ private reduce;
42
+ }