UNPKG

@withstudiocms/effect

Version:

Effect-TS Utilities for Astro

54 lines (53 loc) 1.6 kB
import { scrypt } from "node:crypto"; import { Brand, Context, Data, Effect, Layer } from "./effect.js"; class ScryptError extends Data.TaggedError("ScryptError") { } const ScryptConfigOptions = Brand.nominal(); class ScryptConfig extends Context.Tag("ScryptConfig")() { static Make = (opts) => Layer.succeed(this, ScryptConfigOptions(opts)); } class Scrypt extends Effect.Service()("Scrypt", { effect: Effect.gen(function* () { const config = yield* ScryptConfig; const run = (password) => Effect.async((resume) => { try { scrypt( password, config.encryptionKey, config.keylen, config.options, (err, derivedKey) => { if (err) { resume(Effect.fail(new ScryptError({ error: err }))); } else { resume(Effect.succeed(derivedKey)); } } ); } catch (error) { resume(Effect.fail(new ScryptError({ error }))); } }); return { run }; }) }) { /** * This is used to create a configuration layer for the Scrypt service. */ static makeConfig = (opts) => ScryptConfig.Make(opts); /** * Creates a live instance of the Scrypt service with the specified configuration options. * * @param opts - The configuration options for the Scrypt service. * @returns Layer that provides the Scrypt service with the specified configuration. */ static makeLive = (opts) => Layer.provide(this.Default, this.makeConfig(opts)); } export { Scrypt, ScryptConfig, ScryptConfigOptions, ScryptError };