All files / src/container scoped-container.mts

92.45% Statements 49/53
75% Branches 18/24
100% Functions 16/16
92.45% Lines 49/53

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                                                                  128x     128x       128x 128x 128x     128x   128x 128x               172x       68x       375x       67x       130x                     152x             303x             8x             3x                                                                                               224x 3x       221x 221x   221x 221x 221x   151x       150x       150x         70x       70x       70x               4x 4x         4x                     1x                   86x 86x 86x       86x 52x           52x 5x         81x                     19x 1x     18x             125x       125x     125x     125x      
import type { z, ZodType } from 'zod/v4'
 
import type { Factorable } from '../interfaces/factory.interface.mjs'
import type {
  ClassType,
  ClassTypeWithArgument,
  FactoryInjectionToken,
  InjectionTokenSchemaType,
} from '../token/injection-token.mjs'
import type { Registry } from '../token/registry.mjs'
import type { Join, UnionToArray } from '../utils/types.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 { InjectableScope } from '../enums/index.mjs'
import { UnifiedStorage } from '../internal/holder/unified-storage.mjs'
import {
  BoundInjectionToken,
  InjectionToken,
} from '../token/injection-token.mjs'
import { AbstractContainer } from './abstract-container.mjs'
import { Container } from './container.mjs'
 
/**
 * Request-scoped dependency injection container.
 *
 * Wraps a parent Container and provides isolated request-scoped instances
 * while delegating singleton and transient resolution to the parent.
 * This design eliminates race conditions that can occur with async operations
 * when multiple requests are processed concurrently.
 */
export class ScopedContainer extends AbstractContainer {
  protected readonly defaultScope = InjectableScope.Request
 
  private readonly storage: UnifiedStorage
  private disposed = false
  private readonly metadata: Record<string, any>
 
  constructor(
    private readonly parent: Container,
    private readonly registry: Registry,
    public readonly requestId: string,
    metadata?: Record<string, any>,
  ) {
    super()
    // Create own unified storage for request-scoped services
    this.storage = new UnifiedStorage(InjectableScope.Request)
    this.metadata = metadata || {}
  }
 
  // ============================================================================
  // ABSTRACT METHOD IMPLEMENTATIONS - Delegate to parent
  // ============================================================================
 
  getStorage(): UnifiedStorage {
    return this.storage
  }
 
  protected getRegistry(): Registry {
    return this.registry
  }
 
  protected getTokenResolver(): TokenResolver {
    return this.parent.getTokenResolver()
  }
 
  protected getNameResolver(): NameResolver {
    return this.parent.getNameResolver()
  }
 
  protected getServiceInvalidator(): ServiceInvalidator {
    return this.parent.getServiceInvalidator()
  }
 
  // ============================================================================
  // SCOPED CONTAINER SPECIFIC METHODS
  // ============================================================================
 
  /**
   * Gets the request ID for this scoped container.
   */
  getRequestId(): string {
    return this.requestId
  }
 
  /**
   * Gets the parent container.
   */
  getParent(): Container {
    return this.parent
  }
 
  /**
   * Gets metadata from the request context.
   */
  getMetadata(key: string): any | undefined {
    return this.metadata[key]
  }
 
  /**
   * Sets metadata on the request context.
   */
  setMetadata(key: string, value: any): void {
    this.metadata[key] = value
  }
 
  /**
   * Gets an instance from the container.
   * Request-scoped services are resolved from this container's storage.
   * All other services are delegated to the parent container.
   */
  // #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,
  ) {
    if (this.disposed) {
      throw new Error('ScopedContainer has been disposed')
    }
 
    // Check if this is a request-scoped service
    const tokenResolver = this.getTokenResolver()
    const realToken = tokenResolver.getRegistryToken(token)
 
    Eif (this.registry.has(realToken)) {
      const record = this.registry.get(realToken)
      if (record.scope === InjectableScope.Request) {
        // Resolve request-scoped service from this container
        const [error, instance] = await this.parent
          .getInstanceResolver()
          .resolveRequestScopedInstance(token, args, this)
 
        Iif (error) {
          throw error
        }
 
        return instance
      }
    }
 
    // Delegate singleton/transient services to parent
    const [error, instance] = await this.parent
      .getInstanceResolver()
      .resolveInstance(token, args, this, this.storage, this.requestId)
 
    Iif (error) {
      throw error
    }
 
    return instance
  }
 
  /**
   * Invalidates a service and its dependencies.
   */
  async invalidate(service: unknown): Promise<void> {
    // Find the service by instance in request storage
    const holder = this.storage.findByInstance(service)
    Iif (!holder) {
      // Try parent storage
      return this.parent.invalidate(service)
    }
 
    await this.getServiceInvalidator().invalidateWithStorage(
      holder.name,
      this.storage,
    )
  }
 
  /**
   * Disposes the container and cleans up all resources.
   * Alias for endRequest().
   */
  async dispose(): Promise<void> {
    return this.endRequest()
  }
 
  /**
   * @internal
   * Attempts to get an instance synchronously if it already exists.
   * Checks request storage first, then delegates to parent.
   */
  override tryGetSync<T>(token: any, args?: any): T | null {
    // Check request storage first for request-scoped services
    const tokenResolver = this.getTokenResolver()
    const realToken = tokenResolver.getRegistryToken(token)
    const scope = this.registry.has(realToken)
      ? this.registry.get(realToken).scope
      : InjectableScope.Singleton
 
    if (scope === InjectableScope.Request) {
      const result = this.tryGetSyncFromStorage<T>(
        token,
        args,
        this.storage,
        this.requestId,
      )
      if (result !== null) {
        return result
      }
    }
 
    // Delegate to parent for singleton/transient
    return this.parent.tryGetSync<T>(token, args, this.requestId)
  }
 
  /**
   * Adds an instance to the container.
   * Overrides base class to check disposed state.
   */
  override addInstance<T>(
    token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>,
    instance: T,
  ): void {
    if (this.disposed) {
      throw new Error('ScopedContainer has been disposed')
    }
 
    super.addInstance(token, instance)
  }
 
  /**
   * Ends the request and cleans up all request-scoped services.
   */
  async endRequest(): Promise<void> {
    Iif (this.disposed) {
      return
    }
 
    this.disposed = true
 
    // Clear all request-scoped services
    await this.getServiceInvalidator().clearAllWithStorage(this.storage)
 
    // Remove request ID from parent
    this.parent.removeRequestId(this.requestId)
  }
}