Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | 3x 209x 209x 209x 209x 209x 209x 209x 209x 23x 23x 19x 189x 18x 18x 5x 3x 3x 2x 16x 16x 16x 16x 16x 15x 6x 15x 18x | import type { z, ZodType } from 'zod/v4'
import type { IContainer } from '../interfaces/container.interface.mjs'
import type { Factorable } from '../interfaces/factory.interface.mjs'
import type { NameResolver } from '../internal/core/name-resolver.mjs'
import type { ServiceInvalidator } from '../internal/core/service-invalidator.mjs'
import type { TokenResolver } from '../internal/core/token-resolver.mjs'
import type {
ClassType,
ClassTypeWithArgument,
InjectionTokenSchemaType,
} from '../token/injection-token.mjs'
import type { Registry } from '../token/registry.mjs'
import type { Join, UnionToArray } from '../utils/types.mjs'
import { InjectableScope, InjectableType } from '../enums/index.mjs'
import { DIError, DIErrorCode } from '../errors/index.mjs'
import { InstanceStatus } from '../internal/holder/instance-holder.mjs'
import { UnifiedStorage } from '../internal/holder/unified-storage.mjs'
import { StubFactoryClass } from '../internal/index.mjs'
import {
BoundInjectionToken,
FactoryInjectionToken,
InjectionToken,
} from '../token/injection-token.mjs'
/**
* Abstract base class for dependency injection containers.
*
* Provides shared implementation for common container operations.
* Both Container and ScopedContainer extend this class.
*/
export abstract class AbstractContainer implements IContainer {
/**
* The default scope used when adding instances without explicit registration.
*/
protected abstract readonly defaultScope: InjectableScope
/**
* The request ID for scoped containers, undefined for root container.
*/
protected abstract readonly requestId: string | undefined
// ============================================================================
// ABSTRACT METHODS - Must be implemented by subclasses
// ============================================================================
/**
* Gets the storage for this container.
*/
abstract getStorage(): UnifiedStorage
/**
* Gets the registry for this container.
*/
protected abstract getRegistry(): Registry
/**
* Gets the token resolver.
*/
protected abstract getTokenResolver(): TokenResolver
/**
* Gets the name resolver.
*/
protected abstract getNameResolver(): NameResolver
/**
* Gets the service invalidator.
*/
protected abstract getServiceInvalidator(): ServiceInvalidator
/**
* Gets an instance from the container.
*/
// #1 Simple class
abstract get<T extends ClassType>(
token: T,
): InstanceType<T> extends Factorable<infer R>
? Promise<R>
: Promise<InstanceType<T>>
// #1.1 Simple class with args
abstract get<T extends ClassTypeWithArgument<R>, R>(
token: T,
args: R,
): Promise<InstanceType<T>>
// #2 Token with required Schema
abstract get<T, S extends InjectionTokenSchemaType>(
token: InjectionToken<T, S>,
args: z.input<S>,
): Promise<T>
// #3 Token with optional Schema
abstract get<T, S extends InjectionTokenSchemaType, R extends boolean>(
token: InjectionToken<T, S, R>,
): R extends false
? Promise<T>
: S extends ZodType<infer Type>
? `Error: Your token requires args: ${Join<
UnionToArray<keyof Type>,
', '
>}`
: 'Error: Your token requires args'
// #4 Token with no Schema
abstract get<T>(token: InjectionToken<T, undefined>): Promise<T>
abstract get<T>(token: BoundInjectionToken<T, any>): Promise<T>
abstract get<T>(token: FactoryInjectionToken<T, any>): Promise<T>
/**
* Invalidates a service and its dependencies.
*/
abstract invalidate(service: unknown): Promise<void>
/**
* Disposes the container and cleans up all resources.
*/
abstract dispose(): Promise<void>
// ============================================================================
// SHARED IMPLEMENTATIONS
// ============================================================================
/**
* Calculates the instance name for a given token and optional arguments.
*
* @internal
* @param token The class type, InjectionToken, BoundInjectionToken, or FactoryInjectionToken
* @param args Optional arguments (ignored for BoundInjectionToken which uses its bound value)
* @returns The calculated instance name string, or null if the token is a FactoryInjectionToken that is not yet resolved
*/
calculateInstanceName(
token:
| ClassType
| InjectionToken<any, any>
| BoundInjectionToken<any, any>
| FactoryInjectionToken<any, any>,
args?: unknown,
): string | null {
const tokenResolver = this.getTokenResolver()
// Use validateAndResolveTokenArgs to handle token normalization and arg resolution
const [err, { actualToken, validatedArgs }] =
tokenResolver.validateAndResolveTokenArgs(token, args)
if (err) {
// Return null if factory token is not resolved
if (
err instanceof DIError &&
err.code === DIErrorCode.FactoryTokenNotResolved
) {
return null
}
// Return null if validation fails (can't calculate name with invalid args)
if (
err instanceof DIError &&
err.code === DIErrorCode.TokenValidationError
) {
return null
}
}
// Get the real token for registry lookup to determine scope
const realToken = this.getTokenResolver().getRealToken(actualToken)
const registry = this.getRegistry()
// Get scope from registry, or use default scope if not registered
const scope = registry.has(realToken)
? registry.get(realToken).scope
: this.defaultScope
// Generate instance name using the name resolver with actual token and validated args
return this.getNameResolver().generateInstanceName(
actualToken,
validatedArgs,
scope === InjectableScope.Request ? this.requestId : undefined,
scope,
)
}
/**
* Checks if a service is registered in the container.
*/
isRegistered(token: any): boolean {
const realToken = this.getTokenResolver().getRegistryToken(token)
return this.getRegistry().has(realToken)
}
/**
* Waits for all pending operations to complete.
*/
async ready(): Promise<void> {
await this.getServiceInvalidator().readyWithStorage(this.getStorage())
}
/**
* @internal
* Attempts to get an instance synchronously if it already exists.
*/
tryGetSync<T>(token: any, args?: any): T | null {
return this.tryGetSyncFromStorage(
token,
args,
this.getStorage(),
this.requestId,
)
}
/**
* @internal
* Internal method for getting instances synchronously with configurable storage.
*/
protected tryGetSyncFromStorage<T>(
token: any,
args: any,
storage: UnifiedStorage,
requestId?: string,
): T | null {
const tokenResolver = this.getTokenResolver()
const realToken = tokenResolver.getRegistryToken(token)
const registry = this.getRegistry()
const scope = registry.has(realToken)
? registry.get(realToken).scope
: InjectableScope.Singleton
try {
const instanceName = this.getNameResolver().generateInstanceName(
tokenResolver.normalizeToken(token),
args,
requestId,
scope,
)
const result = storage.get(instanceName)
if (result && result[0] === undefined && result[1]) {
const holder = result[1]
if (holder.status === InstanceStatus.Created) {
return holder.instance as T
}
}
} catch {
// Ignore error
}
return null
}
/**
* Adds an instance to the container.
* Accepts class types, InjectionTokens, and BoundInjectionTokens.
* Rejects InjectionTokens with required schemas (use BoundInjectionToken instead).
*
* @param token The class type, InjectionToken, or BoundInjectionToken to register the instance for
* @param instance The instance to store
*/
addInstance<T>(
token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>,
instance: T,
): void {
this.addInstanceToStorage(
token,
instance,
this.getStorage(),
this.defaultScope,
this.requestId,
)
}
/**
* @internal
* Internal method for adding instances with configurable scope and storage.
*/
protected addInstanceToStorage<T>(
token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>,
instance: T,
storage: UnifiedStorage,
scope: InjectableScope,
requestId?: string,
): void {
// Check if token is an InjectionToken with required schema
// BoundInjectionToken is allowed (it already has a value bound)
if (token instanceof InjectionToken) {
// Check if schema exists and is required (not optional)
if (token.schema) {
const schemaType = (token.schema as ZodType)?.def?.type
if (schemaType !== 'optional') {
throw DIError.tokenSchemaRequiredError(token.name)
}
}
}
const tokenResolver = this.getTokenResolver()
const registry = this.getRegistry()
// Normalize the token
const normalizedToken = tokenResolver.normalizeToken(token)
const realToken = tokenResolver.getRegistryToken(token)
// If it's a class type and not registered, register it with the given scope
Iif (typeof token === 'function' && !registry.has(realToken)) {
registry.set(realToken, scope, token, InjectableType.Class)
} else if (!registry.has(realToken)) {
// Set a stub factory class to avoid errors when getting instances of unregistered factory tokens
registry.set(
realToken,
scope,
StubFactoryClass,
InjectableType.Class,
// Lowest priority to avoid conflicts with other registered tokens
-1,
)
}
// Generate instance name with the given scope
const instanceName = this.getNameResolver().generateInstanceName(
normalizedToken,
normalizedToken instanceof BoundInjectionToken
? normalizedToken.value
: undefined,
requestId,
scope,
)
// Store the instance
storage.storeInstance(instanceName, instance)
}
}
|