All files / src/internal/core scope-tracker.mts

87.75% Statements 43/49
83.87% Branches 26/31
100% Functions 5/5
87.75% Lines 43/49

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                                    289x 289x 289x                                                       37x       3x       34x 3x     3x       31x       37x 37x               37x 31x                                                           2x 2x               2x 1x   1x                                                             33x           33x 33x 1x     1x       32x 32x   6x     26x 26x             26x           26x   24x   24x   24x   24x           24x       2x 2x 2x     2x             2x                                   29x     29x 29x        
import type { InjectableScope } from '../../enums/index.mjs'
import type { InjectionToken } from '../../token/injection-token.mjs'
import type { IHolderStorage } from '../holder/holder-storage.interface.mjs'
 
import { InjectableScope as Scope } from '../../enums/index.mjs'
import { DIError } from '../../errors/index.mjs'
import { InstanceStatus } from '../holder/instance-holder.mjs'
import { Registry } from '../../token/registry.mjs'
import { NameResolver } from './name-resolver.mjs'
 
/**
 * Component for tracking and handling scope upgrades.
 *
 * Detects when a Singleton service needs to be upgraded to Request scope
 * and coordinates the scope upgrade process atomically.
 */
export class ScopeTracker {
  constructor(
    private readonly registry: Registry,
    private readonly nameResolver: NameResolver,
    private readonly logger: Console | null = null,
  ) {}
 
  /**
   * Checks if a dependency requires scope upgrade and performs it if needed.
   * Called during service resolution when a dependency is resolved.
   *
   * @param currentServiceName - Name of the service being created
   * @param currentServiceScope - Current scope of the service being created
   * @param dependencyName - Name of the dependency being resolved
   * @param dependencyScope - Scope of the dependency
   * @param dependencyToken - Token of the dependency
   * @param singletonStorage - Singleton storage instance
   * @param requestStorage - Request storage instance (if in request context)
   * @param requestId - Request ID (if in request context)
   * @returns [needsUpgrade: boolean, newName?: string] - whether upgrade occurred and new name
   */
  checkAndUpgradeScope(
    currentServiceName: string,
    currentServiceScope: InjectableScope,
    dependencyName: string,
    dependencyScope: InjectableScope,
    dependencyToken: InjectionToken<any, any>,
    singletonStorage: IHolderStorage,
    requestStorage?: IHolderStorage,
    requestId?: string,
  ): [boolean, string?] {
    // Only upgrade if current service is Singleton and dependency is Request
    if (
      currentServiceScope !== Scope.Singleton ||
      dependencyScope !== Scope.Request
    ) {
      return [false]
    }
 
    // Need request storage and requestId for upgrade
    if (!requestStorage || !requestId) {
      this.logger?.warn(
        `[ScopeTracker] Cannot upgrade scope for ${currentServiceName}: missing requestStorage or requestId`,
      )
      return [false]
    }
 
    // Perform the upgrade
    this.logger?.log(
      `[ScopeTracker] Upgrading ${currentServiceName} from Singleton to Request scope`,
    )
 
    try {
      const [success, newName] = this.upgradeScopeToRequestSync(
        currentServiceName,
        dependencyToken,
        singletonStorage,
        requestStorage,
        requestId,
      )
 
      if (success && newName) {
        return [true, newName]
      }
    } catch (error) {
      this.logger?.error(
        `[ScopeTracker] Error upgrading scope for ${currentServiceName}:`,
        error,
      )
    }
 
    return [false]
  }
 
  /**
   * Performs the actual scope upgrade from Singleton to Request.
   * This is the core migration logic.
   *
   * @param serviceName - Current service name (without requestId)
   * @param token - Service injection token
   * @param singletonStorage - Source storage
   * @param requestStorage - Target storage
   * @param requestId - Request ID to include in new name
   * @returns [success: boolean, newName?: string, error?: DIError]
   */
  async upgradeScopeToRequest(
    serviceName: string,
    token: InjectionToken<any, any>,
    singletonStorage: IHolderStorage,
    requestStorage: IHolderStorage,
    requestId: string,
  ): Promise<[boolean, string?, DIError?]> {
    try {
      const [success, newName] = this.upgradeScopeToRequestSync(
        serviceName,
        token,
        singletonStorage,
        requestStorage,
        requestId,
      )
 
      if (success && newName) {
        return [true, newName]
      }
      return [
        false,
        undefined,
        DIError.storageError(
          'Scope upgrade failed',
          'upgradeScopeToRequest',
          serviceName,
        ),
      ]
    } catch (error) {
      return [
        false,
        undefined,
        error instanceof DIError ? error : DIError.unknown(error as Error),
      ]
    }
  }
 
  /**
   * Synchronous part of scope upgrade - handles immediate updates.
   * Async operations (like waiting for holder creation) should be done separately.
   */
  private upgradeScopeToRequestSync(
    serviceName: string,
    token: InjectionToken<any, any>,
    singletonStorage: IHolderStorage,
    requestStorage: IHolderStorage,
    requestId: string,
  ): [boolean, string?] {
    // 1. Upgrade existing instance name to include requestId
    // This preserves any args hash that might be in the original name
    const newName = this.nameResolver.upgradeInstanceNameToRequest(
      serviceName,
      requestId,
    )
 
    // 2. Update Registry scope to Request (synchronous)
    const updated = this.registry.updateScope(token, Scope.Request)
    if (!updated) {
      this.logger?.warn(
        `[ScopeTracker] Could not update scope in registry for ${serviceName}`,
      )
      return [false]
    }
 
    // 3. Check if holder exists in singleton storage
    const holderResult = singletonStorage.get(serviceName)
    if (holderResult === null) {
      // No holder exists yet - just update registry, future resolutions will use request storage
      return [true, newName]
    }
 
    const [error, holder] = holderResult
    Iif (error) {
      this.logger?.warn(
        `[ScopeTracker] Holder for ${serviceName} is in error state: ${error.message}`,
      )
      return [false]
    }
 
    Iif (!holder) {
      return [false]
    }
 
    // 4. If holder is in "Creating" state, we need to wait for it before migrating
    // For now, we'll update the name and move it, but the caller should wait for creation
    if (holder.status === InstanceStatus.Creating) {
      // Update holder name
      holder.name = newName
      // Move to request storage
      requestStorage.set(newName, holder)
      // Remove from singleton storage
      singletonStorage.delete(serviceName)
      // Update parent dependencies
      this.updateParentDependencies(
        serviceName,
        newName,
        singletonStorage,
        requestStorage,
      )
      return [true, newName]
    }
 
    // 5. Move holder from singleton to request storage
    holder.name = newName
    requestStorage.set(newName, holder)
    singletonStorage.delete(serviceName)
 
    // 6. Update all parent dependencies
    this.updateParentDependencies(
      serviceName,
      newName,
      singletonStorage,
      requestStorage,
    )
 
    return [true, newName]
  }
 
  /**
   * Updates all parent dependencies to reference the new service name.
   *
   * @param oldName - Original service name
   * @param newName - New service name with requestId
   * @param singletonStorage - Singleton storage to check
   * @param requestStorage - Request storage to check
   */
  updateParentDependencies(
    oldName: string,
    newName: string,
    singletonStorage: IHolderStorage,
    requestStorage?: IHolderStorage,
  ): void {
    // Update dependencies in singleton storage
    singletonStorage.updateDependencyReference(oldName, newName)
 
    // Update dependencies in request storage if provided
    Eif (requestStorage) {
      requestStorage.updateDependencyReference(oldName, newName)
    }
  }
}