@speedkit/cli 4.50.2 → 4.50.4
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/dist/helpers/origin-proxy.js +43 -1
- package/dist/helpers/spec/origin-proxy.spec.js +53 -0
- package/dist/services/origin-request/asset-variations.d.ts +1 -1
- package/dist/services/origin-request/asset-variations.js +3 -1
- package/dist/services/origin-request/origin-request-service-factory.js +2 -6
- package/dist/services/origin-request/spec/asset-variations.spec.js +9 -0
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [4.50.4](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.50.3...v4.50.4) (2026-11-02)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **origin-request:** run without configured variations and map desktop to default ([1d7306d](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/1d7306d79cc6ef0c1acaeeb2b7017d2488448322))
|
|
7
|
+
|
|
8
|
+
## [4.50.3](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.50.2...v4.50.3) (2026-11-02)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **origin-request:** send the host header of an origin map entry ([22eff25](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/22eff257fbb89c6d2bb64d36f947cd39bb35e738))
|
|
14
|
+
|
|
1
15
|
## [4.50.2](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v4.50.1...v4.50.2) (2026-10-29)
|
|
2
16
|
|
|
3
17
|
|
|
@@ -195,7 +195,11 @@ export function originFetch(url, init, options = {}) {
|
|
|
195
195
|
export function directOriginFetch(url, init, options = {}) {
|
|
196
196
|
const sent = new Headers(init?.headers);
|
|
197
197
|
const added = FETCH_DEFAULT_HEADERS.filter((name) => !sent.has(name));
|
|
198
|
-
const
|
|
198
|
+
const host = sent.get("host");
|
|
199
|
+
const interceptors = [withoutHeaders(added)];
|
|
200
|
+
if (host)
|
|
201
|
+
interceptors.push(withHost(host, new URL(url).origin));
|
|
202
|
+
const dispatcher = originDispatcher(options.tolerateLegacyTls === true).compose(interceptors);
|
|
199
203
|
const request = { ...init, dispatcher };
|
|
200
204
|
return undiciFetch(url, request);
|
|
201
205
|
}
|
|
@@ -216,6 +220,44 @@ function withoutHeaders(names) {
|
|
|
216
220
|
return dispatch({ ...opts, headers: Object.fromEntries(kept) }, handler);
|
|
217
221
|
};
|
|
218
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Sends the caller's `host` header, which fetch drops as a forbidden header. An origin map entry
|
|
225
|
+
* with `overrideHostHeader` needs it, because its reverse proxy routes by the original host.
|
|
226
|
+
*
|
|
227
|
+
* Only requests to `origin` get it, so a redirect to another origin keeps its own host.
|
|
228
|
+
*
|
|
229
|
+
* The TLS servername stays the connected host. Undici derives it from the `host` header otherwise,
|
|
230
|
+
* and the certificate of the mapped origin does not cover the original host.
|
|
231
|
+
*/
|
|
232
|
+
function withHost(host, origin) {
|
|
233
|
+
return (dispatch) => (opts, handler) => {
|
|
234
|
+
const connected = new URL(opts.origin);
|
|
235
|
+
if (connected.origin !== origin)
|
|
236
|
+
return dispatch(opts, handler);
|
|
237
|
+
const headers = Object.fromEntries(headerEntries(opts.headers).filter(([name]) => name.toLowerCase() !== "host"));
|
|
238
|
+
headers.host = host;
|
|
239
|
+
// Undici reads `servername` per request, but its types do not declare it.
|
|
240
|
+
const next = {
|
|
241
|
+
...opts,
|
|
242
|
+
headers,
|
|
243
|
+
servername: connected.hostname,
|
|
244
|
+
};
|
|
245
|
+
return dispatch(next, handler);
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
/** Reads dispatch headers, which arrive as a plain object or as a flat name-value array. */
|
|
249
|
+
function headerEntries(headers) {
|
|
250
|
+
if (!headers)
|
|
251
|
+
return [];
|
|
252
|
+
if (Array.isArray(headers)) {
|
|
253
|
+
const entries = [];
|
|
254
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
255
|
+
entries.push([String(headers[i]), String(headers[i + 1])]);
|
|
256
|
+
}
|
|
257
|
+
return entries;
|
|
258
|
+
}
|
|
259
|
+
return Object.entries(headers).map(([name, value]) => [name, String(value)]);
|
|
260
|
+
}
|
|
219
261
|
function currentState() {
|
|
220
262
|
if (!state) {
|
|
221
263
|
applyOriginProxy();
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { expect } from "chai";
|
|
2
2
|
import { describe, beforeEach, afterEach, it } from "mocha";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
3
4
|
import http from "node:http";
|
|
5
|
+
import https from "node:https";
|
|
6
|
+
import { join, dirname } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
4
8
|
import zlib from "node:zlib";
|
|
5
9
|
import { applyOriginProxy, chromeProxyFlag, findProxyInChromeFlags, originFetch, parseSocksProxy, PROXY_ENVIRONMENT_VARIABLE, resetOriginProxy, setOriginProxy, withOriginProxyFlag, } from "../origin-proxy.js";
|
|
10
|
+
/** Self-signed for `localhost`, only to serve the TLS spec. */
|
|
11
|
+
const FIXTURES = join(dirname(fileURLToPath(import.meta.url)), "fixtures");
|
|
12
|
+
const LOCALHOST_KEY = readFileSync(join(FIXTURES, "localhost.key"), "utf8");
|
|
13
|
+
const LOCALHOST_CERT = readFileSync(join(FIXTURES, "localhost.crt"), "utf8");
|
|
6
14
|
/** Sets or clears the enforcing environment variable. */
|
|
7
15
|
function setEnvironmentProxy(value) {
|
|
8
16
|
if (value === undefined) {
|
|
@@ -168,6 +176,10 @@ describe("origin proxy", () => {
|
|
|
168
176
|
if (request.url === "/redirect") {
|
|
169
177
|
response.writeHead(302, { location: "/" }).end();
|
|
170
178
|
}
|
|
179
|
+
else if (request.url.startsWith("/redirect-to?")) {
|
|
180
|
+
const location = decodeURIComponent(request.url.split("?")[1]);
|
|
181
|
+
response.writeHead(302, { location }).end();
|
|
182
|
+
}
|
|
171
183
|
else if (request.url === "/missing") {
|
|
172
184
|
response.writeHead(404).end(body);
|
|
173
185
|
}
|
|
@@ -208,6 +220,22 @@ describe("origin proxy", () => {
|
|
|
208
220
|
expect(headers["accept-language"]).to.equal("en-GB");
|
|
209
221
|
expect(headers.accept).to.equal("text/html");
|
|
210
222
|
});
|
|
223
|
+
it("sends the host header the caller sets", async () => {
|
|
224
|
+
const headers = await received("", {
|
|
225
|
+
headers: { Host: "www.example.com" },
|
|
226
|
+
});
|
|
227
|
+
expect(headers.host).to.equal("www.example.com");
|
|
228
|
+
});
|
|
229
|
+
it("keeps the host header off a redirect to another origin", async () => {
|
|
230
|
+
const other = echoUrl.replace("127.0.0.1", "localhost");
|
|
231
|
+
const response = await originFetch(`${echoUrl}redirect-to?${encodeURIComponent(other)}`, { headers: { host: "www.example.com" } });
|
|
232
|
+
expect(response.url).to.equal(other);
|
|
233
|
+
expect((await response.json()).host).to.equal(new URL(other).host);
|
|
234
|
+
});
|
|
235
|
+
it("sends the url's host without a host header", async () => {
|
|
236
|
+
const headers = await received();
|
|
237
|
+
expect(headers.host).to.equal(new URL(echoUrl).host);
|
|
238
|
+
});
|
|
211
239
|
it("drops the defaults when legacy tls is tolerated as well", async () => {
|
|
212
240
|
const headers = await received("", undefined, {
|
|
213
241
|
tolerateLegacyTls: true,
|
|
@@ -242,6 +270,31 @@ describe("origin proxy", () => {
|
|
|
242
270
|
expect(failure?.name).to.equal("TimeoutError");
|
|
243
271
|
});
|
|
244
272
|
});
|
|
273
|
+
describe("over tls", () => {
|
|
274
|
+
let secure;
|
|
275
|
+
let secureUrl;
|
|
276
|
+
beforeEach(async () => {
|
|
277
|
+
// Answers the host header and the servername the client sent.
|
|
278
|
+
secure = https.createServer({ key: LOCALHOST_KEY, cert: LOCALHOST_CERT }, (request, response) => response.end(JSON.stringify({
|
|
279
|
+
host: request.headers.host,
|
|
280
|
+
servername: request.socket.servername,
|
|
281
|
+
})));
|
|
282
|
+
await new Promise((resolve) => secure.listen(0, resolve));
|
|
283
|
+
secureUrl = `https://localhost:${secure.address().port}/`;
|
|
284
|
+
});
|
|
285
|
+
afterEach(() => {
|
|
286
|
+
secure.closeAllConnections();
|
|
287
|
+
secure.close();
|
|
288
|
+
});
|
|
289
|
+
it("keeps the connected host as servername when it overrides the host header", async () => {
|
|
290
|
+
// The self-signed certificate needs the tolerant transport.
|
|
291
|
+
const response = await originFetch(secureUrl, { headers: { host: "www.example.com" } }, { tolerateLegacyTls: true });
|
|
292
|
+
expect(await response.json()).to.deep.equal({
|
|
293
|
+
host: "www.example.com",
|
|
294
|
+
servername: "localhost",
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
});
|
|
245
298
|
it("routes through the installed proxy", async () => {
|
|
246
299
|
// Nothing listens on port 1, so a proxied request must fail to connect.
|
|
247
300
|
setOriginProxy({ type: 5, host: "127.0.0.1", port: 1 });
|
|
@@ -56,7 +56,7 @@ export declare class UndefinedVariationError extends Error {
|
|
|
56
56
|
* Resolves which variation configs apply, in server order. The template path returns
|
|
57
57
|
* `[wildcard, template]` and skips the device defaults; otherwise `[default, wildcard, named]`.
|
|
58
58
|
*/
|
|
59
|
-
export declare function getVariations(custom: AssetVariationMap,
|
|
59
|
+
export declare function getVariations(custom: AssetVariationMap, requestedType?: string): AssetVariation[];
|
|
60
60
|
/** What a request is resolved against besides the variations. */
|
|
61
61
|
export interface ResolveOptions {
|
|
62
62
|
/** A Host header for every request, as `--host` sends it. */
|
|
@@ -77,7 +77,9 @@ function applyTemplate(entry, variationType) {
|
|
|
77
77
|
* Resolves which variation configs apply, in server order. The template path returns
|
|
78
78
|
* `[wildcard, template]` and skips the device defaults; otherwise `[default, wildcard, named]`.
|
|
79
79
|
*/
|
|
80
|
-
export function getVariations(custom,
|
|
80
|
+
export function getVariations(custom, requestedType) {
|
|
81
|
+
// The client sends `desktop` as `default`, so the server never sees it.
|
|
82
|
+
const variationType = requestedType?.toLowerCase() === "desktop" ? DEFAULT_TYPE : requestedType;
|
|
81
83
|
const entries = Object.entries(custom ?? {}).map(([key, entry]) => ({
|
|
82
84
|
...entry,
|
|
83
85
|
matchedKey: key,
|
|
@@ -23,13 +23,9 @@ export class OriginRequestServiceFactory {
|
|
|
23
23
|
: undefined;
|
|
24
24
|
// `--json` owns stdout, so the proxy line stays out of a machine-readable run.
|
|
25
25
|
applyOriginProxy(customer?.chromeFlags, this.context.json ? undefined : cli);
|
|
26
|
+
// An empty variation set still runs: device defaults resolve, originMap and authentications apply.
|
|
26
27
|
const config = await this.loadRuntimeConfig(customer?.app);
|
|
27
|
-
|
|
28
|
-
const usable = config && (!customer || Object.keys(config.variations).length > 0);
|
|
29
|
-
if (config && !usable) {
|
|
30
|
-
cli.writeError("the config carries no speedKit.variations — nothing to simulate");
|
|
31
|
-
}
|
|
32
|
-
this.service = new OriginRequestService(this.context, cli, usable
|
|
28
|
+
this.service = new OriginRequestService(this.context, cli, config
|
|
33
29
|
? new OriginFetcher({
|
|
34
30
|
cli,
|
|
35
31
|
config,
|
|
@@ -78,6 +78,15 @@ describe("asset variations", () => {
|
|
|
78
78
|
}, "https://www.example.com/plp?page=2");
|
|
79
79
|
expect(url).to.equal("https://proxy.example.com/?target=https%3A%2F%2Fwww.example.com%2Fplp%3Fpage%3D2&bq=1");
|
|
80
80
|
});
|
|
81
|
+
it("resolves desktop as the default variation, with its configured entry", () => {
|
|
82
|
+
const custom = { default: { headers: { "x-variant": "default" } } };
|
|
83
|
+
expect(getVariations(custom, "desktop")).to.deep.equal(getVariations(custom, "default"));
|
|
84
|
+
});
|
|
85
|
+
it("resolves the device variations without any configured variation", () => {
|
|
86
|
+
for (const type of ["desktop", "default", "mobile", "tablet"]) {
|
|
87
|
+
expect(() => getVariations({}, type)).not.to.throw();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
81
90
|
it("reports an unknown variation instead of guessing", () => {
|
|
82
91
|
expect(() => getVariations({}, "b2b-unknown")).to.throw(UndefinedVariationError);
|
|
83
92
|
});
|
package/oclif.manifest.json
CHANGED