Skip to content

Commit

Permalink
Update and cleanup in preparation for release
Browse files Browse the repository at this point in the history
* Update all dependencies. At the time of writing, any remaining 
vulnerabilities are because of Jest.
* Upgrade from Node's deprecated builtin `url.parse` to `url-parse` 
module. (v12 compatibility)
* Apply new Standard linting rules.
* Remove ESDoc
  • Loading branch information
tprobinson committed Jul 17, 2019
1 parent 16b3c86 commit bba526f
Show file tree
Hide file tree
Showing 10 changed files with 1,924 additions and 2,217 deletions.
20 changes: 10 additions & 10 deletions __mocks__/aws-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class S3 {
responseObject.Body = Buffer.from(responseObject.Body)

cb(err, responseObject)
return {send: () => {}}
return { send: () => {} }
}

putObject(inputParams = {}, cb) {
Expand Down Expand Up @@ -129,7 +129,7 @@ class S3 {

this.cache[params.Bucket][params.Key] = params
cb(err, null)
return {send: () => {}}
return { send: () => {} }
}

listObjectsV2(inputParams = {}, cb) {
Expand All @@ -143,7 +143,7 @@ class S3 {
this._log('listObjects', params)

if( !(params.Bucket in this.cache) ) {
cb(null, {Contents: []})
cb(null, { Contents: [] })
return
}

Expand All @@ -159,7 +159,7 @@ class S3 {
results = results.filter(x => x.Key.startsWith(params.Prefix))
}

const returnedStuff = {Contents: results}
const returnedStuff = { Contents: results }

// Slice the array if we're "continuing"
if( results.length > 1000 ) {
Expand All @@ -168,7 +168,7 @@ class S3 {
sliceBegin = params.ContinuationToken
}

let sliceEnd = Math.min(sliceBegin + 1000, results.length)
const sliceEnd = Math.min(sliceBegin + 1000, results.length)
returnedStuff.Contents = results.slice(sliceBegin, sliceEnd)
returnedStuff.NextContinuationToken = sliceEnd

Expand All @@ -178,7 +178,7 @@ class S3 {
}

cb(null, returnedStuff)
return {send: () => {}}
return { send: () => {} }
}

deleteObject(inputParams = {}, cb) {
Expand Down Expand Up @@ -211,7 +211,7 @@ class S3 {
}

cb(err, {})
return {send: () => {}}
return { send: () => {} }
}

deleteObjects(inputParams = {}, cb) {
Expand Down Expand Up @@ -248,7 +248,7 @@ class S3 {
}

cb(err, {})
return {send: () => {}}
return { send: () => {} }
}

headObject(inputParams = {}, cb) {
Expand All @@ -269,12 +269,12 @@ class S3 {

cb(err, result)
})
return {send: () => {}}
return { send: () => {} }
}
}

if( process.env.USE_REAL_AWS && (process.env.USE_REAL_AWS === 'true' || process.env.USE_REAL_AWS === true) ) {
module.exports = require.requireActual('aws-sdk')
} else {
module.exports = {S3}
module.exports = { S3 }
}
57 changes: 26 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
"description": "Cache manager module for S3",
"main": "src/index.js",
"keywords": [
"cache", "caching", "cache-manager",
"s3", "aws", "bucket",
"plugin", "backend", "module"
"cache",
"caching",
"cache-manager",
"s3",
"aws",
"bucket",
"plugin",
"backend",
"module"
],
"repository": "https://github.com/tprobinson/node-cache-manager-s3.git",
"bugs": {
Expand All @@ -23,46 +29,35 @@
"lintfix": "eslint --fix src/ test/ __mocks__/",
"test": "yarn run lint && jest --maxWorkers=4",
"test:coveralls": "yarn run test --coverage --coverageReporters=text-lcov | yarn exec coveralls",
"test:real": "yarn run lint && env USE_REAL_AWS=true yarn run test --runInBand",
"doc": "esdoc",
"docdev": "esdoc && serve -s docs"
"test:real": "yarn run lint && env USE_REAL_AWS=true yarn run test --runInBand"
},
"devDependencies": {
"cache-manager": "^2.9.0",
"coveralls": "^3.0.2",
"esdoc": "^1.1.0",
"esdoc-standard-plugin": "^1.0.0",
"eslint": "^5.3.0",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.13.0",
"cache-manager": "^2.10.0",
"coveralls": "^3.0.5",
"eslint": "^6.0.1",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-jest": "^21.18.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-standard": "^3.1.0",
"jest": "^23.4.2",
"random-words": "^1.1.0",
"serve": "^9.4.0"
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.8.0",
"random-words": "^1.1.0"
},
"jest": {
"testEnvironment": "node",
"collectCoverageFrom": [
"src/**/*.{js,jsx}"
]
},
"esdoc": {
"source": "./src",
"destination": "./docs",
"plugins": [{
"name": "esdoc-standard-plugin"
}]
},
"dependencies": {
"async": "^2.6.1",
"aws-sdk": "^2.287.0",
"chalk": "^2.4.1",
"async": "^2.6.3",
"aws-sdk": "^2.493.0",
"chalk": "^2.4.2",
"checksum": "^0.1.1",
"loglevel": "^1.6.1",
"loglevel": "^1.6.3",
"loglevel-plugin-prefix": "^0.8.4",
"moment": "^2.22.2"
"moment": "^2.24.0",
"url-parse": "^1.4.7"
}
}
23 changes: 11 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @module S3Cache */
const async = require('async')
const path = require('path')
const url = require('url')
const Url = require('url-parse')
const querystring = require('querystring')
const checksum = require('checksum')
const moment = require('moment')
Expand Down Expand Up @@ -133,7 +133,7 @@ class S3Cache {
validateOption('bucket')

// Translate passed in params to S3 constructor params.
let constructorOptions = {
const constructorOptions = {
params: {
Bucket: this.options.bucket,
},
Expand Down Expand Up @@ -180,7 +180,7 @@ class S3Cache {
this._log = ['get', 'set', 'keys', 'head', 'ttl', 'del', 'reset', 'normalizePath', 'timestampToMoment', 'stringifyResponse']
.reduce((memo, type) => {
// Create the logger
Object.assign(memo, {[type]: log.getLogger(type)})
Object.assign(memo, { [type]: log.getLogger(type) })

// Look for an environment variable with this logger's name to set level
if( `S3CACHE_${type.toUpperCase()}_LOGLEVEL` in process.env && process.env[`S3CACHE_${type.toUpperCase()}_LOGLEVEL`] ) {
Expand All @@ -201,21 +201,20 @@ class S3Cache {
* @return {string} - The input string, normalized.
*/
_normalizeUrl(str, options = this.options) {
const request = url.parse(str)
const request = new Url(str)

if( options.normalizeUrl ) {
if( request.search !== null ) {
// Sort query parameters -- slice off the leading ?
const params = querystring.parse(request.search.slice(1))

// Sort param keys
request.search = Object.keys(params).sort().map(key =>
querystring.stringify({[key]: params[key]})
const sanitizedQuery = Object.keys(request.query).sort().map(key =>
querystring.stringify({ [key]: request.query[key] })
).join('&')

request.set('search', sanitizedQuery)
}
}

return url.format(request)
return request.toString()
}

/**
Expand Down Expand Up @@ -681,13 +680,13 @@ class S3Cache {
reset(cb) {
this._log.reset.warn('Resetting bucket!')
async.waterfall([
waterCb => this.keys({dontConcatPages: true}, waterCb),
waterCb => this.keys({ dontConcatPages: true }, waterCb),
(results, waterCb) => async.mapLimit(results, 2, (dataset, mapCb) => {
if( dataset.length === 0 ) { mapCb(); return }
this.s3.deleteObjects({
Delete: {
// deleteObjects does not accept any parameters except key and version
Objects: dataset.map(({Key, VersionId}) => ({Key, VersionId})),
Objects: dataset.map(({ Key, VersionId }) => ({ Key, VersionId })),
},
}, mapCb)
}, waterCb)
Expand Down
16 changes: 8 additions & 8 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('class construction options', () => {
test('can add options to constructor', () => {
const region = 'us-west-2'
const cache = new S3Cache(Object.assign({}, keyParams, {
s3Options: {region}
s3Options: { region }
}))

expect(cache).toHaveProperty('s3.config.region', region)
Expand All @@ -25,20 +25,20 @@ describe('class construction options', () => {
const headerKey = 'ContentType'
const headerValue = 'text/plain'
const cache = new S3Cache(Object.assign({}, keyParams, {
s3Options: {params: {[headerKey]: headerValue}}
s3Options: { params: { [headerKey]: headerValue } }
}))

expect(cache).toHaveProperty(`s3.config.params.${headerKey}`, headerValue)
})

test('construction fails without required parameters', () => {
expect(() => new S3Cache({accessKey: 'a', secretKey: 'b'})).toThrow()
expect(() => new S3Cache({secretKey: 'b', 'bucket': 'c'})).toThrow()
expect(() => new S3Cache({'bucket': 'c', accessKey: 'a'})).toThrow()
expect(() => new S3Cache({ accessKey: 'a', secretKey: 'b' })).toThrow()
expect(() => new S3Cache({ secretKey: 'b', bucket: 'c' })).toThrow()
expect(() => new S3Cache({ bucket: 'c', accessKey: 'a' })).toThrow()
})

test('construction fails with incorrect parameters', () => {
expect(() => new S3Cache(Object.assign({}, keyParams, {s3Options: 2}))).toThrow()
expect(() => new S3Cache(Object.assign({}, keyParams, { s3Options: 2 }))).toThrow()
})
})

Expand All @@ -56,7 +56,7 @@ describe('basic function test', () => {
const largeCountOfKeys = (Math.random() + 1) * 100 + 1000
const largeListOfKeys = []
for( let i = largeCountOfKeys; i > 0; i-- ) {
largeListOfKeys.push({key: random(utils.largeRandomOptions), value: random()})
largeListOfKeys.push({ key: random(utils.largeRandomOptions), value: random() })
}

beforeAll(done => cache.reset(done))
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('basic function test', () => {
async.series([
seriesCb => cache.set(testBinaryKey, testBinaryValue, seriesCb),
seriesCb => cache.get(testBinaryKey, seriesCb),
seriesCb => cache.get(testBinaryKey, {stringifyResponses: false}, seriesCb),
seriesCb => cache.get(testBinaryKey, { stringifyResponses: false }, seriesCb),
], (err, values) => {
expect(err).toBeNull()
expect(values[1]).not.toEqual(testBinaryValue)
Expand Down
16 changes: 8 additions & 8 deletions test/basicExtra.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('basic function test with options overrides', () => {
const testValue = random()
const headerName = 'ContentType'
const headerValue = 'text/plain'
const headers = {s3Options: {[headerName]: headerValue}}
const headers = { s3Options: { [headerName]: headerValue } }

beforeAll(done => cache.reset(done))
afterAll(done => cache.reset(done))
Expand All @@ -73,7 +73,7 @@ describe('basic function test with options overrides', () => {
})

test('get string with options', done => {
cache.get(testKey, {s3Options: {IfModifiedSince: 0}}, (err, value) => {
cache.get(testKey, { s3Options: { IfModifiedSince: 0 } }, (err, value) => {
expect(err).toBeNull()
expect(value).toEqual(testValue)
done()
Expand All @@ -97,7 +97,7 @@ describe('basic function test with options overrides', () => {
})

test('list keys with options', done => {
cache.keys({s3Options: {MaxKeys: 1000}}, (err, values) => {
cache.keys({ s3Options: { MaxKeys: 1000 } }, (err, values) => {
expect(err).toBeNull()
expect(values).toHaveLength(1)
done()
Expand All @@ -111,7 +111,7 @@ describe('basic function test with options overrides', () => {
})

test('get string with no checksum on key', done => {
cache.get(testKey, {checksumAlgorithm: 'none'}, (err, value) => {
cache.get(testKey, { checksumAlgorithm: 'none' }, (err, value) => {
expect(err).toBeNull()
expect(value).toEqual(testValue)
done()
Expand All @@ -134,7 +134,7 @@ describe('basic function test with options overrides', () => {
})

test('get string with base64 checksum', done => {
cache.get(testKey, {checksumAlgorithm: 'none', checksumEncoding: 'base64'}, (err, value) => {
cache.get(testKey, { checksumAlgorithm: 'none', checksumEncoding: 'base64' }, (err, value) => {
expect(err).toBeNull()
expect(value).toEqual(testValue)
done()
Expand All @@ -150,14 +150,14 @@ describe('basic function test with options overrides', () => {
})

test('fail to get keys when API error triggered.', done => {
cache.keys('', {s3Options: {PleaseBreakApi: 1}}, (err, values) => {
cache.keys('', { s3Options: { PleaseBreakApi: 1 } }, (err, values) => {
expect(err).toEqual(new UnexpectedParameter("Unexpected key 'PleaseBreakApi' found in params"))
done()
})
})

test('get key metadata', done => {
cache.head(testKey, {s3Options: {IfModifiedSince: 0}}, (err, value) => {
cache.head(testKey, { s3Options: { IfModifiedSince: 0 } }, (err, value) => {
expect(err).toBeNull()
expect(value).toHaveProperty(headerName, headerValue)
done()
Expand All @@ -174,7 +174,7 @@ describe('basic function test with options overrides', () => {

test('delete string', done => {
// Force this bucket option so we test the s3Option assign code
cache.del(testKey, {s3Options: {Bucket: keyParams.bucket}}, done)
cache.del(testKey, { s3Options: { Bucket: keyParams.bucket } }, done)
})
})

Expand Down
8 changes: 4 additions & 4 deletions test/logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jest.spyOn(global.console, 'error').mockImplementation(fakeConsoleError)

describe('logging', () => {
test('debug logging with option', done => {
const cache = new S3Cache(Object.assign({}, keyParams, {logLevel: 'TRACE'}))
const cache = new S3Cache(Object.assign({}, keyParams, { logLevel: 'TRACE' }))

cache.keys(() => {
expect(fakeConsoleLog).toHaveBeenCalled()
Expand Down Expand Up @@ -117,8 +117,8 @@ describe('test internal functions with alternate parameters', () => {
const string = random()
const bufferString = random()
const buffer = Buffer.from(bufferString)
expect(cache._stringifyResponse({Body: string})).toEqual(string)
expect(cache._stringifyResponse({Body: buffer})).toEqual(bufferString)
expect(cache._stringifyResponse({Body: buffer}, {stringifyResponse: false})).toEqual(buffer)
expect(cache._stringifyResponse({ Body: string })).toEqual(string)
expect(cache._stringifyResponse({ Body: buffer })).toEqual(bufferString)
expect(cache._stringifyResponse({ Body: buffer }, { stringifyResponse: false })).toEqual(buffer)
})
})
Loading

0 comments on commit bba526f

Please sign in to comment.