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