From 45cad4ba2df61c2ea3cd22a0c630c78a92c894b1 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Tue, 22 Oct 2024 17:51:55 -0500 Subject: [PATCH 1/2] add preferred option --- index.js | 11 ++++++++--- test/encoding.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4f2840c..d0e575e 100644 --- a/index.js +++ b/index.js @@ -119,17 +119,22 @@ Accepts.prototype.types = function (types_) { * ['gzip', 'deflate'] * * @param {String|Array} encodings... + * @param {Object} options_ * @return {String|Array} * @public */ Accepts.prototype.encoding = -Accepts.prototype.encodings = function (encodings_) { +Accepts.prototype.encodings = function (encodings_, options_) { var encodings = encodings_ + var preferred = arguments[arguments.length - 1]?.preferred || null // support flattened arguments if (encodings && !Array.isArray(encodings)) { - encodings = new Array(arguments.length) + var lenght = preferred ? arguments.length - 1 : arguments.length + + encodings = new Array(lenght) + for (var i = 0; i < encodings.length; i++) { encodings[i] = arguments[i] } @@ -140,7 +145,7 @@ Accepts.prototype.encodings = function (encodings_) { return this.negotiator.encodings() } - return this.negotiator.encodings(encodings)[0] || false + return this.negotiator.encodings(encodings, { preferred: preferred })[0] || false } /** diff --git a/test/encoding.js b/test/encoding.js index 0e8585a..58a45fa 100644 --- a/test/encoding.js +++ b/test/encoding.js @@ -56,6 +56,13 @@ describe('accepts.encodings()', function () { assert.strictEqual(accept.encodings('compress', 'gzip'), 'gzip') assert.strictEqual(accept.encodings('gzip', 'compress'), 'gzip') }) + + it('should accept a preferred encoding', function () { + var req = createRequest('gzip, br, compress') + var accept = accepts(req) + assert.strictEqual(accept.encodings('gzip', 'br', 'identity', { preferred: ['br'] }), 'br') + assert.strictEqual(accept.encodings('gzip', 'identity', 'br', { preferred: ['br'] }), 'br') + }) }) describe('with an array', function () { @@ -65,6 +72,14 @@ describe('accepts.encodings()', function () { assert.strictEqual(accept.encodings(['compress', 'gzip']), 'gzip') }) }) + + describe('with preferred encoding', function () { + it('should return the preferred encoding', function () { + var req = createRequest('gzip, br') + var accept = accepts(req) + assert.strictEqual(accept.encodings(['br', 'gzip', 'identity'], { preferred: ['br'] }), 'br') + }) + }) }) function createRequest (encoding) { From ca63979f627b0f5ab5c171468ca7b9c265dbc16d Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Tue, 22 Oct 2024 20:09:55 -0500 Subject: [PATCH 2/2] support old node.js versions --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d0e575e..abdfeb1 100644 --- a/index.js +++ b/index.js @@ -127,7 +127,7 @@ Accepts.prototype.types = function (types_) { Accepts.prototype.encoding = Accepts.prototype.encodings = function (encodings_, options_) { var encodings = encodings_ - var preferred = arguments[arguments.length - 1]?.preferred || null + var preferred = arguments[arguments.length - 1] == null ? null : arguments[arguments.length - 1].preferred // support flattened arguments if (encodings && !Array.isArray(encodings)) {