@speedkit/cli 4.41.2 → 4.43.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 (47) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +1 -2
  3. package/dist/services/bundler/bundle-service-factory.d.ts +6 -1
  4. package/dist/services/bundler/bundle-service-factory.js +9 -2
  5. package/dist/services/bundler/bundle-service.d.ts +10 -1
  6. package/dist/services/bundler/bundle-service.js +16 -1
  7. package/dist/services/bundler/error/packjockel-error.d.ts +3 -0
  8. package/dist/services/bundler/error/packjockel-error.js +3 -0
  9. package/dist/services/bundler/index.d.ts +1 -0
  10. package/dist/services/bundler/index.js +1 -0
  11. package/dist/services/bundler/packjockel-service.d.ts +70 -0
  12. package/dist/services/bundler/packjockel-service.js +171 -0
  13. package/dist/services/bundler/spec/packjockel-service.spec.d.ts +1 -0
  14. package/dist/services/bundler/spec/packjockel-service.spec.js +149 -0
  15. package/dist/services/cli/cli-environment.js +1 -0
  16. package/dist/services/cli/cli-settings.d.ts +5 -0
  17. package/dist/services/cli/cli-settings.js +1 -0
  18. package/dist/services/deploy/checks/activation/customer-approval-check.js +1 -1
  19. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +2 -3
  20. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +2 -3
  21. package/dist/services/deploy/deploy-service-factory.js +1 -1
  22. package/dist/services/deploy/mr/merge-request-deploy-service-factory.js +1 -1
  23. package/dist/services/document-handler-runtime/document-handler-runtime-model.d.ts +2 -0
  24. package/dist/services/document-handler-runtime/document-handler-runtime-model.js +2 -0
  25. package/dist/services/document-handler-runtime/document-handler-server.d.ts +17 -0
  26. package/dist/services/document-handler-runtime/document-handler-server.js +82 -20
  27. package/dist/services/document-handler-runtime/factory/document-handler-runtime-service-factory.js +1 -1
  28. package/dist/services/integration-api/integration-api-factory.js +1 -1
  29. package/dist/services/integration-api/integration-api-model.d.ts +2 -1
  30. package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +70 -8
  31. package/dist/services/integration-api/struct/customer-config.d.ts +19 -1
  32. package/dist/services/integration-api/struct/customer-config.js +11 -3
  33. package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +9 -4
  34. package/dist/services/integration-api/struct/speed-kit-config-file.js +30 -10
  35. package/dist/services/onboarding/config-renderer/speed-kit-install-context.d.ts +2 -2
  36. package/dist/services/onboarding/config-renderer/speed-kit-install-context.js +26 -14
  37. package/dist/services/onboarding/file-events/file-watcher.d.ts +1 -1
  38. package/dist/services/onboarding/file-events/file-watcher.js +3 -3
  39. package/dist/services/onboarding/onboarding-model.d.ts +1 -1
  40. package/dist/services/onboarding/onboarding-service-build-context.d.ts +0 -1
  41. package/dist/services/onboarding/onboarding-service-build-context.js +7 -3
  42. package/dist/services/onboarding/onboarding-service-factory.js +7 -9
  43. package/dist/services/onboarding/spec/onboarding-service-factory.spec.js +0 -1
  44. package/dist/services/onboarding/templates/install-speed-kit-js-template.js +5 -0
  45. package/dist/services/query-builder/query-builder-factory.js +3 -3
  46. package/oclif.manifest.json +6 -2
  47. package/package.json +2 -1
@@ -13,6 +13,11 @@ export declare class DocumentHandlerServer {
13
13
  private entityManager?;
14
14
  private documentHandlerCode;
15
15
  private documentHandlerConfigCode;
16
+ /**
17
+ * The packjockel bundle for a customer with the `configPlugins` feature: the handler, its config
18
+ * and the Speed Kit config in one CommonJS module, which exports `post`.
19
+ */
20
+ private documentHandlerBundle?;
16
21
  private database;
17
22
  /** Sources of app modules already looked up; `null` marks a lookup that came back empty. */
18
23
  private readonly appModules;
@@ -22,11 +27,23 @@ export declare class DocumentHandlerServer {
22
27
  */
23
28
  constructor(customerConfig: CustomerConfig, files: FileListInterface, bundler: BundleService, cli: CliServiceInterface, workSpace: string, verboseLevel?: boolean, entityManager?: EntityManager);
24
29
  transform(contentType: string, html: string, variation: string, url: string, headers: Record<string, string>): Promise<DocumentHandlerResponse>;
30
+ /** Runs the handler in the context, and returns the config, which calls its `post`. */
31
+ private createScript;
32
+ /** Loads the packjockel bundle like a CommonJS module, and calls the `post` it exports. */
33
+ private createBundleScript;
34
+ private getDocumentHandlerConfigPath;
25
35
  private getTransformFunctionWrapper;
26
36
  buildDocumentHandler(): Promise<void>;
37
+ /**
38
+ * Bundles the handler, its config and the Speed Kit config into one module, the way the server
39
+ * deploys a customer with the `configPlugins` feature.
40
+ */
41
+ private bundleWithPackjockel;
27
42
  private deactivateReleaseAndDeploymentDetection;
28
43
  private getDocumentHandler;
29
44
  private getDynamicFetcher;
45
+ /** The customer's own dynamic fetcher, or else the source of the latest one. */
46
+ private getDynamicFetcherSource;
30
47
  private getDynamicFetcherConfig;
31
48
  private getStyles;
32
49
  private getDocumentHandlerConfig;
@@ -1,7 +1,9 @@
1
- import { LATEST_DYNAMIC_FETCHER_URL, LOCAL_DOCUMENT_HANDLER, LOCAL_DOCUMENT_HANDLER_CONFIG, LOCAL_DYNAMIC_FETCHER, LOCAL_DYNAMIC_FETCHER_CONFIG, LOCAL_STYLES, } from "./document-handler-runtime-model.js";
1
+ import { DYNAMIC_FETCHER_BASE_PATH, LATEST_DYNAMIC_FETCHER_URL, LOCAL_DOCUMENT_HANDLER, LOCAL_DOCUMENT_HANDLER_CONFIG, LOCAL_DYNAMIC_FETCHER, LOCAL_DYNAMIC_FETCHER_CONFIG, LOCAL_STYLES, } from "./document-handler-runtime-model.js";
2
2
  import RequiredFileNotFoundError from "./error/required-file-not-found-error.js";
3
3
  import DatabaseMock from "./templates/database-mock.js";
4
4
  import * as vm from "node:vm";
5
+ import { speedKitConfigBasePath, } from "../bundler/index.js";
6
+ import { usesConfigPlugins, } from "../integration-api/index.js";
5
7
  import { INTEGRATION_FILES } from "../../models/files.js";
6
8
  // documentHandlerDependencies
7
9
  import fetch from "node-fetch";
@@ -25,6 +27,11 @@ export class DocumentHandlerServer {
25
27
  entityManager;
26
28
  documentHandlerCode;
27
29
  documentHandlerConfigCode;
30
+ /**
31
+ * The packjockel bundle for a customer with the `configPlugins` feature: the handler, its config
32
+ * and the Speed Kit config in one CommonJS module, which exports `post`.
33
+ */
34
+ documentHandlerBundle;
28
35
  database;
29
36
  /** Sources of app modules already looked up; `null` marks a lookup that came back empty. */
30
37
  appModules = new Map();
@@ -42,7 +49,11 @@ export class DocumentHandlerServer {
42
49
  this.entityManager = entityManager;
43
50
  }
44
51
  async transform(contentType, html, variation, url, headers) {
45
- if (!this.documentHandlerCode || !this.documentHandlerConfigCode) {
52
+ const configPlugins = usesConfigPlugins(this.customerConfig);
53
+ const built = configPlugins
54
+ ? this.documentHandlerBundle
55
+ : this.documentHandlerCode && this.documentHandlerConfigCode;
56
+ if (!built) {
46
57
  await this.buildDocumentHandler();
47
58
  }
48
59
  const skInternalRequest = new Request(url, html, variation, headers, {
@@ -57,26 +68,21 @@ export class DocumentHandlerServer {
57
68
  skInternalError: {},
58
69
  Abort: DocumentHandlerAbort,
59
70
  APP: this.customerConfig.app,
60
- DYNAMIC_FETCHER: await this.getDynamicFetcher(),
61
- DYNAMIC_FETCHER_CONFIG: await this.getDynamicFetcherConfig(),
62
- CUSTOM_STYLES: this.getStyles(),
71
+ // packjockel defines these in the bundle itself.
72
+ ...(configPlugins
73
+ ? {}
74
+ : {
75
+ DYNAMIC_FETCHER: await this.getDynamicFetcher(),
76
+ DYNAMIC_FETCHER_CONFIG: await this.getDynamicFetcherConfig(),
77
+ CUSTOM_STYLES: this.getStyles(),
78
+ }),
63
79
  ...this.getContextVars(),
64
80
  ...this.createNodeVmContext(),
65
81
  skErrorContext: {},
66
82
  };
67
- const customDocumentHandlerFile = this.files.getByName(INTEGRATION_FILES.CUSTOM.DOCUMENT_HANDLER);
68
- if (customDocumentHandlerFile.path) {
69
- vm.runInNewContext(this.documentHandlerCode, vmContext, {
70
- filename: customDocumentHandlerFile.path,
71
- displayErrors: true,
72
- });
73
- }
74
- else {
75
- vm.runInNewContext(this.documentHandlerCode, vmContext);
76
- }
77
- const script = new vm.Script(this.documentHandlerConfigCode + this.getTransformFunctionWrapper(), {
78
- filename: this.files.getByName("config_documentHandler").path,
79
- });
83
+ const script = configPlugins
84
+ ? this.createBundleScript()
85
+ : this.createScript(vmContext);
80
86
  const vmResponse = await safe(new Promise((resolve, reject) => {
81
87
  vmContext.skInternalResponse.resolve = resolve;
82
88
  // @ts-expect-error reject is unknown
@@ -92,17 +98,66 @@ export class DocumentHandlerServer {
92
98
  }
93
99
  throw new DocumentHandlerTransformError(vmResponse.errorObj);
94
100
  }
101
+ /** Runs the handler in the context, and returns the config, which calls its `post`. */
102
+ createScript(vmContext) {
103
+ const customDocumentHandlerFile = this.files.getByName(INTEGRATION_FILES.CUSTOM.DOCUMENT_HANDLER);
104
+ if (customDocumentHandlerFile.path) {
105
+ vm.runInNewContext(this.documentHandlerCode, vmContext, {
106
+ filename: customDocumentHandlerFile.path,
107
+ displayErrors: true,
108
+ });
109
+ }
110
+ else {
111
+ vm.runInNewContext(this.documentHandlerCode, vmContext);
112
+ }
113
+ return new vm.Script(this.documentHandlerConfigCode + this.getTransformFunctionWrapper(), { filename: this.getDocumentHandlerConfigPath() });
114
+ }
115
+ /** Loads the packjockel bundle like a CommonJS module, and calls the `post` it exports. */
116
+ createBundleScript() {
117
+ return new vm.Script(`(function (module, exports) {\n${this.documentHandlerBundle}\n})(module, module.exports);\n\n` +
118
+ `module.exports.post(db, skInternalRequest, skInternalResponse).catch((error)=>reject(error));`, { filename: this.getDocumentHandlerConfigPath() });
119
+ }
120
+ getDocumentHandlerConfigPath() {
121
+ return this.files.getByName("config_documentHandler")
122
+ .path;
123
+ }
95
124
  getTransformFunctionWrapper() {
96
125
  return `\n\npost(db, skInternalRequest, skInternalResponse).catch((error)=>reject(error));`;
97
126
  }
98
127
  async buildDocumentHandler() {
99
128
  this.cli.startAction("DOCUMENTHANDLER:BUILD", "rebuild documentHandler");
129
+ if (usesConfigPlugins(this.customerConfig)) {
130
+ this.documentHandlerBundle = await this.bundleWithPackjockel();
131
+ this.cli.successAction("DOCUMENTHANDLER:BUILD");
132
+ return;
133
+ }
100
134
  this.documentHandlerCode = await this.bundleDocumentHandlerCode("custom_documentHandler.js", await this.getDocumentHandler());
101
135
  this.documentHandlerConfigCode = await this.bundleDocumentHandlerCode("config_documentHandler.js", this.deactivateReleaseAndDeploymentDetection());
102
136
  this.cli.successAction("DOCUMENTHANDLER:BUILD");
103
137
  }
138
+ /**
139
+ * Bundles the handler, its config and the Speed Kit config into one module, the way the server
140
+ * deploys a customer with the `configPlugins` feature.
141
+ */
142
+ async bundleWithPackjockel() {
143
+ return this.bundler.packjockel.bundleDocumentHandler({
144
+ documentHandler: await this.getDocumentHandler(),
145
+ documentHandlerConfig: this.deactivateReleaseAndDeploymentDetection(),
146
+ speedKitConfig: this.files
147
+ .getByName(INTEGRATION_FILES.CONFIG.SPEED_KIT)
148
+ .getContent(),
149
+ appName: this.customerConfig.app,
150
+ dynamicFetcher: await this.getDynamicFetcherSource(),
151
+ dynamicFetcherConfig: await this.getDynamicFetcherConfig(),
152
+ customStyle: this.getStyles(),
153
+ basePaths: {
154
+ "speed-kit-config": speedKitConfigBasePath(this.customerConfig.configPluginsVersion),
155
+ "dynamic-fetcher": DYNAMIC_FETCHER_BASE_PATH,
156
+ },
157
+ });
158
+ }
104
159
  deactivateReleaseAndDeploymentDetection() {
105
- return this.getDocumentHandlerConfig().replace("const documentHandler =", (selected) => {
160
+ return this.getDocumentHandlerConfig().replace(/const documentHandler =|export default new /, (selected) => {
106
161
  return (`config["executeDeploymentDetection"] = false;config["executeReleaseDetection"] = false;\n` +
107
162
  selected);
108
163
  });
@@ -113,10 +168,17 @@ export class DocumentHandlerServer {
113
168
  .getContent();
114
169
  }
115
170
  async getDynamicFetcher() {
171
+ if (!this.files.hasFile(LOCAL_DYNAMIC_FETCHER)) {
172
+ return await this.bundleDynamicFetcher({ "dynamic-fetcher": DYNAMIC_FETCHER_BASE_PATH }, await this.getDynamicFetcherSource());
173
+ }
174
+ return this.getDynamicFetcherSource();
175
+ }
176
+ /** The customer's own dynamic fetcher, or else the source of the latest one. */
177
+ async getDynamicFetcherSource() {
116
178
  // todo use fetchRemoteFile from integration api
117
179
  if (!this.files.hasFile(LOCAL_DYNAMIC_FETCHER)) {
118
180
  const response = await fetch(LATEST_DYNAMIC_FETCHER_URL);
119
- return await this.bundleDynamicFetcher({ "dynamic-fetcher": "https://www.baqend.com/speed-kit/latest/" }, await response.text());
181
+ return await response.text();
120
182
  }
121
183
  return this.files
122
184
  .getByName(INTEGRATION_FILES.CUSTOM.DYNAMIC_FETCHER)
@@ -25,7 +25,7 @@ export default class DocumentHandlerRuntimeServiceFactory {
25
25
  const customerConfig = this.getCustomerConfig(files);
26
26
  const cliService = this.context.environment.cli;
27
27
  await this.installAdditionalDependencies(customerConfig, cliService);
28
- const bundler = new BundleServiceFactory(this.context.customerPath).buildService();
28
+ const bundler = new BundleServiceFactory(this.context.customerPath, this.context.environment.settings.packjockelPath).buildService();
29
29
  // the non-prompting lookup: the document handler runs with a session when
30
30
  // there is one and without when there is not, so it must not stop the
31
31
  // command at a login prompt to find out
@@ -30,7 +30,7 @@ export class IntegrationApiFactory {
30
30
  const virtualFileHandler = new VirtualFileHandler(cli);
31
31
  this.service = new IntegrationApiService(fileReader, virtualFileHandler, new MandatoryFilesValidator(), [
32
32
  new CustomerConfigHandler(fileReader, cli, new CustomerConfigValidator(), this.context.configName, this.context.environment.settings.testApp, this.context.useTestConfig ?? false),
33
- new InstallResourceHandler(fileReader, new BundleServiceFactory("./").buildService()),
33
+ new InstallResourceHandler(fileReader, new BundleServiceFactory("./", this.context.environment.settings.packjockelPath).buildService()),
34
34
  new ServerConfigHandler(fileReader),
35
35
  new ModuleHandler(fileReader),
36
36
  ], new FileWriter(fs, this.context.basePath), this.whitelist.length === 0);
@@ -13,6 +13,7 @@ export * from "./virtual/external-recipe.js";
13
13
  export * from "./virtual/static-recipe.js";
14
14
  export * from "./external/external-file-reader.js";
15
15
  import type { SpeedKitConfig } from "./struct/speed-kit-config.js";
16
+ import type { SpeedKitConfigCustomer } from "./struct/speed-kit-config-file.js";
16
17
  import { CustomerConfig } from "./struct/customer-config.js";
17
18
  import { RoFile } from "./struct/ro-file.js";
18
19
  import { VirtualFile } from "./struct/virtual-file.js";
@@ -89,7 +90,7 @@ export interface InstallResourceInterface extends ConfigFileInterface, FileInter
89
90
  readonly remoteKey: string;
90
91
  }
91
92
  export interface SpeedKitConfigInterface extends InstallResourceInterface {
92
- evaluate(app: string, origin: string): Promise<SpeedKitConfig>;
93
+ evaluate(customer: SpeedKitConfigCustomer, origin: string): Promise<SpeedKitConfig>;
93
94
  }
94
95
  export interface CustomerConfigInterface extends ConfigFileInterface {
95
96
  config: CustomerConfig;
@@ -2,8 +2,21 @@ import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
3
  import { SpeedKitConfigFile, VirtualSpeedKitConfigFile } from "../index.js";
4
4
  import { INTEGRATION_FILES } from "../../../models/files.js";
5
- import { BundleServiceFactory } from "../../bundler/index.js";
5
+ import { BundleService, BundleServiceFactory, } from "../../bundler/index.js";
6
6
  const bundler = new BundleServiceFactory("./").buildService();
7
+ const SHOP_DE = { app: "shop-de" };
8
+ /** Answers every bundle request with one bundle and keeps the requests. */
9
+ class FakePackjockel {
10
+ bundled;
11
+ requests = [];
12
+ constructor(bundled) {
13
+ this.bundled = bundled;
14
+ }
15
+ async bundle(request) {
16
+ this.requests.push(request);
17
+ return this.bundled;
18
+ }
19
+ }
7
20
  function configFile(source) {
8
21
  return new SpeedKitConfigFile({
9
22
  name: INTEGRATION_FILES.CONFIG.SPEED_KIT,
@@ -13,7 +26,7 @@ function configFile(source) {
13
26
  }, bundler);
14
27
  }
15
28
  function evaluate(source) {
16
- return configFile(source).evaluate("shop-de", "https://www.shop.de");
29
+ return configFile(source).evaluate(SHOP_DE, "https://www.shop.de");
17
30
  }
18
31
  /** What the config assigned, for the tests about the browser mock itself. */
19
32
  async function evaluateRaw(source) {
@@ -89,19 +102,19 @@ describe("SpeedKitConfigFile", () => {
89
102
  // builder and the pre-deploy checks both go through here.
90
103
  const file = configFile(`window.speedKit = { host: window.location.host };`);
91
104
  const [first, second] = await Promise.all([
92
- file.evaluate("shop-de", "https://www.shop.de"),
93
- file.evaluate("shop-de", "https://www.shop.de"),
105
+ file.evaluate(SHOP_DE, "https://www.shop.de"),
106
+ file.evaluate(SHOP_DE, "https://www.shop.de"),
94
107
  ]);
95
- const otherOrigin = await file.evaluate("shop-de", "https://m.shop.de");
108
+ const otherOrigin = await file.evaluate(SHOP_DE, "https://m.shop.de");
96
109
  expect(first).to.equal(second);
97
110
  expect(otherOrigin).to.not.equal(first);
98
111
  expect(otherOrigin.raw.host).to.equal("m.shop.de");
99
112
  });
100
113
  it("should forget what it read when the file changes", async () => {
101
114
  const file = configFile(`window.speedKit = { split: 0.1 };`);
102
- const before = await file.evaluate("shop-de", "https://www.shop.de");
115
+ const before = await file.evaluate(SHOP_DE, "https://www.shop.de");
103
116
  file.setContent(`window.speedKit = { split: 0.5 };`);
104
- const after = await file.evaluate("shop-de", "https://www.shop.de");
117
+ const after = await file.evaluate(SHOP_DE, "https://www.shop.de");
105
118
  expect(before.split).to.equal(0.1);
106
119
  expect(after.split).to.equal(0.5);
107
120
  });
@@ -109,8 +122,57 @@ describe("SpeedKitConfigFile", () => {
109
122
  const source = `window.speedKit = { split: 0.5, appName: APP };`;
110
123
  const remote = await VirtualSpeedKitConfigFile.createRemote(source, bundler);
111
124
  const local = await evaluate(source);
112
- const deployed = await remote.evaluate("shop-de", "https://www.shop.de");
125
+ const deployed = await remote.evaluate(SHOP_DE, "https://www.shop.de");
113
126
  expect(deployed).to.deep.equal(local);
114
127
  expect(remote.getContent()).to.equal(source);
115
128
  });
129
+ describe("with the configPlugins feature", () => {
130
+ const SOURCE = "window.speedKit = { split: 0.1 };";
131
+ const EMBEDDED = {
132
+ app: "shop-de",
133
+ features: { configPlugins: true },
134
+ configPluginsVersion: "@release",
135
+ };
136
+ function embeddedFile(bundled) {
137
+ const packjockel = new FakePackjockel(bundled);
138
+ const file = new SpeedKitConfigFile({
139
+ name: INTEGRATION_FILES.CONFIG.SPEED_KIT,
140
+ path: "config_SpeedKit.js",
141
+ content: SOURCE,
142
+ remoteKey: "config",
143
+ }, new BundleService("./", packjockel));
144
+ return { file, packjockel };
145
+ }
146
+ it("should bundle with packjockel and its plugins of the pinned release", async () => {
147
+ const { file, packjockel } = embeddedFile(`SpeedKit.config = {};`);
148
+ await file.evaluate(EMBEDDED, "https://www.shop.de");
149
+ expect(packjockel.requests).to.have.length(1);
150
+ expect(packjockel.requests[0]).to.deep.equal({
151
+ entryPoints: { "config.js": SOURCE },
152
+ basePaths: {
153
+ "speed-kit-config": "https://www.baqend.com/speed-kit-config/release/",
154
+ },
155
+ define: { APP: '"shop-de"' },
156
+ platform: "browser",
157
+ });
158
+ });
159
+ it("should read SpeedKit.config and the plugins pushed without a guard", async () => {
160
+ const { file } = embeddedFile(`SpeedKit.configPlugins.push({ name: "StripQueryParamPlugin" });
161
+ SpeedKit.config = { split: 0.3, appName: "shop-de" };`);
162
+ const config = await file.evaluate(EMBEDDED, "https://www.shop.de");
163
+ expect(config.split).to.equal(0.3);
164
+ expect(config.raw.appName).to.equal("shop-de");
165
+ });
166
+ it("should still read a config that assigns window.speedKit", async () => {
167
+ const { file } = embeddedFile(`window.speedKit = { split: 0.7 };`);
168
+ const config = await file.evaluate(EMBEDDED, "https://www.shop.de");
169
+ expect(config.split).to.equal(0.7);
170
+ });
171
+ it("should not reuse a reading of the other bundler", async () => {
172
+ const { file, packjockel } = embeddedFile(`SpeedKit.config = {};`);
173
+ await file.evaluate(EMBEDDED, "https://www.shop.de");
174
+ await file.evaluate({ app: "shop-de" }, "https://www.shop.de");
175
+ expect(packjockel.requests).to.have.length(1);
176
+ });
177
+ });
116
178
  });
@@ -1,3 +1,15 @@
1
+ /**
2
+ * The installation features of a config. `null` leaves a feature at its default.
3
+ */
4
+ export interface CustomerFeatures {
5
+ /**
6
+ * packjockel bundles the config with the config plugins of its target and
7
+ * embeds it into the document handler and the install script. The config
8
+ * writes `SpeedKit.config`.
9
+ */
10
+ configPlugins?: boolean | null;
11
+ [feature: string]: unknown;
12
+ }
1
13
  export interface CustomerConfigs {
2
14
  production: CustomerConfig;
3
15
  [configName: string]: CustomerConfig;
@@ -21,6 +33,12 @@ export declare class CustomerConfig {
21
33
  * installation whose split is greater than 0 for any origin.
22
34
  */
23
35
  readonly needsCustomerApproval?: boolean;
24
- constructor(app: string, origins: string[], domain: string, swPath: string, scope: string, appDomain?: string, installPath?: string, installParameters?: string, name?: string, forceInstall?: boolean, chromeFlags?: string[], dependencies?: Record<string, string>, needsCustomerApproval?: boolean);
36
+ readonly features?: CustomerFeatures;
37
+ /**
38
+ * The speed-kit-config release the packjockel bundles import plugins from: a version such as
39
+ * `1.8.0`, or a channel such as `@release`.
40
+ */
41
+ readonly configPluginsVersion?: string;
42
+ constructor(app: string, origins: string[], domain: string, swPath: string, scope: string, appDomain?: string, installPath?: string, installParameters?: string, name?: string, forceInstall?: boolean, chromeFlags?: string[], dependencies?: Record<string, string>, needsCustomerApproval?: boolean, features?: CustomerFeatures, configPluginsVersion?: string);
25
43
  static createFrom(configFile: Partial<CustomerConfig>, configName?: string): CustomerConfig;
26
44
  }
@@ -18,7 +18,13 @@ export class CustomerConfig {
18
18
  * installation whose split is greater than 0 for any origin.
19
19
  */
20
20
  needsCustomerApproval;
21
- constructor(app, origins, domain, swPath, scope, appDomain, installPath, installParameters, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval) {
21
+ features;
22
+ /**
23
+ * The speed-kit-config release the packjockel bundles import plugins from: a version such as
24
+ * `1.8.0`, or a channel such as `@release`.
25
+ */
26
+ configPluginsVersion;
27
+ constructor(app, origins, domain, swPath, scope, appDomain, installPath, installParameters, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval, features, configPluginsVersion) {
22
28
  this.app = app;
23
29
  this.origins = origins;
24
30
  this.domain = domain;
@@ -32,11 +38,13 @@ export class CustomerConfig {
32
38
  this.chromeFlags = chromeFlags;
33
39
  this.dependencies = dependencies;
34
40
  this.needsCustomerApproval = needsCustomerApproval;
41
+ this.features = features;
42
+ this.configPluginsVersion = configPluginsVersion;
35
43
  }
36
44
  static createFrom(configFile, configName) {
37
- const { app, origins, domain, swPath, scope, appDomain, installPath, installParams, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval, } = configFile;
45
+ const { app, origins, domain, swPath, scope, appDomain, installPath, installParams, name, forceInstall, chromeFlags, dependencies, needsCustomerApproval, features, configPluginsVersion, } = configFile;
38
46
  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);
47
+ `https://${app}.app.baqend.com/v1/speedkit/install.js?d=${name || configName}`, installParams || 'async="" crossorigin="anonymous"', name, forceInstall || false, chromeFlags, dependencies || {}, needsCustomerApproval || false, features || {}, configPluginsVersion);
40
48
  }
41
49
  }
42
50
  class RequiredParameter {
@@ -1,7 +1,10 @@
1
- import type { BundleService } from "../../bundler/index.js";
1
+ import { type 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" | "configPluginsVersion">;
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,5 @@ export declare class VirtualSpeedKitConfigFile extends SpeedKitConfigFile implem
60
63
  delete(): Promise<void>;
61
64
  onChange(): void;
62
65
  }
66
+ /** The customer has the `configPlugins` feature: packjockel bundles its config. */
67
+ export declare function usesConfigPlugins(customer: SpeedKitConfigCustomer): boolean;
@@ -1,9 +1,10 @@
1
1
  import * as vm from "node:vm";
2
+ import { speedKitConfigBasePath, } from "../../bundler/index.js";
2
3
  import { EvaluateSkConfigError } from "../error/evaluate-sk-config-error.js";
3
4
  import ApplicationError from "../../error-handling/error/application-error.js";
4
5
  import { StaticRecipe } from "../virtual/static-recipe.js";
5
6
  import { InstallResourceFile } from "./install-resource-file.js";
6
- import { SpeedKitConfig } from "./speed-kit-config.js";
7
+ import { SpeedKitConfig, } from "./speed-kit-config.js";
7
8
  /** The name the config deployed to the app is read back under. */
8
9
  export const REMOTE_SPEED_KIT_CONFIG = "config_SpeedKit@remote";
9
10
  /**
@@ -17,7 +18,7 @@ export const REMOTE_SPEED_KIT_CONFIG = "config_SpeedKit@remote";
17
18
  export class SpeedKitConfigFile extends InstallResourceFile {
18
19
  bundler;
19
20
  /**
20
- * One evaluation per app and origin. A config branches on
21
+ * One evaluation per app, origin and bundler. A config branches on
21
22
  * `window.location`, so the same file read for two origins is two different
22
23
  * configs — and bundling one is expensive enough not to repeat per caller.
23
24
  */
@@ -34,14 +35,15 @@ export class SpeedKitConfigFile extends InstallResourceFile {
34
35
  super.setContent(content);
35
36
  this.evaluations.clear();
36
37
  }
37
- /** The config this file describes for one app and origin. */
38
- evaluate(app, origin) {
39
- const key = `${app}|${origin}`;
38
+ /** The config this file describes for one customer and origin. */
39
+ evaluate(customer, origin) {
40
+ const { app } = customer;
41
+ const key = `${app}|${origin}|${usesConfigPlugins(customer) ? "packjockel" : "esbuild"}`;
40
42
  const cached = this.evaluations.get(key);
41
43
  if (cached) {
42
44
  return cached;
43
45
  }
44
- const evaluation = bundle(this.bundler, this.getContent(), app)
46
+ const evaluation = bundle(this.bundler, this.getContent(), customer)
45
47
  .then((code) => evaluateSpeedKitConfig(app, origin, code, this.path))
46
48
  .catch((error) => {
47
49
  // A failure is not an answer: forget it, so the next caller — or the
@@ -95,7 +97,8 @@ export class VirtualSpeedKitConfigFile extends SpeedKitConfigFile {
95
97
  }
96
98
  /**
97
99
  * 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.
100
+ * `SpeedKit.config`, or else to `window.speedKit`, together with the config
101
+ * plugins it registered.
99
102
  *
100
103
  * @param app the app the config is evaluated for, exposed to it as `APP`
101
104
  * @param origin the origin the config is evaluated for, its `window.location`
@@ -113,7 +116,7 @@ function evaluateSpeedKitConfig(app, origin, code, filename) {
113
116
  if (browserContext.vmError !== null) {
114
117
  throw new EvaluateSkConfigError(`[${filename}] ${browserContext.vmError?.message}`);
115
118
  }
116
- return SpeedKitConfig.createFrom(browserContext.window.speedKit, registeredPlugins(browserContext));
119
+ return SpeedKitConfig.createFrom(browserContext.window.SpeedKit.config ?? browserContext.window.speedKit, registeredPlugins(browserContext));
117
120
  }
118
121
  /**
119
122
  * The plugins a config registered, read the way `ConfigController.getPlugins`
@@ -136,7 +139,9 @@ function buildBrowserMock(origin, app) {
136
139
  },
137
140
  customVariation: [],
138
141
  },
139
- SpeedKit: {},
142
+ // An embedded config pushes its plugins without creating the list, the
143
+ // way packjockel hands it over.
144
+ SpeedKit: { configPlugins: [] },
140
145
  serviceWorkerUrl: "",
141
146
  screen: { width: 0, height: 0 },
142
147
  devicePixelRatio: 1,
@@ -214,10 +219,25 @@ function safeFileContentWrapper(fileContent) {
214
219
  }
215
220
  `;
216
221
  }
217
- async function bundle(bundler, content, app) {
222
+ /** The customer has the `configPlugins` feature: packjockel bundles its config. */
223
+ export function usesConfigPlugins(customer) {
224
+ return customer.features?.configPlugins === true;
225
+ }
226
+ async function bundle(bundler, content, customer) {
218
227
  if (!content) {
219
228
  throw new ApplicationError("Content is empty");
220
229
  }
230
+ const app = customer.app;
231
+ if (usesConfigPlugins(customer)) {
232
+ return bundler.packjockel.bundle({
233
+ entryPoints: { "config.js": content },
234
+ basePaths: {
235
+ "speed-kit-config": speedKitConfigBasePath(customer.configPluginsVersion),
236
+ },
237
+ define: { APP: JSON.stringify(app) },
238
+ platform: "browser",
239
+ });
240
+ }
221
241
  return bundler.bundle({
222
242
  basePaths: {
223
243
  "speed-kit-config": "https://www.baqend.com/speed-kit-config/latest/",
@@ -7,10 +7,10 @@ export declare class SpeedKitInstallContext {
7
7
  private files;
8
8
  private bundler;
9
9
  private skTemplate;
10
- private isLocalRuntime;
11
- constructor(customerConfig: CustomerConfig, configName: string, files: FileListInterface, bundler: BundleService, skTemplate: InstallSpeedKitJsTemplate, isLocalRuntime?: boolean);
10
+ constructor(customerConfig: CustomerConfig, configName: string, files: FileListInterface, bundler: BundleService, skTemplate: InstallSpeedKitJsTemplate);
12
11
  getSpeedKitInstallJs(): Promise<string>;
13
12
  private prepareConfigLoadHandler;
13
+ /** The Speed Kit config, with `delayRacedOrigin` set to 100 ms for the local documentHandler. */
14
14
  private getConfigSpeedKit;
15
15
  /**
16
16
  * Fetches and returns the latest dynamic fetcher from the code repository. If a new version is available online it is
@@ -1,3 +1,5 @@
1
+ import { speedKitConfigBasePath } from "../../bundler/index.js";
2
+ import { usesConfigPlugins, } from "../../integration-api/index.js";
1
3
  import { INTEGRATION_FILES } from "../../../models/files.js";
2
4
  const IMPORT_PATHS = {
3
5
  rum: "https://www.baqend.com/rum/latest/",
@@ -11,18 +13,18 @@ export class SpeedKitInstallContext {
11
13
  files;
12
14
  bundler;
13
15
  skTemplate;
14
- isLocalRuntime;
15
- constructor(customerConfig, configName, files, bundler, skTemplate, isLocalRuntime = false) {
16
+ constructor(customerConfig, configName, files, bundler, skTemplate) {
16
17
  this.customerConfig = customerConfig;
17
18
  this.configName = configName;
18
19
  this.files = files;
19
20
  this.bundler = bundler;
20
21
  this.skTemplate = skTemplate;
21
- this.isLocalRuntime = isLocalRuntime;
22
22
  }
23
23
  async getSpeedKitInstallJs() {
24
+ const configPlugins = usesConfigPlugins(this.customerConfig);
24
25
  const configJs = this.skTemplate.render({
25
26
  config: this.getConfigSpeedKit(),
27
+ configPlugins,
26
28
  forceInstall: this.customerConfig.forceInstall,
27
29
  installPath: this.customerConfig.installPath,
28
30
  installParams: this.customerConfig.installParams,
@@ -31,15 +33,27 @@ export class SpeedKitInstallContext {
31
33
  .getByName(INTEGRATION_FILES.CUSTOM.DYNAMIC_FETCHER)
32
34
  .getContent(),
33
35
  });
36
+ const entryPoints = {
37
+ "rum.js": this.prepareConfigLoadHandler(),
38
+ "config.js": configJs,
39
+ "loader.js": this.files
40
+ .getByName(INTEGRATION_FILES.CUSTOM.LOADER)
41
+ .getContent(),
42
+ };
43
+ if (configPlugins) {
44
+ return this.bundler.packjockel.bundle({
45
+ entryPoints,
46
+ basePaths: {
47
+ ...IMPORT_PATHS,
48
+ "speed-kit-config": speedKitConfigBasePath(this.customerConfig.configPluginsVersion),
49
+ },
50
+ define: { APP: JSON.stringify(this.customerConfig.app) },
51
+ platform: "browser",
52
+ });
53
+ }
34
54
  return this.bundler.bundle({
35
55
  basePaths: IMPORT_PATHS,
36
- entryPoints: {
37
- "rum.js": this.prepareConfigLoadHandler(),
38
- "config.js": configJs,
39
- "loader.js": this.files
40
- .getByName(INTEGRATION_FILES.CUSTOM.LOADER)
41
- .getContent(),
42
- },
56
+ entryPoints,
43
57
  logLevel: "error",
44
58
  minify: false,
45
59
  platform: "browser",
@@ -64,19 +78,17 @@ export class SpeedKitInstallContext {
64
78
  // `import debugPreloading from "predictive-preloading/predictivePreloading.dev.js"`,
65
79
  // );
66
80
  }
81
+ /** The Speed Kit config, with `delayRacedOrigin` set to 100 ms for the local documentHandler. */
67
82
  getConfigSpeedKit() {
68
83
  const configString = this.files
69
84
  .getByName(INTEGRATION_FILES.CONFIG.SPEED_KIT)
70
85
  .getContent();
71
- if (!this.isLocalRuntime) {
72
- return configString;
73
- }
74
86
  if (configString.includes("delayRacedOrigin")) {
75
87
  return configString.replace(/delayRacedOrigin:[^\d,|](\d*)[^,]*,/, (config, value) => {
76
88
  return config.replace(value, "100");
77
89
  });
78
90
  }
79
- return configString.replace(/window\.speedKit[^{]*=[^{]*{/, (configOpenTag) => {
91
+ return configString.replace(/(?:window\.speedKit|SpeedKit\.config)[^{]*=[^{]*{/, (configOpenTag) => {
80
92
  return configOpenTag + `\n delayRacedOrigin: 100,\n`;
81
93
  });
82
94
  }