cacheable-request
Advanced tools
Comparing version 0.0.0 to 0.1.0
{ | ||
"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'); | ||
}); |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
6998
89
6
2
+ Addedget-stream@^3.0.0
+ Addedhttp-cache-semantics@^3.7.3
+ Addedlowercase-keys@^1.0.0
+ Addednormalize-url@^1.9.1
+ Addedresponselike@^0.1.0
+ Addedurl-parse-lax@^1.0.0
+ Addedget-stream@3.0.0(transitive)
+ Addedhttp-cache-semantics@3.8.1(transitive)
+ Addedis-plain-obj@1.1.0(transitive)
+ Addedlowercase-keys@1.0.1(transitive)
+ Addednormalize-url@1.9.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedprepend-http@1.0.4(transitive)
+ Addedquery-string@4.3.4(transitive)
+ Addedresponselike@0.1.0(transitive)
+ Addedsort-keys@1.1.2(transitive)
+ Addedstrict-uri-encode@1.1.0(transitive)
+ Addedurl-parse-lax@1.0.0(transitive)