@neurocode-ai/http-recorder 1.18.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +217 -0
  3. package/package.json +60 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 opencode
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,217 @@
1
+ # @opencode-ai/http-recorder
2
+
3
+ Record real Effect HTTP and WebSocket traffic once, then replay it from deterministic JSON cassettes.
4
+
5
+ Use it for provider integrations, retries, polling, multi-step flows, and any test where hand-written HTTP mocks hide too much of the real request shape.
6
+
7
+ > Public beta. The API depends on Effect 4 beta and may change with Effect's unstable transport modules.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ bun add effect@4.0.0-beta.74
13
+ bun add -d @opencode-ai/http-recorder@beta @effect/vitest vitest
14
+ ```
15
+
16
+ The package supports Node.js 22+ and Bun. It is not intended for browsers, workers, or Deno.
17
+
18
+ Effect `4.0.0-beta.74` has a known declaration error (`SchemaErrorTypeId` is missing). Until that upstream declaration is fixed, TypeScript consumers need:
19
+
20
+ ```json
21
+ {
22
+ "compilerOptions": {
23
+ "skipLibCheck": true
24
+ }
25
+ }
26
+ ```
27
+
28
+ ## Quick Start
29
+
30
+ ```ts
31
+ import { assert, describe, it } from "@effect/vitest"
32
+ import { Effect, Schema } from "effect"
33
+ import { HttpClient, HttpClientRequest } from "effect/unstable/http"
34
+ import { HttpRecorder } from "@opencode-ai/http-recorder"
35
+
36
+ const User = Schema.Struct({
37
+ id: Schema.Number,
38
+ name: Schema.String,
39
+ })
40
+
41
+ const getUser = Effect.gen(function* () {
42
+ const http = yield* HttpClient.HttpClient
43
+ const response = yield* http.execute(HttpClientRequest.get("https://jsonplaceholder.typicode.com/users/1"))
44
+ return yield* Schema.decodeUnknownEffect(User)(yield* response.json)
45
+ })
46
+
47
+ describe("getUser", () => {
48
+ it.effect("loads a user", () =>
49
+ Effect.gen(function* () {
50
+ const user = yield* getUser
51
+
52
+ assert.strictEqual(user.id, 1)
53
+ assert.strictEqual(user.name, "Leanne Graham")
54
+ }).pipe(Effect.provide(HttpRecorder.http("users/get-one"))),
55
+ )
56
+ })
57
+ ```
58
+
59
+ Run the test with Vitest. The first local run calls the real API and records:
60
+
61
+ ```sh
62
+ bunx vitest run users.test.ts
63
+ ```
64
+
65
+ ```text
66
+ test/fixtures/recordings/users/get-one.json
67
+ ```
68
+
69
+ Later runs replay that cassette without contacting the upstream server. When `CI=true`, missing cassettes fail instead of recording.
70
+
71
+ ```mermaid
72
+ flowchart TD
73
+ Run[Run test] --> Recorded{Cassette recorded?}
74
+ Recorded -->|Yes| Replay[Replay cassette]
75
+ Recorded -->|No, local| Record[Call service and record cassette]
76
+ Recorded -->|No, CI| Fail[Fail: cassette missing]
77
+ ```
78
+
79
+ Application code does not need to know whether a response is live or replayed.
80
+
81
+ ## API
82
+
83
+ ```ts
84
+ HttpRecorder.http(name, options?)
85
+ HttpRecorder.socket(name, options?)
86
+ ```
87
+
88
+ That is the complete public API. `http` provides a fetch-backed recorded `HttpClient`. `socket` decorates a standard Effect `Socket.Socket` supplied beneath it.
89
+
90
+ ## WebSockets
91
+
92
+ WebSocket cassettes preserve one ordered transcript of client and server text or binary frames. Replay follows that chronology: server frames are released until the next recorded client frame, then replay waits for the application to send the matching frame before continuing.
93
+
94
+ ```ts
95
+ import { assert, it } from "@effect/vitest"
96
+ import { NodeSocket } from "@effect/platform-node"
97
+ import { Effect, Layer } from "effect"
98
+ import { Socket } from "effect/unstable/socket"
99
+ import { HttpRecorder } from "@opencode-ai/http-recorder"
100
+
101
+ const echo = Effect.gen(function* () {
102
+ const socket = yield* Socket.Socket
103
+ const write = yield* socket.writer
104
+
105
+ yield* socket.runString(
106
+ (message) =>
107
+ Effect.gen(function* () {
108
+ assert.strictEqual(message, "hello")
109
+ yield* write(new Socket.CloseEvent(1000))
110
+ }),
111
+ { onOpen: write("hello") },
112
+ )
113
+ })
114
+
115
+ const recordedSocket = HttpRecorder.socket("echo/hello").pipe(
116
+ Layer.provide(
117
+ NodeSocket.layerWebSocket("wss://ws.postman-echo.com/raw", {
118
+ closeCodeIsError: (code) => code !== 1000,
119
+ }),
120
+ ),
121
+ )
122
+
123
+ it.effect("exchanges WebSocket frames", () => echo.pipe(Effect.provide(recordedSocket)))
124
+ ```
125
+
126
+ The application owns the WebSocket URL and protocols through normal Effect layer wiring. The recorder wraps that socket without duplicating its URL in recorder configuration. Provide separate socket layers for separate endpoints or concurrent connections.
127
+
128
+ Text frames use the same JSON-field and body redaction as HTTP bodies. Binary frames are stored losslessly as base64. Client and server frame kinds must match during replay.
129
+
130
+ ## Refresh A Cassette
131
+
132
+ Delete exactly the recordings you want to replace, then rerun their tests:
133
+
134
+ ```sh
135
+ rm test/fixtures/recordings/users/get-one.json
136
+ bun run test users.test.ts
137
+ ```
138
+
139
+ There is intentionally no public overwrite mode. Deletion makes the set of recordings being refreshed visible and reviewable.
140
+
141
+ ## Redaction
142
+
143
+ Secure defaults remove most headers and redact common credentials in headers, URLs, and JSON bodies. Extend those defaults at layer construction:
144
+
145
+ ```ts
146
+ HttpRecorder.http("anthropic/messages", {
147
+ redact: {
148
+ headers: ["x-project-token"],
149
+ allowRequestHeaders: ["anthropic-version"],
150
+ queryParameters: ["session-id"],
151
+ jsonFields: ["user_id"],
152
+ url: (url) => url.replace(/\/accounts\/[^/]+/, "/accounts/{account}"),
153
+ body: (body) => body.replaceAll(/usr_[a-z0-9]+/g, "usr_redacted"),
154
+ },
155
+ })
156
+ ```
157
+
158
+ | Option | Purpose |
159
+ | ---------------------- | -------------------------------------------------------------------- |
160
+ | `headers` | Add sensitive header names. They are retained as `[REDACTED]`. |
161
+ | `allowRequestHeaders` | Preserve additional non-sensitive request headers for matching. |
162
+ | `allowResponseHeaders` | Preserve additional non-sensitive response headers for replay. |
163
+ | `queryParameters` | Add sensitive URL query parameter names. |
164
+ | `jsonFields` | Recursively redact matching JSON keys in requests and responses. |
165
+ | `url` | Stabilize a URL after built-in redaction. |
166
+ | `body` | Stabilize request and response bodies after built-in JSON redaction. |
167
+
168
+ Before writing, the recorder scans the complete cassette for common credential formats and values from credential-like environment variables. Unsafe cassettes fail without replacing an existing recording.
169
+
170
+ Redaction is defense in depth, not a substitute for review. Inspect cassette diffs before committing them.
171
+
172
+ ## Matching And Ordering
173
+
174
+ A cassette contains an ordered sequence of interactions. The first runtime request is checked against the first recorded request, the second against the second, and so on.
175
+
176
+ This strict ordering correctly models repeated identical requests whose responses change, including retries, polling, and cache tests. JSON object keys are canonicalized before matching.
177
+
178
+ Concurrent requests are recorded in request-start order even when their responses complete out of order.
179
+
180
+ Supply a custom equivalence rule when a request contains intentionally volatile data:
181
+
182
+ ```ts
183
+ HttpRecorder.http("events/create", {
184
+ match: (incoming, recorded) =>
185
+ incoming.method === recorded.method && new URL(incoming.url).pathname === new URL(recorded.url).pathname,
186
+ })
187
+ ```
188
+
189
+ ## Configuration
190
+
191
+ ```ts
192
+ interface RecorderOptions {
193
+ readonly directory?: string
194
+ readonly metadata?: Record<string, unknown>
195
+ readonly redact?: RedactOptions
196
+ readonly match?: RequestMatcher
197
+ }
198
+ ```
199
+
200
+ `directory` defaults to `<cwd>/test/fixtures/recordings`.
201
+
202
+ ## Cassettes
203
+
204
+ Cassettes are readable JSON files intended to be committed with your tests. HTTP interactions are stored in request order. WebSocket cassettes preserve the observed order of client and server frames. Text stays readable; binary bodies and frames are stored losslessly as base64.
205
+
206
+ ## Current Limits
207
+
208
+ - Responses are buffered while recording and replaying, so this beta is not suitable for tests that assert streaming timing, cancellation, or backpressure.
209
+ - WebSocket replay preserves frame chronology and content, not real network timing or backpressure.
210
+ - WebSocket V1 cassettes do not reproduce terminal close codes, close reasons, or transport failures. Failed and interrupted live runs are not recorded.
211
+ - WebSocket transcripts are retained in memory until the connection finishes; avoid using this beta for unbounded sessions.
212
+ - The package currently requires the exact Effect beta listed above.
213
+ - Cassette format version `1` has no migration tooling yet.
214
+
215
+ ## License
216
+
217
+ MIT
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/package.json",
3
+ "version": "1.18.8",
4
+ "name": "@neurocode-ai/http-recorder",
5
+ "description": "Record and replay Effect HTTP client traffic with deterministic cassettes",
6
+ "type": "module",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/anomalyco/opencode.git",
11
+ "directory": "packages/http-recorder"
12
+ },
13
+ "homepage": "https://github.com/anomalyco/opencode/tree/dev/packages/http-recorder",
14
+ "bugs": "https://github.com/anomalyco/opencode/issues",
15
+ "keywords": [
16
+ "effect",
17
+ "http",
18
+ "recording",
19
+ "replay",
20
+ "testing",
21
+ "vcr"
22
+ ],
23
+ "engines": {
24
+ "node": ">=22"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "test": "bun test --timeout 30000 --only-failures",
31
+ "typecheck": "tsgo --noEmit",
32
+ "build": "bun ./script/build.ts",
33
+ "verify:package": "bun ./script/verify-package.ts"
34
+ },
35
+ "exports": {
36
+ ".": "./src/index.ts",
37
+ "./internal": "./src/internal.ts"
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "README.md",
42
+ "CHANGELOG.md",
43
+ "LICENSE"
44
+ ],
45
+ "devDependencies": {
46
+ "@tsconfig/node22": "22.0.2",
47
+ "@types/bun": "1.3.13",
48
+ "@types/node": "24.12.2",
49
+ "@typescript/native-preview": "7.0.0-dev.20251207.1",
50
+ "effect": "4.0.0-beta.83",
51
+ "typescript": "5.8.2"
52
+ },
53
+ "dependencies": {
54
+ "@effect/platform-node": "4.0.0-beta.83",
55
+ "@effect/platform-node-shared": "4.0.0-beta.83"
56
+ },
57
+ "peerDependencies": {
58
+ "effect": "4.0.0-beta.83"
59
+ }
60
+ }