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 ServerActivation from "../serverActivation.ts";
12import * as ServerSettings from "../serverSettings.ts";
13import * as ProviderCredentialStore from "./ProviderCredentialStore.ts";
14
15export const layer = Layer.effect(
16 ProviderHost.ProviderHost,
17 Effect.gen(function* () {
18 const config = yield* ServerConfig.ServerConfig;
19 const serverSettings = yield* ServerSettings.ServerSettingsService;
20 const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
21 const secrets = yield* ServerSecretStore.ServerSecretStore;
22 const crypto = yield* Crypto.Crypto;
23 const backgroundStart = yield* ServerActivation.ServerBackgroundStart;
24 return ProviderHost.ProviderHost.of({
25 paths: {
26 cwd: config.cwd,
27 baseDir: config.baseDir,
28 stateDir: config.stateDir,
29 providerStatusCacheDir: config.providerStatusCacheDir,
30 attachmentsDir: config.attachmentsDir,
31 },
32 settings: {
33 get: serverSettings.getSettings,
34 withSnapshot: serverSettings.withSettingsSnapshot,
35 changes: serverSettings.streamChanges,
36 subscribe: serverSettings.subscribeChanges,
37 },
38 shouldRunBackgroundWork: backgroundPolicy.shouldRunScopeWork,
39 ...(backgroundStart === undefined
40 ? {}
41 : { awaitStartupProbe: backgroundStart.awaitFirstClient }),
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);