@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.
- package/.turbo/turbo-build.log +136 -90
- package/README.md +385 -0
- package/dist/ColdStorage.cjs +60 -0
- package/dist/ColdStorage.d.cts +53 -0
- package/dist/ColdStorage.d.cts.map +1 -0
- package/dist/ColdStorage.d.mts +53 -0
- package/dist/ColdStorage.d.mts.map +1 -0
- package/dist/ColdStorage.mjs +60 -0
- package/dist/ColdStorage.mjs.map +1 -0
- package/dist/DocumentManager.cjs +263 -82
- package/dist/DocumentManager.d.cts +44 -22
- package/dist/DocumentManager.d.cts.map +1 -1
- package/dist/DocumentManager.d.mts +44 -22
- package/dist/DocumentManager.d.mts.map +1 -1
- package/dist/DocumentManager.mjs +259 -67
- package/dist/DocumentManager.mjs.map +1 -1
- package/dist/Errors.cjs +54 -0
- package/dist/Errors.d.cts +96 -0
- package/dist/Errors.d.cts.map +1 -0
- package/dist/Errors.d.mts +96 -0
- package/dist/Errors.d.mts.map +1 -0
- package/dist/Errors.mjs +48 -0
- package/dist/Errors.mjs.map +1 -0
- package/dist/HotStorage.cjs +100 -0
- package/dist/HotStorage.d.cts +70 -0
- package/dist/HotStorage.d.cts.map +1 -0
- package/dist/HotStorage.d.mts +70 -0
- package/dist/HotStorage.d.mts.map +1 -0
- package/dist/HotStorage.mjs +100 -0
- package/dist/HotStorage.mjs.map +1 -0
- package/dist/Metrics.cjs +143 -0
- package/dist/Metrics.d.cts +31 -0
- package/dist/Metrics.d.cts.map +1 -0
- package/dist/Metrics.d.mts +31 -0
- package/dist/Metrics.d.mts.map +1 -0
- package/dist/Metrics.mjs +126 -0
- package/dist/Metrics.mjs.map +1 -0
- package/dist/MimicAuthService.cjs +61 -45
- package/dist/MimicAuthService.d.cts +61 -48
- package/dist/MimicAuthService.d.cts.map +1 -1
- package/dist/MimicAuthService.d.mts +61 -48
- package/dist/MimicAuthService.d.mts.map +1 -1
- package/dist/MimicAuthService.mjs +60 -36
- package/dist/MimicAuthService.mjs.map +1 -1
- package/dist/MimicClusterServerEngine.cjs +521 -0
- package/dist/MimicClusterServerEngine.d.cts +17 -0
- package/dist/MimicClusterServerEngine.d.cts.map +1 -0
- package/dist/MimicClusterServerEngine.d.mts +17 -0
- package/dist/MimicClusterServerEngine.d.mts.map +1 -0
- package/dist/MimicClusterServerEngine.mjs +523 -0
- package/dist/MimicClusterServerEngine.mjs.map +1 -0
- package/dist/MimicServer.cjs +205 -96
- package/dist/MimicServer.d.cts +9 -110
- package/dist/MimicServer.d.cts.map +1 -1
- package/dist/MimicServer.d.mts +9 -110
- package/dist/MimicServer.d.mts.map +1 -1
- package/dist/MimicServer.mjs +206 -90
- package/dist/MimicServer.mjs.map +1 -1
- package/dist/MimicServerEngine.cjs +97 -0
- package/dist/MimicServerEngine.d.cts +78 -0
- package/dist/MimicServerEngine.d.cts.map +1 -0
- package/dist/MimicServerEngine.d.mts +78 -0
- package/dist/MimicServerEngine.d.mts.map +1 -0
- package/dist/MimicServerEngine.mjs +97 -0
- package/dist/MimicServerEngine.mjs.map +1 -0
- package/dist/PresenceManager.cjs +75 -91
- package/dist/PresenceManager.d.cts +17 -66
- package/dist/PresenceManager.d.cts.map +1 -1
- package/dist/PresenceManager.d.mts +17 -66
- package/dist/PresenceManager.d.mts.map +1 -1
- package/dist/PresenceManager.mjs +74 -78
- package/dist/PresenceManager.mjs.map +1 -1
- package/dist/Protocol.cjs +146 -0
- package/dist/Protocol.d.cts +203 -0
- package/dist/Protocol.d.cts.map +1 -0
- package/dist/Protocol.d.mts +203 -0
- package/dist/Protocol.d.mts.map +1 -0
- package/dist/Protocol.mjs +132 -0
- package/dist/Protocol.mjs.map +1 -0
- package/dist/Types.d.cts +172 -0
- package/dist/Types.d.cts.map +1 -0
- package/dist/Types.d.mts +172 -0
- package/dist/Types.d.mts.map +1 -0
- package/dist/_virtual/rolldown_runtime.cjs +1 -25
- package/dist/_virtual/rolldown_runtime.mjs +4 -1
- package/dist/index.cjs +37 -75
- package/dist/index.d.cts +13 -12
- package/dist/index.d.mts +13 -12
- package/dist/index.mjs +12 -12
- package/dist/testing/ColdStorageTestSuite.cjs +508 -0
- package/dist/testing/ColdStorageTestSuite.d.cts +36 -0
- package/dist/testing/ColdStorageTestSuite.d.cts.map +1 -0
- package/dist/testing/ColdStorageTestSuite.d.mts +36 -0
- package/dist/testing/ColdStorageTestSuite.d.mts.map +1 -0
- package/dist/testing/ColdStorageTestSuite.mjs +508 -0
- package/dist/testing/ColdStorageTestSuite.mjs.map +1 -0
- package/dist/testing/FailingStorage.cjs +135 -0
- package/dist/testing/FailingStorage.d.cts +43 -0
- package/dist/testing/FailingStorage.d.cts.map +1 -0
- package/dist/testing/FailingStorage.d.mts +43 -0
- package/dist/testing/FailingStorage.d.mts.map +1 -0
- package/dist/testing/FailingStorage.mjs +136 -0
- package/dist/testing/FailingStorage.mjs.map +1 -0
- package/dist/testing/HotStorageTestSuite.cjs +585 -0
- package/dist/testing/HotStorageTestSuite.d.cts +40 -0
- package/dist/testing/HotStorageTestSuite.d.cts.map +1 -0
- package/dist/testing/HotStorageTestSuite.d.mts +40 -0
- package/dist/testing/HotStorageTestSuite.d.mts.map +1 -0
- package/dist/testing/HotStorageTestSuite.mjs +585 -0
- package/dist/testing/HotStorageTestSuite.mjs.map +1 -0
- package/dist/testing/StorageIntegrationTestSuite.cjs +349 -0
- package/dist/testing/StorageIntegrationTestSuite.d.cts +35 -0
- package/dist/testing/StorageIntegrationTestSuite.d.cts.map +1 -0
- package/dist/testing/StorageIntegrationTestSuite.d.mts +35 -0
- package/dist/testing/StorageIntegrationTestSuite.d.mts.map +1 -0
- package/dist/testing/StorageIntegrationTestSuite.mjs +349 -0
- package/dist/testing/StorageIntegrationTestSuite.mjs.map +1 -0
- package/dist/testing/assertions.cjs +114 -0
- package/dist/testing/assertions.mjs +109 -0
- package/dist/testing/assertions.mjs.map +1 -0
- package/dist/testing/index.cjs +14 -0
- package/dist/testing/index.d.cts +6 -0
- package/dist/testing/index.d.mts +6 -0
- package/dist/testing/index.mjs +7 -0
- package/dist/testing/types.cjs +15 -0
- package/dist/testing/types.d.cts +90 -0
- package/dist/testing/types.d.cts.map +1 -0
- package/dist/testing/types.d.mts +90 -0
- package/dist/testing/types.d.mts.map +1 -0
- package/dist/testing/types.mjs +16 -0
- package/dist/testing/types.mjs.map +1 -0
- package/package.json +18 -3
- package/src/ColdStorage.ts +136 -0
- package/src/DocumentManager.ts +550 -190
- package/src/Errors.ts +114 -0
- package/src/HotStorage.ts +239 -0
- package/src/Metrics.ts +187 -0
- package/src/MimicAuthService.ts +126 -64
- package/src/MimicClusterServerEngine.ts +946 -0
- package/src/MimicServer.ts +448 -195
- package/src/MimicServerEngine.ts +276 -0
- package/src/PresenceManager.ts +169 -240
- package/src/Protocol.ts +350 -0
- package/src/Types.ts +231 -0
- package/src/index.ts +57 -23
- package/src/testing/ColdStorageTestSuite.ts +589 -0
- package/src/testing/FailingStorage.ts +286 -0
- package/src/testing/HotStorageTestSuite.ts +762 -0
- package/src/testing/StorageIntegrationTestSuite.ts +504 -0
- package/src/testing/assertions.ts +181 -0
- package/src/testing/index.ts +83 -0
- package/src/testing/types.ts +100 -0
- package/tests/ColdStorage.test.ts +24 -0
- package/tests/DocumentManager.test.ts +158 -287
- package/tests/HotStorage.test.ts +24 -0
- package/tests/MimicAuthService.test.ts +102 -134
- package/tests/MimicClusterServerEngine.test.ts +587 -0
- package/tests/MimicServer.test.ts +90 -226
- package/tests/MimicServerEngine.test.ts +521 -0
- package/tests/PresenceManager.test.ts +22 -63
- package/tests/Protocol.test.ts +190 -0
- package/tests/StorageIntegration.test.ts +259 -0
- package/tsconfig.json +1 -1
- package/tsdown.config.ts +1 -1
- package/dist/DocumentProtocol.cjs +0 -94
- package/dist/DocumentProtocol.d.cts +0 -113
- package/dist/DocumentProtocol.d.cts.map +0 -1
- package/dist/DocumentProtocol.d.mts +0 -113
- package/dist/DocumentProtocol.d.mts.map +0 -1
- package/dist/DocumentProtocol.mjs +0 -89
- package/dist/DocumentProtocol.mjs.map +0 -1
- package/dist/MimicConfig.cjs +0 -60
- package/dist/MimicConfig.d.cts +0 -141
- package/dist/MimicConfig.d.cts.map +0 -1
- package/dist/MimicConfig.d.mts +0 -141
- package/dist/MimicConfig.d.mts.map +0 -1
- package/dist/MimicConfig.mjs +0 -50
- package/dist/MimicConfig.mjs.map +0 -1
- package/dist/MimicDataStorage.cjs +0 -83
- package/dist/MimicDataStorage.d.cts +0 -113
- package/dist/MimicDataStorage.d.cts.map +0 -1
- package/dist/MimicDataStorage.d.mts +0 -113
- package/dist/MimicDataStorage.d.mts.map +0 -1
- package/dist/MimicDataStorage.mjs +0 -74
- package/dist/MimicDataStorage.mjs.map +0 -1
- package/dist/WebSocketHandler.cjs +0 -365
- package/dist/WebSocketHandler.d.cts +0 -34
- package/dist/WebSocketHandler.d.cts.map +0 -1
- package/dist/WebSocketHandler.d.mts +0 -34
- package/dist/WebSocketHandler.d.mts.map +0 -1
- package/dist/WebSocketHandler.mjs +0 -355
- package/dist/WebSocketHandler.mjs.map +0 -1
- package/dist/auth/NoAuth.cjs +0 -43
- package/dist/auth/NoAuth.d.cts +0 -22
- package/dist/auth/NoAuth.d.cts.map +0 -1
- package/dist/auth/NoAuth.d.mts +0 -22
- package/dist/auth/NoAuth.d.mts.map +0 -1
- package/dist/auth/NoAuth.mjs +0 -36
- package/dist/auth/NoAuth.mjs.map +0 -1
- package/dist/errors.cjs +0 -74
- package/dist/errors.d.cts +0 -89
- package/dist/errors.d.cts.map +0 -1
- package/dist/errors.d.mts +0 -89
- package/dist/errors.d.mts.map +0 -1
- package/dist/errors.mjs +0 -67
- package/dist/errors.mjs.map +0 -1
- package/dist/storage/InMemoryDataStorage.cjs +0 -57
- package/dist/storage/InMemoryDataStorage.d.cts +0 -19
- package/dist/storage/InMemoryDataStorage.d.cts.map +0 -1
- package/dist/storage/InMemoryDataStorage.d.mts +0 -19
- package/dist/storage/InMemoryDataStorage.d.mts.map +0 -1
- package/dist/storage/InMemoryDataStorage.mjs +0 -48
- package/dist/storage/InMemoryDataStorage.mjs.map +0 -1
- package/src/DocumentProtocol.ts +0 -112
- package/src/MimicConfig.ts +0 -211
- package/src/MimicDataStorage.ts +0 -157
- package/src/WebSocketHandler.ts +0 -735
- package/src/auth/NoAuth.ts +0 -46
- package/src/errors.ts +0 -113
- package/src/storage/InMemoryDataStorage.ts +0 -66
- package/tests/DocumentProtocol.test.ts +0 -113
- package/tests/InMemoryDataStorage.test.ts +0 -190
- package/tests/MimicConfig.test.ts +0 -290
- package/tests/MimicDataStorage.test.ts +0 -190
- package/tests/NoAuth.test.ts +0 -94
- package/tests/WebSocketHandler.test.ts +0 -321
- package/tests/errors.test.ts +0 -77
|
@@ -1,65 +1,78 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
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
|
-
|
|
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
|
-
*
|
|
26
|
-
*
|
|
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
|
|
31
|
-
*
|
|
32
|
-
* @
|
|
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<
|
|
26
|
+
readonly authenticate: (token: string, documentId: string) => Effect.Effect<AuthContext, AuthenticationError>;
|
|
35
27
|
}
|
|
36
|
-
declare const MimicAuthServiceTag_base: Context.TagClass<MimicAuthServiceTag, "@voidhash/mimic-
|
|
28
|
+
declare const MimicAuthServiceTag_base: Context.TagClass<MimicAuthServiceTag, "@voidhash/mimic-effect/MimicAuthService", MimicAuthService>;
|
|
37
29
|
/**
|
|
38
|
-
* Context tag for MimicAuthService
|
|
30
|
+
* Context tag for MimicAuthService
|
|
39
31
|
*/
|
|
40
32
|
declare class MimicAuthServiceTag extends MimicAuthServiceTag_base {}
|
|
41
33
|
/**
|
|
42
|
-
*
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
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
|
-
*
|
|
47
|
+
* Static permissions implementation.
|
|
48
|
+
*
|
|
49
|
+
* Permissions are defined at configuration time.
|
|
50
|
+
* The token is treated as the userId.
|
|
61
51
|
*/
|
|
62
|
-
declare
|
|
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 {
|
|
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":"
|
|
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 {
|
|
2
|
-
import
|
|
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
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
|
11
|
+
* Context tag for MimicAuthService
|
|
22
12
|
*/
|
|
23
|
-
var MimicAuthServiceTag = class extends Context.Tag("@voidhash/mimic-
|
|
13
|
+
var MimicAuthServiceTag = class extends Context.Tag("@voidhash/mimic-effect/MimicAuthService")() {};
|
|
24
14
|
/**
|
|
25
|
-
* Create a MimicAuthService layer from an
|
|
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
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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 {
|
|
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 * @
|
|
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"}
|