@skrillex1224/playwright-toolkit 2.1.183 → 2.1.185

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.cjs CHANGED
@@ -990,6 +990,22 @@ var normalizeAuth = (value) => {
990
990
  return acc;
991
991
  }, {});
992
992
  };
993
+ var normalizeCookieSameSite = (value) => {
994
+ const raw = String(value || "").trim().toLowerCase();
995
+ if (!raw) return "";
996
+ switch (raw) {
997
+ case "strict":
998
+ return "Strict";
999
+ case "lax":
1000
+ return "Lax";
1001
+ case "none":
1002
+ case "no_restriction":
1003
+ return "None";
1004
+ case "unspecified":
1005
+ default:
1006
+ return "";
1007
+ }
1008
+ };
993
1009
  var normalizeCookies = (value) => {
994
1010
  if (!Array.isArray(value)) return [];
995
1011
  return value.map((item) => {
@@ -999,14 +1015,14 @@ var normalizeCookies = (value) => {
999
1015
  if (!name || !cookieValue || cookieValue === "<nil>") return null;
1000
1016
  const domain = String(raw.domain || "").trim();
1001
1017
  const path2 = String(raw.path || "").trim() || "/";
1002
- const sameSiteRaw = String(raw.sameSite || "").trim();
1018
+ const sameSite = normalizeCookieSameSite(raw.sameSite);
1003
1019
  return {
1004
1020
  ...raw,
1005
1021
  name,
1006
1022
  value: cookieValue,
1007
1023
  domain,
1008
1024
  path: path2,
1009
- ...sameSiteRaw ? { sameSite: sameSiteRaw } : {}
1025
+ ...sameSite ? { sameSite } : {}
1010
1026
  };
1011
1027
  }).filter(Boolean);
1012
1028
  };
@@ -1014,6 +1030,191 @@ var buildCookieMap = (cookies) => cookies.reduce((acc, item) => {
1014
1030
  acc[item.name] = item.value;
1015
1031
  return acc;
1016
1032
  }, {});
1033
+ var normalizeHttpUrl = (value) => {
1034
+ const raw = String(value || "").trim();
1035
+ if (!raw) return "";
1036
+ try {
1037
+ const parsed = new URL(raw);
1038
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return "";
1039
+ parsed.hash = "";
1040
+ return parsed.toString();
1041
+ } catch {
1042
+ return "";
1043
+ }
1044
+ };
1045
+ var uniqueStrings = (value) => {
1046
+ const list = Array.isArray(value) ? value : [];
1047
+ const seen = /* @__PURE__ */ new Set();
1048
+ const result = [];
1049
+ list.forEach((item) => {
1050
+ const normalized = String(item || "").trim();
1051
+ if (!normalized || seen.has(normalized)) return;
1052
+ seen.add(normalized);
1053
+ result.push(normalized);
1054
+ });
1055
+ return result;
1056
+ };
1057
+ var normalizeHttpUrlList = (value) => uniqueStrings(
1058
+ (Array.isArray(value) ? value : [value]).map((item) => normalizeHttpUrl(item)).filter(Boolean)
1059
+ );
1060
+ var normalizeCookieDomain = (value) => String(value || "").trim().replace(/^\./, "").toLowerCase();
1061
+ var normalizeCookiePath = (value) => {
1062
+ const raw = String(value || "").trim();
1063
+ if (!raw) return "/";
1064
+ return raw.startsWith("/") ? raw : `/${raw}`;
1065
+ };
1066
+ var doesCookieMatchUrl = (cookie, url) => {
1067
+ if (!cookie || !url) return false;
1068
+ try {
1069
+ const parsed = new URL(url);
1070
+ const host = String(parsed.hostname || "").trim().toLowerCase();
1071
+ const pathname = String(parsed.pathname || "/").trim() || "/";
1072
+ const cookieDomain = normalizeCookieDomain(cookie.domain);
1073
+ const cookiePath = normalizeCookiePath(cookie.path);
1074
+ if (!host || !cookieDomain) return false;
1075
+ const domainMatched = host === cookieDomain || host.endsWith(`.${cookieDomain}`);
1076
+ if (!domainMatched) return false;
1077
+ if (cookie.secure && parsed.protocol !== "https:") return false;
1078
+ return pathname.startsWith(cookiePath);
1079
+ } catch {
1080
+ return false;
1081
+ }
1082
+ };
1083
+ var normalizeRawSnapshot = (value = {}) => {
1084
+ const source = isPlainObject(value) ? value : {};
1085
+ const pageUrl = normalizeHttpUrl(source.page_url ?? source.pageUrl);
1086
+ const frameUrls = normalizeHttpUrlList(source.frame_urls ?? source.frameUrls);
1087
+ const resourceUrls = normalizeHttpUrlList(source.resource_urls ?? source.resourceUrls);
1088
+ const browserProfileObservedSource = source.browser_profile_observed ?? source.browserProfileObserved ?? source.browser_profile?.observed ?? source.browserProfile?.observed ?? source.browserProfile;
1089
+ return {
1090
+ page_url: pageUrl,
1091
+ frame_urls: frameUrls,
1092
+ resource_urls: resourceUrls,
1093
+ cookies: Array.isArray(source.cookies) ? deepClone(source.cookies) : [],
1094
+ local_storage: normalizeLocalStorage(source.local_storage ?? source.localStorage),
1095
+ session_storage: normalizeSessionStorage(source.session_storage ?? source.sessionStorage),
1096
+ browser_profile_observed: normalizeObservedBrowserProfile(browserProfileObservedSource),
1097
+ auth: normalizeAuth(source.auth)
1098
+ };
1099
+ };
1100
+ var collectCookieUrlsFromSnapshot = (snapshot = {}) => {
1101
+ const normalized = normalizeRawSnapshot(snapshot);
1102
+ return uniqueStrings([
1103
+ normalized.page_url,
1104
+ ...normalized.frame_urls,
1105
+ ...normalized.resource_urls
1106
+ ].filter(Boolean));
1107
+ };
1108
+ var filterCookiesBySnapshot = (cookies = [], snapshot = {}) => {
1109
+ const normalizedCookies = normalizeCookies(cookies);
1110
+ const cookieUrls = collectCookieUrlsFromSnapshot(snapshot);
1111
+ if (cookieUrls.length === 0) {
1112
+ return normalizedCookies;
1113
+ }
1114
+ return normalizedCookies.filter((cookie) => cookieUrls.some((url) => doesCookieMatchUrl(cookie, url)));
1115
+ };
1116
+ var buildEnvPayloadFromSnapshot = (snapshot = {}, options = {}) => {
1117
+ const normalized = normalizeRawSnapshot(snapshot);
1118
+ const cookies = filterCookiesBySnapshot(snapshot?.cookies || normalized.cookies, normalized);
1119
+ const localStorage = normalized.local_storage;
1120
+ const sessionStorage = normalized.session_storage;
1121
+ const auth = normalizeAuth(options.auth ?? normalized.auth);
1122
+ const browserProfileCore = normalizeBrowserProfileCore(
1123
+ options.browserProfileCore ?? snapshot?.browser_profile?.core ?? snapshot?.browserProfile?.core
1124
+ );
1125
+ const browserProfile = buildBrowserProfilePayload(browserProfileCore, normalized.browser_profile_observed);
1126
+ const payload = {
1127
+ ...cookies.length > 0 ? { cookies } : {},
1128
+ ...Object.keys(localStorage).length > 0 ? { local_storage: localStorage } : {},
1129
+ ...Object.keys(sessionStorage).length > 0 ? { session_storage: sessionStorage } : {},
1130
+ ...Object.keys(auth).length > 0 ? { auth } : {},
1131
+ ...Object.keys(browserProfile).length > 0 ? { browser_profile: browserProfile } : {}
1132
+ };
1133
+ return Object.keys(payload).length > 0 ? payload : null;
1134
+ };
1135
+ var captureRawSnapshotFromPage = async (page) => {
1136
+ const frameUrls = typeof page?.frames === "function" ? page.frames().map((frame) => {
1137
+ try {
1138
+ return frame.url();
1139
+ } catch {
1140
+ return "";
1141
+ }
1142
+ }) : [];
1143
+ const snapshot = await page.evaluate(() => {
1144
+ const localStorage = {};
1145
+ const sessionStorage = {};
1146
+ const resourceUrls = [];
1147
+ const frameUrls2 = [];
1148
+ try {
1149
+ for (let i = 0; i < window.localStorage.length; i += 1) {
1150
+ const key = window.localStorage.key(i);
1151
+ if (!key) continue;
1152
+ const value = window.localStorage.getItem(key);
1153
+ if (value !== null) localStorage[key] = value;
1154
+ }
1155
+ } catch {
1156
+ }
1157
+ try {
1158
+ for (let i = 0; i < window.sessionStorage.length; i += 1) {
1159
+ const key = window.sessionStorage.key(i);
1160
+ if (!key) continue;
1161
+ const value = window.sessionStorage.getItem(key);
1162
+ if (value !== null) sessionStorage[key] = value;
1163
+ }
1164
+ } catch {
1165
+ }
1166
+ try {
1167
+ const perfEntries = performance.getEntriesByType("resource") || [];
1168
+ perfEntries.forEach((entry) => {
1169
+ const name = String(entry?.name || "").trim();
1170
+ if (name) resourceUrls.push(name);
1171
+ });
1172
+ } catch {
1173
+ }
1174
+ try {
1175
+ document.querySelectorAll("iframe,frame").forEach((node) => {
1176
+ const src = String(node?.src || "").trim();
1177
+ if (src) frameUrls2.push(src);
1178
+ });
1179
+ } catch {
1180
+ }
1181
+ const nav = window.navigator || {};
1182
+ const screen = window.screen || {};
1183
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "";
1184
+ return {
1185
+ page_url: window.location.href || "",
1186
+ frame_urls: frameUrls2,
1187
+ resource_urls: resourceUrls,
1188
+ local_storage: localStorage,
1189
+ session_storage: sessionStorage,
1190
+ browser_profile_observed: {
1191
+ user_agent: nav.userAgent || "",
1192
+ platform: nav.platform || "",
1193
+ language: nav.language || "",
1194
+ languages: Array.isArray(nav.languages) ? nav.languages : [],
1195
+ hardware_concurrency: Number(nav.hardwareConcurrency || 0),
1196
+ device_memory: Number(nav.deviceMemory || 0),
1197
+ timezone,
1198
+ viewport: {
1199
+ width: Number(window.innerWidth || 0),
1200
+ height: Number(window.innerHeight || 0)
1201
+ },
1202
+ screen: {
1203
+ width: Number(screen.width || 0),
1204
+ height: Number(screen.height || 0),
1205
+ avail_width: Number(screen.availWidth || 0),
1206
+ avail_height: Number(screen.availHeight || 0),
1207
+ color_depth: Number(screen.colorDepth || 0),
1208
+ pixel_depth: Number(screen.pixelDepth || 0)
1209
+ }
1210
+ }
1211
+ };
1212
+ });
1213
+ return normalizeRawSnapshot({
1214
+ ...snapshot,
1215
+ frame_urls: [...frameUrls, ...snapshot?.frame_urls || []]
1216
+ });
1217
+ };
1017
1218
  var normalizeObservedBrowserProfile = (value) => {
1018
1219
  const source = isPlainObject(value) ? value : {};
1019
1220
  const profile = {};
@@ -1197,6 +1398,18 @@ var RuntimeEnv = {
1197
1398
  normalizeAuth,
1198
1399
  normalizeBrowserProfileCore,
1199
1400
  normalizeObservedBrowserProfile,
1401
+ normalizeSnapshot(snapshot = {}) {
1402
+ return normalizeRawSnapshot(snapshot);
1403
+ },
1404
+ collectCookieUrls(snapshot = {}) {
1405
+ return collectCookieUrlsFromSnapshot(snapshot);
1406
+ },
1407
+ buildRuntimeEnvFromSnapshot(snapshot = {}, options = {}) {
1408
+ return buildEnvPayloadFromSnapshot(snapshot, options);
1409
+ },
1410
+ buildEnvPatchFromSnapshot(snapshot = {}, options = {}) {
1411
+ return buildEnvPayloadFromSnapshot(snapshot, options);
1412
+ },
1200
1413
  mergeEnvPatches(...patches) {
1201
1414
  return mergeEnvPatchObjects(...patches);
1202
1415
  },
@@ -1337,81 +1550,32 @@ var RuntimeEnv = {
1337
1550
  async captureEnvPatch(page, source = {}, options = {}) {
1338
1551
  const state = normalizeRuntimeState(source, options?.actor || "");
1339
1552
  const baseline = RuntimeEnv.buildEnvPatch(state) || {};
1340
- const patch = {
1341
- ...baseline
1342
- };
1343
1553
  if (!page || typeof page.evaluate !== "function" || typeof page.context !== "function") {
1344
- return Object.keys(patch).length > 0 ? patch : null;
1345
- }
1346
- try {
1347
- const contextCookies = await page.context().cookies();
1348
- const normalizedCookies = normalizeCookies(contextCookies || []);
1349
- patch.cookies = normalizedCookies;
1350
- } catch (error) {
1554
+ return Object.keys(baseline).length > 0 ? baseline : null;
1351
1555
  }
1352
1556
  try {
1353
- const snapshot = await page.evaluate(() => {
1354
- const localStorage2 = {};
1355
- const sessionStorage2 = {};
1356
- try {
1357
- for (let i = 0; i < window.localStorage.length; i += 1) {
1358
- const key = window.localStorage.key(i);
1359
- if (!key) continue;
1360
- const value = window.localStorage.getItem(key);
1361
- if (value !== null) localStorage2[key] = value;
1362
- }
1363
- } catch (error) {
1364
- }
1365
- try {
1366
- for (let i = 0; i < window.sessionStorage.length; i += 1) {
1367
- const key = window.sessionStorage.key(i);
1368
- if (!key) continue;
1369
- const value = window.sessionStorage.getItem(key);
1370
- if (value !== null) sessionStorage2[key] = value;
1371
- }
1372
- } catch (error) {
1373
- }
1374
- const nav = window.navigator || {};
1375
- const screen = window.screen || {};
1376
- const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "";
1377
- return {
1378
- localStorage: localStorage2,
1379
- sessionStorage: sessionStorage2,
1380
- browserProfileObserved: {
1381
- user_agent: nav.userAgent || "",
1382
- platform: nav.platform || "",
1383
- language: nav.language || "",
1384
- languages: Array.isArray(nav.languages) ? nav.languages : [],
1385
- hardware_concurrency: Number(nav.hardwareConcurrency || 0),
1386
- device_memory: Number(nav.deviceMemory || 0),
1387
- timezone,
1388
- viewport: {
1389
- width: Number(window.innerWidth || 0),
1390
- height: Number(window.innerHeight || 0)
1391
- },
1392
- screen: {
1393
- width: Number(screen.width || 0),
1394
- height: Number(screen.height || 0),
1395
- avail_width: Number(screen.availWidth || 0),
1396
- avail_height: Number(screen.availHeight || 0),
1397
- color_depth: Number(screen.colorDepth || 0),
1398
- pixel_depth: Number(screen.pixelDepth || 0)
1399
- }
1400
- }
1401
- };
1402
- });
1403
- const localStorage = normalizeLocalStorage(snapshot?.localStorage);
1404
- patch.local_storage = localStorage;
1405
- const sessionStorage = normalizeSessionStorage(snapshot?.sessionStorage);
1406
- patch.session_storage = sessionStorage;
1407
- const observedProfile = normalizeObservedBrowserProfile(snapshot?.browserProfileObserved);
1408
- const browserProfile = buildBrowserProfilePayload(state.browserProfileCore, observedProfile);
1409
- if (Object.keys(browserProfile).length > 0) {
1410
- patch.browser_profile = browserProfile;
1557
+ const rawSnapshot = await captureRawSnapshotFromPage(page);
1558
+ const cookieUrls = collectCookieUrlsFromSnapshot(rawSnapshot);
1559
+ let cookies = [];
1560
+ try {
1561
+ cookies = cookieUrls.length > 0 ? await page.context().cookies(cookieUrls) : await page.context().cookies();
1562
+ } catch {
1563
+ cookies = [];
1411
1564
  }
1412
- } catch (error) {
1565
+ const capturedPatch = RuntimeEnv.buildEnvPatchFromSnapshot(
1566
+ {
1567
+ ...rawSnapshot,
1568
+ cookies
1569
+ },
1570
+ {
1571
+ auth: state.auth,
1572
+ browserProfileCore: state.browserProfileCore
1573
+ }
1574
+ );
1575
+ return RuntimeEnv.mergeEnvPatches(baseline, capturedPatch);
1576
+ } catch {
1413
1577
  }
1414
- return Object.keys(patch).length > 0 ? patch : null;
1578
+ return Object.keys(baseline).length > 0 ? baseline : null;
1415
1579
  }
1416
1580
  };
1417
1581