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

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 (227) hide show
  1. package/.turbo/turbo-build.log +136 -90
  2. package/README.md +385 -0
  3. package/dist/ColdStorage.cjs +60 -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 +60 -0
  9. package/dist/ColdStorage.mjs.map +1 -0
  10. package/dist/DocumentManager.cjs +263 -82
  11. package/dist/DocumentManager.d.cts +44 -22
  12. package/dist/DocumentManager.d.cts.map +1 -1
  13. package/dist/DocumentManager.d.mts +44 -22
  14. package/dist/DocumentManager.d.mts.map +1 -1
  15. package/dist/DocumentManager.mjs +259 -67
  16. package/dist/DocumentManager.mjs.map +1 -1
  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 +100 -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 +100 -0
  30. package/dist/HotStorage.mjs.map +1 -0
  31. package/dist/Metrics.cjs +143 -0
  32. package/dist/Metrics.d.cts +31 -0
  33. package/dist/Metrics.d.cts.map +1 -0
  34. package/dist/Metrics.d.mts +31 -0
  35. package/dist/Metrics.d.mts.map +1 -0
  36. package/dist/Metrics.mjs +126 -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 +521 -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 +523 -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 +97 -0
  60. package/dist/MimicServerEngine.d.cts +78 -0
  61. package/dist/MimicServerEngine.d.cts.map +1 -0
  62. package/dist/MimicServerEngine.d.mts +78 -0
  63. package/dist/MimicServerEngine.d.mts.map +1 -0
  64. package/dist/MimicServerEngine.mjs +97 -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 +172 -0
  81. package/dist/Types.d.cts.map +1 -0
  82. package/dist/Types.d.mts +172 -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 -75
  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 +135 -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 +136 -0
  103. package/dist/testing/FailingStorage.mjs.map +1 -0
  104. package/dist/testing/HotStorageTestSuite.cjs +585 -0
  105. package/dist/testing/HotStorageTestSuite.d.cts +40 -0
  106. package/dist/testing/HotStorageTestSuite.d.cts.map +1 -0
  107. package/dist/testing/HotStorageTestSuite.d.mts +40 -0
  108. package/dist/testing/HotStorageTestSuite.d.mts.map +1 -0
  109. package/dist/testing/HotStorageTestSuite.mjs +585 -0
  110. package/dist/testing/HotStorageTestSuite.mjs.map +1 -0
  111. package/dist/testing/StorageIntegrationTestSuite.cjs +349 -0
  112. package/dist/testing/StorageIntegrationTestSuite.d.cts +35 -0
  113. package/dist/testing/StorageIntegrationTestSuite.d.cts.map +1 -0
  114. package/dist/testing/StorageIntegrationTestSuite.d.mts +35 -0
  115. package/dist/testing/StorageIntegrationTestSuite.d.mts.map +1 -0
  116. package/dist/testing/StorageIntegrationTestSuite.mjs +349 -0
  117. package/dist/testing/StorageIntegrationTestSuite.mjs.map +1 -0
  118. package/dist/testing/assertions.cjs +114 -0
  119. package/dist/testing/assertions.mjs +109 -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 +136 -0
  134. package/src/DocumentManager.ts +550 -190
  135. package/src/Errors.ts +114 -0
  136. package/src/HotStorage.ts +239 -0
  137. package/src/Metrics.ts +187 -0
  138. package/src/MimicAuthService.ts +126 -64
  139. package/src/MimicClusterServerEngine.ts +946 -0
  140. package/src/MimicServer.ts +448 -195
  141. package/src/MimicServerEngine.ts +276 -0
  142. package/src/PresenceManager.ts +169 -240
  143. package/src/Protocol.ts +350 -0
  144. package/src/Types.ts +231 -0
  145. package/src/index.ts +57 -23
  146. package/src/testing/ColdStorageTestSuite.ts +589 -0
  147. package/src/testing/FailingStorage.ts +286 -0
  148. package/src/testing/HotStorageTestSuite.ts +762 -0
  149. package/src/testing/StorageIntegrationTestSuite.ts +504 -0
  150. package/src/testing/assertions.ts +181 -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/DocumentManager.test.ts +158 -287
  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/DocumentProtocol.cjs +0 -94
  166. package/dist/DocumentProtocol.d.cts +0 -113
  167. package/dist/DocumentProtocol.d.cts.map +0 -1
  168. package/dist/DocumentProtocol.d.mts +0 -113
  169. package/dist/DocumentProtocol.d.mts.map +0 -1
  170. package/dist/DocumentProtocol.mjs +0 -89
  171. package/dist/DocumentProtocol.mjs.map +0 -1
  172. package/dist/MimicConfig.cjs +0 -60
  173. package/dist/MimicConfig.d.cts +0 -141
  174. package/dist/MimicConfig.d.cts.map +0 -1
  175. package/dist/MimicConfig.d.mts +0 -141
  176. package/dist/MimicConfig.d.mts.map +0 -1
  177. package/dist/MimicConfig.mjs +0 -50
  178. package/dist/MimicConfig.mjs.map +0 -1
  179. package/dist/MimicDataStorage.cjs +0 -83
  180. package/dist/MimicDataStorage.d.cts +0 -113
  181. package/dist/MimicDataStorage.d.cts.map +0 -1
  182. package/dist/MimicDataStorage.d.mts +0 -113
  183. package/dist/MimicDataStorage.d.mts.map +0 -1
  184. package/dist/MimicDataStorage.mjs +0 -74
  185. package/dist/MimicDataStorage.mjs.map +0 -1
  186. package/dist/WebSocketHandler.cjs +0 -365
  187. package/dist/WebSocketHandler.d.cts +0 -34
  188. package/dist/WebSocketHandler.d.cts.map +0 -1
  189. package/dist/WebSocketHandler.d.mts +0 -34
  190. package/dist/WebSocketHandler.d.mts.map +0 -1
  191. package/dist/WebSocketHandler.mjs +0 -355
  192. package/dist/WebSocketHandler.mjs.map +0 -1
  193. package/dist/auth/NoAuth.cjs +0 -43
  194. package/dist/auth/NoAuth.d.cts +0 -22
  195. package/dist/auth/NoAuth.d.cts.map +0 -1
  196. package/dist/auth/NoAuth.d.mts +0 -22
  197. package/dist/auth/NoAuth.d.mts.map +0 -1
  198. package/dist/auth/NoAuth.mjs +0 -36
  199. package/dist/auth/NoAuth.mjs.map +0 -1
  200. package/dist/errors.cjs +0 -74
  201. package/dist/errors.d.cts +0 -89
  202. package/dist/errors.d.cts.map +0 -1
  203. package/dist/errors.d.mts +0 -89
  204. package/dist/errors.d.mts.map +0 -1
  205. package/dist/errors.mjs +0 -67
  206. package/dist/errors.mjs.map +0 -1
  207. package/dist/storage/InMemoryDataStorage.cjs +0 -57
  208. package/dist/storage/InMemoryDataStorage.d.cts +0 -19
  209. package/dist/storage/InMemoryDataStorage.d.cts.map +0 -1
  210. package/dist/storage/InMemoryDataStorage.d.mts +0 -19
  211. package/dist/storage/InMemoryDataStorage.d.mts.map +0 -1
  212. package/dist/storage/InMemoryDataStorage.mjs +0 -48
  213. package/dist/storage/InMemoryDataStorage.mjs.map +0 -1
  214. package/src/DocumentProtocol.ts +0 -112
  215. package/src/MimicConfig.ts +0 -211
  216. package/src/MimicDataStorage.ts +0 -157
  217. package/src/WebSocketHandler.ts +0 -735
  218. package/src/auth/NoAuth.ts +0 -46
  219. package/src/errors.ts +0 -113
  220. package/src/storage/InMemoryDataStorage.ts +0 -66
  221. package/tests/DocumentProtocol.test.ts +0 -113
  222. package/tests/InMemoryDataStorage.test.ts +0 -190
  223. package/tests/MimicConfig.test.ts +0 -290
  224. package/tests/MimicDataStorage.test.ts +0 -190
  225. package/tests/NoAuth.test.ts +0 -94
  226. package/tests/WebSocketHandler.test.ts +0 -321
  227. package/tests/errors.test.ts +0 -77
@@ -1,65 +1,78 @@
1
- import * as Effect from "effect/Effect";
2
- import * as Layer from "effect/Layer";
3
- import * as Context from "effect/Context";
1
+ import { AuthContext, Permission } from "./Types.mjs";
2
+ import { AuthenticationError } from "./Errors.mjs";
3
+ import { Context, Effect, Layer } from "effect";
4
4
 
5
5
  //#region src/MimicAuthService.d.ts
6
- declare namespace MimicAuthService_d_exports {
7
- export { AuthHandler, AuthResult, MimicAuthService, MimicAuthServiceTag, layer, layerEffect, layerService, make, makeEffect };
8
- }
9
- /**
10
- * Result of an authentication attempt.
11
- */
12
- type AuthResult = {
13
- readonly success: true;
14
- readonly userId?: string;
15
- } | {
16
- readonly success: false;
17
- readonly error: string;
18
- };
19
- /**
20
- * Authentication handler function type.
21
- * Can be synchronous or return a Promise.
22
- */
23
- type AuthHandler = (token: string) => Promise<AuthResult> | AuthResult;
6
+
24
7
  /**
25
- * Authentication service interface.
26
- * Implementations can authenticate connections using various methods (JWT, API keys, etc.)
8
+ * MimicAuthService interface for authentication and authorization.
9
+ *
10
+ * The `authenticate` method receives the token from the client's auth message
11
+ * and the document ID being accessed. It should return an AuthContext on success
12
+ * or fail with AuthenticationError on failure.
13
+ *
14
+ * The permission in AuthContext determines what the user can do:
15
+ * - "read": Can subscribe, receive transactions, get snapshots
16
+ * - "write": All of the above, plus can submit transactions and set presence
27
17
  */
28
18
  interface MimicAuthService {
29
19
  /**
30
- * Authenticate a connection using the provided token.
31
- * @param token - The authentication token provided by the client
32
- * @returns The authentication result
20
+ * Authenticate a connection and return authorization context.
21
+ *
22
+ * @param token - The token provided by the client
23
+ * @param documentId - The document ID being accessed
24
+ * @returns AuthContext with userId and permission level
33
25
  */
34
- readonly authenticate: (token: string) => Effect.Effect<AuthResult>;
26
+ readonly authenticate: (token: string, documentId: string) => Effect.Effect<AuthContext, AuthenticationError>;
35
27
  }
36
- declare const MimicAuthServiceTag_base: Context.TagClass<MimicAuthServiceTag, "@voidhash/mimic-server-effect/MimicAuthService", MimicAuthService>;
28
+ declare const MimicAuthServiceTag_base: Context.TagClass<MimicAuthServiceTag, "@voidhash/mimic-effect/MimicAuthService", MimicAuthService>;
37
29
  /**
38
- * Context tag for MimicAuthService service.
30
+ * Context tag for MimicAuthService
39
31
  */
40
32
  declare class MimicAuthServiceTag extends MimicAuthServiceTag_base {}
41
33
  /**
42
- * Create a MimicAuthService layer from an auth handler function.
43
- */
44
- declare const layer: (options: {
45
- readonly authHandler: AuthHandler;
46
- }) => Layer.Layer<MimicAuthServiceTag>;
47
- /**
48
- * Create a MimicAuthService layer from an auth service implementation.
49
- */
50
- declare const layerService: (service: MimicAuthService) => Layer.Layer<MimicAuthServiceTag>;
51
- /**
52
- * Create a MimicAuthService layer from an Effect that produces an auth service.
53
- */
54
- declare const layerEffect: <E, R>(effect: Effect.Effect<MimicAuthService, E, R>) => Layer.Layer<MimicAuthServiceTag, E, R>;
55
- /**
56
- * Create an auth service from an auth handler function.
34
+ * No-authentication implementation.
35
+ *
36
+ * Everyone gets write access with userId "anonymous".
37
+ * ONLY USE FOR DEVELOPMENT/TESTING.
57
38
  */
58
- declare const make: (authHandler: AuthHandler) => MimicAuthService;
39
+ declare namespace NoAuth {
40
+ /**
41
+ * Create a NoAuth layer.
42
+ * All connections are authenticated with write permission.
43
+ */
44
+ const make: () => Layer.Layer<MimicAuthServiceTag>;
45
+ }
59
46
  /**
60
- * Create an auth service from an Effect-based authenticate function.
47
+ * Static permissions implementation.
48
+ *
49
+ * Permissions are defined at configuration time.
50
+ * The token is treated as the userId.
61
51
  */
62
- declare const makeEffect: (authenticate: (token: string) => Effect.Effect<AuthResult>) => MimicAuthService;
52
+ declare namespace Static {
53
+ interface Options {
54
+ /**
55
+ * Map of userId (token) to permission level
56
+ */
57
+ readonly permissions: Record<string, Permission>;
58
+ /**
59
+ * Default permission for users not in the permissions map.
60
+ * If undefined, unknown users will fail authentication.
61
+ */
62
+ readonly defaultPermission?: Permission;
63
+ }
64
+ /**
65
+ * Create a Static auth layer.
66
+ * The token is treated as the userId, and permissions are looked up from the config.
67
+ */
68
+ const make: (options: Options) => Layer.Layer<MimicAuthServiceTag>;
69
+ }
70
+ declare const MimicAuthService: {
71
+ Tag: typeof MimicAuthServiceTag;
72
+ make: <E, R>(effect: Effect.Effect<MimicAuthService, E, R>) => Layer.Layer<MimicAuthServiceTag, E, R>;
73
+ NoAuth: typeof NoAuth;
74
+ Static: typeof Static;
75
+ };
63
76
  //#endregion
64
- export { MimicAuthServiceTag, MimicAuthService_d_exports };
77
+ export { MimicAuthService, MimicAuthServiceTag };
65
78
  //# sourceMappingURL=MimicAuthService.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"MimicAuthService.d.mts","names":[],"sources":["../src/MimicAuthService.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;KAgBY,UAAA;;;;;;AAAZ,CAAA;AAQA;;;;AAA6E,KAAjE,WAAA,GAAiE,CAAA,KAAA,EAAA,MAAA,EAAA,GAAhC,OAAgC,CAAxB,UAAwB,CAAA,GAAV,UAAU;AAU7E;AAOC;;;UAPgB,gBAAA;;AAgBjB;AAWA;;;EAEI,SAAM,YAAA,EAAA,CAAA,KAAA,EAAA,MAAA,EAAA,GAvBkC,MAAA,CAAO,MAuBzC,CAvBgD,UAuBhD,CAAA;;AASV,cA/BC,wBAgC4C,kBAAA,oBAAA,EAAA,gDAAA,kBAAA,CAAA;;;;AADuB,cAtBvD,mBAAA,SAA4B,wBAAA,CAsB2B;;;;AAO1D,cAlBG,KAkBI,EAAA,CAAA,OAAA,EAAA;EACF,SAAA,WAAA,EAlBS,WAkBT;CAAqB,EAAA,GAjBhC,KAAA,CAAM,KAiB0B,CAjBpB,mBAiBoB,CAAA;;;;AAUvB,cAlBA,YAkBqB,EAAA,CAAc,OAAA,EAlBV,gBAqBpC,EAAA,GArBuD,KAAA,CAAM,KAqB7D,CArBmE,mBAqBnE,CAAA;AAKF;;;AAEG,cAtBU,WAsBV,EAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,MAAA,EArBO,MAAA,CAAO,MAqBd,CArBqB,gBAqBrB,EArBuC,CAqBvC,EArB0C,CAqB1C,CAAA,EAAA,GApBA,KAAA,CAAM,KAoBN,CApBY,mBAoBZ,EApBiC,CAoBjC,EApBoC,CAoBpC,CAAA;;;;cAVU,oBAAqB,gBAAc;;;;cAQnC,8CACsB,MAAA,CAAO,OAAO,gBAC9C"}
1
+ {"version":3,"file":"MimicAuthService.d.mts","names":[],"sources":["../src/MimicAuthService.ts"],"sourcesContent":[],"mappings":";;;;;;;;AAoCC;;;;;AASD;AAmDA;AAyBA;;AAK0B,UAtGT,gBAAA,CAsGS;EAKO;;;;;AA4BjC;;EA7EwB,SAAA,YAAA,EAAA,CAAA,KAAA,EAAA,MAAA,EAAA,UAAA,EAAA,MAAA,EAAA,GA/CjB,MAAA,CAAO,MA+CU,CA/CH,WA+CG,EA/CU,mBA+CV,CAAA;;cA9CvB,wBA8C4C,kBAAA,oBAAA,EAAA,yCAAA,kBAAA,CAAA;;;;AACN,cAtC1B,mBAAA,SAA4B,wBAAA,CAsCF;;;;;;;kBAatB,MAAA;;;;;oBAKS,KAAA,CAAM,MAAM;;;;;;;;kBAoBrB,MAAA;;;;;0BAKS,eAAe;;;;;iCAKR;;;;;;wBAOD,YAAU,KAAA,CAAM,MAAM;;cAqBzC;;uBA7EH,MAAA,CAAO,OAAO,kBAAkB,GAAG,OAC1C,KAAA,CAAM,MAAM,qBAAqB,GAAG"}
@@ -1,47 +1,71 @@
1
- import { __export } from "./_virtual/rolldown_runtime.mjs";
2
- import * as Effect from "effect/Effect";
3
- import * as Layer from "effect/Layer";
4
- import * as Context from "effect/Context";
1
+ import { AuthenticationError } from "./Errors.mjs";
2
+ import { Context, Effect, Layer } from "effect";
5
3
 
6
4
  //#region src/MimicAuthService.ts
7
5
  /**
8
- * @since 0.0.1
9
- * Authentication service interface for Mimic connections.
10
- * Provides pluggable authentication adapters.
6
+ * @voidhash/mimic-effect - MimicAuthService
7
+ *
8
+ * Authentication and authorization service interface and implementations.
11
9
  */
12
- var MimicAuthService_exports = /* @__PURE__ */ __export({
13
- MimicAuthServiceTag: () => MimicAuthServiceTag,
14
- layer: () => layer,
15
- layerEffect: () => layerEffect,
16
- layerService: () => layerService,
17
- make: () => make,
18
- makeEffect: () => makeEffect
19
- });
20
10
  /**
21
- * Context tag for MimicAuthService service.
11
+ * Context tag for MimicAuthService
22
12
  */
23
- var MimicAuthServiceTag = class extends Context.Tag("@voidhash/mimic-server-effect/MimicAuthService")() {};
13
+ var MimicAuthServiceTag = class extends Context.Tag("@voidhash/mimic-effect/MimicAuthService")() {};
24
14
  /**
25
- * Create a MimicAuthService layer from an auth handler function.
15
+ * Create a MimicAuthService layer from an Effect that produces the service.
16
+ *
17
+ * This allows you to access other Effect services when implementing authentication.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const Auth = MimicAuthService.make(
22
+ * Effect.gen(function*() {
23
+ * const db = yield* DatabaseService
24
+ * const jwt = yield* JwtService
25
+ *
26
+ * return {
27
+ * authenticate: (token, documentId) =>
28
+ * Effect.gen(function*() {
29
+ * const payload = yield* jwt.verify(token).pipe(
30
+ * Effect.mapError(() => new AuthenticationError({ reason: "Invalid token" }))
31
+ * )
32
+ *
33
+ * const permission = yield* db.getDocumentPermission(payload.userId, documentId)
34
+ *
35
+ * return { userId: payload.userId, permission }
36
+ * })
37
+ * }
38
+ * })
39
+ * )
40
+ * ```
26
41
  */
27
- const layer = (options) => Layer.succeed(MimicAuthServiceTag, { authenticate: (token) => Effect.promise(() => Promise.resolve(options.authHandler(token))) });
28
- /**
29
- * Create a MimicAuthService layer from an auth service implementation.
30
- */
31
- const layerService = (service) => Layer.succeed(MimicAuthServiceTag, service);
32
- /**
33
- * Create a MimicAuthService layer from an Effect that produces an auth service.
34
- */
35
- const layerEffect = (effect) => Layer.effect(MimicAuthServiceTag, effect);
36
- /**
37
- * Create an auth service from an auth handler function.
38
- */
39
- const make = (authHandler) => ({ authenticate: (token) => Effect.promise(() => Promise.resolve(authHandler(token))) });
40
- /**
41
- * Create an auth service from an Effect-based authenticate function.
42
- */
43
- const makeEffect = (authenticate) => ({ authenticate });
42
+ const make = (effect) => Layer.effect(MimicAuthServiceTag, effect);
43
+ let NoAuth;
44
+ (function(_NoAuth) {
45
+ _NoAuth.make = () => Layer.succeed(MimicAuthServiceTag, { authenticate: (_token, _documentId) => Effect.succeed({
46
+ userId: "anonymous",
47
+ permission: "write"
48
+ }) });
49
+ })(NoAuth || (NoAuth = {}));
50
+ let Static;
51
+ (function(_Static) {
52
+ _Static.make = (options) => Layer.succeed(MimicAuthServiceTag, { authenticate: (token, _documentId) => {
53
+ var _options$permissions$;
54
+ const permission = (_options$permissions$ = options.permissions[token]) !== null && _options$permissions$ !== void 0 ? _options$permissions$ : options.defaultPermission;
55
+ if (permission === void 0) return Effect.fail(new AuthenticationError({ reason: "Unknown user" }));
56
+ return Effect.succeed({
57
+ userId: token,
58
+ permission
59
+ });
60
+ } });
61
+ })(Static || (Static = {}));
62
+ const MimicAuthService = {
63
+ Tag: MimicAuthServiceTag,
64
+ make,
65
+ NoAuth,
66
+ Static
67
+ };
44
68
 
45
69
  //#endregion
46
- export { MimicAuthServiceTag, MimicAuthService_exports };
70
+ export { MimicAuthService, MimicAuthServiceTag };
47
71
  //# sourceMappingURL=MimicAuthService.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"MimicAuthService.mjs","names":[],"sources":["../src/MimicAuthService.ts"],"sourcesContent":["/**\n * @since 0.0.1\n * Authentication service interface for Mimic connections.\n * Provides pluggable authentication adapters.\n */\nimport * as Effect from \"effect/Effect\";\nimport * as Context from \"effect/Context\";\nimport * as Layer from \"effect/Layer\";\n\n// =============================================================================\n// Authentication Types\n// =============================================================================\n\n/**\n * Result of an authentication attempt.\n */\nexport type AuthResult =\n | { readonly success: true; readonly userId?: string }\n | { readonly success: false; readonly error: string };\n\n/**\n * Authentication handler function type.\n * Can be synchronous or return a Promise.\n */\nexport type AuthHandler = (token: string) => Promise<AuthResult> | AuthResult;\n\n// =============================================================================\n// Auth Service Interface\n// =============================================================================\n\n/**\n * Authentication service interface.\n * Implementations can authenticate connections using various methods (JWT, API keys, etc.)\n */\nexport interface MimicAuthService {\n /**\n * Authenticate a connection using the provided token.\n * @param token - The authentication token provided by the client\n * @returns The authentication result\n */\n readonly authenticate: (token: string) => Effect.Effect<AuthResult>;\n}\n\n// =============================================================================\n// Context Tag\n// =============================================================================\n\n/**\n * Context tag for MimicAuthService service.\n */\nexport class MimicAuthServiceTag extends Context.Tag(\n \"@voidhash/mimic-server-effect/MimicAuthService\"\n)<MimicAuthServiceTag, MimicAuthService>() {}\n\n// =============================================================================\n// Layer Constructors\n// =============================================================================\n\n/**\n * Create a MimicAuthService layer from an auth handler function.\n */\nexport const layer = (options: {\n readonly authHandler: AuthHandler;\n}): Layer.Layer<MimicAuthServiceTag> =>\n Layer.succeed(MimicAuthServiceTag, {\n authenticate: (token: string) =>\n Effect.promise(() => Promise.resolve(options.authHandler(token))),\n });\n\n/**\n * Create a MimicAuthService layer from an auth service implementation.\n */\nexport const layerService = (service: MimicAuthService): Layer.Layer<MimicAuthServiceTag> =>\n Layer.succeed(MimicAuthServiceTag, service);\n\n/**\n * Create a MimicAuthService layer from an Effect that produces an auth service.\n */\nexport const layerEffect = <E, R>(\n effect: Effect.Effect<MimicAuthService, E, R>\n): Layer.Layer<MimicAuthServiceTag, E, R> =>\n Layer.effect(MimicAuthServiceTag, effect);\n\n// =============================================================================\n// Helper Functions\n// =============================================================================\n\n/**\n * Create an auth service from an auth handler function.\n */\nexport const make = (authHandler: AuthHandler): MimicAuthService => ({\n authenticate: (token: string) =>\n Effect.promise(() => Promise.resolve(authHandler(token))),\n});\n\n/**\n * Create an auth service from an Effect-based authenticate function.\n */\nexport const makeEffect = (\n authenticate: (token: string) => Effect.Effect<AuthResult>\n): MimicAuthService => ({\n authenticate,\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAkDA,IAAa,sBAAb,cAAyC,QAAQ,IAC/C,iDACD,EAAyC,CAAC;;;;AAS3C,MAAa,SAAS,YAGpB,MAAM,QAAQ,qBAAqB,EACjC,eAAe,UACb,OAAO,cAAc,QAAQ,QAAQ,QAAQ,YAAY,MAAM,CAAC,CAAC,EACpE,CAAC;;;;AAKJ,MAAa,gBAAgB,YAC3B,MAAM,QAAQ,qBAAqB,QAAQ;;;;AAK7C,MAAa,eACX,WAEA,MAAM,OAAO,qBAAqB,OAAO;;;;AAS3C,MAAa,QAAQ,iBAAgD,EACnE,eAAe,UACb,OAAO,cAAc,QAAQ,QAAQ,YAAY,MAAM,CAAC,CAAC,EAC5D;;;;AAKD,MAAa,cACX,kBACsB,EACtB,cACD"}
1
+ {"version":3,"file":"MimicAuthService.mjs","names":[],"sources":["../src/MimicAuthService.ts"],"sourcesContent":["/**\n * @voidhash/mimic-effect - MimicAuthService\n *\n * Authentication and authorization service interface and implementations.\n */\nimport { Context, Effect, Layer } from \"effect\";\nimport type { AuthContext, Permission } from \"./Types\";\nimport { AuthenticationError } from \"./Errors\";\n\n// =============================================================================\n// MimicAuthService Interface\n// =============================================================================\n\n/**\n * MimicAuthService interface for authentication and authorization.\n *\n * The `authenticate` method receives the token from the client's auth message\n * and the document ID being accessed. It should return an AuthContext on success\n * or fail with AuthenticationError on failure.\n *\n * The permission in AuthContext determines what the user can do:\n * - \"read\": Can subscribe, receive transactions, get snapshots\n * - \"write\": All of the above, plus can submit transactions and set presence\n */\nexport interface MimicAuthService {\n /**\n * Authenticate a connection and return authorization context.\n *\n * @param token - The token provided by the client\n * @param documentId - The document ID being accessed\n * @returns AuthContext with userId and permission level\n */\n readonly authenticate: (\n token: string,\n documentId: string\n ) => Effect.Effect<AuthContext, AuthenticationError>;\n}\n\n// =============================================================================\n// Context Tag\n// =============================================================================\n\n/**\n * Context tag for MimicAuthService\n */\nexport class MimicAuthServiceTag extends Context.Tag(\n \"@voidhash/mimic-effect/MimicAuthService\"\n)<MimicAuthServiceTag, MimicAuthService>() {}\n\n// =============================================================================\n// Factory\n// =============================================================================\n\n/**\n * Create a MimicAuthService layer from an Effect that produces the service.\n *\n * This allows you to access other Effect services when implementing authentication.\n *\n * @example\n * ```typescript\n * const Auth = MimicAuthService.make(\n * Effect.gen(function*() {\n * const db = yield* DatabaseService\n * const jwt = yield* JwtService\n *\n * return {\n * authenticate: (token, documentId) =>\n * Effect.gen(function*() {\n * const payload = yield* jwt.verify(token).pipe(\n * Effect.mapError(() => new AuthenticationError({ reason: \"Invalid token\" }))\n * )\n *\n * const permission = yield* db.getDocumentPermission(payload.userId, documentId)\n *\n * return { userId: payload.userId, permission }\n * })\n * }\n * })\n * )\n * ```\n */\nexport const make = <E, R>(\n effect: Effect.Effect<MimicAuthService, E, R>\n): Layer.Layer<MimicAuthServiceTag, E, R> =>\n Layer.effect(MimicAuthServiceTag, effect);\n\n// =============================================================================\n// NoAuth Implementation\n// =============================================================================\n\n/**\n * No-authentication implementation.\n *\n * Everyone gets write access with userId \"anonymous\".\n * ONLY USE FOR DEVELOPMENT/TESTING.\n */\nexport namespace NoAuth {\n /**\n * Create a NoAuth layer.\n * All connections are authenticated with write permission.\n */\n export const make = (): Layer.Layer<MimicAuthServiceTag> =>\n Layer.succeed(MimicAuthServiceTag, {\n authenticate: (_token, _documentId) =>\n Effect.succeed({\n userId: \"anonymous\",\n permission: \"write\" as const,\n }),\n });\n}\n\n// =============================================================================\n// Static Implementation\n// =============================================================================\n\n/**\n * Static permissions implementation.\n *\n * Permissions are defined at configuration time.\n * The token is treated as the userId.\n */\nexport namespace Static {\n export interface Options {\n /**\n * Map of userId (token) to permission level\n */\n readonly permissions: Record<string, Permission>;\n /**\n * Default permission for users not in the permissions map.\n * If undefined, unknown users will fail authentication.\n */\n readonly defaultPermission?: Permission;\n }\n\n /**\n * Create a Static auth layer.\n * The token is treated as the userId, and permissions are looked up from the config.\n */\n export const make = (options: Options): Layer.Layer<MimicAuthServiceTag> =>\n Layer.succeed(MimicAuthServiceTag, {\n authenticate: (token, _documentId) => {\n const permission = options.permissions[token] ?? options.defaultPermission;\n if (permission === undefined) {\n return Effect.fail(\n new AuthenticationError({ reason: \"Unknown user\" })\n );\n }\n return Effect.succeed({\n userId: token,\n permission,\n });\n },\n });\n}\n\n// =============================================================================\n// Re-export namespace\n// =============================================================================\n\nexport const MimicAuthService = {\n Tag: MimicAuthServiceTag,\n make,\n NoAuth,\n Static,\n};\n"],"mappings":";;;;;;;;;;;;AA6CA,IAAa,sBAAb,cAAyC,QAAQ,IAC/C,0CACD,EAAyC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkC3C,MAAa,QACX,WAEA,MAAM,OAAO,qBAAqB,OAAO;;;sBAkBvC,MAAM,QAAQ,qBAAqB,EACjC,eAAe,QAAQ,gBACrB,OAAO,QAAQ;EACb,QAAQ;EACR,YAAY;EACb,CAAC,EACL,CAAC;;;;iBA8BiB,YACnB,MAAM,QAAQ,qBAAqB,EACjC,eAAe,OAAO,gBAAgB;;EACpC,MAAM,sCAAa,QAAQ,YAAY,+EAAU,QAAQ;AACzD,MAAI,eAAe,OACjB,QAAO,OAAO,KACZ,IAAI,oBAAoB,EAAE,QAAQ,gBAAgB,CAAC,CACpD;AAEH,SAAO,OAAO,QAAQ;GACpB,QAAQ;GACR;GACD,CAAC;IAEL,CAAC;;AAON,MAAa,mBAAmB;CAC9B,KAAK;CACL;CACA;CACA;CACD"}