@sliday/tamp 0.8.7 → 0.8.16
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 +6 -1
- package/bin/tamp.js +1 -1
- package/compress.js +92 -16
- package/config.js +23 -4
- package/detect.js +48 -1
- package/index.js +9 -2
- package/lib/bm25.js +344 -45
- package/lib/code-chunks.js +269 -0
- package/lib/redact.js +78 -0
- package/lib/rewriters/commands/docker.js +7 -5
- package/lib/rewriters/commands/npm.js +5 -3
- package/lib/rewriters/commands/pytest.js +10 -4
- package/metadata.js +1 -1
- package/package.json +3 -1
- package/providers.js +13 -15
- package/session-graph.js +8 -1
- package/stats.js +8 -3
package/README.md
CHANGED
|
@@ -247,14 +247,19 @@ Supported on all providers: Anthropic, OpenAI Chat, OpenAI Responses (Codex), Ge
|
|
|
247
247
|
| Variable | Default | Description |
|
|
248
248
|
|----------|---------|-------------|
|
|
249
249
|
| `TAMP_PORT` | `7778` | Listen port |
|
|
250
|
+
| `TAMP_HOST` | `127.0.0.1` | Bind address. Loopback by default; set `0.0.0.0` to expose beyond the local machine (e.g. in a container) |
|
|
250
251
|
| `TAMP_UPSTREAM` | `https://api.anthropic.com` | Anthropic (default) upstream |
|
|
251
252
|
| `TAMP_UPSTREAM_OPENAI` | `https://api.openai.com` | Upstream for OpenAI-compatible routes (`/v1/chat/completions`, `/v1/responses`) |
|
|
252
253
|
| `TAMP_UPSTREAM_GEMINI` | `https://generativelanguage.googleapis.com` | Upstream for Gemini (`generateContent`) routes |
|
|
253
254
|
| `TAMP_UPSTREAM_KIMI` | `https://api.kimi.com` | Upstream for Kimi Code routes |
|
|
254
255
|
| `TAMP_UPSTREAM_MOONSHOT` | `https://api.moonshot.cn` | Upstream for Moonshot OpenAI-compatible routes |
|
|
255
256
|
| `TAMP_MIN_SIZE` | `200` | Min content size (chars) |
|
|
257
|
+
| `TAMP_MAX_BODY` | `10485760` | Max request body (bytes, 10 MB). Larger bodies pass through uncompressed |
|
|
256
258
|
| `TAMP_LOG` | `true` | Enable logging |
|
|
259
|
+
| `TAMP_LOG_FILE` | *(none)* | Append logs to this file path, in addition to stderr |
|
|
257
260
|
| `TAMP_CACHE_SAFE` | `true` | Compress newest only (prompt-cache safe) |
|
|
261
|
+
| `TAMP_REDACT` | `true` | Mask secrets in tool output before anything leaves the machine |
|
|
262
|
+
| `TAMP_REDACT_MODE` | `mask` | `mask` (replace with marker) or `remove` (delete value) |
|
|
258
263
|
| `TAMP_LLMLINGUA_URL` | *(none)* | LLMLingua sidecar URL. Set to `http://localhost:8788` to skip the auto-probe (avoids startup race if sidecar is still loading the model) |
|
|
259
264
|
|
|
260
265
|
**Recommended setups:**
|
|
@@ -361,7 +366,7 @@ The real win in v0.8 is **the level knob itself** — a zip-like 1–9 dial that
|
|
|
361
366
|
|
|
362
367
|
Tamp runs on `localhost` and forwards to the upstream you configure. Most stages are local-only: `llmlingua` talks to a sidecar on `127.0.0.1`, `foundation-models` stays on-device. The one stage that sends content off-box is `textpress` (opt-in, level 7+), which posts to OpenRouter when `OPENROUTER_API_KEY` is set — leave it off if your tool output is sensitive.
|
|
363
368
|
|
|
364
|
-
**Secret redaction** is on
|
|
369
|
+
**Secret redaction** is on by default ([#6](https://github.com/sliday/tamp/issues/6)). Before any stage runs — including ones that ship text off-box — Tamp masks high-confidence secrets in tool output: provider-shaped keys (AWS, GitHub, Anthropic/OpenAI, Google, Slack, Stripe), JWTs, PEM private-key blocks, and `UPPER_SNAKE` secret assignments from `.env`/shell (`API_TOKEN=…`, `DATABASE_PASSWORD=…`). Masking keeps structure (`API_TOKEN=‹redacted:secret›`) so the model still reasons about it. Set `TAMP_REDACT=false` to disable, or `TAMP_REDACT_MODE=remove` to delete rather than mask. Detection favors precision over recall: it won't catch every secret, so treat your upstream's data policy as the final boundary.
|
|
365
370
|
|
|
366
371
|
This repo ships an agent harness (built with [harn.app](https://harn.app)): `AGENTS.md` plus PreToolUse / Stop hooks under `scripts/harness/` that block dangerous shell commands and gate completion on `npm test`. See `.claude/settings.json`.
|
|
367
372
|
|
package/bin/tamp.js
CHANGED
|
@@ -529,7 +529,7 @@ if (skipPrompt) {
|
|
|
529
529
|
process.exit(1)
|
|
530
530
|
})
|
|
531
531
|
|
|
532
|
-
server.listen(config.port, () => {
|
|
532
|
+
server.listen(config.port, config.host, () => {
|
|
533
533
|
try { writePidFile(config.port) } catch (err) {
|
|
534
534
|
console.error(`[tamp] Warning: could not write PID file: ${err.message}`)
|
|
535
535
|
}
|
package/compress.js
CHANGED
|
@@ -3,8 +3,9 @@ import { spawn } from 'node:child_process'
|
|
|
3
3
|
import { encode } from '@toon-format/toon'
|
|
4
4
|
import { countTokens } from '@anthropic-ai/tokenizer'
|
|
5
5
|
import { createPatch } from 'diff'
|
|
6
|
-
import { tryParseJSON, classifyContent, stripLineNumbers } from './detect.js'
|
|
6
|
+
import { tryParseJSON, classifyContent, stripLineNumbers, jsonHasUnsafeInteger } from './detect.js'
|
|
7
7
|
import { rewriteCommandOutput } from './lib/rewriters/index.js'
|
|
8
|
+
import { redactText } from './lib/redact.js'
|
|
8
9
|
import { anthropic } from './providers.js'
|
|
9
10
|
import { graphDeduplicateTargets } from './session-graph.js'
|
|
10
11
|
import { detectTaskType, generateOutputRules, isDangerousTask } from './lib/rules-generator.js'
|
|
@@ -23,9 +24,17 @@ function cacheKey(text) {
|
|
|
23
24
|
export function clearCache() { cache.clear() }
|
|
24
25
|
|
|
25
26
|
function normalizeWhitespace(text) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
// Right-trim each line by hand. The former `/[ \t]+$/gm` is quadratic: a long
|
|
28
|
+
// run of spaces/tabs NOT at line end makes the greedy match restart at every
|
|
29
|
+
// offset, so a padded/aligned tool_result (tables, ASCII art) stalled the
|
|
30
|
+
// proxy event loop for seconds. This scan touches each char once.
|
|
31
|
+
let out = ''
|
|
32
|
+
for (let line of text.split('\n')) {
|
|
33
|
+
let e = line.length
|
|
34
|
+
while (e > 0 && (line[e - 1] === ' ' || line[e - 1] === '\t')) e--
|
|
35
|
+
out += (e === line.length ? line : line.slice(0, e)) + '\n'
|
|
36
|
+
}
|
|
37
|
+
return out.slice(0, -1).replace(/\n{3,}/g, '\n\n')
|
|
29
38
|
}
|
|
30
39
|
|
|
31
40
|
// --- Stage: dedup ---
|
|
@@ -158,14 +167,20 @@ function bm25TrimTargets(targets, body, provider) {
|
|
|
158
167
|
if (!userText) return 0
|
|
159
168
|
if (isDangerousTask(userText)) return 0
|
|
160
169
|
|
|
170
|
+
// File paths let the trimmer pick a block detector by extension so dropped
|
|
171
|
+
// lines respect code structure. Anthropic-only (see lib/path-extract.js);
|
|
172
|
+
// elsewhere `path` is null and the trimmer sniffs the body instead.
|
|
173
|
+
const paths = extractTargetPaths(body, targets)
|
|
174
|
+
|
|
161
175
|
let trimmed = 0
|
|
162
|
-
for (
|
|
176
|
+
for (let i = 0; i < targets.length; i++) {
|
|
177
|
+
const target = targets[i]
|
|
163
178
|
if (target.skip || target.dedup || target.diffed || target.readDiffed || target.graphed || target.disclosed || target.compressed) continue
|
|
164
179
|
if (typeof target.text !== 'string') continue
|
|
165
180
|
const byteLen = Buffer.byteLength(target.text, 'utf8')
|
|
166
181
|
if (byteLen <= 64 * 1024) continue
|
|
167
182
|
|
|
168
|
-
const result = trimLinesByRelevance(target.text, userText)
|
|
183
|
+
const result = trimLinesByRelevance(target.text, userText, { path: paths[i] })
|
|
169
184
|
if (!result) continue
|
|
170
185
|
if (result.text.length >= target.text.length) continue
|
|
171
186
|
target.compressed = result.text
|
|
@@ -181,16 +196,19 @@ function bm25TrimTargets(targets, body, provider) {
|
|
|
181
196
|
|
|
182
197
|
// --- Stage: prune ---
|
|
183
198
|
// Unambiguously npm-internal keys — safe to drop regardless of value.
|
|
184
|
-
const PRUNE_KEYS = new Set(['
|
|
185
|
-
// `integrity` / `shasum` are common field names; only drop them when the
|
|
186
|
-
// is actually
|
|
187
|
-
// in a non-npm tool_result is
|
|
199
|
+
const PRUNE_KEYS = new Set(['_from', '_resolved', '_integrity', '_nodeVersion', '_npmVersion', '_phantomChildren', '_requiredBy'])
|
|
200
|
+
// `_id` / `integrity` / `shasum` are common field names; only drop them when the
|
|
201
|
+
// value is actually npm metadata, so a legitimately-named field (e.g.
|
|
202
|
+
// integrity:"high", or a MongoDB `_id` document key) in a non-npm tool_result is
|
|
203
|
+
// preserved. npm's `_id` is always `name@version` (optionally @scope/-prefixed).
|
|
188
204
|
const NPM_INTEGRITY = /^sha\d{1,3}-[A-Za-z0-9+/]+={0,2}$/
|
|
189
205
|
const NPM_SHASUM = /^[a-f0-9]{40}$/i
|
|
206
|
+
const NPM_PKG_ID = /^(@[\w.-]+\/)?[\w.-]+@\d[\w.\-+]*$/
|
|
190
207
|
|
|
191
208
|
function shouldPrune(key, val) {
|
|
192
209
|
if (PRUNE_KEYS.has(key)) return true
|
|
193
210
|
if (typeof val !== 'string') return false
|
|
211
|
+
if (key === '_id' && NPM_PKG_ID.test(val)) return true
|
|
194
212
|
if (key === 'resolved' && val.startsWith('https://registry.')) return true
|
|
195
213
|
if (key === 'integrity' && NPM_INTEGRITY.test(val)) return true
|
|
196
214
|
if (key === 'shasum' && NPM_SHASUM.test(val)) return true
|
|
@@ -234,12 +252,46 @@ function stripLineComments(line) {
|
|
|
234
252
|
return line
|
|
235
253
|
}
|
|
236
254
|
|
|
255
|
+
// Remove line-level `/* ... */` block comments (opener at start of line after
|
|
256
|
+
// optional spaces/tabs) and `<!-- ... -->` HTML comments, in ONE linear pass.
|
|
257
|
+
// A regex can't do this without O(n^2): `/^[ \t]*\/\*[\s\S]*?\*\//gm` rescans to
|
|
258
|
+
// EOF at every unclosed opener, so a file full of `/*` line-starts stalled the
|
|
259
|
+
// event loop (256 KB took ~40s). An unterminated comment is left unchanged, to
|
|
260
|
+
// match the old lazy regex (which required a closing delimiter to match).
|
|
261
|
+
function stripBlockAndHtmlComments(text) {
|
|
262
|
+
let out = ''
|
|
263
|
+
let i = 0
|
|
264
|
+
const n = text.length
|
|
265
|
+
let lineStart = true
|
|
266
|
+
while (i < n) {
|
|
267
|
+
if (lineStart) {
|
|
268
|
+
let j = i
|
|
269
|
+
while (j < n && (text[j] === ' ' || text[j] === '\t')) j++
|
|
270
|
+
if (text[j] === '/' && text[j + 1] === '*') {
|
|
271
|
+
const close = text.indexOf('*/', j + 2)
|
|
272
|
+
if (close === -1) { out += text.slice(i); break }
|
|
273
|
+
i = close + 2
|
|
274
|
+
lineStart = false
|
|
275
|
+
continue
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (text[i] === '<' && text.startsWith('<!--', i)) {
|
|
279
|
+
const close = text.indexOf('-->', i + 4)
|
|
280
|
+
if (close === -1) { out += text.slice(i); break }
|
|
281
|
+
i = close + 3
|
|
282
|
+
lineStart = false
|
|
283
|
+
continue
|
|
284
|
+
}
|
|
285
|
+
const c = text[i]
|
|
286
|
+
out += c
|
|
287
|
+
lineStart = c === '\n'
|
|
288
|
+
i++
|
|
289
|
+
}
|
|
290
|
+
return out
|
|
291
|
+
}
|
|
292
|
+
|
|
237
293
|
function stripComments(text) {
|
|
238
|
-
|
|
239
|
-
let result = text
|
|
240
|
-
.replace(/^[ \t]*\/\*\*[\s\S]*?\*\//gm, '')
|
|
241
|
-
.replace(/^[ \t]*\/\*[\s\S]*?\*\//gm, '')
|
|
242
|
-
.replace(/<!--[\s\S]*?-->/g, '')
|
|
294
|
+
let result = stripBlockAndHtmlComments(text)
|
|
243
295
|
result = result.split('\n').map(stripLineComments).join('\n')
|
|
244
296
|
return result.replace(/\n\s*\n\s*\n/g, '\n\n')
|
|
245
297
|
}
|
|
@@ -374,6 +426,11 @@ export function compressText(text, config) {
|
|
|
374
426
|
|
|
375
427
|
let raw = cls === 'json-lined' ? stripLineNumbers(text) : text
|
|
376
428
|
|
|
429
|
+
// Integers beyond 2^53 lose precision through JSON.parse/stringify, silently
|
|
430
|
+
// corrupting numeric ids/amounts. We can't compress such JSON without changing
|
|
431
|
+
// its meaning, so forward it untouched.
|
|
432
|
+
if (jsonHasUnsafeInteger(raw)) return null
|
|
433
|
+
|
|
377
434
|
// Prune low-value fields before minifying
|
|
378
435
|
let parsedValue
|
|
379
436
|
if (config.stages.includes('prune')) {
|
|
@@ -598,6 +655,20 @@ export async function compressRequest(body, config, provider) {
|
|
|
598
655
|
|
|
599
656
|
const targets = provider.extract(body, { ...config, cacheSafe: config.cacheSafe !== false })
|
|
600
657
|
|
|
658
|
+
// Redaction runs FIRST (security, not compression): mask secrets in every
|
|
659
|
+
// target's text before any stage — dedup/diff/cache/llmlingua/textpress —
|
|
660
|
+
// sees it, so secrets never enter a disk cache or get shipped to a third
|
|
661
|
+
// party. We mutate target.text in place so the whole pipeline operates on
|
|
662
|
+
// the redacted body; the non-compressible case is closed in the loop below.
|
|
663
|
+
let redactedCount = 0
|
|
664
|
+
if (config.redact !== false) {
|
|
665
|
+
for (const target of targets) {
|
|
666
|
+
if (target.skip || typeof target.text !== 'string') continue
|
|
667
|
+
const r = redactText(target.text, config.redactMode)
|
|
668
|
+
if (r.count > 0) { target.text = r.text; target.redacted = true; redactedCount += r.count }
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
601
672
|
// Read-diff: session-scoped unified diff for re-reads (lossless, opt-in)
|
|
602
673
|
if (config.stages.includes('read-diff') && config.readCache && config.sessionKey && provider.name === 'anthropic') {
|
|
603
674
|
readDiffTargets(targets, body, config.sessionKey, config.readCache)
|
|
@@ -662,12 +733,17 @@ export async function compressRequest(body, config, provider) {
|
|
|
662
733
|
if (result) {
|
|
663
734
|
target.compressed = result.text
|
|
664
735
|
stats.push({ index: target.index, ...result })
|
|
736
|
+
} else if (target.redacted) {
|
|
737
|
+
// Not otherwise compressible, but it was redacted — apply() only writes
|
|
738
|
+
// back when .compressed is set, so push the redacted text through.
|
|
739
|
+
target.compressed = target.text
|
|
740
|
+
stats.push({ index: target.index, method: 'redact', originalLen: target.text.length, compressedLen: target.text.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.text) })
|
|
665
741
|
} else {
|
|
666
742
|
stats.push({ index: target.index, skipped: 'not-compressible' })
|
|
667
743
|
}
|
|
668
744
|
}
|
|
669
745
|
provider.apply(body, targets)
|
|
670
|
-
return { body, stats, targetCount: targets.length, outputHint, disclosure: disclosureStats }
|
|
746
|
+
return { body, stats, targetCount: targets.length, outputHint, disclosure: disclosureStats, redacted: redactedCount }
|
|
671
747
|
}
|
|
672
748
|
|
|
673
749
|
export async function compressMessages(body, config) {
|
package/config.js
CHANGED
|
@@ -37,14 +37,17 @@ function coerceLevelInput(raw) {
|
|
|
37
37
|
const v = resolveLevel(n) ? n : null
|
|
38
38
|
return { value: v, valid: v !== null }
|
|
39
39
|
}
|
|
40
|
-
|
|
40
|
+
// Object.hasOwn, NOT `in`: `in` walks the prototype chain, so
|
|
41
|
+
// TAMP_LEVEL=constructor / toString / valueOf pass as valid and later crash
|
|
42
|
+
// stagesForLevel with an inherited function instead of falling back cleanly.
|
|
43
|
+
if (Object.hasOwn(LEVEL_ALIASES, s)) return { value: s, valid: true }
|
|
41
44
|
return { value: null, valid: false }
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
// Convert a level input (number or alias) to its canonical integer.
|
|
45
48
|
function levelInputToInt(input) {
|
|
46
49
|
if (typeof input === 'number') return input
|
|
47
|
-
if (typeof input === 'string' && input
|
|
50
|
+
if (typeof input === 'string' && Object.hasOwn(LEVEL_ALIASES, input)) return LEVEL_ALIASES[input]
|
|
48
51
|
return null
|
|
49
52
|
}
|
|
50
53
|
|
|
@@ -152,7 +155,10 @@ export function loadConfig(env = process.env, options = {}) {
|
|
|
152
155
|
stages = stagesForLevel(level)
|
|
153
156
|
levelSource = chosen.source
|
|
154
157
|
} else if (presetExplicit) {
|
|
155
|
-
|
|
158
|
+
// Object.hasOwn guard: a bare `COMPRESSION_PRESETS[presetNameRaw]` lets
|
|
159
|
+
// __proto__/constructor/toString return truthy prototype objects that
|
|
160
|
+
// pass the `if (preset)` check and then crash on `...preset.stages`.
|
|
161
|
+
const preset = Object.hasOwn(COMPRESSION_PRESETS, presetNameRaw) ? COMPRESSION_PRESETS[presetNameRaw] : null
|
|
156
162
|
if (preset) {
|
|
157
163
|
stages = [...preset.stages]
|
|
158
164
|
level = typeof preset.level === 'number' ? preset.level : null
|
|
@@ -200,6 +206,11 @@ export function loadConfig(env = process.env, options = {}) {
|
|
|
200
206
|
return Object.freeze({
|
|
201
207
|
version: VERSION,
|
|
202
208
|
port: parseInt(get('TAMP_PORT'), 10) || 7778,
|
|
209
|
+
// Bind loopback by default. The proxy forwards the caller's API key to the
|
|
210
|
+
// upstream, so binding all interfaces (the old no-host default) turned any
|
|
211
|
+
// LAN peer into an authenticated relay. Set TAMP_HOST=0.0.0.0 to opt into
|
|
212
|
+
// broader binding (e.g. inside a container).
|
|
213
|
+
host: get('TAMP_HOST') || '127.0.0.1',
|
|
203
214
|
upstream: get('TAMP_UPSTREAM') || 'https://api.anthropic.com',
|
|
204
215
|
upstreams: Object.freeze({
|
|
205
216
|
anthropic: get('TAMP_UPSTREAM') || 'https://api.anthropic.com',
|
|
@@ -225,12 +236,17 @@ export function loadConfig(env = process.env, options = {}) {
|
|
|
225
236
|
levelSource,
|
|
226
237
|
outputMode,
|
|
227
238
|
outputModeDefault: defaultOutputMode && VALID_OUTPUT_MODES.has(defaultOutputMode) ? defaultOutputMode : null,
|
|
228
|
-
agent: get('TAMP_AGENT') || null,
|
|
239
|
+
agent: get('TAMP_AGENT')?.toLowerCase() || null,
|
|
229
240
|
autoDetectTaskType: parseBoolean(get('TAMP_AUTO_DETECT_TASK_TYPE'), true),
|
|
230
241
|
log: get('TAMP_LOG') !== 'false',
|
|
231
242
|
logFile: get('TAMP_LOG_FILE') || null,
|
|
232
243
|
maxBody: parseInt(get('TAMP_MAX_BODY'), 10) || 10_485_760,
|
|
233
244
|
cacheSafe: parseBoolean(get('TAMP_CACHE_SAFE'), true),
|
|
245
|
+
// Secret redaction is a security pass, not a compression stage: on by
|
|
246
|
+
// default at every level, masks secrets before any stage (incl. ones that
|
|
247
|
+
// ship text to third parties) sees the body. TAMP_REDACT=false disables it.
|
|
248
|
+
redact: parseBoolean(get('TAMP_REDACT'), true),
|
|
249
|
+
redactMode: get('TAMP_REDACT_MODE') === 'remove' ? 'remove' : 'mask',
|
|
234
250
|
llmLinguaUrl: get('TAMP_LLMLINGUA_URL') || null,
|
|
235
251
|
textpressOllamaUrl: get('TAMP_TEXTPRESS_OLLAMA_URL') || 'http://localhost:11434',
|
|
236
252
|
textpressOllamaModel: get('TAMP_TEXTPRESS_OLLAMA_MODEL') || 'qwen3.5:0.8b',
|
|
@@ -248,6 +264,7 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
|
|
|
248
264
|
# https://github.com/sliday/tamp
|
|
249
265
|
|
|
250
266
|
# TAMP_PORT=7778
|
|
267
|
+
# TAMP_HOST=127.0.0.1 # bind address; set 0.0.0.0 to expose beyond loopback
|
|
251
268
|
# TAMP_UPSTREAM=https://api.anthropic.com
|
|
252
269
|
# TAMP_UPSTREAM_OPENAI=https://api.openai.com
|
|
253
270
|
# TAMP_UPSTREAM_GEMINI=https://generativelanguage.googleapis.com
|
|
@@ -286,6 +303,8 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
|
|
|
286
303
|
# TAMP_LOG_FILE=
|
|
287
304
|
# TAMP_MAX_BODY=10485760
|
|
288
305
|
# TAMP_CACHE_SAFE=true
|
|
306
|
+
# TAMP_REDACT=true
|
|
307
|
+
# TAMP_REDACT_MODE=mask
|
|
289
308
|
# TAMP_TOKEN_COST=3
|
|
290
309
|
# TAMP_FOUNDATION_MODELS_PATH=apfel
|
|
291
310
|
# TAMP_FOUNDATION_MODELS_TIMEOUT=10000
|
package/detect.js
CHANGED
|
@@ -27,6 +27,48 @@ export function stripLineNumbers(str) {
|
|
|
27
27
|
return lines.map(l => l.replace(LINE_NUM_RE, '')).join('\n')
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
// True if the JSON text contains an integer literal outside the JS safe-integer
|
|
31
|
+
// range (|n| > 2^53-1). Such values lose precision through JSON.parse, so the
|
|
32
|
+
// minify/toon stages would silently alter a numeric id/amount — a semantic
|
|
33
|
+
// corruption. Scans number tokens outside string literals; floats are left
|
|
34
|
+
// alone (they are inherently approximate). Conservative on the safe side: when
|
|
35
|
+
// in doubt it returns true so the caller skips compression.
|
|
36
|
+
export function jsonHasUnsafeInteger(str) {
|
|
37
|
+
if (typeof str !== 'string') return false
|
|
38
|
+
let inString = false
|
|
39
|
+
let escaped = false
|
|
40
|
+
for (let i = 0; i < str.length; i++) {
|
|
41
|
+
const ch = str[i]
|
|
42
|
+
if (inString) {
|
|
43
|
+
if (escaped) escaped = false
|
|
44
|
+
else if (ch === '\\') escaped = true
|
|
45
|
+
else if (ch === '"') inString = false
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
if (ch === '"') { inString = true; continue }
|
|
49
|
+
if (ch !== '-' && (ch < '0' || ch > '9')) continue
|
|
50
|
+
// Start of a number token. Consume the integer-part digit run.
|
|
51
|
+
let j = ch === '-' ? i + 1 : i
|
|
52
|
+
const digitStart = j
|
|
53
|
+
while (j < str.length && str[j] >= '0' && str[j] <= '9') j++
|
|
54
|
+
const intDigits = j - digitStart
|
|
55
|
+
// Consume any fraction + exponent so their digits aren't re-scanned as a
|
|
56
|
+
// separate integer. Floats are left alone (inherently approximate).
|
|
57
|
+
let isFloat = false
|
|
58
|
+
if (str[j] === '.') { isFloat = true; j++; while (j < str.length && str[j] >= '0' && str[j] <= '9') j++ }
|
|
59
|
+
if (str[j] === 'e' || str[j] === 'E') {
|
|
60
|
+
isFloat = true; j++
|
|
61
|
+
if (str[j] === '+' || str[j] === '-') j++
|
|
62
|
+
while (j < str.length && str[j] >= '0' && str[j] <= '9') j++
|
|
63
|
+
}
|
|
64
|
+
if (!isFloat && intDigits >= 16 && !Number.isSafeInteger(Number(str.slice(i, j)))) {
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
67
|
+
i = j - 1
|
|
68
|
+
}
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
71
|
+
|
|
30
72
|
export function tryParseJSON(str) {
|
|
31
73
|
if (typeof str !== 'string' || str.length === 0) return { ok: false }
|
|
32
74
|
try {
|
|
@@ -40,7 +82,12 @@ export function tryParseJSON(str) {
|
|
|
40
82
|
export function isTOON(str) {
|
|
41
83
|
if (typeof str !== 'string') return false
|
|
42
84
|
const firstLine = str.trimStart().split('\n')[0]
|
|
43
|
-
|
|
85
|
+
// Anchored + bounded. The former unanchored `\w+\[\d+\]\{` was quadratic: on a
|
|
86
|
+
// long word-char run with no bracket the greedy `\w+` backtracks and, being
|
|
87
|
+
// unanchored, retries at every offset — a single-line blob (minified JSON, a
|
|
88
|
+
// long token) stalled the event loop. TOON headers begin the line, so `^`
|
|
89
|
+
// alone kills it; the {1,64}/{1,15} bounds are belt-and-suspenders.
|
|
90
|
+
return /^\[TOON\]/.test(firstLine) || /^\s*\w{1,64}\[\d{1,15}\]\{/.test(firstLine) || /^\s*\w{1,64}\[\d{1,15}\]:/.test(firstLine)
|
|
44
91
|
}
|
|
45
92
|
|
|
46
93
|
export function classifyContent(str) {
|
package/index.js
CHANGED
|
@@ -175,6 +175,13 @@ function pipeRequest(req, res, upstreamUrl, prefixChunks) {
|
|
|
175
175
|
const headers = { ...req.headers }
|
|
176
176
|
delete headers.host
|
|
177
177
|
const upstream = openUpstream(req.method, upstreamUrl, headers, res)
|
|
178
|
+
// Guard the inbound stream: a client RST mid-upload emits 'error' on `req`,
|
|
179
|
+
// and with no listener Node rethrows it and crashes the whole proxy. The
|
|
180
|
+
// buffered path already guards this; the piped passthrough must too.
|
|
181
|
+
req.on('error', (err) => {
|
|
182
|
+
log(`[tamp] client upload error: ${err.code || ''} ${err.message}`)
|
|
183
|
+
upstream.destroy()
|
|
184
|
+
})
|
|
178
185
|
if (prefixChunks) {
|
|
179
186
|
for (const chunk of prefixChunks) upstream.write(chunk)
|
|
180
187
|
}
|
|
@@ -415,8 +422,8 @@ const isMain = !process.argv[1]?.includes('node_modules') && process.argv[1] ===
|
|
|
415
422
|
|
|
416
423
|
if (isMain) {
|
|
417
424
|
const { config, server } = createProxy()
|
|
418
|
-
server.listen(config.port, () => {
|
|
419
|
-
console.error(`[tamp] proxy listening on http
|
|
425
|
+
server.listen(config.port, config.host, () => {
|
|
426
|
+
console.error(`[tamp] proxy listening on http://${config.host}:${config.port}`)
|
|
420
427
|
console.error(`[tamp] upstream: ${config.upstream}`)
|
|
421
428
|
console.error(`[tamp] stages: ${config.stages.join(', ')}`)
|
|
422
429
|
|