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

ProviderHostLive.ts

86 lines3.6 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";
7import * as Option from "effect/Option";
8​
9import { resolveAttachmentPath } from "../attachmentStore.ts";
10import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
11import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
12import * as ServerConfig from "../config.ts";
13import * as ServerLifecycleEvents from "../serverLifecycleEvents.ts";
14import * as ServerSettings from "../serverSettings.ts";
15import * as DocumentText from "./DocumentText.ts";
16import * as ProviderCredentialStore from "./ProviderCredentialStore.ts";
17​
18export const layer = Layer.effect(
19 ProviderHost.ProviderHost,
20 Effect.gen(function* () {
21 const config = yield* ServerConfig.ServerConfig;
22 const serverSettings = yield* ServerSettings.ServerSettingsService;
23 const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
24 const secrets = yield* ServerSecretStore.ServerSecretStore;
25 const digest = yield* Digest.Digest;
26 const fileSystem = yield* FileSystem.FileSystem;
27 // Optional so standalone provider layers in tests probe at once.
28 const lifecycleEvents = yield* Effect.serviceOption(
29 ServerLifecycleEvents.ServerLifecycleEvents,
30 );
31 return ProviderHost.ProviderHost.of({
32 paths: {
33 cwd: config.cwd,
34 baseDir: config.baseDir,
35 stateDir: config.stateDir,
36 providerStatusCacheDir: config.providerStatusCacheDir,
37 attachmentsDir: config.attachmentsDir,
38 },
39 settings: {
40 get: serverSettings.getSettings,
41 withSnapshot: serverSettings.withSettingsSnapshot,
42 changes: serverSettings.streamChanges,
43 subscribe: serverSettings.subscribeChanges,
44 },
45 shouldRunBackgroundWork: backgroundPolicy.shouldRunScopeWork,
46 ...Option.match(lifecycleEvents, {
47 onNone: () => ({}),
48 onSome: (events) => ({ awaitServerReady: events.awaitReady }),
49 }),
50 resolveAttachmentPath: (attachment) =>
51 resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment }),
52 // DocumentText owns the path-resolution span.
53 resolveExtractedTextPath: Effect.fnUntraced(
54 function* (path) {
55 return yield* DocumentText.resolveExtractedTextPath(path);
56 },
57 Effect.provideService(FileSystem.FileSystem, fileSystem),
58 ),
59 credentials: Effect.fn("ProviderHost.credentials")(function (namespace, bindingId) {
60 return ProviderCredentialStore.make(namespace, bindingId).pipe(
61 Effect.map((store) => ({
62 binding: store.binding,
63 get: store.get.pipe(
64 Effect.mapError((cause) => new ProviderCredentialError({ operation: "get", cause })),
65 ),
66 set: (credentials: Uint8Array) =>
67 store
68 .set(credentials)
69 .pipe(
70 Effect.mapError(
71 (cause) => new ProviderCredentialError({ operation: "set", cause }),
72 ),
73 ),
74 remove: store.remove.pipe(
75 Effect.mapError(
76 (cause) => new ProviderCredentialError({ operation: "remove", cause }),
77 ),
78 ),
79 })),
80 Effect.provideService(ServerSecretStore.ServerSecretStore, secrets),
81 Effect.provideService(Digest.Digest, digest),
82 );
83 }),
84 });
85 }),
86);