@astrogate/sdk 0.0.0-stage → 0.1.0

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 (440) hide show
  1. package/README.md +6 -2
  2. package/THIRD_PARTY_NOTICES.md +17 -0
  3. package/dist/browser.d.ts +85 -0
  4. package/dist/browser.js +16 -0
  5. package/dist/database-binding.d.ts +7 -0
  6. package/dist/database-binding.js +11 -0
  7. package/dist/hosted-session.d.ts +1 -0
  8. package/dist/hosted-session.js +11 -0
  9. package/dist/http.d.ts +24 -0
  10. package/dist/http.js +92 -0
  11. package/dist/inventory.d.ts +7 -0
  12. package/dist/inventory.js +18 -0
  13. package/dist/management-read.d.ts +17 -0
  14. package/dist/management-read.js +104 -0
  15. package/dist/management.d.ts +347 -0
  16. package/dist/management.js +137 -0
  17. package/dist/operator.d.ts +136 -0
  18. package/dist/operator.js +53 -0
  19. package/dist/react.d.ts +16 -0
  20. package/dist/react.js +10 -0
  21. package/dist/server.d.ts +27 -0
  22. package/dist/server.js +30 -0
  23. package/dist/services.d.ts +7 -0
  24. package/dist/services.js +53 -0
  25. package/dist/storage.d.ts +68 -0
  26. package/dist/storage.js +30 -0
  27. package/dist/tasks.d.ts +42 -0
  28. package/dist/tasks.js +24 -0
  29. package/dist/wire.gen.d.ts +7107 -0
  30. package/dist/wire.gen.js +5 -0
  31. package/native-provenance.json +15 -0
  32. package/node_modules/@instantdb/admin/LICENSE +202 -0
  33. package/node_modules/@instantdb/admin/dist/esm/index.d.ts +607 -0
  34. package/node_modules/@instantdb/admin/dist/esm/index.js +1036 -0
  35. package/node_modules/@instantdb/admin/dist/esm/polyfill.d.ts +16 -0
  36. package/node_modules/@instantdb/admin/dist/esm/polyfill.js +21 -0
  37. package/node_modules/@instantdb/admin/dist/esm/subscribe.d.ts +38 -0
  38. package/node_modules/@instantdb/admin/dist/esm/subscribe.js +301 -0
  39. package/node_modules/@instantdb/admin/dist/esm/version.d.ts +2 -0
  40. package/node_modules/@instantdb/admin/dist/esm/version.js +2 -0
  41. package/node_modules/@instantdb/admin/package.json +23 -0
  42. package/node_modules/@instantdb/core/LICENSE +202 -0
  43. package/node_modules/@instantdb/core/dist/esm/Connection.d.ts +92 -0
  44. package/node_modules/@instantdb/core/dist/esm/Connection.js +188 -0
  45. package/node_modules/@instantdb/core/dist/esm/InMemoryStorage.d.ts +10 -0
  46. package/node_modules/@instantdb/core/dist/esm/InMemoryStorage.js +25 -0
  47. package/node_modules/@instantdb/core/dist/esm/IndexedDBStorage.d.ts +17 -0
  48. package/node_modules/@instantdb/core/dist/esm/IndexedDBStorage.js +309 -0
  49. package/node_modules/@instantdb/core/dist/esm/InstantError.d.ts +6 -0
  50. package/node_modules/@instantdb/core/dist/esm/InstantError.js +23 -0
  51. package/node_modules/@instantdb/core/dist/esm/Reactor.d.ts +430 -0
  52. package/node_modules/@instantdb/core/dist/esm/Reactor.js +2369 -0
  53. package/node_modules/@instantdb/core/dist/esm/StorageAPI.d.ts +39 -0
  54. package/node_modules/@instantdb/core/dist/esm/StorageAPI.js +65 -0
  55. package/node_modules/@instantdb/core/dist/esm/Stream.d.ts +120 -0
  56. package/node_modules/@instantdb/core/dist/esm/Stream.js +811 -0
  57. package/node_modules/@instantdb/core/dist/esm/SyncTable.d.ts +144 -0
  58. package/node_modules/@instantdb/core/dist/esm/SyncTable.js +523 -0
  59. package/node_modules/@instantdb/core/dist/esm/WindowNetworkListener.d.ts +4 -0
  60. package/node_modules/@instantdb/core/dist/esm/WindowNetworkListener.js +19 -0
  61. package/node_modules/@instantdb/core/dist/esm/attrTypes.d.ts +25 -0
  62. package/node_modules/@instantdb/core/dist/esm/attrTypes.js +1 -0
  63. package/node_modules/@instantdb/core/dist/esm/authAPI.d.ts +64 -0
  64. package/node_modules/@instantdb/core/dist/esm/authAPI.js +103 -0
  65. package/node_modules/@instantdb/core/dist/esm/clientTypes.d.ts +40 -0
  66. package/node_modules/@instantdb/core/dist/esm/clientTypes.js +1 -0
  67. package/node_modules/@instantdb/core/dist/esm/coreTypes.d.ts +33 -0
  68. package/node_modules/@instantdb/core/dist/esm/coreTypes.js +1 -0
  69. package/node_modules/@instantdb/core/dist/esm/createRouteHandler.d.ts +7 -0
  70. package/node_modules/@instantdb/core/dist/esm/createRouteHandler.js +51 -0
  71. package/node_modules/@instantdb/core/dist/esm/datalog.d.ts +7 -0
  72. package/node_modules/@instantdb/core/dist/esm/datalog.js +99 -0
  73. package/node_modules/@instantdb/core/dist/esm/devtool.d.ts +3 -0
  74. package/node_modules/@instantdb/core/dist/esm/devtool.js +144 -0
  75. package/node_modules/@instantdb/core/dist/esm/framework.d.ts +80 -0
  76. package/node_modules/@instantdb/core/dist/esm/framework.js +243 -0
  77. package/node_modules/@instantdb/core/dist/esm/helperTypes.d.ts +75 -0
  78. package/node_modules/@instantdb/core/dist/esm/helperTypes.js +1 -0
  79. package/node_modules/@instantdb/core/dist/esm/index.d.ts +597 -0
  80. package/node_modules/@instantdb/core/dist/esm/index.js +649 -0
  81. package/node_modules/@instantdb/core/dist/esm/infiniteQuery.d.ts +25 -0
  82. package/node_modules/@instantdb/core/dist/esm/infiniteQuery.js +430 -0
  83. package/node_modules/@instantdb/core/dist/esm/instaml.d.ts +17 -0
  84. package/node_modules/@instantdb/core/dist/esm/instaml.js +576 -0
  85. package/node_modules/@instantdb/core/dist/esm/instaql.d.ts +12 -0
  86. package/node_modules/@instantdb/core/dist/esm/instaql.js +632 -0
  87. package/node_modules/@instantdb/core/dist/esm/instatx.d.ts +127 -0
  88. package/node_modules/@instantdb/core/dist/esm/instatx.js +93 -0
  89. package/node_modules/@instantdb/core/dist/esm/model/instaqlResult.d.ts +7 -0
  90. package/node_modules/@instantdb/core/dist/esm/model/instaqlResult.js +24 -0
  91. package/node_modules/@instantdb/core/dist/esm/parseSchemaFromJSON.d.ts +2 -0
  92. package/node_modules/@instantdb/core/dist/esm/parseSchemaFromJSON.js +142 -0
  93. package/node_modules/@instantdb/core/dist/esm/presence.d.ts +50 -0
  94. package/node_modules/@instantdb/core/dist/esm/presence.js +50 -0
  95. package/node_modules/@instantdb/core/dist/esm/presenceTypes.d.ts +9 -0
  96. package/node_modules/@instantdb/core/dist/esm/presenceTypes.js +3 -0
  97. package/node_modules/@instantdb/core/dist/esm/queryTypes.d.ts +271 -0
  98. package/node_modules/@instantdb/core/dist/esm/queryTypes.js +3 -0
  99. package/node_modules/@instantdb/core/dist/esm/queryValidation.d.ts +5 -0
  100. package/node_modules/@instantdb/core/dist/esm/queryValidation.js +287 -0
  101. package/node_modules/@instantdb/core/dist/esm/reactorTypes.d.ts +29 -0
  102. package/node_modules/@instantdb/core/dist/esm/reactorTypes.js +1 -0
  103. package/node_modules/@instantdb/core/dist/esm/routeHandlerProtocol.d.ts +19 -0
  104. package/node_modules/@instantdb/core/dist/esm/routeHandlerProtocol.js +1 -0
  105. package/node_modules/@instantdb/core/dist/esm/rulesTypes.d.ts +85 -0
  106. package/node_modules/@instantdb/core/dist/esm/rulesTypes.js +1 -0
  107. package/node_modules/@instantdb/core/dist/esm/sanityCheckQueries.d.ts +18 -0
  108. package/node_modules/@instantdb/core/dist/esm/sanityCheckQueries.js +152 -0
  109. package/node_modules/@instantdb/core/dist/esm/schema.d.ts +72 -0
  110. package/node_modules/@instantdb/core/dist/esm/schema.js +128 -0
  111. package/node_modules/@instantdb/core/dist/esm/schemaTypes.d.ts +244 -0
  112. package/node_modules/@instantdb/core/dist/esm/schemaTypes.js +106 -0
  113. package/node_modules/@instantdb/core/dist/esm/store.d.ts +89 -0
  114. package/node_modules/@instantdb/core/dist/esm/store.js +722 -0
  115. package/node_modules/@instantdb/core/dist/esm/transactionValidation.d.ts +7 -0
  116. package/node_modules/@instantdb/core/dist/esm/transactionValidation.js +130 -0
  117. package/node_modules/@instantdb/core/dist/esm/utils/Deferred.d.ts +8 -0
  118. package/node_modules/@instantdb/core/dist/esm/utils/Deferred.js +17 -0
  119. package/node_modules/@instantdb/core/dist/esm/utils/PersistedObject.d.ts +80 -0
  120. package/node_modules/@instantdb/core/dist/esm/utils/PersistedObject.js +448 -0
  121. package/node_modules/@instantdb/core/dist/esm/utils/dates.d.ts +1 -0
  122. package/node_modules/@instantdb/core/dist/esm/utils/dates.js +211 -0
  123. package/node_modules/@instantdb/core/dist/esm/utils/error.d.ts +2 -0
  124. package/node_modules/@instantdb/core/dist/esm/utils/error.js +8 -0
  125. package/node_modules/@instantdb/core/dist/esm/utils/fetch.d.ts +125 -0
  126. package/node_modules/@instantdb/core/dist/esm/utils/fetch.js +31 -0
  127. package/node_modules/@instantdb/core/dist/esm/utils/flags.d.ts +4 -0
  128. package/node_modules/@instantdb/core/dist/esm/utils/flags.js +10 -0
  129. package/node_modules/@instantdb/core/dist/esm/utils/id.d.ts +3 -0
  130. package/node_modules/@instantdb/core/dist/esm/utils/id.js +25 -0
  131. package/node_modules/@instantdb/core/dist/esm/utils/linkIndex.d.ts +7 -0
  132. package/node_modules/@instantdb/core/dist/esm/utils/linkIndex.js +17 -0
  133. package/node_modules/@instantdb/core/dist/esm/utils/log.d.ts +6 -0
  134. package/node_modules/@instantdb/core/dist/esm/utils/log.js +13 -0
  135. package/node_modules/@instantdb/core/dist/esm/utils/object.d.ts +16 -0
  136. package/node_modules/@instantdb/core/dist/esm/utils/object.js +143 -0
  137. package/node_modules/@instantdb/core/dist/esm/utils/pgtime.d.ts +197 -0
  138. package/node_modules/@instantdb/core/dist/esm/utils/pgtime.js +197 -0
  139. package/node_modules/@instantdb/core/dist/esm/utils/pick.d.ts +1 -0
  140. package/node_modules/@instantdb/core/dist/esm/utils/pick.js +9 -0
  141. package/node_modules/@instantdb/core/dist/esm/utils/strings.d.ts +1 -0
  142. package/node_modules/@instantdb/core/dist/esm/utils/strings.js +15 -0
  143. package/node_modules/@instantdb/core/dist/esm/utils/weakHash.d.ts +15 -0
  144. package/node_modules/@instantdb/core/dist/esm/utils/weakHash.js +71 -0
  145. package/node_modules/@instantdb/core/dist/esm/utils/weakHashLegacy.d.ts +7 -0
  146. package/node_modules/@instantdb/core/dist/esm/utils/weakHashLegacy.js +72 -0
  147. package/node_modules/@instantdb/core/dist/esm/version.d.ts +2 -0
  148. package/node_modules/@instantdb/core/dist/esm/version.js +2 -0
  149. package/node_modules/@instantdb/core/dist/esm/warningToggle.d.ts +2 -0
  150. package/node_modules/@instantdb/core/dist/esm/warningToggle.js +4 -0
  151. package/node_modules/@instantdb/core/package.json +20 -0
  152. package/node_modules/@instantdb/eventsource/LICENSE +22 -0
  153. package/node_modules/@instantdb/eventsource/README.md +226 -0
  154. package/node_modules/@instantdb/eventsource/dist/index.cjs +279 -0
  155. package/node_modules/@instantdb/eventsource/dist/index.cjs.map +1 -0
  156. package/node_modules/@instantdb/eventsource/dist/index.d.cts +346 -0
  157. package/node_modules/@instantdb/eventsource/dist/index.d.ts +346 -0
  158. package/node_modules/@instantdb/eventsource/dist/index.js +279 -0
  159. package/node_modules/@instantdb/eventsource/dist/index.js.map +1 -0
  160. package/node_modules/@instantdb/eventsource/package.json +128 -0
  161. package/node_modules/@instantdb/eventsource/src/EventSource.ts +614 -0
  162. package/node_modules/@instantdb/eventsource/src/errors.ts +141 -0
  163. package/node_modules/@instantdb/eventsource/src/index.ts +3 -0
  164. package/node_modules/@instantdb/eventsource/src/types.ts +164 -0
  165. package/node_modules/@instantdb/react/LICENSE +202 -0
  166. package/node_modules/@instantdb/react/dist/esm/Cursors.d.ts +18 -0
  167. package/node_modules/@instantdb/react/dist/esm/Cursors.js +103 -0
  168. package/node_modules/@instantdb/react/dist/esm/InstantReactWebDatabase.d.ts +5 -0
  169. package/node_modules/@instantdb/react/dist/esm/InstantReactWebDatabase.js +5 -0
  170. package/node_modules/@instantdb/react/dist/esm/index.d.ts +6 -0
  171. package/node_modules/@instantdb/react/dist/esm/index.js +22 -0
  172. package/node_modules/@instantdb/react/dist/esm/init.d.ts +40 -0
  173. package/node_modules/@instantdb/react/dist/esm/init.js +49 -0
  174. package/node_modules/@instantdb/react/dist/esm/next-ssr/HydrationStreamProvider.d.ts +65 -0
  175. package/node_modules/@instantdb/react/dist/esm/next-ssr/HydrationStreamProvider.js +96 -0
  176. package/node_modules/@instantdb/react/dist/esm/next-ssr/InstantNextDatabase.d.ts +11 -0
  177. package/node_modules/@instantdb/react/dist/esm/next-ssr/InstantNextDatabase.js +31 -0
  178. package/node_modules/@instantdb/react/dist/esm/next-ssr/InstantSuspenseProvider.d.ts +24 -0
  179. package/node_modules/@instantdb/react/dist/esm/next-ssr/InstantSuspenseProvider.js +162 -0
  180. package/node_modules/@instantdb/react/dist/esm/next-ssr/getUserFromInstantCookie.d.ts +9 -0
  181. package/node_modules/@instantdb/react/dist/esm/next-ssr/getUserFromInstantCookie.js +16 -0
  182. package/node_modules/@instantdb/react/dist/esm/next-ssr/htmlescape.d.ts +2 -0
  183. package/node_modules/@instantdb/react/dist/esm/next-ssr/htmlescape.js +20 -0
  184. package/node_modules/@instantdb/react/dist/esm/next-ssr/index.d.ts +29 -0
  185. package/node_modules/@instantdb/react/dist/esm/next-ssr/index.js +34 -0
  186. package/node_modules/@instantdb/react/dist/esm/version.d.ts +2 -0
  187. package/node_modules/@instantdb/react/dist/esm/version.js +2 -0
  188. package/node_modules/@instantdb/react/package.json +24 -0
  189. package/node_modules/@instantdb/react-common/LICENSE +202 -0
  190. package/node_modules/@instantdb/react-common/dist/esm/InstantReactAbstractDatabase.d.ts +282 -0
  191. package/node_modules/@instantdb/react-common/dist/esm/InstantReactAbstractDatabase.js +386 -0
  192. package/node_modules/@instantdb/react-common/dist/esm/InstantReactRoom.d.ts +182 -0
  193. package/node_modules/@instantdb/react-common/dist/esm/InstantReactRoom.js +280 -0
  194. package/node_modules/@instantdb/react-common/dist/esm/index.d.ts +4 -0
  195. package/node_modules/@instantdb/react-common/dist/esm/index.js +3 -0
  196. package/node_modules/@instantdb/react-common/dist/esm/useInfiniteQuerySubscription.d.ts +27 -0
  197. package/node_modules/@instantdb/react-common/dist/esm/useInfiniteQuerySubscription.js +53 -0
  198. package/node_modules/@instantdb/react-common/dist/esm/useQuery.d.ts +6 -0
  199. package/node_modules/@instantdb/react-common/dist/esm/useQuery.js +75 -0
  200. package/node_modules/@instantdb/react-common/dist/esm/useTimeout.d.ts +4 -0
  201. package/node_modules/@instantdb/react-common/dist/esm/useTimeout.js +17 -0
  202. package/node_modules/@instantdb/react-common/dist/esm/version.d.ts +2 -0
  203. package/node_modules/@instantdb/react-common/dist/esm/version.js +2 -0
  204. package/node_modules/@instantdb/react-common/package.json +22 -0
  205. package/node_modules/@instantdb/version/LICENSE +202 -0
  206. package/node_modules/@instantdb/version/dist/esm/index.d.ts +2 -0
  207. package/node_modules/@instantdb/version/dist/esm/index.js +2 -0
  208. package/node_modules/@instantdb/version/dist/esm/version.d.ts +2 -0
  209. package/node_modules/@instantdb/version/dist/esm/version.js +5 -0
  210. package/node_modules/@instantdb/version/package.json +16 -0
  211. package/node_modules/@instantdb/webhooks/LICENSE +202 -0
  212. package/node_modules/@instantdb/webhooks/dist/esm/index.d.ts +587 -0
  213. package/node_modules/@instantdb/webhooks/dist/esm/index.js +748 -0
  214. package/node_modules/@instantdb/webhooks/package.json +18 -0
  215. package/node_modules/cookie/LICENSE +24 -0
  216. package/node_modules/cookie/README.md +295 -0
  217. package/node_modules/cookie/dist/index.d.ts +143 -0
  218. package/node_modules/cookie/dist/index.js +355 -0
  219. package/node_modules/cookie/dist/index.js.map +1 -0
  220. package/node_modules/cookie/package.json +57 -0
  221. package/node_modules/eventsource/LICENSE +22 -0
  222. package/node_modules/eventsource/README.md +252 -0
  223. package/node_modules/eventsource/dist/EventSource.d.ts +128 -0
  224. package/node_modules/eventsource/dist/EventSource.d.ts.map +1 -0
  225. package/node_modules/eventsource/dist/EventSource.js +556 -0
  226. package/node_modules/eventsource/dist/EventSource.js.map +1 -0
  227. package/node_modules/eventsource/dist/errors.d.ts +53 -0
  228. package/node_modules/eventsource/dist/errors.d.ts.map +1 -0
  229. package/node_modules/eventsource/dist/errors.js +120 -0
  230. package/node_modules/eventsource/dist/errors.js.map +1 -0
  231. package/node_modules/eventsource/dist/index.d.ts +4 -0
  232. package/node_modules/eventsource/dist/index.d.ts.map +1 -0
  233. package/node_modules/eventsource/dist/index.js +3 -0
  234. package/node_modules/eventsource/dist/index.js.map +1 -0
  235. package/node_modules/eventsource/dist/types.d.ts +156 -0
  236. package/node_modules/eventsource/dist/types.d.ts.map +1 -0
  237. package/node_modules/eventsource/dist/types.js +2 -0
  238. package/node_modules/eventsource/dist/types.js.map +1 -0
  239. package/node_modules/eventsource/node_modules/eventsource-parser/LICENSE +21 -0
  240. package/node_modules/eventsource/node_modules/eventsource-parser/README.md +177 -0
  241. package/node_modules/eventsource/node_modules/eventsource-parser/dist/errors.d.ts +35 -0
  242. package/node_modules/eventsource/node_modules/eventsource-parser/dist/errors.d.ts.map +1 -0
  243. package/node_modules/eventsource/node_modules/eventsource-parser/dist/errors.js +16 -0
  244. package/node_modules/eventsource/node_modules/eventsource-parser/dist/errors.js.map +1 -0
  245. package/node_modules/eventsource/node_modules/eventsource-parser/dist/index.d.ts +4 -0
  246. package/node_modules/eventsource/node_modules/eventsource-parser/dist/index.d.ts.map +1 -0
  247. package/node_modules/eventsource/node_modules/eventsource-parser/dist/index.js +3 -0
  248. package/node_modules/eventsource/node_modules/eventsource-parser/dist/index.js.map +1 -0
  249. package/node_modules/eventsource/node_modules/eventsource-parser/dist/parse.d.ts +12 -0
  250. package/node_modules/eventsource/node_modules/eventsource-parser/dist/parse.d.ts.map +1 -0
  251. package/node_modules/eventsource/node_modules/eventsource-parser/dist/parse.js +509 -0
  252. package/node_modules/eventsource/node_modules/eventsource-parser/dist/parse.js.map +1 -0
  253. package/node_modules/eventsource/node_modules/eventsource-parser/dist/stream.d.ts +76 -0
  254. package/node_modules/eventsource/node_modules/eventsource-parser/dist/stream.d.ts.map +1 -0
  255. package/node_modules/eventsource/node_modules/eventsource-parser/dist/stream.js +59 -0
  256. package/node_modules/eventsource/node_modules/eventsource-parser/dist/stream.js.map +1 -0
  257. package/node_modules/eventsource/node_modules/eventsource-parser/dist/types.d.ts +130 -0
  258. package/node_modules/eventsource/node_modules/eventsource-parser/dist/types.d.ts.map +1 -0
  259. package/node_modules/eventsource/node_modules/eventsource-parser/dist/types.js +1 -0
  260. package/node_modules/eventsource/node_modules/eventsource-parser/dist/types.js.map +1 -0
  261. package/node_modules/eventsource/node_modules/eventsource-parser/package.json +96 -0
  262. package/node_modules/eventsource/node_modules/eventsource-parser/src/errors.ts +44 -0
  263. package/node_modules/eventsource/node_modules/eventsource-parser/src/index.ts +3 -0
  264. package/node_modules/eventsource/node_modules/eventsource-parser/src/parse.ts +576 -0
  265. package/node_modules/eventsource/node_modules/eventsource-parser/src/stream.ts +118 -0
  266. package/node_modules/eventsource/node_modules/eventsource-parser/src/types.ts +138 -0
  267. package/node_modules/eventsource/package.json +103 -0
  268. package/node_modules/eventsource/src/EventSource.ts +844 -0
  269. package/node_modules/eventsource/src/errors.ts +141 -0
  270. package/node_modules/eventsource/src/index.ts +3 -0
  271. package/node_modules/eventsource/src/types.ts +166 -0
  272. package/node_modules/eventsource-parser/LICENSE +21 -0
  273. package/node_modules/eventsource-parser/README.md +157 -0
  274. package/node_modules/eventsource-parser/dist/index.cjs +177 -0
  275. package/node_modules/eventsource-parser/dist/index.cjs.map +1 -0
  276. package/node_modules/eventsource-parser/dist/index.d.cts +169 -0
  277. package/node_modules/eventsource-parser/dist/index.d.ts +169 -0
  278. package/node_modules/eventsource-parser/dist/index.js +177 -0
  279. package/node_modules/eventsource-parser/dist/index.js.map +1 -0
  280. package/node_modules/eventsource-parser/dist/stream.cjs +29 -0
  281. package/node_modules/eventsource-parser/dist/stream.cjs.map +1 -0
  282. package/node_modules/eventsource-parser/dist/stream.d.cts +134 -0
  283. package/node_modules/eventsource-parser/dist/stream.d.ts +134 -0
  284. package/node_modules/eventsource-parser/dist/stream.js +30 -0
  285. package/node_modules/eventsource-parser/dist/stream.js.map +1 -0
  286. package/node_modules/eventsource-parser/package.json +92 -0
  287. package/node_modules/eventsource-parser/src/errors.ts +44 -0
  288. package/node_modules/eventsource-parser/src/index.ts +8 -0
  289. package/node_modules/eventsource-parser/src/parse.ts +439 -0
  290. package/node_modules/eventsource-parser/src/stream.ts +107 -0
  291. package/node_modules/eventsource-parser/src/types.ts +122 -0
  292. package/node_modules/eventsource-parser/stream.js +2 -0
  293. package/node_modules/mutative/LICENSE +21 -0
  294. package/node_modules/mutative/README.md +870 -0
  295. package/node_modules/mutative/dist/apply.d.ts +31 -0
  296. package/node_modules/mutative/dist/array.d.ts +45 -0
  297. package/node_modules/mutative/dist/constant.d.ts +8 -0
  298. package/node_modules/mutative/dist/create.d.ts +46 -0
  299. package/node_modules/mutative/dist/current.d.ts +23 -0
  300. package/node_modules/mutative/dist/draft.d.ts +3 -0
  301. package/node_modules/mutative/dist/draftify.d.ts +2 -0
  302. package/node_modules/mutative/dist/error.d.ts +54 -0
  303. package/node_modules/mutative/dist/index.d.mts +1 -0
  304. package/node_modules/mutative/dist/index.d.ts +12 -0
  305. package/node_modules/mutative/dist/index.js +8 -0
  306. package/node_modules/mutative/dist/index.mjs +3 -0
  307. package/node_modules/mutative/dist/interface.d.ts +200 -0
  308. package/node_modules/mutative/dist/internal.d.ts +4 -0
  309. package/node_modules/mutative/dist/makeCreator.d.ts +54 -0
  310. package/node_modules/mutative/dist/map.d.ts +15 -0
  311. package/node_modules/mutative/dist/mutative.cjs.development.js +2126 -0
  312. package/node_modules/mutative/dist/mutative.cjs.development.js.map +1 -0
  313. package/node_modules/mutative/dist/mutative.cjs.production.min.js +2 -0
  314. package/node_modules/mutative/dist/mutative.cjs.production.min.js.map +1 -0
  315. package/node_modules/mutative/dist/mutative.esm.js +2123 -0
  316. package/node_modules/mutative/dist/mutative.esm.js.map +1 -0
  317. package/node_modules/mutative/dist/mutative.esm.mjs +2123 -0
  318. package/node_modules/mutative/dist/mutative.esm.mjs.map +1 -0
  319. package/node_modules/mutative/dist/mutative.esm.production.min.mjs +2 -0
  320. package/node_modules/mutative/dist/mutative.esm.production.min.mjs.map +1 -0
  321. package/node_modules/mutative/dist/mutative.umd.development.js +2132 -0
  322. package/node_modules/mutative/dist/mutative.umd.development.js.map +1 -0
  323. package/node_modules/mutative/dist/mutative.umd.production.min.js +2 -0
  324. package/node_modules/mutative/dist/mutative.umd.production.min.js.map +1 -0
  325. package/node_modules/mutative/dist/original.d.ts +19 -0
  326. package/node_modules/mutative/dist/patch.d.ts +2 -0
  327. package/node_modules/mutative/dist/rawReturn.d.ts +19 -0
  328. package/node_modules/mutative/dist/set.d.ts +14 -0
  329. package/node_modules/mutative/dist/unsafe.d.ts +34 -0
  330. package/node_modules/mutative/dist/utils/cast.d.ts +13 -0
  331. package/node_modules/mutative/dist/utils/copy.d.ts +5 -0
  332. package/node_modules/mutative/dist/utils/deepFreeze.d.ts +1 -0
  333. package/node_modules/mutative/dist/utils/draft.d.ts +27 -0
  334. package/node_modules/mutative/dist/utils/finalize.d.ts +13 -0
  335. package/node_modules/mutative/dist/utils/forEach.d.ts +1 -0
  336. package/node_modules/mutative/dist/utils/index.d.ts +7 -0
  337. package/node_modules/mutative/dist/utils/mark.d.ts +2 -0
  338. package/node_modules/mutative/dist/utils/marker.d.ts +5 -0
  339. package/node_modules/mutative/dist/utils/proto.d.ts +10 -0
  340. package/node_modules/mutative/package.json +142 -0
  341. package/node_modules/mutative/src/apply.ts +239 -0
  342. package/node_modules/mutative/src/array.ts +679 -0
  343. package/node_modules/mutative/src/constant.ts +17 -0
  344. package/node_modules/mutative/src/create.ts +27 -0
  345. package/node_modules/mutative/src/current.ts +206 -0
  346. package/node_modules/mutative/src/draft.ts +489 -0
  347. package/node_modules/mutative/src/draftify.ts +57 -0
  348. package/node_modules/mutative/src/error.ts +129 -0
  349. package/node_modules/mutative/src/index.ts +21 -0
  350. package/node_modules/mutative/src/interface.ts +321 -0
  351. package/node_modules/mutative/src/internal.ts +5 -0
  352. package/node_modules/mutative/src/makeCreator.ts +382 -0
  353. package/node_modules/mutative/src/map.ts +160 -0
  354. package/node_modules/mutative/src/original.ts +28 -0
  355. package/node_modules/mutative/src/patch.ts +386 -0
  356. package/node_modules/mutative/src/rawReturn.ts +41 -0
  357. package/node_modules/mutative/src/set.ts +201 -0
  358. package/node_modules/mutative/src/unsafe.ts +66 -0
  359. package/node_modules/mutative/src/utils/cast.ts +22 -0
  360. package/node_modules/mutative/src/utils/copy.ts +224 -0
  361. package/node_modules/mutative/src/utils/deepFreeze.ts +103 -0
  362. package/node_modules/mutative/src/utils/draft.ts +189 -0
  363. package/node_modules/mutative/src/utils/finalize.ts +232 -0
  364. package/node_modules/mutative/src/utils/forEach.ts +24 -0
  365. package/node_modules/mutative/src/utils/index.ts +7 -0
  366. package/node_modules/mutative/src/utils/mark.ts +11 -0
  367. package/node_modules/mutative/src/utils/marker.ts +34 -0
  368. package/node_modules/mutative/src/utils/proto.ts +38 -0
  369. package/node_modules/uuid/LICENSE.md +9 -0
  370. package/node_modules/uuid/README.md +491 -0
  371. package/node_modules/uuid/dist/index.d.ts +15 -0
  372. package/node_modules/uuid/dist/index.js +14 -0
  373. package/node_modules/uuid/dist/max.d.ts +2 -0
  374. package/node_modules/uuid/dist/max.js +1 -0
  375. package/node_modules/uuid/dist/md5.d.ts +2 -0
  376. package/node_modules/uuid/dist/md5.js +135 -0
  377. package/node_modules/uuid/dist/nil.d.ts +2 -0
  378. package/node_modules/uuid/dist/nil.js +1 -0
  379. package/node_modules/uuid/dist/parse.d.ts +3 -0
  380. package/node_modules/uuid/dist/parse.js +9 -0
  381. package/node_modules/uuid/dist/regex.d.ts +2 -0
  382. package/node_modules/uuid/dist/regex.js +1 -0
  383. package/node_modules/uuid/dist/rng.d.ts +1 -0
  384. package/node_modules/uuid/dist/rng.js +4 -0
  385. package/node_modules/uuid/dist/sha1.d.ts +2 -0
  386. package/node_modules/uuid/dist/sha1.js +70 -0
  387. package/node_modules/uuid/dist/stringify.d.ts +3 -0
  388. package/node_modules/uuid/dist/stringify.js +35 -0
  389. package/node_modules/uuid/dist/types.d.ts +22 -0
  390. package/node_modules/uuid/dist/types.js +1 -0
  391. package/node_modules/uuid/dist/uuid-bin.d.ts +1 -0
  392. package/node_modules/uuid/dist/uuid-bin.js +70 -0
  393. package/node_modules/uuid/dist/v1.d.ts +11 -0
  394. package/node_modules/uuid/dist/v1.js +87 -0
  395. package/node_modules/uuid/dist/v1ToV6.d.ts +3 -0
  396. package/node_modules/uuid/dist/v1ToV6.js +10 -0
  397. package/node_modules/uuid/dist/v3.d.ts +9 -0
  398. package/node_modules/uuid/dist/v3.js +9 -0
  399. package/node_modules/uuid/dist/v35.d.ts +7 -0
  400. package/node_modules/uuid/dist/v35.js +39 -0
  401. package/node_modules/uuid/dist/v4.d.ts +4 -0
  402. package/node_modules/uuid/dist/v4.js +29 -0
  403. package/node_modules/uuid/dist/v5.d.ts +9 -0
  404. package/node_modules/uuid/dist/v5.js +9 -0
  405. package/node_modules/uuid/dist/v6.d.ts +4 -0
  406. package/node_modules/uuid/dist/v6.js +20 -0
  407. package/node_modules/uuid/dist/v6ToV1.d.ts +2 -0
  408. package/node_modules/uuid/dist/v6ToV1.js +10 -0
  409. package/node_modules/uuid/dist/v7.d.ts +9 -0
  410. package/node_modules/uuid/dist/v7.js +68 -0
  411. package/node_modules/uuid/dist/validate.d.ts +2 -0
  412. package/node_modules/uuid/dist/validate.js +5 -0
  413. package/node_modules/uuid/dist/version.d.ts +2 -0
  414. package/node_modules/uuid/dist/version.js +8 -0
  415. package/node_modules/uuid/dist-node/bin/uuid +2 -0
  416. package/node_modules/uuid/dist-node/index.js +14 -0
  417. package/node_modules/uuid/dist-node/max.js +1 -0
  418. package/node_modules/uuid/dist-node/md5.js +11 -0
  419. package/node_modules/uuid/dist-node/nil.js +1 -0
  420. package/node_modules/uuid/dist-node/parse.js +9 -0
  421. package/node_modules/uuid/dist-node/regex.js +1 -0
  422. package/node_modules/uuid/dist-node/rng.js +4 -0
  423. package/node_modules/uuid/dist-node/sha1.js +11 -0
  424. package/node_modules/uuid/dist-node/stringify.js +35 -0
  425. package/node_modules/uuid/dist-node/types.js +1 -0
  426. package/node_modules/uuid/dist-node/uuid-bin.js +70 -0
  427. package/node_modules/uuid/dist-node/v1.js +87 -0
  428. package/node_modules/uuid/dist-node/v1ToV6.js +10 -0
  429. package/node_modules/uuid/dist-node/v3.js +9 -0
  430. package/node_modules/uuid/dist-node/v35.js +39 -0
  431. package/node_modules/uuid/dist-node/v4.js +29 -0
  432. package/node_modules/uuid/dist-node/v5.js +9 -0
  433. package/node_modules/uuid/dist-node/v6.js +20 -0
  434. package/node_modules/uuid/dist-node/v6ToV1.js +10 -0
  435. package/node_modules/uuid/dist-node/v7.js +68 -0
  436. package/node_modules/uuid/dist-node/validate.js +5 -0
  437. package/node_modules/uuid/dist-node/version.js +8 -0
  438. package/node_modules/uuid/package.json +115 -0
  439. package/package.json +80 -4
  440. package/release.json +78 -0
@@ -0,0 +1,614 @@
1
+ import {createParser, type EventSourceMessage, type EventSourceParser} from 'eventsource-parser'
2
+
3
+ import {ErrorEvent, flattenError, syntaxError} from './errors.js'
4
+ import type {
5
+ AddEventListenerOptions,
6
+ EventListenerOptions,
7
+ EventListenerOrEventListenerObject,
8
+ EventSourceEventMap,
9
+ EventSourceFetchInit,
10
+ EventSourceInit,
11
+ FetchLike,
12
+ FetchLikeResponse,
13
+ MessageEventLike,
14
+ } from './types.js'
15
+
16
+ /**
17
+ * An `EventSource` instance opens a persistent connection to an HTTP server, which sends events
18
+ * in `text/event-stream` format. The connection remains open until closed by calling `.close()`.
19
+ *
20
+ * @public
21
+ * @example
22
+ * ```js
23
+ * const eventSource = new EventSource('https://example.com/stream')
24
+ * eventSource.addEventListener('error', (error) => {
25
+ * console.error(error)
26
+ * })
27
+ * eventSource.addEventListener('message', (event) => {
28
+ * console.log('Received message:', event.data)
29
+ * })
30
+ * ```
31
+ */
32
+ export class EventSource extends EventTarget {
33
+ /**
34
+ * ReadyState representing an EventSource currently trying to connect
35
+ *
36
+ * @public
37
+ */
38
+ static CONNECTING = 0 as const
39
+
40
+ /**
41
+ * ReadyState representing an EventSource connection that is open (eg connected)
42
+ *
43
+ * @public
44
+ */
45
+ static OPEN = 1 as const
46
+
47
+ /**
48
+ * ReadyState representing an EventSource connection that is closed (eg disconnected)
49
+ *
50
+ * @public
51
+ */
52
+ static CLOSED = 2 as const
53
+
54
+ /**
55
+ * ReadyState representing an EventSource currently trying to connect
56
+ *
57
+ * @public
58
+ */
59
+ readonly CONNECTING = 0 as const
60
+
61
+ /**
62
+ * ReadyState representing an EventSource connection that is open (eg connected)
63
+ *
64
+ * @public
65
+ */
66
+ readonly OPEN = 1 as const
67
+
68
+ /**
69
+ * ReadyState representing an EventSource connection that is closed (eg disconnected)
70
+ *
71
+ * @public
72
+ */
73
+ readonly CLOSED = 2 as const
74
+
75
+ /**
76
+ * Returns the state of this EventSource object's connection. It can have the values described below.
77
+ *
78
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState)
79
+ *
80
+ * Note: typed as `number` instead of `0 | 1 | 2` for compatibility with the `EventSource` interface,
81
+ * defined in the TypeScript `dom` library.
82
+ *
83
+ * @public
84
+ */
85
+ public get readyState(): number {
86
+ return this.#readyState
87
+ }
88
+
89
+ /**
90
+ * Returns the URL providing the event stream.
91
+ *
92
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url)
93
+ *
94
+ * @public
95
+ */
96
+ public get url(): string {
97
+ return this.#url.href
98
+ }
99
+
100
+ /**
101
+ * Returns true if the credentials mode for connection requests to the URL providing the event stream is set to "include", and false otherwise.
102
+ *
103
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials)
104
+ */
105
+ public get withCredentials(): boolean {
106
+ return this.#withCredentials
107
+ }
108
+
109
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */
110
+ public get onerror(): ((ev: ErrorEvent) => unknown) | null {
111
+ return this.#onError
112
+ }
113
+ public set onerror(value: ((ev: ErrorEvent) => unknown) | null) {
114
+ this.#onError = value
115
+ }
116
+
117
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */
118
+ public get onmessage(): ((ev: MessageEvent) => unknown) | null {
119
+ return this.#onMessage
120
+ }
121
+ public set onmessage(value: ((ev: MessageEvent) => unknown) | null) {
122
+ this.#onMessage = value
123
+ }
124
+
125
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */
126
+ public get onopen(): ((ev: Event) => unknown) | null {
127
+ return this.#onOpen
128
+ }
129
+ public set onopen(value: ((ev: Event) => unknown) | null) {
130
+ this.#onOpen = value
131
+ }
132
+
133
+ override addEventListener<K extends keyof EventSourceEventMap>(
134
+ type: K,
135
+ listener: (this: EventSource, ev: EventSourceEventMap[K]) => unknown,
136
+ options?: boolean | AddEventListenerOptions,
137
+ ): void
138
+ override addEventListener(
139
+ type: string,
140
+ listener: (this: EventSource, event: MessageEvent) => unknown,
141
+ options?: boolean | AddEventListenerOptions,
142
+ ): void
143
+ override addEventListener(
144
+ type: string,
145
+ listener: EventListenerOrEventListenerObject,
146
+ options?: boolean | AddEventListenerOptions,
147
+ ): void
148
+ override addEventListener(
149
+ type: string,
150
+ listener:
151
+ | ((this: EventSource, event: MessageEvent) => unknown)
152
+ | EventListenerOrEventListenerObject,
153
+ options?: boolean | AddEventListenerOptions,
154
+ ): void {
155
+ const listen = listener as (this: EventSource, event: Event) => unknown
156
+ super.addEventListener(type, listen, options)
157
+ }
158
+
159
+ override removeEventListener<K extends keyof EventSourceEventMap>(
160
+ type: K,
161
+ listener: (this: EventSource, ev: EventSourceEventMap[K]) => unknown,
162
+ options?: boolean | EventListenerOptions,
163
+ ): void
164
+ override removeEventListener(
165
+ type: string,
166
+ listener: (this: EventSource, event: MessageEvent) => unknown,
167
+ options?: boolean | EventListenerOptions,
168
+ ): void
169
+ override removeEventListener(
170
+ type: string,
171
+ listener: EventListenerOrEventListenerObject,
172
+ options?: boolean | EventListenerOptions,
173
+ ): void
174
+ override removeEventListener(
175
+ type: string,
176
+ listener:
177
+ | ((this: EventSource, event: MessageEvent) => unknown)
178
+ | EventListenerOrEventListenerObject,
179
+ options?: boolean | EventListenerOptions,
180
+ ): void {
181
+ const listen = listener as (this: EventSource, event: Event) => unknown
182
+ super.removeEventListener(type, listen, options)
183
+ }
184
+
185
+ constructor(url: string | URL, eventSourceInitDict?: EventSourceInit) {
186
+ super()
187
+
188
+ try {
189
+ if (url instanceof URL) {
190
+ this.#url = url
191
+ } else if (typeof url === 'string') {
192
+ this.#url = new URL(url, getBaseURL())
193
+ } else {
194
+ throw new Error('Invalid URL')
195
+ }
196
+ } catch (err) {
197
+ throw syntaxError('An invalid or illegal string was specified')
198
+ }
199
+
200
+ this.#parser = createParser({
201
+ onEvent: this.#onEvent,
202
+ onRetry: this.#onRetryChange,
203
+ })
204
+
205
+ this.#readyState = this.CONNECTING
206
+ this.#reconnectInterval = 3000
207
+ this.#fetch = eventSourceInitDict?.fetch ?? globalThis.fetch
208
+ this.#MessageEvent = eventSourceInitDict?.messageEvent ?? globalThis.MessageEvent
209
+ this.#withCredentials = eventSourceInitDict?.withCredentials ?? false
210
+
211
+ this.#connect()
212
+ }
213
+
214
+ /**
215
+ * Aborts any instances of the fetch algorithm started for this EventSource object, and sets the readyState attribute to CLOSED.
216
+ *
217
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close)
218
+ *
219
+ * @public
220
+ */
221
+ close(): void {
222
+ if (this.#reconnectTimer) clearTimeout(this.#reconnectTimer)
223
+ if (this.#readyState === this.CLOSED) return
224
+ if (this.#controller) this.#controller.abort()
225
+ this.#readyState = this.CLOSED
226
+ this.#controller = undefined
227
+ }
228
+
229
+ // PRIVATES FOLLOW
230
+
231
+ /**
232
+ * Current connection state
233
+ *
234
+ * @internal
235
+ */
236
+ #readyState: number
237
+
238
+ /**
239
+ * Original URL used to connect.
240
+ *
241
+ * Note that this will stay the same even after a redirect.
242
+ *
243
+ * @internal
244
+ */
245
+ #url: URL
246
+
247
+ /**
248
+ * The destination URL after a redirect. Is reset on reconnection.
249
+ *
250
+ * @internal
251
+ */
252
+ #redirectUrl: URL | undefined
253
+
254
+ /**
255
+ * Whether to include credentials in the request
256
+ *
257
+ * @internal
258
+ */
259
+ #withCredentials: boolean
260
+
261
+ /**
262
+ * The fetch implementation to use
263
+ *
264
+ * @internal
265
+ */
266
+ #fetch: FetchLike
267
+
268
+ /**
269
+ * The MessageEvent constructor to use
270
+ *
271
+ * @internal
272
+ */
273
+ #MessageEvent: MessageEventLike
274
+
275
+ /**
276
+ * The reconnection time in milliseconds
277
+ *
278
+ * @internal
279
+ */
280
+ #reconnectInterval: number
281
+
282
+ /**
283
+ * Reference to an ongoing reconnect attempt, if any
284
+ *
285
+ * @internal
286
+ */
287
+ #reconnectTimer: ReturnType<typeof setTimeout> | undefined
288
+
289
+ /**
290
+ * The last event ID seen by the EventSource, which will be sent as `Last-Event-ID` in the
291
+ * request headers on a reconnection attempt.
292
+ *
293
+ * @internal
294
+ */
295
+ #lastEventId: string | null = null
296
+
297
+ /**
298
+ * The AbortController instance used to abort the fetch request
299
+ *
300
+ * @internal
301
+ */
302
+ #controller: AbortController | undefined
303
+
304
+ /**
305
+ * Instance of an EventSource parser (`eventsource-parser` npm module)
306
+ *
307
+ * @internal
308
+ */
309
+ #parser: EventSourceParser
310
+
311
+ /**
312
+ * Holds the current error handler, attached through `onerror` property directly.
313
+ * Note that `addEventListener('error', …)` will not be stored here.
314
+ *
315
+ * @internal
316
+ */
317
+ #onError: ((ev: ErrorEvent) => unknown) | null = null
318
+
319
+ /**
320
+ * Holds the current message handler, attached through `onmessage` property directly.
321
+ * Note that `addEventListener('message', …)` will not be stored here.
322
+ *
323
+ * @internal
324
+ */
325
+ #onMessage: ((ev: MessageEvent) => unknown) | null = null
326
+
327
+ /**
328
+ * Holds the current open handler, attached through `onopen` property directly.
329
+ * Note that `addEventListener('open', …)` will not be stored here.
330
+ *
331
+ * @internal
332
+ */
333
+ #onOpen: ((ev: Event) => unknown) | null = null
334
+
335
+ /**
336
+ * Connect to the given URL and start receiving events
337
+ *
338
+ * @internal
339
+ */
340
+ #connect() {
341
+ this.#readyState = this.CONNECTING
342
+ this.#controller = new AbortController()
343
+
344
+ // Browser tests are failing if we directly call `this.#fetch()`, thus the indirection.
345
+ const fetch = this.#fetch
346
+ fetch(this.#url, this.#getRequestOptions())
347
+ .then(this.#onFetchResponse)
348
+ .catch(this.#onFetchError)
349
+ }
350
+
351
+ /**
352
+ * Handles the fetch response
353
+ *
354
+ * @param response - The Fetch(ish) response
355
+ * @internal
356
+ */
357
+ #onFetchResponse = async (response: FetchLikeResponse) => {
358
+ this.#parser.reset()
359
+
360
+ const {body, redirected, status, headers} = response
361
+
362
+ // [spec] a client can be told to stop reconnecting using the HTTP 204 No Content response code.
363
+ if (status === 204) {
364
+ // We still need to emit an error event - this mirrors the browser behavior,
365
+ // and without it there is no way to tell the user that the connection was closed.
366
+ this.#failConnection('Server sent HTTP 204, not reconnecting', 204)
367
+ this.close()
368
+ return
369
+ }
370
+
371
+ // [spec] …Event stream requests can be redirected using HTTP 301 and 307 redirects as with
372
+ // [spec] normal HTTP requests.
373
+ // Spec does not say anything about other redirect codes (302, 308), but this seems an
374
+ // unintended omission, rather than a feature. Browsers will happily redirect on other 3xxs's.
375
+ if (redirected) {
376
+ this.#redirectUrl = new URL(response.url)
377
+ } else {
378
+ this.#redirectUrl = undefined
379
+ }
380
+
381
+ // [spec] if res's status is not 200, …, then fail the connection.
382
+ if (status !== 200) {
383
+ this.#failConnection(`Non-200 status code (${status})`, status)
384
+ return
385
+ }
386
+
387
+ // [spec] …or if res's `Content-Type` is not `text/event-stream`, then fail the connection.
388
+ const contentType = headers.get('content-type') || ''
389
+ if (!contentType.startsWith('text/event-stream')) {
390
+ this.#failConnection('Invalid content type, expected "text/event-stream"', status)
391
+ return
392
+ }
393
+
394
+ // [spec] …if the readyState attribute is set to a value other than CLOSED…
395
+ if (this.#readyState === this.CLOSED) {
396
+ return
397
+ }
398
+
399
+ // [spec] …sets the readyState attribute to OPEN and fires an event
400
+ // [spec] …named open at the EventSource object.
401
+ this.#readyState = this.OPEN
402
+
403
+ const openEvent = new Event('open')
404
+ this.#onOpen?.(openEvent)
405
+ this.dispatchEvent(openEvent)
406
+
407
+ // Ensure that the response stream is a web stream
408
+ if (typeof body !== 'object' || !body || !('getReader' in body)) {
409
+ this.#failConnection('Invalid response body, expected a web ReadableStream', status)
410
+ this.close() // This should only happen if `fetch` provided is "faulty" - don't reconnect
411
+ return
412
+ }
413
+
414
+ const decoder = new TextDecoder()
415
+
416
+ const reader = body.getReader()
417
+ let open = true
418
+
419
+ do {
420
+ const {done, value} = await reader.read()
421
+ if (value) {
422
+ this.#parser.feed(decoder.decode(value, {stream: !done}))
423
+ }
424
+
425
+ if (!done) {
426
+ continue
427
+ }
428
+
429
+ open = false
430
+ this.#parser.reset()
431
+
432
+ this.#scheduleReconnect()
433
+ } while (open)
434
+ }
435
+
436
+ /**
437
+ * Handles rejected requests for the EventSource endpoint
438
+ *
439
+ * @param err - The error from `fetch()`
440
+ * @internal
441
+ */
442
+ #onFetchError = (err: Error & {type?: string}) => {
443
+ this.#controller = undefined
444
+
445
+ // We expect abort errors when the user manually calls `close()` - ignore those
446
+ if (err.name === 'AbortError' || err.type === 'aborted') {
447
+ return
448
+ }
449
+
450
+ this.#scheduleReconnect(flattenError(err))
451
+ }
452
+
453
+ /**
454
+ * Get request options for the `fetch()` request
455
+ *
456
+ * @returns The request options
457
+ * @internal
458
+ */
459
+ #getRequestOptions(): EventSourceFetchInit {
460
+ const lastEvent = this.#lastEventId ? {'Last-Event-ID': this.#lastEventId} : undefined
461
+
462
+ const init: EventSourceFetchInit = {
463
+ // [spec] Let `corsAttributeState` be `Anonymous`…
464
+ // [spec] …will have their mode set to "cors"…
465
+ mode: 'cors',
466
+ redirect: 'follow',
467
+ headers: {Accept: 'text/event-stream', ...lastEvent},
468
+ cache: 'no-store',
469
+ signal: this.#controller?.signal,
470
+ }
471
+
472
+ // Some environments crash if attempting to set `credentials` where it is not supported,
473
+ // eg on Cloudflare Workers. To avoid this, we only set it in browser-like environments.
474
+ if ('window' in globalThis) {
475
+ // [spec] …and their credentials mode set to "same-origin"
476
+ // [spec] …if the `withCredentials` attribute is `true`, set the credentials mode to "include"…
477
+ init.credentials = this.withCredentials ? 'include' : 'same-origin'
478
+ }
479
+
480
+ return init
481
+ }
482
+
483
+ /**
484
+ * Called by EventSourceParser instance when an event has successfully been parsed
485
+ * and is ready to be processed.
486
+ *
487
+ * @param event - The parsed event
488
+ * @internal
489
+ */
490
+ #onEvent = (event: EventSourceMessage) => {
491
+ if (typeof event.id === 'string') {
492
+ this.#lastEventId = event.id
493
+ }
494
+
495
+ const messageEvent = new this.#MessageEvent(event.event || 'message', {
496
+ data: event.data,
497
+ origin: this.#redirectUrl ? this.#redirectUrl.origin : this.#url.origin,
498
+ lastEventId: event.id || '',
499
+ })
500
+
501
+ // The `onmessage` property of the EventSource instance only triggers on messages without an
502
+ // `event` field, or ones that explicitly set `message`.
503
+ if (this.#onMessage && (!event.event || event.event === 'message')) {
504
+ this.#onMessage(messageEvent)
505
+ }
506
+
507
+ this.dispatchEvent(messageEvent)
508
+ }
509
+
510
+ /**
511
+ * Called by EventSourceParser instance when a new reconnection interval is received
512
+ * from the EventSource endpoint.
513
+ *
514
+ * @param value - The new reconnection interval in milliseconds
515
+ * @internal
516
+ */
517
+ #onRetryChange = (value: number) => {
518
+ this.#reconnectInterval = value
519
+ }
520
+
521
+ /**
522
+ * Handles the process referred to in the EventSource specification as "failing a connection".
523
+ *
524
+ * @param error - The error causing the connection to fail
525
+ * @param code - The HTTP status code, if available
526
+ * @internal
527
+ */
528
+ #failConnection(message?: string, code?: number) {
529
+ // [spec] …if the readyState attribute is set to a value other than CLOSED,
530
+ // [spec] sets the readyState attribute to CLOSED…
531
+ if (this.#readyState !== this.CLOSED) {
532
+ this.#readyState = this.CLOSED
533
+ }
534
+
535
+ // [spec] …and fires an event named `error` at the `EventSource` object.
536
+ // [spec] Once the user agent has failed the connection, it does not attempt to reconnect.
537
+ // [spec] > Implementations are especially encouraged to report detailed information
538
+ // [spec] > to their development consoles whenever an error event is fired, since little
539
+ // [spec] > to no information can be made available in the events themselves.
540
+ // Printing to console is not very programatically helpful, though, so we emit a custom event.
541
+ const errorEvent = new ErrorEvent('error', {code, message})
542
+
543
+ this.#onError?.(errorEvent)
544
+ this.dispatchEvent(errorEvent)
545
+ }
546
+
547
+ /**
548
+ * Schedules a reconnection attempt against the EventSource endpoint.
549
+ *
550
+ * @param message - The error causing the connection to fail
551
+ * @param code - The HTTP status code, if available
552
+ * @internal
553
+ */
554
+ #scheduleReconnect(message?: string, code?: number) {
555
+ // [spec] If the readyState attribute is set to CLOSED, abort the task.
556
+ if (this.#readyState === this.CLOSED) {
557
+ return
558
+ }
559
+
560
+ // [spec] Set the readyState attribute to CONNECTING.
561
+ this.#readyState = this.CONNECTING
562
+
563
+ // [spec] Fire an event named `error` at the EventSource object.
564
+ const errorEvent = new ErrorEvent('error', {code, message})
565
+ this.#onError?.(errorEvent)
566
+ this.dispatchEvent(errorEvent)
567
+
568
+ // [spec] Wait a delay equal to the reconnection time of the event source.
569
+ this.#reconnectTimer = setTimeout(this.#reconnect, this.#reconnectInterval)
570
+ }
571
+
572
+ /**
573
+ * Reconnects to the EventSource endpoint after a disconnect/failure
574
+ *
575
+ * @internal
576
+ */
577
+ #reconnect = () => {
578
+ this.#reconnectTimer = undefined
579
+
580
+ // [spec] If the EventSource's readyState attribute is not set to CONNECTING, then return.
581
+ if (this.#readyState !== this.CONNECTING) {
582
+ return
583
+ }
584
+
585
+ this.#connect()
586
+ }
587
+ }
588
+
589
+ // Provides a way to detect that the EventSource implementation supports passing `fetch`
590
+ // that can be used to customize the request, eg custom headers and similar.
591
+ Object.defineProperty(EventSource, Symbol.for('eventsource.supports-fetch-override'), {
592
+ value: true,
593
+ writable: false,
594
+ configurable: false,
595
+ enumerable: false,
596
+ })
597
+
598
+ /**
599
+ * According to spec, when constructing a URL:
600
+ * > 1. Let baseURL be environment's base URL, if environment is a Document object
601
+ * > 2. Return the result of applying the URL parser to url, with baseURL.
602
+ *
603
+ * Thus we should use `document.baseURI` if available, since it can be set through a base tag.
604
+ *
605
+ * @returns The base URL, if available - otherwise `undefined`
606
+ * @internal
607
+ */
608
+ function getBaseURL(): string | undefined {
609
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
610
+ const doc = 'document' in globalThis ? (globalThis as any).document : undefined
611
+ return doc && typeof doc === 'object' && 'baseURI' in doc && typeof doc.baseURI === 'string'
612
+ ? doc.baseURI
613
+ : undefined
614
+ }