sweetlink 0.2.0 → 0.2.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.
Files changed (229) hide show
  1. package/CHANGELOG.md +6 -2
  2. package/LICENSE +1 -1
  3. package/README.md +6 -6
  4. package/dist/daemon/src/codename.d.ts.map +1 -1
  5. package/dist/daemon/src/codename.js +11 -11
  6. package/dist/daemon/src/codename.js.map +1 -1
  7. package/dist/daemon/src/index.d.ts +1 -1
  8. package/dist/daemon/src/index.js +131 -122
  9. package/dist/daemon/src/index.js.map +1 -1
  10. package/dist/shared/src/env.d.ts.map +1 -1
  11. package/dist/shared/src/env.js +12 -12
  12. package/dist/shared/src/env.js.map +1 -1
  13. package/dist/shared/src/index.d.ts +30 -30
  14. package/dist/shared/src/index.d.ts.map +1 -1
  15. package/dist/shared/src/index.js +17 -17
  16. package/dist/shared/src/index.js.map +1 -1
  17. package/dist/shared/src/node.d.ts +1 -1
  18. package/dist/shared/src/node.d.ts.map +1 -1
  19. package/dist/shared/src/node.js +18 -18
  20. package/dist/shared/src/node.js.map +1 -1
  21. package/dist/src/codex.d.ts +1 -1
  22. package/dist/src/codex.js +19 -19
  23. package/dist/src/codex.js.map +1 -1
  24. package/dist/src/commands/click.d.ts +1 -1
  25. package/dist/src/commands/click.d.ts.map +1 -1
  26. package/dist/src/commands/click.js +32 -24
  27. package/dist/src/commands/click.js.map +1 -1
  28. package/dist/src/commands/run-js.d.ts +1 -1
  29. package/dist/src/commands/run-js.d.ts.map +1 -1
  30. package/dist/src/commands/run-js.js +10 -10
  31. package/dist/src/commands/run-js.js.map +1 -1
  32. package/dist/src/commands/trust-ca.d.ts +1 -1
  33. package/dist/src/commands/trust-ca.js +15 -15
  34. package/dist/src/core/config-file.d.ts.map +1 -1
  35. package/dist/src/core/config-file.js +49 -41
  36. package/dist/src/core/config-file.js.map +1 -1
  37. package/dist/src/core/config.d.ts +2 -2
  38. package/dist/src/core/config.d.ts.map +1 -1
  39. package/dist/src/core/config.js +22 -18
  40. package/dist/src/core/config.js.map +1 -1
  41. package/dist/src/core/dev-bootstrap.d.ts +1 -1
  42. package/dist/src/core/dev-bootstrap.d.ts.map +1 -1
  43. package/dist/src/core/dev-bootstrap.js +12 -12
  44. package/dist/src/core/dev-bootstrap.js.map +1 -1
  45. package/dist/src/core/env.d.ts +1 -1
  46. package/dist/src/core/env.d.ts.map +1 -1
  47. package/dist/src/core/env.js +3 -3
  48. package/dist/src/core/env.js.map +1 -1
  49. package/dist/src/devtools-registry.d.ts.map +1 -1
  50. package/dist/src/devtools-registry.js +48 -46
  51. package/dist/src/devtools-registry.js.map +1 -1
  52. package/dist/src/env.d.ts +1 -1
  53. package/dist/src/env.js +1 -1
  54. package/dist/src/http.d.ts.map +1 -1
  55. package/dist/src/http.js +7 -6
  56. package/dist/src/http.js.map +1 -1
  57. package/dist/src/index.d.ts +7 -7
  58. package/dist/src/index.d.ts.map +1 -1
  59. package/dist/src/index.js +399 -367
  60. package/dist/src/index.js.map +1 -1
  61. package/dist/src/runtime/browser/client.d.ts +3 -3
  62. package/dist/src/runtime/browser/client.d.ts.map +1 -1
  63. package/dist/src/runtime/browser/client.js +79 -72
  64. package/dist/src/runtime/browser/client.js.map +1 -1
  65. package/dist/src/runtime/browser/commands/index.d.ts +2 -2
  66. package/dist/src/runtime/browser/commands/index.js +17 -17
  67. package/dist/src/runtime/browser/commands/index.js.map +1 -1
  68. package/dist/src/runtime/browser/dom-to-image-loader.d.ts.map +1 -1
  69. package/dist/src/runtime/browser/dom-to-image-loader.js +7 -7
  70. package/dist/src/runtime/browser/dom-to-image-loader.js.map +1 -1
  71. package/dist/src/runtime/browser/index.d.ts +3 -3
  72. package/dist/src/runtime/browser/index.js +2 -2
  73. package/dist/src/runtime/browser/module-loader.js +2 -2
  74. package/dist/src/runtime/browser/screenshot/hooks.d.ts +5 -5
  75. package/dist/src/runtime/browser/screenshot/hooks.d.ts.map +1 -1
  76. package/dist/src/runtime/browser/screenshot/hooks.js +32 -29
  77. package/dist/src/runtime/browser/screenshot/hooks.js.map +1 -1
  78. package/dist/src/runtime/browser/screenshot/index.d.ts +3 -3
  79. package/dist/src/runtime/browser/screenshot/index.d.ts.map +1 -1
  80. package/dist/src/runtime/browser/screenshot/index.js +17 -15
  81. package/dist/src/runtime/browser/screenshot/index.js.map +1 -1
  82. package/dist/src/runtime/browser/screenshot/renderers/dom-to-image.d.ts +1 -1
  83. package/dist/src/runtime/browser/screenshot/renderers/dom-to-image.js +13 -13
  84. package/dist/src/runtime/browser/screenshot/renderers/dom-to-image.js.map +1 -1
  85. package/dist/src/runtime/browser/screenshot/renderers/html2canvas.d.ts +1 -1
  86. package/dist/src/runtime/browser/screenshot/renderers/html2canvas.d.ts.map +1 -1
  87. package/dist/src/runtime/browser/screenshot/renderers/html2canvas.js +24 -24
  88. package/dist/src/runtime/browser/screenshot/renderers/html2canvas.js.map +1 -1
  89. package/dist/src/runtime/browser/screenshot/targets.d.ts +2 -2
  90. package/dist/src/runtime/browser/screenshot/targets.js +9 -9
  91. package/dist/src/runtime/browser/screenshot/utils.d.ts +1 -1
  92. package/dist/src/runtime/browser/screenshot/utils.d.ts.map +1 -1
  93. package/dist/src/runtime/browser/screenshot/utils.js +73 -71
  94. package/dist/src/runtime/browser/screenshot/utils.js.map +1 -1
  95. package/dist/src/runtime/browser/selector-discovery.d.ts +1 -1
  96. package/dist/src/runtime/browser/selector-discovery.d.ts.map +1 -1
  97. package/dist/src/runtime/browser/selector-discovery.js +24 -22
  98. package/dist/src/runtime/browser/selector-discovery.js.map +1 -1
  99. package/dist/src/runtime/browser/storage/session-storage.d.ts +1 -1
  100. package/dist/src/runtime/browser/storage/session-storage.d.ts.map +1 -1
  101. package/dist/src/runtime/browser/storage/session-storage.js +12 -10
  102. package/dist/src/runtime/browser/storage/session-storage.js.map +1 -1
  103. package/dist/src/runtime/browser/types.d.ts +7 -7
  104. package/dist/src/runtime/browser/types.js +1 -1
  105. package/dist/src/runtime/browser/utils/console.d.ts +1 -1
  106. package/dist/src/runtime/browser/utils/console.js +16 -16
  107. package/dist/src/runtime/browser/utils/console.js.map +1 -1
  108. package/dist/src/runtime/browser/utils/environment.js +2 -2
  109. package/dist/src/runtime/browser/utils/errors.d.ts.map +1 -1
  110. package/dist/src/runtime/browser/utils/errors.js +11 -11
  111. package/dist/src/runtime/browser/utils/number.d.ts.map +1 -1
  112. package/dist/src/runtime/browser/utils/number.js.map +1 -1
  113. package/dist/src/runtime/browser/utils/object.d.ts.map +1 -1
  114. package/dist/src/runtime/browser/utils/object.js +2 -2
  115. package/dist/src/runtime/browser/utils/object.js.map +1 -1
  116. package/dist/src/runtime/browser/utils/sanitize.js +2 -2
  117. package/dist/src/runtime/browser/utils/time.d.ts.map +1 -1
  118. package/dist/src/runtime/browser/utils/time.js +1 -1
  119. package/dist/src/runtime/chrome/cookies.d.ts +4 -4
  120. package/dist/src/runtime/chrome/cookies.d.ts.map +1 -1
  121. package/dist/src/runtime/chrome/cookies.js +31 -31
  122. package/dist/src/runtime/chrome/cookies.js.map +1 -1
  123. package/dist/src/runtime/chrome/deep-link.d.ts +1 -1
  124. package/dist/src/runtime/chrome/deep-link.d.ts.map +1 -1
  125. package/dist/src/runtime/chrome/deep-link.js +46 -36
  126. package/dist/src/runtime/chrome/deep-link.js.map +1 -1
  127. package/dist/src/runtime/chrome/diagnostics.d.ts.map +1 -1
  128. package/dist/src/runtime/chrome/diagnostics.js +13 -11
  129. package/dist/src/runtime/chrome/diagnostics.js.map +1 -1
  130. package/dist/src/runtime/chrome/focus.d.ts.map +1 -1
  131. package/dist/src/runtime/chrome/focus.js +5 -5
  132. package/dist/src/runtime/chrome/focus.js.map +1 -1
  133. package/dist/src/runtime/chrome/launch.d.ts.map +1 -1
  134. package/dist/src/runtime/chrome/launch.js +30 -30
  135. package/dist/src/runtime/chrome/launch.js.map +1 -1
  136. package/dist/src/runtime/chrome/puppeteer.d.ts +2 -2
  137. package/dist/src/runtime/chrome/puppeteer.d.ts.map +1 -1
  138. package/dist/src/runtime/chrome/puppeteer.js +16 -11
  139. package/dist/src/runtime/chrome/puppeteer.js.map +1 -1
  140. package/dist/src/runtime/chrome/reuse.d.ts.map +1 -1
  141. package/dist/src/runtime/chrome/reuse.js +38 -35
  142. package/dist/src/runtime/chrome/reuse.js.map +1 -1
  143. package/dist/src/runtime/chrome/session.d.ts +1 -1
  144. package/dist/src/runtime/chrome/session.d.ts.map +1 -1
  145. package/dist/src/runtime/chrome/session.js +12 -10
  146. package/dist/src/runtime/chrome/session.js.map +1 -1
  147. package/dist/src/runtime/chrome.d.ts +8 -8
  148. package/dist/src/runtime/chrome.js +8 -8
  149. package/dist/src/runtime/cookies.d.ts +1 -16
  150. package/dist/src/runtime/cookies.d.ts.map +1 -1
  151. package/dist/src/runtime/cookies.js +175 -286
  152. package/dist/src/runtime/cookies.js.map +1 -1
  153. package/dist/src/runtime/devstack.d.ts +1 -1
  154. package/dist/src/runtime/devstack.d.ts.map +1 -1
  155. package/dist/src/runtime/devstack.js +31 -28
  156. package/dist/src/runtime/devstack.js.map +1 -1
  157. package/dist/src/runtime/devtools/background.js +22 -22
  158. package/dist/src/runtime/devtools/cdp.d.ts +3 -3
  159. package/dist/src/runtime/devtools/cdp.d.ts.map +1 -1
  160. package/dist/src/runtime/devtools/cdp.js +59 -55
  161. package/dist/src/runtime/devtools/cdp.js.map +1 -1
  162. package/dist/src/runtime/devtools/config.d.ts.map +1 -1
  163. package/dist/src/runtime/devtools/config.js +15 -15
  164. package/dist/src/runtime/devtools/config.js.map +1 -1
  165. package/dist/src/runtime/devtools/constants.d.ts.map +1 -1
  166. package/dist/src/runtime/devtools/constants.js +18 -10
  167. package/dist/src/runtime/devtools/constants.js.map +1 -1
  168. package/dist/src/runtime/devtools/diagnostics.d.ts +4 -4
  169. package/dist/src/runtime/devtools/diagnostics.d.ts.map +1 -1
  170. package/dist/src/runtime/devtools/diagnostics.js +40 -38
  171. package/dist/src/runtime/devtools/diagnostics.js.map +1 -1
  172. package/dist/src/runtime/devtools/oauth.d.ts +1 -1
  173. package/dist/src/runtime/devtools/oauth.d.ts.map +1 -1
  174. package/dist/src/runtime/devtools/oauth.js +35 -28
  175. package/dist/src/runtime/devtools/oauth.js.map +1 -1
  176. package/dist/src/runtime/devtools/types.d.ts +3 -3
  177. package/dist/src/runtime/devtools/types.d.ts.map +1 -1
  178. package/dist/src/runtime/devtools.d.ts +7 -7
  179. package/dist/src/runtime/devtools.js +7 -7
  180. package/dist/src/runtime/next-devtools.d.ts.map +1 -1
  181. package/dist/src/runtime/next-devtools.js +12 -12
  182. package/dist/src/runtime/next-devtools.js.map +1 -1
  183. package/dist/src/runtime/screenshot.d.ts +9 -9
  184. package/dist/src/runtime/screenshot.d.ts.map +1 -1
  185. package/dist/src/runtime/screenshot.js +65 -46
  186. package/dist/src/runtime/screenshot.js.map +1 -1
  187. package/dist/src/runtime/scripts.d.ts +1 -1
  188. package/dist/src/runtime/scripts.d.ts.map +1 -1
  189. package/dist/src/runtime/scripts.js +28 -28
  190. package/dist/src/runtime/scripts.js.map +1 -1
  191. package/dist/src/runtime/session.d.ts +6 -6
  192. package/dist/src/runtime/session.d.ts.map +1 -1
  193. package/dist/src/runtime/session.js +35 -35
  194. package/dist/src/runtime/session.js.map +1 -1
  195. package/dist/src/runtime/smoke.d.ts +3 -3
  196. package/dist/src/runtime/smoke.d.ts.map +1 -1
  197. package/dist/src/runtime/smoke.js +85 -79
  198. package/dist/src/runtime/smoke.js.map +1 -1
  199. package/dist/src/runtime/url.d.ts +2 -2
  200. package/dist/src/runtime/url.d.ts.map +1 -1
  201. package/dist/src/runtime/url.js +20 -20
  202. package/dist/src/runtime/url.js.map +1 -1
  203. package/dist/src/screenshot-hooks.d.ts +1 -1
  204. package/dist/src/screenshot-hooks.d.ts.map +1 -1
  205. package/dist/src/screenshot-hooks.js +12 -12
  206. package/dist/src/screenshot-hooks.js.map +1 -1
  207. package/dist/src/shared/env.d.ts.map +1 -1
  208. package/dist/src/shared/env.js +11 -11
  209. package/dist/src/shared/env.js.map +1 -1
  210. package/dist/src/shared/index.d.ts +30 -30
  211. package/dist/src/shared/index.d.ts.map +1 -1
  212. package/dist/src/shared/index.js +17 -17
  213. package/dist/src/shared/index.js.map +1 -1
  214. package/dist/src/shared/node.d.ts +1 -1
  215. package/dist/src/shared/node.d.ts.map +1 -1
  216. package/dist/src/shared/node.js +18 -18
  217. package/dist/src/shared/node.js.map +1 -1
  218. package/dist/src/token.d.ts +1 -1
  219. package/dist/src/token.d.ts.map +1 -1
  220. package/dist/src/token.js +16 -16
  221. package/dist/src/token.js.map +1 -1
  222. package/dist/src/types.d.ts +1 -1
  223. package/dist/src/util/app-label.js +4 -4
  224. package/dist/src/util/errors.d.ts.map +1 -1
  225. package/dist/src/util/errors.js +12 -10
  226. package/dist/src/util/errors.js.map +1 -1
  227. package/dist/src/util/path.js +2 -2
  228. package/dist/src/util/regex.js +4 -4
  229. package/package.json +45 -39
@@ -1,58 +1,74 @@
1
- import { regex } from 'arkregex';
2
- import { spawn } from 'node:child_process';
3
- import { loadSweetLinkFileConfig } from '../core/config-file.js';
4
- import { cloneProcessEnv } from '../core/env.js';
5
- import { cliEnv } from '../env.js';
6
- import { describeUnknown } from '../util/errors.js';
1
+ import { existsSync } from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { regex } from "arkregex";
5
+ import { getCookies } from "@steipete/sweet-cookie";
6
+ import { loadSweetLinkFileConfig } from "../core/config-file.js";
7
+ import { cliEnv } from "../env.js";
8
+ import { describeUnknown } from "../util/errors.js";
7
9
  let tldPatchedForLocalhost;
8
- let attemptedSqliteRebuild;
9
- const PROTOCOL_PREFIX_PATTERN = regex.as('^[a-z]+://', 'i');
10
+ const PROTOCOL_PREFIX_PATTERN = regex.as("^[a-z]+://", "i");
10
11
  const LEADING_DOT_PATTERN = regex.as(String.raw `^\.`);
11
- const SECURE_PREFIX_PATTERN = regex.as('^__Secure-');
12
- const HOST_PREFIX_PATTERN = regex.as('^__Host-');
13
- const SQLITE_NODE_PATTERN = regex.as(String.raw `node_sqlite3\.node`, 'i');
14
- const SQLITE_BINDINGS_PATTERN = regex.as('bindings file', 'i');
15
- const SQLITE_SELF_REGISTER_PATTERN = regex.as('Module did not self-register', 'i');
12
+ const SECURE_PREFIX_PATTERN = regex.as("^__Secure-");
13
+ const HOST_PREFIX_PATTERN = regex.as("^__Host-");
14
+ const ORACLE_COOKIE_FILES = ["cookies.base64", "cookies.json"];
15
+ const resolveOracleInlineCookiesFile = () => {
16
+ const oracleDir = path.join(os.homedir(), ".oracle");
17
+ for (const filename of ORACLE_COOKIE_FILES) {
18
+ const candidate = path.join(oracleDir, filename);
19
+ if (existsSync(candidate)) {
20
+ return candidate;
21
+ }
22
+ }
23
+ return;
24
+ };
16
25
  /** Collects cookies from the main Chrome profile matching the provided URL. */
17
26
  export async function collectChromeCookies(targetUrl) {
18
27
  await ensureTldPatchedForLocalhost();
19
- const secureModule = await loadChromeCookiesModule();
20
- if (!secureModule) {
21
- return [];
22
- }
23
- const profileOverride = cliEnv.chromeProfilePath ?? undefined;
24
- const origins = buildCookieOrigins(targetUrl);
25
- const collected = new Map();
26
28
  const debugCookies = cliEnv.cookieDebug;
27
- const targetBaseUrl = new URL(targetUrl);
28
29
  if (debugCookies) {
29
- console.log('Cookie sync debug enabled.');
30
+ console.log("Cookie sync debug enabled.");
30
31
  }
31
- // chrome-cookies-secure can miss results under parallel reads; keep this sequential.
32
- for (const origin of origins) {
33
- // biome-ignore lint/performance/noAwaitInLoops: cookie reads must stay sequential for reliability.
34
- await collectCookiesForOrigin({
35
- origin,
36
- secureModule,
37
- profileOverride,
38
- collected,
39
- debugCookies,
40
- targetBaseUrl,
41
- });
32
+ const origins = buildCookieOrigins(targetUrl);
33
+ const expandedOrigins = expandCookieOriginsForFallbacks(origins);
34
+ const targetBaseUrl = new URL(targetUrl);
35
+ const inlineCookiesFile = resolveOracleInlineCookiesFile();
36
+ if (debugCookies && inlineCookiesFile) {
37
+ console.log(`[SweetLink] Using inline cookie fallback from ${inlineCookiesFile}.`);
38
+ }
39
+ const { cookies, warnings } = await getCookies({
40
+ url: targetUrl,
41
+ origins: expandedOrigins,
42
+ browsers: ["chrome"],
43
+ chromeProfile: cliEnv.chromeProfilePath ?? undefined,
44
+ debug: debugCookies,
45
+ ...(inlineCookiesFile ? { inlineCookiesFile } : {}),
46
+ mode: "first",
47
+ });
48
+ if (debugCookies && warnings.length > 0) {
49
+ for (const warning of warnings) {
50
+ console.warn("[SweetLink] Cookie warning:", warning);
51
+ }
42
52
  }
53
+ const collected = new Map();
54
+ ingestSweetCookieCookies({
55
+ cookies,
56
+ targetBaseUrl,
57
+ collected,
58
+ debug: debugCookies,
59
+ });
43
60
  pruneIncompatibleCookies(targetBaseUrl, collected);
44
61
  return [...collected.values()];
45
62
  }
46
63
  /** Collects cookies for each domain and groups them by the originating host. */
47
64
  export async function collectChromeCookiesForDomains(domains) {
48
65
  await ensureTldPatchedForLocalhost();
49
- const secureModule = await loadChromeCookiesModule();
50
- if (!secureModule) {
51
- return {};
52
- }
53
- const profileOverride = cliEnv.chromeProfilePath ?? undefined;
54
66
  const debugCookies = cliEnv.cookieDebug;
55
67
  const results = {};
68
+ const inlineCookiesFile = resolveOracleInlineCookiesFile();
69
+ if (debugCookies && inlineCookiesFile) {
70
+ console.log(`[SweetLink] Using inline cookie fallback from ${inlineCookiesFile}.`);
71
+ }
56
72
  for (const domainCandidate of domains) {
57
73
  if (!domainCandidate) {
58
74
  continue;
@@ -65,22 +81,35 @@ export async function collectChromeCookiesForDomains(domains) {
65
81
  origins.add(extra);
66
82
  }
67
83
  }
84
+ const expandedOrigins = expandCookieOriginsForFallbacks([...origins.values()]);
68
85
  const collected = new Map();
69
- const originList = [...origins.values()].filter((origin) => Boolean(origin));
70
- for (const origin of originList) {
71
- // biome-ignore lint/performance/noAwaitInLoops: cookie reads must stay sequential for reliability.
72
- await collectCookiesForOrigin({
73
- origin,
74
- secureModule,
75
- profileOverride,
76
- collected,
77
- debugCookies,
78
- targetBaseUrl: new URL(origin),
79
- });
80
- }
81
86
  const targetIterator = origins.values().next();
82
87
  const targetCandidate = targetIterator.done ? domain : targetIterator.value;
83
88
  const targetBase = targetCandidate ? tryParseUrl(targetCandidate) : null;
89
+ const targetBaseUrl = targetBase ?? (targetCandidate ? tryParseUrl(`https://${targetCandidate}`) : null);
90
+ if (targetCandidate && targetBaseUrl) {
91
+ // biome-ignore lint/performance/noAwaitInLoops: cookie reads should stay sequential for predictable prompts/logging.
92
+ const { cookies, warnings } = await getCookies({
93
+ url: targetCandidate,
94
+ origins: expandedOrigins,
95
+ browsers: ["chrome"],
96
+ chromeProfile: cliEnv.chromeProfilePath ?? undefined,
97
+ debug: debugCookies,
98
+ ...(inlineCookiesFile ? { inlineCookiesFile } : {}),
99
+ mode: "first",
100
+ });
101
+ if (debugCookies && warnings.length > 0) {
102
+ for (const warning of warnings) {
103
+ console.warn("[SweetLink] Cookie warning:", warning);
104
+ }
105
+ }
106
+ ingestSweetCookieCookies({
107
+ cookies,
108
+ targetBaseUrl,
109
+ collected,
110
+ debug: debugCookies,
111
+ });
112
+ }
84
113
  if (targetBase) {
85
114
  pruneIncompatibleCookies(targetBase, collected);
86
115
  }
@@ -98,147 +127,68 @@ export function buildCookieOrigins(targetUrl) {
98
127
  }
99
128
  return [...origins];
100
129
  }
101
- async function collectCookiesForOrigin({ origin, secureModule, profileOverride, collected, debugCookies, targetBaseUrl, }) {
102
- const cookieOrigin = origin.endsWith('/') ? origin : `${origin}/`;
103
- let sourceBaseUrl = null;
104
- try {
105
- sourceBaseUrl = new URL(cookieOrigin);
106
- }
107
- catch {
108
- if (debugCookies) {
109
- console.log(`Skipping malformed cookie origin candidate ${cookieOrigin}`);
110
- }
111
- return;
112
- }
113
- const fallbackOrigins = deriveCookieOriginFallbacks(sourceBaseUrl);
114
- const attemptCookieRead = async (candidateOrigin, reason) => {
115
- if (debugCookies) {
116
- if (reason === 'primary') {
117
- console.log(`Reading Chrome cookies for ${candidateOrigin}`);
118
- }
119
- else {
120
- console.log(`Retrying cookie collection for ${cookieOrigin} using fallback ${candidateOrigin}`);
121
- }
122
- }
123
- try {
124
- let candidateBase = null;
125
- try {
126
- candidateBase = new URL(candidateOrigin);
127
- }
128
- catch {
129
- candidateBase = null;
130
- }
131
- if (!candidateBase) {
132
- return 'parse-error';
133
- }
134
- sourceBaseUrl = candidateBase;
135
- const beforeSize = collected.size;
136
- const raw = (await secureModule.getCookiesPromised(candidateOrigin, 'puppeteer', profileOverride));
137
- if (raw && raw.length > 0) {
138
- ingestChromeCookies({
139
- rawCookies: raw,
140
- sourceBaseUrl: candidateBase,
141
- targetBaseUrl,
142
- collected,
143
- sourceOrigin: candidateOrigin,
144
- debug: debugCookies,
145
- });
146
- return collected.size > beforeSize ? 'added' : 'empty';
147
- }
148
- return 'empty';
149
- }
150
- catch (error) {
151
- const message = error instanceof Error ? error.message : String(error);
152
- if (message.includes('Could not parse domain from URI')) {
153
- if (reason === 'fallback' && debugCookies) {
154
- console.log(`Fallback cookie origin ${candidateOrigin} also failed: ${message}`);
155
- }
156
- return 'parse-error';
157
- }
158
- if (reason === 'primary') {
159
- console.warn(`Failed to read cookies from Chrome for ${candidateOrigin}:`, message);
160
- console.warn('If this persists, ensure Chrome is running and you are logged in, then rerun the command.');
161
- }
162
- else if (debugCookies) {
163
- console.log(`Fallback cookie origin ${candidateOrigin} also failed: ${message}`);
164
- }
165
- return 'failed';
166
- }
167
- };
168
- const primaryResult = await attemptCookieRead(cookieOrigin, 'primary');
169
- const shouldAttemptFallback = fallbackOrigins.length > 0 && (primaryResult === 'empty' || primaryResult === 'parse-error');
170
- if (!shouldAttemptFallback) {
171
- if (primaryResult === 'parse-error' && debugCookies) {
172
- console.log(`Giving up on cookie sync for ${cookieOrigin}; chrome-cookies-secure cannot parse the host.`);
173
- }
174
- return;
175
- }
176
- let sawParseError = primaryResult === 'parse-error';
177
- for (const fallbackOrigin of fallbackOrigins) {
178
- // biome-ignore lint/performance/noAwaitInLoops: fallback attempts must run sequentially to stop on the first success.
179
- const result = await attemptCookieRead(fallbackOrigin, 'fallback');
180
- if (result === 'added') {
181
- return;
182
- }
183
- if (result === 'parse-error') {
184
- sawParseError = true;
185
- }
186
- }
187
- if (debugCookies) {
188
- const reasonMessage = sawParseError
189
- ? 'chrome-cookies-secure cannot parse the host.'
190
- : 'no cookies were found after fallback candidates.';
191
- console.log(`Giving up on cookie sync for ${cookieOrigin}; ${reasonMessage}`);
192
- }
193
- }
194
- function ingestChromeCookies({ rawCookies, sourceBaseUrl, targetBaseUrl, collected, sourceOrigin, debug, }) {
195
- if (!rawCookies?.length) {
130
+ function ingestSweetCookieCookies({ cookies, targetBaseUrl, collected, debug, }) {
131
+ if (!cookies?.length) {
196
132
  return;
197
133
  }
198
- for (const cookie of rawCookies) {
134
+ for (const cookie of cookies) {
199
135
  if (debug) {
200
- console.log(`Saw cookie ${describeUnknown(cookie.name, 'unknown')} from ${sourceOrigin}`);
136
+ console.log(`Saw cookie ${describeUnknown(cookie.name, "unknown")} from Sweet Cookie`);
201
137
  }
202
- const mapped = normalizePuppeteerCookie(cookie, {
203
- sourceBase: sourceBaseUrl,
138
+ const mapped = normalizePuppeteerCookie(sweetCookieToChromeCookieRecord(cookie), {
139
+ sourceBase: resolveCookieSourceBase(cookie, targetBaseUrl),
204
140
  targetBase: targetBaseUrl,
205
141
  });
206
142
  if (!mapped) {
207
143
  continue;
208
144
  }
209
- const key = `${mapped.domain ?? mapped.url ?? sourceBaseUrl.origin}|${mapped.path ?? '/'}|${mapped.name}`;
145
+ const key = `${mapped.domain ?? mapped.url ?? targetBaseUrl.origin}|${mapped.path ?? "/"}|${mapped.name}`;
210
146
  if (!collected.has(key)) {
211
147
  collected.set(key, mapped);
212
148
  }
213
149
  }
214
150
  if (debug) {
215
- console.log(`${collected.size} cookies captured so far after ${sourceOrigin}`);
151
+ console.log(`${collected.size} cookies captured so far`);
216
152
  }
217
153
  }
218
154
  function deriveCookieOriginFallbacks(baseUrl) {
219
155
  if (!baseUrl) {
220
156
  return [];
221
157
  }
222
- const protocol = baseUrl.protocol || 'https:';
158
+ const protocol = baseUrl.protocol || "https:";
223
159
  const host = baseUrl.hostname;
224
160
  if (!host) {
225
161
  return [];
226
162
  }
227
163
  const candidates = new Set();
228
164
  const originWithSlash = `${protocol}//${host}/`;
229
- if (host === 'localhost' || host === '127.0.0.1') {
165
+ if (host === "localhost" || host === "127.0.0.1") {
230
166
  candidates.add(originWithSlash);
231
- candidates.add('http://localhost/');
232
- candidates.add('http://127.0.0.1/');
233
- candidates.add('https://localhost/');
234
- candidates.add('https://127.0.0.1/');
235
- if (host === 'localhost') {
167
+ candidates.add("http://localhost/");
168
+ candidates.add("http://127.0.0.1/");
169
+ candidates.add("https://localhost/");
170
+ candidates.add("https://127.0.0.1/");
171
+ if (host === "localhost") {
236
172
  candidates.add(`${protocol}//${host}.localdomain/`);
237
173
  }
238
174
  }
239
175
  candidates.delete(`${baseUrl.origin}/`);
240
176
  return [...candidates];
241
177
  }
178
+ function expandCookieOriginsForFallbacks(origins) {
179
+ const expanded = new Set();
180
+ for (const origin of origins) {
181
+ expanded.add(origin);
182
+ const base = tryParseUrl(origin.endsWith("/") ? origin : `${origin}/`);
183
+ if (!base) {
184
+ continue;
185
+ }
186
+ for (const fallback of deriveCookieOriginFallbacks(base)) {
187
+ expanded.add(fallback);
188
+ }
189
+ }
190
+ return [...expanded.values()];
191
+ }
242
192
  function normalizeDomainToOrigins(domain) {
243
193
  const trimmed = domain.trim();
244
194
  if (!trimmed) {
@@ -269,7 +219,7 @@ function extractHostCandidate(domain) {
269
219
  return null;
270
220
  }
271
221
  try {
272
- const url = new URL(trimmed.includes('://') ? trimmed : `https://${trimmed}`);
222
+ const url = new URL(trimmed.includes("://") ? trimmed : `https://${trimmed}`);
273
223
  return url.hostname.toLowerCase();
274
224
  }
275
225
  catch {
@@ -318,7 +268,7 @@ function isConfiguredCookieDomain(host) {
318
268
  function isHostMatch(host, pattern) {
319
269
  const normalizedHost = host.toLowerCase();
320
270
  let normalizedPattern = pattern.toLowerCase();
321
- if (normalizedPattern.startsWith('*.')) {
271
+ if (normalizedPattern.startsWith("*.")) {
322
272
  normalizedPattern = normalizedPattern.slice(2);
323
273
  }
324
274
  if (!normalizedPattern) {
@@ -348,8 +298,8 @@ function normalizeOrigin(value) {
348
298
  }
349
299
  }
350
300
  export function normalizePuppeteerCookie(cookie, bases) {
351
- const originalName = typeof cookie.name === 'string' ? cookie.name : null;
352
- const value = typeof cookie.value === 'string' ? cookie.value : null;
301
+ const originalName = typeof cookie.name === "string" ? cookie.name : null;
302
+ const value = typeof cookie.value === "string" ? cookie.value : null;
353
303
  if (!originalName || value === null) {
354
304
  return null;
355
305
  }
@@ -357,17 +307,19 @@ export function normalizePuppeteerCookie(cookie, bases) {
357
307
  name: originalName,
358
308
  value,
359
309
  };
360
- const domain = typeof cookie.domain === 'string' && cookie.domain.length > 0 ? cookie.domain : null;
361
- const path = typeof cookie.path === 'string' && cookie.path.length > 0 ? cookie.path : '/';
310
+ const domain = typeof cookie.domain === "string" && cookie.domain.length > 0 ? cookie.domain : null;
311
+ const path = typeof cookie.path === "string" && cookie.path.length > 0 ? cookie.path : "/";
362
312
  const targetHost = bases.targetBase.hostname;
363
- const isLocalTarget = targetHost === 'localhost' || targetHost === '127.0.0.1';
364
- const normalizedDomain = domain?.replace(LEADING_DOT_PATTERN, '') ?? null;
313
+ const isLocalTarget = targetHost === "localhost" || targetHost === "127.0.0.1";
314
+ const normalizedDomain = domain?.replace(LEADING_DOT_PATTERN, "") ?? null;
365
315
  const isConfiguredDomain = normalizedDomain ? isConfiguredCookieDomain(normalizedDomain) : false;
366
- const isLocalDomain = normalizedDomain === 'localhost' || normalizedDomain === '127.0.0.1' || normalizedDomain === '::1';
367
- if (isLocalTarget && result.name.startsWith('__Secure-better-auth.')) {
368
- result.name = result.name.replace(SECURE_PREFIX_PATTERN, '');
316
+ const isLocalDomain = normalizedDomain === "localhost" ||
317
+ normalizedDomain === "127.0.0.1" ||
318
+ normalizedDomain === "::1";
319
+ if (isLocalTarget && result.name.startsWith("__Secure-better-auth.")) {
320
+ result.name = result.name.replace(SECURE_PREFIX_PATTERN, "");
369
321
  }
370
- if (domain && domain !== 'localhost') {
322
+ if (domain && domain !== "localhost") {
371
323
  if (isLocalTarget && (isConfiguredDomain || isLocalDomain)) {
372
324
  result.url = bases.targetBase.origin;
373
325
  }
@@ -387,60 +339,62 @@ export function normalizePuppeteerCookie(cookie, bases) {
387
339
  if (cookie.HttpOnly === true || cookie.httpOnly === true) {
388
340
  result.httpOnly = true;
389
341
  }
390
- const sameSiteSource = typeof cookie.sameSite === 'string' ? cookie.sameSite : undefined;
342
+ const sameSiteSource = typeof cookie.sameSite === "string" ? cookie.sameSite : undefined;
391
343
  const sameSite = normalizeSameSite(sameSiteSource);
392
344
  if (sameSite) {
393
345
  result.sameSite = sameSite;
394
346
  }
395
- if (sameSite === 'None' && !result.secure) {
347
+ if (sameSite === "None" && !result.secure) {
396
348
  result.secure = true;
397
349
  }
398
- if (typeof cookie.expires === 'number' && Number.isFinite(cookie.expires) && cookie.expires > 0) {
350
+ if (typeof cookie.expires === "number" && Number.isFinite(cookie.expires) && cookie.expires > 0) {
399
351
  result.expires = Math.round(cookie.expires);
400
352
  }
401
- const rehomedToTarget = typeof result.url === 'string' && result.url.length > 0 && result.url === bases.targetBase.origin;
402
- if (rehomedToTarget && bases.targetBase.protocol === 'http:') {
353
+ const rehomedToTarget = typeof result.url === "string" &&
354
+ result.url.length > 0 &&
355
+ result.url === bases.targetBase.origin;
356
+ if (rehomedToTarget && bases.targetBase.protocol === "http:") {
403
357
  if (result.secure) {
404
358
  result.secure = false;
405
359
  }
406
- if (result.sameSite === 'None') {
407
- result.sameSite = 'Lax';
360
+ if (result.sameSite === "None") {
361
+ result.sameSite = "Lax";
408
362
  }
409
- if (result.name.startsWith('__Secure-')) {
410
- result.name = result.name.replace(SECURE_PREFIX_PATTERN, '');
363
+ if (result.name.startsWith("__Secure-")) {
364
+ result.name = result.name.replace(SECURE_PREFIX_PATTERN, "");
411
365
  }
412
- if (result.name.startsWith('__Host-')) {
413
- result.name = result.name.replace(HOST_PREFIX_PATTERN, '');
414
- result.path = '/';
366
+ if (result.name.startsWith("__Host-")) {
367
+ result.name = result.name.replace(HOST_PREFIX_PATTERN, "");
368
+ result.path = "/";
415
369
  }
416
370
  }
417
371
  return result;
418
372
  }
419
373
  function normalizeSameSite(value) {
420
374
  if (!value) {
421
- return;
375
+ return undefined;
422
376
  }
423
377
  const normalized = value.toLowerCase();
424
- if (normalized === 'strict') {
425
- return 'Strict';
378
+ if (normalized === "strict") {
379
+ return "Strict";
426
380
  }
427
- if (normalized === 'lax') {
428
- return 'Lax';
381
+ if (normalized === "lax") {
382
+ return "Lax";
429
383
  }
430
- if (normalized === 'no_restriction' || normalized === 'none') {
431
- return 'None';
384
+ if (normalized === "no_restriction" || normalized === "none") {
385
+ return "None";
432
386
  }
433
- return;
387
+ return undefined;
434
388
  }
435
389
  function pruneIncompatibleCookies(targetBaseUrl, collected) {
436
390
  if (collected.size === 0) {
437
391
  return;
438
392
  }
439
- const localHosts = new Set(['localhost', '127.0.0.1', '::1']);
393
+ const localHosts = new Set(["localhost", "127.0.0.1", "::1"]);
440
394
  if (!localHosts.has(targetBaseUrl.hostname)) {
441
395
  return;
442
396
  }
443
- const disallowedNames = new Set(['_vercel_session', '_vercel_jwt']);
397
+ const disallowedNames = new Set(["_vercel_session", "_vercel_jwt"]);
444
398
  for (const [key, cookie] of collected.entries()) {
445
399
  const name = cookie.name?.toLowerCase();
446
400
  if (name && disallowedNames.has(name)) {
@@ -454,7 +408,7 @@ function tryParseUrl(candidate) {
454
408
  }
455
409
  catch {
456
410
  try {
457
- return new URL(candidate.includes('://') ? candidate : `http://${candidate}`);
411
+ return new URL(candidate.includes("://") ? candidate : `http://${candidate}`);
458
412
  }
459
413
  catch {
460
414
  return null;
@@ -466,9 +420,9 @@ async function ensureTldPatchedForLocalhost() {
466
420
  return;
467
421
  }
468
422
  try {
469
- const importedModule = await import('tldjs');
423
+ const importedModule = await import("tldjs");
470
424
  const tld = resolveTldModule(importedModule);
471
- if (tld && typeof tld.getDomain === 'function') {
425
+ if (tld && typeof tld.getDomain === "function") {
472
426
  const originalGetDomain = tld.getDomain.bind(tld);
473
427
  tld.getDomain = (uri) => {
474
428
  const domain = originalGetDomain(uri);
@@ -486,114 +440,49 @@ async function ensureTldPatchedForLocalhost() {
486
440
  }
487
441
  }
488
442
  catch (error) {
489
- console.warn('Failed to patch tldjs for localhost support:', error);
443
+ console.warn("Failed to patch tldjs for localhost support:", error);
490
444
  }
491
445
  }
492
- const SQLITE_BINDING_HINT = [
493
- 'SweetLink needs chrome-cookies-secure to read your Chrome cookie DB.',
494
- 'Rebuild the native modules once per workspace so Node 25 picks up the sqlite3 binding:',
495
- ' PYTHON=/usr/bin/python3 npm_config_build_from_source=1 pnpm rebuild chrome-cookies-secure sqlite3 keytar --workspace-root',
496
- ].join('\n');
497
- const isSqliteBindingError = (error) => {
498
- if (!(error instanceof Error)) {
499
- return false;
500
- }
501
- const message = error.message ?? '';
502
- return (SQLITE_NODE_PATTERN.test(message) ||
503
- SQLITE_BINDINGS_PATTERN.test(message) ||
504
- SQLITE_SELF_REGISTER_PATTERN.test(message));
505
- };
506
- async function loadChromeCookiesModule() {
507
- let imported;
508
- try {
509
- imported = await import('chrome-cookies-secure');
510
- }
511
- catch (error) {
512
- console.warn('Failed to load chrome-cookies-secure to copy cookies:', error);
513
- if (isSqliteBindingError(error)) {
514
- const rebuilt = await attemptSqliteRebuild();
515
- if (rebuilt) {
516
- return loadChromeCookiesModule();
517
- }
518
- console.warn(SQLITE_BINDING_HINT);
519
- }
520
- else {
521
- console.warn('If this persists, run `pnpm rebuild chrome-cookies-secure sqlite3 keytar --workspace-root`.');
522
- }
523
- return null;
524
- }
525
- const secureModule = resolveChromeCookieModule(imported);
526
- if (!secureModule) {
527
- console.warn('chrome-cookies-secure does not expose getCookiesPromised(); skipping cookie copy.');
528
- return null;
529
- }
530
- return secureModule;
446
+ function sweetCookieToChromeCookieRecord(cookie) {
447
+ const record = {
448
+ name: cookie.name,
449
+ value: cookie.value,
450
+ domain: cookie.domain,
451
+ path: cookie.path,
452
+ secure: cookie.secure,
453
+ httpOnly: cookie.httpOnly,
454
+ sameSite: cookie.sameSite,
455
+ expires: cookie.expires,
456
+ };
457
+ return record;
531
458
  }
532
- function resolveChromeCookieModule(candidate) {
533
- if (hasGetCookiesPromised(candidate)) {
534
- return candidate;
535
- }
536
- if (typeof candidate === 'object' && candidate !== null) {
537
- const defaultExport = Reflect.get(candidate, 'default');
538
- if (hasGetCookiesPromised(defaultExport)) {
539
- return defaultExport;
459
+ function resolveCookieSourceBase(cookie, fallback) {
460
+ const origin = cookie.source?.origin;
461
+ if (typeof origin === "string" && origin.length > 0) {
462
+ try {
463
+ return new URL(origin);
464
+ }
465
+ catch {
466
+ return fallback;
540
467
  }
541
468
  }
542
- return null;
543
- }
544
- function hasGetCookiesPromised(value) {
545
- return Boolean(value && typeof value.getCookiesPromised === 'function');
469
+ return fallback;
546
470
  }
547
471
  function resolveTldModule(value) {
548
- if (typeof value !== 'object' || value === null) {
472
+ if (typeof value !== "object" || value === null) {
549
473
  return null;
550
474
  }
551
475
  const record = value;
552
- if (typeof record.getDomain === 'function') {
476
+ if (typeof record.getDomain === "function") {
553
477
  return record;
554
478
  }
555
479
  const defaultExport = record.default;
556
- if (typeof defaultExport === 'object' && defaultExport !== null) {
480
+ if (typeof defaultExport === "object" && defaultExport !== null) {
557
481
  const defaultRecord = defaultExport;
558
- if (typeof defaultRecord.getDomain === 'function') {
482
+ if (typeof defaultRecord.getDomain === "function") {
559
483
  return defaultRecord;
560
484
  }
561
485
  }
562
486
  return null;
563
487
  }
564
- function attemptSqliteRebuild() {
565
- if (attemptedSqliteRebuild) {
566
- return Promise.resolve(false);
567
- }
568
- attemptedSqliteRebuild = true;
569
- const pnpmCommand = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm';
570
- const args = ['rebuild', 'chrome-cookies-secure', 'sqlite3', 'keytar', '--workspace-root'];
571
- const childEnv = cloneProcessEnv();
572
- const pythonBinary = childEnv.PYTHON ?? '/usr/bin/python3';
573
- const rebuildCommand = `${pnpmCommand} ${args.join(' ')}`;
574
- console.warn('[SweetLink] Attempting to rebuild sqlite3 bindings automatically…');
575
- console.warn(`[SweetLink] Running: npm_config_build_from_source=1 PYTHON=${pythonBinary} ${rebuildCommand}`);
576
- return new Promise((resolve) => {
577
- childEnv.npm_config_build_from_source = '1';
578
- childEnv.PYTHON = childEnv.PYTHON ?? '/usr/bin/python3';
579
- const child = spawn(pnpmCommand, args, {
580
- stdio: 'inherit',
581
- env: childEnv,
582
- });
583
- child.on('exit', (code) => {
584
- if (code === 0) {
585
- console.warn('[SweetLink] sqlite3 rebuild completed successfully.');
586
- resolve(true);
587
- }
588
- else {
589
- console.warn('[SweetLink] sqlite3 rebuild failed with exit code', code ?? 0);
590
- resolve(false);
591
- }
592
- });
593
- child.on('error', (spawnError) => {
594
- console.warn('[SweetLink] Unable to spawn pnpm to rebuild sqlite3:', spawnError);
595
- resolve(false);
596
- });
597
- });
598
- }
599
488
  //# sourceMappingURL=cookies.js.map