How to use configCache.get method in ava

Best JavaScript code snippet using ava

index.mjs

Source:index.mjs Github

copy

Full Screen

1process.env.GOOGLE_CLOUD_PROJECT = "endpointservice";2import {default as express} from 'express';3import {default as admin} from 'firebase-admin';4import {default as bodyParser} from 'body-parser';5import {SecretManagerServiceClient} from '@google-cloud/secret-manager';6import cors from 'cors';7import * as routes from './routes.mjs';8import * as observable from './observable.mjs';9import * as useragent from './useragent.mjs';10import * as configcache from './configcache.mjs';11import * as browsercache from './browsercache.mjs';12import * as billing from './billing.mjs';13import * as livecode from './livecode.mjs';14import {promiseRecursive} from './utils.mjs';15import {loopbreak} from './loopbreak.mjs';16import {Logger} from './logging.mjs';17import {default as compression} from 'compression';18import {puppeteerProxy} from './puppeteer.mjs';19import * as _ from 'lodash-es';20import createError from "http-errors";21import {installRtdbRedirect} from './rtdb.mjs';22import {default as proxy} from 'express-http-proxy';23const firebase = admin.initializeApp({24 apiKey: "AIzaSyD882c8YEgeYpNkX01fhpUDfioWl_ETQyQ",25 authDomain: "endpointservice.firebaseapp.com",26 projectId: "endpointservice",27 databaseURL: "https://endpointservice-eu.europe-west1.firebasedatabase.app/"28});29const users = admin.initializeApp({30 apiKey: "AIzaSyBquSsEgQnG_rHyasUA95xHN5INnvnh3gc",31 authDomain: "endpointserviceusers.firebaseapp.com",32 projectId: "endpointserviceusers",33 appId: "1:283622646315:web:baa488124636283783006e",34}, 'users');35configcache.setCacheFirebase(firebase);36livecode.setLivecodeFirebase(firebase)37billing.setBillingFirebase(firebase);38const secretsClient = new SecretManagerServiceClient({39 projectId: "endpointservice"40});41export const app = express();42app.use(cors({43 origin: true // Use origin.header44}));45app.use(bodyParser.raw({type: '*/*', limit: '50mb'})); // This means we buffer the body, really we should move towards streaming46app.use(compression());47// RATE LIMITERS48import {checkRate, OBSERVABLE_RATE_LIMIT, requestLimiter} from './limits.mjs';49// Periodic tasks50browsercache.scheduleCleanup();51// Cross cache tasks52configcache.setInvalidationCallback((namespace, endpointURL) => {53 browsercache.invalidate(namespace, endpointURL);54});55const localmode = process.env.LOCAL || false;56// Start loading secrets ASAP in the background57async function lookupSecret(key) {58 // Access the secret.59 const [accessResponse] = await secretsClient.accessSecretVersion({60 name: `projects/1986724398/secrets/${key}/versions/latest`,61 });62 const responsePayload = accessResponse.payload.data.toString('utf8');63 return responsePayload;64}65// Legacy route, forward to other handler66app.all(routes.pattern, async (req, res) => {67 const {68 shard,69 userURL,70 deploy,71 } = routes.decode(req);72 req.url = `/observablehq.com/${shard};${deploy}${userURL}`;73 app.handle(req, res);74});75// Adapter to make Realtime Database requests get handled76installRtdbRedirect(app);77// Handler for observablehq.com notebooks78const responses = [];79app.all(observable.pattern, [80 async (req, res, next) => { 81 req.id = Math.random().toString(36).substr(2, 9); 82 req.requestConfig = observable.decode(req);83 req.cachedConfig = await configcache.get(req.requestConfig.endpointURL);84 next()85 },86 loopbreak,87 requestLimiter,88 async (req, res, next) => { 89 if (req.cachedConfig) {90 req.pendingSecrets = (req.cachedConfig.secrets || []).reduce(91 (acc, key) => {92 acc[key] = lookupSecret(key);93 return acc;94 },95 {}96 );97 }98 next()99 },100 livecode.livecodeMiddleware,101 async (req, res, next) => { 102 let page = null;103 const throwError = (status, message) => {104 const err = new Error(message);105 err.status = status;106 throw err;107 }108 // Default no cache in CDN109 res.header('Cache-Control', 'no-store');110 const logger = new Logger();111 const t_start = Date.now();112 const notebookURL = observable.notebookURL(req, req.cachedConfig);113 const shard = req.cachedConfig?.namespace || req.requestConfig.namespace || notebookURL;114 const releasePage = async () => {115 if (page) delete page.requests[req.id];116 if (page && !localmode && !page.isClosed()) {117 if (!req?.cachedConfig?.reusable) await page.close();118 page = undefined;119 if (shard === notebookURL) {120 // can't close might be used by a parrallel request121 // console.log(`Closing browser on shard ${shard}`);122 // We used a notebookURL shard so kill the browser too as we know the true namespace now123 // await (await browsers[shard]).close();124 }125 }126 }127 try {128 responses[req.id] = res;129 let pageReused = false;130 if (!req.cachedConfig || !req.cachedConfig.reusable) {131 page = await browsercache.newPage(shard, ['--proxy-server=127.0.0.1:8888']);132 // Tidy page on cancelled request133 req.on('close', releasePage);134 } else {135 page = await browsercache.newPage(shard, ['--proxy-server=127.0.0.1:8888'], notebookURL);136 pageReused = page.setup !== undefined;137 if (!page.setup) page.setup = true;138 }139 page.requests[req.id] = true; // indicate we are running a request on this page140 if (req.cachedConfig) {141 await page.setUserAgent(useragent.encode({142 terminal: req.cachedConfig.modifiers.includes("terminal"),143 orchestrator: req.cachedConfig.modifiers.includes("orchestrator")144 }));145 }146 147 if (!pageReused) {148 try {149 await checkRate(shard, OBSERVABLE_RATE_LIMIT);150 } catch (err) {151 throw createError(429, "Shared OBSERVABLE_RATE_LIMIT exceeded, try the resuable flag");152 }153 await page.evaluateOnNewDocument((notebook) => {154 window["@endpointservices.context"] = {155 serverless: true,156 notebook: notebook,157 secrets: {}158 };159 }, req.requestConfig.notebook);160 // Wire up logging161 page.on('console', message => logger.log(`${message.type().substr(0, 3).toUpperCase()} ${message.text()}`))162 .on('pageerror', ({ message }) => logger.log(message))163 .on('requestfailed', request => logger.log(`${request.failure().errorText} ${request.url()}`));164 165 // Wire up response channel166 await page.exposeFunction(167 '@endpointservices.callback',168 (reqId, operation, args) => {169 if (!responses[reqId]) {170 console.error("No response found for reqId: " + reqId);171 return new Error("No response found");172 } else if (operation === 'header') {173 responses[reqId].header(args[0], args[1]);174 } else if (operation === 'status') {175 responses[reqId].status(args[0]);176 } else if (operation === 'write') {177 return new Promise((resolve, reject) => {178 let chunk = args[0];179 if (chunk.ARuRQygChDsaTvPRztEb === "bufferBase64") {180 chunk = Buffer.from(chunk.value, 'base64')181 } 182 responses[reqId].write(chunk, (err) => err ? reject(err): resolve())183 })184 };185 }186 );187 console.log(`Fetching: ${notebookURL}`);188 const pageResult = await page.goto(notebookURL, { waitUntil: 'domcontentloaded' });189 if (!pageResult.ok()) {190 browsercache.invalidatePage(shard, notebookURL)191 res.status(pageResult.status()).send(pageResult.statusText());192 return;193 }194 if (pageResult.headers()['login']) {195 page.namespace = pageResult.headers()['login'];196 if (!billing.isPro(page.namespace)) {197 return res.status(402).send(`A PRO subscription is required to use private source code endpoints. Upgrade at https://webcode.run`);198 }199 };200 }201 function waitForFrame() {202 let fulfill;203 const promise = new Promise(x => fulfill = x);204 checkFrame();205 return promise;206 207 function checkFrame() {208 const frame = page.frames().find(iframe => {209 // console.log("iframe.url()" + iframe.url())210 return iframe.url().includes('observableusercontent')211 });212 if (frame)213 fulfill(frame);214 else215 setTimeout(checkFrame, 25)216 }217 }218 let namespace; 219 let executionContext;220 if (page.namespace) {221 namespace = page.namespace;222 executionContext = page;223 } else {224 executionContext = await waitForFrame();225 // iframe.url()226 // e.g. https://tomlarkworthy.static.observableusercontent.com/worker/embedworker.7fea46af439a70e4d3d6c96e0dfa09953c430187dd07bc9aa6b9050a6691721a.html?cell=buggy_rem227 namespace = executionContext.url().match(/^https:\/\/([^.]*)/)[1];228 }229 if (!pageReused) {230 logger.initialize({231 project_id: process.env.GOOGLE_CLOUD_PROJECT,232 location: undefined,233 namespace,234 notebook: req.requestConfig.notebook,235 job: req.requestConfig.endpointURL,236 task_id: req.id237 });238 }239 240 const deploymentHandle = await executionContext.waitForFunction(241 (name) => window["deployments"] && window["deployments"][name],242 {243 timeout: 20000244 }, req.requestConfig.name);245 const deploymentConfig = await executionContext.evaluate(x => {246 return x.config;247 }, deploymentHandle248 ); 249 250 // Update cache so we always have latest251 await configcache.setNotebook(req.requestConfig.endpointURL, {252 modifiers: deploymentConfig.modifiers || [],253 secrets: deploymentConfig.secrets || [],254 reusable: deploymentConfig.reusable || false,255 namespace256 });257 // This will be cached, and drive restart so be very carful as if it is not stable258 // we will have loops259 req.config = await configcache.get(req.requestConfig.endpointURL);260 // Now we decide to restart or not261 // If the modifiers change we just need to rerun the loopbreaking logic262 if (req.cachedConfig === undefined || 263 !_.isEqual(req.cachedConfig.modifiers, req.config.modifiers)) {264 let nextCalled = false;265 loopbreak(req, res, () => nextCalled = true)266 if (!nextCalled) {267 return;268 }269 }270 // The namespace can change (we just ran the code in the wrong browser shard but functionally 271 // for the user it should not matter)272 // Anything else though (only SECRETs at present) should restart.273 const remainingCachedConfig = {...(req.cachedConfig || {274 secrets: []275 }), modifiers: undefined, namespace: undefined, reusable: undefined};276 const remainingConfig = {...req.config,277 modifiers: undefined, namespace: undefined, reusable: undefined};278 279 if (!_.isEqual(remainingConfig, remainingCachedConfig)) {280 console.log("Config change, rerequesting");281 return app.handle(req, res); // Do the whole thing again282 };283 // SECURITY: Now we ensure all the secrets resolve and they are keyed by the domain being executed284 Object.keys(req.pendingSecrets || {}).map(key => {285 if (!key.startsWith(namespace)) {286 throwError(403, `Notebooks by ${namespace} cannot access ${key}`)287 }288 });289 290 // Resolve all outstanding secret fetches291 const secrets = await promiseRecursive(req.pendingSecrets || {});292 // ergonomics improvement, strip namespace_ prefix of all secrets293 Object.keys(secrets).forEach(294 secretName => secrets[secretName.replace(`${namespace}_`, '')] = secrets[secretName]);295 // mixin api_key to secrets296 if (req.config.api_key) secrets.api_key = req.config.api_key;297 // Resolve all the promises298 const context = {299 serverless: true,300 namespace,301 notebook: req.requestConfig.notebook,302 secrets: secrets303 };304 const cellReq = observable.createCellRequest(req);305 const result = await executionContext.evaluate(306 (req, name, context) => window["deployments"][name](req, context),307 cellReq, req.requestConfig.name, context308 ); 309 310 releasePage();311 const millis = Date.now() - t_start;312 313 result.json ? res.json(result.json) : null;314 if (result.send) {315 if (result.send.ARuRQygChDsaTvPRztEb === "bufferBase64") {316 res.send(Buffer.from(result.send.value, 'base64'))317 } else {318 res.send(result.send)319 }320 }321 result.end ? res.end() : null;322 logger.log({323 url: req.url,324 method: req.method,325 status: 200 || result.status,326 duration: millis327 });328 } catch (err) {329 if (err.message.startsWith("waiting for function failed")) {330 err.message = `Deployment '${req.requestConfig.name}' not found, did you remember to publish your notebook, or is your deploy function slow?`331 err.status = 404;332 } else if (err.message.endsWith("Most likely the page has been closed.")) {333 err.message = "Stale cache, should not happen"334 err.status = 500;335 browsercache.invalidatePage(shard, notebookURL)336 } else if (!err.status) {337 err.status = err.status || 500; // Default to 500 error code338 }339 console.error(err);340 releasePage()341 const millis = Date.now() - t_start;342 logger.log({343 url: req.url,344 method: req.method,345 status: err.status,346 duration: millis347 });348 res.status(err.status).send(err.message);349 } finally {350 delete responses[req.id];351 }352 }353]);354app.use('(/regions/:region)?/puppeteer', async (req, res, next) => {355 try {356 const decoded = await users.auth().verifyIdToken(req.query.token)357 console.log("puppeteer user", decoded.uid);358 next();359 } catch (err) {360 console.error(err);361 res.status(403).send(err.message)362 }363});364app.use('(/regions/:region)?/puppeteer', puppeteerProxy);365app.use('(/regions/:region)?/.stats', browsercache.statsHandler);366app.use(proxy('https://loving-leakey-0c4e88.netlify.app', {367 userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {368 headers['Cache-Control'] = 'public, max-age=3600';369 return headers;370 }371}));372app.server = app.listen(process.env.PORT || 8080);373 374export const shutdown = async () => {375 console.log("Shutting down...")376 app.server.close();377 await browsercache.shutdown();...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var availableCache = require('available-cache');2var configCache = availableCache.createCache('configCache');3var config = configCache.get('config');4var availableCache = require('available-cache');5var configCache = availableCache.createCache('configCache');6configCache.set('config', {name: 'available-cache', version: '1.0.0'});7### createCache(cacheName, [options])8Options to pass to the Redis client. Only applicable when `storage` is set to `redis`. See the [`redis` package](

Full Screen

Using AI Code Generation

copy

Full Screen

1console.log(configCache.get('db.host'));2console.log(configCache.get('db.port'));3console.log(configCache.get('db.host'));4console.log(configCache.get('db.port'));5console.log(configCache.get('db.host'));6console.log(configCache.get('db.port'));7console.log(configCache.get('db.host'));8console.log(configCache.get('db.port'));9console.log(configCache.get('db.host'));10console.log(configCache.get('db.port'));11console.log(configCache.get('db.host'));12console.log(configCache.get('db.port'));13console.log(configCache.get('db.host'));14console.log(configCache.get('db.port'));15console.log(configCache.get('db.host'));16console.log(configCache.get('db.port'));17console.log(configCache.get('db.host'));18console.log(configCache.get('db.port'));19console.log(configCache.get('db.host'));20console.log(configCache.get('db.port'));21console.log(configCache.get('db.host'));22console.log(configCache.get('db.port'));

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ava automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful