@speedkit/cli 4.36.2 → 4.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +12 -5
  3. package/dist/commands/find-pops.js +2 -2
  4. package/dist/commands/generate-pop-config.js +9 -2
  5. package/dist/commands/login.js +0 -1
  6. package/dist/commands/onboarding.d.ts +2 -1
  7. package/dist/commands/onboarding.js +2 -0
  8. package/dist/helpers/origin-proxy.d.ts +2 -2
  9. package/dist/helpers/origin-proxy.js +24 -11
  10. package/dist/helpers/spec/origin-proxy.spec.js +85 -0
  11. package/dist/services/athena/athena-service-build-context.d.ts +8 -0
  12. package/dist/services/athena/athena-service-factory.d.ts +10 -0
  13. package/dist/services/athena/athena-service-factory.js +18 -2
  14. package/dist/services/athena/baqend-athena-service.d.ts +27 -0
  15. package/dist/services/athena/baqend-athena-service.js +69 -7
  16. package/dist/services/athena/error/baqend-query-error.d.ts +4 -0
  17. package/dist/services/athena/error/baqend-query-error.js +4 -0
  18. package/dist/services/athena/error/unreadable-response.d.ts +18 -0
  19. package/dist/services/athena/error/unreadable-response.js +29 -0
  20. package/dist/services/athena/index.d.ts +2 -0
  21. package/dist/services/athena/index.js +2 -0
  22. package/dist/services/athena/spec/baqend-athena-service.spec.js +84 -8
  23. package/dist/services/cli/cli-environment.d.ts +7 -0
  24. package/dist/services/cli/cli-environment.js +7 -0
  25. package/dist/services/config-api/client.js +3 -3
  26. package/dist/services/config-api/config-api-factory.d.ts +12 -1
  27. package/dist/services/config-api/config-api-factory.js +13 -3
  28. package/dist/services/config-api/ensure-login.d.ts +52 -0
  29. package/dist/services/config-api/ensure-login.js +93 -0
  30. package/dist/services/config-api/entity-manager-provider.d.ts +24 -2
  31. package/dist/services/config-api/entity-manager-provider.js +40 -6
  32. package/dist/services/config-api/error/login-error.d.ts +7 -1
  33. package/dist/services/config-api/error/login-error.js +13 -2
  34. package/dist/services/config-api/spec/ensure-login.spec.d.ts +1 -0
  35. package/dist/services/config-api/spec/ensure-login.spec.js +95 -0
  36. package/dist/services/deploy/deploy-service-build-context.d.ts +7 -0
  37. package/dist/services/deploy/deploy-service-factory.js +6 -1
  38. package/dist/services/deploy/mr/merge-request-deploy-service-factory.js +1 -0
  39. package/dist/services/document-handler-runtime/factory/document-handler-runtime-service-factory.js +4 -2
  40. package/dist/services/onboarding/browse/browse-service.d.ts +18 -0
  41. package/dist/services/onboarding/browse/browse-service.js +61 -0
  42. package/dist/services/onboarding/browse/browse-url.d.ts +10 -0
  43. package/dist/services/onboarding/browse/browse-url.js +19 -0
  44. package/dist/services/onboarding/browser/executable/executable-validator.js +3 -1
  45. package/dist/services/onboarding/browser/extension/extension-downloader.js +2 -0
  46. package/dist/services/onboarding/browser/launch-browser.d.ts +9 -0
  47. package/dist/services/onboarding/browser/launch-browser.js +20 -0
  48. package/dist/services/onboarding/error/browse-domain-error.d.ts +6 -0
  49. package/dist/services/onboarding/error/browse-domain-error.js +11 -0
  50. package/dist/services/onboarding/error/missing-customer-path-error.d.ts +6 -0
  51. package/dist/services/onboarding/error/missing-customer-path-error.js +11 -0
  52. package/dist/services/onboarding/onboarding-service-build-context.d.ts +5 -1
  53. package/dist/services/onboarding/onboarding-service-build-context.js +15 -3
  54. package/dist/services/onboarding/onboarding-service-factory.d.ts +34 -1
  55. package/dist/services/onboarding/onboarding-service-factory.js +88 -24
  56. package/dist/services/onboarding/onboarding-service.d.ts +2 -0
  57. package/dist/services/onboarding/onboarding-service.js +5 -16
  58. package/dist/services/onboarding/spec/browse/browse-url.spec.d.ts +1 -0
  59. package/dist/services/onboarding/spec/browse/browse-url.spec.js +28 -0
  60. package/dist/services/onboarding/spec/onboarding-service-factory.spec.d.ts +1 -0
  61. package/dist/services/onboarding/spec/onboarding-service-factory.spec.js +155 -0
  62. package/dist/services/onboarding/spec/virtual-orestes-app/crawler.spec.js +34 -1
  63. package/dist/services/pop-config/pop-config-factory.js +1 -0
  64. package/dist/services/prewarm/auto-pre-warm-factory.js +11 -1
  65. package/dist/services/query-builder/query-builder-factory.js +4 -0
  66. package/oclif.manifest.json +12 -4
  67. package/package.json +1 -1
@@ -1,7 +1,6 @@
1
1
  import { IntegrationApiServiceBuildContext, IntegrationApiFactory, } from "../../integration-api/index.js";
2
2
  import { DocumentHandlerServer } from "../document-handler-server.js";
3
3
  import { BundleServiceFactory } from "../../bundler/index.js";
4
- import { EntityManagerProvider } from "../../config-api/entity-manager-provider.js";
5
4
  import { safe } from "../../../helpers/safe.js";
6
5
  import { existsSync, mkdirSync, writeFileSync } from "node:fs";
7
6
  import { resolve } from "node:path";
@@ -27,7 +26,10 @@ export default class DocumentHandlerRuntimeServiceFactory {
27
26
  const cliService = this.context.environment.cli;
28
27
  await this.installAdditionalDependencies(customerConfig, cliService);
29
28
  const bundler = new BundleServiceFactory(this.context.customerPath).buildService();
30
- const entityManagerResult = await safe(new EntityManagerProvider().getEntityManager(customerConfig.app));
29
+ // the non-prompting lookup: the document handler runs with a session when
30
+ // there is one and without when there is not, so it must not stop the
31
+ // command at a login prompt to find out
32
+ const entityManagerResult = await this.context.environment.entityManagerProvider.tryGetEntityManager(customerConfig.app);
31
33
  return new DocumentHandlerServer(customerConfig, files, bundler, cliService, this.context.environment.nodeModulesPath, this.context.verboseLevel, entityManagerResult.success ? entityManagerResult.data : null);
32
34
  }
33
35
  getCustomerConfig(files) {
@@ -0,0 +1,18 @@
1
+ import { CliServiceInterface } from "../../cli/index.js";
2
+ import { BrowserContext } from "../onboarding-model.js";
3
+ /**
4
+ * `sk onboarding --browse`: the onboarding Chrome on the live site, as a visitor gets it.
5
+ *
6
+ * It launches the same Chrome as onboarding, with the same flags, profile and extensions. It
7
+ * installs no request interception, serves no local files and injects no Speed Kit, so a site that
8
+ * runs Speed Kit in production runs its own deployed build.
9
+ */
10
+ export declare class BrowseService {
11
+ private readonly browserContext;
12
+ private readonly cli;
13
+ constructor(browserContext: BrowserContext, cli: CliServiceInterface);
14
+ /** Opens the site, then blocks until the caller aborts `externalSignal` or Chrome disconnects. */
15
+ run(externalSignal: AbortSignal): Promise<void>;
16
+ private readyLine;
17
+ private findPage;
18
+ }
@@ -0,0 +1,61 @@
1
+ import { safe } from "../../../helpers/safe.js";
2
+ import { launchBrowser, resetBrowserState } from "../browser/launch-browser.js";
3
+ import { BROWSER_EVENTS } from "../onboarding-model.js";
4
+ import { waitForAbort } from "../onboarding-service.js";
5
+ /**
6
+ * `sk onboarding --browse`: the onboarding Chrome on the live site, as a visitor gets it.
7
+ *
8
+ * It launches the same Chrome as onboarding, with the same flags, profile and extensions. It
9
+ * installs no request interception, serves no local files and injects no Speed Kit, so a site that
10
+ * runs Speed Kit in production runs its own deployed build.
11
+ */
12
+ export class BrowseService {
13
+ browserContext;
14
+ cli;
15
+ constructor(browserContext, cli) {
16
+ this.browserContext = browserContext;
17
+ this.cli = cli;
18
+ }
19
+ /** Opens the site, then blocks until the caller aborts `externalSignal` or Chrome disconnects. */
20
+ async run(externalSignal) {
21
+ this.cli.startAction("ONBOARDING:BROWSE:BROWSER", `prepare browser ${this.cli.style.bold(this.browserContext.browserVersionString)}`);
22
+ const disconnected = new AbortController();
23
+ const shutdownSignal = AbortSignal.any([
24
+ externalSignal,
25
+ disconnected.signal,
26
+ ]);
27
+ const browser = await launchBrowser(this.browserContext);
28
+ browser.on(BROWSER_EVENTS.DISCONNECTED, () => {
29
+ this.cli.writeError("Browser disconnected, shutting down");
30
+ disconnected.abort();
31
+ });
32
+ const page = await this.findPage(browser);
33
+ const url = this.browserContext.domain;
34
+ const reset = await safe(page
35
+ .target()
36
+ .createCDPSession()
37
+ .then((session) => resetBrowserState(session, new URL(url).origin)));
38
+ if (reset.success !== true) {
39
+ this.cli.writeWarning(`Could not reset the browser state: ${reset.error}`);
40
+ }
41
+ this.cli.successAction("ONBOARDING:BROWSE:BROWSER");
42
+ const navigation = await safe(page.goto(url));
43
+ if (navigation.success !== true) {
44
+ this.cli.writeWarning(`Could not open ${url}: ${navigation.error}`);
45
+ }
46
+ this.cli.writeSuccess(this.readyLine(url));
47
+ await waitForAbort(shutdownSignal);
48
+ await safe(browser.close());
49
+ }
50
+ readyLine(url) {
51
+ const { debuggingPort, headless, debugPort } = this.browserContext;
52
+ const cdp = debuggingPort || headless
53
+ ? `CDP on http://127.0.0.1:${debugPort}`
54
+ : "no CDP port, pass -p or --headless to expose one";
55
+ return `Browsing ${url} live, without interception or Speed Kit injection; ${cdp}. Press Ctrl+C to stop.`;
56
+ }
57
+ async findPage(browser) {
58
+ const [page] = await browser.pages();
59
+ return page ?? browser.newPage();
60
+ }
61
+ }
@@ -0,0 +1,10 @@
1
+ import { CustomerConfig } from "../../integration-api/index.js";
2
+ /**
3
+ * The URL that browse mode opens.
4
+ *
5
+ * The `--domain` flag wins. Without it, the customer config supplies its `domain`, else its first
6
+ * origin: browse mode never prompts, so it cannot ask which origin to take.
7
+ *
8
+ * @throws {BrowseDomainError} When neither the flag nor a customer config names a domain.
9
+ */
10
+ export declare function resolveBrowseUrl(domain?: string, customerConfig?: Pick<CustomerConfig, "domain" | "origins">): string;
@@ -0,0 +1,19 @@
1
+ import { BrowseDomainError } from "../error/browse-domain-error.js";
2
+ /**
3
+ * The URL that browse mode opens.
4
+ *
5
+ * The `--domain` flag wins. Without it, the customer config supplies its `domain`, else its first
6
+ * origin: browse mode never prompts, so it cannot ask which origin to take.
7
+ *
8
+ * @throws {BrowseDomainError} When neither the flag nor a customer config names a domain.
9
+ */
10
+ export function resolveBrowseUrl(domain, customerConfig) {
11
+ const candidate = domain || customerConfig?.domain || customerConfig?.origins?.[0];
12
+ if (!candidate) {
13
+ throw new BrowseDomainError();
14
+ }
15
+ const url = /^https?:\/\//i.test(candidate)
16
+ ? candidate
17
+ : `https://${candidate}`;
18
+ return new URL(url).href;
19
+ }
@@ -35,7 +35,9 @@ export class ExecutableValidator {
35
35
  this.cli.writeWarning(`No Browser configured`);
36
36
  }
37
37
  // ask if you want to download stable chrome
38
- const result = await this.cli.confirm("Do you want to download Chrome@latest via puppeteer?", true);
38
+ // Without a TTY a prompt never gets an answer and ends the process, so take its default: download.
39
+ const result = this.cli.nonInteractive ||
40
+ (await this.cli.confirm("Do you want to download Chrome@latest via puppeteer?", true));
39
41
  // @todo set more detailed error message here.
40
42
  if (!result) {
41
43
  throw new ApplicationError("No Browser found to use", [
@@ -24,6 +24,8 @@ export class ExtensionDownloader {
24
24
  * update is declined or its download breaks.
25
25
  */
26
26
  async installOrUpdate({ confirm = true, force = false, } = {}) {
27
+ // Without a TTY a prompt never gets an answer and ends the process, so take its default: install.
28
+ confirm = confirm && !this.cli.nonInteractive;
27
29
  // check for already installed devExtension
28
30
  const localVersion = this.getInstalledVersion();
29
31
  const installed = { path: this.installedPath, version: localVersion };
@@ -0,0 +1,9 @@
1
+ import { Browser, CDPSession } from "puppeteer";
2
+ import { BrowserContext } from "../onboarding-model.js";
3
+ /** Launches the onboarding Chrome. Onboarding and browse mode share it, so their hardening cannot drift. */
4
+ export declare function launchBrowser(browserContext: BrowserContext): Promise<Browser>;
5
+ /**
6
+ * Starts from a clean state for `origin`: no service worker, no cookies, no stored data, no
7
+ * emulation left over from an earlier session on the same profile.
8
+ */
9
+ export declare function resetBrowserState(cdpSession: CDPSession, origin: string): Promise<void>;
@@ -0,0 +1,20 @@
1
+ import puppeteerExtra from "puppeteer-extra";
2
+ /** Launches the onboarding Chrome. Onboarding and browse mode share it, so their hardening cannot drift. */
3
+ export function launchBrowser(browserContext) {
4
+ return puppeteerExtra.default.launch(browserContext.getPuppeteerLaunchOptions());
5
+ }
6
+ /**
7
+ * Starts from a clean state for `origin`: no service worker, no cookies, no stored data, no
8
+ * emulation left over from an earlier session on the same profile.
9
+ */
10
+ export async function resetBrowserState(cdpSession, origin) {
11
+ await cdpSession.send("ServiceWorker.enable");
12
+ await cdpSession.send("ServiceWorker.unregister", { scopeURL: origin });
13
+ await cdpSession.send("Storage.clearCookies");
14
+ await cdpSession.send("Storage.clearDataForOrigin", {
15
+ origin,
16
+ storageTypes: "all",
17
+ });
18
+ await cdpSession.send("Emulation.clearDeviceMetricsOverride");
19
+ await cdpSession.send("Emulation.clearGeolocationOverride");
20
+ }
@@ -0,0 +1,6 @@
1
+ import ApplicationError from "../../error-handling/error/application-error.js";
2
+ /** `sk onboarding --browse` has neither a `--domain` nor a customer config to take a domain from. */
3
+ export declare class BrowseDomainError extends ApplicationError {
4
+ readonly code = "BROWSE_DOMAIN_ERROR";
5
+ constructor();
6
+ }
@@ -0,0 +1,11 @@
1
+ import ApplicationError from "../../error-handling/error/application-error.js";
2
+ /** `sk onboarding --browse` has neither a `--domain` nor a customer config to take a domain from. */
3
+ export class BrowseDomainError extends ApplicationError {
4
+ code = "BROWSE_DOMAIN_ERROR";
5
+ constructor() {
6
+ super("Browse mode needs a domain to open.", [
7
+ "pass one with -d/--domain, e.g. `sk onboarding --browse -d www.example.com`",
8
+ "or pass a CUSTOMERPATH whose config_customer.json names a domain",
9
+ ]);
10
+ }
11
+ }
@@ -0,0 +1,6 @@
1
+ import ApplicationError from "../../error-handling/error/application-error.js";
2
+ /** `sk onboarding` without `--browse` runs a customer config, so it needs its directory. */
3
+ export declare class MissingCustomerPathError extends ApplicationError {
4
+ readonly code = "MISSING_CUSTOMER_PATH";
5
+ constructor();
6
+ }
@@ -0,0 +1,11 @@
1
+ import ApplicationError from "../../error-handling/error/application-error.js";
2
+ /** `sk onboarding` without `--browse` runs a customer config, so it needs its directory. */
3
+ export class MissingCustomerPathError extends ApplicationError {
4
+ code = "MISSING_CUSTOMER_PATH";
5
+ constructor() {
6
+ super("Missing CUSTOMERPATH: onboarding needs a customer config.", [
7
+ "pass the customer directory, e.g. `sk onboarding customers/example-shop`",
8
+ "or add --browse to open the live site without a config",
9
+ ]);
10
+ }
11
+ }
@@ -13,12 +13,14 @@ declare const flags: {
13
13
  verboseLevel: Interfaces.BooleanFlag<boolean>;
14
14
  ignoreContentSecurityPolicy: Interfaces.BooleanFlag<boolean>;
15
15
  headless: Interfaces.BooleanFlag<boolean>;
16
+ browse: Interfaces.BooleanFlag<boolean>;
16
17
  browserFetch: Interfaces.BooleanFlag<boolean>;
17
18
  useTestConfig: Interfaces.BooleanFlag<boolean>;
18
19
  configName: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
19
20
  };
20
21
  export interface OnboardingServiceBuildContextValues extends CommandContextValues {
21
- readonly customerPath: string;
22
+ /** Missing only in browse mode. */
23
+ readonly customerPath?: string;
22
24
  readonly configName: string;
23
25
  readonly domain?: string;
24
26
  readonly local: boolean;
@@ -31,6 +33,7 @@ export interface OnboardingServiceBuildContextValues extends CommandContextValue
31
33
  readonly headless: boolean;
32
34
  readonly debugPort: number;
33
35
  readonly browserFetch: boolean;
36
+ readonly browse: boolean;
34
37
  }
35
38
  declare const OnboardingServiceBuildContext_base: new (values: OnboardingServiceBuildContextValues) => Readonly<OnboardingServiceBuildContextValues>;
36
39
  /** What `sk onboarding` starts: a local Speed Kit dev environment for one customer. */
@@ -48,6 +51,7 @@ export declare class OnboardingServiceBuildContext extends OnboardingServiceBuil
48
51
  verboseLevel: Interfaces.BooleanFlag<boolean>;
49
52
  ignoreContentSecurityPolicy: Interfaces.BooleanFlag<boolean>;
50
53
  headless: Interfaces.BooleanFlag<boolean>;
54
+ browse: Interfaces.BooleanFlag<boolean>;
51
55
  browserFetch: Interfaces.BooleanFlag<boolean>;
52
56
  useTestConfig: Interfaces.BooleanFlag<boolean>;
53
57
  configName: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
@@ -1,7 +1,14 @@
1
- import { Flags } from "@oclif/core";
2
- import { CLI_CONFIG_IS_TEST, CLI_CONFIG_NAME, CLI_CUSTOMER_CONFIG, } from "../integration-api/customer-config-flags.js";
1
+ import { Args, Flags } from "@oclif/core";
2
+ import { CLI_CONFIG_IS_TEST, CLI_CONFIG_NAME, } from "../integration-api/customer-config-flags.js";
3
3
  import { Context } from "../../models/context.js";
4
- const args = { ...CLI_CUSTOMER_CONFIG };
4
+ const args = {
5
+ // Optional for `--browse`, which analyses a site before its config exists; the factory requires it otherwise.
6
+ customerPath: Args.directory({
7
+ name: "customerPath",
8
+ required: false,
9
+ description: "The customer config path (optional with --browse)",
10
+ }),
11
+ };
5
12
  const flags = {
6
13
  ...CLI_CONFIG_NAME,
7
14
  ...CLI_CONFIG_IS_TEST,
@@ -37,6 +44,10 @@ const flags = {
37
44
  default: false,
38
45
  description: "Run Chrome in headless mode (for CI environments). Implicitly enables remote debugging port.",
39
46
  }),
47
+ browse: Flags.boolean({
48
+ default: false,
49
+ description: "Open the live site in the onboarding Chrome, with its extensions, but without request interception or Speed Kit injection. Takes the domain from -d or from the customer config.",
50
+ }),
40
51
  browserFetch: Flags.boolean({
41
52
  default: false,
42
53
  description: "Fetch every origin document with a second headless Chrome instead of a direct request. Only needed when bot protection refuses the CLI outright — a refusal is otherwise detected and rerouted on its own.",
@@ -61,6 +72,7 @@ export class OnboardingServiceBuildContext extends Context() {
61
72
  headless: parsedFlags.headless,
62
73
  debugPort: parsedFlags.debugPort,
63
74
  browserFetch: parsedFlags.browserFetch,
75
+ browse: parsedFlags.browse,
64
76
  });
65
77
  }
66
78
  }
@@ -1,10 +1,43 @@
1
1
  import { OnboardingServiceBuildContext } from "./onboarding-service-build-context.js";
2
+ import { BrowserContext } from "./onboarding-model.js";
2
3
  import { OnboardingService } from "./onboarding-service.js";
4
+ import { BrowseService } from "./browse/browse-service.js";
5
+ import { BrowserExtensions } from "./browser/extension/extension-validator.js";
3
6
  export declare class OnboardingServiceFactory {
4
7
  private readonly context;
5
8
  constructor(context: OnboardingServiceBuildContext);
6
- getService(): Promise<OnboardingService>;
9
+ /**
10
+ * The service `sk onboarding` runs: the browse service under `--browse`, the onboarding service
11
+ * otherwise.
12
+ *
13
+ * @throws {MissingCustomerPathError} Without `--browse` and without a CUSTOMERPATH.
14
+ */
15
+ getService(): Promise<OnboardingService | BrowseService>;
16
+ /** The live site in the onboarding Chrome: the same launch, none of the interception. */
17
+ private getBrowseService;
18
+ /** Only config_customer.json: browse mode reads its domain and chromeFlags, nothing else. */
19
+ private readCustomerConfig;
20
+ /**
21
+ * Resolves the extensions and the Chrome executable, then builds the launch. Onboarding and
22
+ * browse mode both come through here, so they launch the same Chrome.
23
+ */
24
+ protected prepareBrowser(domain: string, customerChromeFlags?: string[]): Promise<{
25
+ browserContext: BrowserContext;
26
+ browserExtensions: BrowserExtensions;
27
+ }>;
28
+ /** The launch for `domain`. It reads no mode of the build context, so browse mode cannot drift from onboarding. */
29
+ buildBrowserContext({ domain, browserVersionString, customerChromeFlags, }: {
30
+ domain: string;
31
+ browserVersionString: string;
32
+ customerChromeFlags?: string[];
33
+ }): BrowserContext;
34
+ private getOnboardingService;
7
35
  private prepareMessagehandler;
36
+ /**
37
+ * The app token for the devtools extension, or `null` when nobody is logged
38
+ * in — onboarding shows what it can without one rather than stopping for a
39
+ * login it does not strictly need.
40
+ */
8
41
  private getAppToken;
9
42
  private buildInstallResource;
10
43
  private getAthenaClient;
@@ -23,10 +23,13 @@ import { SpeedKitServiceWorkerJs } from "./fetch-events/speed-kit-service-worker
23
23
  import { FileWatcher } from "./file-events/file-watcher.js";
24
24
  import { BrowserContext, } from "./onboarding-model.js";
25
25
  import { OnboardingService } from "./onboarding-service.js";
26
+ import { BrowseService } from "./browse/browse-service.js";
27
+ import { resolveBrowseUrl } from "./browse/browse-url.js";
28
+ import { MissingCustomerPathError } from "./error/missing-customer-path-error.js";
26
29
  import { Cache } from "./request-cache/cache.js";
27
30
  import { InstallSpeedKitHtmlTemplate } from "./templates/install-speed-kit-html-template.js";
28
31
  import { InstallSpeedKitJsTemplate } from "./templates/install-speed-kit-js-template.js";
29
- import { ExtensionValidator } from "./browser/extension/extension-validator.js";
32
+ import { ExtensionValidator, } from "./browser/extension/extension-validator.js";
30
33
  import { DevtoolsExtensionApi } from "./browser/extension/devtools-extension-api.js";
31
34
  import { ExtensionBridge } from "./browser/extension/extension-bridge.js";
32
35
  import { TodoCollector } from "./todo-collector.js";
@@ -34,7 +37,6 @@ import { installBrowserOriginFetch } from "../browser-fetch/index.js";
34
37
  import { Crawler } from "./virtual-orestes-app/crawler.js";
35
38
  import { VirtualOrestesApp } from "./virtual-orestes-app/index.js";
36
39
  import { ConfigApiContext, ConfigApiServiceFactory, } from "../config-api/index.js";
37
- import { account } from "baqend/cli";
38
40
  import * as fs from "node:fs";
39
41
  import * as path from "node:path";
40
42
  import { StaticRecipe } from "../integration-api/virtual/static-recipe.js";
@@ -50,7 +52,81 @@ export class OnboardingServiceFactory {
50
52
  constructor(context) {
51
53
  this.context = context;
52
54
  }
55
+ /**
56
+ * The service `sk onboarding` runs: the browse service under `--browse`, the onboarding service
57
+ * otherwise.
58
+ *
59
+ * @throws {MissingCustomerPathError} Without `--browse` and without a CUSTOMERPATH.
60
+ */
53
61
  async getService() {
62
+ if (this.context.browse) {
63
+ return this.getBrowseService();
64
+ }
65
+ if (!this.context.customerPath) {
66
+ throw new MissingCustomerPathError();
67
+ }
68
+ return this.getOnboardingService();
69
+ }
70
+ /** The live site in the onboarding Chrome: the same launch, none of the interception. */
71
+ async getBrowseService() {
72
+ const cli = this.context.environment.cli;
73
+ const customerConfig = this.context.customerPath
74
+ ? await this.readCustomerConfig()
75
+ : undefined;
76
+ applyOriginProxy(customerConfig?.chromeFlags, cli);
77
+ const url = resolveBrowseUrl(this.context.domain, customerConfig);
78
+ const { browserContext } = await this.prepareBrowser(url, customerConfig?.chromeFlags);
79
+ return new BrowseService(browserContext, cli);
80
+ }
81
+ /** Only config_customer.json: browse mode reads its domain and chromeFlags, nothing else. */
82
+ async readCustomerConfig() {
83
+ const integrationApi = new IntegrationApiFactory(new IntegrationApiServiceBuildContext({
84
+ environment: this.context.environment,
85
+ basePath: this.context.customerPath,
86
+ configName: this.context.configName,
87
+ whitelist: [INTEGRATION_FILES.CONFIG.CUSTOMER],
88
+ blockedFolders: [],
89
+ useTestConfig: this.context.useTestConfig,
90
+ })).buildService();
91
+ const files = await integrationApi.run();
92
+ return files.getCustomerConfig().config;
93
+ }
94
+ /**
95
+ * Resolves the extensions and the Chrome executable, then builds the launch. Onboarding and
96
+ * browse mode both come through here, so they launch the same Chrome.
97
+ */
98
+ async prepareBrowser(domain, customerChromeFlags) {
99
+ const cli = this.context.environment.cli;
100
+ const extensionDownloader = new ExtensionDownloader(this.context.environment, cli);
101
+ const browserExtensions = await new ExtensionValidator(extensionDownloader, this.context.environment, cli).getValidBrowserExtensions();
102
+ this.context.environment.settings.chromeExtensionPaths =
103
+ browserExtensions.paths.join(",");
104
+ const browserValidator = new ExecutableValidator(this.context.environment, cli);
105
+ const browserVersionString = await browserValidator.validateOrInstall();
106
+ const browserContext = this.buildBrowserContext({
107
+ domain,
108
+ browserVersionString,
109
+ customerChromeFlags,
110
+ });
111
+ for (const duplicate of browserContext.findDuplicateFlags()) {
112
+ cli.writeWarning(formatChromeFlagDuplicate(duplicate));
113
+ }
114
+ for (const finding of browserContext.findFeatureFindings()) {
115
+ cli.writeWarning(formatChromeFeatureFinding(finding));
116
+ }
117
+ if (this.context.debuggingPort || this.context.headless) {
118
+ cli.writeWarning(`Chrome remote debugging enabled on port ${this.context.debugPort}.`);
119
+ }
120
+ if (this.context.headless) {
121
+ cli.writeWarning("Running in headless mode.");
122
+ }
123
+ return { browserContext, browserExtensions };
124
+ }
125
+ /** The launch for `domain`. It reads no mode of the build context, so browse mode cannot drift from onboarding. */
126
+ buildBrowserContext({ domain, browserVersionString, customerChromeFlags, }) {
127
+ return new BrowserContext(domain, browserVersionString, withOriginProxyFlag(customerChromeFlags), this.context.environment, undefined, this.context.debuggingPort, this.context.headless, this.context.debugPort);
128
+ }
129
+ async getOnboardingService() {
54
130
  const cli = this.context.environment.cli;
55
131
  const files = await this.getIntegrationFiles(cli);
56
132
  const customerConfig = files.getCustomerConfig().config;
@@ -78,25 +154,7 @@ export class OnboardingServiceFactory {
78
154
  const skTemplate = new InstallSpeedKitJsTemplate();
79
155
  const speedKitInstallContext = new SpeedKitInstallContext(customerConfig, this.context.configName, files, bundler, skTemplate, this.context.local);
80
156
  await this.buildInstallResource(speedKitInstallContext, files, cli);
81
- const extensionDownloader = new ExtensionDownloader(this.context.environment, cli);
82
- const browserExtensions = await new ExtensionValidator(extensionDownloader, this.context.environment, cli).getValidBrowserExtensions();
83
- this.context.environment.settings.chromeExtensionPaths =
84
- browserExtensions.paths.join(",");
85
- const browserValidator = new ExecutableValidator(this.context.environment, cli);
86
- const browserVersionString = await browserValidator.validateOrInstall();
87
- const browserContext = new BrowserContext(domainToStart, browserVersionString, withOriginProxyFlag(customerConfig.chromeFlags), this.context.environment, undefined, this.context.debuggingPort, this.context.headless, this.context.debugPort);
88
- for (const duplicate of browserContext.findDuplicateFlags()) {
89
- cli.writeWarning(formatChromeFlagDuplicate(duplicate));
90
- }
91
- for (const finding of browserContext.findFeatureFindings()) {
92
- cli.writeWarning(formatChromeFeatureFinding(finding));
93
- }
94
- if (this.context.debuggingPort || this.context.headless) {
95
- cli.writeWarning(`Chrome remote debugging enabled on port ${this.context.debugPort}.`);
96
- }
97
- if (this.context.headless) {
98
- cli.writeWarning("Running in headless mode.");
99
- }
157
+ const { browserContext, browserExtensions } = await this.prepareBrowser(domainToStart, customerConfig.chromeFlags);
100
158
  const fileWatcher = new FileWatcher(cli, customerConfig, files, documentHandler, browserContext, cache);
101
159
  const athenaClient = (await this.getAthenaClient(customerConfig.app)) || null;
102
160
  const messageHandler = await this.prepareMessagehandler(athenaClient, files, customerConfig);
@@ -133,9 +191,13 @@ export class OnboardingServiceFactory {
133
191
  }
134
192
  return messageHandler;
135
193
  }
194
+ /**
195
+ * The app token for the devtools extension, or `null` when nobody is logged
196
+ * in — onboarding shows what it can without one rather than stopping for a
197
+ * login it does not strictly need.
198
+ */
136
199
  async getAppToken(app) {
137
- // skipInput=true -> fail instead of prompting when no valid login is cached
138
- const result = await safe(account.login({ app, skipInput: true }));
200
+ const result = await this.context.environment.entityManagerProvider.tryGetEntityManager(app);
139
201
  if (result.success !== true) {
140
202
  return null;
141
203
  }
@@ -153,6 +215,8 @@ export class OnboardingServiceFactory {
153
215
  cli.failAction("ONBOARDING:BUILD:INSTALL_RESOURCE", result.error);
154
216
  }
155
217
  async getAthenaClient(app) {
218
+ // no promptForLogin: onboarding shows the Athena data when a login is
219
+ // there and carries on without one, so it must not stop at a prompt
156
220
  const athenaFactory = new AthenaServiceFactory(new AthenaServiceBuildContext({
157
221
  environment: this.context.environment,
158
222
  app,
@@ -164,7 +228,7 @@ export class OnboardingServiceFactory {
164
228
  }
165
229
  getConfigApi(app) {
166
230
  const configApiContext = new ConfigApiContext(app);
167
- return new ConfigApiServiceFactory(configApiContext).getService();
231
+ return new ConfigApiServiceFactory(configApiContext, this.context.environment.entityManagerProvider).getService();
168
232
  }
169
233
  async getFetchEventHandler(files, customerConfig, documentHandler, cache, cli, browserContext, athenaClient, messageHandler, fileWatcher, speedKitServiceWorkerJs) {
170
234
  const configApi = this.getConfigApi(customerConfig.app);
@@ -69,3 +69,5 @@ export declare class OnboardingService {
69
69
  private registerReinstallSpeedKitOnNavigate;
70
70
  private navigate;
71
71
  }
72
+ /** Resolves once `signal` aborts. */
73
+ export declare function waitForAbort(signal: AbortSignal): Promise<void>;
@@ -1,7 +1,7 @@
1
- import puppeteerExtra from "puppeteer-extra";
2
1
  import { BROWSER_EVENTS, CDP_SESSION, } from "./onboarding-model.js";
3
2
  import { safe } from "../../helpers/safe.js";
4
3
  import { isEmptyServiceWorkerUrl } from "./empty-service-worker.js";
4
+ import { launchBrowser, resetBrowserState } from "./browser/launch-browser.js";
5
5
  // A SW version activating later than this after the last locally served sw.js
6
6
  // cannot originate from our interception — it is a deployed-build takeover.
7
7
  const LOCAL_SW_SERVE_GRACE_MS = 10_000;
@@ -60,7 +60,7 @@ export class OnboardingService {
60
60
  await safe(this.browserFetch.close());
61
61
  }
62
62
  async prepareBrowser(localController) {
63
- const browser = await puppeteerExtra.default.launch(this.browserContext.getPuppeteerLaunchOptions());
63
+ const browser = await launchBrowser(this.browserContext);
64
64
  for (const target of browser.targets()) {
65
65
  if (target.type() === "browser") {
66
66
  await this.fetchEventHandler.handleRequestFetchEvents(target);
@@ -109,19 +109,7 @@ export class OnboardingService {
109
109
  }
110
110
  async prepareCleanState(page) {
111
111
  const cdpSession = await page.target().createCDPSession();
112
- // cleanUp old serviceWorkers
113
- await cdpSession.send("ServiceWorker.enable");
114
- await cdpSession.send("ServiceWorker.unregister", {
115
- scopeURL: this.browserContext.domain,
116
- });
117
- // clear storage for a clean start
118
- await cdpSession.send("Storage.clearCookies");
119
- await cdpSession.send("Storage.clearDataForOrigin", {
120
- origin: this.browserContext.domain,
121
- storageTypes: "all",
122
- });
123
- await cdpSession.send("Emulation.clearDeviceMetricsOverride");
124
- await cdpSession.send("Emulation.clearGeolocationOverride");
112
+ await resetBrowserState(cdpSession, this.browserContext.domain);
125
113
  this.registerForeignSwGuard(page, cdpSession);
126
114
  safe(() => {
127
115
  cdpSession.on(CDP_SESSION.EVENTS.SERVICE_WORKER.REGISTRATION_UPDATED, ({ registrations, }) => {
@@ -226,7 +214,8 @@ export class OnboardingService {
226
214
  await safe(page.goto(goto.toString()));
227
215
  }
228
216
  }
229
- function waitForAbort(signal) {
217
+ /** Resolves once `signal` aborts. */
218
+ export function waitForAbort(signal) {
230
219
  if (signal.aborted) {
231
220
  return Promise.resolve();
232
221
  }
@@ -0,0 +1,28 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { resolveBrowseUrl } from "../../browse/browse-url.js";
4
+ import { BrowseDomainError } from "../../error/browse-domain-error.js";
5
+ describe("resolveBrowseUrl", () => {
6
+ const customerConfig = {
7
+ domain: "www.example.com",
8
+ origins: ["https://www.example.com", "https://shop.example.com"],
9
+ };
10
+ it("takes the --domain flag over the customer config", () => {
11
+ expect(resolveBrowseUrl("other.example.com", customerConfig)).to.equal("https://other.example.com/");
12
+ });
13
+ it("takes the domain of the customer config without a flag", () => {
14
+ expect(resolveBrowseUrl(undefined, customerConfig)).to.equal("https://www.example.com/");
15
+ });
16
+ it("falls back to the first origin when the config names no domain", () => {
17
+ expect(resolveBrowseUrl(undefined, {
18
+ domain: undefined,
19
+ origins: ["https://shop.example.com"],
20
+ })).to.equal("https://shop.example.com/");
21
+ });
22
+ it("keeps an explicit scheme and path", () => {
23
+ expect(resolveBrowseUrl("http://example.com/de/")).to.equal("http://example.com/de/");
24
+ });
25
+ it("fails without a flag and without a customer config", () => {
26
+ expect(() => resolveBrowseUrl()).to.throw(BrowseDomainError);
27
+ });
28
+ });