httpCompression
Official sources are open to Staxis GitLab group members: how to join and view the code.
Repository:
httpCompression development
Versions:
tags
and releases
Description
httpCompression provides reusable response-optimization helpers: native zlib, HTML, JavaScript, and CSS minifiers, a buffered gzip middleware factory, and cache options for consuming HTTP packs. Its label and typology are both httpCompression. It publishes no routes of its own; packs such as renderers and static-file handlers consume its assets.
Read before flight
-
We suggest comparing transformed output with the original, including script behaviour, string values, and whitespace-sensitive content. The internal minifiers are lightweight text transformations; use them only for content you have validated.
-
We suggest reserving the custom buffered wrapper for finite responses and budgeting memory for concurrent requests. maxBytes controls eligibility after buffering; it is not a memory ceiling. Keep streaming and long-lived responses on a suitable streaming path.
-
We suggest choosing one compression owner for each response and inspecting both the headers and decoded body. For caching, match expiry and validation policy to how your resources are updated, then check the headers emitted by the consuming pack.
Requirements and Installation
The pack requires serverApp. Native helper use needs no extra npm dependency: zlib is built into Node.js. Install through the Manager:
manager -> install -> httpCompression
Place it after serverApp and before its consumers. Prepare the project keys and integrity records. The supplied provider manifest permits ejs, htmlexpress, react, and httpStatics. For your own pack, add its manifest.label to httpCompression's permits while preserving the other authorized consumers; its manifest can declare "require": ["httpCompression"].
Optional framework integration uses koa-compress for Koa or @fastify/compress for Fastify. The supplied Guardian configuration enables the global allowBareDeps option and declares those dependency names for httpCompression. Missing optional compression packages are logged and skipped.
Files Map
| File | Purpose |
|---|---|
| manifest.json | Declares the identity, serverApp requirement, and permitted consumers. |
| configs.js | Provides serveropt.compressor, serveropt.cache, and dependency rules. |
| main.js | Builds the helper bundle and runtime-specific compression integration. |
| minify.js | Implements the internal HTML, JavaScript, and CSS transformations. |
| README.md | Contains the pack reference. |
Configuration
In the project's config.js, these defaults belong to config.serveropt.compressor and config.serveropt.cache. Merge the following serveropt branch into the exported config object:
// config.js -> config (merge into the exported object)
serveropt: {
compressor: {
compression: true,
zlib: true,
minifyHTML: true,
minifyJS: true,
minifyCSS: true,
nocomments: true,
nosta: true,
noQuotes: true,
noRedunands: true,
minBytes: 1024,
maxBytes: 8 * 1024 * 1024
},
cache: {
active: false,
maxAge: '7d',
lastModified: true,
immutable: true,
etag: true
}
}
compression controls automatic framework registration. zlib and the three minify switches control which helpers are published. Disabling automatic compression does not remove those helpers. minBytes and maxBytes are byte thresholds for the custom wrapper; false or zero currently selects its fallback of 1024 bytes and 8 MiB respectively. gzipTimeoutMs is an additional wrapper setting with a 1000-millisecond fallback.
nocomments maps to comment removal, nosta to script type/language cleanup, noQuotes to attribute-quote removal, and noRedunands to redundant style/link type cleanup. The spelling noRedunands is the current configuration key. These settings are passed to the minifiers through the published settings object. Their behaviour depends on the transformation selected; validate the resulting content.
Request Access
This example runs inside someOtherLayer, the requesting pack's manifest.label. Replace that name with your own permitted label. Vault takes the requester's label and the provider's label; layers.require takes the provider's typology, its runtime slot. The official provider uses httpCompression for both label and typology.
const token = engine.vault.requireAccess(
'someOtherLayer', // Requester's manifest.label
'httpCompression' // Provider's manifest.label
)
if (typeof token !== 'string' || token.startsWith('error:')) {
throw new Error('httpCompression access denied')
}
const bundle = layers.require(
'httpCompression', // Provider's manifest.typology: runtime slot
token,
false
)
const compression = bundle?.assets
if (!compression || typeof compression !== 'object') {
throw new Error('httpCompression is unavailable')
}
Use the Published Helpers
| Asset | Use |
|---|---|
| zlib | Native Node.js zlib module, or false when disabled |
| minifyHTML(html, settings, functions) | HTML transformation; the optional functions object supplies inline JS/CSS processors |
| minifyJs(source, settings) | JavaScript transformation; note the lowercase s in the API name |
| minifyCSS(source, settings) | CSS transformation |
| settings | Resolved minifier options |
| compressor() | Creates a Node-style response middleware function |
| cache | Options intended for a compatible static-serving consumer |
Continuing with the acquired compression object, the README's direct-helper approach becomes:
const source = '<html> <body>hello</body> </html>'
const output = typeof compression.minifyHTML === 'function'
? compression.minifyHTML(source, compression.settings)
: source
A disabled minifier is published as false, so check availability before calling it. Pass the settings object explicitly when you want the pack's resolved options. Direct zlib calls use Node.js's own API; the caller remains responsible for choosing an acceptable encoding and sending headers that describe the actual bytes.
Native and Framework Integration
In native mode, attach publishes the helpers without automatically registering compressor(). A consumer can mount that middleware explicitly when it owns the response-compression policy. If it already holds the serverApp instance, the registration has this form:
if (config.serveropt.compressor.compression) {
serverApp.using(
'response-compression', // Middleware diagnostic label
compression.compressor()
)
}
Register before the responses you want it to process, and avoid installing it again through another consumer. The custom wrapper targets gzip-capable GET requests, skips Range requests, already encoded responses, and common compressed-file extensions. At response end it checks status and buffered size; only eligible status-200 bodies proceed to transformation and gzip. These thresholds do not prevent the preceding buffering.
The Express attach branch attempts to register the custom wrapper. Koa and Fastify use their own optional compression plugins with gzip enabled and the configured minimum threshold; those paths do not automatically inherit every custom-wrapper minifier or size option. Validate the chosen integration together with the serverApp adapter.
Cache Options
cache is an options object, not a response store or an automatic cache middleware. A consuming pack must interpret and apply it. The current bundle exports these values:
| Exported option | cache.active=true | cache.active=false |
|---|---|---|
| etag | false | true |
| maxAge | Configured maxAge | 0 |
| lastModified | true | false |
| setHeaders | Adds an x-timestamp header | No additional header |
immutable is not included in the exported object, and etag and lastModified are currently selected by active rather than their individual configuration flags. The maxAge format is interpreted by the consumer; do not copy a value such as 7d directly into an HTTP Cache-Control header. We suggest checking the resulting cache headers and conditional requests in your actual serving pack.
Documentation
This page is the HTML adaptation of the pack's README.md, with practical clarifications checked against the implementation. Complete Staxis onboarding to request GitLab access to the official sources.