@percy/core 1.32.12-beta.1 → 1.32.12-beta.2

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 (3) hide show
  1. package/dist/network.js +39 -14
  2. package/dist/utils.js +104 -0
  3. package/package.json +10 -10
package/dist/network.js CHANGED
@@ -2,7 +2,7 @@ import { request as makeRequest } from '@percy/client/utils';
2
2
  import logger from '@percy/logger';
3
3
  import mime from 'mime-types';
4
4
  import dns from 'dns';
5
- import { AbortError, DefaultMap, createResource, hostnameMatches, normalizeURL, waitFor, decodeAndEncodeURLWithLogging, handleIncorrectFontMimeType, executeDomainValidation, isMetadataTarget, isMetadataIP } from './utils.js';
5
+ import { AbortError, DefaultMap, createResource, hostnameMatches, normalizeURL, waitFor, decodeAndEncodeURLWithLogging, handleIncorrectFontMimeType, executeDomainValidation, isMetadataTarget, isMetadataIP, browserProxyFor } from './utils.js';
6
6
  export const MAX_RESOURCE_SIZE = 25 * 1024 ** 2 * 0.63; // 25MB, 0.63 factor for accounting for base64 encoding
7
7
  // CDP returns binary bodies via Network.getResponseBody as base64 in the JSON-RPC
8
8
  // response. Base64 inflates by 4/3, so an N-byte body becomes ~1.33×N on the wire.
@@ -29,6 +29,8 @@ const ABORTED_MESSAGE = 'Request was aborted by browser';
29
29
  const RESPONSE_RECEIVED_TIMEOUT = 2000;
30
30
  // Cap idle() impact when a host accepts the TCP connection then stalls during a direct fetch.
31
31
  const DIRECT_FETCH_TIMEOUT = 5000;
32
+ // Under PERCY_GZIP the size ceiling can reach tens of MB; 5s is too short.
33
+ const directFetchTimeout = () => process.env.PERCY_GZIP ? DIRECT_FETCH_TIMEOUT_WITH_GZIP : DIRECT_FETCH_TIMEOUT;
32
34
 
33
35
  // Stable, machine-readable codes for abort errors thrown from this module.
34
36
  // Consumers should prefer `error.code` over string matching on `error.message`.
@@ -641,7 +643,7 @@ export class Network {
641
643
  // DNS rebinding on the direct-fetch path. Kept as an instance method so tests
642
644
  // can stub the transport (and the connected IP) without hitting the network.
643
645
  async directFetch(request, session) {
644
- var _this$meta3;
646
+ var _this$meta3, _this$page$session;
645
647
  let cookies = [];
646
648
  let cookieSession = pickCookieSession(this, session);
647
649
  try {
@@ -681,23 +683,44 @@ export class Network {
681
683
  remoteAddresses.push(...flattenLookupAddresses(address));
682
684
  cb(err, address, family);
683
685
  });
684
- let {
685
- body,
686
- status,
687
- headers: responseHeaders
688
- } = await makeRequest(request.url, {
686
+
687
+ // Take the same route as the browser: when Chrome was launched with `--proxy-server`, hosts
688
+ // may only resolve through that proxy (e.g. privoxy in front of a BrowserStack Local tunnel).
689
+ // The proxy then resolves the target, so as on the browser path (where remoteIPAddress is the
690
+ // proxy's) the connected-IP metadata gate only sees the proxy address.
691
+ let proxy = browserProxyFor((_this$page$session = this.page.session) === null || _this$page$session === void 0 || (_this$page$session = _this$page$session.browser) === null || _this$page$session === void 0 ? void 0 : _this$page$session.args, request.url);
692
+ let fetch = (proxy, timeout) => makeRequest(request.url, {
689
693
  buffer: true,
690
694
  headers,
691
- lookup
695
+ lookup,
696
+ proxy,
697
+ timeout
692
698
  }, (body, res) => ({
693
699
  body,
694
700
  status: res.statusCode,
695
701
  headers: res.headers
696
702
  }));
703
+ let response;
704
+ if (proxy) {
705
+ this.log.debug('- Requesting directly through the browser proxy', this.meta);
706
+ try {
707
+ // an idle timeout, so a proxy that accepts the connection and then stalls still leaves
708
+ // the fallback reachable (the font re-fetch otherwise has no timeout at all)
709
+ response = await fetch(proxy, directFetchTimeout());
710
+ } catch (error) {
711
+ // The browser can answer a proxy's auth challenge (with discovery.authorization) but this
712
+ // fetch cannot, and the proxy may refuse a non-browser client. When the proxy itself fails
713
+ // (407, or no response from the target at all, including a stall), fall back to the pre-browser-proxy route
714
+ // (env proxy or direct) so anything that fetched before still does.
715
+ if (error.response && error.response.statusCode !== 407) throw error;
716
+ this.log.debug(`- Browser proxy failed (${error.message.split('\n')[0]}), retrying via the default route`, this.meta);
717
+ response = await fetch();
718
+ }
719
+ } else {
720
+ response = await fetch();
721
+ }
697
722
  return {
698
- body,
699
- status,
700
- headers: responseHeaders,
723
+ ...response,
701
724
  remoteAddresses
702
725
  };
703
726
  }
@@ -1013,15 +1036,17 @@ async function makeDirectRequest(network, request, session) {
1013
1036
  // never surfaces — Chrome 143+ fetches dedicated worker scripts in the browser
1014
1037
  // process (PlzDedicatedWorker) so loadingFinished fires without a body on CDP.
1015
1038
  async function captureResourceDirectly(network, request, session) {
1039
+ var _network$page$session;
1016
1040
  let log = network.log;
1017
1041
  let url = originURL(request);
1018
1042
  let meta = {
1019
1043
  ...network.meta,
1020
1044
  url
1021
1045
  };
1022
-
1023
- // Under PERCY_GZIP the size ceiling can reach tens of MB; 5s is too short.
1024
- let timeoutMs = process.env.PERCY_GZIP ? DIRECT_FETCH_TIMEOUT_WITH_GZIP : DIRECT_FETCH_TIMEOUT;
1046
+ let timeoutMs = directFetchTimeout();
1047
+ // through a browser proxy, directFetch may spend one timeout on the proxied attempt before
1048
+ // falling back to the default route; budget for both so the fallback is not cut off
1049
+ if (browserProxyFor((_network$page$session = network.page.session) === null || _network$page$session === void 0 || (_network$page$session = _network$page$session.browser) === null || _network$page$session === void 0 ? void 0 : _network$page$session.args, request.url)) timeoutMs *= 2;
1025
1050
  try {
1026
1051
  log.debug('- Requesting resource directly (responseReceived timeout fallback)', meta);
1027
1052
  let {
package/dist/utils.js CHANGED
@@ -2,6 +2,7 @@ import EventEmitter from 'events';
2
2
  import { sha256hash, request } from '@percy/client/utils';
3
3
  import { camelcase, merge } from '@percy/config/utils';
4
4
  import YAML from 'yaml';
5
+ import net from 'net';
5
6
  import path from 'path';
6
7
  import url from 'url';
7
8
  import { readFileSync } from 'fs';
@@ -143,6 +144,109 @@ export function isMetadataIP(remoteIP) {
143
144
  return matchMetadataHost(remoteIP);
144
145
  }
145
146
 
147
+ // Returns the proxy URL Chrome would route `url` through, given the browser's launch `args`
148
+ // (`--proxy-server` and `--proxy-bypass-list`), so Node-side fetches made on the browser's behalf
149
+ // take the same route. Hosts reachable only through that proxy (e.g. a BrowserStack Local tunnel)
150
+ // otherwise fail DNS from Node. Only http(s) proxies are supported: SOCKS and `direct://` rules,
151
+ // like a bypassed host, return undefined and leave the fetch to the proxy env vars. Only the first
152
+ // proxy of a fallback list is used; Chrome's failover to later entries is not mirrored.
153
+ export function browserProxyFor(args, url) {
154
+ // Chrome honours the last occurrence of a repeated switch
155
+ let flag = name => {
156
+ var _concat$reverse$find;
157
+ return (_concat$reverse$find = [].concat(args ?? []).reverse().find(arg => arg.startsWith(`--${name}=`))) === null || _concat$reverse$find === void 0 ? void 0 : _concat$reverse$find.slice(name.length + 3);
158
+ };
159
+ let server = flag('proxy-server');
160
+ if (!server) return;
161
+ let {
162
+ protocol,
163
+ hostname,
164
+ port
165
+ } = new URL(url);
166
+ let scheme = protocol.slice(0, -1);
167
+
168
+ // rules are `[<url-scheme>=]<proxy>[,<fallback>...]` separated by `;`, where the `socks=`
169
+ // mapping is the fallback for schemes without a mapping of their own
170
+ let rules = server.split(';').map(rule => rule.trim());
171
+ let mapping = name => rules.find(rule => rule.toLowerCase().startsWith(`${name}=`));
172
+ let rule = mapping(scheme) ?? rules.find(rule => !rule.includes('=')) ?? mapping('socks');
173
+ if (!rule) return;
174
+
175
+ // a proxy without a scheme is http, or SOCKS4 within the `socks=` mapping
176
+ let proxy = rule.replace(/^\w+=/, '').split(',')[0].trim();
177
+ if (!proxy.includes('://')) proxy = `${rule === mapping('socks') ? 'socks4' : 'http'}://${proxy}`;
178
+ if (!/^https?:\/\//.test(proxy)) return;
179
+ port || (port = scheme === 'https' ? '443' : '80');
180
+ if (bypassesBrowserProxy(flag('proxy-bypass-list'), {
181
+ scheme,
182
+ hostname,
183
+ port
184
+ })) return;
185
+ return proxy;
186
+ }
187
+
188
+ // Mirrors Chrome's `--proxy-bypass-list` matching: `,`/`;` separated rules that are either an IP
189
+ // range in CIDR notation or a host glob with an optional scheme and port (`.host` meaning
190
+ // `*.host`), plus `<local>` for dotless hosts. Loopback and link-local hosts are bypassed
191
+ // implicitly unless the list contains `<-loopback>`.
192
+ function bypassesBrowserProxy(list = '', {
193
+ scheme,
194
+ hostname,
195
+ port
196
+ }) {
197
+ let rules = list.split(/[,;]/).map(rule => rule.trim()).filter(Boolean);
198
+ if (!rules.includes('<-loopback>') && (/^(localhost|127(\.\d+){3}|169\.254(\.\d+){2}|\[::1\])$|\.localhost$/.test(hostname) || /^\[fe[89ab][0-9a-f]:/.test(hostname))) return true;
199
+ return rules.some(rule => {
200
+ if (rule === '<local>') return !hostname.includes('.');
201
+ if (rule === '<-loopback>') return false;
202
+ let host = rule.toLowerCase();
203
+ let sep = host.indexOf('://');
204
+ if (sep !== -1) {
205
+ if (host.slice(0, sep) !== scheme) return false;
206
+ host = host.slice(sep + 3);
207
+ }
208
+ if (host.includes('/')) return cidrMatches(host, hostname);
209
+ let colon = host.lastIndexOf(':');
210
+ if (colon > host.lastIndexOf(']') && /^\d+$/.test(host.slice(colon + 1))) {
211
+ if (host.slice(colon + 1) !== port) return false;
212
+ host = host.slice(0, colon);
213
+ }
214
+ if (host.startsWith('.')) host = `*${host}`;
215
+ return globMatches(host, hostname);
216
+ });
217
+ }
218
+
219
+ // Returns true when the IP-literal `hostname` falls within the `cidr` range (IPv4 or IPv6)
220
+ function cidrMatches(cidr, hostname) {
221
+ let [ip, prefix, ...extra] = cidr.split('/');
222
+ let address = hostname.replace(/^\[/, '').replace(/\]$/, '');
223
+ let family = net.isIP(ip);
224
+ // only a plain decimal prefix is valid; Number() would read '' as 0 (matching everything)
225
+ // and also accept forms like '1e1' or '0x8'
226
+ let bits = !extra.length && /^\d{1,3}$/.test(prefix) ? Number(prefix) : NaN;
227
+
228
+ // net.BlockList needs Node >= 14.18; without it CIDR rules are left unmatched
229
+ /* istanbul ignore next: CI runs a Node version that has net.BlockList */
230
+ if (typeof net.BlockList !== 'function') return false;
231
+ if (!family || net.isIP(address) !== family) return false;
232
+ if (!(bits <= (family === 4 ? 32 : 128))) return false;
233
+ let range = new net.BlockList();
234
+ range.addSubnet(ip, bits, `ipv${family}`);
235
+ return range.check(address, `ipv${family}`);
236
+ }
237
+
238
+ // Returns true when `subject` matches `glob`, where `*` matches any run of characters and `?` any
239
+ // single one (as in Chrome's MatchPattern). A linear greedy matcher with single-star
240
+ // backtracking, so user-supplied patterns need no RegExp.
241
+ function globMatches(glob, subject) {
242
+ let [g, s, star, mark] = [0, 0, -1, 0];
243
+ while (s < subject.length) {
244
+ if (glob[g] === '*') [star, mark] = [g++, s];else if (glob[g] === '?' || glob[g] === subject[s]) [g, s] = [g + 1, s + 1];else if (star !== -1) [g, s] = [star + 1, ++mark];else return false;
245
+ }
246
+ while (glob[g] === '*') g++;
247
+ return g === glob.length;
248
+ }
249
+
146
250
  // Throws when the URL points at a cloud instance-metadata endpoint. Used to
147
251
  // refuse navigating the top-level snapshot URL to such a target.
148
252
  export function assertNotMetadataTarget(rawUrl) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/core",
3
- "version": "1.32.12-beta.1",
3
+ "version": "1.32.12-beta.2",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -46,12 +46,12 @@
46
46
  "dependencies": {
47
47
  "@grpc/grpc-js": "^1.14.3",
48
48
  "@grpc/proto-loader": "^0.8.0",
49
- "@percy/client": "1.32.12-beta.1",
50
- "@percy/config": "1.32.12-beta.1",
51
- "@percy/dom": "1.32.12-beta.1",
52
- "@percy/logger": "1.32.12-beta.1",
53
- "@percy/monitoring": "1.32.12-beta.1",
54
- "@percy/webdriver-utils": "1.32.12-beta.1",
49
+ "@percy/client": "1.32.12-beta.2",
50
+ "@percy/config": "1.32.12-beta.2",
51
+ "@percy/dom": "1.32.12-beta.2",
52
+ "@percy/logger": "1.32.12-beta.2",
53
+ "@percy/monitoring": "1.32.12-beta.2",
54
+ "@percy/webdriver-utils": "1.32.12-beta.2",
55
55
  "adm-zip": "^0.6.0",
56
56
  "busboy": "^1.6.0",
57
57
  "content-disposition": "^0.5.4",
@@ -67,8 +67,8 @@
67
67
  "yaml": "^2.4.1"
68
68
  },
69
69
  "optionalDependencies": {
70
- "@percy/cli-doctor": "1.32.12-beta.1",
71
- "@percy/cli-pdf": "1.32.12-beta.1"
70
+ "@percy/cli-doctor": "1.32.12-beta.2",
71
+ "@percy/cli-pdf": "1.32.12-beta.2"
72
72
  },
73
- "gitHead": "80a7983e35606c049956af87c5f74646cb2333fa"
73
+ "gitHead": "35e95e257d7f09d763698765e429dbae0ce7d5e9"
74
74
  }