← All languages

Servirtium for

JavaScript and TypeScript

Ships a built package on the GitHub release. Release v2.0.0-alpha.20.

FFI mechanism
koffi. A CommonJS package (with bundled .d.ts) that calls the native library through koffi, a Node FFI with prebuilt binaries and no node-gyp. The release tarball bundles the native libraries, so nothing else is fetched.
Toolchain
Node.js 22+ (package.json engines); TypeScript optional, types are bundled; koffi ^2.9.0 comes from the npm registry
Distribution
Ships: servirtium-vcr-2.0.0-alpha.20.tgz (npm tarball, eight natives bundled: linux x64/arm64 glibc and musl, macOS x64/arm64, Windows x64/arm64)
Finding the library
Loader order: .nativeLib(path) on the builder, SERVIRTIUM_VCR_LIB, the package's own native/<platform>-<arch>[-musl]/ directory (the tarball bundles these), then the OS loader. Normally you set nothing; FreeBSD is not in the tarball, so fetch that asset and set SERVIRTIUM_VCR_LIB.
On GitHub
javascript/ · README

Getting it

This language has a built artifact on the release: no source to vendor.

  1. Install the release tarball. npm 12 refuses URL dependencies by default, so allow them for direct dependencies of your project (commit the .npmrc so npm ci works later).
    VER=2.0.0-alpha.20
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/v$VER
    [ -f package.json ] || npm init -y
    grep -qs '^allow-remote' .npmrc || echo 'allow-remote=root' >> .npmrc
    npm install $REL/servirtium-vcr-$VER.tgz
    
  2. Or as a package.json dependency (a .sha256 sits beside the tarball on the release page).
    "@servirtium/vcr": "https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20/servirtium-vcr-2.0.0-alpha.20.tgz"
    
  3. Check it: replay a recorded tape through the bundled library.
    node -e '
    const { Vcr, VcrOutcome } = require("@servirtium/vcr");
    const vcr = Vcr.playback("tapes/single_get.md").port(0).start();
    fetch(vcr.baseUrl + "/ok").then(r => r.text()).then(t => {
      console.log(t.trim(), vcr.lastKind === VcrOutcome.Ok ? "(clean match)" : "(MISMATCH)");
      vcr.close();
    });'
    

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

import { Vcr } from '@servirtium/vcr'

const vcr = Vcr.record('tapes/climate_api.md', 'https://climatedataapi.worldbank.org')
  .port(0)
  .start()
try {
  const res = await fetch(`${vcr.baseUrl}/api/v1/countries`, { signal: AbortSignal.timeout(10000) })
  await res.text()          // consume the body before committing
  vcr.commit()              // the only thing that writes the tape
} finally {
  vcr.discard()             // no-op after commit(); drops the recording if anything threw
}

Playback

The same tape, replayed with no network.

import assert from 'node:assert/strict'
import { Vcr, VcrOutcome } from '@servirtium/vcr'

const vcr = Vcr.playback('tapes/climate_api.md').port(0).start()
try {
  const res = await fetch(`${vcr.baseUrl}/api/v1/countries`)
  await res.text()
  assert.equal(vcr.lastKind, VcrOutcome.Ok, vcr.lastError)   // optional: assert a clean match
} finally {
  vcr.close()
}

Idiomatic extras

discard() in finally, commit() in the body

The pattern the README quick-start uses: commit() on success, and the finally block drops the recording if an assertion threw.

const rec = Vcr.record(tape, upstream).port(0).start()
try {
  await drive(rec.baseUrl)
  rec.commit()
} finally {
  rec.discard()   // no-op after commit()
}

Redact credentials and JSON fields at record time

Applied when the tape is written; the live test still sees real bytes.

import { Vcr, VcrField } from '@servirtium/vcr'

Vcr.record(tape, upstream)
  .redactCredentials()
  .redactJsonField('api_key', '[REDACTED]', VcrField.ResponseBody)
  .port(0)
  .start()

Drift detection

With failIfChanged, commit() writes the fresh tape and throws VcrError if it differs from the one on disk.

const rec = Vcr.record(tape, upstream).failIfChanged().port(0).start()
// ... drive the SUT ...
rec.commit()   // throws VcrError on drift

Gotchas

  • Needs Node 22+; on older Node npm warns EBADENGINE and the package may fail at run time.
  • npm 12 fails a URL dependency with EALLOWREMOTE unless .npmrc has allow-remote=root (do not set 'all').
  • npm 12 prints 'install scripts blocked' for @servirtium/vcr and koffi; harmless, the tarball is prebuilt and koffi ships prebuilt binaries. Do not approve or force the scripts.
  • npm install @servirtium/vcr from the registry fails with E404: not published. Do not npm install github:servirtium/servirtium-vcr (monorepo, no package at the root).
  • Tape paths are relative to the process working directory, not the test file.
  • A library that cannot be opened makes koffi throw 'Failed to load shared library' at the first .start(), not at require.
  • Consume the response body before closing or committing the recorder.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your JavaScript and TypeScript project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this JavaScript and TypeScript project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for JavaScript and TypeScript. Follow https://servirtium.dev/languages/javascript/ exactly. Servirtium ships a built package for this language on the GitHub release v2.0.0-alpha.20: servirtium-vcr-2.0.0-alpha.20.tgz (npm tarball, eight natives bundled: linux x64/arm64 glibc and musl, macOS x64/arm64, Windows x64/arm64).
   Toolchain and minimum version: Node.js 22+ (package.json engines); TypeScript optional, types are bundled; koffi ^2.9.0 comes from the npm registry.
   How the binding finds the native library: Loader order: .nativeLib(path) on the builder, SERVIRTIUM_VCR_LIB, the package's own native/<platform>-<arch>[-musl]/ directory (the tarball bundles these), then the OS loader. Normally you set nothing; FreeBSD is not in the tarball, so fetch that asset and set SERVIRTIUM_VCR_LIB.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic JavaScript and TypeScript way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for JavaScript and TypeScript is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- Needs Node 22+; on older Node npm warns EBADENGINE and the package may fail at run time.
- npm 12 fails a URL dependency with EALLOWREMOTE unless .npmrc has allow-remote=root (do not set 'all').
- npm 12 prints 'install scripts blocked' for @servirtium/vcr and koffi; harmless, the tarball is prebuilt and koffi ships prebuilt binaries. Do not approve or force the scripts.
- npm install @servirtium/vcr from the registry fails with E404: not published. Do not npm install github:servirtium/servirtium-vcr (monorepo, no package at the root).
- Tape paths are relative to the process working directory, not the test file.
- A library that cannot be opened makes koffi throw 'Failed to load shared library' at the first .start(), not at require.
- Consume the response body before closing or committing the recorder.

References:
- This language's page: https://servirtium.dev/languages/javascript/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/javascript
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

Real run on macOS arm64 (Node 26.13.1, npm 11.20.0): npm install of the actual release asset servirtium-vcr-2.0.0-alpha.20.tgz (bundled macOS arm64 native, nothing set in the environment). Ran the README 'Getting it' playback check (ok-body (clean match)); the full README 'Record and replay a local service' script (hello.md recorded with commit, replayed with the upstream stopped); redactCredentials + redactJsonField (secret and bearer token absent, [REDACTED] present); a discarded recording (no tape). Not run: npm 12 (the allow-remote line is from the README), TypeScript compile, failIfChanged.

They mirror these files in the repository:

See also the feature reference, llms.txt and the javascript/ directory.