@sliday/tamp 0.4.1 → 0.4.2
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/compress.js +15 -8
- package/index.js +22 -47
- package/package.json +1 -1
- package/stats.js +3 -1
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
|
|
102
|
-
if (
|
|
103
|
-
return
|
|
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)
|
|
234
|
+
if (pruned) {
|
|
235
|
+
parsedValue = pruned.value
|
|
236
|
+
raw = pruned.minified
|
|
237
|
+
}
|
|
234
238
|
}
|
|
235
239
|
|
|
236
|
-
|
|
237
|
-
|
|
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(
|
|
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(
|
|
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
|
|
39
|
+
function openUpstream(method, upstreamUrl, headers, res) {
|
|
40
40
|
const mod = upstreamUrl.protocol === 'https:' ? https : http
|
|
41
|
-
const
|
|
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) => {
|
|
@@ -68,63 +66,40 @@ function forwardRequest(method, upstreamUrl, headers, body, res) {
|
|
|
68
66
|
upstream.destroy()
|
|
69
67
|
})
|
|
70
68
|
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|
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.
|
|
13
|
+
"version": "0.4.2",
|
|
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
|
}
|