amphora 8.4.1 → 8.4.2-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc CHANGED
@@ -51,7 +51,6 @@
51
51
  "no-trailing-spaces": 2,
52
52
  "no-underscore-dangle": 0,
53
53
  "no-unneeded-ternary": 1,
54
- "one-var": 2,
55
54
  "quotes": [2, "single", "avoid-escape"],
56
55
  "semi": [2, "always"],
57
56
  "keyword-spacing": 2,
package/lib/responses.js CHANGED
@@ -445,6 +445,47 @@ function expectJSON(fn, res) {
445
445
  }).catch(handleError(res));
446
446
  }
447
447
 
448
+ /**
449
+ * Checks the page size requested to make sure it's a Number and within the maximum.
450
+ * Returns the default or 0 if there's no default or size is not a Number.
451
+ * @param {Object} req
452
+ * @returns {number}
453
+ */
454
+ function checkPageSize(req) {
455
+ // Casts value to a number > 0 or undefined.
456
+ const toInt = (n) => Math.floor(Math.abs(Number(n))) || undefined;
457
+
458
+ const sizeDefault = toInt(process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE);
459
+ const sizeParam = toInt(req.query.size);
460
+ const sizeMax = toInt(process.env.CLAY_STORAGE_MAX_PAGE_SIZE);
461
+
462
+ const size = sizeParam || sizeDefault;
463
+
464
+ if (size) {
465
+ return Math.min(sizeMax, size) || size;
466
+ }
467
+
468
+ return sizeMax;
469
+ }
470
+
471
+ /**
472
+ * Checks whether the previous value for a page request matches up with the uri,
473
+ * ie. all the ids returned should contain the request uri.
474
+ * @param {Object} req
475
+ * @param {Object} res
476
+ * @returns {string|undefined}
477
+ */
478
+ function checkPrevious(req, res) {
479
+ const prefix = req.uri;
480
+ const previous = req.query.prev;
481
+
482
+ if (previous && prefix && !previous.includes(prefix)) {
483
+ return handleError(res)(Error('Client: Invalid previous'));
484
+ }
485
+
486
+ return previous;
487
+ }
488
+
448
489
  /**
449
490
  * List all things in the db
450
491
  * @param {object} [options]
@@ -458,7 +499,9 @@ function list(options) {
458
499
  let list,
459
500
  listOptions = _.assign({
460
501
  prefix: req.uri,
461
- values: false
502
+ values: false,
503
+ previous: checkPrevious(req, res),
504
+ size: checkPageSize(req)
462
505
  }, options);
463
506
 
464
507
  list = db.list(listOptions);
@@ -478,7 +521,7 @@ function listWithoutVersions() {
478
521
  return [filter({wantStrings: true}, function (str) {
479
522
  return str.indexOf('@') === -1;
480
523
  })];
481
- }
524
+ },
482
525
  };
483
526
 
484
527
  return list(options);
@@ -603,3 +646,5 @@ module.exports.deleteRouteFromDB = deleteRouteFromDB;
603
646
  // For testing
604
647
  module.exports.setLog = mock => log = mock;
605
648
  module.exports.setDb = mock => db = mock;
649
+ module.exports.checkPrevious = checkPrevious;
650
+ module.exports.checkPageSize = checkPageSize;
@@ -314,6 +314,124 @@ describe(_.startCase(filename), function () {
314
314
  });
315
315
  });
316
316
 
317
+ describe('checkPageSize', function () {
318
+ const fn = lib[this.title];
319
+
320
+ afterEach(function () {
321
+ delete process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE;
322
+ delete process.env.CLAY_STORAGE_MAX_PAGE_SIZE;
323
+ });
324
+
325
+ it('falls back to default if there is no size param', function () {
326
+ const req = {
327
+ query: {},
328
+ uri: 'domain.com/_components/component/instances',
329
+ };
330
+
331
+ const res = createMockRes();
332
+
333
+ process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10';
334
+
335
+ expect(fn(req, res)).to.equal(10);
336
+ });
337
+
338
+ it('returns max when max is defined and size > max', function () {
339
+ const req = {
340
+ query: { size: '300' },
341
+ uri: 'domain.com/_components/component/instances',
342
+ };
343
+
344
+ const res = createMockRes();
345
+
346
+ process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10';
347
+ process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100';
348
+
349
+ expect(fn(req, res)).to.equal(100);
350
+ });
351
+
352
+ it('returns valid size that is smaller than max', function () {
353
+ const req = {
354
+ query: { size: '42' },
355
+ uri: 'domain.com/_components/component/instances',
356
+ };
357
+
358
+ const res = createMockRes();
359
+
360
+ process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10';
361
+ process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100';
362
+
363
+ expect(fn(req, res)).to.equal(42);
364
+ });
365
+
366
+ it('returns default for an invalid size query param', function () {
367
+ const req = {
368
+ query: { size: 'string' },
369
+ uri: 'domain.com/_components/component/instances',
370
+ };
371
+
372
+ const res = createMockRes();
373
+
374
+ process.env.CLAY_STORAGE_DEFAULT_PAGE_SIZE = '10';
375
+ process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100';
376
+
377
+ expect(fn(req, res)).to.equal(10);
378
+ });
379
+
380
+ it('returns max when no size or default', function () {
381
+ const req = {
382
+ query: {},
383
+ uri: 'domain.com/_components/component/instances',
384
+ };
385
+
386
+ const res = createMockRes();
387
+
388
+ process.env.CLAY_STORAGE_MAX_PAGE_SIZE = '100';
389
+
390
+ expect(fn(req, res)).to.equal(100);
391
+ });
392
+ });
393
+
394
+ describe('checkPrevious', function () {
395
+ const fn = lib[this.title];
396
+
397
+ it('returns valid previous query param value', function () {
398
+ const req = {
399
+ query: {
400
+ prev: 'domain.com/_components/component/instances/aaa',
401
+ },
402
+ uri: 'domain.com/_components/component/instances',
403
+ };
404
+ const res = createMockRes();
405
+
406
+ expect(fn(req, res)).to.equal(
407
+ 'domain.com/_components/component/instances/aaa'
408
+ );
409
+ });
410
+
411
+ it('throws and responds with 400 for an invalid previous value', function () {
412
+ const req = {
413
+ query: {
414
+ prev: 'random value',
415
+ },
416
+ uri: 'domain.com/_components/component/instances',
417
+ };
418
+ const res = createMockRes();
419
+
420
+ expectStatus(res, 400);
421
+ fn(req, res);
422
+ });
423
+
424
+ it('returns undefined if no previous value', function () {
425
+ const req = {
426
+ query: {},
427
+ uri: 'domain.com/_components/component/instances',
428
+ };
429
+ const res = createMockRes();
430
+
431
+ expect(fn(req, res)).to.equal(undefined);
432
+ });
433
+ });
434
+
317
435
  describe('list', function () {
318
436
  const fn = lib[this.title];
319
437
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amphora",
3
- "version": "8.4.1",
3
+ "version": "8.4.2-0",
4
4
  "description": "An API mixin for Express that saves, publishes and composes data with the key-value store of your choice.",
5
5
  "main": "index.js",
6
6
  "scripts": {