@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,576 @@
1
+ /**
2
+ * EventSource/Server-Sent Events parser
3
+ * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
4
+ */
5
+ import {ParseError} from './errors.ts'
6
+ import type {EventSourceParser, ParserConfig} from './types.ts'
7
+
8
+ // ASCII codes used in the hot parsing paths.
9
+ const LF = 10
10
+ const CR = 13
11
+ const SPACE = 32
12
+ const MAX_FIELD_PREFIX_LENGTH = 6
13
+
14
+ /**
15
+ * Creates a new EventSource parser.
16
+ *
17
+ * @param config - Parser configuration. Accepts callbacks (see {@link ParserCallbacks})
18
+ * and options like `maxBufferSize` (see {@link ParserConfig}).
19
+ *
20
+ * @returns A new EventSource parser, with `feed` and `reset` methods.
21
+ * @public
22
+ */
23
+ export function createParser(config: ParserConfig): EventSourceParser {
24
+ if (typeof config === 'function') {
25
+ throw new TypeError(
26
+ '`config` must be an object, got a function instead. Did you mean `createParser({onEvent: fn})`?',
27
+ )
28
+ }
29
+
30
+ const {maxBufferSize, onComment, onError, onEvent, onId, onRetry} = config
31
+
32
+ // Trailing bytes from prior `feed()` calls that did not yet form a complete line.
33
+ // Stored as an array of fragments and only joined when a line terminator arrives.
34
+ // Concatenating per-feed (`prefix + chunk`) is O(N²) when a single SSE line spans
35
+ // many chunks (e.g. a large `data:` payload streamed in tiny slices, or an MCP-style
36
+ // server that emits one giant content block). Buffering as fragments + joining once
37
+ // makes the same workload linear.
38
+ const pendingFragments: string[] = []
39
+
40
+ // Running total of `pendingFragments` lengths, kept in sync with the array so the
41
+ // `maxBufferSize` check doesn't have to walk the fragment list on every feed.
42
+ let pendingFragmentsLength = 0
43
+
44
+ // Empty or partial leading BOM; undefined once the BOM check is complete.
45
+ let bomPrefix: string | undefined = ''
46
+ let id: string | undefined
47
+ let data = ''
48
+ let dataLines = 0
49
+ let eventType: string | undefined
50
+
51
+ // Set after a `maxBufferSize` overflow. Once tripped, `feed()` throws until
52
+ // `reset()` is called — see the comment on `maxBufferSize` in `ParserConfig`.
53
+ let terminated = false
54
+ let skippingLine = false
55
+
56
+ // Set when a line (parsed or discarded) was terminated by a trailing `\r` at the very
57
+ // end of a chunk. That `\r` is ambiguous: it may be a bare-CR terminator or the first
58
+ // half of a `\r\n` whose `\n` lands in the next chunk. The line itself is complete
59
+ // either way, but we must remember to swallow a single leading `\n` from the next
60
+ // chunk so the pair is treated as one terminator rather than a blank line (which
61
+ // would dispatch an event prematurely).
62
+ let skipNextLineFeed = false
63
+
64
+ /**
65
+ * Feeds a chunk of the SSE stream to the parser. Any trailing bytes that do
66
+ * not yet form a complete line are held back and prepended to the next chunk,
67
+ * so callers can pass arbitrary slices of the stream without worrying about
68
+ * line boundaries.
69
+ *
70
+ * Per the SSE spec, one leading UTF-8 BOM is stripped before parsing,
71
+ * even when split across chunks. This handles both the raw 3-byte form (0xEF 0xBB
72
+ * 0xBF) and a single decoded U+FEFF, so a leading BOM is ignored regardless of
73
+ * how the caller decoded the bytes.
74
+ *
75
+ * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
76
+ */
77
+ function feed(chunk: string) {
78
+ if (terminated) {
79
+ throw new Error(
80
+ 'Cannot feed parser: it was terminated after exceeding the configured max buffer size. Call `reset()` to resume parsing.',
81
+ )
82
+ }
83
+
84
+ if (bomPrefix !== undefined) {
85
+ chunk = bomPrefix + chunk
86
+ // Wait only while the input could still be a raw BOM. Empty chunks must
87
+ // not finish the check, and a mismatched prefix must remain ordinary input.
88
+ if (chunk === '' || chunk === '\xEF' || chunk === '\xEF\xBB') {
89
+ bomPrefix = chunk
90
+ return
91
+ }
92
+ bomPrefix = undefined
93
+ // Strip exactly one leading BOM, in decoded or raw byte-valued form.
94
+ chunk = chunk.replace(/^(?:\uFEFF|\xEF\xBB\xBF)/, '')
95
+ }
96
+
97
+ // Rare resume states from a prior chunk boundary: a pending `\r\n` split or a
98
+ // line being discarded. Single combined check keeps the hot path to one branch;
99
+ // the actual handling lives out-of-line in `resumeAfterSkip`.
100
+ if (skippingLine || skipNextLineFeed) {
101
+ chunk = resumeAfterSkip(chunk)
102
+ if (!chunk) {
103
+ return
104
+ }
105
+ }
106
+
107
+ // Hot path: no buffered prefix from a prior partial line. Hand the chunk
108
+ // straight to `processLines`, exactly like the original implementation.
109
+ // Zero new work in the common case (every chunk ends with `\n\n`).
110
+ if (!pendingFragments.length) {
111
+ const trailing = processLines(chunk)
112
+ if (trailing !== '') {
113
+ storeTrailing(trailing)
114
+ }
115
+ checkForBufferOverflow()
116
+ return
117
+ }
118
+
119
+ // We have a buffered prefix. If this chunk also has no terminator, append
120
+ // to the buffer without concatenating — that's the O(N²) trap we're
121
+ // avoiding (large single `data:` payload split across many tiny chunks).
122
+ if (chunk.indexOf('\n') === -1 && chunk.indexOf('\r') === -1) {
123
+ if (pendingFragmentsLength < MAX_FIELD_PREFIX_LENGTH) {
124
+ const head =
125
+ pendingFragments.join('') +
126
+ chunk.slice(0, MAX_FIELD_PREFIX_LENGTH - pendingFragmentsLength)
127
+ if (!shouldBufferTrailing(head)) {
128
+ pendingFragments.length = 0
129
+ pendingFragmentsLength = 0
130
+ skippingLine = true
131
+ return
132
+ }
133
+ }
134
+ pendingFragments.push(chunk)
135
+ pendingFragmentsLength += chunk.length
136
+ checkForBufferOverflow()
137
+ return
138
+ }
139
+
140
+ // Terminator arrived. Join the accumulated fragments + this chunk once,
141
+ // process, and buffer any new trailing partial line.
142
+ pendingFragments.push(chunk)
143
+ const input = pendingFragments.join('')
144
+ pendingFragments.length = 0
145
+ pendingFragmentsLength = 0
146
+ storeTrailing(processLines(input))
147
+ checkForBufferOverflow()
148
+ }
149
+
150
+ // Out-of-line handler for the rare post-boundary states. `skipNextLineFeed` means a
151
+ // line (parsed or discarded) ended with a trailing `\r` at the previous chunk boundary,
152
+ // so a single leading `\n` must be swallowed to consume a split `\r\n` as one terminator.
153
+ // `skippingLine` means we are discarding an invalid line until its terminator arrives.
154
+ // The two states are mutually exclusive. Returns the remaining chunk to parse ('' if
155
+ // this chunk was fully consumed).
156
+ function resumeAfterSkip(chunk: string): string {
157
+ if (chunk.length === 0) {
158
+ return chunk
159
+ }
160
+
161
+ if (skipNextLineFeed) {
162
+ skipNextLineFeed = false
163
+ return chunk.charCodeAt(0) === LF ? chunk.slice(1) : chunk
164
+ }
165
+
166
+ const crIndex = chunk.indexOf('\r')
167
+ const lfIndex = chunk.indexOf('\n')
168
+ const lineEnd =
169
+ crIndex === -1 ? lfIndex : lfIndex === -1 ? crIndex : crIndex < lfIndex ? crIndex : lfIndex
170
+
171
+ if (lineEnd === -1) {
172
+ return ''
173
+ }
174
+
175
+ // Trailing `\r` at the very end of the chunk: defer the CRLF/CR decision to the
176
+ // next chunk (see `skipNextLineFeed`) rather than greedily consuming just the `\r`.
177
+ if (lineEnd === chunk.length - 1 && chunk.charCodeAt(lineEnd) === CR) {
178
+ skippingLine = false
179
+ skipNextLineFeed = true
180
+ return ''
181
+ }
182
+
183
+ skippingLine = false
184
+ return chunk.slice(
185
+ lineEnd + (chunk.charCodeAt(lineEnd) === CR && chunk.charCodeAt(lineEnd + 1) === LF ? 2 : 1),
186
+ )
187
+ }
188
+
189
+ function storeTrailing(trailing: string) {
190
+ if (!trailing) return
191
+
192
+ // A trailing that ends with `\r` is not a partial line: the `\r` terminates it,
193
+ // and only the CRLF-vs-CR ambiguity remains (the matching `\n` may start the next
194
+ // chunk). Parse the completed line now — buffering it could grow unbounded through
195
+ // the no-terminator append path, and discarding it would eat the terminator and
196
+ // make skip mode swallow the next line.
197
+ if (trailing.charCodeAt(trailing.length - 1) === CR) {
198
+ parseLine(trailing, 0, trailing.length - 1)
199
+ if (checkForBufferOverflow()) return
200
+ skipNextLineFeed = true
201
+ return
202
+ }
203
+
204
+ if (shouldBufferTrailing(trailing)) {
205
+ pendingFragments.push(trailing)
206
+ pendingFragmentsLength = trailing.length
207
+ return
208
+ }
209
+
210
+ skippingLine = true
211
+ }
212
+
213
+ function shouldBufferTrailing(trailing: string) {
214
+ const firstCharCode = trailing.charCodeAt(0)
215
+ return (
216
+ (firstCharCode === 58 && !!onComment) ||
217
+ (firstCharCode === 100 && isPotentialField(trailing, 'data')) ||
218
+ (firstCharCode === 101 && isPotentialField(trailing, 'event')) ||
219
+ (firstCharCode === 105 && isPotentialField(trailing, 'id')) ||
220
+ (firstCharCode === 114 && isPotentialField(trailing, 'retry'))
221
+ )
222
+ }
223
+
224
+ // Returns true when parsing must stop for the current chunk (the parser was already
225
+ // terminated or has just overflowed), and false when processing can continue.
226
+ function checkForBufferOverflow() {
227
+ if (terminated) return true
228
+ if (maxBufferSize === undefined) return false
229
+ if (pendingFragmentsLength + data.length <= maxBufferSize) return false
230
+
231
+ terminated = true
232
+ pendingFragments.length = 0
233
+ pendingFragmentsLength = 0
234
+ id = undefined
235
+ data = ''
236
+ dataLines = 0
237
+ eventType = undefined
238
+ skippingLine = false
239
+ skipNextLineFeed = false
240
+ onError?.(
241
+ new ParseError(`Buffered data exceeded max buffer size of ${maxBufferSize} characters`, {
242
+ type: 'max-buffer-size-exceeded',
243
+ }),
244
+ )
245
+ return true
246
+ }
247
+
248
+ /**
249
+ * Splits `chunk` into SSE lines and dispatches each to the appropriate handler.
250
+ * Returns any trailing bytes that did not terminate with a line break, so the
251
+ * caller can prepend them to the next chunk.
252
+ *
253
+ * The SSE spec permits three line terminators: `\n`, `\r`, and `\r\n`. Real-world
254
+ * streams almost always use plain `\n`, so we take a fast path when no `\r` is
255
+ * present in the chunk. The slow path is spec-correct but does more work per line.
256
+ */
257
+ function processLines(chunk: string): string {
258
+ let searchIndex = 0
259
+
260
+ // Fast path: LF-only chunk (the common case for typical SSE servers).
261
+ // We can scan forward with a single `indexOf('\n')` per line and inline
262
+ // the hot-path branches for `data:` and `event:` without the CR bookkeeping
263
+ // the slow path needs.
264
+ if (chunk.indexOf('\r') === -1) {
265
+ let lfIndex = chunk.indexOf('\n', searchIndex)
266
+ while (lfIndex !== -1) {
267
+ // Blank line: end-of-event marker. Dispatch the accumulated event (if any)
268
+ // and reset the buffered fields. This is hoisted out of `parseLine` because
269
+ // it's the single most common line shape after `data:` lines.
270
+ if (searchIndex === lfIndex) {
271
+ if (id !== undefined) {
272
+ onId?.(id)
273
+ }
274
+ if (dataLines > 0) {
275
+ onEvent?.({id, event: eventType, data})
276
+ }
277
+ id = undefined
278
+ data = ''
279
+ dataLines = 0
280
+ eventType = undefined
281
+ searchIndex = lfIndex + 1
282
+ lfIndex = chunk.indexOf('\n', searchIndex)
283
+ continue
284
+ }
285
+ const firstCharCode = chunk.charCodeAt(searchIndex)
286
+ if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
287
+ // `data:` line — append the value to the event's data buffer.
288
+ // 'data:'.length === 5, 'data: '.length === 6
289
+ const valueStart =
290
+ chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5
291
+ const value = chunk.slice(valueStart, lfIndex)
292
+ // Fast path within a fast path: if this is the first data line AND the
293
+ // next char is another LF (i.e. `data:foo\n\n`), dispatch immediately
294
+ // without ever writing to the `data` buffer. This is the shape of a
295
+ // typical single-line SSE event (ChatGPT-style streams, etc.) and is
296
+ // hot enough to be worth the duplication.
297
+ if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
298
+ if (id !== undefined) {
299
+ onId?.(id)
300
+ }
301
+ onEvent?.({id, event: eventType, data: value})
302
+ id = undefined
303
+ data = ''
304
+ eventType = undefined
305
+ searchIndex = lfIndex + 2
306
+ lfIndex = chunk.indexOf('\n', searchIndex)
307
+ continue
308
+ }
309
+ // Multi-line data: concatenate with newline separator per spec.
310
+ data = dataLines === 0 ? value : `${data}\n${value}`
311
+ dataLines++
312
+ } else if (isEventPrefix(chunk, searchIndex, firstCharCode)) {
313
+ // `event:` line — set the event type for the next dispatch. Per spec,
314
+ // an empty value resets `event type` to its default (undefined here).
315
+ // 'event:'.length === 6, 'event: '.length === 7
316
+ eventType =
317
+ chunk.slice(
318
+ chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
319
+ lfIndex,
320
+ ) || undefined
321
+ } else {
322
+ // Everything else: `id:`, `retry:`, comment lines (`:` prefix), unknown
323
+ // fields, or malformed lines. These are rarer and go through the full
324
+ // per-line parser, which handles the SSE field grammar in detail.
325
+ parseLine(chunk, searchIndex, lfIndex)
326
+ }
327
+ if (checkForBufferOverflow()) return ''
328
+ searchIndex = lfIndex + 1
329
+ lfIndex = chunk.indexOf('\n', searchIndex)
330
+ }
331
+ return chunk.slice(searchIndex)
332
+ }
333
+
334
+ // Slow path: the chunk contains at least one `\r`, so lines may be terminated
335
+ // by `\r`, `\n`, or `\r\n`. We locate the next terminator by looking at both
336
+ // the nearest `\r` and `\n` and picking whichever comes first.
337
+ let crIndex = chunk.indexOf('\r', searchIndex)
338
+ let lfIndex = chunk.indexOf('\n', searchIndex)
339
+ while (searchIndex < chunk.length) {
340
+ let lineEnd = -1
341
+ if (crIndex !== -1 && lfIndex !== -1) {
342
+ lineEnd = crIndex < lfIndex ? crIndex : lfIndex
343
+ } else if (crIndex !== -1) {
344
+ // A trailing `\r` at the very end of the chunk is ambiguous: it could be
345
+ // a bare-CR terminator, or the first half of a `\r\n` whose `\n` arrives
346
+ // in the next chunk. Defer until we see more input.
347
+ if (crIndex === chunk.length - 1) {
348
+ lineEnd = -1
349
+ } else {
350
+ lineEnd = crIndex
351
+ }
352
+ } else if (lfIndex !== -1) {
353
+ lineEnd = lfIndex
354
+ }
355
+
356
+ if (lineEnd === -1) {
357
+ break
358
+ }
359
+
360
+ parseLine(chunk, searchIndex, lineEnd)
361
+ if (checkForBufferOverflow()) return ''
362
+ searchIndex = lineEnd + 1
363
+ // If we just consumed a `\r` and the next char is `\n`, skip it so the
364
+ // pair is treated as a single terminator rather than an empty line.
365
+ if (chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF) {
366
+ searchIndex++
367
+ }
368
+
369
+ // Reuse unconsumed matches and failed searches instead of rescanning the suffix.
370
+ if (crIndex !== -1 && crIndex < searchIndex) {
371
+ crIndex = chunk.indexOf('\r', searchIndex)
372
+ }
373
+ if (lfIndex !== -1 && lfIndex < searchIndex) {
374
+ lfIndex = chunk.indexOf('\n', searchIndex)
375
+ }
376
+ }
377
+
378
+ return chunk.slice(searchIndex)
379
+ }
380
+
381
+ function parseLine(chunk: string, start: number, end: number) {
382
+ if (start === end) {
383
+ dispatchEvent()
384
+ return
385
+ }
386
+
387
+ const firstCharCode = chunk.charCodeAt(start)
388
+
389
+ if (isDataPrefix(chunk, start, firstCharCode)) {
390
+ // 'data:'.length === 5, 'data: '.length === 6
391
+ const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5
392
+ const value = chunk.slice(valueStart, end)
393
+ data = dataLines === 0 ? value : `${data}\n${value}`
394
+ dataLines++
395
+ return
396
+ }
397
+
398
+ if (isEventPrefix(chunk, start, firstCharCode)) {
399
+ // 'event:'.length === 6, 'event: '.length === 7
400
+ eventType =
401
+ chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || undefined
402
+ return
403
+ }
404
+
405
+ // Fast path for "id:" — 'i' = 105, 'd' = 100, ':' = 58
406
+ if (
407
+ firstCharCode === 105 &&
408
+ chunk.charCodeAt(start + 1) === 100 &&
409
+ chunk.charCodeAt(start + 2) === 58
410
+ ) {
411
+ // 'id:'.length === 3, 'id: '.length === 4
412
+ const value = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end)
413
+ // If the field value does not contain U+0000 NULL, then set the `ID` buffer to
414
+ // the field value. Otherwise, ignore the field.
415
+ if (!value.includes('\0')) id = value
416
+ return
417
+ }
418
+
419
+ // Comment line — ':' = 58
420
+ if (firstCharCode === 58) {
421
+ if (onComment) {
422
+ const line = chunk.slice(start, end)
423
+ // skip ':' (+1), or ': ' (+2) when a space follows
424
+ onComment(line.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1))
425
+ }
426
+ return
427
+ }
428
+
429
+ const line = chunk.slice(start, end)
430
+ const fieldSeparatorIndex = line.indexOf(':')
431
+ if (fieldSeparatorIndex === -1) {
432
+ processField(line, '', line)
433
+ return
434
+ }
435
+
436
+ const field = line.slice(0, fieldSeparatorIndex)
437
+ // skip ':' (+1), or ': ' (+2) when a space follows
438
+ const offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1
439
+ const value = line.slice(fieldSeparatorIndex + offset)
440
+ processField(field, value, line)
441
+ }
442
+
443
+ function processField(field: string, value: string, line: string) {
444
+ // Field names must be compared literally, with no case folding performed.
445
+ switch (field) {
446
+ case 'event':
447
+ // Set the `event type` buffer to field value
448
+ eventType = value || undefined
449
+ break
450
+ case 'data':
451
+ data = dataLines === 0 ? value : `${data}\n${value}`
452
+ dataLines++
453
+ break
454
+ case 'id':
455
+ // If the field value does not contain U+0000 NULL, then set the `ID` buffer to
456
+ // the field value. Otherwise, ignore the field.
457
+ if (!value.includes('\0')) id = value
458
+ break
459
+ case 'retry':
460
+ // If the field value consists of only ASCII digits, then interpret the field value as an
461
+ // integer in base ten, and set the event stream's reconnection time to that integer.
462
+ // Otherwise, ignore the field.
463
+ if (/^\d+$/.test(value)) {
464
+ onRetry?.(parseInt(value, 10))
465
+ } else {
466
+ onError?.(
467
+ new ParseError(`Invalid \`retry\` value: "${value}"`, {
468
+ type: 'invalid-retry',
469
+ value,
470
+ line,
471
+ }),
472
+ )
473
+ }
474
+ break
475
+ default:
476
+ // Otherwise, the field is ignored.
477
+ onError?.(
478
+ new ParseError(
479
+ `Unknown field "${field.length > 20 ? `${field.slice(0, 20)}…` : field}"`,
480
+ {type: 'unknown-field', field, value, line},
481
+ ),
482
+ )
483
+ break
484
+ }
485
+ }
486
+
487
+ function dispatchEvent() {
488
+ if (id !== undefined) {
489
+ onId?.(id)
490
+ }
491
+
492
+ if (dataLines > 0) {
493
+ onEvent?.({
494
+ id,
495
+ event: eventType,
496
+ data,
497
+ })
498
+ }
499
+
500
+ id = undefined
501
+ data = ''
502
+ dataLines = 0
503
+ eventType = undefined
504
+ }
505
+
506
+ function reset(options: {consume?: boolean} = {}) {
507
+ if (options.consume && pendingFragments.length > 0) {
508
+ const incompleteLine = pendingFragments.join('')
509
+ parseLine(incompleteLine, 0, incompleteLine.length)
510
+ }
511
+
512
+ bomPrefix = ''
513
+ id = undefined
514
+ data = ''
515
+ dataLines = 0
516
+ eventType = undefined
517
+ pendingFragments.length = 0
518
+ pendingFragmentsLength = 0
519
+ terminated = false
520
+ skippingLine = false
521
+ skipNextLineFeed = false
522
+ }
523
+
524
+ return {feed, reset}
525
+ }
526
+
527
+ /**
528
+ * Checks if `chunk` starts with the literal `data:` at index `i`.
529
+ *
530
+ * Equivalent to `chunk.startsWith('data:', i)`, but benchmarks show this
531
+ * hand-unrolled char-code comparison is ~20% faster on common event types.
532
+ * The caller passes `firstCharCode` (the code at `i`) so it can be reused
533
+ * across prefix checks.
534
+ *
535
+ * ASCII: 'd' = 100, 'a' = 97, 't' = 116, 'a' = 97, ':' = 58
536
+ */
537
+ function isDataPrefix(chunk: string, i: number, firstCharCode: number): boolean {
538
+ return (
539
+ firstCharCode === 100 &&
540
+ chunk.charCodeAt(i + 1) === 97 &&
541
+ chunk.charCodeAt(i + 2) === 116 &&
542
+ chunk.charCodeAt(i + 3) === 97 &&
543
+ chunk.charCodeAt(i + 4) === 58
544
+ )
545
+ }
546
+
547
+ /**
548
+ * Checks if `chunk` starts with the literal `event:` at index `i`.
549
+ *
550
+ * See {@link isDataPrefix} for why this is hand-unrolled rather than using
551
+ * `String.prototype.startsWith`.
552
+ *
553
+ * ASCII: 'e' = 101, 'v' = 118, 'e' = 101, 'n' = 110, 't' = 116, ':' = 58
554
+ */
555
+ function isEventPrefix(chunk: string, i: number, firstCharCode: number): boolean {
556
+ return (
557
+ firstCharCode === 101 &&
558
+ chunk.charCodeAt(i + 1) === 118 &&
559
+ chunk.charCodeAt(i + 2) === 101 &&
560
+ chunk.charCodeAt(i + 3) === 110 &&
561
+ chunk.charCodeAt(i + 4) === 116 &&
562
+ chunk.charCodeAt(i + 5) === 58
563
+ )
564
+ }
565
+
566
+ function isPotentialField(line: string, field: string): boolean {
567
+ let i = 1
568
+ while (i < line.length && i < field.length) {
569
+ if (line.charCodeAt(i) !== field.charCodeAt(i)) {
570
+ return false
571
+ }
572
+ i++
573
+ }
574
+
575
+ return line.length <= field.length || line.charCodeAt(field.length) === 58
576
+ }
@@ -0,0 +1,118 @@
1
+ import {createParser} from './parse.ts'
2
+ import type {EventSourceMessage, EventSourceParser} from './types.ts'
3
+
4
+ /**
5
+ * Options for the EventSourceParserStream.
6
+ *
7
+ * @public
8
+ */
9
+ export interface StreamOptions {
10
+ /**
11
+ * Callback for an `id` field when a blank line ends an event block.
12
+ *
13
+ * Called once with the last valid `id` value in the block, whether or not the block contains a
14
+ * `data` field. When the block produces an event, `onId` is called before the event is enqueued.
15
+ * An empty `id` field is reported as an empty string. An `id` field containing U+0000 is ignored.
16
+ *
17
+ * @param id - The value of the last valid `id` field in the block
18
+ */
19
+ onId?: ((id: string) => void) | undefined
20
+
21
+ /**
22
+ * Behavior when a parsing error occurs.
23
+ *
24
+ * - A custom function can be provided to handle the error.
25
+ * - `'terminate'` will error the stream and stop parsing.
26
+ * - Any other value will ignore the error and continue parsing.
27
+ *
28
+ * @defaultValue `undefined`
29
+ */
30
+ onError?: ('terminate' | ((error: Error) => void)) | undefined
31
+
32
+ /**
33
+ * Callback for when a reconnection interval is sent from the server.
34
+ *
35
+ * @param retry - The number of milliseconds to wait before reconnecting.
36
+ */
37
+ onRetry?: ((retry: number) => void) | undefined
38
+
39
+ /**
40
+ * Callback for when a comment is encountered in the stream.
41
+ *
42
+ * @param comment - The comment encountered in the stream.
43
+ */
44
+ onComment?: ((comment: string) => void) | undefined
45
+
46
+ /**
47
+ * Maximum number of characters the parser is allowed to buffer across calls to `feed()`.
48
+ *
49
+ * When the limit is exceeded, the stream is always errored (regardless of the `onError`
50
+ * setting) since the underlying parser is unrecoverable without a `reset()`.
51
+ *
52
+ * @defaultValue `undefined` (unbounded)
53
+ */
54
+ maxBufferSize?: number | undefined
55
+ }
56
+
57
+ /**
58
+ * A TransformStream that ingests a stream of strings and produces a stream of `EventSourceMessage`.
59
+ *
60
+ * @example Basic usage
61
+ * ```
62
+ * const eventStream =
63
+ * response.body
64
+ * .pipeThrough(new TextDecoderStream())
65
+ * .pipeThrough(new EventSourceParserStream())
66
+ * ```
67
+ *
68
+ * @example Terminate stream on parsing errors
69
+ * ```
70
+ * const eventStream =
71
+ * response.body
72
+ * .pipeThrough(new TextDecoderStream())
73
+ * .pipeThrough(new EventSourceParserStream({onError: 'terminate'}))
74
+ * ```
75
+ *
76
+ * @public
77
+ */
78
+ export class EventSourceParserStream extends TransformStream<string, EventSourceMessage> {
79
+ constructor({onError, onRetry, onComment, onId, maxBufferSize}: StreamOptions = {}) {
80
+ let parser!: EventSourceParser
81
+
82
+ super({
83
+ start(controller) {
84
+ parser = createParser({
85
+ onEvent: (event) => {
86
+ controller.enqueue(event)
87
+ },
88
+ onError(error) {
89
+ if (typeof onError === 'function') {
90
+ onError(error)
91
+ }
92
+
93
+ // `max-buffer-size-exceeded` is fatal — the parser is unusable until
94
+ // `reset()`, which the stream wrapper has no way to call. Always
95
+ // terminate the stream in that case so consumers see the meaningful
96
+ // `ParseError` instead of an opaque "cannot feed terminated parser"
97
+ // throw from the next chunk.
98
+ if (onError === 'terminate' || error.type === 'max-buffer-size-exceeded') {
99
+ controller.error(error)
100
+ }
101
+
102
+ // Ignore by default
103
+ },
104
+ onRetry,
105
+ onComment,
106
+ onId,
107
+ maxBufferSize,
108
+ })
109
+ },
110
+ transform(chunk) {
111
+ parser.feed(chunk)
112
+ },
113
+ })
114
+ }
115
+ }
116
+
117
+ export {type ErrorType, ParseError} from './errors.ts'
118
+ export type {EventSourceMessage} from './types.ts'