From 7f32d2b0cebc1c30392c9c467c75fa03293eebbc Mon Sep 17 00:00:00 2001 From: james-owen Date: Thu, 3 Dec 2020 11:09:46 -0500 Subject: [PATCH 01/19] call paginate for list requests --- lib/services/db.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/services/db.js b/lib/services/db.js index 3161f43d..e0c5b7c7 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -80,7 +80,7 @@ function list(options = {}) { transformOptions.isArray = true; } - readStream = module.exports.createReadStream(options); + readStream = module.exports.paginate(options); if (_.isFunction(options.transforms)) { options.transforms = options.transforms(); @@ -136,6 +136,7 @@ module.exports.patchMeta; module.exports.getMeta; module.exports.raw; module.exports.createReadStream; +module.exports.paginate; // For testing module.exports.defer = defer; From 622b9d6a473abb8bfc7aabf9d306434489e7ca7d Mon Sep 17 00:00:00 2001 From: james-owen Date: Thu, 3 Dec 2020 11:10:17 -0500 Subject: [PATCH 02/19] handle prev and size in list --- lib/responses.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index edfcb598..3809a24c 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -421,7 +421,9 @@ function list(options) { let list, listOptions = _.assign({ prefix: req.uri, - values: false + values: false, + previous: req.query.prev, + size: req.query.size || 0 }, options); list = db.list(listOptions); @@ -441,12 +443,21 @@ function listWithoutVersions() { return [filter({wantStrings: true}, function (str) { return str.indexOf('@') === -1; })]; - } + }, }; return list(options); } +function paginateComponents(previous, size) { + // const options = { + // previous: req.query.prev, + // size: req.query.size || 0 + // } + + return list(); +} + /** * List all things in the db that start with this prefix * _and_ are published @@ -558,6 +569,7 @@ module.exports.checkArchivedToPublish = checkArchivedToPublish; // straight from DB module.exports.list = list; module.exports.listWithPublishedVersions = listWithPublishedVersions; +module.exports.paginateComponents = paginateComponents; module.exports.listWithoutVersions = listWithoutVersions; module.exports.getRouteFromDB = getRouteFromDB; module.exports.putRouteFromDB = putRouteFromDB; From 4c09d6c81d8317ae0a3799db01310c132e8f1e7a Mon Sep 17 00:00:00 2001 From: james-owen Date: Thu, 3 Dec 2020 11:14:02 -0500 Subject: [PATCH 03/19] remove draft function --- lib/responses.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 3809a24c..a21dc565 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -449,15 +449,6 @@ function listWithoutVersions() { return list(options); } -function paginateComponents(previous, size) { - // const options = { - // previous: req.query.prev, - // size: req.query.size || 0 - // } - - return list(); -} - /** * List all things in the db that start with this prefix * _and_ are published @@ -569,7 +560,6 @@ module.exports.checkArchivedToPublish = checkArchivedToPublish; // straight from DB module.exports.list = list; module.exports.listWithPublishedVersions = listWithPublishedVersions; -module.exports.paginateComponents = paginateComponents; module.exports.listWithoutVersions = listWithoutVersions; module.exports.getRouteFromDB = getRouteFromDB; module.exports.putRouteFromDB = putRouteFromDB; From bf7f4ff197323255ab69105ed21fa005c45a9f60 Mon Sep 17 00:00:00 2001 From: james-owen Date: Fri, 4 Dec 2020 10:34:32 -0500 Subject: [PATCH 04/19] add paginate mock --- test/fixtures/mocks/storage.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fixtures/mocks/storage.js b/test/fixtures/mocks/storage.js index 11da54ff..214ec5fa 100644 --- a/test/fixtures/mocks/storage.js +++ b/test/fixtures/mocks/storage.js @@ -20,6 +20,7 @@ class Storage { this.clearMem = this.clear; this.pipeToPromise = db.pipeToPromise; this.createReadStream = (ops) => this.inMem.createReadStream(ops); + this.paginate = (ops) => this.inMem.createReadStream(ops); this.getLatestData = sinon.stub(); db.registerStorage(this); From 9ad6f7dcb69532718fe47b17a9cec8f4cb864ae0 Mon Sep 17 00:00:00 2001 From: james-owen Date: Fri, 4 Dec 2020 10:47:02 -0500 Subject: [PATCH 05/19] linting --- lib/responses.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index a21dc565..8b435115 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -422,7 +422,7 @@ function list(options) { listOptions = _.assign({ prefix: req.uri, values: false, - previous: req.query.prev, + previous: req.query.prev, size: req.query.size || 0 }, options); @@ -443,7 +443,7 @@ function listWithoutVersions() { return [filter({wantStrings: true}, function (str) { return str.indexOf('@') === -1; })]; - }, + }, }; return list(options); From e41eb1d4d176e6186a286f0dae2ca1f55635e505 Mon Sep 17 00:00:00 2001 From: james-owen Date: Mon, 7 Dec 2020 17:10:17 -0500 Subject: [PATCH 06/19] check page request query params --- lib/responses.js | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 8b435115..9876c611 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -408,6 +408,41 @@ function expectJSON(fn, res) { }).catch(handleError(res)); } +/** + * Checks the page size requested to make sure it's a Number and within the maximum. + * Returns the default or 0 if there's no default or size is not a Number. + * @param {Object} req + * @returns {number} + */ +function checkPageSize(req) { + const size = req.query.size || process.env.CLAY_STORAGE_PAGE_SIZE || 0; + const num = Math.abs(Number(size)); + const max = process.env.CLAY_STORAGE_MAX_PAGE || null; + + if (isNaN(num)) return 0; + + if (max) { + return num > max ? max : num; + } else return num; +} + +/** + * Checks whether the previous value for a page request matches up with the uri, + * ie. all the ids returned should contain the request uri. + * @param {Object} req + * @returns {string|undefined} + */ +function checkPrevious(req) { + const prefix = req.uri || null; + const previous = req.query.prev || null; + + if (previous && previous.includes(prefix)) { + return previous; + } else { + return; + } +} + /** * List all things in the db * @param {object} [options] @@ -422,8 +457,8 @@ function list(options) { listOptions = _.assign({ prefix: req.uri, values: false, - previous: req.query.prev, - size: req.query.size || 0 + previous: checkPrevious(req), + size: checkPageSize(req) }, options); list = db.list(listOptions); From 4673924ea731d17d8fcb1dc66b7fa8bc4433bea2 Mon Sep 17 00:00:00 2001 From: james-owen Date: Mon, 7 Dec 2020 17:19:24 -0500 Subject: [PATCH 07/19] remove one-var --- .eslintrc | 1 - 1 file changed, 1 deletion(-) diff --git a/.eslintrc b/.eslintrc index fefcb720..9f4ff8a9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -51,7 +51,6 @@ "no-trailing-spaces": 2, "no-underscore-dangle": 0, "no-unneeded-ternary": 1, - "one-var": 2, "quotes": [2, "single", "avoid-escape"], "semi": [2, "always"], "keyword-spacing": 2, From 24e368a5f9c3c843adfa2615b169c3340db66a66 Mon Sep 17 00:00:00 2001 From: james-owen Date: Mon, 7 Dec 2020 17:21:01 -0500 Subject: [PATCH 08/19] linting --- lib/responses.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 9876c611..95564c97 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -427,7 +427,7 @@ function checkPageSize(req) { } /** - * Checks whether the previous value for a page request matches up with the uri, + * Checks whether the previous value for a page request matches up with the uri, * ie. all the ids returned should contain the request uri. * @param {Object} req * @returns {string|undefined} @@ -457,7 +457,7 @@ function list(options) { listOptions = _.assign({ prefix: req.uri, values: false, - previous: checkPrevious(req), + previous: checkPrevious(req), size: checkPageSize(req) }, options); From c69a42d7e37836a963bdce760d5f0c68dd34d342 Mon Sep 17 00:00:00 2001 From: james-owen Date: Tue, 8 Dec 2020 13:48:09 -0500 Subject: [PATCH 09/19] remove references to paginate --- lib/services/db.js | 3 +-- test/fixtures/mocks/storage.js | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/services/db.js b/lib/services/db.js index e0c5b7c7..3161f43d 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -80,7 +80,7 @@ function list(options = {}) { transformOptions.isArray = true; } - readStream = module.exports.paginate(options); + readStream = module.exports.createReadStream(options); if (_.isFunction(options.transforms)) { options.transforms = options.transforms(); @@ -136,7 +136,6 @@ module.exports.patchMeta; module.exports.getMeta; module.exports.raw; module.exports.createReadStream; -module.exports.paginate; // For testing module.exports.defer = defer; diff --git a/test/fixtures/mocks/storage.js b/test/fixtures/mocks/storage.js index 214ec5fa..11da54ff 100644 --- a/test/fixtures/mocks/storage.js +++ b/test/fixtures/mocks/storage.js @@ -20,7 +20,6 @@ class Storage { this.clearMem = this.clear; this.pipeToPromise = db.pipeToPromise; this.createReadStream = (ops) => this.inMem.createReadStream(ops); - this.paginate = (ops) => this.inMem.createReadStream(ops); this.getLatestData = sinon.stub(); db.registerStorage(this); From 2d6db61e3caec4badffb693b25cd82186a0898f3 Mon Sep 17 00:00:00 2001 From: james-owen Date: Tue, 8 Dec 2020 14:19:24 -0500 Subject: [PATCH 10/19] handle max page size string --- lib/responses.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 95564c97..5bc0433d 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -417,9 +417,9 @@ function expectJSON(fn, res) { function checkPageSize(req) { const size = req.query.size || process.env.CLAY_STORAGE_PAGE_SIZE || 0; const num = Math.abs(Number(size)); - const max = process.env.CLAY_STORAGE_MAX_PAGE || null; + const max = Math.abs(Number(process.env.CLAY_STORAGE_MAX_PAGE)) || null; - if (isNaN(num)) return 0; + if (isNaN(num) || isNaN(max)) return 0; if (max) { return num > max ? max : num; From 05c3d9173feb2464bd50da1d683f121958bf0af7 Mon Sep 17 00:00:00 2001 From: james-owen Date: Tue, 8 Dec 2020 14:28:46 -0500 Subject: [PATCH 11/19] update var names --- lib/responses.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 5bc0433d..312f3ebe 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -415,9 +415,9 @@ function expectJSON(fn, res) { * @returns {number} */ function checkPageSize(req) { - const size = req.query.size || process.env.CLAY_STORAGE_PAGE_SIZE || 0; + const size = req.query.size || process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE || 0; const num = Math.abs(Number(size)); - const max = Math.abs(Number(process.env.CLAY_STORAGE_MAX_PAGE)) || null; + const max = Math.abs(Number(process.env.CLAY_STORAGE_MAX_PAGE_SIZE)) || null; if (isNaN(num) || isNaN(max)) return 0; From 644fe67ac0c9eb38f442c4f9cf1cc7b9b3602577 Mon Sep 17 00:00:00 2001 From: james-owen Date: Tue, 8 Dec 2020 17:39:35 -0500 Subject: [PATCH 12/19] change checks to throw for invalid params --- lib/responses.js | 57 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 312f3ebe..085b9f3e 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -414,16 +414,31 @@ function expectJSON(fn, res) { * @param {Object} req * @returns {number} */ -function checkPageSize(req) { - const size = req.query.size || process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE || 0; - const num = Math.abs(Number(size)); - const max = Math.abs(Number(process.env.CLAY_STORAGE_MAX_PAGE_SIZE)) || null; +function checkPageSize(req, res) { + try { + let size; + + // fall back to default + if (!req.query.size && process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE) { + size = Math.abs(Number(process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE)); + } else if (req.query.size) { + // invalid size query param + if (isNaN(Math.abs(Number(req.query.size)))) { + throw Error('Client: Invalid size'); + } else { + // valid size query + size = Math.abs(Number(req.query.size)); + } + } - if (isNaN(num) || isNaN(max)) return 0; + const max = Math.abs(Number(process.env.CLAY_STORAGE_MAX_PAGE_SIZE)); - if (max) { - return num > max ? max : num; - } else return num; + if (max) { + return size > max ? max : size; + } else return size; + } catch (error) { + handleError(res)(error); + } } /** @@ -432,14 +447,22 @@ function checkPageSize(req) { * @param {Object} req * @returns {string|undefined} */ -function checkPrevious(req) { - const prefix = req.uri || null; - const previous = req.query.prev || null; +function checkPrevious(req, res) { + try { + const prefix = req.uri || null; + const previous = req.query.prev || null; - if (previous && previous.includes(prefix)) { - return previous; - } else { - return; + if (previous) { + if (previous.includes(prefix)) { + return previous; + } else { + throw Error('Client: Invalid previous'); + } + } else { + return; + } + } catch (error) { + handleError(res)(error); } } @@ -457,8 +480,8 @@ function list(options) { listOptions = _.assign({ prefix: req.uri, values: false, - previous: checkPrevious(req), - size: checkPageSize(req) + previous: checkPrevious(req, res), + size: checkPageSize(req, res) }, options); list = db.list(listOptions); From 4ba377b1fdbbf06c96c2ef1ac5f5e1068ac01f41 Mon Sep 17 00:00:00 2001 From: james-owen Date: Wed, 9 Dec 2020 09:37:14 -0500 Subject: [PATCH 13/19] add param to jsdoc --- lib/responses.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/responses.js b/lib/responses.js index 085b9f3e..ad29b83f 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -412,6 +412,7 @@ function expectJSON(fn, res) { * Checks the page size requested to make sure it's a Number and within the maximum. * Returns the default or 0 if there's no default or size is not a Number. * @param {Object} req + * @param {Object} res * @returns {number} */ function checkPageSize(req, res) { @@ -445,6 +446,7 @@ function checkPageSize(req, res) { * Checks whether the previous value for a page request matches up with the uri, * ie. all the ids returned should contain the request uri. * @param {Object} req + * @param {Object} res * @returns {string|undefined} */ function checkPrevious(req, res) { From 9bcbf81e8d78f0710e36c84ecc64e87b1f7225de Mon Sep 17 00:00:00 2001 From: james-owen Date: Wed, 9 Dec 2020 15:02:46 -0500 Subject: [PATCH 14/19] tests --- lib/responses.js | 4 +- lib/responses.test.js | 108 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/lib/responses.js b/lib/responses.js index ad29b83f..1718428c 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -451,7 +451,7 @@ function checkPageSize(req, res) { */ function checkPrevious(req, res) { try { - const prefix = req.uri || null; + const prefix = req.uri; const previous = req.query.prev || null; if (previous) { @@ -628,3 +628,5 @@ module.exports.deleteRouteFromDB = deleteRouteFromDB; // For testing module.exports.setLog = mock => log = mock; module.exports.setDb = mock => db = mock; +module.exports.checkPrevious = checkPrevious; +module.exports.checkPageSize = checkPageSize; \ No newline at end of file diff --git a/lib/responses.test.js b/lib/responses.test.js index 168c5d68..dfe8c3b2 100644 --- a/lib/responses.test.js +++ b/lib/responses.test.js @@ -298,6 +298,114 @@ describe(_.startCase(filename), function () { }); }); + describe('checkPageSize', function () { + const fn = lib[this.title]; + + afterEach(function () { + delete process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE; + delete process.env.CLAY_STORAGE_MAX_PAGE_SIZE; + }); + + it('falls back to default if there is no size param', function () { + const req = { + query: {}, + uri: 'domain.com/_components/component/instances', + }; + + const res = createMockRes(); + + process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10'; + + expect(fn(req, res)).to.equal(10); + }); + + it('returns max when max is defined and size > max', function () { + const req = { + query: { size: '300' }, + uri: 'domain.com/_components/component/instances', + }; + + const res = createMockRes(); + + process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10'; + process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100'; + + expect(fn(req, res)).to.equal(100); + }); + + it('returns valid size that is smaller than max', function () { + const req = { + query: { size: '42' }, + uri: 'domain.com/_components/component/instances', + }; + + const res = createMockRes(); + + process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10'; + process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100'; + + expect(fn(req, res)).to.equal(42); + }); + + it('errors for invalid size query param', function () { + const req = { + query: { size: 'string' }, + uri: 'domain.com/_components/component/instances', + }; + + const res = createMockRes(); + + process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10'; + process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100'; + + fn(req, res); + + expectStatus(res, 400); + }); + }); + + describe('checkPrevious', function () { + const fn = lib[this.title]; + + it('returns valid previous query param value', function () { + const req = { + query: { + prev: 'domain.com/_components/component/instances/aaa', + }, + uri: 'domain.com/_components/component/instances', + }; + const res = createMockRes(); + + expect(fn(req, res)).to.equal( + 'domain.com/_components/component/instances/aaa' + ); + }); + + it('throws and responds with 400 for an invalid previous value', function () { + const req = { + query: { + prev: 'random value', + }, + uri: 'domain.com/_components/component/instances', + }; + const res = createMockRes(); + + fn(req, res); + + expectStatus(res, 400); + }); + + it('returns undefined if no previous value', function () { + const req = { + query: {}, + uri: 'domain.com/_components/component/instances', + }; + const res = createMockRes(); + + expect(fn(req, res)).to.equal(undefined); + }); + }); + describe('list', function () { const fn = lib[this.title]; From 3f134cf4ce69b6f55262300242a9c41df5fdb06f Mon Sep 17 00:00:00 2001 From: James_Owen <35383768+james-owen@users.noreply.github.com> Date: Thu, 10 Dec 2020 11:59:49 -0500 Subject: [PATCH 15/19] Update lib/responses.js Co-authored-by: Matt Oberle --- lib/responses.js | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 1718428c..2406c839 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -416,30 +416,21 @@ function expectJSON(fn, res) { * @returns {number} */ function checkPageSize(req, res) { - try { - let size; - - // fall back to default - if (!req.query.size && process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE) { - size = Math.abs(Number(process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE)); - } else if (req.query.size) { - // invalid size query param - if (isNaN(Math.abs(Number(req.query.size)))) { - throw Error('Client: Invalid size'); - } else { - // valid size query - size = Math.abs(Number(req.query.size)); - } - } - - const max = Math.abs(Number(process.env.CLAY_STORAGE_MAX_PAGE_SIZE)); - - if (max) { - return size > max ? max : size; - } else return size; - } catch (error) { - handleError(res)(error); - } +function checkPageSize(req, res) { + // Casts value to a number > 0 or undefined. + const toInt = (n) => Math.floor(Math.abs(Number(n))) || undefined; + + const sizeDefault = toInt(process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE); + const sizeParam = toInt(req.query.size); + const sizeMax = toInt(process.env.CLAY_STORAGE_MAX_PAGE_SIZE); + + const size = sizeParam || sizeDefault; + + if (size) { + return Math.min(sizeMax, size) || size; + } + + return sizeMax; } /** @@ -629,4 +620,4 @@ module.exports.deleteRouteFromDB = deleteRouteFromDB; module.exports.setLog = mock => log = mock; module.exports.setDb = mock => db = mock; module.exports.checkPrevious = checkPrevious; -module.exports.checkPageSize = checkPageSize; \ No newline at end of file +module.exports.checkPageSize = checkPageSize; From 515eed1e3b8deac13499d562e8b6131e74ca8e1a Mon Sep 17 00:00:00 2001 From: james-owen Date: Thu, 10 Dec 2020 12:01:49 -0500 Subject: [PATCH 16/19] commit suggestions --- lib/responses.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 2406c839..453554cc 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -412,11 +412,9 @@ function expectJSON(fn, res) { * Checks the page size requested to make sure it's a Number and within the maximum. * Returns the default or 0 if there's no default or size is not a Number. * @param {Object} req - * @param {Object} res * @returns {number} */ -function checkPageSize(req, res) { -function checkPageSize(req, res) { +function checkPageSize(req) { // Casts value to a number > 0 or undefined. const toInt = (n) => Math.floor(Math.abs(Number(n))) || undefined; @@ -441,22 +439,14 @@ function checkPageSize(req, res) { * @returns {string|undefined} */ function checkPrevious(req, res) { - try { - const prefix = req.uri; - const previous = req.query.prev || null; - - if (previous) { - if (previous.includes(prefix)) { - return previous; - } else { - throw Error('Client: Invalid previous'); - } - } else { - return; - } - } catch (error) { - handleError(res)(error); - } + const prefix = req.uri; + const previous = req.query.prev; + + if (previous && prefix && !previous.includes(prefix)) { + return handleError(res)(Error('Client: Invalid previous')); + } + + return previous; } /** @@ -474,7 +464,7 @@ function list(options) { prefix: req.uri, values: false, previous: checkPrevious(req, res), - size: checkPageSize(req, res) + size: checkPageSize(req) }, options); list = db.list(listOptions); From 61dd05b3a025ea9fd320b4cb08929b606410eee5 Mon Sep 17 00:00:00 2001 From: james-owen Date: Thu, 10 Dec 2020 13:14:32 -0500 Subject: [PATCH 17/19] remove extra spaces --- lib/responses.js | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/responses.js b/lib/responses.js index 453554cc..fcf3d311 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -414,21 +414,21 @@ function expectJSON(fn, res) { * @param {Object} req * @returns {number} */ -function checkPageSize(req) { - // Casts value to a number > 0 or undefined. - const toInt = (n) => Math.floor(Math.abs(Number(n))) || undefined; - - const sizeDefault = toInt(process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE); - const sizeParam = toInt(req.query.size); - const sizeMax = toInt(process.env.CLAY_STORAGE_MAX_PAGE_SIZE); - - const size = sizeParam || sizeDefault; - - if (size) { - return Math.min(sizeMax, size) || size; - } - - return sizeMax; +function checkPageSize(req) { + // Casts value to a number > 0 or undefined. + const toInt = (n) => Math.floor(Math.abs(Number(n))) || undefined; + + const sizeDefault = toInt(process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE); + const sizeParam = toInt(req.query.size); + const sizeMax = toInt(process.env.CLAY_STORAGE_MAX_PAGE_SIZE); + + const size = sizeParam || sizeDefault; + + if (size) { + return Math.min(sizeMax, size) || size; + } + + return sizeMax; } /** @@ -439,14 +439,14 @@ function checkPageSize(req) { * @returns {string|undefined} */ function checkPrevious(req, res) { - const prefix = req.uri; - const previous = req.query.prev; - - if (previous && prefix && !previous.includes(prefix)) { - return handleError(res)(Error('Client: Invalid previous')); - } - - return previous; + const prefix = req.uri; + const previous = req.query.prev; + + if (previous && prefix && !previous.includes(prefix)) { + return handleError(res)(Error('Client: Invalid previous')); + } + + return previous; } /** From 03f42031db24688a7df08589704611f72f6c781b Mon Sep 17 00:00:00 2001 From: james-owen Date: Thu, 10 Dec 2020 13:21:11 -0500 Subject: [PATCH 18/19] update tests --- lib/responses.test.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/responses.test.js b/lib/responses.test.js index dfe8c3b2..b4965cb7 100644 --- a/lib/responses.test.js +++ b/lib/responses.test.js @@ -347,7 +347,7 @@ describe(_.startCase(filename), function () { expect(fn(req, res)).to.equal(42); }); - it('errors for invalid size query param', function () { + it('returns default for an invalid size query param', function () { const req = { query: { size: 'string' }, uri: 'domain.com/_components/component/instances', @@ -358,9 +358,7 @@ describe(_.startCase(filename), function () { process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10'; process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100'; - fn(req, res); - - expectStatus(res, 400); + expect(fn(req, res)).to.equal(10); }); }); @@ -390,9 +388,8 @@ describe(_.startCase(filename), function () { }; const res = createMockRes(); - fn(req, res); - expectStatus(res, 400); + fn(req, res); }); it('returns undefined if no previous value', function () { From f6d1db732a9a232378b27a69ee27f85bb3c1fc68 Mon Sep 17 00:00:00 2001 From: james-owen Date: Thu, 10 Dec 2020 15:11:37 -0500 Subject: [PATCH 19/19] add test for return max --- lib/responses.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/responses.test.js b/lib/responses.test.js index b4965cb7..8a8b0218 100644 --- a/lib/responses.test.js +++ b/lib/responses.test.js @@ -360,6 +360,19 @@ describe(_.startCase(filename), function () { expect(fn(req, res)).to.equal(10); }); + + it('returns max when no size or default', function () { + const req = { + query: {}, + uri: 'domain.com/_components/component/instances', + }; + + const res = createMockRes(); + + process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100'; + + expect(fn(req, res)).to.equal(100); + }); }); describe('checkPrevious', function () {