@speedkit/cli 4.29.0 → 4.31.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 (62) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +1 -1
  3. package/dist/services/customer-config/customer-config-service-model.d.ts +6 -0
  4. package/dist/services/customer-config/customer-config-service-model.js +1 -0
  5. package/dist/services/customer-config/customer-config-service.d.ts +5 -0
  6. package/dist/services/customer-config/customer-config-service.js +15 -0
  7. package/dist/services/deploy/checks/activation/customer-approval-check.d.ts +21 -0
  8. package/dist/services/deploy/checks/activation/customer-approval-check.js +62 -0
  9. package/dist/services/deploy/checks/activation/customer-approval-check.spec.js +64 -0
  10. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +9 -7
  11. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +7 -6
  12. package/dist/services/deploy/deploy-service-factory.js +2 -1
  13. package/dist/services/deploy/handler/install-resource-handler.d.ts +7 -1
  14. package/dist/services/deploy/handler/install-resource-handler.js +40 -19
  15. package/dist/services/deploy/handler/testing-installation-links.d.ts +10 -0
  16. package/dist/services/deploy/handler/testing-installation-links.js +27 -0
  17. package/dist/services/deploy/handler/testing-installation-links.spec.d.ts +1 -0
  18. package/dist/services/deploy/handler/testing-installation-links.spec.js +43 -0
  19. package/dist/services/integration-api/file-list.d.ts +2 -1
  20. package/dist/services/integration-api/file-list.js +7 -1
  21. package/dist/services/integration-api/handler/install-resource-handler.js +10 -2
  22. package/dist/services/integration-api/integration-api-model.d.ts +8 -0
  23. package/dist/services/integration-api/integration-api-model.js +3 -0
  24. package/dist/services/integration-api/spec/config-plugin.spec.d.ts +1 -0
  25. package/dist/services/integration-api/spec/config-plugin.spec.js +188 -0
  26. package/dist/services/integration-api/spec/speed-kit-config-file.spec.d.ts +1 -0
  27. package/dist/{helpers/evaluate-speed-kit-config.spec.js → services/integration-api/spec/speed-kit-config-file.spec.js} +43 -12
  28. package/dist/services/integration-api/spec/speed-kit-config.spec.d.ts +1 -0
  29. package/dist/services/integration-api/spec/speed-kit-config.spec.js +108 -0
  30. package/dist/services/integration-api/struct/config-plugin.d.ts +41 -0
  31. package/dist/services/integration-api/struct/config-plugin.js +241 -0
  32. package/dist/services/integration-api/struct/customer-config.d.ts +7 -1
  33. package/dist/services/integration-api/struct/customer-config.js +10 -3
  34. package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +46 -0
  35. package/dist/services/integration-api/struct/speed-kit-config-file.js +223 -0
  36. package/dist/services/integration-api/struct/speed-kit-config.d.ts +84 -0
  37. package/dist/services/integration-api/struct/speed-kit-config.js +114 -0
  38. package/dist/services/integration-api/virtual/static-recipe.d.ts +1 -1
  39. package/dist/services/query-builder/queries/spec/golden-configs.d.ts +2 -0
  40. package/dist/services/query-builder/queries/spec/golden-configs.js +37 -0
  41. package/dist/services/query-builder/queries/spec/golden.spec.js +5 -4
  42. package/dist/services/query-builder/queries/spec/queries.spec.js +2 -1
  43. package/dist/services/query-builder/queries/spec/reported-bugs.spec.js +2 -1
  44. package/dist/services/query-builder/query-builder-factory.js +4 -8
  45. package/dist/services/query-builder/query-builder-service.js +4 -0
  46. package/dist/services/query-builder/query-builder-service.spec.js +2 -1
  47. package/dist/services/query-builder/rules/rule-model.d.ts +6 -2
  48. package/dist/services/query-builder/rules/rule-model.js +0 -2
  49. package/dist/services/query-builder/rules/rule-parser.d.ts +7 -8
  50. package/dist/services/query-builder/rules/rule-parser.js +41 -47
  51. package/dist/services/query-builder/rules/spec/rule-parser.spec.js +30 -32
  52. package/dist/services/query-builder/rules/spec/rule-reducer.spec.js +2 -1
  53. package/dist/services/query-builder/rules/spec/rule-relevance.spec.js +7 -3
  54. package/dist/services/query-builder/rules/spec/rule-to-sql.spec.js +3 -2
  55. package/dist/services/query-builder/rules/spec/strip-parameters.spec.js +5 -2
  56. package/oclif.manifest.json +1 -1
  57. package/package.json +1 -1
  58. package/dist/helpers/evaluate-speed-kit-config.d.ts +0 -11
  59. package/dist/helpers/evaluate-speed-kit-config.js +0 -150
  60. /package/dist/{helpers/evaluate-speed-kit-config.spec.d.ts → services/deploy/checks/activation/customer-approval-check.spec.d.ts} +0 -0
  61. /package/dist/services/{deploy → integration-api}/error/evaluate-sk-config-error.d.ts +0 -0
  62. /package/dist/services/{deploy → integration-api}/error/evaluate-sk-config-error.js +0 -0
@@ -0,0 +1,114 @@
1
+ import { applyConfigPlugins } from "./config-plugin.js";
2
+ /** The rule-set version a config runs under when it declares none. */
3
+ export const DEFAULT_RULE_SET_VERSION = 1;
4
+ /**
5
+ * A Speed Kit config, the way `config_customer.json` has {@link CustomerConfig}.
6
+ *
7
+ * The evaluated config is whatever a customer's JavaScript assigned — a rule
8
+ * set can be missing, or be a string where a list belongs, and every reader had
9
+ * to defend against that on its own. This is where that happens once: the
10
+ * fields below are the shape the CLI reads, and {@link raw} is still there for
11
+ * everything a customer puts in a config that the CLI knows nothing about.
12
+ */
13
+ export class SpeedKitConfig {
14
+ raw;
15
+ effective;
16
+ plugins;
17
+ ruleSetVersion;
18
+ blacklist;
19
+ disabledScopes;
20
+ disabledSites;
21
+ enabledSites;
22
+ stripQueryParams;
23
+ whitelist;
24
+ split;
25
+ splitTestId;
26
+ detectDevice;
27
+ customVariation;
28
+ constructor(
29
+ /**
30
+ * The config as the file wrote it: everything `window.speedKit` carried
31
+ * before any plugin ran, including what is not read here. This is the
32
+ * config a human edits, which is what a review of a change looks at.
33
+ */
34
+ raw,
35
+ /**
36
+ * The config as Speed Kit runs it: {@link raw} with every config plugin
37
+ * applied. The rule sets below are read from here, because a rule a plugin
38
+ * contributes governs the site exactly like one written into the file.
39
+ */
40
+ effective,
41
+ /** The plugins that contributed to {@link effective}, in order. */
42
+ plugins, ruleSetVersion, blacklist, disabledScopes, disabledSites, enabledSites,
43
+ /** Either a bare condition or a list of rules scoping the stripping. */
44
+ stripQueryParams, whitelist,
45
+ // Read from `raw`, not from `effective`: these answer "what does this file
46
+ // say", which is the question the pre-deploy checks ask before a human
47
+ // confirms a change. `SSRPlugin` replaces `detectDevice` with a wrapper of
48
+ // its own, and comparing that wrapper against itself would tell nobody
49
+ // anything.
50
+ split, splitTestId, detectDevice, customVariation) {
51
+ this.raw = raw;
52
+ this.effective = effective;
53
+ this.plugins = plugins;
54
+ this.ruleSetVersion = ruleSetVersion;
55
+ this.blacklist = blacklist;
56
+ this.disabledScopes = disabledScopes;
57
+ this.disabledSites = disabledSites;
58
+ this.enabledSites = enabledSites;
59
+ this.stripQueryParams = stripQueryParams;
60
+ this.whitelist = whitelist;
61
+ this.split = split;
62
+ this.splitTestId = splitTestId;
63
+ this.detectDevice = detectDevice;
64
+ this.customVariation = customVariation;
65
+ }
66
+ /**
67
+ * @param evaluated what the config assigned to `window.speedKit`
68
+ * @param plugins what it registered on `window.SpeedKit.configPlugins`
69
+ */
70
+ static createFrom(evaluated, plugins = []) {
71
+ const raw = asRecord(evaluated) ?? {};
72
+ const applied = applyConfigPlugins(raw, plugins);
73
+ const config = applied.config;
74
+ return new SpeedKitConfig(raw, config, applied.plugins, parseRuleSetVersion(config), asRules(config.blacklist), asList(config.disabledScopes), asRules(config.disabledSites), asRules(config.enabledSites),
75
+ // A bare condition is a single string or regular expression, so an
76
+ // unwrapped value is a list of one — not a missing one.
77
+ config.stripQueryParams === undefined || config.stripQueryParams === null
78
+ ? []
79
+ : asList(config.stripQueryParams, { wrapSingle: true }), asRules(config.whitelist), raw.split, raw.splitTestId, raw.detectDevice, raw.customVariation === undefined
80
+ ? undefined
81
+ : asRules(raw.customVariation));
82
+ }
83
+ }
84
+ /** The declared rule-set version, or 1 for a config that declares none. */
85
+ function parseRuleSetVersion(config) {
86
+ const version = Number(asRecord(config.ruleSetConfig)?.version);
87
+ return Number.isFinite(version) && version > 0
88
+ ? version
89
+ : DEFAULT_RULE_SET_VERSION;
90
+ }
91
+ /** A rule set is a list of rules — anything else carries no rule at all. */
92
+ function asRules(value) {
93
+ const rules = [];
94
+ for (const entry of asList(value)) {
95
+ const rule = asRecord(entry);
96
+ if (rule) {
97
+ rules.push(rule);
98
+ }
99
+ }
100
+ return rules;
101
+ }
102
+ function asList(value, options = {}) {
103
+ if (Array.isArray(value)) {
104
+ return value;
105
+ }
106
+ return options.wrapSingle && value !== undefined && value !== null
107
+ ? [value]
108
+ : [];
109
+ }
110
+ function asRecord(value) {
111
+ return typeof value === "object" && value !== null && !Array.isArray(value)
112
+ ? value
113
+ : undefined;
114
+ }
@@ -1,4 +1,4 @@
1
- import { VirtualFileRecipe } from "../integration-api-model.js";
1
+ import type { VirtualFileRecipe } from "../integration-api-model.js";
2
2
  export declare class StaticRecipe implements VirtualFileRecipe {
3
3
  private content;
4
4
  constructor(content: string);
@@ -14,5 +14,7 @@ export interface GoldenConfig {
14
14
  readonly describes: string;
15
15
  readonly hosts: readonly string[];
16
16
  readonly name: string;
17
+ /** What the config registered on `window.SpeedKit.configPlugins`. */
18
+ readonly plugins?: readonly Record<string, unknown>[];
17
19
  }
18
20
  export declare const GOLDEN_CONFIGS: readonly GoldenConfig[];
@@ -1,4 +1,41 @@
1
1
  export const GOLDEN_CONFIGS = [
2
+ {
3
+ config: {
4
+ enabledSites: [{ host: ["www.shop.de"], pathname: [/^\/$/, /^\/p\//i] }],
5
+ ruleSetConfig: { version: 2 },
6
+ stripQueryParams: ["size"],
7
+ whitelist: [{ host: ["www.shop.de"] }],
8
+ },
9
+ describes: "a config whose cart, scope and tracking rules come from plugins rather than from the file",
10
+ hosts: ["www.shop.de"],
11
+ name: "config-plugins",
12
+ // The shapes the published bundles have: a name, and every hook as an own
13
+ // instance field.
14
+ plugins: [
15
+ {
16
+ name: "BlacklistPlugin",
17
+ options: { exclude: ["admin"] },
18
+ addBlacklist: () => [
19
+ { pathname: [/cart/i], tags: ["cart"] },
20
+ { pathname: [/checkout/i, /order/i], tags: ["checkout"] },
21
+ { pathname: [/\/admin/i], tags: ["admin"] },
22
+ { pathname: [/\.pdf/], tags: ["misc"] },
23
+ ],
24
+ },
25
+ {
26
+ name: "StripQueryParamPlugin",
27
+ options: {},
28
+ addStripQueryParams: () => [
29
+ { params: ["gclid", /^utm[_-]/], tags: ["ads"] },
30
+ ],
31
+ },
32
+ {
33
+ name: "ShopifyPlugin",
34
+ options: {},
35
+ addDisabledScopes: () => ["/cart", "/checkout", "/account"],
36
+ },
37
+ ],
38
+ },
2
39
  {
3
40
  config: {
4
41
  blacklist: [
@@ -4,6 +4,7 @@ import { fileURLToPath } from "node:url";
4
4
  import { expect } from "chai";
5
5
  import { describe, it } from "mocha";
6
6
  import { parseRuleSets } from "../../rules/index.js";
7
+ import { SpeedKitConfig } from "../../../integration-api/struct/speed-kit-config.js";
7
8
  import { buildParameterQuery } from "../parameter-query.js";
8
9
  import { buildPopsQuery } from "../pops-query.js";
9
10
  import { buildPrewarmQuery } from "../prewarm-query.js";
@@ -57,25 +58,25 @@ function expectGolden(name, text) {
57
58
  }
58
59
  /** The prewarm query for one of the golden configs, with options on top. */
59
60
  function prewarmQueryFor(name, options) {
60
- const { config, hosts } = GOLDEN_CONFIGS.find((candidate) => candidate.name === name);
61
+ const { config, hosts, plugins } = GOLDEN_CONFIGS.find((candidate) => candidate.name === name);
61
62
  return buildPrewarmQuery({
62
63
  app: APP,
63
64
  days: 10,
64
65
  hosts,
65
66
  now: NOW,
66
- ruleSets: parseRuleSets(config),
67
+ ruleSets: parseRuleSets(SpeedKitConfig.createFrom(config, plugins)),
67
68
  ...options,
68
69
  });
69
70
  }
70
71
  describe("golden queries", () => {
71
- for (const { config, describes, hosts, name } of GOLDEN_CONFIGS) {
72
+ for (const { config, describes, hosts, name, plugins } of GOLDEN_CONFIGS) {
72
73
  describe(`${name} — ${describes}`, () => {
73
74
  const input = {
74
75
  app: APP,
75
76
  days: 10,
76
77
  hosts,
77
78
  now: NOW,
78
- ruleSets: parseRuleSets(config),
79
+ ruleSets: parseRuleSets(SpeedKitConfig.createFrom(config, plugins)),
79
80
  };
80
81
  it("should build the same prewarm query", () => {
81
82
  expectGolden(`${name}.prewarm`, goldenText(buildPrewarmQuery(input)));
@@ -1,6 +1,7 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
3
  import { parseRuleSets } from "../../rules/index.js";
4
+ import { SpeedKitConfig, } from "../../../integration-api/struct/speed-kit-config.js";
4
5
  import { buildParameterQuery } from "../parameter-query.js";
5
6
  import { buildPrewarmQuery } from "../prewarm-query.js";
6
7
  import { sinceDate } from "../query-model.js";
@@ -9,7 +10,7 @@ function inputFor(config, options = {}) {
9
10
  app: "shop-de",
10
11
  days: 10,
11
12
  hosts: ["www.shop.de"],
12
- ruleSets: parseRuleSets(config),
13
+ ruleSets: parseRuleSets(SpeedKitConfig.createFrom(config)),
13
14
  ...options,
14
15
  };
15
16
  }
@@ -1,6 +1,7 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
3
  import { parseRuleSets } from "../../rules/index.js";
4
+ import { SpeedKitConfig, } from "../../../integration-api/struct/speed-kit-config.js";
4
5
  import { buildParameterQuery } from "../parameter-query.js";
5
6
  import { buildPrewarmQuery } from "../prewarm-query.js";
6
7
  /**
@@ -16,7 +17,7 @@ function inputFor(config, hosts) {
16
17
  days: 30,
17
18
  hosts,
18
19
  now: new Date("2026-09-04T09:30:00Z"),
19
- ruleSets: parseRuleSets(config),
20
+ ruleSets: parseRuleSets(SpeedKitConfig.createFrom(config)),
20
21
  };
21
22
  }
22
23
  describe("ED-277: a query from whitelisted pathnames", () => {
@@ -6,7 +6,6 @@ import { CliServiceFactory } from "../cli/index.js";
6
6
  import { AthenaServiceFactory } from "../athena/index.js";
7
7
  import { safe } from "../../helpers/safe.js";
8
8
  import { ParseConfigSpeedKitError } from "./error/parse-config-speed-kit-error.js";
9
- import { evaluateLocalSpeedKitConfig } from "../../helpers/evaluate-speed-kit-config.js";
10
9
  export class QueryBuilderFactory {
11
10
  context;
12
11
  userConfig;
@@ -20,8 +19,7 @@ export class QueryBuilderFactory {
20
19
  const customerConfig = files.getCustomerConfig().config;
21
20
  const app = customerConfig.app;
22
21
  const origins = customerConfig.origins ?? [];
23
- const speedKitConfigFile = files.getByName(INTEGRATION_FILES.CONFIG.SPEED_KIT);
24
- const speedKitConfig = await this.getSpeedKitConfig(app, origins[0], speedKitConfigFile);
22
+ const speedKitConfig = await this.getSpeedKitConfig(app, origins[0], files.getSpeedKitConfig());
25
23
  const serviceContext = {
26
24
  app,
27
25
  bySet: this.context.bySet,
@@ -53,14 +51,12 @@ export class QueryBuilderFactory {
53
51
  return new QueryBuilderService(serviceContext, parseRuleSets(speedKitConfig), cli, athenaService);
54
52
  }
55
53
  async getIntegrationFiles() {
56
- const integrationApiContext = new IntegrationApiContext(this.context.customerPath, this.context.configName, ["config_SpeedKit"]);
54
+ const integrationApiContext = new IntegrationApiContext(this.context.customerPath, this.context.configName, [INTEGRATION_FILES.CONFIG.SPEED_KIT]);
57
55
  const integrationApi = new IntegrationApiFactory(integrationApiContext, this.userConfig).buildService();
58
56
  return await integrationApi.run();
59
57
  }
60
- getSpeedKitConfig(app, origin, speedKitConfigFile) {
61
- const result = safe(() => {
62
- return evaluateLocalSpeedKitConfig(app, origin, speedKitConfigFile);
63
- });
58
+ async getSpeedKitConfig(app, origin, speedKitConfigFile) {
59
+ const result = await safe(speedKitConfigFile.evaluate(app, origin));
64
60
  if (result.success !== true) {
65
61
  throw new ParseConfigSpeedKitError(result.error);
66
62
  }
@@ -145,6 +145,10 @@ export class QueryBuilderService {
145
145
  * decides is not a divergence, it is arithmetic.
146
146
  */
147
147
  report(query) {
148
+ const { plugins } = this.ruleSets;
149
+ if (plugins.length > 0) {
150
+ this.cli.write(this.cli.style.dim(`Config plugins applied: ${plugins.join(", ")} — their rules are part of this query`));
151
+ }
148
152
  for (const { reason, source } of query.findings) {
149
153
  this.cli.writeWarning(`${source} ${reason}`);
150
154
  }
@@ -3,6 +3,7 @@ import { describe, it } from "mocha";
3
3
  import { QueryBuilderService } from "./query-builder-service.js";
4
4
  import { QueryType } from "./query-builder-model.js";
5
5
  import { parseRuleSets } from "./rules/index.js";
6
+ import { SpeedKitConfig } from "../integration-api/struct/speed-kit-config.js";
6
7
  /** Records the actions so a left-running spinner shows up as a missing fail. */
7
8
  function recordingCli() {
8
9
  const calls = [];
@@ -32,7 +33,7 @@ function countService(cli) {
32
33
  },
33
34
  // The real parser on an empty config: a hand-rolled stand-in would fail the
34
35
  // build step and the test would pass for the wrong reason.
35
- parseRuleSets({}), cli, refusingAthena);
36
+ parseRuleSets(SpeedKitConfig.createFrom({})), cli, refusingAthena);
36
37
  }
37
38
  describe("QueryBuilderService — a query that could not run", () => {
38
39
  // Regression: this used to print the message and return, so the process
@@ -9,8 +9,6 @@ import { ConditionKey } from "@speedkit/rules";
9
9
  * even means. Everything downstream — relevance, reduction, translation —
10
10
  * works on the types here instead of on the raw config.
11
11
  */
12
- /** The rule-set version a config runs under when it declares none. */
13
- export declare const DEFAULT_RULE_SET_VERSION = 1;
14
12
  /** Content types a page impression can have. */
15
13
  export declare const DOCUMENT_CONTENT_TYPES: readonly ["document", "navigate"];
16
14
  /** The condition keys naming a part of the URL a query can match on. */
@@ -83,6 +81,12 @@ export interface ParsedRuleSets {
83
81
  readonly disabledScopes: readonly NormalizedRule[];
84
82
  readonly disabledSites: readonly NormalizedRule[];
85
83
  readonly enabledSites: readonly NormalizedRule[];
84
+ /**
85
+ * The config plugins the rules were read through. A plugin contributes
86
+ * rules that are nowhere in `config_SpeedKit.js`, so a query can exclude
87
+ * pages the file never mentions — worth saying out loud.
88
+ */
89
+ readonly plugins: readonly string[];
86
90
  readonly ruleSetVersion: number;
87
91
  readonly stripQueryParams: readonly StripParameterRule[];
88
92
  readonly whitelist: readonly NormalizedRule[];
@@ -9,8 +9,6 @@ import { ConditionKey } from "@speedkit/rules";
9
9
  * even means. Everything downstream — relevance, reduction, translation —
10
10
  * works on the types here instead of on the raw config.
11
11
  */
12
- /** The rule-set version a config runs under when it declares none. */
13
- export const DEFAULT_RULE_SET_VERSION = 1;
14
12
  /** Content types a page impression can have. */
15
13
  export const DOCUMENT_CONTENT_TYPES = ["document", "navigate"];
16
14
  /** The condition keys naming a part of the URL a query can match on. */
@@ -1,10 +1,9 @@
1
+ import type { SpeedKitConfig, SpeedKitRule } from "../../integration-api/struct/speed-kit-config.js";
1
2
  import { ConditionPattern, NormalizedRule, ParsedRuleSets, StripParameterRule } from "./rule-model.js";
2
- /** Reads the rule sets a query is built from out of an evaluated config. */
3
- export declare function parseRuleSets(config: unknown): ParsedRuleSets;
4
- /** The declared rule-set version, or 1 for a config that declares none. */
5
- export declare function parseRuleSetVersion(config: unknown): number;
6
- /** Normalizes a `SpeedKitRule[]` — anything else yields no rules. */
7
- export declare function parseRules(value: unknown, source: string, ruleSetVersion: number): NormalizedRule[];
3
+ /** Reads the rule sets a query is built from out of a Speed Kit config. */
4
+ export declare function parseRuleSets(config: SpeedKitConfig): ParsedRuleSets;
5
+ /** Normalizes the rules of one rule set. */
6
+ export declare function parseRules(rules: readonly SpeedKitRule[], source: string, ruleSetVersion: number): NormalizedRule[];
8
7
  /**
9
8
  * Normalizes a `Condition` — a string, a regular expression, or an array of
10
9
  * both — into a flat list of patterns.
@@ -24,10 +23,10 @@ export declare function parseCondition(value: unknown): ConditionPattern[];
24
23
  * never touches them — they belong in neither a pre-warm list nor a parameter
25
24
  * analysis.
26
25
  */
27
- export declare function parseDisabledScopes(value: unknown): NormalizedRule[];
26
+ export declare function parseDisabledScopes(scopes: readonly unknown[]): NormalizedRule[];
28
27
  /**
29
28
  * Normalizes `stripQueryParams`, which is either a bare condition (every
30
29
  * parameter matching it is stripped everywhere) or a list of rules that scope
31
30
  * the stripping to certain pages or resources.
32
31
  */
33
- export declare function parseStripQueryParams(value: unknown, ruleSetVersion: number): StripParameterRule[];
32
+ export declare function parseStripQueryParams(entries: readonly unknown[], ruleSetVersion: number): StripParameterRule[];
@@ -1,43 +1,29 @@
1
1
  import { types } from "node:util";
2
2
  import { ConditionKey } from "@speedkit/rules";
3
3
  import { escapeRegexLiteral } from "../sql/index.js";
4
- import { CRITERIA_OUTSIDE_THE_URL, DEFAULT_RULE_SET_VERSION, } from "./rule-model.js";
4
+ import { CRITERIA_OUTSIDE_THE_URL, } from "./rule-model.js";
5
5
  const SUBJECTS = ["host", "pathname", "url"];
6
- /** Reads the rule sets a query is built from out of an evaluated config. */
6
+ /** Reads the rule sets a query is built from out of a Speed Kit config. */
7
7
  export function parseRuleSets(config) {
8
- const speedKitConfig = asRecord(config) ?? {};
9
- const ruleSetVersion = parseRuleSetVersion(speedKitConfig);
8
+ const { ruleSetVersion } = config;
10
9
  return {
11
- blacklist: parseRules(speedKitConfig.blacklist, "blacklist", ruleSetVersion),
12
- disabledScopes: parseDisabledScopes(speedKitConfig.disabledScopes),
13
- disabledSites: parseRules(speedKitConfig.disabledSites, "disabledSites", ruleSetVersion),
14
- enabledSites: parseRules(speedKitConfig.enabledSites, "enabledSites", ruleSetVersion),
10
+ blacklist: parseRules(config.blacklist, "blacklist", ruleSetVersion),
11
+ disabledScopes: parseDisabledScopes(config.disabledScopes),
12
+ disabledSites: parseRules(config.disabledSites, "disabledSites", ruleSetVersion),
13
+ enabledSites: parseRules(config.enabledSites, "enabledSites", ruleSetVersion),
14
+ plugins: config.plugins,
15
15
  ruleSetVersion,
16
- stripQueryParams: parseStripQueryParams(speedKitConfig.stripQueryParams, ruleSetVersion),
17
- whitelist: parseRules(speedKitConfig.whitelist, "whitelist", ruleSetVersion),
16
+ stripQueryParams: parseStripQueryParams(config.stripQueryParams, ruleSetVersion),
17
+ whitelist: parseRules(config.whitelist, "whitelist", ruleSetVersion),
18
18
  };
19
19
  }
20
- /** The declared rule-set version, or 1 for a config that declares none. */
21
- export function parseRuleSetVersion(config) {
22
- const ruleSetConfig = asRecord(asRecord(config)?.ruleSetConfig);
23
- const version = Number(ruleSetConfig?.version);
24
- return Number.isFinite(version) && version > 0
25
- ? version
26
- : DEFAULT_RULE_SET_VERSION;
27
- }
28
- /** Normalizes a `SpeedKitRule[]` — anything else yields no rules. */
29
- export function parseRules(value, source, ruleSetVersion) {
30
- if (!Array.isArray(value)) {
31
- return [];
32
- }
33
- const rules = [];
34
- for (const [index, entry] of value.entries()) {
35
- const rule = parseRule(entry, `${source}[${index}]`, ruleSetVersion);
36
- if (rule) {
37
- rules.push(rule);
38
- }
20
+ /** Normalizes the rules of one rule set. */
21
+ export function parseRules(rules, source, ruleSetVersion) {
22
+ const parsed = [];
23
+ for (const [index, rule] of rules.entries()) {
24
+ parsed.push(parseRule(rule, `${source}[${index}]`, ruleSetVersion));
39
25
  }
40
- return rules;
26
+ return parsed;
41
27
  }
42
28
  /**
43
29
  * Normalizes a `Condition` — a string, a regular expression, or an array of
@@ -65,11 +51,7 @@ export function parseCondition(value) {
65
51
  }
66
52
  return patterns;
67
53
  }
68
- function parseRule(value, source, ruleSetVersion) {
69
- const rule = asRecord(value);
70
- if (!rule) {
71
- return undefined;
72
- }
54
+ function parseRule(rule, source, ruleSetVersion) {
73
55
  const conditions = [];
74
56
  for (const subject of SUBJECTS) {
75
57
  const patterns = parseCondition(rule[subject]);
@@ -115,12 +97,9 @@ function parseContentTypes(value) {
115
97
  * never touches them — they belong in neither a pre-warm list nor a parameter
116
98
  * analysis.
117
99
  */
118
- export function parseDisabledScopes(value) {
119
- if (!Array.isArray(value)) {
120
- return [];
121
- }
100
+ export function parseDisabledScopes(scopes) {
122
101
  const rules = [];
123
- for (const [index, entry] of value.entries()) {
102
+ for (const [index, entry] of scopes.entries()) {
124
103
  if (typeof entry !== "string" || entry.length === 0) {
125
104
  continue;
126
105
  }
@@ -151,13 +130,14 @@ export function parseDisabledScopes(value) {
151
130
  * parameter matching it is stripped everywhere) or a list of rules that scope
152
131
  * the stripping to certain pages or resources.
153
132
  */
154
- export function parseStripQueryParams(value, ruleSetVersion) {
155
- if (value === undefined || value === null) {
133
+ export function parseStripQueryParams(entries, ruleSetVersion) {
134
+ if (entries.length === 0) {
156
135
  return [];
157
136
  }
158
- const entries = Array.isArray(value) ? value : [value];
159
- // A bare condition: strings and regular expressions, no scoping.
160
- const bare = entries.filter((entry) => typeof entry === "string" || entry instanceof RegExp);
137
+ // A bare condition: strings and regular expressions, no scoping. The config
138
+ // is evaluated in a vm context, so `instanceof RegExp` is false for its own
139
+ // regular expressions — `isRegExp` is what recognizes them across realms.
140
+ const bare = entries.filter((entry) => typeof entry === "string" || types.isRegExp(entry));
161
141
  if (bare.length === entries.length) {
162
142
  const params = parseCondition(bare);
163
143
  return params.length > 0
@@ -173,15 +153,29 @@ export function parseStripQueryParams(value, ruleSetVersion) {
173
153
  }
174
154
  const source = `stripQueryParams[${index}]`;
175
155
  rules.push({
176
- enabledSites: parseRules(rule.enabledSites, `${source}.enabledSites`, ruleSetVersion),
156
+ enabledSites: parseRules(asRules(rule.enabledSites), `${source}.enabledSites`, ruleSetVersion),
177
157
  params,
178
- rules: parseRules(rule.rules, `${source}.rules`, ruleSetVersion),
158
+ rules: parseRules(asRules(rule.rules), `${source}.rules`, ruleSetVersion),
179
159
  ruleSetVersion,
180
160
  source,
181
161
  });
182
162
  }
183
163
  return rules;
184
164
  }
165
+ /** A nested rule set of a `stripQueryParams` rule. */
166
+ function asRules(value) {
167
+ if (!Array.isArray(value)) {
168
+ return [];
169
+ }
170
+ const rules = [];
171
+ for (const entry of value) {
172
+ const rule = asRecord(entry);
173
+ if (rule) {
174
+ rules.push(rule);
175
+ }
176
+ }
177
+ return rules;
178
+ }
185
179
  function asRecord(value) {
186
180
  return typeof value === "object" && value !== null && !Array.isArray(value)
187
181
  ? value