every-plugin 0.0.4 → 0.0.6
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/README.md +256 -6
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +2 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +3 -3
- package/dist/plugin.d.ts +57 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +1 -1
- package/dist/runtime/errors.d.ts +41 -0
- package/dist/runtime/errors.d.ts.map +1 -0
- package/dist/runtime/errors.js +7 -0
- package/dist/runtime/index.d.ts +21 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +34 -0
- package/dist/runtime/runtime.d.ts +58 -0
- package/dist/runtime/runtime.d.ts.map +1 -0
- package/dist/runtime/runtime.js +75 -0
- package/dist/runtime/runtime.test.d.ts +2 -0
- package/dist/runtime/runtime.test.d.ts.map +1 -0
- package/dist/runtime/runtime.test.js +308 -0
- package/dist/runtime/services/module-federation.service.d.ts +17 -0
- package/dist/runtime/services/module-federation.service.d.ts.map +1 -0
- package/dist/runtime/services/module-federation.service.js +108 -0
- package/dist/runtime/services/plugin-cache.service.d.ts +18 -0
- package/dist/runtime/services/plugin-cache.service.d.ts.map +1 -0
- package/dist/runtime/services/plugin-cache.service.js +55 -0
- package/dist/runtime/services/plugin.service.d.ts +22 -0
- package/dist/runtime/services/plugin.service.d.ts.map +1 -0
- package/dist/runtime/services/plugin.service.js +130 -0
- package/dist/runtime/services/secrets.service.d.ts +17 -0
- package/dist/runtime/services/secrets.service.d.ts.map +1 -0
- package/dist/runtime/services/secrets.service.js +32 -0
- package/dist/runtime/services.d.ts +5 -0
- package/dist/runtime/services.d.ts.map +1 -0
- package/dist/runtime/services.js +4 -0
- package/dist/runtime/types.d.ts +40 -0
- package/dist/runtime/types.d.ts.map +1 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/validation.d.ts +10 -0
- package/dist/runtime/validation.d.ts.map +1 -0
- package/dist/runtime/validation.js +13 -0
- package/dist/source.d.ts +7 -7
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +35 -19
- package/package.json +40 -23
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.test.d.ts","sourceRoot":"","sources":["../../src/runtime/runtime.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { Effect, Duration } from "effect";
|
|
3
|
+
import { PluginRuntime, createPluginRuntime } from "./index";
|
|
4
|
+
// Local template plugin for testing
|
|
5
|
+
const TEST_REGISTRY = {
|
|
6
|
+
"test-plugin": {
|
|
7
|
+
remoteUrl: "http://localhost:3000/remoteEntry.js",
|
|
8
|
+
type: "transformer",
|
|
9
|
+
version: "0.0.1",
|
|
10
|
+
description: "Local template plugin for testing",
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
const TEST_CONFIG = {
|
|
14
|
+
variables: {
|
|
15
|
+
baseUrl: "http://localhost:1337",
|
|
16
|
+
timeout: 5000,
|
|
17
|
+
},
|
|
18
|
+
secrets: {
|
|
19
|
+
apiKey: "{{API_KEY}}", // This will be hydrated from secrets config
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
const TEST_INPUT = {
|
|
23
|
+
query: "test query for processing",
|
|
24
|
+
options: {
|
|
25
|
+
limit: 2,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
// Secrets configuration for hydration
|
|
29
|
+
const SECRETS_CONFIG = {
|
|
30
|
+
API_KEY: "test-api-key-value",
|
|
31
|
+
};
|
|
32
|
+
describe("Plugin Runtime Integration", () => {
|
|
33
|
+
it("should create and execute plugin end-to-end", async () => {
|
|
34
|
+
// Create the managed runtime
|
|
35
|
+
const runtime = createPluginRuntime({
|
|
36
|
+
registry: TEST_REGISTRY,
|
|
37
|
+
secrets: SECRETS_CONFIG,
|
|
38
|
+
});
|
|
39
|
+
// Execute the test within the runtime
|
|
40
|
+
const result = await runtime.runPromise(Effect.gen(function* () {
|
|
41
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
42
|
+
// Create plugin (load → instantiate → initialize)
|
|
43
|
+
const initializedPlugin = yield* pluginRuntime.createPlugin("test-plugin", TEST_CONFIG);
|
|
44
|
+
// Verify plugin was initialized
|
|
45
|
+
expect(initializedPlugin).toBeDefined();
|
|
46
|
+
expect(initializedPlugin.metadata.pluginId).toBe("test-plugin");
|
|
47
|
+
expect(initializedPlugin.plugin).toBeDefined();
|
|
48
|
+
// Execute plugin
|
|
49
|
+
const output = yield* pluginRuntime.executePlugin(initializedPlugin, TEST_INPUT);
|
|
50
|
+
return output;
|
|
51
|
+
}));
|
|
52
|
+
// Validate output structure
|
|
53
|
+
expect(result).toBeDefined();
|
|
54
|
+
expect(typeof result).toBe("object");
|
|
55
|
+
const typedResult = result;
|
|
56
|
+
// Verify success wrapper structure
|
|
57
|
+
expect(typedResult).toHaveProperty("success");
|
|
58
|
+
expect(typedResult).toHaveProperty("data");
|
|
59
|
+
expect(typedResult.success).toBe(true);
|
|
60
|
+
// Verify data content
|
|
61
|
+
expect(typedResult.data).toHaveProperty("results");
|
|
62
|
+
expect(typedResult.data).toHaveProperty("count");
|
|
63
|
+
expect(Array.isArray(typedResult.data.results)).toBe(true);
|
|
64
|
+
expect(typedResult.data.count).toBe(2); // Should match the limit we set
|
|
65
|
+
expect(typedResult.data.results).toHaveLength(2);
|
|
66
|
+
// Verify results contain processed query
|
|
67
|
+
expect(typedResult.data.results[0]).toHaveProperty("id");
|
|
68
|
+
expect(typedResult.data.results[0]).toHaveProperty("content");
|
|
69
|
+
expect(typedResult.data.results[0].content).toContain("test query for processing");
|
|
70
|
+
expect(typedResult.data.results[1].content).toContain("test query for processing");
|
|
71
|
+
});
|
|
72
|
+
it("should handle plugin lifecycle correctly", async () => {
|
|
73
|
+
const runtime = createPluginRuntime({
|
|
74
|
+
registry: TEST_REGISTRY,
|
|
75
|
+
secrets: SECRETS_CONFIG,
|
|
76
|
+
});
|
|
77
|
+
await runtime.runPromise(Effect.gen(function* () {
|
|
78
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
79
|
+
// Test individual lifecycle steps
|
|
80
|
+
// 1. Load plugin
|
|
81
|
+
const pluginConstructor = yield* pluginRuntime.loadPlugin("test-plugin");
|
|
82
|
+
expect(pluginConstructor).toBeDefined();
|
|
83
|
+
expect(pluginConstructor.metadata.pluginId).toBe("test-plugin");
|
|
84
|
+
// 2. Instantiate plugin
|
|
85
|
+
const pluginInstance = yield* pluginRuntime.instantiatePlugin(pluginConstructor);
|
|
86
|
+
expect(pluginInstance).toBeDefined();
|
|
87
|
+
expect(pluginInstance.plugin).toBeDefined();
|
|
88
|
+
expect(pluginInstance.metadata.pluginId).toBe("test-plugin");
|
|
89
|
+
// 3. Initialize plugin
|
|
90
|
+
const initializedPlugin = yield* pluginRuntime.initializePlugin(pluginInstance, TEST_CONFIG);
|
|
91
|
+
expect(initializedPlugin).toBeDefined();
|
|
92
|
+
expect(initializedPlugin.config).toBeDefined();
|
|
93
|
+
// 4. Execute plugin
|
|
94
|
+
const output = yield* pluginRuntime.executePlugin(initializedPlugin, TEST_INPUT);
|
|
95
|
+
expect(output).toBeDefined();
|
|
96
|
+
}));
|
|
97
|
+
});
|
|
98
|
+
it("should handle secret hydration", async () => {
|
|
99
|
+
const configWithSecrets = {
|
|
100
|
+
variables: {
|
|
101
|
+
baseUrl: "http://localhost:1337",
|
|
102
|
+
timeout: 10000,
|
|
103
|
+
},
|
|
104
|
+
secrets: {
|
|
105
|
+
apiKey: "{{API_KEY}}", // This should be hydrated
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
const secretsConfig = {
|
|
109
|
+
API_KEY: "hydrated-api-key-value",
|
|
110
|
+
};
|
|
111
|
+
const runtime = createPluginRuntime({
|
|
112
|
+
registry: TEST_REGISTRY,
|
|
113
|
+
secrets: secretsConfig,
|
|
114
|
+
});
|
|
115
|
+
await runtime.runPromise(Effect.gen(function* () {
|
|
116
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
117
|
+
const initializedPlugin = yield* pluginRuntime.createPlugin("test-plugin", configWithSecrets);
|
|
118
|
+
// Verify secrets were hydrated in the config
|
|
119
|
+
expect(initializedPlugin.config).toBeDefined();
|
|
120
|
+
// Verify the config structure and that secrets were hydrated
|
|
121
|
+
const config = initializedPlugin.config;
|
|
122
|
+
expect(config).toHaveProperty("secrets");
|
|
123
|
+
expect(config.secrets).toHaveProperty("apiKey");
|
|
124
|
+
expect(config.secrets.apiKey).toBe("hydrated-api-key-value");
|
|
125
|
+
// Verify variables are also present
|
|
126
|
+
expect(config).toHaveProperty("variables");
|
|
127
|
+
expect(config.variables.baseUrl).toBe("http://localhost:1337");
|
|
128
|
+
expect(config.variables.timeout).toBe(10000);
|
|
129
|
+
// Verify the plugin was successfully initialized with hydrated secrets
|
|
130
|
+
// by executing it to ensure it works with the hydrated config
|
|
131
|
+
const output = yield* pluginRuntime.executePlugin(initializedPlugin, TEST_INPUT);
|
|
132
|
+
expect(output).toBeDefined();
|
|
133
|
+
}));
|
|
134
|
+
});
|
|
135
|
+
it("should handle validation error for missing apiKey", async () => {
|
|
136
|
+
const configWithoutApiKey = {
|
|
137
|
+
variables: {
|
|
138
|
+
baseUrl: "http://localhost:1337",
|
|
139
|
+
timeout: 5000,
|
|
140
|
+
},
|
|
141
|
+
secrets: {
|
|
142
|
+
// Missing required apiKey
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
const runtime = createPluginRuntime({
|
|
146
|
+
registry: TEST_REGISTRY,
|
|
147
|
+
secrets: SECRETS_CONFIG,
|
|
148
|
+
});
|
|
149
|
+
const result = await runtime.runPromise(Effect.gen(function* () {
|
|
150
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
151
|
+
return yield* pluginRuntime.createPlugin("test-plugin", configWithoutApiKey).pipe(Effect.catchTag("PluginRuntimeError", (error) => {
|
|
152
|
+
// Should catch validation error
|
|
153
|
+
expect(error.operation).toBe("validate-config");
|
|
154
|
+
expect(error.retryable).toBe(false);
|
|
155
|
+
expect(error.pluginId).toBe("test-plugin");
|
|
156
|
+
expect(error.cause).toBeDefined();
|
|
157
|
+
// The error should mention the missing apiKey
|
|
158
|
+
const errorMessage = error.cause?.message || "";
|
|
159
|
+
// Zod validation errors might be in JSON format, so check for both formats
|
|
160
|
+
const lowerMessage = errorMessage.toLowerCase();
|
|
161
|
+
const hasApiKeyError = lowerMessage.includes("api key") ||
|
|
162
|
+
lowerMessage.includes("apikey") ||
|
|
163
|
+
lowerMessage.includes("required");
|
|
164
|
+
expect(hasApiKeyError).toBe(true);
|
|
165
|
+
console.log("Caught expected validation error:", {
|
|
166
|
+
operation: error.operation,
|
|
167
|
+
pluginId: error.pluginId,
|
|
168
|
+
retryable: error.retryable,
|
|
169
|
+
cause: error.cause?.message,
|
|
170
|
+
});
|
|
171
|
+
return Effect.succeed("validation-error-handled-properly");
|
|
172
|
+
}), Effect.catchAll((unexpectedError) => {
|
|
173
|
+
console.error("Unexpected error type:", unexpectedError);
|
|
174
|
+
expect.fail(`Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`);
|
|
175
|
+
return Effect.succeed("should-not-reach-here");
|
|
176
|
+
}));
|
|
177
|
+
}));
|
|
178
|
+
expect(result).toBe("validation-error-handled-properly");
|
|
179
|
+
});
|
|
180
|
+
it("should handle runtime shutdown gracefully", async () => {
|
|
181
|
+
const runtime = createPluginRuntime({
|
|
182
|
+
registry: TEST_REGISTRY,
|
|
183
|
+
secrets: SECRETS_CONFIG,
|
|
184
|
+
});
|
|
185
|
+
await runtime.runPromise(Effect.gen(function* () {
|
|
186
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
187
|
+
// Test shutdown
|
|
188
|
+
yield* pluginRuntime.shutdown();
|
|
189
|
+
// Shutdown should complete without errors
|
|
190
|
+
expect(true).toBe(true); // If we reach here, shutdown was successful
|
|
191
|
+
}));
|
|
192
|
+
});
|
|
193
|
+
it("should handle invalid remoteUrl with proper error handling", async () => {
|
|
194
|
+
const INVALID_REGISTRY = {
|
|
195
|
+
"invalid-plugin": {
|
|
196
|
+
remoteUrl: "https://invalid-plugin-url-that-does-not-exist.com/plugin.js",
|
|
197
|
+
type: "transformer",
|
|
198
|
+
version: "1.0.0",
|
|
199
|
+
description: "Invalid plugin for testing error handling",
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
const runtime = createPluginRuntime({
|
|
203
|
+
registry: INVALID_REGISTRY,
|
|
204
|
+
secrets: {},
|
|
205
|
+
});
|
|
206
|
+
const result = await runtime.runPromise(Effect.gen(function* () {
|
|
207
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
208
|
+
return yield* pluginRuntime.createPlugin("invalid-plugin", {}).pipe(Effect.catchTag("PluginRuntimeError", (error) => {
|
|
209
|
+
expect(error.operation).toBe("register-remote");
|
|
210
|
+
expect(error.retryable).toBe(true);
|
|
211
|
+
expect(error.pluginId).toBe("invalid-plugin");
|
|
212
|
+
expect(error.cause).toBeDefined();
|
|
213
|
+
console.debug("Caught expected PluginRuntimeError:", {
|
|
214
|
+
operation: error.operation,
|
|
215
|
+
pluginId: error.pluginId,
|
|
216
|
+
retryable: error.retryable,
|
|
217
|
+
cause: error.cause?.message,
|
|
218
|
+
});
|
|
219
|
+
return Effect.succeed("error-handled-properly");
|
|
220
|
+
}), Effect.catchAll((unexpectedError) => {
|
|
221
|
+
// If any other error type is thrown, fail the test
|
|
222
|
+
console.error("Unexpected error type:", unexpectedError);
|
|
223
|
+
expect.fail(`Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`);
|
|
224
|
+
return Effect.succeed("should-not-reach-here");
|
|
225
|
+
}));
|
|
226
|
+
}));
|
|
227
|
+
expect(result).toBe("error-handled-properly");
|
|
228
|
+
});
|
|
229
|
+
it("should handle plugin not found in registry", async () => {
|
|
230
|
+
// Empty registry to test missing plugin
|
|
231
|
+
const EMPTY_REGISTRY = {};
|
|
232
|
+
const runtime = createPluginRuntime({
|
|
233
|
+
registry: EMPTY_REGISTRY,
|
|
234
|
+
secrets: {},
|
|
235
|
+
});
|
|
236
|
+
const result = await runtime.runPromise(Effect.gen(function* () {
|
|
237
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
238
|
+
return yield* pluginRuntime.loadPlugin("non-existent-plugin").pipe(Effect.catchTag("PluginRuntimeError", (error) => {
|
|
239
|
+
// Validate error properties
|
|
240
|
+
expect(error.operation).toBe("load-plugin");
|
|
241
|
+
expect(error.retryable).toBe(false);
|
|
242
|
+
expect(error.pluginId).toBe("non-existent-plugin");
|
|
243
|
+
expect(error.cause).toBeDefined();
|
|
244
|
+
expect(error.cause?.message).toContain("not found in registry");
|
|
245
|
+
// Log error details for debugging
|
|
246
|
+
console.log("Caught expected PluginRuntimeError for missing plugin:", {
|
|
247
|
+
operation: error.operation,
|
|
248
|
+
pluginId: error.pluginId,
|
|
249
|
+
retryable: error.retryable,
|
|
250
|
+
cause: error.cause?.message,
|
|
251
|
+
});
|
|
252
|
+
// Return success to make test pass
|
|
253
|
+
return Effect.succeed("plugin-not-found-handled-properly");
|
|
254
|
+
}), Effect.catchAll((unexpectedError) => {
|
|
255
|
+
// If any other error type is thrown, fail the test
|
|
256
|
+
console.error("Unexpected error type:", unexpectedError);
|
|
257
|
+
expect.fail(`Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`);
|
|
258
|
+
return Effect.succeed("should-not-reach-here");
|
|
259
|
+
}));
|
|
260
|
+
}));
|
|
261
|
+
expect(result).toBe("plugin-not-found-handled-properly");
|
|
262
|
+
});
|
|
263
|
+
it("should handle constructor instantiation failure", async () => {
|
|
264
|
+
// Registry with real remote that should fail during instantiation/initialization
|
|
265
|
+
const REAL_REMOTE_REGISTRY = {
|
|
266
|
+
"simple-transform": {
|
|
267
|
+
remoteUrl: "https://unpkg.com/@curatedotfun/simple-transform@latest/dist/remoteEntry.js",
|
|
268
|
+
type: "transformer",
|
|
269
|
+
version: "latest",
|
|
270
|
+
description: "Real remote plugin for testing constructor failure",
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
const runtime = createPluginRuntime({
|
|
274
|
+
registry: REAL_REMOTE_REGISTRY,
|
|
275
|
+
secrets: {},
|
|
276
|
+
});
|
|
277
|
+
const result = await runtime.runPromise(Effect.gen(function* () {
|
|
278
|
+
const pluginRuntime = yield* PluginRuntime;
|
|
279
|
+
return yield* pluginRuntime.createPlugin("simple-transform", {}).pipe(Effect.timeout(Duration.seconds(1)), // Timeout after 3 seconds
|
|
280
|
+
Effect.catchTag("PluginRuntimeError", (error) => {
|
|
281
|
+
// Validate error properties
|
|
282
|
+
expect(["load-remote"]).toContain(error.operation);
|
|
283
|
+
expect(error.retryable).toBe(true);
|
|
284
|
+
expect(error.pluginId).toBe("simple-transform");
|
|
285
|
+
expect(error.cause).toBeDefined();
|
|
286
|
+
// Log error details for debugging
|
|
287
|
+
console.log("Caught expected PluginRuntimeError for constructor failure:", {
|
|
288
|
+
operation: error.operation,
|
|
289
|
+
pluginId: error.pluginId,
|
|
290
|
+
retryable: error.retryable,
|
|
291
|
+
cause: error.cause?.message,
|
|
292
|
+
});
|
|
293
|
+
// Return success to make test pass
|
|
294
|
+
return Effect.succeed("constructor-failure-handled-properly");
|
|
295
|
+
}), Effect.catchTag("TimeoutException", () => {
|
|
296
|
+
// Handle timeout as expected failure - remote is too slow
|
|
297
|
+
console.log("Plugin creation timed out - treating as expected failure");
|
|
298
|
+
return Effect.succeed("timeout-handled-properly");
|
|
299
|
+
}), Effect.catchAll((unexpectedError) => {
|
|
300
|
+
// If any other error type is thrown, fail the test
|
|
301
|
+
console.error("Unexpected error type:", unexpectedError);
|
|
302
|
+
expect.fail(`Expected PluginRuntimeError or TimeoutException but got: ${unexpectedError._tag || typeof unexpectedError}`);
|
|
303
|
+
return Effect.succeed("should-not-reach-here");
|
|
304
|
+
}));
|
|
305
|
+
}));
|
|
306
|
+
expect(["constructor-failure-handled-properly", "timeout-handled-properly"]).toContain(result);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as EffectModule from "effect";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { ModuleFederationError } from "../errors";
|
|
4
|
+
export interface IModuleFederationService {
|
|
5
|
+
readonly registerRemote: (pluginId: string, url: string) => Effect.Effect<void, ModuleFederationError>;
|
|
6
|
+
readonly loadRemoteConstructor: (pluginId: string, url: string) => Effect.Effect<any, ModuleFederationError>;
|
|
7
|
+
}
|
|
8
|
+
declare const ModuleFederationService_base: EffectModule.Context.TagClass<ModuleFederationService, "ModuleFederationService", IModuleFederationService> & EffectModule.Effect.Tag.Proxy<ModuleFederationService, IModuleFederationService> & {
|
|
9
|
+
use: <X>(body: (_: IModuleFederationService) => X) => [X] extends [EffectModule.Effect.Effect<infer A, infer E, infer R>] ? EffectModule.Effect.Effect<A, E, R | ModuleFederationService> : [X] extends [PromiseLike<infer A_1>] ? EffectModule.Effect.Effect<A_1, EffectModule.Cause.UnknownException, ModuleFederationService> : EffectModule.Effect.Effect<X, never, ModuleFederationService>;
|
|
10
|
+
};
|
|
11
|
+
export declare class ModuleFederationService extends ModuleFederationService_base {
|
|
12
|
+
static Live: EffectModule.Layer.Layer<ModuleFederationService, never, never>;
|
|
13
|
+
}
|
|
14
|
+
export declare const ModuleFederationServiceTag: typeof ModuleFederationService;
|
|
15
|
+
export declare const ModuleFederationServiceLive: EffectModule.Layer.Layer<ModuleFederationService, never, never>;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=module-federation.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-federation.service.d.ts","sourceRoot":"","sources":["../../../src/runtime/services/module-federation.service.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,MAAM,EAAS,MAAM,QAAQ,CAAC;AAEvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAElD,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,cAAc,EAAE,CACvB,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,KACR,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,qBAAqB,CAAC,CAAC;IAChD,QAAQ,CAAC,qBAAqB,EAAE,CAC9B,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,KACR,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,qBAAqB,CAAC,CAAC;CAChD;;;;AAyCD,qBAAa,uBAAwB,SAAQ,4BAG1C;IACD,MAAM,CAAC,IAAI,kEAqFT;CACH;AAED,eAAO,MAAM,0BAA0B,gCAA0B,CAAC;AAClE,eAAO,MAAM,2BAA2B,iEAA+B,CAAC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import { createInstance, getInstance, } from "@module-federation/enhanced/runtime";
|
|
3
|
+
import { setGlobalFederationInstance } from "@module-federation/runtime-core";
|
|
4
|
+
import * as EffectModule from "effect";
|
|
5
|
+
import { Effect, Layer } from "effect";
|
|
6
|
+
import * as ZodModule from "zod";
|
|
7
|
+
import { ModuleFederationError } from "../errors";
|
|
8
|
+
const createModuleFederationInstance = Effect.cached(Effect.sync(() => {
|
|
9
|
+
try {
|
|
10
|
+
let instance = getInstance();
|
|
11
|
+
if (!instance) {
|
|
12
|
+
instance = createInstance({
|
|
13
|
+
name: "host",
|
|
14
|
+
remotes: [],
|
|
15
|
+
shared: {
|
|
16
|
+
effect: {
|
|
17
|
+
shareConfig: {
|
|
18
|
+
singleton: true,
|
|
19
|
+
requiredVersion: "^3.17.6",
|
|
20
|
+
eager: true,
|
|
21
|
+
},
|
|
22
|
+
lib: () => EffectModule,
|
|
23
|
+
},
|
|
24
|
+
zod: {
|
|
25
|
+
shareConfig: {
|
|
26
|
+
singleton: true,
|
|
27
|
+
requiredVersion: "^4.0.8",
|
|
28
|
+
eager: true,
|
|
29
|
+
},
|
|
30
|
+
lib: () => ZodModule,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
setGlobalFederationInstance(instance);
|
|
35
|
+
}
|
|
36
|
+
return instance;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new Error(`Failed to initialize Module Federation: ${error}`);
|
|
40
|
+
}
|
|
41
|
+
}));
|
|
42
|
+
export class ModuleFederationService extends Effect.Tag("ModuleFederationService")() {
|
|
43
|
+
static Live = Layer.effect(ModuleFederationService, Effect.gen(function* () {
|
|
44
|
+
const mf = yield* Effect.flatten(createModuleFederationInstance);
|
|
45
|
+
return {
|
|
46
|
+
registerRemote: (pluginId, url) => Effect.gen(function* () {
|
|
47
|
+
// Check if remote is available
|
|
48
|
+
const { error } = yield* Effect.tryPromise({
|
|
49
|
+
try: () => betterFetch(url, { method: "HEAD" }),
|
|
50
|
+
catch: (error) => new ModuleFederationError({
|
|
51
|
+
pluginId,
|
|
52
|
+
remoteUrl: url,
|
|
53
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
56
|
+
if (error) {
|
|
57
|
+
return yield* Effect.fail(new ModuleFederationError({
|
|
58
|
+
pluginId,
|
|
59
|
+
remoteUrl: url,
|
|
60
|
+
cause: new Error(`Remote not available: ${error}`),
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
// Register remote
|
|
64
|
+
const remoteName = pluginId
|
|
65
|
+
.toLowerCase()
|
|
66
|
+
.replace("@", "")
|
|
67
|
+
.replace("/", "_");
|
|
68
|
+
yield* Effect.try({
|
|
69
|
+
try: () => mf.registerRemotes([{ name: remoteName, entry: url }]),
|
|
70
|
+
catch: (error) => new ModuleFederationError({
|
|
71
|
+
pluginId,
|
|
72
|
+
remoteUrl: url,
|
|
73
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
}),
|
|
77
|
+
loadRemoteConstructor: (pluginId, url) => Effect.gen(function* () {
|
|
78
|
+
const remoteName = pluginId
|
|
79
|
+
.toLowerCase()
|
|
80
|
+
.replace("@", "")
|
|
81
|
+
.replace("/", "_");
|
|
82
|
+
const modulePath = `${remoteName}/plugin`;
|
|
83
|
+
return yield* Effect.tryPromise({
|
|
84
|
+
try: async () => {
|
|
85
|
+
const container = await mf.loadRemote(modulePath);
|
|
86
|
+
if (!container) {
|
|
87
|
+
throw new Error(`No container returned for ${modulePath}`);
|
|
88
|
+
}
|
|
89
|
+
const Constructor = typeof container === "function"
|
|
90
|
+
? container
|
|
91
|
+
: container?.default;
|
|
92
|
+
if (!Constructor || typeof Constructor !== "function") {
|
|
93
|
+
throw new Error(`No valid constructor found. Container type: ${typeof container}`);
|
|
94
|
+
}
|
|
95
|
+
return Constructor;
|
|
96
|
+
},
|
|
97
|
+
catch: (error) => new ModuleFederationError({
|
|
98
|
+
pluginId,
|
|
99
|
+
remoteUrl: url,
|
|
100
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
101
|
+
}),
|
|
102
|
+
});
|
|
103
|
+
}),
|
|
104
|
+
};
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
export const ModuleFederationServiceTag = ModuleFederationService;
|
|
108
|
+
export const ModuleFederationServiceLive = ModuleFederationService.Live;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import { PluginRuntimeError } from "../errors";
|
|
3
|
+
import type { PluginConstructor } from "../types";
|
|
4
|
+
import { ModuleFederationService } from "./module-federation.service";
|
|
5
|
+
export interface IPluginCacheService {
|
|
6
|
+
readonly getCachedConstructor: (cacheKey: string) => Effect.Effect<PluginConstructor, PluginRuntimeError>;
|
|
7
|
+
readonly invalidate: (cacheKey: string) => Effect.Effect<void>;
|
|
8
|
+
}
|
|
9
|
+
declare const PluginCacheService_base: import("effect/Context").TagClass<PluginCacheService, "PluginCacheService", IPluginCacheService> & Effect.Tag.Proxy<PluginCacheService, IPluginCacheService> & {
|
|
10
|
+
use: <X>(body: (_: IPluginCacheService) => X) => [X] extends [Effect.Effect<infer A, infer E, infer R>] ? Effect.Effect<A, E, R | PluginCacheService> : [X] extends [PromiseLike<infer A_1>] ? Effect.Effect<A_1, import("effect/Cause").UnknownException, PluginCacheService> : Effect.Effect<X, never, PluginCacheService>;
|
|
11
|
+
};
|
|
12
|
+
export declare class PluginCacheService extends PluginCacheService_base {
|
|
13
|
+
static Live: Layer.Layer<PluginCacheService, never, ModuleFederationService>;
|
|
14
|
+
}
|
|
15
|
+
export declare const PluginCacheServiceTag: typeof PluginCacheService;
|
|
16
|
+
export declare const PluginCacheServiceLive: Layer.Layer<PluginCacheService, never, ModuleFederationService>;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=plugin-cache.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-cache.service.d.ts","sourceRoot":"","sources":["../../../src/runtime/services/plugin-cache.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AAEtE,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,oBAAoB,EAAE,CAC7B,QAAQ,EAAE,MAAM,KACb,MAAM,CAAC,MAAM,CAAC,iBAAiB,EAAE,kBAAkB,CAAC,CAAC;IAC1D,QAAQ,CAAC,UAAU,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;CAChE;;;;AAED,qBAAa,kBAAmB,SAAQ,uBAGrC;IACD,MAAM,CAAC,IAAI,kEAoET;CACH;AAED,eAAO,MAAM,qBAAqB,2BAAqB,CAAC;AACxD,eAAO,MAAM,sBAAsB,iEAA0B,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Cache, Duration, Effect, Layer } from "effect";
|
|
2
|
+
import { PluginRuntimeError } from "../errors";
|
|
3
|
+
import { ModuleFederationService } from "./module-federation.service";
|
|
4
|
+
export class PluginCacheService extends Effect.Tag("PluginCacheService")() {
|
|
5
|
+
static Live = Layer.effect(PluginCacheService, Effect.gen(function* () {
|
|
6
|
+
const mfService = yield* ModuleFederationService;
|
|
7
|
+
const cache = yield* Cache.make({
|
|
8
|
+
capacity: 50,
|
|
9
|
+
timeToLive: Duration.minutes(30),
|
|
10
|
+
lookup: (cacheKey) => {
|
|
11
|
+
// Parse cache key: "pluginId:url"
|
|
12
|
+
const colonIndex = cacheKey.indexOf(":");
|
|
13
|
+
if (colonIndex === -1) {
|
|
14
|
+
return Effect.fail(new PluginRuntimeError({
|
|
15
|
+
pluginId: cacheKey,
|
|
16
|
+
operation: "cache-lookup",
|
|
17
|
+
cause: new Error(`Invalid cache key format: ${cacheKey}`),
|
|
18
|
+
retryable: false,
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
const pluginId = cacheKey.substring(0, colonIndex);
|
|
22
|
+
const url = cacheKey.substring(colonIndex + 1);
|
|
23
|
+
return Effect.gen(function* () {
|
|
24
|
+
yield* mfService.registerRemote(pluginId, url).pipe(Effect.mapError((mfError) => new PluginRuntimeError({
|
|
25
|
+
pluginId,
|
|
26
|
+
operation: "register-remote",
|
|
27
|
+
cause: mfError.cause,
|
|
28
|
+
retryable: true,
|
|
29
|
+
})));
|
|
30
|
+
const ctor = yield* mfService.loadRemoteConstructor(pluginId, url).pipe(Effect.mapError((mfError) => new PluginRuntimeError({
|
|
31
|
+
pluginId,
|
|
32
|
+
operation: "load-remote",
|
|
33
|
+
cause: mfError.cause,
|
|
34
|
+
retryable: true,
|
|
35
|
+
})));
|
|
36
|
+
return {
|
|
37
|
+
ctor,
|
|
38
|
+
metadata: {
|
|
39
|
+
pluginId,
|
|
40
|
+
version: "unknown", // Will be resolved from registry
|
|
41
|
+
description: "Loaded from remote",
|
|
42
|
+
type: "unknown",
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
getCachedConstructor: (cacheKey) => cache.get(cacheKey),
|
|
50
|
+
invalidate: (cacheKey) => cache.invalidate(cacheKey),
|
|
51
|
+
};
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
export const PluginCacheServiceTag = PluginCacheService;
|
|
55
|
+
export const PluginCacheServiceLive = PluginCacheService.Live;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import { PluginLoggerTag, type PluginRegistry } from "../../plugin";
|
|
3
|
+
import { PluginRuntimeError } from "../errors";
|
|
4
|
+
import type { AnyPlugin, InitializedPlugin, PluginConstructor, PluginInstance, SecretsConfig } from "../types";
|
|
5
|
+
import { PluginCacheService } from "./plugin-cache.service";
|
|
6
|
+
import { SecretsService } from "./secrets.service";
|
|
7
|
+
export interface IPluginService {
|
|
8
|
+
readonly loadPlugin: (pluginId: string) => Effect.Effect<PluginConstructor, PluginRuntimeError>;
|
|
9
|
+
readonly instantiatePlugin: <T extends AnyPlugin>(pluginConstructor: PluginConstructor) => Effect.Effect<PluginInstance<T>, PluginRuntimeError>;
|
|
10
|
+
readonly initializePlugin: <T extends AnyPlugin>(pluginInstance: PluginInstance<T>, config: unknown) => Effect.Effect<InitializedPlugin<T>, PluginRuntimeError>;
|
|
11
|
+
readonly executePlugin: <T extends AnyPlugin>(initializedPlugin: InitializedPlugin<T>, input: unknown) => Effect.Effect<any, PluginRuntimeError>;
|
|
12
|
+
}
|
|
13
|
+
declare const PluginService_base: import("effect/Context").TagClass<PluginService, "PluginService", IPluginService> & Effect.Tag.Proxy<PluginService, IPluginService> & {
|
|
14
|
+
use: <X>(body: (_: IPluginService) => X) => [X] extends [Effect.Effect<infer A, infer E, infer R>] ? Effect.Effect<A, E, R | PluginService> : [X] extends [PromiseLike<infer A_1>] ? Effect.Effect<A_1, import("effect/Cause").UnknownException, PluginService> : Effect.Effect<X, never, PluginService>;
|
|
15
|
+
};
|
|
16
|
+
export declare class PluginService extends PluginService_base {
|
|
17
|
+
static Live: (registry: PluginRegistry, secrets: SecretsConfig) => Layer.Layer<PluginService, never, PluginLoggerTag | PluginCacheService | SecretsService>;
|
|
18
|
+
}
|
|
19
|
+
export declare const PluginServiceTag: typeof PluginService;
|
|
20
|
+
export declare const PluginServiceLive: (registry: PluginRegistry, secrets: SecretsConfig) => Layer.Layer<PluginService, never, PluginLoggerTag | PluginCacheService | SecretsService>;
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=plugin.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.service.d.ts","sourceRoot":"","sources":["../../../src/runtime/services/plugin.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,MAAM,EAAE,KAAK,EAAY,MAAM,QAAQ,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,KAAK,EACV,SAAS,EACT,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,aAAa,EACd,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,UAAU,EAAE,CACnB,QAAQ,EAAE,MAAM,KACb,MAAM,CAAC,MAAM,CAAC,iBAAiB,EAAE,kBAAkB,CAAC,CAAC;IAC1D,QAAQ,CAAC,iBAAiB,EAAE,CAAC,CAAC,SAAS,SAAS,EAC9C,iBAAiB,EAAE,iBAAiB,KACjC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC;IAC1D,QAAQ,CAAC,gBAAgB,EAAE,CAAC,CAAC,SAAS,SAAS,EAC7C,cAAc,EAAE,cAAc,CAAC,CAAC,CAAC,EACjC,MAAM,EAAE,OAAO,KACZ,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC;IAC7D,QAAQ,CAAC,aAAa,EAAE,CAAC,CAAC,SAAS,SAAS,EAC1C,iBAAiB,EAAE,iBAAiB,CAAC,CAAC,CAAC,EACvC,KAAK,EAAE,OAAO,KACX,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,kBAAkB,CAAC,CAAC;CAC7C;;;;AAED,qBAAa,aAAc,SAAQ,kBAGhC;IACD,MAAM,CAAC,IAAI,GAAI,UAAU,cAAc,EAAE,SAAS,aAAa,8FA0O3D;CACL;AAED,eAAO,MAAM,gBAAgB,sBAAgB,CAAC;AAC9C,eAAO,MAAM,iBAAiB,aA9OH,cAAc,WAAW,aAAa,6FA8Od,CAAC"}
|