apollo-datasource-http
Advanced tools
Comparing version 0.16.1 to 0.17.0
/// <reference types="node" /> | ||
import { DataSource, DataSourceConfig } from 'apollo-datasource'; | ||
import { Pool } from 'undici'; | ||
import { ResponseData } from 'undici/types/dispatcher'; | ||
import { HttpMethod, ResponseData } from 'undici/types/dispatcher'; | ||
import { EventEmitter } from 'stream'; | ||
@@ -17,3 +17,2 @@ import { Logger } from 'apollo-server-types'; | ||
requestCache?: { | ||
maxCacheTimeout: number; | ||
maxTtl: number; | ||
@@ -35,3 +34,3 @@ maxTtlIfError: number; | ||
path: string; | ||
method: string; | ||
method: HttpMethod; | ||
headers: Dictionary<string>; | ||
@@ -38,0 +37,0 @@ } & CacheTTLOptions; |
@@ -11,4 +11,2 @@ "use strict"; | ||
const quick_lru_1 = __importDefault(require("@alloc/quick-lru")); | ||
const p_timeout_1 = __importDefault(require("p-timeout")); | ||
const secure_json_parse_1 = __importDefault(require("secure-json-parse")); | ||
const apollo_server_errors_1 = require("apollo-server-errors"); | ||
@@ -133,3 +131,3 @@ const url_1 = require("url"); | ||
async performRequest(request, cacheKey) { | ||
var _a, _b, _c, _d, _e; | ||
var _a, _b, _c; | ||
try { | ||
@@ -152,13 +150,8 @@ if (request.body !== null && typeof request.body === 'object') { | ||
const responseData = await this.pool.request(requestOptions); | ||
responseData.body.setEncoding('utf8'); | ||
let data = ''; | ||
for await (const chunk of responseData.body) { | ||
data += chunk; | ||
let body = await responseData.body.text(); | ||
if (((_b = responseData.headers['content-type']) === null || _b === void 0 ? void 0 : _b.includes('application/json')) && | ||
body.length && | ||
typeof body === 'string') { | ||
body = JSON.parse(body); | ||
} | ||
let json = null; | ||
if ((_b = responseData.headers['content-type']) === null || _b === void 0 ? void 0 : _b.includes('application/json')) { | ||
if (data.length && typeof data === 'string') { | ||
json = secure_json_parse_1.default.parse(data); | ||
} | ||
} | ||
const response = { | ||
@@ -168,11 +161,11 @@ isFromCache: false, | ||
...responseData, | ||
body: json !== null && json !== void 0 ? json : data, | ||
body: body, | ||
}; | ||
this.onResponse(request, response); | ||
if (request.requestCache && this.isResponseCacheable(request, response)) { | ||
response.maxTtl = Math.max(request.requestCache.maxTtl, request.requestCache.maxTtlIfError); | ||
response.maxTtl = request.requestCache.maxTtl; | ||
const cachedResponse = JSON.stringify(response); | ||
this.cache | ||
.set(cacheKey, cachedResponse, { | ||
ttl: (_c = request.requestCache) === null || _c === void 0 ? void 0 : _c.maxTtl, | ||
ttl: request.requestCache.maxTtl, | ||
}) | ||
@@ -182,3 +175,3 @@ .catch((err) => { var _a; return (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error(err); }); | ||
.set(`staleIfError:${cacheKey}`, cachedResponse, { | ||
ttl: (_d = request.requestCache) === null || _d === void 0 ? void 0 : _d.maxTtlIfError, | ||
ttl: request.requestCache.maxTtl + request.requestCache.maxTtlIfError, | ||
}) | ||
@@ -190,7 +183,7 @@ .catch((err) => { var _a; return (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error(err); }); | ||
catch (error) { | ||
(_e = this.onError) === null || _e === void 0 ? void 0 : _e.call(this, error, request); | ||
(_c = this.onError) === null || _c === void 0 ? void 0 : _c.call(this, error, request); | ||
if (request.requestCache) { | ||
const cacheItem = await p_timeout_1.default(this.cache.get(`staleIfError:${cacheKey}`), request.requestCache.maxCacheTimeout); | ||
const cacheItem = await this.cache.get(`staleIfError:${cacheKey}`); | ||
if (cacheItem) { | ||
const response = secure_json_parse_1.default.parse(cacheItem); | ||
const response = JSON.parse(cacheItem); | ||
response.isFromCache = true; | ||
@@ -224,5 +217,5 @@ return response; | ||
try { | ||
const cacheItem = await p_timeout_1.default(this.cache.get(cacheKey), request.requestCache.maxCacheTimeout); | ||
const cacheItem = await this.cache.get(cacheKey); | ||
if (cacheItem) { | ||
const cachedResponse = secure_json_parse_1.default.parse(cacheItem); | ||
const cachedResponse = JSON.parse(cacheItem); | ||
cachedResponse.memoized = false; | ||
@@ -229,0 +222,0 @@ cachedResponse.isFromCache = true; |
{ | ||
"name": "apollo-datasource-http", | ||
"version": "0.16.1", | ||
"version": "0.17.0", | ||
"author": "Dustin Deus <deusdustin@gmail.com>", | ||
@@ -58,5 +58,3 @@ "license": "MIT", | ||
"graphql": "^15.5.1", | ||
"p-timeout": "^4.1.0", | ||
"secure-json-parse": "^2.4.0", | ||
"undici": "^4.1.0" | ||
"undici": "^4.4.2" | ||
}, | ||
@@ -70,10 +68,10 @@ "devDependencies": { | ||
"h2url": "^0.2.0", | ||
"nock": "^13.1.0", | ||
"nock": "^13.1.1", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.3.2", | ||
"release-it": "^14.10.0", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.3.4", | ||
"release-it": "^14.11.3", | ||
"ts-node": "^10.2.0", | ||
"typescript": "^4.3.5", | ||
"uid": "^2.0.0" | ||
} | ||
} |
@@ -112,5 +112,4 @@ # Apollo HTTP Data Source | ||
requestCache: { | ||
maxCacheTimeout: 50 // In case of the cache does not respond for any reason (ms). | ||
maxTtl: 1000 * 60 * 10, // 10min, will respond for 10min with the cached result (updated every 10min) | ||
maxTtlIfError: 1000 * 60 * 30, // 30min, will respond in an error case with the cached response (for further 20min) | ||
maxTtlIfError: 1000 * 60 * 30, // 30min, will respond with the cached response in case of an error (for further 20min) | ||
}, | ||
@@ -117,0 +116,0 @@ }) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7
32136
322
149
- Removedp-timeout@^4.1.0
- Removedsecure-json-parse@^2.4.0
- Removedp-timeout@4.1.0(transitive)
- Removedsecure-json-parse@2.7.0(transitive)
Updatedundici@^4.4.2