@speedkit/cli 4.43.0-beta.1 → 4.43.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.
- package/CHANGELOG.md +2 -2
- package/README.md +12 -2
- package/dist/commands/onboarding.d.ts +3 -0
- package/dist/commands/onboarding.js +7 -1
- package/dist/services/bundler/bundle-service-factory.d.ts +1 -6
- package/dist/services/bundler/bundle-service-factory.js +2 -9
- package/dist/services/bundler/bundle-service.d.ts +1 -10
- package/dist/services/bundler/bundle-service.js +1 -16
- package/dist/services/bundler/index.d.ts +0 -1
- package/dist/services/bundler/index.js +0 -1
- package/dist/services/cli/cli-environment.js +0 -1
- package/dist/services/cli/cli-settings.d.ts +0 -5
- package/dist/services/cli/cli-settings.js +0 -1
- package/dist/services/deploy/checks/activation/customer-approval-check.js +1 -1
- package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +3 -2
- package/dist/services/deploy/checks/pre-deploy/split-change-check.js +3 -2
- package/dist/services/deploy/deploy-service-factory.js +1 -1
- package/dist/services/deploy/mr/merge-request-deploy-service-factory.js +1 -1
- package/dist/services/document-handler-runtime/document-handler-runtime-model.d.ts +0 -2
- package/dist/services/document-handler-runtime/document-handler-runtime-model.js +0 -2
- package/dist/services/document-handler-runtime/document-handler-server.d.ts +0 -17
- package/dist/services/document-handler-runtime/document-handler-server.js +20 -82
- package/dist/services/document-handler-runtime/factory/document-handler-runtime-service-factory.js +1 -1
- package/dist/services/integration-api/integration-api-factory.js +1 -1
- package/dist/services/integration-api/integration-api-model.d.ts +1 -2
- package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +8 -70
- package/dist/services/integration-api/struct/customer-config.d.ts +1 -19
- package/dist/services/integration-api/struct/customer-config.js +3 -11
- package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +4 -9
- package/dist/services/integration-api/struct/speed-kit-config-file.js +10 -30
- package/dist/services/onboarding/config-renderer/speed-kit-install-context.js +8 -24
- package/dist/services/onboarding/error/inject-installation-error.d.ts +6 -0
- package/dist/services/onboarding/error/inject-installation-error.js +11 -0
- package/dist/services/onboarding/error/inject-target-error.d.ts +6 -0
- package/dist/services/onboarding/error/inject-target-error.js +11 -0
- package/dist/services/onboarding/error/missing-customer-path-error.d.ts +1 -1
- package/dist/services/onboarding/error/missing-customer-path-error.js +2 -1
- package/dist/services/onboarding/fetch-events/customer-domain-document-response.d.ts +4 -14
- package/dist/services/onboarding/fetch-events/customer-domain-document-response.js +6 -45
- package/dist/services/onboarding/fetch-events/document-rewrite.d.ts +19 -0
- package/dist/services/onboarding/fetch-events/document-rewrite.js +47 -0
- package/dist/services/onboarding/fetch-events/speed-kit-install-html.d.ts +5 -1
- package/dist/services/onboarding/fetch-events/speed-kit-install-html.js +7 -2
- package/dist/services/onboarding/html-rewrites.d.ts +3 -3
- package/dist/services/onboarding/html-rewrites.js +3 -3
- package/dist/services/onboarding/index.d.ts +1 -1
- package/dist/services/onboarding/index.js +1 -1
- package/dist/services/onboarding/inject/content-security-policy.d.ts +14 -0
- package/dist/services/onboarding/inject/content-security-policy.js +54 -0
- package/dist/services/onboarding/inject/deployed-installation.d.ts +15 -0
- package/dist/services/onboarding/inject/deployed-installation.js +32 -0
- package/dist/services/onboarding/inject/foreign-install-request.d.ts +27 -0
- package/dist/services/onboarding/inject/foreign-install-request.js +50 -0
- package/dist/services/onboarding/inject/inject-document-rewrite.d.ts +23 -0
- package/dist/services/onboarding/inject/inject-document-rewrite.js +52 -0
- package/dist/services/onboarding/inject/inject-service.d.ts +32 -0
- package/dist/services/onboarding/inject/inject-service.js +72 -0
- package/dist/services/onboarding/inject/injected-installation.d.ts +29 -0
- package/dist/services/onboarding/inject/injected-installation.js +37 -0
- package/dist/services/onboarding/onboarding-service-build-context.d.ts +15 -1
- package/dist/services/onboarding/onboarding-service-build-context.js +31 -2
- package/dist/services/onboarding/onboarding-service-factory.d.ts +23 -5
- package/dist/services/onboarding/onboarding-service-factory.js +87 -7
- package/dist/services/onboarding/spec/inject/content-security-policy.spec.js +47 -0
- package/dist/services/onboarding/spec/inject/deployed-installation.spec.d.ts +1 -0
- package/dist/services/onboarding/spec/inject/deployed-installation.spec.js +49 -0
- package/dist/services/onboarding/spec/inject/foreign-install-request.spec.d.ts +1 -0
- package/dist/services/onboarding/spec/inject/foreign-install-request.spec.js +47 -0
- package/dist/services/onboarding/spec/inject/inject-document-rewrite.spec.d.ts +1 -0
- package/dist/services/onboarding/spec/inject/inject-document-rewrite.spec.js +66 -0
- package/dist/services/onboarding/spec/inject/injected-installation.spec.d.ts +1 -0
- package/dist/services/onboarding/spec/inject/injected-installation.spec.js +24 -0
- package/dist/services/onboarding/spec/onboarding-service-build-context.spec.d.ts +1 -0
- package/dist/services/onboarding/spec/onboarding-service-build-context.spec.js +22 -0
- package/dist/services/onboarding/spec/onboarding-service-factory.spec.js +53 -2
- package/dist/services/onboarding/templates/install-speed-kit-html-template.js +3 -0
- package/dist/services/onboarding/templates/install-speed-kit-js-template.js +0 -5
- package/dist/services/query-builder/query-builder-factory.js +3 -3
- package/oclif.manifest.json +26 -3
- package/package.json +1 -2
- package/dist/services/bundler/error/packjockel-error.d.ts +0 -3
- package/dist/services/bundler/error/packjockel-error.js +0 -3
- package/dist/services/bundler/packjockel-service.d.ts +0 -70
- package/dist/services/bundler/packjockel-service.js +0 -171
- package/dist/services/bundler/spec/packjockel-service.spec.js +0 -149
- /package/dist/services/{bundler/spec/packjockel-service.spec.d.ts → onboarding/spec/inject/content-security-policy.spec.d.ts} +0 -0
|
@@ -6,6 +6,7 @@ export class SpeedKitInstallHtml {
|
|
|
6
6
|
customerConfig;
|
|
7
7
|
browserContext;
|
|
8
8
|
speedKitInstallHtmlTemplate;
|
|
9
|
+
injectedInstallPath;
|
|
9
10
|
get patterns() {
|
|
10
11
|
const result = [];
|
|
11
12
|
for (const origin of this.customerConfig.origins) {
|
|
@@ -30,14 +31,18 @@ export class SpeedKitInstallHtml {
|
|
|
30
31
|
}
|
|
31
32
|
return result;
|
|
32
33
|
}
|
|
33
|
-
constructor(customerConfig, browserContext, speedKitInstallHtmlTemplate
|
|
34
|
+
constructor(customerConfig, browserContext, speedKitInstallHtmlTemplate,
|
|
35
|
+
/** The install resource of `--inject`. It loads as the snippet of a customer does, with CORS. */
|
|
36
|
+
injectedInstallPath) {
|
|
34
37
|
this.customerConfig = customerConfig;
|
|
35
38
|
this.browserContext = browserContext;
|
|
36
39
|
this.speedKitInstallHtmlTemplate = speedKitInstallHtmlTemplate;
|
|
40
|
+
this.injectedInstallPath = injectedInstallPath;
|
|
37
41
|
}
|
|
38
42
|
async handle() {
|
|
39
43
|
return new BaqendResponse(this.speedKitInstallHtmlTemplate.render({
|
|
40
|
-
installPath: this.customerConfig.installPath,
|
|
44
|
+
installPath: this.injectedInstallPath ?? this.customerConfig.installPath,
|
|
45
|
+
crossOrigin: Boolean(this.injectedInstallPath),
|
|
41
46
|
domain: this.browserContext.domain,
|
|
42
47
|
swPath: this.customerConfig.swPath,
|
|
43
48
|
}));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InstallResource } from "./inject/injected-installation.js";
|
|
2
2
|
/**
|
|
3
3
|
* Injects the Speed Kit install script into the given HTML, after removing any
|
|
4
4
|
* existing install snippet. The origin's script nonce is reused so a
|
|
@@ -6,10 +6,10 @@ import { CustomerConfig } from "../integration-api/index.js";
|
|
|
6
6
|
* opening `<head>`, falling back to before the closing `</head>`.
|
|
7
7
|
*
|
|
8
8
|
* @param {string} text - the HTML to rewrite
|
|
9
|
-
* @param {
|
|
9
|
+
* @param {InstallResource} install - provides the install path/params
|
|
10
10
|
* @returns {string} - the HTML with the install script injected
|
|
11
11
|
*/
|
|
12
|
-
export declare function rewriteInstallResource(text: string,
|
|
12
|
+
export declare function rewriteInstallResource(text: string, install: InstallResource): string;
|
|
13
13
|
/**
|
|
14
14
|
* Removes the content security meta tag from the provided HTML content.
|
|
15
15
|
*
|
|
@@ -6,15 +6,15 @@ import { SpeedKitInstallRegex } from "./onboarding-model.js";
|
|
|
6
6
|
* opening `<head>`, falling back to before the closing `</head>`.
|
|
7
7
|
*
|
|
8
8
|
* @param {string} text - the HTML to rewrite
|
|
9
|
-
* @param {
|
|
9
|
+
* @param {InstallResource} install - provides the install path/params
|
|
10
10
|
* @returns {string} - the HTML with the install script injected
|
|
11
11
|
*/
|
|
12
|
-
export function rewriteInstallResource(text,
|
|
12
|
+
export function rewriteInstallResource(text, install) {
|
|
13
13
|
text = text.replace(SpeedKitInstallRegex, "");
|
|
14
14
|
const matchOpeningHead = /<\s*head\b[^>]*>/;
|
|
15
15
|
const matchClosingHead = /<\/\s*head\s*>/;
|
|
16
16
|
const nonceMatch = text.match(/<script\s(?:[^>]*?\s)?nonce\s*=\s*["']?([^\s"'>]*)/i);
|
|
17
|
-
const installScript = `<script ${nonceMatch ? 'nonce="' + nonceMatch[1] + '"' : ""} src="${
|
|
17
|
+
const installScript = `<script ${nonceMatch ? 'nonce="' + nonceMatch[1] + '"' : ""} src="${install.installPath}" ${install.installParams}></script>`;
|
|
18
18
|
if (matchOpeningHead.test(text)) {
|
|
19
19
|
return text.replace(matchOpeningHead, (head) => `${head}\n${installScript}`);
|
|
20
20
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { OnboardingServiceBuildContext } from "./onboarding-service-build-context.js";
|
|
1
|
+
export { OnboardingServiceBuildContext, withInjectDefault, } from "./onboarding-service-build-context.js";
|
|
2
2
|
export * from "./onboarding-service.js";
|
|
3
3
|
export * from "./onboarding-service-factory.js";
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { OnboardingServiceBuildContext } from "./onboarding-service-build-context.js";
|
|
1
|
+
export { OnboardingServiceBuildContext, withInjectDefault, } from "./onboarding-service-build-context.js";
|
|
2
2
|
export * from "./onboarding-service.js";
|
|
3
3
|
export * from "./onboarding-service-factory.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface AmendedPolicy {
|
|
2
|
+
readonly policy: string;
|
|
3
|
+
/** The directives that gained `source`. Empty when the policy allowed it already. */
|
|
4
|
+
readonly amended: string[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Allows `source` to load scripts and to be fetched under `policy`.
|
|
8
|
+
*
|
|
9
|
+
* A directive that is absent falls back to `default-src`, so it is created from `default-src`
|
|
10
|
+
* first. Without `default-src` an absent directive restricts nothing and stays absent.
|
|
11
|
+
*/
|
|
12
|
+
export declare function allowSource(policy: string, source: string): AmendedPolicy;
|
|
13
|
+
/** Applies {@link allowSource} to every policy that the HTML sets in a meta tag. */
|
|
14
|
+
export declare function allowSourceInMetaTags(html: string, source: string, onAmended: (amended: string[]) => void): string;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** The directives that fall back to `default-src` and that a script from another host needs. */
|
|
2
|
+
const FALLBACK_DIRECTIVES = ["script-src", "connect-src"];
|
|
3
|
+
const CSP_META = /(<meta[^>]*http-equiv=["']content-security-policy["'][^>]*content=")([^"]*)(")/gis;
|
|
4
|
+
/**
|
|
5
|
+
* Allows `source` to load scripts and to be fetched under `policy`.
|
|
6
|
+
*
|
|
7
|
+
* A directive that is absent falls back to `default-src`, so it is created from `default-src`
|
|
8
|
+
* first. Without `default-src` an absent directive restricts nothing and stays absent.
|
|
9
|
+
*/
|
|
10
|
+
export function allowSource(policy, source) {
|
|
11
|
+
const directives = policy
|
|
12
|
+
.split(";")
|
|
13
|
+
.map((directive) => directive.trim())
|
|
14
|
+
.filter(Boolean)
|
|
15
|
+
.map((directive) => directive.split(/\s+/));
|
|
16
|
+
const find = (name) => directives.find(([directive]) => directive.toLowerCase() === name);
|
|
17
|
+
const amended = [];
|
|
18
|
+
const allow = (directive) => {
|
|
19
|
+
const sources = directive.slice(1).filter((entry) => entry !== "'none'");
|
|
20
|
+
if (sources.includes(source)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
directive.splice(1, Infinity, ...sources, source);
|
|
24
|
+
amended.push(directive[0]);
|
|
25
|
+
};
|
|
26
|
+
const fallback = find("default-src");
|
|
27
|
+
for (const name of FALLBACK_DIRECTIVES) {
|
|
28
|
+
let directive = find(name);
|
|
29
|
+
if (!directive && fallback) {
|
|
30
|
+
directive = [name, ...fallback.slice(1)];
|
|
31
|
+
directives.push(directive);
|
|
32
|
+
}
|
|
33
|
+
if (directive) {
|
|
34
|
+
allow(directive);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// It overrides script-src for a script element, so it needs the source too.
|
|
38
|
+
const scriptElement = find("script-src-elem");
|
|
39
|
+
if (scriptElement) {
|
|
40
|
+
allow(scriptElement);
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
policy: directives.map((directive) => directive.join(" ")).join("; "),
|
|
44
|
+
amended,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Applies {@link allowSource} to every policy that the HTML sets in a meta tag. */
|
|
48
|
+
export function allowSourceInMetaTags(html, source, onAmended) {
|
|
49
|
+
return html.replaceAll(CSP_META, (_, open, policy, close) => {
|
|
50
|
+
const result = allowSource(policy, source);
|
|
51
|
+
onAmended(result.amended);
|
|
52
|
+
return `${open}${result.policy}${close}`;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** What inject mode reads from the install resource of an installation, not from a local config. */
|
|
2
|
+
export interface DeployedInstallation {
|
|
3
|
+
/** Where the installation registers its service worker, e.g. `/wrapper-sw.js`. */
|
|
4
|
+
readonly swPath: string;
|
|
5
|
+
/** The scope of that service worker. `/` when the installation names none. */
|
|
6
|
+
readonly scope: string;
|
|
7
|
+
}
|
|
8
|
+
/** Reads the service worker path and the scope out of the config in an install resource. */
|
|
9
|
+
export declare function parseInstallResource(installJs: string, origin: string): DeployedInstallation | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Fetches the install resource as a page of `origin` does, and reads the installation from it.
|
|
12
|
+
*
|
|
13
|
+
* @throws {InjectInstallationError} When the server answers with no install resource for the origin.
|
|
14
|
+
*/
|
|
15
|
+
export declare function readDeployedInstallation(installPath: string, origin: string, fetchResource?: typeof fetch): Promise<DeployedInstallation>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { InjectInstallationError } from "../error/inject-installation-error.js";
|
|
2
|
+
/** Reads the service worker path and the scope out of the config in an install resource. */
|
|
3
|
+
export function parseInstallResource(installJs, origin) {
|
|
4
|
+
const sw = /\bsw:\s*"([^"]+)"/.exec(installJs)?.[1];
|
|
5
|
+
if (!sw) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
const scope = /\bscope:\s*"([^"]+)"/.exec(installJs)?.[1];
|
|
9
|
+
return { swPath: new URL(sw, origin).pathname, scope: scope ?? "/" };
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetches the install resource as a page of `origin` does, and reads the installation from it.
|
|
13
|
+
*
|
|
14
|
+
* @throws {InjectInstallationError} When the server answers with no install resource for the origin.
|
|
15
|
+
*/
|
|
16
|
+
export async function readDeployedInstallation(installPath, origin, fetchResource = fetch) {
|
|
17
|
+
const installUrl = new URL(installPath, origin).href;
|
|
18
|
+
const response = await fetchResource(installUrl, {
|
|
19
|
+
headers: { origin },
|
|
20
|
+
}).catch((error) => {
|
|
21
|
+
throw new InjectInstallationError(installUrl, String(error));
|
|
22
|
+
});
|
|
23
|
+
// The server answers 204 when the origin has no live installation.
|
|
24
|
+
if (response.status !== 200) {
|
|
25
|
+
throw new InjectInstallationError(installUrl, `the server answered ${response.status}`);
|
|
26
|
+
}
|
|
27
|
+
const installation = parseInstallResource(await response.text(), origin);
|
|
28
|
+
if (!installation) {
|
|
29
|
+
throw new InjectInstallationError(installUrl, "the install resource names no service worker");
|
|
30
|
+
}
|
|
31
|
+
return installation;
|
|
32
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Protocol } from "devtools-protocol";
|
|
2
|
+
import { CliServiceInterface } from "../../cli/index.js";
|
|
3
|
+
import { AbortResponse } from "../browser/abort-response.js";
|
|
4
|
+
import { FetchRequestPausedEventHandler } from "../onboarding-model.js";
|
|
5
|
+
/**
|
|
6
|
+
* Keeps every install resource out of the run, except the one that inject mode injects.
|
|
7
|
+
*
|
|
8
|
+
* An origin can carry an integration of its own: another app, or the live installation of the
|
|
9
|
+
* same app while a testing installation is injected. That install resource registers its own
|
|
10
|
+
* service worker on the same scope, which replaces the injected one.
|
|
11
|
+
*/
|
|
12
|
+
export declare class ForeignInstallRequest implements FetchRequestPausedEventHandler {
|
|
13
|
+
/** The domain of the injected app, e.g. `example.app.baqend.com`. */
|
|
14
|
+
private readonly appDomain;
|
|
15
|
+
private readonly cli;
|
|
16
|
+
/** The injected testing installation. Without one, any installation of the app may load. */
|
|
17
|
+
private readonly testingId?;
|
|
18
|
+
readonly patterns: Protocol.Fetch.RequestPattern[];
|
|
19
|
+
private readonly reported;
|
|
20
|
+
constructor(
|
|
21
|
+
/** The domain of the injected app, e.g. `example.app.baqend.com`. */
|
|
22
|
+
appDomain: string, cli: CliServiceInterface,
|
|
23
|
+
/** The injected testing installation. Without one, any installation of the app may load. */
|
|
24
|
+
testingId?: string);
|
|
25
|
+
handle(event: Protocol.Fetch.RequestPausedEvent): Promise<AbortResponse | null>;
|
|
26
|
+
private isInjected;
|
|
27
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AbortResponse } from "../browser/abort-response.js";
|
|
2
|
+
/**
|
|
3
|
+
* Keeps every install resource out of the run, except the one that inject mode injects.
|
|
4
|
+
*
|
|
5
|
+
* An origin can carry an integration of its own: another app, or the live installation of the
|
|
6
|
+
* same app while a testing installation is injected. That install resource registers its own
|
|
7
|
+
* service worker on the same scope, which replaces the injected one.
|
|
8
|
+
*/
|
|
9
|
+
export class ForeignInstallRequest {
|
|
10
|
+
appDomain;
|
|
11
|
+
cli;
|
|
12
|
+
testingId;
|
|
13
|
+
// `FetchEventHandler` reads a pattern a second time as a regular expression, so it must be valid
|
|
14
|
+
// as both. `*.*` is "any host with a dot" for Chrome and "any characters" for the expression.
|
|
15
|
+
patterns = [
|
|
16
|
+
{
|
|
17
|
+
requestStage: "Request",
|
|
18
|
+
urlPattern: "https://*.*app.baqend.com/v1/speedkit/install*",
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
reported = new Set();
|
|
22
|
+
constructor(
|
|
23
|
+
/** The domain of the injected app, e.g. `example.app.baqend.com`. */
|
|
24
|
+
appDomain, cli,
|
|
25
|
+
/** The injected testing installation. Without one, any installation of the app may load. */
|
|
26
|
+
testingId) {
|
|
27
|
+
this.appDomain = appDomain;
|
|
28
|
+
this.cli = cli;
|
|
29
|
+
this.testingId = testingId;
|
|
30
|
+
}
|
|
31
|
+
async handle(event) {
|
|
32
|
+
const url = new URL(event.request.url);
|
|
33
|
+
if (this.isInjected(url)) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
// Once per resource: a page requests it on every navigation.
|
|
37
|
+
const resource = `${url.host}${url.pathname}${url.search}`;
|
|
38
|
+
if (!this.reported.has(resource)) {
|
|
39
|
+
this.reported.add(resource);
|
|
40
|
+
this.cli.writeWarning(`Blocked the install resource ${url.href}: it is not the injected installation.`);
|
|
41
|
+
}
|
|
42
|
+
return new AbortResponse("BlockedByClient");
|
|
43
|
+
}
|
|
44
|
+
isInjected(url) {
|
|
45
|
+
if (url.host !== this.appDomain) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return (!this.testingId || url.searchParams.getAll("r").includes(this.testingId));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Protocol } from "devtools-protocol";
|
|
2
|
+
import { CliServiceInterface } from "../../cli/index.js";
|
|
3
|
+
import { DocumentRewrite } from "../fetch-events/document-rewrite.js";
|
|
4
|
+
import { InjectedInstallation } from "./injected-installation.js";
|
|
5
|
+
/**
|
|
6
|
+
* The rewrite of `--inject`: every document gets the install script of the deployed installation,
|
|
7
|
+
* and nothing of the local config.
|
|
8
|
+
*/
|
|
9
|
+
export declare class InjectDocumentRewrite implements DocumentRewrite {
|
|
10
|
+
private readonly installation;
|
|
11
|
+
/** The origin of the app, e.g. `https://example.app.baqend.com`. */
|
|
12
|
+
private readonly appOrigin;
|
|
13
|
+
private readonly removeContentSecurityPolicy;
|
|
14
|
+
private readonly cli;
|
|
15
|
+
private readonly reported;
|
|
16
|
+
constructor(installation: InjectedInstallation,
|
|
17
|
+
/** The origin of the app, e.g. `https://example.app.baqend.com`. */
|
|
18
|
+
appOrigin: string, removeContentSecurityPolicy: boolean, cli: CliServiceInterface);
|
|
19
|
+
headers(headers: Protocol.Fetch.HeaderEntry[]): Protocol.Fetch.HeaderEntry[];
|
|
20
|
+
html(html: string): string;
|
|
21
|
+
/** Prints each distinct change once, not once per document. */
|
|
22
|
+
private report;
|
|
23
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { removeContentSecurityMetaTag, rewriteInstallResource, } from "../html-rewrites.js";
|
|
2
|
+
import { allowSource, allowSourceInMetaTags, } from "./content-security-policy.js";
|
|
3
|
+
const CSP_HEADER = "content-security-policy";
|
|
4
|
+
/**
|
|
5
|
+
* The rewrite of `--inject`: every document gets the install script of the deployed installation,
|
|
6
|
+
* and nothing of the local config.
|
|
7
|
+
*/
|
|
8
|
+
export class InjectDocumentRewrite {
|
|
9
|
+
installation;
|
|
10
|
+
appOrigin;
|
|
11
|
+
removeContentSecurityPolicy;
|
|
12
|
+
cli;
|
|
13
|
+
reported = new Set();
|
|
14
|
+
constructor(installation,
|
|
15
|
+
/** The origin of the app, e.g. `https://example.app.baqend.com`. */
|
|
16
|
+
appOrigin, removeContentSecurityPolicy, cli) {
|
|
17
|
+
this.installation = installation;
|
|
18
|
+
this.appOrigin = appOrigin;
|
|
19
|
+
this.removeContentSecurityPolicy = removeContentSecurityPolicy;
|
|
20
|
+
this.cli = cli;
|
|
21
|
+
}
|
|
22
|
+
headers(headers) {
|
|
23
|
+
if (this.removeContentSecurityPolicy) {
|
|
24
|
+
return headers.filter((header) => !header.name.toLowerCase().includes(CSP_HEADER));
|
|
25
|
+
}
|
|
26
|
+
return headers.map((header) => {
|
|
27
|
+
// The report-only header blocks nothing, so it stays as the origin sent it.
|
|
28
|
+
if (header.name.toLowerCase() !== CSP_HEADER) {
|
|
29
|
+
return header;
|
|
30
|
+
}
|
|
31
|
+
const { policy, amended } = allowSource(header.value, this.appOrigin);
|
|
32
|
+
this.report(amended);
|
|
33
|
+
return { ...header, value: policy };
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
html(html) {
|
|
37
|
+
const injected = rewriteInstallResource(html, this.installation);
|
|
38
|
+
if (this.removeContentSecurityPolicy) {
|
|
39
|
+
return removeContentSecurityMetaTag(injected);
|
|
40
|
+
}
|
|
41
|
+
return allowSourceInMetaTags(injected, this.appOrigin, (amended) => this.report(amended));
|
|
42
|
+
}
|
|
43
|
+
/** Prints each distinct change once, not once per document. */
|
|
44
|
+
report(amended) {
|
|
45
|
+
const change = amended.join(", ");
|
|
46
|
+
if (!change || this.reported.has(change)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
this.reported.add(change);
|
|
50
|
+
this.cli.writeWarning(`Content Security Policy: added ${this.appOrigin} to ${change}. Pass -s to remove the policy instead.`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CliServiceInterface } from "../../cli/index.js";
|
|
2
|
+
import { FetchEventHandler } from "../fetch-event-handler.js";
|
|
3
|
+
import { BrowserContext } from "../onboarding-model.js";
|
|
4
|
+
import { InjectedInstallation } from "./injected-installation.js";
|
|
5
|
+
export interface InjectServiceDeps {
|
|
6
|
+
readonly browserContext: BrowserContext;
|
|
7
|
+
readonly fetchEventHandler: FetchEventHandler;
|
|
8
|
+
readonly installation: InjectedInstallation;
|
|
9
|
+
/** The `scope` of `config_customer.json`: where the install page is served. */
|
|
10
|
+
readonly scope: string;
|
|
11
|
+
readonly cli: CliServiceInterface;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* `sk onboarding --inject`: the site of a customer with a deployed installation injected, for a
|
|
15
|
+
* customer that has not installed Speed Kit.
|
|
16
|
+
*
|
|
17
|
+
* It launches the same Chrome as onboarding. The install script, the install page and the service
|
|
18
|
+
* worker wrapper come from the CLI. The config, the service worker and the documents come from the
|
|
19
|
+
* deployed installation, so the run needs no local config.
|
|
20
|
+
*/
|
|
21
|
+
export declare class InjectService {
|
|
22
|
+
private readonly browserContext;
|
|
23
|
+
private readonly fetchEventHandler;
|
|
24
|
+
private readonly installation;
|
|
25
|
+
private readonly scope;
|
|
26
|
+
private readonly cli;
|
|
27
|
+
constructor(deps: InjectServiceDeps);
|
|
28
|
+
/** Installs Speed Kit through the install page, then blocks until the caller aborts `externalSignal` or Chrome disconnects. */
|
|
29
|
+
run(externalSignal: AbortSignal): Promise<void>;
|
|
30
|
+
private readyLine;
|
|
31
|
+
private findPage;
|
|
32
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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 --inject`: the site of a customer with a deployed installation injected, for a
|
|
7
|
+
* customer that has not installed Speed Kit.
|
|
8
|
+
*
|
|
9
|
+
* It launches the same Chrome as onboarding. The install script, the install page and the service
|
|
10
|
+
* worker wrapper come from the CLI. The config, the service worker and the documents come from the
|
|
11
|
+
* deployed installation, so the run needs no local config.
|
|
12
|
+
*/
|
|
13
|
+
export class InjectService {
|
|
14
|
+
browserContext;
|
|
15
|
+
fetchEventHandler;
|
|
16
|
+
installation;
|
|
17
|
+
scope;
|
|
18
|
+
cli;
|
|
19
|
+
constructor(deps) {
|
|
20
|
+
this.browserContext = deps.browserContext;
|
|
21
|
+
this.fetchEventHandler = deps.fetchEventHandler;
|
|
22
|
+
this.installation = deps.installation;
|
|
23
|
+
this.scope = deps.scope;
|
|
24
|
+
this.cli = deps.cli;
|
|
25
|
+
}
|
|
26
|
+
/** Installs Speed Kit through the install page, then blocks until the caller aborts `externalSignal` or Chrome disconnects. */
|
|
27
|
+
async run(externalSignal) {
|
|
28
|
+
this.cli.startAction("ONBOARDING:INJECT:BROWSER", `prepare browser ${this.cli.style.bold(this.browserContext.browserVersionString)}`);
|
|
29
|
+
const disconnected = new AbortController();
|
|
30
|
+
const shutdownSignal = AbortSignal.any([
|
|
31
|
+
externalSignal,
|
|
32
|
+
disconnected.signal,
|
|
33
|
+
]);
|
|
34
|
+
const browser = await launchBrowser(this.browserContext);
|
|
35
|
+
for (const target of browser.targets()) {
|
|
36
|
+
if (target.type() === "browser") {
|
|
37
|
+
await this.fetchEventHandler.handleRequestFetchEvents(target);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
browser.on(BROWSER_EVENTS.DISCONNECTED, () => {
|
|
41
|
+
this.cli.writeError("Browser disconnected, shutting down");
|
|
42
|
+
disconnected.abort();
|
|
43
|
+
});
|
|
44
|
+
const page = await this.findPage(browser);
|
|
45
|
+
const reset = await safe(page
|
|
46
|
+
.target()
|
|
47
|
+
.createCDPSession()
|
|
48
|
+
.then((session) => resetBrowserState(session, new URL(this.browserContext.domain).origin)));
|
|
49
|
+
if (reset.success !== true) {
|
|
50
|
+
this.cli.writeWarning(`Could not reset the browser state: ${reset.error}`);
|
|
51
|
+
}
|
|
52
|
+
this.cli.successAction("ONBOARDING:INJECT:BROWSER");
|
|
53
|
+
const url = new URL(this.installation.installPage(this.scope), this.browserContext.domain).href;
|
|
54
|
+
const navigation = await safe(page.goto(url));
|
|
55
|
+
if (navigation.success !== true) {
|
|
56
|
+
this.cli.writeWarning(`Could not open ${url}: ${navigation.error}`);
|
|
57
|
+
}
|
|
58
|
+
this.cli.writeSuccess(this.readyLine());
|
|
59
|
+
await waitForAbort(shutdownSignal);
|
|
60
|
+
await safe(browser.close());
|
|
61
|
+
}
|
|
62
|
+
readyLine() {
|
|
63
|
+
const installation = this.installation.testingId
|
|
64
|
+
? `the testing installation ${this.installation.testingId}`
|
|
65
|
+
: "the latest deployed installation";
|
|
66
|
+
return `Injecting ${installation} into ${this.browserContext.domain}. It uses no local config file. Press Ctrl+C to stop.`;
|
|
67
|
+
}
|
|
68
|
+
async findPage(browser) {
|
|
69
|
+
const [page] = await browser.pages();
|
|
70
|
+
return page ?? browser.newPage();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** The install script of a customer: `installPath` and `installParams` of `config_customer.json`. */
|
|
2
|
+
export interface InstallResource {
|
|
3
|
+
readonly installPath?: string;
|
|
4
|
+
readonly installParams?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The deployed installation that `--inject` puts into the pages of the origin: the latest deployed
|
|
8
|
+
* one, or one testing installation.
|
|
9
|
+
*/
|
|
10
|
+
export declare class InjectedInstallation implements InstallResource {
|
|
11
|
+
private readonly resource;
|
|
12
|
+
/** The id of a testing installation. Without one, the server resolves the installation that is live. */
|
|
13
|
+
readonly testingId?: string;
|
|
14
|
+
readonly installParams: string;
|
|
15
|
+
constructor(resource: InstallResource,
|
|
16
|
+
/** The id of a testing installation. Without one, the server resolves the installation that is live. */
|
|
17
|
+
testingId?: string);
|
|
18
|
+
/**
|
|
19
|
+
* The install resource. A testing installation carries `r=<id>`: the server then answers before
|
|
20
|
+
* a service worker is active, and for an app that has no live installation.
|
|
21
|
+
*/
|
|
22
|
+
get installPath(): string;
|
|
23
|
+
/**
|
|
24
|
+
* The install page. `enableSpeedKit=1` puts the browser into group A of a split test, or no
|
|
25
|
+
* worker installs. `debugSpeedKit=<id>` makes the snippet keep the testing installation for the tab.
|
|
26
|
+
*/
|
|
27
|
+
installPage(scope: string): string;
|
|
28
|
+
private withParam;
|
|
29
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The deployed installation that `--inject` puts into the pages of the origin: the latest deployed
|
|
3
|
+
* one, or one testing installation.
|
|
4
|
+
*/
|
|
5
|
+
export class InjectedInstallation {
|
|
6
|
+
resource;
|
|
7
|
+
testingId;
|
|
8
|
+
installParams;
|
|
9
|
+
constructor(resource,
|
|
10
|
+
/** The id of a testing installation. Without one, the server resolves the installation that is live. */
|
|
11
|
+
testingId) {
|
|
12
|
+
this.resource = resource;
|
|
13
|
+
this.testingId = testingId;
|
|
14
|
+
this.installParams = resource.installParams;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The install resource. A testing installation carries `r=<id>`: the server then answers before
|
|
18
|
+
* a service worker is active, and for an app that has no live installation.
|
|
19
|
+
*/
|
|
20
|
+
get installPath() {
|
|
21
|
+
return this.withParam(this.resource.installPath, "r");
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The install page. `enableSpeedKit=1` puts the browser into group A of a split test, or no
|
|
25
|
+
* worker installs. `debugSpeedKit=<id>` makes the snippet keep the testing installation for the tab.
|
|
26
|
+
*/
|
|
27
|
+
installPage(scope) {
|
|
28
|
+
return this.withParam(`${scope}speed-kit-install.html?url=${encodeURIComponent(scope)}&enableSpeedKit=1`, "debugSpeedKit");
|
|
29
|
+
}
|
|
30
|
+
withParam(url, name) {
|
|
31
|
+
if (!this.testingId) {
|
|
32
|
+
return url;
|
|
33
|
+
}
|
|
34
|
+
const separator = url.includes("?") ? "&" : "?";
|
|
35
|
+
return `${url}${separator}${name}=${encodeURIComponent(this.testingId)}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Interfaces } from "@oclif/core";
|
|
2
2
|
import { CliEnvironment, CommandContextValues } from "../cli/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Gives a bare `--inject` its default value, because oclif knows no flag with an optional value.
|
|
5
|
+
* A value is the next argument, unless that argument is a flag.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withInjectDefault(argv: string[]): string[];
|
|
3
8
|
declare const args: {
|
|
4
9
|
customerPath: Interfaces.Arg<string, {
|
|
5
10
|
exists?: boolean;
|
|
@@ -14,12 +19,14 @@ declare const flags: {
|
|
|
14
19
|
ignoreContentSecurityPolicy: Interfaces.BooleanFlag<boolean>;
|
|
15
20
|
headless: Interfaces.BooleanFlag<boolean>;
|
|
16
21
|
browse: Interfaces.BooleanFlag<boolean>;
|
|
22
|
+
inject: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
|
|
23
|
+
app: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
|
|
17
24
|
browserFetch: Interfaces.BooleanFlag<boolean>;
|
|
18
25
|
useTestConfig: Interfaces.BooleanFlag<boolean>;
|
|
19
26
|
configName: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
|
|
20
27
|
};
|
|
21
28
|
export interface OnboardingServiceBuildContextValues extends CommandContextValues {
|
|
22
|
-
/**
|
|
29
|
+
/** Optional in browse mode and in inject mode. */
|
|
23
30
|
readonly customerPath?: string;
|
|
24
31
|
readonly configName: string;
|
|
25
32
|
readonly domain?: string;
|
|
@@ -33,6 +40,11 @@ export interface OnboardingServiceBuildContextValues extends CommandContextValue
|
|
|
33
40
|
readonly debugPort: number;
|
|
34
41
|
readonly browserFetch: boolean;
|
|
35
42
|
readonly browse: boolean;
|
|
43
|
+
readonly inject: boolean;
|
|
44
|
+
/** The testing installation of `--inject <id>`. Without one, it injects the latest deployed installation. */
|
|
45
|
+
readonly testing?: string;
|
|
46
|
+
/** The app of `--inject`, when no customer config names it. */
|
|
47
|
+
readonly app?: string;
|
|
36
48
|
}
|
|
37
49
|
declare const OnboardingServiceBuildContext_base: new (values: OnboardingServiceBuildContextValues) => Readonly<OnboardingServiceBuildContextValues>;
|
|
38
50
|
/** What `sk onboarding` starts: a local Speed Kit dev environment for one customer. */
|
|
@@ -51,6 +63,8 @@ export declare class OnboardingServiceBuildContext extends OnboardingServiceBuil
|
|
|
51
63
|
ignoreContentSecurityPolicy: Interfaces.BooleanFlag<boolean>;
|
|
52
64
|
headless: Interfaces.BooleanFlag<boolean>;
|
|
53
65
|
browse: Interfaces.BooleanFlag<boolean>;
|
|
66
|
+
inject: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
|
|
67
|
+
app: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
|
|
54
68
|
browserFetch: Interfaces.BooleanFlag<boolean>;
|
|
55
69
|
useTestConfig: Interfaces.BooleanFlag<boolean>;
|
|
56
70
|
configName: Interfaces.OptionFlag<string, Interfaces.CustomOptions>;
|
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
import { Args, Flags } from "@oclif/core";
|
|
2
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
|
+
/** The value `--inject` gets when it is passed without one. */
|
|
5
|
+
const LATEST_INSTALLATION = "latest";
|
|
6
|
+
/**
|
|
7
|
+
* Gives a bare `--inject` its default value, because oclif knows no flag with an optional value.
|
|
8
|
+
* A value is the next argument, unless that argument is a flag.
|
|
9
|
+
*/
|
|
10
|
+
export function withInjectDefault(argv) {
|
|
11
|
+
return argv.map((argument, index) => {
|
|
12
|
+
const next = argv[index + 1];
|
|
13
|
+
const isBare = next === undefined || next.startsWith("-");
|
|
14
|
+
return argument === "--inject" && isBare
|
|
15
|
+
? `--inject=${LATEST_INSTALLATION}`
|
|
16
|
+
: argument;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
4
19
|
const args = {
|
|
5
|
-
// Optional for `--browse`, which
|
|
20
|
+
// Optional for `--browse` and `--inject`, which need no local config; the factory requires it otherwise.
|
|
6
21
|
customerPath: Args.directory({
|
|
7
22
|
name: "customerPath",
|
|
8
23
|
required: false,
|
|
9
|
-
description: "The customer config path (optional with --browse)",
|
|
24
|
+
description: "The customer config path (optional with --browse and --inject)",
|
|
10
25
|
}),
|
|
11
26
|
};
|
|
12
27
|
const flags = {
|
|
@@ -53,6 +68,15 @@ const flags = {
|
|
|
53
68
|
default: false,
|
|
54
69
|
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.",
|
|
55
70
|
}),
|
|
71
|
+
inject: Flags.string({
|
|
72
|
+
exclusive: ["browse"],
|
|
73
|
+
helpValue: "[<installationId>]",
|
|
74
|
+
description: "Inject a deployed Speed Kit installation into the site, for a customer that has not installed Speed Kit. Without a value it uses the latest deployed installation. With an id it uses that testing installation. Needs no local config: without a CUSTOMERPATH, pass --app and -d.",
|
|
75
|
+
}),
|
|
76
|
+
app: Flags.string({
|
|
77
|
+
dependsOn: ["inject"],
|
|
78
|
+
description: "The app of the installation that --inject uses. Wins over the app of the customer config.",
|
|
79
|
+
}),
|
|
56
80
|
browserFetch: Flags.boolean({
|
|
57
81
|
default: false,
|
|
58
82
|
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.",
|
|
@@ -77,6 +101,11 @@ export class OnboardingServiceBuildContext extends Context() {
|
|
|
77
101
|
debugPort: parsedFlags.debugPort,
|
|
78
102
|
browserFetch: parsedFlags.browserFetch,
|
|
79
103
|
browse: parsedFlags.browse,
|
|
104
|
+
app: parsedFlags.app,
|
|
105
|
+
inject: parsedFlags.inject !== undefined,
|
|
106
|
+
testing: parsedFlags.inject === LATEST_INSTALLATION
|
|
107
|
+
? undefined
|
|
108
|
+
: parsedFlags.inject,
|
|
80
109
|
});
|
|
81
110
|
}
|
|
82
111
|
}
|