From 27fa676179c75278ca4b1949c09eb0a55de86da2 Mon Sep 17 00:00:00 2001 From: Bogdan Abaev Date: Tue, 6 Jun 2023 12:54:50 -0400 Subject: [PATCH] obeying cache control for remote styles --- lib/styles.js | 20 +++++++++++++++++++- package.json | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/styles.js b/lib/styles.js index 8859791..5c0120e 100644 --- a/lib/styles.js +++ b/lib/styles.js @@ -47,7 +47,8 @@ var jsdom = require('jsdom'); const { JSDOM } = jsdom; var log = require('npmlog'); var path = require('path'); - +const CachePolicy = require('http-cache-semantics'); +var cache = {}; /** * CslLoader constructor. Runs scanStyles synchronously on instantiation. * @param {Object} config config object. Should have at least 'cslPath' if not @@ -296,6 +297,15 @@ exports.CslLoader.prototype.fetchIndependentStyle = function(styleUrlObj){ styleUrlObj.headers = { 'User-Agent': cslLoader.config.userAgent }; + let cached = cache[styleUrlObj.href]; + const requestHeaders = { + 'user-agent': cslLoader.config.userAgent, + 'host': styleUrlObj.host || "" + }; + if (cached && cached.policy.satisfiesWithoutRevalidation({ headers: requestHeaders })) { + resolve(cached.body); + return; + } let req = httpGetter.request(styleUrlObj, function(response) { if(response.statusCode != 200){ log.error("non-200 status: " + response.statusCode); @@ -310,6 +320,14 @@ exports.CslLoader.prototype.fetchIndependentStyle = function(styleUrlObj){ cslXml += chunk; }); response.on('end', function(){ + response.req.headers = response.req.getHeaders(); + let policy = new CachePolicy(response.req, response); + if (policy.storable()) { + cache[styleUrlObj.href] = { + policy: policy, + body: cslXml, + }; + } //log.info(cslXml); resolve(cslXml); }); diff --git a/package.json b/package.json index d611eb7..ac1ce9f 100644 --- a/package.json +++ b/package.json @@ -12,12 +12,12 @@ }, "dependencies": { "config": "^3.0.0", + "http-cache-semantics": "^4.1.1", "jsdom": "^13.0.0", "npmlog": "^4.1.2", "optimist": ">=0.3.1", "underscore": "^1.9.1" }, - "devDependencies": {}, "scripts": { "start": "node lib/citeServer.js", "test": "node test/testallstyles.js"