Constructs a new in-memory B2 simulator.
Optional simulator overrides. See B2SimulatorOptions.
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.
Milliseconds to advance. Negative values rewind (rarely useful).
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.
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.
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.
The request URL path identifying the file to download.
The HTTP request headers for range or authorization.
The HTTP method; 'HEAD' suppresses the response body.
The download response containing file data and B2 headers.
Dispatches a JSON API request to the appropriate handler.
The HTTP method (unused).
The request URL path containing the B2 endpoint name.
The HTTP request headers; consulted by the strict-auth gate to look up the issued auth token.
The parsed JSON request body.
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.
The upload endpoint URL used to determine the upload type.
The HTTP headers containing file metadata and authorization.
The raw file or part content as bytes.
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.
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()
Creates an HttpTransport that routes requests to this simulator.
A transport instance backed by this in-memory simulator.
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.
Example