Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
before(function () {
redisCacheByUrl = require('cache-manager').caching({
store: redisStore,
// redis://[:password@]host[:port][/db-number][?option=value]
url: 'redis://:' + config.redis.auth_pass + '@' + config.redis.host + ':' + config.redis.port + '/' + config.redis.db + '?ttl=' + config.redis.ttl,
// some fakes to see that url overrides them
host: 'test-host',
port: -78,
db: -7,
auth_pass: 'test_pass',
password: 'test_pass',
ttl: -6,
compress: true
});
});
// @flow
import cacheManager from 'cache-manager';
import type { Cache } from 'cache-manager';
const memoryCache: Cache = new cacheManager.caching({
store: 'memory', max: 100, ttl: 10
});
// $ExpectError store missing
const invalidMemoryCache: Cache = new cacheManager.caching({
max: 200,
});
// $ExpectError
const invalidMemoryCache2: Cache = new cacheManager.caching({ max: '200' });
(memoryCache.set('foo', 'bar', { ttl: 5 }): Promise);
// $ExpectError Wrong Promise return
(memoryCache.set('foo', 'bar', { ttl: 5 }): Promise);
memoryCache.get('foo');
// FIXME: move this to a test helper that can be used by other apps
import cacheManager from 'cache-manager'
import { Application, ApplicationFunction } from '../../src'
const cache = cacheManager.caching({ store: 'memory', ttl: 0 })
export function newApp (): Application {
return new Application({ app: {
getInstallationAccessToken: jest.fn().mockResolvedValue('test'),
getSignedJsonWebToken: jest.fn().mockReturnValue('test')
}, cache })
}
export function createApp (appFn?: ApplicationFunction) {
const app = newApp()
appFn && appFn(app)
return app
}
max: 0,
ttl: 0
};
}
// force 0 ttl
cacheOpts.ttl = 0;
console.log("store options: %j", cacheOpts);
switch (cacheOpts.store) {
case "redis":
cacheOpts.store = redisStore;
break;
}
var cache = cacheManager.caching(cacheOpts);
function set(key, val, ttl = 0) {
ttl = Math.floor(ttl);
return new Promise((resolve, reject) => {
cache.set(key, val, { ttl: ttl }, err => {
if (err) {
reject(err);
}
resolve();
});
});
}
function get(key) {
return new Promise((resolve, reject) => {
cache.get(key, (err, result) => {
import config from 'config';
import extend from 'extend';
import logger from 'logger';
import redisStore from 'cache-manager-redis';
// level 2 cache - redis
let redisConfig = extend({}, config.caching.redis, { store: redisStore });
let redisCache = cacheManager.caching(redisConfig);
redisCache.store.events.on('redisError', function onRedisCacheError(error) {
logger.error(error);
});
// level 1 cache - memory
let memoryConfig = extend({}, config.caching.memory, { store: 'memory' });
let memoryCache = cacheManager.caching(memoryConfig);
// cache hierarchy wrapper
let multiCache = cacheManager.multiCaching([ memoryCache, redisCache ]);
export default multiCache;
inited = new Promise(resolve => {
var fillcb = null;
if (CONF.cache.existing) fillcb = resolve;
diskCache = cacheManager.caching({
store: fsStore,
options: {
ttl: CONF.cache.ttl,
maxsize: CONF.cache.size,
path: cacheFolder,
preventfill: !CONF.cache.existing,
fillcallback: fillcb } });
if (!CONF.cache.existing) resolve();
});
};
function cached(metadataEndpoint, collection, context, cb) {
const cache = (collection in _caches) ? _caches[collection] : cacheManager.caching({
store: 'memory',
max: 1000,
ttl: 60 * 60 * 12
})
return cache.wrap(context, function() {
return new Promise(function(resolve, reject) {
axios.get(`${metadataEndpoint}?uri=${context}`)
.then(({data}) => resolve(data))
.catch(err => reject(err))
})
})
}
init: function() {
this.cache = cacheManager.caching({
store: s3_cache
});
},
this._stores = this._stores.map(store => nodeCacheManager.caching(store));
this._cacheManager = nodeCacheManager.multiCaching(this._stores);
var basicAuth = require('basic-auth')
var request = require('request')
var debug = require('debug')('simple-odk:github-auth')
var cacheManager = require('cache-manager')
var createHash = require('../helpers/sha-hash.js')
var authCache = cacheManager.caching({store: 'memory', max: 500, ttl: 300})
request = request.defaults({
headers: { 'User-Agent': 'simple-odk' }
})
/**
* Middleware to authenticate against Github using Basic Auth.
* Github will return status 404 for unauthorized requests
* https://developer.github.com/v3/auth/#basic-authentication
* This checks a user/pass is valid against Github and returns
* 401 to the ODK Collect client if it not
*/
function GithubAuth () {
return function (req, res, next) {
var auth = basicAuth(req)
var t0 = Date.now()