@sliday/tamp 0.8.1 → 0.8.3

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/index.js +44 -12
  2. package/package.json +1 -1
  3. package/stats.js +21 -1
package/index.js CHANGED
@@ -6,7 +6,7 @@ import * as fzstd from 'fzstd'
6
6
  import { loadConfig } from './config.js'
7
7
  import { compressRequest } from './compress.js'
8
8
  import { detectProvider, detectOpenAIAuthMode, resolveOpenAIUpstream } from './providers.js'
9
- import { createSession, formatRequestLog } from './stats.js'
9
+ import { createSession, formatRequestLog, redactUrl } from './stats.js'
10
10
  import { createSessionStore, deriveSessionKey } from './session-graph.js'
11
11
  import { createReadCache } from './lib/read-cache.js'
12
12
  import { createBrCache } from './lib/br-cache.js'
@@ -95,6 +95,41 @@ export function createProxy(overrides = {}, loadOpts = {}) {
95
95
  return { config, session, sessionStore, readCache, brCache, server: _createServer(config, session, sessionStore, readCache, brCache) }
96
96
  }
97
97
 
98
+ // Stream-decompress a zstd body, aborting as soon as the running output size
99
+ // crosses `limit`. fzstd's one-shot decompress() inflates the entire frame
100
+ // into memory before any size check, so a small "bomb" frame could OOM the
101
+ // proxy; streaming bounds peak memory to ~limit + one block. Throws on excess.
102
+ export function decompressZstd(bytes, limit) {
103
+ const input = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)
104
+ const parts = []
105
+ let total = 0
106
+ const dctx = new fzstd.Decompress((chunk) => {
107
+ total += chunk.length
108
+ if (total > limit) throw new Error('zstd decompression exceeded size limit')
109
+ parts.push(chunk)
110
+ })
111
+ dctx.push(input, true)
112
+ const out = Buffer.allocUnsafe(total)
113
+ let offset = 0
114
+ for (const p of parts) { out.set(p, offset); offset += p.length }
115
+ return out
116
+ }
117
+
118
+ // Surface an upstream connection error to the client. Before any response
119
+ // bytes are sent we can emit a clean 502 JSON body. Once the response has
120
+ // started streaming (headersSent) or finished (writableEnded), appending JSON
121
+ // would corrupt the (possibly SSE) stream the client is reading, so we tear
122
+ // the socket down instead. Returns true if a JSON error body was written.
123
+ export function sendUpstreamError(res) {
124
+ if (res.headersSent || res.writableEnded) {
125
+ res.destroy()
126
+ return false
127
+ }
128
+ res.writeHead(502, { 'Content-Type': 'application/json' })
129
+ res.end(JSON.stringify({ error: 'upstream_error', message: 'failed to connect to upstream' }))
130
+ return true
131
+ }
132
+
98
133
  function _createServer(config, session, sessionStore, readCache, brCache) {
99
134
  const log = createRequestLogger(config)
100
135
  const disclosureTotals = { rehydrated: 0, missed: 0, disclosed: 0 }
@@ -118,10 +153,7 @@ function openUpstream(method, upstreamUrl, headers, res) {
118
153
 
119
154
  upstream.on('error', (err) => {
120
155
  log(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
121
- if (!res.headersSent) {
122
- res.writeHead(502, { 'Content-Type': 'application/json' })
123
- }
124
- res.end(JSON.stringify({ error: 'upstream_error', message: 'failed to connect to upstream' }))
156
+ sendUpstreamError(res)
125
157
  })
126
158
 
127
159
  res.on('error', (err) => {
@@ -209,7 +241,7 @@ return http.createServer(async (req, res) => {
209
241
  const provider = detectProvider(req.method, req.url, req.headers)
210
242
 
211
243
  if (!provider) {
212
- log(`[tamp] ${req.method} ${req.url}`)
244
+ log(`[tamp] ${req.method} ${redactUrl(req.url)}`)
213
245
  const upstreamUrl = buildUpstreamUrl(req.url, config.upstream)
214
246
  return pipeRequest(req, res, upstreamUrl)
215
247
  }
@@ -224,7 +256,11 @@ return http.createServer(async (req, res) => {
224
256
  let size = 0
225
257
  let overflow = false
226
258
 
227
- for await (const chunk of req) {
259
+ // destroyOnReturn:false so breaking out on overflow does NOT destroy the
260
+ // request stream — pipeRequest() still needs to forward the unread remainder
261
+ // to the upstream. Without this, an over-maxBody body is truncated while its
262
+ // original content-length is preserved, hanging the upstream.
263
+ for await (const chunk of req.iterator({ destroyOnReturn: false })) {
228
264
  size += chunk.length
229
265
  chunks.push(chunk)
230
266
  if (size > config.maxBody) {
@@ -263,11 +299,7 @@ return http.createServer(async (req, res) => {
263
299
  textBody = zlib.brotliDecompressSync(rawBody, { maxOutputLength: MAX_DECOMPRESSED })
264
300
  decompressed = true
265
301
  } else if (encoding === 'zstd') {
266
- const decompressedBytes = fzstd.decompress(new Uint8Array(rawBody))
267
- if (decompressedBytes.length > MAX_DECOMPRESSED) {
268
- throw new Error('zstd decompression exceeded size limit')
269
- }
270
- textBody = Buffer.from(decompressedBytes)
302
+ textBody = decompressZstd(rawBody, MAX_DECOMPRESSED)
271
303
  decompressed = true
272
304
  } else if (encoding && encoding !== 'identity') {
273
305
  // Unknown encoding — can't decompress, passthrough as-is
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "sidecar/server.py",
15
15
  "sidecar/requirements.txt"
16
16
  ],
17
- "version": "0.8.1",
17
+ "version": "0.8.3",
18
18
  "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 60-70% combined input+output savings with Caveman-integrated presets and per-command rewriters.",
19
19
  "type": "module",
20
20
  "main": "index.js",
package/stats.js CHANGED
@@ -9,10 +9,30 @@ const c = {
9
9
  red: '\x1b[31m',
10
10
  }
11
11
 
12
+ // Strip secret values from a request URL before logging. Some providers
13
+ // (Gemini and OpenAI-compatible gateways) carry the API key in the query
14
+ // string (?key=, ?api_key=), which would otherwise land in stdout, log
15
+ // files, and the systemd journal. Path-relative URLs only — no base needed.
16
+ const SECRET_QUERY_PARAM = /^(key|api_key|apikey|access_token|token)$/i
17
+ export function redactUrl(url) {
18
+ if (typeof url !== 'string') return url
19
+ const q = url.indexOf('?')
20
+ if (q === -1) return url
21
+ const path = url.slice(0, q)
22
+ const query = url.slice(q + 1)
23
+ const redacted = query.split('&').map((pair) => {
24
+ const eq = pair.indexOf('=')
25
+ if (eq === -1) return pair
26
+ const name = pair.slice(0, eq)
27
+ return SECRET_QUERY_PARAM.test(name) ? `${name}=REDACTED` : pair
28
+ }).join('&')
29
+ return `${path}?${redacted}`
30
+ }
31
+
12
32
  export function formatRequestLog(stats, session, providerName, url, bodySize, tokenCost, sessionBucket) {
13
33
  const compressed = stats.filter(s => s.method)
14
34
  const label = providerName || 'anthropic'
15
- const path = url || '/v1/messages'
35
+ const path = redactUrl(url) || '/v1/messages'
16
36
  const sizeInfo = bodySize ? ` ${c.dim}${fmtSize(bodySize)}${c.reset}` : ''
17
37
 
18
38
  if (!compressed.length && !stats.length) {