@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,44 +1,39 @@
1
1
  /**
2
- * @since 0.0.1
3
- * Authentication service interface for Mimic connections.
4
- * Provides pluggable authentication adapters.
2
+ * @voidhash/mimic-effect - MimicAuthService
3
+ *
4
+ * Authentication and authorization service interface and implementations.
5
5
  */
6
- import * as Effect from "effect/Effect";
7
- import * as Context from "effect/Context";
8
- import * as Layer from "effect/Layer";
6
+ import { Context, Effect, Layer } from "effect";
7
+ import type { AuthContext, Permission } from "./Types";
8
+ import { AuthenticationError } from "./Errors";
9
9
 
10
10
  // =============================================================================
11
- // Authentication Types
11
+ // MimicAuthService Interface
12
12
  // =============================================================================
13
13
 
14
14
  /**
15
- * Result of an authentication attempt.
16
- */
17
- export type AuthResult =
18
- | { readonly success: true; readonly userId?: string }
19
- | { readonly success: false; readonly error: string };
20
-
21
- /**
22
- * Authentication handler function type.
23
- * Can be synchronous or return a Promise.
24
- */
25
- export type AuthHandler = (token: string) => Promise<AuthResult> | AuthResult;
26
-
27
- // =============================================================================
28
- // Auth Service Interface
29
- // =============================================================================
30
-
31
- /**
32
- * Authentication service interface.
33
- * Implementations can authenticate connections using various methods (JWT, API keys, etc.)
15
+ * MimicAuthService interface for authentication and authorization.
16
+ *
17
+ * The `authenticate` method receives the token from the client's auth message
18
+ * and the document ID being accessed. It should return an AuthContext on success
19
+ * or fail with AuthenticationError on failure.
20
+ *
21
+ * The permission in AuthContext determines what the user can do:
22
+ * - "read": Can subscribe, receive transactions, get snapshots
23
+ * - "write": All of the above, plus can submit transactions and set presence
34
24
  */
35
25
  export interface MimicAuthService {
36
26
  /**
37
- * Authenticate a connection using the provided token.
38
- * @param token - The authentication token provided by the client
39
- * @returns The authentication result
27
+ * Authenticate a connection and return authorization context.
28
+ *
29
+ * @param token - The token provided by the client
30
+ * @param documentId - The document ID being accessed
31
+ * @returns AuthContext with userId and permission level
40
32
  */
41
- readonly authenticate: (token: string) => Effect.Effect<AuthResult>;
33
+ readonly authenticate: (
34
+ token: string,
35
+ documentId: string
36
+ ) => Effect.Effect<AuthContext, AuthenticationError>;
42
37
  }
43
38
 
44
39
  // =============================================================================
@@ -46,58 +41,125 @@ export interface MimicAuthService {
46
41
  // =============================================================================
47
42
 
48
43
  /**
49
- * Context tag for MimicAuthService service.
44
+ * Context tag for MimicAuthService
50
45
  */
51
46
  export class MimicAuthServiceTag extends Context.Tag(
52
- "@voidhash/mimic-server-effect/MimicAuthService"
47
+ "@voidhash/mimic-effect/MimicAuthService"
53
48
  )<MimicAuthServiceTag, MimicAuthService>() {}
54
49
 
55
50
  // =============================================================================
56
- // Layer Constructors
51
+ // Factory
57
52
  // =============================================================================
58
53
 
59
54
  /**
60
- * Create a MimicAuthService layer from an auth handler function.
61
- */
62
- export const layer = (options: {
63
- readonly authHandler: AuthHandler;
64
- }): Layer.Layer<MimicAuthServiceTag> =>
65
- Layer.succeed(MimicAuthServiceTag, {
66
- authenticate: (token: string) =>
67
- Effect.promise(() => Promise.resolve(options.authHandler(token))),
68
- });
69
-
70
- /**
71
- * Create a MimicAuthService layer from an auth service implementation.
72
- */
73
- export const layerService = (service: MimicAuthService): Layer.Layer<MimicAuthServiceTag> =>
74
- Layer.succeed(MimicAuthServiceTag, service);
75
-
76
- /**
77
- * Create a MimicAuthService layer from an Effect that produces an auth service.
55
+ * Create a MimicAuthService layer from an Effect that produces the service.
56
+ *
57
+ * This allows you to access other Effect services when implementing authentication.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const Auth = MimicAuthService.make(
62
+ * Effect.gen(function*() {
63
+ * const db = yield* DatabaseService
64
+ * const jwt = yield* JwtService
65
+ *
66
+ * return {
67
+ * authenticate: (token, documentId) =>
68
+ * Effect.gen(function*() {
69
+ * const payload = yield* jwt.verify(token).pipe(
70
+ * Effect.mapError(() => new AuthenticationError({ reason: "Invalid token" }))
71
+ * )
72
+ *
73
+ * const permission = yield* db.getDocumentPermission(payload.userId, documentId)
74
+ *
75
+ * return { userId: payload.userId, permission }
76
+ * })
77
+ * }
78
+ * })
79
+ * )
80
+ * ```
78
81
  */
79
- export const layerEffect = <E, R>(
82
+ export const make = <E, R>(
80
83
  effect: Effect.Effect<MimicAuthService, E, R>
81
84
  ): Layer.Layer<MimicAuthServiceTag, E, R> =>
82
85
  Layer.effect(MimicAuthServiceTag, effect);
83
86
 
84
87
  // =============================================================================
85
- // Helper Functions
88
+ // NoAuth Implementation
86
89
  // =============================================================================
87
90
 
88
91
  /**
89
- * Create an auth service from an auth handler function.
92
+ * No-authentication implementation.
93
+ *
94
+ * Everyone gets write access with userId "anonymous".
95
+ * ONLY USE FOR DEVELOPMENT/TESTING.
90
96
  */
91
- export const make = (authHandler: AuthHandler): MimicAuthService => ({
92
- authenticate: (token: string) =>
93
- Effect.promise(() => Promise.resolve(authHandler(token))),
94
- });
97
+ export namespace NoAuth {
98
+ /**
99
+ * Create a NoAuth layer.
100
+ * All connections are authenticated with write permission.
101
+ */
102
+ export const make = (): Layer.Layer<MimicAuthServiceTag> =>
103
+ Layer.succeed(MimicAuthServiceTag, {
104
+ authenticate: (_token, _documentId) =>
105
+ Effect.succeed({
106
+ userId: "anonymous",
107
+ permission: "write" as const,
108
+ }),
109
+ });
110
+ }
111
+
112
+ // =============================================================================
113
+ // Static Implementation
114
+ // =============================================================================
95
115
 
96
116
  /**
97
- * Create an auth service from an Effect-based authenticate function.
117
+ * Static permissions implementation.
118
+ *
119
+ * Permissions are defined at configuration time.
120
+ * The token is treated as the userId.
98
121
  */
99
- export const makeEffect = (
100
- authenticate: (token: string) => Effect.Effect<AuthResult>
101
- ): MimicAuthService => ({
102
- authenticate,
103
- });
122
+ export namespace Static {
123
+ export interface Options {
124
+ /**
125
+ * Map of userId (token) to permission level
126
+ */
127
+ readonly permissions: Record<string, Permission>;
128
+ /**
129
+ * Default permission for users not in the permissions map.
130
+ * If undefined, unknown users will fail authentication.
131
+ */
132
+ readonly defaultPermission?: Permission;
133
+ }
134
+
135
+ /**
136
+ * Create a Static auth layer.
137
+ * The token is treated as the userId, and permissions are looked up from the config.
138
+ */
139
+ export const make = (options: Options): Layer.Layer<MimicAuthServiceTag> =>
140
+ Layer.succeed(MimicAuthServiceTag, {
141
+ authenticate: (token, _documentId) => {
142
+ const permission = options.permissions[token] ?? options.defaultPermission;
143
+ if (permission === undefined) {
144
+ return Effect.fail(
145
+ new AuthenticationError({ reason: "Unknown user" })
146
+ );
147
+ }
148
+ return Effect.succeed({
149
+ userId: token,
150
+ permission,
151
+ });
152
+ },
153
+ });
154
+ }
155
+
156
+ // =============================================================================
157
+ // Re-export namespace
158
+ // =============================================================================
159
+
160
+ export const MimicAuthService = {
161
+ Tag: MimicAuthServiceTag,
162
+ make,
163
+ NoAuth,
164
+ Static,
165
+ };