@speedkit/cli 4.50.4 → 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 (71) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/services/bundler/bundle-service-factory.d.ts +3 -0
  3. package/dist/services/bundler/bundle-service-factory.js +5 -1
  4. package/dist/services/bundler/bundle-service.d.ts +10 -1
  5. package/dist/services/bundler/bundle-service.js +16 -1
  6. package/dist/services/bundler/error/packjockel-error.d.ts +3 -0
  7. package/dist/services/bundler/error/packjockel-error.js +3 -0
  8. package/dist/services/bundler/index.d.ts +1 -0
  9. package/dist/services/bundler/index.js +1 -0
  10. package/dist/services/bundler/packjockel-service.d.ts +112 -0
  11. package/dist/services/bundler/packjockel-service.js +202 -0
  12. package/dist/services/bundler/spec/packjockel-service.spec.d.ts +1 -0
  13. package/dist/services/bundler/spec/packjockel-service.spec.js +186 -0
  14. package/dist/services/deploy/checks/activation/customer-approval-check.js +1 -1
  15. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +2 -3
  16. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +2 -3
  17. package/dist/services/deploy/handler/customer-config-handler.d.ts +9 -0
  18. package/dist/services/deploy/handler/customer-config-handler.js +41 -6
  19. package/dist/services/deploy/interface/install.d.ts +7 -1
  20. package/dist/services/deploy/mr/live-state-check.js +21 -2
  21. package/dist/services/deploy/policy/auto-deploy-policy.js +2 -0
  22. package/dist/services/deploy/policy/deploy-policy-model.d.ts +4 -0
  23. package/dist/services/deploy/policy/deploy-policy-model.js +4 -0
  24. package/dist/services/deploy/policy/testing-deploy-policy.js +2 -0
  25. package/dist/services/deploy/spec/handler/customer-config-handler.spec.d.ts +1 -0
  26. package/dist/services/deploy/spec/handler/customer-config-handler.spec.js +98 -0
  27. package/dist/services/deploy/spec/mr/live-state-check.spec.js +45 -4
  28. package/dist/services/deploy/spec/policy/auto-deploy-policy.spec.js +2 -0
  29. package/dist/services/deploy/spec/policy/testing-deploy-policy.spec.js +3 -1
  30. package/dist/services/document-handler-runtime/document-handler-browser-mock.d.ts +13 -0
  31. package/dist/services/document-handler-runtime/document-handler-browser-mock.js +69 -0
  32. package/dist/services/document-handler-runtime/document-handler-server.d.ts +27 -0
  33. package/dist/services/document-handler-runtime/document-handler-server.js +112 -19
  34. package/dist/services/document-handler-runtime/spec/document-handler-browser-mock.spec.d.ts +1 -0
  35. package/dist/services/document-handler-runtime/spec/document-handler-browser-mock.spec.js +34 -0
  36. package/dist/services/integration-api/integration-api-model.d.ts +4 -1
  37. package/dist/services/integration-api/spec/customer-config-file.spec.d.ts +1 -0
  38. package/dist/services/integration-api/spec/customer-config-file.spec.js +56 -0
  39. package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +68 -8
  40. package/dist/services/integration-api/struct/customer-config-file.d.ts +15 -0
  41. package/dist/services/integration-api/struct/customer-config-file.js +42 -0
  42. package/dist/services/integration-api/struct/customer-config.d.ts +55 -1
  43. package/dist/services/integration-api/struct/customer-config.js +70 -3
  44. package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +11 -4
  45. package/dist/services/integration-api/struct/speed-kit-config-file.js +31 -10
  46. package/dist/services/onboarding/config-renderer/speed-kit-install-context.d.ts +2 -0
  47. package/dist/services/onboarding/config-renderer/speed-kit-install-context.js +29 -8
  48. package/dist/services/onboarding/fetch-events/customer-domain-document-response.js +4 -25
  49. package/dist/services/onboarding/fetch-events/speed-kit-service-worker-js.d.ts +3 -1
  50. package/dist/services/onboarding/fetch-events/speed-kit-service-worker-js.js +10 -2
  51. package/dist/services/onboarding/file-events/file-watcher.d.ts +3 -1
  52. package/dist/services/onboarding/file-events/file-watcher.js +9 -2
  53. package/dist/services/onboarding/html-charset.d.ts +28 -0
  54. package/dist/services/onboarding/html-charset.js +57 -0
  55. package/dist/services/onboarding/onboarding-model.d.ts +0 -1
  56. package/dist/services/onboarding/onboarding-model.js +0 -1
  57. package/dist/services/onboarding/onboarding-service-factory.js +12 -2
  58. package/dist/services/onboarding/spec/fetch-events/customer-domain-document-response.spec.d.ts +1 -0
  59. package/dist/services/onboarding/spec/fetch-events/customer-domain-document-response.spec.js +39 -0
  60. package/dist/services/onboarding/spec/file-events/file-watcher.spec.d.ts +1 -0
  61. package/dist/services/onboarding/spec/file-events/file-watcher.spec.js +67 -0
  62. package/dist/services/onboarding/spec/html-charset.spec.d.ts +1 -0
  63. package/dist/services/onboarding/spec/html-charset.spec.js +58 -0
  64. package/dist/services/onboarding/spec/virtual-orestes-app/index.spec.d.ts +1 -0
  65. package/dist/services/onboarding/spec/virtual-orestes-app/index.spec.js +19 -0
  66. package/dist/services/onboarding/templates/install-speed-kit-js-template.js +5 -0
  67. package/dist/services/onboarding/virtual-orestes-app/index.js +3 -26
  68. package/dist/services/pull/pull-service.js +23 -5
  69. package/dist/services/query-builder/query-builder-factory.js +3 -3
  70. package/oclif.manifest.json +1 -1
  71. package/package.json +3 -2
@@ -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
  }
@@ -1,8 +1,6 @@
1
- import { META_CONTENT_TYPE_REGEX, } from "../onboarding-model.js";
2
1
  import { OriginResponse } from "../browser/origin-response.js";
3
2
  import iconv from "iconv-lite";
4
- import Encoding from "encoding-japanese";
5
- import { UnsupportedEncodingError } from "../error/unsupported-encoding-error.js";
3
+ import { decodeHtml } from "../html-charset.js";
6
4
  /**
7
5
  * Intercepts any request to the customersOrigin and equivalent requests to baqendAssetApi
8
6
  * @implements {FetchRequestPausedEventHandler}
@@ -93,33 +91,14 @@ export class CustomerDomainDocumentResponse {
93
91
  const contentType = event.responseHeaders.find((headerRecord) => {
94
92
  return headerRecord.name.toLowerCase() === "content-type";
95
93
  })?.["value"] || "";
96
- const charsetRegex = /charset=([^\s"(),./:;<=>?@[\]]*)/i;
97
- const encodingFromHeader = contentType && charsetRegex.test(contentType)
98
- ? charsetRegex.exec(contentType)[1]
99
- : null;
100
94
  // create text from base64Encoded
101
95
  if (base64Encoded) {
102
- const buffer = Buffer.from(body, "base64");
103
- const encodingFromBuffer = Encoding.detect(buffer);
104
- const encodedBody = iconv
105
- .decode(buffer, encodingFromHeader ?? "utf-8")
106
- .toString();
107
- const metaSelector = META_CONTENT_TYPE_REGEX;
108
- const encodingFromBodyMeta = metaSelector.test(encodedBody)
109
- ? metaSelector.exec(encodedBody)[1]
110
- : "";
111
- const detectedEncoding = encodingFromHeader ||
112
- encodingFromBodyMeta ||
113
- encodingFromBuffer ||
114
- "utf-8";
115
- if (!iconv.encodingExists(detectedEncoding)) {
116
- throw new UnsupportedEncodingError(`encoding is not supported by cli: "${detectedEncoding}"`);
117
- }
96
+ const { text, charset } = decodeHtml(contentType, Buffer.from(body, "base64"));
118
97
  event.responseHeaders.push({
119
98
  name: "x-sk-detected-encoding",
120
- value: detectedEncoding,
99
+ value: charset,
121
100
  });
122
- return iconv.decode(buffer, detectedEncoding);
101
+ return text;
123
102
  }
124
103
  return body;
125
104
  }
@@ -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
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Picks the charset to decode an origin HTML document with.
3
+ * It follows the rules of the Baqend server (`detectHtmlCharset`), so onboarding sees the same text as production.
4
+ *
5
+ * - A header that declares UTF-8 wins, and the body is not read.
6
+ * - Otherwise the first 1024 bytes decide: BOM, XML declaration, `<meta charset>` or `<meta http-equiv>`.
7
+ * - A charset found in the body replaces a non-UTF-8 header charset, because origins often send a wrong one.
8
+ * - Without a hint in the body, the header charset stays, else UTF-8.
9
+ *
10
+ * There is no byte-frequency guess, because the server makes none.
11
+ *
12
+ * @param contentType - The `content-type` header value, or an empty string.
13
+ * @param body - The decompressed body bytes.
14
+ * @returns The lower-case charset name.
15
+ */
16
+ export declare function detectHtmlCharset(contentType: string, body: Uint8Array): string;
17
+ /**
18
+ * Decodes an origin HTML document with the charset of {@link detectHtmlCharset}.
19
+ *
20
+ * @param contentType - The `content-type` header value, or an empty string.
21
+ * @param body - The decompressed body bytes.
22
+ * @returns The text, and the charset to encode it back with.
23
+ * @throws {UnsupportedEncodingError} If iconv-lite does not know the charset.
24
+ */
25
+ export declare function decodeHtml(contentType: string, body: Buffer): {
26
+ text: string;
27
+ charset: string;
28
+ };
@@ -0,0 +1,57 @@
1
+ import { ResultType, Sniffer } from "encoding-sniffer/sniffer";
2
+ import iconv from "iconv-lite";
3
+ import { UnsupportedEncodingError } from "./error/unsupported-encoding-error.js";
4
+ const SNIFF_BYTES = 1024;
5
+ const HTML_CONTENT_TYPE_RE = /^\s*text\/html\b/i;
6
+ const UTF8_CHARSET_RE = /;\s*charset=["']?utf-?8["']?/i;
7
+ const HEADER_CHARSET_RE = /charset=["']?([^\s"'(),./:;<=>?@[\]]+)/i;
8
+ const UTF8_RE = /^utf-?8$/i;
9
+ /**
10
+ * Picks the charset to decode an origin HTML document with.
11
+ * It follows the rules of the Baqend server (`detectHtmlCharset`), so onboarding sees the same text as production.
12
+ *
13
+ * - A header that declares UTF-8 wins, and the body is not read.
14
+ * - Otherwise the first 1024 bytes decide: BOM, XML declaration, `<meta charset>` or `<meta http-equiv>`.
15
+ * - A charset found in the body replaces a non-UTF-8 header charset, because origins often send a wrong one.
16
+ * - Without a hint in the body, the header charset stays, else UTF-8.
17
+ *
18
+ * There is no byte-frequency guess, because the server makes none.
19
+ *
20
+ * @param contentType - The `content-type` header value, or an empty string.
21
+ * @param body - The decompressed body bytes.
22
+ * @returns The lower-case charset name.
23
+ */
24
+ export function detectHtmlCharset(contentType, body) {
25
+ const fromHeader = HEADER_CHARSET_RE.exec(contentType)?.[1]?.toLowerCase();
26
+ if (!HTML_CONTENT_TYPE_RE.test(contentType) ||
27
+ UTF8_CHARSET_RE.test(contentType)) {
28
+ return fromHeader ?? "utf-8";
29
+ }
30
+ const sniffer = new Sniffer({
31
+ maxBytes: SNIFF_BYTES,
32
+ defaultEncoding: "utf-8",
33
+ });
34
+ sniffer.write(body.subarray(0, SNIFF_BYTES));
35
+ // A default result means the body carries no hint, so it must not override the header.
36
+ if (sniffer.resultType === ResultType.DEFAULT || !sniffer.encoding) {
37
+ return fromHeader ?? "utf-8";
38
+ }
39
+ return UTF8_RE.test(sniffer.encoding)
40
+ ? "utf-8"
41
+ : sniffer.encoding.toLowerCase();
42
+ }
43
+ /**
44
+ * Decodes an origin HTML document with the charset of {@link detectHtmlCharset}.
45
+ *
46
+ * @param contentType - The `content-type` header value, or an empty string.
47
+ * @param body - The decompressed body bytes.
48
+ * @returns The text, and the charset to encode it back with.
49
+ * @throws {UnsupportedEncodingError} If iconv-lite does not know the charset.
50
+ */
51
+ export function decodeHtml(contentType, body) {
52
+ const charset = detectHtmlCharset(contentType, body);
53
+ if (!iconv.encodingExists(charset)) {
54
+ throw new UnsupportedEncodingError(`encoding is not supported by cli: "${charset}"`);
55
+ }
56
+ return { text: iconv.decode(body, charset), charset };
57
+ }
@@ -206,4 +206,3 @@ export type DevelopmentToolsMessage = {
206
206
  data: LocalCacheError[];
207
207
  };
208
208
  export declare const SpeedKitInstallRegex: RegExp;
209
- export declare const META_CONTENT_TYPE_REGEX: RegExp;
@@ -282,4 +282,3 @@ export const DEFAULT_DF_PATH = "https://www.baqend.com/speed-kit/latest/dynamic-
282
282
  export const DEFAULT_SNIPPET_PATH = "https://www.baqend.com/speed-kit/latest/snippet.js";
283
283
  export const DEFAULT_SW_PATH = "https://www.baqend.com/speed-kit/latest/sw.js";
284
284
  export const SpeedKitInstallRegex = /<script[^>]*src[^>]*app\.baqend\.com\/v1\/speedkit\/install.js[^>]*>(?:(?!<\/script>).)*<\/script>/s;
285
- export const META_CONTENT_TYPE_REGEX = /<meta[^"]*http-equiv="content-type"[^"]*content="text\/html;[^"]*charset=([^\s"(),./:;<=>?@[\]]*)"/is;
@@ -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,39 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import iconv from "iconv-lite";
4
+ import { CustomerDomainDocumentResponse } from "../../fetch-events/customer-domain-document-response.js";
5
+ const TITLE = "掃除機";
6
+ const identity = {
7
+ headers: (headers) => headers,
8
+ html: (html) => html,
9
+ };
10
+ async function serve(contentType, body) {
11
+ const client = {
12
+ send: async () => ({ body: body.toString("base64"), base64Encoded: true }),
13
+ };
14
+ const event = {
15
+ requestId: "1",
16
+ responseStatusCode: 200,
17
+ responseHeaders: [{ name: "Content-Type", value: contentType }],
18
+ };
19
+ const handler = new CustomerDomainDocumentResponse({}, identity);
20
+ const response = await handler.handle(event, client);
21
+ const encoding = response.responseHeaders.find((header) => header.name === "x-sk-detected-encoding").value;
22
+ return { response, encoding };
23
+ }
24
+ describe("CustomerDomainDocumentResponse", () => {
25
+ it("decodes a page that declares its charset only in <meta charset>", async () => {
26
+ const html = `<html><head><meta charset="shift_jis"><title>${TITLE}</title></head></html>`;
27
+ const body = iconv.encode(html, "shift_jis");
28
+ const { response, encoding } = await serve("text/html", body);
29
+ expect(encoding).to.equal("shift_jis");
30
+ // The page reaches Chrome in its own charset, byte for byte.
31
+ expect(Buffer.from(response.body, "base64")).to.deep.equal(body);
32
+ });
33
+ it("decodes a page without any charset hint as UTF-8", async () => {
34
+ const body = Buffer.from(`<html><title>${TITLE}</title></html>`);
35
+ const { response, encoding } = await serve("text/html", body);
36
+ expect(encoding).to.equal("utf-8");
37
+ expect(Buffer.from(response.body, "base64")).to.deep.equal(body);
38
+ });
39
+ });