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