@sliday/tamp 0.4.1 → 0.4.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.
package/README.md CHANGED
@@ -326,6 +326,16 @@ Max plans have fixed token budgets. With 32% fewer input tokens per request, you
326
326
 
327
327
  Based on 5 sessions/day, 200 requests/session, 60% compressible traffic.
328
328
 
329
+ ## Star History
330
+
331
+ <a href="https://www.star-history.com/?repos=sliday%2Ftamp&type=date&legend=top-left">
332
+ <picture>
333
+ <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/image?repos=sliday/tamp&type=date&theme=dark&legend=top-left" />
334
+ <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/image?repos=sliday/tamp&type=date&legend=top-left" />
335
+ <img alt="Star History Chart" src="https://api.star-history.com/image?repos=sliday/tamp&type=date&legend=top-left" />
336
+ </picture>
337
+ </a>
338
+
329
339
  ## License
330
340
 
331
341
  MIT
package/compress.js CHANGED
@@ -98,9 +98,9 @@ function pruneJSON(text) {
98
98
  const { ok, value } = tryParseJSON(text)
99
99
  if (!ok) return null
100
100
  const pruned = deepPrune(value)
101
- const result = JSON.stringify(pruned, null, 2)
102
- if (result.length >= text.length * 0.95) return null
103
- return result
101
+ const minified = JSON.stringify(pruned)
102
+ if (minified.length >= text.length * 0.95) return null
103
+ return { value: pruned, minified }
104
104
  }
105
105
 
106
106
  // --- Stage: strip-comments (opt-in, not in defaults) ---
@@ -228,22 +228,29 @@ export function compressText(text, config) {
228
228
  let raw = cls === 'json-lined' ? stripLineNumbers(text) : text
229
229
 
230
230
  // Prune low-value fields before minifying
231
+ let parsedValue
231
232
  if (config.stages.includes('prune')) {
232
233
  const pruned = pruneJSON(raw)
233
- if (pruned) raw = pruned
234
+ if (pruned) {
235
+ parsedValue = pruned.value
236
+ raw = pruned.minified
237
+ }
234
238
  }
235
239
 
236
- const { ok, value } = tryParseJSON(raw)
237
- if (!ok) return null
240
+ if (!parsedValue) {
241
+ const { ok, value } = tryParseJSON(raw)
242
+ if (!ok) return null
243
+ parsedValue = value
244
+ }
238
245
 
239
- const minified = JSON.stringify(value)
246
+ const minified = JSON.stringify(parsedValue)
240
247
  if (minified.length >= text.length) return null
241
248
 
242
249
  let best = { text: minified, method: 'minify' }
243
250
 
244
251
  if (config.stages.includes('toon')) {
245
252
  try {
246
- const tooned = encode(value)
253
+ const tooned = encode(parsedValue)
247
254
  if (tooned.length < best.text.length) {
248
255
  best = { text: tooned, method: 'toon' }
249
256
  }
package/index.js CHANGED
@@ -36,17 +36,15 @@ export function createProxy(overrides = {}) {
36
36
  function _createServer(config, session) {
37
37
  const log = createRequestLogger(config)
38
38
 
39
- function forwardRequest(method, upstreamUrl, headers, body, res) {
39
+ function openUpstream(method, upstreamUrl, headers, res) {
40
40
  const mod = upstreamUrl.protocol === 'https:' ? https : http
41
- const opts = {
41
+ const upstream = mod.request({
42
42
  hostname: upstreamUrl.hostname,
43
43
  port: upstreamUrl.port,
44
44
  path: upstreamUrl.pathname + upstreamUrl.search,
45
45
  method,
46
46
  headers,
47
- }
48
-
49
- const upstream = mod.request(opts, (upstreamRes) => {
47
+ }, (upstreamRes) => {
50
48
  res.writeHead(upstreamRes.statusCode, upstreamRes.headers)
51
49
  upstreamRes.pipe(res)
52
50
  upstreamRes.on('error', (err) => {
@@ -60,7 +58,7 @@ function forwardRequest(method, upstreamUrl, headers, body, res) {
60
58
  if (!res.headersSent) {
61
59
  res.writeHead(502, { 'Content-Type': 'application/json' })
62
60
  }
63
- res.end(JSON.stringify({ error: 'upstream_error', message: err.message }))
61
+ res.end(JSON.stringify({ error: 'upstream_error', message: 'failed to connect to upstream' }))
64
62
  })
65
63
 
66
64
  res.on('error', (err) => {
@@ -68,63 +66,40 @@ function forwardRequest(method, upstreamUrl, headers, body, res) {
68
66
  upstream.destroy()
69
67
  })
70
68
 
71
- if (body) {
72
- upstream.end(body)
73
- } else {
74
- upstream.end()
75
- }
69
+ return upstream
70
+ }
76
71
 
72
+ function forwardRequest(method, upstreamUrl, headers, body, res) {
73
+ const upstream = openUpstream(method, upstreamUrl, headers, res)
74
+ if (body) upstream.end(body)
75
+ else upstream.end()
77
76
  return upstream
78
77
  }
79
78
 
80
79
  function pipeRequest(req, res, upstreamUrl, prefixChunks) {
81
- const mod = upstreamUrl.protocol === 'https:' ? https : http
82
80
  const headers = { ...req.headers }
83
81
  delete headers.host
84
-
85
- const opts = {
86
- hostname: upstreamUrl.hostname,
87
- port: upstreamUrl.port,
88
- path: upstreamUrl.pathname + upstreamUrl.search,
89
- method: req.method,
90
- headers,
91
- }
92
-
93
- const upstream = mod.request(opts, (upstreamRes) => {
94
- res.writeHead(upstreamRes.statusCode, upstreamRes.headers)
95
- upstreamRes.pipe(res)
96
- upstreamRes.on('error', (err) => {
97
- log(`[tamp] response stream error: ${err.code || ''} ${err.message}`)
98
- res.destroy()
99
- })
100
- })
101
-
102
- upstream.on('error', (err) => {
103
- log(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
104
- if (!res.headersSent) {
105
- res.writeHead(502, { 'Content-Type': 'application/json' })
106
- }
107
- res.end(JSON.stringify({ error: 'upstream_error', message: err.message }))
108
- })
109
-
110
- res.on('error', (err) => {
111
- log(`[tamp] client disconnect: ${err.code || ''} ${err.message}`)
112
- upstream.destroy()
113
- })
114
-
82
+ const upstream = openUpstream(req.method, upstreamUrl, headers, res)
115
83
  if (prefixChunks) {
116
- for (const chunk of prefixChunks) {
117
- upstream.write(chunk)
118
- }
84
+ for (const chunk of prefixChunks) upstream.write(chunk)
119
85
  }
120
-
121
86
  req.pipe(upstream)
122
87
  }
123
88
 
124
89
  return http.createServer(async (req, res) => {
125
90
  // Health check endpoint
126
91
  if (req.url === '/health' && (req.method === 'GET' || req.method === 'HEAD')) {
127
- const body = JSON.stringify({ status: 'ok', version: config.version, stages: config.stages })
92
+ const totals = session.getTotals()
93
+ const body = JSON.stringify({
94
+ status: 'ok', version: config.version, stages: config.stages,
95
+ session: {
96
+ requests: totals.requestCount,
97
+ tokensSaved: totals.totalTokensSaved,
98
+ charsSaved: totals.totalSaved,
99
+ charsOriginal: totals.totalOriginal,
100
+ blocksCompressed: totals.compressionCount,
101
+ },
102
+ })
128
103
  res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) })
129
104
  return res.end(req.method === 'HEAD' ? undefined : body)
130
105
  }
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "providers.js",
11
11
  "stats.js"
12
12
  ],
13
- "version": "0.4.1",
13
+ "version": "0.4.3",
14
14
  "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 52.6% fewer input tokens.",
15
15
  "type": "module",
16
16
  "main": "index.js",
package/stats.js CHANGED
@@ -70,9 +70,11 @@ export function createSession() {
70
70
  let totalOriginal = 0
71
71
  let totalTokensSaved = 0
72
72
  let compressionCount = 0
73
+ let requestCount = 0
73
74
 
74
75
  return {
75
76
  record(stats) {
77
+ requestCount++
76
78
  for (const s of stats) {
77
79
  if (s.method && s.originalLen && s.compressedLen) {
78
80
  totalSaved += s.originalLen - s.compressedLen
@@ -83,7 +85,7 @@ export function createSession() {
83
85
  }
84
86
  },
85
87
  getTotals() {
86
- return { totalSaved, totalOriginal, totalTokensSaved, compressionCount }
88
+ return { totalSaved, totalOriginal, totalTokensSaved, compressionCount, requestCount }
87
89
  },
88
90
  }
89
91
  }