amphora 8.4.2-dev.0 → 8.4.2-dev.8.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 +1 -0
- package/lib/responses.js +2 -47
- package/lib/responses.test.js +0 -118
- package/lib/routes.js +19 -13
- package/lib/setup.js +2 -0
- package/package.json +1 -1
package/.eslintrc
CHANGED
package/lib/responses.js
CHANGED
|
@@ -445,47 +445,6 @@ 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
|
-
|
|
489
448
|
/**
|
|
490
449
|
* List all things in the db
|
|
491
450
|
* @param {object} [options]
|
|
@@ -499,9 +458,7 @@ function list(options) {
|
|
|
499
458
|
let list,
|
|
500
459
|
listOptions = _.assign({
|
|
501
460
|
prefix: req.uri,
|
|
502
|
-
values: false
|
|
503
|
-
previous: checkPrevious(req, res),
|
|
504
|
-
size: checkPageSize(req)
|
|
461
|
+
values: false
|
|
505
462
|
}, options);
|
|
506
463
|
|
|
507
464
|
list = db.list(listOptions);
|
|
@@ -521,7 +478,7 @@ function listWithoutVersions() {
|
|
|
521
478
|
return [filter({wantStrings: true}, function (str) {
|
|
522
479
|
return str.indexOf('@') === -1;
|
|
523
480
|
})];
|
|
524
|
-
}
|
|
481
|
+
}
|
|
525
482
|
};
|
|
526
483
|
|
|
527
484
|
return list(options);
|
|
@@ -646,5 +603,3 @@ module.exports.deleteRouteFromDB = deleteRouteFromDB;
|
|
|
646
603
|
// For testing
|
|
647
604
|
module.exports.setLog = mock => log = mock;
|
|
648
605
|
module.exports.setDb = mock => db = mock;
|
|
649
|
-
module.exports.checkPrevious = checkPrevious;
|
|
650
|
-
module.exports.checkPageSize = checkPageSize;
|
package/lib/responses.test.js
CHANGED
|
@@ -314,124 +314,6 @@ 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
|
-
|
|
435
317
|
describe('list', function () {
|
|
436
318
|
const fn = lib[this.title];
|
|
437
319
|
|
package/lib/routes.js
CHANGED
|
@@ -62,23 +62,28 @@ function addUri(req, res, next) {
|
|
|
62
62
|
* @param {express.Router} router
|
|
63
63
|
*/
|
|
64
64
|
function addControllerRoutes(router) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
try {
|
|
66
|
+
// load all controller routers
|
|
67
|
+
_.each(reservedRoutes, routeName => {
|
|
68
|
+
log('info', 'SETTING RESERVED ROUTES', { reservedRouteName: routeName });
|
|
69
|
+
let pathRouter,
|
|
70
|
+
filename = routeName + '.js',
|
|
71
|
+
controller = files.tryRequire([__dirname, routesPath, filename].join(path.sep));
|
|
70
72
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
// we're okay with an error occurring here because it means we're missing something important in a route
|
|
74
|
+
pathRouter = express.Router();
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
// assume json or text for anything in request bodies
|
|
77
|
+
pathRouter.use(require('body-parser').json({ strict: true, type: 'application/json', limit: '50mb' }));
|
|
78
|
+
pathRouter.use(require('body-parser').text({ type: 'text/*' }));
|
|
77
79
|
|
|
78
|
-
|
|
80
|
+
controller(pathRouter);
|
|
79
81
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
router.use(`/${routeName}`, pathRouter);
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
log('error', 'CATCH CONTROLLER ROUTES', { error });
|
|
86
|
+
}
|
|
82
87
|
}
|
|
83
88
|
|
|
84
89
|
/**
|
|
@@ -122,6 +127,7 @@ function addCORS(site) {
|
|
|
122
127
|
}
|
|
123
128
|
|
|
124
129
|
function setRoutes(controller, router, site) {
|
|
130
|
+
log('info', 'SET ROUTES: ', { site });
|
|
125
131
|
if (_.isFunction(controller.legacyController)) {
|
|
126
132
|
controller.legacyController(router, render, site);
|
|
127
133
|
}
|
package/lib/setup.js
CHANGED
package/package.json
CHANGED