72 lines
2.8 KiB
TypeScript
72 lines
2.8 KiB
TypeScript
/**
|
|
* The server's implementation of `ProviderHost.ProviderHost`, the only server surface
|
|
* provider drivers and adapters may use.
|
|
*
|
|
* @module provider/ProviderHostLive
|
|
*/
|
|
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
|
|
import { ProviderCredentialError } from "@t3tools/provider-core/server/errors";
|
|
import * as Crypto from "effect/Crypto";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
|
|
import { resolveAttachmentPath } from "../attachmentStore.ts";
|
|
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
|
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
|
|
import * as ServerConfig from "../config.ts";
|
|
import * as ServerSettings from "../serverSettings.ts";
|
|
import * as ProviderCredentialStore from "./ProviderCredentialStore.ts";
|
|
|
|
export const layer = Layer.effect(
|
|
ProviderHost.ProviderHost,
|
|
Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const serverSettings = yield* ServerSettings.ServerSettingsService;
|
|
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
|
|
const secrets = yield* ServerSecretStore.ServerSecretStore;
|
|
const crypto = yield* Crypto.Crypto;
|
|
return ProviderHost.ProviderHost.of({
|
|
paths: {
|
|
cwd: config.cwd,
|
|
baseDir: config.baseDir,
|
|
stateDir: config.stateDir,
|
|
providerStatusCacheDir: config.providerStatusCacheDir,
|
|
attachmentsDir: config.attachmentsDir,
|
|
},
|
|
settings: {
|
|
get: serverSettings.getSettings,
|
|
withSnapshot: serverSettings.withSettingsSnapshot,
|
|
changes: serverSettings.streamChanges,
|
|
subscribe: serverSettings.subscribeChanges,
|
|
},
|
|
shouldRunBackgroundWork: backgroundPolicy.shouldRunScopeWork,
|
|
resolveAttachmentPath: (attachment) =>
|
|
resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment }),
|
|
credentials: (namespace, bindingId) =>
|
|
ProviderCredentialStore.make(namespace, bindingId).pipe(
|
|
Effect.map((store) => ({
|
|
binding: store.binding,
|
|
get: store.get.pipe(
|
|
Effect.mapError((cause) => new ProviderCredentialError({ operation: "get", cause })),
|
|
),
|
|
set: (credentials: Uint8Array) =>
|
|
store
|
|
.set(credentials)
|
|
.pipe(
|
|
Effect.mapError(
|
|
(cause) => new ProviderCredentialError({ operation: "set", cause }),
|
|
),
|
|
),
|
|
remove: store.remove.pipe(
|
|
Effect.mapError(
|
|
(cause) => new ProviderCredentialError({ operation: "remove", cause }),
|
|
),
|
|
),
|
|
})),
|
|
Effect.provideService(ServerSecretStore.ServerSecretStore, secrets),
|
|
Effect.provideService(Crypto.Crypto, crypto),
|
|
),
|
|
});
|
|
}),
|
|
);
|