How to use http-cache-semantics - 10 common examples

To help you get started, we’ve selected a few http-cache-semantics examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github StoDevX / AAO-React-Native / modules / fetch / cached.js View on Github external
async function handleInitialFetch(args: {request: Request, key: string}) {
	let {request, key} = args

	debug && console.log(`fetch(${request.url}): no policy cached; fetching`)

	// I explicitly want errors here to propagate. Why? Bundled data will have
	// an expired policy stored, so it won't hit this branch. Thus, the only
	// requests in here will have nothing to fall back to, so we need some way
	// to signal that an error happened.
	let response = await fetch(request)

	let cachePolicyRequest = requestForCachePolicy(request)
	let cachePolicyResponse = responseForCachePolicy(response)

	let policy = new CachePolicy(cachePolicyRequest, cachePolicyResponse)

	if (policy.storable()) {
		debug && console.log(`fetch(${request.url}): caching`)
		await cacheItem({key, response, policy})
	} else {
		debug && console.log(`fetch(${request.url}): not cachable`)
	}

	return response
}
github superfly / fly / packages / v8env / src / cache.ts View on Github external
async match(req) {
    const hashed = hashData(req)
    const key = "httpcache:policy:" + hashed // first try with no vary variant
    for (let i = 0; i < 5; i++) {
      const policyRaw = await fly.cache.getString(key)
      logger.debug("Got policy:", key, policyRaw)
      if (!policyRaw) {
        return undefined
      }
      const policy = CachePolicy.fromObject(JSON.parse(policyRaw))

      // if it fits i sits
      if (policy.satisfiesWithoutRevalidation(req)) {
        const headers = policy.responseHeaders()
        const bodyKey = "httpcache:body:" + hashed

        const body = await fly.cache.get(bodyKey)
        logger.debug("Got body", body.constructor.name, body.byteLength)
        return new Response(body, { status: policy._status, headers })
        // }else if(policy._headers){
        // TODO: try a new vary based key
        // policy._headers has the varies / vary values
        // key = hashData(req, policy._headers)
        // return undefined
      } else {
        return undefined
github sx1989827 / DOClever / node_modules / cacheable-request / src / index.js View on Github external
.then(cacheEntry => {
					if (typeof cacheEntry === 'undefined') {
						return makeRequest(opts);
					}

					const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
					if (policy.satisfiesWithoutRevalidation(opts)) {
						const headers = policy.responseHeaders();
						const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
						response.cachePolicy = policy;
						response.fromCache = true;

						ee.emit('response', response);
						if (typeof cb === 'function') {
							cb(response);
						}
					} else {
						revalidate = cacheEntry;
						opts.headers = policy.revalidationHeaders(opts);
						makeRequest(opts);
					}
				});
github android-js / androidjs-builder / example / helloworld / node_modules / cacheable-request / src / index.js View on Github external
const get = async opts => {
					await Promise.resolve();

					const cacheEntry = opts.cache ? await this.cache.get(key) : undefined;
					if (typeof cacheEntry === 'undefined') {
						return makeRequest(opts);
					}

					const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
					if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
						const headers = policy.responseHeaders();
						const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
						response.cachePolicy = policy;
						response.fromCache = true;

						ee.emit('response', response);
						if (typeof cb === 'function') {
							cb(response);
						}
					} else {
						revalidate = cacheEntry;
						opts.headers = policy.revalidationHeaders(opts);
						makeRequest(opts);
					}
				};
github lukechilds / cacheable-request / src / index.js View on Github external
const get = async opts => {
					await Promise.resolve();

					const cacheEntry = opts.cache ? await this.cache.get(key) : undefined;
					if (typeof cacheEntry === 'undefined') {
						return makeRequest(opts);
					}

					const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
					if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
						const headers = policy.responseHeaders();
						const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
						response.cachePolicy = policy;
						response.fromCache = true;

						ee.emit('response', response);
						if (typeof cb === 'function') {
							cb(response);
						}
					} else {
						revalidate = cacheEntry;
						opts.headers = policy.revalidationHeaders(opts);
						makeRequest(opts);
					}
				};
github superfly / fly / src / bridge / cache.ts View on Github external
return function (request: any, callback: ivm.Reference) {
    const key = `${ctx.meta.get('app').id}:${request.url}`
    log.debug("cache match called! key:", key)
    const found = cache[key]
    log.debug("found:", found)
    if (!found)
      return callback.apply(null, [null, null])

    const { rawPolicy, response } = found
    let policy = CachePolicy.fromObject(rawPolicy)
    log.debug("satisfactory?", policy.satisfiesWithoutRevalidation(request))
    if (policy && policy.satisfiesWithoutRevalidation(request)) {
      response.headers = policy.responseHeaders();
      const body = response.body
      delete response.body
      return callback.apply(null, [null, new ivm.ExternalCopy(response).copyInto(), transferInto(body)]);
    }

    return callback.apply(null, [null, null])
  }
})
github htaussig / ProcProj / canvasSketch / future3D / node_modules / cacheable-request / src / index.js View on Github external
const handler = response => {
					if (revalidate && !opts.forceRefresh) {
						response.status = response.statusCode;
						const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
						if (!revalidatedPolicy.modified) {
							const headers = revalidatedPolicy.policy.responseHeaders();
							response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
							response.cachePolicy = revalidatedPolicy.policy;
							response.fromCache = true;
						}
					}

					if (!response.fromCache) {
						response.cachePolicy = new CachePolicy(opts, response, opts);
						response.fromCache = false;
					}

					let clonedResponse;
					if (opts.cache && response.cachePolicy.storable()) {
						clonedResponse = cloneResponse(response);
github StoDevX / AAO-React-Native / modules / fetch / cached.js View on Github external
async function getItem(key: string): Promise {
	let [[, response], [, policy]] = await AsyncStorage.multiGet([
		`${ROOT}:${key}:response`,
		`${ROOT}:${key}:policy`,
	])

	if (!response) {
		return {response, policy: null}
	}

	let {body, ...init} = JSON.parse(response)

	return {
		response: new Response(body, init),
		policy: CachePolicy.fromObject(JSON.parse(policy)),
	}
}
github lukechilds / cacheable-request / src / index.js View on Github external
const handler = response => {
					if (revalidate && !opts.forceRefresh) {
						response.status = response.statusCode;
						const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
						if (!revalidatedPolicy.modified) {
							const headers = revalidatedPolicy.policy.responseHeaders();
							response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
							response.cachePolicy = revalidatedPolicy.policy;
							response.fromCache = true;
						}
					}

					if (!response.fromCache) {
						response.cachePolicy = new CachePolicy(opts, response, opts);
						response.fromCache = false;
					}

					let clonedResponse;
					if (opts.cache && response.cachePolicy.storable()) {
						clonedResponse = cloneResponse(response);
github android-js / androidjs-builder / example / helloworld / node_modules / cacheable-request / src / index.js View on Github external
const handler = response => {
					if (revalidate && !opts.forceRefresh) {
						response.status = response.statusCode;
						const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
						if (!revalidatedPolicy.modified) {
							const headers = revalidatedPolicy.policy.responseHeaders();
							response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
							response.cachePolicy = revalidatedPolicy.policy;
							response.fromCache = true;
						}
					}

					if (!response.fromCache) {
						response.cachePolicy = new CachePolicy(opts, response, opts);
						response.fromCache = false;
					}

					let clonedResponse;
					if (opts.cache && response.cachePolicy.storable()) {
						clonedResponse = cloneResponse(response);

http-cache-semantics

Parses Cache-Control and other headers. Helps building correct HTTP caches and proxies

BSD-2-Clause
Latest version published 1 year ago

Package Health Score

67 / 100
Full package analysis

Popular http-cache-semantics functions

Similar packages