@voidhash/mimic 0.0.1 → 0.0.3

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 (278) hide show
  1. package/.turbo/turbo-build.log +257 -42
  2. package/dist/Document.cjs +152 -0
  3. package/dist/Document.d.cts +67 -0
  4. package/dist/Document.d.cts.map +1 -0
  5. package/dist/Document.d.mts +68 -0
  6. package/dist/Document.d.mts.map +1 -0
  7. package/dist/Document.mjs +147 -0
  8. package/dist/Document.mjs.map +1 -0
  9. package/dist/EffectSchema.cjs +180 -0
  10. package/dist/EffectSchema.d.cts +84 -0
  11. package/dist/EffectSchema.d.cts.map +1 -0
  12. package/dist/EffectSchema.d.mts +84 -0
  13. package/dist/EffectSchema.d.mts.map +1 -0
  14. package/dist/EffectSchema.mjs +176 -0
  15. package/dist/EffectSchema.mjs.map +1 -0
  16. package/dist/FractionalIndex.cjs +365 -0
  17. package/dist/FractionalIndex.mjs +364 -0
  18. package/dist/FractionalIndex.mjs.map +1 -0
  19. package/dist/Operation.cjs +53 -0
  20. package/dist/Operation.d.cts +39 -0
  21. package/dist/Operation.d.cts.map +1 -0
  22. package/dist/Operation.d.mts +39 -0
  23. package/dist/Operation.d.mts.map +1 -0
  24. package/dist/Operation.mjs +46 -0
  25. package/dist/Operation.mjs.map +1 -0
  26. package/dist/OperationDefinition.cjs +13 -0
  27. package/dist/OperationDefinition.d.cts +12 -0
  28. package/dist/OperationDefinition.d.cts.map +1 -0
  29. package/dist/OperationDefinition.d.mts +12 -0
  30. package/dist/OperationDefinition.d.mts.map +1 -0
  31. package/dist/OperationDefinition.mjs +13 -0
  32. package/dist/OperationDefinition.mjs.map +1 -0
  33. package/dist/OperationPath.cjs +148 -0
  34. package/dist/OperationPath.d.cts +60 -0
  35. package/dist/OperationPath.d.cts.map +1 -0
  36. package/dist/OperationPath.d.mts +60 -0
  37. package/dist/OperationPath.d.mts.map +1 -0
  38. package/dist/OperationPath.mjs +138 -0
  39. package/dist/OperationPath.mjs.map +1 -0
  40. package/dist/{Presence-gWrmGBeu.cjs → Presence.cjs} +4 -39
  41. package/dist/{Presence-N8u7Eppr.d.mts → Presence.d.cts} +2 -2
  42. package/dist/Presence.d.cts.map +1 -0
  43. package/dist/{Presence-DKKP4v5X.d.cts → Presence.d.mts} +2 -2
  44. package/dist/Presence.d.mts.map +1 -0
  45. package/dist/{Presence-DdMVKcOv.mjs → Presence.mjs} +3 -28
  46. package/dist/Presence.mjs.map +1 -0
  47. package/dist/Primitive.cjs +52 -0
  48. package/dist/Primitive.d.cts +20 -0
  49. package/dist/Primitive.d.cts.map +1 -0
  50. package/dist/Primitive.d.mts +20 -0
  51. package/dist/Primitive.d.mts.map +1 -0
  52. package/dist/Primitive.mjs +48 -0
  53. package/dist/Primitive.mjs.map +1 -0
  54. package/dist/ProxyEnvironment.cjs +34 -0
  55. package/dist/ProxyEnvironment.d.cts +31 -0
  56. package/dist/ProxyEnvironment.d.cts.map +1 -0
  57. package/dist/ProxyEnvironment.d.mts +31 -0
  58. package/dist/ProxyEnvironment.d.mts.map +1 -0
  59. package/dist/ProxyEnvironment.mjs +29 -0
  60. package/dist/ProxyEnvironment.mjs.map +1 -0
  61. package/dist/Transaction.cjs +66 -0
  62. package/dist/Transaction.d.cts +56 -0
  63. package/dist/Transaction.d.cts.map +1 -0
  64. package/dist/Transaction.d.mts +56 -0
  65. package/dist/Transaction.d.mts.map +1 -0
  66. package/dist/Transaction.mjs +58 -0
  67. package/dist/Transaction.mjs.map +1 -0
  68. package/dist/Transform.cjs +11 -0
  69. package/dist/Transform.d.cts +21 -0
  70. package/dist/Transform.d.cts.map +1 -0
  71. package/dist/Transform.d.mts +21 -0
  72. package/dist/Transform.d.mts.map +1 -0
  73. package/dist/Transform.mjs +6 -0
  74. package/dist/Transform.mjs.map +1 -0
  75. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
  76. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
  77. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
  78. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
  79. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
  80. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
  81. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
  82. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
  83. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
  84. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
  85. package/dist/_virtual/rolldown_runtime.cjs +43 -0
  86. package/dist/{chunk-CLMFDpHK.mjs → _virtual/rolldown_runtime.mjs} +1 -1
  87. package/dist/client/ClientDocument.cjs +590 -0
  88. package/dist/client/ClientDocument.d.cts +161 -0
  89. package/dist/client/ClientDocument.d.cts.map +1 -0
  90. package/dist/client/ClientDocument.d.mts +162 -0
  91. package/dist/client/ClientDocument.d.mts.map +1 -0
  92. package/dist/client/ClientDocument.mjs +586 -0
  93. package/dist/client/ClientDocument.mjs.map +1 -0
  94. package/dist/client/Rebase.cjs +204 -0
  95. package/dist/client/Rebase.d.cts +87 -0
  96. package/dist/client/Rebase.d.cts.map +1 -0
  97. package/dist/client/Rebase.d.mts +88 -0
  98. package/dist/client/Rebase.d.mts.map +1 -0
  99. package/dist/client/Rebase.mjs +198 -0
  100. package/dist/client/Rebase.mjs.map +1 -0
  101. package/dist/client/StateMonitor.cjs +133 -0
  102. package/dist/client/StateMonitor.d.cts +122 -0
  103. package/dist/client/StateMonitor.d.cts.map +1 -0
  104. package/dist/client/StateMonitor.d.mts +122 -0
  105. package/dist/client/StateMonitor.d.mts.map +1 -0
  106. package/dist/client/StateMonitor.mjs +129 -0
  107. package/dist/client/StateMonitor.mjs.map +1 -0
  108. package/dist/client/Transport.cjs +11 -0
  109. package/dist/client/Transport.d.cts +237 -0
  110. package/dist/client/Transport.d.cts.map +1 -0
  111. package/dist/client/Transport.d.mts +237 -0
  112. package/dist/client/Transport.d.mts.map +1 -0
  113. package/dist/client/Transport.mjs +6 -0
  114. package/dist/client/Transport.mjs.map +1 -0
  115. package/dist/client/WebSocketTransport.cjs +396 -0
  116. package/dist/client/WebSocketTransport.d.cts +29 -0
  117. package/dist/client/WebSocketTransport.d.cts.map +1 -0
  118. package/dist/client/WebSocketTransport.d.mts +29 -0
  119. package/dist/client/WebSocketTransport.d.mts.map +1 -0
  120. package/dist/client/WebSocketTransport.mjs +392 -0
  121. package/dist/client/WebSocketTransport.mjs.map +1 -0
  122. package/dist/client/errors.cjs +135 -0
  123. package/dist/client/errors.d.cts +87 -0
  124. package/dist/client/errors.d.cts.map +1 -0
  125. package/dist/client/errors.d.mts +87 -0
  126. package/dist/client/errors.d.mts.map +1 -0
  127. package/dist/client/errors.mjs +127 -0
  128. package/dist/client/errors.mjs.map +1 -0
  129. package/dist/client/index.cjs +22 -1424
  130. package/dist/client/index.d.cts +8 -692
  131. package/dist/client/index.d.mts +8 -692
  132. package/dist/client/index.mjs +9 -1413
  133. package/dist/index.cjs +30 -2529
  134. package/dist/index.d.cts +12 -143
  135. package/dist/index.d.mts +12 -143
  136. package/dist/index.mjs +13 -2526
  137. package/dist/primitives/Array.cjs +302 -0
  138. package/dist/primitives/Array.d.cts +95 -0
  139. package/dist/primitives/Array.d.cts.map +1 -0
  140. package/dist/primitives/Array.d.mts +95 -0
  141. package/dist/primitives/Array.d.mts.map +1 -0
  142. package/dist/primitives/Array.mjs +301 -0
  143. package/dist/primitives/Array.mjs.map +1 -0
  144. package/dist/primitives/Boolean.cjs +95 -0
  145. package/dist/primitives/Boolean.d.cts +44 -0
  146. package/dist/primitives/Boolean.d.cts.map +1 -0
  147. package/dist/primitives/Boolean.d.mts +44 -0
  148. package/dist/primitives/Boolean.d.mts.map +1 -0
  149. package/dist/primitives/Boolean.mjs +94 -0
  150. package/dist/primitives/Boolean.mjs.map +1 -0
  151. package/dist/primitives/Either.cjs +200 -0
  152. package/dist/primitives/Either.d.cts +113 -0
  153. package/dist/primitives/Either.d.cts.map +1 -0
  154. package/dist/primitives/Either.d.mts +113 -0
  155. package/dist/primitives/Either.d.mts.map +1 -0
  156. package/dist/primitives/Either.mjs +199 -0
  157. package/dist/primitives/Either.mjs.map +1 -0
  158. package/dist/primitives/Lazy.cjs +46 -0
  159. package/dist/primitives/Lazy.d.cts +46 -0
  160. package/dist/primitives/Lazy.d.cts.map +1 -0
  161. package/dist/primitives/Lazy.d.mts +46 -0
  162. package/dist/primitives/Lazy.d.mts.map +1 -0
  163. package/dist/primitives/Lazy.mjs +46 -0
  164. package/dist/primitives/Lazy.mjs.map +1 -0
  165. package/dist/primitives/Literal.cjs +91 -0
  166. package/dist/primitives/Literal.d.cts +46 -0
  167. package/dist/primitives/Literal.d.cts.map +1 -0
  168. package/dist/primitives/Literal.d.mts +46 -0
  169. package/dist/primitives/Literal.d.mts.map +1 -0
  170. package/dist/primitives/Literal.mjs +90 -0
  171. package/dist/primitives/Literal.mjs.map +1 -0
  172. package/dist/primitives/Number.cjs +115 -0
  173. package/dist/primitives/Number.d.cts +54 -0
  174. package/dist/primitives/Number.d.cts.map +1 -0
  175. package/dist/primitives/Number.d.mts +54 -0
  176. package/dist/primitives/Number.d.mts.map +1 -0
  177. package/dist/primitives/Number.mjs +114 -0
  178. package/dist/primitives/Number.mjs.map +1 -0
  179. package/dist/primitives/String.cjs +127 -0
  180. package/dist/primitives/String.d.cts +56 -0
  181. package/dist/primitives/String.d.cts.map +1 -0
  182. package/dist/primitives/String.d.mts +56 -0
  183. package/dist/primitives/String.d.mts.map +1 -0
  184. package/dist/primitives/String.mjs +126 -0
  185. package/dist/primitives/String.mjs.map +1 -0
  186. package/dist/primitives/Struct.cjs +207 -0
  187. package/dist/primitives/Struct.d.cts +96 -0
  188. package/dist/primitives/Struct.d.cts.map +1 -0
  189. package/dist/primitives/Struct.d.mts +97 -0
  190. package/dist/primitives/Struct.d.mts.map +1 -0
  191. package/dist/primitives/Struct.mjs +206 -0
  192. package/dist/primitives/Struct.mjs.map +1 -0
  193. package/dist/primitives/Tree.cjs +575 -0
  194. package/dist/primitives/Tree.d.cts +185 -0
  195. package/dist/primitives/Tree.d.cts.map +1 -0
  196. package/dist/primitives/Tree.d.mts +185 -0
  197. package/dist/primitives/Tree.d.mts.map +1 -0
  198. package/dist/primitives/Tree.mjs +574 -0
  199. package/dist/primitives/Tree.mjs.map +1 -0
  200. package/dist/primitives/TreeNode.cjs +73 -0
  201. package/dist/primitives/TreeNode.d.cts +92 -0
  202. package/dist/primitives/TreeNode.d.cts.map +1 -0
  203. package/dist/primitives/TreeNode.d.mts +93 -0
  204. package/dist/primitives/TreeNode.d.mts.map +1 -0
  205. package/dist/primitives/TreeNode.mjs +72 -0
  206. package/dist/primitives/TreeNode.mjs.map +1 -0
  207. package/dist/primitives/Union.cjs +170 -0
  208. package/dist/primitives/Union.d.cts +81 -0
  209. package/dist/primitives/Union.d.cts.map +1 -0
  210. package/dist/primitives/Union.d.mts +81 -0
  211. package/dist/primitives/Union.d.mts.map +1 -0
  212. package/dist/primitives/Union.mjs +169 -0
  213. package/dist/primitives/Union.mjs.map +1 -0
  214. package/dist/primitives/shared.cjs +60 -0
  215. package/dist/primitives/shared.d.cts +147 -0
  216. package/dist/primitives/shared.d.cts.map +1 -0
  217. package/dist/primitives/shared.d.mts +147 -0
  218. package/dist/primitives/shared.d.mts.map +1 -0
  219. package/dist/primitives/shared.mjs +58 -0
  220. package/dist/primitives/shared.mjs.map +1 -0
  221. package/dist/server/ServerDocument.cjs +110 -0
  222. package/dist/server/ServerDocument.d.cts +98 -0
  223. package/dist/server/ServerDocument.d.cts.map +1 -0
  224. package/dist/server/ServerDocument.d.mts +99 -0
  225. package/dist/server/ServerDocument.d.mts.map +1 -0
  226. package/dist/server/ServerDocument.mjs +106 -0
  227. package/dist/server/ServerDocument.mjs.map +1 -0
  228. package/dist/server/errors.cjs +85 -0
  229. package/dist/server/errors.d.cts +53 -0
  230. package/dist/server/errors.d.cts.map +1 -0
  231. package/dist/server/errors.d.mts +53 -0
  232. package/dist/server/errors.d.mts.map +1 -0
  233. package/dist/server/errors.mjs +81 -0
  234. package/dist/server/errors.mjs.map +1 -0
  235. package/dist/server/index.cjs +9 -185
  236. package/dist/server/index.d.cts +3 -148
  237. package/dist/server/index.d.mts +3 -148
  238. package/dist/server/index.mjs +3 -181
  239. package/dist/types/index.cjs +16 -0
  240. package/dist/types/index.d.cts +16 -0
  241. package/dist/types/index.d.cts.map +1 -0
  242. package/dist/types/index.d.mts +16 -0
  243. package/dist/types/index.d.mts.map +1 -0
  244. package/dist/types/index.mjs +12 -0
  245. package/dist/types/index.mjs.map +1 -0
  246. package/dist/utils/tree-helpers.cjs +443 -0
  247. package/dist/utils/tree-helpers.d.cts +280 -0
  248. package/dist/utils/tree-helpers.d.cts.map +1 -0
  249. package/dist/utils/tree-helpers.d.mts +280 -0
  250. package/dist/utils/tree-helpers.d.mts.map +1 -0
  251. package/dist/utils/tree-helpers.mjs +439 -0
  252. package/dist/utils/tree-helpers.mjs.map +1 -0
  253. package/package.json +2 -2
  254. package/src/index.ts +2 -0
  255. package/src/primitives/shared.ts +7 -1
  256. package/src/types/index.ts +137 -0
  257. package/src/utils/index.ts +7 -0
  258. package/src/utils/tree-helpers.ts +648 -0
  259. package/tsdown.config.ts +1 -1
  260. package/dist/Document-ChuFrTk1.cjs +0 -571
  261. package/dist/Document-CwiAFTIq.mjs +0 -438
  262. package/dist/Document-CwiAFTIq.mjs.map +0 -1
  263. package/dist/Presence-DKKP4v5X.d.cts.map +0 -1
  264. package/dist/Presence-DdMVKcOv.mjs.map +0 -1
  265. package/dist/Presence-N8u7Eppr.d.mts.map +0 -1
  266. package/dist/Primitive-CvFVxR8_.d.cts +0 -1175
  267. package/dist/Primitive-CvFVxR8_.d.cts.map +0 -1
  268. package/dist/Primitive-lEhQyGVL.d.mts +0 -1175
  269. package/dist/Primitive-lEhQyGVL.d.mts.map +0 -1
  270. package/dist/client/index.d.cts.map +0 -1
  271. package/dist/client/index.d.mts.map +0 -1
  272. package/dist/client/index.mjs.map +0 -1
  273. package/dist/index.d.cts.map +0 -1
  274. package/dist/index.d.mts.map +0 -1
  275. package/dist/index.mjs.map +0 -1
  276. package/dist/server/index.d.cts.map +0 -1
  277. package/dist/server/index.d.mts.map +0 -1
  278. package/dist/server/index.mjs.map +0 -1
@@ -1,191 +1,15 @@
1
- const require_Document = require('../Document-ChuFrTk1.cjs');
1
+ const require_ServerDocument = require('./ServerDocument.cjs');
2
+ const require_errors = require('./errors.cjs');
2
3
 
3
- //#region src/server/ServerDocument.ts
4
- var ServerDocument_exports = /* @__PURE__ */ require_Document.__export({ make: () => make });
5
- /**
6
- * Creates a new ServerDocument for the given schema.
7
- */
8
- const make = (options) => {
9
- const { schema, initialState, initialVersion = 0, onBroadcast, onRejection, maxTransactionHistory = 1e3 } = options;
10
- let _document = require_Document.make(schema, { initial: initialState });
11
- let _version = initialVersion;
12
- const _processedTransactions = /* @__PURE__ */ new Set();
13
- const _transactionOrder = [];
14
- /**
15
- * Records a transaction as processed, maintaining the history limit.
16
- */
17
- const recordTransaction = (transactionId) => {
18
- _processedTransactions.add(transactionId);
19
- _transactionOrder.push(transactionId);
20
- while (_transactionOrder.length > maxTransactionHistory) {
21
- const oldest = _transactionOrder.shift();
22
- if (oldest) _processedTransactions.delete(oldest);
23
- }
24
- };
25
- /**
26
- * Validates that the transaction can be applied to the current state.
27
- * Creates a temporary document and attempts to apply the operations.
28
- */
29
- const validateTransaction = (transaction) => {
30
- if (require_Document.isEmpty(transaction)) return {
31
- valid: false,
32
- reason: "Transaction is empty"
33
- };
34
- if (_processedTransactions.has(transaction.id)) return {
35
- valid: false,
36
- reason: "Transaction has already been processed"
37
- };
38
- const currentState = _document.get();
39
- const tempDoc = require_Document.make(schema, { initial: currentState });
40
- try {
41
- tempDoc.apply(transaction.ops);
42
- return { valid: true };
43
- } catch (error) {
44
- return {
45
- valid: false,
46
- reason: error instanceof Error ? error.message : String(error)
47
- };
48
- }
49
- };
50
- return {
51
- schema,
52
- get: () => {
53
- return _document.get();
54
- },
55
- getVersion: () => {
56
- return _version;
57
- },
58
- submit: (transaction) => {
59
- const validation = validateTransaction(transaction);
60
- if (!validation.valid) {
61
- onRejection === null || onRejection === void 0 || onRejection(transaction.id, validation.reason);
62
- return {
63
- success: false,
64
- reason: validation.reason
65
- };
66
- }
67
- try {
68
- _document.apply(transaction.ops);
69
- } catch (error) {
70
- const reason = error instanceof Error ? error.message : String(error);
71
- onRejection === null || onRejection === void 0 || onRejection(transaction.id, reason);
72
- return {
73
- success: false,
74
- reason
75
- };
76
- }
77
- _version += 1;
78
- recordTransaction(transaction.id);
79
- onBroadcast({
80
- type: "transaction",
81
- transaction,
82
- version: _version
83
- });
84
- return {
85
- success: true,
86
- version: _version
87
- };
88
- },
89
- getSnapshot: () => {
90
- return {
91
- type: "snapshot",
92
- state: _document.get(),
93
- version: _version
94
- };
95
- },
96
- hasProcessed: (transactionId) => {
97
- return _processedTransactions.has(transactionId);
98
- }
99
- };
100
- };
101
-
102
- //#endregion
103
- //#region src/server/errors.ts
104
- /**
105
- * Base error class for all mimic-server errors.
106
- */
107
- var MimicServerError = class extends Error {
108
- constructor(message) {
109
- super(message);
110
- require_Document._defineProperty(this, "_tag", "MimicServerError");
111
- this.name = "MimicServerError";
112
- }
113
- };
114
- /**
115
- * Error thrown when a transaction fails validation.
116
- */
117
- var ValidationError = class extends MimicServerError {
118
- constructor(transactionId, message) {
119
- super(`Transaction ${transactionId} validation failed: ${message}`);
120
- require_Document._defineProperty(this, "_tag", "ValidationError");
121
- require_Document._defineProperty(this, "transactionId", void 0);
122
- this.name = "ValidationError";
123
- this.transactionId = transactionId;
124
- }
125
- };
126
- /**
127
- * Error thrown when an operation is invalid for the current schema.
128
- */
129
- var InvalidOperationError = class extends MimicServerError {
130
- constructor(operationKind, path, message) {
131
- super(`Invalid operation ${operationKind} at path "${path}": ${message}`);
132
- require_Document._defineProperty(this, "_tag", "InvalidOperationError");
133
- require_Document._defineProperty(this, "operationKind", void 0);
134
- require_Document._defineProperty(this, "path", void 0);
135
- this.name = "InvalidOperationError";
136
- this.operationKind = operationKind;
137
- this.path = path;
138
- }
139
- };
140
- /**
141
- * Error thrown when an operation cannot be applied to the current state.
142
- */
143
- var StateValidationError = class extends MimicServerError {
144
- constructor(transactionId, message, cause) {
145
- super(`Transaction ${transactionId} cannot be applied to current state: ${message}`);
146
- require_Document._defineProperty(this, "_tag", "StateValidationError");
147
- require_Document._defineProperty(this, "transactionId", void 0);
148
- require_Document._defineProperty(this, "cause", void 0);
149
- this.name = "StateValidationError";
150
- this.transactionId = transactionId;
151
- this.cause = cause;
152
- }
153
- };
154
- /**
155
- * Error thrown when attempting to apply an empty transaction.
156
- */
157
- var EmptyTransactionError = class extends MimicServerError {
158
- constructor(transactionId) {
159
- super(`Transaction ${transactionId} is empty and cannot be applied`);
160
- require_Document._defineProperty(this, "_tag", "EmptyTransactionError");
161
- require_Document._defineProperty(this, "transactionId", void 0);
162
- this.name = "EmptyTransactionError";
163
- this.transactionId = transactionId;
164
- }
165
- };
166
- /**
167
- * Error thrown when a duplicate transaction is submitted.
168
- */
169
- var DuplicateTransactionError = class extends MimicServerError {
170
- constructor(transactionId) {
171
- super(`Transaction ${transactionId} has already been processed`);
172
- require_Document._defineProperty(this, "_tag", "DuplicateTransactionError");
173
- require_Document._defineProperty(this, "transactionId", void 0);
174
- this.name = "DuplicateTransactionError";
175
- this.transactionId = transactionId;
176
- }
177
- };
178
-
179
- //#endregion
180
- exports.DuplicateTransactionError = DuplicateTransactionError;
181
- exports.EmptyTransactionError = EmptyTransactionError;
182
- exports.InvalidOperationError = InvalidOperationError;
183
- exports.MimicServerError = MimicServerError;
4
+ exports.DuplicateTransactionError = require_errors.DuplicateTransactionError;
5
+ exports.EmptyTransactionError = require_errors.EmptyTransactionError;
6
+ exports.InvalidOperationError = require_errors.InvalidOperationError;
7
+ exports.MimicServerError = require_errors.MimicServerError;
184
8
  Object.defineProperty(exports, 'ServerDocument', {
185
9
  enumerable: true,
186
10
  get: function () {
187
- return ServerDocument_exports;
11
+ return require_ServerDocument.ServerDocument_exports;
188
12
  }
189
13
  });
190
- exports.StateValidationError = StateValidationError;
191
- exports.ValidationError = ValidationError;
14
+ exports.StateValidationError = require_errors.StateValidationError;
15
+ exports.ValidationError = require_errors.ValidationError;
@@ -1,148 +1,3 @@
1
- import { p as Transaction, r as AnyPrimitive, s as InferState, v as __export } from "../Primitive-CvFVxR8_.cjs";
2
-
3
- //#region src/server/ServerDocument.d.ts
4
- declare namespace ServerDocument_d_exports {
5
- export { ErrorMessage, ServerDocument, ServerDocumentOptions, ServerMessage, SnapshotMessage, SubmitResult, TransactionMessage, make };
6
- }
7
- /**
8
- * Message sent when broadcasting a committed transaction.
9
- */
10
- interface TransactionMessage {
11
- readonly type: "transaction";
12
- readonly transaction: Transaction;
13
- /** Server-assigned version number for ordering */
14
- readonly version: number;
15
- }
16
- /**
17
- * Message sent when a transaction is rejected.
18
- */
19
- interface ErrorMessage {
20
- readonly type: "error";
21
- readonly transactionId: string;
22
- readonly reason: string;
23
- }
24
- /**
25
- * Message sent as a full state snapshot.
26
- */
27
- interface SnapshotMessage {
28
- readonly type: "snapshot";
29
- readonly state: unknown;
30
- readonly version: number;
31
- }
32
- /**
33
- * Union of all server messages that can be broadcast.
34
- */
35
- type ServerMessage = TransactionMessage | ErrorMessage | SnapshotMessage;
36
- /**
37
- * Result of submitting a transaction to the server.
38
- */
39
- type SubmitResult = {
40
- readonly success: true;
41
- readonly version: number;
42
- } | {
43
- readonly success: false;
44
- readonly reason: string;
45
- };
46
- /**
47
- * Options for creating a ServerDocument.
48
- */
49
- interface ServerDocumentOptions<TSchema extends AnyPrimitive> {
50
- /** The schema defining the document structure */
51
- readonly schema: TSchema;
52
- /** Initial state (optional, will use schema defaults if not provided) */
53
- readonly initialState?: InferState<TSchema>;
54
- /** Initial version number (optional, defaults to 0) */
55
- readonly initialVersion?: number;
56
- /** Called when a transaction is successfully applied and should be broadcast */
57
- readonly onBroadcast: (message: TransactionMessage) => void;
58
- /** Called when a transaction is rejected (optional, for logging/metrics) */
59
- readonly onRejection?: (transactionId: string, reason: string) => void;
60
- /** Maximum number of processed transaction IDs to track for deduplication */
61
- readonly maxTransactionHistory?: number;
62
- }
63
- /**
64
- * A ServerDocument maintains the authoritative state and processes client transactions.
65
- */
66
- interface ServerDocument<TSchema extends AnyPrimitive> {
67
- /** The schema defining this document's structure */
68
- readonly schema: TSchema;
69
- /** Returns the current authoritative state */
70
- get(): InferState<TSchema> | undefined;
71
- /** Returns the current version number */
72
- getVersion(): number;
73
- /**
74
- * Submits a transaction for processing.
75
- * Validates and applies the transaction if valid, or rejects it with a reason.
76
- * @param transaction - The transaction to process
77
- * @returns SubmitResult indicating success with version or failure with reason
78
- */
79
- submit(transaction: Transaction): SubmitResult;
80
- /**
81
- * Returns a snapshot of the current state and version.
82
- * Used to initialize new clients or resync after drift.
83
- */
84
- getSnapshot(): SnapshotMessage;
85
- /**
86
- * Checks if a transaction has already been processed.
87
- * @param transactionId - The transaction ID to check
88
- */
89
- hasProcessed(transactionId: string): boolean;
90
- }
91
- /**
92
- * Creates a new ServerDocument for the given schema.
93
- */
94
- declare const make: <TSchema extends AnyPrimitive>(options: ServerDocumentOptions<TSchema>) => ServerDocument<TSchema>;
95
- //#endregion
96
- //#region src/server/errors.d.ts
97
- /**
98
- * Base error class for all mimic-server errors.
99
- */
100
- declare class MimicServerError extends Error {
101
- readonly _tag: string;
102
- constructor(message: string);
103
- }
104
- /**
105
- * Error thrown when a transaction fails validation.
106
- */
107
- declare class ValidationError extends MimicServerError {
108
- readonly _tag = "ValidationError";
109
- readonly transactionId: string;
110
- constructor(transactionId: string, message: string);
111
- }
112
- /**
113
- * Error thrown when an operation is invalid for the current schema.
114
- */
115
- declare class InvalidOperationError extends MimicServerError {
116
- readonly _tag = "InvalidOperationError";
117
- readonly operationKind: string;
118
- readonly path: string;
119
- constructor(operationKind: string, path: string, message: string);
120
- }
121
- /**
122
- * Error thrown when an operation cannot be applied to the current state.
123
- */
124
- declare class StateValidationError extends MimicServerError {
125
- readonly _tag = "StateValidationError";
126
- readonly transactionId: string;
127
- readonly cause?: Error;
128
- constructor(transactionId: string, message: string, cause?: Error);
129
- }
130
- /**
131
- * Error thrown when attempting to apply an empty transaction.
132
- */
133
- declare class EmptyTransactionError extends MimicServerError {
134
- readonly _tag = "EmptyTransactionError";
135
- readonly transactionId: string;
136
- constructor(transactionId: string);
137
- }
138
- /**
139
- * Error thrown when a duplicate transaction is submitted.
140
- */
141
- declare class DuplicateTransactionError extends MimicServerError {
142
- readonly _tag = "DuplicateTransactionError";
143
- readonly transactionId: string;
144
- constructor(transactionId: string);
145
- }
146
- //#endregion
147
- export { DuplicateTransactionError, EmptyTransactionError, type ErrorMessage, InvalidOperationError, MimicServerError, ServerDocument_d_exports as ServerDocument, type ServerDocumentOptions, type ServerMessage, type SnapshotMessage, StateValidationError, type SubmitResult, type TransactionMessage, ValidationError };
148
- //# sourceMappingURL=index.d.cts.map
1
+ import { ErrorMessage, ServerDocumentOptions, ServerDocument_d_exports, ServerMessage, SnapshotMessage, SubmitResult, TransactionMessage } from "./ServerDocument.cjs";
2
+ import { DuplicateTransactionError, EmptyTransactionError, InvalidOperationError, MimicServerError, StateValidationError, ValidationError } from "./errors.cjs";
3
+ export { DuplicateTransactionError, EmptyTransactionError, type ErrorMessage, InvalidOperationError, MimicServerError, ServerDocument_d_exports as ServerDocument, type ServerDocumentOptions, type ServerMessage, type SnapshotMessage, StateValidationError, type SubmitResult, type TransactionMessage, ValidationError };
@@ -1,148 +1,3 @@
1
- import { p as Transaction, r as AnyPrimitive, s as InferState } from "../Primitive-lEhQyGVL.mjs";
2
-
3
- //#region src/server/ServerDocument.d.ts
4
- declare namespace ServerDocument_d_exports {
5
- export { ErrorMessage, ServerDocument, ServerDocumentOptions, ServerMessage, SnapshotMessage, SubmitResult, TransactionMessage, make };
6
- }
7
- /**
8
- * Message sent when broadcasting a committed transaction.
9
- */
10
- interface TransactionMessage {
11
- readonly type: "transaction";
12
- readonly transaction: Transaction;
13
- /** Server-assigned version number for ordering */
14
- readonly version: number;
15
- }
16
- /**
17
- * Message sent when a transaction is rejected.
18
- */
19
- interface ErrorMessage {
20
- readonly type: "error";
21
- readonly transactionId: string;
22
- readonly reason: string;
23
- }
24
- /**
25
- * Message sent as a full state snapshot.
26
- */
27
- interface SnapshotMessage {
28
- readonly type: "snapshot";
29
- readonly state: unknown;
30
- readonly version: number;
31
- }
32
- /**
33
- * Union of all server messages that can be broadcast.
34
- */
35
- type ServerMessage = TransactionMessage | ErrorMessage | SnapshotMessage;
36
- /**
37
- * Result of submitting a transaction to the server.
38
- */
39
- type SubmitResult = {
40
- readonly success: true;
41
- readonly version: number;
42
- } | {
43
- readonly success: false;
44
- readonly reason: string;
45
- };
46
- /**
47
- * Options for creating a ServerDocument.
48
- */
49
- interface ServerDocumentOptions<TSchema extends AnyPrimitive> {
50
- /** The schema defining the document structure */
51
- readonly schema: TSchema;
52
- /** Initial state (optional, will use schema defaults if not provided) */
53
- readonly initialState?: InferState<TSchema>;
54
- /** Initial version number (optional, defaults to 0) */
55
- readonly initialVersion?: number;
56
- /** Called when a transaction is successfully applied and should be broadcast */
57
- readonly onBroadcast: (message: TransactionMessage) => void;
58
- /** Called when a transaction is rejected (optional, for logging/metrics) */
59
- readonly onRejection?: (transactionId: string, reason: string) => void;
60
- /** Maximum number of processed transaction IDs to track for deduplication */
61
- readonly maxTransactionHistory?: number;
62
- }
63
- /**
64
- * A ServerDocument maintains the authoritative state and processes client transactions.
65
- */
66
- interface ServerDocument<TSchema extends AnyPrimitive> {
67
- /** The schema defining this document's structure */
68
- readonly schema: TSchema;
69
- /** Returns the current authoritative state */
70
- get(): InferState<TSchema> | undefined;
71
- /** Returns the current version number */
72
- getVersion(): number;
73
- /**
74
- * Submits a transaction for processing.
75
- * Validates and applies the transaction if valid, or rejects it with a reason.
76
- * @param transaction - The transaction to process
77
- * @returns SubmitResult indicating success with version or failure with reason
78
- */
79
- submit(transaction: Transaction): SubmitResult;
80
- /**
81
- * Returns a snapshot of the current state and version.
82
- * Used to initialize new clients or resync after drift.
83
- */
84
- getSnapshot(): SnapshotMessage;
85
- /**
86
- * Checks if a transaction has already been processed.
87
- * @param transactionId - The transaction ID to check
88
- */
89
- hasProcessed(transactionId: string): boolean;
90
- }
91
- /**
92
- * Creates a new ServerDocument for the given schema.
93
- */
94
- declare const make: <TSchema extends AnyPrimitive>(options: ServerDocumentOptions<TSchema>) => ServerDocument<TSchema>;
95
- //#endregion
96
- //#region src/server/errors.d.ts
97
- /**
98
- * Base error class for all mimic-server errors.
99
- */
100
- declare class MimicServerError extends Error {
101
- readonly _tag: string;
102
- constructor(message: string);
103
- }
104
- /**
105
- * Error thrown when a transaction fails validation.
106
- */
107
- declare class ValidationError extends MimicServerError {
108
- readonly _tag = "ValidationError";
109
- readonly transactionId: string;
110
- constructor(transactionId: string, message: string);
111
- }
112
- /**
113
- * Error thrown when an operation is invalid for the current schema.
114
- */
115
- declare class InvalidOperationError extends MimicServerError {
116
- readonly _tag = "InvalidOperationError";
117
- readonly operationKind: string;
118
- readonly path: string;
119
- constructor(operationKind: string, path: string, message: string);
120
- }
121
- /**
122
- * Error thrown when an operation cannot be applied to the current state.
123
- */
124
- declare class StateValidationError extends MimicServerError {
125
- readonly _tag = "StateValidationError";
126
- readonly transactionId: string;
127
- readonly cause?: Error;
128
- constructor(transactionId: string, message: string, cause?: Error);
129
- }
130
- /**
131
- * Error thrown when attempting to apply an empty transaction.
132
- */
133
- declare class EmptyTransactionError extends MimicServerError {
134
- readonly _tag = "EmptyTransactionError";
135
- readonly transactionId: string;
136
- constructor(transactionId: string);
137
- }
138
- /**
139
- * Error thrown when a duplicate transaction is submitted.
140
- */
141
- declare class DuplicateTransactionError extends MimicServerError {
142
- readonly _tag = "DuplicateTransactionError";
143
- readonly transactionId: string;
144
- constructor(transactionId: string);
145
- }
146
- //#endregion
147
- export { DuplicateTransactionError, EmptyTransactionError, type ErrorMessage, InvalidOperationError, MimicServerError, ServerDocument_d_exports as ServerDocument, type ServerDocumentOptions, type ServerMessage, type SnapshotMessage, StateValidationError, type SubmitResult, type TransactionMessage, ValidationError };
148
- //# sourceMappingURL=index.d.mts.map
1
+ import { ErrorMessage, ServerDocumentOptions, ServerDocument_d_exports, ServerMessage, SnapshotMessage, SubmitResult, TransactionMessage } from "./ServerDocument.mjs";
2
+ import { DuplicateTransactionError, EmptyTransactionError, InvalidOperationError, MimicServerError, StateValidationError, ValidationError } from "./errors.mjs";
3
+ export { DuplicateTransactionError, EmptyTransactionError, type ErrorMessage, InvalidOperationError, MimicServerError, ServerDocument_d_exports as ServerDocument, type ServerDocumentOptions, type ServerMessage, type SnapshotMessage, StateValidationError, type SubmitResult, type TransactionMessage, ValidationError };
@@ -1,182 +1,4 @@
1
- import { t as __export } from "../chunk-CLMFDpHK.mjs";
2
- import { m as _defineProperty, n as make$1, o as isEmpty } from "../Document-CwiAFTIq.mjs";
1
+ import { ServerDocument_exports } from "./ServerDocument.mjs";
2
+ import { DuplicateTransactionError, EmptyTransactionError, InvalidOperationError, MimicServerError, StateValidationError, ValidationError } from "./errors.mjs";
3
3
 
4
- //#region src/server/ServerDocument.ts
5
- var ServerDocument_exports = /* @__PURE__ */ __export({ make: () => make });
6
- /**
7
- * Creates a new ServerDocument for the given schema.
8
- */
9
- const make = (options) => {
10
- const { schema, initialState, initialVersion = 0, onBroadcast, onRejection, maxTransactionHistory = 1e3 } = options;
11
- let _document = make$1(schema, { initial: initialState });
12
- let _version = initialVersion;
13
- const _processedTransactions = /* @__PURE__ */ new Set();
14
- const _transactionOrder = [];
15
- /**
16
- * Records a transaction as processed, maintaining the history limit.
17
- */
18
- const recordTransaction = (transactionId) => {
19
- _processedTransactions.add(transactionId);
20
- _transactionOrder.push(transactionId);
21
- while (_transactionOrder.length > maxTransactionHistory) {
22
- const oldest = _transactionOrder.shift();
23
- if (oldest) _processedTransactions.delete(oldest);
24
- }
25
- };
26
- /**
27
- * Validates that the transaction can be applied to the current state.
28
- * Creates a temporary document and attempts to apply the operations.
29
- */
30
- const validateTransaction = (transaction) => {
31
- if (isEmpty(transaction)) return {
32
- valid: false,
33
- reason: "Transaction is empty"
34
- };
35
- if (_processedTransactions.has(transaction.id)) return {
36
- valid: false,
37
- reason: "Transaction has already been processed"
38
- };
39
- const currentState = _document.get();
40
- const tempDoc = make$1(schema, { initial: currentState });
41
- try {
42
- tempDoc.apply(transaction.ops);
43
- return { valid: true };
44
- } catch (error) {
45
- return {
46
- valid: false,
47
- reason: error instanceof Error ? error.message : String(error)
48
- };
49
- }
50
- };
51
- return {
52
- schema,
53
- get: () => {
54
- return _document.get();
55
- },
56
- getVersion: () => {
57
- return _version;
58
- },
59
- submit: (transaction) => {
60
- const validation = validateTransaction(transaction);
61
- if (!validation.valid) {
62
- onRejection === null || onRejection === void 0 || onRejection(transaction.id, validation.reason);
63
- return {
64
- success: false,
65
- reason: validation.reason
66
- };
67
- }
68
- try {
69
- _document.apply(transaction.ops);
70
- } catch (error) {
71
- const reason = error instanceof Error ? error.message : String(error);
72
- onRejection === null || onRejection === void 0 || onRejection(transaction.id, reason);
73
- return {
74
- success: false,
75
- reason
76
- };
77
- }
78
- _version += 1;
79
- recordTransaction(transaction.id);
80
- onBroadcast({
81
- type: "transaction",
82
- transaction,
83
- version: _version
84
- });
85
- return {
86
- success: true,
87
- version: _version
88
- };
89
- },
90
- getSnapshot: () => {
91
- return {
92
- type: "snapshot",
93
- state: _document.get(),
94
- version: _version
95
- };
96
- },
97
- hasProcessed: (transactionId) => {
98
- return _processedTransactions.has(transactionId);
99
- }
100
- };
101
- };
102
-
103
- //#endregion
104
- //#region src/server/errors.ts
105
- /**
106
- * Base error class for all mimic-server errors.
107
- */
108
- var MimicServerError = class extends Error {
109
- constructor(message) {
110
- super(message);
111
- _defineProperty(this, "_tag", "MimicServerError");
112
- this.name = "MimicServerError";
113
- }
114
- };
115
- /**
116
- * Error thrown when a transaction fails validation.
117
- */
118
- var ValidationError = class extends MimicServerError {
119
- constructor(transactionId, message) {
120
- super(`Transaction ${transactionId} validation failed: ${message}`);
121
- _defineProperty(this, "_tag", "ValidationError");
122
- _defineProperty(this, "transactionId", void 0);
123
- this.name = "ValidationError";
124
- this.transactionId = transactionId;
125
- }
126
- };
127
- /**
128
- * Error thrown when an operation is invalid for the current schema.
129
- */
130
- var InvalidOperationError = class extends MimicServerError {
131
- constructor(operationKind, path, message) {
132
- super(`Invalid operation ${operationKind} at path "${path}": ${message}`);
133
- _defineProperty(this, "_tag", "InvalidOperationError");
134
- _defineProperty(this, "operationKind", void 0);
135
- _defineProperty(this, "path", void 0);
136
- this.name = "InvalidOperationError";
137
- this.operationKind = operationKind;
138
- this.path = path;
139
- }
140
- };
141
- /**
142
- * Error thrown when an operation cannot be applied to the current state.
143
- */
144
- var StateValidationError = class extends MimicServerError {
145
- constructor(transactionId, message, cause) {
146
- super(`Transaction ${transactionId} cannot be applied to current state: ${message}`);
147
- _defineProperty(this, "_tag", "StateValidationError");
148
- _defineProperty(this, "transactionId", void 0);
149
- _defineProperty(this, "cause", void 0);
150
- this.name = "StateValidationError";
151
- this.transactionId = transactionId;
152
- this.cause = cause;
153
- }
154
- };
155
- /**
156
- * Error thrown when attempting to apply an empty transaction.
157
- */
158
- var EmptyTransactionError = class extends MimicServerError {
159
- constructor(transactionId) {
160
- super(`Transaction ${transactionId} is empty and cannot be applied`);
161
- _defineProperty(this, "_tag", "EmptyTransactionError");
162
- _defineProperty(this, "transactionId", void 0);
163
- this.name = "EmptyTransactionError";
164
- this.transactionId = transactionId;
165
- }
166
- };
167
- /**
168
- * Error thrown when a duplicate transaction is submitted.
169
- */
170
- var DuplicateTransactionError = class extends MimicServerError {
171
- constructor(transactionId) {
172
- super(`Transaction ${transactionId} has already been processed`);
173
- _defineProperty(this, "_tag", "DuplicateTransactionError");
174
- _defineProperty(this, "transactionId", void 0);
175
- this.name = "DuplicateTransactionError";
176
- this.transactionId = transactionId;
177
- }
178
- };
179
-
180
- //#endregion
181
- export { DuplicateTransactionError, EmptyTransactionError, InvalidOperationError, MimicServerError, ServerDocument_exports as ServerDocument, StateValidationError, ValidationError };
182
- //# sourceMappingURL=index.mjs.map
4
+ export { DuplicateTransactionError, EmptyTransactionError, InvalidOperationError, MimicServerError, ServerDocument_exports as ServerDocument, StateValidationError, ValidationError };