@speedkit/cli 4.30.0 → 4.32.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 +14 -0
- package/README.md +1 -1
- package/dist/services/deploy/checks/activation/customer-approval-check.js +5 -2
- package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +9 -7
- package/dist/services/deploy/checks/pre-deploy/split-change-check.js +7 -6
- package/dist/services/integration-api/file-list.d.ts +2 -1
- package/dist/services/integration-api/file-list.js +7 -1
- package/dist/services/integration-api/handler/install-resource-handler.js +10 -2
- package/dist/services/integration-api/integration-api-model.d.ts +8 -0
- package/dist/services/integration-api/integration-api-model.js +3 -0
- package/dist/services/integration-api/spec/config-plugin.spec.js +188 -0
- package/dist/services/integration-api/spec/speed-kit-config-file.spec.d.ts +1 -0
- package/dist/{helpers/evaluate-speed-kit-config.spec.js → services/integration-api/spec/speed-kit-config-file.spec.js} +43 -12
- package/dist/services/integration-api/spec/speed-kit-config.spec.d.ts +1 -0
- package/dist/services/integration-api/spec/speed-kit-config.spec.js +108 -0
- package/dist/services/integration-api/struct/config-plugin.d.ts +41 -0
- package/dist/services/integration-api/struct/config-plugin.js +241 -0
- package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +46 -0
- package/dist/services/integration-api/struct/speed-kit-config-file.js +223 -0
- package/dist/services/integration-api/struct/speed-kit-config.d.ts +84 -0
- package/dist/services/integration-api/struct/speed-kit-config.js +114 -0
- package/dist/services/integration-api/virtual/static-recipe.d.ts +1 -1
- package/dist/services/onboarding/empty-service-worker.d.ts +8 -0
- package/dist/services/onboarding/empty-service-worker.js +17 -0
- package/dist/services/onboarding/empty-service-worker.spec.d.ts +1 -0
- package/dist/services/onboarding/empty-service-worker.spec.js +22 -0
- package/dist/services/onboarding/fetch-events/customer-service-worker-js.js +3 -2
- package/dist/services/onboarding/onboarding-service.js +2 -1
- package/dist/services/query-builder/queries/spec/golden-configs.d.ts +2 -0
- package/dist/services/query-builder/queries/spec/golden-configs.js +37 -0
- package/dist/services/query-builder/queries/spec/golden.spec.js +5 -4
- package/dist/services/query-builder/queries/spec/queries.spec.js +2 -1
- package/dist/services/query-builder/queries/spec/reported-bugs.spec.js +2 -1
- package/dist/services/query-builder/query-builder-factory.js +4 -8
- package/dist/services/query-builder/query-builder-service.js +4 -0
- package/dist/services/query-builder/query-builder-service.spec.js +2 -1
- package/dist/services/query-builder/rules/rule-model.d.ts +6 -2
- package/dist/services/query-builder/rules/rule-model.js +0 -2
- package/dist/services/query-builder/rules/rule-parser.d.ts +7 -8
- package/dist/services/query-builder/rules/rule-parser.js +41 -47
- package/dist/services/query-builder/rules/spec/rule-parser.spec.js +30 -32
- package/dist/services/query-builder/rules/spec/rule-reducer.spec.js +2 -1
- package/dist/services/query-builder/rules/spec/rule-relevance.spec.js +7 -3
- package/dist/services/query-builder/rules/spec/rule-to-sql.spec.js +3 -2
- package/dist/services/query-builder/rules/spec/strip-parameters.spec.js +5 -2
- package/oclif.manifest.json +112 -112
- package/package.json +1 -1
- package/dist/helpers/evaluate-speed-kit-config.d.ts +0 -16
- package/dist/helpers/evaluate-speed-kit-config.js +0 -157
- /package/dist/services/{deploy → integration-api}/error/evaluate-sk-config-error.d.ts +0 -0
- /package/dist/services/{deploy → integration-api}/error/evaluate-sk-config-error.js +0 -0
- /package/dist/{helpers/evaluate-speed-kit-config.spec.d.ts → services/integration-api/spec/config-plugin.spec.d.ts} +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { EvaluatedSpeedKitConfig } from "./speed-kit-config.js";
|
|
2
|
+
/**
|
|
3
|
+
* Config plugins, the way the Speed Kit snippet applies them.
|
|
4
|
+
*
|
|
5
|
+
* A config registers plugins on `window.SpeedKit.configPlugins`, and in the
|
|
6
|
+
* browser it is `ConfigController` — part of the snippet, not of the config —
|
|
7
|
+
* that folds their hooks into `window.speedKit` before Speed Kit starts. A
|
|
8
|
+
* plugin can therefore contribute rules that appear nowhere in
|
|
9
|
+
* `config_SpeedKit.js`: `BlacklistPlugin` brings the whole cart/checkout/account
|
|
10
|
+
* blacklist, `StripQueryParamPlugin` the tracking parameters, `ShopifyPlugin`
|
|
11
|
+
* the Shopify scopes.
|
|
12
|
+
*
|
|
13
|
+
* The CLI evaluates the config in a vm, where the snippet never runs, so this
|
|
14
|
+
* is a port of `ConfigController.executePlugins` and its three modifiers —
|
|
15
|
+
* `speed-kit/snippet/ConfigController.ts` and
|
|
16
|
+
* `speed-kit/snippet/configplugins/`. It follows that code deliberately
|
|
17
|
+
* closely, quirks included, because a reading that differs from production is
|
|
18
|
+
* worse than no reading at all. Two things are on purpose *not* copied
|
|
19
|
+
* verbatim: every `instanceof RegExp` becomes `types.isRegExp`, since the
|
|
20
|
+
* config's regular expressions come out of the vm and belong to another realm,
|
|
21
|
+
* where `instanceof` is false and production's answer would be inverted.
|
|
22
|
+
*
|
|
23
|
+
* Keep this in step with the snippet. If a hook is added there, it is added
|
|
24
|
+
* here, or the query builder quietly goes back to reading half a config.
|
|
25
|
+
*/
|
|
26
|
+
/** A plugin instance as the evaluated config registered it. */
|
|
27
|
+
export type ConfigPluginLike = Record<string, any>;
|
|
28
|
+
export interface AppliedConfigPlugins {
|
|
29
|
+
/** The config as Speed Kit runs it: what the file wrote plus every hook. */
|
|
30
|
+
readonly config: EvaluatedSpeedKitConfig;
|
|
31
|
+
/** The names of the plugins that contributed something, in order. */
|
|
32
|
+
readonly plugins: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Applies the plugins a config registered, leaving the evaluated config itself
|
|
36
|
+
* untouched.
|
|
37
|
+
*
|
|
38
|
+
* @param evaluated what the config assigned to `window.speedKit`
|
|
39
|
+
* @param plugins what it registered on `window.SpeedKit.configPlugins`
|
|
40
|
+
*/
|
|
41
|
+
export declare function applyConfigPlugins(evaluated: EvaluatedSpeedKitConfig, plugins: readonly ConfigPluginLike[]): AppliedConfigPlugins;
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { types } from "node:util";
|
|
2
|
+
import { ConditionKey } from "@speedkit/rules";
|
|
3
|
+
const MODIFIERS = {
|
|
4
|
+
addBlacklist,
|
|
5
|
+
addDisabledScopes,
|
|
6
|
+
addStripQueryParams,
|
|
7
|
+
};
|
|
8
|
+
/** The hook that may modify the config directly, and runs after all others. */
|
|
9
|
+
const APPLY_TO_CONFIG = "applyToConfig";
|
|
10
|
+
/**
|
|
11
|
+
* Applies the plugins a config registered, leaving the evaluated config itself
|
|
12
|
+
* untouched.
|
|
13
|
+
*
|
|
14
|
+
* @param evaluated what the config assigned to `window.speedKit`
|
|
15
|
+
* @param plugins what it registered on `window.SpeedKit.configPlugins`
|
|
16
|
+
*/
|
|
17
|
+
export function applyConfigPlugins(evaluated, plugins) {
|
|
18
|
+
const config = deepClone(evaluated);
|
|
19
|
+
const applied = [];
|
|
20
|
+
if (plugins.length === 0) {
|
|
21
|
+
return { config, plugins: applied };
|
|
22
|
+
}
|
|
23
|
+
// One copy, taken before any modification and shared by every hook, so a
|
|
24
|
+
// hook never sees what an earlier one added.
|
|
25
|
+
const configCopy = deepClone(config);
|
|
26
|
+
const deferred = [];
|
|
27
|
+
for (const plugin of plugins) {
|
|
28
|
+
if (!plugin || typeof plugin !== "object") {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
let contributed = false;
|
|
32
|
+
// Own enumerable keys only — the same reading the snippet does, which is
|
|
33
|
+
// why a plugin declares its hooks as instance fields and its helpers on
|
|
34
|
+
// the prototype.
|
|
35
|
+
for (const key of Object.keys(plugin)) {
|
|
36
|
+
const hook = plugin[key];
|
|
37
|
+
if (typeof hook !== "function") {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (key === APPLY_TO_CONFIG) {
|
|
41
|
+
deferred.push(hook);
|
|
42
|
+
contributed = true;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const modifier = MODIFIERS[key];
|
|
46
|
+
if (!modifier) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
// One faulty plugin must not cost us the rest of the config.
|
|
50
|
+
try {
|
|
51
|
+
const modification = getConfigValues(hook, configCopy, plugin);
|
|
52
|
+
if (modification === undefined) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
modifier(modification, config);
|
|
56
|
+
contributed = true;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
/* the snippet reports it and carries on; so do we */
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (contributed) {
|
|
63
|
+
applied.push(pluginName(plugin));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const hook of deferred) {
|
|
67
|
+
try {
|
|
68
|
+
hook(config);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
/* as above */
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { config, plugins: applied };
|
|
75
|
+
}
|
|
76
|
+
/** What a hook contributes, or undefined when it contributes nothing. */
|
|
77
|
+
function getConfigValues(hook, configCopy, plugin) {
|
|
78
|
+
const value = hook(configCopy);
|
|
79
|
+
if (!value ||
|
|
80
|
+
(Array.isArray(value) && value.length === 0) ||
|
|
81
|
+
// A regular expression has no own keys either, so a bare pattern is
|
|
82
|
+
// dropped here — in the snippet as much as here.
|
|
83
|
+
(typeof value === "object" && Object.keys(value).length === 0)) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
if (!Array.isArray(value)) {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
const name = pluginName(plugin);
|
|
90
|
+
const exclude = plugin.options?.exclude;
|
|
91
|
+
const rules = exclude ? applyExcludeTag(value, exclude, name) : value;
|
|
92
|
+
return prepareRules(rules, name);
|
|
93
|
+
}
|
|
94
|
+
function pluginName(plugin) {
|
|
95
|
+
return typeof plugin.name === "string" ? plugin.name : "";
|
|
96
|
+
}
|
|
97
|
+
/** `speed-kit/snippet/configplugins/modifiers/addBlacklist.ts` */
|
|
98
|
+
function addBlacklist(modification, config) {
|
|
99
|
+
const allowed = new Set(Object.values(ConditionKey));
|
|
100
|
+
allowed.add("tags");
|
|
101
|
+
const isValid = modification.every((rule) => {
|
|
102
|
+
if (typeof rule !== "object") {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return Object.keys(rule).every((key) => allowed.has(key));
|
|
106
|
+
});
|
|
107
|
+
if (!isValid) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (!config.blacklist) {
|
|
111
|
+
config.blacklist = modification;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
config.blacklist.push(...modification);
|
|
115
|
+
}
|
|
116
|
+
/** `speed-kit/snippet/configplugins/modifiers/addDisabledScopes.ts` */
|
|
117
|
+
function addDisabledScopes(modification, config) {
|
|
118
|
+
if (typeof modification !== "string" && !Array.isArray(modification)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const scopes = typeof modification === "string" ? [modification] : modification;
|
|
122
|
+
config.disabledScopes = [...(config.disabledScopes || []), ...scopes];
|
|
123
|
+
}
|
|
124
|
+
/** `speed-kit/snippet/configplugins/modifiers/addStripQueryParams.ts` */
|
|
125
|
+
function addStripQueryParams(modification, config) {
|
|
126
|
+
if (!config.stripQueryParams) {
|
|
127
|
+
config.stripQueryParams = modification;
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (isCondition(config.stripQueryParams) && isCondition(modification)) {
|
|
131
|
+
mergeAsCondition(config, modification);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// Conditions and rules must not be mixed: the service worker converts the
|
|
135
|
+
// whole property at once, so both sides become rules here.
|
|
136
|
+
const converted = toStripParamsRules(modification);
|
|
137
|
+
config.stripQueryParams = toStripParamsRules(config.stripQueryParams);
|
|
138
|
+
config.stripQueryParams.push(...converted);
|
|
139
|
+
}
|
|
140
|
+
/** `speed-kit/snippet/configplugins/helpers/isTypeCondition.ts` */
|
|
141
|
+
function isCondition(value) {
|
|
142
|
+
return (typeof value === "string" ||
|
|
143
|
+
types.isRegExp(value) ||
|
|
144
|
+
(Array.isArray(value) &&
|
|
145
|
+
value.every((entry) => typeof entry === "string" || types.isRegExp(entry))));
|
|
146
|
+
}
|
|
147
|
+
/** `speed-kit/snippet/configplugins/helpers/mergeAsCondition.ts` */
|
|
148
|
+
function mergeAsCondition(config, modification) {
|
|
149
|
+
const original = config.stripQueryParams;
|
|
150
|
+
if (!Array.isArray(original)) {
|
|
151
|
+
config.stripQueryParams = Array.isArray(modification)
|
|
152
|
+
? [original, ...modification]
|
|
153
|
+
: [original, modification];
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (Array.isArray(modification)) {
|
|
157
|
+
original.push(...modification);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
original.push(modification);
|
|
161
|
+
}
|
|
162
|
+
/** `speed-kit/snippet/convertToStripParamsRule.ts` */
|
|
163
|
+
function toStripParamsRules(value) {
|
|
164
|
+
if (!value) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
if (!Array.isArray(value)) {
|
|
168
|
+
return [{ params: value }];
|
|
169
|
+
}
|
|
170
|
+
return value.every((entry) => typeof entry === "string" || types.isRegExp(entry))
|
|
171
|
+
? [{ params: value }]
|
|
172
|
+
: value;
|
|
173
|
+
}
|
|
174
|
+
/** `speed-kit/snippet/configplugins/helpers/applyExcludeTag.ts` */
|
|
175
|
+
function applyExcludeTag(rules, exclude, pluginName) {
|
|
176
|
+
return rules.filter((rule) => {
|
|
177
|
+
if (typeof rule?.tags === "string") {
|
|
178
|
+
return !hasMatchingTag(rule.tags, exclude, pluginName);
|
|
179
|
+
}
|
|
180
|
+
if (Array.isArray(rule?.tags) && rule.tags.length > 0) {
|
|
181
|
+
return !rule.tags.some((tag) => hasMatchingTag(tag, exclude, pluginName));
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function hasMatchingTag(ruleTag, excludeTags, pluginName) {
|
|
187
|
+
return excludeTags.some((tag) => {
|
|
188
|
+
const prefix = `${pluginName}-`;
|
|
189
|
+
const baseTag = tag.startsWith(prefix) ? tag.slice(prefix.length) : tag;
|
|
190
|
+
return new RegExp(`^(${pluginName}-)?${baseTag}$`).test(ruleTag);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/** `speed-kit/snippet/configplugins/helpers/prepareRules.ts` */
|
|
194
|
+
function prepareRules(rules, pluginName) {
|
|
195
|
+
for (const rule of rules) {
|
|
196
|
+
if (typeof rule?.tags === "string") {
|
|
197
|
+
rule.tags = prependPluginName(rule.tags, pluginName);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (Array.isArray(rule?.tags) && rule.tags.length > 0) {
|
|
201
|
+
rule.tags = rule.tags.map((tag) => prependPluginName(tag, pluginName));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return rules;
|
|
205
|
+
}
|
|
206
|
+
function prependPluginName(tag, pluginName) {
|
|
207
|
+
return tag.includes(`${pluginName}-`) ? tag : `${pluginName}-${tag}`;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* `speed-kit/snippet/configplugins/helpers/deepCloneES5.ts`
|
|
211
|
+
*
|
|
212
|
+
* Functions are carried over by reference, the way the snippet's clone does —
|
|
213
|
+
* `detectDevice` and a `precache` callback have to survive it.
|
|
214
|
+
*/
|
|
215
|
+
function deepClone(value, sources = [], clones = []) {
|
|
216
|
+
if (value === null || typeof value !== "object") {
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
const seen = sources.indexOf(value);
|
|
220
|
+
if (seen !== -1) {
|
|
221
|
+
return clones[seen];
|
|
222
|
+
}
|
|
223
|
+
// `instanceof` is no use here: the config comes out of a vm, so its dates
|
|
224
|
+
// and regular expressions are another realm's.
|
|
225
|
+
if (types.isDate(value)) {
|
|
226
|
+
return new Date(value.getTime());
|
|
227
|
+
}
|
|
228
|
+
if (types.isRegExp(value)) {
|
|
229
|
+
const source = value;
|
|
230
|
+
const clone = new RegExp(source.source, source.flags);
|
|
231
|
+
clone.lastIndex = source.lastIndex;
|
|
232
|
+
return clone;
|
|
233
|
+
}
|
|
234
|
+
const out = Array.isArray(value) ? [] : {};
|
|
235
|
+
sources.push(value);
|
|
236
|
+
clones.push(out);
|
|
237
|
+
for (const key of Object.keys(value)) {
|
|
238
|
+
out[key] = deepClone(value[key], sources, clones);
|
|
239
|
+
}
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { SpeedKitConfigInterface, VirtualFileInterface, VirtualFileRecipe } from "../integration-api-model.js";
|
|
2
|
+
import { InstallResourceFile } from "./install-resource-file.js";
|
|
3
|
+
import { SpeedKitConfig } from "./speed-kit-config.js";
|
|
4
|
+
/** The name the config deployed to the app is read back under. */
|
|
5
|
+
export declare const REMOTE_SPEED_KIT_CONFIG = "config_SpeedKit@remote";
|
|
6
|
+
/**
|
|
7
|
+
* `config_SpeedKit.js` — an install resource that can also read itself.
|
|
8
|
+
*
|
|
9
|
+
* The file is a program, not a document: it has to be bundled and run against a
|
|
10
|
+
* browser mock before a single rule in it can be looked at. Everyone who wants
|
|
11
|
+
* those rules — the query builder, the pre-deploy checks — asks this file for
|
|
12
|
+
* them through {@link evaluate}, so they all read the same config the same way.
|
|
13
|
+
*/
|
|
14
|
+
export declare class SpeedKitConfigFile extends InstallResourceFile implements SpeedKitConfigInterface {
|
|
15
|
+
/**
|
|
16
|
+
* One evaluation per app and origin. A config branches on
|
|
17
|
+
* `window.location`, so the same file read for two origins is two different
|
|
18
|
+
* configs — and bundling one is expensive enough not to repeat per caller.
|
|
19
|
+
*/
|
|
20
|
+
private readonly evaluations;
|
|
21
|
+
setContent(content: string): void;
|
|
22
|
+
/** The config this file describes for one app and origin. */
|
|
23
|
+
evaluate(app: string, origin: string): Promise<SpeedKitConfig>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A SpeedKit config that has no file on disk behind it — the one deployed to
|
|
27
|
+
* the app, read back from the active install resource. It reads exactly like
|
|
28
|
+
* the local file, which is the point: a check comparing the two compares two
|
|
29
|
+
* configs, not two ways of parsing one.
|
|
30
|
+
*/
|
|
31
|
+
export declare class VirtualSpeedKitConfigFile extends SpeedKitConfigFile implements VirtualFileInterface {
|
|
32
|
+
private recipe;
|
|
33
|
+
/**
|
|
34
|
+
* The SpeedKit config currently live on the app, as a file that evaluates
|
|
35
|
+
* itself.
|
|
36
|
+
*
|
|
37
|
+
* @param content the `config` of the active install resource
|
|
38
|
+
*/
|
|
39
|
+
static createRemote(content: string): Promise<VirtualSpeedKitConfigFile>;
|
|
40
|
+
constructor(name: string, recipe: VirtualFileRecipe, remoteKey?: string);
|
|
41
|
+
buildContent(): Promise<void>;
|
|
42
|
+
updateFile(): Promise<void>;
|
|
43
|
+
updateIfChanged(): Promise<void>;
|
|
44
|
+
delete(): Promise<void>;
|
|
45
|
+
onChange(): void;
|
|
46
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import * as vm from "node:vm";
|
|
2
|
+
import { BundleServiceFactory } from "../../bundler/index.js";
|
|
3
|
+
import { EvaluateSkConfigError } from "../error/evaluate-sk-config-error.js";
|
|
4
|
+
import ApplicationError from "../../error-handling/error/application-error.js";
|
|
5
|
+
import { StaticRecipe } from "../virtual/static-recipe.js";
|
|
6
|
+
import { InstallResourceFile } from "./install-resource-file.js";
|
|
7
|
+
import { SpeedKitConfig } from "./speed-kit-config.js";
|
|
8
|
+
/** The name the config deployed to the app is read back under. */
|
|
9
|
+
export const REMOTE_SPEED_KIT_CONFIG = "config_SpeedKit@remote";
|
|
10
|
+
/**
|
|
11
|
+
* `config_SpeedKit.js` — an install resource that can also read itself.
|
|
12
|
+
*
|
|
13
|
+
* The file is a program, not a document: it has to be bundled and run against a
|
|
14
|
+
* browser mock before a single rule in it can be looked at. Everyone who wants
|
|
15
|
+
* those rules — the query builder, the pre-deploy checks — asks this file for
|
|
16
|
+
* them through {@link evaluate}, so they all read the same config the same way.
|
|
17
|
+
*/
|
|
18
|
+
export class SpeedKitConfigFile extends InstallResourceFile {
|
|
19
|
+
/**
|
|
20
|
+
* One evaluation per app and origin. A config branches on
|
|
21
|
+
* `window.location`, so the same file read for two origins is two different
|
|
22
|
+
* configs — and bundling one is expensive enough not to repeat per caller.
|
|
23
|
+
*/
|
|
24
|
+
evaluations = new Map();
|
|
25
|
+
setContent(content) {
|
|
26
|
+
super.setContent(content);
|
|
27
|
+
this.evaluations.clear();
|
|
28
|
+
}
|
|
29
|
+
/** The config this file describes for one app and origin. */
|
|
30
|
+
evaluate(app, origin) {
|
|
31
|
+
const key = `${app}|${origin}`;
|
|
32
|
+
const cached = this.evaluations.get(key);
|
|
33
|
+
if (cached) {
|
|
34
|
+
return cached;
|
|
35
|
+
}
|
|
36
|
+
const evaluation = evaluateSpeedKitConfig(app, origin, this.getContent(), this.path).catch((error) => {
|
|
37
|
+
// A failure is not an answer: forget it, so the next caller — or the
|
|
38
|
+
// next read after the file was fixed — evaluates again.
|
|
39
|
+
this.evaluations.delete(key);
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
42
|
+
this.evaluations.set(key, evaluation);
|
|
43
|
+
return evaluation;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A SpeedKit config that has no file on disk behind it — the one deployed to
|
|
48
|
+
* the app, read back from the active install resource. It reads exactly like
|
|
49
|
+
* the local file, which is the point: a check comparing the two compares two
|
|
50
|
+
* configs, not two ways of parsing one.
|
|
51
|
+
*/
|
|
52
|
+
export class VirtualSpeedKitConfigFile extends SpeedKitConfigFile {
|
|
53
|
+
recipe;
|
|
54
|
+
/**
|
|
55
|
+
* The SpeedKit config currently live on the app, as a file that evaluates
|
|
56
|
+
* itself.
|
|
57
|
+
*
|
|
58
|
+
* @param content the `config` of the active install resource
|
|
59
|
+
*/
|
|
60
|
+
static async createRemote(content) {
|
|
61
|
+
const file = new VirtualSpeedKitConfigFile(REMOTE_SPEED_KIT_CONFIG, new StaticRecipe(content));
|
|
62
|
+
await file.buildContent();
|
|
63
|
+
return file;
|
|
64
|
+
}
|
|
65
|
+
constructor(name, recipe, remoteKey = "config") {
|
|
66
|
+
super(name, name, "", remoteKey);
|
|
67
|
+
this.recipe = recipe;
|
|
68
|
+
}
|
|
69
|
+
async buildContent() {
|
|
70
|
+
this.setContent(await this.recipe.createContent());
|
|
71
|
+
}
|
|
72
|
+
updateFile() {
|
|
73
|
+
throw new ApplicationError(`${this.name} has no file to write`);
|
|
74
|
+
}
|
|
75
|
+
updateIfChanged() {
|
|
76
|
+
throw new ApplicationError(`${this.name} has no file to write`);
|
|
77
|
+
}
|
|
78
|
+
delete() {
|
|
79
|
+
throw new ApplicationError(`${this.name} has no file to delete`);
|
|
80
|
+
}
|
|
81
|
+
onChange() {
|
|
82
|
+
// nothing on disk to watch
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Run a config source against a browser mock and hand back what it assigned to
|
|
87
|
+
* `window.speedKit`, together with the config plugins it registered.
|
|
88
|
+
*
|
|
89
|
+
* @param app the app the config is evaluated for, exposed to it as `APP`
|
|
90
|
+
* @param origin the origin the config is evaluated for, its `window.location`
|
|
91
|
+
* @param content the config source, unbundled
|
|
92
|
+
* @param filename what an error in the config is reported under
|
|
93
|
+
*/
|
|
94
|
+
async function evaluateSpeedKitConfig(app, origin, content, filename) {
|
|
95
|
+
const browserContext = buildBrowserMock(origin, app);
|
|
96
|
+
const fileContent = safeFileContentWrapper(await bundle(content, app));
|
|
97
|
+
vm.runInNewContext(fileContent, browserContext, {
|
|
98
|
+
filename,
|
|
99
|
+
displayErrors: false,
|
|
100
|
+
breakOnSigint: true,
|
|
101
|
+
});
|
|
102
|
+
if (browserContext.vmError !== null) {
|
|
103
|
+
throw new EvaluateSkConfigError(`[${filename}] ${browserContext.vmError?.message}`);
|
|
104
|
+
}
|
|
105
|
+
return SpeedKitConfig.createFrom(browserContext.window.speedKit, registeredPlugins(browserContext));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The plugins a config registered, read the way `ConfigController.getPlugins`
|
|
109
|
+
* reads them.
|
|
110
|
+
*/
|
|
111
|
+
function registeredPlugins(browserContext) {
|
|
112
|
+
const plugins = browserContext.window.SpeedKit?.configPlugins;
|
|
113
|
+
return Array.isArray(plugins) ? plugins : [];
|
|
114
|
+
}
|
|
115
|
+
function buildBrowserMock(origin, app) {
|
|
116
|
+
const originUrl = new URL(origin);
|
|
117
|
+
const window = {
|
|
118
|
+
location: originUrl,
|
|
119
|
+
APP: app,
|
|
120
|
+
speedKit: {
|
|
121
|
+
split: undefined,
|
|
122
|
+
splitTestId: undefined,
|
|
123
|
+
detectDevice: () => {
|
|
124
|
+
/* empty */
|
|
125
|
+
},
|
|
126
|
+
customVariation: [],
|
|
127
|
+
},
|
|
128
|
+
SpeedKit: {},
|
|
129
|
+
serviceWorkerUrl: "",
|
|
130
|
+
screen: { width: 0, height: 0 },
|
|
131
|
+
devicePixelRatio: 1,
|
|
132
|
+
setTimeout: () => {
|
|
133
|
+
/* empty */
|
|
134
|
+
},
|
|
135
|
+
clearTimeout: () => {
|
|
136
|
+
/* empty */
|
|
137
|
+
},
|
|
138
|
+
addEventListener: () => {
|
|
139
|
+
/* empty */
|
|
140
|
+
},
|
|
141
|
+
removeEventListener: () => {
|
|
142
|
+
/* empty */
|
|
143
|
+
},
|
|
144
|
+
navigator: {
|
|
145
|
+
userAgent: "",
|
|
146
|
+
connection: {},
|
|
147
|
+
cookieEnabled: false,
|
|
148
|
+
languages: [],
|
|
149
|
+
online: true,
|
|
150
|
+
},
|
|
151
|
+
top: {},
|
|
152
|
+
};
|
|
153
|
+
window.top = window;
|
|
154
|
+
// A config may read the DOM while it builds itself — `precache` is
|
|
155
|
+
// frequently a list of script and link hrefs. There is no document here, so
|
|
156
|
+
// these answer the way the DOM answers for a page without matches: an empty
|
|
157
|
+
// list, not `undefined`, which `Array.from` and `forEach` would choke on.
|
|
158
|
+
const document = {
|
|
159
|
+
cookie: "",
|
|
160
|
+
referrer: "",
|
|
161
|
+
getElementById: () => null,
|
|
162
|
+
querySelector: () => null,
|
|
163
|
+
querySelectorAll: () => [],
|
|
164
|
+
createElement: () => {
|
|
165
|
+
/* empty */
|
|
166
|
+
},
|
|
167
|
+
addEventListener: () => {
|
|
168
|
+
/* empty */
|
|
169
|
+
},
|
|
170
|
+
removeEventListener: () => {
|
|
171
|
+
/* empty */
|
|
172
|
+
},
|
|
173
|
+
head: {},
|
|
174
|
+
body: {},
|
|
175
|
+
};
|
|
176
|
+
document.head = document;
|
|
177
|
+
document.body = document;
|
|
178
|
+
return {
|
|
179
|
+
vmError: null,
|
|
180
|
+
...window,
|
|
181
|
+
window,
|
|
182
|
+
document,
|
|
183
|
+
navigator: { userAgent: "" },
|
|
184
|
+
localStorage: {
|
|
185
|
+
getItem: () => {
|
|
186
|
+
/* empty */
|
|
187
|
+
},
|
|
188
|
+
setItem: () => {
|
|
189
|
+
/* empty */
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
JSON: JSON,
|
|
193
|
+
decodeURIComponent: decodeURIComponent,
|
|
194
|
+
URL: URL,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function safeFileContentWrapper(fileContent) {
|
|
198
|
+
return `
|
|
199
|
+
try{
|
|
200
|
+
${fileContent}
|
|
201
|
+
} catch (error){
|
|
202
|
+
vmError = error
|
|
203
|
+
}
|
|
204
|
+
`;
|
|
205
|
+
}
|
|
206
|
+
async function bundle(content, app) {
|
|
207
|
+
if (!content) {
|
|
208
|
+
throw new ApplicationError("Content is empty");
|
|
209
|
+
}
|
|
210
|
+
const bundler = new BundleServiceFactory("./").buildService();
|
|
211
|
+
return bundler.bundle({
|
|
212
|
+
basePaths: {
|
|
213
|
+
"speed-kit-config": "https://www.baqend.com/speed-kit-config/latest/",
|
|
214
|
+
},
|
|
215
|
+
entryPoints: { "config.js": content },
|
|
216
|
+
logLevel: "error",
|
|
217
|
+
minify: false,
|
|
218
|
+
define: {
|
|
219
|
+
APP: JSON.stringify(app),
|
|
220
|
+
},
|
|
221
|
+
platform: "browser",
|
|
222
|
+
});
|
|
223
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ConfigPluginLike } from "./config-plugin.js";
|
|
2
|
+
/**
|
|
3
|
+
* The `window.speedKit` a config assigned, exactly as it wrote it — before
|
|
4
|
+
* anything has been made sense of.
|
|
5
|
+
*/
|
|
6
|
+
export type EvaluatedSpeedKitConfig = Record<string, any>;
|
|
7
|
+
/**
|
|
8
|
+
* One rule of a rule set.
|
|
9
|
+
*
|
|
10
|
+
* A rule is the customer's to write, so a key being present says nothing about
|
|
11
|
+
* what is under it: a condition may be a string, a regular expression, an array
|
|
12
|
+
* of either, or something that is neither. Reading a value out of a rule stays
|
|
13
|
+
* the reader's job — {@link SpeedKitConfig} only promises that what it hands
|
|
14
|
+
* back is a list of objects.
|
|
15
|
+
*/
|
|
16
|
+
export type SpeedKitRule = Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* One entry of `customVariation`, which a pre-deploy check compares by the
|
|
19
|
+
* source of the function it carries.
|
|
20
|
+
*/
|
|
21
|
+
export interface CustomVariationRule extends SpeedKitRule {
|
|
22
|
+
readonly variationFunction?: (...args: unknown[]) => unknown;
|
|
23
|
+
}
|
|
24
|
+
/** The rule-set version a config runs under when it declares none. */
|
|
25
|
+
export declare const DEFAULT_RULE_SET_VERSION = 1;
|
|
26
|
+
/**
|
|
27
|
+
* A Speed Kit config, the way `config_customer.json` has {@link CustomerConfig}.
|
|
28
|
+
*
|
|
29
|
+
* The evaluated config is whatever a customer's JavaScript assigned — a rule
|
|
30
|
+
* set can be missing, or be a string where a list belongs, and every reader had
|
|
31
|
+
* to defend against that on its own. This is where that happens once: the
|
|
32
|
+
* fields below are the shape the CLI reads, and {@link raw} is still there for
|
|
33
|
+
* everything a customer puts in a config that the CLI knows nothing about.
|
|
34
|
+
*/
|
|
35
|
+
export declare class SpeedKitConfig {
|
|
36
|
+
/**
|
|
37
|
+
* The config as the file wrote it: everything `window.speedKit` carried
|
|
38
|
+
* before any plugin ran, including what is not read here. This is the
|
|
39
|
+
* config a human edits, which is what a review of a change looks at.
|
|
40
|
+
*/
|
|
41
|
+
readonly raw: EvaluatedSpeedKitConfig;
|
|
42
|
+
/**
|
|
43
|
+
* The config as Speed Kit runs it: {@link raw} with every config plugin
|
|
44
|
+
* applied. The rule sets below are read from here, because a rule a plugin
|
|
45
|
+
* contributes governs the site exactly like one written into the file.
|
|
46
|
+
*/
|
|
47
|
+
readonly effective: EvaluatedSpeedKitConfig;
|
|
48
|
+
/** The plugins that contributed to {@link effective}, in order. */
|
|
49
|
+
readonly plugins: readonly string[];
|
|
50
|
+
readonly ruleSetVersion: number;
|
|
51
|
+
readonly blacklist: readonly SpeedKitRule[];
|
|
52
|
+
readonly disabledScopes: readonly unknown[];
|
|
53
|
+
readonly disabledSites: readonly SpeedKitRule[];
|
|
54
|
+
readonly enabledSites: readonly SpeedKitRule[];
|
|
55
|
+
/** Either a bare condition or a list of rules scoping the stripping. */
|
|
56
|
+
readonly stripQueryParams: readonly unknown[];
|
|
57
|
+
readonly whitelist: readonly SpeedKitRule[];
|
|
58
|
+
readonly split?: number;
|
|
59
|
+
readonly splitTestId?: string;
|
|
60
|
+
readonly detectDevice?: (...args: unknown[]) => unknown;
|
|
61
|
+
readonly customVariation?: readonly CustomVariationRule[];
|
|
62
|
+
constructor(
|
|
63
|
+
/**
|
|
64
|
+
* The config as the file wrote it: everything `window.speedKit` carried
|
|
65
|
+
* before any plugin ran, including what is not read here. This is the
|
|
66
|
+
* config a human edits, which is what a review of a change looks at.
|
|
67
|
+
*/
|
|
68
|
+
raw: EvaluatedSpeedKitConfig,
|
|
69
|
+
/**
|
|
70
|
+
* The config as Speed Kit runs it: {@link raw} with every config plugin
|
|
71
|
+
* applied. The rule sets below are read from here, because a rule a plugin
|
|
72
|
+
* contributes governs the site exactly like one written into the file.
|
|
73
|
+
*/
|
|
74
|
+
effective: EvaluatedSpeedKitConfig,
|
|
75
|
+
/** The plugins that contributed to {@link effective}, in order. */
|
|
76
|
+
plugins: readonly string[], ruleSetVersion: number, blacklist: readonly SpeedKitRule[], disabledScopes: readonly unknown[], disabledSites: readonly SpeedKitRule[], enabledSites: readonly SpeedKitRule[],
|
|
77
|
+
/** Either a bare condition or a list of rules scoping the stripping. */
|
|
78
|
+
stripQueryParams: readonly unknown[], whitelist: readonly SpeedKitRule[], split?: number, splitTestId?: string, detectDevice?: (...args: unknown[]) => unknown, customVariation?: readonly CustomVariationRule[]);
|
|
79
|
+
/**
|
|
80
|
+
* @param evaluated what the config assigned to `window.speedKit`
|
|
81
|
+
* @param plugins what it registered on `window.SpeedKit.configPlugins`
|
|
82
|
+
*/
|
|
83
|
+
static createFrom(evaluated: EvaluatedSpeedKitConfig, plugins?: readonly ConfigPluginLike[]): SpeedKitConfig;
|
|
84
|
+
}
|