@voidhash/mimic-effect 0.0.9 → 1.0.0-beta.10

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 (236) hide show
  1. package/.turbo/turbo-build.log +136 -90
  2. package/README.md +385 -0
  3. package/dist/ColdStorage.cjs +64 -0
  4. package/dist/ColdStorage.d.cts +53 -0
  5. package/dist/ColdStorage.d.cts.map +1 -0
  6. package/dist/ColdStorage.d.mts +53 -0
  7. package/dist/ColdStorage.d.mts.map +1 -0
  8. package/dist/ColdStorage.mjs +64 -0
  9. package/dist/ColdStorage.mjs.map +1 -0
  10. package/dist/DocumentInstance.cjs +263 -0
  11. package/dist/DocumentInstance.d.cts +78 -0
  12. package/dist/DocumentInstance.d.cts.map +1 -0
  13. package/dist/DocumentInstance.d.mts +78 -0
  14. package/dist/DocumentInstance.d.mts.map +1 -0
  15. package/dist/DocumentInstance.mjs +264 -0
  16. package/dist/DocumentInstance.mjs.map +1 -0
  17. package/dist/Errors.cjs +54 -0
  18. package/dist/Errors.d.cts +96 -0
  19. package/dist/Errors.d.cts.map +1 -0
  20. package/dist/Errors.d.mts +96 -0
  21. package/dist/Errors.d.mts.map +1 -0
  22. package/dist/Errors.mjs +48 -0
  23. package/dist/Errors.mjs.map +1 -0
  24. package/dist/HotStorage.cjs +104 -0
  25. package/dist/HotStorage.d.cts +70 -0
  26. package/dist/HotStorage.d.cts.map +1 -0
  27. package/dist/HotStorage.d.mts +70 -0
  28. package/dist/HotStorage.d.mts.map +1 -0
  29. package/dist/HotStorage.mjs +104 -0
  30. package/dist/HotStorage.mjs.map +1 -0
  31. package/dist/Metrics.cjs +149 -0
  32. package/dist/Metrics.d.cts +32 -0
  33. package/dist/Metrics.d.cts.map +1 -0
  34. package/dist/Metrics.d.mts +32 -0
  35. package/dist/Metrics.d.mts.map +1 -0
  36. package/dist/Metrics.mjs +131 -0
  37. package/dist/Metrics.mjs.map +1 -0
  38. package/dist/MimicAuthService.cjs +61 -45
  39. package/dist/MimicAuthService.d.cts +61 -48
  40. package/dist/MimicAuthService.d.cts.map +1 -1
  41. package/dist/MimicAuthService.d.mts +61 -48
  42. package/dist/MimicAuthService.d.mts.map +1 -1
  43. package/dist/MimicAuthService.mjs +60 -36
  44. package/dist/MimicAuthService.mjs.map +1 -1
  45. package/dist/MimicClusterServerEngine.cjs +348 -0
  46. package/dist/MimicClusterServerEngine.d.cts +17 -0
  47. package/dist/MimicClusterServerEngine.d.cts.map +1 -0
  48. package/dist/MimicClusterServerEngine.d.mts +17 -0
  49. package/dist/MimicClusterServerEngine.d.mts.map +1 -0
  50. package/dist/MimicClusterServerEngine.mjs +350 -0
  51. package/dist/MimicClusterServerEngine.mjs.map +1 -0
  52. package/dist/MimicServer.cjs +205 -96
  53. package/dist/MimicServer.d.cts +9 -110
  54. package/dist/MimicServer.d.cts.map +1 -1
  55. package/dist/MimicServer.d.mts +9 -110
  56. package/dist/MimicServer.d.mts.map +1 -1
  57. package/dist/MimicServer.mjs +206 -90
  58. package/dist/MimicServer.mjs.map +1 -1
  59. package/dist/MimicServerEngine.cjs +178 -0
  60. package/dist/MimicServerEngine.d.cts +83 -0
  61. package/dist/MimicServerEngine.d.cts.map +1 -0
  62. package/dist/MimicServerEngine.d.mts +83 -0
  63. package/dist/MimicServerEngine.d.mts.map +1 -0
  64. package/dist/MimicServerEngine.mjs +178 -0
  65. package/dist/MimicServerEngine.mjs.map +1 -0
  66. package/dist/PresenceManager.cjs +75 -91
  67. package/dist/PresenceManager.d.cts +17 -66
  68. package/dist/PresenceManager.d.cts.map +1 -1
  69. package/dist/PresenceManager.d.mts +17 -66
  70. package/dist/PresenceManager.d.mts.map +1 -1
  71. package/dist/PresenceManager.mjs +74 -78
  72. package/dist/PresenceManager.mjs.map +1 -1
  73. package/dist/Protocol.cjs +146 -0
  74. package/dist/Protocol.d.cts +203 -0
  75. package/dist/Protocol.d.cts.map +1 -0
  76. package/dist/Protocol.d.mts +203 -0
  77. package/dist/Protocol.d.mts.map +1 -0
  78. package/dist/Protocol.mjs +132 -0
  79. package/dist/Protocol.mjs.map +1 -0
  80. package/dist/Types.d.cts +179 -0
  81. package/dist/Types.d.cts.map +1 -0
  82. package/dist/Types.d.mts +179 -0
  83. package/dist/Types.d.mts.map +1 -0
  84. package/dist/_virtual/rolldown_runtime.cjs +1 -25
  85. package/dist/_virtual/rolldown_runtime.mjs +4 -1
  86. package/dist/index.cjs +37 -76
  87. package/dist/index.d.cts +13 -12
  88. package/dist/index.d.mts +13 -12
  89. package/dist/index.mjs +12 -12
  90. package/dist/testing/ColdStorageTestSuite.cjs +508 -0
  91. package/dist/testing/ColdStorageTestSuite.d.cts +36 -0
  92. package/dist/testing/ColdStorageTestSuite.d.cts.map +1 -0
  93. package/dist/testing/ColdStorageTestSuite.d.mts +36 -0
  94. package/dist/testing/ColdStorageTestSuite.d.mts.map +1 -0
  95. package/dist/testing/ColdStorageTestSuite.mjs +508 -0
  96. package/dist/testing/ColdStorageTestSuite.mjs.map +1 -0
  97. package/dist/testing/FailingStorage.cjs +162 -0
  98. package/dist/testing/FailingStorage.d.cts +43 -0
  99. package/dist/testing/FailingStorage.d.cts.map +1 -0
  100. package/dist/testing/FailingStorage.d.mts +43 -0
  101. package/dist/testing/FailingStorage.d.mts.map +1 -0
  102. package/dist/testing/FailingStorage.mjs +163 -0
  103. package/dist/testing/FailingStorage.mjs.map +1 -0
  104. package/dist/testing/HotStorageTestSuite.cjs +820 -0
  105. package/dist/testing/HotStorageTestSuite.d.cts +42 -0
  106. package/dist/testing/HotStorageTestSuite.d.cts.map +1 -0
  107. package/dist/testing/HotStorageTestSuite.d.mts +42 -0
  108. package/dist/testing/HotStorageTestSuite.d.mts.map +1 -0
  109. package/dist/testing/HotStorageTestSuite.mjs +820 -0
  110. package/dist/testing/HotStorageTestSuite.mjs.map +1 -0
  111. package/dist/testing/StorageIntegrationTestSuite.cjs +487 -0
  112. package/dist/testing/StorageIntegrationTestSuite.d.cts +37 -0
  113. package/dist/testing/StorageIntegrationTestSuite.d.cts.map +1 -0
  114. package/dist/testing/StorageIntegrationTestSuite.d.mts +37 -0
  115. package/dist/testing/StorageIntegrationTestSuite.d.mts.map +1 -0
  116. package/dist/testing/StorageIntegrationTestSuite.mjs +487 -0
  117. package/dist/testing/StorageIntegrationTestSuite.mjs.map +1 -0
  118. package/dist/testing/assertions.cjs +117 -0
  119. package/dist/testing/assertions.mjs +112 -0
  120. package/dist/testing/assertions.mjs.map +1 -0
  121. package/dist/testing/index.cjs +14 -0
  122. package/dist/testing/index.d.cts +6 -0
  123. package/dist/testing/index.d.mts +6 -0
  124. package/dist/testing/index.mjs +7 -0
  125. package/dist/testing/types.cjs +15 -0
  126. package/dist/testing/types.d.cts +90 -0
  127. package/dist/testing/types.d.cts.map +1 -0
  128. package/dist/testing/types.d.mts +90 -0
  129. package/dist/testing/types.d.mts.map +1 -0
  130. package/dist/testing/types.mjs +16 -0
  131. package/dist/testing/types.mjs.map +1 -0
  132. package/package.json +18 -3
  133. package/src/ColdStorage.ts +145 -0
  134. package/src/DocumentInstance.ts +527 -0
  135. package/src/Errors.ts +114 -0
  136. package/src/HotStorage.ts +256 -0
  137. package/src/Metrics.ts +193 -0
  138. package/src/MimicAuthService.ts +126 -64
  139. package/src/MimicClusterServerEngine.ts +669 -0
  140. package/src/MimicServer.ts +459 -198
  141. package/src/MimicServerEngine.ts +472 -0
  142. package/src/PresenceManager.ts +173 -234
  143. package/src/Protocol.ts +350 -0
  144. package/src/Types.ts +238 -0
  145. package/src/index.ts +27 -23
  146. package/src/testing/ColdStorageTestSuite.ts +589 -0
  147. package/src/testing/FailingStorage.ts +338 -0
  148. package/src/testing/HotStorageTestSuite.ts +1105 -0
  149. package/src/testing/StorageIntegrationTestSuite.ts +736 -0
  150. package/src/testing/assertions.ts +188 -0
  151. package/src/testing/index.ts +83 -0
  152. package/src/testing/types.ts +100 -0
  153. package/tests/ColdStorage.test.ts +24 -0
  154. package/tests/DocumentInstance.test.ts +669 -0
  155. package/tests/HotStorage.test.ts +24 -0
  156. package/tests/MimicAuthService.test.ts +102 -134
  157. package/tests/MimicClusterServerEngine.test.ts +587 -0
  158. package/tests/MimicServer.test.ts +90 -226
  159. package/tests/MimicServerEngine.test.ts +521 -0
  160. package/tests/PresenceManager.test.ts +22 -63
  161. package/tests/Protocol.test.ts +190 -0
  162. package/tests/StorageIntegration.test.ts +259 -0
  163. package/tsconfig.json +1 -1
  164. package/tsdown.config.ts +1 -1
  165. package/dist/DocumentManager.cjs +0 -118
  166. package/dist/DocumentManager.d.cts +0 -45
  167. package/dist/DocumentManager.d.cts.map +0 -1
  168. package/dist/DocumentManager.d.mts +0 -45
  169. package/dist/DocumentManager.d.mts.map +0 -1
  170. package/dist/DocumentManager.mjs +0 -105
  171. package/dist/DocumentManager.mjs.map +0 -1
  172. package/dist/DocumentProtocol.cjs +0 -94
  173. package/dist/DocumentProtocol.d.cts +0 -113
  174. package/dist/DocumentProtocol.d.cts.map +0 -1
  175. package/dist/DocumentProtocol.d.mts +0 -113
  176. package/dist/DocumentProtocol.d.mts.map +0 -1
  177. package/dist/DocumentProtocol.mjs +0 -89
  178. package/dist/DocumentProtocol.mjs.map +0 -1
  179. package/dist/MimicConfig.cjs +0 -60
  180. package/dist/MimicConfig.d.cts +0 -141
  181. package/dist/MimicConfig.d.cts.map +0 -1
  182. package/dist/MimicConfig.d.mts +0 -141
  183. package/dist/MimicConfig.d.mts.map +0 -1
  184. package/dist/MimicConfig.mjs +0 -50
  185. package/dist/MimicConfig.mjs.map +0 -1
  186. package/dist/MimicDataStorage.cjs +0 -83
  187. package/dist/MimicDataStorage.d.cts +0 -113
  188. package/dist/MimicDataStorage.d.cts.map +0 -1
  189. package/dist/MimicDataStorage.d.mts +0 -113
  190. package/dist/MimicDataStorage.d.mts.map +0 -1
  191. package/dist/MimicDataStorage.mjs +0 -74
  192. package/dist/MimicDataStorage.mjs.map +0 -1
  193. package/dist/WebSocketHandler.cjs +0 -365
  194. package/dist/WebSocketHandler.d.cts +0 -34
  195. package/dist/WebSocketHandler.d.cts.map +0 -1
  196. package/dist/WebSocketHandler.d.mts +0 -34
  197. package/dist/WebSocketHandler.d.mts.map +0 -1
  198. package/dist/WebSocketHandler.mjs +0 -355
  199. package/dist/WebSocketHandler.mjs.map +0 -1
  200. package/dist/auth/NoAuth.cjs +0 -43
  201. package/dist/auth/NoAuth.d.cts +0 -22
  202. package/dist/auth/NoAuth.d.cts.map +0 -1
  203. package/dist/auth/NoAuth.d.mts +0 -22
  204. package/dist/auth/NoAuth.d.mts.map +0 -1
  205. package/dist/auth/NoAuth.mjs +0 -36
  206. package/dist/auth/NoAuth.mjs.map +0 -1
  207. package/dist/errors.cjs +0 -74
  208. package/dist/errors.d.cts +0 -89
  209. package/dist/errors.d.cts.map +0 -1
  210. package/dist/errors.d.mts +0 -89
  211. package/dist/errors.d.mts.map +0 -1
  212. package/dist/errors.mjs +0 -67
  213. package/dist/errors.mjs.map +0 -1
  214. package/dist/storage/InMemoryDataStorage.cjs +0 -57
  215. package/dist/storage/InMemoryDataStorage.d.cts +0 -19
  216. package/dist/storage/InMemoryDataStorage.d.cts.map +0 -1
  217. package/dist/storage/InMemoryDataStorage.d.mts +0 -19
  218. package/dist/storage/InMemoryDataStorage.d.mts.map +0 -1
  219. package/dist/storage/InMemoryDataStorage.mjs +0 -48
  220. package/dist/storage/InMemoryDataStorage.mjs.map +0 -1
  221. package/src/DocumentManager.ts +0 -254
  222. package/src/DocumentProtocol.ts +0 -112
  223. package/src/MimicConfig.ts +0 -211
  224. package/src/MimicDataStorage.ts +0 -157
  225. package/src/WebSocketHandler.ts +0 -735
  226. package/src/auth/NoAuth.ts +0 -46
  227. package/src/errors.ts +0 -113
  228. package/src/storage/InMemoryDataStorage.ts +0 -66
  229. package/tests/DocumentManager.test.ts +0 -464
  230. package/tests/DocumentProtocol.test.ts +0 -113
  231. package/tests/InMemoryDataStorage.test.ts +0 -190
  232. package/tests/MimicConfig.test.ts +0 -290
  233. package/tests/MimicDataStorage.test.ts +0 -190
  234. package/tests/NoAuth.test.ts +0 -94
  235. package/tests/WebSocketHandler.test.ts +0 -321
  236. package/tests/errors.test.ts +0 -77
@@ -1,119 +1,235 @@
1
- import { __export } from "./_virtual/rolldown_runtime.mjs";
2
- import { MimicServerConfigTag, layer } from "./MimicConfig.mjs";
3
- import { DocumentManagerTag, layer as layer$1 } from "./DocumentManager.mjs";
1
+ import { MissingDocumentIdError } from "./Errors.mjs";
2
+ import { connectionsActive, connectionsDuration, connectionsErrors, connectionsTotal } from "./Metrics.mjs";
3
+ import { MimicServerEngineTag } from "./MimicServerEngine.mjs";
4
+ import { authResultFailure, authResultSuccess, encodeServerMessage, errorMessage, parseClientMessage, pong, presenceRemoveMessage, presenceSnapshotMessage, presenceUpdateMessage, snapshotMessage } from "./Protocol.mjs";
4
5
  import { MimicAuthServiceTag } from "./MimicAuthService.mjs";
5
- import { PresenceManagerTag, layer as layer$2 } from "./PresenceManager.mjs";
6
- import { extractDocumentId, handleConnection } from "./WebSocketHandler.mjs";
7
- import { layerDefault } from "./storage/InMemoryDataStorage.mjs";
8
- import { layerDefault as layerDefault$1 } from "./auth/NoAuth.mjs";
9
- import * as Effect from "effect/Effect";
10
- import * as Layer from "effect/Layer";
11
- import { HttpLayerRouter, HttpServerRequest, HttpServerResponse } from "@effect/platform";
6
+ import { Duration, Effect, Fiber, Layer, Metric, Stream } from "effect";
7
+ import { Presence } from "@voidhash/mimic";
8
+ import { HttpLayerRouter, HttpServerResponse } from "@effect/platform";
12
9
 
13
10
  //#region src/MimicServer.ts
14
11
  /**
15
- * @since 0.0.1
16
- * Mimic server layer composition.
12
+ * @voidhash/mimic-effect - MimicServer
13
+ *
14
+ * WebSocket route layer for MimicServerEngine.
15
+ * Creates routes compatible with HttpLayerRouter.
17
16
  */
18
- var MimicServer_exports = /* @__PURE__ */ __export({
19
- documentManagerLayer: () => documentManagerLayer,
20
- layerHttpLayerRouter: () => layerHttpLayerRouter
21
- });
17
+ const DEFAULT_PATH = "/mimic";
18
+ const DEFAULT_HEARTBEAT_INTERVAL = Duration.seconds(30);
19
+ const DEFAULT_HEARTBEAT_TIMEOUT = Duration.seconds(10);
20
+ /**
21
+ * Resolve route configuration with defaults
22
+ */
23
+ const resolveRouteConfig = (config) => {
24
+ var _config$path;
25
+ return {
26
+ path: (_config$path = config === null || config === void 0 ? void 0 : config.path) !== null && _config$path !== void 0 ? _config$path : DEFAULT_PATH,
27
+ heartbeatInterval: (config === null || config === void 0 ? void 0 : config.heartbeatInterval) ? Duration.decode(config.heartbeatInterval) : DEFAULT_HEARTBEAT_INTERVAL,
28
+ heartbeatTimeout: (config === null || config === void 0 ? void 0 : config.heartbeatTimeout) ? Duration.decode(config.heartbeatTimeout) : DEFAULT_HEARTBEAT_TIMEOUT
29
+ };
30
+ };
22
31
  /**
23
- * Create the document manager layer.
32
+ * Extract document ID from URL path.
33
+ * Expected format: /basePath/doc/{documentId}
24
34
  */
25
- const documentManagerLayer = (options) => layer$1.pipe(Layer.provide(layer(options)), Layer.provide(layerDefault), Layer.provide(layerDefault$1));
35
+ const extractDocumentId = (path) => {
36
+ const parts = path.replace(/^\/+/, "").split("/");
37
+ const docIndex = parts.lastIndexOf("doc");
38
+ const part = parts[docIndex + 1];
39
+ if (docIndex !== -1 && part) return Effect.succeed(decodeURIComponent(part));
40
+ return Effect.fail(new MissingDocumentIdError({ path }));
41
+ };
26
42
  /**
27
- * Create the HTTP handler effect for WebSocket upgrade.
28
- * This handler:
29
- * 1. Extracts the document ID from the URL path
30
- * 2. Upgrades the HTTP connection to WebSocket
31
- * 3. Delegates to the WebSocketHandler for document sync
43
+ * Handle a WebSocket connection for a document.
32
44
  */
33
- const makeMimicHandler = Effect.gen(function* () {
34
- const config = yield* MimicServerConfigTag;
35
- const authService = yield* MimicAuthServiceTag;
36
- const documentManager = yield* DocumentManagerTag;
37
- const presenceManager = yield* PresenceManagerTag;
38
- return Effect.gen(function* () {
39
- const request = yield* HttpServerRequest.HttpServerRequest;
40
- yield* extractDocumentId(request.url);
41
- const socket = yield* request.upgrade;
42
- yield* handleConnection(socket, request.url).pipe(Effect.provideService(MimicServerConfigTag, config), Effect.provideService(MimicAuthServiceTag, authService), Effect.provideService(DocumentManagerTag, documentManager), Effect.provideService(PresenceManagerTag, presenceManager), Effect.scoped, Effect.catchAll((error) => Effect.logError("WebSocket connection error", error)));
43
- return HttpServerResponse.empty();
44
- }).pipe(Effect.catchAll((error) => Effect.gen(function* () {
45
- yield* Effect.logWarning("WebSocket upgrade failed", error);
46
- return HttpServerResponse.text("WebSocket upgrade failed", { status: 400 });
47
- })));
45
+ const handleWebSocketConnection = Effect.fn("websocket.connection.handle")(function* (socket, documentId, engine, authService, _routeConfig) {
46
+ const connectionId = crypto.randomUUID();
47
+ const connectionStartTime = Date.now();
48
+ yield* Metric.increment(connectionsTotal);
49
+ yield* Metric.incrementBy(connectionsActive, 1);
50
+ const state = {
51
+ documentId,
52
+ connectionId,
53
+ authenticated: false,
54
+ hasPresence: false
55
+ };
56
+ const write = yield* socket.writer;
57
+ const sendMessage = (message) => write(encodeServerMessage(message));
58
+ const sendPresenceSnapshot = Effect.fn("presence.snapshot.send")(function* () {
59
+ if (!engine.config.presence) return;
60
+ const snapshot = yield* engine.getPresenceSnapshot(documentId);
61
+ yield* sendMessage(presenceSnapshotMessage(connectionId, snapshot.presences));
62
+ });
63
+ const sendDocumentSnapshot = Effect.fn("document.snapshot.send")(function* () {
64
+ const snapshot = yield* engine.getSnapshot(documentId);
65
+ yield* sendMessage(snapshotMessage(snapshot.state, snapshot.version));
66
+ });
67
+ const handleAuth = Effect.fn("auth.handle")(function* (token) {
68
+ const result = yield* Effect.either(authService.authenticate(token, documentId));
69
+ if (result._tag === "Right") {
70
+ state.authenticated = true;
71
+ state.authContext = result.right;
72
+ yield* sendMessage(authResultSuccess(result.right.userId, result.right.permission));
73
+ yield* sendDocumentSnapshot();
74
+ yield* sendPresenceSnapshot();
75
+ } else {
76
+ var _result$left$reason;
77
+ yield* Metric.increment(connectionsErrors);
78
+ yield* sendMessage(authResultFailure((_result$left$reason = result.left.reason) !== null && _result$left$reason !== void 0 ? _result$left$reason : "Authentication failed"));
79
+ }
80
+ });
81
+ const handlePresenceSet = Effect.fn("presence.set.handle")(function* (data) {
82
+ if (!state.authenticated) return;
83
+ if (!state.authContext) return;
84
+ if (!engine.config.presence) return;
85
+ if (state.authContext.permission !== "write") {
86
+ yield* Effect.logWarning("Presence set rejected - read-only user", { connectionId });
87
+ return;
88
+ }
89
+ const validated = Presence.validateSafe(engine.config.presence, data);
90
+ if (validated === void 0) {
91
+ yield* Effect.logWarning("Invalid presence data received", {
92
+ connectionId,
93
+ data
94
+ });
95
+ return;
96
+ }
97
+ yield* engine.setPresence(documentId, connectionId, {
98
+ data: validated,
99
+ userId: state.authContext.userId
100
+ });
101
+ state.hasPresence = true;
102
+ });
103
+ const handlePresenceClear = Effect.fn("presence.clear.handle")(function* () {
104
+ if (!state.authenticated) return;
105
+ if (!engine.config.presence) return;
106
+ yield* engine.removePresence(documentId, connectionId);
107
+ state.hasPresence = false;
108
+ });
109
+ const handleMessage = Effect.fn("message.handle")(function* (message) {
110
+ yield* engine.touch(documentId);
111
+ switch (message.type) {
112
+ case "auth":
113
+ yield* handleAuth(message.token);
114
+ break;
115
+ case "ping":
116
+ yield* sendMessage(pong());
117
+ break;
118
+ case "submit":
119
+ var _state$authContext;
120
+ if (!state.authenticated) {
121
+ yield* sendMessage(errorMessage(message.transaction.id, "Not authenticated"));
122
+ return;
123
+ }
124
+ if (((_state$authContext = state.authContext) === null || _state$authContext === void 0 ? void 0 : _state$authContext.permission) !== "write") {
125
+ yield* sendMessage(errorMessage(message.transaction.id, "Write permission required"));
126
+ return;
127
+ }
128
+ const submitResult = yield* engine.submit(documentId, message.transaction);
129
+ if (!submitResult.success) yield* sendMessage(errorMessage(message.transaction.id, submitResult.reason));
130
+ break;
131
+ case "request_snapshot":
132
+ if (!state.authenticated) return;
133
+ const snapshot = yield* engine.getSnapshot(documentId);
134
+ yield* sendMessage(snapshotMessage(snapshot.state, snapshot.version));
135
+ break;
136
+ case "presence_set":
137
+ yield* handlePresenceSet(message.data);
138
+ break;
139
+ case "presence_clear":
140
+ yield* handlePresenceClear();
141
+ break;
142
+ }
143
+ });
144
+ const subscribeFiber = yield* Effect.fork(Effect.fn("subscriptions.document.start")(function* () {
145
+ while (!state.authenticated) yield* Effect.sleep(Duration.millis(100));
146
+ const broadcastStream = yield* engine.subscribe(documentId);
147
+ yield* Stream.runForEach(broadcastStream, (broadcast) => sendMessage(broadcast));
148
+ })().pipe(Effect.scoped));
149
+ const presenceFiber = yield* Effect.fork(Effect.fn("subscriptions.presence.start")(function* () {
150
+ if (!engine.config.presence) return;
151
+ while (!state.authenticated) yield* Effect.sleep(Duration.millis(100));
152
+ const presenceStream = yield* engine.subscribePresence(documentId);
153
+ yield* Stream.runForEach(presenceStream, (event) => Effect.gen(function* () {
154
+ if (event.id === connectionId) return;
155
+ if (event.type === "presence_update") yield* sendMessage(presenceUpdateMessage(event.id, event.data, event.userId));
156
+ else if (event.type === "presence_remove") yield* sendMessage(presenceRemoveMessage(event.id));
157
+ }));
158
+ })().pipe(Effect.scoped));
159
+ yield* Effect.addFinalizer(() => Effect.fn("connection.cleanup")(function* () {
160
+ const duration = Date.now() - connectionStartTime;
161
+ yield* Fiber.interrupt(subscribeFiber);
162
+ yield* Fiber.interrupt(presenceFiber);
163
+ if (state.hasPresence && engine.config.presence) yield* engine.removePresence(documentId, connectionId);
164
+ yield* Metric.incrementBy(connectionsActive, -1);
165
+ yield* Metric.update(connectionsDuration, duration);
166
+ yield* Effect.logDebug("WebSocket connection closed", {
167
+ connectionId,
168
+ documentId,
169
+ durationMs: duration
170
+ });
171
+ })());
172
+ yield* socket.runRaw((data) => Effect.fn("message.process")(function* () {
173
+ yield* handleMessage(yield* parseClientMessage(data));
174
+ })().pipe(Effect.catchAll((error) => Effect.logError("Message handling error", error))));
48
175
  });
49
176
  /**
50
- * Create a Mimic server layer that integrates with HttpLayerRouter.
51
- *
52
- * This function creates a layer that:
53
- * 1. Registers a WebSocket route at the specified base path
54
- * 2. Handles WebSocket upgrades for document sync
55
- * 3. Provides all required dependencies (config, auth, storage, document manager)
177
+ * Create a route layer for MimicServerEngine.
56
178
  *
57
- * By default, uses in-memory storage and no authentication.
58
- * To override these defaults, provide custom layers before the defaults:
179
+ * This creates a WebSocket route that connects to the engine.
180
+ * Use Layer.mergeAll to compose with other routes.
59
181
  *
60
182
  * @example
61
183
  * ```typescript
62
- * import { MimicServer, MimicAuthService } from "@voidhash/mimic-effect";
63
- * import { HttpLayerRouter } from "@effect/platform";
64
- * import { Primitive } from "@voidhash/mimic";
184
+ * // 1. Create the engine
185
+ * const Engine = MimicServerEngine.make({
186
+ * schema: DocSchema,
187
+ * initial: { title: "Untitled" },
188
+ * })
65
189
  *
66
- * const TodoSchema = Primitive.Struct({
67
- * title: Primitive.String(),
68
- * completed: Primitive.Boolean(),
69
- * });
70
- *
71
- * // Create the Mimic route layer with defaults
190
+ * // 2. Create the WebSocket route
72
191
  * const MimicRoute = MimicServer.layerHttpLayerRouter({
73
- * basePath: "/mimic/todo",
74
- * schema: TodoSchema
75
- * });
192
+ * path: "/mimic",
193
+ * })
194
+ *
195
+ * // 3. Wire together
196
+ * const MimicLive = MimicRoute.pipe(
197
+ * Layer.provide(Engine),
198
+ * Layer.provide(ColdStorage.InMemory.make()),
199
+ * Layer.provide(HotStorage.InMemory.make()),
200
+ * Layer.provide(MimicAuthService.NoAuth.make()),
201
+ * )
76
202
  *
77
- * // Or with custom auth - use Layer.provide to inject before defaults
78
- * const MimicRouteWithAuth = MimicServer.layerHttpLayerRouter({
79
- * basePath: "/mimic/todo",
80
- * schema: TodoSchema,
81
- * authLayer: MimicAuthService.layer({
82
- * authHandler: (token) => ({ success: true, userId: token })
83
- * })
84
- * });
203
+ * // 4. Compose with other routes
204
+ * const AllRoutes = Layer.mergeAll(MimicLive, DocsRoute, OtherRoutes)
85
205
  *
86
- * // Merge with other routes and serve
87
- * const AllRoutes = Layer.mergeAll(MimicRoute, OtherRoutes);
206
+ * // 5. Serve
88
207
  * HttpLayerRouter.serve(AllRoutes).pipe(
89
- * Layer.provide(BunHttpServer.layer({ port: 3000 })),
208
+ * Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),
90
209
  * Layer.launch,
91
- * BunRuntime.runMain
92
- * );
210
+ * NodeRuntime.runMain
211
+ * )
93
212
  * ```
94
213
  */
95
- const layerHttpLayerRouter = (optionsEf) => {
96
- return Layer.unwrapScoped(Effect.gen(function* () {
97
- var _options$basePath, _options$authLayer, _options$storageLayer;
98
- const options = yield* optionsEf;
99
- const wsPath = `${(_options$basePath = options.basePath) !== null && _options$basePath !== void 0 ? _options$basePath : "/mimic"}/doc/*`;
100
- const configLayer = layer({
101
- schema: options.schema,
102
- maxTransactionHistory: options.maxTransactionHistory,
103
- presence: options.presence,
104
- initial: options.initial
214
+ const layerHttpLayerRouter = (options) => {
215
+ const routeConfig = resolveRouteConfig(options);
216
+ const routePath = `${routeConfig.path}/doc/:documentId`;
217
+ return Layer.scopedDiscard(Effect.gen(function* () {
218
+ const router = yield* HttpLayerRouter.HttpRouter;
219
+ const engine = yield* MimicServerEngineTag;
220
+ const authService = yield* MimicAuthServiceTag;
221
+ const handler = Effect.fn("websocket.route.handler")(function* (request) {
222
+ const documentIdResult = yield* Effect.either(extractDocumentId(request.url));
223
+ if (documentIdResult._tag === "Left") return HttpServerResponse.text(`Missing document ID in path: ${request.url}`, { status: 400 });
224
+ const documentId = documentIdResult.right;
225
+ yield* handleWebSocketConnection(yield* request.upgrade, documentId, engine, authService, routeConfig).pipe(Effect.scoped, Effect.catchAll((error) => Effect.logError("WebSocket connection error", error)));
226
+ return HttpServerResponse.empty();
105
227
  });
106
- const authLayer = (_options$authLayer = options.authLayer) !== null && _options$authLayer !== void 0 ? _options$authLayer : layerDefault$1;
107
- const storageLayer = (_options$storageLayer = options.storageLayer) !== null && _options$storageLayer !== void 0 ? _options$storageLayer : layerDefault;
108
- const depsLayer = Layer.mergeAll(configLayer, authLayer, storageLayer);
109
- return Layer.scopedDiscard(Effect.gen(function* () {
110
- const router = yield* HttpLayerRouter.HttpRouter;
111
- const handler = yield* makeMimicHandler;
112
- yield* router.add("GET", wsPath, handler);
113
- })).pipe(Layer.provide(layer$1), Layer.provide(layer$2), Layer.provide(depsLayer));
228
+ yield* router.add("GET", routePath, handler);
114
229
  }));
115
230
  };
231
+ const MimicServer = { layerHttpLayerRouter };
116
232
 
117
233
  //#endregion
118
- export { MimicServer_exports };
234
+ export { MimicServer };
119
235
  //# sourceMappingURL=MimicServer.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"MimicServer.mjs","names":["MimicConfig.layer","InMemoryDataStorage.layerDefault","NoAuth.layerDefault","MimicConfig.MimicServerConfigTag","DocumentManager.DocumentManagerTag","PresenceManager.PresenceManagerTag","WebSocketHandler.extractDocumentId","WebSocketHandler.handleConnection","wsPath: PathInput","DocumentManager.layer","PresenceManager.layer"],"sources":["../src/MimicServer.ts"],"sourcesContent":["/**\n * @since 0.0.1\n * Mimic server layer composition.\n */\nimport * as Effect from \"effect/Effect\";\nimport * as Layer from \"effect/Layer\";\nimport * as Context from \"effect/Context\";\nimport type * as Socket from \"@effect/platform/Socket\";\nimport { SocketServer } from \"@effect/platform/SocketServer\";\nimport type { Primitive, Presence } from \"@voidhash/mimic\";\n\nimport * as DocumentManager from \"./DocumentManager.js\";\nimport * as WebSocketHandler from \"./WebSocketHandler.js\";\nimport * as MimicConfig from \"./MimicConfig.js\";\nimport { MimicDataStorageTag } from \"./MimicDataStorage.js\";\nimport { MimicAuthServiceTag } from \"./MimicAuthService.js\";\nimport * as PresenceManager from \"./PresenceManager.js\";\nimport * as InMemoryDataStorage from \"./storage/InMemoryDataStorage.js\";\nimport * as NoAuth from \"./auth/NoAuth.js\";\nimport { HttpLayerRouter, HttpServerRequest, HttpServerResponse } from \"@effect/platform\";\nimport { PathInput } from \"@effect/platform/HttpRouter\";\n\n// =============================================================================\n// Layer Composition Options\n// =============================================================================\n\n/**\n * Options for creating a Mimic server layer.\n */\nexport interface MimicLayerOptions<\n TSchema extends Primitive.AnyPrimitive,\n> {\n /**\n * Base path for document routes (used for path matching).\n * @example \"/mimic/todo\" - documents accessed at \"/mimic/todo/:documentId\"\n */\n readonly basePath?: PathInput;\n /**\n * The schema defining the document structure.\n */\n readonly schema: TSchema;\n /**\n * Maximum number of processed transaction IDs to track for deduplication.\n * @default 1000\n */\n readonly maxTransactionHistory?: number;\n /**\n * Optional presence schema for ephemeral per-user data.\n * When provided, enables presence features on WebSocket connections.\n */\n readonly presence?: Presence.AnyPresence;\n /**\n * Initial state for new documents.\n * Can be either:\n * - A plain object with the initial state values\n * - A function that receives context (with documentId) and returns an Effect producing the initial state\n *\n * When using a function that requires Effect services (has R requirements),\n * you must also provide `initialLayer` to supply those dependencies.\n *\n * Type-safe: required fields (without defaults) must be provided,\n * while optional fields and fields with defaults can be omitted.\n *\n * @default undefined (documents start empty or use schema defaults)\n */\n readonly initial?: Primitive.InferSetInput<TSchema> | MimicConfig.InitialFn<TSchema>;\n}\n\n\n/**\n * Create the document manager layer.\n */\nexport const documentManagerLayer = <TSchema extends Primitive.AnyPrimitive>(\n options: MimicConfig.MimicServerConfigOptions<TSchema>\n): Layer.Layer<DocumentManager.DocumentManagerTag> =>\n DocumentManager.layer.pipe(\n Layer.provide(MimicConfig.layer(options)),\n // Provide defaults\n Layer.provide(InMemoryDataStorage.layerDefault),\n Layer.provide(NoAuth.layerDefault)\n );\n\n/**\n * Create the HTTP handler effect for WebSocket upgrade.\n * This handler:\n * 1. Extracts the document ID from the URL path\n * 2. Upgrades the HTTP connection to WebSocket\n * 3. Delegates to the WebSocketHandler for document sync\n */\nconst makeMimicHandler = Effect.gen(function* () {\n const config = yield* MimicConfig.MimicServerConfigTag;\n const authService = yield* MimicAuthServiceTag;\n const documentManager = yield* DocumentManager.DocumentManagerTag;\n const presenceManager = yield* PresenceManager.PresenceManagerTag;\n\n return Effect.gen(function* () {\n const request = yield* HttpServerRequest.HttpServerRequest;\n\n // Extract document ID from the URL path\n // Expected format: /basePath/doc/{documentId}\n const documentId = yield* WebSocketHandler.extractDocumentId(request.url);\n\n // Upgrade to WebSocket\n const socket = yield* request.upgrade;\n\n // Handle the WebSocket connection\n yield* WebSocketHandler.handleConnection(socket, request.url).pipe(\n Effect.provideService(MimicConfig.MimicServerConfigTag, config),\n Effect.provideService(MimicAuthServiceTag, authService),\n Effect.provideService(DocumentManager.DocumentManagerTag, documentManager),\n Effect.provideService(PresenceManager.PresenceManagerTag, presenceManager),\n Effect.scoped,\n Effect.catchAll((error) =>\n Effect.logError(\"WebSocket connection error\", error)\n )\n );\n\n // Return empty response - the WebSocket upgrade handles the connection\n return HttpServerResponse.empty();\n }).pipe(\n Effect.catchAll((error) =>\n Effect.gen(function* () {\n yield* Effect.logWarning(\"WebSocket upgrade failed\", error);\n return HttpServerResponse.text(\"WebSocket upgrade failed\", {\n status: 400,\n });\n })\n )\n );\n});\n\n\n\n/**\n * Options for layerHttpLayerRouter including optional custom layers.\n */\nexport interface MimicLayerRouterOptions<TSchema extends Primitive.AnyPrimitive>\n extends MimicLayerOptions<TSchema> {\n /** Custom auth layer. Defaults to NoAuth (all connections allowed). */\n readonly authLayer?: Layer.Layer<MimicAuthServiceTag>;\n /** Custom storage layer. Defaults to InMemoryDataStorage. */\n readonly storageLayer?: Layer.Layer<MimicDataStorageTag>;\n}\n\n/**\n * Create a Mimic server layer that integrates with HttpLayerRouter.\n *\n * This function creates a layer that:\n * 1. Registers a WebSocket route at the specified base path\n * 2. Handles WebSocket upgrades for document sync\n * 3. Provides all required dependencies (config, auth, storage, document manager)\n *\n * By default, uses in-memory storage and no authentication.\n * To override these defaults, provide custom layers before the defaults:\n *\n * @example\n * ```typescript\n * import { MimicServer, MimicAuthService } from \"@voidhash/mimic-effect\";\n * import { HttpLayerRouter } from \"@effect/platform\";\n * import { Primitive } from \"@voidhash/mimic\";\n *\n * const TodoSchema = Primitive.Struct({\n * title: Primitive.String(),\n * completed: Primitive.Boolean(),\n * });\n *\n * // Create the Mimic route layer with defaults\n * const MimicRoute = MimicServer.layerHttpLayerRouter({\n * basePath: \"/mimic/todo\",\n * schema: TodoSchema\n * });\n *\n * // Or with custom auth - use Layer.provide to inject before defaults\n * const MimicRouteWithAuth = MimicServer.layerHttpLayerRouter({\n * basePath: \"/mimic/todo\",\n * schema: TodoSchema,\n * authLayer: MimicAuthService.layer({\n * authHandler: (token) => ({ success: true, userId: token })\n * })\n * });\n *\n * // Merge with other routes and serve\n * const AllRoutes = Layer.mergeAll(MimicRoute, OtherRoutes);\n * HttpLayerRouter.serve(AllRoutes).pipe(\n * Layer.provide(BunHttpServer.layer({ port: 3000 })),\n * Layer.launch,\n * BunRuntime.runMain\n * );\n * ```\n */\nexport const layerHttpLayerRouter = <\n TSchema extends Primitive.AnyPrimitive,\n TError,\n TRequirements\n>(\n optionsEf: Effect.Effect<MimicLayerRouterOptions<TSchema>, TError, TRequirements>\n): Layer.Layer<never, TError, TRequirements | HttpLayerRouter.HttpRouter> => {\n return Layer.unwrapScoped(\n Effect.gen(function* () {\n const options = yield* optionsEf;\n\n // Build the base path pattern for WebSocket routes\n // Append /doc/* to match /basePath/doc/{documentId}\n const basePath = options.basePath ?? \"/mimic\";\n const wsPath: PathInput = `${basePath}/doc/*` as PathInput;\n\n // Create the config layer with properly typed initial function\n const configLayer = MimicConfig.layer<TSchema>({\n schema: options.schema,\n maxTransactionHistory: options.maxTransactionHistory,\n presence: options.presence,\n initial: options.initial,\n });\n\n // Use provided layers or defaults\n const authLayer = options.authLayer ?? NoAuth.layerDefault;\n const storageLayer = options.storageLayer ?? InMemoryDataStorage.layerDefault;\n\n // Combine all dependency layers\n const depsLayer = Layer.mergeAll(configLayer, authLayer, storageLayer);\n\n // Create the route registration layer\n const routeLayer = Layer.scopedDiscard(\n Effect.gen(function* () {\n const router = yield* HttpLayerRouter.HttpRouter;\n const handler = yield* makeMimicHandler;\n yield* router.add(\"GET\", wsPath, handler);\n })\n );\n\n // Build the complete layer with all dependencies provided\n return routeLayer.pipe(\n Layer.provide(DocumentManager.layer),\n Layer.provide(PresenceManager.layer),\n Layer.provide(depsLayer),\n );\n })\n );\n};"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwEA,MAAa,wBACX,oBAEsB,KACpB,MAAM,QAAQA,MAAkB,QAAQ,CAAC,EAEzC,MAAM,QAAQC,aAAiC,EAC/C,MAAM,QAAQC,eAAoB,CACnC;;;;;;;;AASH,MAAM,mBAAmB,OAAO,IAAI,aAAa;CAC/C,MAAM,SAAS,OAAOC;CACtB,MAAM,cAAc,OAAO;CAC3B,MAAM,kBAAkB,OAAOC;CAC/B,MAAM,kBAAkB,OAAOC;AAE/B,QAAO,OAAO,IAAI,aAAa;EAC7B,MAAM,UAAU,OAAO,kBAAkB;AAItB,SAAOC,kBAAmC,QAAQ,IAAI;EAGzE,MAAM,SAAS,OAAO,QAAQ;AAG9B,SAAOC,iBAAkC,QAAQ,QAAQ,IAAI,CAAC,KAC5D,OAAO,eAAeJ,sBAAkC,OAAO,EAC/D,OAAO,eAAe,qBAAqB,YAAY,EACvD,OAAO,eAAeC,oBAAoC,gBAAgB,EAC1E,OAAO,eAAeC,oBAAoC,gBAAgB,EAC1E,OAAO,QACP,OAAO,UAAU,UACf,OAAO,SAAS,8BAA8B,MAAM,CACrD,CACF;AAGD,SAAO,mBAAmB,OAAO;GACjC,CAAC,KACD,OAAO,UAAU,UACf,OAAO,IAAI,aAAa;AACtB,SAAO,OAAO,WAAW,4BAA4B,MAAM;AAC3D,SAAO,mBAAmB,KAAK,4BAA4B,EACzD,QAAQ,KACT,CAAC;GACF,CACH,CACF;EACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DF,MAAa,wBAKX,cAC2E;AAC3E,QAAO,MAAM,aACX,OAAO,IAAI,aAAa;;EACtB,MAAM,UAAU,OAAO;EAKvB,MAAMG,SAAoB,wBADT,QAAQ,yEAAY,SACC;EAGtC,MAAM,cAAcR,MAA2B;GAC7C,QAAQ,QAAQ;GAChB,uBAAuB,QAAQ;GAC/B,UAAU,QAAQ;GAClB,SAAS,QAAQ;GAClB,CAAC;EAGF,MAAM,kCAAY,QAAQ,4EAAaE;EACvC,MAAM,wCAAe,QAAQ,qFAAgBD;EAG7C,MAAM,YAAY,MAAM,SAAS,aAAa,WAAW,aAAa;AAYtE,SATmB,MAAM,cACvB,OAAO,IAAI,aAAa;GACtB,MAAM,SAAS,OAAO,gBAAgB;GACtC,MAAM,UAAU,OAAO;AACvB,UAAO,OAAO,IAAI,OAAO,QAAQ,QAAQ;IACzC,CACH,CAGiB,KAChB,MAAM,QAAQQ,QAAsB,EACpC,MAAM,QAAQC,QAAsB,EACpC,MAAM,QAAQ,UAAU,CACzB;GACD,CACH"}
1
+ {"version":3,"file":"MimicServer.mjs","names":["Metrics.connectionsTotal","Metrics.connectionsActive","state: ConnectionState","Protocol.encodeServerMessage","Protocol.presenceSnapshotMessage","Protocol.snapshotMessage","Protocol.authResultSuccess","Metrics.connectionsErrors","Protocol.authResultFailure","Protocol.pong","Protocol.errorMessage","Protocol.presenceUpdateMessage","Protocol.presenceRemoveMessage","Metrics.connectionsDuration","Protocol.parseClientMessage"],"sources":["../src/MimicServer.ts"],"sourcesContent":["/**\n * @voidhash/mimic-effect - MimicServer\n *\n * WebSocket route layer for MimicServerEngine.\n * Creates routes compatible with HttpLayerRouter.\n */\nimport {\n Duration,\n Effect,\n Fiber,\n Layer,\n Metric,\n Stream,\n} from \"effect\";\nimport {\n HttpLayerRouter,\n HttpServerRequest,\n HttpServerResponse,\n} from \"@effect/platform\";\nimport type * as Socket from \"@effect/platform/Socket\";\nimport { Presence } from \"@voidhash/mimic\";\nimport type { MimicServerRouteConfig, ResolvedRouteConfig } from \"./Types\";\nimport * as Protocol from \"./Protocol\";\nimport { MissingDocumentIdError } from \"./Errors\";\nimport { MimicServerEngineTag, type MimicServerEngine } from \"./MimicServerEngine\";\nimport { MimicAuthServiceTag, type MimicAuthService } from \"./MimicAuthService\";\nimport * as Metrics from \"./Metrics\";\nimport type { AuthContext } from \"./Types\";\n\n// =============================================================================\n// Default Configuration\n// =============================================================================\n\nconst DEFAULT_PATH = \"/mimic\";\nconst DEFAULT_HEARTBEAT_INTERVAL = Duration.seconds(30);\nconst DEFAULT_HEARTBEAT_TIMEOUT = Duration.seconds(10);\n\n/**\n * Resolve route configuration with defaults\n */\nconst resolveRouteConfig = (\n config?: MimicServerRouteConfig\n): ResolvedRouteConfig => ({\n path: config?.path ?? DEFAULT_PATH,\n heartbeatInterval: config?.heartbeatInterval\n ? Duration.decode(config.heartbeatInterval)\n : DEFAULT_HEARTBEAT_INTERVAL,\n heartbeatTimeout: config?.heartbeatTimeout\n ? Duration.decode(config.heartbeatTimeout)\n : DEFAULT_HEARTBEAT_TIMEOUT,\n});\n\n// =============================================================================\n// URL Path Parsing\n// =============================================================================\n\n/**\n * Extract document ID from URL path.\n * Expected format: /basePath/doc/{documentId}\n */\nconst extractDocumentId = (\n path: string\n): Effect.Effect<string, MissingDocumentIdError> => {\n // Remove leading slash and split\n const parts = path.replace(/^\\/+/, \"\").split(\"/\");\n\n // Find the last occurrence of 'doc' in the path\n const docIndex = parts.lastIndexOf(\"doc\");\n const part = parts[docIndex + 1];\n if (docIndex !== -1 && part) {\n return Effect.succeed(decodeURIComponent(part));\n }\n return Effect.fail(new MissingDocumentIdError({ path }));\n};\n\n// =============================================================================\n// Connection State\n// =============================================================================\n\ninterface ConnectionState {\n readonly documentId: string;\n readonly connectionId: string;\n authenticated: boolean;\n authContext?: AuthContext;\n hasPresence: boolean;\n}\n\n// =============================================================================\n// WebSocket Connection Handler\n// =============================================================================\n\n/**\n * Handle a WebSocket connection for a document.\n */\nconst handleWebSocketConnection = Effect.fn(\"websocket.connection.handle\")(\n function* (\n socket: Socket.Socket,\n documentId: string,\n engine: MimicServerEngine,\n authService: MimicAuthService,\n _routeConfig: ResolvedRouteConfig\n ) {\n const connectionId = crypto.randomUUID();\n const connectionStartTime = Date.now();\n\n // Track connection metrics\n yield* Metric.increment(Metrics.connectionsTotal);\n yield* Metric.incrementBy(Metrics.connectionsActive, 1);\n\n // Track connection state (mutable for simplicity)\n const state: ConnectionState = {\n documentId,\n connectionId,\n authenticated: false,\n hasPresence: false,\n };\n\n // Get the socket writer\n const write = yield* socket.writer;\n\n // Helper to send a message to the client\n const sendMessage = (message: Protocol.ServerMessage) =>\n write(Protocol.encodeServerMessage(message));\n\n // Send presence snapshot after auth\n const sendPresenceSnapshot = Effect.fn(\"presence.snapshot.send\")(\n function* () {\n if (!engine.config.presence) return;\n\n const snapshot = yield* engine.getPresenceSnapshot(documentId);\n yield* sendMessage(\n Protocol.presenceSnapshotMessage(connectionId, snapshot.presences)\n );\n }\n );\n\n // Send document snapshot after auth\n const sendDocumentSnapshot = Effect.fn(\"document.snapshot.send\")(\n function* () {\n const snapshot = yield* engine.getSnapshot(documentId);\n yield* sendMessage(\n Protocol.snapshotMessage(snapshot.state, snapshot.version)\n );\n }\n );\n\n // Handle authentication\n const handleAuth = Effect.fn(\"auth.handle\")(function* (token: string) {\n const result = yield* Effect.either(\n authService.authenticate(token, documentId)\n );\n\n if (result._tag === \"Right\") {\n state.authenticated = true;\n state.authContext = result.right;\n\n yield* sendMessage(\n Protocol.authResultSuccess(\n result.right.userId,\n result.right.permission\n )\n );\n\n // Send document snapshot after successful auth\n yield* sendDocumentSnapshot();\n\n // Send presence snapshot after successful auth\n yield* sendPresenceSnapshot();\n } else {\n yield* Metric.increment(Metrics.connectionsErrors);\n yield* sendMessage(\n Protocol.authResultFailure(\n result.left.reason ?? \"Authentication failed\"\n )\n );\n }\n });\n\n // Handle presence set\n const handlePresenceSet = Effect.fn(\"presence.set.handle\")(\n function* (data: unknown) {\n if (!state.authenticated) return;\n if (!state.authContext) return;\n if (!engine.config.presence) return;\n\n // Check write permission\n if (state.authContext.permission !== \"write\") {\n yield* Effect.logWarning(\"Presence set rejected - read-only user\", {\n connectionId,\n });\n return;\n }\n\n // Validate presence data against schema\n const validated = Presence.validateSafe(engine.config.presence, data);\n if (validated === undefined) {\n yield* Effect.logWarning(\"Invalid presence data received\", {\n connectionId,\n data,\n });\n return;\n }\n\n // Store in engine\n yield* engine.setPresence(documentId, connectionId, {\n data: validated,\n userId: state.authContext.userId,\n });\n\n state.hasPresence = true;\n }\n );\n\n // Handle presence clear\n const handlePresenceClear = Effect.fn(\"presence.clear.handle\")(\n function* () {\n if (!state.authenticated) return;\n if (!engine.config.presence) return;\n\n yield* engine.removePresence(documentId, connectionId);\n state.hasPresence = false;\n }\n );\n\n // Handle a client message\n const handleMessage = Effect.fn(\"message.handle\")(\n function* (message: Protocol.ClientMessage) {\n // Touch document on any activity (prevents idle GC)\n yield* engine.touch(documentId);\n\n switch (message.type) {\n case \"auth\":\n yield* handleAuth(message.token);\n break;\n\n case \"ping\":\n yield* sendMessage(Protocol.pong());\n break;\n\n case \"submit\":\n if (!state.authenticated) {\n yield* sendMessage(\n Protocol.errorMessage(\n message.transaction.id,\n \"Not authenticated\"\n )\n );\n return;\n }\n\n // Check write permission\n if (state.authContext?.permission !== \"write\") {\n yield* sendMessage(\n Protocol.errorMessage(\n message.transaction.id,\n \"Write permission required\"\n )\n );\n return;\n }\n\n // Submit to the engine\n const submitResult = yield* engine.submit(\n documentId,\n message.transaction\n );\n\n // If rejected, send error (success is broadcast to all)\n if (!submitResult.success) {\n yield* sendMessage(\n Protocol.errorMessage(message.transaction.id, submitResult.reason)\n );\n }\n break;\n\n case \"request_snapshot\":\n if (!state.authenticated) {\n return;\n }\n const snapshot = yield* engine.getSnapshot(documentId);\n yield* sendMessage(\n Protocol.snapshotMessage(snapshot.state, snapshot.version)\n );\n break;\n\n case \"presence_set\":\n yield* handlePresenceSet(message.data);\n break;\n\n case \"presence_clear\":\n yield* handlePresenceClear();\n break;\n }\n }\n );\n\n // Subscribe to document broadcasts\n const subscribeFiber = yield* Effect.fork(\n Effect.fn(\"subscriptions.document.start\")(function* () {\n // Wait until authenticated before subscribing\n while (!state.authenticated) {\n yield* Effect.sleep(Duration.millis(100));\n }\n\n // Subscribe to the document\n const broadcastStream = yield* engine.subscribe(documentId);\n\n // Forward broadcasts to the WebSocket\n yield* Stream.runForEach(broadcastStream, (broadcast) =>\n sendMessage(broadcast as Protocol.ServerMessage)\n );\n })().pipe(Effect.scoped)\n );\n\n // Subscribe to presence events (if presence is enabled)\n const presenceFiber = yield* Effect.fork(\n Effect.fn(\"subscriptions.presence.start\")(function* () {\n if (!engine.config.presence) return;\n\n // Wait until authenticated before subscribing\n while (!state.authenticated) {\n yield* Effect.sleep(Duration.millis(100));\n }\n\n // Subscribe to presence events\n const presenceStream = yield* engine.subscribePresence(documentId);\n\n // Forward presence events to the WebSocket, filtering out our own events (no-echo)\n yield* Stream.runForEach(presenceStream, (event) =>\n Effect.gen(function* () {\n // Don't echo our own presence events\n if (event.id === connectionId) return;\n\n if (event.type === \"presence_update\") {\n yield* sendMessage(\n Protocol.presenceUpdateMessage(event.id, event.data, event.userId)\n );\n } else if (event.type === \"presence_remove\") {\n yield* sendMessage(Protocol.presenceRemoveMessage(event.id));\n }\n })\n );\n })().pipe(Effect.scoped)\n );\n\n // Ensure cleanup on disconnect\n yield* Effect.addFinalizer(() =>\n Effect.fn(\"connection.cleanup\")(function* () {\n // Calculate connection duration\n const duration = Date.now() - connectionStartTime;\n\n // Interrupt the subscribe fibers\n yield* Fiber.interrupt(subscribeFiber);\n yield* Fiber.interrupt(presenceFiber);\n\n // Remove presence if we had any\n if (state.hasPresence && engine.config.presence) {\n yield* engine.removePresence(documentId, connectionId);\n }\n\n // Update connection metrics\n yield* Metric.incrementBy(Metrics.connectionsActive, -1);\n yield* Metric.update(Metrics.connectionsDuration, duration);\n\n yield* Effect.logDebug(\"WebSocket connection closed\", {\n connectionId,\n documentId,\n durationMs: duration,\n });\n })()\n );\n\n // Process incoming messages\n yield* socket.runRaw((data) =>\n Effect.fn(\"message.process\")(function* () {\n const message = yield* Protocol.parseClientMessage(data);\n yield* handleMessage(message);\n })().pipe(\n Effect.catchAll((error) =>\n Effect.logError(\"Message handling error\", error)\n )\n )\n );\n }\n);\n\n// =============================================================================\n// Factory\n// =============================================================================\n\n/**\n * Create a route layer for MimicServerEngine.\n *\n * This creates a WebSocket route that connects to the engine.\n * Use Layer.mergeAll to compose with other routes.\n *\n * @example\n * ```typescript\n * // 1. Create the engine\n * const Engine = MimicServerEngine.make({\n * schema: DocSchema,\n * initial: { title: \"Untitled\" },\n * })\n *\n * // 2. Create the WebSocket route\n * const MimicRoute = MimicServer.layerHttpLayerRouter({\n * path: \"/mimic\",\n * })\n *\n * // 3. Wire together\n * const MimicLive = MimicRoute.pipe(\n * Layer.provide(Engine),\n * Layer.provide(ColdStorage.InMemory.make()),\n * Layer.provide(HotStorage.InMemory.make()),\n * Layer.provide(MimicAuthService.NoAuth.make()),\n * )\n *\n * // 4. Compose with other routes\n * const AllRoutes = Layer.mergeAll(MimicLive, DocsRoute, OtherRoutes)\n *\n * // 5. Serve\n * HttpLayerRouter.serve(AllRoutes).pipe(\n * Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),\n * Layer.launch,\n * NodeRuntime.runMain\n * )\n * ```\n */\nexport const layerHttpLayerRouter = (\n options?: MimicServerRouteConfig\n) => {\n const routeConfig = resolveRouteConfig(options);\n\n // Build the route path pattern: {path}/doc/:documentId\n const routePath =\n `${routeConfig.path}/doc/:documentId` as HttpLayerRouter.PathInput;\n\n return Layer.scopedDiscard(\n Effect.gen(function* () {\n const router = yield* HttpLayerRouter.HttpRouter;\n // Capture engine and auth service at layer creation time\n const engine = yield* MimicServerEngineTag;\n const authService = yield* MimicAuthServiceTag;\n\n // Create the handler that receives the request\n // Engine and authService are captured in closure, not yielded per-request\n const handler = Effect.fn(\"websocket.route.handler\")(\n function* (request: HttpServerRequest.HttpServerRequest) {\n // Extract document ID from path\n const documentIdResult = yield* Effect.either(\n extractDocumentId(request.url)\n );\n if (documentIdResult._tag === \"Left\") {\n return HttpServerResponse.text(\n `Missing document ID in path: ${request.url}`,\n { status: 400 }\n );\n }\n const documentId = documentIdResult.right;\n\n // Upgrade to WebSocket\n const socket = yield* request.upgrade;\n\n // Handle the WebSocket connection\n yield* handleWebSocketConnection(\n socket,\n documentId,\n engine,\n authService,\n routeConfig\n ).pipe(\n Effect.scoped,\n Effect.catchAll((error) =>\n Effect.logError(\"WebSocket connection error\", error)\n )\n );\n\n // Return empty response - the WebSocket upgrade handles the connection\n return HttpServerResponse.empty();\n }\n );\n\n yield* router.add(\"GET\", routePath, handler);\n })\n );\n};\n\n// =============================================================================\n// Re-export namespace\n// =============================================================================\n\nexport const MimicServer = {\n layerHttpLayerRouter,\n};\n\n// =============================================================================\n// Re-export types\n// =============================================================================\n\nexport type { MimicServerRouteConfig };\n"],"mappings":"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"}
@@ -0,0 +1,178 @@
1
+ const require_ColdStorage = require('./ColdStorage.cjs');
2
+ const require_HotStorage = require('./HotStorage.cjs');
3
+ const require_Metrics = require('./Metrics.cjs');
4
+ const require_DocumentInstance = require('./DocumentInstance.cjs');
5
+ const require_PresenceManager = require('./PresenceManager.cjs');
6
+ let effect = require("effect");
7
+
8
+ //#region src/MimicServerEngine.ts
9
+ /**
10
+ * @voidhash/mimic-effect - MimicServerEngine
11
+ *
12
+ * Core document management service for Mimic real-time collaboration.
13
+ * Handles document lifecycle, storage, presence, and transaction processing.
14
+ *
15
+ * This is the engine layer - for WebSocket routes, use MimicServer.layerHttpLayerRouter().
16
+ */
17
+ /**
18
+ * Context tag for MimicServerEngine
19
+ */
20
+ var MimicServerEngineTag = class extends effect.Context.Tag("@voidhash/mimic-effect/MimicServerEngine")() {};
21
+ const DEFAULT_MAX_IDLE_TIME = effect.Duration.minutes(5);
22
+ const DEFAULT_MAX_TRANSACTION_HISTORY = 1e3;
23
+ const DEFAULT_SNAPSHOT_INTERVAL = effect.Duration.minutes(5);
24
+ const DEFAULT_SNAPSHOT_THRESHOLD = 100;
25
+ const DEFAULT_SNAPSHOT_IDLE_TIMEOUT = effect.Duration.seconds(30);
26
+ /**
27
+ * Resolve configuration with defaults
28
+ */
29
+ const resolveConfig = (config) => {
30
+ var _config$maxTransactio, _config$snapshot, _config$snapshot$tran, _config$snapshot2, _config$snapshot3;
31
+ return {
32
+ schema: config.schema,
33
+ initial: config.initial,
34
+ presence: config.presence,
35
+ maxIdleTime: config.maxIdleTime ? effect.Duration.decode(config.maxIdleTime) : DEFAULT_MAX_IDLE_TIME,
36
+ maxTransactionHistory: (_config$maxTransactio = config.maxTransactionHistory) !== null && _config$maxTransactio !== void 0 ? _config$maxTransactio : DEFAULT_MAX_TRANSACTION_HISTORY,
37
+ snapshot: {
38
+ interval: ((_config$snapshot = config.snapshot) === null || _config$snapshot === void 0 ? void 0 : _config$snapshot.interval) ? effect.Duration.decode(config.snapshot.interval) : DEFAULT_SNAPSHOT_INTERVAL,
39
+ transactionThreshold: (_config$snapshot$tran = (_config$snapshot2 = config.snapshot) === null || _config$snapshot2 === void 0 ? void 0 : _config$snapshot2.transactionThreshold) !== null && _config$snapshot$tran !== void 0 ? _config$snapshot$tran : DEFAULT_SNAPSHOT_THRESHOLD,
40
+ idleTimeout: ((_config$snapshot3 = config.snapshot) === null || _config$snapshot3 === void 0 ? void 0 : _config$snapshot3.idleTimeout) ? effect.Duration.decode(config.snapshot.idleTimeout) : DEFAULT_SNAPSHOT_IDLE_TIMEOUT
41
+ }
42
+ };
43
+ };
44
+ /**
45
+ * Create a MimicServerEngine layer.
46
+ *
47
+ * This creates the core document management service. To expose it via WebSocket,
48
+ * use MimicServer.layerHttpLayerRouter().
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * // 1. Create the engine
53
+ * const Engine = MimicServerEngine.make({
54
+ * schema: DocSchema,
55
+ * initial: { title: "Untitled" },
56
+ * presence: CursorPresence,
57
+ * maxIdleTime: "5 minutes",
58
+ * snapshot: { interval: "5 minutes", transactionThreshold: 100 },
59
+ * })
60
+ *
61
+ * // 2. Create the WebSocket route
62
+ * const MimicRoute = MimicServer.layerHttpLayerRouter({
63
+ * path: "/mimic",
64
+ * })
65
+ *
66
+ * // 3. Wire together
67
+ * const MimicLive = MimicRoute.pipe(
68
+ * Layer.provide(Engine),
69
+ * Layer.provide(ColdStorage.InMemory.make()),
70
+ * Layer.provide(HotStorage.InMemory.make()),
71
+ * Layer.provide(MimicAuthService.NoAuth.make()),
72
+ * )
73
+ * ```
74
+ */
75
+ const make = (config) => {
76
+ const resolvedConfig = resolveConfig(config);
77
+ return effect.Layer.scoped(MimicServerEngineTag, effect.Effect.gen(function* () {
78
+ const coldStorage = yield* require_ColdStorage.ColdStorageTag;
79
+ const hotStorage = yield* require_HotStorage.HotStorageTag;
80
+ const presenceManager = yield* require_PresenceManager.PresenceManagerTag;
81
+ const store = yield* effect.Ref.make(effect.HashMap.empty());
82
+ /**
83
+ * Get or create a document instance
84
+ */
85
+ const getOrCreateDocument = effect.Effect.fn("engine.document.get-or-create")(function* (documentId) {
86
+ const current = yield* effect.Ref.get(store);
87
+ const existing = effect.HashMap.get(current, documentId);
88
+ if (existing._tag === "Some") {
89
+ yield* effect.Ref.set(existing.value.lastActivityTime, Date.now());
90
+ return existing.value.instance;
91
+ }
92
+ const instance = yield* require_DocumentInstance.DocumentInstance.make(documentId, {
93
+ schema: config.schema,
94
+ initial: config.initial,
95
+ maxTransactionHistory: resolvedConfig.maxTransactionHistory,
96
+ snapshot: resolvedConfig.snapshot
97
+ }, coldStorage, hotStorage);
98
+ const lastActivityTime = yield* effect.Ref.make(Date.now());
99
+ yield* effect.Ref.update(store, (map) => effect.HashMap.set(map, documentId, {
100
+ instance,
101
+ lastActivityTime
102
+ }));
103
+ return instance;
104
+ });
105
+ yield* effect.Effect.fn("engine.gc.start")(function* () {
106
+ yield* effect.Effect.fn("engine.gc.loop")(function* () {
107
+ const current = yield* effect.Ref.get(store);
108
+ const now = Date.now();
109
+ const maxIdleMs = effect.Duration.toMillis(resolvedConfig.maxIdleTime);
110
+ for (const [documentId, entry] of current) if (now - (yield* effect.Ref.get(entry.lastActivityTime)) >= maxIdleMs) {
111
+ yield* effect.Effect.catchAll(entry.instance.saveSnapshot(), (e) => effect.Effect.logError("Failed to save snapshot during eviction", {
112
+ documentId,
113
+ error: e
114
+ }));
115
+ yield* effect.Ref.update(store, (map) => effect.HashMap.remove(map, documentId));
116
+ yield* effect.Metric.increment(require_Metrics.documentsEvicted);
117
+ yield* effect.Metric.incrementBy(require_Metrics.documentsActive, -1);
118
+ yield* effect.Effect.logInfo("Document evicted due to idle timeout", { documentId });
119
+ }
120
+ })().pipe(effect.Effect.repeat(effect.Schedule.spaced("1 minute")), effect.Effect.fork);
121
+ })();
122
+ yield* effect.Effect.fn("engine.snapshot.fiber.start")(function* () {
123
+ const idleTimeoutMs = effect.Duration.toMillis(resolvedConfig.snapshot.idleTimeout);
124
+ if (idleTimeoutMs <= 0) return;
125
+ yield* effect.Effect.fn("engine.snapshot.loop")(function* () {
126
+ const current = yield* effect.Ref.get(store);
127
+ const now = Date.now();
128
+ for (const [documentId, entry] of current) {
129
+ if (now - (yield* effect.Ref.get(entry.lastActivityTime)) < idleTimeoutMs) continue;
130
+ if (!(yield* entry.instance.needsSnapshot())) continue;
131
+ yield* effect.Effect.catchAll(entry.instance.saveSnapshot(), (e) => effect.Effect.logWarning("Periodic snapshot save failed", {
132
+ documentId,
133
+ error: e
134
+ }));
135
+ yield* effect.Metric.increment(require_Metrics.storageIdleSnapshots);
136
+ }
137
+ })().pipe(effect.Effect.repeat(effect.Schedule.spaced("10 seconds")), effect.Effect.fork);
138
+ })();
139
+ yield* effect.Effect.addFinalizer(() => effect.Effect.fn("engine.shutdown")(function* () {
140
+ const current = yield* effect.Ref.get(store);
141
+ for (const [documentId, entry] of current) yield* effect.Effect.catchAll(entry.instance.saveSnapshot(), (e) => effect.Effect.logError("Failed to save snapshot during shutdown", {
142
+ documentId,
143
+ error: e
144
+ }));
145
+ yield* effect.Effect.logInfo("MimicServerEngine shutdown complete");
146
+ })());
147
+ return {
148
+ submit: (documentId, transaction) => effect.Effect.gen(function* () {
149
+ return yield* (yield* getOrCreateDocument(documentId)).submit(transaction);
150
+ }),
151
+ getSnapshot: (documentId) => effect.Effect.gen(function* () {
152
+ return (yield* getOrCreateDocument(documentId)).getSnapshot();
153
+ }),
154
+ subscribe: (documentId) => effect.Effect.gen(function* () {
155
+ const instance = yield* getOrCreateDocument(documentId);
156
+ return effect.Stream.fromPubSub(instance.pubsub);
157
+ }),
158
+ touch: (documentId) => effect.Effect.gen(function* () {
159
+ const current = yield* effect.Ref.get(store);
160
+ const existing = effect.HashMap.get(current, documentId);
161
+ if (existing._tag === "Some") yield* effect.Ref.set(existing.value.lastActivityTime, Date.now());
162
+ }),
163
+ getPresenceSnapshot: (documentId) => presenceManager.getSnapshot(documentId),
164
+ setPresence: (documentId, connectionId, entry) => presenceManager.set(documentId, connectionId, entry),
165
+ removePresence: (documentId, connectionId) => presenceManager.remove(documentId, connectionId),
166
+ subscribePresence: (documentId) => presenceManager.subscribe(documentId),
167
+ config: resolvedConfig
168
+ };
169
+ })).pipe(effect.Layer.provide(require_PresenceManager.layer));
170
+ };
171
+ const MimicServerEngine = {
172
+ Tag: MimicServerEngineTag,
173
+ make
174
+ };
175
+
176
+ //#endregion
177
+ exports.MimicServerEngine = MimicServerEngine;
178
+ exports.MimicServerEngineTag = MimicServerEngineTag;