@hammadj/better-auth-core 1.5.0-beta.9

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 (353) hide show
  1. package/.turbo/turbo-build.log +266 -0
  2. package/.turbo/turbo-test.log +2 -0
  3. package/LICENSE.md +20 -0
  4. package/dist/api/index.d.mts +181 -0
  5. package/dist/api/index.mjs +34 -0
  6. package/dist/api/index.mjs.map +1 -0
  7. package/dist/async_hooks/index.d.mts +7 -0
  8. package/dist/async_hooks/index.mjs +22 -0
  9. package/dist/async_hooks/index.mjs.map +1 -0
  10. package/dist/async_hooks/pure.index.d.mts +7 -0
  11. package/dist/async_hooks/pure.index.mjs +35 -0
  12. package/dist/async_hooks/pure.index.mjs.map +1 -0
  13. package/dist/context/endpoint-context.d.mts +19 -0
  14. package/dist/context/endpoint-context.mjs +32 -0
  15. package/dist/context/endpoint-context.mjs.map +1 -0
  16. package/dist/context/global.d.mts +7 -0
  17. package/dist/context/global.mjs +38 -0
  18. package/dist/context/global.mjs.map +1 -0
  19. package/dist/context/index.d.mts +5 -0
  20. package/dist/context/index.mjs +6 -0
  21. package/dist/context/request-state.d.mts +26 -0
  22. package/dist/context/request-state.mjs +50 -0
  23. package/dist/context/request-state.mjs.map +1 -0
  24. package/dist/context/transaction.d.mts +25 -0
  25. package/dist/context/transaction.mjs +96 -0
  26. package/dist/context/transaction.mjs.map +1 -0
  27. package/dist/db/adapter/factory.d.mts +28 -0
  28. package/dist/db/adapter/factory.mjs +716 -0
  29. package/dist/db/adapter/factory.mjs.map +1 -0
  30. package/dist/db/adapter/get-default-field-name.d.mts +19 -0
  31. package/dist/db/adapter/get-default-field-name.mjs +39 -0
  32. package/dist/db/adapter/get-default-field-name.mjs.map +1 -0
  33. package/dist/db/adapter/get-default-model-name.d.mts +13 -0
  34. package/dist/db/adapter/get-default-model-name.mjs +33 -0
  35. package/dist/db/adapter/get-default-model-name.mjs.map +1 -0
  36. package/dist/db/adapter/get-field-attributes.d.mts +30 -0
  37. package/dist/db/adapter/get-field-attributes.mjs +40 -0
  38. package/dist/db/adapter/get-field-attributes.mjs.map +1 -0
  39. package/dist/db/adapter/get-field-name.d.mts +19 -0
  40. package/dist/db/adapter/get-field-name.mjs +34 -0
  41. package/dist/db/adapter/get-field-name.mjs.map +1 -0
  42. package/dist/db/adapter/get-id-field.d.mts +40 -0
  43. package/dist/db/adapter/get-id-field.mjs +68 -0
  44. package/dist/db/adapter/get-id-field.mjs.map +1 -0
  45. package/dist/db/adapter/get-model-name.d.mts +13 -0
  46. package/dist/db/adapter/get-model-name.mjs +24 -0
  47. package/dist/db/adapter/get-model-name.mjs.map +1 -0
  48. package/dist/db/adapter/index.d.mts +515 -0
  49. package/dist/db/adapter/index.mjs +10 -0
  50. package/dist/db/adapter/types.d.mts +140 -0
  51. package/dist/db/adapter/utils.d.mts +8 -0
  52. package/dist/db/adapter/utils.mjs +39 -0
  53. package/dist/db/adapter/utils.mjs.map +1 -0
  54. package/dist/db/get-tables.d.mts +9 -0
  55. package/dist/db/get-tables.mjs +267 -0
  56. package/dist/db/get-tables.mjs.map +1 -0
  57. package/dist/db/index.d.mts +10 -0
  58. package/dist/db/index.mjs +9 -0
  59. package/dist/db/plugin.d.mts +13 -0
  60. package/dist/db/schema/account.d.mts +27 -0
  61. package/dist/db/schema/account.mjs +20 -0
  62. package/dist/db/schema/account.mjs.map +1 -0
  63. package/dist/db/schema/rate-limit.d.mts +15 -0
  64. package/dist/db/schema/rate-limit.mjs +12 -0
  65. package/dist/db/schema/rate-limit.mjs.map +1 -0
  66. package/dist/db/schema/session.d.mts +22 -0
  67. package/dist/db/schema/session.mjs +15 -0
  68. package/dist/db/schema/session.mjs.map +1 -0
  69. package/dist/db/schema/shared.d.mts +11 -0
  70. package/dist/db/schema/shared.mjs +12 -0
  71. package/dist/db/schema/shared.mjs.map +1 -0
  72. package/dist/db/schema/user.d.mts +21 -0
  73. package/dist/db/schema/user.mjs +14 -0
  74. package/dist/db/schema/user.mjs.map +1 -0
  75. package/dist/db/schema/verification.d.mts +20 -0
  76. package/dist/db/schema/verification.mjs +13 -0
  77. package/dist/db/schema/verification.mjs.map +1 -0
  78. package/dist/db/type.d.mts +147 -0
  79. package/dist/env/color-depth.d.mts +5 -0
  80. package/dist/env/color-depth.mjs +89 -0
  81. package/dist/env/color-depth.mjs.map +1 -0
  82. package/dist/env/env-impl.d.mts +33 -0
  83. package/dist/env/env-impl.mjs +83 -0
  84. package/dist/env/env-impl.mjs.map +1 -0
  85. package/dist/env/index.d.mts +4 -0
  86. package/dist/env/index.mjs +5 -0
  87. package/dist/env/logger.d.mts +49 -0
  88. package/dist/env/logger.mjs +82 -0
  89. package/dist/env/logger.mjs.map +1 -0
  90. package/dist/error/codes.d.mts +199 -0
  91. package/dist/error/codes.mjs +57 -0
  92. package/dist/error/codes.mjs.map +1 -0
  93. package/dist/error/index.d.mts +20 -0
  94. package/dist/error/index.mjs +30 -0
  95. package/dist/error/index.mjs.map +1 -0
  96. package/dist/index.d.mts +8 -0
  97. package/dist/index.mjs +1 -0
  98. package/dist/oauth2/client-credentials-token.d.mts +37 -0
  99. package/dist/oauth2/client-credentials-token.mjs +55 -0
  100. package/dist/oauth2/client-credentials-token.mjs.map +1 -0
  101. package/dist/oauth2/create-authorization-url.d.mts +46 -0
  102. package/dist/oauth2/create-authorization-url.mjs +43 -0
  103. package/dist/oauth2/create-authorization-url.mjs.map +1 -0
  104. package/dist/oauth2/index.d.mts +8 -0
  105. package/dist/oauth2/index.mjs +8 -0
  106. package/dist/oauth2/oauth-provider.d.mts +195 -0
  107. package/dist/oauth2/refresh-access-token.d.mts +36 -0
  108. package/dist/oauth2/refresh-access-token.mjs +59 -0
  109. package/dist/oauth2/refresh-access-token.mjs.map +1 -0
  110. package/dist/oauth2/utils.d.mts +8 -0
  111. package/dist/oauth2/utils.mjs +28 -0
  112. package/dist/oauth2/utils.mjs.map +1 -0
  113. package/dist/oauth2/validate-authorization-code.d.mts +56 -0
  114. package/dist/oauth2/validate-authorization-code.mjs +72 -0
  115. package/dist/oauth2/validate-authorization-code.mjs.map +1 -0
  116. package/dist/oauth2/verify.d.mts +43 -0
  117. package/dist/oauth2/verify.mjs +96 -0
  118. package/dist/oauth2/verify.mjs.map +1 -0
  119. package/dist/social-providers/apple.d.mts +120 -0
  120. package/dist/social-providers/apple.mjs +105 -0
  121. package/dist/social-providers/apple.mjs.map +1 -0
  122. package/dist/social-providers/atlassian.d.mts +73 -0
  123. package/dist/social-providers/atlassian.mjs +84 -0
  124. package/dist/social-providers/atlassian.mjs.map +1 -0
  125. package/dist/social-providers/cognito.d.mts +88 -0
  126. package/dist/social-providers/cognito.mjs +166 -0
  127. package/dist/social-providers/cognito.mjs.map +1 -0
  128. package/dist/social-providers/discord.d.mts +127 -0
  129. package/dist/social-providers/discord.mjs +65 -0
  130. package/dist/social-providers/discord.mjs.map +1 -0
  131. package/dist/social-providers/dropbox.d.mts +72 -0
  132. package/dist/social-providers/dropbox.mjs +76 -0
  133. package/dist/social-providers/dropbox.mjs.map +1 -0
  134. package/dist/social-providers/facebook.d.mts +82 -0
  135. package/dist/social-providers/facebook.mjs +121 -0
  136. package/dist/social-providers/facebook.mjs.map +1 -0
  137. package/dist/social-providers/figma.d.mts +64 -0
  138. package/dist/social-providers/figma.mjs +87 -0
  139. package/dist/social-providers/figma.mjs.map +1 -0
  140. package/dist/social-providers/github.d.mts +105 -0
  141. package/dist/social-providers/github.mjs +97 -0
  142. package/dist/social-providers/github.mjs.map +1 -0
  143. package/dist/social-providers/gitlab.d.mts +126 -0
  144. package/dist/social-providers/gitlab.mjs +83 -0
  145. package/dist/social-providers/gitlab.mjs.map +1 -0
  146. package/dist/social-providers/google.d.mts +100 -0
  147. package/dist/social-providers/google.mjs +109 -0
  148. package/dist/social-providers/google.mjs.map +1 -0
  149. package/dist/social-providers/huggingface.d.mts +86 -0
  150. package/dist/social-providers/huggingface.mjs +76 -0
  151. package/dist/social-providers/huggingface.mjs.map +1 -0
  152. package/dist/social-providers/index.d.mts +1725 -0
  153. package/dist/social-providers/index.mjs +77 -0
  154. package/dist/social-providers/index.mjs.map +1 -0
  155. package/dist/social-providers/kakao.d.mts +164 -0
  156. package/dist/social-providers/kakao.mjs +73 -0
  157. package/dist/social-providers/kakao.mjs.map +1 -0
  158. package/dist/social-providers/kick.d.mts +76 -0
  159. package/dist/social-providers/kick.mjs +72 -0
  160. package/dist/social-providers/kick.mjs.map +1 -0
  161. package/dist/social-providers/line.d.mts +108 -0
  162. package/dist/social-providers/line.mjs +114 -0
  163. package/dist/social-providers/line.mjs.map +1 -0
  164. package/dist/social-providers/linear.d.mts +71 -0
  165. package/dist/social-providers/linear.mjs +89 -0
  166. package/dist/social-providers/linear.mjs.map +1 -0
  167. package/dist/social-providers/linkedin.d.mts +70 -0
  168. package/dist/social-providers/linkedin.mjs +77 -0
  169. package/dist/social-providers/linkedin.mjs.map +1 -0
  170. package/dist/social-providers/microsoft-entra-id.d.mts +175 -0
  171. package/dist/social-providers/microsoft-entra-id.mjs +107 -0
  172. package/dist/social-providers/microsoft-entra-id.mjs.map +1 -0
  173. package/dist/social-providers/naver.d.mts +95 -0
  174. package/dist/social-providers/naver.mjs +68 -0
  175. package/dist/social-providers/naver.mjs.map +1 -0
  176. package/dist/social-providers/notion.d.mts +67 -0
  177. package/dist/social-providers/notion.mjs +76 -0
  178. package/dist/social-providers/notion.mjs.map +1 -0
  179. package/dist/social-providers/paybin.d.mts +74 -0
  180. package/dist/social-providers/paybin.mjs +86 -0
  181. package/dist/social-providers/paybin.mjs.map +1 -0
  182. package/dist/social-providers/paypal.d.mts +132 -0
  183. package/dist/social-providers/paypal.mjs +145 -0
  184. package/dist/social-providers/paypal.mjs.map +1 -0
  185. package/dist/social-providers/polar.d.mts +77 -0
  186. package/dist/social-providers/polar.mjs +74 -0
  187. package/dist/social-providers/polar.mjs.map +1 -0
  188. package/dist/social-providers/reddit.d.mts +65 -0
  189. package/dist/social-providers/reddit.mjs +84 -0
  190. package/dist/social-providers/reddit.mjs.map +1 -0
  191. package/dist/social-providers/roblox.d.mts +73 -0
  192. package/dist/social-providers/roblox.mjs +60 -0
  193. package/dist/social-providers/roblox.mjs.map +1 -0
  194. package/dist/social-providers/salesforce.d.mts +82 -0
  195. package/dist/social-providers/salesforce.mjs +92 -0
  196. package/dist/social-providers/salesforce.mjs.map +1 -0
  197. package/dist/social-providers/slack.d.mts +86 -0
  198. package/dist/social-providers/slack.mjs +69 -0
  199. package/dist/social-providers/slack.mjs.map +1 -0
  200. package/dist/social-providers/spotify.d.mts +66 -0
  201. package/dist/social-providers/spotify.mjs +72 -0
  202. package/dist/social-providers/spotify.mjs.map +1 -0
  203. package/dist/social-providers/tiktok.d.mts +171 -0
  204. package/dist/social-providers/tiktok.mjs +63 -0
  205. package/dist/social-providers/tiktok.mjs.map +1 -0
  206. package/dist/social-providers/twitch.d.mts +82 -0
  207. package/dist/social-providers/twitch.mjs +79 -0
  208. package/dist/social-providers/twitch.mjs.map +1 -0
  209. package/dist/social-providers/twitter.d.mts +129 -0
  210. package/dist/social-providers/twitter.mjs +88 -0
  211. package/dist/social-providers/twitter.mjs.map +1 -0
  212. package/dist/social-providers/vercel.d.mts +65 -0
  213. package/dist/social-providers/vercel.mjs +62 -0
  214. package/dist/social-providers/vercel.mjs.map +1 -0
  215. package/dist/social-providers/vk.d.mts +73 -0
  216. package/dist/social-providers/vk.mjs +84 -0
  217. package/dist/social-providers/vk.mjs.map +1 -0
  218. package/dist/social-providers/zoom.d.mts +173 -0
  219. package/dist/social-providers/zoom.mjs +73 -0
  220. package/dist/social-providers/zoom.mjs.map +1 -0
  221. package/dist/types/context.d.mts +267 -0
  222. package/dist/types/cookie.d.mts +16 -0
  223. package/dist/types/helper.d.mts +10 -0
  224. package/dist/types/index.d.mts +8 -0
  225. package/dist/types/init-options.d.mts +1314 -0
  226. package/dist/types/plugin-client.d.mts +112 -0
  227. package/dist/types/plugin.d.mts +125 -0
  228. package/dist/utils/db.d.mts +12 -0
  229. package/dist/utils/db.mjs +17 -0
  230. package/dist/utils/db.mjs.map +1 -0
  231. package/dist/utils/deprecate.d.mts +10 -0
  232. package/dist/utils/deprecate.mjs +18 -0
  233. package/dist/utils/deprecate.mjs.map +1 -0
  234. package/dist/utils/error-codes.d.mts +13 -0
  235. package/dist/utils/error-codes.mjs +12 -0
  236. package/dist/utils/error-codes.mjs.map +1 -0
  237. package/dist/utils/id.d.mts +5 -0
  238. package/dist/utils/id.mjs +10 -0
  239. package/dist/utils/id.mjs.map +1 -0
  240. package/dist/utils/ip.d.mts +55 -0
  241. package/dist/utils/ip.mjs +119 -0
  242. package/dist/utils/ip.mjs.map +1 -0
  243. package/dist/utils/json.d.mts +5 -0
  244. package/dist/utils/json.mjs +26 -0
  245. package/dist/utils/json.mjs.map +1 -0
  246. package/dist/utils/string.d.mts +5 -0
  247. package/dist/utils/string.mjs +8 -0
  248. package/dist/utils/string.mjs.map +1 -0
  249. package/dist/utils/url.d.mts +21 -0
  250. package/dist/utils/url.mjs +33 -0
  251. package/dist/utils/url.mjs.map +1 -0
  252. package/package.json +147 -0
  253. package/src/api/index.ts +106 -0
  254. package/src/async_hooks/index.ts +40 -0
  255. package/src/async_hooks/pure.index.ts +46 -0
  256. package/src/context/endpoint-context.ts +50 -0
  257. package/src/context/global.ts +57 -0
  258. package/src/context/index.ts +23 -0
  259. package/src/context/request-state.test.ts +94 -0
  260. package/src/context/request-state.ts +91 -0
  261. package/src/context/transaction.ts +136 -0
  262. package/src/db/adapter/factory.ts +1362 -0
  263. package/src/db/adapter/get-default-field-name.ts +59 -0
  264. package/src/db/adapter/get-default-model-name.ts +51 -0
  265. package/src/db/adapter/get-field-attributes.ts +62 -0
  266. package/src/db/adapter/get-field-name.ts +43 -0
  267. package/src/db/adapter/get-id-field.ts +141 -0
  268. package/src/db/adapter/get-model-name.ts +36 -0
  269. package/src/db/adapter/index.ts +554 -0
  270. package/src/db/adapter/types.ts +171 -0
  271. package/src/db/adapter/utils.ts +61 -0
  272. package/src/db/get-tables.ts +296 -0
  273. package/src/db/index.ts +18 -0
  274. package/src/db/plugin.ts +11 -0
  275. package/src/db/schema/account.ts +34 -0
  276. package/src/db/schema/rate-limit.ts +21 -0
  277. package/src/db/schema/session.ts +17 -0
  278. package/src/db/schema/shared.ts +7 -0
  279. package/src/db/schema/user.ts +16 -0
  280. package/src/db/schema/verification.ts +15 -0
  281. package/src/db/test/get-tables.test.ts +116 -0
  282. package/src/db/type.ts +180 -0
  283. package/src/env/color-depth.ts +172 -0
  284. package/src/env/env-impl.ts +124 -0
  285. package/src/env/index.ts +23 -0
  286. package/src/env/logger.test.ts +34 -0
  287. package/src/env/logger.ts +145 -0
  288. package/src/error/codes.ts +58 -0
  289. package/src/error/index.ts +35 -0
  290. package/src/index.ts +1 -0
  291. package/src/oauth2/client-credentials-token.ts +102 -0
  292. package/src/oauth2/create-authorization-url.ts +87 -0
  293. package/src/oauth2/index.ts +26 -0
  294. package/src/oauth2/oauth-provider.ts +222 -0
  295. package/src/oauth2/refresh-access-token.ts +124 -0
  296. package/src/oauth2/utils.ts +38 -0
  297. package/src/oauth2/validate-authorization-code.ts +149 -0
  298. package/src/oauth2/validate-token.test.ts +174 -0
  299. package/src/oauth2/verify.ts +221 -0
  300. package/src/social-providers/apple.ts +223 -0
  301. package/src/social-providers/atlassian.ts +132 -0
  302. package/src/social-providers/cognito.ts +279 -0
  303. package/src/social-providers/discord.ts +169 -0
  304. package/src/social-providers/dropbox.ts +112 -0
  305. package/src/social-providers/facebook.ts +206 -0
  306. package/src/social-providers/figma.ts +117 -0
  307. package/src/social-providers/github.ts +184 -0
  308. package/src/social-providers/gitlab.ts +155 -0
  309. package/src/social-providers/google.ts +199 -0
  310. package/src/social-providers/huggingface.ts +118 -0
  311. package/src/social-providers/index.ts +127 -0
  312. package/src/social-providers/kakao.ts +178 -0
  313. package/src/social-providers/kick.ts +109 -0
  314. package/src/social-providers/line.ts +169 -0
  315. package/src/social-providers/linear.ts +121 -0
  316. package/src/social-providers/linkedin.ts +110 -0
  317. package/src/social-providers/microsoft-entra-id.ts +259 -0
  318. package/src/social-providers/naver.ts +112 -0
  319. package/src/social-providers/notion.ts +108 -0
  320. package/src/social-providers/paybin.ts +122 -0
  321. package/src/social-providers/paypal.ts +263 -0
  322. package/src/social-providers/polar.ts +110 -0
  323. package/src/social-providers/reddit.ts +122 -0
  324. package/src/social-providers/roblox.ts +111 -0
  325. package/src/social-providers/salesforce.ts +159 -0
  326. package/src/social-providers/slack.ts +111 -0
  327. package/src/social-providers/spotify.ts +93 -0
  328. package/src/social-providers/tiktok.ts +209 -0
  329. package/src/social-providers/twitch.ts +111 -0
  330. package/src/social-providers/twitter.ts +198 -0
  331. package/src/social-providers/vercel.ts +87 -0
  332. package/src/social-providers/vk.ts +124 -0
  333. package/src/social-providers/zoom.ts +238 -0
  334. package/src/types/context.ts +396 -0
  335. package/src/types/cookie.ts +10 -0
  336. package/src/types/helper.ts +26 -0
  337. package/src/types/index.ts +32 -0
  338. package/src/types/init-options.ts +1529 -0
  339. package/src/types/plugin-client.ts +127 -0
  340. package/src/types/plugin.ts +157 -0
  341. package/src/utils/db.ts +20 -0
  342. package/src/utils/deprecate.test.ts +72 -0
  343. package/src/utils/deprecate.ts +21 -0
  344. package/src/utils/error-codes.ts +65 -0
  345. package/src/utils/id.ts +5 -0
  346. package/src/utils/ip.test.ts +255 -0
  347. package/src/utils/ip.ts +211 -0
  348. package/src/utils/json.ts +25 -0
  349. package/src/utils/string.ts +3 -0
  350. package/src/utils/url.ts +43 -0
  351. package/tsconfig.json +7 -0
  352. package/tsdown.config.ts +35 -0
  353. package/vitest.config.ts +3 -0
@@ -0,0 +1,19 @@
1
+ import { AuthContext } from "../types/context.mjs";
2
+ import "../types/index.mjs";
3
+ import { EndpointContext, InputContext } from "better-call";
4
+
5
+ //#region src/context/endpoint-context.d.ts
6
+ type AuthEndpointContext = Partial<InputContext<string, any> & EndpointContext<string, any>> & {
7
+ context: AuthContext;
8
+ };
9
+ /**
10
+ * This is for internal use only. Most users should use `getCurrentAuthContext` instead.
11
+ *
12
+ * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
13
+ */
14
+ declare function getCurrentAuthContextAsyncLocalStorage(): Promise<AsyncLocalStorage<AuthEndpointContext>>;
15
+ declare function getCurrentAuthContext(): Promise<AuthEndpointContext>;
16
+ declare function runWithEndpointContext<T>(context: AuthEndpointContext, fn: () => T): Promise<T>;
17
+ //#endregion
18
+ export { AuthEndpointContext, getCurrentAuthContext, getCurrentAuthContextAsyncLocalStorage, runWithEndpointContext };
19
+ //# sourceMappingURL=endpoint-context.d.mts.map
@@ -0,0 +1,32 @@
1
+ import { __getBetterAuthGlobal } from "./global.mjs";
2
+ import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
+
4
+ //#region src/context/endpoint-context.ts
5
+ const ensureAsyncStorage = async () => {
6
+ const betterAuthGlobal = __getBetterAuthGlobal();
7
+ if (!betterAuthGlobal.context.endpointContextAsyncStorage) {
8
+ const AsyncLocalStorage = await getAsyncLocalStorage();
9
+ betterAuthGlobal.context.endpointContextAsyncStorage = new AsyncLocalStorage();
10
+ }
11
+ return betterAuthGlobal.context.endpointContextAsyncStorage;
12
+ };
13
+ /**
14
+ * This is for internal use only. Most users should use `getCurrentAuthContext` instead.
15
+ *
16
+ * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
17
+ */
18
+ async function getCurrentAuthContextAsyncLocalStorage() {
19
+ return ensureAsyncStorage();
20
+ }
21
+ async function getCurrentAuthContext() {
22
+ const context = (await ensureAsyncStorage()).getStore();
23
+ if (!context) throw new Error("No auth context found. Please make sure you are calling this function within a `runWithEndpointContext` callback.");
24
+ return context;
25
+ }
26
+ async function runWithEndpointContext(context, fn) {
27
+ return (await ensureAsyncStorage()).run(context, fn);
28
+ }
29
+
30
+ //#endregion
31
+ export { getCurrentAuthContext, getCurrentAuthContextAsyncLocalStorage, runWithEndpointContext };
32
+ //# sourceMappingURL=endpoint-context.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint-context.mjs","names":[],"sources":["../../src/context/endpoint-context.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport { getAsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport type { EndpointContext, InputContext } from \"better-call\";\nimport type { AuthContext } from \"../types\";\nimport { __getBetterAuthGlobal } from \"./global\";\n\nexport type AuthEndpointContext = Partial<\n\tInputContext<string, any> & EndpointContext<string, any>\n> & {\n\tcontext: AuthContext;\n};\n\nconst ensureAsyncStorage = async () => {\n\tconst betterAuthGlobal = __getBetterAuthGlobal();\n\tif (!betterAuthGlobal.context.endpointContextAsyncStorage) {\n\t\tconst AsyncLocalStorage = await getAsyncLocalStorage();\n\t\tbetterAuthGlobal.context.endpointContextAsyncStorage =\n\t\t\tnew AsyncLocalStorage<AuthEndpointContext>();\n\t}\n\treturn betterAuthGlobal.context\n\t\t.endpointContextAsyncStorage as AsyncLocalStorage<AuthEndpointContext>;\n};\n\n/**\n * This is for internal use only. Most users should use `getCurrentAuthContext` instead.\n *\n * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.\n */\nexport async function getCurrentAuthContextAsyncLocalStorage() {\n\treturn ensureAsyncStorage();\n}\n\nexport async function getCurrentAuthContext(): Promise<AuthEndpointContext> {\n\tconst als = await ensureAsyncStorage();\n\tconst context = als.getStore();\n\tif (!context) {\n\t\tthrow new Error(\n\t\t\t\"No auth context found. Please make sure you are calling this function within a `runWithEndpointContext` callback.\",\n\t\t);\n\t}\n\treturn context;\n}\n\nexport async function runWithEndpointContext<T>(\n\tcontext: AuthEndpointContext,\n\tfn: () => T,\n): Promise<T> {\n\tconst als = await ensureAsyncStorage();\n\treturn als.run(context, fn);\n}\n"],"mappings":";;;;AAYA,MAAM,qBAAqB,YAAY;CACtC,MAAM,mBAAmB,uBAAuB;AAChD,KAAI,CAAC,iBAAiB,QAAQ,6BAA6B;EAC1D,MAAM,oBAAoB,MAAM,sBAAsB;AACtD,mBAAiB,QAAQ,8BACxB,IAAI,mBAAwC;;AAE9C,QAAO,iBAAiB,QACtB;;;;;;;AAQH,eAAsB,yCAAyC;AAC9D,QAAO,oBAAoB;;AAG5B,eAAsB,wBAAsD;CAE3E,MAAM,WADM,MAAM,oBAAoB,EAClB,UAAU;AAC9B,KAAI,CAAC,QACJ,OAAM,IAAI,MACT,oHACA;AAEF,QAAO;;AAGR,eAAsB,uBACrB,SACA,IACa;AAEb,SADY,MAAM,oBAAoB,EAC3B,IAAI,SAAS,GAAG"}
@@ -0,0 +1,7 @@
1
+ import "@better-auth/core/async_hooks";
2
+
3
+ //#region src/context/global.d.ts
4
+ declare function getBetterAuthVersion(): string;
5
+ //#endregion
6
+ export { getBetterAuthVersion };
7
+ //# sourceMappingURL=global.d.mts.map
@@ -0,0 +1,38 @@
1
+ //#region src/context/global.ts
2
+ const symbol = Symbol.for("better-auth:global");
3
+ let bind = null;
4
+ const __context = {};
5
+ const __betterAuthVersion = "1.5.0-beta.9";
6
+ /**
7
+ * We store context instance in the globalThis.
8
+ *
9
+ * The reason we do this is that some bundlers, web framework, or package managers might
10
+ * create multiple copies of BetterAuth in the same process intentionally or unintentionally.
11
+ *
12
+ * For example, yarn v1, Next.js, SSR, Vite...
13
+ *
14
+ * @internal
15
+ */
16
+ function __getBetterAuthGlobal() {
17
+ if (!globalThis[symbol]) {
18
+ globalThis[symbol] = {
19
+ version: __betterAuthVersion,
20
+ epoch: 1,
21
+ context: __context
22
+ };
23
+ bind = globalThis[symbol];
24
+ }
25
+ bind = globalThis[symbol];
26
+ if (bind.version !== __betterAuthVersion) {
27
+ bind.version = __betterAuthVersion;
28
+ bind.epoch++;
29
+ }
30
+ return globalThis[symbol];
31
+ }
32
+ function getBetterAuthVersion() {
33
+ return __getBetterAuthGlobal().version;
34
+ }
35
+
36
+ //#endregion
37
+ export { __getBetterAuthGlobal, getBetterAuthVersion };
38
+ //# sourceMappingURL=global.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global.mjs","names":[],"sources":["../../src/context/global.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"@better-auth/core/async_hooks\";\n\ninterface BetterAuthGlobal {\n\t/**\n\t * The version of BetterAuth.\n\t */\n\tversion: string;\n\t/**\n\t * Used to track the number of BetterAuth instances in the same process.\n\t *\n\t * Debugging purposes only.\n\t */\n\tepoch: number;\n\t/**\n\t * Stores the AsyncLocalStorage instances for each context.\n\t */\n\tcontext: Record<string, AsyncLocalStorage<unknown>>;\n}\n\nconst symbol = Symbol.for(\"better-auth:global\");\nlet bind: BetterAuthGlobal | null = null;\n\nconst __context: Record<string, AsyncLocalStorage<unknown>> = {};\nconst __betterAuthVersion: string = import.meta.env\n\t.BETTER_AUTH_VERSION as string;\n\n/**\n * We store context instance in the globalThis.\n *\n * The reason we do this is that some bundlers, web framework, or package managers might\n * create multiple copies of BetterAuth in the same process intentionally or unintentionally.\n *\n * For example, yarn v1, Next.js, SSR, Vite...\n *\n * @internal\n */\nexport function __getBetterAuthGlobal(): BetterAuthGlobal {\n\tif (!(globalThis as any)[symbol]) {\n\t\t(globalThis as any)[symbol] = {\n\t\t\tversion: __betterAuthVersion,\n\t\t\tepoch: 1,\n\t\t\tcontext: __context,\n\t\t};\n\t\tbind = (globalThis as any)[symbol] as BetterAuthGlobal;\n\t}\n\tbind = (globalThis as any)[symbol] as BetterAuthGlobal;\n\tif (bind.version !== __betterAuthVersion) {\n\t\tbind.version = __betterAuthVersion;\n\t\t// Different versions of BetterAuth are loaded in the same process.\n\t\tbind.epoch++;\n\t}\n\treturn (globalThis as any)[symbol] as BetterAuthGlobal;\n}\n\nexport function getBetterAuthVersion(): string {\n\treturn __getBetterAuthGlobal().version;\n}\n"],"mappings":";AAmBA,MAAM,SAAS,OAAO,IAAI,qBAAqB;AAC/C,IAAI,OAAgC;AAEpC,MAAM,YAAwD,EAAE;AAChE,MAAM;;;;;;;;;;;AAaN,SAAgB,wBAA0C;AACzD,KAAI,CAAE,WAAmB,SAAS;AACjC,EAAC,WAAmB,UAAU;GAC7B,SAAS;GACT,OAAO;GACP,SAAS;GACT;AACD,SAAQ,WAAmB;;AAE5B,QAAQ,WAAmB;AAC3B,KAAI,KAAK,YAAY,qBAAqB;AACzC,OAAK,UAAU;AAEf,OAAK;;AAEN,QAAQ,WAAmB;;AAG5B,SAAgB,uBAA+B;AAC9C,QAAO,uBAAuB,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { AuthEndpointContext, getCurrentAuthContext, getCurrentAuthContextAsyncLocalStorage, runWithEndpointContext } from "./endpoint-context.mjs";
2
+ import { getBetterAuthVersion } from "./global.mjs";
3
+ import { RequestState, RequestStateWeakMap, defineRequestState, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, runWithRequestState } from "./request-state.mjs";
4
+ import { getCurrentAdapter, getCurrentDBAdapterAsyncLocalStorage, queueAfterTransactionHook, runWithAdapter, runWithTransaction } from "./transaction.mjs";
5
+ export { type AuthEndpointContext, type RequestState, type RequestStateWeakMap, defineRequestState, getBetterAuthVersion, getCurrentAdapter, getCurrentAuthContext, getCurrentAuthContextAsyncLocalStorage, getCurrentDBAdapterAsyncLocalStorage, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, queueAfterTransactionHook, runWithAdapter, runWithEndpointContext, runWithRequestState, runWithTransaction };
@@ -0,0 +1,6 @@
1
+ import { getBetterAuthVersion } from "./global.mjs";
2
+ import { getCurrentAuthContext, getCurrentAuthContextAsyncLocalStorage, runWithEndpointContext } from "./endpoint-context.mjs";
3
+ import { defineRequestState, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, runWithRequestState } from "./request-state.mjs";
4
+ import { getCurrentAdapter, getCurrentDBAdapterAsyncLocalStorage, queueAfterTransactionHook, runWithAdapter, runWithTransaction } from "./transaction.mjs";
5
+
6
+ export { defineRequestState, getBetterAuthVersion, getCurrentAdapter, getCurrentAuthContext, getCurrentAuthContextAsyncLocalStorage, getCurrentDBAdapterAsyncLocalStorage, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, queueAfterTransactionHook, runWithAdapter, runWithEndpointContext, runWithRequestState, runWithTransaction };
@@ -0,0 +1,26 @@
1
+ //#region src/context/request-state.d.ts
2
+ type RequestStateWeakMap = WeakMap<object, any>;
3
+ declare function getRequestStateAsyncLocalStorage(): Promise<AsyncLocalStorage<RequestStateWeakMap>>;
4
+ declare function hasRequestState(): Promise<boolean>;
5
+ declare function getCurrentRequestState(): Promise<RequestStateWeakMap>;
6
+ declare function runWithRequestState<T>(store: RequestStateWeakMap, fn: () => T): Promise<T>;
7
+ interface RequestState<T> {
8
+ get(): Promise<T>;
9
+ set(value: T): Promise<void>;
10
+ readonly ref: Readonly<object>;
11
+ }
12
+ /**
13
+ * Defines a request-scoped state with lazy initialization.
14
+ *
15
+ * @param initFn - A function that initializes the state. It is called the first time `get()` is invoked within each request context, and only once per context.
16
+ * @returns A RequestState object with `get` and `set` methods, and a unique `ref` for debugging.
17
+ *
18
+ * @example
19
+ * const userState = defineRequestState(() => ({ id: '', name: '' }));
20
+ * // Later, within a request context:
21
+ * const user = await userState.get();
22
+ */
23
+ declare function defineRequestState<T>(initFn: () => T | Promise<T>): RequestState<T>;
24
+ //#endregion
25
+ export { RequestState, RequestStateWeakMap, defineRequestState, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, runWithRequestState };
26
+ //# sourceMappingURL=request-state.d.mts.map
@@ -0,0 +1,50 @@
1
+ import { __getBetterAuthGlobal } from "./global.mjs";
2
+ import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
+
4
+ //#region src/context/request-state.ts
5
+ const ensureAsyncStorage = async () => {
6
+ const betterAuthGlobal = __getBetterAuthGlobal();
7
+ if (!betterAuthGlobal.context.requestStateAsyncStorage) {
8
+ const AsyncLocalStorage = await getAsyncLocalStorage();
9
+ betterAuthGlobal.context.requestStateAsyncStorage = new AsyncLocalStorage();
10
+ }
11
+ return betterAuthGlobal.context.requestStateAsyncStorage;
12
+ };
13
+ async function getRequestStateAsyncLocalStorage() {
14
+ return ensureAsyncStorage();
15
+ }
16
+ async function hasRequestState() {
17
+ return (await ensureAsyncStorage()).getStore() !== void 0;
18
+ }
19
+ async function getCurrentRequestState() {
20
+ const store = (await ensureAsyncStorage()).getStore();
21
+ if (!store) throw new Error("No request state found. Please make sure you are calling this function within a `runWithRequestState` callback.");
22
+ return store;
23
+ }
24
+ async function runWithRequestState(store, fn) {
25
+ return (await ensureAsyncStorage()).run(store, fn);
26
+ }
27
+ function defineRequestState(initFn) {
28
+ const ref = Object.freeze({});
29
+ return {
30
+ get ref() {
31
+ return ref;
32
+ },
33
+ async get() {
34
+ const store = await getCurrentRequestState();
35
+ if (!store.has(ref)) {
36
+ const initialValue = await initFn();
37
+ store.set(ref, initialValue);
38
+ return initialValue;
39
+ }
40
+ return store.get(ref);
41
+ },
42
+ async set(value) {
43
+ (await getCurrentRequestState()).set(ref, value);
44
+ }
45
+ };
46
+ }
47
+
48
+ //#endregion
49
+ export { defineRequestState, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, runWithRequestState };
50
+ //# sourceMappingURL=request-state.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-state.mjs","names":[],"sources":["../../src/context/request-state.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport { getAsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport { __getBetterAuthGlobal } from \"./global\";\n\nexport type RequestStateWeakMap = WeakMap<object, any>;\n\nconst ensureAsyncStorage = async () => {\n\tconst betterAuthGlobal = __getBetterAuthGlobal();\n\tif (!betterAuthGlobal.context.requestStateAsyncStorage) {\n\t\tconst AsyncLocalStorage = await getAsyncLocalStorage();\n\t\tbetterAuthGlobal.context.requestStateAsyncStorage =\n\t\t\tnew AsyncLocalStorage<RequestStateWeakMap>();\n\t}\n\treturn betterAuthGlobal.context\n\t\t.requestStateAsyncStorage as AsyncLocalStorage<RequestStateWeakMap>;\n};\n\nexport async function getRequestStateAsyncLocalStorage() {\n\treturn ensureAsyncStorage();\n}\n\nexport async function hasRequestState() {\n\tconst als = await ensureAsyncStorage();\n\treturn als.getStore() !== undefined;\n}\n\nexport async function getCurrentRequestState(): Promise<RequestStateWeakMap> {\n\tconst als = await ensureAsyncStorage();\n\tconst store = als.getStore();\n\tif (!store) {\n\t\tthrow new Error(\n\t\t\t\"No request state found. Please make sure you are calling this function within a `runWithRequestState` callback.\",\n\t\t);\n\t}\n\treturn store;\n}\n\nexport async function runWithRequestState<T>(\n\tstore: RequestStateWeakMap,\n\tfn: () => T,\n): Promise<T> {\n\tconst als = await ensureAsyncStorage();\n\treturn als.run(store, fn);\n}\n\nexport interface RequestState<T> {\n\tget(): Promise<T>;\n\tset(value: T): Promise<void>;\n\n\t// A unique reference used as a key to identify this state within the request's WeakMap. Useful for debugging purposes.\n\treadonly ref: Readonly<object>;\n}\n\n/**\n * Defines a request-scoped state with lazy initialization.\n *\n * @param initFn - A function that initializes the state. It is called the first time `get()` is invoked within each request context, and only once per context.\n * @returns A RequestState object with `get` and `set` methods, and a unique `ref` for debugging.\n *\n * @example\n * const userState = defineRequestState(() => ({ id: '', name: '' }));\n * // Later, within a request context:\n * const user = await userState.get();\n */\nexport function defineRequestState<T>(\n\tinitFn: () => T | Promise<T>,\n): RequestState<T>;\nexport function defineRequestState(\n\tinitFn: () => any | Promise<any>,\n): RequestState<any> {\n\tconst ref = Object.freeze({});\n\treturn {\n\t\tget ref(): Readonly<object> {\n\t\t\treturn ref;\n\t\t},\n\t\tasync get() {\n\t\t\tconst store = await getCurrentRequestState();\n\t\t\tif (!store.has(ref)) {\n\t\t\t\tconst initialValue = await initFn();\n\t\t\t\tstore.set(ref, initialValue);\n\t\t\t\treturn initialValue;\n\t\t\t}\n\t\t\treturn store.get(ref);\n\t\t},\n\n\t\tasync set(value) {\n\t\t\tconst store = await getCurrentRequestState();\n\t\t\tstore.set(ref, value);\n\t\t},\n\t};\n}\n"],"mappings":";;;;AAMA,MAAM,qBAAqB,YAAY;CACtC,MAAM,mBAAmB,uBAAuB;AAChD,KAAI,CAAC,iBAAiB,QAAQ,0BAA0B;EACvD,MAAM,oBAAoB,MAAM,sBAAsB;AACtD,mBAAiB,QAAQ,2BACxB,IAAI,mBAAwC;;AAE9C,QAAO,iBAAiB,QACtB;;AAGH,eAAsB,mCAAmC;AACxD,QAAO,oBAAoB;;AAG5B,eAAsB,kBAAkB;AAEvC,SADY,MAAM,oBAAoB,EAC3B,UAAU,KAAK;;AAG3B,eAAsB,yBAAuD;CAE5E,MAAM,SADM,MAAM,oBAAoB,EACpB,UAAU;AAC5B,KAAI,CAAC,MACJ,OAAM,IAAI,MACT,kHACA;AAEF,QAAO;;AAGR,eAAsB,oBACrB,OACA,IACa;AAEb,SADY,MAAM,oBAAoB,EAC3B,IAAI,OAAO,GAAG;;AAyB1B,SAAgB,mBACf,QACoB;CACpB,MAAM,MAAM,OAAO,OAAO,EAAE,CAAC;AAC7B,QAAO;EACN,IAAI,MAAwB;AAC3B,UAAO;;EAER,MAAM,MAAM;GACX,MAAM,QAAQ,MAAM,wBAAwB;AAC5C,OAAI,CAAC,MAAM,IAAI,IAAI,EAAE;IACpB,MAAM,eAAe,MAAM,QAAQ;AACnC,UAAM,IAAI,KAAK,aAAa;AAC5B,WAAO;;AAER,UAAO,MAAM,IAAI,IAAI;;EAGtB,MAAM,IAAI,OAAO;AAEhB,IADc,MAAM,wBAAwB,EACtC,IAAI,KAAK,MAAM;;EAEtB"}
@@ -0,0 +1,25 @@
1
+ import { DBAdapter, DBTransactionAdapter } from "../db/adapter/index.mjs";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+
4
+ //#region src/context/transaction.d.ts
5
+ type HookContext = {
6
+ adapter: DBTransactionAdapter;
7
+ pendingHooks: Array<() => Promise<void>>;
8
+ };
9
+ /**
10
+ * This is for internal use only. Most users should use `getCurrentAdapter` instead.
11
+ *
12
+ * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
13
+ */
14
+ declare const getCurrentDBAdapterAsyncLocalStorage: () => Promise<AsyncLocalStorage<HookContext>>;
15
+ declare const getCurrentAdapter: (fallback: DBTransactionAdapter) => Promise<DBTransactionAdapter>;
16
+ declare const runWithAdapter: <R>(adapter: DBAdapter, fn: () => R) => Promise<R>;
17
+ declare const runWithTransaction: <R>(adapter: DBAdapter, fn: () => R) => Promise<R>;
18
+ /**
19
+ * Queue a hook to be executed after the current transaction commits.
20
+ * If not in a transaction, the hook will execute immediately.
21
+ */
22
+ declare const queueAfterTransactionHook: (hook: () => Promise<void>) => Promise<void>;
23
+ //#endregion
24
+ export { getCurrentAdapter, getCurrentDBAdapterAsyncLocalStorage, queueAfterTransactionHook, runWithAdapter, runWithTransaction };
25
+ //# sourceMappingURL=transaction.d.mts.map
@@ -0,0 +1,96 @@
1
+ import { __getBetterAuthGlobal } from "./global.mjs";
2
+ import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
+
4
+ //#region src/context/transaction.ts
5
+ const ensureAsyncStorage = async () => {
6
+ const betterAuthGlobal = __getBetterAuthGlobal();
7
+ if (!betterAuthGlobal.context.adapterAsyncStorage) {
8
+ const AsyncLocalStorage = await getAsyncLocalStorage();
9
+ betterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();
10
+ }
11
+ return betterAuthGlobal.context.adapterAsyncStorage;
12
+ };
13
+ /**
14
+ * This is for internal use only. Most users should use `getCurrentAdapter` instead.
15
+ *
16
+ * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
17
+ */
18
+ const getCurrentDBAdapterAsyncLocalStorage = async () => {
19
+ return ensureAsyncStorage();
20
+ };
21
+ const getCurrentAdapter = async (fallback) => {
22
+ return ensureAsyncStorage().then((als) => {
23
+ return als.getStore()?.adapter || fallback;
24
+ }).catch(() => {
25
+ return fallback;
26
+ });
27
+ };
28
+ const runWithAdapter = async (adapter, fn) => {
29
+ let called = false;
30
+ return ensureAsyncStorage().then(async (als) => {
31
+ called = true;
32
+ const pendingHooks = [];
33
+ let result;
34
+ let error;
35
+ let hasError = false;
36
+ try {
37
+ result = await als.run({
38
+ adapter,
39
+ pendingHooks
40
+ }, fn);
41
+ } catch (err) {
42
+ error = err;
43
+ hasError = true;
44
+ }
45
+ for (const hook of pendingHooks) await hook();
46
+ if (hasError) throw error;
47
+ return result;
48
+ }).catch((err) => {
49
+ if (!called) return fn();
50
+ throw err;
51
+ });
52
+ };
53
+ const runWithTransaction = async (adapter, fn) => {
54
+ let called = true;
55
+ return ensureAsyncStorage().then(async (als) => {
56
+ called = true;
57
+ const pendingHooks = [];
58
+ let result;
59
+ let error;
60
+ let hasError = false;
61
+ try {
62
+ result = await adapter.transaction(async (trx) => {
63
+ return als.run({
64
+ adapter: trx,
65
+ pendingHooks
66
+ }, fn);
67
+ });
68
+ } catch (e) {
69
+ hasError = true;
70
+ error = e;
71
+ }
72
+ for (const hook of pendingHooks) await hook();
73
+ if (hasError) throw error;
74
+ return result;
75
+ }).catch((err) => {
76
+ if (!called) return fn();
77
+ throw err;
78
+ });
79
+ };
80
+ /**
81
+ * Queue a hook to be executed after the current transaction commits.
82
+ * If not in a transaction, the hook will execute immediately.
83
+ */
84
+ const queueAfterTransactionHook = async (hook) => {
85
+ return ensureAsyncStorage().then((als) => {
86
+ const store = als.getStore();
87
+ if (store) store.pendingHooks.push(hook);
88
+ else return hook();
89
+ }).catch(() => {
90
+ return hook();
91
+ });
92
+ };
93
+
94
+ //#endregion
95
+ export { getCurrentAdapter, getCurrentDBAdapterAsyncLocalStorage, queueAfterTransactionHook, runWithAdapter, runWithTransaction };
96
+ //# sourceMappingURL=transaction.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.mjs","names":[],"sources":["../../src/context/transaction.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"node:async_hooks\";\nimport { getAsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport type { DBAdapter, DBTransactionAdapter } from \"../db/adapter\";\nimport { __getBetterAuthGlobal } from \"./global\";\n\ntype HookContext = {\n\tadapter: DBTransactionAdapter;\n\tpendingHooks: Array<() => Promise<void>>;\n};\n\nconst ensureAsyncStorage = async () => {\n\tconst betterAuthGlobal = __getBetterAuthGlobal();\n\tif (!betterAuthGlobal.context.adapterAsyncStorage) {\n\t\tconst AsyncLocalStorage = await getAsyncLocalStorage();\n\t\tbetterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();\n\t}\n\treturn betterAuthGlobal.context\n\t\t.adapterAsyncStorage as AsyncLocalStorage<HookContext>;\n};\n\n/**\n * This is for internal use only. Most users should use `getCurrentAdapter` instead.\n *\n * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.\n */\nexport const getCurrentDBAdapterAsyncLocalStorage = async () => {\n\treturn ensureAsyncStorage();\n};\n\nexport const getCurrentAdapter = async (\n\tfallback: DBTransactionAdapter,\n): Promise<DBTransactionAdapter> => {\n\treturn ensureAsyncStorage()\n\t\t.then((als) => {\n\t\t\tconst store = als.getStore();\n\t\t\treturn store?.adapter || fallback;\n\t\t})\n\t\t.catch(() => {\n\t\t\treturn fallback;\n\t\t});\n};\n\nexport const runWithAdapter = async <R>(\n\tadapter: DBAdapter,\n\tfn: () => R,\n): Promise<R> => {\n\tlet called = false;\n\treturn ensureAsyncStorage()\n\t\t.then(async (als) => {\n\t\t\tcalled = true;\n\t\t\tconst pendingHooks: Array<() => Promise<void>> = [];\n\t\t\tlet result: Awaited<R>;\n\t\t\tlet error: unknown;\n\t\t\tlet hasError = false;\n\t\t\ttry {\n\t\t\t\tresult = await als.run({ adapter, pendingHooks }, fn);\n\t\t\t} catch (err) {\n\t\t\t\terror = err;\n\t\t\t\thasError = true;\n\t\t\t}\n\t\t\t// Execute pending hooks after the function completes (even if it threw)\n\t\t\tfor (const hook of pendingHooks) {\n\t\t\t\tawait hook();\n\t\t\t}\n\t\t\tif (hasError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\treturn result!;\n\t\t})\n\t\t.catch((err) => {\n\t\t\tif (!called) {\n\t\t\t\treturn fn();\n\t\t\t}\n\t\t\tthrow err;\n\t\t});\n};\n\nexport const runWithTransaction = async <R>(\n\tadapter: DBAdapter,\n\tfn: () => R,\n): Promise<R> => {\n\tlet called = true;\n\treturn ensureAsyncStorage()\n\t\t.then(async (als) => {\n\t\t\tcalled = true;\n\t\t\tconst pendingHooks: Array<() => Promise<void>> = [];\n\t\t\tlet result: Awaited<R>;\n\t\t\tlet error: unknown;\n\t\t\tlet hasError = false;\n\t\t\ttry {\n\t\t\t\tresult = await adapter.transaction(async (trx) => {\n\t\t\t\t\treturn als.run({ adapter: trx, pendingHooks }, fn);\n\t\t\t\t});\n\t\t\t} catch (e) {\n\t\t\t\thasError = true;\n\t\t\t\terror = e;\n\t\t\t}\n\t\t\tfor (const hook of pendingHooks) {\n\t\t\t\tawait hook();\n\t\t\t}\n\t\t\tif (hasError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\treturn result!;\n\t\t})\n\t\t.catch((err) => {\n\t\t\tif (!called) {\n\t\t\t\treturn fn();\n\t\t\t}\n\t\t\tthrow err;\n\t\t});\n};\n\n/**\n * Queue a hook to be executed after the current transaction commits.\n * If not in a transaction, the hook will execute immediately.\n */\nexport const queueAfterTransactionHook = async (\n\thook: () => Promise<void>,\n): Promise<void> => {\n\treturn ensureAsyncStorage()\n\t\t.then((als) => {\n\t\t\tconst store = als.getStore();\n\t\t\tif (store) {\n\t\t\t\t// We're in a transaction context, queue the hook\n\t\t\t\tstore.pendingHooks.push(hook);\n\t\t\t} else {\n\t\t\t\t// Not in a transaction, execute immediately\n\t\t\t\treturn hook();\n\t\t\t}\n\t\t})\n\t\t.catch(() => {\n\t\t\t// No async storage available, execute immediately\n\t\t\treturn hook();\n\t\t});\n};\n"],"mappings":";;;;AAUA,MAAM,qBAAqB,YAAY;CACtC,MAAM,mBAAmB,uBAAuB;AAChD,KAAI,CAAC,iBAAiB,QAAQ,qBAAqB;EAClD,MAAM,oBAAoB,MAAM,sBAAsB;AACtD,mBAAiB,QAAQ,sBAAsB,IAAI,mBAAmB;;AAEvE,QAAO,iBAAiB,QACtB;;;;;;;AAQH,MAAa,uCAAuC,YAAY;AAC/D,QAAO,oBAAoB;;AAG5B,MAAa,oBAAoB,OAChC,aACmC;AACnC,QAAO,oBAAoB,CACzB,MAAM,QAAQ;AAEd,SADc,IAAI,UAAU,EACd,WAAW;GACxB,CACD,YAAY;AACZ,SAAO;GACN;;AAGJ,MAAa,iBAAiB,OAC7B,SACA,OACgB;CAChB,IAAI,SAAS;AACb,QAAO,oBAAoB,CACzB,KAAK,OAAO,QAAQ;AACpB,WAAS;EACT,MAAM,eAA2C,EAAE;EACnD,IAAI;EACJ,IAAI;EACJ,IAAI,WAAW;AACf,MAAI;AACH,YAAS,MAAM,IAAI,IAAI;IAAE;IAAS;IAAc,EAAE,GAAG;WAC7C,KAAK;AACb,WAAQ;AACR,cAAW;;AAGZ,OAAK,MAAM,QAAQ,aAClB,OAAM,MAAM;AAEb,MAAI,SACH,OAAM;AAEP,SAAO;GACN,CACD,OAAO,QAAQ;AACf,MAAI,CAAC,OACJ,QAAO,IAAI;AAEZ,QAAM;GACL;;AAGJ,MAAa,qBAAqB,OACjC,SACA,OACgB;CAChB,IAAI,SAAS;AACb,QAAO,oBAAoB,CACzB,KAAK,OAAO,QAAQ;AACpB,WAAS;EACT,MAAM,eAA2C,EAAE;EACnD,IAAI;EACJ,IAAI;EACJ,IAAI,WAAW;AACf,MAAI;AACH,YAAS,MAAM,QAAQ,YAAY,OAAO,QAAQ;AACjD,WAAO,IAAI,IAAI;KAAE,SAAS;KAAK;KAAc,EAAE,GAAG;KACjD;WACM,GAAG;AACX,cAAW;AACX,WAAQ;;AAET,OAAK,MAAM,QAAQ,aAClB,OAAM,MAAM;AAEb,MAAI,SACH,OAAM;AAEP,SAAO;GACN,CACD,OAAO,QAAQ;AACf,MAAI,CAAC,OACJ,QAAO,IAAI;AAEZ,QAAM;GACL;;;;;;AAOJ,MAAa,4BAA4B,OACxC,SACmB;AACnB,QAAO,oBAAoB,CACzB,MAAM,QAAQ;EACd,MAAM,QAAQ,IAAI,UAAU;AAC5B,MAAI,MAEH,OAAM,aAAa,KAAK,KAAK;MAG7B,QAAO,MAAM;GAEb,CACD,YAAY;AAEZ,SAAO,MAAM;GACZ"}
@@ -0,0 +1,28 @@
1
+ import { initGetDefaultFieldName } from "./get-default-field-name.mjs";
2
+ import { initGetDefaultModelName } from "./get-default-model-name.mjs";
3
+ import { initGetFieldAttributes } from "./get-field-attributes.mjs";
4
+ import { initGetFieldName } from "./get-field-name.mjs";
5
+ import { initGetIdField } from "./get-id-field.mjs";
6
+ import { initGetModelName } from "./get-model-name.mjs";
7
+ import { AdapterConfig, AdapterFactoryConfig, AdapterFactoryCustomizeAdapterCreator, AdapterFactoryOptions, AdapterTestDebugLogs, CleanedWhere, CreateAdapterOptions, CreateCustomAdapter, CustomAdapter } from "./types.mjs";
8
+ import { DBAdapter } from "./index.mjs";
9
+ import { BetterAuthOptions } from "../../types/init-options.mjs";
10
+ import "../../types/index.mjs";
11
+
12
+ //#region src/db/adapter/factory.d.ts
13
+ type AdapterFactory = (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
14
+ declare const createAdapterFactory: ({
15
+ adapter: customAdapter,
16
+ config: cfg
17
+ }: AdapterFactoryOptions) => AdapterFactory;
18
+ /**
19
+ * @deprecated Use `createAdapterFactory` instead. This export will be removed in a future version.
20
+ * @alias
21
+ */
22
+ declare const createAdapter: ({
23
+ adapter: customAdapter,
24
+ config: cfg
25
+ }: AdapterFactoryOptions) => AdapterFactory;
26
+ //#endregion
27
+ export { AdapterFactory, createAdapter, createAdapterFactory };
28
+ //# sourceMappingURL=factory.d.mts.map