@backblaze-labs/b2-sdk - v0.1.0
    Preparing search index...

    Class B2Simulator

    In-memory B2 simulator for testing. Implements the B2 native API at the request/response level without any network I/O. Supports 25+ operations including buckets, files, large files, keys, and notifications.

    const sim = new B2Simulator()
    const client = new B2Client({
    applicationKeyId: 'test-key-id',
    applicationKey: 'test-key',
    transport: sim.transport(),
    })
    await client.authorize()
    Index

    Constructors

    Methods

    • Advance the simulator's virtual clock by ms milliseconds. Used in conjunction with strictAuth: true + a finite authTokenTtlMs to force token expiry without setTimeout-based delays.

      Parameters

      • ms: number

        Milliseconds to advance. Negative values rewind (rarely useful).

      Returns void

    • Remove every registered fault. Equivalent to calling .clear() on every handle returned by injectFailure, plus a defensive reset for tests that re-use a simulator across cases.

      Returns void

    • Wait for every fire-and-forget hook (onWebhookDeliver, onReplicate) currently in flight to settle. Use in tests after an upload/copy/finish-large-file call to deterministically observe hook side effects without microtask-flush guesswork.

      Returns Promise<void>

      A promise that resolves once every pending hook callback has either resolved or rejected.

    • Handles file download requests (b2_download_file_by_id, /file/ by name). Returns the file data along with B2 response headers.

      Parameters

      • path: string

        The request URL path identifying the file to download.

      • headers: Record<string, string>

        The HTTP request headers for range or authorization.

      • method: "GET" | "HEAD" = 'GET'

        The HTTP method; 'HEAD' suppresses the response body.

      Returns SimulatorDownloadResponse

      The download response containing file data and B2 headers.

    • Dispatches a JSON API request to the appropriate handler.

      Parameters

      • _method: string

        The HTTP method (unused).

      • path: string

        The request URL path containing the B2 endpoint name.

      • headers: Record<string, string>

        The HTTP request headers; consulted by the strict-auth gate to look up the issued auth token.

      • body: unknown

        The parsed JSON request body.

      Returns Promise<SimulatorJsonResponse>

      An object with HTTP status and JSON response body.

    • Handles file and part upload requests (b2_upload_file, b2_upload_part). Dispatches to the appropriate internal handler based on the URL.

      Parameters

      • url: string

        The upload endpoint URL used to determine the upload type.

      • headers: Record<string, string>

        The HTTP headers containing file metadata and authorization.

      • data: Uint8Array

        The raw file or part content as bytes.

      Returns Promise<SimulatorJsonResponse>

      A promise resolving to an object with HTTP status and JSON response body.

    • Register a synthetic failure to inject on requests whose URL contains spec.on. Use this to exercise retry / backoff / error-handling paths in tests without hand-rolling a wrapping HttpTransport. The fault is consumed in registration order on each matched request; once its count budget is exhausted it auto-retires.

      Faults are checked BEFORE the simulator's real handlers run, so a matched request never touches in-memory state — failed uploads don't create partial parts, failed deletes don't remove anything.

      Parameters

      Returns FaultHandle

      A handle whose clear() method removes this specific fault registration (other faults remain in effect).

      // Fail the next 2 b2_upload_part calls with 503, then succeed.
      sim.injectFailure({ on: 'b2_upload_part', status: 503, count: 2 })

      // Fail every b2_authorize_account with 401 + Retry-After: 5.
      const handle = sim.injectFailure({
      on: 'b2_authorize_account',
      status: 401,
      code: 'expired_auth_token',
      retryAfter: 5,
      })
      // ... later
      handle.clear()