@voidhash/mimic-effect 0.0.3 → 0.0.5
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 +25 -25
- package/dist/DocumentManager.cjs +3 -3
- package/dist/DocumentManager.d.cts +1 -1
- package/dist/DocumentManager.d.cts.map +1 -1
- package/dist/DocumentManager.d.mts.map +1 -1
- package/dist/DocumentManager.mjs +2 -2
- package/dist/DocumentManager.mjs.map +1 -1
- package/dist/MimicConfig.cjs +12 -2
- package/dist/MimicConfig.d.cts +45 -3
- package/dist/MimicConfig.d.cts.map +1 -1
- package/dist/MimicConfig.d.mts +44 -2
- package/dist/MimicConfig.d.mts.map +1 -1
- package/dist/MimicConfig.mjs +11 -2
- package/dist/MimicConfig.mjs.map +1 -1
- package/dist/MimicDataStorage.d.cts +5 -5
- package/dist/MimicServer.cjs +21 -126
- package/dist/MimicServer.d.cts +25 -101
- package/dist/MimicServer.d.cts.map +1 -1
- package/dist/MimicServer.d.mts +25 -101
- package/dist/MimicServer.d.mts.map +1 -1
- package/dist/MimicServer.mjs +26 -130
- package/dist/MimicServer.mjs.map +1 -1
- package/dist/PresenceManager.cjs +2 -2
- package/dist/PresenceManager.mjs +1 -1
- package/dist/WebSocketHandler.cjs +2 -3
- package/dist/WebSocketHandler.d.cts +1 -1
- package/dist/WebSocketHandler.d.mts +1 -1
- package/dist/WebSocketHandler.mjs +2 -2
- package/dist/errors.d.cts +9 -9
- package/package.json +3 -3
- package/src/DocumentManager.ts +5 -3
- package/src/MimicConfig.ts +92 -12
- package/src/MimicServer.ts +76 -200
- package/tests/DocumentManager.test.ts +124 -0
- package/tests/MimicConfig.test.ts +115 -0
- package/tests/MimicServer.test.ts +55 -162
package/dist/MimicServer.cjs
CHANGED
|
@@ -10,9 +10,6 @@ let effect_Effect = require("effect/Effect");
|
|
|
10
10
|
effect_Effect = require_rolldown_runtime.__toESM(effect_Effect);
|
|
11
11
|
let effect_Layer = require("effect/Layer");
|
|
12
12
|
effect_Layer = require_rolldown_runtime.__toESM(effect_Layer);
|
|
13
|
-
let effect_Context = require("effect/Context");
|
|
14
|
-
effect_Context = require_rolldown_runtime.__toESM(effect_Context);
|
|
15
|
-
let _effect_platform_SocketServer = require("@effect/platform/SocketServer");
|
|
16
13
|
let _effect_platform = require("@effect/platform");
|
|
17
14
|
|
|
18
15
|
//#region src/MimicServer.ts
|
|
@@ -21,120 +18,14 @@ let _effect_platform = require("@effect/platform");
|
|
|
21
18
|
* Mimic server layer composition.
|
|
22
19
|
*/
|
|
23
20
|
var MimicServer_exports = /* @__PURE__ */ require_rolldown_runtime.__export({
|
|
24
|
-
MimicWebSocketHandler: () => MimicWebSocketHandler,
|
|
25
21
|
documentManagerLayer: () => documentManagerLayer,
|
|
26
|
-
|
|
27
|
-
layer: () => layer,
|
|
28
|
-
layerHttpLayerRouter: () => layerHttpLayerRouter,
|
|
29
|
-
run: () => run
|
|
22
|
+
layerHttpLayerRouter: () => layerHttpLayerRouter
|
|
30
23
|
});
|
|
31
24
|
/**
|
|
32
|
-
* Tag for the WebSocket handler function.
|
|
33
|
-
*/
|
|
34
|
-
var MimicWebSocketHandler = class extends effect_Context.Tag("@voidhash/mimic-server-effect/MimicWebSocketHandler")() {};
|
|
35
|
-
/**
|
|
36
|
-
* Create a Mimic WebSocket handler layer.
|
|
37
|
-
*
|
|
38
|
-
* This layer provides a handler function that can be used with any WebSocket server
|
|
39
|
-
* implementation. The handler takes a socket and document ID and manages the
|
|
40
|
-
* document synchronization.
|
|
41
|
-
*
|
|
42
|
-
* By default, uses in-memory storage and no authentication.
|
|
43
|
-
* Override these by providing MimicDataStorage and MimicAuthService layers.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* import { MimicServer, MimicAuthService } from "@voidhash/mimic-effect";
|
|
48
|
-
* import { Primitive } from "@voidhash/mimic";
|
|
49
|
-
*
|
|
50
|
-
* const TodoSchema = Primitive.Struct({
|
|
51
|
-
* title: Primitive.String(),
|
|
52
|
-
* completed: Primitive.Boolean(),
|
|
53
|
-
* });
|
|
54
|
-
*
|
|
55
|
-
* // Create the handler layer with defaults
|
|
56
|
-
* const HandlerLayer = MimicServer.layer({
|
|
57
|
-
* basePath: "/mimic/todo",
|
|
58
|
-
* schema: TodoSchema
|
|
59
|
-
* });
|
|
60
|
-
*
|
|
61
|
-
* // Or with custom auth
|
|
62
|
-
* const HandlerLayerWithAuth = MimicServer.layer({
|
|
63
|
-
* basePath: "/mimic/todo",
|
|
64
|
-
* schema: TodoSchema
|
|
65
|
-
* }).pipe(
|
|
66
|
-
* Layer.provideMerge(MimicAuthService.layer({
|
|
67
|
-
* authHandler: (token) => ({ success: true, userId: "user-123" })
|
|
68
|
-
* }))
|
|
69
|
-
* );
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
const layer = (options) => {
|
|
73
|
-
const configLayer = require_MimicConfig.layer({
|
|
74
|
-
schema: options.schema,
|
|
75
|
-
maxTransactionHistory: options.maxTransactionHistory,
|
|
76
|
-
presence: options.presence
|
|
77
|
-
});
|
|
78
|
-
return effect_Layer.merge(effect_Layer.effect(MimicWebSocketHandler, require_WebSocketHandler.makeHandler).pipe(effect_Layer.provide(require_DocumentManager.layer), effect_Layer.provide(require_PresenceManager.layer), effect_Layer.provide(configLayer)), require_DocumentManager.layer.pipe(effect_Layer.provide(configLayer))).pipe(effect_Layer.provide(require_InMemoryDataStorage.layerDefault), effect_Layer.provide(require_NoAuth.layerDefault));
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* Create the Mimic server handler layer.
|
|
82
|
-
* This layer provides the WebSocket handler that can be used with any WebSocket server.
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```typescript
|
|
86
|
-
* import { MimicServer } from "@voidhash/mimic-server-effect";
|
|
87
|
-
* import { SocketServer } from "@effect/platform/SocketServer";
|
|
88
|
-
* import { Primitive } from "@voidhash/mimic";
|
|
89
|
-
*
|
|
90
|
-
* // Define your document schema
|
|
91
|
-
* const TodoSchema = Primitive.Struct({
|
|
92
|
-
* title: Primitive.String(),
|
|
93
|
-
* completed: Primitive.Boolean(),
|
|
94
|
-
* });
|
|
95
|
-
*
|
|
96
|
-
* // Create the server layer
|
|
97
|
-
* const serverLayer = MimicServer.handlerLayer({
|
|
98
|
-
* schema: TodoSchema,
|
|
99
|
-
* });
|
|
100
|
-
*
|
|
101
|
-
* // Run with your socket server
|
|
102
|
-
* Effect.gen(function* () {
|
|
103
|
-
* const handler = yield* MimicServer.MimicWebSocketHandler;
|
|
104
|
-
* const server = yield* SocketServer;
|
|
105
|
-
*
|
|
106
|
-
* yield* server.run((socket) =>
|
|
107
|
-
* // Extract document ID from request and call handler
|
|
108
|
-
* handler(socket, "my-document-id")
|
|
109
|
-
* );
|
|
110
|
-
* }).pipe(
|
|
111
|
-
* Effect.provide(serverLayer),
|
|
112
|
-
* Effect.provide(YourSocketServerLayer),
|
|
113
|
-
* );
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
const handlerLayer = (options) => effect_Layer.effect(MimicWebSocketHandler, require_WebSocketHandler.makeHandler).pipe(effect_Layer.provide(require_DocumentManager.layer), effect_Layer.provide(require_PresenceManager.layer), effect_Layer.provide(require_MimicConfig.layer(options)), effect_Layer.provide(require_InMemoryDataStorage.layerDefault), effect_Layer.provide(require_NoAuth.layerDefault));
|
|
117
|
-
/**
|
|
118
25
|
* Create the document manager layer.
|
|
119
26
|
*/
|
|
120
27
|
const documentManagerLayer = (options) => require_DocumentManager.layer.pipe(effect_Layer.provide(require_MimicConfig.layer(options)), effect_Layer.provide(require_InMemoryDataStorage.layerDefault), effect_Layer.provide(require_NoAuth.layerDefault));
|
|
121
28
|
/**
|
|
122
|
-
* Run a Mimic WebSocket server with the provided handler.
|
|
123
|
-
*
|
|
124
|
-
* This is a helper that:
|
|
125
|
-
* 1. Gets the WebSocket handler from context
|
|
126
|
-
* 2. Runs the socket server with the handler
|
|
127
|
-
*
|
|
128
|
-
* Note: The document ID extraction from socket is implementation-specific.
|
|
129
|
-
* You may need to customize this based on your socket server.
|
|
130
|
-
*/
|
|
131
|
-
const run = (extractDocumentId$1) => effect_Effect.gen(function* () {
|
|
132
|
-
const handler = yield* MimicWebSocketHandler;
|
|
133
|
-
yield* (yield* _effect_platform_SocketServer.SocketServer).run((socket) => effect_Effect.gen(function* () {
|
|
134
|
-
yield* handler(socket, yield* extractDocumentId$1(socket));
|
|
135
|
-
}).pipe(effect_Effect.catchAll((error) => effect_Effect.logError("Connection error", error))));
|
|
136
|
-
});
|
|
137
|
-
/**
|
|
138
29
|
* Create the HTTP handler effect for WebSocket upgrade.
|
|
139
30
|
* This handler:
|
|
140
31
|
* 1. Extracts the document ID from the URL path
|
|
@@ -203,22 +94,26 @@ const makeMimicHandler = effect_Effect.gen(function* () {
|
|
|
203
94
|
* );
|
|
204
95
|
* ```
|
|
205
96
|
*/
|
|
206
|
-
const layerHttpLayerRouter = (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
97
|
+
const layerHttpLayerRouter = (optionsEf) => {
|
|
98
|
+
return effect_Layer.unwrapScoped(effect_Effect.gen(function* () {
|
|
99
|
+
var _options$basePath, _options$authLayer, _options$storageLayer;
|
|
100
|
+
const options = yield* optionsEf;
|
|
101
|
+
const wsPath = `${(_options$basePath = options.basePath) !== null && _options$basePath !== void 0 ? _options$basePath : "/mimic"}/doc/*`;
|
|
102
|
+
const configLayer = require_MimicConfig.layer({
|
|
103
|
+
schema: options.schema,
|
|
104
|
+
maxTransactionHistory: options.maxTransactionHistory,
|
|
105
|
+
presence: options.presence,
|
|
106
|
+
initial: options.initial
|
|
107
|
+
});
|
|
108
|
+
const authLayer = (_options$authLayer = options.authLayer) !== null && _options$authLayer !== void 0 ? _options$authLayer : require_NoAuth.layerDefault;
|
|
109
|
+
const storageLayer = (_options$storageLayer = options.storageLayer) !== null && _options$storageLayer !== void 0 ? _options$storageLayer : require_InMemoryDataStorage.layerDefault;
|
|
110
|
+
const depsLayer = effect_Layer.mergeAll(configLayer, authLayer, storageLayer);
|
|
111
|
+
return effect_Layer.scopedDiscard(effect_Effect.gen(function* () {
|
|
112
|
+
const router = yield* _effect_platform.HttpLayerRouter.HttpRouter;
|
|
113
|
+
const handler = yield* makeMimicHandler;
|
|
114
|
+
yield* router.add("GET", wsPath, handler);
|
|
115
|
+
})).pipe(effect_Layer.provide(require_DocumentManager.layer), effect_Layer.provide(require_PresenceManager.layer), effect_Layer.provide(depsLayer));
|
|
116
|
+
}));
|
|
222
117
|
};
|
|
223
118
|
|
|
224
119
|
//#endregion
|
package/dist/MimicServer.d.cts
CHANGED
|
@@ -1,26 +1,17 @@
|
|
|
1
|
-
import { MimicServerConfigOptions } from "./MimicConfig.cjs";
|
|
1
|
+
import { InitialFn, MimicServerConfigOptions } from "./MimicConfig.cjs";
|
|
2
2
|
import { MimicDataStorageTag } from "./MimicDataStorage.cjs";
|
|
3
3
|
import { DocumentManagerTag } from "./DocumentManager.cjs";
|
|
4
4
|
import { MimicAuthServiceTag } from "./MimicAuthService.cjs";
|
|
5
|
-
import * as _effect_platform_SocketServer0 from "@effect/platform/SocketServer";
|
|
6
|
-
import { SocketServer } from "@effect/platform/SocketServer";
|
|
7
5
|
import * as Effect from "effect/Effect";
|
|
8
6
|
import * as Layer from "effect/Layer";
|
|
9
|
-
import * as Context from "effect/Context";
|
|
10
|
-
import * as Socket from "@effect/platform/Socket";
|
|
11
7
|
import { Presence, Primitive } from "@voidhash/mimic";
|
|
12
8
|
import { HttpLayerRouter } from "@effect/platform";
|
|
13
9
|
import { PathInput } from "@effect/platform/HttpRouter";
|
|
14
10
|
|
|
15
11
|
//#region src/MimicServer.d.ts
|
|
16
12
|
declare namespace MimicServer_d_exports {
|
|
17
|
-
export { MimicLayerOptions,
|
|
13
|
+
export { MimicLayerOptions, MimicLayerRouterOptions, documentManagerLayer, layerHttpLayerRouter };
|
|
18
14
|
}
|
|
19
|
-
declare const MimicWebSocketHandler_base: Context.TagClass<MimicWebSocketHandler, "@voidhash/mimic-server-effect/MimicWebSocketHandler", (socket: Socket.Socket, documentId: string) => Effect.Effect<void, unknown>>;
|
|
20
|
-
/**
|
|
21
|
-
* Tag for the WebSocket handler function.
|
|
22
|
-
*/
|
|
23
|
-
declare class MimicWebSocketHandler extends MimicWebSocketHandler_base {}
|
|
24
15
|
/**
|
|
25
16
|
* Options for creating a Mimic server layer.
|
|
26
17
|
*/
|
|
@@ -44,97 +35,35 @@ interface MimicLayerOptions<TSchema extends Primitive.AnyPrimitive> {
|
|
|
44
35
|
* When provided, enables presence features on WebSocket connections.
|
|
45
36
|
*/
|
|
46
37
|
readonly presence?: Presence.AnyPresence;
|
|
38
|
+
/**
|
|
39
|
+
* Initial state for new documents.
|
|
40
|
+
* Can be either:
|
|
41
|
+
* - A plain object with the initial state values
|
|
42
|
+
* - A function that receives context (with documentId) and returns an Effect producing the initial state
|
|
43
|
+
*
|
|
44
|
+
* When using a function that requires Effect services (has R requirements),
|
|
45
|
+
* you must also provide `initialLayer` to supply those dependencies.
|
|
46
|
+
*
|
|
47
|
+
* Type-safe: required fields (without defaults) must be provided,
|
|
48
|
+
* while optional fields and fields with defaults can be omitted.
|
|
49
|
+
*
|
|
50
|
+
* @default undefined (documents start empty or use schema defaults)
|
|
51
|
+
*/
|
|
52
|
+
readonly initial?: Primitive.InferSetInput<TSchema> | InitialFn<TSchema>;
|
|
47
53
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Create a Mimic WebSocket handler layer.
|
|
50
|
-
*
|
|
51
|
-
* This layer provides a handler function that can be used with any WebSocket server
|
|
52
|
-
* implementation. The handler takes a socket and document ID and manages the
|
|
53
|
-
* document synchronization.
|
|
54
|
-
*
|
|
55
|
-
* By default, uses in-memory storage and no authentication.
|
|
56
|
-
* Override these by providing MimicDataStorage and MimicAuthService layers.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* import { MimicServer, MimicAuthService } from "@voidhash/mimic-effect";
|
|
61
|
-
* import { Primitive } from "@voidhash/mimic";
|
|
62
|
-
*
|
|
63
|
-
* const TodoSchema = Primitive.Struct({
|
|
64
|
-
* title: Primitive.String(),
|
|
65
|
-
* completed: Primitive.Boolean(),
|
|
66
|
-
* });
|
|
67
|
-
*
|
|
68
|
-
* // Create the handler layer with defaults
|
|
69
|
-
* const HandlerLayer = MimicServer.layer({
|
|
70
|
-
* basePath: "/mimic/todo",
|
|
71
|
-
* schema: TodoSchema
|
|
72
|
-
* });
|
|
73
|
-
*
|
|
74
|
-
* // Or with custom auth
|
|
75
|
-
* const HandlerLayerWithAuth = MimicServer.layer({
|
|
76
|
-
* basePath: "/mimic/todo",
|
|
77
|
-
* schema: TodoSchema
|
|
78
|
-
* }).pipe(
|
|
79
|
-
* Layer.provideMerge(MimicAuthService.layer({
|
|
80
|
-
* authHandler: (token) => ({ success: true, userId: "user-123" })
|
|
81
|
-
* }))
|
|
82
|
-
* );
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
declare const layer: <TSchema extends Primitive.AnyPrimitive>(options: MimicLayerOptions<TSchema>) => Layer.Layer<MimicWebSocketHandler | DocumentManagerTag>;
|
|
86
|
-
/**
|
|
87
|
-
* Create the Mimic server handler layer.
|
|
88
|
-
* This layer provides the WebSocket handler that can be used with any WebSocket server.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```typescript
|
|
92
|
-
* import { MimicServer } from "@voidhash/mimic-server-effect";
|
|
93
|
-
* import { SocketServer } from "@effect/platform/SocketServer";
|
|
94
|
-
* import { Primitive } from "@voidhash/mimic";
|
|
95
|
-
*
|
|
96
|
-
* // Define your document schema
|
|
97
|
-
* const TodoSchema = Primitive.Struct({
|
|
98
|
-
* title: Primitive.String(),
|
|
99
|
-
* completed: Primitive.Boolean(),
|
|
100
|
-
* });
|
|
101
|
-
*
|
|
102
|
-
* // Create the server layer
|
|
103
|
-
* const serverLayer = MimicServer.handlerLayer({
|
|
104
|
-
* schema: TodoSchema,
|
|
105
|
-
* });
|
|
106
|
-
*
|
|
107
|
-
* // Run with your socket server
|
|
108
|
-
* Effect.gen(function* () {
|
|
109
|
-
* const handler = yield* MimicServer.MimicWebSocketHandler;
|
|
110
|
-
* const server = yield* SocketServer;
|
|
111
|
-
*
|
|
112
|
-
* yield* server.run((socket) =>
|
|
113
|
-
* // Extract document ID from request and call handler
|
|
114
|
-
* handler(socket, "my-document-id")
|
|
115
|
-
* );
|
|
116
|
-
* }).pipe(
|
|
117
|
-
* Effect.provide(serverLayer),
|
|
118
|
-
* Effect.provide(YourSocketServerLayer),
|
|
119
|
-
* );
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
declare const handlerLayer: <TSchema extends Primitive.AnyPrimitive>(options: MimicServerConfigOptions<TSchema>) => Layer.Layer<MimicWebSocketHandler>;
|
|
123
54
|
/**
|
|
124
55
|
* Create the document manager layer.
|
|
125
56
|
*/
|
|
126
57
|
declare const documentManagerLayer: <TSchema extends Primitive.AnyPrimitive>(options: MimicServerConfigOptions<TSchema>) => Layer.Layer<DocumentManagerTag>;
|
|
127
58
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* This is a helper that:
|
|
131
|
-
* 1. Gets the WebSocket handler from context
|
|
132
|
-
* 2. Runs the socket server with the handler
|
|
133
|
-
*
|
|
134
|
-
* Note: The document ID extraction from socket is implementation-specific.
|
|
135
|
-
* You may need to customize this based on your socket server.
|
|
59
|
+
* Options for layerHttpLayerRouter including optional custom layers.
|
|
136
60
|
*/
|
|
137
|
-
|
|
61
|
+
interface MimicLayerRouterOptions<TSchema extends Primitive.AnyPrimitive> extends MimicLayerOptions<TSchema> {
|
|
62
|
+
/** Custom auth layer. Defaults to NoAuth (all connections allowed). */
|
|
63
|
+
readonly authLayer?: Layer.Layer<MimicAuthServiceTag>;
|
|
64
|
+
/** Custom storage layer. Defaults to InMemoryDataStorage. */
|
|
65
|
+
readonly storageLayer?: Layer.Layer<MimicDataStorageTag>;
|
|
66
|
+
}
|
|
138
67
|
/**
|
|
139
68
|
* Create a Mimic server layer that integrates with HttpLayerRouter.
|
|
140
69
|
*
|
|
@@ -181,12 +110,7 @@ declare const run: (extractDocumentId: (socket: Socket.Socket) => Effect.Effect<
|
|
|
181
110
|
* );
|
|
182
111
|
* ```
|
|
183
112
|
*/
|
|
184
|
-
declare const layerHttpLayerRouter: <TSchema extends Primitive.AnyPrimitive>(
|
|
185
|
-
/** Custom auth layer. Defaults to NoAuth (all connections allowed). */
|
|
186
|
-
readonly authLayer?: Layer.Layer<MimicAuthServiceTag>;
|
|
187
|
-
/** Custom storage layer. Defaults to InMemoryDataStorage. */
|
|
188
|
-
readonly storageLayer?: Layer.Layer<MimicDataStorageTag>;
|
|
189
|
-
}) => Layer.Layer<never, never, HttpLayerRouter.HttpRouter>;
|
|
113
|
+
declare const layerHttpLayerRouter: <TSchema extends Primitive.AnyPrimitive, TError, TRequirements>(optionsEf: Effect.Effect<MimicLayerRouterOptions<TSchema>, TError, TRequirements>) => Layer.Layer<never, TError, TRequirements | HttpLayerRouter.HttpRouter>;
|
|
190
114
|
//#endregion
|
|
191
115
|
export { MimicServer_d_exports };
|
|
192
116
|
//# sourceMappingURL=MimicServer.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MimicServer.d.cts","names":[],"sources":["../src/MimicServer.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MimicServer.d.cts","names":[],"sources":["../src/MimicServer.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;;;UA6BiB,kCACC,SAAA,CAAU;;AAD5B;;;EAWmB,SAAA,QAAA,CAAA,EAJG,SAIH;EAUG;;;EAewD,SAAA,MAAA,EAzB3D,OAyB2D;EAAtB;;AAOxD;;EACgD,SAAA,qBAAA,CAAA,EAAA,MAAA;EAArC;;;;EA+DM,SAAA,QAAA,CAAA,EAtFK,QAAA,CAAS,WAsFS;EAAiB;;;;;;;;AAsDzD;;;;;;EAKa,SAAO,OAAA,CAAA,EAlIC,SAAA,CAAU,aAkIX,CAlIyB,OAkIzB,CAAA,GAlIoC,SAkIpC,CAlI0D,OAkI1D,CAAA;;;;;AACN,cA5HD,oBA4HC,EAAA,CAAA,gBA5HuC,SAAA,CAAU,YA4HjD,CAAA,CAAA,OAAA,EA3HH,wBA2HG,CA3HkC,OA2HlC,CAAA,EAAA,GA1HX,KAAA,CAAM,KA0HK,CA1HC,kBA0HD,CAAA;;;;UA5DG,wCAAwC,SAAA,CAAU,sBACzD,kBAAkB;;uBAEL,KAAA,CAAM,MAAM;;0BAET,KAAA,CAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAiDzB,uCACK,SAAA,CAAU,gDAIf,MAAA,CAAO,OAAO,wBAAwB,UAAU,QAAQ,mBAClE,KAAA,CAAM,aAAa,QAAQ,gBAAgB,eAAA,CAAgB"}
|
package/dist/MimicServer.d.mts
CHANGED
|
@@ -1,26 +1,17 @@
|
|
|
1
|
-
import { MimicServerConfigOptions } from "./MimicConfig.mjs";
|
|
1
|
+
import { InitialFn, MimicServerConfigOptions } from "./MimicConfig.mjs";
|
|
2
2
|
import { MimicDataStorageTag } from "./MimicDataStorage.mjs";
|
|
3
3
|
import { DocumentManagerTag } from "./DocumentManager.mjs";
|
|
4
4
|
import { MimicAuthServiceTag } from "./MimicAuthService.mjs";
|
|
5
5
|
import * as Effect from "effect/Effect";
|
|
6
6
|
import * as Layer from "effect/Layer";
|
|
7
|
-
import * as Context from "effect/Context";
|
|
8
|
-
import * as _effect_platform_SocketServer0 from "@effect/platform/SocketServer";
|
|
9
|
-
import { SocketServer } from "@effect/platform/SocketServer";
|
|
10
7
|
import { Presence, Primitive } from "@voidhash/mimic";
|
|
11
8
|
import { HttpLayerRouter } from "@effect/platform";
|
|
12
|
-
import * as Socket from "@effect/platform/Socket";
|
|
13
9
|
import { PathInput } from "@effect/platform/HttpRouter";
|
|
14
10
|
|
|
15
11
|
//#region src/MimicServer.d.ts
|
|
16
12
|
declare namespace MimicServer_d_exports {
|
|
17
|
-
export { MimicLayerOptions,
|
|
13
|
+
export { MimicLayerOptions, MimicLayerRouterOptions, documentManagerLayer, layerHttpLayerRouter };
|
|
18
14
|
}
|
|
19
|
-
declare const MimicWebSocketHandler_base: Context.TagClass<MimicWebSocketHandler, "@voidhash/mimic-server-effect/MimicWebSocketHandler", (socket: Socket.Socket, documentId: string) => Effect.Effect<void, unknown>>;
|
|
20
|
-
/**
|
|
21
|
-
* Tag for the WebSocket handler function.
|
|
22
|
-
*/
|
|
23
|
-
declare class MimicWebSocketHandler extends MimicWebSocketHandler_base {}
|
|
24
15
|
/**
|
|
25
16
|
* Options for creating a Mimic server layer.
|
|
26
17
|
*/
|
|
@@ -44,97 +35,35 @@ interface MimicLayerOptions<TSchema extends Primitive.AnyPrimitive> {
|
|
|
44
35
|
* When provided, enables presence features on WebSocket connections.
|
|
45
36
|
*/
|
|
46
37
|
readonly presence?: Presence.AnyPresence;
|
|
38
|
+
/**
|
|
39
|
+
* Initial state for new documents.
|
|
40
|
+
* Can be either:
|
|
41
|
+
* - A plain object with the initial state values
|
|
42
|
+
* - A function that receives context (with documentId) and returns an Effect producing the initial state
|
|
43
|
+
*
|
|
44
|
+
* When using a function that requires Effect services (has R requirements),
|
|
45
|
+
* you must also provide `initialLayer` to supply those dependencies.
|
|
46
|
+
*
|
|
47
|
+
* Type-safe: required fields (without defaults) must be provided,
|
|
48
|
+
* while optional fields and fields with defaults can be omitted.
|
|
49
|
+
*
|
|
50
|
+
* @default undefined (documents start empty or use schema defaults)
|
|
51
|
+
*/
|
|
52
|
+
readonly initial?: Primitive.InferSetInput<TSchema> | InitialFn<TSchema>;
|
|
47
53
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Create a Mimic WebSocket handler layer.
|
|
50
|
-
*
|
|
51
|
-
* This layer provides a handler function that can be used with any WebSocket server
|
|
52
|
-
* implementation. The handler takes a socket and document ID and manages the
|
|
53
|
-
* document synchronization.
|
|
54
|
-
*
|
|
55
|
-
* By default, uses in-memory storage and no authentication.
|
|
56
|
-
* Override these by providing MimicDataStorage and MimicAuthService layers.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* import { MimicServer, MimicAuthService } from "@voidhash/mimic-effect";
|
|
61
|
-
* import { Primitive } from "@voidhash/mimic";
|
|
62
|
-
*
|
|
63
|
-
* const TodoSchema = Primitive.Struct({
|
|
64
|
-
* title: Primitive.String(),
|
|
65
|
-
* completed: Primitive.Boolean(),
|
|
66
|
-
* });
|
|
67
|
-
*
|
|
68
|
-
* // Create the handler layer with defaults
|
|
69
|
-
* const HandlerLayer = MimicServer.layer({
|
|
70
|
-
* basePath: "/mimic/todo",
|
|
71
|
-
* schema: TodoSchema
|
|
72
|
-
* });
|
|
73
|
-
*
|
|
74
|
-
* // Or with custom auth
|
|
75
|
-
* const HandlerLayerWithAuth = MimicServer.layer({
|
|
76
|
-
* basePath: "/mimic/todo",
|
|
77
|
-
* schema: TodoSchema
|
|
78
|
-
* }).pipe(
|
|
79
|
-
* Layer.provideMerge(MimicAuthService.layer({
|
|
80
|
-
* authHandler: (token) => ({ success: true, userId: "user-123" })
|
|
81
|
-
* }))
|
|
82
|
-
* );
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
declare const layer: <TSchema extends Primitive.AnyPrimitive>(options: MimicLayerOptions<TSchema>) => Layer.Layer<MimicWebSocketHandler | DocumentManagerTag>;
|
|
86
|
-
/**
|
|
87
|
-
* Create the Mimic server handler layer.
|
|
88
|
-
* This layer provides the WebSocket handler that can be used with any WebSocket server.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```typescript
|
|
92
|
-
* import { MimicServer } from "@voidhash/mimic-server-effect";
|
|
93
|
-
* import { SocketServer } from "@effect/platform/SocketServer";
|
|
94
|
-
* import { Primitive } from "@voidhash/mimic";
|
|
95
|
-
*
|
|
96
|
-
* // Define your document schema
|
|
97
|
-
* const TodoSchema = Primitive.Struct({
|
|
98
|
-
* title: Primitive.String(),
|
|
99
|
-
* completed: Primitive.Boolean(),
|
|
100
|
-
* });
|
|
101
|
-
*
|
|
102
|
-
* // Create the server layer
|
|
103
|
-
* const serverLayer = MimicServer.handlerLayer({
|
|
104
|
-
* schema: TodoSchema,
|
|
105
|
-
* });
|
|
106
|
-
*
|
|
107
|
-
* // Run with your socket server
|
|
108
|
-
* Effect.gen(function* () {
|
|
109
|
-
* const handler = yield* MimicServer.MimicWebSocketHandler;
|
|
110
|
-
* const server = yield* SocketServer;
|
|
111
|
-
*
|
|
112
|
-
* yield* server.run((socket) =>
|
|
113
|
-
* // Extract document ID from request and call handler
|
|
114
|
-
* handler(socket, "my-document-id")
|
|
115
|
-
* );
|
|
116
|
-
* }).pipe(
|
|
117
|
-
* Effect.provide(serverLayer),
|
|
118
|
-
* Effect.provide(YourSocketServerLayer),
|
|
119
|
-
* );
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
declare const handlerLayer: <TSchema extends Primitive.AnyPrimitive>(options: MimicServerConfigOptions<TSchema>) => Layer.Layer<MimicWebSocketHandler>;
|
|
123
54
|
/**
|
|
124
55
|
* Create the document manager layer.
|
|
125
56
|
*/
|
|
126
57
|
declare const documentManagerLayer: <TSchema extends Primitive.AnyPrimitive>(options: MimicServerConfigOptions<TSchema>) => Layer.Layer<DocumentManagerTag>;
|
|
127
58
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* This is a helper that:
|
|
131
|
-
* 1. Gets the WebSocket handler from context
|
|
132
|
-
* 2. Runs the socket server with the handler
|
|
133
|
-
*
|
|
134
|
-
* Note: The document ID extraction from socket is implementation-specific.
|
|
135
|
-
* You may need to customize this based on your socket server.
|
|
59
|
+
* Options for layerHttpLayerRouter including optional custom layers.
|
|
136
60
|
*/
|
|
137
|
-
|
|
61
|
+
interface MimicLayerRouterOptions<TSchema extends Primitive.AnyPrimitive> extends MimicLayerOptions<TSchema> {
|
|
62
|
+
/** Custom auth layer. Defaults to NoAuth (all connections allowed). */
|
|
63
|
+
readonly authLayer?: Layer.Layer<MimicAuthServiceTag>;
|
|
64
|
+
/** Custom storage layer. Defaults to InMemoryDataStorage. */
|
|
65
|
+
readonly storageLayer?: Layer.Layer<MimicDataStorageTag>;
|
|
66
|
+
}
|
|
138
67
|
/**
|
|
139
68
|
* Create a Mimic server layer that integrates with HttpLayerRouter.
|
|
140
69
|
*
|
|
@@ -181,12 +110,7 @@ declare const run: (extractDocumentId: (socket: Socket.Socket) => Effect.Effect<
|
|
|
181
110
|
* );
|
|
182
111
|
* ```
|
|
183
112
|
*/
|
|
184
|
-
declare const layerHttpLayerRouter: <TSchema extends Primitive.AnyPrimitive>(
|
|
185
|
-
/** Custom auth layer. Defaults to NoAuth (all connections allowed). */
|
|
186
|
-
readonly authLayer?: Layer.Layer<MimicAuthServiceTag>;
|
|
187
|
-
/** Custom storage layer. Defaults to InMemoryDataStorage. */
|
|
188
|
-
readonly storageLayer?: Layer.Layer<MimicDataStorageTag>;
|
|
189
|
-
}) => Layer.Layer<never, never, HttpLayerRouter.HttpRouter>;
|
|
113
|
+
declare const layerHttpLayerRouter: <TSchema extends Primitive.AnyPrimitive, TError, TRequirements>(optionsEf: Effect.Effect<MimicLayerRouterOptions<TSchema>, TError, TRequirements>) => Layer.Layer<never, TError, TRequirements | HttpLayerRouter.HttpRouter>;
|
|
190
114
|
//#endregion
|
|
191
115
|
export { MimicServer_d_exports };
|
|
192
116
|
//# sourceMappingURL=MimicServer.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MimicServer.d.mts","names":[],"sources":["../src/MimicServer.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MimicServer.d.mts","names":[],"sources":["../src/MimicServer.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;;;UA6BiB,kCACC,SAAA,CAAU;;AAD5B;;;EAWmB,SAAA,QAAA,CAAA,EAJG,SAIH;EAUG;;;EAewD,SAAA,MAAA,EAzB3D,OAyB2D;EAAtB;;AAOxD;;EACgD,SAAA,qBAAA,CAAA,EAAA,MAAA;EAArC;;;;EA+DM,SAAA,QAAA,CAAA,EAtFK,QAAA,CAAS,WAsFS;EAAiB;;;;;;;;AAsDzD;;;;;;EAKa,SAAO,OAAA,CAAA,EAlIC,SAAA,CAAU,aAkIX,CAlIyB,OAkIzB,CAAA,GAlIoC,SAkIpC,CAlI0D,OAkI1D,CAAA;;;;;AACN,cA5HD,oBA4HC,EAAA,CAAA,gBA5HuC,SAAA,CAAU,YA4HjD,CAAA,CAAA,OAAA,EA3HH,wBA2HG,CA3HkC,OA2HlC,CAAA,EAAA,GA1HX,KAAA,CAAM,KA0HK,CA1HC,kBA0HD,CAAA;;;;UA5DG,wCAAwC,SAAA,CAAU,sBACzD,kBAAkB;;uBAEL,KAAA,CAAM,MAAM;;0BAET,KAAA,CAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAiDzB,uCACK,SAAA,CAAU,gDAIf,MAAA,CAAO,OAAO,wBAAwB,UAAU,QAAQ,mBAClE,KAAA,CAAM,aAAa,QAAQ,gBAAgB,eAAA,CAAgB"}
|