Socket
Socket
Sign inDemoInstall

cacheable-request

Package Overview
Dependencies
12
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.0 to 0.1.0

27

package.json
{
"name": "cacheable-request",
"version": "0.0.0",
"version": "0.1.0",
"description": "Wrap native HTTP requests with RFC compliant cache support",

@@ -18,8 +18,12 @@ "main": "src/index.js",

"keywords": [
"async",
"asynchronous",
"non-blocking",
"base64",
"encode",
"decode"
"HTTP",
"HTTPS",
"cache",
"caching",
"layer",
"cacheable",
"RFC 7234",
"RFC",
"7234",
"compliant"
],

@@ -32,3 +36,10 @@ "author": "Luke Childs <lukechilds123@gmail.com> (http://lukechilds.co.uk)",

"homepage": "https://github.com/lukechilds/cacheable-request",
"dependencies": {},
"dependencies": {
"get-stream": "^3.0.0",
"http-cache-semantics": "^3.7.3",
"lowercase-keys": "^1.0.0",
"normalize-url": "^1.9.1",
"responselike": "^0.1.0",
"url-parse-lax": "^1.0.0"
},
"devDependencies": {

@@ -35,0 +46,0 @@ "ava": "^0.19.1",

'use strict';
const cacheableRequest = () => {
const EventEmitter = require('events');
const urlLib = require('url');
const normalizeUrl = require('normalize-url');
const getStream = require('get-stream');
const CachePolicy = require('http-cache-semantics');
const urlParseLax = require('url-parse-lax');
const Response = require('responselike');
const lowercaseKeys = require('lowercase-keys');
const cacheKey = opts => {
const url = normalizeUrl(urlLib.format(opts));
return `${opts.method}:${url}`;
};
const cacheableRequest = (request, cache) => (opts, cb) => {
if (typeof opts === 'string') {
opts = urlParseLax(opts);
}
opts = Object.assign({
headers: {},
method: 'GET'
}, opts);
opts.headers = lowercaseKeys(opts.headers);
const ee = new EventEmitter();
const key = cacheKey(opts);
const makeRequest = opts => {
const req = request(opts, response => {
if (opts._revalidate) {
const revalidatedPolicy = CachePolicy.fromObject(opts._revalidate.cachePolicy).revalidatedPolicy(opts, response);
if (!revalidatedPolicy.modified) {
const headers = revalidatedPolicy.policy.responseHeaders();
response = new Response(opts._revalidate.statusCode, headers, opts._revalidate.body, opts._revalidate.url);
response.cachePolicy = revalidatedPolicy.policy;
response.fromCache = true;
}
}
if (!response.fromCache) {
response.cachePolicy = new CachePolicy(opts, response);
response.fromCache = false;
}
if (cache && response.cachePolicy.storable()) {
getStream.buffer(response).then(body => {
const value = {
cachePolicy: response.cachePolicy.toObject(),
url: response.url,
statusCode: response.statusCode,
body
};
const ttl = response.cachePolicy.timeToLive();
cache.set(key, value, ttl);
});
}
if (typeof cb === 'function') {
cb(response);
}
});
ee.emit('request', req);
};
const get = opts => Promise.resolve(cache.get(key)).then(cacheEntry => {
if (typeof cacheEntry === 'undefined') {
return makeRequest(opts, cb);
}
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;
if (typeof cb === 'function') {
cb(response);
}
} else {
opts._revalidate = cacheEntry;
opts.headers = policy.revalidationHeaders(opts);
makeRequest(opts);
}
});
get(opts);
return ee;
};
module.exports = cacheableRequest;

@@ -0,1 +1,2 @@

import { request } from 'http';
import test from 'ava';

@@ -7,1 +8,5 @@ import cacheableRequest from '../';

});
test('cacheableRequest(request) returns a function', t => {
t.is(typeof cacheableRequest(request), 'function');
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc