@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,2126 @@
1
+ "use strict";
2
+
3
+ //#region src/interface.ts
4
+ const Operation = {
5
+ Remove: "remove",
6
+ Replace: "replace",
7
+ Add: "add"
8
+ };
9
+
10
+ //#endregion
11
+ //#region src/constant.ts
12
+ const PROXY_DRAFT = Symbol.for("__MUTATIVE_PROXY_DRAFT__");
13
+ const RAW_RETURN_SYMBOL = Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__");
14
+ const iteratorSymbol = Symbol.iterator;
15
+ const iteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][iteratorSymbol]()));
16
+ const dataTypes = {
17
+ mutable: "mutable",
18
+ immutable: "immutable"
19
+ };
20
+
21
+ //#endregion
22
+ //#region src/internal.ts
23
+ const internal = {};
24
+
25
+ //#endregion
26
+ //#region src/utils/draft.ts
27
+ function latest(proxyDraft) {
28
+ var _a;
29
+ return (_a = proxyDraft.copy) !== null && _a !== void 0 ? _a : proxyDraft.original;
30
+ }
31
+ function getSetMap(target) {
32
+ return target.setMap || (target.setMap = /* @__PURE__ */ new Map());
33
+ }
34
+ function setItemValue(target, item) {
35
+ const value = target.setMap && target.setMap.get(item);
36
+ return value === void 0 ? item : value;
37
+ }
38
+ const setValues = Set.prototype.values;
39
+ function iterateSet(set) {
40
+ return isDraft(set) ? set.values() : setValues.call(set);
41
+ }
42
+ /**
43
+ * Check if the value is a draft
44
+ */
45
+ function isDraft(target) {
46
+ return !!getProxyDraft(target);
47
+ }
48
+ function getProxyDraft(value) {
49
+ if (typeof value !== "object") return null;
50
+ return value === null || value === void 0 ? void 0 : value[PROXY_DRAFT];
51
+ }
52
+ function assertDraftActive(target) {
53
+ if (target.finalities.revoke.length === 0) getProxyDraft(target.proxy);
54
+ }
55
+ function getValue(value) {
56
+ const proxyDraft = getProxyDraft(value);
57
+ if (!proxyDraft) return value;
58
+ return proxyDraft.operated ? proxyDraft.copy : proxyDraft.original;
59
+ }
60
+ /**
61
+ * Check if a value is draftable
62
+ */
63
+ function isDraftable(value, options) {
64
+ if (!value || typeof value !== "object") return false;
65
+ let markResult;
66
+ return Object.getPrototypeOf(value) === Object.prototype || Array.isArray(value) || value instanceof Map || value instanceof Set || !!(options === null || options === void 0 ? void 0 : options.mark) && ((markResult = options.mark(value, dataTypes)) === dataTypes.immutable || typeof markResult === "function");
67
+ }
68
+ function checkPathKey(type, key, pathAsArray) {
69
+ if (pathAsArray === false && typeof key !== "string" && (type === 2 || typeof key === "symbol")) throw new Error(`Patches with string paths only support string Map keys and no symbol keys, got a key of type '${typeof key}'. Set 'pathAsArray' to true to keep keys of other types.`);
70
+ }
71
+ function getPath(target, path = []) {
72
+ const parent = target.parent;
73
+ if (parent) {
74
+ const parentCopy = parent.copy;
75
+ let key = target.key;
76
+ if (parent.type === 3) {
77
+ if (parent.assignedMap.size > 0) return null;
78
+ let index = 0;
79
+ for (const item of iterateSet(parent.original)) {
80
+ if (item === key) break;
81
+ index += 1;
82
+ }
83
+ key = index;
84
+ } else if (get(parentCopy, key) !== target.proxy) return null;
85
+ path.push(key);
86
+ const resolved = getPath(parent, path);
87
+ if (resolved) checkPathKey(parent.type, key, parent.options.enablePatches.pathAsArray);
88
+ return resolved;
89
+ }
90
+ path.reverse();
91
+ return path;
92
+ }
93
+ function getType(target) {
94
+ if (Array.isArray(target)) return 1;
95
+ if (target instanceof Map) return 2;
96
+ if (target instanceof Set) return 3;
97
+ return 0;
98
+ }
99
+ function get(target, key) {
100
+ return getType(target) === 2 ? target.get(key) : target[key];
101
+ }
102
+ function set(target, key, value) {
103
+ if (getType(target) === 2) target.set(key, value);
104
+ else target[key] = value;
105
+ }
106
+ function isEqual(x, y) {
107
+ if (x === y) return x !== 0 || 1 / x === 1 / y;
108
+ else return x !== x && y !== y;
109
+ }
110
+ function revokeProxy(finalities) {
111
+ const revoke = finalities.revoke;
112
+ while (revoke.length > 0) revoke.pop()();
113
+ }
114
+ function escapePath(path, pathAsArray) {
115
+ return pathAsArray ? path : [""].concat(path).map((_item) => {
116
+ const item = `${_item}`;
117
+ if (item.indexOf("/") === -1 && item.indexOf("~") === -1) return item;
118
+ return item.replace(/~/g, "~0").replace(/\//g, "~1");
119
+ }).join("/");
120
+ }
121
+ function unescapePath(path) {
122
+ if (Array.isArray(path)) return path;
123
+ return path.split("/").map((_item) => _item.replace(/~1/g, "/").replace(/~0/g, "~")).slice(1);
124
+ }
125
+
126
+ //#endregion
127
+ //#region src/utils/proto.ts
128
+ function has(target, key) {
129
+ return target instanceof Map ? target.has(key) : Object.prototype.hasOwnProperty.call(target, key);
130
+ }
131
+ function getDescriptor(target, key) {
132
+ if (key in target) {
133
+ let prototype = Reflect.getPrototypeOf(target);
134
+ while (prototype) {
135
+ const descriptor = Reflect.getOwnPropertyDescriptor(prototype, key);
136
+ if (descriptor) return descriptor;
137
+ prototype = Reflect.getPrototypeOf(prototype);
138
+ }
139
+ }
140
+ }
141
+ /**
142
+ * Whether `concat` and the array methods treat `array` like any array: no
143
+ * subclass, and no own `constructor` or `Symbol.isConcatSpreadable` that
144
+ * changes what `concat` creates.
145
+ */
146
+ function isPlainArray(array) {
147
+ return !Object.prototype.hasOwnProperty.call(array, "constructor") && !Object.prototype.hasOwnProperty.call(array, Symbol.isConcatSpreadable) && Object.getPrototypeOf(array) === Array.prototype;
148
+ }
149
+ function isBaseSetInstance(obj) {
150
+ return Object.getPrototypeOf(obj) === Set.prototype;
151
+ }
152
+ function isBaseMapInstance(obj) {
153
+ return Object.getPrototypeOf(obj) === Map.prototype;
154
+ }
155
+
156
+ //#endregion
157
+ //#region src/error.ts
158
+ const errors = [
159
+ () => `Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.`,
160
+ () => `Map/Set draft does not support any property assignment.`,
161
+ () => `Only supports setting array indices and the 'length' property.`,
162
+ () => `Cannot call 'setPrototypeOf()' on drafts`,
163
+ () => `Cannot call 'defineProperty()' on drafts`,
164
+ () => `Either the value is returned as a new non-draft value, or only the draft is modified without returning any value.`,
165
+ () => `Cannot return a modified child draft.`,
166
+ (target) => `current() is only used for Draft, parameter: ${target}`,
167
+ () => `Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.`,
168
+ (markResult) => `Unsupported mark result: ${markResult}`,
169
+ () => `Please check mark() to ensure that it is a stable marker draftable function.`,
170
+ () => `Cannot modify frozen object`,
171
+ void 0,
172
+ () => `Patching reserved attributes like __proto__ and constructor is not allowed.`,
173
+ (path) => `Cannot apply patch at '${path.join("/")}'.`,
174
+ () => `Cannot apply replace patch to set.`,
175
+ (op) => `Unsupported patch operation: ${op}.`,
176
+ () => `Cannot apply patches with options to a draft.`,
177
+ (target) => `original() is only used for a draft, parameter: ${target}`,
178
+ () => "rawReturn() must be called with a value.",
179
+ () => "rawReturn() must be called with one argument.",
180
+ () => "apply() with the mutable option changes the state in place, so a patch cannot replace the root state with another value."
181
+ ];
182
+ function die(error, ...args) {
183
+ throw new Error(errors[error](...args));
184
+ }
185
+
186
+ //#endregion
187
+ //#region src/utils/copy.ts
188
+ function strictCopy(target) {
189
+ const copy = Object.create(Object.getPrototypeOf(target));
190
+ Reflect.ownKeys(target).forEach((key) => {
191
+ let desc = Reflect.getOwnPropertyDescriptor(target, key);
192
+ if (key !== "__proto__" && desc.enumerable && desc.configurable && desc.writable) {
193
+ copy[key] = target[key];
194
+ return;
195
+ }
196
+ if (!desc.writable) {
197
+ desc.writable = true;
198
+ desc.configurable = true;
199
+ }
200
+ if (desc.get || desc.set) desc = {
201
+ configurable: true,
202
+ writable: true,
203
+ enumerable: desc.enumerable,
204
+ value: target[key]
205
+ };
206
+ Reflect.defineProperty(copy, key, desc);
207
+ });
208
+ return copy;
209
+ }
210
+ const propIsEnum = Object.prototype.propertyIsEnumerable;
211
+ /**
212
+ * Plain objects with at most this many own enumerable string keys are copied
213
+ * with object spread, which V8 clones in one step. Wider objects are copied
214
+ * key by key into a dictionary-mode object: a map transition per key is far
215
+ * slower there, and whether V8 stays in fast mode depends on transitions that
216
+ * other code may have created for the same key sequence.
217
+ */
218
+ const SPREAD_KEY_LIMIT = 128;
219
+ function copyPlainObject(original) {
220
+ const keys = Object.keys(original);
221
+ if (keys.length <= SPREAD_KEY_LIMIT) return { ...original };
222
+ const copy = {};
223
+ copy.a = 1;
224
+ copy.b = 1;
225
+ delete copy.a;
226
+ delete copy.b;
227
+ for (let index = 0; index < keys.length; index += 1) {
228
+ const key = keys[index];
229
+ if (key === "__proto__") Object.defineProperty(copy, key, {
230
+ value: original[key],
231
+ writable: true,
232
+ enumerable: true,
233
+ configurable: true
234
+ });
235
+ else copy[key] = original[key];
236
+ }
237
+ const symbols = Object.getOwnPropertySymbols(original);
238
+ for (let index = 0; index < symbols.length; index += 1) {
239
+ const key = symbols[index];
240
+ if (propIsEnum.call(original, key)) copy[key] = original[key];
241
+ }
242
+ return copy;
243
+ }
244
+ function concatCopy(original) {
245
+ const copy = Array.prototype.concat.call(original);
246
+ return copy.length === 1 && copy[0] === original ? Array.prototype.slice.call(original) : copy;
247
+ }
248
+ const arrayValues = Array.prototype[Symbol.iterator];
249
+ const arrayIterator = Object.getPrototypeOf(arrayValues.call([]));
250
+ const arrayIteratorNext = arrayIterator.next;
251
+ /**
252
+ * `concat` leaves V8's fast path for frozen, sealed and non-extensible arrays
253
+ * and takes about ten times as long as a spread there. A spread copies a
254
+ * plain array the same way when it has no holes and none of the iterators it
255
+ * calls has been replaced. Holes read as `undefined`, so arrays that hold
256
+ * `undefined` keep `concat`.
257
+ */
258
+ function copyLockedArray(original) {
259
+ return isPlainArray(original) && !Object.prototype.hasOwnProperty.call(original, Symbol.iterator) && Array.prototype[Symbol.iterator] === arrayValues && arrayIterator.next === arrayIteratorNext && !Array.prototype.includes.call(original, void 0) ? [...original] : concatCopy(original);
260
+ }
261
+ function shallowCopy(original, options) {
262
+ let markResult;
263
+ if (Array.isArray(original)) return (options === null || options === void 0 ? void 0 : options.enableAutoFreeze) && !Object.isExtensible(original) ? copyLockedArray(original) : concatCopy(original);
264
+ else if (original instanceof Set) {
265
+ if (!isBaseSetInstance(original)) {
266
+ const SubClass = Object.getPrototypeOf(original).constructor;
267
+ const copy = new SubClass(original.values());
268
+ Set.prototype.clear.call(copy);
269
+ for (const item of iterateSet(original)) Set.prototype.add.call(copy, item);
270
+ return copy;
271
+ }
272
+ return !isDraft(original) && Set.prototype.difference ? Set.prototype.difference.call(original, /* @__PURE__ */ new Set()) : new Set(original.values());
273
+ } else if (original instanceof Map) {
274
+ if (!isBaseMapInstance(original)) {
275
+ const SubClass = Object.getPrototypeOf(original).constructor;
276
+ return new SubClass(original);
277
+ }
278
+ return new Map(original);
279
+ } else if ((options === null || options === void 0 ? void 0 : options.mark) && (markResult = options.mark(original, dataTypes), markResult !== void 0) && markResult !== dataTypes.mutable) {
280
+ if (markResult === dataTypes.immutable) return strictCopy(original);
281
+ else if (typeof markResult === "function") {
282
+ if (options.enablePatches || options.enableAutoFreeze) throw new Error(`You can't use mark and patches or auto freeze together.`);
283
+ return markResult();
284
+ }
285
+ die(9, markResult);
286
+ } else if (typeof original === "object" && Object.getPrototypeOf(original) === Object.prototype) return copyPlainObject(original);
287
+ else die(10);
288
+ }
289
+ function ensureShallowCopy(target) {
290
+ if (target.copy) return;
291
+ target.copy = shallowCopy(target.original, target.options);
292
+ }
293
+ function deepClone(target) {
294
+ if (!isDraftable(target)) return getValue(target);
295
+ if (Array.isArray(target)) return target.map(deepClone);
296
+ if (target instanceof Map) {
297
+ const iterable = Array.from(target.entries()).map(([k, v]) => [k, deepClone(v)]);
298
+ if (!isBaseMapInstance(target)) {
299
+ const SubClass = Object.getPrototypeOf(target).constructor;
300
+ return new SubClass(iterable);
301
+ }
302
+ return new Map(iterable);
303
+ }
304
+ if (target instanceof Set) {
305
+ const iterable = Array.from(target).map(deepClone);
306
+ if (!isBaseSetInstance(target)) {
307
+ const SubClass = Object.getPrototypeOf(target).constructor;
308
+ return new SubClass(iterable);
309
+ }
310
+ return new Set(iterable);
311
+ }
312
+ const copy = { ...target };
313
+ for (const key in copy) {
314
+ const value = copy[key];
315
+ if (typeof value === "object" && value !== null) copy[key] = deepClone(value);
316
+ }
317
+ const symbols = Object.getOwnPropertySymbols(copy);
318
+ for (let index = 0; index < symbols.length; index += 1) {
319
+ const value = copy[symbols[index]];
320
+ if (typeof value === "object" && value !== null) copy[symbols[index]] = deepClone(value);
321
+ }
322
+ return copy;
323
+ }
324
+
325
+ //#endregion
326
+ //#region src/utils/mark.ts
327
+ function markChanged(proxyDraft) {
328
+ var _a;
329
+ proxyDraft.assignedMap = (_a = proxyDraft.assignedMap) !== null && _a !== void 0 ? _a : /* @__PURE__ */ new Map();
330
+ if (!proxyDraft.operated) {
331
+ proxyDraft.operated = true;
332
+ if (proxyDraft.parent) markChanged(proxyDraft.parent);
333
+ }
334
+ }
335
+
336
+ //#endregion
337
+ //#region src/utils/deepFreeze.ts
338
+ function throwFrozenError() {
339
+ die(11);
340
+ }
341
+ function isFreezable(value) {
342
+ return true;
343
+ }
344
+ function deepFreeze(target, subKey, stack, keys) {
345
+ stack = stack !== null && stack !== void 0 ? stack : [];
346
+ keys = keys !== null && keys !== void 0 ? keys : [];
347
+ if (stack.length > 0) {
348
+ const index = stack.indexOf(target);
349
+ if (target && typeof target === "object" && index !== -1) {
350
+ if (stack[0] === target) throw new Error(`Forbids circular reference`);
351
+ throw new Error(`Forbids circular reference: ~/${keys.slice(0, index).map((key, index) => {
352
+ if (typeof key === "symbol") return `[${key.toString()}]`;
353
+ const parent = stack[index];
354
+ if (typeof key === "object" && (parent instanceof Map || parent instanceof Set)) return Array.from(parent.keys()).indexOf(key);
355
+ return key;
356
+ }).join("/")}`);
357
+ }
358
+ stack.push(target);
359
+ keys.push(subKey);
360
+ } else stack.push(target);
361
+ if (Object.isFrozen(target) || isDraft(target)) {
362
+ stack.pop();
363
+ keys.pop();
364
+ return;
365
+ }
366
+ switch (getType(target)) {
367
+ case 2:
368
+ target.set = target.clear = target.delete = throwFrozenError;
369
+ Object.freeze(target);
370
+ for (const [key, value] of target) {
371
+ if (isFreezable(key)) deepFreeze(key, key, stack, keys);
372
+ if (isFreezable(value)) deepFreeze(value, key, stack, keys);
373
+ }
374
+ break;
375
+ case 3:
376
+ target.add = target.clear = target.delete = throwFrozenError;
377
+ Object.freeze(target);
378
+ for (const value of target) if (isFreezable(value)) deepFreeze(value, value, stack, keys);
379
+ break;
380
+ case 1:
381
+ Object.freeze(target);
382
+ let index = 0;
383
+ for (const value of target) {
384
+ if (isFreezable(value)) deepFreeze(value, index, stack, keys);
385
+ index += 1;
386
+ }
387
+ break;
388
+ default:
389
+ Object.freeze(target);
390
+ Object.keys(target).forEach((name) => {
391
+ const value = target[name];
392
+ if (isFreezable(value)) deepFreeze(value, name, stack, keys);
393
+ });
394
+ }
395
+ stack.pop();
396
+ keys.pop();
397
+ }
398
+
399
+ //#endregion
400
+ //#region src/utils/forEach.ts
401
+ function forEach(target, iter) {
402
+ const type = getType(target);
403
+ if (type === 0) Reflect.ownKeys(target).forEach((key) => {
404
+ iter(key, target[key], target);
405
+ });
406
+ else if (type === 1) {
407
+ let index = 0;
408
+ for (const entry of target) {
409
+ iter(index, entry, target);
410
+ index += 1;
411
+ }
412
+ } else target.forEach((entry, index) => iter(index, entry, target));
413
+ }
414
+
415
+ //#endregion
416
+ //#region src/utils/finalize.ts
417
+ function handleValue(target, handledSet, options) {
418
+ if (isDraft(target) || !isDraftable(target, options) || handledSet.has(target) || Object.isFrozen(target)) return;
419
+ const isSet = target instanceof Set;
420
+ const setMap = isSet ? /* @__PURE__ */ new Map() : void 0;
421
+ handledSet.add(target);
422
+ forEach(target, (key, value) => {
423
+ var _a;
424
+ if (isDraft(value)) {
425
+ const proxyDraft = getProxyDraft(value);
426
+ const updatedValue = ((_a = proxyDraft.assignedMap) === null || _a === void 0 ? void 0 : _a.size) || proxyDraft.operated ? proxyDraft.copy : proxyDraft.original;
427
+ set(isSet ? setMap : target, key, updatedValue);
428
+ } else handleValue(value, handledSet, options);
429
+ });
430
+ if (setMap) {
431
+ const set = target;
432
+ const values = Array.from(set);
433
+ set.clear();
434
+ values.forEach((value) => {
435
+ set.add(setMap.has(value) ? setMap.get(value) : value);
436
+ });
437
+ }
438
+ }
439
+ function finalizeAssigned(proxyDraft, key) {
440
+ const copy = proxyDraft.type === 3 ? proxyDraft.setMap : proxyDraft.copy;
441
+ if (proxyDraft.finalities.revoke.length > 1 && proxyDraft.assignedMap.get(key) && copy) handleValue(get(copy, key), proxyDraft.finalities.handledSet, proxyDraft.options);
442
+ }
443
+ function finalizeSetValue(target, again) {
444
+ const setMap = target.setMap;
445
+ if (!setMap || !target.operated) return;
446
+ let items = setMap.items;
447
+ if (!items) {
448
+ if (again) return;
449
+ let differs = target.assignedMap.size === 0;
450
+ if (!differs) {
451
+ for (const [item, value] of setMap) if (getValue(value) !== item) {
452
+ differs = true;
453
+ break;
454
+ }
455
+ }
456
+ if (!differs) return;
457
+ items = setMap.items = Array.from(iterateSet(target.copy));
458
+ }
459
+ const copy = target.copy;
460
+ copy.clear();
461
+ items.forEach((item) => {
462
+ const value = setMap.get(item);
463
+ copy.add(value === void 0 ? item : getValue(value));
464
+ });
465
+ }
466
+ function finalizePatches(target, generatePatches, patches, inversePatches) {
467
+ if (target.operated && (target.arrayState !== null && target.arrayState.relocated || target.assignedMap !== void 0 && target.assignedMap.size > 0) && !target.finalized) {
468
+ if (patches && inversePatches) {
469
+ const basePath = getPath(target);
470
+ if (basePath) generatePatches(target, basePath, patches, inversePatches);
471
+ }
472
+ target.finalized = true;
473
+ }
474
+ }
475
+ /**
476
+ * Finalize one draft node: write the final value back into the parent's copy,
477
+ * generate its patches and run callbacks registered for it, for example
478
+ * because it was assigned to another location.
479
+ */
480
+ function finalizeNode(node, generatePatches, patches, inversePatches) {
481
+ const parent = node.parent;
482
+ if (parent) {
483
+ const parentType = parent.type;
484
+ const copy = parentType === 3 ? parent.setMap : parent.copy;
485
+ const key = node.key;
486
+ const isMapLike = parentType === 3 || parentType === 2;
487
+ const draft = isMapLike ? copy.get(key) : copy[key];
488
+ const proxyDraft = draft === node.proxy ? node : getProxyDraft(draft);
489
+ if (proxyDraft !== node) finalizeSetValue(node);
490
+ if (proxyDraft) {
491
+ const updatedValue = proxyDraft.operated ? proxyDraft.copy : proxyDraft.original;
492
+ if (proxyDraft.finalities === node.finalities) finalizeSetValue(proxyDraft);
493
+ finalizePatches(proxyDraft, generatePatches, patches, inversePatches);
494
+ if (isMapLike) copy.set(key, updatedValue);
495
+ else copy[key] = updatedValue;
496
+ }
497
+ } else {
498
+ finalizeSetValue(node);
499
+ finalizePatches(node, generatePatches, patches, inversePatches);
500
+ }
501
+ const callbacks = node.callbacks;
502
+ if (callbacks) for (let index = 0; index < callbacks.length; index += 1) callbacks[index](patches, inversePatches);
503
+ }
504
+ function markFinalization(target, key, value) {
505
+ const proxyDraft = getProxyDraft(value);
506
+ if (proxyDraft) {
507
+ if (!proxyDraft.callbacks) proxyDraft.callbacks = [];
508
+ proxyDraft.callbacks.push(() => {
509
+ const copy = target.type === 3 ? target.setMap : target.copy;
510
+ if (isEqual(get(copy, key), value)) {
511
+ const updatedValue = proxyDraft.operated ? proxyDraft.copy : proxyDraft.original;
512
+ if (proxyDraft.finalities !== target.finalities) finalizeSetValue(target, true);
513
+ set(copy, key, updatedValue);
514
+ }
515
+ });
516
+ if (target.options.enableAutoFreeze) {
517
+ if (proxyDraft.finalities !== target.finalities) target.options.enableAutoFreeze = false;
518
+ }
519
+ } else if (isDraftable(value, target.options)) target.finalities.draft.push(() => {
520
+ const copy = target.type === 3 ? target.setMap : target.copy;
521
+ if (isEqual(get(copy, key), value)) finalizeAssigned(target, key);
522
+ });
523
+ }
524
+
525
+ //#endregion
526
+ //#region src/unsafe.ts
527
+ let readable = false;
528
+ const checkReadable = (value, options, ignoreCheckDraftable = false) => {
529
+ if (typeof value === "object" && value !== null && (!isDraftable(value, options) || ignoreCheckDraftable) && !readable) die(8);
530
+ };
531
+ const checksReads = (options) => !!options.strict && !readable;
532
+ /**
533
+ * `unsafe(callback)` to access mutable data directly in strict mode.
534
+ *
535
+ * ## Example
536
+ *
537
+ * ```ts
538
+ * import { create, unsafe } from 'mutative';
539
+ *
540
+ * class Foobar {
541
+ * bar = 1;
542
+ * }
543
+ *
544
+ * const baseState = { foobar: new Foobar() };
545
+ * const state = create(
546
+ * baseState,
547
+ * (draft) => {
548
+ * unsafe(() => {
549
+ * draft.foobar.bar = 2;
550
+ * });
551
+ * },
552
+ * {
553
+ * strict: true,
554
+ * }
555
+ * );
556
+ *
557
+ * expect(state).toBe(baseState);
558
+ * expect(state.foobar).toBe(baseState.foobar);
559
+ * expect(state.foobar.bar).toBe(2);
560
+ * ```
561
+ */
562
+ function unsafe(callback) {
563
+ const previous = readable;
564
+ readable = true;
565
+ try {
566
+ return callback();
567
+ } finally {
568
+ readable = previous;
569
+ }
570
+ }
571
+
572
+ //#endregion
573
+ //#region src/map.ts
574
+ function getEntry(target, key) {
575
+ var _a, _b;
576
+ const value = latest(target).get(key);
577
+ const mutable = ((_b = (_a = target.options).mark) === null || _b === void 0 ? void 0 : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
578
+ if (target.options.strict) checkReadable(value, target.options, mutable);
579
+ if (mutable) return value;
580
+ if (target.finalized || !isDraftable(value, target.options)) return value;
581
+ if (value !== target.original.get(key)) return value;
582
+ const draft = internal.createDraft(value, target, key, target.finalities, target.options);
583
+ ensureShallowCopy(target);
584
+ target.copy.set(key, draft);
585
+ return draft;
586
+ }
587
+ const mapHandler = {
588
+ get size() {
589
+ return latest(getProxyDraft(this)).size;
590
+ },
591
+ has(key) {
592
+ return latest(getProxyDraft(this)).has(key);
593
+ },
594
+ set(key, value) {
595
+ {
596
+ let isDraftKey = false;
597
+ try {
598
+ isDraftKey = !!getProxyDraft(key);
599
+ } catch (_a) {}
600
+ if (isDraftKey) throw new Error(`A draft cannot be a Map key: its producer revokes it when it ends. Use original(key), current(key) or an id as the key; see https://mutative.js.org/docs/extra-topics/faq`);
601
+ }
602
+ const target = getProxyDraft(this);
603
+ const source = latest(target);
604
+ if (!source.has(key) || !isEqual(source.get(key), value)) {
605
+ ensureShallowCopy(target);
606
+ markChanged(target);
607
+ target.assignedMap.set(key, true);
608
+ target.copy.set(key, value);
609
+ markFinalization(target, key, value);
610
+ }
611
+ return this;
612
+ },
613
+ delete(key) {
614
+ if (!this.has(key)) return false;
615
+ const target = getProxyDraft(this);
616
+ ensureShallowCopy(target);
617
+ markChanged(target);
618
+ if (target.original.has(key)) target.assignedMap.set(key, false);
619
+ else target.assignedMap.delete(key);
620
+ target.copy.delete(key);
621
+ return true;
622
+ },
623
+ clear() {
624
+ const target = getProxyDraft(this);
625
+ if (!this.size) return;
626
+ ensureShallowCopy(target);
627
+ markChanged(target);
628
+ target.assignedMap = /* @__PURE__ */ new Map();
629
+ for (const [key] of target.original) target.assignedMap.set(key, false);
630
+ target.copy.clear();
631
+ },
632
+ forEach(callback, thisArg) {
633
+ const target = getProxyDraft(this);
634
+ latest(target).forEach((_value, key) => {
635
+ assertDraftActive(target);
636
+ callback.call(thisArg, getEntry(target, key), key, this);
637
+ });
638
+ },
639
+ get(key) {
640
+ return getEntry(getProxyDraft(this), key);
641
+ },
642
+ keys() {
643
+ return latest(getProxyDraft(this)).keys();
644
+ },
645
+ values() {
646
+ const target = getProxyDraft(this);
647
+ const iterator = this.keys();
648
+ return {
649
+ __proto__: iteratorPrototype,
650
+ next: () => {
651
+ const result = iterator.next();
652
+ if (result.done) return result;
653
+ assertDraftActive(target);
654
+ return {
655
+ done: false,
656
+ value: getEntry(target, result.value)
657
+ };
658
+ }
659
+ };
660
+ },
661
+ entries() {
662
+ const target = getProxyDraft(this);
663
+ const iterator = this.keys();
664
+ return {
665
+ __proto__: iteratorPrototype,
666
+ next: () => {
667
+ const result = iterator.next();
668
+ if (result.done) return result;
669
+ assertDraftActive(target);
670
+ return {
671
+ done: false,
672
+ value: [result.value, getEntry(target, result.value)]
673
+ };
674
+ }
675
+ };
676
+ },
677
+ [iteratorSymbol]() {
678
+ return this.entries();
679
+ }
680
+ };
681
+ const mapHandlerKeys = Reflect.ownKeys(mapHandler);
682
+
683
+ //#endregion
684
+ //#region src/set.ts
685
+ const getNextIterator = (target, iterator, isValuesIterator) => () => {
686
+ var _a, _b;
687
+ const result = iterator.next();
688
+ if (result.done) return result;
689
+ assertDraftActive(target);
690
+ const key = result.value;
691
+ let value = setItemValue(target, key);
692
+ const currentDraft = getProxyDraft(value);
693
+ const mutable = ((_b = (_a = target.options).mark) === null || _b === void 0 ? void 0 : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
694
+ if (target.options.strict) checkReadable(key, target.options, mutable);
695
+ if (!mutable && !currentDraft && value === key && isDraftable(key, target.options) && !target.finalized && target.original.has(key)) {
696
+ const proxy = internal.createDraft(key, target, key, target.finalities, target.options);
697
+ getSetMap(target).set(key, proxy);
698
+ value = proxy;
699
+ } else if (currentDraft) value = currentDraft.proxy;
700
+ return {
701
+ done: false,
702
+ value: isValuesIterator ? value : [value, value]
703
+ };
704
+ };
705
+ const setHandler = {
706
+ get size() {
707
+ return latest(getProxyDraft(this)).size;
708
+ },
709
+ has(value) {
710
+ const items = latest(getProxyDraft(this));
711
+ let valueProxyDraft;
712
+ return items.has(value) || !!(valueProxyDraft = getProxyDraft(value)) && items.has(valueProxyDraft.original);
713
+ },
714
+ add(value) {
715
+ const target = getProxyDraft(this);
716
+ if (!this.has(value)) {
717
+ ensureShallowCopy(target);
718
+ markChanged(target);
719
+ target.assignedMap.set(value, true);
720
+ target.copy.add(value);
721
+ if (value && typeof value === "object") getSetMap(target).set(value, value);
722
+ markFinalization(target, value, value);
723
+ }
724
+ return this;
725
+ },
726
+ delete(value) {
727
+ if (!this.has(value)) return false;
728
+ const target = getProxyDraft(this);
729
+ ensureShallowCopy(target);
730
+ markChanged(target);
731
+ const copy = target.copy;
732
+ const valueProxyDraft = getProxyDraft(value);
733
+ const item = valueProxyDraft && copy.has(valueProxyDraft.original) ? valueProxyDraft.original : value;
734
+ if (valueProxyDraft && item === value) target.assignedMap.delete(value);
735
+ else target.assignedMap.set(item, false);
736
+ if (target.setMap) target.setMap.delete(item);
737
+ return copy.delete(item);
738
+ },
739
+ clear() {
740
+ if (!this.size) return;
741
+ const target = getProxyDraft(this);
742
+ ensureShallowCopy(target);
743
+ markChanged(target);
744
+ for (const value of target.original) target.assignedMap.set(value, false);
745
+ if (target.setMap) target.setMap.clear();
746
+ target.copy.clear();
747
+ },
748
+ values() {
749
+ const target = getProxyDraft(this);
750
+ ensureShallowCopy(target);
751
+ const iterator = iterateSet(target.copy);
752
+ return {
753
+ __proto__: iteratorPrototype,
754
+ next: getNextIterator(target, iterator, true)
755
+ };
756
+ },
757
+ entries() {
758
+ const target = getProxyDraft(this);
759
+ ensureShallowCopy(target);
760
+ const iterator = iterateSet(target.copy);
761
+ return {
762
+ __proto__: iteratorPrototype,
763
+ next: getNextIterator(target, iterator, false)
764
+ };
765
+ },
766
+ keys() {
767
+ return this.values();
768
+ },
769
+ [iteratorSymbol]() {
770
+ return this.values();
771
+ },
772
+ forEach(callback, thisArg) {
773
+ const iterator = this.values();
774
+ let result = iterator.next();
775
+ while (!result.done) {
776
+ callback.call(thisArg, result.value, result.value, this);
777
+ result = iterator.next();
778
+ }
779
+ }
780
+ };
781
+ if ("difference" in Set.prototype) [
782
+ "intersection",
783
+ "union",
784
+ "difference",
785
+ "symmetricDifference",
786
+ "isSubsetOf",
787
+ "isSupersetOf",
788
+ "isDisjointFrom"
789
+ ].forEach((method) => {
790
+ setHandler[method] = function(other) {
791
+ return Set.prototype[method].call(new Set(this.values()), other);
792
+ };
793
+ });
794
+ const setHandlerKeys = Reflect.ownKeys(setHandler);
795
+
796
+ //#endregion
797
+ //#region src/array.ts
798
+ const arrayProto = Array.prototype;
799
+ const arrayIncludes = arrayProto.includes;
800
+ const hasOwn$2 = Object.prototype.hasOwnProperty;
801
+ let recentProxy = null;
802
+ let recentRevoke = null;
803
+ const arrayProxies = /* @__PURE__ */ new WeakSet();
804
+ /**
805
+ * Records that the get trap handed out an optimized method of `target`. Only
806
+ * a switch between arrays of a live producer adds a weak-collection entry, so
807
+ * the common case of one array per producer allocates nothing.
808
+ */
809
+ function trackArrayMethod(target) {
810
+ const proxy = target.proxy;
811
+ if (proxy !== recentProxy) {
812
+ if (recentRevoke !== null && recentRevoke.length > 0) arrayProxies.add(recentProxy);
813
+ if (target.finalities.scoped) {
814
+ recentProxy = proxy;
815
+ recentRevoke = target.finalities.revoke;
816
+ } else {
817
+ arrayProxies.add(proxy);
818
+ recentProxy = null;
819
+ recentRevoke = null;
820
+ }
821
+ }
822
+ }
823
+ /**
824
+ * Called by `create` whenever a producer ends, normally or by an error, so
825
+ * this module keeps no reference to its drafts. Another producer's entry, such
826
+ * as an outer producer's, is left in place.
827
+ */
828
+ function releaseArrayMethods(revoke) {
829
+ if (recentRevoke === revoke) {
830
+ recentProxy = null;
831
+ recentRevoke = null;
832
+ }
833
+ }
834
+ function canExecute(value) {
835
+ return typeof value === "object" && value !== null || typeof value === "function";
836
+ }
837
+ /**
838
+ * The array draft behind `this` when a method may run natively on its copy:
839
+ * plain arrays only, outside `current()` and without a custom mark.
840
+ */
841
+ function nativeState(self) {
842
+ if (self !== recentProxy && !arrayProxies.has(self)) return null;
843
+ const target = self[PROXY_DRAFT];
844
+ return !target.finalized && !target.options.mark && isPlainArray(target.original) ? target : null;
845
+ }
846
+ const toInteger = (value) => Math.trunc(+value) || 0;
847
+ function relativeIndex(value, length, fallback) {
848
+ if (value === void 0) return fallback;
849
+ const integer = toInteger(value);
850
+ return integer < 0 ? Math.max(length + integer, 0) : Math.min(integer, length);
851
+ }
852
+ function arrayState(target) {
853
+ var _a;
854
+ return (_a = target.arrayState) !== null && _a !== void 0 ? _a : target.arrayState = {
855
+ relocated: false,
856
+ diffStart: 0,
857
+ diffEnd: 0,
858
+ baseRefs: null,
859
+ delta: 0,
860
+ lookups: 0,
861
+ inert: null,
862
+ dense: null
863
+ };
864
+ }
865
+ function hasHoles(array) {
866
+ if (!arrayIncludes.call(array, void 0)) return false;
867
+ for (let index = 0; index < array.length; index += 1) if (!(index in array)) return true;
868
+ return false;
869
+ }
870
+ function isDense(target) {
871
+ const state = arrayState(target);
872
+ let dense = state.dense;
873
+ if (dense === null) dense = state.dense = !hasHoles(latest(target));
874
+ return dense;
875
+ }
876
+ function baseIndices(target) {
877
+ const state = arrayState(target);
878
+ let indices = state.baseRefs;
879
+ if (indices === null) {
880
+ indices = state.baseRefs = /* @__PURE__ */ new Map();
881
+ const original = target.original;
882
+ for (let index = 0; index < original.length; index += 1) indices.set(original[index], index);
883
+ }
884
+ return indices;
885
+ }
886
+ /**
887
+ * The original index of `value`, found at `index` once elements have moved
888
+ * natively, or undefined when it is not an element of the original array. The
889
+ * first few lookups find the index the map of original indices holds for a
890
+ * repeated element without indexing the array; later ones build the map.
891
+ */
892
+ function baseIndexOf(target, value, index) {
893
+ const state = arrayState(target);
894
+ if (state.baseRefs === null && state.lookups < 8) {
895
+ state.lookups += 1;
896
+ const original = target.original;
897
+ let found = +index + state.delta;
898
+ if (!(found >= 0 && original[found] === value)) {
899
+ found = arrayProto.lastIndexOf.call(original, value);
900
+ return found === -1 ? void 0 : found;
901
+ }
902
+ let step = 1;
903
+ for (let next, copies = 0; (next = arrayProto.indexOf.call(original, value, found + step)) !== -1;) {
904
+ found = next;
905
+ if (++copies > 16) step *= 2;
906
+ }
907
+ return step > 1 ? arrayProto.lastIndexOf.call(original, value, found + step - 1) : found;
908
+ }
909
+ return baseIndices(target).get(value);
910
+ }
911
+ /**
912
+ * The original index under which a read of `value` at `index` drafts it, or
913
+ * -1 when the read hands it out as is: an element at its original index, or,
914
+ * once native operations moved elements, any element of the original array
915
+ * except a value the recipe assigned at `index`, whose record moves along
916
+ * with it. Reads, searches and removals all decide by it.
917
+ */
918
+ function baseIndex(target, value, index) {
919
+ if (target.original[index] === value) return index;
920
+ const state = target.arrayState;
921
+ if (state === null || !state.relocated || target.assignedMap.get(String(index))) return -1;
922
+ const found = baseIndexOf(target, value, index);
923
+ return found === void 0 ? -1 : found;
924
+ }
925
+ function childAt(target, index) {
926
+ var _a;
927
+ return target.child !== null && target.childKey === String(index) ? target.child : (_a = target.children) === null || _a === void 0 ? void 0 : _a[index];
928
+ }
929
+ function registerChild(target, index, draft) {
930
+ var _a;
931
+ if (target.child === null) {
932
+ target.child = draft;
933
+ target.childKey = String(index);
934
+ } else ((_a = target.children) !== null && _a !== void 0 ? _a : target.children = [])[index] = draft;
935
+ }
936
+ function registerAssigned(target, index, value) {
937
+ const key = String(index);
938
+ const state = target.arrayState;
939
+ if (state !== null && canExecute(value)) state.inert = null;
940
+ const original = target.original;
941
+ if (hasOwn$2.call(original, index) && isEqual(value, original[index])) return;
942
+ target.assignedMap.set(key, true);
943
+ if (typeof value === "object" && value !== null) markFinalization(target, key, value);
944
+ }
945
+ function isInert(target) {
946
+ const state = arrayState(target);
947
+ let inert = state.inert;
948
+ if (inert === null) {
949
+ const source = latest(target);
950
+ inert = true;
951
+ for (let index = 0; index < source.length; index += 1) if (canExecute(source[index])) {
952
+ inert = false;
953
+ break;
954
+ }
955
+ state.inert = inert;
956
+ }
957
+ return inert;
958
+ }
959
+ function markRange(target, from, to) {
960
+ const state = arrayState(target);
961
+ if (state.diffEnd > state.diffStart) {
962
+ state.diffStart = Math.min(state.diffStart, from);
963
+ state.diffEnd = Math.max(state.diffEnd, to);
964
+ } else {
965
+ state.diffStart = from;
966
+ state.diffEnd = to;
967
+ }
968
+ state.relocated = true;
969
+ }
970
+ /**
971
+ * After the proxy lengthened an array draft, by its length or by an assignment
972
+ * past its end, holes follow. An index that the array exposes again below the
973
+ * original length lost its element to a shorter length earlier; no assignment
974
+ * records such an index, and a native move may have dropped one, so patches
975
+ * compare it. Marking the range as moved leaves reads as they are: an exposed
976
+ * index holds a hole, and an assigned value is handed out as it is.
977
+ */
978
+ function resized(target, previous) {
979
+ const state = target.arrayState;
980
+ if (state !== null) state.dense = null;
981
+ const end = Math.min(target.copy.length, target.original.length);
982
+ if (previous < end) markRange(target, previous, end);
983
+ }
984
+ /**
985
+ * After a native operation moved elements: drafts created here take their
986
+ * new keys, assigned values are registered again at their new indices, and
987
+ * the array remembers that identity against the original index can no longer
988
+ * tell a moved base element from an assigned value.
989
+ */
990
+ function relocate(target, map, from, to, delta) {
991
+ var _a;
992
+ markRange(target, from, to);
993
+ target.arrayState.delta = delta;
994
+ const copy = target.copy;
995
+ const entries = [];
996
+ if (target.child !== null) entries.push([Number(target.childKey), target.child]);
997
+ (_a = target.children) === null || _a === void 0 || _a.forEach((draft, index) => {
998
+ entries.push([index, draft]);
999
+ });
1000
+ target.child = null;
1001
+ target.childKey = null;
1002
+ target.children = void 0;
1003
+ for (let position = 0; position < entries.length; position += 1) {
1004
+ const [index, draft] = entries[position];
1005
+ const next = map(index);
1006
+ if (next >= 0) {
1007
+ registerChild(target, next, draft);
1008
+ if (next !== index) markFinalization(target, String(next), draft);
1009
+ }
1010
+ }
1011
+ const assignedMap = target.assignedMap;
1012
+ const assigned = [];
1013
+ assignedMap.forEach((flag, key) => {
1014
+ if (key !== "length") assigned.push([key, flag]);
1015
+ });
1016
+ for (let position = 0; position < assigned.length; position += 1) assignedMap.delete(assigned[position][0]);
1017
+ for (let position = 0; position < assigned.length; position += 1) {
1018
+ const [key, flag] = assigned[position];
1019
+ const index = Number(key);
1020
+ const next = map(index);
1021
+ if (next >= 0) {
1022
+ if (flag && next !== index) registerAssigned(target, next, copy[next]);
1023
+ else assignedMap.set(String(next), flag);
1024
+ }
1025
+ }
1026
+ }
1027
+ function removalKey(target, index) {
1028
+ const value = latest(target)[index];
1029
+ if (typeof value !== "object" || value === null) return -1;
1030
+ if (childAt(target, index) === value) return value;
1031
+ return isDraftable(value, target.options) ? baseIndex(target, value, index) : -1;
1032
+ }
1033
+ function removed(target, value, key) {
1034
+ if (typeof key !== "number") return key;
1035
+ if (key < 0) return value;
1036
+ ensureShallowCopy(target);
1037
+ return internal.createDraft(value, target, key, target.finalities, target.options);
1038
+ }
1039
+ function prepare(target) {
1040
+ ensureShallowCopy(target);
1041
+ markChanged(target);
1042
+ return target.copy;
1043
+ }
1044
+ function native(method, impl) {
1045
+ const original = arrayProto[method];
1046
+ return function(...args) {
1047
+ const target = nativeState(this);
1048
+ return target === null || checksReads(target.options) && !isInert(target) ? original.apply(this, args) : impl(target, this, args, original);
1049
+ };
1050
+ }
1051
+ function isBaseElement(target, value, index) {
1052
+ return childAt(target, index) !== value && baseIndex(target, value, index) !== -1;
1053
+ }
1054
+ /**
1055
+ * Whether `value`, which the array already holds at `index`, is an element of
1056
+ * the base state that native methods moved there. The proxy path moves
1057
+ * elements by reading them, so it holds a draft there, and assigning the raw
1058
+ * element replaces the draft and records an assignment; the set trap records
1059
+ * one as well instead of ignoring the assignment.
1060
+ */
1061
+ function isMovedElement(target, value, index) {
1062
+ return typeof value === "object" && value !== null && value !== target.original[index] && baseIndex(target, value, index) !== -1;
1063
+ }
1064
+ function search(method) {
1065
+ const nativeSearch = arrayProto[method];
1066
+ const backwards = method === "lastIndexOf";
1067
+ const find = backwards ? arrayProto.lastIndexOf : arrayProto.indexOf;
1068
+ return native(method, (target, self, args, original) => {
1069
+ const source = latest(target);
1070
+ if (source.length === 0) return method === "includes" ? false : -1;
1071
+ if (canExecute(args[1])) return original.apply(self, args);
1072
+ const value = args[0];
1073
+ if (typeof value !== "object" || value === null) return nativeSearch.apply(source, args);
1074
+ let index = args.length > 1 ? find.call(source, value, args[1]) : find.call(source, value);
1075
+ while (index !== -1) {
1076
+ if (!hasOwn$2.call(source, index)) return original.apply(self, args);
1077
+ if (!isBaseElement(target, value, index)) break;
1078
+ if (!isDense(target)) return original.apply(self, args);
1079
+ if (!isDraftable(value, target.options)) break;
1080
+ index = backwards ? index === 0 ? -1 : find.call(source, value, index - 1) : find.call(source, value, index + 1);
1081
+ }
1082
+ return method === "includes" ? index !== -1 : index;
1083
+ });
1084
+ }
1085
+ /**
1086
+ * Array methods without callbacks run natively on the draft's copy instead
1087
+ * of moving every element through the proxy traps.
1088
+ */
1089
+ const arrayMethods = Object.assign(Object.create(null), {
1090
+ indexOf: search("indexOf"),
1091
+ lastIndexOf: search("lastIndexOf"),
1092
+ includes: search("includes"),
1093
+ join: native("join", (target, self, args) => arrayProto.join.call(canExecute(args[0]) || !isInert(target) ? self : latest(target), args[0])),
1094
+ shift: native("shift", (target, self, args, original) => {
1095
+ if (latest(target).length === 0) return void 0;
1096
+ if (!isDense(target)) return original.apply(self, args);
1097
+ const key = removalKey(target, 0);
1098
+ const copy = prepare(target);
1099
+ const value = arrayProto.shift.call(copy);
1100
+ relocate(target, (index) => index - 1, 0, copy.length + 1, 1);
1101
+ return removed(target, value, key);
1102
+ }),
1103
+ unshift: native("unshift", (target, self, items, original) => {
1104
+ const count = items.length;
1105
+ if (count === 0) return latest(target).length;
1106
+ if (!isDense(target)) return original.apply(self, items);
1107
+ const copy = prepare(target);
1108
+ arrayProto.unshift.apply(copy, items);
1109
+ relocate(target, (index) => index + count, 0, copy.length, -count);
1110
+ for (let index = 0; index < count; index += 1) registerAssigned(target, index, items[index]);
1111
+ return copy.length;
1112
+ }),
1113
+ splice: native("splice", (target, self, args, original) => {
1114
+ if (canExecute(args[0]) || canExecute(args[1])) return original.apply(self, args);
1115
+ const length = latest(target).length;
1116
+ const start = relativeIndex(args[0], length, 0);
1117
+ const deleteCount = args.length === 0 ? 0 : args.length === 1 ? length - start : Math.min(Math.max(toInteger(args[1]), 0), length - start);
1118
+ const insertCount = Math.max(args.length - 2, 0);
1119
+ if (deleteCount === 0 && insertCount === 0) return original.apply(self, args);
1120
+ const source = latest(target);
1121
+ if (deleteCount === insertCount) {
1122
+ let same = true;
1123
+ for (let index = 0; same && index < insertCount; index += 1) {
1124
+ const at = start + index;
1125
+ const value = source[at];
1126
+ const item = args[index + 2];
1127
+ if (!isEqual(item, value)) {
1128
+ if (typeof value !== "object" || value === null || hasOwn$2.call(source, at) && isBaseElement(target, value, at)) same = false;
1129
+ else if (childAt(target, at) === value) same = isEqual(item, getProxyDraft(value).original);
1130
+ else return original.apply(self, args);
1131
+ }
1132
+ }
1133
+ if (same) return original.apply(self, args);
1134
+ }
1135
+ if (!isDense(target)) return original.apply(self, args);
1136
+ const keys = [];
1137
+ for (let index = 0; index < deleteCount; index += 1) keys.push(removalKey(target, start + index));
1138
+ const copy = prepare(target);
1139
+ const spliceArgs = [start, deleteCount];
1140
+ for (let index = 2; index < args.length; index += 1) spliceArgs.push(args[index]);
1141
+ const values = arrayProto.splice.apply(copy, spliceArgs);
1142
+ relocate(target, (index) => index < start ? index : index < start + deleteCount ? -1 : index - deleteCount + insertCount, start, deleteCount === insertCount ? start + deleteCount : Math.max(length, copy.length), deleteCount - insertCount);
1143
+ for (let index = 0; index < insertCount; index += 1) registerAssigned(target, start + index, args[index + 2]);
1144
+ for (let index = 0; index < values.length; index += 1) values[index] = removed(target, values[index], keys[index]);
1145
+ return values;
1146
+ }),
1147
+ sort: native("sort", (target, self, [compare], original) => {
1148
+ if (compare !== void 0 && typeof compare !== "function" || !isInert(target) || !isDense(target)) return original.call(self, compare);
1149
+ const source = latest(target);
1150
+ const length = source.length;
1151
+ const sorted = new Array(length);
1152
+ for (let index = 0; index < length; index += 1) sorted[index] = source[index];
1153
+ arrayProto.sort.call(sorted, compare);
1154
+ let changed = false;
1155
+ for (let index = 0; index < length; index += 1) if (!isEqual(sorted[index], source[index])) {
1156
+ changed = true;
1157
+ break;
1158
+ }
1159
+ const copy = changed ? prepare(target) : target.copy;
1160
+ if (copy !== null) for (let index = 0; index < length; index += 1) copy[index] = sorted[index];
1161
+ if (changed) markRange(target, 0, length);
1162
+ return self;
1163
+ }),
1164
+ reverse: native("reverse", (target, self, args, original) => {
1165
+ const source = latest(target);
1166
+ const length = source.length;
1167
+ if (length < 2) return self;
1168
+ if (!isDense(target)) return original.apply(self, args);
1169
+ const assigned = target.assignedMap;
1170
+ let changed = assigned !== void 0 && assigned.size > 0;
1171
+ for (let low = 0, high = length - 1; !changed && low < high; low += 1, high -= 1) changed = !isEqual(source[low], source[high]);
1172
+ if (changed) {
1173
+ arrayProto.reverse.call(prepare(target));
1174
+ relocate(target, (index) => length - 1 - index, 0, length);
1175
+ }
1176
+ return self;
1177
+ })
1178
+ });
1179
+
1180
+ //#endregion
1181
+ //#region src/current.ts
1182
+ function handleReturnValue(rootDraft, value, useRawReturn) {
1183
+ let containsDraft = false;
1184
+ const skipsFrozen = !!(rootDraft === null || rootDraft === void 0 ? void 0 : rootDraft.options.enableAutoFreeze);
1185
+ const replaceDrafts = (target, frozen) => forEach(target, (key, item, source) => {
1186
+ const proxyDraft = getProxyDraft(item);
1187
+ if (proxyDraft && rootDraft && proxyDraft.finalities === rootDraft.finalities) {
1188
+ if (frozen) throw new Error(`The return value holds a draft in a frozen object or in a value it holds. With auto-freeze, production builds do not search frozen objects for drafts, so do not put drafts there.`);
1189
+ containsDraft = true;
1190
+ const currentValue = proxyDraft.original;
1191
+ if (source instanceof Set) {
1192
+ const arr = Array.from(source);
1193
+ source.clear();
1194
+ arr.forEach((_item) => source.add(key === _item ? currentValue : _item));
1195
+ } else set(source, key, currentValue);
1196
+ } else if (typeof item === "object" && item !== null) {
1197
+ if (!skipsFrozen || !Object.isFrozen(item)) replaceDrafts(item, frozen);
1198
+ else replaceDrafts(item, true);
1199
+ }
1200
+ });
1201
+ if (!skipsFrozen || !Object.isFrozen(value)) replaceDrafts(value);
1202
+ else replaceDrafts(value, true);
1203
+ if (useRawReturn) {
1204
+ if (containsDraft) console.warn(`The return value contains drafts, please don't use 'rawReturn()' to wrap the return value.`);
1205
+ } else if (!containsDraft) console.warn(`The return value does not contain any draft, please use 'rawReturn()' to wrap the return value to improve performance.`);
1206
+ }
1207
+ function getCurrent(target, rootOptions) {
1208
+ var _a;
1209
+ const proxyDraft = getProxyDraft(target);
1210
+ const options = proxyDraft ? proxyDraft.options : rootOptions;
1211
+ if (!isDraftable(target, options)) return target;
1212
+ const type = getType(target);
1213
+ if (proxyDraft && !proxyDraft.operated) return proxyDraft.original;
1214
+ const array = type === 1 && proxyDraft && !checksReads(proxyDraft.options) ? proxyDraft : null;
1215
+ let currentValue;
1216
+ let changed = false;
1217
+ function ensureShallowCopy() {
1218
+ currentValue = type === 2 ? !isBaseMapInstance(target) ? new (Object.getPrototypeOf(target)).constructor(target) : new Map(target) : type === 3 ? proxyDraft ? Array.from(iterateSet(latest(proxyDraft)), (item) => setItemValue(proxyDraft, item)) : Array.from(target) : shallowCopy(array ? latest(array) : target, options);
1219
+ }
1220
+ if (proxyDraft) {
1221
+ proxyDraft.finalized = true;
1222
+ try {
1223
+ ensureShallowCopy();
1224
+ } finally {
1225
+ proxyDraft.finalized = false;
1226
+ }
1227
+ } else if (type === 3) ensureShallowCopy();
1228
+ else currentValue = target;
1229
+ forEach(currentValue, (key, value) => {
1230
+ if (proxyDraft && (isEqual(get(proxyDraft.original, key), value) || array && !isDraft(value) && (typeof value !== "object" || !(array.assignedMap.size && array.assignedMap.get(String(key)))))) return;
1231
+ const newValue = getCurrent(value, options);
1232
+ if (newValue !== value) {
1233
+ changed = true;
1234
+ if (currentValue === target) ensureShallowCopy();
1235
+ set(currentValue, key, newValue);
1236
+ }
1237
+ });
1238
+ if (type === 3) {
1239
+ if (!proxyDraft && !changed) return target;
1240
+ const value = (_a = proxyDraft === null || proxyDraft === void 0 ? void 0 : proxyDraft.original) !== null && _a !== void 0 ? _a : target;
1241
+ return !isBaseSetInstance(value) ? new (Object.getPrototypeOf(value)).constructor(currentValue) : new Set(currentValue);
1242
+ }
1243
+ return currentValue;
1244
+ }
1245
+ function current(target) {
1246
+ if (!isDraft(target)) die(7, target);
1247
+ return getCurrent(target);
1248
+ }
1249
+
1250
+ //#endregion
1251
+ //#region src/patch.ts
1252
+ /**
1253
+ * Patch values must not contain drafts. A draft that is still present when its
1254
+ * container generates patches is represented by its original object when it
1255
+ * was not modified, or by a snapshot of its current state otherwise.
1256
+ */
1257
+ function cloneIfNeeded(target) {
1258
+ const proxyDraft = getProxyDraft(target);
1259
+ if (!proxyDraft) return target;
1260
+ return proxyDraft.operated ? current(target) : proxyDraft.original;
1261
+ }
1262
+ const hasOwn$1 = Object.prototype.hasOwnProperty;
1263
+ const SPARSE_HOLE_LIMIT = 65536;
1264
+ function restoreSuffix(to, start, indices, basePath, result, pathAsArray) {
1265
+ const restore = (index) => result.push({
1266
+ op: Operation.Replace,
1267
+ path: escapePath(basePath.concat([index]), pathAsArray),
1268
+ value: cloneIfNeeded(to[index])
1269
+ });
1270
+ if (indices !== null) {
1271
+ for (let position = 0; position < indices.length; position += 1) {
1272
+ const index = indices[position];
1273
+ if (index >= start && index < to.length && hasOwn$1.call(to, index)) restore(index);
1274
+ }
1275
+ return;
1276
+ }
1277
+ let index = start;
1278
+ let holes = 0;
1279
+ while (index < to.length && holes <= SPARSE_HOLE_LIMIT) {
1280
+ if (hasOwn$1.call(to, index)) restore(index);
1281
+ else holes += 1;
1282
+ index += 1;
1283
+ }
1284
+ if (index === to.length) return;
1285
+ for (const key of Object.getOwnPropertyNames(to)) {
1286
+ const own = Number(key);
1287
+ if (Number.isInteger(own) && own >= index && own < to.length && String(own) === key) restore(own);
1288
+ }
1289
+ }
1290
+ function rebuildSuffix(from, to, start, indices, basePath, result, pathAsArray, arrayLengthAssignment) {
1291
+ const lengthPath = escapePath(basePath.concat(["length"]), pathAsArray);
1292
+ const cut = Math.min(start, to.length);
1293
+ if (arrayLengthAssignment) {
1294
+ if (from.length > cut) result.push({
1295
+ op: Operation.Replace,
1296
+ path: lengthPath,
1297
+ value: cut
1298
+ });
1299
+ } else for (let index = from.length - 1; index >= cut; index -= 1) result.push({
1300
+ op: Operation.Remove,
1301
+ path: escapePath(basePath.concat([index]), pathAsArray)
1302
+ });
1303
+ if (to.length <= cut) return;
1304
+ result.push({
1305
+ op: Operation.Replace,
1306
+ path: lengthPath,
1307
+ value: to.length
1308
+ });
1309
+ restoreSuffix(to, cut, indices, basePath, result, pathAsArray);
1310
+ }
1311
+ function generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray) {
1312
+ let { original, assignedMap, options } = proxyState;
1313
+ let copy = proxyState.copy;
1314
+ const { arrayLengthAssignment = true } = options.enablePatches;
1315
+ const shorter = copy.length < original.length;
1316
+ if (shorter) {
1317
+ [original, copy] = [copy, original];
1318
+ [patches, inversePatches] = [inversePatches, patches];
1319
+ }
1320
+ const arrayState = proxyState.arrayState;
1321
+ const diffStart = arrayState === null ? 0 : arrayState.diffStart;
1322
+ const diffEnd = arrayState === null ? 0 : arrayState.diffEnd;
1323
+ const assigned = [];
1324
+ assignedMap.forEach((flag, key) => {
1325
+ if (flag && key !== "length") {
1326
+ const index = Number(key);
1327
+ if (index < diffStart || index >= diffEnd) assigned.push(index);
1328
+ }
1329
+ });
1330
+ assigned.sort((a, b) => a - b);
1331
+ const indices = [];
1332
+ let position = 0;
1333
+ for (; position < assigned.length && assigned[position] < diffStart; position += 1) indices.push(assigned[position]);
1334
+ for (let index = diffStart; index < diffEnd; index += 1) indices.push(index);
1335
+ for (; position < assigned.length; position += 1) indices.push(assigned[position]);
1336
+ let forward = -1;
1337
+ let inverse = -1;
1338
+ for (let position = 0; position < indices.length; position += 1) {
1339
+ const index = indices[position];
1340
+ if (index >= original.length) break;
1341
+ const value = copy[index];
1342
+ const previous = original[index];
1343
+ let inCopy = true;
1344
+ let inOriginal = true;
1345
+ if (value === void 0 || previous === void 0) {
1346
+ inCopy = hasOwn$1.call(copy, index);
1347
+ inOriginal = hasOwn$1.call(original, index);
1348
+ }
1349
+ if (inCopy !== inOriginal || !isEqual(value, previous)) {
1350
+ const _path = basePath.concat([index]);
1351
+ const path = escapePath(_path, pathAsArray);
1352
+ if (forward === -1) {
1353
+ if (inCopy) patches.push({
1354
+ op: Operation.Replace,
1355
+ path,
1356
+ value: cloneIfNeeded(value)
1357
+ });
1358
+ else forward = index;
1359
+ }
1360
+ if (inverse === -1) {
1361
+ if (inOriginal) inversePatches.push({
1362
+ op: Operation.Replace,
1363
+ path,
1364
+ value: cloneIfNeeded(previous)
1365
+ });
1366
+ else inverse = index;
1367
+ }
1368
+ if (forward !== -1 && inverse !== -1) break;
1369
+ }
1370
+ }
1371
+ if (forward === -1) for (let index = original.length; index < copy.length; index += 1) {
1372
+ const value = copy[index];
1373
+ if (value === void 0 && !hasOwn$1.call(copy, index)) {
1374
+ forward = index;
1375
+ break;
1376
+ }
1377
+ const _path = basePath.concat([index]);
1378
+ const path = escapePath(_path, pathAsArray);
1379
+ patches.push({
1380
+ op: Operation.Add,
1381
+ path,
1382
+ value: cloneIfNeeded(value)
1383
+ });
1384
+ }
1385
+ if (forward !== -1) rebuildSuffix(original, copy, forward, shorter || forward < original.length ? null : indices, basePath, patches, pathAsArray, arrayLengthAssignment);
1386
+ if (inverse !== -1) rebuildSuffix(copy, original, inverse, null, basePath, inversePatches, pathAsArray, arrayLengthAssignment);
1387
+ else if (original.length < copy.length) {
1388
+ if (arrayLengthAssignment) {
1389
+ const _path = basePath.concat(["length"]);
1390
+ const path = escapePath(_path, pathAsArray);
1391
+ inversePatches.push({
1392
+ op: Operation.Replace,
1393
+ path,
1394
+ value: original.length
1395
+ });
1396
+ } else for (let index = copy.length; original.length < index; index -= 1) {
1397
+ const _path = basePath.concat([index - 1]);
1398
+ const path = escapePath(_path, pathAsArray);
1399
+ inversePatches.push({
1400
+ op: Operation.Remove,
1401
+ path
1402
+ });
1403
+ }
1404
+ }
1405
+ }
1406
+ function generatePatchesFromAssigned({ original, copy, assignedMap }, basePath, patches, inversePatches, pathAsArray) {
1407
+ assignedMap.forEach((assignedValue, key) => {
1408
+ const originalValue = get(original, key);
1409
+ const value = cloneIfNeeded(get(copy, key));
1410
+ const op = !assignedValue ? Operation.Remove : has(original, key) ? Operation.Replace : Operation.Add;
1411
+ if (isEqual(originalValue, value) && op === Operation.Replace) return;
1412
+ checkPathKey(getType(original), key, pathAsArray);
1413
+ const _path = basePath.concat([key]);
1414
+ const path = escapePath(_path, pathAsArray);
1415
+ patches.push(op === Operation.Remove ? {
1416
+ op,
1417
+ path
1418
+ } : {
1419
+ op,
1420
+ path,
1421
+ value
1422
+ });
1423
+ inversePatches.push(op === Operation.Add ? {
1424
+ op: Operation.Remove,
1425
+ path
1426
+ } : op === Operation.Remove ? {
1427
+ op: Operation.Add,
1428
+ path,
1429
+ value: originalValue
1430
+ } : {
1431
+ op: Operation.Replace,
1432
+ path,
1433
+ value: originalValue
1434
+ });
1435
+ });
1436
+ }
1437
+ function generateSetPatches({ original, copy }, basePath, patches, inversePatches, pathAsArray) {
1438
+ let index = 0;
1439
+ original.forEach((value) => {
1440
+ if (!copy.has(value)) {
1441
+ const _path = basePath.concat([index]);
1442
+ const path = escapePath(_path, pathAsArray);
1443
+ patches.push({
1444
+ op: Operation.Remove,
1445
+ path,
1446
+ value
1447
+ });
1448
+ inversePatches.unshift({
1449
+ op: Operation.Add,
1450
+ path,
1451
+ value
1452
+ });
1453
+ }
1454
+ index += 1;
1455
+ });
1456
+ index = 0;
1457
+ copy.forEach((value) => {
1458
+ if (!original.has(value)) {
1459
+ const _path = basePath.concat([index]);
1460
+ const path = escapePath(_path, pathAsArray);
1461
+ patches.push({
1462
+ op: Operation.Add,
1463
+ path,
1464
+ value
1465
+ });
1466
+ inversePatches.unshift({
1467
+ op: Operation.Remove,
1468
+ path,
1469
+ value
1470
+ });
1471
+ }
1472
+ index += 1;
1473
+ });
1474
+ }
1475
+ function generatePatches(proxyState, basePath, patches, inversePatches) {
1476
+ const { pathAsArray = true } = proxyState.options.enablePatches;
1477
+ switch (proxyState.type) {
1478
+ case 0:
1479
+ case 2: return generatePatchesFromAssigned(proxyState, basePath, patches, inversePatches, pathAsArray);
1480
+ case 1: return generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
1481
+ case 3: return generateSetPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
1482
+ }
1483
+ }
1484
+
1485
+ //#endregion
1486
+ //#region src/draft.ts
1487
+ /**
1488
+ * Accepts the keys the array set trap has always accepted: canonical
1489
+ * non-negative integer strings via an allocation-free scan, and other
1490
+ * spellings such as '-0' via the original numeric round trip.
1491
+ */
1492
+ function isArrayIndexKey(key) {
1493
+ if (typeof key === "string" && key.length < 16) {
1494
+ let index = 0;
1495
+ while (index < key.length) {
1496
+ const code = key.charCodeAt(index);
1497
+ if (code < 48 || code > 57) break;
1498
+ index += 1;
1499
+ }
1500
+ if (index > 0 && index === key.length && (index === 1 || key.charCodeAt(0) !== 48)) return true;
1501
+ }
1502
+ let _key;
1503
+ return Number.isInteger(_key = Number(key)) && _key >= 0 && (key === 0 || _key === 0 || String(_key) === String(key));
1504
+ }
1505
+ const hasOwn = Object.prototype.hasOwnProperty;
1506
+ function getTrap(target, key, receiver) {
1507
+ var _a, _b, _c;
1508
+ if (key === PROXY_DRAFT) return target;
1509
+ const { options, type } = target;
1510
+ const source = latest(target);
1511
+ let markResult;
1512
+ if (options.mark) {
1513
+ const value = key === "size" && (source instanceof Map || source instanceof Set) ? Reflect.get(source, key) : Reflect.get(source, key, receiver);
1514
+ markResult = options.mark(value, dataTypes);
1515
+ if (markResult === dataTypes.mutable) {
1516
+ if (options.strict) checkReadable(value, options, true);
1517
+ return value;
1518
+ }
1519
+ }
1520
+ if (type === 2) {
1521
+ if (mapHandlerKeys.includes(key)) {
1522
+ if (key === "size") return Object.getOwnPropertyDescriptor(mapHandler, "size").get.call(target.proxy);
1523
+ return mapHandler[key].bind(target.proxy);
1524
+ }
1525
+ } else if (type === 3) {
1526
+ if (setHandlerKeys.includes(key)) {
1527
+ if (key === "size") return Object.getOwnPropertyDescriptor(setHandler, "size").get.call(target.proxy);
1528
+ return setHandler[key].bind(target.proxy);
1529
+ }
1530
+ }
1531
+ if (type === 2 ? !source.has(key) : !hasOwn.call(source, key)) {
1532
+ if (type === 1 && Object.getPrototypeOf(target.original) === Array.prototype) {
1533
+ const method = arrayMethods[key];
1534
+ if (method !== void 0) {
1535
+ trackArrayMethod(target);
1536
+ return method;
1537
+ }
1538
+ }
1539
+ const desc = getDescriptor(source, key);
1540
+ return desc ? `value` in desc ? desc.value : (_a = desc.get) === null || _a === void 0 ? void 0 : _a.call(target.proxy) : void 0;
1541
+ }
1542
+ const value = source[key];
1543
+ if (options.strict) checkReadable(value, options);
1544
+ if (target.finalized || typeof value !== "object" || value === null) return value;
1545
+ if (value === target.child && key === target.childKey) return value;
1546
+ const children = target.children;
1547
+ if (children !== void 0 && (type === 1 ? children[key] : children.get(key)) === value) return value;
1548
+ if (!isDraftable(value, options)) return value;
1549
+ let draftKey = key;
1550
+ if (value !== target.original[key]) {
1551
+ draftKey = baseIndex(target, value, key);
1552
+ if (draftKey === -1) return value;
1553
+ }
1554
+ ensureShallowCopy(target);
1555
+ const draft = createDraft(value, target, type === 1 ? Number(draftKey) : draftKey, target.finalities, options);
1556
+ target.copy[key] = draft;
1557
+ if (draftKey !== key) markFinalization(target, key, draft);
1558
+ if (target.child === null || target.childKey === key) {
1559
+ target.child = draft;
1560
+ target.childKey = key;
1561
+ } else if (type === 1) ((_b = target.children) !== null && _b !== void 0 ? _b : target.children = [])[key] = draft;
1562
+ else ((_c = target.children) !== null && _c !== void 0 ? _c : target.children = /* @__PURE__ */ new Map()).set(key, draft);
1563
+ if (typeof markResult === "function") {
1564
+ const subProxyDraft = getProxyDraft(draft);
1565
+ ensureShallowCopy(subProxyDraft);
1566
+ markChanged(subProxyDraft);
1567
+ return subProxyDraft.copy;
1568
+ }
1569
+ return draft;
1570
+ }
1571
+ function setTrap(target, key, value) {
1572
+ var _a;
1573
+ if (target.type === 3 || target.type === 2) die(1);
1574
+ if (target.type === 1 && key !== "length" && !isArrayIndexKey(key)) die(2);
1575
+ const source = latest(target);
1576
+ if (!hasOwn.call(source, key)) {
1577
+ const desc = getDescriptor(source, key);
1578
+ if (desc === null || desc === void 0 ? void 0 : desc.set) {
1579
+ desc.set.call(target.proxy, value);
1580
+ return true;
1581
+ }
1582
+ }
1583
+ const current = source[key];
1584
+ const currentProxyDraft = getProxyDraft(current);
1585
+ const original = target.original;
1586
+ if (currentProxyDraft && isEqual(currentProxyDraft.original, value)) {
1587
+ target.copy[key] = value;
1588
+ target.assignedMap = (_a = target.assignedMap) !== null && _a !== void 0 ? _a : /* @__PURE__ */ new Map();
1589
+ if (hasOwn.call(original, key) && isEqual(value, original[key])) target.assignedMap.delete(key);
1590
+ else {
1591
+ markChanged(target);
1592
+ target.assignedMap.set(key, true);
1593
+ markFinalization(target, key, value);
1594
+ }
1595
+ return true;
1596
+ }
1597
+ if (isEqual(value, current) && (value !== void 0 || hasOwn.call(source, key)) && (target.arrayState === null || !isMovedElement(target, value, key))) return true;
1598
+ ensureShallowCopy(target);
1599
+ markChanged(target);
1600
+ const arrayState = target.arrayState;
1601
+ if (arrayState !== null && (typeof value === "object" && value !== null || typeof value === "function")) arrayState.inert = null;
1602
+ if (hasOwn.call(original, key) && isEqual(value, original[key])) target.assignedMap.delete(key);
1603
+ else target.assignedMap.set(key, true);
1604
+ const length = target.type === 1 ? target.copy.length : -1;
1605
+ target.copy[key] = value;
1606
+ if (length !== -1 && target.copy.length > length + (key === "length" ? 0 : 1)) resized(target, length);
1607
+ markFinalization(target, key, value);
1608
+ return true;
1609
+ }
1610
+ function hasTrap(target, key) {
1611
+ return key in latest(target);
1612
+ }
1613
+ function ownKeysTrap(target) {
1614
+ return Reflect.ownKeys(latest(target));
1615
+ }
1616
+ function getOwnPropertyDescriptorTrap(target, key) {
1617
+ const source = latest(target);
1618
+ const descriptor = Reflect.getOwnPropertyDescriptor(source, key);
1619
+ if (!descriptor) return descriptor;
1620
+ return {
1621
+ writable: true,
1622
+ configurable: target.type !== 1 || key !== "length",
1623
+ enumerable: descriptor.enumerable,
1624
+ value: source[key]
1625
+ };
1626
+ }
1627
+ function getPrototypeOfTrap(target) {
1628
+ return Reflect.getPrototypeOf(target.original);
1629
+ }
1630
+ function setPrototypeOfTrap() {
1631
+ die(3);
1632
+ }
1633
+ function definePropertyTrap() {
1634
+ die(4);
1635
+ }
1636
+ function deletePropertyTrap(target, key) {
1637
+ var _a;
1638
+ if (target.type === 1) {
1639
+ if (!hasOwn.call(latest(target), key) && isArrayIndexKey(key)) return true;
1640
+ return setTrap(target, key, void 0);
1641
+ }
1642
+ if (target.original[key] !== void 0 || key in target.original) {
1643
+ ensureShallowCopy(target);
1644
+ markChanged(target);
1645
+ target.assignedMap.set(key, false);
1646
+ } else {
1647
+ target.assignedMap = (_a = target.assignedMap) !== null && _a !== void 0 ? _a : /* @__PURE__ */ new Map();
1648
+ target.assignedMap.delete(key);
1649
+ }
1650
+ if (target.copy) delete target.copy[key];
1651
+ return true;
1652
+ }
1653
+ const objectHandler = {
1654
+ get: getTrap,
1655
+ set: setTrap,
1656
+ has: hasTrap,
1657
+ ownKeys: ownKeysTrap,
1658
+ getOwnPropertyDescriptor: getOwnPropertyDescriptorTrap,
1659
+ getPrototypeOf: getPrototypeOfTrap,
1660
+ setPrototypeOf: setPrototypeOfTrap,
1661
+ defineProperty: definePropertyTrap,
1662
+ deleteProperty: deletePropertyTrap
1663
+ };
1664
+ const arrayHandler = {};
1665
+ Object.keys(objectHandler).forEach((name) => {
1666
+ const trap = objectHandler[name];
1667
+ arrayHandler[name] = (target, key, value) => trap(target[0], key, value);
1668
+ });
1669
+ function createDraft(original, parentDraft, key, finalities, options) {
1670
+ const type = getType(original);
1671
+ const proxyDraft = {
1672
+ type,
1673
+ finalized: false,
1674
+ operated: false,
1675
+ parent: parentDraft,
1676
+ key,
1677
+ original,
1678
+ copy: null,
1679
+ proxy: null,
1680
+ finalities,
1681
+ options,
1682
+ setMap: void 0,
1683
+ assignedMap: void 0,
1684
+ callbacks: void 0,
1685
+ children: void 0,
1686
+ child: null,
1687
+ childKey: null,
1688
+ arrayState: null
1689
+ };
1690
+ const { proxy, revoke } = type === 1 ? Proxy.revocable([proxyDraft], arrayHandler) : Proxy.revocable(proxyDraft, objectHandler);
1691
+ finalities.revoke.push(revoke);
1692
+ proxyDraft.proxy = proxy;
1693
+ finalities.draft.push(proxyDraft);
1694
+ return proxy;
1695
+ }
1696
+ internal.createDraft = createDraft;
1697
+ const UNCHANGED_DRAFTS_WARNING_THRESHOLD = 1e3;
1698
+ let unchangedDraftsWarned = false;
1699
+ /**
1700
+ * Every object read through a draft becomes a draft, so a search through a
1701
+ * large draft array creates one per element it visits. Drafts that are
1702
+ * unchanged and still at the key they were read from count; elements that
1703
+ * array methods drafted when removing or moving them do not. An object or
1704
+ * array draft has a copy before it drafts a child.
1705
+ */
1706
+ function warnUnchangedDrafts(finalities) {
1707
+ let count = 0;
1708
+ for (const entry of finalities.draft) if (typeof entry !== "function" && !entry.operated) {
1709
+ const parent = entry.parent;
1710
+ if (parent !== null && (parent.type === 1 || parent.type === 0) && parent.copy[entry.key] === entry.proxy) count += 1;
1711
+ }
1712
+ if (count >= UNCHANGED_DRAFTS_WARNING_THRESHOLD) {
1713
+ unchangedDraftsWarned = true;
1714
+ console.warn(`Strict mode: the recipe left ${count} drafts unchanged; every object read through a draft becomes one. For read-only scans of large arrays, search 'current(draft.list)' instead; see https://mutative.js.org/docs/api-reference/current`);
1715
+ }
1716
+ }
1717
+ function finalizeDraft(result, returnedValue, patches, inversePatches, enableAutoFreeze) {
1718
+ var _a;
1719
+ const proxyDraft = getProxyDraft(result);
1720
+ if ((proxyDraft === null || proxyDraft === void 0 ? void 0 : proxyDraft.options.strict) && !unchangedDraftsWarned) warnUnchangedDrafts(proxyDraft.finalities);
1721
+ const original = (_a = proxyDraft === null || proxyDraft === void 0 ? void 0 : proxyDraft.original) !== null && _a !== void 0 ? _a : result;
1722
+ const hasReturnedValue = !!returnedValue.length;
1723
+ if (proxyDraft === null || proxyDraft === void 0 ? void 0 : proxyDraft.operated) {
1724
+ const list = proxyDraft.finalities.draft;
1725
+ while (list.length > 0) {
1726
+ const entry = list.pop();
1727
+ if (typeof entry === "function") entry(patches, inversePatches);
1728
+ else finalizeNode(entry, generatePatches, patches, inversePatches);
1729
+ }
1730
+ }
1731
+ const state = hasReturnedValue ? returnedValue[0] : proxyDraft ? proxyDraft.operated ? proxyDraft.copy : proxyDraft.original : result;
1732
+ if (proxyDraft) revokeProxy(proxyDraft.finalities);
1733
+ if (enableAutoFreeze) deepFreeze(state);
1734
+ return [
1735
+ state,
1736
+ patches && hasReturnedValue ? [{
1737
+ op: Operation.Replace,
1738
+ path: [],
1739
+ value: returnedValue[0]
1740
+ }] : patches,
1741
+ inversePatches && hasReturnedValue ? [{
1742
+ op: Operation.Replace,
1743
+ path: [],
1744
+ value: original
1745
+ }] : inversePatches
1746
+ ];
1747
+ }
1748
+
1749
+ //#endregion
1750
+ //#region src/draftify.ts
1751
+ function draftify(baseState, options) {
1752
+ var _a;
1753
+ const finalities = {
1754
+ draft: [],
1755
+ revoke: [],
1756
+ handledSet: /* @__PURE__ */ new WeakSet(),
1757
+ scoped: true
1758
+ };
1759
+ let patches;
1760
+ let inversePatches;
1761
+ if (options.enablePatches) {
1762
+ patches = [];
1763
+ inversePatches = [];
1764
+ }
1765
+ const draft = ((_a = options.mark) === null || _a === void 0 ? void 0 : _a.call(options, baseState, dataTypes)) === dataTypes.mutable || !isDraftable(baseState, options) ? baseState : createDraft(baseState, null, void 0, finalities, options);
1766
+ return [
1767
+ draft,
1768
+ (returnedValue = []) => {
1769
+ const [finalizedState, finalizedPatches, finalizedInversePatches] = finalizeDraft(draft, returnedValue, patches, inversePatches, options.enableAutoFreeze);
1770
+ return options.enablePatches ? [
1771
+ finalizedState,
1772
+ finalizedPatches,
1773
+ finalizedInversePatches
1774
+ ] : finalizedState;
1775
+ },
1776
+ finalities
1777
+ ];
1778
+ }
1779
+
1780
+ //#endregion
1781
+ //#region src/makeCreator.ts
1782
+ let draftBaseWarned = false;
1783
+ /**
1784
+ * `makeCreator(options)` to make a creator function.
1785
+ *
1786
+ * ## Example
1787
+ *
1788
+ * ```ts
1789
+ * import { makeCreator } from 'mutative';
1790
+ *
1791
+ * const baseState = { foo: { bar: 'str' }, arr: [] };
1792
+ * const create = makeCreator({ enableAutoFreeze: true });
1793
+ * const state = create(
1794
+ * baseState,
1795
+ * (draft) => {
1796
+ * draft.foo.bar = 'str2';
1797
+ * },
1798
+ * );
1799
+ *
1800
+ * expect(state).toEqual({ foo: { bar: 'str2' }, arr: [] });
1801
+ * expect(state).not.toBe(baseState);
1802
+ * expect(state.foo).not.toBe(baseState.foo);
1803
+ * expect(state.arr).toBe(baseState.arr);
1804
+ * expect(Object.isFrozen(state)).toBeTruthy();
1805
+ * ```
1806
+ */
1807
+ const makeCreator = (arg) => {
1808
+ if (arg !== void 0 && Object.prototype.toString.call(arg) !== "[object Object]") throw new Error(`Invalid options: ${String(arg)}, 'options' should be an object.`);
1809
+ return function create(arg0, arg1, arg2) {
1810
+ var _a, _b, _c;
1811
+ if (typeof arg0 === "function" && typeof arg1 !== "function") return function(base, ...args) {
1812
+ return create(base, (draft) => arg0.call(this, draft, ...args), arg1);
1813
+ };
1814
+ const base = arg0;
1815
+ const mutate = arg1;
1816
+ let options = arg2;
1817
+ if (typeof arg1 !== "function") options = arg1;
1818
+ if (options !== void 0 && Object.prototype.toString.call(options) !== "[object Object]") throw new Error(`Invalid options: ${options}, 'options' should be an object.`);
1819
+ options = {
1820
+ ...arg,
1821
+ ...options
1822
+ };
1823
+ const draftBase = isDraft(base);
1824
+ if (draftBase && typeof arg1 === "function" && !draftBaseWarned) {
1825
+ draftBaseWarned = true;
1826
+ console.warn(`create() received a draft as its base and drafts current(draft), unlike Immer's produce: the values that its recipe leaves unchanged are objects of the base state, so changing them after the result is assigned back changes the base state. Make such changes in the recipe or before calling create(); see https://mutative.js.org/docs/api-reference/create#create-on-a-draft`);
1827
+ }
1828
+ const state = draftBase ? current(base) : base;
1829
+ const mark = Array.isArray(options.mark) ? ((value, types) => {
1830
+ for (const mark of options.mark) {
1831
+ if (typeof mark !== "function") throw new Error(`Invalid mark: ${mark}, 'mark' should be a function.`);
1832
+ const result = mark(value, types);
1833
+ if (result) return result;
1834
+ }
1835
+ }) : options.mark;
1836
+ const enablePatches = (_a = options.enablePatches) !== null && _a !== void 0 ? _a : false;
1837
+ const strict = (_b = options.strict) !== null && _b !== void 0 ? _b : false;
1838
+ const _options = {
1839
+ enableAutoFreeze: (_c = options.enableAutoFreeze) !== null && _c !== void 0 ? _c : false,
1840
+ mark,
1841
+ strict,
1842
+ enablePatches
1843
+ };
1844
+ if (!isDraftable(state, _options) && typeof state === "object" && state !== null) die(0);
1845
+ const [draft, finalize, finalities] = draftify(state, _options);
1846
+ if (typeof arg1 !== "function") {
1847
+ if (!isDraftable(state, _options)) die(0);
1848
+ finalities.scoped = false;
1849
+ return [draft, finalize];
1850
+ }
1851
+ function fail(error) {
1852
+ revokeProxy(finalities);
1853
+ releaseArrayMethods(finalities.revoke);
1854
+ throw error;
1855
+ }
1856
+ let result;
1857
+ try {
1858
+ result = mutate(draft);
1859
+ } catch (error) {
1860
+ fail(error);
1861
+ }
1862
+ const returnValue = (value) => {
1863
+ const proxyDraft = getProxyDraft(draft);
1864
+ if (!isDraft(value)) {
1865
+ if (value !== void 0 && !isEqual(value, draft) && (proxyDraft === null || proxyDraft === void 0 ? void 0 : proxyDraft.operated)) die(5);
1866
+ const rawReturnValue = value === null || value === void 0 ? void 0 : value[RAW_RETURN_SYMBOL];
1867
+ if (rawReturnValue) {
1868
+ const _value = rawReturnValue[0];
1869
+ if (_options.strict && typeof value === "object" && value !== null) handleReturnValue(proxyDraft, value, true);
1870
+ return finalize([_value]);
1871
+ }
1872
+ if (value !== void 0) {
1873
+ if (typeof value === "object" && value !== null) handleReturnValue(proxyDraft, value);
1874
+ return finalize([value]);
1875
+ }
1876
+ }
1877
+ if (value === draft || value === void 0) return finalize([]);
1878
+ const returnedProxyDraft = getProxyDraft(value);
1879
+ if (_options === returnedProxyDraft.options) {
1880
+ if (returnedProxyDraft.operated) die(6);
1881
+ return finalize([current(value)]);
1882
+ }
1883
+ return finalize([value]);
1884
+ };
1885
+ const finish = (value) => {
1886
+ try {
1887
+ return returnValue(value);
1888
+ } catch (error) {
1889
+ return fail(error);
1890
+ } finally {
1891
+ releaseArrayMethods(finalities.revoke);
1892
+ }
1893
+ };
1894
+ try {
1895
+ if (result instanceof Promise) return result.then(finish, fail);
1896
+ } catch (error) {
1897
+ return fail(error);
1898
+ }
1899
+ return finish(result);
1900
+ };
1901
+ };
1902
+
1903
+ //#endregion
1904
+ //#region src/create.ts
1905
+ /**
1906
+ * `create(baseState, callback, options)` to create the next state
1907
+ *
1908
+ * ## Example
1909
+ *
1910
+ * ```ts
1911
+ * import { create } from 'mutative';
1912
+ *
1913
+ * const baseState = { foo: { bar: 'str' }, arr: [] };
1914
+ * const state = create(
1915
+ * baseState,
1916
+ * (draft) => {
1917
+ * draft.foo.bar = 'str2';
1918
+ * },
1919
+ * );
1920
+ *
1921
+ * expect(state).toEqual({ foo: { bar: 'str2' }, arr: [] });
1922
+ * expect(state).not.toBe(baseState);
1923
+ * expect(state.foo).not.toBe(baseState.foo);
1924
+ * expect(state.arr).toBe(baseState.arr);
1925
+ * ```
1926
+ */
1927
+ const create = makeCreator();
1928
+
1929
+ //#endregion
1930
+ //#region src/apply.ts
1931
+ function getPatchTargetType(value) {
1932
+ try {
1933
+ const proxyDraft = getProxyDraft(value);
1934
+ if (proxyDraft) return proxyDraft.type;
1935
+ } catch (_a) {}
1936
+ return getType(value);
1937
+ }
1938
+ function normalizePatchKey(key) {
1939
+ return typeof key === "object" || typeof key === "function" ? Reflect.ownKeys({ [key]: 0 })[0] : key;
1940
+ }
1941
+ function apply(state, patches, applyOptions) {
1942
+ const mutable = applyOptions === null || applyOptions === void 0 ? void 0 : applyOptions.mutable;
1943
+ let i;
1944
+ for (i = patches.length - 1; i >= 0; i -= 1) {
1945
+ const { value, op, path } = patches[i];
1946
+ if (!path.length && op === Operation.Replace || path === "" && op === Operation.Add) {
1947
+ if (mutable && value !== state) die(21);
1948
+ state = value;
1949
+ break;
1950
+ }
1951
+ }
1952
+ if (i > -1) patches = patches.slice(i + 1);
1953
+ const getTargetType = mutable ? getType : getPatchTargetType;
1954
+ const mutate = (draft) => {
1955
+ patches.forEach((patch) => {
1956
+ const { path: _path, op } = patch;
1957
+ const path = unescapePath(_path);
1958
+ let base = draft;
1959
+ for (let index = 0; index < path.length - 1; index += 1) {
1960
+ const parentType = getTargetType(base);
1961
+ let key = path[index];
1962
+ if (parentType <= 1) {
1963
+ key = normalizePatchKey(key);
1964
+ if (key === "__proto__" || key === "constructor" || typeof base === "function" && key === "prototype") die(13);
1965
+ }
1966
+ if (parentType === 3) {
1967
+ key = normalizePatchKey(key);
1968
+ base = Array.from(base);
1969
+ if (typeof key !== "number" && !has(base, key)) die(14, path);
1970
+ }
1971
+ base = parentType === 2 ? base.get(key) : base[key];
1972
+ if (typeof base !== "object") die(14, path);
1973
+ }
1974
+ const type = getTargetType(base);
1975
+ const value = deepClone(patch.value);
1976
+ let key = path[path.length - 1];
1977
+ if (type <= 1) {
1978
+ if (type === 0 || op === Operation.Replace) key = normalizePatchKey(key);
1979
+ if (key === "__proto__") die(13);
1980
+ }
1981
+ switch (op) {
1982
+ case Operation.Replace: switch (type) {
1983
+ case 2: return base.set(key, value);
1984
+ case 3: return die(15);
1985
+ default: return base[key] = value;
1986
+ }
1987
+ case Operation.Add: switch (type) {
1988
+ case 1: return key === "-" ? base.push(value) : base.splice(key, 0, value);
1989
+ case 2: return base.set(key, value);
1990
+ case 3: return base.add(value);
1991
+ default: return base[key] = value;
1992
+ }
1993
+ case Operation.Remove: switch (type) {
1994
+ case 1: return base.splice(key, 1);
1995
+ case 2: return base.delete(key);
1996
+ case 3: return base.delete(patch.value);
1997
+ default: return delete base[key];
1998
+ }
1999
+ default: die(16, op);
2000
+ }
2001
+ });
2002
+ };
2003
+ if (mutable) {
2004
+ if (Object.keys(applyOptions).filter((key) => key !== "mutable").length) console.warn("The \"mutable\" option is not allowed to be used with other options.");
2005
+ mutate(state);
2006
+ return;
2007
+ }
2008
+ if (isDraft(state)) {
2009
+ if (applyOptions !== void 0) die(17);
2010
+ mutate(state);
2011
+ return state;
2012
+ }
2013
+ return create(state, mutate, {
2014
+ ...applyOptions,
2015
+ enablePatches: false
2016
+ });
2017
+ }
2018
+
2019
+ //#endregion
2020
+ //#region src/original.ts
2021
+ /**
2022
+ * `original(draft)` to get original state in the draft mutation function.
2023
+ *
2024
+ * ## Example
2025
+ *
2026
+ * ```ts
2027
+ * import { create, original } from 'mutative';
2028
+ *
2029
+ * const baseState = { foo: { bar: 'str' }, arr: [] };
2030
+ * const state = create(
2031
+ * baseState,
2032
+ * (draft) => {
2033
+ * draft.foo.bar = 'str2';
2034
+ * expect(original(draft.foo)).toEqual({ bar: 'str' });
2035
+ * }
2036
+ * );
2037
+ * ```
2038
+ */
2039
+ function original(target) {
2040
+ const proxyDraft = getProxyDraft(target);
2041
+ if (!proxyDraft) die(18, target);
2042
+ return proxyDraft.original;
2043
+ }
2044
+
2045
+ //#endregion
2046
+ //#region src/rawReturn.ts
2047
+ /**
2048
+ * Use rawReturn() to wrap the return value to skip the draft check and thus improve performance.
2049
+ *
2050
+ * ## Example
2051
+ *
2052
+ * ```ts
2053
+ * import { create, rawReturn } from 'mutative';
2054
+ *
2055
+ * const baseState = { foo: { bar: 'str' }, arr: [] };
2056
+ * const state = create(
2057
+ * baseState,
2058
+ * (draft) => {
2059
+ * return rawReturn(baseState);
2060
+ * },
2061
+ * );
2062
+ * expect(state).toBe(baseState);
2063
+ * ```
2064
+ */
2065
+ function rawReturn(value) {
2066
+ if (arguments.length === 0) die(19);
2067
+ if (arguments.length > 1) die(20);
2068
+ if (value !== void 0 && (typeof value !== "object" || value === null)) console.warn("rawReturn() must be called with an object(including plain object, arrays, Set, Map, etc.) or `undefined`, other types do not need to be returned via rawReturn().");
2069
+ return { [RAW_RETURN_SYMBOL]: [value] };
2070
+ }
2071
+
2072
+ //#endregion
2073
+ //#region src/utils/marker.ts
2074
+ const constructorString = Object.prototype.constructor.toString();
2075
+ const isSimpleObject = (value) => {
2076
+ if (!value || typeof value !== "object") return false;
2077
+ const prototype = Object.getPrototypeOf(value);
2078
+ if (prototype === null) return true;
2079
+ const constructor = Object.hasOwnProperty.call(prototype, "constructor") && prototype.constructor;
2080
+ if (constructor === Object) return true;
2081
+ return typeof constructor === "function" && Function.toString.call(constructor) === constructorString;
2082
+ };
2083
+ /**
2084
+ * A `mark` that drafts plain objects, objects without a prototype and plain
2085
+ * objects of other realms, such as same-origin iframes.
2086
+ */
2087
+ const markSimpleObject = (value) => {
2088
+ if (isSimpleObject(value)) return dataTypes.immutable;
2089
+ };
2090
+
2091
+ //#endregion
2092
+ //#region src/utils/cast.ts
2093
+ /**
2094
+ * Cast a value to its draft type; it returns the value as it is.
2095
+ */
2096
+ function castDraft(value) {
2097
+ return value;
2098
+ }
2099
+ /**
2100
+ * Cast a value to its immutable type, without freezing it.
2101
+ */
2102
+ function castImmutable(value) {
2103
+ return value;
2104
+ }
2105
+ /**
2106
+ * Cast a draft to the type of its base state.
2107
+ */
2108
+ function castMutable(draft) {
2109
+ return draft;
2110
+ }
2111
+
2112
+ //#endregion
2113
+ exports.apply = apply;
2114
+ exports.castDraft = castDraft;
2115
+ exports.castImmutable = castImmutable;
2116
+ exports.castMutable = castMutable;
2117
+ exports.create = create;
2118
+ exports.current = current;
2119
+ exports.isDraft = isDraft;
2120
+ exports.isDraftable = isDraftable;
2121
+ exports.makeCreator = makeCreator;
2122
+ exports.markSimpleObject = markSimpleObject;
2123
+ exports.original = original;
2124
+ exports.rawReturn = rawReturn;
2125
+ exports.unsafe = unsafe;
2126
+ //# sourceMappingURL=mutative.cjs.development.js.map