@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
@@ -0,0 +1,93 @@
1
+ import ApplicationError from "../error-handling/error/application-error.js";
2
+ import { formatHtml } from "./html-format.js";
3
+ import { documentHandlerFailure, originFailure, PageSideFailure, } from "./side-failure.js";
4
+ import { baselineOf } from "./noise-baseline.js";
5
+ /** The meta tag the documentHandler writes with the page's own url, which differs per url by design. */
6
+ const ASSET_URL_META = /<meta[^>]*name="baqend:asset_url"[^>]*>/g;
7
+ /**
8
+ * One side of a page diff: the page fetched from origin as Speed Kit's server
9
+ * requests it for a variation, rendered by the customer's local
10
+ * documentHandler, and reduced to what should count as a difference.
11
+ */
12
+ export class PageSource {
13
+ deps;
14
+ constructor(deps) {
15
+ this.deps = deps;
16
+ }
17
+ async fetch(url, variation, label, options = {}) {
18
+ const side = await this.fetchOnce(url, variation, label, options);
19
+ if (!options.baseline) {
20
+ return side;
21
+ }
22
+ const again = await this.fetchOnce(url, variation, label, options);
23
+ return { ...side, baseline: baselineOf(side.content, again.content) };
24
+ }
25
+ async fetchOnce(url, variation, label, options) {
26
+ const fetched = await this.deps.fetcher.fetch(url, variation);
27
+ if (!fetched) {
28
+ throw new ApplicationError(`${url} (${variation}) could not be fetched.`);
29
+ }
30
+ const { response } = fetched;
31
+ // A bot protection answers a node fetch with the same challenge page for
32
+ // every request, and two challenge pages are equal: a miss must not
33
+ // read as "no diff".
34
+ if (response.status !== 200) {
35
+ throw new PageSideFailure({
36
+ url,
37
+ variation,
38
+ failure: originFailure(response.status, response.statusText),
39
+ });
40
+ }
41
+ const side = await this.fromHtml(await response.text(), url, variation, {
42
+ ...options,
43
+ label,
44
+ contentType: response.headers.get("content-type") ?? undefined,
45
+ });
46
+ return {
47
+ ...side,
48
+ details: {
49
+ ...side.details,
50
+ requestUrl: fetched.url,
51
+ status: response.status,
52
+ },
53
+ };
54
+ }
55
+ /**
56
+ * A side from HTML the caller already has, such as the page a browser
57
+ * shows: rendered and reduced the same way a fetched page is.
58
+ */
59
+ async fromHtml(html, url, variation, options) {
60
+ const rendered = options.raw
61
+ ? html
62
+ : await this.render(html, options.contentType ?? "text/html; charset=utf-8", variation, url);
63
+ return {
64
+ label: options.label,
65
+ content: formatHtml(this.reduce(rendered)),
66
+ details: { url, variation },
67
+ };
68
+ }
69
+ async render(html, contentType, variation, url) {
70
+ if (!this.deps.documentHandler) {
71
+ return html;
72
+ }
73
+ const response = await this.deps.documentHandler
74
+ .transform(contentType, html, variation, url, {})
75
+ .catch((error) => {
76
+ const failure = documentHandlerFailure(error);
77
+ throw failure
78
+ ? new PageSideFailure({ url, variation, failure })
79
+ : error;
80
+ });
81
+ if (Buffer.isBuffer(response.body)) {
82
+ throw new ApplicationError(`the documentHandler answered ${url} with a binary body, which cannot be diffed`);
83
+ }
84
+ return response.body;
85
+ }
86
+ reduce(html) {
87
+ let reduced = html.replaceAll(ASSET_URL_META, "");
88
+ for (const pattern of this.deps.ignore) {
89
+ reduced = reduced.replaceAll(pattern, "");
90
+ }
91
+ return reduced;
92
+ }
93
+ }
@@ -0,0 +1,28 @@
1
+ import ApplicationError from "../error-handling/error/application-error.js";
2
+ import { SideFailure } from "./diff-model.js";
3
+ /**
4
+ * A side of a diff that could not be built, classified as Speed Kit's server
5
+ * classifies it: an origin error, a documentHandler abort or a documentHandler
6
+ * error, each with its Speed Kit error code. The message and hints follow
7
+ * the kind, so a missing page is not mistaken for a bot protection.
8
+ */
9
+ export declare class PageSideFailure extends ApplicationError {
10
+ readonly url: string;
11
+ readonly variation: string;
12
+ readonly failure: SideFailure;
13
+ constructor(page: {
14
+ url: string;
15
+ variation: string;
16
+ failure: SideFailure;
17
+ });
18
+ /** The documentHandler stopped on purpose: a verdict, not a fault. */
19
+ get isAbort(): boolean;
20
+ }
21
+ /** The failure an origin status other than 200 is, as onboarding's Asset API reports it. */
22
+ export declare function originFailure(status: number, statusText?: string): SideFailure;
23
+ /**
24
+ * The failure a documentHandler error is: an abort (`blacklistHTML`, a
25
+ * non-200 pre-render) or an ordinary error. Undefined for an error that did
26
+ * not come from running the documentHandler, such as a missing file.
27
+ */
28
+ export declare function documentHandlerFailure(error: unknown): SideFailure | undefined;
@@ -0,0 +1,100 @@
1
+ import { ABORT_ERROR_CODE, DOCUMENT_HANDLER_ERROR_CODE, isDocumentHandlerAbort, } from "../document-handler-runtime/document-handler-abort.js";
2
+ import ApplicationError from "../error-handling/error/application-error.js";
3
+ import { DocumentHandlerTransformError } from "../onboarding/error/document-handler-transform-error.js";
4
+ import { VmEmptyResponseError } from "../onboarding/error/vm-empty-response-error.js";
5
+ import { ORIGIN_ERROR_CODE, originErrorCauseName, originErrorCode, } from "../origin-request/index.js";
6
+ /** How a failure reads in a log line: its cause name and Speed Kit error code. */
7
+ function causeLine(failure) {
8
+ return `${failure.cause} (${failure.code})`;
9
+ }
10
+ /** What to do about a failure, by its kind and code. */
11
+ function hints(failure) {
12
+ if (failure.kind === "documentHandlerAbort") {
13
+ return [
14
+ "Speed Kit never caches this page: the documentHandler aborts it, and the service worker falls back to origin.",
15
+ ];
16
+ }
17
+ if (failure.kind === "documentHandlerError") {
18
+ return [
19
+ "Check config_documentHandler.es6 | custom_documentHandler.js: the documentHandler threw.",
20
+ ];
21
+ }
22
+ switch (failure.code) {
23
+ case ORIGIN_ERROR_CODE.BLOCKED:
24
+ return [
25
+ "The origin blocked the request, usually a bot protection refusing a direct fetch: check the page with `sk origin-request`, or fetch it through a headless Chrome with --browserFetch.",
26
+ ];
27
+ case ORIGIN_ERROR_CODE.NOT_FOUND:
28
+ case ORIGIN_ERROR_CODE.GONE:
29
+ return [
30
+ "The page does not exist at this url, e.g. a page number past the last page: diff a url the shop answers.",
31
+ ];
32
+ default:
33
+ return ["Check the page with `sk origin-request`."];
34
+ }
35
+ }
36
+ function message(url, variation, failure) {
37
+ const page = `${url} (${variation})`;
38
+ if (failure.kind === "origin") {
39
+ const answer = [failure.status, failure.reason].filter(Boolean).join(" ");
40
+ return `${page}: the origin answered ${answer}, ${causeLine(failure)}`;
41
+ }
42
+ const what = failure.kind === "documentHandlerAbort" ? "aborted" : "threw";
43
+ return `${page}: the documentHandler ${what}, ${causeLine(failure)}: ${failure.reason}`;
44
+ }
45
+ /**
46
+ * A side of a diff that could not be built, classified as Speed Kit's server
47
+ * classifies it: an origin error, a documentHandler abort or a documentHandler
48
+ * error, each with its Speed Kit error code. The message and hints follow
49
+ * the kind, so a missing page is not mistaken for a bot protection.
50
+ */
51
+ export class PageSideFailure extends ApplicationError {
52
+ url;
53
+ variation;
54
+ failure;
55
+ constructor(page) {
56
+ super(message(page.url, page.variation, page.failure), hints(page.failure));
57
+ this.url = page.url;
58
+ this.variation = page.variation;
59
+ this.failure = page.failure;
60
+ }
61
+ /** The documentHandler stopped on purpose: a verdict, not a fault. */
62
+ get isAbort() {
63
+ return this.failure.kind === "documentHandlerAbort";
64
+ }
65
+ }
66
+ /** The failure an origin status other than 200 is, as onboarding's Asset API reports it. */
67
+ export function originFailure(status, statusText) {
68
+ const code = originErrorCode(status);
69
+ return {
70
+ kind: "origin",
71
+ code,
72
+ cause: originErrorCauseName(code),
73
+ status,
74
+ reason: statusText || undefined,
75
+ };
76
+ }
77
+ /**
78
+ * The failure a documentHandler error is: an abort (`blacklistHTML`, a
79
+ * non-200 pre-render) or an ordinary error. Undefined for an error that did
80
+ * not come from running the documentHandler, such as a missing file.
81
+ */
82
+ export function documentHandlerFailure(error) {
83
+ if (!(error instanceof DocumentHandlerTransformError) &&
84
+ !(error instanceof VmEmptyResponseError)) {
85
+ return undefined;
86
+ }
87
+ return isDocumentHandlerAbort(error)
88
+ ? {
89
+ kind: "documentHandlerAbort",
90
+ code: ABORT_ERROR_CODE,
91
+ cause: "ServerDocumentHandlerAbort",
92
+ reason: error.message,
93
+ }
94
+ : {
95
+ kind: "documentHandlerError",
96
+ code: DOCUMENT_HANDLER_ERROR_CODE,
97
+ cause: "ServerDocumentHandlerError",
98
+ reason: error.message,
99
+ };
100
+ }
@@ -0,0 +1,32 @@
1
+ import { CliServiceInterface } from "../../cli/index.js";
2
+ import { ComparisonOutcome, DiffResult, DiffSink, FailedComparison, SideFailure } from "../diff-model.js";
3
+ /** One result as the JSON output lists it. */
4
+ export interface JsonDiffEntry {
5
+ readonly outcome: ComparisonOutcome;
6
+ readonly left: JsonDiffSideEntry;
7
+ readonly right: JsonDiffSideEntry;
8
+ /** Equal apart from whitespace; false when a side failed. */
9
+ readonly equal: boolean;
10
+ /** The unified diff, null when the sides are equal or a side failed. */
11
+ readonly patch: string | null;
12
+ }
13
+ export interface JsonDiffSideEntry {
14
+ readonly label: string;
15
+ /** Absent for a side that failed. */
16
+ readonly bytes?: number;
17
+ /** Why the side could not be built: its kind, Speed Kit error code and cause. */
18
+ readonly failure?: SideFailure;
19
+ readonly [detail: string]: unknown;
20
+ }
21
+ /**
22
+ * Collects every result, equal or not, and prints them as one JSON array on
23
+ * stdout when the run is finished: the form a script or an agent parses.
24
+ */
25
+ export declare class JsonDiffSink implements DiffSink {
26
+ private readonly cli;
27
+ private readonly entries;
28
+ constructor(cli: CliServiceInterface);
29
+ show(result: DiffResult): Promise<undefined>;
30
+ fail(comparison: FailedComparison): void;
31
+ finish(): void;
32
+ }
@@ -0,0 +1,44 @@
1
+ import { unifiedPatch } from "./unified-patch.js";
2
+ function sideEntry(side) {
3
+ if ("failure" in side) {
4
+ return { ...side.details, label: side.label, failure: side.failure };
5
+ }
6
+ return {
7
+ ...side.details,
8
+ label: side.label,
9
+ bytes: Buffer.byteLength(side.content),
10
+ };
11
+ }
12
+ /**
13
+ * Collects every result, equal or not, and prints them as one JSON array on
14
+ * stdout when the run is finished: the form a script or an agent parses.
15
+ */
16
+ export class JsonDiffSink {
17
+ cli;
18
+ entries = [];
19
+ constructor(cli) {
20
+ this.cli = cli;
21
+ }
22
+ async show(result) {
23
+ this.entries.push({
24
+ outcome: result.equal ? "equal" : "differs",
25
+ left: sideEntry(result.left),
26
+ right: sideEntry(result.right),
27
+ equal: result.equal,
28
+ patch: result.equal ? null : unifiedPatch(result),
29
+ });
30
+ return undefined;
31
+ }
32
+ fail(comparison) {
33
+ this.entries.push({
34
+ outcome: comparison.outcome,
35
+ left: sideEntry(comparison.left),
36
+ right: sideEntry(comparison.right),
37
+ equal: false,
38
+ patch: null,
39
+ });
40
+ }
41
+ finish() {
42
+ this.cli.pipe(`${JSON.stringify(this.entries, null, 2)}\n`);
43
+ }
44
+ }
@@ -0,0 +1,5 @@
1
+ import { DiffSink } from "../diff-model.js";
2
+ /** Shows nothing: the caller only wants to know whether two sides are equal. */
3
+ export declare class NoDiffSink implements DiffSink {
4
+ show(): Promise<undefined>;
5
+ }
@@ -0,0 +1,6 @@
1
+ /** Shows nothing: the caller only wants to know whether two sides are equal. */
2
+ export class NoDiffSink {
3
+ async show() {
4
+ return undefined;
5
+ }
6
+ }
@@ -0,0 +1,19 @@
1
+ import { CliServiceInterface } from "../../cli/index.js";
2
+ import { DiffResult, DiffSink } from "../diff-model.js";
3
+ export interface PatchDiffSinkDeps {
4
+ readonly cli: CliServiceInterface;
5
+ /** Lines of one patch printed before the rest is cut. */
6
+ readonly maxLines?: number;
7
+ }
8
+ /**
9
+ * Prints a differing result as a unified diff on stdout: the direct feedback
10
+ * an agent or a CI log reads, where no diff tool can open.
11
+ *
12
+ * A patch longer than `maxLines` is cut, with the number of lines left out, so
13
+ * one noisy page does not bury the results after it.
14
+ */
15
+ export declare class PatchDiffSink implements DiffSink {
16
+ private readonly deps;
17
+ constructor(deps: PatchDiffSinkDeps);
18
+ show(result: DiffResult): Promise<undefined>;
19
+ }
@@ -0,0 +1,29 @@
1
+ import { unifiedPatch } from "./unified-patch.js";
2
+ /** How many lines of one patch are printed before the rest is cut. */
3
+ const DEFAULT_MAX_LINES = 400;
4
+ /**
5
+ * Prints a differing result as a unified diff on stdout: the direct feedback
6
+ * an agent or a CI log reads, where no diff tool can open.
7
+ *
8
+ * A patch longer than `maxLines` is cut, with the number of lines left out, so
9
+ * one noisy page does not bury the results after it.
10
+ */
11
+ export class PatchDiffSink {
12
+ deps;
13
+ constructor(deps) {
14
+ this.deps = deps;
15
+ }
16
+ async show(result) {
17
+ if (result.equal) {
18
+ return undefined;
19
+ }
20
+ const maxLines = this.deps.maxLines ?? DEFAULT_MAX_LINES;
21
+ const lines = unifiedPatch(result).trimEnd().split("\n");
22
+ const shown = lines.slice(0, maxLines);
23
+ if (lines.length > maxLines) {
24
+ shown.push(`… ${lines.length - maxLines} more lines`);
25
+ }
26
+ this.deps.cli.pipe(`${shown.join("\n")}\n`);
27
+ return undefined;
28
+ }
29
+ }
@@ -0,0 +1,26 @@
1
+ import { CliServiceInterface, CliSettings } from "../../cli/index.js";
2
+ import { DiffResult, DiffSink } from "../diff-model.js";
3
+ export interface ToolDiffSinkDeps {
4
+ readonly cli: CliServiceInterface;
5
+ readonly settings: Pick<CliSettings, "diffExec" | "diffExecTemplate" | "ideaDirectory">;
6
+ /** Creates the directory the two sides are written to, once, on first use. */
7
+ readonly createDirectory: () => string;
8
+ }
9
+ /**
10
+ * Opens a differing result in the configured diff tool (`diffExec`), or in
11
+ * IntelliJ IDEA when none is configured.
12
+ *
13
+ * Both sides are written to files under the CLI's cache directory, which
14
+ * outlive the command: the tool opens them after `idea diff` has returned.
15
+ * The right side is read back, so an edit made in the tool reaches the caller.
16
+ */
17
+ export declare class ToolDiffSink implements DiffSink {
18
+ private readonly deps;
19
+ private directory?;
20
+ constructor(deps: ToolDiffSinkDeps);
21
+ show(result: DiffResult): Promise<string | undefined>;
22
+ private write;
23
+ private launch;
24
+ private runTemplate;
25
+ private runIdea;
26
+ }
@@ -0,0 +1,88 @@
1
+ import path from "node:path";
2
+ import { readFileSync, writeFileSync } from "node:fs";
3
+ import { execSync } from "node:child_process";
4
+ import { DEFAULT_INTELLIJ_IDEA_PATH_MACOS } from "../../../models/os.js";
5
+ /**
6
+ * Remove the backslashes a shell-quoted path carries before its spaces, so the
7
+ * value can be handed to a child process as a single argument again.
8
+ */
9
+ function unescapeSpaces(value) {
10
+ return value.replaceAll("\\ ", " ");
11
+ }
12
+ /** A label as a file name: no path separators, nothing a shell trips over. */
13
+ function fileName(label) {
14
+ return label.replaceAll(/[^\w.-]+/g, "_");
15
+ }
16
+ /**
17
+ * Opens a differing result in the configured diff tool (`diffExec`), or in
18
+ * IntelliJ IDEA when none is configured.
19
+ *
20
+ * Both sides are written to files under the CLI's cache directory, which
21
+ * outlive the command: the tool opens them after `idea diff` has returned.
22
+ * The right side is read back, so an edit made in the tool reaches the caller.
23
+ */
24
+ export class ToolDiffSink {
25
+ deps;
26
+ directory;
27
+ constructor(deps) {
28
+ this.deps = deps;
29
+ }
30
+ async show(result) {
31
+ if (result.equal) {
32
+ return undefined;
33
+ }
34
+ const leftPath = this.write(result.left);
35
+ const rightPath = this.write(result.right);
36
+ this.launch(leftPath, rightPath);
37
+ return readFileSync(rightPath, "utf8");
38
+ }
39
+ write(side) {
40
+ this.directory ??= this.deps.createDirectory();
41
+ const filePath = path.join(this.directory, fileName(side.label));
42
+ writeFileSync(filePath, side.content);
43
+ return filePath;
44
+ }
45
+ launch(leftPath, rightPath) {
46
+ const { diffExec, diffExecTemplate } = this.deps.settings;
47
+ if (diffExec && diffExecTemplate) {
48
+ this.runTemplate(diffExecTemplate, diffExec, leftPath, rightPath);
49
+ return;
50
+ }
51
+ this.runIdea(leftPath, rightPath);
52
+ }
53
+ runTemplate(template, executable, leftPath, rightPath) {
54
+ // A function replacement, because a `$` in a path is no pattern.
55
+ const command = template
56
+ .replaceAll("$exec", () => executable)
57
+ .replaceAll("$file1", () => leftPath)
58
+ .replaceAll("$file2", () => rightPath);
59
+ try {
60
+ execSync(command, { stdio: "pipe" });
61
+ }
62
+ catch (error) {
63
+ // A diff tool that exits non-zero for "files differ" is not an error.
64
+ this.deps.cli.code(String(error.stdout ?? ""));
65
+ }
66
+ }
67
+ runIdea(leftPath, rightPath) {
68
+ try {
69
+ execSync(`idea diff "${leftPath}" "${rightPath}"`, {
70
+ stdio: ["pipe", "ignore", "pipe"], // prevent output of "/bin/sh: idea: command not found"
71
+ });
72
+ return;
73
+ }
74
+ catch {
75
+ // No `idea` launcher on the path: call IDEA from its install directory.
76
+ }
77
+ const directory = unescapeSpaces(this.deps.settings.ideaDirectory || DEFAULT_INTELLIJ_IDEA_PATH_MACOS);
78
+ const idea = path.join(directory, "idea");
79
+ try {
80
+ execSync(`"${idea}" diff "${leftPath}" "${rightPath}"`, {
81
+ stdio: "pipe",
82
+ });
83
+ }
84
+ catch (error) {
85
+ this.deps.cli.writeError(`Error executing diff command: ${error.message} (diff/tool-diff-sink)`);
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,7 @@
1
+ import { DiffResult } from "../diff-model.js";
2
+ /**
3
+ * The result as a unified diff, `--- left` / `+++ right` and its hunks.
4
+ * Whitespace is ignored as it is for equality, so a hunk never shows a change
5
+ * that did not count as one.
6
+ */
7
+ export declare function unifiedPatch(result: DiffResult): string;
@@ -0,0 +1,12 @@
1
+ import { FILE_HEADERS_ONLY, formatPatch, structuredPatch } from "diff";
2
+ /** Lines of unchanged context around each change, as `diff -u` has. */
3
+ const CONTEXT_LINES = 3;
4
+ /**
5
+ * The result as a unified diff, `--- left` / `+++ right` and its hunks.
6
+ * Whitespace is ignored as it is for equality, so a hunk never shows a change
7
+ * that did not count as one.
8
+ */
9
+ export function unifiedPatch(result) {
10
+ const patch = structuredPatch(result.left.label, result.right.label, result.left.content, result.right.content, undefined, undefined, { context: CONTEXT_LINES, ignoreWhitespace: true });
11
+ return formatPatch(patch, FILE_HEADERS_ONLY);
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import ApplicationError from "../../error-handling/error/application-error.js";
4
+ import { assertCustomerOrigin } from "../customer-origin.js";
5
+ const origins = ["https://www.shop.test", "https://shop.test"];
6
+ describe("assertCustomerOrigin", () => {
7
+ it("accepts a page on one of the customer's origins", () => {
8
+ expect(() => assertCustomerOrigin("https://shop.test/c/a?b=1", origins, "customers/shop")).to.not.throw();
9
+ });
10
+ it("ignores case and a default port", () => {
11
+ expect(() => assertCustomerOrigin("https://WWW.Shop.test:443/", origins, "customers/shop")).to.not.throw();
12
+ });
13
+ it("refuses a page of another site, naming the customer's origins", () => {
14
+ let error;
15
+ try {
16
+ assertCustomerOrigin("https://other.test/", origins, "customers/shop");
17
+ }
18
+ catch (error_) {
19
+ error = error_;
20
+ }
21
+ expect(error).to.be.instanceOf(ApplicationError);
22
+ expect(error.suggestions[0]).to.contain("https://www.shop.test, https://shop.test");
23
+ });
24
+ it("refuses another scheme on the same host", () => {
25
+ expect(() => assertCustomerOrigin("http://shop.test/", origins, "customers/shop")).to.throw(ApplicationError);
26
+ });
27
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,47 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { DiffEngine } from "../diff-engine.js";
4
+ /** A sink that records what it was shown and answers with `edited`. */
5
+ function recordingSink(edited) {
6
+ const shown = [];
7
+ return {
8
+ shown,
9
+ async show(result) {
10
+ shown.push(result);
11
+ return edited;
12
+ },
13
+ };
14
+ }
15
+ describe("DiffEngine", () => {
16
+ it("treats contents that differ only in whitespace as equal", async () => {
17
+ const sink = recordingSink();
18
+ const engine = new DiffEngine({ sink });
19
+ const result = await engine.compare({ label: "a", content: "<p>\n x</p>" }, { label: "b", content: "<p>x</p>" });
20
+ expect(result.equal).to.equal(true);
21
+ expect(sink.shown).to.have.length(1);
22
+ });
23
+ it("hands a differing result to the sink with LF line endings", async () => {
24
+ const sink = recordingSink();
25
+ const engine = new DiffEngine({ sink });
26
+ const result = await engine.compare({ label: "a", content: "one\r\ntwo" }, { label: "b", content: "one\nthree" });
27
+ expect(result.equal).to.equal(false);
28
+ expect(sink.shown[0].left.content).to.equal("one\ntwo");
29
+ });
30
+ it("returns the right side as the sink edited it", async () => {
31
+ const engine = new DiffEngine({ sink: recordingSink("taken over") });
32
+ const { updatedFile, equal } = await engine.diffContent("config.js", "remote", "local");
33
+ expect(equal).to.equal(false);
34
+ expect(updatedFile).to.equal("taken over");
35
+ });
36
+ it("names the sides of a deploy diff after the file", async () => {
37
+ const sink = recordingSink();
38
+ await new DiffEngine({ sink }).diffContent("sw.js", "a", "b");
39
+ expect(sink.shown[0].left.label).to.equal("sw.js-remote");
40
+ expect(sink.shown[0].right.label).to.equal("sw.js-local");
41
+ });
42
+ it("treats a missing content as empty", async () => {
43
+ const engine = new DiffEngine({ sink: recordingSink() });
44
+ const result = await engine.compare({ label: "a", content: undefined }, { label: "b", content: "" });
45
+ expect(result.equal).to.equal(true);
46
+ });
47
+ });