better-effect 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -6
- package/dist/adapters/iti.d.mts +1 -1
- package/dist/adapters/iti.d.mts.map +1 -1
- package/dist/adapters/iti.mjs +4 -3
- package/dist/adapters/iti.mjs.map +1 -1
- package/dist/errors-GR3K_nRu.mjs.map +1 -1
- package/dist/index-DMfjhNR_.d.mts +500 -0
- package/dist/index-DMfjhNR_.d.mts.map +1 -0
- package/dist/index.d.mts +57 -61
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +87 -112
- package/dist/index.mjs.map +1 -1
- package/dist/{internal-identity-BnZC3Au-.mjs → internal-identity-C6Awrc33.mjs} +4 -3
- package/dist/internal-identity-C6Awrc33.mjs.map +1 -0
- package/dist/runtime-CDcCF5cb.mjs +10 -0
- package/dist/runtime-CDcCF5cb.mjs.map +1 -0
- package/dist/testing.d.mts +1 -1
- package/dist/testing.d.mts.map +1 -1
- package/dist/testing.mjs +3 -2
- package/dist/testing.mjs.map +1 -1
- package/package.json +13 -5
- package/dist/index-D77AvuBl.d.mts +0 -510
- package/dist/index-D77AvuBl.d.mts.map +0 -1
- package/dist/internal-identity-BnZC3Au-.mjs.map +0 -1
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["storage","Constructor","SCOPE_SUCCESS","SCOPE_SUCCESS","next"],"sources":["../src/service/runtime.ts","../src/service/service.ts","../src/layer/internal.ts","../src/layer/layer.ts","../src/scope/errors.ts","../src/scope/disposable.ts","../src/scope/runtime.ts","../src/scope/internal.ts","../src/scope/scope.ts","../src/effect/combinators.ts","../src/effect/effect.ts","../src/function/pipe.ts","../src/resource/errors.ts","../src/resource/internal.ts","../src/resource/resource.ts","../src/runtime/outcome.ts","../src/layer/runtime.ts","../src/runtime/runtime.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\n\nimport { ServiceRuntimeNotConfiguredError } from './errors'\n\nimport type { AnyServiceToken } from './types'\n\n/** Resolves class-backed Service tokens for a runtime execution. */\nexport interface ServiceResolver {\n /** Resolve a token to its corresponding Service instance. */\n resolve<T extends AnyServiceToken>(token: T): InstanceType<T> | PromiseLike<InstanceType<T>>\n}\n\nconst storage = new AsyncLocalStorage<ServiceResolver>()\n\n/** Provides the resolver context used by Service tokens during execution. */\nexport class ServiceRuntime {\n /**\n * Run a callback with a resolver available to `yield* Service` expressions.\n *\n * The context is scoped to the callback and is restored afterward.\n *\n * @example\n * ```ts\n * const value = ServiceRuntime.run(resolver, () => {\n * return ServiceRuntime.resolve(Database)\n * })\n * ```\n */\n static run<A>(resolver: ServiceResolver, program: () => A): A {\n return storage.run(resolver, program)\n }\n\n /** Return the resolver active in the current execution context. */\n static current(): ServiceResolver {\n const resolver = storage.getStore()\n\n if (!resolver) {\n throw new ServiceRuntimeNotConfiguredError()\n }\n\n return resolver\n }\n\n /** Resolve a Service token using the active resolver. */\n static async resolve<T extends AnyServiceToken>(token: T): Promise<InstanceType<T>> {\n const resolver = ServiceRuntime.current()\n\n return await resolver.resolve(token)\n }\n}\n","import { ServiceRuntime } from './runtime'\n\nimport type { ServiceRequirement } from '../effect/types'\n\nimport type { ServiceToken } from './types'\n\ntype ServiceTagLiteral<Tag extends string> = string extends Tag\n ? never\n : Tag extends ''\n ? never\n : Tag\n\n/**\n * Declare a class-backed Service with a stable string-literal identity.\n *\n * The returned class is simultaneously the implementation type, the runtime\n * dependency token, and the value yielded by `yield*` in an Effect generator.\n * The explicit self type preserves exact instance inference, while the second\n * call captures the tag as a literal for Layer composition and diagnostics.\n *\n * @example\n * ```ts\n * class Database extends Service<Database>()('Database') {\n * query(): string {\n * return 'ok'\n * }\n * }\n *\n * const database = yield* Database\n * database.query()\n * ```\n *\n * @typeParam Self The instance type implemented by the declared Service.\n */\nexport function Service<Self>() {\n return function <const Tag extends string>(tag: ServiceTagLiteral<Tag>) {\n if (tag.length === 0) {\n throw new TypeError('Service tags must not be empty')\n }\n\n abstract class BaseService {\n /** The stable logical identity used by Layers and resolver backends. */\n static readonly serviceTag: Tag = tag\n\n /**\n * Type-check a structural implementation of this Service.\n *\n * This is an identity helper. It returns the supplied value unchanged\n * and does not invoke a constructor or modify its prototype.\n *\n * @example\n * ```ts\n * class Database extends Service<Database>()('Database') {\n * query(sql: string): string {\n * return sql\n * }\n * }\n *\n * const database = Database.of({\n * query: (sql) => `Result: ${sql}`\n * })\n *\n * database.query('SELECT 1')\n * // 'Result: SELECT 1'\n * // database is the original object, not an instance of Database\n * ```\n */\n static of(this: void, implementation: Self): Self {\n return implementation\n }\n\n /** Resolve this Service from the resolver active in the current runtime. */\n // oxlint-disable-next-line require-yield\n static async *[Symbol.asyncIterator](\n this: ServiceToken<Tag, Self>\n ): AsyncGenerator<ServiceRequirement<ServiceToken<Tag, Self>>, Self, unknown> {\n return await ServiceRuntime.resolve(this)\n }\n }\n\n return BaseService\n }\n}\n","import { LayerGeneratorYieldError } from './errors'\n\nimport type { ServiceRequirement } from '../effect/types'\nimport type { AnyServiceToken, ServiceClass } from '../service'\n\nimport type { LayerGenerator } from './types'\n\nexport const runLayerGenerator = async <\n S extends ServiceClass<any, any>,\n Yield extends ServiceRequirement<AnyServiceToken>\n>(\n service: S,\n factory: LayerGenerator<S, Yield>\n): Promise<InstanceType<S>> => {\n const iterator = factory()\n\n const state = await iterator.next()\n\n if (!state.done) {\n try {\n await iterator.return(undefined as InstanceType<S>)\n } finally {\n // oxlint-disable-next-line no-unsafe-finally\n throw new LayerGeneratorYieldError(service)\n }\n }\n\n return state.value\n}\n","import type { ServiceRequirement } from '../effect/types'\nimport type { AnyServiceToken, ServiceClass, ServiceRequirements } from '../service'\nimport type { ScopeOutcome } from '../scope'\nimport type { MaybePromise } from '../utils/types'\n\nimport { DuplicateServiceError, ServiceTagCollisionError } from './errors'\n\nimport { runLayerGenerator } from './internal'\n\nimport type {\n LayerGenerator,\n LayerGeneratorRequirements,\n LayerRegistration,\n LayerSpec\n} from './types'\n\nimport type { AnyLayerSpec } from './types'\n\nimport type { OverrideLayerCollisions, OverrideLayerSpecs } from './inference'\n\ndeclare const LayerTypeId: unique symbol\ndeclare const LayerCollisionTypeId: unique symbol\n\ninterface LayerProvider extends LayerRegistration {\n readonly release?: (instance: unknown, outcome: ScopeOutcome) => MaybePromise<void>\n}\n\n/** A Service class whose constructor can be called without arguments. */\ntype DefaultConstructibleServiceClass<Tag extends string = string, Instance = any> = ServiceClass<\n Tag,\n Instance\n> &\n (new () => Instance)\n\n/**\n * Declarative collection of Service providers.\n *\n * A Layer describes how to acquire implementations; it does not execute\n * providers until a `Runtime` is created. Use `merge` to compose distinct\n * providers and `override` when replacing an existing provider intentionally.\n *\n * @example\n * ```ts\n * const AppLive = Layer.merge(\n * Layer.succeed(Database, database),\n * Layer.make(UserRepository)\n * )\n *\n * const runtime = await Runtime.make(AppLive, backend)\n * ```\n */\nexport class Layer<\n Specs extends AnyLayerSpec = AnyLayerSpec,\n Collisions extends AnyServiceToken = never\n> {\n declare readonly [LayerTypeId]: Specs\n declare readonly [LayerCollisionTypeId]: Collisions\n\n /** The provider registrations retained by this Layer. */\n readonly providers: readonly LayerProvider[]\n\n private constructor(providers: readonly LayerProvider[]) {\n this.providers = Object.freeze([...providers])\n }\n\n /**\n * Create a Layer that lazily acquires a Service instance.\n *\n * When the acquire callback is omitted, the Service must be constructible\n * without required constructor arguments and is instantiated with `new`.\n * Supplying an acquire callback remains available for custom construction.\n *\n * The acquire callback runs when the provider is first resolved by a\n * Runtime. Dependencies declared by Effect-returning Service methods are\n * tracked in the Layer's type.\n *\n * @example\n * ```ts\n * const DatabaseLive = Layer.make(Database)\n * ```\n *\n * @example\n * ```ts\n * const DatabaseLive = Layer.make(Database, () => new Database(config))\n * ```\n */\n static make<S extends DefaultConstructibleServiceClass<any, any>>(\n service: S\n ): Layer<LayerSpec<S, ServiceRequirements<S>>>\n\n static make<S extends ServiceClass<any, any>>(\n service: S,\n acquire: () => MaybePromise<InstanceType<S>>\n ): Layer<LayerSpec<S, ServiceRequirements<S>>>\n\n static make<S extends ServiceClass<any, any>>(\n service: S,\n acquire?: () => MaybePromise<InstanceType<S>>\n ): Layer<LayerSpec<S, ServiceRequirements<S>>> {\n const defaultAcquire = (): InstanceType<S> => {\n const Constructor = service as new () => InstanceType<S>\n\n return new Constructor()\n }\n\n return new Layer([\n {\n service,\n acquire: acquire ?? defaultAcquire\n }\n ])\n }\n\n /**\n * Create a Layer from an already-constructed Service instance.\n *\n * The instance is returned as-is whenever the Service is resolved.\n *\n * @example\n * ```ts\n * const DatabaseLive = Layer.succeed(Database, database)\n * ```\n */\n static succeed<S extends ServiceClass<any, any>>(\n service: S,\n instance: InstanceType<S>\n ): Layer<LayerSpec<S, ServiceRequirements<S>>> {\n return Layer.make(service, () => instance)\n }\n\n /**\n * Define a provider with Runtime-root cleanup.\n *\n * The release callback intentionally keeps its compatibility-friendly\n * one-argument shape and runs when the owning Runtime is disposed. Use\n * `scopedGen` when acquisition needs contextual Services or cleanup needs\n * `ScopeOutcome`.\n *\n * @example\n * ```ts\n * const DatabaseLive = Layer.scoped(\n * Database,\n * () => openDatabase(),\n * (database) => database.close()\n * )\n * ```\n */\n static scoped<S extends ServiceClass<any, any>>(\n service: S,\n acquire: () => MaybePromise<InstanceType<S>>,\n release: (instance: InstanceType<S>) => MaybePromise<void>\n ): Layer<LayerSpec<S, ServiceRequirements<S>>> {\n return new Layer([\n {\n service,\n acquire,\n\n release: (instance) => release(instance as InstanceType<S>)\n }\n ])\n }\n\n /**\n * Define a provider whose acquisition can yield contextual Services.\n *\n * The release callback receives the acquired instance and the final\n * `ScopeOutcome` selected by the owning Runtime.\n *\n * @example\n * ```ts\n * const RepositoryLive = Layer.scopedGen(\n * UserRepository,\n * async function* () {\n * const database = yield* Database\n * return new UserRepository(database)\n * },\n * (repository, outcome) => repository.close(outcome)\n * )\n * ```\n */\n static scopedGen<\n S extends ServiceClass<any, any>,\n Yield extends ServiceRequirement<AnyServiceToken>\n >(\n service: S,\n factory: LayerGenerator<S, Yield>,\n release: (instance: InstanceType<S>, outcome: ScopeOutcome) => MaybePromise<void>\n ): Layer<LayerSpec<S, LayerGeneratorRequirements<S, Yield>>> {\n return new Layer([\n {\n service,\n acquire: () => runLayerGenerator(service, factory),\n release: (instance, outcome) => release(instance as InstanceType<S>, outcome)\n }\n ])\n }\n\n /**\n * Define a provider whose acquisition can yield contextual Services.\n *\n * Unlike `scopedGen`, this variant has no release callback. Use it for\n * providers whose lifetime is managed elsewhere or that need no cleanup.\n *\n * @example\n * ```ts\n * const RepositoryLive = Layer.gen(UserRepository, async function* () {\n * const database = yield* Database\n * return new UserRepository(database)\n * })\n * ```\n */\n static gen<S extends ServiceClass<any, any>, Yield extends ServiceRequirement<AnyServiceToken>>(\n service: S,\n factory: LayerGenerator<S, Yield>\n ): Layer<LayerSpec<S, LayerGeneratorRequirements<S, Yield>>> {\n return Layer.make(service, () => runLayerGenerator(service, factory))\n }\n\n /**\n * Compose Layers without replacing providers.\n *\n * Each Service tag may appear only once. Duplicate tags are rejected at\n * runtime; use `override` when replacement is intentional.\n *\n * @example\n * ```ts\n * const AppLive = Layer.merge(DatabaseLive, RepositoryLive)\n * ```\n */\n static merge<const Layers extends readonly Layer<any, any>[]>(\n ...layers: Layers\n ): Layer<\n Layers[number] extends Layer<infer Specs, any> ? Specs : never,\n Layers[number] extends Layer<any, infer Collisions> ? Collisions : never\n > {\n const providers = new Map<string, LayerProvider>()\n\n for (const layer of layers) {\n for (const provider of layer.providers) {\n const service = provider.service\n\n const existing = providers.get(service.serviceTag)\n\n if (existing) {\n if (existing.service !== service) {\n throw new ServiceTagCollisionError(existing.service, service)\n }\n\n throw new DuplicateServiceError(service)\n }\n\n providers.set(service.serviceTag, provider)\n }\n }\n\n return new Layer([...providers.values()])\n }\n\n /**\n * Replace providers in a base Layer, using tag identity and compatible\n * instance contracts.\n *\n * Overrides are applied from left to right; the last compatible provider for\n * a tag wins. Incompatible same-tag replacements remain visible as a type\n * diagnostic and cannot be passed as a complete Layer.\n *\n * @example\n * ```ts\n * const TestLive = Layer.override(AppLive, Layer.succeed(Database, fakeDb))\n * ```\n */\n static override<Base extends Layer<any, any>, const Overrides extends readonly Layer<any, any>[]>(\n base: Base,\n ...overrides: Overrides\n ): Layer<\n OverrideLayerSpecs<Base extends Layer<infer Specs, any> ? Specs : never, Overrides>,\n | (Base extends Layer<any, infer Collisions> ? Collisions : never)\n | (Overrides[number] extends Layer<any, infer Collisions> ? Collisions : never)\n | OverrideLayerCollisions<Base extends Layer<infer Specs, any> ? Specs : never, Overrides>\n > {\n const providers = new Map<string, LayerProvider>()\n\n for (const provider of base.providers) {\n providers.set(provider.service.serviceTag, provider)\n }\n\n for (const layer of overrides) {\n for (const provider of layer.providers) {\n providers.set(provider.service.serviceTag, provider)\n }\n }\n\n return new Layer([...providers.values()]) as Layer<\n OverrideLayerSpecs<Base extends Layer<infer Specs, any> ? Specs : never, Overrides>,\n | (Base extends Layer<any, infer Collisions> ? Collisions : never)\n | (Overrides[number] extends Layer<any, infer Collisions> ? Collisions : never)\n | OverrideLayerCollisions<Base extends Layer<infer Specs, any> ? Specs : never, Overrides>\n >\n }\n}\n","/** Thrown when Scope context is accessed outside an active Scope execution. */\nexport class ScopeRuntimeNotConfiguredError extends Error {\n constructor() {\n super('No Scope is available in the current execution context')\n\n this.name = 'ScopeRuntimeNotConfiguredError'\n }\n}\n\n/** Thrown when a resource or finalizer is added after Scope closure begins. */\nexport class ScopeClosedError extends Error {\n constructor() {\n super('Cannot add resources or finalizers to a closed Scope')\n\n this.name = 'ScopeClosedError'\n }\n}\n\n/** Aggregates finalizer failures encountered while closing a Scope. */\nexport class ScopeCloseError extends Error {\n constructor(readonly causes: readonly unknown[]) {\n super(\n `Failed to close Scope (${causes.length} finalizer${causes.length === 1 ? '' : 's'} failed)`\n )\n\n this.name = 'ScopeCloseError'\n }\n}\n\n/** Thrown when a value has neither Symbol.dispose nor Symbol.asyncDispose. */\nexport class ResourceNotDisposableError extends Error {\n constructor() {\n super('Resource does not implement Symbol.dispose or Symbol.asyncDispose')\n\n this.name = 'ResourceNotDisposableError'\n }\n}\n","import type { ScopeFinalizer } from './types'\n\nconst SCOPE_SUCCESS = { status: 'success' } as const\n\ntype DisposableCandidate = {\n [Symbol.dispose]?: unknown\n\n [Symbol.asyncDispose]?: unknown\n}\n\n/** Return a Scope finalizer for a value's async or sync disposal protocol. */\nexport const getDisposeFinalizer = (resource: unknown): ScopeFinalizer | undefined => {\n const candidate = Object(resource) as DisposableCandidate\n const asyncDispose = candidate[Symbol.asyncDispose]\n\n if (typeof asyncDispose === 'function') {\n return () => asyncDispose.call(resource)\n }\n\n const dispose = candidate[Symbol.dispose]\n\n if (typeof dispose === 'function') {\n return () => dispose.call(resource)\n }\n\n return undefined\n}\n\n/** Dispose a value immediately when it implements a disposal protocol. */\nexport const disposeResource = (resource: unknown): void | PromiseLike<void> => {\n const finalizer = getDisposeFinalizer(resource)\n\n return finalizer?.(SCOPE_SUCCESS)\n}\n","import { AsyncLocalStorage } from 'node:async_hooks'\n\nimport { ScopeRuntimeNotConfiguredError } from './errors'\n\nimport type { Scope } from './scope'\n\nconst storage = new AsyncLocalStorage<Scope>()\n\n/** Bridges the current Scope through async execution context. */\nexport class ScopeRuntime {\n /** Supply a Scope while invoking a callback. */\n static run<A>(scope: Scope, program: () => A): A {\n return storage.run(scope, program)\n }\n\n /** Return the Scope active in the current execution context. */\n static current(): Scope {\n const scope = storage.getStore()\n\n if (!scope) {\n throw new ScopeRuntimeNotConfiguredError()\n }\n\n return scope\n }\n}\n","import { ScopeCloseError } from './errors'\n\nimport { ScopeRuntime } from './runtime'\n\nimport type { CloseableScope, Scope } from './scope'\n\nimport type { CleanupFailureDiagnostic, MaybePromise, ScopeOutcome } from './types'\n\nexport type OutcomeClassifier<A> = (value: A) => ScopeOutcome\n\nexport type RunScopedOptions<A> = {\n readonly classify: OutcomeClassifier<A>\n readonly onCleanupFailure?: (diagnostic: CleanupFailureDiagnostic) => MaybePromise<void>\n}\n\nconst notifyCleanupFailure = async (\n observer: ((diagnostic: CleanupFailureDiagnostic) => MaybePromise<void>) | undefined,\n diagnostic: CleanupFailureDiagnostic\n): Promise<void> => {\n if (!observer) {\n return\n }\n\n try {\n await observer(diagnostic)\n } catch {\n // Cleanup diagnostics are best effort and never affect the primary result.\n }\n}\n\nexport const runScoped = async <A>(\n scope: CloseableScope,\n program: () => A | PromiseLike<A>,\n options: RunScopedOptions<Awaited<A>>\n): Promise<Awaited<A>> => {\n let value!: Awaited<A>\n\n let programFailed = false\n let programFailure: unknown\n\n try {\n value = await ScopeRuntime.run(scope as Scope, program)\n } catch (cause) {\n programFailed = true\n programFailure = cause\n }\n\n const outcome: ScopeOutcome = programFailed\n ? {\n status: 'failure',\n cause: programFailure\n }\n : options.classify(value)\n\n let cleanupFailed = false\n let cleanupFailure: unknown\n\n try {\n await scope.close(outcome)\n } catch (cause) {\n cleanupFailed = true\n cleanupFailure = cause\n }\n\n if (cleanupFailed) {\n const error =\n cleanupFailure instanceof ScopeCloseError\n ? cleanupFailure\n : new ScopeCloseError([cleanupFailure])\n\n await notifyCleanupFailure(options.onCleanupFailure, {\n outcome,\n error\n })\n\n cleanupFailure = error\n }\n\n if (programFailed) {\n throw programFailure\n }\n\n if (outcome.status === 'failure') {\n return value\n }\n\n if (cleanupFailed) {\n throw cleanupFailure\n }\n\n return value\n}\n","import { ResourceNotDisposableError, ScopeCloseError, ScopeClosedError } from './errors'\n\nimport { getDisposeFinalizer } from './disposable'\n\nimport { runScoped } from './internal'\n\nimport { ScopeRuntime } from './runtime'\n\nimport type { DisposableResource, MaybePromise, ScopeFinalizer, ScopeOutcome } from './types'\n\n/**\n * Non-owning lifecycle context for finalizers and child Scopes.\n *\n * A Scope can register cleanup and create children, but it cannot close\n * itself. Use `Scope.make()` or `Scope.run()` when your code owns the Scope.\n */\nexport interface Scope {\n /** Register a finalizer that runs when the owning Scope closes. */\n addFinalizer(finalizer: ScopeFinalizer): void\n\n /** Acquire a resource and register its outcome-aware release callback. */\n acquire<R>(\n acquire: () => MaybePromise<R>,\n release: (resource: R, outcome: ScopeOutcome) => MaybePromise<void>\n ): Promise<R>\n\n /** Register an already-acquired disposable resource. */\n add<R extends DisposableResource>(resource: R): Promise<R>\n\n /** Create a child Scope owned by this Scope. */\n fork(): CloseableScope\n}\n\n/** A Scope whose owner is responsible for calling `close()`. */\nexport interface CloseableScope extends Scope {\n /** Close the Scope and run children and finalizers in child-first LIFO order. */\n close(outcome?: ScopeOutcome): Promise<void>\n}\n\nconst SCOPE_SUCCESS: ScopeOutcome = Object.freeze({ status: 'success' })\n\nclass ScopeImpl implements CloseableScope {\n private readonly children = new Set<ScopeImpl>()\n\n private readonly finalizers: ScopeFinalizer[] = []\n\n private closePromise: Promise<void> | undefined\n\n private closeOutcome: ScopeOutcome | undefined\n\n constructor(private parent?: ScopeImpl) {}\n\n fork(): CloseableScope {\n this.assertOpen()\n\n const child = new ScopeImpl(this)\n\n this.children.add(child)\n\n return child\n }\n\n addFinalizer(finalizer: ScopeFinalizer): void {\n this.assertOpen()\n\n this.finalizers.push(finalizer)\n }\n\n async acquire<R>(\n acquire: () => MaybePromise<R>,\n release: (resource: R, outcome: ScopeOutcome) => MaybePromise<void>\n ): Promise<R> {\n this.assertOpen()\n\n const resource = await acquire()\n\n try {\n this.addFinalizer((outcome) => release(resource, outcome))\n\n return resource\n } catch (scopeFailure) {\n try {\n await release(resource, this.closeOutcome ?? SCOPE_SUCCESS)\n } catch (releaseFailure) {\n throw new AggregateError(\n [scopeFailure, releaseFailure],\n 'Scope closed while acquiring a resource and immediate cleanup also failed'\n )\n }\n\n throw scopeFailure\n }\n }\n\n async add<R extends DisposableResource>(resource: R): Promise<R> {\n const finalizer = getDisposeFinalizer(resource)\n\n if (!finalizer) {\n throw new ResourceNotDisposableError()\n }\n\n try {\n this.addFinalizer(finalizer)\n\n return resource\n } catch (scopeFailure) {\n try {\n await finalizer(this.closeOutcome ?? SCOPE_SUCCESS)\n } catch (releaseFailure) {\n throw new AggregateError(\n [scopeFailure, releaseFailure],\n 'Scope closed while adding a disposable resource and cleanup also failed'\n )\n }\n\n throw scopeFailure\n }\n }\n\n close(outcome: ScopeOutcome = SCOPE_SUCCESS): Promise<void> {\n if (this.closePromise) {\n return this.closePromise\n }\n\n this.closeOutcome = outcome\n this.closePromise = ScopeRuntime.run(this, () => this.closeInternal(outcome))\n\n return this.closePromise\n }\n\n private async closeInternal(outcome: ScopeOutcome): Promise<void> {\n const failures: unknown[] = []\n\n const children = [...this.children]\n\n this.children.clear()\n\n for (let index = children.length - 1; index >= 0; index--) {\n const child = children[index]\n\n if (!child) {\n continue\n }\n\n try {\n await child.close(outcome)\n } catch (cause) {\n if (cause instanceof ScopeCloseError) {\n failures.push(...cause.causes)\n } else {\n failures.push(cause)\n }\n }\n }\n\n for (let index = this.finalizers.length - 1; index >= 0; index--) {\n const finalizer = this.finalizers[index]\n\n if (!finalizer) {\n continue\n }\n\n try {\n await finalizer(outcome)\n } catch (cause) {\n failures.push(cause)\n }\n }\n\n this.finalizers.length = 0\n\n this.detach()\n\n if (failures.length > 0) {\n throw new ScopeCloseError(failures)\n }\n }\n\n private detach(): void {\n const parent = this.parent\n\n if (!parent) {\n return\n }\n\n parent.children.delete(this)\n this.parent = undefined\n }\n\n private assertOpen(): void {\n if (this.closePromise) {\n throw new ScopeClosedError()\n }\n }\n}\n\nexport const Scope = {\n /** Create an owned, initially open Scope. */\n make(): CloseableScope {\n return new ScopeImpl()\n },\n\n /** Return the non-owning Scope available in the current execution context. */\n current(): Scope {\n return ScopeRuntime.current()\n },\n\n /** Run a callback with an existing Scope supplied as the current context. */\n provide<A>(scope: Scope, program: () => A): A {\n return ScopeRuntime.run(scope, program)\n },\n\n /** Resolve the current Scope through `yield* Scope` inside an Effect. */\n // oxlint-disable-next-line require-yield\n *[Symbol.iterator](): Generator<never, Scope, unknown> {\n return ScopeRuntime.current()\n },\n\n /**\n * Run a program in a newly owned Scope.\n *\n * Scope is independent from `better-result`, so returned values—including\n * `Result.err`—close this Scope with a successful outcome. Result-aware\n * outcome classification belongs to `Runtime.run`.\n *\n * @example\n * ```ts\n * await Scope.run(async (scope) => {\n * const connection = await scope.acquire(connect, (connection) => connection.close())\n * return connection.query()\n * })\n * ```\n */\n run<A>(program: (scope: Scope) => A | PromiseLike<A>): Promise<Awaited<A>> {\n const scope = new ScopeImpl()\n\n return runScoped(scope, () => program(scope), {\n classify: () => SCOPE_SUCCESS\n })\n }\n} as const\n","import { Result } from 'better-result'\n\nimport type { Result as ResultType } from 'better-result'\n\nimport type { EffectError, EffectRequirements, EffectResult, EffectSuccess } from './types'\n\ntype EffectInput<A, E, Requirements> =\n | EffectResult<A, E, Requirements>\n | PromiseLike<EffectResult<A, E, Requirements>>\n\ntype AnyEffectInput = EffectInput<any, any, any>\ntype AnyEffectResult = EffectResult<any, any, any>\ntype AnyAsyncEffectInput = PromiseLike<AnyEffectResult>\n\ntype PreserveAsync<Input, Output> = Input extends PromiseLike<unknown> ? Promise<Output> : Output\n\ntype MappedResult<Input, B> = EffectResult<B, EffectError<Input>, EffectRequirements<Input>>\n\ntype ErrorMappedResult<Input, E2> = EffectResult<\n EffectSuccess<Input>,\n E2,\n EffectRequirements<Input>\n>\n\ntype ChainedResult<First, Next> = EffectResult<\n EffectSuccess<Next>,\n EffectError<First> | EffectError<Next>,\n EffectRequirements<First> | EffectRequirements<Next>\n>\n\ntype ChainedOutput<First, Next> = ChainedResult<First, Next>\n\ntype AsyncChainedOutput<First, Next> = Promise<ChainedResult<First, Next>>\n\ntype MapOperation<A, B> = {\n <Input>(effect: Input & EffectInput<A, any, any>): PreserveAsync<Input, MappedResult<Input, B>>\n}\n\ntype MapErrorOperation<E1, E2> = {\n <Input>(\n effect: Input & EffectInput<any, E1, any>\n ): PreserveAsync<Input, ErrorMappedResult<Input, E2>>\n}\n\ntype AndThenOperation<A, Next> = {\n <Input>(effect: Input & EffectResult<A, any, any>): ChainedOutput<Input, Next>\n}\n\ntype AndThenAsyncOperation<A, Next> = {\n <Input>(effect: Input & EffectInput<A, any, any>): AsyncChainedOutput<Input, Next>\n}\n\nconst isPromiseLike = (value: unknown): value is PromiseLike<unknown> => {\n if ((typeof value !== 'object' && typeof value !== 'function') || value === null) {\n return false\n }\n\n return 'then' in value && typeof value.then === 'function'\n}\n\nconst mapResult = <A, B, E, Requirements>(\n result: EffectResult<A, E, Requirements>,\n fn: (value: A) => B\n): EffectResult<B, E, Requirements> => Result.map(result, fn) as EffectResult<B, E, Requirements>\n\nconst mapErrorResult = <A, E1, E2, Requirements>(\n result: EffectResult<A, E1, Requirements>,\n fn: (error: E1) => E2\n): EffectResult<A, E2, Requirements> =>\n Result.mapError(result, fn) as EffectResult<A, E2, Requirements>\n\nconst andThenResult = <A, B, E1, E2, Requirements1, Requirements2>(\n result: EffectResult<A, E1, Requirements1>,\n next: (value: A) => EffectResult<B, E2, Requirements2>\n): EffectResult<B, E1 | E2, Requirements1 | Requirements2> =>\n Result.andThen(result, next as (value: A) => ResultType<B, E2>) as EffectResult<\n B,\n E1 | E2,\n Requirements1 | Requirements2\n >\n\nconst andThenAsyncResult = <A, B, E1, E2, Requirements1, Requirements2>(\n result: EffectResult<A, E1, Requirements1>,\n next: (value: A) => PromiseLike<EffectResult<B, E2, Requirements2>>\n): Promise<EffectResult<B, E1 | E2, Requirements1 | Requirements2>> =>\n Result.andThenAsync(\n result,\n (value) => Promise.resolve(next(value)) as Promise<ResultType<B, E2>>\n ) as Promise<EffectResult<B, E1 | E2, Requirements1 | Requirements2>>\n\n/**\n * Map the successful value of a Result or Effect result.\n *\n * Supports both data-first and data-last forms and preserves asynchronous\n * results and phantom Service requirements.\n *\n * @example\n * ```ts\n * const doubled = Effect.map(Result.ok(2), (value) => value * 2)\n * const toLabel = Effect.map((value: number) => `#${value}`)\n * ```\n */\nexport function map<A, B>(fn: (value: A) => B): MapOperation<A, B>\nexport function map<Input, B>(\n effect: Input & AnyEffectInput,\n fn: (value: EffectSuccess<Input>) => B\n): PreserveAsync<Input, MappedResult<Input, B>>\nexport function map(first: unknown, second?: unknown): unknown {\n if (typeof first === 'function' && second === undefined) {\n return (effect: unknown) => map(effect as never, first as never)\n }\n\n const fn = second as (value: unknown) => unknown\n\n if (isPromiseLike(first)) {\n return Promise.resolve(first).then((result) =>\n mapResult(result as EffectResult<unknown, unknown, never>, fn)\n )\n }\n\n return mapResult(first as EffectResult<unknown, unknown, never>, fn)\n}\n\n/**\n * Map the error value of a Result or Effect result while preserving its\n * successful value, asynchronous shape, and Service requirements.\n *\n * @example\n * ```ts\n * const labelled = Effect.mapError(Result.err('missing'), (error) => ({ error }))\n * ```\n */\nexport function mapError<E1, E2>(fn: (error: E1) => E2): MapErrorOperation<E1, E2>\nexport function mapError<Input, E2>(\n effect: Input & AnyEffectInput,\n fn: (error: EffectError<Input>) => E2\n): PreserveAsync<Input, ErrorMappedResult<Input, E2>>\nexport function mapError(first: unknown, second?: unknown): unknown {\n if (typeof first === 'function' && second === undefined) {\n return (effect: unknown) => mapError(effect as never, first as never)\n }\n\n const fn = second as (error: unknown) => unknown\n\n if (isPromiseLike(first)) {\n return Promise.resolve(first).then((result) =>\n mapErrorResult(result as EffectResult<unknown, unknown, never>, fn)\n )\n }\n\n return mapErrorResult(first as EffectResult<unknown, unknown, never>, fn)\n}\n\n/**\n * Chain a synchronous Result-producing operation after a successful result.\n *\n * The next operation is skipped when the input is an error. Both error types\n * and both sets of Service requirements are preserved in the output.\n *\n * @example\n * ```ts\n * const user = Effect.andThen(Result.ok('u1'), (id) => repository.find(id))\n * ```\n */\nexport function andThen<A, Next extends AnyEffectResult>(\n next: (value: A) => Next\n): AndThenOperation<A, Next>\nexport function andThen<Input, Next extends AnyEffectResult>(\n effect: Input & AnyEffectResult,\n next: (value: EffectSuccess<Input>) => Next\n): ChainedOutput<Input, Next>\nexport function andThen(first: unknown, second?: unknown): unknown {\n if (typeof first === 'function' && second === undefined) {\n return (effect: unknown) => andThen(effect as never, first as never)\n }\n\n const next = second as (value: unknown) => EffectResult<unknown, unknown, never>\n\n return andThenResult(first as EffectResult<unknown, unknown, never>, next)\n}\n\n/**\n * Chain an asynchronous Result-producing operation after a successful result.\n *\n * The returned value is always a Promise and retains both operations' error\n * and Service-requirement metadata.\n *\n * @example\n * ```ts\n * const user = Effect.andThenAsync(loadUser(), (user) => fetchProfile(user.id))\n * ```\n */\nexport function andThenAsync<A, Next extends AnyAsyncEffectInput>(\n next: (value: A) => Next\n): AndThenAsyncOperation<A, Next>\nexport function andThenAsync<Input, Next extends AnyAsyncEffectInput>(\n effect: Input & AnyEffectInput,\n next: (value: EffectSuccess<Input>) => Next\n): AsyncChainedOutput<Input, Next>\nexport function andThenAsync(first: unknown, second?: unknown): unknown {\n if (typeof first === 'function' && second === undefined) {\n return (effect: unknown) => andThenAsync(effect as never, first as never)\n }\n\n const next = second as (value: unknown) => PromiseLike<EffectResult<unknown, unknown, never>>\n\n if (isPromiseLike(first)) {\n return Promise.resolve(first).then((result) =>\n andThenAsyncResult(result as EffectResult<unknown, unknown, never>, next)\n )\n }\n\n return andThenAsyncResult(first as EffectResult<unknown, unknown, never>, next)\n}\n","import { Result } from 'better-result'\n\nimport type { Err, Result as ResultType, UnhandledException } from 'better-result'\n\nimport { Scope } from '../scope'\n\nimport type { DisposableResource, MaybePromise, ScopeOutcome } from '../scope'\n\nimport type { EffectFromGenerator, EffectYield } from './types'\n\nimport { andThen, andThenAsync, map, mapError } from './combinators'\n\ntype AnyResult = ResultType<any, any>\n\ntype RuntimeGenerator =\n | (() => Generator<Err<never, unknown>, AnyResult, unknown>)\n | (() => AsyncGenerator<Err<never, unknown>, AnyResult, unknown>)\n\ntype EffectGenerator =\n | (() => Generator<EffectYield, AnyResult, unknown>)\n | (() => AsyncGenerator<EffectYield, AnyResult, unknown>)\n\n/**\n * Compose `better-result` operations while preserving Service requirements in\n * a phantom type channel.\n *\n * A generator may yield Service tokens and Result operations. It must return a\n * `Result` as its final value; Service yields are resolved by the active\n * Runtime and do not add runtime values to the Result stream.\n *\n * @example\n * ```ts\n * const loadUser = Effect.gen(async function* () {\n * const database = yield* Database\n * const user = yield* Result.await(database.findUser('u1'))\n *\n * return Result.ok(user)\n * })\n * ```\n */\nexport function gen<Yield extends EffectYield, Returned extends AnyResult>(\n body: () => Generator<Yield, Returned, unknown>\n): EffectFromGenerator<Yield, Returned>\n\nexport function gen<Yield extends EffectYield, Returned extends AnyResult>(\n body: () => AsyncGenerator<Yield, Returned, unknown>\n): Promise<EffectFromGenerator<Yield, Returned>>\n\nexport function gen(body: EffectGenerator): AnyResult | Promise<AnyResult> {\n /*\n * ServiceRequirement is phantom. The Service iterator returns its resolved\n * instance without yielding a marker, so Result.gen still receives only\n * the Err values that exist at runtime.\n */\n return (Result.gen as unknown as (body: RuntimeGenerator) => AnyResult | Promise<AnyResult>)(\n body as RuntimeGenerator\n )\n}\n\n/**\n * Acquire a resource in the current Scope and register its release callback.\n *\n * Acquisition failures are represented in the Effect Result error channel;\n * release failures remain owned by Scope cleanup. The release callback\n * receives the final outcome chosen by the enclosing execution boundary.\n *\n * @example\n * ```ts\n * const connection = yield* Effect.acquireRelease(\n * () => pool.connect(),\n * (connection, outcome) => connection.close(outcome)\n * )\n * ```\n */\nexport function acquireRelease<R>(\n acquire: () => MaybePromise<R>,\n release: (resource: R, outcome: ScopeOutcome) => MaybePromise<void>\n): AsyncGenerator<Err<never, UnhandledException>, R, unknown> {\n const scope = Scope.current()\n\n return Result.await(Result.tryPromise(() => scope.acquire(acquire, release)))\n}\n\n/**\n * Register an already-acquired disposable resource in the current Scope.\n *\n * The resource is not acquired by this helper. Registration failures are\n * represented in the Effect Result error channel; disposal failures remain\n * owned by Scope cleanup.\n *\n * @example\n * ```ts\n * const file = yield* Effect.add(await openFile('notes.txt'))\n * ```\n */\nexport function add<R extends DisposableResource>(\n resource: R\n): AsyncGenerator<Err<never, UnhandledException>, R, unknown> {\n const scope = Scope.current()\n\n return Result.await(Result.tryPromise(() => scope.add(resource)))\n}\n\n/**\n * Effect namespace containing generator, resource, and Result combinators.\n *\n * Prefer these helpers when a program needs typed Service requirements or\n * Scope-aware acquisition and cleanup.\n */\nexport const Effect = {\n /** Compose a generator-based Effect program. */\n gen,\n /** Acquire and register a resource in the current Scope. */\n acquireRelease,\n /** Register an already-acquired disposable in the current Scope. */\n add,\n /** Map a successful Effect result. */\n map,\n /** Map an Effect error. */\n mapError,\n /** Chain a synchronous Effect result. */\n andThen,\n /** Chain an asynchronous Effect result. */\n andThenAsync\n} as const\n","type Unary<A, B> = (value: A) => B\n\n/**\n * Compose a value through a sequence of unary functions.\n *\n * `pipe` is deliberately independent of Effect, Result, Promise, Scope, and\n * Service metadata.\n *\n * @example\n * ```ts\n * const label = pipe(\n * 'alice',\n * (name) => name.trim(),\n * (name) => name.toUpperCase()\n * )\n * ```\n */\nexport function pipe<A>(value: A): A\nexport function pipe<A, B>(value: A, ab: Unary<A, B>): B\nexport function pipe<A, B, C>(value: A, ab: Unary<A, B>, bc: Unary<B, C>): C\nexport function pipe<A, B, C, D>(value: A, ab: Unary<A, B>, bc: Unary<B, C>, cd: Unary<C, D>): D\nexport function pipe<A, B, C, D, E>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>\n): E\nexport function pipe<A, B, C, D, E, F>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>\n): F\nexport function pipe<A, B, C, D, E, F, G>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>\n): G\nexport function pipe<A, B, C, D, E, F, G, H>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>\n): H\nexport function pipe<A, B, C, D, E, F, G, H, I>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>,\n hi: Unary<H, I>\n): I\nexport function pipe<A, B, C, D, E, F, G, H, I, J>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>,\n hi: Unary<H, I>,\n ij: Unary<I, J>\n): J\nexport function pipe<A, B, C, D, E, F, G, H, I, J, K>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>,\n hi: Unary<H, I>,\n ij: Unary<I, J>,\n jk: Unary<J, K>\n): K\nexport function pipe(\n value: unknown,\n ...operations: ReadonlyArray<Unary<unknown, unknown>>\n): unknown {\n return operations.reduce((current, operation) => operation(current), value)\n}\n","import { TaggedError } from 'better-result'\n\n/** Describes a failure encountered while releasing a Resource. */\nexport class ResourceReleaseFailure extends TaggedError('ResourceReleaseFailure')<{\n readonly resource: string\n readonly cause: unknown\n readonly message: string\n}> {}\n","import { Result, type Result as ResultType, type UnhandledException } from 'better-result'\n\nimport { ResourceReleaseFailure } from './errors'\n\nimport { disposeResource } from '../scope/disposable'\n\nexport { disposeResource }\n\nimport type { AsyncResult, MaybePromise, ReleaseFailureObserver, ReleaseOutcome } from './types'\n\nconst toReleaseFailure = (resource: string, cause: unknown): ResourceReleaseFailure =>\n new ResourceReleaseFailure({\n resource,\n cause,\n message: `Failed to release resource: ${resource}`\n })\n\nexport const runResult = async <T, E>(\n operation: () => AsyncResult<T, E>\n): Promise<ResultType<T, E | UnhandledException>> => {\n const execution = await Result.tryPromise(() => Promise.resolve(operation()))\n\n return execution.andThen((result) => result)\n}\n\nconst normalizeReleaseOutcome = (\n name: string,\n outcome: ReleaseOutcome\n): ResultType<void, ResourceReleaseFailure> => {\n if (outcome === undefined) {\n return Result.ok()\n }\n\n return outcome.mapError((cause) => toReleaseFailure(name, cause))\n}\n\nexport const runRelease = async <R>(\n name: string,\n resource: R,\n release: (resource: R) => MaybePromise<ReleaseOutcome>\n): Promise<ResultType<void, ResourceReleaseFailure>> => {\n const execution = await Result.tryPromise({\n try: () => Promise.resolve(release(resource)),\n catch: (cause) => toReleaseFailure(name, cause)\n })\n\n return execution.andThen((outcome) => normalizeReleaseOutcome(name, outcome))\n}\n\nconst notifyReleaseFailure = async (\n observer: ReleaseFailureObserver | undefined,\n\n failure: ResourceReleaseFailure\n): Promise<void> => {\n if (!observer) {\n return\n }\n\n try {\n await observer(failure)\n } catch {\n /*\n * Diagnostics must never replace\n * the actual operation error.\n */\n }\n}\n\nexport const combineUseAndRelease = async <A, E>(\n used: ResultType<A, E>,\n\n released: ResultType<void, ResourceReleaseFailure>,\n\n onReleaseFailure?: ReleaseFailureObserver\n): Promise<ResultType<A, E | ResourceReleaseFailure>> => {\n if (Result.isError(used)) {\n if (Result.isError(released)) {\n await notifyReleaseFailure(onReleaseFailure, released.error)\n }\n\n return Result.err<A, E | ResourceReleaseFailure>(used.error)\n }\n\n if (Result.isError(released)) {\n await notifyReleaseFailure(onReleaseFailure, released.error)\n\n return Result.err<A, E | ResourceReleaseFailure>(released.error)\n }\n\n return Result.ok<A, E | ResourceReleaseFailure>(used.value)\n}\n","import { Result, type Result as ResultType, type UnhandledException } from 'better-result'\n\nimport { Scope } from '../scope'\n\nimport { ResourceReleaseFailure } from './errors'\n\nimport { combineUseAndRelease, disposeResource, runRelease, runResult } from './internal'\n\nimport type { AcquireUseReleaseOptions } from './types'\n\n/**\n * Acquire a resource, use it, and always attempt release afterward.\n *\n * Acquisition, use, and release may be synchronous or asynchronous Result\n * operations. If both use and release fail, the use error remains primary and\n * `onReleaseFailure` receives the cleanup failure as a diagnostic.\n *\n * When `release` is omitted, `Symbol.asyncDispose` is preferred over\n * `Symbol.dispose`.\n *\n * @example\n * ```ts\n * const result = await Resource.acquireUseRelease({\n * name: 'database connection',\n * acquire: () => connect(),\n * use: (connection) => query(connection),\n * release: (connection) => connection.close()\n * })\n * ```\n */\nconst acquireUseRelease = <R, A, AcquireError, UseError>({\n name,\n acquire,\n use,\n release = disposeResource,\n onReleaseFailure\n}: AcquireUseReleaseOptions<R, A, AcquireError, UseError>): Promise<\n ResultType<A, AcquireError | UseError | UnhandledException | ResourceReleaseFailure>\n> =>\n Result.gen(async function* () {\n const resource = yield* Result.await(runResult(acquire))\n\n const scope = Scope.make()\n\n let released: ResultType<void, ResourceReleaseFailure> = Result.ok()\n\n scope.addFinalizer(async () => {\n released = await runRelease(name, resource, release)\n })\n\n const used = await runResult(() => use(resource))\n\n await scope.close()\n\n return await combineUseAndRelease(used, released, onReleaseFailure)\n })\n\nexport const Resource = {\n /** Acquire, use, and release a resource with deterministic error precedence. */\n acquireUseRelease\n} as const\n","import { Result } from 'better-result'\n\nimport type { Result as ResultType } from 'better-result'\n\nimport type { LayerDisposeError } from '../layer/errors'\n\nimport type { CleanupFailureDiagnostic, MaybePromise, ScopeOutcome } from '../scope'\n\n/** Aggregated cleanup information reported during Runtime shutdown. */\nexport type RuntimeShutdownDiagnostic = {\n /** Final outcome supplied to the Runtime root Scope. */\n readonly outcome: ScopeOutcome\n /** Aggregated root-Scope and backend cleanup failure. */\n readonly error: LayerDisposeError\n}\n\n/** Observer notified about cleanup failures without changing primary results. */\nexport type CleanupFailureObserver = (\n diagnostic: CleanupFailureDiagnostic | RuntimeShutdownDiagnostic\n) => MaybePromise<void>\n\n/** Optional Runtime configuration for cleanup diagnostics. */\nexport type RuntimeOptions = {\n /** Optional observer for best-effort cleanup diagnostics. */\n readonly onCleanupFailure?: CleanupFailureObserver\n}\n\nconst isResultLike = (value: unknown): value is ResultType<unknown, unknown> =>\n typeof value === 'object' &&\n value !== null &&\n 'status' in value &&\n (value.status === 'ok' || value.status === 'error')\n\nexport const classifyRuntimeOutcome = (value: unknown): ScopeOutcome => {\n if (isResultLike(value) && Result.isError(value)) {\n return {\n status: 'failure',\n cause: value.error\n }\n }\n\n return {\n status: 'success'\n }\n}\n","import { ServiceRuntime } from '../service'\n\nimport type { AnyServiceToken } from '../service'\n\nimport { Scope, type CloseableScope } from '../scope'\nimport { runScoped } from '../scope/internal'\nimport { ScopeRuntime } from '../scope/runtime'\n\nimport {\n classifyRuntimeOutcome,\n type CleanupFailureObserver,\n type RuntimeOptions,\n type RuntimeShutdownDiagnostic\n} from '../runtime/outcome'\n\nimport { LayerDisposeError, LayerRegistrationError } from './errors'\n\nimport type { LayerBackend } from './backend'\n\nimport type { AnyLayer, CompleteExecution, CompleteLayer, LayerProvided } from './inference'\n\nimport type { LayerRegistration } from './types'\n\nimport type { ScopeOutcome } from '../scope'\n\ntype LayerProvider = AnyLayer['providers'][number]\n\n/** Runtime-facing handle that owns a Layer's resources and execution scopes. */\nexport interface RuntimeHandle<Provided extends AnyServiceToken = AnyServiceToken> {\n /** The backend used to resolve this Layer's providers. */\n readonly backend: LayerBackend\n\n /** Run a program in a child Scope of the Layer's root Scope. */\n run<A>(program: CompleteExecution<Provided, A>): Promise<Awaited<A>>\n\n /** Stop new executions and release Layer-owned resources. */\n dispose(outcome?: ScopeOutcome): Promise<void>\n}\n\nconst SCOPE_SUCCESS: ScopeOutcome = Object.freeze({ status: 'success' })\n\nclass RuntimeHandleDisposedError extends Error {\n constructor() {\n super('Cannot run a program using a disposed Layer')\n\n this.name = 'RuntimeHandleDisposedError'\n }\n}\n\nconst normalizeDisposeCauses = (cause: unknown): readonly unknown[] => {\n if (cause instanceof AggregateError) {\n return [...cause.errors]\n }\n\n return [cause]\n}\n\nconst notifyShutdownFailure = async (\n observer: CleanupFailureObserver | undefined,\n diagnostic: RuntimeShutdownDiagnostic\n): Promise<void> => {\n if (!observer) {\n return\n }\n\n try {\n await observer(diagnostic)\n } catch {\n // Shutdown diagnostics are best effort and never affect the primary result.\n }\n}\n\nconst bindProviderToScope = (\n provider: LayerProvider,\n rootScope: CloseableScope\n): LayerRegistration => ({\n service: provider.service,\n\n acquire: () =>\n ScopeRuntime.run(rootScope, async () => {\n if (!provider.release) {\n return await provider.acquire()\n }\n\n return await rootScope.acquire(\n () => provider.acquire(),\n (resource, outcome) => provider.release!(resource, outcome)\n )\n })\n})\n\nclass RuntimeHandleImpl<Provided extends AnyServiceToken> implements RuntimeHandle<Provided> {\n private disposePromise: Promise<void> | undefined\n\n private readonly executions = new Set<Promise<unknown>>()\n\n private state: 'active' | 'disposing' | 'disposed' = 'active'\n\n constructor(\n readonly backend: LayerBackend,\n private readonly rootScope: CloseableScope,\n private readonly onCleanupFailure: CleanupFailureObserver | undefined\n ) {}\n\n run<A>(program: CompleteExecution<Provided, A>): Promise<Awaited<A>> {\n this.assertActive()\n\n const executionScope = this.rootScope.fork()\n\n let resolveExecution!: (value: Awaited<A> | PromiseLike<Awaited<A>>) => void\n let rejectExecution!: (cause?: unknown) => void\n\n const execution = new Promise<Awaited<A>>((resolve, reject) => {\n resolveExecution = resolve\n rejectExecution = reject\n })\n\n this.executions.add(execution)\n\n void execution.then(\n () => {\n this.executions.delete(execution)\n },\n () => {\n this.executions.delete(execution)\n }\n )\n\n try {\n const running = this.runExecution(executionScope, program)\n\n void running.then(\n (value) => {\n resolveExecution(value)\n },\n (cause) => {\n rejectExecution(cause)\n }\n )\n } catch (cause) {\n rejectExecution(cause)\n }\n\n return execution\n }\n\n private runExecution<A>(\n executionScope: CloseableScope,\n program: CompleteExecution<Provided, A>\n ): Promise<Awaited<A>> {\n const options = this.onCleanupFailure\n ? {\n classify: classifyRuntimeOutcome,\n onCleanupFailure: this.onCleanupFailure\n }\n : {\n classify: classifyRuntimeOutcome\n }\n\n return runScoped(executionScope, () => ServiceRuntime.run(this.backend, program), options)\n }\n\n dispose(outcome: ScopeOutcome = SCOPE_SUCCESS): Promise<void> {\n if (this.disposePromise) {\n return this.disposePromise\n }\n\n this.state = 'disposing'\n\n const executions = [...this.executions]\n\n this.disposePromise = this.performDispose(executions, outcome)\n\n return this.disposePromise\n }\n\n private async performDispose(\n executions: readonly Promise<unknown>[],\n outcome: ScopeOutcome\n ): Promise<void> {\n const failures: unknown[] = []\n\n await Promise.allSettled(executions)\n\n try {\n await ServiceRuntime.run(this.backend, () => this.rootScope.close(outcome))\n } catch (cause) {\n failures.push(cause)\n }\n\n try {\n await this.backend.disposeAll()\n } catch (cause) {\n failures.push(cause)\n }\n\n this.state = 'disposed'\n\n if (failures.length > 0) {\n const error = new LayerDisposeError(failures.flatMap(normalizeDisposeCauses))\n\n await notifyShutdownFailure(this.onCleanupFailure, {\n outcome,\n error\n })\n\n throw error\n }\n }\n\n private assertActive(): void {\n if (this.state !== 'active') {\n throw new RuntimeHandleDisposedError()\n }\n }\n}\n\n/** Build a Runtime handle for a complete Layer and register its providers. */\nexport const createRuntimeHandle = async <L extends AnyLayer>(\n layer: CompleteLayer<L>,\n backend: LayerBackend,\n options: RuntimeOptions = {}\n): Promise<RuntimeHandle<LayerProvided<L>>> => {\n const rootScope = Scope.make()\n let current: LayerProvider | undefined\n\n try {\n for (const provider of layer.providers) {\n current = provider\n\n await backend.register(bindProviderToScope(provider, rootScope))\n }\n } catch (registrationCause) {\n const outcome: ScopeOutcome = {\n status: 'failure',\n cause: registrationCause\n }\n const cleanupCauses: unknown[] = []\n\n try {\n await ServiceRuntime.run(backend, () => rootScope.close(outcome))\n } catch (cause) {\n cleanupCauses.push(cause)\n }\n\n try {\n await backend.disposeAll()\n } catch (cause) {\n cleanupCauses.push(cause)\n }\n\n if (cleanupCauses.length > 0) {\n const shutdownError = new LayerDisposeError(cleanupCauses.flatMap(normalizeDisposeCauses))\n\n await notifyShutdownFailure(options.onCleanupFailure, {\n outcome,\n error: shutdownError\n })\n }\n\n let cleanupCause: unknown\n\n if (cleanupCauses.length === 1) {\n cleanupCause = cleanupCauses[0]\n } else if (cleanupCauses.length > 1) {\n cleanupCause = new LayerDisposeError(cleanupCauses.flatMap(normalizeDisposeCauses))\n }\n\n throw new LayerRegistrationError(current?.service, registrationCause, cleanupCause)\n }\n\n return new RuntimeHandleImpl<LayerProvided<L>>(backend, rootScope, options.onCleanupFailure)\n}\n","import type { LayerBackend } from '../layer'\n\nimport { createRuntimeHandle, type RuntimeHandle } from '../layer/runtime'\n\nimport type { AnyLayer, CompleteLayer, LayerProvided } from '../layer/inference'\n\nimport type { CompleteExecution } from '../layer/inference'\n\nimport type { AnyServiceToken } from '../service'\n\nimport { classifyRuntimeOutcome, type RuntimeOptions } from './outcome'\n\nimport type { ScopeOutcome } from '../scope'\n\n/**\n * Long-lived execution environment backed by a complete Layer.\n *\n * A Runtime owns Layer resources until `dispose()` is called. Each `run()` is\n * isolated in a child Scope, while Layer-scoped resources remain shared.\n *\n * @example\n * ```ts\n * const runtime = await Runtime.make(AppLive, new MemoryLayerBackend())\n * const result = await runtime.run(loadUser('u1'))\n * await runtime.dispose()\n * ```\n *\n * @typeParam Provided The Service constructors supplied by the Layer.\n */\nexport class Runtime<Provided extends AnyServiceToken = AnyServiceToken> {\n private constructor(private readonly handle: RuntimeHandle<Provided>) {}\n\n /**\n * Create a long-lived Runtime that owns its Layer resources.\n *\n * @example\n * ```ts\n * const runtime = await Runtime.make(AppLive, backend)\n * const result = await runtime.run(program)\n * await runtime.dispose()\n * ```\n */\n static async make<L extends AnyLayer>(\n layer: CompleteLayer<L>,\n backend: LayerBackend,\n options: RuntimeOptions = {}\n ): Promise<Runtime<LayerProvided<L>>> {\n const handle = await createRuntimeHandle(layer, backend, options)\n\n return new Runtime<LayerProvided<L>>(handle)\n }\n\n /**\n * Run one program and dispose its Layer resources before resolving.\n *\n * This is convenient for request-style or command-style execution where a\n * Runtime should not outlive the operation.\n */\n static async run<A, L extends AnyLayer>(\n layer: CompleteLayer<L>,\n backend: LayerBackend,\n program: CompleteExecution<LayerProvided<L>, A>,\n options: RuntimeOptions = {}\n ): Promise<Awaited<A>> {\n const runtime = await Runtime.make(layer, backend, options)\n\n let value!: Awaited<A>\n let executionFailed = false\n let executionFailure: unknown\n let programOutcome: ScopeOutcome | undefined\n\n try {\n value = await runtime.runUnchecked(async () => {\n try {\n const programValue = await program()\n\n programOutcome = classifyRuntimeOutcome(programValue)\n\n return programValue\n } catch (cause) {\n programOutcome = {\n status: 'failure',\n cause\n }\n\n throw cause\n }\n })\n } catch (cause) {\n executionFailed = true\n executionFailure = cause\n }\n\n const outcome: ScopeOutcome =\n programOutcome ??\n ({\n status: 'failure',\n cause: executionFailure\n } as const)\n\n try {\n await runtime.disposeWithOutcome(outcome)\n } catch (shutdownFailure) {\n if (!executionFailed && outcome.status === 'success') {\n throw shutdownFailure\n }\n }\n\n if (executionFailed) {\n throw executionFailure\n }\n\n return value\n }\n\n /** Run one execution in this Runtime's child Scope. */\n run<A>(program: CompleteExecution<Provided, A>): Promise<Awaited<A>> {\n return this.handle.run(program)\n }\n\n private runUnchecked<A>(program: () => A | PromiseLike<A>): Promise<Awaited<A>> {\n return this.handle.run(program as CompleteExecution<Provided, A>)\n }\n\n /** Stop new executions and release the Runtime's Layer resources. */\n dispose(): Promise<void> {\n return this.handle.dispose()\n }\n\n private disposeWithOutcome(outcome: ScopeOutcome): Promise<void> {\n return this.handle.dispose(outcome)\n }\n}\n"],"mappings":";;;;AAYA,MAAMA,YAAU,IAAI,kBAAmC;;AAGvD,IAAa,iBAAb,MAAa,eAAe;;;;;;;;;;;;;CAa1B,OAAO,IAAO,UAA2B,SAAqB;EAC5D,OAAOA,UAAQ,IAAI,UAAU,OAAO;CACtC;;CAGA,OAAO,UAA2B;EAChC,MAAM,WAAWA,UAAQ,SAAS;EAElC,IAAI,CAAC,UACH,MAAM,IAAI,iCAAiC;EAG7C,OAAO;CACT;;CAGA,aAAa,QAAmC,OAAoC;EAGlF,OAAO,MAFU,eAAe,QAEZ,CAAC,CAAC,QAAQ,KAAK;CACrC;AACF;;;;;;;;;;;;;;;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AAQ;CAC7B;CAEA,mBAA2B,SAAsC;EAC/D,OAAO,KAAK,OAAO,QAAQ,OAAO;CACpC;AACF"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["storage","Constructor","SCOPE_SUCCESS","SCOPE_SUCCESS","next"],"sources":["../src/service/runtime.ts","../src/service/service.ts","../src/layer/internal.ts","../src/layer/layer.ts","../src/scope/errors.ts","../src/scope/disposable.ts","../src/scope/runtime.ts","../src/scope/internal.ts","../src/scope/scope.ts","../src/effect/combinators.ts","../src/effect/effect.ts","../src/function/pipe.ts","../src/resource/errors.ts","../src/resource/internal.ts","../src/resource/resource.ts","../src/runtime/outcome.ts","../src/layer/runtime.ts","../src/runtime/runtime.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\n\nimport { ServiceRuntimeNotConfiguredError } from './errors'\n\nimport type { AnyServiceToken } from './types'\n\n/** Resolves class-backed Service tokens for a runtime execution. */\nexport interface ServiceResolver {\n /** Resolve a token to its corresponding Service instance. */\n resolve<T extends AnyServiceToken>(token: T): InstanceType<T> | PromiseLike<InstanceType<T>>\n}\n\nconst storage = new AsyncLocalStorage<ServiceResolver>()\n\n/** Provides the resolver context used by Service tokens during execution. */\nexport class ServiceRuntime {\n /**\n * Run a callback with a resolver available to `yield* Service` expressions.\n *\n * The context is scoped to the callback and is restored afterward.\n *\n * @example\n * ```ts\n * const value = ServiceRuntime.run(resolver, () => {\n * return ServiceRuntime.resolve(Database)\n * })\n * ```\n */\n static run<A>(resolver: ServiceResolver, program: () => A): A {\n return storage.run(resolver, program)\n }\n\n /** Return the resolver active in the current execution context. */\n static current(): ServiceResolver {\n const resolver = storage.getStore()\n\n if (!resolver) {\n throw new ServiceRuntimeNotConfiguredError()\n }\n\n return resolver\n }\n\n /** Resolve a Service token using the active resolver. */\n static async resolve<T extends AnyServiceToken>(token: T): Promise<InstanceType<T>> {\n const resolver = ServiceRuntime.current()\n\n return await resolver.resolve(token)\n }\n}\n","import { ServiceRuntime } from './runtime'\n\nimport type { ServiceRequirement } from '../effect/types'\n\nimport type {\n AnyService,\n AnyServiceToken,\n ServiceClass,\n ServiceContract,\n ServiceIdentity,\n ServiceIdentityTypeId,\n ServiceInstance,\n ServiceRequirements,\n ServiceTag,\n ServiceToken,\n ServiceTokenOf\n} from './types'\n\ntype ServiceTagLiteral<Tag extends string> = string extends Tag\n ? never\n : Tag extends ''\n ? never\n : Tag\n\ninterface ServiceFactory<Self> {\n <const Tag extends string>(\n tag: ServiceTagLiteral<Tag>\n ): (abstract new () => ServiceIdentity<Tag>) & {\n readonly name: string\n readonly serviceTag: Tag\n } & {\n readonly of: Service.FactoryOf<Self, Tag>\n readonly [Symbol.asyncIterator]: (\n this: ServiceToken<Tag, Self & ServiceIdentity<Tag>>\n ) => AsyncGenerator<ServiceRequirement<Self>, Self, unknown>\n }\n}\n\n/**\n * Declare a class-backed Service with a stable string-literal identity.\n *\n * The returned class is simultaneously the implementation type, the runtime\n * dependency token, and the value yielded by `yield*` in an Effect generator.\n * The explicit self type preserves exact instance inference, while the second\n * call captures the tag as a literal for Layer composition and diagnostics.\n *\n * @example\n * ```ts\n * class Database extends Service<Database>()('Database') {\n * query(): string {\n * return 'ok'\n * }\n * }\n *\n * const database = yield* Database\n * database.query()\n * ```\n *\n * @typeParam Self The instance type implemented by the declared Service.\n */\nexport function Service<Self>(): ServiceFactory<Self> {\n return function <const Tag extends string>(tag: ServiceTagLiteral<Tag>) {\n if (tag.length === 0) {\n throw new TypeError('Service tags must not be empty')\n }\n\n abstract class BaseService implements ServiceIdentity<Tag> {\n /** The stable logical identity used by Layers and resolver backends. */\n static readonly serviceTag: Tag = tag\n declare readonly [ServiceIdentityTypeId]: Tag\n\n /**\n * Type-check a structural implementation of this Service.\n *\n * This is an identity helper. It returns the supplied value unchanged\n * and does not invoke a constructor or modify its prototype.\n *\n * @example\n * ```ts\n * class Database extends Service<Database>()('Database') {\n * query(sql: string): string {\n * return sql\n * }\n * }\n *\n * const database = Database.of({\n * query: (sql) => `Result: ${sql}`\n * })\n *\n * database.query('SELECT 1')\n * // 'Result: SELECT 1'\n * // database is the original object, not an instance of Database\n * ```\n */\n static of(this: void, implementation: ServiceContract<Self & ServiceIdentity<Tag>>): Self {\n // SAFETY: ServiceContract removes only the phantom marker; this boundary restores Self.\n return implementation as Self\n }\n\n /** Resolve this Service from the resolver active in the current runtime. */\n // oxlint-disable-next-line require-yield\n static async *[Symbol.asyncIterator](\n this: ServiceToken<Tag, Self & ServiceIdentity<Tag>>\n ): AsyncGenerator<ServiceRequirement<Self>, Self, unknown> {\n return await ServiceRuntime.resolve(this)\n }\n }\n\n return BaseService\n }\n}\n\n/** Type-level aliases for Service tokens and their instance contracts. */\nexport declare namespace Service {\n /** The widened Service instance constraint. */\n export type Any = AnyService\n\n /** A class-backed Service token with a stable tag and instance contract. */\n export type Token<Tag extends string = string, Instance extends AnyService = any> = ServiceToken<\n Tag,\n Instance\n >\n\n /** A constructible Service class with a stable tag and instance contract. */\n export type Class<\n Tag extends string = string,\n Instance extends AnyService = AnyService\n > = ServiceClass<Tag, Instance>\n\n /** Extract the instance represented by a Service token. */\n export type Instance<T extends AnyServiceToken> = ServiceInstance<T>\n\n /** Extract the stable tag represented by a Service instance. */\n export type Tag<S extends AnyService> = ServiceTag<S>\n\n /** A branded Service instance identity with a stable tag. */\n export type Identity<Tag extends string = string> = ServiceIdentity<Tag>\n\n /** Remove the internal identity marker from a Service implementation contract. */\n export type Contract<S extends AnyService> = ServiceContract<S>\n\n /** Extract the Service token represented by a branded Service instance. */\n export type TokenOf<S extends AnyService> = ServiceTokenOf<S>\n\n /** Declaration bridge for the recursive structural `Service.of` signature. */\n export type FactoryOf<Self, Tag extends string> = (\n this: void,\n implementation: ServiceContract<Self & ServiceIdentity<Tag>>\n ) => Self\n\n /** Extract Effect Service requirements from a Service instance. */\n export type Requirements<S extends AnyService> = ServiceRequirements<S>\n}\n","import { LayerGeneratorYieldError } from './errors'\n\nimport type { ServiceRequirement } from '../effect/types'\nimport type { ServiceClass } from '../service'\n\nimport type { LayerGenerator } from './types'\n\nexport const runLayerGenerator = async <\n S extends ServiceClass<any, any>,\n Yield extends ServiceRequirement<unknown>\n>(\n service: S,\n factory: LayerGenerator<S, Yield>\n): Promise<InstanceType<S>> => {\n const iterator = factory()\n\n const state = await iterator.next()\n\n if (!state.done) {\n try {\n // SAFETY: The iterator is closed only to discard an invalid yield; its return value is ignored.\n await iterator.return(undefined as never)\n } finally {\n // oxlint-disable-next-line no-unsafe-finally\n throw new LayerGeneratorYieldError(service)\n }\n }\n\n // SAFETY: The public generator boundary accepts only the requested Service contract.\n return state.value as InstanceType<S>\n}\n","import type { ServiceRequirement } from '../effect/types'\nimport type { AnyService, ServiceClass, ServiceContract, ServiceRequirements } from '../service'\nimport type { ScopeOutcome } from '../scope'\nimport type { Covariant, Invariant } from '../internal/variance'\nimport type { MaybePromise } from '../utils/types'\n\nimport { DuplicateServiceError, ServiceTagCollisionError } from './errors'\nimport { runLayerGenerator } from './internal'\n\nimport type {\n LayerInput,\n CompleteInput,\n LayerResult,\n MergeResult,\n OverrideLayerResult,\n ValidateLayerInput,\n ValidateLayerTuple,\n ValidateOverrides,\n ProvidedEnvironment,\n RequiredEnvironment\n} from './inference'\nimport type { ProviderEntry } from './metadata'\nimport type { LayerGenerator, LayerGeneratorRequirements, LayerRegistration } from './types'\n\ndeclare const LayerTypeId: unique symbol\n\ninterface LayerVariance<in out Provided, out Required> {\n readonly _Provided: Invariant<Provided>\n readonly _Required: Covariant<Required>\n}\n\ninterface LayerProvider extends LayerRegistration {\n /** Provider storage deliberately erases the concrete instance type. */\n // oxlint-disable-next-line anti-slop/no-unknown-parameters\n readonly release?: (instance: unknown, outcome: ScopeOutcome) => MaybePromise<void>\n}\n\n/** A Service class whose constructor can be called without arguments. */\ntype DefaultConstructibleServiceClass<\n Tag extends string = string,\n Instance extends AnyService = AnyService\n> = ServiceClass<Tag, Instance> & (new () => Instance)\n\n/**\n * Declarative collection of Service providers.\n *\n * A Layer describes how to acquire implementations; it does not execute\n * providers until a `Runtime` is created. Use `merge` to compose distinct\n * providers and `override` when replacing an existing provider intentionally.\n *\n * @example\n * ```ts\n * const AppLive = Layer.merge(\n * Layer.succeed(Database, database),\n * Layer.make(UserRepository)\n * )\n *\n * const runtime = await Runtime.make(AppLive, backend)\n * ```\n */\nexport class Layer<\n in out Provided extends AnyService = AnyService,\n out Required extends AnyService = AnyService\n> {\n declare readonly [LayerTypeId]: LayerVariance<Provided, Required>\n\n /** The provider registrations retained by this Layer. */\n readonly providers: readonly LayerProvider[]\n\n private constructor(providers: readonly LayerProvider[]) {\n this.providers = Object.freeze([...providers])\n }\n\n /** Create a Layer that lazily acquires a Service instance. */\n static make<S extends DefaultConstructibleServiceClass<any, any>>(\n service: S\n ): LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>>\n\n static make<S extends ServiceClass<any, any>>(\n service: S,\n acquire: () => MaybePromise<ServiceContract<InstanceType<S>>>\n ): LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>>\n\n static make<S extends ServiceClass<any, any>>(\n service: S,\n acquire?: () => MaybePromise<ServiceContract<InstanceType<S>>>\n ): LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>> {\n const defaultAcquire = (): InstanceType<S> => {\n // SAFETY: The no-argument overload constrains `service` to a default constructible class.\n const Constructor = service as new () => InstanceType<S>\n\n return new Constructor()\n }\n\n const normalizedAcquire = normalizeAcquire<S>(acquire ?? defaultAcquire)\n\n // SAFETY: Runtime storage erases only the concrete provider metadata; the public constructor result restores its typed provenance.\n return new Layer([\n {\n service,\n acquire: normalizedAcquire\n }\n ]) as LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>>\n }\n\n /** Create a Layer from an already-constructed Service instance. */\n static succeed<S extends ServiceClass<any, any>>(\n service: S,\n instance: ServiceContract<InstanceType<S>>\n ): LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>> {\n const normalizedAcquire = normalizeAcquire<S>(() => instance)\n\n // SAFETY: The structural instance has been checked against the requested Service contract.\n return new Layer([\n {\n service,\n acquire: normalizedAcquire\n }\n ]) as LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>>\n }\n\n /** Define a provider with Runtime-root cleanup. */\n static scoped<S extends ServiceClass<any, any>>(\n service: S,\n acquire: () => MaybePromise<ServiceContract<InstanceType<S>>>,\n release: (instance: InstanceType<S>) => MaybePromise<void>\n ): LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>> {\n // SAFETY: The public callbacks constrain acquisition and release to the requested Service.\n return new Layer([\n {\n service,\n acquire: normalizeAcquire<S>(acquire),\n release: (instance) => {\n // SAFETY: The backend invokes release with the instance acquired for this token.\n return release(instance as InstanceType<S>)\n }\n }\n ]) as LayerResult<ProviderEntry<InstanceType<S>, ServiceRequirements<InstanceType<S>>>>\n }\n\n /** Define a provider whose acquisition can yield contextual Services. */\n static scopedGen<S extends ServiceClass<any, any>, Yield extends ServiceRequirement<unknown>>(\n service: S,\n factory: LayerGenerator<S, Yield>,\n release: (instance: InstanceType<S>, outcome: ScopeOutcome) => MaybePromise<void>\n ): LayerResult<ProviderEntry<InstanceType<S>, LayerGeneratorRequirements<S, Yield>>> {\n // SAFETY: The generator and release callback are checked against the requested Service.\n return new Layer([\n {\n service,\n acquire: () => runLayerGenerator(service, factory),\n release: (instance, outcome) => {\n // SAFETY: The backend invokes release with the instance acquired for this token.\n return release(instance as InstanceType<S>, outcome)\n }\n }\n ]) as LayerResult<ProviderEntry<InstanceType<S>, LayerGeneratorRequirements<S, Yield>>>\n }\n\n /** Define a provider whose acquisition can yield contextual Services. */\n static gen<S extends ServiceClass<any, any>, Yield extends ServiceRequirement<unknown>>(\n service: S,\n factory: LayerGenerator<S, Yield>\n ): LayerResult<ProviderEntry<InstanceType<S>, LayerGeneratorRequirements<S, Yield>>> {\n // SAFETY: The generator result is normalized to the requested Service at the runtime boundary.\n return new Layer([\n {\n service,\n acquire: () => runLayerGenerator(service, factory)\n }\n ]) as LayerResult<ProviderEntry<InstanceType<S>, LayerGeneratorRequirements<S, Yield>>>\n }\n\n /** Compose Layers without replacing providers. */\n static merge<const Layers extends readonly LayerInput[]>(\n ...layers: Layers & ValidateLayerTuple<Layers>\n ): MergeResult<Layers> {\n const providers = new Map<string, LayerProvider>()\n\n for (const layer of layers) {\n for (const provider of layer.providers) {\n const service = provider.service\n const existing = providers.get(service.serviceTag)\n\n if (existing) {\n if (existing.service !== service) {\n throw new ServiceTagCollisionError(existing.service, service)\n }\n\n throw new DuplicateServiceError(service)\n }\n\n providers.set(service.serviceTag, provider)\n }\n }\n\n // SAFETY: The heterogeneous provider list is erased only at this internal storage boundary.\n return new Layer([...providers.values()]) as MergeResult<Layers>\n }\n\n /** Replace providers in a base Layer, using tag identity and compatible contracts. */\n static override<Base extends LayerInput, const Overrides extends readonly LayerInput[]>(\n base: Base & ValidateLayerInput<Base>,\n ...overrides: Overrides & ValidateOverrides<Base, Overrides>\n ): OverrideLayerResult<Base, Overrides> {\n const providers = new Map<string, LayerProvider>()\n\n for (const provider of base.providers) {\n providers.set(provider.service.serviceTag, provider)\n }\n\n for (const layer of overrides) {\n for (const provider of layer.providers) {\n providers.set(provider.service.serviceTag, provider)\n }\n }\n\n // SAFETY: Runtime provider replacement preserves the computed override metadata.\n return new Layer([...providers.values()]) as OverrideLayerResult<Base, Overrides>\n }\n}\n\nconst normalizeAcquire =\n <S extends ServiceClass<any, any>>(\n acquire: () => MaybePromise<ServiceContract<InstanceType<S>>>\n ): (() => MaybePromise<InstanceType<S>>) =>\n () => {\n // SAFETY: ServiceContract removes only the declaration-only identity; runtime values are unchanged.\n return acquire() as MaybePromise<InstanceType<S>>\n }\n\n/** Type-level aliases for inspecting Layer environments and completeness. */\nexport declare namespace Layer {\n /** The widened Layer shape accepted by generic Layer infrastructure. */\n export type Any = LayerInput\n\n /** Extract the branded Service instances provided by a Layer. */\n export type Provided<L extends LayerInput> = ProvidedEnvironment<L>\n\n /** Extract the external Service requirements of a Layer. */\n export type Required<L extends LayerInput> = RequiredEnvironment<L>\n\n /** Validate a Layer's requirements and input shape. */\n export type Complete<L extends LayerInput> = CompleteInput<L>\n}\n","/** Thrown when Scope context is accessed outside an active Scope execution. */\nexport class ScopeRuntimeNotConfiguredError extends Error {\n constructor() {\n super('No Scope is available in the current execution context')\n\n this.name = 'ScopeRuntimeNotConfiguredError'\n }\n}\n\n/** Thrown when a resource or finalizer is added after Scope closure begins. */\nexport class ScopeClosedError extends Error {\n constructor() {\n super('Cannot add resources or finalizers to a closed Scope')\n\n this.name = 'ScopeClosedError'\n }\n}\n\n/** Aggregates finalizer failures encountered while closing a Scope. */\nexport class ScopeCloseError extends Error {\n constructor(readonly causes: readonly unknown[]) {\n super(\n `Failed to close Scope (${causes.length} finalizer${causes.length === 1 ? '' : 's'} failed)`\n )\n\n this.name = 'ScopeCloseError'\n }\n}\n\n/** Thrown when a value has neither Symbol.dispose nor Symbol.asyncDispose. */\nexport class ResourceNotDisposableError extends Error {\n constructor() {\n super('Resource does not implement Symbol.dispose or Symbol.asyncDispose')\n\n this.name = 'ResourceNotDisposableError'\n }\n}\n","import type { ScopeFinalizer } from './types'\n\nconst SCOPE_SUCCESS = { status: 'success' } as const\n\ntype Disposer = (...args: never[]) => void | PromiseLike<void>\n\ntype DisposableCandidate = {\n [Symbol.dispose]?: Disposer\n\n [Symbol.asyncDispose]?: Disposer\n}\n\n/** Return a Scope finalizer for a value's async or sync disposal protocol. */\nexport const getDisposeFinalizer = <Resource>(resource: Resource): ScopeFinalizer | undefined => {\n // SAFETY: Object() provides a property-bearing view for protocol lookup; each method is checked for callability before invocation.\n const candidate = Object(resource) as DisposableCandidate\n const asyncDispose = candidate[Symbol.asyncDispose]\n\n if (asyncDispose instanceof Function) {\n return () => asyncDispose.call(resource)\n }\n\n const dispose = candidate[Symbol.dispose]\n\n if (dispose instanceof Function) {\n return () => dispose.call(resource)\n }\n\n return undefined\n}\n\n/** Dispose a value immediately when it implements a disposal protocol. */\nexport const disposeResource = <Resource>(resource: Resource): void | PromiseLike<void> => {\n const finalizer = getDisposeFinalizer(resource)\n\n return finalizer?.(SCOPE_SUCCESS)\n}\n","import { AsyncLocalStorage } from 'node:async_hooks'\n\nimport { ScopeRuntimeNotConfiguredError } from './errors'\n\nimport type { Scope } from './scope'\n\nconst storage = new AsyncLocalStorage<Scope>()\n\n/** Bridges the current Scope through async execution context. */\nexport class ScopeRuntime {\n /** Supply a Scope while invoking a callback. */\n static run<A>(scope: Scope, program: () => A): A {\n return storage.run(scope, program)\n }\n\n /** Return the Scope active in the current execution context. */\n static current(): Scope {\n const scope = storage.getStore()\n\n if (!scope) {\n throw new ScopeRuntimeNotConfiguredError()\n }\n\n return scope\n }\n}\n","import { ScopeCloseError } from './errors'\n\nimport { ScopeRuntime } from './runtime'\n\nimport type { CloseableScope } from './scope'\n\nimport type { CleanupFailureDiagnostic, MaybePromise, ScopeOutcome } from './types'\n\nexport type OutcomeClassifier<A> = (value: A) => ScopeOutcome\n\nexport type RunScopedOptions<A> = {\n readonly classify: OutcomeClassifier<A>\n readonly onCleanupFailure?: (diagnostic: CleanupFailureDiagnostic) => MaybePromise<void>\n}\n\nconst notifyCleanupFailure = async (\n observer: ((diagnostic: CleanupFailureDiagnostic) => MaybePromise<void>) | undefined,\n diagnostic: CleanupFailureDiagnostic\n): Promise<void> => {\n if (!observer) {\n return\n }\n\n try {\n await observer(diagnostic)\n } catch {\n // Cleanup diagnostics are best effort and never affect the primary result.\n }\n}\n\nexport const runScoped = async <A>(\n scope: CloseableScope,\n program: () => A | PromiseLike<A>,\n options: RunScopedOptions<Awaited<A>>\n): Promise<Awaited<A>> => {\n let value!: Awaited<A>\n\n let programFailed = false\n let programFailure: unknown\n\n try {\n value = await ScopeRuntime.run(scope, program)\n } catch (cause) {\n programFailed = true\n programFailure = cause\n }\n\n const outcome: ScopeOutcome = programFailed\n ? {\n status: 'failure',\n cause: programFailure\n }\n : options.classify(value)\n\n let cleanupFailed = false\n let cleanupFailure: unknown\n\n try {\n await scope.close(outcome)\n } catch (cause) {\n cleanupFailed = true\n cleanupFailure = cause\n }\n\n if (cleanupFailed) {\n const error =\n cleanupFailure instanceof ScopeCloseError\n ? cleanupFailure\n : new ScopeCloseError([cleanupFailure])\n\n await notifyCleanupFailure(options.onCleanupFailure, {\n outcome,\n error\n })\n\n cleanupFailure = error\n }\n\n if (programFailed) {\n throw programFailure\n }\n\n if (outcome.status === 'failure') {\n return value\n }\n\n if (cleanupFailed) {\n throw cleanupFailure\n }\n\n return value\n}\n","import { ResourceNotDisposableError, ScopeCloseError, ScopeClosedError } from './errors'\n\nimport { getDisposeFinalizer } from './disposable'\n\nimport { runScoped } from './internal'\n\nimport { ScopeRuntime } from './runtime'\n\nimport type { DisposableResource, MaybePromise, ScopeFinalizer, ScopeOutcome } from './types'\n\n/**\n * Non-owning lifecycle context for finalizers and child Scopes.\n *\n * A Scope can register cleanup and create children, but it cannot close\n * itself. Use `Scope.make()` or `Scope.run()` when your code owns the Scope.\n */\nexport interface Scope {\n /** Register a finalizer that runs when the owning Scope closes. */\n addFinalizer(finalizer: ScopeFinalizer): void\n\n /** Acquire a resource and register its outcome-aware release callback. */\n acquire<R>(\n acquire: () => MaybePromise<R>,\n release: (resource: R, outcome: ScopeOutcome) => MaybePromise<void>\n ): Promise<R>\n\n /** Register an already-acquired disposable resource. */\n add<R extends DisposableResource>(resource: R): Promise<R>\n\n /** Create a child Scope owned by this Scope. */\n fork(): CloseableScope\n}\n\n/** A Scope whose owner is responsible for calling `close()`. */\nexport interface CloseableScope extends Scope {\n /** Close the Scope and run children and finalizers in child-first LIFO order. */\n close(outcome?: ScopeOutcome): Promise<void>\n}\n\nconst SCOPE_SUCCESS: ScopeOutcome = Object.freeze({ status: 'success' })\n\nclass ScopeImpl implements CloseableScope {\n private readonly children = new Set<ScopeImpl>()\n\n private readonly finalizers: ScopeFinalizer[] = []\n\n private closePromise: Promise<void> | undefined\n\n private closeOutcome: ScopeOutcome | undefined\n\n constructor(private parent?: ScopeImpl) {}\n\n fork(): CloseableScope {\n this.assertOpen()\n\n const child = new ScopeImpl(this)\n\n this.children.add(child)\n\n return child\n }\n\n addFinalizer(finalizer: ScopeFinalizer): void {\n this.assertOpen()\n\n this.finalizers.push(finalizer)\n }\n\n async acquire<R>(\n acquire: () => MaybePromise<R>,\n release: (resource: R, outcome: ScopeOutcome) => MaybePromise<void>\n ): Promise<R> {\n this.assertOpen()\n\n const resource = await acquire()\n\n try {\n this.addFinalizer((outcome) => release(resource, outcome))\n\n return resource\n } catch (scopeFailure) {\n try {\n await release(resource, this.closeOutcome ?? SCOPE_SUCCESS)\n } catch (releaseFailure) {\n throw new AggregateError(\n [scopeFailure, releaseFailure],\n 'Scope closed while acquiring a resource and immediate cleanup also failed'\n )\n }\n\n throw scopeFailure\n }\n }\n\n async add<R extends DisposableResource>(resource: R): Promise<R> {\n const finalizer = getDisposeFinalizer(resource)\n\n if (!finalizer) {\n throw new ResourceNotDisposableError()\n }\n\n try {\n this.addFinalizer(finalizer)\n\n return resource\n } catch (scopeFailure) {\n try {\n await finalizer(this.closeOutcome ?? SCOPE_SUCCESS)\n } catch (releaseFailure) {\n throw new AggregateError(\n [scopeFailure, releaseFailure],\n 'Scope closed while adding a disposable resource and cleanup also failed'\n )\n }\n\n throw scopeFailure\n }\n }\n\n close(outcome: ScopeOutcome = SCOPE_SUCCESS): Promise<void> {\n if (this.closePromise) {\n return this.closePromise\n }\n\n this.closeOutcome = outcome\n this.closePromise = ScopeRuntime.run(this, () => this.closeInternal(outcome))\n\n return this.closePromise\n }\n\n private async closeInternal(outcome: ScopeOutcome): Promise<void> {\n const failures: unknown[] = []\n\n const children = [...this.children]\n\n this.children.clear()\n\n for (let index = children.length - 1; index >= 0; index--) {\n const child = children[index]\n\n if (!child) {\n continue\n }\n\n try {\n await child.close(outcome)\n } catch (cause) {\n if (cause instanceof ScopeCloseError) {\n failures.push(...cause.causes)\n } else {\n failures.push(cause)\n }\n }\n }\n\n for (let index = this.finalizers.length - 1; index >= 0; index--) {\n const finalizer = this.finalizers[index]\n\n if (!finalizer) {\n continue\n }\n\n try {\n await finalizer(outcome)\n } catch (cause) {\n failures.push(cause)\n }\n }\n\n this.finalizers.length = 0\n\n this.detach()\n\n if (failures.length > 0) {\n throw new ScopeCloseError(failures)\n }\n }\n\n private detach(): void {\n const parent = this.parent\n\n if (!parent) {\n return\n }\n\n parent.children.delete(this)\n this.parent = undefined\n }\n\n private assertOpen(): void {\n if (this.closePromise) {\n throw new ScopeClosedError()\n }\n }\n}\n\nexport const Scope = {\n /** Create an owned, initially open Scope. */\n make(): CloseableScope {\n return new ScopeImpl()\n },\n\n /** Return the non-owning Scope available in the current execution context. */\n current(): Scope {\n return ScopeRuntime.current()\n },\n\n /** Run a callback with an existing Scope supplied as the current context. */\n provide<A>(scope: Scope, program: () => A): A {\n return ScopeRuntime.run(scope, program)\n },\n\n /** Resolve the current Scope through `yield* Scope` inside an Effect. */\n // oxlint-disable-next-line require-yield\n *[Symbol.iterator](): Generator<never, Scope, unknown> {\n return ScopeRuntime.current()\n },\n\n /**\n * Run a program in a newly owned Scope.\n *\n * Scope is independent from `better-result`, so returned values—including\n * `Result.err`—close this Scope with a successful outcome. Result-aware\n * outcome classification belongs to `Runtime.run`.\n *\n * @example\n * ```ts\n * await Scope.run(async (scope) => {\n * const connection = await scope.acquire(connect, (connection) => connection.close())\n * return connection.query()\n * })\n * ```\n */\n run<A>(program: (scope: Scope) => A | PromiseLike<A>): Promise<Awaited<A>> {\n const scope = new ScopeImpl()\n\n return runScoped(scope, () => program(scope), {\n classify: () => SCOPE_SUCCESS\n })\n }\n} as const\n\n/** Type-level aliases for Scope ownership, outcomes, and cleanup contracts. */\nexport declare namespace Scope {\n /** A Scope whose owner is responsible for calling `close()`. */\n export type Closeable = CloseableScope\n\n /** The outcome supplied to Scope finalizers and resource releases. */\n export type Outcome = ScopeOutcome\n\n /** A cleanup callback registered with a Scope. */\n export type Finalizer = ScopeFinalizer\n\n /** A value implementing a JavaScript disposal protocol. */\n export type Disposable = DisposableResource\n}\n","import { Result } from 'better-result'\n\nimport type { Result as ResultType } from 'better-result'\n\nimport type { AnyService } from '../service'\nimport { isPromiseLike } from '../utils/runtime'\nimport type { Effect, EffectError, EffectRequirements, EffectSuccess } from './types'\n\ntype EffectInput<A, E, Requirements extends AnyService> =\n | Effect<A, E, Requirements>\n | PromiseLike<Effect<A, E, Requirements>>\n\ntype AnyEffectInput = EffectInput<any, any, any>\ntype AnyEffectValue = Effect<any, any, any>\ntype AnyAsyncEffectInput = PromiseLike<AnyEffectValue>\ntype CombinatorCallback = (value: any) => any\ntype CombinatorInput = AnyEffectInput | CombinatorCallback\n\ntype PreserveAsync<Input, Output> = Input extends PromiseLike<unknown> ? Promise<Output> : Output\n\ntype MappedResult<Input, B> = Effect<B, EffectError<Input>, EffectRequirements<Input>>\n\ntype ErrorMappedResult<Input, E2> = Effect<EffectSuccess<Input>, E2, EffectRequirements<Input>>\n\ntype ChainedResult<First, Next> = Effect<\n EffectSuccess<Next>,\n EffectError<First> | EffectError<Next>,\n EffectRequirements<First> | EffectRequirements<Next>\n>\n\ntype ChainedOutput<First, Next> = ChainedResult<First, Next>\n\ntype AsyncChainedOutput<First, Next> = Promise<ChainedResult<First, Next>>\n\ntype MapOperation<A, B> = {\n <Input>(effect: Input & EffectInput<A, any, any>): PreserveAsync<Input, MappedResult<Input, B>>\n}\n\ntype MapErrorOperation<E1, E2> = {\n <Input>(\n effect: Input & EffectInput<any, E1, any>\n ): PreserveAsync<Input, ErrorMappedResult<Input, E2>>\n}\n\ntype AndThenOperation<A, Next> = {\n <Input>(effect: Input & Effect<A, any, any>): ChainedOutput<Input, Next>\n}\n\ntype AndThenAsyncOperation<A, Next> = {\n <Input>(effect: Input & EffectInput<A, any, any>): AsyncChainedOutput<Input, Next>\n}\n\nconst mapResult = <A, B, E, Requirements extends AnyService>(\n result: Effect<A, E, Requirements>,\n fn: (value: A) => B\n): Effect<B, E, Requirements> => {\n // SAFETY: Result.map changes only the success channel; Effect requirements are phantom metadata restored by this adapter.\n return Result.map(result, fn) as Effect<B, E, Requirements>\n}\n\nconst mapErrorResult = <A, E1, E2, Requirements extends AnyService>(\n result: Effect<A, E1, Requirements>,\n fn: (error: E1) => E2\n): Effect<A, E2, Requirements> => {\n // SAFETY: Result.mapError changes only the error channel; Effect requirements are phantom metadata restored by this adapter.\n return Result.mapError(result, fn) as Effect<A, E2, Requirements>\n}\n\nconst andThenResult = <\n A,\n B,\n E1,\n E2,\n Requirements1 extends AnyService,\n Requirements2 extends AnyService\n>(\n result: Effect<A, E1, Requirements1>,\n next: (value: A) => Effect<B, E2, Requirements2>\n): Effect<B, E1 | E2, Requirements1 | Requirements2> => {\n // SAFETY: The public callback returns an Effect, whose only runtime contract is the underlying Result.\n const resultNext = next as (value: A) => ResultType<B, E2>\n\n // SAFETY: Result.andThen unions Result errors; Effect requirements are phantom metadata restored by this adapter.\n return Result.andThen(result, resultNext) as Effect<B, E1 | E2, Requirements1 | Requirements2>\n}\n\nconst andThenAsyncResult = <\n A,\n B,\n E1,\n E2,\n Requirements1 extends AnyService,\n Requirements2 extends AnyService\n>(\n result: Effect<A, E1, Requirements1>,\n next: (value: A) => PromiseLike<Effect<B, E2, Requirements2>>\n): Promise<Effect<B, E1 | E2, Requirements1 | Requirements2>> => {\n // SAFETY: The public callback returns an Effect, whose only runtime contract is the underlying Result.\n const resultNext = (value: A) => {\n // SAFETY: Promise resolution preserves the callback's Result value; only phantom Effect metadata is erased.\n return Promise.resolve(next(value)) as Promise<ResultType<B, E2>>\n }\n\n // SAFETY: Result.andThenAsync unions Result errors; Effect requirements are phantom metadata restored by this adapter.\n return Result.andThenAsync(result, resultNext) as Promise<\n Effect<B, E1 | E2, Requirements1 | Requirements2>\n >\n}\n\n/**\n * Map the successful value of a Result or Effect result.\n *\n * Supports both data-first and data-last forms and preserves asynchronous\n * results and phantom Service requirements.\n *\n * @example\n * ```ts\n * const doubled = Effect.map(Result.ok(2), (value) => value * 2)\n * const toLabel = Effect.map((value: number) => `#${value}`)\n * ```\n */\nexport function map<A, B>(fn: (value: A) => B): MapOperation<A, B>\nexport function map<Input, B>(\n effect: Input & AnyEffectInput,\n fn: (value: EffectSuccess<Input>) => B\n): PreserveAsync<Input, MappedResult<Input, B>>\nexport function map(first: CombinatorInput, second?: CombinatorCallback): any {\n if (first instanceof Function && second === undefined) {\n // SAFETY: The curried overload accepts a unary mapping callback in this branch.\n const callback = first as CombinatorCallback\n\n return (effect: AnyEffectInput) => {\n // SAFETY: The overload implementation has already established the callback and Effect input positions.\n return map(effect as never, callback as never)\n }\n }\n\n // SAFETY: The data-first overload requires the second argument to be a unary mapping callback.\n const fn = second as CombinatorCallback\n\n if (isPromiseLike(first)) {\n return Promise.resolve(first).then((result) => {\n // SAFETY: Result is the runtime representation shared by Effect and better-result.\n return mapResult(result as Effect<any, any, never>, fn)\n })\n }\n\n // SAFETY: The data-first overload supplies a Result-compatible Effect value.\n return mapResult(first as Effect<any, any, never>, fn)\n}\n\n/**\n * Map the error value of a Result or Effect result while preserving its\n * successful value, asynchronous shape, and Service requirements.\n *\n * @example\n * ```ts\n * const labelled = Effect.mapError(Result.err('missing'), (error) => ({ error }))\n * ```\n */\nexport function mapError<E1, E2>(fn: (error: E1) => E2): MapErrorOperation<E1, E2>\nexport function mapError<Input, E2>(\n effect: Input & AnyEffectInput,\n fn: (error: EffectError<Input>) => E2\n): PreserveAsync<Input, ErrorMappedResult<Input, E2>>\nexport function mapError(first: CombinatorInput, second?: CombinatorCallback): any {\n if (first instanceof Function && second === undefined) {\n // SAFETY: The curried overload accepts a unary error-mapping callback in this branch.\n const callback = first as CombinatorCallback\n\n return (effect: AnyEffectInput) => {\n // SAFETY: The overload implementation has already established the callback and Effect input positions.\n return mapError(effect as never, callback as never)\n }\n }\n\n // SAFETY: The data-first overload requires the second argument to be a unary error-mapping callback.\n const fn = second as CombinatorCallback\n\n if (isPromiseLike(first)) {\n return Promise.resolve(first).then((result) => {\n // SAFETY: Result is the runtime representation shared by Effect and better-result.\n return mapErrorResult(result as Effect<any, any, never>, fn)\n })\n }\n\n // SAFETY: The data-first overload supplies a Result-compatible Effect value.\n return mapErrorResult(first as Effect<any, any, never>, fn)\n}\n\n/**\n * Chain a synchronous Result-producing operation after a successful result.\n *\n * The next operation is skipped when the input is an error. Both error types\n * and both sets of Service requirements are preserved in the output.\n *\n * @example\n * ```ts\n * const user = Effect.andThen(Result.ok('u1'), (id) => repository.find(id))\n * ```\n */\nexport function andThen<A, Next extends AnyEffectValue>(\n next: (value: A) => Next\n): AndThenOperation<A, Next>\nexport function andThen<Input, Next extends AnyEffectValue>(\n effect: Input & AnyEffectValue,\n next: (value: EffectSuccess<Input>) => Next\n): ChainedOutput<Input, Next>\nexport function andThen(first: CombinatorInput, second?: CombinatorCallback): any {\n if (first instanceof Function && second === undefined) {\n // SAFETY: The curried overload accepts a unary continuation in this branch.\n const callback = first as CombinatorCallback\n\n return (effect: AnyEffectInput) => {\n // SAFETY: The overload implementation has already established the callback and Effect input positions.\n return andThen(effect as never, callback as never)\n }\n }\n\n // SAFETY: The data-first overload requires the second argument to return an Effect.\n const next = second as CombinatorCallback\n\n // SAFETY: The data-first overload supplies a Result-compatible Effect value.\n return andThenResult(first as Effect<any, any, never>, next)\n}\n\n/**\n * Chain an asynchronous Result-producing operation after a successful result.\n *\n * The returned value is always a Promise and retains both operations' error\n * and Service-requirement metadata.\n *\n * @example\n * ```ts\n * const user = Effect.andThenAsync(loadUser(), (user) => fetchProfile(user.id))\n * ```\n */\nexport function andThenAsync<A, Next extends AnyAsyncEffectInput>(\n next: (value: A) => Next\n): AndThenAsyncOperation<A, Next>\nexport function andThenAsync<Input, Next extends AnyAsyncEffectInput>(\n effect: Input & AnyEffectInput,\n next: (value: EffectSuccess<Input>) => Next\n): AsyncChainedOutput<Input, Next>\nexport function andThenAsync(first: CombinatorInput, second?: CombinatorCallback): any {\n if (first instanceof Function && second === undefined) {\n // SAFETY: The curried overload accepts a unary asynchronous continuation in this branch.\n const callback = first as CombinatorCallback\n\n return (effect: AnyEffectInput) => {\n // SAFETY: The overload implementation has already established the callback and Effect input positions.\n return andThenAsync(effect as never, callback as never)\n }\n }\n\n // SAFETY: The data-first overload requires the second argument to return a PromiseLike Effect.\n const next = second as CombinatorCallback\n\n if (isPromiseLike(first)) {\n return Promise.resolve(first).then((result) => {\n // SAFETY: Result is the runtime representation shared by Effect and better-result.\n return andThenAsyncResult(result as Effect<any, any, never>, next)\n })\n }\n\n // SAFETY: The data-first overload supplies a Result-compatible Effect value.\n return andThenAsyncResult(first as Effect<any, any, never>, next)\n}\n","import { Result } from 'better-result'\n\nimport type { Err, Result as ResultType, UnhandledException } from 'better-result'\n\nimport { Scope } from '../scope'\n\nimport type { DisposableResource, MaybePromise, ScopeOutcome } from '../scope'\nimport type { AnyService } from '../service'\n\nimport type {\n AnyEffect,\n Effect as EffectType,\n EffectError,\n EffectFromGenerator,\n EffectRequirements,\n EffectSuccess,\n EffectYield\n} from './types'\n\nimport { andThen, andThenAsync, map, mapError } from './combinators'\n\nexport type Effect<A, E, R extends AnyService = never> = EffectType<A, E, R>\n\ntype AnyResult = ResultType<any, any>\n\ntype EffectGenerator =\n | (() => Generator<EffectYield, AnyResult, unknown>)\n | (() => AsyncGenerator<EffectYield, AnyResult, unknown>)\n\ntype RuntimeResultGenerator = (body: EffectGenerator) => AnyResult | Promise<AnyResult>\n\n/**\n * Compose `better-result` operations while preserving Service requirements in\n * a phantom type channel.\n *\n * A generator may yield Service tokens and Result operations. It must return a\n * `Result` as its final value; Service yields are resolved by the active\n * Runtime and do not add runtime values to the Result stream.\n *\n * @example\n * ```ts\n * const loadUser = Effect.gen(async function* () {\n * const database = yield* Database\n * const user = yield* Result.await(database.findUser('u1'))\n *\n * return Result.ok(user)\n * })\n * ```\n */\nexport function gen<Yield extends EffectYield, Returned extends AnyResult>(\n body: () => Generator<Yield, Returned, unknown>\n): EffectFromGenerator<Yield, Returned>\n\nexport function gen<Yield extends EffectYield, Returned extends AnyResult>(\n body: () => AsyncGenerator<Yield, Returned, unknown>\n): Promise<EffectFromGenerator<Yield, Returned>>\n\nexport function gen(body: EffectGenerator): AnyResult | Promise<AnyResult> {\n /*\n * ServiceRequirement is phantom. The Service iterator returns its resolved\n * instance without yielding a marker, so Result.gen still receives only\n * the Err values that exist at runtime.\n */\n // SAFETY: Service iterators yield no runtime markers, so the phantom Service yield channel is erased before delegating to better-result.\n const runResultGenerator = Result.gen as RuntimeResultGenerator\n\n return runResultGenerator(body)\n}\n\n/**\n * Acquire a resource in the current Scope and register its release callback.\n *\n * Acquisition failures are represented in the Effect Result error channel;\n * release failures remain owned by Scope cleanup. The release callback\n * receives the final outcome chosen by the enclosing execution boundary.\n *\n * @example\n * ```ts\n * const connection = yield* Effect.acquireRelease(\n * () => pool.connect(),\n * (connection, outcome) => connection.close(outcome)\n * )\n * ```\n */\nexport function acquireRelease<R>(\n acquire: () => MaybePromise<R>,\n release: (resource: R, outcome: ScopeOutcome) => MaybePromise<void>\n): AsyncGenerator<Err<never, UnhandledException>, R, unknown> {\n const scope = Scope.current()\n\n return Result.await(Result.tryPromise(() => scope.acquire(acquire, release)))\n}\n\n/**\n * Register an already-acquired disposable resource in the current Scope.\n *\n * The resource is not acquired by this helper. Registration failures are\n * represented in the Effect Result error channel; disposal failures remain\n * owned by Scope cleanup.\n *\n * @example\n * ```ts\n * const file = yield* Effect.add(await openFile('notes.txt'))\n * ```\n */\nexport function add<R extends DisposableResource>(\n resource: R\n): AsyncGenerator<Err<never, UnhandledException>, R, unknown> {\n const scope = Scope.current()\n\n return Result.await(Result.tryPromise(() => scope.add(resource)))\n}\n\n/**\n * Effect namespace containing generator, resource, and Result combinators.\n *\n * Prefer these helpers when a program needs typed Service requirements or\n * Scope-aware acquisition and cleanup.\n */\nexport const Effect = {\n /** Compose a generator-based Effect program. */\n gen,\n /** Acquire and register a resource in the current Scope. */\n acquireRelease,\n /** Register an already-acquired disposable in the current Scope. */\n add,\n /** Map a successful Effect result. */\n map,\n /** Map an Effect error. */\n mapError,\n /** Chain a synchronous Effect result. */\n andThen,\n /** Chain an asynchronous Effect result. */\n andThenAsync\n} as const\n\n/** Type-level aliases for inspecting Effect result channels and requirements. */\nexport declare namespace Effect {\n /** Extract the success channel from an Effect result or Promise. */\n export type Success<T> = EffectSuccess<T>\n\n /** Extract the error channel from an Effect result or Promise. */\n export type Error<T> = EffectError<T>\n\n /** Extract the Service requirements from an Effect result or Promise. */\n export type Requirements<T> = EffectRequirements<T>\n\n /** An Effect with erased success, error, and requirements. */\n export type Any = AnyEffect\n}\n","type Unary<A, B> = (value: A) => B\n\n/**\n * Compose a value through a sequence of unary functions.\n *\n * `pipe` is deliberately independent of Effect, Result, Promise, Scope, and\n * Service metadata.\n *\n * @example\n * ```ts\n * const label = pipe(\n * 'alice',\n * (name) => name.trim(),\n * (name) => name.toUpperCase()\n * )\n * ```\n */\ntype PipeRuntimeValue = Parameters<Unary<any, any>>[0]\n\ntype PipeRuntimeOperation = Unary<PipeRuntimeValue, PipeRuntimeValue>\n\nexport function pipe<A>(value: A): A\nexport function pipe<A, B>(value: A, ab: Unary<A, B>): B\nexport function pipe<A, B, C>(value: A, ab: Unary<A, B>, bc: Unary<B, C>): C\nexport function pipe<A, B, C, D>(value: A, ab: Unary<A, B>, bc: Unary<B, C>, cd: Unary<C, D>): D\nexport function pipe<A, B, C, D, E>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>\n): E\nexport function pipe<A, B, C, D, E, F>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>\n): F\nexport function pipe<A, B, C, D, E, F, G>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>\n): G\nexport function pipe<A, B, C, D, E, F, G, H>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>\n): H\nexport function pipe<A, B, C, D, E, F, G, H, I>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>,\n hi: Unary<H, I>\n): I\nexport function pipe<A, B, C, D, E, F, G, H, I, J>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>,\n hi: Unary<H, I>,\n ij: Unary<I, J>\n): J\nexport function pipe<A, B, C, D, E, F, G, H, I, J, K>(\n value: A,\n ab: Unary<A, B>,\n bc: Unary<B, C>,\n cd: Unary<C, D>,\n de: Unary<D, E>,\n ef: Unary<E, F>,\n fg: Unary<F, G>,\n gh: Unary<G, H>,\n hi: Unary<H, I>,\n ij: Unary<I, J>,\n jk: Unary<J, K>\n): K\nexport function pipe(\n value: PipeRuntimeValue,\n ...operations: ReadonlyArray<PipeRuntimeOperation>\n): PipeRuntimeValue {\n return operations.reduce((current, operation) => operation(current), value)\n}\n","import { TaggedError } from 'better-result'\n\n/** Describes a failure encountered while releasing a Resource. */\nexport class ResourceReleaseFailure extends TaggedError('ResourceReleaseFailure')<{\n readonly resource: string\n readonly cause: unknown\n readonly message: string\n}> {}\n","import { Result, type Result as ResultType, type UnhandledException } from 'better-result'\n\nimport { ResourceReleaseFailure } from './errors'\n\nimport { disposeResource } from '../scope/disposable'\n\nexport { disposeResource }\n\nimport type { AsyncResult, MaybePromise, ReleaseFailureObserver, ReleaseOutcome } from './types'\n\nconst toReleaseFailure = (resource: string, cause: unknown): ResourceReleaseFailure =>\n new ResourceReleaseFailure({\n resource,\n cause,\n message: `Failed to release resource: ${resource}`\n })\n\nexport const runResult = async <T, E>(\n operation: () => AsyncResult<T, E>\n): Promise<ResultType<T, E | UnhandledException>> => {\n const execution = await Result.tryPromise(() => Promise.resolve(operation()))\n\n return execution.andThen((result) => result)\n}\n\nconst normalizeReleaseOutcome = (\n name: string,\n outcome: ReleaseOutcome\n): ResultType<void, ResourceReleaseFailure> => {\n if (outcome === undefined) {\n return Result.ok()\n }\n\n return outcome.mapError((cause) => toReleaseFailure(name, cause))\n}\n\nexport const runRelease = async <R>(\n name: string,\n resource: R,\n release: (resource: R) => MaybePromise<ReleaseOutcome>\n): Promise<ResultType<void, ResourceReleaseFailure>> => {\n const execution = await Result.tryPromise({\n try: () => Promise.resolve(release(resource)),\n catch: (cause) => toReleaseFailure(name, cause)\n })\n\n return execution.andThen((outcome) => normalizeReleaseOutcome(name, outcome))\n}\n\nconst notifyReleaseFailure = async (\n observer: ReleaseFailureObserver | undefined,\n\n failure: ResourceReleaseFailure\n): Promise<void> => {\n if (!observer) {\n return\n }\n\n try {\n await observer(failure)\n } catch {\n /*\n * Diagnostics must never replace\n * the actual operation error.\n */\n }\n}\n\nexport const combineUseAndRelease = async <A, E>(\n used: ResultType<A, E>,\n\n released: ResultType<void, ResourceReleaseFailure>,\n\n onReleaseFailure?: ReleaseFailureObserver\n): Promise<ResultType<A, E | ResourceReleaseFailure>> => {\n if (Result.isError(used)) {\n if (Result.isError(released)) {\n await notifyReleaseFailure(onReleaseFailure, released.error)\n }\n\n return Result.err<A, E | ResourceReleaseFailure>(used.error)\n }\n\n if (Result.isError(released)) {\n await notifyReleaseFailure(onReleaseFailure, released.error)\n\n return Result.err<A, E | ResourceReleaseFailure>(released.error)\n }\n\n return Result.ok<A, E | ResourceReleaseFailure>(used.value)\n}\n","import { Result, type Result as ResultType, type UnhandledException } from 'better-result'\n\nimport { Scope } from '../scope'\n\nimport { ResourceReleaseFailure } from './errors'\n\nimport { combineUseAndRelease, disposeResource, runRelease, runResult } from './internal'\n\nimport type { AcquireUseReleaseOptions } from './types'\n\n/**\n * Acquire a resource, use it, and always attempt release afterward.\n *\n * Acquisition, use, and release may be synchronous or asynchronous Result\n * operations. If both use and release fail, the use error remains primary and\n * `onReleaseFailure` receives the cleanup failure as a diagnostic.\n *\n * When `release` is omitted, `Symbol.asyncDispose` is preferred over\n * `Symbol.dispose`.\n *\n * @example\n * ```ts\n * const result = await Resource.acquireUseRelease({\n * name: 'database connection',\n * acquire: () => connect(),\n * use: (connection) => query(connection),\n * release: (connection) => connection.close()\n * })\n * ```\n */\nconst acquireUseRelease = <R, A, AcquireError, UseError>({\n name,\n acquire,\n use,\n release = disposeResource,\n onReleaseFailure\n}: AcquireUseReleaseOptions<R, A, AcquireError, UseError>): Promise<\n ResultType<A, AcquireError | UseError | UnhandledException | ResourceReleaseFailure>\n> =>\n Result.gen(async function* () {\n const resource = yield* Result.await(runResult(acquire))\n\n const scope = Scope.make()\n\n let released: ResultType<void, ResourceReleaseFailure> = Result.ok()\n\n scope.addFinalizer(async () => {\n released = await runRelease(name, resource, release)\n })\n\n const used = await runResult(() => use(resource))\n\n await scope.close()\n\n return await combineUseAndRelease(used, released, onReleaseFailure)\n })\n\nexport const Resource = {\n /** Acquire, use, and release a resource with deterministic error precedence. */\n acquireUseRelease\n} as const\n","import { Result } from 'better-result'\n\nimport type { Result as ResultType } from 'better-result'\n\nimport type { LayerDisposeError } from '../layer/errors'\n\nimport type { CleanupFailureDiagnostic, MaybePromise, ScopeOutcome } from '../scope'\n\n/** Aggregated cleanup information reported during Runtime shutdown. */\nexport type RuntimeShutdownDiagnostic = {\n /** Final outcome supplied to the Runtime root Scope. */\n readonly outcome: ScopeOutcome\n /** Aggregated root-Scope and backend cleanup failure. */\n readonly error: LayerDisposeError\n}\n\n/** Observer notified about cleanup failures without changing primary results. */\nexport type CleanupFailureObserver = (\n diagnostic: CleanupFailureDiagnostic | RuntimeShutdownDiagnostic\n) => MaybePromise<void>\n\n/** Optional Runtime configuration for cleanup diagnostics. */\nexport type RuntimeOptions = {\n /** Optional observer for best-effort cleanup diagnostics. */\n readonly onCleanupFailure?: CleanupFailureObserver\n}\n\ntype ResultLike = ResultType<any, any>\n\nconst isResultLike = <A>(value: A): value is A & ResultLike => {\n const candidate = Object(value)\n const tag = Object.prototype.toString.call(value)\n\n return (\n tag !== '[object Function]' &&\n 'status' in candidate &&\n (candidate.status === 'ok' || candidate.status === 'error')\n )\n}\n\nexport const classifyRuntimeOutcome = <A>(value: A): ScopeOutcome => {\n if (isResultLike(value) && Result.isError(value)) {\n return {\n status: 'failure',\n cause: value.error\n }\n }\n\n return {\n status: 'success'\n }\n}\n","import { ServiceRuntime } from '../service'\n\nimport type { AnyService } from '../service'\n\nimport { Scope, type CloseableScope } from '../scope'\nimport { runScoped } from '../scope/internal'\nimport { ScopeRuntime } from '../scope/runtime'\n\nimport {\n classifyRuntimeOutcome,\n type CleanupFailureObserver,\n type RuntimeOptions,\n type RuntimeShutdownDiagnostic\n} from '../runtime/outcome'\n\nimport { LayerDisposeError, LayerRegistrationError } from './errors'\n\nimport type { LayerBackend } from './backend'\n\nimport type { LayerInput, CompleteExecution, CompleteInput, ProvidedEnvironment } from './inference'\n\nimport type { LayerRegistration } from './types'\n\nimport type { ScopeOutcome } from '../scope'\n\ntype LayerProvider = LayerInput['providers'][number]\n\ninterface RuntimeHandleCore<Provided extends AnyService> {\n /** The backend used to resolve this Layer's providers. */\n readonly backend: LayerBackend\n\n /** Run a program in a child Scope of the Layer's root Scope. */\n run<A>(program: CompleteExecution<Provided, A>): Promise<Awaited<A>>\n\n /** Stop new executions and release Layer-owned resources. */\n dispose(outcome?: ScopeOutcome): Promise<void>\n}\n\n/** Runtime-facing handle that owns a Layer's resources and execution scopes. */\nexport type RuntimeHandle<Provided extends AnyService = any> = RuntimeHandleCore<Provided>\n\nconst SCOPE_SUCCESS: ScopeOutcome = Object.freeze({ status: 'success' })\n\nclass RuntimeHandleDisposedError extends Error {\n constructor() {\n super('Cannot run a program using a disposed Layer')\n\n this.name = 'RuntimeHandleDisposedError'\n }\n}\n\nconst normalizeDisposeCauses = (cause: unknown): readonly unknown[] => {\n if (cause instanceof AggregateError) {\n return [...cause.errors]\n }\n\n return [cause]\n}\n\nconst notifyShutdownFailure = async (\n observer: CleanupFailureObserver | undefined,\n diagnostic: RuntimeShutdownDiagnostic\n): Promise<void> => {\n if (!observer) {\n return\n }\n\n try {\n await observer(diagnostic)\n } catch {\n // Shutdown diagnostics are best effort and never affect the primary result.\n }\n}\n\nconst bindProviderToScope = (\n provider: LayerProvider,\n rootScope: CloseableScope\n): LayerRegistration => ({\n service: provider.service,\n\n acquire: () =>\n ScopeRuntime.run(rootScope, async () => {\n if (!provider.release) {\n return await provider.acquire()\n }\n\n return await rootScope.acquire(\n () => provider.acquire(),\n (resource, outcome) => provider.release!(resource, outcome)\n )\n })\n})\n\nclass RuntimeHandleImpl<Provided extends AnyService> implements RuntimeHandleCore<Provided> {\n private disposePromise: Promise<void> | undefined\n\n private readonly executions = new Set<Promise<unknown>>()\n\n private state: 'active' | 'disposing' | 'disposed' = 'active'\n\n constructor(\n readonly backend: LayerBackend,\n private readonly rootScope: CloseableScope,\n private readonly onCleanupFailure: CleanupFailureObserver | undefined\n ) {}\n\n run<A>(program: CompleteExecution<Provided, A>): Promise<Awaited<A>> {\n this.assertActive()\n\n const executionScope = this.rootScope.fork()\n\n let resolveExecution!: (value: Awaited<A> | PromiseLike<Awaited<A>>) => void\n let rejectExecution!: (cause?: unknown) => void\n\n const execution = new Promise<Awaited<A>>((resolve, reject) => {\n resolveExecution = resolve\n rejectExecution = reject\n })\n\n this.executions.add(execution)\n\n void execution.then(\n () => {\n this.executions.delete(execution)\n },\n () => {\n this.executions.delete(execution)\n }\n )\n\n try {\n const running = this.runExecution(executionScope, program)\n\n void running.then(\n (value) => {\n resolveExecution(value)\n },\n (cause) => {\n rejectExecution(cause)\n }\n )\n } catch (cause) {\n rejectExecution(cause)\n }\n\n return execution\n }\n\n private runExecution<A>(\n executionScope: CloseableScope,\n program: CompleteExecution<Provided, A>\n ): Promise<Awaited<A>> {\n const options = this.onCleanupFailure\n ? {\n classify: classifyRuntimeOutcome,\n onCleanupFailure: this.onCleanupFailure\n }\n : {\n classify: classifyRuntimeOutcome\n }\n\n return runScoped(executionScope, () => ServiceRuntime.run(this.backend, program), options)\n }\n\n dispose(outcome: ScopeOutcome = SCOPE_SUCCESS): Promise<void> {\n if (this.disposePromise) {\n return this.disposePromise\n }\n\n this.state = 'disposing'\n\n const executions = [...this.executions]\n\n this.disposePromise = this.performDispose(executions, outcome)\n\n return this.disposePromise\n }\n\n private async performDispose(\n executions: readonly Promise<unknown>[],\n outcome: ScopeOutcome\n ): Promise<void> {\n const failures: unknown[] = []\n\n await Promise.allSettled(executions)\n\n try {\n await ServiceRuntime.run(this.backend, () => this.rootScope.close(outcome))\n } catch (cause) {\n failures.push(cause)\n }\n\n try {\n await this.backend.disposeAll()\n } catch (cause) {\n failures.push(cause)\n }\n\n this.state = 'disposed'\n\n if (failures.length > 0) {\n const error = new LayerDisposeError(failures.flatMap(normalizeDisposeCauses))\n\n await notifyShutdownFailure(this.onCleanupFailure, {\n outcome,\n error\n })\n\n throw error\n }\n }\n\n private assertActive(): void {\n if (this.state !== 'active') {\n throw new RuntimeHandleDisposedError()\n }\n }\n}\n\n/** Build a Runtime handle for a complete Layer and register its providers. */\nexport const createRuntimeHandle = async <L extends LayerInput>(\n layer: L & CompleteInput<L>,\n backend: LayerBackend,\n options: RuntimeOptions = {}\n): Promise<RuntimeHandle<ProvidedEnvironment<L>>> => {\n const rootScope = Scope.make()\n let current: LayerProvider | undefined\n\n try {\n for (const provider of layer.providers) {\n current = provider\n\n await backend.register(bindProviderToScope(provider, rootScope))\n }\n } catch (registrationCause) {\n const outcome: ScopeOutcome = {\n status: 'failure',\n cause: registrationCause\n }\n const cleanupCauses: unknown[] = []\n\n try {\n await ServiceRuntime.run(backend, () => rootScope.close(outcome))\n } catch (cause) {\n cleanupCauses.push(cause)\n }\n\n try {\n await backend.disposeAll()\n } catch (cause) {\n cleanupCauses.push(cause)\n }\n\n if (cleanupCauses.length > 0) {\n const shutdownError = new LayerDisposeError(cleanupCauses.flatMap(normalizeDisposeCauses))\n\n await notifyShutdownFailure(options.onCleanupFailure, {\n outcome,\n error: shutdownError\n })\n }\n\n const cleanupCause =\n cleanupCauses.length === 1\n ? cleanupCauses[0]\n : cleanupCauses.length > 1\n ? new LayerDisposeError(cleanupCauses.flatMap(normalizeDisposeCauses))\n : undefined\n\n throw new LayerRegistrationError(current?.service, registrationCause, cleanupCause)\n }\n\n return new RuntimeHandleImpl<ProvidedEnvironment<L>>(backend, rootScope, options.onCleanupFailure)\n}\n","import type { LayerBackend } from '../layer'\n\nimport { createRuntimeHandle, type RuntimeHandle } from '../layer/runtime'\n\nimport type { LayerInput, CompleteInput, ProvidedEnvironment } from '../layer/inference'\n\nimport type { CompleteExecution } from '../layer/inference'\n\nimport type { AnyService } from '../service'\n\nimport {\n classifyRuntimeOutcome,\n type RuntimeOptions,\n type RuntimeShutdownDiagnostic\n} from './outcome'\n\nimport type { ScopeOutcome } from '../scope'\n\nimport type { RuntimeFor } from './types'\n\n/**\n * Long-lived execution environment backed by a complete Layer.\n *\n * A Runtime owns Layer resources until `dispose()` is called. Each `run()` is\n * isolated in a child Scope, while Layer-scoped resources remain shared.\n *\n * @example\n * ```ts\n * const runtime = await Runtime.make(AppLive, new MemoryLayerBackend())\n * const result = await runtime.run(loadUser('u1'))\n * await runtime.dispose()\n * ```\n *\n * @typeParam Provided The branded Service instances supplied by the Layer.\n */\nexport class Runtime<Provided extends AnyService = any> {\n private constructor(private readonly handle: RuntimeHandle<any>) {}\n\n /**\n * Create a long-lived Runtime that owns its Layer resources.\n *\n * @example\n * ```ts\n * const runtime = await Runtime.make(AppLive, backend)\n * const result = await runtime.run(program)\n * await runtime.dispose()\n * ```\n */\n static async make<L extends LayerInput>(\n layer: L & CompleteInput<L>,\n backend: LayerBackend,\n options: RuntimeOptions = {}\n ): Promise<Runtime<ProvidedEnvironment<L>>> {\n const handle = await createRuntimeHandle(layer, backend, options)\n\n return new Runtime<ProvidedEnvironment<L>>(handle)\n }\n\n /**\n * Run one program and dispose its Layer resources before resolving.\n *\n * This is convenient for request-style or command-style execution where a\n * Runtime should not outlive the operation.\n */\n static async run<A, L extends LayerInput>(\n layer: L & CompleteInput<L>,\n backend: LayerBackend,\n program: CompleteExecution<ProvidedEnvironment<L>, A>,\n options: RuntimeOptions = {}\n ): Promise<Awaited<A>> {\n const runtime = await Runtime.make(layer, backend, options)\n\n let value!: Awaited<A>\n let executionFailed = false\n let executionFailure: unknown\n let programOutcome: ScopeOutcome | undefined\n\n try {\n value = await runtime.runUnchecked(async () => {\n try {\n const programValue = await program()\n\n programOutcome = classifyRuntimeOutcome(programValue)\n\n return programValue\n } catch (cause) {\n programOutcome = {\n status: 'failure',\n cause\n }\n\n throw cause\n }\n })\n } catch (cause) {\n executionFailed = true\n executionFailure = cause\n }\n\n const outcome: ScopeOutcome =\n programOutcome ??\n ({\n status: 'failure',\n cause: executionFailure\n } as const)\n\n try {\n await runtime.disposeWithOutcome(outcome)\n } catch (shutdownFailure) {\n if (!executionFailed && outcome.status === 'success') {\n throw shutdownFailure\n }\n }\n\n if (executionFailed) {\n throw executionFailure\n }\n\n return value\n }\n\n /** Run one execution in this Runtime's child Scope. */\n run<A>(program: CompleteExecution<Provided, A>): Promise<Awaited<A>> {\n return this.handle.run(program)\n }\n\n private runUnchecked<A>(program: () => A | PromiseLike<A>): Promise<Awaited<A>> {\n // SAFETY: One-shot Runtime.run performs the same complete-program validation at its public boundary before using this internal escape hatch.\n return this.handle.run(program as CompleteExecution<Provided, A>)\n }\n\n /** Stop new executions and release the Runtime's Layer resources. */\n dispose(): Promise<void> {\n return this.handle.dispose()\n }\n\n private disposeWithOutcome(outcome: ScopeOutcome): Promise<void> {\n return this.handle.dispose(outcome)\n }\n}\n\n/** Type-level aliases for naming Runtime handles and shutdown options. */\nexport declare namespace Runtime {\n /** Name a Runtime type from a concrete Layer. */\n export type For<L extends LayerInput> = RuntimeFor<L>\n\n /** Optional Runtime shutdown configuration. */\n export type Options = RuntimeOptions\n\n /** Diagnostic reported for aggregated Runtime shutdown cleanup failures. */\n export type ShutdownDiagnostic = RuntimeShutdownDiagnostic\n}\n"],"mappings":";;;;;AAYA,MAAMA,YAAU,IAAI,kBAAmC;;AAGvD,IAAa,iBAAb,MAAa,eAAe;;;;;;;;;;;;;CAa1B,OAAO,IAAO,UAA2B,SAAqB;EAC5D,OAAOA,UAAQ,IAAI,UAAU,OAAO;CACtC;;CAGA,OAAO,UAA2B;EAChC,MAAM,WAAWA,UAAQ,SAAS;EAElC,IAAI,CAAC,UACH,MAAM,IAAI,iCAAiC;EAG7C,OAAO;CACT;;CAGA,aAAa,QAAmC,OAAoC;EAGlF,OAAO,MAFU,eAAe,QAEZ,CAAC,CAAC,QAAQ,KAAK;CACrC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;ACWA,SAAgB,UAAsC;CACpD,OAAO,SAAoC,KAA6B;EACtE,IAAI,IAAI,WAAW,GACjB,MAAM,IAAI,UAAU,gCAAgC;EAGtD,MAAe,YAA4C;;GAEzD,OAAgB,aAAkB;;;;;;;;;;;;;;;;;;;;;;;;GA0BlC,OAAO,GAAe,gBAAoE;IAExF,OAAO;GACT;;GAIA,eAAe,OAAO,iBAEqC;IACzD,OAAO,MAAM,eAAe,QAAQ,IAAI;GAC1C;EACF;EAEA,OAAO;CACT;AACF;;;ACvGA,MAAa,oBAAoB,OAI/B,SACA,YAC6B;CAC7B,MAAM,WAAW,QAAQ;CAEzB,MAAM,QAAQ,MAAM,SAAS,KAAK;CAElC,IAAI,CAAC,MAAM,MACT,IAAI;EAEF,MAAM,SAAS,OAAO,KAAA,CAAkB;CAC1C,UAAU;EAER,MAAM,IAAI,yBAAyB,OAAO;CAC5C;CAIF,OAAO,MAAM;AACf;;;;;;;;;;;;;;;;;;;;AC8BA,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Aa,QAA2C;CACjB;CAArC,YAAoB,QAA6C;EAA5B,KAAA,SAAA;CAA6B;;;;;;;;;;;CAYlE,aAAa,KACX,OACA,SACA,UAA0B,CAAC,GACe;EAC1C,MAAM,SAAS,MAAM,oBAAoB,OAAO,SAAS,OAAO;EAEhE,OAAO,IAAI,QAAgC,MAAM;CACnD;;;;;;;CAQA,aAAa,IACX,OACA,SACA,SACA,UAA0B,CAAC,GACN;EACrB,MAAM,UAAU,MAAM,QAAQ,KAAK,OAAO,SAAS,OAAO;EAE1D,IAAI;EACJ,IAAI,kBAAkB;EACtB,IAAI;EACJ,IAAI;EAEJ,IAAI;GACF,QAAQ,MAAM,QAAQ,aAAa,YAAY;IAC7C,IAAI;KACF,MAAM,eAAe,MAAM,QAAQ;KAEnC,iBAAiB,uBAAuB,YAAY;KAEpD,OAAO;IACT,SAAS,OAAO;KACd,iBAAiB;MACf,QAAQ;MACR;KACF;KAEA,MAAM;IACR;GACF,CAAC;EACH,SAAS,OAAO;GACd,kBAAkB;GAClB,mBAAmB;EACrB;EAEA,MAAM,UACJ,kBACC;GACC,QAAQ;GACR,OAAO;EACT;EAEF,IAAI;GACF,MAAM,QAAQ,mBAAmB,OAAO;EAC1C,SAAS,iBAAiB;GACxB,IAAI,CAAC,mBAAmB,QAAQ,WAAW,WACzC,MAAM;EAEV;EAEA,IAAI,iBACF,MAAM;EAGR,OAAO;CACT;;CAGA,IAAO,SAA8D;EACnE,OAAO,KAAK,OAAO,IAAI,OAAO;CAChC;CAEA,aAAwB,SAAwD;EAE9E,OAAO,KAAK,OAAO,IAAI,OAAyC;CAClE;;CAGA,UAAyB;EACvB,OAAO,KAAK,OAAO,QAAQ;CAC7B;CAEA,mBAA2B,SAAsC;EAC/D,OAAO,KAAK,OAAO,QAAQ,OAAO;CACpC;AACF"}
|
|
@@ -17,10 +17,11 @@ const serviceMemberNames = (token) => {
|
|
|
17
17
|
*/
|
|
18
18
|
const assertServiceCompatibility = (requested, registered, instance) => {
|
|
19
19
|
if (requested === registered || requested.serviceTag !== registered.serviceTag) return;
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
const candidate = Object(instance);
|
|
21
|
+
if (candidate !== instance) throw new ServiceTagCollisionError(registered, requested);
|
|
22
|
+
for (const name of serviceMemberNames(requested)) if (!(name in candidate)) throw new ServiceTagCollisionError(registered, requested);
|
|
22
23
|
};
|
|
23
24
|
//#endregion
|
|
24
25
|
export { assertServiceCompatibility as t };
|
|
25
26
|
|
|
26
|
-
//# sourceMappingURL=internal-identity-
|
|
27
|
+
//# sourceMappingURL=internal-identity-C6Awrc33.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-identity-C6Awrc33.mjs","names":[],"sources":["../src/layer/internal-identity.ts"],"sourcesContent":["import type { AnyServiceToken } from '../service'\n\nimport { ServiceTagCollisionError } from './errors'\n\nimport type { LayerRegistration } from './types'\n\ntype LayerAcquiredValue = Awaited<ReturnType<LayerRegistration['acquire']>>\n\nconst serviceMemberNames = (token: AnyServiceToken): readonly (string | symbol)[] => {\n const names = new Set<string | symbol>()\n let prototype = token.prototype\n\n while (prototype && prototype !== Object.prototype) {\n for (const name of Object.getOwnPropertyNames(prototype)) {\n if (name !== 'constructor') {\n names.add(name)\n }\n }\n\n for (const symbol of Object.getOwnPropertySymbols(prototype)) {\n names.add(symbol)\n }\n\n prototype = Object.getPrototypeOf(prototype)\n }\n\n return [...names]\n}\n\n/**\n * Check the runtime portion of a same-tag association before returning it.\n * TypeScript remains authoritative for full structural compatibility; this\n * check catches the common incompatible-method collision after erasure.\n */\nexport const assertServiceCompatibility = (\n requested: AnyServiceToken,\n registered: AnyServiceToken,\n instance: LayerAcquiredValue\n): void => {\n if (requested === registered || requested.serviceTag !== registered.serviceTag) {\n return\n }\n\n const candidate = Object(instance)\n\n // SAFETY: Service providers are object instances; Object() lets this runtime boundary reject rogue primitive values without trusting their static type.\n if (candidate !== instance) {\n throw new ServiceTagCollisionError(registered, requested)\n }\n\n for (const name of serviceMemberNames(requested)) {\n if (!(name in candidate)) {\n throw new ServiceTagCollisionError(registered, requested)\n }\n }\n}\n"],"mappings":";;AAQA,MAAM,sBAAsB,UAAyD;CACnF,MAAM,wBAAQ,IAAI,IAAqB;CACvC,IAAI,YAAY,MAAM;CAEtB,OAAO,aAAa,cAAc,OAAO,WAAW;EAClD,KAAK,MAAM,QAAQ,OAAO,oBAAoB,SAAS,GACrD,IAAI,SAAS,eACX,MAAM,IAAI,IAAI;EAIlB,KAAK,MAAM,UAAU,OAAO,sBAAsB,SAAS,GACzD,MAAM,IAAI,MAAM;EAGlB,YAAY,OAAO,eAAe,SAAS;CAC7C;CAEA,OAAO,CAAC,GAAG,KAAK;AAClB;;;;;;AAOA,MAAa,8BACX,WACA,YACA,aACS;CACT,IAAI,cAAc,cAAc,UAAU,eAAe,WAAW,YAClE;CAGF,MAAM,YAAY,OAAO,QAAQ;CAGjC,IAAI,cAAc,UAChB,MAAM,IAAI,yBAAyB,YAAY,SAAS;CAG1D,KAAK,MAAM,QAAQ,mBAAmB,SAAS,GAC7C,IAAI,EAAE,QAAQ,YACZ,MAAM,IAAI,yBAAyB,YAAY,SAAS;AAG9D"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/utils/runtime.ts
|
|
2
|
+
/** Detect a thenable while preserving the caller's value type. */
|
|
3
|
+
const isPromiseLike = (value) => {
|
|
4
|
+
const candidate = Object(value);
|
|
5
|
+
return "then" in candidate && candidate.then instanceof Function;
|
|
6
|
+
};
|
|
7
|
+
//#endregion
|
|
8
|
+
export { isPromiseLike as t };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=runtime-CDcCF5cb.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-CDcCF5cb.mjs","names":[],"sources":["../src/utils/runtime.ts"],"sourcesContent":["/** Detect a thenable while preserving the caller's value type. */\nexport const isPromiseLike = <Value>(value: Value): value is Value & PromiseLike<unknown> => {\n const candidate = Object(value)\n\n return 'then' in candidate && candidate.then instanceof Function\n}\n"],"mappings":";;AACA,MAAa,iBAAwB,UAAwD;CAC3F,MAAM,YAAY,OAAO,KAAK;CAE9B,OAAO,UAAU,aAAa,UAAU,gBAAgB;AAC1D"}
|
package/dist/testing.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as LayerRegistration, t as LayerBackend, z as AnyServiceToken } from "./index-DMfjhNR_.mjs";
|
|
2
2
|
//#region src/testing/memory-layer-backend.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* In-memory Layer backend for tests and small local programs.
|
package/dist/testing.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.d.mts","names":[],"sources":["../src/testing/memory-layer-backend.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"testing.d.mts","names":[],"sources":["../src/testing/memory-layer-backend.ts"],"mappings":";;;;;;;;cAmBa,8BAA8B;mBACxB;mBAEA;mBAEA;;EAGjB,SAAS,cAAc;;EAgBjB,QAAQ,UAAU,iBAAiB,OAAO,IAAI,QAAQ,aAAa;;EA4CnE,cAAc"}
|
package/dist/testing.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as ServiceTagCollisionError, o as ServiceNotFoundError, t as DuplicateServiceError } from "./errors-GR3K_nRu.mjs";
|
|
2
|
-
import { t as assertServiceCompatibility } from "./internal-identity-
|
|
2
|
+
import { t as assertServiceCompatibility } from "./internal-identity-C6Awrc33.mjs";
|
|
3
3
|
//#region src/testing/memory-layer-backend.ts
|
|
4
4
|
/**
|
|
5
5
|
* In-memory Layer backend for tests and small local programs.
|
|
@@ -30,7 +30,8 @@ var MemoryLayerBackend = class {
|
|
|
30
30
|
assertServiceCompatibility(token, provider.service, instance);
|
|
31
31
|
return instance;
|
|
32
32
|
};
|
|
33
|
-
|
|
33
|
+
const cached = this.instances.get(tag);
|
|
34
|
+
if (cached !== void 0) return validate(cached);
|
|
34
35
|
const pending = this.pending.get(tag);
|
|
35
36
|
if (pending) return validate(await pending);
|
|
36
37
|
const acquisition = Promise.resolve(provider.acquire()).then((instance) => {
|
package/dist/testing.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.mjs","names":[],"sources":["../src/testing/memory-layer-backend.ts"],"sourcesContent":["import {\n DuplicateServiceError,\n ServiceTagCollisionError,\n type LayerBackend,\n type LayerRegistration\n} from '../layer'\n\nimport { ServiceNotFoundError, type AnyServiceToken } from '../service'\n\nimport { assertServiceCompatibility } from '../layer/internal-identity'\n\n/**\n * In-memory Layer backend for tests and small local programs.\n *\n * Providers are lazy, instances are cached by Service tag, and all cached\n * state is cleared by `disposeAll()`.\n */\nexport class MemoryLayerBackend implements LayerBackend {\n private readonly providers = new Map<string, LayerRegistration>()\n\n private readonly instances = new Map<string,
|
|
1
|
+
{"version":3,"file":"testing.mjs","names":[],"sources":["../src/testing/memory-layer-backend.ts"],"sourcesContent":["import {\n DuplicateServiceError,\n ServiceTagCollisionError,\n type LayerBackend,\n type LayerRegistration\n} from '../layer'\n\nimport { ServiceNotFoundError, type AnyServiceToken } from '../service'\n\nimport { assertServiceCompatibility } from '../layer/internal-identity'\n\ntype LayerAcquiredValue = Awaited<ReturnType<LayerRegistration['acquire']>>\n\n/**\n * In-memory Layer backend for tests and small local programs.\n *\n * Providers are lazy, instances are cached by Service tag, and all cached\n * state is cleared by `disposeAll()`.\n */\nexport class MemoryLayerBackend implements LayerBackend {\n private readonly providers = new Map<string, LayerRegistration>()\n\n private readonly instances = new Map<string, LayerAcquiredValue>()\n\n private readonly pending = new Map<string, Promise<LayerAcquiredValue>>()\n\n /** Register a provider, rejecting duplicate or colliding Service tags. */\n register(registration: LayerRegistration): void {\n const tag = registration.service.serviceTag\n const existing = this.providers.get(tag)\n\n if (existing) {\n if (existing.service !== registration.service) {\n throw new ServiceTagCollisionError(existing.service, registration.service)\n }\n\n throw new DuplicateServiceError(registration.service)\n }\n\n this.providers.set(tag, registration)\n }\n\n /** Resolve and cache a provider instance by Service tag. */\n async resolve<T extends AnyServiceToken>(token: T): Promise<InstanceType<T>> {\n const tag = token.serviceTag\n const provider = this.providers.get(tag)\n\n if (!provider) {\n throw new ServiceNotFoundError(token)\n }\n\n const validate = (instance: LayerAcquiredValue): InstanceType<T> => {\n assertServiceCompatibility(token, provider.service, instance)\n\n // SAFETY: The provider and requested token share a tag, and compatibility checks verify the registered members before restoring the token-specific instance type.\n return instance as InstanceType<T>\n }\n\n const cached = this.instances.get(tag)\n\n if (cached !== undefined) {\n return validate(cached)\n }\n\n const pending = this.pending.get(tag)\n\n if (pending) {\n return validate(await pending)\n }\n\n const acquisition = Promise.resolve(provider.acquire())\n .then((instance) => {\n validate(instance)\n this.instances.set(tag, instance)\n\n return instance\n })\n .finally(() => {\n this.pending.delete(tag)\n })\n\n this.pending.set(tag, acquisition)\n\n return validate(await acquisition)\n }\n\n /** Clear pending acquisitions, cached instances, and provider registrations. */\n async disposeAll(): Promise<void> {\n if (this.pending.size > 0) {\n await Promise.allSettled(this.pending.values())\n }\n\n this.instances.clear()\n this.pending.clear()\n this.providers.clear()\n }\n}\n"],"mappings":";;;;;;;;;AAmBA,IAAa,qBAAb,MAAwD;CACtD,4BAA6B,IAAI,IAA+B;CAEhE,4BAA6B,IAAI,IAAgC;CAEjE,0BAA2B,IAAI,IAAyC;;CAGxE,SAAS,cAAuC;EAC9C,MAAM,MAAM,aAAa,QAAQ;EACjC,MAAM,WAAW,KAAK,UAAU,IAAI,GAAG;EAEvC,IAAI,UAAU;GACZ,IAAI,SAAS,YAAY,aAAa,SACpC,MAAM,IAAI,yBAAyB,SAAS,SAAS,aAAa,OAAO;GAG3E,MAAM,IAAI,sBAAsB,aAAa,OAAO;EACtD;EAEA,KAAK,UAAU,IAAI,KAAK,YAAY;CACtC;;CAGA,MAAM,QAAmC,OAAoC;EAC3E,MAAM,MAAM,MAAM;EAClB,MAAM,WAAW,KAAK,UAAU,IAAI,GAAG;EAEvC,IAAI,CAAC,UACH,MAAM,IAAI,qBAAqB,KAAK;EAGtC,MAAM,YAAY,aAAkD;GAClE,2BAA2B,OAAO,SAAS,SAAS,QAAQ;GAG5D,OAAO;EACT;EAEA,MAAM,SAAS,KAAK,UAAU,IAAI,GAAG;EAErC,IAAI,WAAW,KAAA,GACb,OAAO,SAAS,MAAM;EAGxB,MAAM,UAAU,KAAK,QAAQ,IAAI,GAAG;EAEpC,IAAI,SACF,OAAO,SAAS,MAAM,OAAO;EAG/B,MAAM,cAAc,QAAQ,QAAQ,SAAS,QAAQ,CAAC,CAAC,CACpD,MAAM,aAAa;GAClB,SAAS,QAAQ;GACjB,KAAK,UAAU,IAAI,KAAK,QAAQ;GAEhC,OAAO;EACT,CAAC,CAAC,CACD,cAAc;GACb,KAAK,QAAQ,OAAO,GAAG;EACzB,CAAC;EAEH,KAAK,QAAQ,IAAI,KAAK,WAAW;EAEjC,OAAO,SAAS,MAAM,WAAW;CACnC;;CAGA,MAAM,aAA4B;EAChC,IAAI,KAAK,QAAQ,OAAO,GACtB,MAAM,QAAQ,WAAW,KAAK,QAAQ,OAAO,CAAC;EAGhD,KAAK,UAAU,MAAM;EACrB,KAAK,QAAQ,MAAM;EACnB,KAAK,UAAU,MAAM;CACvB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-effect",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Effect-inspired application architecture for better-result with typechecked Services and environments, scoped resource lifetimes, and pluggable DI backends.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"better-result",
|
|
@@ -37,15 +37,23 @@
|
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsdown",
|
|
40
|
-
"
|
|
40
|
+
"dev": "tsdown --watch",
|
|
41
|
+
"typecheck": "tsc --noEmit && bun run typecheck:example",
|
|
42
|
+
"typecheck:example": "tsc -p examples/todo-api/tsconfig.json",
|
|
41
43
|
"test": "bun test",
|
|
42
44
|
"test:coverage": "bun test --coverage",
|
|
45
|
+
"test:package-types": "tsc --version && tsc -p tests/package/public-type-namespaces/tsconfig.json",
|
|
46
|
+
"test:package-types:minimum": "bunx --bun --package typescript@5.7.2 tsc --version && bunx --bun --package typescript@5.7.2 tsc -p tests/package/public-type-namespaces/tsconfig.json",
|
|
47
|
+
"test:package-variance": "rm -rf tests/package/public-type-variance/out && tsc --version && tsc -p tests/package/public-type-variance/tsconfig.json && rm -rf tests/package/public-type-variance/out",
|
|
48
|
+
"test:package-variance:minimum": "rm -rf tests/package/public-type-variance/out && bunx --bun --package typescript@5.7.2 tsc --version && bunx --bun --package typescript@5.7.2 tsc -p tests/package/public-type-variance/tsconfig.json && rm -rf tests/package/public-type-variance/out",
|
|
43
49
|
"lint": "oxlint --type-aware .",
|
|
44
50
|
"lint:fix": "oxlint --type-aware --fix .",
|
|
45
51
|
"format": "oxfmt --write .",
|
|
46
|
-
"format:check": "oxfmt --check .",
|
|
47
52
|
"publint": "publint",
|
|
48
|
-
"check": "bun run
|
|
53
|
+
"check:public-type-namespaces": "bun run test:package-types && bun run test:package-types:minimum && bun tests/package/public-type-namespaces/check.ts",
|
|
54
|
+
"check:public-type-variance": "bun run test:package-variance && bun run test:package-variance:minimum && bun tests/package/public-type-variance/check.ts",
|
|
55
|
+
"check:instance-requirements": "bun tests/package/instance-requirements/check.ts",
|
|
56
|
+
"check": "bun run typecheck && bun test && bun run format && bun run build && bun run check:public-type-namespaces && bun run check:public-type-variance && bun run check:instance-requirements && bun run publint && bun run lint",
|
|
49
57
|
"prepublishOnly": "bun run check",
|
|
50
58
|
"release:dry": "bun publish --dry-run"
|
|
51
59
|
},
|
|
@@ -62,7 +70,7 @@
|
|
|
62
70
|
"peerDependencies": {
|
|
63
71
|
"better-result": "^3.0.0",
|
|
64
72
|
"iti": "^0.8.0",
|
|
65
|
-
"typescript": ">=5.
|
|
73
|
+
"typescript": ">=5.7.0"
|
|
66
74
|
},
|
|
67
75
|
"peerDependenciesMeta": {
|
|
68
76
|
"iti": {
|