Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'lru-memoizer' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
import async from 'async';
import nconf from 'nconf';
import moment from 'moment';
import Promise from 'bluebird';
import request from 'request';
import memoizer from 'lru-memoizer';
import { getDb } from './storage/getdb';
// TODO: Move to static client.
export const getTokenCached = memoizer({
load: (sub, callback) => {
getDb().getToken(sub)
.then((token) => {
callback(null, token.accessToken);
})
.catch(callback);
},
hash: (sub) => sub,
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
export const getToken = (sub, cb) => {
const token = nconf.get('AUTH0_APIV2_TOKEN');
if (token) {
return cb(null, token);
constructor(storage, cacheAge = 1000 * 10) {
if (storage === null || storage === undefined) {
throw new ArgumentError('Must provide a storage object.');
}
this.log = logger.debug.bind(logger);
this.cache = { };
this.storage = storage;
this.getCached = Promise.promisify(
memoizer({
load: (name, callback) => {
this.get(name)
.then((script) => {
callback(null, script);
return null;
})
.catch(callback);
},
hash: name => name,
max: 100,
maxAge: cacheAge
})
);
}
load: (db, callback) => {
db.getRoles()
.then(roles => {
callback(null, roles);
})
.catch(err => callback(err));
},
hash: (db) => db.hash || 'roles',
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
/*
* Cache groups.
*/
export const getGroupsCached = memoizer({
load: (db, callback) => {
db.getGroups()
.then(groups => {
callback(null, groups);
})
.catch(err => callback(err));
},
hash: (db) => db.hash || 'groups',
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
/*
* Get the full connection names for all mappings.
*/
export const getMappingsWithNames = (auth0, groupMappings) =>
import ms from 'ms';
import { ManagementClient } from 'auth0';
import Promise from 'bluebird';
import memoizer from 'lru-memoizer';
import request from 'request';
import logger from './logger';
const getAccessToken = Promise.promisify(
memoizer({
load: (domain, clientId, clientSecret, callback) => {
logger.debug(`Requesting access token for ${clientId} - https://${domain}/api/v2/`);
const options = {
uri: `https://${domain}/oauth/token`,
body: {
audience: `https://${domain}/api/v2/`,
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret
},
json: true
};
request.post(options, (err, res, body) => {
if (err) {
apiCall(auth0, auth0.connections.getAll, [ { fields: 'id,name,strategy' } ])
.then(connections => _.chain(connections)
.sortBy((conn) => conn.name.toLowerCase())
.value())
.then(connections => callback(null, connections))
.catch(err => callback(err));
},
hash: (auth0) => auth0.hash || 'connections',
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
/*
* Cache permissions.
*/
export const getPermissionsCached = memoizer({
load: (db, callback) => {
db.getPermissions()
.then(permissions => {
callback(null, permissions);
})
.catch(err => callback(err));
},
hash: (db) => db.hash || 'permissions',
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
/*
* Cache roles.
*/
export const getRolesCached = memoizer({
load: (db, callback) => {
db.getPermissions()
.then(permissions => {
callback(null, permissions);
})
.catch(err => callback(err));
},
hash: (db) => db.hash || 'permissions',
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
/*
* Cache roles.
*/
export const getRolesCached = memoizer({
load: (db, callback) => {
db.getRoles()
.then(roles => {
callback(null, roles);
})
.catch(err => callback(err));
},
hash: (db) => db.hash || 'roles',
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
/*
* Cache groups.
*/
export const getGroupsCached = memoizer({
import _ from 'lodash';
import nconf from 'nconf';
import Promise from 'bluebird';
import memoizer from 'lru-memoizer';
import apiCall from './apiCall';
const compact = (entity) => ({
_id: entity._id,
name: entity.name,
description: entity.description
});
/*
* Cache connections.
*/
export const getConnectionsCached = memoizer({
load: (auth0, callback) => {
apiCall(auth0, auth0.connections.getAll, [ { fields: 'id,name,strategy' } ])
.then(connections => _.chain(connections)
.sortBy((conn) => conn.name.toLowerCase())
.value())
.then(connections => callback(null, connections))
.catch(err => callback(err));
},
hash: (auth0) => auth0.hash || 'connections',
max: 100,
maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});
/*
* Cache permissions.
*/
import ms from 'ms';
import Promise from 'bluebird';
import memoizer from 'lru-memoizer';
import request from 'request-promise';
import config from './config';
import logger from './logger';
let auth0 = require('auth0');
if (config('HOSTING_ENV') === 'webtask') {
auth0 = require('auth0@2.0.0');
}
const getAccessToken = Promise.promisify(
memoizer({
load: (domain, clientId, clientSecret, callback) => {
logger.debug(`Requesting access token for ${clientId} - https://${domain}/api/v2/`);
const options = {
uri: `https://${domain}/oauth/token`,
body: {
audience: `https://${domain}/api/v2/`,
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret
},
json: true
};
request.post(options)
.then((data) => callback(null, data.access_token))
request('POST', `https://${config('AUTH0_DOMAIN')}/oauth/token`)
.send(body)
.set('Content-Type', 'application/json')
.end((err, res) => {
if (err || !res.body || !res.body.access_token) {
logger.error(res.body);
return reject(err);
}
return resolve(res.body.access_token);
});
});
const getAuthorizationTokenCached = Promise.promisify(
memoizer({
load: (callback) => {
getAuthorizationToken()
.then(accessToken => callback(null, accessToken))
.catch(err => callback(err));
},
hash: () => 'auth0-authz-apiToken',
max: 100,
maxAge: 60 * 60000
}
));
export const getGroups = () =>
new Promise((resolve, reject) => {
getAuthorizationTokenCached()
.then((token) => {
if (!token) {