devflare 1.0.0-next.14 → 1.0.0-next.16

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 (380) hide show
  1. package/LLM.md +9360 -1784
  2. package/README.md +391 -32
  3. package/bin/devflare.js +17 -7
  4. package/dist/account-0w8wdzjv.js +475 -0
  5. package/dist/account-eygq6qx7.js +475 -0
  6. package/dist/account-fw8nafav.js +475 -0
  7. package/dist/account-pzq69nys.js +475 -0
  8. package/dist/account-s66jb15j.js +475 -0
  9. package/dist/api-d6ekexs5.js +25 -0
  10. package/dist/bridge/index.d.ts +1 -1
  11. package/dist/bridge/index.d.ts.map +1 -1
  12. package/dist/bridge/miniflare.d.ts.map +1 -1
  13. package/dist/bridge/protocol.d.ts +1 -1
  14. package/dist/bridge/protocol.d.ts.map +1 -1
  15. package/dist/bridge/proxy.d.ts +0 -4
  16. package/dist/bridge/proxy.d.ts.map +1 -1
  17. package/dist/bridge/serialization.d.ts.map +1 -1
  18. package/dist/bridge/server.d.ts +1 -1
  19. package/dist/bridge/server.d.ts.map +1 -1
  20. package/dist/browser-shim/handler.d.ts +1 -1
  21. package/dist/browser-shim/handler.d.ts.map +1 -1
  22. package/dist/browser.d.ts +1651 -34
  23. package/dist/browser.d.ts.map +1 -1
  24. package/dist/build-1kmkwqgh.js +53 -0
  25. package/dist/build-506kjhcm.js +53 -0
  26. package/dist/build-66866ahs.js +53 -0
  27. package/dist/build-g1adm3ww.js +53 -0
  28. package/dist/build-p3r3117t.js +53 -0
  29. package/dist/bundler/do-bundler.d.ts.map +1 -1
  30. package/dist/bundler/rolldown-shared.d.ts +24 -0
  31. package/dist/bundler/rolldown-shared.d.ts.map +1 -0
  32. package/dist/bundler/worker-bundler.d.ts +0 -1
  33. package/dist/bundler/worker-bundler.d.ts.map +1 -1
  34. package/dist/cli/command-utils.d.ts +18 -0
  35. package/dist/cli/command-utils.d.ts.map +1 -0
  36. package/dist/cli/commands/account.d.ts +1 -1
  37. package/dist/cli/commands/account.d.ts.map +1 -1
  38. package/dist/cli/commands/build-artifacts.d.ts +27 -0
  39. package/dist/cli/commands/build-artifacts.d.ts.map +1 -0
  40. package/dist/cli/commands/build.d.ts.map +1 -1
  41. package/dist/cli/commands/config.d.ts +4 -0
  42. package/dist/cli/commands/config.d.ts.map +1 -0
  43. package/dist/cli/commands/deploy.d.ts.map +1 -1
  44. package/dist/cli/commands/dev.d.ts.map +1 -1
  45. package/dist/cli/commands/doctor.d.ts.map +1 -1
  46. package/dist/cli/commands/init.d.ts.map +1 -1
  47. package/dist/cli/commands/login.d.ts +4 -0
  48. package/dist/cli/commands/login.d.ts.map +1 -0
  49. package/dist/cli/commands/previews-support/cleanup.d.ts +9 -0
  50. package/dist/cli/commands/previews-support/cleanup.d.ts.map +1 -0
  51. package/dist/cli/commands/previews-support/family.d.ts +10 -0
  52. package/dist/cli/commands/previews-support/family.d.ts.map +1 -0
  53. package/dist/cli/commands/previews-support/render.d.ts +8 -0
  54. package/dist/cli/commands/previews-support/render.d.ts.map +1 -0
  55. package/dist/cli/commands/previews-support/theme.d.ts +10 -0
  56. package/dist/cli/commands/previews-support/theme.d.ts.map +1 -0
  57. package/dist/cli/commands/previews-support/types.d.ts +70 -0
  58. package/dist/cli/commands/previews-support/types.d.ts.map +1 -0
  59. package/dist/cli/commands/previews.d.ts +4 -0
  60. package/dist/cli/commands/previews.d.ts.map +1 -0
  61. package/dist/cli/commands/productions.d.ts +4 -0
  62. package/dist/cli/commands/productions.d.ts.map +1 -0
  63. package/dist/cli/commands/token.d.ts +4 -0
  64. package/dist/cli/commands/token.d.ts.map +1 -0
  65. package/dist/cli/commands/type-generation/discovery.d.ts +7 -0
  66. package/dist/cli/commands/type-generation/discovery.d.ts.map +1 -0
  67. package/dist/cli/commands/type-generation/generator.d.ts +44 -0
  68. package/dist/cli/commands/type-generation/generator.d.ts.map +1 -0
  69. package/dist/cli/commands/type-generation/models.d.ts +27 -0
  70. package/dist/cli/commands/type-generation/models.d.ts.map +1 -0
  71. package/dist/cli/commands/types.d.ts.map +1 -1
  72. package/dist/cli/commands/worker.d.ts +4 -0
  73. package/dist/cli/commands/worker.d.ts.map +1 -0
  74. package/dist/cli/config-path.d.ts +2 -1
  75. package/dist/cli/config-path.d.ts.map +1 -1
  76. package/dist/cli/deploy-strategy.d.ts +17 -0
  77. package/dist/cli/deploy-strategy.d.ts.map +1 -0
  78. package/dist/cli/deploy-target.d.ts +17 -0
  79. package/dist/cli/deploy-target.d.ts.map +1 -0
  80. package/dist/cli/generated-artifacts.d.ts +12 -0
  81. package/dist/cli/generated-artifacts.d.ts.map +1 -0
  82. package/dist/cli/help-pages/pages/account.d.ts +3 -0
  83. package/dist/cli/help-pages/pages/account.d.ts.map +1 -0
  84. package/dist/cli/help-pages/pages/core.d.ts +4 -0
  85. package/dist/cli/help-pages/pages/core.d.ts.map +1 -0
  86. package/dist/cli/help-pages/pages/index.d.ts +3 -0
  87. package/dist/cli/help-pages/pages/index.d.ts.map +1 -0
  88. package/dist/cli/help-pages/pages/misc.d.ts +3 -0
  89. package/dist/cli/help-pages/pages/misc.d.ts.map +1 -0
  90. package/dist/cli/help-pages/pages/previews.d.ts +3 -0
  91. package/dist/cli/help-pages/pages/previews.d.ts.map +1 -0
  92. package/dist/cli/help-pages/pages/productions.d.ts +3 -0
  93. package/dist/cli/help-pages/pages/productions.d.ts.map +1 -0
  94. package/dist/cli/help-pages/render.d.ts +12 -0
  95. package/dist/cli/help-pages/render.d.ts.map +1 -0
  96. package/dist/cli/help-pages/shared.d.ts +15 -0
  97. package/dist/cli/help-pages/shared.d.ts.map +1 -0
  98. package/dist/cli/help-pages/types.d.ts +23 -0
  99. package/dist/cli/help-pages/types.d.ts.map +1 -0
  100. package/dist/cli/help.d.ts +6 -0
  101. package/dist/cli/help.d.ts.map +1 -0
  102. package/dist/cli/index.d.ts +1 -0
  103. package/dist/cli/index.d.ts.map +1 -1
  104. package/dist/cli/preview-bindings.d.ts +42 -0
  105. package/dist/cli/preview-bindings.d.ts.map +1 -0
  106. package/dist/cli/preview.d.ts +11 -0
  107. package/dist/cli/preview.d.ts.map +1 -0
  108. package/dist/cli/ui.d.ts +37 -0
  109. package/dist/cli/ui.d.ts.map +1 -0
  110. package/dist/cli/workspace-build-guard.d.ts +14 -0
  111. package/dist/cli/workspace-build-guard.d.ts.map +1 -0
  112. package/dist/cloudflare/account-core.d.ts +6 -0
  113. package/dist/cloudflare/account-core.d.ts.map +1 -0
  114. package/dist/cloudflare/account-resources.d.ts +40 -0
  115. package/dist/cloudflare/account-resources.d.ts.map +1 -0
  116. package/dist/cloudflare/account-status.d.ts +11 -0
  117. package/dist/cloudflare/account-status.d.ts.map +1 -0
  118. package/dist/cloudflare/account-workers.d.ts +14 -0
  119. package/dist/cloudflare/account-workers.d.ts.map +1 -0
  120. package/dist/cloudflare/account.d.ts +7 -64
  121. package/dist/cloudflare/account.d.ts.map +1 -1
  122. package/dist/cloudflare/api.d.ts +4 -0
  123. package/dist/cloudflare/api.d.ts.map +1 -1
  124. package/dist/cloudflare/index.d.ts +57 -2
  125. package/dist/cloudflare/index.d.ts.map +1 -1
  126. package/dist/cloudflare/kv-namespace.d.ts +3 -0
  127. package/dist/cloudflare/kv-namespace.d.ts.map +1 -0
  128. package/dist/cloudflare/preferences.d.ts.map +1 -1
  129. package/dist/cloudflare/preview-registry-cache.d.ts +6 -0
  130. package/dist/cloudflare/preview-registry-cache.d.ts.map +1 -0
  131. package/dist/cloudflare/preview-registry-records.d.ts +61 -0
  132. package/dist/cloudflare/preview-registry-records.d.ts.map +1 -0
  133. package/dist/cloudflare/preview-registry-store.d.ts +14 -0
  134. package/dist/cloudflare/preview-registry-store.d.ts.map +1 -0
  135. package/dist/cloudflare/preview-registry-types.d.ts +103 -0
  136. package/dist/cloudflare/preview-registry-types.d.ts.map +1 -0
  137. package/dist/cloudflare/preview-registry.d.ts +42 -0
  138. package/dist/cloudflare/preview-registry.d.ts.map +1 -0
  139. package/dist/cloudflare/registry-schema.d.ts +253 -0
  140. package/dist/cloudflare/registry-schema.d.ts.map +1 -0
  141. package/dist/cloudflare/tokens.d.ts +18 -0
  142. package/dist/cloudflare/tokens.d.ts.map +1 -0
  143. package/dist/cloudflare/types.d.ts +122 -5
  144. package/dist/cloudflare/types.d.ts.map +1 -1
  145. package/dist/cloudflare/usage.d.ts.map +1 -1
  146. package/dist/config/compiler.d.ts +4 -0
  147. package/dist/config/compiler.d.ts.map +1 -1
  148. package/dist/config/framework-providers.d.ts +9 -0
  149. package/dist/config/framework-providers.d.ts.map +1 -0
  150. package/dist/config/index.d.ts +5 -3
  151. package/dist/config/index.d.ts.map +1 -1
  152. package/dist/config/loader.d.ts +1 -0
  153. package/dist/config/loader.d.ts.map +1 -1
  154. package/dist/config/preview-resources.d.ts +77 -0
  155. package/dist/config/preview-resources.d.ts.map +1 -0
  156. package/dist/config/preview.d.ts +31 -0
  157. package/dist/config/preview.d.ts.map +1 -0
  158. package/dist/config/ref.d.ts +0 -22
  159. package/dist/config/ref.d.ts.map +1 -1
  160. package/dist/config/resolve.d.ts +1 -0
  161. package/dist/config/resolve.d.ts.map +1 -1
  162. package/dist/config/resource-resolution.d.ts +60 -0
  163. package/dist/config/resource-resolution.d.ts.map +1 -0
  164. package/dist/config/schema-bindings.d.ts +693 -0
  165. package/dist/config/schema-bindings.d.ts.map +1 -0
  166. package/dist/config/schema-build.d.ts +67 -0
  167. package/dist/config/schema-build.d.ts.map +1 -0
  168. package/dist/config/schema-env.d.ts +1341 -0
  169. package/dist/config/schema-env.d.ts.map +1 -0
  170. package/dist/config/schema-normalization.d.ts +64 -0
  171. package/dist/config/schema-normalization.d.ts.map +1 -0
  172. package/dist/config/schema-runtime.d.ts +230 -0
  173. package/dist/config/schema-runtime.d.ts.map +1 -0
  174. package/dist/config/schema.d.ts +640 -3669
  175. package/dist/config/schema.d.ts.map +1 -1
  176. package/dist/config-entry.d.ts +5 -0
  177. package/dist/config-entry.d.ts.map +1 -0
  178. package/dist/config-fjwke42y.js +59 -0
  179. package/dist/config-hwdqjse7.js +59 -0
  180. package/dist/config-pxvewrhv.js +59 -0
  181. package/dist/config-q0g5qdga.js +59 -0
  182. package/dist/decorators/durable-object.d.ts.map +1 -1
  183. package/dist/deploy-7nmzc9r8.js +609 -0
  184. package/dist/deploy-csfhdr64.js +691 -0
  185. package/dist/deploy-ex4g5avz.js +621 -0
  186. package/dist/deploy-jnb0bhka.js +609 -0
  187. package/dist/deploy-tp0g6qdp.js +609 -0
  188. package/dist/deploy-ykpcjkc2.js +690 -0
  189. package/dist/{dev-c1xc1gq9.js → dev-2pd33m28.js} +392 -348
  190. package/dist/dev-7ef5e2j1.js +2409 -0
  191. package/dist/dev-8nssqatr.js +2409 -0
  192. package/dist/dev-grznx8fn.js +2409 -0
  193. package/dist/dev-server/d1-migrations.d.ts +14 -0
  194. package/dist/dev-server/d1-migrations.d.ts.map +1 -0
  195. package/dist/dev-server/gateway-script.d.ts +8 -0
  196. package/dist/dev-server/gateway-script.d.ts.map +1 -0
  197. package/dist/dev-server/runtime-stdio.d.ts.map +1 -1
  198. package/dist/dev-server/server.d.ts.map +1 -1
  199. package/dist/dev-server/vite-process.d.ts +14 -0
  200. package/dist/dev-server/vite-process.d.ts.map +1 -0
  201. package/dist/dev-server/vite-utils.d.ts +1 -1
  202. package/dist/dev-server/vite-utils.d.ts.map +1 -1
  203. package/dist/dev-server/worker-source-watcher.d.ts +11 -0
  204. package/dist/dev-server/worker-source-watcher.d.ts.map +1 -0
  205. package/dist/dev-server/worker-surface-paths.d.ts +6 -0
  206. package/dist/dev-server/worker-surface-paths.d.ts.map +1 -0
  207. package/dist/{doctor-z4ffybce.js → doctor-04ammrrh.js} +67 -31
  208. package/dist/doctor-fmjj65mc.js +245 -0
  209. package/dist/doctor-fzkznce1.js +245 -0
  210. package/dist/doctor-sa5xv1bz.js +245 -0
  211. package/dist/index-091sh1ma.js +1229 -0
  212. package/dist/index-0apbm26n.js +788 -0
  213. package/dist/index-0eqksag4.js +418 -0
  214. package/dist/{index-dr6sbp8d.js → index-0kfzdywd.js} +15 -2
  215. package/dist/index-0w826dsr.js +379 -0
  216. package/dist/{index-rfhx0yd5.js → index-11m5a8wd.js} +110 -32
  217. package/dist/{index-xxwbb2nt.js → index-1sp39f2f.js} +114 -58
  218. package/dist/index-2jnrqbny.js +1301 -0
  219. package/dist/index-2pb7b9mw.js +378 -0
  220. package/dist/{index-0kzg8wed.js → index-2x53aqjm.js} +1071 -890
  221. package/dist/index-3ke5d2vn.js +1229 -0
  222. package/dist/index-43dq8yx8.js +788 -0
  223. package/dist/index-4rrttqj5.js +378 -0
  224. package/dist/index-4v9bc2pc.js +1367 -0
  225. package/dist/index-61jsjnsv.js +280 -0
  226. package/dist/index-6jef5emv.js +176 -0
  227. package/dist/index-6psz1h4c.js +788 -0
  228. package/dist/index-72mve6vh.js +168 -0
  229. package/dist/{index-zbvmtcn2.js → index-74198nxd.js} +179 -77
  230. package/dist/index-7g8zyws4.js +192 -0
  231. package/dist/index-7kcxjhta.js +456 -0
  232. package/dist/index-7v583xan.js +418 -0
  233. package/dist/index-7x0ybbtx.js +133 -0
  234. package/dist/index-816krz9p.js +52 -0
  235. package/dist/index-82f1z98k.js +41 -0
  236. package/dist/index-8t5nb4qx.js +133 -0
  237. package/dist/index-9az6s7ad.js +52 -0
  238. package/dist/{index-59df49vn.js → index-9ba1etyz.js} +29 -51
  239. package/dist/{index-001mw014.js → index-9fbtk7gv.js} +134 -248
  240. package/dist/index-9n6djthj.js +490 -0
  241. package/dist/index-aabgympv.js +39 -0
  242. package/dist/index-b8m6883k.js +74 -0
  243. package/dist/{index-5yxg30va.js → index-cgbvmse6.js} +15 -6
  244. package/dist/index-d8etnfef.js +1229 -0
  245. package/dist/index-e9yw4d6y.js +133 -0
  246. package/dist/index-epw1jxz5.js +1204 -0
  247. package/dist/index-f85s8gj3.js +2649 -0
  248. package/dist/index-fe2ngvh7.js +1229 -0
  249. package/dist/index-fvsadj32.js +192 -0
  250. package/dist/index-gs4y9gdf.js +456 -0
  251. package/dist/{index-fef08w43.js → index-h18pxvzs.js} +7 -6
  252. package/dist/index-hfj1a2c4.js +2649 -0
  253. package/dist/{index-8gtqgb3q.js → index-hjy8ctpc.js} +14 -92
  254. package/dist/index-htzf0py1.js +1204 -0
  255. package/dist/index-j185x270.js +897 -0
  256. package/dist/index-jb75kwa4.js +519 -0
  257. package/dist/index-jwd8pcb2.js +897 -0
  258. package/dist/index-k29yjhv0.js +52 -0
  259. package/dist/index-k6vq6kkt.js +456 -0
  260. package/dist/{index-vky23txa.js → index-m3fmw6mx.js} +2 -2
  261. package/dist/index-maxpsfk8.js +402 -0
  262. package/dist/index-mbdmrner.js +402 -0
  263. package/dist/index-mea5bc45.js +418 -0
  264. package/dist/index-mqekt778.js +185 -0
  265. package/dist/index-na3mnm1k.js +74 -0
  266. package/dist/index-p03n4qet.js +1367 -0
  267. package/dist/index-p296ban8.js +191 -0
  268. package/dist/index-pnbs1b8k.js +280 -0
  269. package/dist/index-q4kaz181.js +1207 -0
  270. package/dist/index-ry131z23.js +378 -0
  271. package/dist/index-sgb7c8nm.js +402 -0
  272. package/dist/index-sqrksgb2.js +133 -0
  273. package/dist/index-stgn34cr.js +148 -0
  274. package/dist/{index-v8vvsn9x.js → index-t08te69w.js} +1 -18
  275. package/dist/index-thna1tkd.js +280 -0
  276. package/dist/index-v5nmqthy.js +74 -0
  277. package/dist/{index-n932ytmq.js → index-vt4yxkmf.js} +2 -2
  278. package/dist/index-wyq6c6yj.js +402 -0
  279. package/dist/index-wztc9stx.js +418 -0
  280. package/dist/index-x9cwdxw5.js +456 -0
  281. package/dist/index-xk9djfjp.js +519 -0
  282. package/dist/index-yc0gcchc.js +418 -0
  283. package/dist/index-yqbxjysa.js +897 -0
  284. package/dist/index-yzddwp02.js +788 -0
  285. package/dist/index-zfhq6s96.js +74 -0
  286. package/dist/index-zt22fe2j.js +54 -0
  287. package/dist/index-zyt5byt6.js +2649 -0
  288. package/dist/index.d.ts +2 -2
  289. package/dist/index.d.ts.map +1 -1
  290. package/dist/{init-na2atvz2.js → init-r4hnxan3.js} +24 -17
  291. package/dist/login-2hnz4m4n.js +77 -0
  292. package/dist/login-5bsxxpvc.js +77 -0
  293. package/dist/login-6tzvczw2.js +77 -0
  294. package/dist/login-bhaw72zc.js +77 -0
  295. package/dist/login-x8tgckqm.js +77 -0
  296. package/dist/previews-3rn8mz2c.js +1168 -0
  297. package/dist/previews-d487qde5.js +1200 -0
  298. package/dist/previews-gm3z0syj.js +1168 -0
  299. package/dist/previews-j9ymq4ys.js +1169 -0
  300. package/dist/previews-q031mx34.js +1168 -0
  301. package/dist/productions-120xg0aq.js +505 -0
  302. package/dist/productions-5ev5qweg.js +505 -0
  303. package/dist/productions-me3tdvr9.js +505 -0
  304. package/dist/productions-p5rbgp2f.js +505 -0
  305. package/dist/productions-x9p0pym1.js +505 -0
  306. package/dist/runtime/context-events.d.ts +13 -0
  307. package/dist/runtime/context-events.d.ts.map +1 -0
  308. package/dist/runtime/context-types.d.ts +82 -0
  309. package/dist/runtime/context-types.d.ts.map +1 -0
  310. package/dist/runtime/context.d.ts +6 -267
  311. package/dist/runtime/context.d.ts.map +1 -1
  312. package/dist/runtime/exports.d.ts +3 -3
  313. package/dist/runtime/index.d.ts +1 -1
  314. package/dist/runtime/index.d.ts.map +1 -1
  315. package/dist/runtime/middleware.d.ts +8 -38
  316. package/dist/runtime/middleware.d.ts.map +1 -1
  317. package/dist/src/browser.js +23 -14
  318. package/dist/src/cli/index.js +3 -1
  319. package/dist/src/cloudflare/index.js +49 -2
  320. package/dist/src/config-entry.js +14 -0
  321. package/dist/src/index.js +33 -20
  322. package/dist/src/runtime/index.js +3 -9
  323. package/dist/src/sveltekit/index.js +10 -7
  324. package/dist/src/test/index.js +16 -18
  325. package/dist/src/vite/index.js +7 -4
  326. package/dist/sveltekit/platform.d.ts +1 -1
  327. package/dist/sveltekit/platform.d.ts.map +1 -1
  328. package/dist/test/cf.d.ts +10 -10
  329. package/dist/test/email.d.ts.map +1 -1
  330. package/dist/test/index.d.ts +1 -6
  331. package/dist/test/index.d.ts.map +1 -1
  332. package/dist/test/queue.d.ts.map +1 -1
  333. package/dist/test/remote-ai.d.ts.map +1 -1
  334. package/dist/test/remote-cloudflare.d.ts +13 -0
  335. package/dist/test/remote-cloudflare.d.ts.map +1 -0
  336. package/dist/test/remote-vectorize.d.ts.map +1 -1
  337. package/dist/test/resolve-service-bindings.d.ts.map +1 -1
  338. package/dist/test/scheduled.d.ts.map +1 -1
  339. package/dist/test/should-skip.d.ts +0 -18
  340. package/dist/test/should-skip.d.ts.map +1 -1
  341. package/dist/test/simple-context-durable-objects.d.ts +6 -0
  342. package/dist/test/simple-context-durable-objects.d.ts.map +1 -0
  343. package/dist/test/simple-context-gateway-script.d.ts +2 -0
  344. package/dist/test/simple-context-gateway-script.d.ts.map +1 -0
  345. package/dist/test/simple-context-paths.d.ts +40 -0
  346. package/dist/test/simple-context-paths.d.ts.map +1 -0
  347. package/dist/test/simple-context.d.ts +1 -23
  348. package/dist/test/simple-context.d.ts.map +1 -1
  349. package/dist/test/tail.d.ts.map +1 -1
  350. package/dist/test/worker.d.ts.map +1 -1
  351. package/dist/token-kedhcret.js +419 -0
  352. package/dist/token-m8jmnjwk.js +419 -0
  353. package/dist/{types-sffr9681.js → types-0sqwkp7x.js} +244 -139
  354. package/dist/types-1gwr2ex6.js +572 -0
  355. package/dist/types-6e5yx6km.js +572 -0
  356. package/dist/types-p0gckpn6.js +572 -0
  357. package/dist/utils/send-email.d.ts.map +1 -1
  358. package/dist/vite/config-file.d.ts.map +1 -1
  359. package/dist/vite/plugin.d.ts.map +1 -1
  360. package/dist/worker-0srh2jfr.js +513 -0
  361. package/dist/worker-4xrfd10a.js +513 -0
  362. package/dist/worker-entry/composed-worker.d.ts +0 -7
  363. package/dist/worker-entry/composed-worker.d.ts.map +1 -1
  364. package/dist/worker-entry/surface-paths.d.ts +15 -0
  365. package/dist/worker-entry/surface-paths.d.ts.map +1 -0
  366. package/dist/worker-qtam8grz.js +513 -0
  367. package/dist/worker-qzm0b7br.js +513 -0
  368. package/dist/worker-y9ha6g44.js +513 -0
  369. package/package.json +17 -10
  370. package/R2.md +0 -200
  371. package/dist/account-8psavtg6.js +0 -420
  372. package/dist/build-n639efmn.js +0 -101
  373. package/dist/deploy-zvnq6xh7.js +0 -117
  374. package/dist/index-2q3pmzrx.js +0 -90
  375. package/dist/index-f4q0jbnj.js +0 -195
  376. package/dist/index-n7rs26ft.js +0 -77
  377. package/dist/index-tfyxa77h.js +0 -850
  378. package/dist/index-wyf3s77s.js +0 -343
  379. package/dist/test/multi-worker-context.d.ts +0 -114
  380. package/dist/test/multi-worker-context.d.ts.map +0 -1
@@ -1,850 +0,0 @@
1
- // src/cloudflare/auth.ts
2
- import { homedir } from "node:os";
3
- import { join } from "node:path";
4
- import { readFileSync, existsSync } from "node:fs";
5
- import { execSync } from "node:child_process";
6
- var WRANGLER_CONFIG_FILE = "config/default.toml";
7
- var EXPIRY_BUFFER_MS = 5 * 60 * 1000;
8
- var cachedToken = null;
9
- var cacheExpiresAt = 0;
10
- function invalidateToken() {
11
- cachedToken = null;
12
- cacheExpiresAt = 0;
13
- }
14
- function getWranglerConfigPaths() {
15
- const paths = [];
16
- if (process.platform === "win32" && process.env.APPDATA) {
17
- paths.push(join(process.env.APPDATA, "xdg.config", ".wrangler", WRANGLER_CONFIG_FILE));
18
- }
19
- if (process.env.XDG_CONFIG_HOME) {
20
- paths.push(join(process.env.XDG_CONFIG_HOME, ".wrangler", WRANGLER_CONFIG_FILE));
21
- }
22
- paths.push(join(homedir(), ".wrangler", WRANGLER_CONFIG_FILE));
23
- return paths;
24
- }
25
- function getWranglerConfigPath() {
26
- const paths = getWranglerConfigPaths();
27
- for (const path of paths) {
28
- if (existsSync(path)) {
29
- return path;
30
- }
31
- }
32
- return null;
33
- }
34
- function hasWranglerConfig() {
35
- return getWranglerConfigPath() !== null;
36
- }
37
- function parseSimpleToml(content) {
38
- const result = {};
39
- const lines = content.split(`
40
- `);
41
- for (const line of lines) {
42
- const trimmed = line.trim();
43
- if (trimmed.startsWith("#") || trimmed === "")
44
- continue;
45
- if (trimmed.startsWith("["))
46
- continue;
47
- const match = trimmed.match(/^(\w+)\s*=\s*["'](.*)["']$/);
48
- if (match) {
49
- result[match[1]] = match[2];
50
- }
51
- }
52
- return result;
53
- }
54
- function readWranglerConfig() {
55
- const configPath = getWranglerConfigPath();
56
- if (!configPath || !existsSync(configPath)) {
57
- return null;
58
- }
59
- try {
60
- const content = readFileSync(configPath, "utf-8");
61
- const config = parseSimpleToml(content);
62
- const token = config.oauth_token;
63
- const expirationTime = config.expiration_time;
64
- if (token && expirationTime) {
65
- return {
66
- token,
67
- expiresAt: new Date(expirationTime)
68
- };
69
- }
70
- return null;
71
- } catch {
72
- return null;
73
- }
74
- }
75
- async function getWranglerAuth() {
76
- const config = readWranglerConfig();
77
- if (!config)
78
- return null;
79
- const configPath = getWranglerConfigPath();
80
- if (!configPath)
81
- return null;
82
- try {
83
- const content = readFileSync(configPath, "utf-8");
84
- const parsed = parseSimpleToml(content);
85
- return {
86
- oauthToken: parsed.oauth_token,
87
- refreshToken: parsed.refresh_token,
88
- expiresAt: config.expiresAt
89
- };
90
- } catch {
91
- return null;
92
- }
93
- }
94
- function refreshWranglerToken() {
95
- try {
96
- const result = execSync("bunx wrangler auth token", {
97
- encoding: "utf-8",
98
- stdio: ["pipe", "pipe", "pipe"],
99
- timeout: 15000
100
- });
101
- const lines = result.trim().split(/\r?\n/).filter((l) => l.trim().length > 0);
102
- const token = lines[lines.length - 1]?.trim();
103
- if (token && token.length >= 20 && !token.includes("wrangler") && !token.includes("⛅")) {
104
- return token;
105
- }
106
- return null;
107
- } catch {
108
- return null;
109
- }
110
- }
111
- async function getApiToken(forceRefresh = false) {
112
- if (!forceRefresh && cachedToken && Date.now() < cacheExpiresAt) {
113
- return cachedToken;
114
- }
115
- const envToken = process.env.CLOUDFLARE_API_TOKEN;
116
- if (envToken) {
117
- return envToken;
118
- }
119
- if (!forceRefresh) {
120
- const config = readWranglerConfig();
121
- if (config) {
122
- const now = Date.now();
123
- const expiresAt = config.expiresAt.getTime();
124
- if (now < expiresAt - EXPIRY_BUFFER_MS) {
125
- cachedToken = config.token;
126
- cacheExpiresAt = expiresAt - EXPIRY_BUFFER_MS;
127
- return config.token;
128
- }
129
- }
130
- }
131
- const refreshedToken = refreshWranglerToken();
132
- if (refreshedToken) {
133
- cachedToken = refreshedToken;
134
- cacheExpiresAt = Date.now() + EXPIRY_BUFFER_MS;
135
- return refreshedToken;
136
- }
137
- return null;
138
- }
139
- async function isAuthenticated() {
140
- const token = await getApiToken();
141
- return token !== null;
142
- }
143
-
144
- // src/cloudflare/api.ts
145
- var API_BASE = "https://api.cloudflare.com/client/v4";
146
- var DEFAULT_TIMEOUT = 1e4;
147
- var hasRetriedWithFreshToken = false;
148
- async function fetchWithTimeout(url, init, timeoutMs) {
149
- const controller = new AbortController;
150
- const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
151
- try {
152
- const response = await Promise.race([
153
- fetch(url, { ...init, signal: controller.signal }),
154
- new Promise((_, reject) => setTimeout(() => reject(new Error(`Request timed out after ${timeoutMs}ms`)), timeoutMs))
155
- ]);
156
- return response;
157
- } finally {
158
- clearTimeout(timeoutId);
159
- }
160
- }
161
-
162
- class CloudflareAPIError extends Error {
163
- code;
164
- errors;
165
- constructor(message, code, errors) {
166
- super(message);
167
- this.code = code;
168
- this.errors = errors;
169
- this.name = "CloudflareAPIError";
170
- }
171
- }
172
-
173
- class AuthenticationError extends Error {
174
- constructor(message = "Not authenticated. Run: bunx wrangler login") {
175
- super(message);
176
- this.name = "AuthenticationError";
177
- }
178
- }
179
- async function createHeaders(options, forceRefresh = false) {
180
- const token = options?.token ?? await getApiToken(forceRefresh);
181
- if (!token) {
182
- throw new AuthenticationError;
183
- }
184
- return new Headers({
185
- Authorization: `Bearer ${token}`,
186
- "Content-Type": "application/json"
187
- });
188
- }
189
- function isAuthError(response, data) {
190
- if (response.status === 401)
191
- return true;
192
- if (!data.success && data.errors?.some((e) => e.code === 1e4 || e.message?.toLowerCase().includes("authentication") || e.message?.toLowerCase().includes("token"))) {
193
- return true;
194
- }
195
- return false;
196
- }
197
- async function apiGet(path, options) {
198
- const makeRequest = async (forceRefresh) => {
199
- const headers = await createHeaders(options, forceRefresh);
200
- const url = `${API_BASE}${path}`;
201
- const timeout = options?.timeout ?? DEFAULT_TIMEOUT;
202
- const response2 = await fetchWithTimeout(url, {
203
- method: "GET",
204
- headers
205
- }, timeout);
206
- const data2 = await response2.json();
207
- return { response: response2, data: data2 };
208
- };
209
- let { response, data } = await makeRequest(false);
210
- if (isAuthError(response, data) && !hasRetriedWithFreshToken && !options?.token) {
211
- hasRetriedWithFreshToken = true;
212
- invalidateToken();
213
- ({ response, data } = await makeRequest(true));
214
- hasRetriedWithFreshToken = false;
215
- }
216
- if (!data.success) {
217
- throw new CloudflareAPIError(data.errors[0]?.message || "API request failed", response.status, data.errors);
218
- }
219
- return data.result;
220
- }
221
- async function apiPost(path, body, options) {
222
- const headers = await createHeaders(options);
223
- const url = `${API_BASE}${path}`;
224
- const timeout = options?.timeout ?? DEFAULT_TIMEOUT;
225
- const response = await fetchWithTimeout(url, {
226
- method: "POST",
227
- headers,
228
- body: JSON.stringify(body)
229
- }, timeout);
230
- const data = await response.json();
231
- if (!data.success) {
232
- throw new CloudflareAPIError(data.errors[0]?.message || "API request failed", response.status, data.errors);
233
- }
234
- return data.result;
235
- }
236
- async function apiGetAll(path, options) {
237
- const results = [];
238
- let page = 1;
239
- const perPage = 50;
240
- const maxPages = 100;
241
- while (page <= maxPages) {
242
- const separator = path.includes("?") ? "&" : "?";
243
- const pagedPath = `${path}${separator}page=${page}&per_page=${perPage}`;
244
- const headers = await createHeaders(options);
245
- const url = `${API_BASE}${pagedPath}`;
246
- const timeout = options?.timeout ?? DEFAULT_TIMEOUT;
247
- const response = await fetchWithTimeout(url, {
248
- method: "GET",
249
- headers
250
- }, timeout);
251
- const data = await response.json();
252
- if (!data.success) {
253
- throw new CloudflareAPIError(data.errors[0]?.message || "API request failed", response.status, data.errors);
254
- }
255
- results.push(...data.result);
256
- if (!data.result_info) {
257
- break;
258
- }
259
- if (data.result.length === 0) {
260
- break;
261
- }
262
- if (data.result_info.total_count !== undefined) {
263
- if (results.length >= data.result_info.total_count) {
264
- break;
265
- }
266
- }
267
- if (data.result_info.total_pages !== undefined) {
268
- if (page >= data.result_info.total_pages) {
269
- break;
270
- }
271
- }
272
- page++;
273
- }
274
- return results;
275
- }
276
- async function kvGet(accountId, namespaceId, key, options) {
277
- const token = options?.token ?? await getApiToken();
278
- if (!token)
279
- throw new AuthenticationError;
280
- const encodedKey = encodeURIComponent(key);
281
- const url = `${API_BASE}/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodedKey}`;
282
- const timeout = options?.timeout ?? DEFAULT_TIMEOUT;
283
- const response = await fetchWithTimeout(url, {
284
- method: "GET",
285
- headers: { Authorization: `Bearer ${token}` }
286
- }, timeout);
287
- if (response.status === 404) {
288
- return null;
289
- }
290
- if (!response.ok) {
291
- try {
292
- const errorData = await response.json();
293
- throw new CloudflareAPIError(errorData.errors[0]?.message || "KV read failed", response.status, errorData.errors);
294
- } catch {
295
- throw new CloudflareAPIError("KV read failed", response.status, []);
296
- }
297
- }
298
- return response.text();
299
- }
300
- async function kvPut(accountId, namespaceId, key, value, options) {
301
- const token = options?.token ?? await getApiToken();
302
- if (!token)
303
- throw new AuthenticationError;
304
- const encodedKey = encodeURIComponent(key);
305
- const url = `${API_BASE}/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodedKey}`;
306
- const timeout = options?.timeout ?? DEFAULT_TIMEOUT;
307
- const response = await fetchWithTimeout(url, {
308
- method: "PUT",
309
- headers: {
310
- Authorization: `Bearer ${token}`,
311
- "Content-Type": "text/plain"
312
- },
313
- body: value
314
- }, timeout);
315
- if (!response.ok) {
316
- try {
317
- const errorData = await response.json();
318
- throw new CloudflareAPIError(errorData.errors[0]?.message || "KV write failed", response.status, errorData.errors);
319
- } catch {
320
- throw new CloudflareAPIError("KV write failed", response.status, []);
321
- }
322
- }
323
- }
324
-
325
- // src/cloudflare/account.ts
326
- async function getAccounts() {
327
- const accounts = await apiGetAll("/accounts");
328
- return accounts.map((acc) => ({
329
- id: acc.id,
330
- name: acc.name,
331
- type: acc.type,
332
- createdOn: acc.created_on ? new Date(acc.created_on) : undefined
333
- }));
334
- }
335
- async function getPrimaryAccount() {
336
- const accounts = await getAccounts();
337
- return accounts[0] ?? null;
338
- }
339
- async function getAccountById(accountId) {
340
- try {
341
- const account = await apiGet(`/accounts/${accountId}`);
342
- return {
343
- id: account.id,
344
- name: account.name,
345
- type: account.type,
346
- createdOn: account.created_on ? new Date(account.created_on) : undefined
347
- };
348
- } catch {
349
- return null;
350
- }
351
- }
352
- async function listWorkers(accountId, options) {
353
- const scripts = await apiGetAll(`/accounts/${accountId}/workers/scripts`, options);
354
- return scripts.map((s) => ({
355
- name: s.name ?? s.id,
356
- createdOn: new Date(s.created_on),
357
- modifiedOn: new Date(s.modified_on)
358
- }));
359
- }
360
- async function listKVNamespaces(accountId, options) {
361
- const namespaces = await apiGetAll(`/accounts/${accountId}/storage/kv/namespaces`, options);
362
- return namespaces.map((ns) => ({
363
- id: ns.id,
364
- name: ns.title
365
- }));
366
- }
367
- async function listD1Databases(accountId, options) {
368
- const databases = await apiGetAll(`/accounts/${accountId}/d1/database`, options);
369
- return databases.map((db) => ({
370
- id: db.uuid,
371
- name: db.name,
372
- version: db.version,
373
- tableCount: db.num_tables,
374
- sizeBytes: db.file_size
375
- }));
376
- }
377
- async function listR2Buckets(accountId, options) {
378
- const buckets = await apiGetAll(`/accounts/${accountId}/r2/buckets`, options);
379
- return buckets.map((b) => ({
380
- name: b.name,
381
- createdOn: new Date(b.creation_date),
382
- location: b.location
383
- }));
384
- }
385
- async function listVectorizeIndexes(accountId, options) {
386
- try {
387
- const indexes = await apiGetAll(`/accounts/${accountId}/vectorize/v2/indexes`, options);
388
- return indexes.map((idx) => ({
389
- name: idx.name,
390
- dimensions: idx.config.dimensions,
391
- metric: idx.config.metric,
392
- description: idx.description
393
- }));
394
- } catch {
395
- return [];
396
- }
397
- }
398
- async function listAIModels(accountId, options) {
399
- try {
400
- const models = await apiGetAll(`/accounts/${accountId}/ai/models/search`, options);
401
- return models.map((m) => ({
402
- id: m.id,
403
- name: m.name,
404
- task: m.task?.name,
405
- description: m.description
406
- }));
407
- } catch {
408
- return [];
409
- }
410
- }
411
- async function getServiceStatus(accountId, service) {
412
- const timeout = 1e4;
413
- try {
414
- switch (service) {
415
- case "workers": {
416
- const workers = await Promise.race([
417
- listWorkers(accountId),
418
- new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout))
419
- ]);
420
- return {
421
- service,
422
- available: true,
423
- count: workers.length
424
- };
425
- }
426
- case "kv": {
427
- const namespaces = await Promise.race([
428
- listKVNamespaces(accountId),
429
- new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout))
430
- ]);
431
- return {
432
- service,
433
- available: true,
434
- count: namespaces.length
435
- };
436
- }
437
- case "d1": {
438
- const databases = await Promise.race([
439
- listD1Databases(accountId),
440
- new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout))
441
- ]);
442
- return {
443
- service,
444
- available: true,
445
- count: databases.length
446
- };
447
- }
448
- case "r2": {
449
- const buckets = await Promise.race([
450
- listR2Buckets(accountId),
451
- new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout))
452
- ]);
453
- return {
454
- service,
455
- available: true,
456
- count: buckets.length
457
- };
458
- }
459
- case "vectorize": {
460
- const indexes = await Promise.race([
461
- listVectorizeIndexes(accountId),
462
- new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout))
463
- ]);
464
- return {
465
- service,
466
- available: true,
467
- count: indexes.length
468
- };
469
- }
470
- case "ai": {
471
- const models = await Promise.race([
472
- listAIModels(accountId),
473
- new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout))
474
- ]);
475
- return {
476
- service,
477
- available: models.length > 0,
478
- count: models.length
479
- };
480
- }
481
- default:
482
- return {
483
- service,
484
- available: false
485
- };
486
- }
487
- } catch {
488
- return {
489
- service,
490
- available: false
491
- };
492
- }
493
- }
494
- async function getAllServiceStatus(accountId) {
495
- const services = [
496
- "workers",
497
- "kv",
498
- "d1",
499
- "r2",
500
- "vectorize",
501
- "ai"
502
- ];
503
- const statuses = await Promise.all(services.map((s) => getServiceStatus(accountId, s)));
504
- return statuses;
505
- }
506
- async function hasService(accountId, service) {
507
- const status = await getServiceStatus(accountId, service);
508
- return status.available;
509
- }
510
- async function getAccountSummary(accountId) {
511
- const account = await getAccountById(accountId);
512
- if (!account)
513
- return null;
514
- const services = await getAllServiceStatus(accountId);
515
- return {
516
- account,
517
- services
518
- };
519
- }
520
-
521
- // src/cloudflare/preferences.ts
522
- import { homedir as homedir2 } from "node:os";
523
- import { join as join2 } from "node:path";
524
- import { existsSync as existsSync2, readFileSync as readFileSync2, writeFileSync, mkdirSync } from "node:fs";
525
- var DEVFLARE_KV_NAMESPACE_TITLE = "devflare-usage";
526
- var GLOBAL_ACCOUNT_KEY = "settings:defaultAccountId";
527
- var LOCAL_CACHE_DIR = ".devflare";
528
- var LOCAL_CACHE_FILE = "preferences.json";
529
- function getLocalPreferencesPath() {
530
- return join2(homedir2(), LOCAL_CACHE_DIR, LOCAL_CACHE_FILE);
531
- }
532
- function readLocalPreferences() {
533
- const path = getLocalPreferencesPath();
534
- if (!existsSync2(path)) {
535
- return {};
536
- }
537
- try {
538
- const content = readFileSync2(path, "utf-8");
539
- return JSON.parse(content);
540
- } catch {
541
- return {};
542
- }
543
- }
544
- function writeLocalPreferences(prefs) {
545
- const path = getLocalPreferencesPath();
546
- const dir = join2(homedir2(), LOCAL_CACHE_DIR);
547
- if (!existsSync2(dir)) {
548
- mkdirSync(dir, { recursive: true });
549
- }
550
- writeFileSync(path, JSON.stringify(prefs, null, "\t"), "utf-8");
551
- }
552
- function findPackageJsonPath(startDir) {
553
- let dir = startDir ?? process.cwd();
554
- while (dir !== join2(dir, "..")) {
555
- const pkgPath = join2(dir, "package.json");
556
- if (existsSync2(pkgPath)) {
557
- return pkgPath;
558
- }
559
- dir = join2(dir, "..");
560
- }
561
- return null;
562
- }
563
- function readPackageJson(path) {
564
- try {
565
- const content = readFileSync2(path, "utf-8");
566
- return JSON.parse(content);
567
- } catch {
568
- return null;
569
- }
570
- }
571
- function writePackageJson(path, pkg) {
572
- writeFileSync(path, JSON.stringify(pkg, null, "\t") + `
573
- `, "utf-8");
574
- }
575
- function getWorkspaceAccountId() {
576
- const pkgPath = findPackageJsonPath();
577
- if (!pkgPath)
578
- return null;
579
- const pkg = readPackageJson(pkgPath);
580
- return pkg?.devflare?.accountId ?? null;
581
- }
582
- function setWorkspaceAccountId(accountId) {
583
- let pkgPath = findPackageJsonPath();
584
- let pkg;
585
- if (pkgPath) {
586
- pkg = readPackageJson(pkgPath) ?? {};
587
- } else {
588
- pkgPath = join2(process.cwd(), "package.json");
589
- pkg = {
590
- name: "workspace",
591
- private: true
592
- };
593
- }
594
- if (!pkg.devflare) {
595
- pkg.devflare = {};
596
- }
597
- pkg.devflare.accountId = accountId;
598
- writePackageJson(pkgPath, pkg);
599
- return pkgPath;
600
- }
601
- async function getOrCreatePreferencesNamespace(accountId) {
602
- const namespaces = await apiGet(`/accounts/${accountId}/storage/kv/namespaces`);
603
- const existing = namespaces.find((ns) => ns.title === DEVFLARE_KV_NAMESPACE_TITLE);
604
- if (existing) {
605
- return existing.id;
606
- }
607
- const created = await apiPost(`/accounts/${accountId}/storage/kv/namespaces`, { title: DEVFLARE_KV_NAMESPACE_TITLE });
608
- return created.id;
609
- }
610
- async function getGlobalDefaultAccountId(fallbackAccountId) {
611
- const local = readLocalPreferences();
612
- if (local.defaultAccountId) {
613
- return local.defaultAccountId;
614
- }
615
- try {
616
- const namespaceId = await getOrCreatePreferencesNamespace(fallbackAccountId);
617
- const value = await kvGet(fallbackAccountId, namespaceId, GLOBAL_ACCOUNT_KEY);
618
- if (value) {
619
- writeLocalPreferences({
620
- ...local,
621
- defaultAccountId: value,
622
- lastUpdated: new Date().toISOString()
623
- });
624
- return value;
625
- }
626
- } catch {}
627
- return null;
628
- }
629
- async function setGlobalDefaultAccountId(accountId, anyAccountId) {
630
- const kvAccountId = anyAccountId ?? accountId;
631
- const local = readLocalPreferences();
632
- writeLocalPreferences({
633
- ...local,
634
- defaultAccountId: accountId,
635
- lastUpdated: new Date().toISOString()
636
- });
637
- try {
638
- const namespaceId = await getOrCreatePreferencesNamespace(kvAccountId);
639
- await kvPut(kvAccountId, namespaceId, GLOBAL_ACCOUNT_KEY, accountId);
640
- } catch {}
641
- }
642
- async function getEffectiveAccountId(primaryAccountId) {
643
- const workspaceId = getWorkspaceAccountId();
644
- if (workspaceId) {
645
- return { accountId: workspaceId, source: "workspace" };
646
- }
647
- const globalId = await getGlobalDefaultAccountId(primaryAccountId);
648
- if (globalId) {
649
- return { accountId: globalId, source: "global" };
650
- }
651
- return { accountId: primaryAccountId, source: "primary" };
652
- }
653
- async function clearGlobalDefaultAccountId(anyAccountId) {
654
- const local = readLocalPreferences();
655
- delete local.defaultAccountId;
656
- local.lastUpdated = new Date().toISOString();
657
- writeLocalPreferences(local);
658
- try {
659
- const namespaceId = await getOrCreatePreferencesNamespace(anyAccountId);
660
- await kvPut(anyAccountId, namespaceId, GLOBAL_ACCOUNT_KEY, "");
661
- } catch {}
662
- }
663
-
664
- // src/cloudflare/usage.ts
665
- var DEVFLARE_KV_NAMESPACE_TITLE2 = "devflare-usage";
666
- var USAGE_KEY_PREFIX = "usage:";
667
- var LIMITS_KEY = "limits";
668
- var DEFAULT_LIMITS = {
669
- aiTokensPerDay: 1e4,
670
- aiRequestsPerDay: 100,
671
- vectorizeOpsPerDay: 1000,
672
- enabled: true
673
- };
674
- async function getOrCreateUsageNamespace(accountId) {
675
- const namespaces = await apiGet(`/accounts/${accountId}/storage/kv/namespaces`);
676
- const existing = namespaces.find((ns) => ns.title === DEVFLARE_KV_NAMESPACE_TITLE2);
677
- if (existing) {
678
- return existing.id;
679
- }
680
- const created = await apiPost(`/accounts/${accountId}/storage/kv/namespaces`, { title: DEVFLARE_KV_NAMESPACE_TITLE2 });
681
- return created.id;
682
- }
683
- function getTodayDate() {
684
- return new Date().toISOString().split("T")[0];
685
- }
686
- function buildUsageKey(service, date) {
687
- return `${USAGE_KEY_PREFIX}${service}:${date}`;
688
- }
689
- async function getUsage(accountId, service, date) {
690
- const targetDate = date ?? getTodayDate();
691
- const namespaceId = await getOrCreateUsageNamespace(accountId);
692
- const key = buildUsageKey(service, targetDate);
693
- const value = await kvGet(accountId, namespaceId, key);
694
- if (value === null) {
695
- return null;
696
- }
697
- try {
698
- return JSON.parse(value);
699
- } catch {
700
- return null;
701
- }
702
- }
703
- async function recordUsage(accountId, service, count = 1) {
704
- const today = getTodayDate();
705
- const namespaceId = await getOrCreateUsageNamespace(accountId);
706
- const key = buildUsageKey(service, today);
707
- const existing = await getUsage(accountId, service, today);
708
- const currentCount = existing?.count ?? 0;
709
- const record = {
710
- service,
711
- date: today,
712
- count: currentCount + count,
713
- updatedAt: new Date().toISOString()
714
- };
715
- await kvPut(accountId, namespaceId, key, JSON.stringify(record));
716
- return record;
717
- }
718
- async function resetUsage(accountId, service) {
719
- const today = getTodayDate();
720
- const namespaceId = await getOrCreateUsageNamespace(accountId);
721
- const key = buildUsageKey(service, today);
722
- const record = {
723
- service,
724
- date: today,
725
- count: 0,
726
- updatedAt: new Date().toISOString()
727
- };
728
- await kvPut(accountId, namespaceId, key, JSON.stringify(record));
729
- }
730
- async function getLimits(accountId) {
731
- const namespaceId = await getOrCreateUsageNamespace(accountId);
732
- const value = await kvGet(accountId, namespaceId, LIMITS_KEY);
733
- if (value === null) {
734
- return DEFAULT_LIMITS;
735
- }
736
- try {
737
- return { ...DEFAULT_LIMITS, ...JSON.parse(value) };
738
- } catch {
739
- return DEFAULT_LIMITS;
740
- }
741
- }
742
- async function setLimits(accountId, limits) {
743
- const namespaceId = await getOrCreateUsageNamespace(accountId);
744
- const current = await getLimits(accountId);
745
- const updated = {
746
- ...current,
747
- ...limits
748
- };
749
- await kvPut(accountId, namespaceId, LIMITS_KEY, JSON.stringify(updated));
750
- return updated;
751
- }
752
- async function setLimitsEnabled(accountId, enabled) {
753
- return setLimits(accountId, { enabled });
754
- }
755
- async function isWithinLimits(accountId, service) {
756
- const limits = await getLimits(accountId);
757
- if (!limits.enabled) {
758
- return true;
759
- }
760
- const usage = await getUsage(accountId, service);
761
- const currentCount = usage?.count ?? 0;
762
- switch (service) {
763
- case "ai":
764
- if (limits.aiRequestsPerDay && currentCount >= limits.aiRequestsPerDay) {
765
- return false;
766
- }
767
- return true;
768
- case "vectorize":
769
- if (limits.vectorizeOpsPerDay && currentCount >= limits.vectorizeOpsPerDay) {
770
- return false;
771
- }
772
- return true;
773
- default:
774
- return true;
775
- }
776
- }
777
- async function getUsageSummary(accountId, service) {
778
- const limits = await getLimits(accountId);
779
- const usage = await getUsage(accountId, service);
780
- const currentCount = usage?.count ?? 0;
781
- let limit;
782
- switch (service) {
783
- case "ai":
784
- limit = limits.aiRequestsPerDay;
785
- break;
786
- case "vectorize":
787
- limit = limits.vectorizeOpsPerDay;
788
- break;
789
- }
790
- const withinLimit = limit === undefined || currentCount < limit;
791
- const percentUsed = limit ? currentCount / limit * 100 : undefined;
792
- return {
793
- service,
794
- today: currentCount,
795
- limit,
796
- withinLimit,
797
- percentUsed
798
- };
799
- }
800
- async function getAllUsageSummaries(accountId) {
801
- const trackedServices = ["ai", "vectorize"];
802
- return Promise.all(trackedServices.map((s) => getUsageSummary(accountId, s)));
803
- }
804
- async function canProceedWithTest(accountId, service) {
805
- const limits = await getLimits(accountId);
806
- if (!limits.enabled) {
807
- return { allowed: true };
808
- }
809
- const withinLimits = await isWithinLimits(accountId, service);
810
- if (!withinLimits) {
811
- const summary = await getUsageSummary(accountId, service);
812
- return {
813
- allowed: false,
814
- reason: `Daily limit exceeded for ${service}: ${summary.today}/${summary.limit} (${summary.percentUsed?.toFixed(1)}%)`
815
- };
816
- }
817
- return { allowed: true };
818
- }
819
- async function recordTestUsage(accountId, service, count = 1) {
820
- await recordUsage(accountId, service, count);
821
- }
822
- async function shouldSkip(service) {
823
- try {
824
- const isAuth = await isAuthenticated();
825
- if (!isAuth) {
826
- console.log(`⏭️ ${service.toUpperCase()} tests skipped: Not authenticated. Run: bunx wrangler login`);
827
- return true;
828
- }
829
- const primary = await getPrimaryAccount();
830
- if (!primary) {
831
- console.log(`⏭️ ${service.toUpperCase()} tests skipped: No Cloudflare account found`);
832
- return true;
833
- }
834
- const { accountId } = await getEffectiveAccountId(primary.id);
835
- try {
836
- const { allowed, reason } = await canProceedWithTest(accountId, service);
837
- if (!allowed) {
838
- console.log(`⏭️ ${service.toUpperCase()} tests skipped: ${reason}`);
839
- return true;
840
- }
841
- } catch {}
842
- return false;
843
- } catch (error) {
844
- const message = error instanceof Error ? error.message : "Unknown error";
845
- console.log(`⏭️ ${service.toUpperCase()} tests skipped: ${message}`);
846
- return true;
847
- }
848
- }
849
-
850
- export { hasWranglerConfig, getWranglerAuth, getApiToken, isAuthenticated, CloudflareAPIError, AuthenticationError, getAccounts, getPrimaryAccount, getAccountById, listWorkers, listKVNamespaces, listD1Databases, listR2Buckets, listVectorizeIndexes, listAIModels, getServiceStatus, getAllServiceStatus, hasService, getAccountSummary, getWorkspaceAccountId, setWorkspaceAccountId, getGlobalDefaultAccountId, setGlobalDefaultAccountId, getEffectiveAccountId, clearGlobalDefaultAccountId, getUsage, recordUsage, resetUsage, getLimits, setLimits, setLimitsEnabled, isWithinLimits, getUsageSummary, getAllUsageSummaries, canProceedWithTest, recordTestUsage, shouldSkip };