Add Brotli-first compression for local API sidecar and Nginx proxy#168
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
facusturla
pushed a commit
to facusturla/worldmonitor
that referenced
this pull request
Feb 27, 2026
…oala73#168) ### Motivation - Reduce sidecar API egress by preferring Brotli (br) for clients that support it while keeping gzip as a fallback for broad compatibility. - Ensure the sidecar negotiates compression consistently (set `Content-Encoding`, append `Vary: Accept-Encoding`) and avoids sending stale `Content-Length` when responses are compressed. - Provide an Nginx proxy baseline that preserves client `Accept-Encoding` and prefers Brotli at the proxy layer. ### Description - Updated `src-tauri/sidecar/local-api-server.mjs` to negotiate response compression via a new `maybeCompressResponseBody` routine that prefers Brotli (`br`) and falls back to gzip for payloads > 1KB, preserves existing `Content-Encoding` from handlers, and appends `Vary: Accept-Encoding` when compressing. - Added a small helper `canCompress` and promisified `brotliCompress` so Brotli compression runs asynchronously while gzip remains synchronous for fallback. - Ensure `Content-Length` is removed when `Content-Encoding` is applied so downstream callers/proxies don't see stale lengths. - Added tests to `src-tauri/sidecar/local-api-server.test.mjs` that verify Brotli is preferred when `Accept-Encoding` includes `br` and gzip is used when `br` is not present, including decompression checks with `brotliDecompressSync`/`gunzipSync`. - Added `deploy/nginx/brotli-api-proxy.conf` as an example Nginx configuration enabling Brotli with gzip fallback and forwarding `Accept-Encoding` while disabling upstream decompression (`gunzip off`). - Updated `README.md` to reflect the sidecar now advertising `br/gzip` behavior and adjusted the expected payload reduction notes. ### Testing - Ran the sidecar unit tests with `node --test src-tauri/sidecar/local-api-server.test.mjs`, which executed 19 tests and all passed (`# pass 19`, `# fail 0`). - No other automated test suites were modified; the new tests are self-contained under the sidecar test file and validated the compression behavior. ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_6997871302188333bb1ecb20714d2ad3)
koala73
added a commit
that referenced
this pull request
Apr 11, 2026
…riter Resolves all 5 critical findings from the multi-agent code review of PR #2940. End-to-end smoke test passes for all 8 regions with confidence=1.0. P1 #166: health-seed-meta-not-in-keys-loops - Add regionalSnapshots to STANDALONE_KEYS in api/health.js - Without this, the SEED_META entry was dead wiring and the 12h staleness budget was unobservable. Same failure mode as the 'empty data ok keys bootstrap blind spot' the team has hit before. P1 #167: oref-trigger-key-not-in-freshness-registry - Add relay:oref:history:v1 to FRESHNESS_REGISTRY (the canonical OREF key written by ais-relay, not the wrong intelligence:oref-alerts:v1 that the code was originally reading). - Update trigger-evaluator to read activeAlertCount/historyCount24h from the actual relay:oref:history:v1 payload shape. - oref_cluster trigger now fires when activeAlertCount > 10 (verified end-to-end against mock data). P1 #168: zombie-freshness-registry-keys - Remove 4 freshness registry entries that no compute module reads: supply_chain:shipping_stress, energy:chokepoint-flows, intelligence:advisories-bootstrap, market:commodities-bootstrap. - These were dragging snapshot_confidence below 1.0 and wasting Redis pipeline reads. Add a header comment forbidding speculative entries. P1 #169: diff-field-leaks-into-persisted-snapshot - Change computeSnapshot to return { snapshot, diff } separately so the diff is consumed locally for inferTriggerReason and never serialized into the persisted snapshot. The diff field was not part of the RegionalSnapshot type and would have broken Phase 1 proto codegen. - Update main() to destructure { snapshot } before persistSnapshot. P1 #170: jsdoc-types-not-enforced-jsconfig-missing - Add scripts/seed-regional-snapshots.mjs and scripts/regional-snapshot/** to scripts/jsconfig.json's include array so tsc --checkJs validates the JSDoc @type annotations. - Add // @ts-check directive to all 14 .mjs files in the regional-snapshot pipeline. - Fix 4 type-safety bugs surfaced by enabling type-checking: actor-scoring: explicit ActorState[] type on local actors array; cast leverage_domains to ActorLeverageDomain[]; typed ActorRole return on inferRole. evidence-collector: explicit EvidenceItem[] type on local out array. scenario-builder: typed HORIZONS as ScenarioHorizon[] and LANE_NAMES as ScenarioName[]. transmission-templates: cast tpl.affectedRegions to RegionId[] when building TransmissionPath objects. - Pre-existing seed-forecasts.mjs and _r2-storage.mjs errors (46 total) are not part of this PR's scope and remain untouched. Verification - npx tsc --noEmit -p scripts/jsconfig.json: 0 errors in regional-snapshot files - npm run typecheck:all: clean - npm run test:data: 3946/3946 pass - tests/regional-snapshot.test.mjs: 50/50 pass - End-to-end smoke test: all 8 regions compute clean with confidence=1.0 - Verified: oref_cluster trigger fires when activeAlertCount > 10 - Verified: persisted snapshot has no diff field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Content-Encoding, appendVary: Accept-Encoding) and avoids sending staleContent-Lengthwhen responses are compressed.Accept-Encodingand prefers Brotli at the proxy layer.Description
src-tauri/sidecar/local-api-server.mjsto negotiate response compression via a newmaybeCompressResponseBodyroutine that prefers Brotli (br) and falls back to gzip for payloads > 1KB, preserves existingContent-Encodingfrom handlers, and appendsVary: Accept-Encodingwhen compressing.canCompressand promisifiedbrotliCompressso Brotli compression runs asynchronously while gzip remains synchronous for fallback.Content-Lengthis removed whenContent-Encodingis applied so downstream callers/proxies don't see stale lengths.src-tauri/sidecar/local-api-server.test.mjsthat verify Brotli is preferred whenAccept-Encodingincludesbrand gzip is used whenbris not present, including decompression checks withbrotliDecompressSync/gunzipSync.deploy/nginx/brotli-api-proxy.confas an example Nginx configuration enabling Brotli with gzip fallback and forwardingAccept-Encodingwhile disabling upstream decompression (gunzip off).README.mdto reflect the sidecar now advertisingbr/gzipbehavior and adjusted the expected payload reduction notes.Testing
node --test src-tauri/sidecar/local-api-server.test.mjs, which executed 19 tests and all passed (# pass 19,# fail 0).Codex Task