-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from lob/MI-471/dependency-upgrades
MI-471: Upgrade Hapi and other vulnerable deps
- Loading branch information
Showing
20 changed files
with
4,299 additions
and
9,333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
{ | ||
extends: "lob", | ||
globals: { | ||
expect: false | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
}, | ||
rules: { | ||
"parserOptions": { | ||
"ecmaVersion": 2020 | ||
}, | ||
"extends": "lob", | ||
"globals": { | ||
"expect": false | ||
}, | ||
"rules": { | ||
"no-extra-parens": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ jobs: | |
|
||
strategy: | ||
matrix: | ||
node-version: [12, 14, 16] | ||
node-version: [18, 20] | ||
|
||
services: | ||
redis: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,8 @@ node_modules | |
|
||
# SQLite databases | ||
*.sqlite3 | ||
|
||
.idea/ | ||
|
||
.nyc_output/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
16.14.0 | ||
20.15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
|
||
services: | ||
redis: | ||
image: redis:7 | ||
ports: | ||
- "6379:6379" | ||
volumes: | ||
- redis_data:/data | ||
|
||
|
||
volumes: | ||
redis_data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
'use strict'; | ||
|
||
const Bluebird = require('bluebird'); | ||
|
||
const RADIX = 10; | ||
|
||
exports.count = (Model, request, Redis, key, ttlFn) => { | ||
return Bluebird.resolve() | ||
.then(() => { | ||
if (typeof Model.prototype.filter === 'function') { | ||
return new Model().filter(request.query.filter, request.auth.credentials).count('*'); | ||
} | ||
return new Model().count('*'); | ||
}) | ||
.then((count) => { | ||
return parseInt(count, RADIX); | ||
}) | ||
.tap((count) => { | ||
if (Redis) { | ||
const seconds = ttlFn(count); | ||
return Redis.setAsync(key, count, 'EX', seconds); | ||
} | ||
}); | ||
return Promise.resolve() | ||
.then(() => { | ||
if (typeof Model.prototype.filter === 'function') { | ||
return new Model().filter(request.query.filter, request.auth.credentials).count('*'); | ||
} | ||
return new Model().count('*'); | ||
}) | ||
.then((count) => { | ||
return parseInt(count, RADIX); | ||
}) | ||
.then(async (count) => { | ||
if (Redis) { | ||
const seconds = ttlFn(count); | ||
await Redis.set(key, count, { EX: seconds }); | ||
} | ||
return count; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,65 @@ | ||
'use strict'; | ||
|
||
const Bluebird = require('bluebird'); | ||
const Joi = require('joi'); | ||
|
||
const ApproximateCountValidator = require('./validators/approximate-count'); | ||
const Counter = require('./counter'); | ||
const Key = require('./key'); | ||
const TotalCountValidator = require('./validators/total-count'); | ||
|
||
exports.register = (server, options, next) => { | ||
/** @typedef {import('@hapi/hapi').Server} Server } */ | ||
|
||
/** @typedef {import('redis').RedisClientType} RedisClient } */ | ||
|
||
server.ext('onPreResponse', (request, reply) => { | ||
/** | ||
* Registers the events routes. | ||
* @param {Server} server | ||
* @param {{ ttl: number?, redisClient: RedisClient?, uniqueKey: Function? }} options | ||
*/ | ||
function register (server, options) { | ||
|
||
server.ext('onPreResponse', (request, h) => { | ||
|
||
const settings = request.route.settings.plugins.totalCount; | ||
const includeApproximate = !settings || !settings.include || settings.include.indexOf('approximate') !== -1; | ||
const includeTotal = !settings || !settings.include || settings.include.indexOf('total') !== -1; | ||
const approximateCountValid = includeApproximate ? !Joi.validate(request, ApproximateCountValidator, { allowUnknown: true }).error : false; | ||
const totalCountValid = includeTotal ? !Joi.validate(request, TotalCountValidator, { allowUnknown: true }).error : false; | ||
const approximateCountValid = includeApproximate ? !ApproximateCountValidator.validate(request, { allowUnknown: true }).error : false; | ||
const totalCountValid = includeTotal ? !TotalCountValidator.validate(request, { allowUnknown: true }).error : false; | ||
|
||
if (!settings || !settings.model || (!approximateCountValid && !totalCountValid)) { | ||
return reply.continue(); | ||
return h.continue; | ||
} | ||
|
||
const Model = settings.model; | ||
const Redis = options.redisClient; | ||
const ttl = options.ttl; | ||
const key = Key.generate(Model, request, options.uniqueKey); | ||
|
||
return Bluebird.resolve() | ||
.then(() => { | ||
if (Redis && approximateCountValid) { | ||
return Redis.getAsync(key); | ||
} | ||
}) | ||
.then((cachedCount) => { | ||
if (cachedCount) { | ||
return parseInt(cachedCount); | ||
} | ||
|
||
return Counter.count(Model, request, Redis, key, ttl); | ||
}) | ||
.then((count) => { | ||
if (approximateCountValid) { | ||
request.response.source.approximate_count = count; | ||
} else { | ||
request.response.source.total_count = count; | ||
} | ||
return reply.continue(); | ||
}) | ||
.catch((err) => reply(err)); | ||
}); | ||
return Promise.resolve() | ||
.then(() => { | ||
if (Redis && approximateCountValid) { | ||
return Redis.get(key); | ||
} | ||
}) | ||
.then((cachedCount) => { | ||
if (cachedCount) { | ||
return parseInt(cachedCount); | ||
} | ||
|
||
next(); | ||
|
||
}; | ||
return Counter.count(Model, request, Redis, key, ttl); | ||
}) | ||
.then((count) => { | ||
if (approximateCountValid) { | ||
request.response.source.approximate_count = count; | ||
} else { | ||
request.response.source.total_count = count; | ||
} | ||
return h.continue; | ||
}).catch((err) => { | ||
throw err; | ||
}); | ||
}); | ||
} | ||
|
||
exports.register.attributes = { | ||
pkg: require('../package.json') | ||
exports.plugin = { | ||
pkg: require('../package.json'), | ||
register | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.