@speedkit/cli 4.20.3 → 4.20.5
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/integration-api/spec/file-watch.spec.d.ts +1 -0
- package/dist/services/integration-api/spec/file-watch.spec.js +81 -0
- package/dist/services/integration-api/struct/file.js +17 -5
- package/dist/services/onboarding/browser/baqend-response.js +21 -3
- package/dist/services/onboarding/fetch-event-handler.js +10 -3
- package/dist/services/onboarding/fetch-events/speed-kit-service-worker-js.d.ts +8 -0
- package/dist/services/onboarding/fetch-events/speed-kit-service-worker-js.js +9 -0
- package/dist/services/onboarding/onboarding-model.d.ts +1 -0
- package/dist/services/onboarding/onboarding-model.js +1 -0
- package/dist/services/onboarding/onboarding-service-factory.js +5 -4
- package/dist/services/onboarding/onboarding-service.d.ts +25 -1
- package/dist/services/onboarding/onboarding-service.js +70 -1
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [4.20.5](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.20.4...v4.20.5) (2026-08-10)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **onboarding:** keep watching a config file after git replaces it ([ef2571a](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/ef2571a5fa07862382c338096e3311c107f20a74))
|
|
7
|
+
|
|
8
|
+
## [4.20.4](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.20.3...v4.20.4) (2026-08-10)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **onboarding:** detect and repair deployed service worker takeovers ([6d61ae7](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/6d61ae71bab7dc2bbd4926d0c17fc8505d7f523e))
|
|
14
|
+
|
|
1
15
|
## [4.20.3](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.20.2...v4.20.3) (2026-08-07)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import { describe, it, afterEach } from "mocha";
|
|
3
|
+
import { mkdtempSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
// via the model barrel, not struct/file.js directly: the struct modules form an
|
|
7
|
+
// import cycle that only resolves in this order.
|
|
8
|
+
import { File } from "../integration-api-model.js";
|
|
9
|
+
// chokidar needs a real filesystem, so these run against a temp dir rather than memfs.
|
|
10
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
+
/** Wait until the change callback has fired at least `count` times. */
|
|
12
|
+
async function waitForCalls(getCalls, count, timeoutMs = 5000) {
|
|
13
|
+
const deadline = Date.now() + timeoutMs;
|
|
14
|
+
while (Date.now() < deadline) {
|
|
15
|
+
if (getCalls() >= count)
|
|
16
|
+
return;
|
|
17
|
+
await delay(50);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
describe("File onChange", function () {
|
|
21
|
+
// real filesystem events plus chokidar's write-finish debounce
|
|
22
|
+
this.timeout(20000);
|
|
23
|
+
let dir;
|
|
24
|
+
let controller;
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
controller?.abort();
|
|
27
|
+
controller = undefined;
|
|
28
|
+
if (dir) {
|
|
29
|
+
rmSync(dir, { recursive: true, force: true });
|
|
30
|
+
dir = undefined;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
it("keeps reporting edits after the file is replaced instead of written in place", async () => {
|
|
34
|
+
dir = mkdtempSync(join(tmpdir(), "sk-file-watch-"));
|
|
35
|
+
const path = join(dir, "config_documentHandler.js");
|
|
36
|
+
writeFileSync(path, "v0");
|
|
37
|
+
const file = new File("config_documentHandler", path, "v0");
|
|
38
|
+
controller = new AbortController();
|
|
39
|
+
let calls = 0;
|
|
40
|
+
file.onChange(async () => {
|
|
41
|
+
calls += 1;
|
|
42
|
+
}, { signal: controller.signal });
|
|
43
|
+
await delay(700); // let the watcher attach
|
|
44
|
+
writeFileSync(path, "v1");
|
|
45
|
+
await waitForCalls(() => calls, 1);
|
|
46
|
+
expect(calls, "an in-place write is reported").to.be.at.least(1);
|
|
47
|
+
// git checkout / reset / rebase / stash / pull replace the file rather than
|
|
48
|
+
// writing into it. A single-file watch stops firing for good at this point.
|
|
49
|
+
const callsBeforeReplace = calls;
|
|
50
|
+
unlinkSync(path);
|
|
51
|
+
writeFileSync(path, "v2");
|
|
52
|
+
await waitForCalls(() => calls, callsBeforeReplace + 1);
|
|
53
|
+
expect(calls, "the replacement itself is reported").to.be.at.least(callsBeforeReplace + 1);
|
|
54
|
+
// The regression: every edit after the replacement used to be dropped, so
|
|
55
|
+
// onboarding kept serving the previous build until it was restarted.
|
|
56
|
+
const callsAfterReplace = calls;
|
|
57
|
+
writeFileSync(path, "v3");
|
|
58
|
+
await waitForCalls(() => calls, callsAfterReplace + 1);
|
|
59
|
+
expect(calls, "an edit after the replacement is still reported").to.be.at.least(callsAfterReplace + 1);
|
|
60
|
+
});
|
|
61
|
+
it("ignores changes to sibling files in the same directory", async () => {
|
|
62
|
+
dir = mkdtempSync(join(tmpdir(), "sk-file-watch-"));
|
|
63
|
+
const path = join(dir, "config_documentHandler.js");
|
|
64
|
+
const sibling = join(dir, "config_SpeedKit.js");
|
|
65
|
+
writeFileSync(path, "v0");
|
|
66
|
+
writeFileSync(sibling, "other");
|
|
67
|
+
const file = new File("config_documentHandler", path, "v0");
|
|
68
|
+
controller = new AbortController();
|
|
69
|
+
let calls = 0;
|
|
70
|
+
file.onChange(async () => {
|
|
71
|
+
calls += 1;
|
|
72
|
+
}, { signal: controller.signal });
|
|
73
|
+
await delay(700);
|
|
74
|
+
writeFileSync(sibling, "other-changed");
|
|
75
|
+
await delay(900);
|
|
76
|
+
expect(calls, "a sibling write is not reported").to.equal(0);
|
|
77
|
+
writeFileSync(path, "v1");
|
|
78
|
+
await waitForCalls(() => calls, 1);
|
|
79
|
+
expect(calls, "the watched file is still reported").to.be.at.least(1);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { readFile, unlink, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
2
3
|
import { watch as chokidarWatch } from "chokidar";
|
|
3
4
|
import { FILE_TYPE, } from "../integration-api-model.js";
|
|
4
5
|
export class File {
|
|
@@ -47,15 +48,26 @@ export class File {
|
|
|
47
48
|
if (signal?.aborted) {
|
|
48
49
|
return;
|
|
49
50
|
}
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
|
|
51
|
+
// Watch the containing directory, not the file: a single-file watch stops
|
|
52
|
+
// firing for good once the file is *replaced* rather than written in place,
|
|
53
|
+
// which is what git does on checkout/reset/rebase/stash/pull. The replace
|
|
54
|
+
// itself still arrives, so the breakage is silent — every later edit is
|
|
55
|
+
// dropped, no rebuild and no cache clear run, and onboarding keeps serving
|
|
56
|
+
// the previous build until it is restarted.
|
|
57
|
+
// depth 0 keeps this to the directory's own entries, and chokidar's atomic
|
|
58
|
+
// option still folds write-then-rename (Claude Code, VS Code atomicSave,
|
|
59
|
+
// vim writebackup, …) into a single change event.
|
|
60
|
+
const target = resolve(this.path);
|
|
61
|
+
const watcher = chokidarWatch(dirname(target), {
|
|
54
62
|
ignoreInitial: true,
|
|
55
63
|
atomic: true,
|
|
64
|
+
depth: 0,
|
|
56
65
|
awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 50 },
|
|
57
66
|
});
|
|
58
|
-
const handle = async () => {
|
|
67
|
+
const handle = async (changedPath) => {
|
|
68
|
+
if (resolve(changedPath) !== target) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
59
71
|
const content = await readFile(this.path, "utf-8");
|
|
60
72
|
if (content === this.content) {
|
|
61
73
|
return;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes a value safe for Fetch.fulfillRequest: collapse line breaks and strip
|
|
3
|
+
* characters outside latin-1, either of which Chrome rejects as "Invalid header".
|
|
4
|
+
*/
|
|
5
|
+
function sanitizeHeaderValue(value) {
|
|
6
|
+
return String(value)
|
|
7
|
+
.replace(/[\r\n]+/g, " ")
|
|
8
|
+
.replace(/[^\x20-\xff\t]/g, "?");
|
|
9
|
+
}
|
|
1
10
|
const DEFAULT_HEADERS = [
|
|
2
11
|
{ name: "Date", value: new Date().toUTCString() },
|
|
3
12
|
{ name: "Content-Encoding", value: "identity" },
|
|
@@ -24,19 +33,28 @@ export class BaqendResponse {
|
|
|
24
33
|
: Buffer.from(body).toString("base64");
|
|
25
34
|
}
|
|
26
35
|
setHeader(name, value) {
|
|
36
|
+
const sanitized = sanitizeHeaderValue(value);
|
|
27
37
|
for (const header of this.responseHeaders) {
|
|
28
38
|
if (header.name === name) {
|
|
29
|
-
header.value =
|
|
39
|
+
header.value = sanitized;
|
|
30
40
|
return;
|
|
31
41
|
}
|
|
32
42
|
}
|
|
33
|
-
this.responseHeaders.push({ name, value });
|
|
43
|
+
this.responseHeaders.push({ name, value: sanitized });
|
|
34
44
|
}
|
|
35
45
|
addCustomHeaders(headers) {
|
|
46
|
+
// Fetch.fulfillRequest rejects header values with line breaks or
|
|
47
|
+
// non-latin-1 characters ("Invalid header") — e.g. an error message with
|
|
48
|
+
// a newline placed into x-error left the request hanging in paused state.
|
|
49
|
+
for (const header of headers) {
|
|
50
|
+
header.value = sanitizeHeaderValue(header.value);
|
|
51
|
+
}
|
|
36
52
|
if (!headers?.find((o) => o.name === "Cache-Control")) {
|
|
37
53
|
headers.push({
|
|
38
54
|
name: "Cache-Control",
|
|
39
|
-
|
|
55
|
+
// NOTE: was "nocache, nostore" — invalid directive names that browsers
|
|
56
|
+
// ignore, leaving locally served responses cacheable.
|
|
57
|
+
value: "max-age=0, no-cache, no-store",
|
|
40
58
|
});
|
|
41
59
|
}
|
|
42
60
|
const origin = headers?.find((o) => o.name.toLowerCase() === "origin")?.value;
|
|
@@ -41,10 +41,17 @@ export class FetchEventHandler {
|
|
|
41
41
|
if (response.errorObj) {
|
|
42
42
|
this.cli.writeError(`error on ${event.request.url}`);
|
|
43
43
|
this.cli.writeError(`${response.errorObj.message}`);
|
|
44
|
-
return;
|
|
45
44
|
}
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
else {
|
|
46
|
+
this.cli.writeError(`error on ${target.url()}`);
|
|
47
|
+
this.cli.writeError(JSON.stringify(response.errorObj));
|
|
48
|
+
}
|
|
49
|
+
// A request left in paused state hangs until the browser times out.
|
|
50
|
+
// Fail it explicitly so the page sees a network error instead.
|
|
51
|
+
await safe(cdpSession.send("Fetch.failRequest", {
|
|
52
|
+
requestId: event.requestId,
|
|
53
|
+
errorReason: "Failed",
|
|
54
|
+
}));
|
|
48
55
|
});
|
|
49
56
|
}
|
|
50
57
|
canHandlePattern(fetchEventHandler, url, resourceType, requestStage) {
|
|
@@ -11,6 +11,14 @@ export declare class SpeedKitServiceWorkerJs implements FetchRequestPausedEventH
|
|
|
11
11
|
private customerConfig;
|
|
12
12
|
private serviceWorker;
|
|
13
13
|
readonly patterns: Protocol.Fetch.RequestPattern[];
|
|
14
|
+
/**
|
|
15
|
+
* Timestamp of the last locally served sw.js. Chrome's SW update checker
|
|
16
|
+
* fetches scripts in the browser process, outside CDP Fetch interception —
|
|
17
|
+
* an update that slips through installs the DEPLOYED build. A SW version
|
|
18
|
+
* that activates without a recent local serve is such a takeover; the
|
|
19
|
+
* OnboardingService uses this timestamp to detect and repair it.
|
|
20
|
+
*/
|
|
21
|
+
lastServedAt: number;
|
|
14
22
|
constructor(customerConfig: CustomerConfig, serviceWorker: FileInterface);
|
|
15
23
|
handle(): Promise<Partial<Protocol.Fetch.FulfillRequestRequest>>;
|
|
16
24
|
}
|
|
@@ -9,6 +9,14 @@ export class SpeedKitServiceWorkerJs {
|
|
|
9
9
|
customerConfig;
|
|
10
10
|
serviceWorker;
|
|
11
11
|
patterns;
|
|
12
|
+
/**
|
|
13
|
+
* Timestamp of the last locally served sw.js. Chrome's SW update checker
|
|
14
|
+
* fetches scripts in the browser process, outside CDP Fetch interception —
|
|
15
|
+
* an update that slips through installs the DEPLOYED build. A SW version
|
|
16
|
+
* that activates without a recent local serve is such a takeover; the
|
|
17
|
+
* OnboardingService uses this timestamp to detect and repair it.
|
|
18
|
+
*/
|
|
19
|
+
lastServedAt = 0;
|
|
12
20
|
constructor(customerConfig, serviceWorker) {
|
|
13
21
|
this.customerConfig = customerConfig;
|
|
14
22
|
this.serviceWorker = serviceWorker;
|
|
@@ -20,6 +28,7 @@ export class SpeedKitServiceWorkerJs {
|
|
|
20
28
|
];
|
|
21
29
|
}
|
|
22
30
|
async handle() {
|
|
31
|
+
this.lastServedAt = Date.now();
|
|
23
32
|
return new BaqendResponse(this.serviceWorker.getContent(), "text/javascript; charset=utf-8");
|
|
24
33
|
}
|
|
25
34
|
}
|
|
@@ -83,9 +83,10 @@ export class OnboardingServiceFactory {
|
|
|
83
83
|
const fileWatcher = new FileWatcher(cli, customerConfig, files, documentHandler, browserContext, cache);
|
|
84
84
|
const athenaClient = (await this.getAthenaClient(customerConfig.app)) || null;
|
|
85
85
|
const messageHandler = await this.prepareMessagehandler(athenaClient, files, customerConfig);
|
|
86
|
-
const
|
|
86
|
+
const speedKitServiceWorkerJs = new SpeedKitServiceWorkerJs(customerConfig, files.getByName(INTEGRATION_FILES.CUSTOM.SW));
|
|
87
|
+
const fetchHandler = await this.getFetchEventHandler(files, customerConfig, documentHandler, cache, cli, browserContext, athenaClient, messageHandler, fileWatcher, speedKitServiceWorkerJs);
|
|
87
88
|
const extensionBridge = new ExtensionBridge(cli);
|
|
88
|
-
return new OnboardingService(browserContext, customerConfig, fetchHandler, fileWatcher, cli, messageHandler, extensionBridge);
|
|
89
|
+
return new OnboardingService(browserContext, customerConfig, fetchHandler, fileWatcher, cli, messageHandler, extensionBridge, speedKitServiceWorkerJs);
|
|
89
90
|
}
|
|
90
91
|
async prepareMessagehandler(athenaClient, files, customerConfig) {
|
|
91
92
|
const messageHandler = new DevtoolsExtensionApi();
|
|
@@ -145,7 +146,7 @@ export class OnboardingServiceFactory {
|
|
|
145
146
|
const configApiContext = new ConfigApiContext(app);
|
|
146
147
|
return new ConfigApiServiceFactory(configApiContext).getService();
|
|
147
148
|
}
|
|
148
|
-
async getFetchEventHandler(files, customerConfig, documentHandler, cache, cli, browserContext, athenaClient, messageHandler, fileWatcher) {
|
|
149
|
+
async getFetchEventHandler(files, customerConfig, documentHandler, cache, cli, browserContext, athenaClient, messageHandler, fileWatcher, speedKitServiceWorkerJs) {
|
|
149
150
|
const configApi = this.getConfigApi(customerConfig.app);
|
|
150
151
|
const serverConfig = await this.getSpeedKitServerConfig(configApi, cli);
|
|
151
152
|
const agent = this.getCrawlerAgent(customerConfig, cli);
|
|
@@ -162,7 +163,7 @@ export class OnboardingServiceFactory {
|
|
|
162
163
|
new SpeedKitInstallHtml(customerConfig, browserContext, new InstallSpeedKitHtmlTemplate()),
|
|
163
164
|
new SpeedKitInstallJs(customerConfig, files.getByName(INTEGRATION_FILES.BUILD.INSTALL)),
|
|
164
165
|
new CustomerServiceWorkerJs(customerConfig),
|
|
165
|
-
|
|
166
|
+
speedKitServiceWorkerJs,
|
|
166
167
|
new DashboardRequest(new Dashboard(customerConfig, parameterQueryBuilder, athenaClient, requestDiffService, new DiffAgainstCurrentPage(DiffService, documentHandler, crawler, cli), cache)),
|
|
167
168
|
];
|
|
168
169
|
if (this.context.local) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CustomerConfig } from "../integration-api/index.js";
|
|
2
2
|
import { BrowserContext } from "./onboarding-model.js";
|
|
3
3
|
import { FetchEventHandler } from "./fetch-event-handler.js";
|
|
4
|
+
import { SpeedKitServiceWorkerJs } from "./fetch-events/speed-kit-service-worker-js.js";
|
|
4
5
|
import { FileWatcher } from "./file-events/file-watcher.js";
|
|
5
6
|
import { CliService } from "../cli/index.js";
|
|
6
7
|
import { DevtoolsExtensionApi } from "./browser/extension/devtools-extension-api.js";
|
|
@@ -16,8 +17,11 @@ export declare class OnboardingService {
|
|
|
16
17
|
private cli;
|
|
17
18
|
private developmentToolsMessages;
|
|
18
19
|
private extensionBridge;
|
|
20
|
+
private speedKitServiceWorkerJs;
|
|
19
21
|
private reinstallOnNextNavigate;
|
|
20
|
-
|
|
22
|
+
private lastForeignSwReinstall;
|
|
23
|
+
private readonly activatedSwVersions;
|
|
24
|
+
constructor(browserContext: BrowserContext, customerConfig: CustomerConfig, fetchEventHandler: FetchEventHandler, fileWatcher: FileWatcher, cli: CliService, developmentToolsMessages: DevtoolsExtensionApi, extensionBridge: ExtensionBridge, speedKitServiceWorkerJs: SpeedKitServiceWorkerJs);
|
|
21
25
|
/**
|
|
22
26
|
* Prepare the puppeteer browser, wire event listeners, navigate to the
|
|
23
27
|
* install page, then block until either the caller aborts the signal
|
|
@@ -37,6 +41,26 @@ export declare class OnboardingService {
|
|
|
37
41
|
private attachExtensionBridge;
|
|
38
42
|
private findPage;
|
|
39
43
|
private prepareCleanState;
|
|
44
|
+
/**
|
|
45
|
+
* Detects when the DEPLOYED service worker takes over the local session and
|
|
46
|
+
* repairs it by reinstalling through the install page.
|
|
47
|
+
*
|
|
48
|
+
* Chrome's SW update checker fetches the wrapper and its imported sw.js in
|
|
49
|
+
* the browser process, where CDP Fetch interception does not apply — those
|
|
50
|
+
* fetches reach production. Because the production wrapper differs from the
|
|
51
|
+
* locally served one, any update check that slips through installs the
|
|
52
|
+
* deployed build. With static routing (`activateStaticRouting`), that is no
|
|
53
|
+
* longer harmless: routes are frozen at install time from the deployed
|
|
54
|
+
* config, so pages only enabled locally bypass the SW entirely (symptom:
|
|
55
|
+
* `responseCause: "SwBooting"` on every navigation of those pages).
|
|
56
|
+
*
|
|
57
|
+
* Only page-initiated `register()` script fetches are interceptable, so a
|
|
58
|
+
* version that activates without `SpeedKitServiceWorkerJs` having served
|
|
59
|
+
* sw.js just before must be a takeover — reinstall via the install page,
|
|
60
|
+
* which goes through the intercepted path again.
|
|
61
|
+
*/
|
|
62
|
+
private registerForeignSwGuard;
|
|
63
|
+
private isForeignSwVersion;
|
|
40
64
|
private reinstallSpeedKit;
|
|
41
65
|
private registerFileWatcher;
|
|
42
66
|
private registerReinstallSpeedKitOnNavigate;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import puppeteerExtra from "puppeteer-extra";
|
|
2
2
|
import { BROWSER_EVENTS, CDP_SESSION, } from "./onboarding-model.js";
|
|
3
3
|
import { safe } from "../../helpers/safe.js";
|
|
4
|
+
// A SW version activating later than this after the last locally served sw.js
|
|
5
|
+
// cannot originate from our interception — it is a deployed-build takeover.
|
|
6
|
+
const LOCAL_SW_SERVE_GRACE_MS = 10_000;
|
|
7
|
+
// Takeovers can retrigger on every update check — don't reinstall in a tight loop.
|
|
8
|
+
const FOREIGN_SW_REINSTALL_COOLDOWN_MS = 30_000;
|
|
4
9
|
/**
|
|
5
10
|
* Represents the OnboardingService which is responsible for setting up the browser to test a config using Puppeteer.
|
|
6
11
|
*/
|
|
@@ -12,8 +17,11 @@ export class OnboardingService {
|
|
|
12
17
|
cli;
|
|
13
18
|
developmentToolsMessages;
|
|
14
19
|
extensionBridge;
|
|
20
|
+
speedKitServiceWorkerJs;
|
|
15
21
|
reinstallOnNextNavigate = false;
|
|
16
|
-
|
|
22
|
+
lastForeignSwReinstall = 0;
|
|
23
|
+
activatedSwVersions = new Set();
|
|
24
|
+
constructor(browserContext, customerConfig, fetchEventHandler, fileWatcher, cli, developmentToolsMessages, extensionBridge, speedKitServiceWorkerJs) {
|
|
17
25
|
this.browserContext = browserContext;
|
|
18
26
|
this.customerConfig = customerConfig;
|
|
19
27
|
this.fetchEventHandler = fetchEventHandler;
|
|
@@ -21,6 +29,7 @@ export class OnboardingService {
|
|
|
21
29
|
this.cli = cli;
|
|
22
30
|
this.developmentToolsMessages = developmentToolsMessages;
|
|
23
31
|
this.extensionBridge = extensionBridge;
|
|
32
|
+
this.speedKitServiceWorkerJs = speedKitServiceWorkerJs;
|
|
24
33
|
}
|
|
25
34
|
/**
|
|
26
35
|
* Prepare the puppeteer browser, wire event listeners, navigate to the
|
|
@@ -102,6 +111,7 @@ export class OnboardingService {
|
|
|
102
111
|
});
|
|
103
112
|
await cdpSession.send("Emulation.clearDeviceMetricsOverride");
|
|
104
113
|
await cdpSession.send("Emulation.clearGeolocationOverride");
|
|
114
|
+
this.registerForeignSwGuard(page, cdpSession);
|
|
105
115
|
safe(() => {
|
|
106
116
|
cdpSession.on(CDP_SESSION.EVENTS.SERVICE_WORKER.REGISTRATION_UPDATED, ({ registrations, }) => {
|
|
107
117
|
for (const registration of registrations) {
|
|
@@ -116,6 +126,65 @@ export class OnboardingService {
|
|
|
116
126
|
});
|
|
117
127
|
});
|
|
118
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Detects when the DEPLOYED service worker takes over the local session and
|
|
131
|
+
* repairs it by reinstalling through the install page.
|
|
132
|
+
*
|
|
133
|
+
* Chrome's SW update checker fetches the wrapper and its imported sw.js in
|
|
134
|
+
* the browser process, where CDP Fetch interception does not apply — those
|
|
135
|
+
* fetches reach production. Because the production wrapper differs from the
|
|
136
|
+
* locally served one, any update check that slips through installs the
|
|
137
|
+
* deployed build. With static routing (`activateStaticRouting`), that is no
|
|
138
|
+
* longer harmless: routes are frozen at install time from the deployed
|
|
139
|
+
* config, so pages only enabled locally bypass the SW entirely (symptom:
|
|
140
|
+
* `responseCause: "SwBooting"` on every navigation of those pages).
|
|
141
|
+
*
|
|
142
|
+
* Only page-initiated `register()` script fetches are interceptable, so a
|
|
143
|
+
* version that activates without `SpeedKitServiceWorkerJs` having served
|
|
144
|
+
* sw.js just before must be a takeover — reinstall via the install page,
|
|
145
|
+
* which goes through the intercepted path again.
|
|
146
|
+
*/
|
|
147
|
+
registerForeignSwGuard(page, cdpSession) {
|
|
148
|
+
cdpSession.on(CDP_SESSION.EVENTS.SERVICE_WORKER.VERSION_UPDATED, async ({ versions, }) => {
|
|
149
|
+
for (const version of versions) {
|
|
150
|
+
if (!this.isForeignSwVersion(version)) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
this.cli.writeWarning(`${this.cli.style.yellow("Deployed Service Worker took over")} (version ${version.versionId}) — an update check bypassed the local interception. Reinstalling the local build.`);
|
|
154
|
+
this.developmentToolsMessages.addMessage({
|
|
155
|
+
id: "serviceWorker",
|
|
156
|
+
data: `Deployed Service Worker took over (version ${version.versionId}); reinstalling the local build`,
|
|
157
|
+
});
|
|
158
|
+
if (Date.now() - this.lastForeignSwReinstall <
|
|
159
|
+
FOREIGN_SW_REINSTALL_COOLDOWN_MS) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
this.lastForeignSwReinstall = Date.now();
|
|
163
|
+
await safe(this.reinstallSpeedKit(page, new URL(page.url()).pathname));
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
isForeignSwVersion(version) {
|
|
168
|
+
if (version.status !== "activated") {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
// Only guard the Speed Kit wrapper; ignore the empty SWs on disabledScopes
|
|
172
|
+
// and any site-own service workers.
|
|
173
|
+
if (!version.scriptURL.includes(this.customerConfig.swPath)) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (version.scriptURL.includes("empty=1")) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
// Activation events repeat per version (runningStatus changes) — handle each version once.
|
|
180
|
+
if (this.activatedSwVersions.has(version.versionId)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
this.activatedSwVersions.add(version.versionId);
|
|
184
|
+
// A locally served sw.js right before the activation means we installed it.
|
|
185
|
+
return (Date.now() - this.speedKitServiceWorkerJs.lastServedAt >
|
|
186
|
+
LOCAL_SW_SERVE_GRACE_MS);
|
|
187
|
+
}
|
|
119
188
|
async reinstallSpeedKit(page, pathname) {
|
|
120
189
|
const url = new URL(page.url());
|
|
121
190
|
let target = url.href;
|
package/oclif.manifest.json
CHANGED