@envelop/response-cache 2.3.2-alpha-763ffe6.0 → 2.3.3-alpha-60f5d29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +77 -81
- package/index.mjs +77 -81
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -124,7 +124,7 @@ const hashSHA256 = async (text) => {
|
|
|
124
124
|
if (nodeCrypto) {
|
|
125
125
|
return nodeCrypto.then(crypto => crypto.createHash('sha256').update(text).digest('hex'));
|
|
126
126
|
}
|
|
127
|
-
|
|
127
|
+
if (webCrypto) {
|
|
128
128
|
let buf;
|
|
129
129
|
if (typeof TextEncoder !== 'undefined') {
|
|
130
130
|
buf = new TextEncoder().encode(text);
|
|
@@ -258,102 +258,98 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
258
258
|
},
|
|
259
259
|
};
|
|
260
260
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
hit: true,
|
|
279
|
-
},
|
|
261
|
+
const documentString = getDocumentStringFromContext(ctx.args.contextValue);
|
|
262
|
+
if (documentString != null) {
|
|
263
|
+
const operationId = await buildResponseCacheKey({
|
|
264
|
+
documentString,
|
|
265
|
+
variableValues: ctx.args.variableValues,
|
|
266
|
+
operationName: ctx.args.operationName,
|
|
267
|
+
sessionId: session(ctx.args.contextValue),
|
|
268
|
+
});
|
|
269
|
+
if (((_a = enabled === null || enabled === void 0 ? void 0 : enabled(ctx.args.contextValue)) !== null && _a !== void 0 ? _a : true) === true) {
|
|
270
|
+
const cachedResponse = await cache.get(operationId);
|
|
271
|
+
if (cachedResponse != null) {
|
|
272
|
+
if (includeExtensionMetadata) {
|
|
273
|
+
ctx.setResultAndStopExecution({
|
|
274
|
+
...cachedResponse,
|
|
275
|
+
extensions: {
|
|
276
|
+
responseCache: {
|
|
277
|
+
hit: true,
|
|
280
278
|
},
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
ctx.setResultAndStopExecution(cachedResponse);
|
|
285
|
-
}
|
|
286
|
-
return;
|
|
279
|
+
},
|
|
280
|
+
});
|
|
287
281
|
}
|
|
282
|
+
else {
|
|
283
|
+
ctx.setResultAndStopExecution(cachedResponse);
|
|
284
|
+
}
|
|
285
|
+
return;
|
|
288
286
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
},
|
|
302
|
-
}));
|
|
303
|
-
}
|
|
304
|
-
return {
|
|
305
|
-
onExecuteDone({ result, setResult }) {
|
|
306
|
-
var _a;
|
|
307
|
-
if (core.isAsyncIterable(result)) {
|
|
308
|
-
// eslint-disable-next-line no-console
|
|
309
|
-
console.warn('[useResponseCache] AsyncIterable returned from execute is currently unsupported.');
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
if (context.skip) {
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
if (!shouldCacheResult({ result })) {
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
// we only use the global ttl if no currentTtl has been determined.
|
|
319
|
-
const finalTtl = (_a = context.currentTtl) !== null && _a !== void 0 ? _a : globalTtl;
|
|
320
|
-
if (finalTtl === 0) {
|
|
321
|
-
if (includeExtensionMetadata) {
|
|
322
|
-
setResult({
|
|
323
|
-
...result,
|
|
324
|
-
extensions: {
|
|
325
|
-
responseCache: {
|
|
326
|
-
hit: false,
|
|
327
|
-
didCache: false,
|
|
328
|
-
},
|
|
329
|
-
},
|
|
330
|
-
});
|
|
287
|
+
}
|
|
288
|
+
if (ttlPerSchemaCoordinate) {
|
|
289
|
+
const typeInfo = new graphql.TypeInfo(ctx.args.schema);
|
|
290
|
+
graphql.visit(ctx.args.document, graphql.visitWithTypeInfo(typeInfo, {
|
|
291
|
+
Field(fieldNode) {
|
|
292
|
+
const parentType = typeInfo.getParentType();
|
|
293
|
+
if (parentType) {
|
|
294
|
+
const schemaCoordinate = `${parentType.name}.${fieldNode.name.value}`;
|
|
295
|
+
const maybeTtl = ttlPerSchemaCoordinate[schemaCoordinate];
|
|
296
|
+
if (maybeTtl !== undefined) {
|
|
297
|
+
context.currentTtl = calculateTtl(maybeTtl, context.currentTtl);
|
|
331
298
|
}
|
|
332
|
-
return;
|
|
333
299
|
}
|
|
334
|
-
|
|
300
|
+
},
|
|
301
|
+
}));
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
onExecuteDone({ result, setResult }) {
|
|
305
|
+
var _a;
|
|
306
|
+
if (core.isAsyncIterable(result)) {
|
|
307
|
+
// eslint-disable-next-line no-console
|
|
308
|
+
console.warn('[useResponseCache] AsyncIterable returned from execute is currently unsupported.');
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
if (context.skip) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (!shouldCacheResult({ result })) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
// we only use the global ttl if no currentTtl has been determined.
|
|
318
|
+
const finalTtl = (_a = context.currentTtl) !== null && _a !== void 0 ? _a : globalTtl;
|
|
319
|
+
if (finalTtl === 0) {
|
|
335
320
|
if (includeExtensionMetadata) {
|
|
336
321
|
setResult({
|
|
337
322
|
...result,
|
|
338
323
|
extensions: {
|
|
339
324
|
responseCache: {
|
|
340
325
|
hit: false,
|
|
341
|
-
didCache:
|
|
342
|
-
ttl: finalTtl,
|
|
326
|
+
didCache: false,
|
|
343
327
|
},
|
|
344
328
|
},
|
|
345
329
|
});
|
|
346
330
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
cache.set(operationId, result, identifier.values(), finalTtl);
|
|
334
|
+
if (includeExtensionMetadata) {
|
|
335
|
+
setResult({
|
|
336
|
+
...result,
|
|
337
|
+
extensions: {
|
|
338
|
+
responseCache: {
|
|
339
|
+
hit: false,
|
|
340
|
+
didCache: true,
|
|
341
|
+
ttl: finalTtl,
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
};
|
|
356
348
|
}
|
|
349
|
+
// eslint-disable-next-line no-console
|
|
350
|
+
console.warn(`[useResponseCache] Failed extracting document string from the context. The response will not be cached or served from the cache. ` +
|
|
351
|
+
`If you are overriding the 'parse' behavior make sure to pass a custom 'getDocumentStringFromContext' function for getting the document string, which is required for building the response cache key.`);
|
|
352
|
+
return undefined;
|
|
357
353
|
},
|
|
358
354
|
};
|
|
359
355
|
}
|
package/index.mjs
CHANGED
|
@@ -118,7 +118,7 @@ const hashSHA256 = async (text) => {
|
|
|
118
118
|
if (nodeCrypto) {
|
|
119
119
|
return nodeCrypto.then(crypto => crypto.createHash('sha256').update(text).digest('hex'));
|
|
120
120
|
}
|
|
121
|
-
|
|
121
|
+
if (webCrypto) {
|
|
122
122
|
let buf;
|
|
123
123
|
if (typeof TextEncoder !== 'undefined') {
|
|
124
124
|
buf = new TextEncoder().encode(text);
|
|
@@ -252,102 +252,98 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
252
252
|
},
|
|
253
253
|
};
|
|
254
254
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
hit: true,
|
|
273
|
-
},
|
|
255
|
+
const documentString = getDocumentStringFromContext(ctx.args.contextValue);
|
|
256
|
+
if (documentString != null) {
|
|
257
|
+
const operationId = await buildResponseCacheKey({
|
|
258
|
+
documentString,
|
|
259
|
+
variableValues: ctx.args.variableValues,
|
|
260
|
+
operationName: ctx.args.operationName,
|
|
261
|
+
sessionId: session(ctx.args.contextValue),
|
|
262
|
+
});
|
|
263
|
+
if (((_a = enabled === null || enabled === void 0 ? void 0 : enabled(ctx.args.contextValue)) !== null && _a !== void 0 ? _a : true) === true) {
|
|
264
|
+
const cachedResponse = await cache.get(operationId);
|
|
265
|
+
if (cachedResponse != null) {
|
|
266
|
+
if (includeExtensionMetadata) {
|
|
267
|
+
ctx.setResultAndStopExecution({
|
|
268
|
+
...cachedResponse,
|
|
269
|
+
extensions: {
|
|
270
|
+
responseCache: {
|
|
271
|
+
hit: true,
|
|
274
272
|
},
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
else {
|
|
278
|
-
ctx.setResultAndStopExecution(cachedResponse);
|
|
279
|
-
}
|
|
280
|
-
return;
|
|
273
|
+
},
|
|
274
|
+
});
|
|
281
275
|
}
|
|
276
|
+
else {
|
|
277
|
+
ctx.setResultAndStopExecution(cachedResponse);
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
282
280
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
},
|
|
296
|
-
}));
|
|
297
|
-
}
|
|
298
|
-
return {
|
|
299
|
-
onExecuteDone({ result, setResult }) {
|
|
300
|
-
var _a;
|
|
301
|
-
if (isAsyncIterable(result)) {
|
|
302
|
-
// eslint-disable-next-line no-console
|
|
303
|
-
console.warn('[useResponseCache] AsyncIterable returned from execute is currently unsupported.');
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
if (context.skip) {
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
if (!shouldCacheResult({ result })) {
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
// we only use the global ttl if no currentTtl has been determined.
|
|
313
|
-
const finalTtl = (_a = context.currentTtl) !== null && _a !== void 0 ? _a : globalTtl;
|
|
314
|
-
if (finalTtl === 0) {
|
|
315
|
-
if (includeExtensionMetadata) {
|
|
316
|
-
setResult({
|
|
317
|
-
...result,
|
|
318
|
-
extensions: {
|
|
319
|
-
responseCache: {
|
|
320
|
-
hit: false,
|
|
321
|
-
didCache: false,
|
|
322
|
-
},
|
|
323
|
-
},
|
|
324
|
-
});
|
|
281
|
+
}
|
|
282
|
+
if (ttlPerSchemaCoordinate) {
|
|
283
|
+
const typeInfo = new TypeInfo(ctx.args.schema);
|
|
284
|
+
visit(ctx.args.document, visitWithTypeInfo(typeInfo, {
|
|
285
|
+
Field(fieldNode) {
|
|
286
|
+
const parentType = typeInfo.getParentType();
|
|
287
|
+
if (parentType) {
|
|
288
|
+
const schemaCoordinate = `${parentType.name}.${fieldNode.name.value}`;
|
|
289
|
+
const maybeTtl = ttlPerSchemaCoordinate[schemaCoordinate];
|
|
290
|
+
if (maybeTtl !== undefined) {
|
|
291
|
+
context.currentTtl = calculateTtl(maybeTtl, context.currentTtl);
|
|
325
292
|
}
|
|
326
|
-
return;
|
|
327
293
|
}
|
|
328
|
-
|
|
294
|
+
},
|
|
295
|
+
}));
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
onExecuteDone({ result, setResult }) {
|
|
299
|
+
var _a;
|
|
300
|
+
if (isAsyncIterable(result)) {
|
|
301
|
+
// eslint-disable-next-line no-console
|
|
302
|
+
console.warn('[useResponseCache] AsyncIterable returned from execute is currently unsupported.');
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (context.skip) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (!shouldCacheResult({ result })) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
// we only use the global ttl if no currentTtl has been determined.
|
|
312
|
+
const finalTtl = (_a = context.currentTtl) !== null && _a !== void 0 ? _a : globalTtl;
|
|
313
|
+
if (finalTtl === 0) {
|
|
329
314
|
if (includeExtensionMetadata) {
|
|
330
315
|
setResult({
|
|
331
316
|
...result,
|
|
332
317
|
extensions: {
|
|
333
318
|
responseCache: {
|
|
334
319
|
hit: false,
|
|
335
|
-
didCache:
|
|
336
|
-
ttl: finalTtl,
|
|
320
|
+
didCache: false,
|
|
337
321
|
},
|
|
338
322
|
},
|
|
339
323
|
});
|
|
340
324
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
cache.set(operationId, result, identifier.values(), finalTtl);
|
|
328
|
+
if (includeExtensionMetadata) {
|
|
329
|
+
setResult({
|
|
330
|
+
...result,
|
|
331
|
+
extensions: {
|
|
332
|
+
responseCache: {
|
|
333
|
+
hit: false,
|
|
334
|
+
didCache: true,
|
|
335
|
+
ttl: finalTtl,
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
};
|
|
350
342
|
}
|
|
343
|
+
// eslint-disable-next-line no-console
|
|
344
|
+
console.warn(`[useResponseCache] Failed extracting document string from the context. The response will not be cached or served from the cache. ` +
|
|
345
|
+
`If you are overriding the 'parse' behavior make sure to pass a custom 'getDocumentStringFromContext' function for getting the document string, which is required for building the response cache key.`);
|
|
346
|
+
return undefined;
|
|
351
347
|
},
|
|
352
348
|
};
|
|
353
349
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/response-cache",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.3-alpha-60f5d29.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.3.
|
|
6
|
+
"@envelop/core": "2.3.3-alpha-60f5d29.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
|
-
"url": "https://github.com/
|
|
16
|
+
"url": "https://github.com/n1ru4l/envelop.git",
|
|
17
17
|
"directory": "packages/plugins/response-cache"
|
|
18
18
|
},
|
|
19
19
|
"author": "Kamil Kisiela <kamilkisiela@gmail.com>",
|