@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.js CHANGED
@@ -962,6 +962,22 @@ var normalizeAuth = (value) => {
962
962
  return acc;
963
963
  }, {});
964
964
  };
965
+ var normalizeCookieSameSite = (value) => {
966
+ const raw = String(value || "").trim().toLowerCase();
967
+ if (!raw) return "";
968
+ switch (raw) {
969
+ case "strict":
970
+ return "Strict";
971
+ case "lax":
972
+ return "Lax";
973
+ case "none":
974
+ case "no_restriction":
975
+ return "None";
976
+ case "unspecified":
977
+ default:
978
+ return "";
979
+ }
980
+ };
965
981
  var normalizeCookies = (value) => {
966
982
  if (!Array.isArray(value)) return [];
967
983
  return value.map((item) => {
@@ -971,14 +987,14 @@ var normalizeCookies = (value) => {
971
987
  if (!name || !cookieValue || cookieValue === "<nil>") return null;
972
988
  const domain = String(raw.domain || "").trim();
973
989
  const path2 = String(raw.path || "").trim() || "/";
974
- const sameSiteRaw = String(raw.sameSite || "").trim();
990
+ const sameSite = normalizeCookieSameSite(raw.sameSite);
975
991
  return {
976
992
  ...raw,
977
993
  name,
978
994
  value: cookieValue,
979
995
  domain,
980
996
  path: path2,
981
- ...sameSiteRaw ? { sameSite: sameSiteRaw } : {}
997
+ ...sameSite ? { sameSite } : {}
982
998
  };
983
999
  }).filter(Boolean);
984
1000
  };
@@ -986,6 +1002,191 @@ var buildCookieMap = (cookies) => cookies.reduce((acc, item) => {
986
1002
  acc[item.name] = item.value;
987
1003
  return acc;
988
1004
  }, {});
1005
+ var normalizeHttpUrl = (value) => {
1006
+ const raw = String(value || "").trim();
1007
+ if (!raw) return "";
1008
+ try {
1009
+ const parsed = new URL(raw);
1010
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return "";
1011
+ parsed.hash = "";
1012
+ return parsed.toString();
1013
+ } catch {
1014
+ return "";
1015
+ }
1016
+ };
1017
+ var uniqueStrings = (value) => {
1018
+ const list = Array.isArray(value) ? value : [];
1019
+ const seen = /* @__PURE__ */ new Set();
1020
+ const result = [];
1021
+ list.forEach((item) => {
1022
+ const normalized = String(item || "").trim();
1023
+ if (!normalized || seen.has(normalized)) return;
1024
+ seen.add(normalized);
1025
+ result.push(normalized);
1026
+ });
1027
+ return result;
1028
+ };
1029
+ var normalizeHttpUrlList = (value) => uniqueStrings(
1030
+ (Array.isArray(value) ? value : [value]).map((item) => normalizeHttpUrl(item)).filter(Boolean)
1031
+ );
1032
+ var normalizeCookieDomain = (value) => String(value || "").trim().replace(/^\./, "").toLowerCase();
1033
+ var normalizeCookiePath = (value) => {
1034
+ const raw = String(value || "").trim();
1035
+ if (!raw) return "/";
1036
+ return raw.startsWith("/") ? raw : `/${raw}`;
1037
+ };
1038
+ var doesCookieMatchUrl = (cookie, url) => {
1039
+ if (!cookie || !url) return false;
1040
+ try {
1041
+ const parsed = new URL(url);
1042
+ const host = String(parsed.hostname || "").trim().toLowerCase();
1043
+ const pathname = String(parsed.pathname || "/").trim() || "/";
1044
+ const cookieDomain = normalizeCookieDomain(cookie.domain);
1045
+ const cookiePath = normalizeCookiePath(cookie.path);
1046
+ if (!host || !cookieDomain) return false;
1047
+ const domainMatched = host === cookieDomain || host.endsWith(`.${cookieDomain}`);
1048
+ if (!domainMatched) return false;
1049
+ if (cookie.secure && parsed.protocol !== "https:") return false;
1050
+ return pathname.startsWith(cookiePath);
1051
+ } catch {
1052
+ return false;
1053
+ }
1054
+ };
1055
+ var normalizeRawSnapshot = (value = {}) => {
1056
+ const source = isPlainObject(value) ? value : {};
1057
+ const pageUrl = normalizeHttpUrl(source.page_url ?? source.pageUrl);
1058
+ const frameUrls = normalizeHttpUrlList(source.frame_urls ?? source.frameUrls);
1059
+ const resourceUrls = normalizeHttpUrlList(source.resource_urls ?? source.resourceUrls);
1060
+ const browserProfileObservedSource = source.browser_profile_observed ?? source.browserProfileObserved ?? source.browser_profile?.observed ?? source.browserProfile?.observed ?? source.browserProfile;
1061
+ return {
1062
+ page_url: pageUrl,
1063
+ frame_urls: frameUrls,
1064
+ resource_urls: resourceUrls,
1065
+ cookies: Array.isArray(source.cookies) ? deepClone(source.cookies) : [],
1066
+ local_storage: normalizeLocalStorage(source.local_storage ?? source.localStorage),
1067
+ session_storage: normalizeSessionStorage(source.session_storage ?? source.sessionStorage),
1068
+ browser_profile_observed: normalizeObservedBrowserProfile(browserProfileObservedSource),
1069
+ auth: normalizeAuth(source.auth)
1070
+ };
1071
+ };
1072
+ var collectCookieUrlsFromSnapshot = (snapshot = {}) => {
1073
+ const normalized = normalizeRawSnapshot(snapshot);
1074
+ return uniqueStrings([
1075
+ normalized.page_url,
1076
+ ...normalized.frame_urls,
1077
+ ...normalized.resource_urls
1078
+ ].filter(Boolean));
1079
+ };
1080
+ var filterCookiesBySnapshot = (cookies = [], snapshot = {}) => {
1081
+ const normalizedCookies = normalizeCookies(cookies);
1082
+ const cookieUrls = collectCookieUrlsFromSnapshot(snapshot);
1083
+ if (cookieUrls.length === 0) {
1084
+ return normalizedCookies;
1085
+ }
1086
+ return normalizedCookies.filter((cookie) => cookieUrls.some((url) => doesCookieMatchUrl(cookie, url)));
1087
+ };
1088
+ var buildEnvPayloadFromSnapshot = (snapshot = {}, options = {}) => {
1089
+ const normalized = normalizeRawSnapshot(snapshot);
1090
+ const cookies = filterCookiesBySnapshot(snapshot?.cookies || normalized.cookies, normalized);
1091
+ const localStorage = normalized.local_storage;
1092
+ const sessionStorage = normalized.session_storage;
1093
+ const auth = normalizeAuth(options.auth ?? normalized.auth);
1094
+ const browserProfileCore = normalizeBrowserProfileCore(
1095
+ options.browserProfileCore ?? snapshot?.browser_profile?.core ?? snapshot?.browserProfile?.core
1096
+ );
1097
+ const browserProfile = buildBrowserProfilePayload(browserProfileCore, normalized.browser_profile_observed);
1098
+ const payload = {
1099
+ ...cookies.length > 0 ? { cookies } : {},
1100
+ ...Object.keys(localStorage).length > 0 ? { local_storage: localStorage } : {},
1101
+ ...Object.keys(sessionStorage).length > 0 ? { session_storage: sessionStorage } : {},
1102
+ ...Object.keys(auth).length > 0 ? { auth } : {},
1103
+ ...Object.keys(browserProfile).length > 0 ? { browser_profile: browserProfile } : {}
1104
+ };
1105
+ return Object.keys(payload).length > 0 ? payload : null;
1106
+ };
1107
+ var captureRawSnapshotFromPage = async (page) => {
1108
+ const frameUrls = typeof page?.frames === "function" ? page.frames().map((frame) => {
1109
+ try {
1110
+ return frame.url();
1111
+ } catch {
1112
+ return "";
1113
+ }
1114
+ }) : [];
1115
+ const snapshot = await page.evaluate(() => {
1116
+ const localStorage = {};
1117
+ const sessionStorage = {};
1118
+ const resourceUrls = [];
1119
+ const frameUrls2 = [];
1120
+ try {
1121
+ for (let i = 0; i < window.localStorage.length; i += 1) {
1122
+ const key = window.localStorage.key(i);
1123
+ if (!key) continue;
1124
+ const value = window.localStorage.getItem(key);
1125
+ if (value !== null) localStorage[key] = value;
1126
+ }
1127
+ } catch {
1128
+ }
1129
+ try {
1130
+ for (let i = 0; i < window.sessionStorage.length; i += 1) {
1131
+ const key = window.sessionStorage.key(i);
1132
+ if (!key) continue;
1133
+ const value = window.sessionStorage.getItem(key);
1134
+ if (value !== null) sessionStorage[key] = value;
1135
+ }
1136
+ } catch {
1137
+ }
1138
+ try {
1139
+ const perfEntries = performance.getEntriesByType("resource") || [];
1140
+ perfEntries.forEach((entry) => {
1141
+ const name = String(entry?.name || "").trim();
1142
+ if (name) resourceUrls.push(name);
1143
+ });
1144
+ } catch {
1145
+ }
1146
+ try {
1147
+ document.querySelectorAll("iframe,frame").forEach((node) => {
1148
+ const src = String(node?.src || "").trim();
1149
+ if (src) frameUrls2.push(src);
1150
+ });
1151
+ } catch {
1152
+ }
1153
+ const nav = window.navigator || {};
1154
+ const screen = window.screen || {};
1155
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "";
1156
+ return {
1157
+ page_url: window.location.href || "",
1158
+ frame_urls: frameUrls2,
1159
+ resource_urls: resourceUrls,
1160
+ local_storage: localStorage,
1161
+ session_storage: sessionStorage,
1162
+ browser_profile_observed: {
1163
+ user_agent: nav.userAgent || "",
1164
+ platform: nav.platform || "",
1165
+ language: nav.language || "",
1166
+ languages: Array.isArray(nav.languages) ? nav.languages : [],
1167
+ hardware_concurrency: Number(nav.hardwareConcurrency || 0),
1168
+ device_memory: Number(nav.deviceMemory || 0),
1169
+ timezone,
1170
+ viewport: {
1171
+ width: Number(window.innerWidth || 0),
1172
+ height: Number(window.innerHeight || 0)
1173
+ },
1174
+ screen: {
1175
+ width: Number(screen.width || 0),
1176
+ height: Number(screen.height || 0),
1177
+ avail_width: Number(screen.availWidth || 0),
1178
+ avail_height: Number(screen.availHeight || 0),
1179
+ color_depth: Number(screen.colorDepth || 0),
1180
+ pixel_depth: Number(screen.pixelDepth || 0)
1181
+ }
1182
+ }
1183
+ };
1184
+ });
1185
+ return normalizeRawSnapshot({
1186
+ ...snapshot,
1187
+ frame_urls: [...frameUrls, ...snapshot?.frame_urls || []]
1188
+ });
1189
+ };
989
1190
  var normalizeObservedBrowserProfile = (value) => {
990
1191
  const source = isPlainObject(value) ? value : {};
991
1192
  const profile = {};
@@ -1169,6 +1370,18 @@ var RuntimeEnv = {
1169
1370
  normalizeAuth,
1170
1371
  normalizeBrowserProfileCore,
1171
1372
  normalizeObservedBrowserProfile,
1373
+ normalizeSnapshot(snapshot = {}) {
1374
+ return normalizeRawSnapshot(snapshot);
1375
+ },
1376
+ collectCookieUrls(snapshot = {}) {
1377
+ return collectCookieUrlsFromSnapshot(snapshot);
1378
+ },
1379
+ buildRuntimeEnvFromSnapshot(snapshot = {}, options = {}) {
1380
+ return buildEnvPayloadFromSnapshot(snapshot, options);
1381
+ },
1382
+ buildEnvPatchFromSnapshot(snapshot = {}, options = {}) {
1383
+ return buildEnvPayloadFromSnapshot(snapshot, options);
1384
+ },
1172
1385
  mergeEnvPatches(...patches) {
1173
1386
  return mergeEnvPatchObjects(...patches);
1174
1387
  },
@@ -1309,81 +1522,32 @@ var RuntimeEnv = {
1309
1522
  async captureEnvPatch(page, source = {}, options = {}) {
1310
1523
  const state = normalizeRuntimeState(source, options?.actor || "");
1311
1524
  const baseline = RuntimeEnv.buildEnvPatch(state) || {};
1312
- const patch = {
1313
- ...baseline
1314
- };
1315
1525
  if (!page || typeof page.evaluate !== "function" || typeof page.context !== "function") {
1316
- return Object.keys(patch).length > 0 ? patch : null;
1317
- }
1318
- try {
1319
- const contextCookies = await page.context().cookies();
1320
- const normalizedCookies = normalizeCookies(contextCookies || []);
1321
- patch.cookies = normalizedCookies;
1322
- } catch (error) {
1526
+ return Object.keys(baseline).length > 0 ? baseline : null;
1323
1527
  }
1324
1528
  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;
1529
+ const rawSnapshot = await captureRawSnapshotFromPage(page);
1530
+ const cookieUrls = collectCookieUrlsFromSnapshot(rawSnapshot);
1531
+ let cookies = [];
1532
+ try {
1533
+ cookies = cookieUrls.length > 0 ? await page.context().cookies(cookieUrls) : await page.context().cookies();
1534
+ } catch {
1535
+ cookies = [];
1383
1536
  }
1384
- } catch (error) {
1537
+ const capturedPatch = RuntimeEnv.buildEnvPatchFromSnapshot(
1538
+ {
1539
+ ...rawSnapshot,
1540
+ cookies
1541
+ },
1542
+ {
1543
+ auth: state.auth,
1544
+ browserProfileCore: state.browserProfileCore
1545
+ }
1546
+ );
1547
+ return RuntimeEnv.mergeEnvPatches(baseline, capturedPatch);
1548
+ } catch {
1385
1549
  }
1386
- return Object.keys(patch).length > 0 ? patch : null;
1550
+ return Object.keys(baseline).length > 0 ? baseline : null;
1387
1551
  }
1388
1552
  };
1389
1553