Skip to content
Source
Sign inSign up

ProviderHostLive.ts

76 lines3.2 KB

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