@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,144 @@
1
+ import * as s from './store.ts';
2
+ import { Logger } from './Reactor.js';
3
+ import { InstaQLResponse, ValidQuery } from './queryTypes.ts';
4
+ import { EntitiesDef, IContainEntitiesAndLinks } from './schemaTypes.ts';
5
+ import { StoreInterface } from './index.ts';
6
+ type StartMsg = {
7
+ op: 'start-sync';
8
+ q: string;
9
+ };
10
+ type EndMsg = {
11
+ op: 'remove-sync';
12
+ 'subscription-id': string;
13
+ 'keep-subscription': boolean;
14
+ };
15
+ type ResyncMsg = {
16
+ op: 'resync-table';
17
+ 'subscription-id': string;
18
+ 'tx-id': number;
19
+ token: string;
20
+ };
21
+ type SendMsg = StartMsg | EndMsg | ResyncMsg;
22
+ type StartSyncOkMsg = {
23
+ 'subscription-id': string;
24
+ 'client-event-id': string;
25
+ q: string;
26
+ token: string;
27
+ };
28
+ type Triple = [string, string, any, number];
29
+ type SyncLoadBatchMsg = {
30
+ 'subscription-id': string;
31
+ 'join-rows': Array<Triple[]>;
32
+ };
33
+ type SyncInitFinishMsg = {
34
+ 'subscription-id': string;
35
+ 'tx-id': number;
36
+ };
37
+ type SyncUpdateTriplesMsg = {
38
+ 'subscription-id': string;
39
+ txes: {
40
+ 'tx-id': number;
41
+ changes: {
42
+ action: 'added' | 'removed';
43
+ triple: Triple;
44
+ }[];
45
+ }[];
46
+ };
47
+ type TrySend = (eventId: string, msg: SendMsg) => void;
48
+ type Config = {
49
+ useDateObjects: boolean;
50
+ };
51
+ type CreateStore = (triples: Triple[]) => s.Store;
52
+ export declare enum CallbackEventType {
53
+ InitialSyncBatch = "InitialSyncBatch",
54
+ InitialSyncComplete = "InitialSyncComplete",
55
+ LoadFromStorage = "LoadFromStorage",
56
+ SyncTransaction = "SyncTransaction",
57
+ Error = "Error"
58
+ }
59
+ type QueryEntities<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = InstaQLResponse<Schema, Q, UseDates>[keyof InstaQLResponse<Schema, Q, UseDates>];
60
+ type QueryEntity<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = QueryEntities<Schema, Q, UseDates> extends (infer E)[] ? E : never;
61
+ type ChangedFields<Entity> = {
62
+ [K in keyof Entity]?: {
63
+ oldValue: Entity[K];
64
+ newValue: Entity[K];
65
+ };
66
+ };
67
+ export interface BaseCallbackEvent<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> {
68
+ type: CallbackEventType;
69
+ data: InstaQLResponse<Schema, Q, UseDates>;
70
+ }
71
+ export interface InitialSyncBatch<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> extends BaseCallbackEvent<Schema, Q, UseDates> {
72
+ type: CallbackEventType.InitialSyncBatch;
73
+ batch: QueryEntities<Schema, Q, UseDates>;
74
+ }
75
+ export interface InitialSyncComplete<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> extends BaseCallbackEvent<Schema, Q, UseDates> {
76
+ type: CallbackEventType.InitialSyncComplete;
77
+ }
78
+ export interface SyncTransaction<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> extends BaseCallbackEvent<Schema, Q, UseDates> {
79
+ type: CallbackEventType.SyncTransaction;
80
+ added: QueryEntities<Schema, Q, UseDates>;
81
+ removed: QueryEntities<Schema, Q, UseDates>;
82
+ updated: {
83
+ oldEntity: QueryEntity<Schema, Q, UseDates>;
84
+ newEntity: QueryEntity<Schema, Q, UseDates>;
85
+ changedFields: ChangedFields<QueryEntity<Schema, Q, UseDates>>;
86
+ }[];
87
+ }
88
+ export interface LoadFromStorage<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> extends BaseCallbackEvent<Schema, Q, UseDates> {
89
+ type: CallbackEventType.LoadFromStorage;
90
+ }
91
+ export interface SetupError<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> extends BaseCallbackEvent<Schema, Q, UseDates> {
92
+ type: CallbackEventType.Error;
93
+ error: {
94
+ message: string;
95
+ hint?: any;
96
+ type: string;
97
+ status: number;
98
+ };
99
+ }
100
+ export type CallbackEvent<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = InitialSyncBatch<Schema, Q, UseDates> | InitialSyncComplete<Schema, Q, UseDates> | SyncTransaction<Schema, Q, UseDates> | LoadFromStorage<Schema, Q, UseDates> | SetupError<Schema, Q, UseDates>;
101
+ export type SyncTableCallback<Schema extends IContainEntitiesAndLinks<EntitiesDef, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = (event: CallbackEvent<Schema, Q, UseDates>) => void;
102
+ export declare class SyncTable {
103
+ private trySend;
104
+ private subs;
105
+ private callbacks;
106
+ private config;
107
+ private idToHash;
108
+ private log;
109
+ private createStore;
110
+ private getAttrs;
111
+ constructor(trySend: TrySend, storage: StoreInterface, config: Config, log: Logger, createStore: CreateStore, getAttrs: () => s.AttrsStore);
112
+ beforeUnload(): void;
113
+ subscribe(q: any, cb: SyncTableCallback<any, any, any>): (opts?: {
114
+ keepSubscription?: boolean | null | undefined;
115
+ } | null | undefined) => void;
116
+ private unsubscribe;
117
+ private sendStart;
118
+ private sendResync;
119
+ private sendRemove;
120
+ private initSubscription;
121
+ flushPending(): Promise<void>;
122
+ onStartSyncOk(msg: StartSyncOkMsg): void;
123
+ private notifyCbs;
124
+ onSyncLoadBatch(msg: SyncLoadBatchMsg): void;
125
+ onSyncInitFinish(msg: SyncInitFinishMsg): void;
126
+ onSyncUpdateTriples(msg: SyncUpdateTriplesMsg): void;
127
+ private clearSubscriptionData;
128
+ onStartSyncError(msg: {
129
+ op: 'error';
130
+ 'original-event': StartMsg;
131
+ 'client-event-id': string;
132
+ status: number;
133
+ type: string;
134
+ message?: string;
135
+ hint?: any;
136
+ }): void;
137
+ onResyncError(msg: {
138
+ op: 'error';
139
+ 'original-event': ResyncMsg;
140
+ status: number;
141
+ type: string;
142
+ }): void;
143
+ }
144
+ export {};
@@ -0,0 +1,523 @@
1
+ import { PersistedObject } from './utils/PersistedObject.js';
2
+ import * as s from './store.js';
3
+ import weakHash from './utils/weakHash.js';
4
+ import uuid from './utils/id.js';
5
+ import instaql, { compareOrder } from './instaql.js';
6
+ // Modifies the data in place because it comes directly from storage
7
+ function syncSubFromStorage(sub, useDateObjects) {
8
+ const values = sub.values;
9
+ if (values) {
10
+ const attrsStore = s.attrsStoreFromJSON(values.attrsStore, null);
11
+ if (attrsStore) {
12
+ for (const e of values.entities || []) {
13
+ e.store.useDateObjects = useDateObjects;
14
+ e.store = s.fromJSON(attrsStore, e.store);
15
+ }
16
+ values.attrsStore = attrsStore;
17
+ }
18
+ }
19
+ return sub;
20
+ }
21
+ function syncSubToStorage(_k, sub) {
22
+ if (sub.values) {
23
+ const entities = [];
24
+ for (const e of sub.values?.entities) {
25
+ const store = s.toJSON(e.store);
26
+ entities.push({ ...e, store });
27
+ }
28
+ return {
29
+ ...sub,
30
+ values: { attrsStore: sub.values.attrsStore.toJSON(), entities },
31
+ };
32
+ }
33
+ else {
34
+ return sub;
35
+ }
36
+ }
37
+ function onMergeSub(_key, storageSub, inMemorySub) {
38
+ const storageTxId = storageSub?.state?.txId;
39
+ const memoryTxId = inMemorySub?.state?.txId;
40
+ if (storageTxId && (!memoryTxId || storageTxId > memoryTxId)) {
41
+ return storageSub;
42
+ }
43
+ if (memoryTxId && (!storageTxId || memoryTxId > storageTxId)) {
44
+ return inMemorySub;
45
+ }
46
+ return storageSub || inMemorySub;
47
+ }
48
+ function queryEntity(sub, store, attrsStore) {
49
+ const res = instaql({ store, attrsStore, pageInfo: null, aggregate: null }, sub.query);
50
+ return res.data[sub.table][0];
51
+ }
52
+ function getServerCreatedAt(sub, store, attrsStore, entityId) {
53
+ const aid = s.getAttrByFwdIdentName(attrsStore, sub.table, 'id')?.id;
54
+ if (!aid) {
55
+ return -1;
56
+ }
57
+ const t = s.getInMap(store.eav, [entityId, aid, entityId]);
58
+ if (!t) {
59
+ return -1;
60
+ }
61
+ return t[3];
62
+ }
63
+ function applyChangesToStore(store, attrsStore, changes) {
64
+ for (const { action, triple } of changes) {
65
+ switch (action) {
66
+ case 'added':
67
+ s.addTriple(store, attrsStore, triple);
68
+ break;
69
+ case 'removed':
70
+ s.retractTriple(store, attrsStore, triple);
71
+ break;
72
+ }
73
+ }
74
+ }
75
+ function changedFieldsOfChanges(store, attrsStore, changes) {
76
+ // This will be more complicated when we include links, we can either add a
77
+ // changedLinks field or we can have something like 'bookshelves.title`
78
+ const changedFields = {};
79
+ for (const { action, triple } of changes) {
80
+ const [e, a, v] = triple;
81
+ const field = attrsStore.getAttr(a)?.['forward-identity']?.[2];
82
+ if (!field)
83
+ continue;
84
+ const fields = changedFields[e] ?? {};
85
+ changedFields[e] = fields;
86
+ const oldNew = fields[field] ?? {};
87
+ switch (action) {
88
+ case 'added':
89
+ oldNew.newValue = v;
90
+ break;
91
+ case 'removed':
92
+ // Only take the first thing that was removed, in case we modified things in the middle
93
+ if (oldNew.oldValue === undefined) {
94
+ oldNew.oldValue = v;
95
+ }
96
+ break;
97
+ }
98
+ fields[field] = oldNew;
99
+ }
100
+ for (const [_eid, fields] of Object.entries(changedFields)) {
101
+ for (const [k, { oldValue, newValue }] of Object.entries(fields)) {
102
+ if (oldValue === newValue) {
103
+ delete fields[k];
104
+ }
105
+ }
106
+ }
107
+ return changedFields;
108
+ }
109
+ function subData(sub, entities) {
110
+ return { [sub.table]: entities.map((e) => e.entity) };
111
+ }
112
+ // Updates the sub order field type if it hasn't been set
113
+ // and returns the type. We have to wait until the attrs
114
+ // are loaded before we can determine the type.
115
+ function orderFieldTypeMutative(sub, getAttrs) {
116
+ if (sub.orderFieldType) {
117
+ return sub.orderFieldType;
118
+ }
119
+ const orderFieldType = sub.orderField === 'serverCreatedAt'
120
+ ? 'number'
121
+ : s.getAttrByFwdIdentName(getAttrs(), sub.table, sub.orderField)?.['checked-data-type'];
122
+ sub.orderFieldType = orderFieldType;
123
+ return orderFieldType;
124
+ }
125
+ function sortEntitiesInPlace(sub, orderFieldType, entities) {
126
+ const dataType = orderFieldType;
127
+ if (sub.orderField === 'serverCreatedAt') {
128
+ entities.sort(sub.orderDirection === 'asc'
129
+ ? function compareEntities(a, b) {
130
+ return compareOrder(a.entity.id, a.serverCreatedAt, b.entity.id, b.serverCreatedAt, dataType);
131
+ }
132
+ : function compareEntities(b, a) {
133
+ return compareOrder(a.entity.id, a.serverCreatedAt, b.entity.id, b.serverCreatedAt, dataType);
134
+ });
135
+ return;
136
+ }
137
+ const field = sub.orderField;
138
+ entities.sort(sub.orderDirection === 'asc'
139
+ ? function compareEntities(a, b) {
140
+ return compareOrder(a.entity.id, a.entity[field], b.entity.id, b.entity[field], dataType);
141
+ }
142
+ : function compareEntities(b, a) {
143
+ return compareOrder(a.entity.id, a.entity[field], b.entity.id, b.entity[field], dataType);
144
+ });
145
+ }
146
+ export var CallbackEventType;
147
+ (function (CallbackEventType) {
148
+ CallbackEventType["InitialSyncBatch"] = "InitialSyncBatch";
149
+ CallbackEventType["InitialSyncComplete"] = "InitialSyncComplete";
150
+ CallbackEventType["LoadFromStorage"] = "LoadFromStorage";
151
+ CallbackEventType["SyncTransaction"] = "SyncTransaction";
152
+ CallbackEventType["Error"] = "Error";
153
+ })(CallbackEventType || (CallbackEventType = {}));
154
+ export class SyncTable {
155
+ trySend;
156
+ subs;
157
+ // Using any for the SyncCallback because we'd need Reactor to be typed
158
+ callbacks = {};
159
+ config;
160
+ idToHash = {};
161
+ log;
162
+ createStore;
163
+ getAttrs;
164
+ constructor(trySend, storage, config, log, createStore, getAttrs) {
165
+ this.trySend = trySend;
166
+ this.config = config;
167
+ this.log = log;
168
+ this.createStore = createStore;
169
+ this.getAttrs = getAttrs;
170
+ this.subs = new PersistedObject({
171
+ persister: storage,
172
+ merge: onMergeSub,
173
+ serialize: syncSubToStorage,
174
+ parse: (_key, x) => syncSubFromStorage(x, this.config.useDateObjects),
175
+ objectSize: (sub) => sub.values?.entities.length || 0,
176
+ logger: log,
177
+ gc: {
178
+ maxAgeMs: 1000 * 60 * 60 * 24 * 7 * 52, // 1 year
179
+ maxEntries: 1000,
180
+ // Size of each sub is the number of entity
181
+ maxSize: 1_000_000, // 1 million entities
182
+ },
183
+ });
184
+ }
185
+ beforeUnload() {
186
+ this.subs.flush();
187
+ }
188
+ subscribe(q, cb) {
189
+ const hash = weakHash(q);
190
+ this.callbacks[hash] = this.callbacks[hash] || [];
191
+ this.callbacks[hash].push(cb);
192
+ this.initSubscription(q, hash, cb);
193
+ return (opts) => {
194
+ this.unsubscribe(hash, cb, opts?.keepSubscription);
195
+ };
196
+ }
197
+ unsubscribe(hash, cb, keepSubscription) {
198
+ const cbs = (this.callbacks[hash] || []).filter((x) => x !== cb);
199
+ this.callbacks[hash] = cbs;
200
+ if (!cbs.length) {
201
+ delete this.callbacks[hash];
202
+ const sub = this.subs.currentValue[hash];
203
+ if (sub?.state) {
204
+ this.clearSubscriptionData(sub.state.subscriptionId, !!keepSubscription);
205
+ }
206
+ if (!keepSubscription) {
207
+ this.subs.updateInPlace((prev) => {
208
+ delete prev[hash];
209
+ });
210
+ }
211
+ }
212
+ }
213
+ sendStart(query) {
214
+ this.trySend(uuid(), {
215
+ op: 'start-sync',
216
+ q: query,
217
+ });
218
+ }
219
+ sendResync(sub, state, txId) {
220
+ // Make sure we can find the hash from the subscriptionId
221
+ this.idToHash[state.subscriptionId] = sub.hash;
222
+ this.trySend(state.subscriptionId, {
223
+ op: 'resync-table',
224
+ 'subscription-id': state.subscriptionId,
225
+ 'tx-id': txId,
226
+ token: state.token,
227
+ });
228
+ }
229
+ sendRemove(state, keepSubscription) {
230
+ this.trySend(uuid(), {
231
+ op: 'remove-sync',
232
+ 'subscription-id': state.subscriptionId,
233
+ 'keep-subscription': keepSubscription,
234
+ });
235
+ }
236
+ async initSubscription(query, hash, cb) {
237
+ // Wait for storage to load so that we know if we already have an existing subscription
238
+ await this.subs.waitForKeyToLoad(hash);
239
+ const existingSub = this.subs.currentValue[hash];
240
+ if (existingSub && existingSub.state && existingSub.state.txId) {
241
+ this.sendResync(existingSub, existingSub.state, existingSub.state.txId);
242
+ if (existingSub.values?.entities && cb) {
243
+ cb({
244
+ type: CallbackEventType.LoadFromStorage,
245
+ data: subData(existingSub, existingSub.values?.entities),
246
+ });
247
+ }
248
+ return;
249
+ }
250
+ const table = Object.keys(query)[0];
251
+ const orderBy = query[table]?.$?.order || { serverCreatedAt: 'asc' };
252
+ const [orderField, orderDirection] = Object.entries(orderBy)[0];
253
+ this.subs.updateInPlace((prev) => {
254
+ prev[hash] = {
255
+ query,
256
+ hash: hash,
257
+ table,
258
+ orderDirection,
259
+ orderField,
260
+ createdAt: Date.now(),
261
+ updatedAt: Date.now(),
262
+ };
263
+ });
264
+ this.sendStart(query);
265
+ }
266
+ async flushPending() {
267
+ for (const hash of Object.keys(this.callbacks)) {
268
+ await this.subs.waitForKeyToLoad(hash);
269
+ const sub = this.subs.currentValue[hash];
270
+ if (sub) {
271
+ await this.initSubscription(sub.query, sub.hash);
272
+ }
273
+ else {
274
+ this.log.error('Missing sub for hash in flushPending', hash);
275
+ }
276
+ }
277
+ }
278
+ onStartSyncOk(msg) {
279
+ const subscriptionId = msg['subscription-id'];
280
+ const q = msg.q;
281
+ const hash = weakHash(q);
282
+ this.idToHash[subscriptionId] = hash;
283
+ this.subs.updateInPlace((prev) => {
284
+ const sub = prev[hash];
285
+ if (!sub) {
286
+ this.log.error('Missing sub for hash', hash, 'subscription-id', subscriptionId, 'query', q);
287
+ return prev;
288
+ }
289
+ sub.state = {
290
+ subscriptionId: subscriptionId,
291
+ token: msg.token,
292
+ };
293
+ });
294
+ }
295
+ notifyCbs(hash, event) {
296
+ for (const cb of this.callbacks[hash] || []) {
297
+ cb(event);
298
+ }
299
+ }
300
+ onSyncLoadBatch(msg) {
301
+ const subscriptionId = msg['subscription-id'];
302
+ const joinRows = msg['join-rows'];
303
+ const hash = this.idToHash[subscriptionId];
304
+ if (!hash) {
305
+ this.log.error('Missing hash for subscription', msg);
306
+ return;
307
+ }
308
+ const batch = [];
309
+ const sub = this.subs.currentValue[hash];
310
+ if (!sub) {
311
+ this.log.error('Missing sub for hash', hash, msg);
312
+ return;
313
+ }
314
+ const values = sub.values ?? {
315
+ entities: [],
316
+ attrsStore: this.getAttrs(),
317
+ };
318
+ sub.values = values;
319
+ const entities = values.entities;
320
+ for (const entRows of joinRows) {
321
+ const store = this.createStore(entRows);
322
+ const entity = queryEntity(sub, store, values.attrsStore);
323
+ entities.push({
324
+ store,
325
+ entity,
326
+ serverCreatedAt: getServerCreatedAt(sub, store, values.attrsStore, entity.id),
327
+ });
328
+ batch.push(entity);
329
+ }
330
+ this.subs.updateInPlace((prev) => {
331
+ prev[hash] = sub;
332
+ // Make sure we write a field or mutative won't
333
+ // see the change because sub === prev[hash]
334
+ prev[hash].updatedAt = Date.now();
335
+ });
336
+ if (sub.values) {
337
+ this.notifyCbs(hash, {
338
+ type: CallbackEventType.InitialSyncBatch,
339
+ data: subData(sub, sub.values.entities),
340
+ batch,
341
+ });
342
+ }
343
+ }
344
+ onSyncInitFinish(msg) {
345
+ const subscriptionId = msg['subscription-id'];
346
+ const hash = this.idToHash[subscriptionId];
347
+ if (!hash) {
348
+ this.log.error('Missing hash for subscription', msg);
349
+ return;
350
+ }
351
+ this.subs.updateInPlace((prev) => {
352
+ const sub = prev[hash];
353
+ if (!sub) {
354
+ this.log.error('Missing sub for hash', hash, msg);
355
+ return;
356
+ }
357
+ const state = sub.state;
358
+ if (!state) {
359
+ this.log.error('Sub never set init, missing result', sub, msg);
360
+ return prev;
361
+ }
362
+ state.txId = msg['tx-id'];
363
+ sub.updatedAt = Date.now();
364
+ });
365
+ const sub = this.subs.currentValue[hash];
366
+ if (sub) {
367
+ this.notifyCbs(hash, {
368
+ type: CallbackEventType.InitialSyncComplete,
369
+ data: subData(sub, sub.values?.entities || []),
370
+ });
371
+ }
372
+ }
373
+ onSyncUpdateTriples(msg) {
374
+ const subscriptionId = msg['subscription-id'];
375
+ const hash = this.idToHash[subscriptionId];
376
+ if (!hash) {
377
+ this.log.error('Missing hash for subscription', msg);
378
+ return;
379
+ }
380
+ const sub = this.subs.currentValue[hash];
381
+ if (!sub) {
382
+ this.log.error('Missing sub for hash', hash, msg);
383
+ return;
384
+ }
385
+ const state = sub.state;
386
+ if (!state) {
387
+ this.log.error('Missing state for sub', sub, msg);
388
+ return;
389
+ }
390
+ for (const tx of msg.txes) {
391
+ if (state.txId && state.txId >= tx['tx-id']) {
392
+ continue;
393
+ }
394
+ state.txId = tx['tx-id'];
395
+ const idxesToDelete = [];
396
+ // Note: this won't work as well when links are involved
397
+ const byEid = {};
398
+ for (const change of tx.changes) {
399
+ const eidChanges = byEid[change.triple[0]] ?? [];
400
+ byEid[change.triple[0]] = eidChanges;
401
+ eidChanges.push(change);
402
+ }
403
+ const values = sub.values ?? {
404
+ entities: [],
405
+ attrsStore: this.getAttrs(),
406
+ };
407
+ const entities = values.entities;
408
+ sub.values = values;
409
+ const updated = [];
410
+ // Update the existing stores, if we already know about this entity
411
+ eidLoop: for (const [eid, changes] of Object.entries(byEid)) {
412
+ for (let i = 0; i < entities.length; i++) {
413
+ const ent = entities[i];
414
+ if (s.hasEntity(ent.store, eid)) {
415
+ applyChangesToStore(ent.store, values.attrsStore, changes);
416
+ const entity = queryEntity(sub, ent.store, values.attrsStore);
417
+ const changedFields = changedFieldsOfChanges(ent.store, values.attrsStore, changes)[eid];
418
+ if (entity) {
419
+ updated.push({
420
+ oldEntity: ent.entity,
421
+ newEntity: entity,
422
+ changedFields: (changedFields || {}),
423
+ });
424
+ ent.entity = entity;
425
+ }
426
+ else {
427
+ idxesToDelete.push(i);
428
+ }
429
+ delete byEid[eid];
430
+ continue eidLoop;
431
+ }
432
+ }
433
+ }
434
+ const added = [];
435
+ // If we have anything left in byEid, then this must be a new entity we don't know about
436
+ for (const [_eid, changes] of Object.entries(byEid)) {
437
+ const store = this.createStore([]);
438
+ applyChangesToStore(store, values.attrsStore, changes);
439
+ const entity = queryEntity(sub, store, values.attrsStore);
440
+ if (!entity) {
441
+ this.log.error('No entity found after applying change', {
442
+ sub,
443
+ changes,
444
+ store,
445
+ });
446
+ continue;
447
+ }
448
+ entities.push({
449
+ store,
450
+ entity,
451
+ serverCreatedAt: getServerCreatedAt(sub, store, values.attrsStore, entity.id),
452
+ });
453
+ added.push(entity);
454
+ }
455
+ const removed = [];
456
+ for (const idx of idxesToDelete.sort().reverse()) {
457
+ removed.push(entities[idx].entity);
458
+ entities.splice(idx, 1);
459
+ }
460
+ const orderFieldType = orderFieldTypeMutative(sub, this.getAttrs);
461
+ sortEntitiesInPlace(sub, orderFieldType, entities);
462
+ this.notifyCbs(hash, {
463
+ type: CallbackEventType.SyncTransaction,
464
+ data: subData(sub, sub.values?.entities),
465
+ added,
466
+ removed,
467
+ updated,
468
+ });
469
+ }
470
+ this.subs.updateInPlace((prev) => {
471
+ prev[hash] = sub;
472
+ // Make sure we write a field or mutative won't
473
+ // see the change because sub === prev[hash]
474
+ prev[hash].updatedAt = Date.now();
475
+ });
476
+ }
477
+ clearSubscriptionData(subscriptionId, keepSubscription) {
478
+ const hash = this.idToHash[subscriptionId];
479
+ if (hash) {
480
+ delete this.idToHash[subscriptionId];
481
+ const sub = this.subs.currentValue[hash];
482
+ if (sub.state) {
483
+ this.sendRemove(sub.state, keepSubscription);
484
+ }
485
+ if (keepSubscription) {
486
+ this.subs.unloadKey(hash);
487
+ }
488
+ else {
489
+ this.subs.updateInPlace((prev) => {
490
+ delete prev[hash];
491
+ });
492
+ }
493
+ if (sub) {
494
+ return sub;
495
+ }
496
+ }
497
+ }
498
+ onStartSyncError(msg) {
499
+ const hash = weakHash(msg['original-event']['q']);
500
+ const error = {
501
+ message: msg.message || 'Uh-oh, something went wrong. Ping Joe & Stopa.',
502
+ status: msg.status,
503
+ type: msg.type,
504
+ hint: msg.hint,
505
+ };
506
+ const k = Object.keys(msg['original-event']['q'])[0];
507
+ this.notifyCbs(hash, {
508
+ type: CallbackEventType.Error,
509
+ data: { [k]: [] },
510
+ error,
511
+ });
512
+ }
513
+ onResyncError(msg) {
514
+ // Clear the subscription and start from scrath on any resync error
515
+ // This can happen if the auth changed and we need to refetch with the
516
+ // new auth or if the subscription is too far behind.
517
+ const subscriptionId = msg['original-event']['subscription-id'];
518
+ const removedSub = this.clearSubscriptionData(subscriptionId, false);
519
+ if (removedSub) {
520
+ this.initSubscription(removedSub.query, removedSub.hash);
521
+ }
522
+ }
523
+ }
@@ -0,0 +1,4 @@
1
+ export default class WindowNetworkListener {
2
+ static getIsOnline(): Promise<boolean>;
3
+ static listen(f: any): () => void;
4
+ }
@@ -0,0 +1,19 @@
1
+ export default class WindowNetworkListener {
2
+ static async getIsOnline() {
3
+ return navigator.onLine;
4
+ }
5
+ static listen(f) {
6
+ const onOnline = () => {
7
+ f(true);
8
+ };
9
+ const onOffline = () => {
10
+ f(false);
11
+ };
12
+ addEventListener('online', onOnline);
13
+ addEventListener('offline', onOffline);
14
+ return () => {
15
+ removeEventListener('online', onOnline);
16
+ removeEventListener('offline', onOffline);
17
+ };
18
+ }
19
+ }