@percy/core 1.32.2 → 1.32.3-beta.1
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/dist/api.js +98 -14
- package/dist/maestro-hierarchy.js +89 -14
- package/dist/percy.js +1 -1
- package/dist/utils.js +71 -11
- package/package.json +10 -10
package/dist/api.js
CHANGED
|
@@ -448,15 +448,19 @@ export function createPercyServer(percy, port) {
|
|
|
448
448
|
sessionId
|
|
449
449
|
} = req.body || {};
|
|
450
450
|
if (!name) throw new ServerError(400, 'Missing required field: name');
|
|
451
|
-
|
|
451
|
+
|
|
452
|
+
// Self-hosted vs BrowserStack is signaled by sessionId presence: BS
|
|
453
|
+
// host-injection always supplies it; self-hosted runs never do.
|
|
454
|
+
let selfHosted = !sessionId;
|
|
452
455
|
|
|
453
456
|
// Strict character-class validation — rejects path separators, shell metacharacters,
|
|
454
457
|
// NUL, newlines, and anything else that could confuse the glob or the filesystem.
|
|
458
|
+
// `name` is load-bearing for the recursive glob — must not be loosened.
|
|
455
459
|
const SAFE_ID = /^[a-zA-Z0-9_-]+$/;
|
|
456
460
|
if (!SAFE_ID.test(name)) {
|
|
457
461
|
throw new ServerError(400, 'Invalid screenshot name');
|
|
458
462
|
}
|
|
459
|
-
if (!SAFE_ID.test(sessionId)) {
|
|
463
|
+
if (sessionId && !SAFE_ID.test(sessionId)) {
|
|
460
464
|
throw new ServerError(400, 'Invalid sessionId');
|
|
461
465
|
}
|
|
462
466
|
|
|
@@ -496,6 +500,51 @@ export function createPercyServer(percy, port) {
|
|
|
496
500
|
suppliedFilePath = req.body.filePath;
|
|
497
501
|
}
|
|
498
502
|
|
|
503
|
+
// Resolve the file-find scope root. On BrowserStack (sessionId present),
|
|
504
|
+
// the root is the BS host's /tmp/{sessionId}{_test_suite} convention.
|
|
505
|
+
// Self-hosted (sessionId absent) requires PERCY_MAESTRO_SCREENSHOT_DIR
|
|
506
|
+
// (read from process.env, never the request body) to be an absolute,
|
|
507
|
+
// existing directory — typically the customer's
|
|
508
|
+
// `maestro test --test-output-dir <DIR>` path. The realpath + prefix
|
|
509
|
+
// check below enforces the security invariant at whichever root applies;
|
|
510
|
+
// the boundary is relocated, not removed.
|
|
511
|
+
let scopeRoot;
|
|
512
|
+
if (selfHosted) {
|
|
513
|
+
// Reject filePath outright in self-hosted mode. The SDK never emits
|
|
514
|
+
// it (it sends a relative SCREENSHOT_NAME); honoring an absolute
|
|
515
|
+
// filePath against a caller-influenceable root would re-open
|
|
516
|
+
// arbitrary in-root reads.
|
|
517
|
+
if (suppliedFilePath) {
|
|
518
|
+
throw new ServerError(400, 'filePath is not accepted in self-hosted mode (omit it; PERCY_MAESTRO_SCREENSHOT_DIR + relative SCREENSHOT_NAME is the supported path)');
|
|
519
|
+
}
|
|
520
|
+
let dir = process.env.PERCY_MAESTRO_SCREENSHOT_DIR;
|
|
521
|
+
if (!dir) {
|
|
522
|
+
throw new ServerError(400, 'Missing required env: PERCY_MAESTRO_SCREENSHOT_DIR (set it to your `maestro test --test-output-dir` path)');
|
|
523
|
+
}
|
|
524
|
+
if (!path.isAbsolute(dir)) {
|
|
525
|
+
throw new ServerError(400, 'PERCY_MAESTRO_SCREENSHOT_DIR must be an absolute path');
|
|
526
|
+
}
|
|
527
|
+
// UX guard ONLY: surface an actionable 400 ("dir not found") instead
|
|
528
|
+
// of the opaque 404 the realpath+prefix containment check below would
|
|
529
|
+
// emit for a missing dir. There is a small TOCTOU window between this
|
|
530
|
+
// stat and the realpath at line 647 — that window is acceptable here
|
|
531
|
+
// because realpath (not stat) is the security invariant: even if the
|
|
532
|
+
// dir is replaced with a symlink in between, realpath resolves the
|
|
533
|
+
// target and the sep-prefix check rejects anything outside scopeRoot.
|
|
534
|
+
let stat;
|
|
535
|
+
try {
|
|
536
|
+
stat = await fs.promises.stat(dir);
|
|
537
|
+
} catch {
|
|
538
|
+
stat = null;
|
|
539
|
+
}
|
|
540
|
+
if (!stat || !stat.isDirectory()) {
|
|
541
|
+
throw new ServerError(400, `PERCY_MAESTRO_SCREENSHOT_DIR is not an existing directory: ${dir}`);
|
|
542
|
+
}
|
|
543
|
+
scopeRoot = dir;
|
|
544
|
+
} else {
|
|
545
|
+
scopeRoot = platform === 'ios' ? `/tmp/${sessionId}` : `/tmp/${sessionId}_test_suite`;
|
|
546
|
+
}
|
|
547
|
+
|
|
499
548
|
// Validate regions input shape early (before file I/O and ADB work) so
|
|
500
549
|
// malformed requests don't consume resolver/relay work. Three parallel
|
|
501
550
|
// input arrays share the same per-item shape; algorithm semantics differ
|
|
@@ -552,20 +601,45 @@ export function createPercyServer(percy, port) {
|
|
|
552
601
|
// {device}_maestro_debug_ root. The `**` recursive match handles any depth.
|
|
553
602
|
// Exact {name}.png match at the leaf filters out Maestro's emoji-prefixed
|
|
554
603
|
// debug frames (e.g., `screenshot-❌-<timestamp>-(flow).png`).
|
|
555
|
-
let searchPattern
|
|
604
|
+
let searchPattern;
|
|
605
|
+
if (selfHosted) {
|
|
606
|
+
// Self-hosted: recursive glob under the customer's --test-output-dir
|
|
607
|
+
// (PERCY_MAESTRO_SCREENSHOT_DIR). Recursive depth handles arbitrary
|
|
608
|
+
// Maestro layouts; `name` is SAFE_ID-validated above so it cannot
|
|
609
|
+
// contain separators or traversal characters.
|
|
610
|
+
//
|
|
611
|
+
// fast-glob requires forward-slashes in patterns on every platform
|
|
612
|
+
// (per its docs: "Always use forward-slashes in glob expressions").
|
|
613
|
+
// On Windows the scopeRoot from path.resolve contains backslashes,
|
|
614
|
+
// so we normalize before embedding into the pattern. Production-
|
|
615
|
+
// code Windows portability — verified by the CI Windows runner.
|
|
616
|
+
const globRoot = scopeRoot.replace(/\\/g, '/');
|
|
617
|
+
searchPattern = `${globRoot}/**/${name}.png`;
|
|
618
|
+
} else {
|
|
619
|
+
searchPattern = platform === 'ios' ? `/tmp/${sessionId}/*_maestro_debug_*/**/${name}.png` : `/tmp/${sessionId}_test_suite/logs/*/screenshots/${name}.png`;
|
|
620
|
+
}
|
|
556
621
|
let files;
|
|
557
622
|
try {
|
|
558
623
|
let {
|
|
559
624
|
default: glob
|
|
560
625
|
} = await import('fast-glob');
|
|
561
|
-
|
|
626
|
+
// Self-hosted needs `dot: true` because Maestro's default output
|
|
627
|
+
// directory is `.maestro/` — a dot-prefixed entry that fast-glob
|
|
628
|
+
// hides by default. BS layouts have no dot-prefixed segments, so
|
|
629
|
+
// omitting the option there keeps the byte-identical behavior.
|
|
630
|
+
files = await glob(searchPattern, selfHosted ? {
|
|
631
|
+
dot: true
|
|
632
|
+
} : undefined);
|
|
562
633
|
} catch {
|
|
563
634
|
// Fast-glob import / glob call failed — fall back to manual walker.
|
|
564
635
|
// See manualScreenshotWalk() at file top for the rationale + the
|
|
565
636
|
// file-level .semgrepignore covering path-traversal sinks inside.
|
|
637
|
+
// Self-hosted has no walker fallback (no fixed-layout convention) —
|
|
638
|
+
// empty files → 404 with the actionable PERCY_MAESTRO_SCREENSHOT_DIR
|
|
639
|
+
// guidance above.
|
|
566
640
|
/* istanbul ignore next — only fires when fast-glob import throws
|
|
567
641
|
(broken install / FS corruption); integration-test territory. */
|
|
568
|
-
files = await manualScreenshotWalk(platform, sessionId, name);
|
|
642
|
+
files = selfHosted ? [] : await manualScreenshotWalk(platform, sessionId, name);
|
|
569
643
|
}
|
|
570
644
|
if (!files || files.length === 0) {
|
|
571
645
|
throw new ServerError(404, `Screenshot not found: ${name}.png (searched ${searchPattern})`);
|
|
@@ -599,20 +673,27 @@ export function createPercyServer(percy, port) {
|
|
|
599
673
|
}
|
|
600
674
|
}
|
|
601
675
|
|
|
602
|
-
// Canonicalize and confirm the resolved path still lives under
|
|
603
|
-
// Defeats symlink swaps where
|
|
604
|
-
//
|
|
605
|
-
//
|
|
606
|
-
|
|
676
|
+
// Canonicalize and confirm the resolved path still lives under scopeRoot.
|
|
677
|
+
// Defeats symlink swaps where the root points elsewhere. Both ends are
|
|
678
|
+
// realpath'd because /tmp is a symlink on macOS (where iOS hosts run).
|
|
679
|
+
// The trailing `/` on the prefix is load-bearing — it prevents
|
|
680
|
+
// sibling-prefix bypass (e.g. /x/.maestro vs /x/.maestro-secrets).
|
|
681
|
+
//
|
|
682
|
+
// Normalize both sides to forward-slashes before the prefix check so
|
|
683
|
+
// the same code works on Windows (real-fs returns backslashes) AND on
|
|
684
|
+
// POSIX (no-op) AND on memfs in tests (POSIX-style virtual paths
|
|
685
|
+
// regardless of host OS).
|
|
607
686
|
let realPath, realPrefix;
|
|
608
687
|
try {
|
|
609
688
|
realPath = await fs.promises.realpath(chosenFile);
|
|
610
|
-
realPrefix = await fs.promises.realpath(
|
|
689
|
+
realPrefix = await fs.promises.realpath(scopeRoot);
|
|
611
690
|
} catch {
|
|
612
691
|
throw new ServerError(404, `Screenshot not found: ${name}.png (path resolution failed)`);
|
|
613
692
|
}
|
|
614
|
-
|
|
615
|
-
|
|
693
|
+
const realPathFwd = realPath.replace(/\\/g, '/');
|
|
694
|
+
const realPrefixFwd = realPrefix.replace(/\\/g, '/');
|
|
695
|
+
if (!realPathFwd.startsWith(`${realPrefixFwd}/`)) {
|
|
696
|
+
throw new ServerError(404, `Screenshot not found: ${name}.png (resolved outside ${selfHosted ? 'PERCY_MAESTRO_SCREENSHOT_DIR' : 'session dir'})`);
|
|
616
697
|
}
|
|
617
698
|
|
|
618
699
|
// Read and base64-encode the screenshot
|
|
@@ -746,7 +827,10 @@ export function createPercyServer(percy, port) {
|
|
|
746
827
|
if (cachedDump === null) {
|
|
747
828
|
// Thread the per-Percy gRPC client cache so the Android gRPC
|
|
748
829
|
// primary path can reuse channels across snapshots in the same
|
|
749
|
-
// session (D9 of 2026-05-07-002 plan). iOS path ignores it
|
|
830
|
+
// session (D9 of 2026-05-07-002 plan). iOS path ignores it
|
|
831
|
+
// (the iOS resolver reads PERCY_IOS_DRIVER_HOST_PORT directly;
|
|
832
|
+
// no per-session port cache needed since the port is prescribed
|
|
833
|
+
// upstream by `@percy/cli-app`'s `maybeInjectDriverHostPort`).
|
|
750
834
|
cachedDump = await maestroDump({
|
|
751
835
|
platform,
|
|
752
836
|
sessionId,
|
|
@@ -99,10 +99,16 @@ const SELECTOR_KEYS_UNION = ['resource-id', 'text', 'content-desc', 'class', 'id
|
|
|
99
99
|
// past the socket timeout.
|
|
100
100
|
const IOS_HTTP_HEALTHY_DEADLINE_MS = 1500;
|
|
101
101
|
const IOS_HTTP_CIRCUIT_BREAKER_MS = 5000;
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
// `parseIosDriverHostPort` accepts any valid TCP port (1-65535). The
|
|
103
|
+
// BrowserStack-realmobile canonical range (11100-11110, derived as
|
|
104
|
+
// wda_port + 2700 from WDA ports 8400-8410) is a strict subset. Self-hosted
|
|
105
|
+
// callers prescribe the port via `@percy/cli-app`'s `maybeInjectDriverHostPort`
|
|
106
|
+
// helper, which auto-injects `--driver-host-port <PORT>` into the
|
|
107
|
+
// `maestro test` argv and mirrors the chosen value to
|
|
108
|
+
// `PERCY_IOS_DRIVER_HOST_PORT`. This relay reads the env var unconditionally
|
|
109
|
+
// and no longer auto-discovers the port via probe / lsof — see Phase 3 of
|
|
110
|
+
// the self-hosted Maestro architecture doc and brainstorm
|
|
111
|
+
// `2026-06-11-ios-port-discovery-simplification-requirements.md`.
|
|
106
112
|
// HTTP response cap before parse — sized for WebView-heavy iOS apps.
|
|
107
113
|
const IOS_HTTP_RESPONSE_MAX_BYTES = 20 * 1024 * 1024;
|
|
108
114
|
|
|
@@ -1069,17 +1075,23 @@ function defaultHttpRequest({
|
|
|
1069
1075
|
});
|
|
1070
1076
|
}
|
|
1071
1077
|
|
|
1072
|
-
// Validate PERCY_IOS_DRIVER_HOST_PORT env value as integer in the
|
|
1073
|
-
// range (
|
|
1078
|
+
// Validate PERCY_IOS_DRIVER_HOST_PORT env value as an integer in the valid
|
|
1079
|
+
// TCP range (1-65535). Was previously clamped to the BS-specific realmobile
|
|
1080
|
+
// range (wda_port+2700 = 11100-11110); the relaxed range is a superset so BS
|
|
1081
|
+
// continues to accept its canonical port unchanged. Self-hosted callers can
|
|
1082
|
+
// pass any port they pinned via `maestro test --driver-host-port <P>` (e.g.
|
|
1083
|
+
// the deterministic `7001` on a simulator, or a forwarded port like ~6001 on
|
|
1084
|
+
// a real device).
|
|
1074
1085
|
function parseIosDriverHostPort(raw) {
|
|
1075
1086
|
/* istanbul ignore if — undefined/null/empty raw value branch; iOS dispatch
|
|
1076
1087
|
pre-checks PERCY_IOS_DRIVER_HOST_PORT before calling, so these never fire. */
|
|
1077
1088
|
if (raw === undefined || raw === null || raw === '') return null;
|
|
1078
1089
|
const n = Number(raw);
|
|
1079
1090
|
/* istanbul ignore if — non-integer port (e.g. NaN from non-numeric env);
|
|
1080
|
-
env var is set by realmobile as the canonical wda_port+2700 integer
|
|
1091
|
+
env var is set by realmobile as the canonical wda_port+2700 integer or by
|
|
1092
|
+
a self-hosted customer as their explicit `--driver-host-port` value. */
|
|
1081
1093
|
if (!Number.isInteger(n)) return null;
|
|
1082
|
-
if (n <
|
|
1094
|
+
if (n < 1 || n > 65535) return null;
|
|
1083
1095
|
return n;
|
|
1084
1096
|
}
|
|
1085
1097
|
|
|
@@ -1499,11 +1511,51 @@ export async function dump(options) {
|
|
|
1499
1511
|
getEnv = getEnv || defaultGetEnv;
|
|
1500
1512
|
const started = Date.now();
|
|
1501
1513
|
if (platform === 'ios') {
|
|
1502
|
-
// iOS dispatch: read realmobile-injected env vars; warn-skip if absent.
|
|
1503
1514
|
const udid = getEnv('PERCY_IOS_DEVICE_UDID');
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1515
|
+
let driverHostPortRaw = getEnv('PERCY_IOS_DRIVER_HOST_PORT');
|
|
1516
|
+
|
|
1517
|
+
// Self-hosted env-absent path: probe the Maestro 2.4.0 documented
|
|
1518
|
+
// single-simulator default port (127.0.0.1:7001) before giving up.
|
|
1519
|
+
// Maestro 2.4.0's TestCommand binds the driver to 7001 deterministically
|
|
1520
|
+
// for the single-simulator case, so a single HTTP probe resolves the
|
|
1521
|
+
// dominant self-hosted scenario without any port discovery from outside
|
|
1522
|
+
// the process. Older releases used the same default; newer releases
|
|
1523
|
+
// (2.6+, ephemeral port via ServerSocket(0)) require the customer to
|
|
1524
|
+
// pin via `PERCY_IOS_DRIVER_HOST_PORT` (or `maestro --driver-host-port`,
|
|
1525
|
+
// which 2.6+ accepts at the parent level).
|
|
1526
|
+
//
|
|
1527
|
+
// `PERCY_IOS_DRIVER_HOST_PORT` is the only signal the explicit branch
|
|
1528
|
+
// below reads. Set by:
|
|
1529
|
+
// * BrowserStack hosts (canonical 11100-11110 range, via
|
|
1530
|
+
// `cli_manager.rb#start_percy_cli` env injection)
|
|
1531
|
+
// * Customers who pin the driver host port themselves
|
|
1532
|
+
//
|
|
1533
|
+
// If neither the env var nor a live driver on 7001 is found, element
|
|
1534
|
+
// regions degrade gracefully with `env-missing` — coordinate regions
|
|
1535
|
+
// and the snapshot itself continue to upload via the relay's other
|
|
1536
|
+
// paths.
|
|
1537
|
+
if (!driverHostPortRaw) {
|
|
1538
|
+
const probe = await runIosHttpDump({
|
|
1539
|
+
port: 7001,
|
|
1540
|
+
sessionId,
|
|
1541
|
+
httpRequest
|
|
1542
|
+
});
|
|
1543
|
+
if (probe.kind === 'hierarchy' || probe.kind === 'no-aut-tree') {
|
|
1544
|
+
log.debug(`dump took ${Date.now() - started}ms via maestro-http (self-hosted probe :7001, ${probe.kind === 'hierarchy' ? `${probe.nodes.length} nodes` : probe.reason})`);
|
|
1545
|
+
if (probe.kind === 'hierarchy') {
|
|
1546
|
+
recordResolverSuccess({
|
|
1547
|
+
platform: 'ios',
|
|
1548
|
+
via: 'maestro-http'
|
|
1549
|
+
});
|
|
1550
|
+
} else {
|
|
1551
|
+
recordResolverFinalFailure({
|
|
1552
|
+
platform: 'ios',
|
|
1553
|
+
failureClass: failureClassFromReason(probe.reason)
|
|
1554
|
+
});
|
|
1555
|
+
}
|
|
1556
|
+
return probe;
|
|
1557
|
+
}
|
|
1558
|
+
log.warn('[percy] iOS element regions unavailable — no Maestro driver responded on 127.0.0.1:7001. Set PERCY_IOS_DRIVER_HOST_PORT if your driver binds a non-default port (e.g. Maestro 2.6+ ephemeral or sharded runs).');
|
|
1507
1559
|
recordResolverFinalFailure({
|
|
1508
1560
|
platform: 'ios',
|
|
1509
1561
|
failureClass: 'other'
|
|
@@ -1514,6 +1566,12 @@ export async function dump(options) {
|
|
|
1514
1566
|
};
|
|
1515
1567
|
}
|
|
1516
1568
|
|
|
1569
|
+
// ─── Explicit — PERCY_IOS_DRIVER_HOST_PORT present ─────────────────────
|
|
1570
|
+
// BrowserStack (host-injected) and self-hosted-with-explicit-port both
|
|
1571
|
+
// take this path. The HTTP primary runs against the supplied port; the
|
|
1572
|
+
// CLI fallback runs only when UDID is also present (without UDID
|
|
1573
|
+
// there's no way to target a specific device via `maestro --udid`).
|
|
1574
|
+
|
|
1517
1575
|
// D3 kill switch (PERCY_MAESTRO_GRPC=0): same env name gates BOTH Maestro
|
|
1518
1576
|
// primaries. On iOS this skips runIosHttpDump and routes straight to the
|
|
1519
1577
|
// maestro-CLI fallback below. Read every call so toggling at runtime is
|
|
@@ -1523,8 +1581,10 @@ export async function dump(options) {
|
|
|
1523
1581
|
log.warn('PERCY_MAESTRO_GRPC=0 kill switch active; skipping iOS HTTP primary');
|
|
1524
1582
|
}
|
|
1525
1583
|
|
|
1526
|
-
// Validate driver-host-port
|
|
1527
|
-
//
|
|
1584
|
+
// Validate driver-host-port — integer 1-65535 (relaxed from the BS
|
|
1585
|
+
// 11100-11110 range; BS values stay valid as a subset). Out-of-range
|
|
1586
|
+
// values skip the HTTP path entirely and fall through to maestro-CLI
|
|
1587
|
+
// (when UDID is present) or warn-skip (when not).
|
|
1528
1588
|
const driverHostPort = parseIosDriverHostPort(driverHostPortRaw);
|
|
1529
1589
|
let httpResult = null;
|
|
1530
1590
|
if (!iosKillSwitch && driverHostPort !== null) {
|
|
@@ -1572,6 +1632,21 @@ export async function dump(options) {
|
|
|
1572
1632
|
});
|
|
1573
1633
|
log.info(`[percy] hierarchy: maestro-http failed (other: ${oorReason}) → falling back to maestro-cli-fallback`);
|
|
1574
1634
|
}
|
|
1635
|
+
|
|
1636
|
+
// CLI fallback requires UDID. Without it (port-only self-hosted), the
|
|
1637
|
+
// HTTP attempt above was the only available path — emit warn-skip with
|
|
1638
|
+
// an actionable message rather than running maestro without a target.
|
|
1639
|
+
if (!udid) {
|
|
1640
|
+
log.warn('[percy] iOS resolver: PERCY_IOS_DEVICE_UDID absent — no CLI fallback. HTTP primary either succeeded above and returned, or failed and there is no further path. Set UDID to enable the CLI fallback.');
|
|
1641
|
+
recordResolverFinalFailure({
|
|
1642
|
+
platform: 'ios',
|
|
1643
|
+
failureClass: 'other'
|
|
1644
|
+
});
|
|
1645
|
+
return {
|
|
1646
|
+
kind: 'unavailable',
|
|
1647
|
+
reason: 'self-hosted-no-udid'
|
|
1648
|
+
};
|
|
1649
|
+
}
|
|
1575
1650
|
const cliResult = await runMaestroIosDump(udid, driverHostPort ?? driverHostPortRaw, execMaestro, getEnv);
|
|
1576
1651
|
const httpReason = httpResult ? `${httpResult.kind}/${httpResult.reason}` : 'out-of-range-port';
|
|
1577
1652
|
log.debug(`dump took ${Date.now() - started}ms via maestro-cli-fallback (${httpReason}) kind=${cliResult.kind}`);
|
package/dist/percy.js
CHANGED
|
@@ -606,7 +606,7 @@ export class Percy {
|
|
|
606
606
|
options = validateSnapshotOptions(options);
|
|
607
607
|
// process CORS iframes in domSnapshot before validation
|
|
608
608
|
if (options.domSnapshot) {
|
|
609
|
-
options.domSnapshot = processCorsIframes(options.domSnapshot);
|
|
609
|
+
options.domSnapshot = processCorsIframes(options.domSnapshot, options.url);
|
|
610
610
|
}
|
|
611
611
|
this.client.addClientInfo(options.clientInfo);
|
|
612
612
|
this.client.addEnvironmentInfo(options.environmentInfo);
|
package/dist/utils.js
CHANGED
|
@@ -39,8 +39,50 @@ export function appendUrlSearchParam(urlString, key, value) {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
//
|
|
43
|
-
|
|
42
|
+
// Returns true when the URL has an http or https scheme. Percy can only fetch
|
|
43
|
+
// and serve http(s) resources, so a frame URL with another scheme (blob:,
|
|
44
|
+
// data:, about:, etc.) cannot be used as-is for an upload resource — its
|
|
45
|
+
// captured content is re-hosted under a synthetic http(s) URL instead.
|
|
46
|
+
export function isHttpOrHttpsUrl(urlString) {
|
|
47
|
+
try {
|
|
48
|
+
const {
|
|
49
|
+
protocol
|
|
50
|
+
} = new URL(urlString);
|
|
51
|
+
return protocol === 'http:' || protocol === 'https:';
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Rewrites localhost/127.0.0.1 origins to Percy's internal render host so
|
|
58
|
+
// serialized resources resolve consistently during rendering. Mirrors the
|
|
59
|
+
// rewrite applied to serialized DOM resources in @percy/dom (serialize-cssom).
|
|
60
|
+
export function rewriteLocalhostURL(url) {
|
|
61
|
+
return url.replace(/(http[s]{0,1}:\/\/)(localhost|127.0.0.1)[:\d+]*/, '$1render.percy.local');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Builds a synthetic, renderable http(s) URL for a CORS iframe whose own URL is
|
|
65
|
+
// not http(s) (e.g. a blob: iframe). The captured iframe HTML is re-hosted at a
|
|
66
|
+
// `/__serialized__/` path resolved against the page origin — the same scheme
|
|
67
|
+
// @percy/dom uses for blob stylesheets — so Percy's renderer can serve it and
|
|
68
|
+
// the API's http(s)-only URL validation passes. Returns null when there is no
|
|
69
|
+
// percyElementId, since without it the iframe `src` cannot be rewritten to point
|
|
70
|
+
// at the synthetic URL and the resource would be orphaned.
|
|
71
|
+
export function buildSyntheticFrameResourceUrl(rootUrl, percyElementId) {
|
|
72
|
+
if (!percyElementId) return null;
|
|
73
|
+
const path = `/__serialized__/cors-iframe-${encodeURIComponent(percyElementId)}.html`;
|
|
74
|
+
const base = rootUrl && isHttpOrHttpsUrl(rootUrl) ? rootUrl : 'https://render.percy.local';
|
|
75
|
+
try {
|
|
76
|
+
return rewriteLocalhostURL(new URL(path, base).toString());
|
|
77
|
+
} catch {
|
|
78
|
+
/* istanbul ignore next: base is always a valid absolute URL, kept as a defensive guard */
|
|
79
|
+
return rewriteLocalhostURL(`https://render.percy.local${path}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Process CORS iframes in a single domSnapshot object. `rootUrl` is the page
|
|
84
|
+
// URL of the snapshot, used as the base for synthetic resource URLs.
|
|
85
|
+
export function processCorsIframesInDomSnapshot(domSnapshot, rootUrl) {
|
|
44
86
|
var _domSnapshot$corsIfra;
|
|
45
87
|
if (!(domSnapshot !== null && domSnapshot !== void 0 && (_domSnapshot$corsIfra = domSnapshot.corsIframes) !== null && _domSnapshot$corsIfra !== void 0 && _domSnapshot$corsIfra.length)) {
|
|
46
88
|
return domSnapshot;
|
|
@@ -64,9 +106,26 @@ export function processCorsIframesInDomSnapshot(domSnapshot) {
|
|
|
64
106
|
continue;
|
|
65
107
|
}
|
|
66
108
|
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
|
|
109
|
+
// Determine the URL used for both the uploaded resource and the rewritten
|
|
110
|
+
// iframe src. For http(s) frames this is the frame URL (plus width); for
|
|
111
|
+
// non-http(s) frames (e.g. blob:) the frame URL cannot be served or
|
|
112
|
+
// validated by Percy, so the captured iframe HTML is re-hosted under a
|
|
113
|
+
// synthetic http(s) URL instead — mirroring blob stylesheet handling in
|
|
114
|
+
// @percy/dom. Frames we cannot re-host (no percyElementId) are skipped so a
|
|
115
|
+
// bad URL never reaches upload validation and fails the whole snapshot.
|
|
116
|
+
let resourceUrl;
|
|
117
|
+
if (isHttpOrHttpsUrl(frameUrl)) {
|
|
118
|
+
// width is only passed in case of responsiveSnapshotCapture
|
|
119
|
+
resourceUrl = domSnapshot.width ? appendUrlSearchParam(frameUrl, 'percy_width', domSnapshot.width) : frameUrl;
|
|
120
|
+
} else {
|
|
121
|
+
const syntheticUrl = buildSyntheticFrameResourceUrl(rootUrl, iframeData === null || iframeData === void 0 ? void 0 : iframeData.percyElementId);
|
|
122
|
+
if (!syntheticUrl) {
|
|
123
|
+
logger('core:utils').debug(`Skipping corsIframes entry with non-http(s) frameUrl and no percyElementId: ${frameUrl}`);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
resourceUrl = domSnapshot.width ? appendUrlSearchParam(syntheticUrl, 'percy_width', domSnapshot.width) : syntheticUrl;
|
|
127
|
+
logger('core:utils').debug(`Re-hosting non-http(s) corsIframes frameUrl ${frameUrl} as ${resourceUrl}`);
|
|
128
|
+
}
|
|
70
129
|
|
|
71
130
|
// Add iframe snapshot resources to main resources
|
|
72
131
|
if (iframeSnapshot !== null && iframeSnapshot !== void 0 && iframeSnapshot.resources) {
|
|
@@ -75,7 +134,7 @@ export function processCorsIframesInDomSnapshot(domSnapshot) {
|
|
|
75
134
|
|
|
76
135
|
// Create a new resource for the iframe's HTML
|
|
77
136
|
const iframeResource = {
|
|
78
|
-
url:
|
|
137
|
+
url: resourceUrl,
|
|
79
138
|
content: iframeSnapshot.html,
|
|
80
139
|
mimetype: 'text/html'
|
|
81
140
|
};
|
|
@@ -90,7 +149,7 @@ export function processCorsIframesInDomSnapshot(domSnapshot) {
|
|
|
90
149
|
/* istanbul ignore next: iframe matching logic depends on DOM structure */
|
|
91
150
|
if (match) {
|
|
92
151
|
const iframeTag = match[1];
|
|
93
|
-
const newIframeTag = iframeTag.replace(/src="[^"]*"/i, `src="${
|
|
152
|
+
const newIframeTag = iframeTag.replace(/src="[^"]*"/i, `src="${resourceUrl}"`);
|
|
94
153
|
domSnapshot.html = domSnapshot.html.replace(iframeTag, newIframeTag);
|
|
95
154
|
}
|
|
96
155
|
}
|
|
@@ -98,13 +157,14 @@ export function processCorsIframesInDomSnapshot(domSnapshot) {
|
|
|
98
157
|
return domSnapshot;
|
|
99
158
|
}
|
|
100
159
|
|
|
101
|
-
// Process CORS iframes - handles both single object and array of domSnapshots
|
|
102
|
-
|
|
160
|
+
// Process CORS iframes - handles both single object and array of domSnapshots.
|
|
161
|
+
// `rootUrl` is the snapshot's page URL, used as the base for synthetic URLs.
|
|
162
|
+
export function processCorsIframes(domSnapshot, rootUrl) {
|
|
103
163
|
if (!domSnapshot) return domSnapshot;
|
|
104
164
|
if (Array.isArray(domSnapshot)) {
|
|
105
|
-
return domSnapshot.map(snap => processCorsIframesInDomSnapshot(snap));
|
|
165
|
+
return domSnapshot.map(snap => processCorsIframesInDomSnapshot(snap, rootUrl));
|
|
106
166
|
}
|
|
107
|
-
return processCorsIframesInDomSnapshot(domSnapshot);
|
|
167
|
+
return processCorsIframesInDomSnapshot(domSnapshot, rootUrl);
|
|
108
168
|
}
|
|
109
169
|
|
|
110
170
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@percy/core",
|
|
3
|
-
"version": "1.32.
|
|
3
|
+
"version": "1.32.3-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public",
|
|
12
|
-
"tag": "
|
|
12
|
+
"tag": "beta"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=14"
|
|
@@ -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.
|
|
50
|
-
"@percy/config": "1.32.
|
|
51
|
-
"@percy/dom": "1.32.
|
|
52
|
-
"@percy/logger": "1.32.
|
|
53
|
-
"@percy/monitoring": "1.32.
|
|
54
|
-
"@percy/webdriver-utils": "1.32.
|
|
49
|
+
"@percy/client": "1.32.3-beta.1",
|
|
50
|
+
"@percy/config": "1.32.3-beta.1",
|
|
51
|
+
"@percy/dom": "1.32.3-beta.1",
|
|
52
|
+
"@percy/logger": "1.32.3-beta.1",
|
|
53
|
+
"@percy/monitoring": "1.32.3-beta.1",
|
|
54
|
+
"@percy/webdriver-utils": "1.32.3-beta.1",
|
|
55
55
|
"busboy": "^1.6.0",
|
|
56
56
|
"content-disposition": "^0.5.4",
|
|
57
57
|
"cross-spawn": "^7.0.3",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"yaml": "^2.4.1"
|
|
68
68
|
},
|
|
69
69
|
"optionalDependencies": {
|
|
70
|
-
"@percy/cli-doctor": "1.32.
|
|
70
|
+
"@percy/cli-doctor": "1.32.3-beta.1"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "92723d8feb00ce604c67dcd1e28c93b498c6996f"
|
|
73
73
|
}
|