@speedkit/cli 4.50.5 → 4.51.0-beta.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 (58) hide show
  1. package/dist/services/bundler/bundle-service-factory.d.ts +3 -0
  2. package/dist/services/bundler/bundle-service-factory.js +5 -1
  3. package/dist/services/bundler/bundle-service.d.ts +10 -1
  4. package/dist/services/bundler/bundle-service.js +16 -1
  5. package/dist/services/bundler/error/packjockel-error.d.ts +3 -0
  6. package/dist/services/bundler/error/packjockel-error.js +3 -0
  7. package/dist/services/bundler/index.d.ts +1 -0
  8. package/dist/services/bundler/index.js +1 -0
  9. package/dist/services/bundler/packjockel-service.d.ts +112 -0
  10. package/dist/services/bundler/packjockel-service.js +202 -0
  11. package/dist/services/bundler/spec/packjockel-service.spec.d.ts +1 -0
  12. package/dist/services/bundler/spec/packjockel-service.spec.js +186 -0
  13. package/dist/services/deploy/checks/activation/customer-approval-check.js +1 -1
  14. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +2 -3
  15. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +2 -3
  16. package/dist/services/deploy/handler/customer-config-handler.d.ts +9 -0
  17. package/dist/services/deploy/handler/customer-config-handler.js +41 -6
  18. package/dist/services/deploy/interface/install.d.ts +7 -1
  19. package/dist/services/deploy/mr/live-state-check.js +21 -2
  20. package/dist/services/deploy/policy/auto-deploy-policy.js +2 -0
  21. package/dist/services/deploy/policy/deploy-policy-model.d.ts +4 -0
  22. package/dist/services/deploy/policy/deploy-policy-model.js +4 -0
  23. package/dist/services/deploy/policy/testing-deploy-policy.js +2 -0
  24. package/dist/services/deploy/spec/handler/customer-config-handler.spec.d.ts +1 -0
  25. package/dist/services/deploy/spec/handler/customer-config-handler.spec.js +98 -0
  26. package/dist/services/deploy/spec/mr/live-state-check.spec.js +45 -4
  27. package/dist/services/deploy/spec/policy/auto-deploy-policy.spec.js +2 -0
  28. package/dist/services/deploy/spec/policy/testing-deploy-policy.spec.js +3 -1
  29. package/dist/services/document-handler-runtime/document-handler-browser-mock.d.ts +13 -0
  30. package/dist/services/document-handler-runtime/document-handler-browser-mock.js +69 -0
  31. package/dist/services/document-handler-runtime/document-handler-server.d.ts +27 -0
  32. package/dist/services/document-handler-runtime/document-handler-server.js +112 -19
  33. package/dist/services/document-handler-runtime/spec/document-handler-browser-mock.spec.d.ts +1 -0
  34. package/dist/services/document-handler-runtime/spec/document-handler-browser-mock.spec.js +34 -0
  35. package/dist/services/integration-api/integration-api-model.d.ts +4 -1
  36. package/dist/services/integration-api/spec/customer-config-file.spec.d.ts +1 -0
  37. package/dist/services/integration-api/spec/customer-config-file.spec.js +56 -0
  38. package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +68 -8
  39. package/dist/services/integration-api/struct/customer-config-file.d.ts +15 -0
  40. package/dist/services/integration-api/struct/customer-config-file.js +42 -0
  41. package/dist/services/integration-api/struct/customer-config.d.ts +55 -1
  42. package/dist/services/integration-api/struct/customer-config.js +70 -3
  43. package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +11 -4
  44. package/dist/services/integration-api/struct/speed-kit-config-file.js +31 -10
  45. package/dist/services/onboarding/config-renderer/speed-kit-install-context.d.ts +2 -0
  46. package/dist/services/onboarding/config-renderer/speed-kit-install-context.js +29 -8
  47. package/dist/services/onboarding/fetch-events/speed-kit-service-worker-js.d.ts +3 -1
  48. package/dist/services/onboarding/fetch-events/speed-kit-service-worker-js.js +10 -2
  49. package/dist/services/onboarding/file-events/file-watcher.d.ts +3 -1
  50. package/dist/services/onboarding/file-events/file-watcher.js +9 -2
  51. package/dist/services/onboarding/onboarding-service-factory.js +12 -2
  52. package/dist/services/onboarding/spec/file-events/file-watcher.spec.d.ts +1 -0
  53. package/dist/services/onboarding/spec/file-events/file-watcher.spec.js +67 -0
  54. package/dist/services/onboarding/templates/install-speed-kit-js-template.js +5 -0
  55. package/dist/services/pull/pull-service.js +23 -5
  56. package/dist/services/query-builder/query-builder-factory.js +3 -3
  57. package/oclif.manifest.json +1 -1
  58. package/package.json +2 -1
@@ -1,4 +1,41 @@
1
1
  import CustomerConfigError from "../error/customer-config-error.js";
2
+ /** The fields that are set, without the `null` ones. */
3
+ export function declaredFields(features) {
4
+ return Object.fromEntries(Object.entries(features ?? {}).filter(([, value]) => value !== null && value !== undefined));
5
+ }
6
+ /**
7
+ * The fields whose value in `local` differs from `live`, with the value of `local`. A field that either
8
+ * side omits is `null`.
9
+ */
10
+ export function changedFields(local, live) {
11
+ const names = new Set([
12
+ ...Object.keys(local ?? {}),
13
+ ...Object.keys(live ?? {}),
14
+ ]);
15
+ return Object.fromEntries([...names]
16
+ .map((name) => [name, local?.[name] ?? null])
17
+ .filter(([name, value]) => JSON.stringify(value) !== JSON.stringify(live?.[name] ?? null)));
18
+ }
19
+ /** Each version tag of a config, with the field of the installation that stores it. */
20
+ export const VERSION_FIELDS = {
21
+ speedKitVersion: "swVersion",
22
+ rumPluginsVersion: "rumVersion",
23
+ ppVersion: "ppVersion",
24
+ configPluginsVersion: "configPluginsVersion",
25
+ documentHandlerVersion: "documentHandlerVersion",
26
+ };
27
+ /** The version tags of a config. A blank or omitted tag is `null`, which means `latest`. */
28
+ export function versionsOfConfig(config) {
29
+ return Object.fromEntries(Object.keys(VERSION_FIELDS).map((tag) => [tag, config?.[tag] || null]));
30
+ }
31
+ /** The version tags an installation stores, under the names of the config. */
32
+ export function versionsOfInstall(install) {
33
+ const fields = (install ?? {});
34
+ return Object.fromEntries(Object.entries(VERSION_FIELDS).map(([tag, field]) => [
35
+ tag,
36
+ fields[field] || null,
37
+ ]));
38
+ }
2
39
  export class CustomerConfig {
3
40
  app;
4
41
  origins;
@@ -18,7 +55,25 @@ export class CustomerConfig {
18
55
  * installation whose split is greater than 0 for any origin.
19
56
  */
20
57
  needsCustomerApproval;
21
- constructor(app, origins, domain, swPath, scope, appDomain, installPath, installParameters, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval) {
58
+ features;
59
+ /**
60
+ * The tag of Speed Kit (snippet, service worker, dynamic fetcher) for the `enableSpeedKitConfigPlugins` feature: a
61
+ * version such as `2.22.4`, or a channel such as `@release`; `latest` when not set. This and the other
62
+ * tags are named like the client config keys of packjockel.
63
+ */
64
+ speedKitVersion;
65
+ /** The tag of RUM for the `enableSpeedKitConfigPlugins` feature, see {@link speedKitVersion}. */
66
+ rumPluginsVersion;
67
+ /** The tag of predictive preloading for the `enableSpeedKitConfigPlugins` feature, see {@link speedKitVersion}. */
68
+ ppVersion;
69
+ /** The tag of the speed-kit-config plugins for the `enableSpeedKitConfigPlugins` feature, see {@link speedKitVersion}. */
70
+ configPluginsVersion;
71
+ /**
72
+ * The tag of the published document handler for the `enableSpeedKitConfigPlugins` feature, see {@link speedKitVersion}.
73
+ * A `custom_documentHandler.js` replaces it.
74
+ */
75
+ documentHandlerVersion;
76
+ constructor(app, origins, domain, swPath, scope, appDomain, installPath, installParameters, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval, features, versions = {}) {
22
77
  this.app = app;
23
78
  this.origins = origins;
24
79
  this.domain = domain;
@@ -32,11 +87,23 @@ export class CustomerConfig {
32
87
  this.chromeFlags = chromeFlags;
33
88
  this.dependencies = dependencies;
34
89
  this.needsCustomerApproval = needsCustomerApproval;
90
+ this.features = features;
91
+ this.speedKitVersion = versions.speedKitVersion;
92
+ this.rumPluginsVersion = versions.rumPluginsVersion;
93
+ this.ppVersion = versions.ppVersion;
94
+ this.configPluginsVersion = versions.configPluginsVersion;
95
+ this.documentHandlerVersion = versions.documentHandlerVersion;
35
96
  }
36
97
  static createFrom(configFile, configName) {
37
- const { app, origins, domain, swPath, scope, appDomain, installPath, installParams, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval, } = configFile;
98
+ const { app, origins, domain, swPath, scope, appDomain, installPath, installParams, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval, features, speedKitVersion, rumPluginsVersion, ppVersion, configPluginsVersion, documentHandlerVersion, } = configFile;
38
99
  return new CustomerConfig(new RequiredParameter("app", app).isString().getValue(), new RequiredParameter("origins", origins).isArray().getValue(), domain, new RequiredParameter("swPath", swPath).isString().getValue(), new RequiredParameter("scope", scope).isString().getValue(), appDomain || `${app}.app.baqend.com`, installPath ||
39
- `https://${app}.app.baqend.com/v1/speedkit/install.js?d=${name || configName}`, installParams || 'async="" crossorigin="anonymous"', name, forceInstall || false, chromeFlags, dependencies || {}, needsCustomerApproval || false);
100
+ `https://${app}.app.baqend.com/v1/speedkit/install.js?d=${name || configName}`, installParams || 'async="" crossorigin="anonymous"', name, forceInstall || false, chromeFlags, dependencies || {}, needsCustomerApproval || false, features || {}, {
101
+ speedKitVersion,
102
+ rumPluginsVersion,
103
+ ppVersion,
104
+ configPluginsVersion,
105
+ documentHandlerVersion,
106
+ });
40
107
  }
41
108
  }
42
109
  class RequiredParameter {
@@ -1,7 +1,10 @@
1
- import type { BundleService } from "../../bundler/index.js";
1
+ import type { AssetVersions, BundleService } from "../../bundler/index.js";
2
2
  import type { SpeedKitConfigInterface, VirtualFileInterface, VirtualFileRecipe } from "../integration-api-model.js";
3
3
  import { InstallResourceFile } from "./install-resource-file.js";
4
4
  import { SpeedKitConfig } from "./speed-kit-config.js";
5
+ import type { CustomerConfig } from "./customer-config.js";
6
+ /** What of the customer config decides how a Speed Kit config is bundled and read. */
7
+ export type SpeedKitConfigCustomer = Pick<CustomerConfig, "app" | "features"> & AssetVersions;
5
8
  /** The name the config deployed to the app is read back under. */
6
9
  export declare const REMOTE_SPEED_KIT_CONFIG = "config_SpeedKit@remote";
7
10
  /** The file a {@link SpeedKitConfigFile} stands for. */
@@ -23,7 +26,7 @@ export interface SpeedKitConfigFileInit {
23
26
  export declare class SpeedKitConfigFile extends InstallResourceFile implements SpeedKitConfigInterface {
24
27
  private readonly bundler;
25
28
  /**
26
- * One evaluation per app and origin. A config branches on
29
+ * One evaluation per app, origin and bundler. A config branches on
27
30
  * `window.location`, so the same file read for two origins is two different
28
31
  * configs — and bundling one is expensive enough not to repeat per caller.
29
32
  */
@@ -34,8 +37,8 @@ export declare class SpeedKitConfigFile extends InstallResourceFile implements S
34
37
  */
35
38
  constructor(file: SpeedKitConfigFileInit, bundler: BundleService);
36
39
  setContent(content: string): void;
37
- /** The config this file describes for one app and origin. */
38
- evaluate(app: string, origin: string): Promise<SpeedKitConfig>;
40
+ /** The config this file describes for one customer and origin. */
41
+ evaluate(customer: SpeedKitConfigCustomer, origin: string): Promise<SpeedKitConfig>;
39
42
  }
40
43
  /**
41
44
  * A SpeedKit config that has no file on disk behind it — the one deployed to
@@ -60,3 +63,7 @@ export declare class VirtualSpeedKitConfigFile extends SpeedKitConfigFile implem
60
63
  delete(): Promise<void>;
61
64
  onChange(): void;
62
65
  }
66
+ /** The customer has the `enableSpeedKitConfigPlugins` feature: packjockel bundles its config. */
67
+ export declare function usesConfigPlugins(customer: SpeedKitConfigCustomer): boolean;
68
+ /** The customer also has the `embeddedConfig` feature: the service worker embeds the config too. */
69
+ export declare function embedsConfigInServiceWorker(customer: SpeedKitConfigCustomer): boolean;
@@ -3,7 +3,7 @@ import { EvaluateSkConfigError } from "../error/evaluate-sk-config-error.js";
3
3
  import ApplicationError from "../../error-handling/error/application-error.js";
4
4
  import { StaticRecipe } from "../virtual/static-recipe.js";
5
5
  import { InstallResourceFile } from "./install-resource-file.js";
6
- import { SpeedKitConfig } from "./speed-kit-config.js";
6
+ import { SpeedKitConfig, } from "./speed-kit-config.js";
7
7
  /** The name the config deployed to the app is read back under. */
8
8
  export const REMOTE_SPEED_KIT_CONFIG = "config_SpeedKit@remote";
9
9
  /**
@@ -17,7 +17,7 @@ export const REMOTE_SPEED_KIT_CONFIG = "config_SpeedKit@remote";
17
17
  export class SpeedKitConfigFile extends InstallResourceFile {
18
18
  bundler;
19
19
  /**
20
- * One evaluation per app and origin. A config branches on
20
+ * One evaluation per app, origin and bundler. A config branches on
21
21
  * `window.location`, so the same file read for two origins is two different
22
22
  * configs — and bundling one is expensive enough not to repeat per caller.
23
23
  */
@@ -34,14 +34,15 @@ export class SpeedKitConfigFile extends InstallResourceFile {
34
34
  super.setContent(content);
35
35
  this.evaluations.clear();
36
36
  }
37
- /** The config this file describes for one app and origin. */
38
- evaluate(app, origin) {
39
- const key = `${app}|${origin}`;
37
+ /** The config this file describes for one customer and origin. */
38
+ evaluate(customer, origin) {
39
+ const { app } = customer;
40
+ const key = `${app}|${origin}|${usesConfigPlugins(customer) ? "packjockel" : "esbuild"}`;
40
41
  const cached = this.evaluations.get(key);
41
42
  if (cached) {
42
43
  return cached;
43
44
  }
44
- const evaluation = bundle(this.bundler, this.getContent(), app)
45
+ const evaluation = bundle(this.bundler, this.getContent(), customer)
45
46
  .then((code) => evaluateSpeedKitConfig(app, origin, code, this.path))
46
47
  .catch((error) => {
47
48
  // A failure is not an answer: forget it, so the next caller — or the
@@ -95,7 +96,8 @@ export class VirtualSpeedKitConfigFile extends SpeedKitConfigFile {
95
96
  }
96
97
  /**
97
98
  * Run a bundled config against a browser mock and hand back what it assigned to
98
- * `window.speedKit`, together with the config plugins it registered.
99
+ * `SpeedKit.config`, or else to `window.speedKit`, together with the config
100
+ * plugins it registered.
99
101
  *
100
102
  * @param app the app the config is evaluated for, exposed to it as `APP`
101
103
  * @param origin the origin the config is evaluated for, its `window.location`
@@ -113,7 +115,7 @@ function evaluateSpeedKitConfig(app, origin, code, filename) {
113
115
  if (browserContext.vmError !== null) {
114
116
  throw new EvaluateSkConfigError(`[${filename}] ${browserContext.vmError?.message}`);
115
117
  }
116
- return SpeedKitConfig.createFrom(browserContext.window.speedKit, registeredPlugins(browserContext));
118
+ return SpeedKitConfig.createFrom(browserContext.window.SpeedKit.config ?? browserContext.window.speedKit, registeredPlugins(browserContext));
117
119
  }
118
120
  /**
119
121
  * The plugins a config registered, read the way `ConfigController.getPlugins`
@@ -136,7 +138,9 @@ function buildBrowserMock(origin, app) {
136
138
  },
137
139
  customVariation: [],
138
140
  },
139
- SpeedKit: {},
141
+ // An embedded config pushes its plugins without creating the list, the
142
+ // way packjockel hands it over.
143
+ SpeedKit: { configPlugins: [] },
140
144
  serviceWorkerUrl: "",
141
145
  screen: { width: 0, height: 0 },
142
146
  devicePixelRatio: 1,
@@ -214,10 +218,27 @@ function safeFileContentWrapper(fileContent) {
214
218
  }
215
219
  `;
216
220
  }
217
- async function bundle(bundler, content, app) {
221
+ /** The customer has the `enableSpeedKitConfigPlugins` feature: packjockel bundles its config. */
222
+ export function usesConfigPlugins(customer) {
223
+ return customer.features?.enableSpeedKitConfigPlugins === true;
224
+ }
225
+ /** The customer also has the `embeddedConfig` feature: the service worker embeds the config too. */
226
+ export function embedsConfigInServiceWorker(customer) {
227
+ return (usesConfigPlugins(customer) && customer.features?.embeddedConfig === true);
228
+ }
229
+ async function bundle(bundler, content, customer) {
218
230
  if (!content) {
219
231
  throw new ApplicationError("Content is empty");
220
232
  }
233
+ const app = customer.app;
234
+ if (usesConfigPlugins(customer)) {
235
+ return bundler.packjockel.bundle({
236
+ entryPoints: { "config.js": content },
237
+ versions: customer,
238
+ define: { APP: JSON.stringify(app) },
239
+ platform: "browser",
240
+ });
241
+ }
221
242
  return bundler.bundle({
222
243
  basePaths: {
223
244
  "speed-kit-config": "https://www.baqend.com/speed-kit-config/latest/",
@@ -9,6 +9,8 @@ export declare class SpeedKitInstallContext {
9
9
  private skTemplate;
10
10
  constructor(customerConfig: CustomerConfig, configName: string, files: FileListInterface, bundler: BundleService, skTemplate: InstallSpeedKitJsTemplate);
11
11
  getSpeedKitInstallJs(): Promise<string>;
12
+ /** The boolean feature flags of the customer: packjockel rejects a request with any other value. */
13
+ private getFeatures;
12
14
  private prepareConfigLoadHandler;
13
15
  /** The Speed Kit config, with `delayRacedOrigin` set to 100 ms for the local documentHandler. */
14
16
  private getConfigSpeedKit;
@@ -1,3 +1,4 @@
1
+ import { usesConfigPlugins, } from "../../integration-api/index.js";
1
2
  import { INTEGRATION_FILES } from "../../../models/files.js";
2
3
  const IMPORT_PATHS = {
3
4
  rum: "https://www.baqend.com/rum/latest/",
@@ -19,8 +20,10 @@ export class SpeedKitInstallContext {
19
20
  this.skTemplate = skTemplate;
20
21
  }
21
22
  async getSpeedKitInstallJs() {
23
+ const configPlugins = usesConfigPlugins(this.customerConfig);
22
24
  const configJs = this.skTemplate.render({
23
25
  config: this.getConfigSpeedKit(),
26
+ configPlugins,
24
27
  forceInstall: this.customerConfig.forceInstall,
25
28
  installPath: this.customerConfig.installPath,
26
29
  installParams: this.customerConfig.installParams,
@@ -29,15 +32,29 @@ export class SpeedKitInstallContext {
29
32
  .getByName(INTEGRATION_FILES.CUSTOM.DYNAMIC_FETCHER)
30
33
  .getContent(),
31
34
  });
35
+ const loader = this.files
36
+ .getByName(INTEGRATION_FILES.CUSTOM.LOADER)
37
+ .getContent();
38
+ if (configPlugins) {
39
+ return this.bundler.packjockel.bundleInstallScript({
40
+ plugins: this.prepareConfigLoadHandler(),
41
+ config: configJs,
42
+ loader,
43
+ // The local install script belongs to no deployed installation.
44
+ installationId: "onboarding",
45
+ features: this.getFeatures(),
46
+ versions: this.customerConfig,
47
+ define: { APP: JSON.stringify(this.customerConfig.app) },
48
+ });
49
+ }
50
+ const entryPoints = {
51
+ "rum.js": this.prepareConfigLoadHandler(),
52
+ "config.js": configJs,
53
+ "loader.js": loader,
54
+ };
32
55
  return this.bundler.bundle({
33
56
  basePaths: IMPORT_PATHS,
34
- entryPoints: {
35
- "rum.js": this.prepareConfigLoadHandler(),
36
- "config.js": configJs,
37
- "loader.js": this.files
38
- .getByName(INTEGRATION_FILES.CUSTOM.LOADER)
39
- .getContent(),
40
- },
57
+ entryPoints,
41
58
  logLevel: "error",
42
59
  minify: false,
43
60
  platform: "browser",
@@ -46,6 +63,10 @@ export class SpeedKitInstallContext {
46
63
  },
47
64
  });
48
65
  }
66
+ /** The boolean feature flags of the customer: packjockel rejects a request with any other value. */
67
+ getFeatures() {
68
+ return Object.fromEntries(Object.entries(this.customerConfig.features ?? {}).filter((entry) => typeof entry[1] === "boolean"));
69
+ }
49
70
  prepareConfigLoadHandler() {
50
71
  return this.files
51
72
  .getByName(INTEGRATION_FILES.CONFIG.LOAD_HANDLER)
@@ -72,7 +93,7 @@ export class SpeedKitInstallContext {
72
93
  return config.replace(value, "100");
73
94
  });
74
95
  }
75
- return configString.replace(/window\.speedKit[^{]*=[^{]*{/, (configOpenTag) => {
96
+ return configString.replace(/(?:window\.speedKit|SpeedKit\.config)[^{]*=[^{]*{/, (configOpenTag) => {
76
97
  return configOpenTag + `\n delayRacedOrigin: 100,\n`;
77
98
  });
78
99
  }
@@ -4,12 +4,14 @@ import { FetchRequestPausedEventHandler } from "../onboarding-model.js";
4
4
  /**
5
5
  * SpeedKitServiceWorkerJs class is responsible for handling fetch requests for the SpeedKit service worker.
6
6
  * Injects serviceWorker from local file, if custom_sw is present.
7
+ * With `bundle`, it serves the service worker bundled with the embedded config instead.
7
8
  *
8
9
  * @implements FetchRequestPausedEventHandler
9
10
  */
10
11
  export declare class SpeedKitServiceWorkerJs implements FetchRequestPausedEventHandler {
11
12
  private customerConfig;
12
13
  private serviceWorker;
14
+ private bundle?;
13
15
  readonly patterns: Protocol.Fetch.RequestPattern[];
14
16
  /**
15
17
  * Timestamp of the last locally served sw.js. Chrome's SW update checker
@@ -19,6 +21,6 @@ export declare class SpeedKitServiceWorkerJs implements FetchRequestPausedEventH
19
21
  * OnboardingService uses this timestamp to detect and repair it.
20
22
  */
21
23
  lastServedAt: number;
22
- constructor(customerConfig: CustomerConfig, serviceWorker: FileInterface);
24
+ constructor(customerConfig: CustomerConfig, serviceWorker: FileInterface, bundle?: (serviceWorker: string) => Promise<string>);
23
25
  handle(): Promise<Partial<Protocol.Fetch.FulfillRequestRequest>>;
24
26
  }
@@ -2,12 +2,14 @@ import { BaqendResponse } from "../browser/baqend-response.js";
2
2
  /**
3
3
  * SpeedKitServiceWorkerJs class is responsible for handling fetch requests for the SpeedKit service worker.
4
4
  * Injects serviceWorker from local file, if custom_sw is present.
5
+ * With `bundle`, it serves the service worker bundled with the embedded config instead.
5
6
  *
6
7
  * @implements FetchRequestPausedEventHandler
7
8
  */
8
9
  export class SpeedKitServiceWorkerJs {
9
10
  customerConfig;
10
11
  serviceWorker;
12
+ bundle;
11
13
  patterns;
12
14
  /**
13
15
  * Timestamp of the last locally served sw.js. Chrome's SW update checker
@@ -17,9 +19,10 @@ export class SpeedKitServiceWorkerJs {
17
19
  * OnboardingService uses this timestamp to detect and repair it.
18
20
  */
19
21
  lastServedAt = 0;
20
- constructor(customerConfig, serviceWorker) {
22
+ constructor(customerConfig, serviceWorker, bundle) {
21
23
  this.customerConfig = customerConfig;
22
24
  this.serviceWorker = serviceWorker;
25
+ this.bundle = bundle;
23
26
  this.patterns = [
24
27
  {
25
28
  requestStage: "Request",
@@ -28,7 +31,12 @@ export class SpeedKitServiceWorkerJs {
28
31
  ];
29
32
  }
30
33
  async handle() {
34
+ const serviceWorker = this.serviceWorker.getContent();
35
+ // shortcut: bundles on every request, about a second; cache it per config change if updates feel slow.
36
+ const content = this.bundle
37
+ ? await this.bundle(serviceWorker)
38
+ : serviceWorker;
31
39
  this.lastServedAt = Date.now();
32
- return new BaqendResponse(this.serviceWorker.getContent(), "text/javascript; charset=utf-8");
40
+ return new BaqendResponse(content, "text/javascript; charset=utf-8");
33
41
  }
34
42
  }
@@ -70,7 +70,9 @@ export declare class FileWatcher {
70
70
  */
71
71
  private handleDynamicStyles;
72
72
  /**
73
- * fileWatcherCallback that will reinstall the serviceWorker
73
+ * fileWatcherCallback that will reinstall the serviceWorker.
74
+ * With the `enableSpeedKitConfigPlugins` feature, the document handler embeds `config_SpeedKit`,
75
+ * so a change to that file also rebuilds the document handler and drops its cache.
74
76
  *
75
77
  * @param file
76
78
  * @param page
@@ -1,5 +1,5 @@
1
1
  import { safe } from "../../../helpers/safe.js";
2
- import { FILE_TYPE, } from "../../integration-api/index.js";
2
+ import { FILE_TYPE, usesConfigPlugins, } from "../../integration-api/index.js";
3
3
  import { INTEGRATION_FILES } from "../../../models/files.js";
4
4
  import { race } from "../../../helpers/race.js";
5
5
  /**
@@ -153,7 +153,9 @@ export class FileWatcher {
153
153
  }, { signal });
154
154
  }
155
155
  /**
156
- * fileWatcherCallback that will reinstall the serviceWorker
156
+ * fileWatcherCallback that will reinstall the serviceWorker.
157
+ * With the `enableSpeedKitConfigPlugins` feature, the document handler embeds `config_SpeedKit`,
158
+ * so a change to that file also rebuilds the document handler and drops its cache.
157
159
  *
158
160
  * @param file
159
161
  * @param page
@@ -166,6 +168,11 @@ export class FileWatcher {
166
168
  // clear speed-kit-service-worker-caches and uninstall serviceWorker
167
169
  await this.safeClearPageBrowsersCacheStorage(page);
168
170
  await this.unregisterServiceWorker(page);
171
+ if (file.name === INTEGRATION_FILES.CONFIG.SPEED_KIT &&
172
+ usesConfigPlugins(this.customerConfig)) {
173
+ await this.documentHandler.buildDocumentHandler();
174
+ this.clearOrRefreshCache(page);
175
+ }
169
176
  // rebuild installResource
170
177
  await installResource.buildContent();
171
178
  // tell puppeteer to reinstall serviceworker on next navigate
@@ -7,7 +7,7 @@ import { createDiffEngine, PageSource } from "../diff/index.js";
7
7
  import { DocumentHandlerRuntimeServiceBuildContext } from "../document-handler-runtime/document-handler-runtime-service-build-context.js";
8
8
  import DocumentHandlerRuntimeServiceFactory from "../document-handler-runtime/factory/document-handler-runtime-service-factory.js";
9
9
  import { documentHandlerRuntimeFiles } from "../document-handler-runtime/runtime-files.js";
10
- import { CustomerConfig, ExternalFileReader, IntegrationApiServiceBuildContext, VirtualFile, ExternalRecipe, IntegrationApiFactory, } from "../integration-api/index.js";
10
+ import { CustomerConfig, ExternalFileReader, IntegrationApiServiceBuildContext, VirtualFile, ExternalRecipe, IntegrationApiFactory, embedsConfigInServiceWorker, } from "../integration-api/index.js";
11
11
  import { InstallFileRecipe } from "./config-renderer/install-file-recipe.js";
12
12
  import { SpeedKitInstallContext } from "./config-renderer/speed-kit-install-context.js";
13
13
  import { Dashboard } from "./dashboard/index.js";
@@ -243,7 +243,17 @@ export class OnboardingServiceFactory {
243
243
  const fileWatcher = new FileWatcher(cli, customerConfig, files, documentHandler, browserContext, cache);
244
244
  const athenaClient = (await this.getAthenaClient(customerConfig.app)) || null;
245
245
  const messageHandler = await this.prepareMessagehandler(athenaClient, files, customerConfig);
246
- const speedKitServiceWorkerJs = new SpeedKitServiceWorkerJs(customerConfig, files.getByName(INTEGRATION_FILES.CUSTOM.SW));
246
+ const speedKitServiceWorkerJs = new SpeedKitServiceWorkerJs(customerConfig, files.getByName(INTEGRATION_FILES.CUSTOM.SW), embedsConfigInServiceWorker(customerConfig)
247
+ ? (serviceWorker) => bundler.packjockel.bundleServiceWorker({
248
+ serviceWorker,
249
+ // The service worker gets the split of the onboarding page, which runs every visitor in group A.
250
+ speedKitConfig: `${files
251
+ .getByName(INTEGRATION_FILES.CONFIG.SPEED_KIT)
252
+ .getContent()}\nSpeedKit.config.split = 1;\nSpeedKit.config.splitTestId = "debug";`,
253
+ appName: customerConfig.app,
254
+ versions: customerConfig,
255
+ })
256
+ : undefined);
247
257
  const fetchHandler = await this.getFetchEventHandler(files, customerConfig, documentHandler, cache, cli, browserContext, athenaClient, messageHandler, fileWatcher, speedKitServiceWorkerJs);
248
258
  const extensionBridge = new ExtensionBridge(cli, browserExtensions.hasDevtoolsExtension);
249
259
  return new OnboardingService(browserContext, customerConfig, fetchHandler, fileWatcher, cli, messageHandler, extensionBridge, speedKitServiceWorkerJs, browserFetch);
@@ -0,0 +1,67 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { FileWatcher } from "../../file-events/file-watcher.js";
4
+ import { INTEGRATION_FILES } from "../../../../models/files.js";
5
+ /**
6
+ * Builds a FileWatcher with fakes and returns what it did on a change of `config_SpeedKit`.
7
+ *
8
+ * @param features the features of the customer config
9
+ */
10
+ async function changeSpeedKitConfig(features) {
11
+ const calls = [];
12
+ const callbacks = new Map();
13
+ const file = (name) => ({
14
+ name,
15
+ type: "file",
16
+ onChange: (callback) => callbacks.set(name, callback),
17
+ });
18
+ const files = {
19
+ hasFile: (name) => name === INTEGRATION_FILES.CONFIG.SPEED_KIT,
20
+ getByName: (name) => name === INTEGRATION_FILES.BUILD.INSTALL
21
+ ? {
22
+ buildContent: async () => calls.push("buildInstallResource"),
23
+ }
24
+ : file(name),
25
+ };
26
+ const page = {
27
+ url: () => "https://www.example.com/products/1",
28
+ target: () => ({
29
+ createCDPSession: async () => ({
30
+ send: async (method) => method === "CacheStorage.requestCacheNames" ? { caches: [] } : {},
31
+ }),
32
+ }),
33
+ };
34
+ const watcher = new FileWatcher({
35
+ writeWarning: () => undefined,
36
+ writeError: () => undefined,
37
+ }, { app: "example", features }, files, {
38
+ buildDocumentHandler: async () => calls.push("buildDocumentHandler"),
39
+ }, { domain: "https://www.example.com" }, { clear: () => calls.push("clearCache") });
40
+ watcher.addCallbackOnFileChange(page, async () => {
41
+ calls.push("reinstallServiceWorker");
42
+ }, new AbortController().signal);
43
+ await callbacks.get(INTEGRATION_FILES.CONFIG.SPEED_KIT)(file(INTEGRATION_FILES.CONFIG.SPEED_KIT));
44
+ return calls;
45
+ }
46
+ describe("FileWatcher", () => {
47
+ describe("on a change of config_SpeedKit", () => {
48
+ it("rebuilds the document handler when it embeds the config", async () => {
49
+ const calls = await changeSpeedKitConfig({
50
+ enableSpeedKitConfigPlugins: true,
51
+ });
52
+ expect(calls).to.deep.equal([
53
+ "buildDocumentHandler",
54
+ "clearCache",
55
+ "buildInstallResource",
56
+ "reinstallServiceWorker",
57
+ ]);
58
+ });
59
+ it("only reinstalls the service worker without config plugins", async () => {
60
+ const calls = await changeSpeedKitConfig(undefined);
61
+ expect(calls).to.deep.equal([
62
+ "buildInstallResource",
63
+ "reinstallServiceWorker",
64
+ ]);
65
+ });
66
+ });
67
+ });
@@ -5,6 +5,11 @@ export class InstallSpeedKitJsTemplate extends AbstractTemplate {
5
5
  // Config
6
6
  {{{config}}}
7
7
 
8
+ {{#if configPlugins}}
9
+ // The config writes SpeedKit.config, which packjockel exposes; the overrides below change it.
10
+ var speedKit = SpeedKit.config;
11
+ {{/if}}
12
+
8
13
  // Reset potential split test parameters
9
14
  speedKit.split = 1.0;
10
15
  speedKit.splitTestId = 'debug';
@@ -1,4 +1,4 @@
1
- import { InstallResourceFile, } from "../integration-api/index.js";
1
+ import { InstallResourceFile, versionsOfInstall, } from "../integration-api/index.js";
2
2
  import { InstallResourceToFileMapping } from "./index.js";
3
3
  import { safe } from "../../helpers/safe.js";
4
4
  import { execSync } from "node:child_process";
@@ -107,13 +107,31 @@ export class PullService {
107
107
  async handleConfigCustomer(installResource) {
108
108
  const customerConfigFile = this.fileList.getCustomerConfig();
109
109
  const config = customerConfigFile.config;
110
- if (JSON.stringify(installResource.origins.sort()) !==
111
- JSON.stringify(config.origins.sort())) {
110
+ const updatesOrigins = JSON.stringify(installResource.origins.sort()) !==
111
+ JSON.stringify(config.origins.sort());
112
+ if (updatesOrigins) {
112
113
  this.cli.writeWarning(`update: config_customer(origins)`);
113
114
  customerConfigFile.updateOrigins(installResource.origins);
115
+ }
116
+ else {
117
+ this.cli.writeSuccess("equal, skip: config_customer(origins)");
118
+ }
119
+ const updatesFeatures = customerConfigFile.updateFeatures(installResource.features);
120
+ if (updatesFeatures) {
121
+ this.cli.writeWarning(`update: config_customer(features)`);
122
+ }
123
+ else {
124
+ this.cli.writeSuccess("equal, skip: config_customer(features)");
125
+ }
126
+ const updatesVersions = customerConfigFile.updateVersions(versionsOfInstall(installResource));
127
+ if (updatesVersions) {
128
+ this.cli.writeWarning(`update: config_customer(versions)`);
129
+ }
130
+ else {
131
+ this.cli.writeSuccess("equal, skip: config_customer(versions)");
132
+ }
133
+ if (updatesOrigins || updatesFeatures || updatesVersions) {
114
134
  await customerConfigFile.updateFile();
115
- return;
116
135
  }
117
- this.cli.writeSuccess("equal, skip: config_customer(origins)");
118
136
  }
119
137
  }
@@ -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(app, origins[0], files.getSpeedKitConfig());
25
+ const speedKitConfig = await this.getSpeedKitConfig(customerConfig, 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(app, origin, speedKitConfigFile) {
76
- const result = await safe(speedKitConfigFile.evaluate(app, origin));
75
+ async getSpeedKitConfig(customer, origin, speedKitConfigFile) {
76
+ const result = await safe(speedKitConfigFile.evaluate(customer, origin));
77
77
  if (result.success !== true) {
78
78
  throw new ParseConfigSpeedKitError(result.error);
79
79
  }
@@ -2113,5 +2113,5 @@
2113
2113
  ]
2114
2114
  }
2115
2115
  },
2116
- "version": "4.50.5"
2116
+ "version": "4.51.0-beta.1"
2117
2117
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@speedkit/cli",
3
3
  "description": "Speed Kit CLI",
4
- "version": "4.50.5",
4
+ "version": "4.51.0-beta.1",
5
5
  "author": {
6
6
  "name": "Baqend.com",
7
7
  "email": "info@baqend.com"
@@ -110,6 +110,7 @@
110
110
  "@oclif/plugin-help": "^6.2.45",
111
111
  "@oclif/plugin-not-found": "^3.2.81",
112
112
  "@oclif/plugin-warn-if-update-available": "^3.1.61",
113
+ "@speedkit/packjockel": "4.1.0",
113
114
  "@speedkit/rules": "^1.3.0-0",
114
115
  "baqend": "^4.2.5",
115
116
  "chalk": "^5.6.2",