Open History to see the last commit
1/**
2 * The server's implementation of `ProviderHost`, the only server surface
3 * provider drivers and adapters may use.
4 *
5 * @module provider/ProviderHostLive
6 */
7import { ProviderHost } from "@t3tools/provider-core/server/ProviderHost";
8import { ProviderCredentialError } from "@t3tools/provider-core/server/errors";
9import * as Crypto from "effect/Crypto";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12
13import { resolveAttachmentPath } from "../attachmentStore.ts";
14import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
15import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
16import * as ServerConfig from "../config.ts";
17import * as ServerSettings from "../serverSettings.ts";
18import * as ProviderCredentialStore from "./ProviderCredentialStore.ts";
19
20export const layer = Layer.effect(
21 ProviderHost,
22 Effect.gen(function* () {
23 const config = yield* ServerConfig.ServerConfig;
24 const serverSettings = yield* ServerSettings.ServerSettingsService;
25 const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
26 const secrets = yield* ServerSecretStore.ServerSecretStore;
27 const crypto = yield* Crypto.Crypto;
28 return ProviderHost.of({
29 paths: {
30 cwd: config.cwd,
31 baseDir: config.baseDir,
32 stateDir: config.stateDir,
33 providerStatusCacheDir: config.providerStatusCacheDir,
34 attachmentsDir: config.attachmentsDir,
35 },
36 settings: {
37 get: serverSettings.getSettings,
38 changes: serverSettings.streamChanges,
39 subscribe: serverSettings.subscribeChanges,
40 },
41 shouldRunBackgroundWork: backgroundPolicy.shouldRunScopeWork,
42 resolveAttachmentPath: (attachment) =>
43 resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment }),
44 credentials: (namespace, bindingId) =>
45 ProviderCredentialStore.make(namespace, bindingId).pipe(
46 Effect.map((store) => ({
47 binding: store.binding,
48 get: store.get.pipe(
49 Effect.mapError((cause) => new ProviderCredentialError({ operation: "get", cause })),
50 ),
51 set: (credentials: Uint8Array) =>
52 store
53 .set(credentials)
54 .pipe(
55 Effect.mapError(
56 (cause) => new ProviderCredentialError({ operation: "set", cause }),
57 ),
58 ),
59 remove: store.remove.pipe(
60 Effect.mapError(
61 (cause) => new ProviderCredentialError({ operation: "remove", cause }),
62 ),
63 ),
64 })),
65 Effect.provideService(ServerSecretStore.ServerSecretStore, secrets),
66 Effect.provideService(Crypto.Crypto, crypto),
67 ),
68 });
69 }),
70);