@skrillex1224/playwright-toolkit 2.1.183 → 2.1.184

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/index.js CHANGED
@@ -986,6 +986,191 @@ var buildCookieMap = (cookies) => cookies.reduce((acc, item) => {
986
986
  acc[item.name] = item.value;
987
987
  return acc;
988
988
  }, {});
989
+ var normalizeHttpUrl = (value) => {
990
+ const raw = String(value || "").trim();
991
+ if (!raw) return "";
992
+ try {
993
+ const parsed = new URL(raw);
994
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return "";
995
+ parsed.hash = "";
996
+ return parsed.toString();
997
+ } catch {
998
+ return "";
999
+ }
1000
+ };
1001
+ var uniqueStrings = (value) => {
1002
+ const list = Array.isArray(value) ? value : [];
1003
+ const seen = /* @__PURE__ */ new Set();
1004
+ const result = [];
1005
+ list.forEach((item) => {
1006
+ const normalized = String(item || "").trim();
1007
+ if (!normalized || seen.has(normalized)) return;
1008
+ seen.add(normalized);
1009
+ result.push(normalized);
1010
+ });
1011
+ return result;
1012
+ };
1013
+ var normalizeHttpUrlList = (value) => uniqueStrings(
1014
+ (Array.isArray(value) ? value : [value]).map((item) => normalizeHttpUrl(item)).filter(Boolean)
1015
+ );
1016
+ var normalizeCookieDomain = (value) => String(value || "").trim().replace(/^\./, "").toLowerCase();
1017
+ var normalizeCookiePath = (value) => {
1018
+ const raw = String(value || "").trim();
1019
+ if (!raw) return "/";
1020
+ return raw.startsWith("/") ? raw : `/${raw}`;
1021
+ };
1022
+ var doesCookieMatchUrl = (cookie, url) => {
1023
+ if (!cookie || !url) return false;
1024
+ try {
1025
+ const parsed = new URL(url);
1026
+ const host = String(parsed.hostname || "").trim().toLowerCase();
1027
+ const pathname = String(parsed.pathname || "/").trim() || "/";
1028
+ const cookieDomain = normalizeCookieDomain(cookie.domain);
1029
+ const cookiePath = normalizeCookiePath(cookie.path);
1030
+ if (!host || !cookieDomain) return false;
1031
+ const domainMatched = host === cookieDomain || host.endsWith(`.${cookieDomain}`);
1032
+ if (!domainMatched) return false;
1033
+ if (cookie.secure && parsed.protocol !== "https:") return false;
1034
+ return pathname.startsWith(cookiePath);
1035
+ } catch {
1036
+ return false;
1037
+ }
1038
+ };
1039
+ var normalizeRawSnapshot = (value = {}) => {
1040
+ const source = isPlainObject(value) ? value : {};
1041
+ const pageUrl = normalizeHttpUrl(source.page_url ?? source.pageUrl);
1042
+ const frameUrls = normalizeHttpUrlList(source.frame_urls ?? source.frameUrls);
1043
+ const resourceUrls = normalizeHttpUrlList(source.resource_urls ?? source.resourceUrls);
1044
+ const browserProfileObservedSource = source.browser_profile_observed ?? source.browserProfileObserved ?? source.browser_profile?.observed ?? source.browserProfile?.observed ?? source.browserProfile;
1045
+ return {
1046
+ page_url: pageUrl,
1047
+ frame_urls: frameUrls,
1048
+ resource_urls: resourceUrls,
1049
+ cookies: Array.isArray(source.cookies) ? deepClone(source.cookies) : [],
1050
+ local_storage: normalizeLocalStorage(source.local_storage ?? source.localStorage),
1051
+ session_storage: normalizeSessionStorage(source.session_storage ?? source.sessionStorage),
1052
+ browser_profile_observed: normalizeObservedBrowserProfile(browserProfileObservedSource),
1053
+ auth: normalizeAuth(source.auth)
1054
+ };
1055
+ };
1056
+ var collectCookieUrlsFromSnapshot = (snapshot = {}) => {
1057
+ const normalized = normalizeRawSnapshot(snapshot);
1058
+ return uniqueStrings([
1059
+ normalized.page_url,
1060
+ ...normalized.frame_urls,
1061
+ ...normalized.resource_urls
1062
+ ].filter(Boolean));
1063
+ };
1064
+ var filterCookiesBySnapshot = (cookies = [], snapshot = {}) => {
1065
+ const normalizedCookies = normalizeCookies(cookies);
1066
+ const cookieUrls = collectCookieUrlsFromSnapshot(snapshot);
1067
+ if (cookieUrls.length === 0) {
1068
+ return normalizedCookies;
1069
+ }
1070
+ return normalizedCookies.filter((cookie) => cookieUrls.some((url) => doesCookieMatchUrl(cookie, url)));
1071
+ };
1072
+ var buildEnvPayloadFromSnapshot = (snapshot = {}, options = {}) => {
1073
+ const normalized = normalizeRawSnapshot(snapshot);
1074
+ const cookies = filterCookiesBySnapshot(snapshot?.cookies || normalized.cookies, normalized);
1075
+ const localStorage = normalized.local_storage;
1076
+ const sessionStorage = normalized.session_storage;
1077
+ const auth = normalizeAuth(options.auth ?? normalized.auth);
1078
+ const browserProfileCore = normalizeBrowserProfileCore(
1079
+ options.browserProfileCore ?? snapshot?.browser_profile?.core ?? snapshot?.browserProfile?.core
1080
+ );
1081
+ const browserProfile = buildBrowserProfilePayload(browserProfileCore, normalized.browser_profile_observed);
1082
+ const payload = {
1083
+ ...cookies.length > 0 ? { cookies } : {},
1084
+ ...Object.keys(localStorage).length > 0 ? { local_storage: localStorage } : {},
1085
+ ...Object.keys(sessionStorage).length > 0 ? { session_storage: sessionStorage } : {},
1086
+ ...Object.keys(auth).length > 0 ? { auth } : {},
1087
+ ...Object.keys(browserProfile).length > 0 ? { browser_profile: browserProfile } : {}
1088
+ };
1089
+ return Object.keys(payload).length > 0 ? payload : null;
1090
+ };
1091
+ var captureRawSnapshotFromPage = async (page) => {
1092
+ const frameUrls = typeof page?.frames === "function" ? page.frames().map((frame) => {
1093
+ try {
1094
+ return frame.url();
1095
+ } catch {
1096
+ return "";
1097
+ }
1098
+ }) : [];
1099
+ const snapshot = await page.evaluate(() => {
1100
+ const localStorage = {};
1101
+ const sessionStorage = {};
1102
+ const resourceUrls = [];
1103
+ const frameUrls2 = [];
1104
+ try {
1105
+ for (let i = 0; i < window.localStorage.length; i += 1) {
1106
+ const key = window.localStorage.key(i);
1107
+ if (!key) continue;
1108
+ const value = window.localStorage.getItem(key);
1109
+ if (value !== null) localStorage[key] = value;
1110
+ }
1111
+ } catch {
1112
+ }
1113
+ try {
1114
+ for (let i = 0; i < window.sessionStorage.length; i += 1) {
1115
+ const key = window.sessionStorage.key(i);
1116
+ if (!key) continue;
1117
+ const value = window.sessionStorage.getItem(key);
1118
+ if (value !== null) sessionStorage[key] = value;
1119
+ }
1120
+ } catch {
1121
+ }
1122
+ try {
1123
+ const perfEntries = performance.getEntriesByType("resource") || [];
1124
+ perfEntries.forEach((entry) => {
1125
+ const name = String(entry?.name || "").trim();
1126
+ if (name) resourceUrls.push(name);
1127
+ });
1128
+ } catch {
1129
+ }
1130
+ try {
1131
+ document.querySelectorAll("iframe,frame").forEach((node) => {
1132
+ const src = String(node?.src || "").trim();
1133
+ if (src) frameUrls2.push(src);
1134
+ });
1135
+ } catch {
1136
+ }
1137
+ const nav = window.navigator || {};
1138
+ const screen = window.screen || {};
1139
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "";
1140
+ return {
1141
+ page_url: window.location.href || "",
1142
+ frame_urls: frameUrls2,
1143
+ resource_urls: resourceUrls,
1144
+ local_storage: localStorage,
1145
+ session_storage: sessionStorage,
1146
+ browser_profile_observed: {
1147
+ user_agent: nav.userAgent || "",
1148
+ platform: nav.platform || "",
1149
+ language: nav.language || "",
1150
+ languages: Array.isArray(nav.languages) ? nav.languages : [],
1151
+ hardware_concurrency: Number(nav.hardwareConcurrency || 0),
1152
+ device_memory: Number(nav.deviceMemory || 0),
1153
+ timezone,
1154
+ viewport: {
1155
+ width: Number(window.innerWidth || 0),
1156
+ height: Number(window.innerHeight || 0)
1157
+ },
1158
+ screen: {
1159
+ width: Number(screen.width || 0),
1160
+ height: Number(screen.height || 0),
1161
+ avail_width: Number(screen.availWidth || 0),
1162
+ avail_height: Number(screen.availHeight || 0),
1163
+ color_depth: Number(screen.colorDepth || 0),
1164
+ pixel_depth: Number(screen.pixelDepth || 0)
1165
+ }
1166
+ }
1167
+ };
1168
+ });
1169
+ return normalizeRawSnapshot({
1170
+ ...snapshot,
1171
+ frame_urls: [...frameUrls, ...snapshot?.frame_urls || []]
1172
+ });
1173
+ };
989
1174
  var normalizeObservedBrowserProfile = (value) => {
990
1175
  const source = isPlainObject(value) ? value : {};
991
1176
  const profile = {};
@@ -1169,6 +1354,18 @@ var RuntimeEnv = {
1169
1354
  normalizeAuth,
1170
1355
  normalizeBrowserProfileCore,
1171
1356
  normalizeObservedBrowserProfile,
1357
+ normalizeSnapshot(snapshot = {}) {
1358
+ return normalizeRawSnapshot(snapshot);
1359
+ },
1360
+ collectCookieUrls(snapshot = {}) {
1361
+ return collectCookieUrlsFromSnapshot(snapshot);
1362
+ },
1363
+ buildRuntimeEnvFromSnapshot(snapshot = {}, options = {}) {
1364
+ return buildEnvPayloadFromSnapshot(snapshot, options);
1365
+ },
1366
+ buildEnvPatchFromSnapshot(snapshot = {}, options = {}) {
1367
+ return buildEnvPayloadFromSnapshot(snapshot, options);
1368
+ },
1172
1369
  mergeEnvPatches(...patches) {
1173
1370
  return mergeEnvPatchObjects(...patches);
1174
1371
  },
@@ -1309,81 +1506,32 @@ var RuntimeEnv = {
1309
1506
  async captureEnvPatch(page, source = {}, options = {}) {
1310
1507
  const state = normalizeRuntimeState(source, options?.actor || "");
1311
1508
  const baseline = RuntimeEnv.buildEnvPatch(state) || {};
1312
- const patch = {
1313
- ...baseline
1314
- };
1315
1509
  if (!page || typeof page.evaluate !== "function" || typeof page.context !== "function") {
1316
- return Object.keys(patch).length > 0 ? patch : null;
1510
+ return Object.keys(baseline).length > 0 ? baseline : null;
1317
1511
  }
1318
1512
  try {
1319
- const contextCookies = await page.context().cookies();
1320
- const normalizedCookies = normalizeCookies(contextCookies || []);
1321
- patch.cookies = normalizedCookies;
1322
- } catch (error) {
1323
- }
1324
- try {
1325
- const snapshot = await page.evaluate(() => {
1326
- const localStorage2 = {};
1327
- const sessionStorage2 = {};
1328
- try {
1329
- for (let i = 0; i < window.localStorage.length; i += 1) {
1330
- const key = window.localStorage.key(i);
1331
- if (!key) continue;
1332
- const value = window.localStorage.getItem(key);
1333
- if (value !== null) localStorage2[key] = value;
1334
- }
1335
- } catch (error) {
1336
- }
1337
- try {
1338
- for (let i = 0; i < window.sessionStorage.length; i += 1) {
1339
- const key = window.sessionStorage.key(i);
1340
- if (!key) continue;
1341
- const value = window.sessionStorage.getItem(key);
1342
- if (value !== null) sessionStorage2[key] = value;
1343
- }
1344
- } catch (error) {
1345
- }
1346
- const nav = window.navigator || {};
1347
- const screen = window.screen || {};
1348
- const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "";
1349
- return {
1350
- localStorage: localStorage2,
1351
- sessionStorage: sessionStorage2,
1352
- browserProfileObserved: {
1353
- user_agent: nav.userAgent || "",
1354
- platform: nav.platform || "",
1355
- language: nav.language || "",
1356
- languages: Array.isArray(nav.languages) ? nav.languages : [],
1357
- hardware_concurrency: Number(nav.hardwareConcurrency || 0),
1358
- device_memory: Number(nav.deviceMemory || 0),
1359
- timezone,
1360
- viewport: {
1361
- width: Number(window.innerWidth || 0),
1362
- height: Number(window.innerHeight || 0)
1363
- },
1364
- screen: {
1365
- width: Number(screen.width || 0),
1366
- height: Number(screen.height || 0),
1367
- avail_width: Number(screen.availWidth || 0),
1368
- avail_height: Number(screen.availHeight || 0),
1369
- color_depth: Number(screen.colorDepth || 0),
1370
- pixel_depth: Number(screen.pixelDepth || 0)
1371
- }
1372
- }
1373
- };
1374
- });
1375
- const localStorage = normalizeLocalStorage(snapshot?.localStorage);
1376
- patch.local_storage = localStorage;
1377
- const sessionStorage = normalizeSessionStorage(snapshot?.sessionStorage);
1378
- patch.session_storage = sessionStorage;
1379
- const observedProfile = normalizeObservedBrowserProfile(snapshot?.browserProfileObserved);
1380
- const browserProfile = buildBrowserProfilePayload(state.browserProfileCore, observedProfile);
1381
- if (Object.keys(browserProfile).length > 0) {
1382
- patch.browser_profile = browserProfile;
1513
+ const rawSnapshot = await captureRawSnapshotFromPage(page);
1514
+ const cookieUrls = collectCookieUrlsFromSnapshot(rawSnapshot);
1515
+ let cookies = [];
1516
+ try {
1517
+ cookies = cookieUrls.length > 0 ? await page.context().cookies(cookieUrls) : await page.context().cookies();
1518
+ } catch {
1519
+ cookies = [];
1383
1520
  }
1384
- } catch (error) {
1521
+ const capturedPatch = RuntimeEnv.buildEnvPatchFromSnapshot(
1522
+ {
1523
+ ...rawSnapshot,
1524
+ cookies
1525
+ },
1526
+ {
1527
+ auth: state.auth,
1528
+ browserProfileCore: state.browserProfileCore
1529
+ }
1530
+ );
1531
+ return RuntimeEnv.mergeEnvPatches(baseline, capturedPatch);
1532
+ } catch {
1385
1533
  }
1386
- return Object.keys(patch).length > 0 ? patch : null;
1534
+ return Object.keys(baseline).length > 0 ? baseline : null;
1387
1535
  }
1388
1536
  };
1389
1537