All files / src/container container.mts

94.73% Statements 54/57
100% Branches 15/15
84.21% Functions 16/19
94.44% Lines 51/54

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                                                                                  16x 16x 268x 268x                   268x     268x 268x 268x   268x   268x 268x 268x 268x 268x 268x 268x 268x                     268x       268x 268x           268x           268x                                                                                                 770x   770x 769x 769x 3x               766x           747x 6x     741x               17x 17x 1x     1x     16x                   215x                 157x                             131x 3x     128x   128x             2x             4x               125x               177x               132x       532x       235x                       159x       221x      
import type { z, ZodType } from 'zod/v4'
 
import type { Factorable } from '../interfaces/factory.interface.mjs'
import type {
  ClassType,
  ClassTypeWithArgument,
  InjectionTokenSchemaType,
} from '../token/injection-token.mjs'
import type { Registry } from '../token/registry.mjs'
import type { Injectors } from '../utils/get-injectors.mjs'
import type { Join, UnionToArray } from '../utils/types.mjs'
 
import { Injectable } from '../decorators/injectable.decorator.mjs'
import { InjectableScope, InjectableType } from '../enums/index.mjs'
import { DIError } from '../errors/index.mjs'
import { InstanceResolver } from '../internal/core/instance-resolver.mjs'
import { NameResolver } from '../internal/core/name-resolver.mjs'
import { ScopeTracker } from '../internal/core/scope-tracker.mjs'
import { ServiceInitializer } from '../internal/core/service-initializer.mjs'
import { ServiceInvalidator } from '../internal/core/service-invalidator.mjs'
import { TokenResolver } from '../internal/core/token-resolver.mjs'
import { UnifiedStorage } from '../internal/holder/unified-storage.mjs'
import { LifecycleEventBus } from '../internal/lifecycle/lifecycle-event-bus.mjs'
import {
  BoundInjectionToken,
  FactoryInjectionToken,
  InjectionToken,
} from '../token/injection-token.mjs'
import { globalRegistry } from '../token/registry.mjs'
import { defaultInjectors } from '../utils/default-injectors.mjs'
import { getInjectableToken } from '../utils/index.mjs'
import { AbstractContainer } from './abstract-container.mjs'
import { ScopedContainer } from './scoped-container.mjs'
 
/**
 * Main dependency injection container.
 *
 * Provides a simplified public API for dependency injection.
 * Handles singleton and transient services directly,
 * while request-scoped services require using beginRequest() to create a ScopedContainer.
 */
@Injectable()
export class Container extends AbstractContainer {
  protected readonly defaultScope = InjectableScope.Singleton
  protected readonly requestId = undefined
 
  private readonly storage: UnifiedStorage
  private readonly serviceInitializer: ServiceInitializer
  private readonly serviceInvalidator: ServiceInvalidator
  private readonly tokenResolver: TokenResolver
  private readonly nameResolver: NameResolver
  private readonly scopeTracker: ScopeTracker
  private readonly eventBus: LifecycleEventBus
  private readonly instanceResolver: InstanceResolver
  private readonly activeRequestIds = new Set<string>()
 
  constructor(
    protected readonly registry: Registry = globalRegistry,
    protected readonly logger: Console | null = null,
    protected readonly injectors: Injectors = defaultInjectors,
  ) {
    super()
    // Initialize components
    this.storage = new UnifiedStorage(InjectableScope.Singleton)
    this.eventBus = new LifecycleEventBus(logger)
    this.nameResolver = new NameResolver(logger)
    this.tokenResolver = new TokenResolver(logger)
    this.scopeTracker = new ScopeTracker(registry, this.nameResolver, logger)
    this.serviceInitializer = new ServiceInitializer(injectors)
    this.serviceInvalidator = new ServiceInvalidator(this.eventBus, logger)
    this.instanceResolver = new InstanceResolver(
      registry,
      this.storage,
      this.serviceInitializer,
      this.tokenResolver,
      this.nameResolver,
      this.scopeTracker,
      this.serviceInvalidator,
      this.eventBus,
      logger,
    )
    this.registerSelf()
  }
 
  private registerSelf() {
    const token = getInjectableToken(Container)
    this.registry.set(
      token,
      InjectableScope.Singleton,
      Container,
      InjectableType.Class,
    )
    const instanceName = this.nameResolver.generateInstanceName(
      token,
      undefined,
      undefined,
      InjectableScope.Singleton,
    )
    this.storage.storeInstance(instanceName, this)
  }
 
  /**
   * Gets an instance from the container.
   * NOTE: Request-scoped services cannot be resolved directly from Container.
   * Use beginRequest() to create a ScopedContainer for request-scoped services.
   */
  // #1 Simple class
  get<T extends ClassType>(
    token: T,
  ): InstanceType<T> extends Factorable<infer R>
    ? Promise<R>
    : Promise<InstanceType<T>>
  // #1.1 Simple class with args
  get<T extends ClassTypeWithArgument<R>, R>(
    token: T,
    args: R,
  ): Promise<InstanceType<T>>
  // #2 Token with required Schema
  get<T, S extends InjectionTokenSchemaType>(
    token: InjectionToken<T, S>,
    args: z.input<S>,
  ): Promise<T>
  // #3 Token with optional Schema
  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
  get<T>(token: InjectionToken<T, undefined>): Promise<T>
  get<T>(token: BoundInjectionToken<T, any>): Promise<T>
  get<T>(token: FactoryInjectionToken<T, any>): Promise<T>
 
  async get(
    token:
      | ClassType
      | InjectionToken<any>
      | BoundInjectionToken<any, any>
      | FactoryInjectionToken<any, any>,
    args?: unknown,
  ) {
    // Check if this is a request-scoped service
    const realToken = this.tokenResolver.getRegistryToken(token)
 
    if (this.registry.has(realToken)) {
      const record = this.registry.get(realToken)
      if (record.scope === InjectableScope.Request) {
        throw DIError.scopeMismatchError(
          realToken.name,
          'ScopedContainer',
          'Container',
        )
      }
    }
 
    const [error, instance] = await this.instanceResolver.resolveInstance(
      token,
      args,
      this,
    )
 
    if (error) {
      throw error
    }
 
    return instance
  }
 
  /**
   * Invalidates a service and its dependencies.
   */
  async invalidate(service: unknown): Promise<void> {
    // Find the service by instance
    const holder = this.storage.findByInstance(service)
    if (!holder) {
      this.logger?.warn(
        `[Container] Service instance not found for invalidation`,
      )
      return
    }
 
    await this.serviceInvalidator.invalidateWithStorage(
      holder.name,
      this.storage,
    )
  }
 
  /**
   * Disposes the container and cleans up all resources.
   */
  async dispose(): Promise<void> {
    await this.serviceInvalidator.clearAllWithStorage(this.storage)
  }
 
  /**
   * @internal
   * Attempts to get an instance synchronously if it already exists.
   * Overrides base class to support requestId parameter for ScopedContainer compatibility.
   */
  override tryGetSync<T>(token: any, args?: any, requestId?: string): T | null {
    return this.tryGetSyncFromStorage(
      token,
      args,
      this.storage,
      requestId ?? this.requestId,
    )
  }
 
  /**
   * Begins a new request context and returns a ScopedContainer.
   */
  beginRequest(
    requestId: string,
    metadata?: Record<string, any>,
  ): ScopedContainer {
    if (this.activeRequestIds.has(requestId)) {
      throw new Error(`Request with ID ${requestId} already exists`)
    }
 
    this.activeRequestIds.add(requestId)
 
    return new ScopedContainer(this, this.registry, requestId, metadata)
  }
 
  /**
   * Gets all active request IDs.
   */
  getActiveRequestIds(): ReadonlySet<string> {
    return this.activeRequestIds
  }
 
  /**
   * Checks if a request is active.
   */
  hasActiveRequest(requestId: string): boolean {
    return this.activeRequestIds.has(requestId)
  }
 
  /**
   * Removes a request ID from active requests.
   * Called by ScopedContainer when request ends.
   */
  removeRequestId(requestId: string): void {
    this.activeRequestIds.delete(requestId)
  }
 
  // ============================================================================
  // INTERNAL METHODS FOR COMPONENT ACCESS
  // ============================================================================
 
  getStorage(): UnifiedStorage {
    return this.storage
  }
 
  getServiceInitializer(): ServiceInitializer {
    return this.serviceInitializer
  }
 
  getServiceInvalidator(): ServiceInvalidator {
    return this.serviceInvalidator
  }
 
  getTokenResolver(): TokenResolver {
    return this.tokenResolver
  }
 
  getNameResolver(): NameResolver {
    return this.nameResolver
  }
 
  getScopeTracker(): ScopeTracker {
    return this.scopeTracker
  }
 
  getEventBus(): LifecycleEventBus {
    return this.eventBus
  }
 
  getRegistry(): Registry {
    return this.registry
  }
 
  getInstanceResolver(): InstanceResolver {
    return this.instanceResolver
  }
}