katagami 3.0.3 → 4.0.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.
Files changed (44) hide show
  1. package/README.md +9 -7
  2. package/dist/chunk-R66WOZUM.js +71 -0
  3. package/dist/container/index.d.cts +142 -35
  4. package/dist/container/index.d.ts +142 -35
  5. package/dist/container/policy.d.cts +36 -0
  6. package/dist/container/policy.d.ts +36 -0
  7. package/dist/disposable/index.cjs +27 -5
  8. package/dist/disposable/index.d.cts +17 -7
  9. package/dist/disposable/index.d.ts +17 -7
  10. package/dist/disposable/index.js +2 -41
  11. package/dist/entrypoint/index.d.cts +20 -0
  12. package/dist/entrypoint/index.d.ts +20 -0
  13. package/dist/index.cjs +551 -149
  14. package/dist/index.d.cts +8 -0
  15. package/dist/index.d.ts +8 -0
  16. package/dist/index.js +495 -150
  17. package/dist/internal.d.cts +65 -0
  18. package/dist/internal.d.ts +65 -0
  19. package/dist/metadata/index.d.cts +67 -0
  20. package/dist/metadata/index.d.ts +67 -0
  21. package/dist/resolver/index.d.cts +5 -0
  22. package/dist/resolver/index.d.ts +5 -0
  23. package/dist/scope/index.d.cts +49 -24
  24. package/dist/scope/index.d.ts +49 -24
  25. package/dist/scope/operations.d.cts +22 -0
  26. package/dist/scope/operations.d.ts +22 -0
  27. package/docs/README.de.md +3 -2
  28. package/docs/README.es.md +3 -2
  29. package/docs/README.fr.md +3 -2
  30. package/docs/README.ja.md +7 -6
  31. package/docs/README.ko.md +3 -2
  32. package/docs/README.zh-CN.md +3 -2
  33. package/docs/README.zh-TW.md +3 -2
  34. package/docs/ai-coding-agents.md +3 -3
  35. package/docs/articles/ai-coding-agents.md +1 -1
  36. package/docs/articles/request-scope.md +2 -1
  37. package/docs/choosing-di.md +7 -6
  38. package/docs/guide.md +35 -4
  39. package/docs/registration-policies.ja.md +261 -0
  40. package/docs/registration-policies.md +435 -0
  41. package/docs/type-safety.md +13 -4
  42. package/llms.txt +1 -0
  43. package/package.json +1 -1
  44. package/dist/chunk-J2NYR3SH.js +0 -6
@@ -1,5 +1,84 @@
1
- import { type ContainerInternals, INTERNALS } from '../internal.js';
1
+ import { type Callable, type EntrypointFactory } from '../entrypoint/index.js';
2
+ import { type ContainerInternals, INTERNALS, type REGISTRATION_STATE } from '../internal.js';
3
+ import { type AnyMetadataEntry, type AnyMetadataKey, type MetadataKeyOf, type MetadataReader, type RegistrationArguments } from '../metadata/index.js';
2
4
  import type { AbstractConstructor, Resolver } from '../resolver/index.js';
5
+ import { type ContainerPolicy, type RequiredMetadata } from './policy.js';
6
+ export type RegisteredTokens<C> = C extends {
7
+ readonly [REGISTRATION_STATE]: infer S;
8
+ } ? S extends {
9
+ readonly untracked: true;
10
+ } ? never : S extends {
11
+ readonly token: infer K;
12
+ } ? K : never : never;
13
+ export interface RegistrationState {
14
+ readonly token: unknown;
15
+ readonly metadata: AnyMetadataKey;
16
+ readonly callable: Callable | never;
17
+ }
18
+ type State<K, E extends readonly AnyMetadataEntry[], F = never> = {
19
+ readonly token: K;
20
+ readonly metadata: MetadataKeyOf<E[number]>;
21
+ readonly callable: F;
22
+ };
23
+ type Replace<S, K, N> = (unknown extends S ? UntrackedState : Exclude<S, {
24
+ readonly token: K;
25
+ }>) | N;
26
+ type UntrackedState<K = unknown> = {
27
+ readonly token: K;
28
+ readonly metadata: never;
29
+ readonly callable: never;
30
+ readonly untracked: true;
31
+ };
32
+ type SourceState<S, K> = unknown extends S ? UntrackedState<K> : S extends {
33
+ readonly untracked: true;
34
+ } ? UntrackedState<K> : S;
35
+ type SourceTokens<S, K> = unknown extends S ? K : S extends {
36
+ readonly untracked: true;
37
+ } ? K : S extends {
38
+ readonly token: infer Token;
39
+ } ? Token : never;
40
+ type Append<S, K, E extends readonly AnyMetadataEntry[], F = never> = unknown extends S ? [MetadataKeyOf<E[number]> | F] extends [never] ? unknown : UntrackedState | State<K, E, F> : Replace<S, K, {
41
+ readonly token: K;
42
+ readonly metadata: [Extract<S, {
43
+ readonly token: K;
44
+ }>] extends [never] ? MetadataKeyOf<E[number]> : Extract<Extract<S, {
45
+ readonly token: K;
46
+ }> extends {
47
+ readonly metadata: infer M;
48
+ } ? M : never, MetadataKeyOf<E[number]>>;
49
+ readonly callable: F;
50
+ }>;
51
+ type TrackedState<S> = S extends {
52
+ readonly metadata: infer M;
53
+ readonly callable: infer F;
54
+ } ? [M | F] extends [never] ? never : S : never;
55
+ type Merge<S, K, N> = unknown extends S ? [TrackedState<N>] extends [never] ? unknown : Replace<S, K, N> : Replace<S, K, N>;
56
+ type MissingMetadata<R, S> = unknown extends S ? R : S extends {
57
+ readonly metadata: infer M;
58
+ } ? Exclude<R, M> : never;
59
+ /**
60
+ * Create a new DI container that follows a shared policy.
61
+ *
62
+ * Containers created with the same policy object share its settings and the origins it observes.
63
+ * Every registration must carry the policy's `requiredMetadata`, and `use()` accepts only modules
64
+ * with the same policy or without one. Declare the policy with `satisfies ContainerPolicy` to keep
65
+ * its concrete types.
66
+ *
67
+ * @param options `policy`: the shared policy object
68
+ */
69
+ export declare function createContainer<const P extends ContainerPolicy>(options: {
70
+ readonly policy: P;
71
+ }): Container<Record<never, never>, never, never, Record<never, never>, never, never, RequiredMetadata<P>, never>;
72
+ /**
73
+ * Create a new DI container that follows a shared policy, with predeclared token type maps.
74
+ *
75
+ * @template T PropertyKey-based token type map (defined via interface, order-independent)
76
+ * @template ScopedT PropertyKey-based token type map for scoped registrations
77
+ * @param options `policy`: the shared policy object
78
+ */
79
+ export declare function createContainer<T, ScopedT, const P extends ContainerPolicy>(options: {
80
+ readonly policy: P;
81
+ }): Container<T, never, never, ScopedT, never, never, RequiredMetadata<P>, never>;
3
82
  /**
4
83
  * Create a new DI container.
5
84
  *
@@ -14,7 +93,9 @@ import type { AbstractConstructor, Resolver } from '../resolver/index.js';
14
93
  * .registerTransient(GenerateTextUseCase, r => new GenerateTextUseCase(r.resolve(TextGenerationService)));
15
94
  * ```
16
95
  */
17
- export declare function createContainer<T = Record<never, never>, ScopedT = Record<never, never>>(): Container<T, never, never, ScopedT>;
96
+ export declare function createContainer<T = Record<never, never>, ScopedT = Record<never, never>>(): Container<T, never, never, ScopedT, never, never, readonly [], [
97
+ keyof T | keyof ScopedT
98
+ ] extends [never] ? never : unknown>;
18
99
  /**
19
100
  * Lightweight DI container — registration only.
20
101
  *
@@ -29,33 +110,43 @@ export declare function createContainer<T = Record<never, never>, ScopedT = Reco
29
110
  * @template ScopedT PropertyKey-based token type map for scoped registrations
30
111
  * @template ScopedSync Union of scoped sync class constructors (accumulated via chaining, order-dependent)
31
112
  * @template ScopedAsync Union of scoped async class constructors (accumulated via chaining, order-dependent)
113
+ * @template Required Metadata keys the container's policy requires on every registration
114
+ * @template Registrations Registration state that tracks metadata and operations per token
32
115
  */
33
- export declare class Container<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> {
116
+ export declare class Container<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never, Required extends readonly AnyMetadataKey[] = readonly [], Registrations = unknown> {
34
117
  private readonly registrations;
35
118
  private readonly singletonCache;
119
+ private readonly requiredMetadata;
120
+ private readonly policy;
36
121
  private disposed;
37
- /**
38
- * Internal state accessor for extension modules (scope, disposable).
39
- *
40
- * @internal
41
- */
42
- readonly [INTERNALS]: ContainerInternals;
43
- constructor();
122
+ readonly [REGISTRATION_STATE]: Registrations;
123
+ readonly [INTERNALS]: ContainerInternals & {
124
+ readonly kind: 'container';
125
+ };
126
+ constructor(options?: {
127
+ readonly policy: ContainerPolicy<Required>;
128
+ });
44
129
  /**
45
130
  * Register a factory function as a singleton for the given token.
46
131
  *
47
132
  * Creates the instance on the first resolve and returns the cached value thereafter.
48
133
  * If the same token is registered multiple times, all factories are accumulated.
49
134
  * `resolve()` returns the last registered instance; `resolveAll()` returns all.
135
+ * Register request-dependent work as scoped instead.
50
136
  *
51
137
  * @param token Any value to use as a token
52
- * @param factory Factory function that receives a resolver and returns an instance
138
+ * @param factory Factory function that receives a resolver and returns an instance. Wrap it with
139
+ * `entrypoint()` to expose the function it returns as an operation.
140
+ * @param options `metadata`: entries created with metadata keys. Required when the policy declares
141
+ * `requiredMetadata`.
53
142
  * @returns The container for method chaining
54
143
  */
55
- registerSingleton<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => Promise<V>): Container<T, Sync, Async | AbstractConstructor<V>, ScopedT, ScopedSync, ScopedAsync>;
56
- registerSingleton<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync | AbstractConstructor<V>, Async, ScopedT, ScopedSync, ScopedAsync>;
57
- registerSingleton<K extends PropertyKey, V>(token: K, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<Record<K, V> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
58
- registerSingleton<V>(token: unknown, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
144
+ registerSingleton<K extends PropertyKey, F extends Callable, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: ((resolver: Resolver<T, Sync, Async>) => F) & EntrypointFactory<Resolver<T, Sync, Async>, F>, ...options: RegistrationArguments<Required, E>): Container<Record<K, F> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E, F>>;
145
+ registerSingleton<K extends PropertyKey, F extends Callable, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: EntrypointFactory<Resolver<T, Sync, Async>, F>, ...options: RegistrationArguments<Required, E>): Container<Record<K, Promise<F>> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E, F>>;
146
+ registerSingleton<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => Promise<V>, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async | AbstractConstructor<V>, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, AbstractConstructor<V>, E>>;
147
+ registerSingleton<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => V, ...options: RegistrationArguments<Required, E>): Container<T, Sync | AbstractConstructor<V>, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, AbstractConstructor<V>, E>>;
148
+ registerSingleton<K extends PropertyKey, V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: (resolver: Resolver<T, Sync, Async>) => V, ...options: RegistrationArguments<Required, E>): Container<Record<K, V> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E>>;
149
+ registerSingleton<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: unknown, factory: (resolver: Resolver<T, Sync, Async>) => V, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Registrations | State<unknown, E>>;
59
150
  /**
60
151
  * Register a factory function as transient for the given token.
61
152
  *
@@ -64,13 +155,18 @@ export declare class Container<T = Record<never, never>, Sync extends AbstractCo
64
155
  * `resolve()` returns the last registered instance; `resolveAll()` returns all.
65
156
  *
66
157
  * @param token Any value to use as a token
67
- * @param factory Factory function that receives a resolver and returns an instance
158
+ * @param factory Factory function that receives a resolver and returns an instance. Wrap it with
159
+ * `entrypoint()` to expose the function it returns as an operation.
160
+ * @param options `metadata`: entries created with metadata keys. Required when the policy declares
161
+ * `requiredMetadata`.
68
162
  * @returns The container for method chaining
69
163
  */
70
- registerTransient<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => Promise<V>): Container<T, Sync, Async | AbstractConstructor<V>, ScopedT, ScopedSync, ScopedAsync>;
71
- registerTransient<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync | AbstractConstructor<V>, Async, ScopedT, ScopedSync, ScopedAsync>;
72
- registerTransient<K extends PropertyKey, V>(token: K, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<Record<K, V> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
73
- registerTransient<V>(token: unknown, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
164
+ registerTransient<K extends PropertyKey, F extends Callable, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: ((resolver: Resolver<T, Sync, Async>) => F) & EntrypointFactory<Resolver<T, Sync, Async>, F>, ...options: RegistrationArguments<Required, E>): Container<Record<K, F> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E, F>>;
165
+ registerTransient<K extends PropertyKey, F extends Callable, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: EntrypointFactory<Resolver<T, Sync, Async>, F>, ...options: RegistrationArguments<Required, E>): Container<Record<K, Promise<F>> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E, F>>;
166
+ registerTransient<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => Promise<V>, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async | AbstractConstructor<V>, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, AbstractConstructor<V>, E>>;
167
+ registerTransient<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => V, ...options: RegistrationArguments<Required, E>): Container<T, Sync | AbstractConstructor<V>, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, AbstractConstructor<V>, E>>;
168
+ registerTransient<K extends PropertyKey, V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: (resolver: Resolver<T, Sync, Async>) => V, ...options: RegistrationArguments<Required, E>): Container<Record<K, V> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E>>;
169
+ registerTransient<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: unknown, factory: (resolver: Resolver<T, Sync, Async>) => V, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Registrations | State<unknown, E>>;
74
170
  /**
75
171
  * Register a factory function as scoped for the given token.
76
172
  *
@@ -79,30 +175,41 @@ export declare class Container<T = Record<never, never>, Sync extends AbstractCo
79
175
  * Scoped tokens cannot be resolved from the root container — use createScope() first.
80
176
  *
81
177
  * @param token Any value to use as a token
82
- * @param factory Factory function that receives a resolver and returns an instance
178
+ * @param factory Factory function that receives a resolver and returns an instance. Wrap it with
179
+ * `entrypoint()` to expose the function it returns as an operation.
180
+ * @param options `metadata`: entries created with metadata keys. Required when the policy declares
181
+ * `requiredMetadata`.
83
182
  * @returns The container for method chaining
84
183
  */
85
- registerScoped<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => Promise<V>): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync | AbstractConstructor<V>>;
86
- registerScoped<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V): Container<T, Sync, Async, ScopedT, ScopedSync | AbstractConstructor<V>, ScopedAsync>;
87
- registerScoped<K extends PropertyKey, V>(token: K, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V): Container<T, Sync, Async, Record<K, V> & ScopedT, ScopedSync, ScopedAsync>;
88
- registerScoped<V>(token: unknown, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
184
+ registerScoped<K extends PropertyKey, F extends Callable, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: ((resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => F) & EntrypointFactory<Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, F>, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, Record<K, F> & ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E, F>>;
185
+ registerScoped<K extends PropertyKey, F extends Callable, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: EntrypointFactory<Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, F>, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, Record<K, Promise<F>> & ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E, F>>;
186
+ registerScoped<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => Promise<V>, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync | AbstractConstructor<V>, Required, Append<Registrations, AbstractConstructor<V>, E>>;
187
+ registerScoped<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, ScopedT, ScopedSync | AbstractConstructor<V>, ScopedAsync, Required, Append<Registrations, AbstractConstructor<V>, E>>;
188
+ registerScoped<K extends PropertyKey, V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: K, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, Record<K, V> & ScopedT, ScopedSync, ScopedAsync, Required, Append<Registrations, K, E>>;
189
+ registerScoped<V, const E extends readonly AnyMetadataEntry[] = readonly []>(token: unknown, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V, ...options: RegistrationArguments<Required, E>): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Registrations | State<unknown, E>>;
190
+ /**
191
+ * Read the metadata of the last registration for a token without creating the instance.
192
+ *
193
+ * @param token A registered token
194
+ * @returns A read-only metadata reader
195
+ * @throws ContainerError if the token is not registered
196
+ */
197
+ getMetadata<V>(token: AbstractConstructor<V> & (Sync | Async | ScopedSync | ScopedAsync)): MetadataReader;
198
+ getMetadata<K extends keyof (T & ScopedT)>(token: K): MetadataReader;
89
199
  /**
90
200
  * Apply all registrations from another container (module) to this container.
91
201
  *
92
- * Copies registration entries (factory + lifetime) by replacing existing entries for each token.
202
+ * Copies registration entries by replacing existing entries for each token.
93
203
  * Singleton instance caches are not shared — each container manages its own.
204
+ * A module with a policy must use this container's policy, and every copied registration must carry
205
+ * the metadata this container's policy requires; otherwise nothing is copied.
94
206
  *
95
207
  * @param source A container whose registrations will be copied into this container
96
208
  * @returns The container for method chaining
97
209
  */
98
- use<MT, MSync extends AbstractConstructor, MAsync extends AbstractConstructor, MScopedT, MScopedSync extends AbstractConstructor, MScopedAsync extends AbstractConstructor>(source: Container<MT, MSync, MAsync, MScopedT, MScopedSync, MScopedAsync>): Container<T & MT, Sync | MSync, Async | MAsync, ScopedT & MScopedT, ScopedSync | MScopedSync, ScopedAsync | MScopedAsync>;
99
- /**
100
- * Add a registration entry. Accumulates registrations for the same token.
101
- *
102
- * @param token Token
103
- * @param factory Factory function
104
- * @param lifetime Lifetime of the registration
105
- * @returns The container for method chaining
106
- */
210
+ use<MT, MS extends AbstractConstructor, MA extends AbstractConstructor, MST, MSS extends AbstractConstructor, MSA extends AbstractConstructor, MR extends readonly AnyMetadataKey[], State>(source: Container<MT, MS, MA, MST, MSS, MSA, MR, State> & ([MissingMetadata<Required[number], State>] extends [never] ? unknown : {
211
+ readonly missingRequiredMetadata: never;
212
+ })): Container<T & MT, Sync | MS, Async | MA, ScopedT & MST, ScopedSync | MSS, ScopedAsync | MSA, Required, Merge<Registrations, SourceTokens<State, keyof MT | keyof MST | MS | MA | MSS | MSA>, SourceState<State, keyof MT | keyof MST | MS | MA | MSS | MSA>>>;
107
213
  private addRegistration;
108
214
  }
215
+ export {};
@@ -0,0 +1,36 @@
1
+ import { type AnyMetadataKey, type MetadataPair } from '../metadata/index.cjs';
2
+ import type { Registration } from '../resolver/index.cjs';
3
+ import type { RegistrationDescription } from '../scope/index.cjs';
4
+ export interface ReturnEvent {
5
+ readonly registrations: readonly RegistrationDescription[];
6
+ }
7
+ export type BeforeReturn = (event: ReturnEvent) => void;
8
+ export interface ContainerPolicy<Required extends readonly AnyMetadataKey[] = readonly AnyMetadataKey[]> {
9
+ readonly name?: string;
10
+ readonly requiredMetadata?: Required;
11
+ readonly beforeReturn?: BeforeReturn;
12
+ }
13
+ export type RequiredMetadata<P> = P extends {
14
+ readonly requiredMetadata: infer R extends readonly AnyMetadataKey[];
15
+ } ? R : readonly [];
16
+ /**
17
+ * An origin recorded for an instance.
18
+ *
19
+ * It holds neither the registration nor its factory. A policy outlives its containers, so holding a
20
+ * registration would keep every per-request container and its factory closures reachable from the record.
21
+ */
22
+ interface Origin {
23
+ readonly description: RegistrationDescription;
24
+ readonly pairs: readonly MetadataPair[] | undefined;
25
+ }
26
+ export interface PolicyState {
27
+ readonly definition: ContainerPolicy;
28
+ readonly name: string | undefined;
29
+ readonly requiredMetadata: readonly AnyMetadataKey[];
30
+ readonly beforeReturn: BeforeReturn | undefined;
31
+ readonly origins: WeakMap<object, Origin[]>;
32
+ }
33
+ export declare function bindPolicy(definition: ContainerPolicy | undefined): PolicyState | undefined;
34
+ export declare function observe(policy: PolicyState | undefined, registration: Registration, token: unknown, value: unknown): void;
35
+ export declare function checkReturn(policy: PolicyState | undefined, value: unknown): void;
36
+ export {};
@@ -0,0 +1,36 @@
1
+ import { type AnyMetadataKey, type MetadataPair } from '../metadata/index.js';
2
+ import type { Registration } from '../resolver/index.js';
3
+ import type { RegistrationDescription } from '../scope/index.js';
4
+ export interface ReturnEvent {
5
+ readonly registrations: readonly RegistrationDescription[];
6
+ }
7
+ export type BeforeReturn = (event: ReturnEvent) => void;
8
+ export interface ContainerPolicy<Required extends readonly AnyMetadataKey[] = readonly AnyMetadataKey[]> {
9
+ readonly name?: string;
10
+ readonly requiredMetadata?: Required;
11
+ readonly beforeReturn?: BeforeReturn;
12
+ }
13
+ export type RequiredMetadata<P> = P extends {
14
+ readonly requiredMetadata: infer R extends readonly AnyMetadataKey[];
15
+ } ? R : readonly [];
16
+ /**
17
+ * An origin recorded for an instance.
18
+ *
19
+ * It holds neither the registration nor its factory. A policy outlives its containers, so holding a
20
+ * registration would keep every per-request container and its factory closures reachable from the record.
21
+ */
22
+ interface Origin {
23
+ readonly description: RegistrationDescription;
24
+ readonly pairs: readonly MetadataPair[] | undefined;
25
+ }
26
+ export interface PolicyState {
27
+ readonly definition: ContainerPolicy;
28
+ readonly name: string | undefined;
29
+ readonly requiredMetadata: readonly AnyMetadataKey[];
30
+ readonly beforeReturn: BeforeReturn | undefined;
31
+ readonly origins: WeakMap<object, Origin[]>;
32
+ }
33
+ export declare function bindPolicy(definition: ContainerPolicy | undefined): PolicyState | undefined;
34
+ export declare function observe(policy: PolicyState | undefined, registration: Registration, token: unknown, value: unknown): void;
35
+ export declare function checkReturn(policy: PolicyState | undefined, value: unknown): void;
36
+ export {};
@@ -25,7 +25,25 @@ __export(disposable_exports, {
25
25
  module.exports = __toCommonJS(disposable_exports);
26
26
 
27
27
  // src/internal.ts
28
- var INTERNALS = /* @__PURE__ */ Symbol.for("katagami.internals.v3");
28
+ var INTERNALS = /* @__PURE__ */ Symbol.for("katagami.internals.v4");
29
+ var CREATIONS = /* @__PURE__ */ Symbol.for("katagami.creations.v4");
30
+ var shared = globalThis;
31
+ if (!shared[CREATIONS]) {
32
+ Object.defineProperty(shared, CREATIONS, { value: /* @__PURE__ */ new WeakMap() });
33
+ }
34
+ var creations = shared[CREATIONS];
35
+ function settle(creation) {
36
+ return creation.then(
37
+ (value) => ({ failed: false, value }),
38
+ (error) => ({ error, failed: true })
39
+ );
40
+ }
41
+ var CONSTRUCTING = /* @__PURE__ */ Symbol.for("katagami.constructing.v4");
42
+ var running = globalThis;
43
+ if (!running[CONSTRUCTING]) {
44
+ Object.defineProperty(running, CONSTRUCTING, { value: [] });
45
+ }
46
+ var constructing = running[CONSTRUCTING];
29
47
 
30
48
  // src/disposable/index.ts
31
49
  function disposable(container) {
@@ -38,11 +56,15 @@ function disposable(container) {
38
56
  const instances = [...internals.ownCache.values()].reverse();
39
57
  const errors = [];
40
58
  for (const instance of instances) {
41
- try {
42
- let resolved = instance;
43
- if (instance instanceof Promise) {
44
- resolved = await instance;
59
+ let resolved = instance;
60
+ if (instance instanceof Promise) {
61
+ const creation = await (creations.get(instance) ?? settle(instance));
62
+ if (creation.failed) {
63
+ continue;
45
64
  }
65
+ resolved = creation.value;
66
+ }
67
+ try {
46
68
  if (resolved != null && typeof resolved === "object") {
47
69
  if (Symbol.asyncDispose in resolved) {
48
70
  await resolved[Symbol.asyncDispose]();
@@ -1,5 +1,6 @@
1
1
  import type { Container } from '../container/index.cjs';
2
- import { type ContainerInternals, INTERNALS, type TYPE_STATE } from '../internal.cjs';
2
+ import { type ContainerInternals, INTERNALS, type REGISTRATION_STATE, type TYPE_STATE } from '../internal.cjs';
3
+ import type { AnyMetadataKey } from '../metadata/index.cjs';
3
4
  import type { AbstractConstructor, Resolver } from '../resolver/index.cjs';
4
5
  import type { Scope } from '../scope/index.cjs';
5
6
  /**
@@ -16,8 +17,12 @@ import type { Scope } from '../scope/index.cjs';
16
17
  * @template ScopedSync Union of scoped sync class constructors
17
18
  * @template ScopedAsync Union of scoped async class constructors
18
19
  */
19
- export interface DisposableContainer<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends AsyncDisposable {
20
- readonly [INTERNALS]: ContainerInternals;
20
+ export interface DisposableContainer<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never, Required extends readonly AnyMetadataKey[] = readonly [], Registrations = unknown> extends AsyncDisposable {
21
+ readonly [INTERNALS]: ContainerInternals & {
22
+ readonly kind: 'container';
23
+ };
24
+ readonly [REGISTRATION_STATE]: Registrations;
25
+ readonly requiredMetadataType?: Required;
21
26
  readonly [TYPE_STATE]?: {
22
27
  readonly kind: 'container';
23
28
  readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
@@ -35,8 +40,11 @@ export interface DisposableContainer<T = Record<never, never>, Sync extends Abst
35
40
  * @template ScopedSync Union of scoped sync class constructors
36
41
  * @template ScopedAsync Union of scoped async class constructors
37
42
  */
38
- export interface DisposableScope<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, AsyncDisposable {
39
- readonly [INTERNALS]: ContainerInternals;
43
+ export interface DisposableScope<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never, Registrations = unknown> extends Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, AsyncDisposable {
44
+ readonly [INTERNALS]: ContainerInternals & {
45
+ readonly kind: 'scope';
46
+ };
47
+ readonly [REGISTRATION_STATE]: Registrations;
40
48
  readonly [TYPE_STATE]?: {
41
49
  readonly kind: 'scope';
42
50
  readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
@@ -48,6 +56,8 @@ export interface DisposableScope<T = Record<never, never>, Sync extends Abstract
48
56
  * Enables `await using` syntax by attaching `[Symbol.asyncDispose]` to the target.
49
57
  * Disposes owned instances in reverse creation order (LIFO), calling
50
58
  * `[Symbol.asyncDispose]()` or `[Symbol.dispose]()` on each instance that implements them.
59
+ * A cached asynchronous creation that rejected yields no resolved value to close: it is awaited,
60
+ * skipped, and its failure is left to the caller that resolved the token.
51
61
  *
52
62
  * The returned type prevents registration methods from being called on a potentially-disposed container.
53
63
  * For scopes, `resolve`, `tryResolve`, `resolveAll`, and `tryResolveAll` remain available.
@@ -65,5 +75,5 @@ export interface DisposableScope<T = Record<never, never>, Sync extends Abstract
65
75
  * );
66
76
  * ```
67
77
  */
68
- export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor>(container: Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>): DisposableContainer<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
69
- export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor>(scope: Scope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>): DisposableScope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
78
+ export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor, Required extends readonly AnyMetadataKey[], Registrations>(container: Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Registrations>): DisposableContainer<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Registrations>;
79
+ export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor, Registrations>(scope: Scope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Registrations>): DisposableScope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Registrations>;
@@ -1,5 +1,6 @@
1
1
  import type { Container } from '../container/index.js';
2
- import { type ContainerInternals, INTERNALS, type TYPE_STATE } from '../internal.js';
2
+ import { type ContainerInternals, INTERNALS, type REGISTRATION_STATE, type TYPE_STATE } from '../internal.js';
3
+ import type { AnyMetadataKey } from '../metadata/index.js';
3
4
  import type { AbstractConstructor, Resolver } from '../resolver/index.js';
4
5
  import type { Scope } from '../scope/index.js';
5
6
  /**
@@ -16,8 +17,12 @@ import type { Scope } from '../scope/index.js';
16
17
  * @template ScopedSync Union of scoped sync class constructors
17
18
  * @template ScopedAsync Union of scoped async class constructors
18
19
  */
19
- export interface DisposableContainer<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends AsyncDisposable {
20
- readonly [INTERNALS]: ContainerInternals;
20
+ export interface DisposableContainer<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never, Required extends readonly AnyMetadataKey[] = readonly [], Registrations = unknown> extends AsyncDisposable {
21
+ readonly [INTERNALS]: ContainerInternals & {
22
+ readonly kind: 'container';
23
+ };
24
+ readonly [REGISTRATION_STATE]: Registrations;
25
+ readonly requiredMetadataType?: Required;
21
26
  readonly [TYPE_STATE]?: {
22
27
  readonly kind: 'container';
23
28
  readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
@@ -35,8 +40,11 @@ export interface DisposableContainer<T = Record<never, never>, Sync extends Abst
35
40
  * @template ScopedSync Union of scoped sync class constructors
36
41
  * @template ScopedAsync Union of scoped async class constructors
37
42
  */
38
- export interface DisposableScope<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, AsyncDisposable {
39
- readonly [INTERNALS]: ContainerInternals;
43
+ export interface DisposableScope<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never, Registrations = unknown> extends Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, AsyncDisposable {
44
+ readonly [INTERNALS]: ContainerInternals & {
45
+ readonly kind: 'scope';
46
+ };
47
+ readonly [REGISTRATION_STATE]: Registrations;
40
48
  readonly [TYPE_STATE]?: {
41
49
  readonly kind: 'scope';
42
50
  readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
@@ -48,6 +56,8 @@ export interface DisposableScope<T = Record<never, never>, Sync extends Abstract
48
56
  * Enables `await using` syntax by attaching `[Symbol.asyncDispose]` to the target.
49
57
  * Disposes owned instances in reverse creation order (LIFO), calling
50
58
  * `[Symbol.asyncDispose]()` or `[Symbol.dispose]()` on each instance that implements them.
59
+ * A cached asynchronous creation that rejected yields no resolved value to close: it is awaited,
60
+ * skipped, and its failure is left to the caller that resolved the token.
51
61
  *
52
62
  * The returned type prevents registration methods from being called on a potentially-disposed container.
53
63
  * For scopes, `resolve`, `tryResolve`, `resolveAll`, and `tryResolveAll` remain available.
@@ -65,5 +75,5 @@ export interface DisposableScope<T = Record<never, never>, Sync extends Abstract
65
75
  * );
66
76
  * ```
67
77
  */
68
- export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor>(container: Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>): DisposableContainer<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
69
- export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor>(scope: Scope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>): DisposableScope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
78
+ export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor, Required extends readonly AnyMetadataKey[], Registrations>(container: Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Registrations>): DisposableContainer<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Required, Registrations>;
79
+ export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor, Registrations>(scope: Scope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Registrations>): DisposableScope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync, Registrations>;
@@ -1,45 +1,6 @@
1
1
  import {
2
- INTERNALS
3
- } from "../chunk-J2NYR3SH.js";
4
-
5
- // src/disposable/index.ts
6
- function disposable(container) {
7
- const asyncDispose = async () => {
8
- const internals = container[INTERNALS];
9
- if (internals.isDisposed()) {
10
- return;
11
- }
12
- internals.markDisposed();
13
- const instances = [...internals.ownCache.values()].reverse();
14
- const errors = [];
15
- for (const instance of instances) {
16
- try {
17
- let resolved = instance;
18
- if (instance instanceof Promise) {
19
- resolved = await instance;
20
- }
21
- if (resolved != null && typeof resolved === "object") {
22
- if (Symbol.asyncDispose in resolved) {
23
- await resolved[Symbol.asyncDispose]();
24
- } else if (Symbol.dispose in resolved) {
25
- resolved[Symbol.dispose]();
26
- }
27
- }
28
- } catch (error) {
29
- errors.push(error);
30
- }
31
- }
32
- internals.ownCache.clear();
33
- if (errors.length > 0) {
34
- throw new AggregateError(errors, "One or more errors occurred during disposal.");
35
- }
36
- };
37
- Object.defineProperty(container, Symbol.asyncDispose, {
38
- configurable: true,
39
- value: asyncDispose
40
- });
41
- return container;
42
- }
2
+ disposable
3
+ } from "../chunk-R66WOZUM.js";
43
4
  export {
44
5
  disposable
45
6
  };
@@ -0,0 +1,20 @@
1
+ declare const ENTRYPOINT: unique symbol;
2
+ export type Callable = (...args: never[]) => unknown;
3
+ export type EntrypointFactory<R, F extends Callable> = ((resolver: R) => F | Promise<F>) & {
4
+ readonly [ENTRYPOINT]: true;
5
+ };
6
+ /**
7
+ * Mark a factory that returns a function as a public operation.
8
+ *
9
+ * An operations scope (`createScope(container, { access: 'operations' })`) exposes only entry points.
10
+ * Calling the operation it returns resolves the registration and runs the function on each call.
11
+ *
12
+ * @param factory Factory function that receives a resolver and returns the operation's function
13
+ * @returns The same factory, marked as an entry point
14
+ */
15
+ export declare function entrypoint<R, F extends Callable>(factory: (resolver: R) => F): ((resolver: R) => F) & {
16
+ readonly [ENTRYPOINT]: true;
17
+ };
18
+ export declare function entrypoint<R, F extends Callable>(factory: (resolver: R) => Promise<F>): EntrypointFactory<R, F>;
19
+ export declare function isEntrypoint(factory: unknown): boolean;
20
+ export {};
@@ -0,0 +1,20 @@
1
+ declare const ENTRYPOINT: unique symbol;
2
+ export type Callable = (...args: never[]) => unknown;
3
+ export type EntrypointFactory<R, F extends Callable> = ((resolver: R) => F | Promise<F>) & {
4
+ readonly [ENTRYPOINT]: true;
5
+ };
6
+ /**
7
+ * Mark a factory that returns a function as a public operation.
8
+ *
9
+ * An operations scope (`createScope(container, { access: 'operations' })`) exposes only entry points.
10
+ * Calling the operation it returns resolves the registration and runs the function on each call.
11
+ *
12
+ * @param factory Factory function that receives a resolver and returns the operation's function
13
+ * @returns The same factory, marked as an entry point
14
+ */
15
+ export declare function entrypoint<R, F extends Callable>(factory: (resolver: R) => F): ((resolver: R) => F) & {
16
+ readonly [ENTRYPOINT]: true;
17
+ };
18
+ export declare function entrypoint<R, F extends Callable>(factory: (resolver: R) => Promise<F>): EntrypointFactory<R, F>;
19
+ export declare function isEntrypoint(factory: unknown): boolean;
20
+ export {};