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,SAAS,EAAW,GAAgB,KAAI,WAC1C,EAAS,QAAQ,KACrB,IAEA,EAAS,QAAQ,MAErB,aAAa,aAAa,EAAS,MAC5B,CAIX,IAGG,EAAS,OACpB,CA9EoB,CAAc,EAAW,GAErC,OADA,QAAQ,SAAS,GACV,CACX,CAEA,SAAS,EAAY,GAOjB,OANA,EAAS,UACL,EAAY,WACZ,EAAY,SAGhB,GAAG,iBAAiB,EAAY,UAAU,KAAK,GACxC,IACX,CAEA,OAAO,EAAM,SAAS,GAAW,MAAK,SAAS,GAC3C,OAAK,EAGM,EAAY,GAFZ,GAIf,GAAG,EACP,CAjCmB,CAAuB,EAAS,EAAU,EACrD,GACJ,MACI,GAER,CAqFJ,CAEA,SAAS,SACL,OAAO,IAAI,MAAO,SACtB,CA/GA,OAAO,QAAU","file":"/npm/connect-response-cache-updated@1.0.0/lib/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/connect-response-cache-updated@1.0.0/lib/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","var fs = require(\"fs\");\nvar path = require(\"path\");\n\nvar q = require(\"q\");\nvar uuid = require(\"node-uuid\");\n\nvar workQueues = require(\"./work-queues\");\nvar interception = require(\"./interception\");\nvar caching = require(\"./caching\");\n\nmodule.exports = middleware;\n\nfunction middleware(options) {\n    var maxAge = options.maxAge;\n    var cachePath = options.cachePath;\n    if (!cachePath) {\n        throw new Error(\"options.cachePath must be set to cache directory\");\n    }\n    \n    var cache = caching.createCache(maxAge, options.backend);\n    var queues = workQueues.createQueueSet();\n    \n    function handle(request, response, next) {\n        if (request.method === \"GET\") {\n            var requestId = request._cacheId = request.headers[\"host\"] + request.url;\n            var queue = queues.get(requestId);\n            queue.add(function() {\n                return handleCacheableRequest(request, response, next);\n            });\n        } else {\n            next();\n        }\n    };\n    \n    function handleCacheableRequest(request, response, next) {\n        var requestId = request._cacheId;\n        \n        function handleNoEntry() {\n            var value = cacheResponse(requestId, response);\n            process.nextTick(next);\n            return value;\n        }\n        \n        function handleEntry(cachedValue) {\n            response.writeHead(\n                cachedValue.statusCode,\n                cachedValue.headers\n            );\n            \n            fs.createReadStream(cachedValue.bodyPath).pipe(response);\n            return null;\n        }\n        \n        return cache.getValue(requestId).then(function(cachedValue) {\n            if (!cachedValue) {\n                return handleNoEntry();\n            } else {\n                return handleEntry(cachedValue);\n            };\n        }, handleNoEntry);\n    }\n    \n    return handle;\n    \n    function cacheResponse(requestId, response) {\n        var deferred = q.defer();\n        \n        var cachedResponse = {\n            headers: {}\n        };\n        \n        var cachedBodyPath = path.join(cachePath, uuid.v4());\n        var cachedBodyStream = fs.createWriteStream(cachedBodyPath);\n        \n        interception.interceptMethods(response, {\n            \n            writeHead: function(statusCode, headers) {\n                for (var name in headers) {\n                    cachedResponse.headers[name] = headers[name];\n                }\n            },\n            \n            setHeader: function(name, value) {\n                cachedResponse.headers[name] = value;\n            },\n            \n            write: function(chunk, encoding) {\n                if (!Buffer.isBuffer(chunk)) {\n                    chunk = new Buffer(chunk, encoding);\n                }\n                cachedBodyStream.write(chunk);\n            },\n            \n            end: function(chunk, encoding) {\n                if (chunk) {\n                    response.write(chunk, encoding);\n                }\n                cachedBodyStream.end();\n                cachedResponse.statusCode = response.statusCode;\n                cachedResponse.bodyPath = cachedBodyPath;\n                if (response.statusCode < 500) {\n                    cache.setValue(requestId, cachedResponse).fin(function() {\n                        deferred.fulfill(null);\n                    });\n                } else {\n                    deferred.fulfill(null);\n                }\n                interception.callOriginal(response.end);\n                return false;\n                // TODO: at the moment, we don't cache errors at all.\n                // We should be more explicit about how we handle errors\n                // e.g. retry on every request, cache error (with different maxAge?)\n            }\n            \n        });\n        return deferred.promise;\n    }\n}\n\nfunction getNow() {\n    return new Date().getTime();\n}\n"]}