mirror of
https://github.com/a-sync/game-server-watcher.git
synced 2026-07-24 06:33:35 -04:00
include prod deps in vcs with some exceptions
* cloudno.de has problems installing new dependencies lately..
This commit is contained in:
21
node_modules/cacheable-request/LICENSE
generated
vendored
Normal file
21
node_modules/cacheable-request/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Luke Childs
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
52
node_modules/cacheable-request/node_modules/get-stream/buffer-stream.js
generated
vendored
Normal file
52
node_modules/cacheable-request/node_modules/get-stream/buffer-stream.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
const {PassThrough: PassThroughStream} = require('stream');
|
||||
|
||||
module.exports = options => {
|
||||
options = {...options};
|
||||
|
||||
const {array} = options;
|
||||
let {encoding} = options;
|
||||
const isBuffer = encoding === 'buffer';
|
||||
let objectMode = false;
|
||||
|
||||
if (array) {
|
||||
objectMode = !(encoding || isBuffer);
|
||||
} else {
|
||||
encoding = encoding || 'utf8';
|
||||
}
|
||||
|
||||
if (isBuffer) {
|
||||
encoding = null;
|
||||
}
|
||||
|
||||
const stream = new PassThroughStream({objectMode});
|
||||
|
||||
if (encoding) {
|
||||
stream.setEncoding(encoding);
|
||||
}
|
||||
|
||||
let length = 0;
|
||||
const chunks = [];
|
||||
|
||||
stream.on('data', chunk => {
|
||||
chunks.push(chunk);
|
||||
|
||||
if (objectMode) {
|
||||
length = chunks.length;
|
||||
} else {
|
||||
length += chunk.length;
|
||||
}
|
||||
});
|
||||
|
||||
stream.getBufferedValue = () => {
|
||||
if (array) {
|
||||
return chunks;
|
||||
}
|
||||
|
||||
return isBuffer ? Buffer.concat(chunks, length) : chunks.join('');
|
||||
};
|
||||
|
||||
stream.getBufferedLength = () => length;
|
||||
|
||||
return stream;
|
||||
};
|
||||
60
node_modules/cacheable-request/node_modules/get-stream/index.js
generated
vendored
Normal file
60
node_modules/cacheable-request/node_modules/get-stream/index.js
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
const {constants: BufferConstants} = require('buffer');
|
||||
const pump = require('pump');
|
||||
const bufferStream = require('./buffer-stream');
|
||||
|
||||
class MaxBufferError extends Error {
|
||||
constructor() {
|
||||
super('maxBuffer exceeded');
|
||||
this.name = 'MaxBufferError';
|
||||
}
|
||||
}
|
||||
|
||||
async function getStream(inputStream, options) {
|
||||
if (!inputStream) {
|
||||
return Promise.reject(new Error('Expected a stream'));
|
||||
}
|
||||
|
||||
options = {
|
||||
maxBuffer: Infinity,
|
||||
...options
|
||||
};
|
||||
|
||||
const {maxBuffer} = options;
|
||||
|
||||
let stream;
|
||||
await new Promise((resolve, reject) => {
|
||||
const rejectPromise = error => {
|
||||
// Don't retrieve an oversized buffer.
|
||||
if (error && stream.getBufferedLength() <= BufferConstants.MAX_LENGTH) {
|
||||
error.bufferedData = stream.getBufferedValue();
|
||||
}
|
||||
|
||||
reject(error);
|
||||
};
|
||||
|
||||
stream = pump(inputStream, bufferStream(options), error => {
|
||||
if (error) {
|
||||
rejectPromise(error);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
|
||||
stream.on('data', () => {
|
||||
if (stream.getBufferedLength() > maxBuffer) {
|
||||
rejectPromise(new MaxBufferError());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return stream.getBufferedValue();
|
||||
}
|
||||
|
||||
module.exports = getStream;
|
||||
// TODO: Remove this for the next major release
|
||||
module.exports.default = getStream;
|
||||
module.exports.buffer = (stream, options) => getStream(stream, {...options, encoding: 'buffer'});
|
||||
module.exports.array = (stream, options) => getStream(stream, {...options, array: true});
|
||||
module.exports.MaxBufferError = MaxBufferError;
|
||||
9
node_modules/cacheable-request/node_modules/get-stream/license
generated
vendored
Normal file
9
node_modules/cacheable-request/node_modules/get-stream/license
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
85
node_modules/cacheable-request/node_modules/get-stream/package.json
generated
vendored
Normal file
85
node_modules/cacheable-request/node_modules/get-stream/package.json
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"get-stream@5.2.0",
|
||||
"C:\\Users\\Smith\\repos\\game-server-watcher"
|
||||
]
|
||||
],
|
||||
"_from": "get-stream@5.2.0",
|
||||
"_id": "get-stream@5.2.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==",
|
||||
"_location": "/cacheable-request/get-stream",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "get-stream@5.2.0",
|
||||
"name": "get-stream",
|
||||
"escapedName": "get-stream",
|
||||
"rawSpec": "5.2.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "5.2.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/cacheable-request"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.2.0.tgz",
|
||||
"_spec": "5.2.0",
|
||||
"_where": "C:\\Users\\Smith\\repos\\game-server-watcher",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/get-stream/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"pump": "^3.0.0"
|
||||
},
|
||||
"description": "Get a stream as a string, buffer, or array",
|
||||
"devDependencies": {
|
||||
"@types/node": "^12.0.7",
|
||||
"ava": "^2.0.0",
|
||||
"into-stream": "^5.0.0",
|
||||
"tsd": "^0.7.2",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"buffer-stream.js"
|
||||
],
|
||||
"funding": "https://github.com/sponsors/sindresorhus",
|
||||
"homepage": "https://github.com/sindresorhus/get-stream#readme",
|
||||
"keywords": [
|
||||
"get",
|
||||
"stream",
|
||||
"promise",
|
||||
"concat",
|
||||
"string",
|
||||
"text",
|
||||
"buffer",
|
||||
"read",
|
||||
"data",
|
||||
"consume",
|
||||
"readable",
|
||||
"readablestream",
|
||||
"array",
|
||||
"object"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "get-stream",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sindresorhus/get-stream.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"version": "5.2.0"
|
||||
}
|
||||
10
node_modules/cacheable-request/node_modules/lowercase-keys/index.js
generated
vendored
Normal file
10
node_modules/cacheable-request/node_modules/lowercase-keys/index.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
module.exports = object => {
|
||||
const result = {};
|
||||
|
||||
for (const [key, value] of Object.entries(object)) {
|
||||
result[key.toLowerCase()] = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
9
node_modules/cacheable-request/node_modules/lowercase-keys/license
generated
vendored
Normal file
9
node_modules/cacheable-request/node_modules/lowercase-keys/license
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
73
node_modules/cacheable-request/node_modules/lowercase-keys/package.json
generated
vendored
Normal file
73
node_modules/cacheable-request/node_modules/lowercase-keys/package.json
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"lowercase-keys@2.0.0",
|
||||
"C:\\Users\\Smith\\repos\\game-server-watcher"
|
||||
]
|
||||
],
|
||||
"_from": "lowercase-keys@2.0.0",
|
||||
"_id": "lowercase-keys@2.0.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA==",
|
||||
"_location": "/cacheable-request/lowercase-keys",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "lowercase-keys@2.0.0",
|
||||
"name": "lowercase-keys",
|
||||
"escapedName": "lowercase-keys",
|
||||
"rawSpec": "2.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "2.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/cacheable-request"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz",
|
||||
"_spec": "2.0.0",
|
||||
"_where": "C:\\Users\\Smith\\repos\\game-server-watcher",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/lowercase-keys/issues"
|
||||
},
|
||||
"description": "Lowercase the keys of an object",
|
||||
"devDependencies": {
|
||||
"ava": "^1.4.1",
|
||||
"tsd": "^0.7.2",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"homepage": "https://github.com/sindresorhus/lowercase-keys#readme",
|
||||
"keywords": [
|
||||
"object",
|
||||
"assign",
|
||||
"extend",
|
||||
"properties",
|
||||
"lowercase",
|
||||
"lower-case",
|
||||
"case",
|
||||
"keys",
|
||||
"key"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "lowercase-keys",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sindresorhus/lowercase-keys.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"version": "2.0.0"
|
||||
}
|
||||
98
node_modules/cacheable-request/package.json
generated
vendored
Normal file
98
node_modules/cacheable-request/package.json
generated
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"cacheable-request@6.1.0",
|
||||
"C:\\Users\\Smith\\repos\\game-server-watcher"
|
||||
]
|
||||
],
|
||||
"_from": "cacheable-request@6.1.0",
|
||||
"_id": "cacheable-request@6.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-Oj3cAGPCqOZX7Rz64Uny2GYAZNliQSqfbePrgAQ1wKAihYmCUnraBtJtKcGR4xz7wF+LoJC+ssFZvv5BgF9Igg==",
|
||||
"_location": "/cacheable-request",
|
||||
"_phantomChildren": {
|
||||
"pump": "3.0.0"
|
||||
},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "cacheable-request@6.1.0",
|
||||
"name": "cacheable-request",
|
||||
"escapedName": "cacheable-request",
|
||||
"rawSpec": "6.1.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "6.1.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/gamedig/got",
|
||||
"/package-json/got"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-6.1.0.tgz",
|
||||
"_spec": "6.1.0",
|
||||
"_where": "C:\\Users\\Smith\\repos\\game-server-watcher",
|
||||
"author": {
|
||||
"name": "Luke Childs",
|
||||
"email": "lukechilds123@gmail.com",
|
||||
"url": "http://lukechilds.co.uk"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/lukechilds/cacheable-request/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"clone-response": "^1.0.2",
|
||||
"get-stream": "^5.1.0",
|
||||
"http-cache-semantics": "^4.0.0",
|
||||
"keyv": "^3.0.0",
|
||||
"lowercase-keys": "^2.0.0",
|
||||
"normalize-url": "^4.1.0",
|
||||
"responselike": "^1.0.2"
|
||||
},
|
||||
"description": "Wrap native HTTP requests with RFC compliant cache support",
|
||||
"devDependencies": {
|
||||
"@keyv/sqlite": "^2.0.0",
|
||||
"ava": "^1.1.0",
|
||||
"coveralls": "^3.0.0",
|
||||
"create-test-server": "3.0.0",
|
||||
"delay": "^4.0.0",
|
||||
"eslint-config-xo-lukechilds": "^1.0.0",
|
||||
"nyc": "^14.1.1",
|
||||
"pify": "^4.0.0",
|
||||
"sqlite3": "^4.0.2",
|
||||
"this": "^1.0.2",
|
||||
"xo": "^0.23.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"files": [
|
||||
"src"
|
||||
],
|
||||
"homepage": "https://github.com/lukechilds/cacheable-request#readme",
|
||||
"keywords": [
|
||||
"HTTP",
|
||||
"HTTPS",
|
||||
"cache",
|
||||
"caching",
|
||||
"layer",
|
||||
"cacheable",
|
||||
"RFC 7234",
|
||||
"RFC",
|
||||
"7234",
|
||||
"compliant"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "src/index.js",
|
||||
"name": "cacheable-request",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/lukechilds/cacheable-request.git"
|
||||
},
|
||||
"scripts": {
|
||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||
"test": "xo && nyc ava"
|
||||
},
|
||||
"version": "6.1.0",
|
||||
"xo": {
|
||||
"extends": "xo-lukechilds"
|
||||
}
|
||||
}
|
||||
251
node_modules/cacheable-request/src/index.js
generated
vendored
Normal file
251
node_modules/cacheable-request/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,251 @@
|
||||
'use strict';
|
||||
|
||||
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 Response = require('responselike');
|
||||
const lowercaseKeys = require('lowercase-keys');
|
||||
const cloneResponse = require('clone-response');
|
||||
const Keyv = require('keyv');
|
||||
|
||||
class CacheableRequest {
|
||||
constructor(request, cacheAdapter) {
|
||||
if (typeof request !== 'function') {
|
||||
throw new TypeError('Parameter `request` must be a function');
|
||||
}
|
||||
|
||||
this.cache = new Keyv({
|
||||
uri: typeof cacheAdapter === 'string' && cacheAdapter,
|
||||
store: typeof cacheAdapter !== 'string' && cacheAdapter,
|
||||
namespace: 'cacheable-request'
|
||||
});
|
||||
|
||||
return this.createCacheableRequest(request);
|
||||
}
|
||||
|
||||
createCacheableRequest(request) {
|
||||
return (opts, cb) => {
|
||||
let url;
|
||||
if (typeof opts === 'string') {
|
||||
url = normalizeUrlObject(urlLib.parse(opts));
|
||||
opts = {};
|
||||
} else if (opts instanceof urlLib.URL) {
|
||||
url = normalizeUrlObject(urlLib.parse(opts.toString()));
|
||||
opts = {};
|
||||
} else {
|
||||
const [pathname, ...searchParts] = (opts.path || '').split('?');
|
||||
const search = searchParts.length > 0 ?
|
||||
`?${searchParts.join('?')}` :
|
||||
'';
|
||||
url = normalizeUrlObject({ ...opts, pathname, search });
|
||||
}
|
||||
|
||||
opts = {
|
||||
headers: {},
|
||||
method: 'GET',
|
||||
cache: true,
|
||||
strictTtl: false,
|
||||
automaticFailover: false,
|
||||
...opts,
|
||||
...urlObjectToRequestOptions(url)
|
||||
};
|
||||
opts.headers = lowercaseKeys(opts.headers);
|
||||
|
||||
const ee = new EventEmitter();
|
||||
const normalizedUrlString = normalizeUrl(
|
||||
urlLib.format(url),
|
||||
{
|
||||
stripWWW: false,
|
||||
removeTrailingSlash: false,
|
||||
stripAuthentication: false
|
||||
}
|
||||
);
|
||||
const key = `${opts.method}:${normalizedUrlString}`;
|
||||
let revalidate = false;
|
||||
let madeRequest = false;
|
||||
|
||||
const makeRequest = opts => {
|
||||
madeRequest = true;
|
||||
let requestErrored = false;
|
||||
let requestErrorCallback;
|
||||
|
||||
const requestErrorPromise = new Promise(resolve => {
|
||||
requestErrorCallback = () => {
|
||||
if (!requestErrored) {
|
||||
requestErrored = true;
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const bodyPromise = getStream.buffer(response);
|
||||
|
||||
await Promise.race([
|
||||
requestErrorPromise,
|
||||
new Promise(resolve => response.once('end', resolve))
|
||||
]);
|
||||
|
||||
if (requestErrored) {
|
||||
return;
|
||||
}
|
||||
|
||||
const body = await bodyPromise;
|
||||
|
||||
const value = {
|
||||
cachePolicy: response.cachePolicy.toObject(),
|
||||
url: response.url,
|
||||
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
|
||||
body
|
||||
};
|
||||
|
||||
let ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
|
||||
if (opts.maxTtl) {
|
||||
ttl = ttl ? Math.min(ttl, opts.maxTtl) : opts.maxTtl;
|
||||
}
|
||||
|
||||
await this.cache.set(key, value, ttl);
|
||||
} catch (error) {
|
||||
ee.emit('error', new CacheableRequest.CacheError(error));
|
||||
}
|
||||
})();
|
||||
} else if (opts.cache && revalidate) {
|
||||
(async () => {
|
||||
try {
|
||||
await this.cache.delete(key);
|
||||
} catch (error) {
|
||||
ee.emit('error', new CacheableRequest.CacheError(error));
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
ee.emit('response', clonedResponse || response);
|
||||
if (typeof cb === 'function') {
|
||||
cb(clonedResponse || response);
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const req = request(opts, handler);
|
||||
req.once('error', requestErrorCallback);
|
||||
req.once('abort', requestErrorCallback);
|
||||
ee.emit('request', req);
|
||||
} catch (error) {
|
||||
ee.emit('error', new CacheableRequest.RequestError(error));
|
||||
}
|
||||
};
|
||||
|
||||
(async () => {
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
const errorHandler = error => ee.emit('error', new CacheableRequest.CacheError(error));
|
||||
this.cache.once('error', errorHandler);
|
||||
ee.on('response', () => this.cache.removeListener('error', errorHandler));
|
||||
|
||||
try {
|
||||
await get(opts);
|
||||
} catch (error) {
|
||||
if (opts.automaticFailover && !madeRequest) {
|
||||
makeRequest(opts);
|
||||
}
|
||||
|
||||
ee.emit('error', new CacheableRequest.CacheError(error));
|
||||
}
|
||||
})();
|
||||
|
||||
return ee;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function urlObjectToRequestOptions(url) {
|
||||
const options = { ...url };
|
||||
options.path = `${url.pathname || '/'}${url.search || ''}`;
|
||||
delete options.pathname;
|
||||
delete options.search;
|
||||
return options;
|
||||
}
|
||||
|
||||
function normalizeUrlObject(url) {
|
||||
// If url was parsed by url.parse or new URL:
|
||||
// - hostname will be set
|
||||
// - host will be hostname[:port]
|
||||
// - port will be set if it was explicit in the parsed string
|
||||
// Otherwise, url was from request options:
|
||||
// - hostname or host may be set
|
||||
// - host shall not have port encoded
|
||||
return {
|
||||
protocol: url.protocol,
|
||||
auth: url.auth,
|
||||
hostname: url.hostname || url.host || 'localhost',
|
||||
port: url.port,
|
||||
pathname: url.pathname,
|
||||
search: url.search
|
||||
};
|
||||
}
|
||||
|
||||
CacheableRequest.RequestError = class extends Error {
|
||||
constructor(error) {
|
||||
super(error.message);
|
||||
this.name = 'RequestError';
|
||||
Object.assign(this, error);
|
||||
}
|
||||
};
|
||||
|
||||
CacheableRequest.CacheError = class extends Error {
|
||||
constructor(error) {
|
||||
super(error.message);
|
||||
this.name = 'CacheError';
|
||||
Object.assign(this, error);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = CacheableRequest;
|
||||
Reference in New Issue
Block a user