@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,439 @@
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
+
13
+ // oxlint-disable-next-line no-unused-vars
14
+ function noop(_arg: unknown) {
15
+ // intentional noop
16
+ }
17
+
18
+ /**
19
+ * Creates a new EventSource parser.
20
+ *
21
+ * @param config - Parser configuration. Accepts callbacks (see {@link ParserCallbacks})
22
+ * and options like `maxBufferSize` (see {@link ParserConfig}).
23
+ *
24
+ * @returns A new EventSource parser, with `feed` and `reset` methods.
25
+ * @public
26
+ */
27
+ export function createParser(config: ParserConfig): EventSourceParser {
28
+ if (typeof config === 'function') {
29
+ throw new TypeError(
30
+ '`config` must be an object, got a function instead. Did you mean `createParser({onEvent: fn})`?',
31
+ )
32
+ }
33
+
34
+ const {onEvent = noop, onError = noop, onRetry = noop, onComment, maxBufferSize} = config
35
+
36
+ // Trailing bytes from prior `feed()` calls that did not yet form a complete line.
37
+ // Stored as an array of fragments and only joined when a line terminator arrives.
38
+ // Concatenating per-feed (`prefix + chunk`) is O(N²) when a single SSE line spans
39
+ // many chunks (e.g. a large `data:` payload streamed in tiny slices, or an MCP-style
40
+ // server that emits one giant content block). Buffering as fragments + joining once
41
+ // makes the same workload linear.
42
+ const pendingFragments: string[] = []
43
+
44
+ // Running total of `pendingFragments` lengths, kept in sync with the array so the
45
+ // `maxBufferSize` check doesn't have to walk the fragment list on every feed.
46
+ let pendingFragmentsLength = 0
47
+
48
+ let isFirstChunk = true
49
+ let id: string | undefined
50
+ let data = ''
51
+ let dataLines = 0
52
+ let eventType: string | undefined
53
+
54
+ // Set after a `maxBufferSize` overflow. Once tripped, `feed()` throws until
55
+ // `reset()` is called — see the comment on `maxBufferSize` in `ParserConfig`.
56
+ let terminated = false
57
+
58
+ /**
59
+ * Feeds a chunk of the SSE stream to the parser. Any trailing bytes that do
60
+ * not yet form a complete line are held back and prepended to the next chunk,
61
+ * so callers can pass arbitrary slices of the stream without worrying about
62
+ * line boundaries.
63
+ *
64
+ * Per the SSE spec, a UTF-8 BOM (0xEF 0xBB 0xBF) at the start of the very
65
+ * first chunk is stripped before parsing.
66
+ *
67
+ * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
68
+ */
69
+ function feed(chunk: string) {
70
+ if (terminated) {
71
+ throw new Error(
72
+ 'Cannot feed parser: it was terminated after exceeding the configured max buffer size. Call `reset()` to resume parsing.',
73
+ )
74
+ }
75
+
76
+ if (isFirstChunk) {
77
+ isFirstChunk = false
78
+ // Match and strip UTF-8 BOM from the start of the stream, if present.
79
+ // (Per the spec, this is only valid at the very start of the stream)
80
+ if (
81
+ chunk.charCodeAt(0) === 0xef &&
82
+ chunk.charCodeAt(1) === 0xbb &&
83
+ chunk.charCodeAt(2) === 0xbf
84
+ ) {
85
+ chunk = chunk.slice(3)
86
+ }
87
+ }
88
+
89
+ // Hot path: no buffered prefix from a prior partial line. Hand the chunk
90
+ // straight to `processLines`, exactly like the original implementation.
91
+ // Zero new work in the common case (every chunk ends with `\n\n`).
92
+ if (pendingFragments.length === 0) {
93
+ const trailing = processLines(chunk)
94
+ if (trailing !== '') {
95
+ pendingFragments.push(trailing)
96
+ pendingFragmentsLength = trailing.length
97
+ }
98
+ checkBufferSize()
99
+ return
100
+ }
101
+
102
+ // We have a buffered prefix. If this chunk also has no terminator, append
103
+ // to the buffer without concatenating — that's the O(N²) trap we're
104
+ // avoiding (large single `data:` payload split across many tiny chunks).
105
+ if (chunk.indexOf('\n') === -1 && chunk.indexOf('\r') === -1) {
106
+ pendingFragments.push(chunk)
107
+ pendingFragmentsLength += chunk.length
108
+ checkBufferSize()
109
+ return
110
+ }
111
+
112
+ // Terminator arrived. Join the accumulated fragments + this chunk once,
113
+ // process, and buffer any new trailing partial line.
114
+ pendingFragments.push(chunk)
115
+ const input = pendingFragments.join('')
116
+ pendingFragments.length = 0
117
+ pendingFragmentsLength = 0
118
+ const trailing = processLines(input)
119
+ if (trailing !== '') {
120
+ pendingFragments.push(trailing)
121
+ pendingFragmentsLength = trailing.length
122
+ }
123
+ checkBufferSize()
124
+ }
125
+
126
+ function checkBufferSize() {
127
+ if (maxBufferSize === undefined) return
128
+ if (pendingFragmentsLength + data.length <= maxBufferSize) return
129
+
130
+ terminated = true
131
+ pendingFragments.length = 0
132
+ pendingFragmentsLength = 0
133
+ id = undefined
134
+ data = ''
135
+ dataLines = 0
136
+ eventType = undefined
137
+ onError(
138
+ new ParseError(`Buffered data exceeded max buffer size of ${maxBufferSize} characters`, {
139
+ type: 'max-buffer-size-exceeded',
140
+ }),
141
+ )
142
+ }
143
+
144
+ /**
145
+ * Splits `chunk` into SSE lines and dispatches each to the appropriate handler.
146
+ * Returns any trailing bytes that did not terminate with a line break, so the
147
+ * caller can prepend them to the next chunk.
148
+ *
149
+ * The SSE spec permits three line terminators: `\n`, `\r`, and `\r\n`. Real-world
150
+ * streams almost always use plain `\n`, so we take a fast path when no `\r` is
151
+ * present in the chunk. The slow path is spec-correct but does more work per line.
152
+ */
153
+ function processLines(chunk: string): string {
154
+ let searchIndex = 0
155
+
156
+ // Fast path: LF-only chunk (the common case for typical SSE servers).
157
+ // We can scan forward with a single `indexOf('\n')` per line and inline
158
+ // the hot-path branches for `data:` and `event:` without the CR bookkeeping
159
+ // the slow path needs.
160
+ if (chunk.indexOf('\r') === -1) {
161
+ let lfIndex = chunk.indexOf('\n', searchIndex)
162
+ while (lfIndex !== -1) {
163
+ // Blank line: end-of-event marker. Dispatch the accumulated event (if any)
164
+ // and reset the buffered fields. This is hoisted out of `parseLine` because
165
+ // it's the single most common line shape after `data:` lines.
166
+ if (searchIndex === lfIndex) {
167
+ if (dataLines > 0) {
168
+ onEvent({id, event: eventType, data})
169
+ }
170
+ id = undefined
171
+ data = ''
172
+ dataLines = 0
173
+ eventType = undefined
174
+ searchIndex = lfIndex + 1
175
+ lfIndex = chunk.indexOf('\n', searchIndex)
176
+ continue
177
+ }
178
+ const firstCharCode = chunk.charCodeAt(searchIndex)
179
+ if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
180
+ // `data:` line — append the value to the event's data buffer.
181
+ // 'data:'.length === 5, 'data: '.length === 6
182
+ const valueStart =
183
+ chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5
184
+ const value = chunk.slice(valueStart, lfIndex)
185
+ // Fast path within a fast path: if this is the first data line AND the
186
+ // next char is another LF (i.e. `data:foo\n\n`), dispatch immediately
187
+ // without ever writing to the `data` buffer. This is the shape of a
188
+ // typical single-line SSE event (ChatGPT-style streams, etc.) and is
189
+ // hot enough to be worth the duplication.
190
+ if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
191
+ onEvent({id, event: eventType, data: value})
192
+ id = undefined
193
+ data = ''
194
+ eventType = undefined
195
+ searchIndex = lfIndex + 2
196
+ lfIndex = chunk.indexOf('\n', searchIndex)
197
+ continue
198
+ }
199
+ // Multi-line data: concatenate with newline separator per spec.
200
+ data = dataLines === 0 ? value : `${data}\n${value}`
201
+ dataLines++
202
+ } else if (isEventPrefix(chunk, searchIndex, firstCharCode)) {
203
+ // `event:` line — set the event type for the next dispatch. Per spec,
204
+ // an empty value resets `event type` to its default (undefined here).
205
+ // 'event:'.length === 6, 'event: '.length === 7
206
+ eventType =
207
+ chunk.slice(
208
+ chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
209
+ lfIndex,
210
+ ) || undefined
211
+ } else {
212
+ // Everything else: `id:`, `retry:`, comment lines (`:` prefix), unknown
213
+ // fields, or malformed lines. These are rarer and go through the full
214
+ // per-line parser, which handles the SSE field grammar in detail.
215
+ parseLine(chunk, searchIndex, lfIndex)
216
+ }
217
+ searchIndex = lfIndex + 1
218
+ lfIndex = chunk.indexOf('\n', searchIndex)
219
+ }
220
+ return chunk.slice(searchIndex)
221
+ }
222
+
223
+ // Slow path: the chunk contains at least one `\r`, so lines may be terminated
224
+ // by `\r`, `\n`, or `\r\n`. We locate the next terminator by looking at both
225
+ // the nearest `\r` and `\n` and picking whichever comes first.
226
+ while (searchIndex < chunk.length) {
227
+ const crIndex = chunk.indexOf('\r', searchIndex)
228
+ const lfIndex = chunk.indexOf('\n', searchIndex)
229
+
230
+ let lineEnd = -1
231
+ if (crIndex !== -1 && lfIndex !== -1) {
232
+ lineEnd = crIndex < lfIndex ? crIndex : lfIndex
233
+ } else if (crIndex !== -1) {
234
+ // A trailing `\r` at the very end of the chunk is ambiguous: it could be
235
+ // a bare-CR terminator, or the first half of a `\r\n` whose `\n` arrives
236
+ // in the next chunk. Defer until we see more input.
237
+ if (crIndex === chunk.length - 1) {
238
+ lineEnd = -1
239
+ } else {
240
+ lineEnd = crIndex
241
+ }
242
+ } else if (lfIndex !== -1) {
243
+ lineEnd = lfIndex
244
+ }
245
+
246
+ if (lineEnd === -1) {
247
+ break
248
+ }
249
+
250
+ parseLine(chunk, searchIndex, lineEnd)
251
+ searchIndex = lineEnd + 1
252
+ // If we just consumed a `\r` and the next char is `\n`, skip it so the
253
+ // pair is treated as a single terminator rather than an empty line.
254
+ if (chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF) {
255
+ searchIndex++
256
+ }
257
+ }
258
+
259
+ return chunk.slice(searchIndex)
260
+ }
261
+
262
+ function parseLine(chunk: string, start: number, end: number) {
263
+ if (start === end) {
264
+ dispatchEvent()
265
+ return
266
+ }
267
+
268
+ const firstCharCode = chunk.charCodeAt(start)
269
+
270
+ if (isDataPrefix(chunk, start, firstCharCode)) {
271
+ // 'data:'.length === 5, 'data: '.length === 6
272
+ const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5
273
+ const value = chunk.slice(valueStart, end)
274
+ data = dataLines === 0 ? value : `${data}\n${value}`
275
+ dataLines++
276
+ return
277
+ }
278
+
279
+ if (isEventPrefix(chunk, start, firstCharCode)) {
280
+ // 'event:'.length === 6, 'event: '.length === 7
281
+ eventType =
282
+ chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || undefined
283
+ return
284
+ }
285
+
286
+ // Fast path for "id:" — 'i' = 105, 'd' = 100, ':' = 58
287
+ if (
288
+ firstCharCode === 105 &&
289
+ chunk.charCodeAt(start + 1) === 100 &&
290
+ chunk.charCodeAt(start + 2) === 58
291
+ ) {
292
+ // 'id:'.length === 3, 'id: '.length === 4
293
+ const value = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end)
294
+ // If the field value does not contain U+0000 NULL, then set the `ID` buffer to
295
+ // the field value. Otherwise, ignore the field.
296
+ if (!value.includes('\0')) id = value
297
+ return
298
+ }
299
+
300
+ // Comment line — ':' = 58
301
+ if (firstCharCode === 58) {
302
+ if (onComment) {
303
+ const line = chunk.slice(start, end)
304
+ // skip ':' (+1), or ': ' (+2) when a space follows
305
+ onComment(line.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1))
306
+ }
307
+ return
308
+ }
309
+
310
+ const line = chunk.slice(start, end)
311
+ const fieldSeparatorIndex = line.indexOf(':')
312
+ if (fieldSeparatorIndex === -1) {
313
+ processField(line, '', line)
314
+ return
315
+ }
316
+
317
+ const field = line.slice(0, fieldSeparatorIndex)
318
+ // skip ':' (+1), or ': ' (+2) when a space follows
319
+ const offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1
320
+ const value = line.slice(fieldSeparatorIndex + offset)
321
+ processField(field, value, line)
322
+ }
323
+
324
+ function processField(field: string, value: string, line: string) {
325
+ // Field names must be compared literally, with no case folding performed.
326
+ switch (field) {
327
+ case 'event':
328
+ // Set the `event type` buffer to field value
329
+ eventType = value || undefined
330
+ break
331
+ case 'data':
332
+ data = dataLines === 0 ? value : `${data}\n${value}`
333
+ dataLines++
334
+ break
335
+ case 'id':
336
+ // If the field value does not contain U+0000 NULL, then set the `ID` buffer to
337
+ // the field value. Otherwise, ignore the field.
338
+ if (!value.includes('\0')) id = value
339
+ break
340
+ case 'retry':
341
+ // If the field value consists of only ASCII digits, then interpret the field value as an
342
+ // integer in base ten, and set the event stream's reconnection time to that integer.
343
+ // Otherwise, ignore the field.
344
+ if (/^\d+$/.test(value)) {
345
+ onRetry(parseInt(value, 10))
346
+ } else {
347
+ onError(
348
+ new ParseError(`Invalid \`retry\` value: "${value}"`, {
349
+ type: 'invalid-retry',
350
+ value,
351
+ line,
352
+ }),
353
+ )
354
+ }
355
+ break
356
+ default:
357
+ // Otherwise, the field is ignored.
358
+ onError(
359
+ new ParseError(
360
+ `Unknown field "${field.length > 20 ? `${field.slice(0, 20)}…` : field}"`,
361
+ {type: 'unknown-field', field, value, line},
362
+ ),
363
+ )
364
+ break
365
+ }
366
+ }
367
+
368
+ function dispatchEvent() {
369
+ if (dataLines > 0) {
370
+ onEvent({
371
+ id,
372
+ event: eventType,
373
+ data,
374
+ })
375
+ }
376
+
377
+ id = undefined
378
+ data = ''
379
+ dataLines = 0
380
+ eventType = undefined
381
+ }
382
+
383
+ function reset(options: {consume?: boolean} = {}) {
384
+ if (options.consume && pendingFragments.length > 0) {
385
+ const incompleteLine = pendingFragments.join('')
386
+ parseLine(incompleteLine, 0, incompleteLine.length)
387
+ }
388
+
389
+ isFirstChunk = true
390
+ id = undefined
391
+ data = ''
392
+ dataLines = 0
393
+ eventType = undefined
394
+ pendingFragments.length = 0
395
+ pendingFragmentsLength = 0
396
+ terminated = false
397
+ }
398
+
399
+ return {feed, reset}
400
+ }
401
+
402
+ /**
403
+ * Checks if `chunk` starts with the literal `data:` at index `i`.
404
+ *
405
+ * Equivalent to `chunk.startsWith('data:', i)`, but benchmarks show this
406
+ * hand-unrolled char-code comparison is ~20% faster on common event types.
407
+ * The caller passes `firstCharCode` (the code at `i`) so it can be reused
408
+ * across prefix checks.
409
+ *
410
+ * ASCII: 'd' = 100, 'a' = 97, 't' = 116, 'a' = 97, ':' = 58
411
+ */
412
+ function isDataPrefix(chunk: string, i: number, firstCharCode: number): boolean {
413
+ return (
414
+ firstCharCode === 100 &&
415
+ chunk.charCodeAt(i + 1) === 97 &&
416
+ chunk.charCodeAt(i + 2) === 116 &&
417
+ chunk.charCodeAt(i + 3) === 97 &&
418
+ chunk.charCodeAt(i + 4) === 58
419
+ )
420
+ }
421
+
422
+ /**
423
+ * Checks if `chunk` starts with the literal `event:` at index `i`.
424
+ *
425
+ * See {@link isDataPrefix} for why this is hand-unrolled rather than using
426
+ * `String.prototype.startsWith`.
427
+ *
428
+ * ASCII: 'e' = 101, 'v' = 118, 'e' = 101, 'n' = 110, 't' = 116, ':' = 58
429
+ */
430
+ function isEventPrefix(chunk: string, i: number, firstCharCode: number): boolean {
431
+ return (
432
+ firstCharCode === 101 &&
433
+ chunk.charCodeAt(i + 1) === 118 &&
434
+ chunk.charCodeAt(i + 2) === 101 &&
435
+ chunk.charCodeAt(i + 3) === 110 &&
436
+ chunk.charCodeAt(i + 4) === 116 &&
437
+ chunk.charCodeAt(i + 5) === 58
438
+ )
439
+ }
@@ -0,0 +1,107 @@
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
+ * Behavior when a parsing error occurs.
12
+ *
13
+ * - A custom function can be provided to handle the error.
14
+ * - `'terminate'` will error the stream and stop parsing.
15
+ * - Any other value will ignore the error and continue parsing.
16
+ *
17
+ * @defaultValue `undefined`
18
+ */
19
+ onError?: ('terminate' | ((error: Error) => void)) | undefined
20
+
21
+ /**
22
+ * Callback for when a reconnection interval is sent from the server.
23
+ *
24
+ * @param retry - The number of milliseconds to wait before reconnecting.
25
+ */
26
+ onRetry?: ((retry: number) => void) | undefined
27
+
28
+ /**
29
+ * Callback for when a comment is encountered in the stream.
30
+ *
31
+ * @param comment - The comment encountered in the stream.
32
+ */
33
+ onComment?: ((comment: string) => void) | undefined
34
+
35
+ /**
36
+ * Maximum number of characters the parser is allowed to buffer across calls to `feed()`.
37
+ * See {@link ParserConfig.maxBufferSize} for details.
38
+ *
39
+ * When the limit is exceeded, the stream is always errored (regardless of the `onError`
40
+ * setting) since the underlying parser is unrecoverable without a `reset()`.
41
+ *
42
+ * @defaultValue `undefined` (unbounded)
43
+ */
44
+ maxBufferSize?: number | undefined
45
+ }
46
+
47
+ /**
48
+ * A TransformStream that ingests a stream of strings and produces a stream of `EventSourceMessage`.
49
+ *
50
+ * @example Basic usage
51
+ * ```
52
+ * const eventStream =
53
+ * response.body
54
+ * .pipeThrough(new TextDecoderStream())
55
+ * .pipeThrough(new EventSourceParserStream())
56
+ * ```
57
+ *
58
+ * @example Terminate stream on parsing errors
59
+ * ```
60
+ * const eventStream =
61
+ * response.body
62
+ * .pipeThrough(new TextDecoderStream())
63
+ * .pipeThrough(new EventSourceParserStream({onError: 'terminate'}))
64
+ * ```
65
+ *
66
+ * @public
67
+ */
68
+ export class EventSourceParserStream extends TransformStream<string, EventSourceMessage> {
69
+ constructor({onError, onRetry, onComment, maxBufferSize}: StreamOptions = {}) {
70
+ let parser!: EventSourceParser
71
+
72
+ super({
73
+ start(controller) {
74
+ parser = createParser({
75
+ onEvent: (event) => {
76
+ controller.enqueue(event)
77
+ },
78
+ onError(error) {
79
+ if (typeof onError === 'function') {
80
+ onError(error)
81
+ }
82
+
83
+ // `max-buffer-size-exceeded` is fatal — the parser is unusable until
84
+ // `reset()`, which the stream wrapper has no way to call. Always
85
+ // terminate the stream in that case so consumers see the meaningful
86
+ // `ParseError` instead of an opaque "cannot feed terminated parser"
87
+ // throw from the next chunk.
88
+ if (onError === 'terminate' || error.type === 'max-buffer-size-exceeded') {
89
+ controller.error(error)
90
+ }
91
+
92
+ // Ignore by default
93
+ },
94
+ onRetry,
95
+ onComment,
96
+ maxBufferSize,
97
+ })
98
+ },
99
+ transform(chunk) {
100
+ parser.feed(chunk)
101
+ },
102
+ })
103
+ }
104
+ }
105
+
106
+ export {type ErrorType, ParseError} from './errors.ts'
107
+ export type {EventSourceMessage} from './types.ts'
@@ -0,0 +1,122 @@
1
+ import type {ParseError} from './errors.ts'
2
+
3
+ /**
4
+ * EventSource parser instance.
5
+ *
6
+ * Needs to be reset between reconnections/when switching data source, using the `reset()` method.
7
+ *
8
+ * @public
9
+ */
10
+ export interface EventSourceParser {
11
+ /**
12
+ * Feeds the parser another chunk. The method _does not_ return a parsed message.
13
+ * Instead, callbacks passed when creating the parser will be triggered once we see enough data
14
+ * for a valid/invalid parsing step (see {@link ParserCallbacks}).
15
+ *
16
+ * @param chunk - The chunk to parse. Can be a partial, eg in the case of streaming messages.
17
+ * @public
18
+ */
19
+ feed(chunk: string): void
20
+
21
+ /**
22
+ * Resets the parser state. This is required when you have a new stream of messages -
23
+ * for instance in the case of a client being disconnected and reconnecting.
24
+ *
25
+ * Previously received, incomplete data will NOT be parsed unless you pass `consume: true`,
26
+ * which tells the parser to attempt to consume any incomplete data as if it ended with a newline
27
+ * character. This is useful for cases when a server sends a non-EventSource message that you
28
+ * want to be able to react to in an `onError` callback.
29
+ *
30
+ * @public
31
+ */
32
+ reset(options?: {consume?: boolean}): void
33
+ }
34
+
35
+ /**
36
+ * A parsed EventSource message event
37
+ *
38
+ * @public
39
+ */
40
+ export interface EventSourceMessage {
41
+ /**
42
+ * The event type sent from the server. Note that this differs from the browser `EventSource`
43
+ * implementation in that browsers will default this to `message`, whereas this parser will
44
+ * leave this as `undefined` if not explicitly declared.
45
+ */
46
+ event?: string | undefined
47
+
48
+ /**
49
+ * ID of the message, if any was provided by the server. Can be used by clients to keep the
50
+ * last received message ID in sync when reconnecting.
51
+ */
52
+ id?: string | undefined
53
+
54
+ /**
55
+ * The data received for this message
56
+ */
57
+ data: string
58
+ }
59
+
60
+ /**
61
+ * Callbacks that can be passed to the parser to handle different types of parsed messages
62
+ * and errors.
63
+ *
64
+ * @public
65
+ */
66
+ export interface ParserCallbacks {
67
+ /**
68
+ * Callback for when a new event/message is parsed from the stream.
69
+ * This is the main callback that clients will use to handle incoming messages.
70
+ *
71
+ * @param event - The parsed event/message
72
+ */
73
+ onEvent?: ((event: EventSourceMessage) => void) | undefined
74
+
75
+ /**
76
+ * Callback for when the server sends a new reconnection interval through the `retry` field.
77
+ *
78
+ * @param retry - The number of milliseconds to wait before reconnecting.
79
+ */
80
+ onRetry?: ((retry: number) => void) | undefined
81
+
82
+ /**
83
+ * Callback for when a comment is encountered in the stream.
84
+ *
85
+ * @param comment - The comment encountered in the stream.
86
+ */
87
+ onComment?: ((comment: string) => void) | undefined
88
+
89
+ /**
90
+ * Callback for when an error occurs during parsing. This is a catch-all for any errors
91
+ * that occur during parsing, and can be used to handle them in a custom way. Most clients
92
+ * tend to silently ignore any errors and instead retry, but it can be helpful to log/debug.
93
+ *
94
+ * @param error - The error that occurred during parsing
95
+ */
96
+ onError?: ((error: ParseError) => void) | undefined
97
+ }
98
+
99
+ /**
100
+ * Configuration accepted by {@link createParser}. Extends {@link ParserCallbacks} with
101
+ * additional options that control parser behavior.
102
+ *
103
+ * @public
104
+ */
105
+ export interface ParserConfig extends ParserCallbacks {
106
+ /**
107
+ * Maximum number of characters the parser is allowed to buffer across calls to `feed()`.
108
+ *
109
+ * Two unbounded surfaces exist in a streaming SSE parser:
110
+ * - A partial line that has not yet been terminated by `\n`, `\r`, or `\r\n`.
111
+ * - A multi-line event whose terminating blank line has not yet arrived (each `data:`
112
+ * field gets appended to the buffered event).
113
+ *
114
+ * When the combined size of these buffers exceeds `maxBufferSize`, the parser emits a
115
+ * `ParseError` with `type: 'max-buffer-size-exceeded'` to `onError` and becomes
116
+ * terminated — subsequent calls to `feed()` will throw until `reset()` is called.
117
+ * This protects against unbounded memory growth from malformed or malicious streams.
118
+ *
119
+ * @defaultValue `undefined` (unbounded)
120
+ */
121
+ maxBufferSize?: number | undefined
122
+ }
@@ -0,0 +1,2 @@
1
+ /* included for compatibility with react-native without package exports support */
2
+ module.exports = require('./dist/stream.cjs')
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Michael Lin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.