Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
test:
uses: hapijs/.github/.github/workflows/ci-module.yml@master
with:
min-node-version: 14
min-node-version: 22
7 changes: 7 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ Default value: '1024'.
Sets the minimum response payload size in bytes that is required for content encoding compression.
If the payload size is under the limit, no compression is performed.

##### <a name="server.options.compression.priority" /> `server.options.compression.priority`

Default value: `null`.

Sets the priority for content encoding compression algorithms in descending order,
e.g.: `['zstd', 'br', 'gzip', 'deflate']`.

#### <a name="server.options.debug" /> `server.options.debug`

Default value: `{ request: ['implementation'] }`.
Expand Down
28 changes: 23 additions & 5 deletions lib/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,37 @@ const Accept = require('@hapi/accept');
const Bounce = require('@hapi/bounce');
const Hoek = require('@hapi/hoek');


const internals = {
common: ['gzip, deflate', 'deflate, gzip', 'gzip', 'deflate', 'gzip, deflate, br']
common: [
'gzip, deflate',
'deflate, gzip',
'gzip',
'deflate',
'br',
'gzip, deflate, br',
'zstd',
'gzip, deflate, br, zstd'
]
};


exports = module.exports = internals.Compression = class {

decoders = {
br: (options) => Zlib.createBrotliDecompress(options),
gzip: (options) => Zlib.createGunzip(options),
deflate: (options) => Zlib.createInflate(options)
deflate: (options) => Zlib.createInflate(options),
zstd: (options) => Zlib.createZstdDecompress(options)
};

encodings = ['identity', 'gzip', 'deflate'];
encodings = ['identity', 'gzip', 'deflate', 'br', 'zstd'];

encoders = {
identity: null,
br: (options) => Zlib.createBrotliCompress(options),
gzip: (options) => Zlib.createGzip(options),
deflate: (options) => Zlib.createDeflate(options)
deflate: (options) => Zlib.createDeflate(options),
zstd: (options) => Zlib.createZstdCompress(options)
};

#common = null;
Expand Down Expand Up @@ -116,4 +128,10 @@ exports = module.exports = internals.Compression = class {
Hoek.assert(encoder !== undefined, `Unknown encoding ${encoding}`);
return encoder(request.route.settings.compression[encoding]);
}

setPriority(priority) {

this.encodings = [...new Set([...priority, ...this.encodings])];
this._updateCommons();
}
};
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ internals.server = Validate.object({
autoListen: Validate.boolean(),
cache: Validate.allow(null), // Validated elsewhere
compression: Validate.object({
minBytes: Validate.number().min(1).integer().default(1024)
minBytes: Validate.number().min(1).integer().default(1024),
priority: Validate.array().items(Validate.string().valid('gzip', 'deflate', 'br', 'zstd')).default(null)
})
.allow(false)
.default(),
Expand Down
4 changes: 4 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ exports = module.exports = internals.Core = class {
this._debug();
this._initializeCache();

if (this.settings.compression.priority) {
this.compression.setPriority(this.settings.compression.priority);
}

if (this.settings.routes.validate.validator) {
this.validator = Validation.validator(this.settings.routes.validate.validator);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/types/server/encoders.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDeflate, createGunzip, createGzip, createInflate } from 'zlib';
import { createBrotliCompress, createBrotliDecompress, createDeflate, createGunzip, createGzip, createInflate, createZstdCompress, createZstdDecompress } from 'zlib';

/**
* Available [content encoders](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder).
Expand All @@ -7,6 +7,8 @@ export interface ContentEncoders {

deflate: typeof createDeflate;
gzip: typeof createGzip;
br: typeof createBrotliCompress;
zstd: typeof createZstdCompress;
}

/**
Expand All @@ -16,4 +18,6 @@ export interface ContentDecoders {

deflate: typeof createInflate;
gzip: typeof createGunzip;
br: typeof createBrotliDecompress;
zstd: typeof createZstdDecompress;
}
1 change: 1 addition & 0 deletions lib/types/server/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SameSitePolicy } from './state';

export interface ServerOptionsCompression {
minBytes: number;
priority: string[];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"engines": {
"node": ">=14.15.0"
"node": ">=22.15.0"
},
"files": [
"lib"
Expand Down Expand Up @@ -51,11 +51,11 @@
"@hapi/lab": "^25.3.2",
"@hapi/vision": "^7.0.3",
"@hapi/wreck": "^18.1.0",
"@types/node": "^18.19.59",
"@types/node": "^24.1.0",
"handlebars": "^4.7.8",
"joi": "^17.13.3",
"legacy-readable-stream": "npm:readable-stream@^1.0.34",
"typescript": "^4.9.4"
"typescript": "^5.8.3"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L -m 5000 -Y",
Expand Down
48 changes: 48 additions & 0 deletions test/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,54 @@ describe('Payload', () => {
expect(res.result).to.equal(message);
});

it('handles br payload', async () => {

const message = { 'msg': 'This message is going to be brotlied.' };
const server = Hapi.server();
server.route({ method: 'POST', path: '/', handler: (request) => request.payload });

const compressed = await new Promise((resolve) => Zlib.brotliCompress(JSON.stringify(message), (ignore, result) => resolve(result)));

const request = {
method: 'POST',
url: '/',
headers: {
'content-type': 'application/json',
'content-encoding': 'br',
'content-length': compressed.length
},
payload: compressed
};

const res = await server.inject(request);
expect(res.result).to.exist();
expect(res.result).to.equal(message);
});

it('handles zstd payload', async () => {

const message = { 'msg': 'This message is going to be zstded.' };
const server = Hapi.server();
server.route({ method: 'POST', path: '/', handler: (request) => request.payload });

const compressed = await new Promise((resolve) => Zlib.zstdCompress(JSON.stringify(message), (ignore, result) => resolve(result)));

const request = {
method: 'POST',
url: '/',
headers: {
'content-type': 'application/json',
'content-encoding': 'zstd',
'content-length': compressed.length
},
payload: compressed
};

const res = await server.inject(request);
expect(res.result).to.exist();
expect(res.result).to.equal(message);
});

it('handles custom compression', async () => {

const message = { 'msg': 'This message is going to be gzipped.' };
Expand Down
Loading