Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderHostLive.ts

72 lines3.0 KB

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 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 crypto = yield* Crypto.Crypto;
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 resolveExtractedTextPath: (path) =>
43 DocumentText.resolveExtractedTextPath(path).pipe(
44 Effect.provideService(FileSystem.FileSystem, fileSystem),
45 ),
46 credentials: (namespace, bindingId) =>
47 ProviderCredentialStore.make(namespace, bindingId).pipe(
48 Effect.map((store) => ({
49 binding: store.binding,
50 get: store.get.pipe(
51 Effect.mapError((cause) => new ProviderCredentialError({ operation: "get", cause })),
52 ),
53 set: (credentials: Uint8Array) =>
54 store
55 .set(credentials)
56 .pipe(
57 Effect.mapError(
58 (cause) => new ProviderCredentialError({ operation: "set", cause }),
59 ),
60 ),
61 remove: store.remove.pipe(
62 Effect.mapError(
63 (cause) => new ProviderCredentialError({ operation: "remove", cause }),
64 ),
65 ),
66 })),
67 Effect.provideService(ServerSecretStore.ServerSecretStore, secrets),
68 Effect.provideService(Crypto.Crypto, crypto),
69 ),
70 });
71 }),
72);