@envelop/response-cache-cloudflare-kv 0.2.0-alpha-20231106140646-27776100
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/LICENSE +21 -0
- package/README.md +61 -0
- package/cjs/cache-key.js +23 -0
- package/cjs/index.js +52 -0
- package/cjs/invalidate.js +44 -0
- package/cjs/package.json +1 -0
- package/cjs/set.js +33 -0
- package/esm/cache-key.js +18 -0
- package/esm/index.js +48 -0
- package/esm/invalidate.js +38 -0
- package/esm/set.js +29 -0
- package/package.json +63 -0
- package/typings/cache-key.d.cts +2 -0
- package/typings/cache-key.d.ts +2 -0
- package/typings/index.d.cts +32 -0
- package/typings/index.d.ts +32 -0
- package/typings/invalidate.d.cts +9 -0
- package/typings/invalidate.d.ts +9 -0
- package/typings/set.d.cts +12 -0
- package/typings/set.d.ts +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Dotan Simha
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
## `@envelop/response-cache-cloudflare-kv`
|
|
2
|
+
|
|
3
|
+
- Supports [Cloudflare KV](https://developers.cloudflare.com/kv/) cache for
|
|
4
|
+
`@envelop/response-cache` plugin
|
|
5
|
+
- Suitable for graphql servers running on [Cloudflare Workers](https://workers.cloudflare.com/)
|
|
6
|
+
|
|
7
|
+
[Check out the GraphQL Response Cache Guide for more information](https://envelop.dev/docs/guides/adding-a-graphql-response-cache)
|
|
8
|
+
|
|
9
|
+
## Getting Started
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
yarn add @envelop/response-cache
|
|
13
|
+
yarn add @envelop/response-cache-cloudflare-kv
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage Example
|
|
17
|
+
|
|
18
|
+
In order to use the Cloudflare KV cache, you need to:
|
|
19
|
+
|
|
20
|
+
- Create a Cloudflare KV namespace
|
|
21
|
+
- Add that namespace to your `wrangler.toml` in order to access it from your worker. Read the
|
|
22
|
+
[KV docs](https://developers.cloudflare.com/kv/get-started/) to get started.
|
|
23
|
+
- Pass the KV namespace to the `createKvCache` function and set to the `useResponseCache` plugin
|
|
24
|
+
options. See the example below.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { createKvCache } from 'envelop-response-cache-cloudflare-kv'
|
|
28
|
+
import { createSchema, createYoga, YogaInitialContext } from 'graphql-yoga'
|
|
29
|
+
import { useResponseCache } from '@envelop/response-cache'
|
|
30
|
+
import { resolvers } from './graphql-schema/resolvers.generated'
|
|
31
|
+
import { typeDefs } from './graphql-schema/typeDefs.generated'
|
|
32
|
+
|
|
33
|
+
export type Env = {
|
|
34
|
+
GRAPHQL_RESPONSE_CACHE: KVNamespace
|
|
35
|
+
}
|
|
36
|
+
export type GraphQLContext = YogaInitialContext & Env & ExecutionContext
|
|
37
|
+
|
|
38
|
+
export default {
|
|
39
|
+
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
|
40
|
+
const kvCache = createKvCache({
|
|
41
|
+
KV: env.GRAPHQL_RESPONSE_CACHE,
|
|
42
|
+
ctx,
|
|
43
|
+
keyPrefix: 'graphql' // optional
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const graphqlServer = createYoga<GraphQLContext>({
|
|
47
|
+
schema: createSchema({ typeDefs, resolvers }),
|
|
48
|
+
plugins: [
|
|
49
|
+
useResponseCache({
|
|
50
|
+
cache: kvCache,
|
|
51
|
+
session: () => null,
|
|
52
|
+
includeExtensionMetadata: true,
|
|
53
|
+
ttl: 1000 * 10 // 10 seconds
|
|
54
|
+
})
|
|
55
|
+
]
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
return graphqlServer.fetch(request, env, ctx)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
package/cjs/cache-key.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildEntityKey = exports.buildOperationKey = void 0;
|
|
4
|
+
function buildOperationKey(operationId, keyPrefix = undefined) {
|
|
5
|
+
if (keyPrefix) {
|
|
6
|
+
return `${keyPrefix}:operation:${operationId}`;
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
return `operation:${operationId}`;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.buildOperationKey = buildOperationKey;
|
|
13
|
+
function buildEntityKey(entityTypename, entityId = undefined, keyPrefix = undefined) {
|
|
14
|
+
let finalKey = keyPrefix ? `${keyPrefix}:` : '';
|
|
15
|
+
if (entityId) {
|
|
16
|
+
finalKey += `entity:${entityTypename}:${entityId}`;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
finalKey += `entity:${entityTypename}`;
|
|
20
|
+
}
|
|
21
|
+
return finalKey;
|
|
22
|
+
}
|
|
23
|
+
exports.buildEntityKey = buildEntityKey;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createKvCache = void 0;
|
|
4
|
+
const cache_key_js_1 = require("./cache-key.js");
|
|
5
|
+
const invalidate_js_1 = require("./invalidate.js");
|
|
6
|
+
const set_js_1 = require("./set.js");
|
|
7
|
+
/**
|
|
8
|
+
* Creates a cache object that uses Cloudflare KV to store GraphQL responses.
|
|
9
|
+
* This cache is optimized for Cloudflare workers and uses the `ctx.waitUntil` method to perform non-blocking actions where possible
|
|
10
|
+
*
|
|
11
|
+
* To find out more about how this cache is implemented see https://the-guild.dev/blog/graphql-response-caching-with-envelop
|
|
12
|
+
*
|
|
13
|
+
* @param config Modify the behavior of the cache as it pertains to Cloudflare KV
|
|
14
|
+
* @returns A cache object that can be passed to envelop's `useResponseCache` plugin
|
|
15
|
+
*/
|
|
16
|
+
function createKvCache(config) {
|
|
17
|
+
if (config.cacheReadTTL && config.cacheReadTTL < 60000) {
|
|
18
|
+
// eslint-disable-next-line no-console
|
|
19
|
+
console.warn('Cloudflare KV cacheReadTTL must be at least 60000 (60 seconds). Using default value of 60000 instead.');
|
|
20
|
+
}
|
|
21
|
+
const computedTtlInSeconds = Math.max(Math.floor((config.cacheReadTTL ?? 60000) / 1000), 60); // KV TTL must be at least 60 seconds
|
|
22
|
+
const cache = {
|
|
23
|
+
async get(id) {
|
|
24
|
+
const kvResponse = await config.KV.get((0, cache_key_js_1.buildOperationKey)(id, config.keyPrefix), {
|
|
25
|
+
type: 'text',
|
|
26
|
+
cacheTtl: computedTtlInSeconds,
|
|
27
|
+
});
|
|
28
|
+
if (kvResponse) {
|
|
29
|
+
return JSON.parse(kvResponse);
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
},
|
|
33
|
+
set(
|
|
34
|
+
/** id/hash of the operation */
|
|
35
|
+
id,
|
|
36
|
+
/** the result that should be cached */
|
|
37
|
+
data,
|
|
38
|
+
/** array of entity records that were collected during execution */
|
|
39
|
+
entities,
|
|
40
|
+
/** how long the operation should be cached (in milliseconds) */
|
|
41
|
+
ttl) {
|
|
42
|
+
// Do not block execution of the worker while caching the result
|
|
43
|
+
config.ctx.waitUntil((0, set_js_1.set)(id, data, entities, ttl, config));
|
|
44
|
+
},
|
|
45
|
+
invalidate(entities) {
|
|
46
|
+
// Do not block execution of the worker while invalidating the cache
|
|
47
|
+
config.ctx.waitUntil((0, invalidate_js_1.invalidate)(entities, config));
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
return cache;
|
|
51
|
+
}
|
|
52
|
+
exports.createKvCache = createKvCache;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAllKvKeysForPrefix = exports.invalidateCacheEntityRecord = exports.invalidate = void 0;
|
|
4
|
+
const cache_key_js_1 = require("./cache-key.js");
|
|
5
|
+
async function invalidate(entities, config) {
|
|
6
|
+
const kvPromises = []; // Collecting all the KV operations so we can await them all at once
|
|
7
|
+
const entityInvalidationPromises = []; // Parallelize invalidation of each entity
|
|
8
|
+
for (const entity of entities) {
|
|
9
|
+
entityInvalidationPromises.push(invalidateCacheEntityRecord(entity, kvPromises, config));
|
|
10
|
+
}
|
|
11
|
+
await Promise.allSettled(entityInvalidationPromises);
|
|
12
|
+
await Promise.allSettled(kvPromises);
|
|
13
|
+
}
|
|
14
|
+
exports.invalidate = invalidate;
|
|
15
|
+
async function invalidateCacheEntityRecord(entity,
|
|
16
|
+
/** Collect all inner promises to batch await all async operations outside the function */
|
|
17
|
+
kvPromiseCollection, config) {
|
|
18
|
+
const entityKey = (0, cache_key_js_1.buildEntityKey)(entity.typename, entity.id, config.keyPrefix);
|
|
19
|
+
for await (const kvKey of getAllKvKeysForPrefix(entityKey, config)) {
|
|
20
|
+
if (kvKey.metadata?.operationKey) {
|
|
21
|
+
kvPromiseCollection.push(config.KV.delete(kvKey.metadata?.operationKey));
|
|
22
|
+
kvPromiseCollection.push(config.KV.delete(kvKey.name));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.invalidateCacheEntityRecord = invalidateCacheEntityRecord;
|
|
27
|
+
async function* getAllKvKeysForPrefix(prefix, config) {
|
|
28
|
+
let keyListComplete = false;
|
|
29
|
+
let cursor;
|
|
30
|
+
do {
|
|
31
|
+
const kvListResponse = await config.KV.list({
|
|
32
|
+
prefix,
|
|
33
|
+
cursor,
|
|
34
|
+
});
|
|
35
|
+
keyListComplete = kvListResponse.list_complete;
|
|
36
|
+
if (!kvListResponse.list_complete) {
|
|
37
|
+
cursor = kvListResponse.cursor;
|
|
38
|
+
}
|
|
39
|
+
for (const keyResult of kvListResponse.keys) {
|
|
40
|
+
yield keyResult;
|
|
41
|
+
}
|
|
42
|
+
} while (!keyListComplete);
|
|
43
|
+
}
|
|
44
|
+
exports.getAllKvKeysForPrefix = getAllKvKeysForPrefix;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/set.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.set = void 0;
|
|
4
|
+
const cache_key_js_1 = require("./cache-key.js");
|
|
5
|
+
async function set(
|
|
6
|
+
/** id/hash of the operation */
|
|
7
|
+
id,
|
|
8
|
+
/** the result that should be cached */
|
|
9
|
+
data,
|
|
10
|
+
/** array of entity records that were collected during execution */
|
|
11
|
+
entities,
|
|
12
|
+
/** how long the operation should be cached (in milliseconds) */
|
|
13
|
+
ttl, config) {
|
|
14
|
+
const ttlInSeconds = Math.max(Math.floor(ttl / 1000), 60); // KV TTL must be at least 60 seconds
|
|
15
|
+
const operationKey = (0, cache_key_js_1.buildOperationKey)(id, config.keyPrefix);
|
|
16
|
+
const operationKeyWithoutPrefix = (0, cache_key_js_1.buildOperationKey)(id);
|
|
17
|
+
const kvPromises = []; // Collecting all the KV operations so we can await them all at once
|
|
18
|
+
kvPromises.push(config.KV.put(operationKey, JSON.stringify(data), {
|
|
19
|
+
expirationTtl: ttlInSeconds,
|
|
20
|
+
metadata: { operationKey },
|
|
21
|
+
}));
|
|
22
|
+
// Store connections between the entities and the operation key
|
|
23
|
+
// E.g if the entities are User:1 and User:2, we need to know that the operation key is connected to both of them
|
|
24
|
+
for (const entity of entities) {
|
|
25
|
+
const entityKey = (0, cache_key_js_1.buildEntityKey)(entity.typename, entity.id, config.keyPrefix);
|
|
26
|
+
kvPromises.push(config.KV.put(`${entityKey}:${operationKeyWithoutPrefix}`, operationKey, {
|
|
27
|
+
expirationTtl: ttlInSeconds,
|
|
28
|
+
metadata: { operationKey },
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
await Promise.allSettled(kvPromises);
|
|
32
|
+
}
|
|
33
|
+
exports.set = set;
|
package/esm/cache-key.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function buildOperationKey(operationId, keyPrefix = undefined) {
|
|
2
|
+
if (keyPrefix) {
|
|
3
|
+
return `${keyPrefix}:operation:${operationId}`;
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
return `operation:${operationId}`;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function buildEntityKey(entityTypename, entityId = undefined, keyPrefix = undefined) {
|
|
10
|
+
let finalKey = keyPrefix ? `${keyPrefix}:` : '';
|
|
11
|
+
if (entityId) {
|
|
12
|
+
finalKey += `entity:${entityTypename}:${entityId}`;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
finalKey += `entity:${entityTypename}`;
|
|
16
|
+
}
|
|
17
|
+
return finalKey;
|
|
18
|
+
}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { buildOperationKey } from './cache-key.js';
|
|
2
|
+
import { invalidate } from './invalidate.js';
|
|
3
|
+
import { set } from './set.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a cache object that uses Cloudflare KV to store GraphQL responses.
|
|
6
|
+
* This cache is optimized for Cloudflare workers and uses the `ctx.waitUntil` method to perform non-blocking actions where possible
|
|
7
|
+
*
|
|
8
|
+
* To find out more about how this cache is implemented see https://the-guild.dev/blog/graphql-response-caching-with-envelop
|
|
9
|
+
*
|
|
10
|
+
* @param config Modify the behavior of the cache as it pertains to Cloudflare KV
|
|
11
|
+
* @returns A cache object that can be passed to envelop's `useResponseCache` plugin
|
|
12
|
+
*/
|
|
13
|
+
export function createKvCache(config) {
|
|
14
|
+
if (config.cacheReadTTL && config.cacheReadTTL < 60000) {
|
|
15
|
+
// eslint-disable-next-line no-console
|
|
16
|
+
console.warn('Cloudflare KV cacheReadTTL must be at least 60000 (60 seconds). Using default value of 60000 instead.');
|
|
17
|
+
}
|
|
18
|
+
const computedTtlInSeconds = Math.max(Math.floor((config.cacheReadTTL ?? 60000) / 1000), 60); // KV TTL must be at least 60 seconds
|
|
19
|
+
const cache = {
|
|
20
|
+
async get(id) {
|
|
21
|
+
const kvResponse = await config.KV.get(buildOperationKey(id, config.keyPrefix), {
|
|
22
|
+
type: 'text',
|
|
23
|
+
cacheTtl: computedTtlInSeconds,
|
|
24
|
+
});
|
|
25
|
+
if (kvResponse) {
|
|
26
|
+
return JSON.parse(kvResponse);
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
},
|
|
30
|
+
set(
|
|
31
|
+
/** id/hash of the operation */
|
|
32
|
+
id,
|
|
33
|
+
/** the result that should be cached */
|
|
34
|
+
data,
|
|
35
|
+
/** array of entity records that were collected during execution */
|
|
36
|
+
entities,
|
|
37
|
+
/** how long the operation should be cached (in milliseconds) */
|
|
38
|
+
ttl) {
|
|
39
|
+
// Do not block execution of the worker while caching the result
|
|
40
|
+
config.ctx.waitUntil(set(id, data, entities, ttl, config));
|
|
41
|
+
},
|
|
42
|
+
invalidate(entities) {
|
|
43
|
+
// Do not block execution of the worker while invalidating the cache
|
|
44
|
+
config.ctx.waitUntil(invalidate(entities, config));
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
return cache;
|
|
48
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { buildEntityKey } from './cache-key.js';
|
|
2
|
+
export async function invalidate(entities, config) {
|
|
3
|
+
const kvPromises = []; // Collecting all the KV operations so we can await them all at once
|
|
4
|
+
const entityInvalidationPromises = []; // Parallelize invalidation of each entity
|
|
5
|
+
for (const entity of entities) {
|
|
6
|
+
entityInvalidationPromises.push(invalidateCacheEntityRecord(entity, kvPromises, config));
|
|
7
|
+
}
|
|
8
|
+
await Promise.allSettled(entityInvalidationPromises);
|
|
9
|
+
await Promise.allSettled(kvPromises);
|
|
10
|
+
}
|
|
11
|
+
export async function invalidateCacheEntityRecord(entity,
|
|
12
|
+
/** Collect all inner promises to batch await all async operations outside the function */
|
|
13
|
+
kvPromiseCollection, config) {
|
|
14
|
+
const entityKey = buildEntityKey(entity.typename, entity.id, config.keyPrefix);
|
|
15
|
+
for await (const kvKey of getAllKvKeysForPrefix(entityKey, config)) {
|
|
16
|
+
if (kvKey.metadata?.operationKey) {
|
|
17
|
+
kvPromiseCollection.push(config.KV.delete(kvKey.metadata?.operationKey));
|
|
18
|
+
kvPromiseCollection.push(config.KV.delete(kvKey.name));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function* getAllKvKeysForPrefix(prefix, config) {
|
|
23
|
+
let keyListComplete = false;
|
|
24
|
+
let cursor;
|
|
25
|
+
do {
|
|
26
|
+
const kvListResponse = await config.KV.list({
|
|
27
|
+
prefix,
|
|
28
|
+
cursor,
|
|
29
|
+
});
|
|
30
|
+
keyListComplete = kvListResponse.list_complete;
|
|
31
|
+
if (!kvListResponse.list_complete) {
|
|
32
|
+
cursor = kvListResponse.cursor;
|
|
33
|
+
}
|
|
34
|
+
for (const keyResult of kvListResponse.keys) {
|
|
35
|
+
yield keyResult;
|
|
36
|
+
}
|
|
37
|
+
} while (!keyListComplete);
|
|
38
|
+
}
|
package/esm/set.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { buildEntityKey, buildOperationKey } from './cache-key.js';
|
|
2
|
+
export async function set(
|
|
3
|
+
/** id/hash of the operation */
|
|
4
|
+
id,
|
|
5
|
+
/** the result that should be cached */
|
|
6
|
+
data,
|
|
7
|
+
/** array of entity records that were collected during execution */
|
|
8
|
+
entities,
|
|
9
|
+
/** how long the operation should be cached (in milliseconds) */
|
|
10
|
+
ttl, config) {
|
|
11
|
+
const ttlInSeconds = Math.max(Math.floor(ttl / 1000), 60); // KV TTL must be at least 60 seconds
|
|
12
|
+
const operationKey = buildOperationKey(id, config.keyPrefix);
|
|
13
|
+
const operationKeyWithoutPrefix = buildOperationKey(id);
|
|
14
|
+
const kvPromises = []; // Collecting all the KV operations so we can await them all at once
|
|
15
|
+
kvPromises.push(config.KV.put(operationKey, JSON.stringify(data), {
|
|
16
|
+
expirationTtl: ttlInSeconds,
|
|
17
|
+
metadata: { operationKey },
|
|
18
|
+
}));
|
|
19
|
+
// Store connections between the entities and the operation key
|
|
20
|
+
// E.g if the entities are User:1 and User:2, we need to know that the operation key is connected to both of them
|
|
21
|
+
for (const entity of entities) {
|
|
22
|
+
const entityKey = buildEntityKey(entity.typename, entity.id, config.keyPrefix);
|
|
23
|
+
kvPromises.push(config.KV.put(`${entityKey}:${operationKeyWithoutPrefix}`, operationKey, {
|
|
24
|
+
expirationTtl: ttlInSeconds,
|
|
25
|
+
metadata: { operationKey },
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
await Promise.allSettled(kvPromises);
|
|
29
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@envelop/response-cache-cloudflare-kv",
|
|
3
|
+
"version": "0.2.0-alpha-20231106140646-27776100",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"peerDependencies": {
|
|
6
|
+
"@envelop/response-cache": "^6.1.0",
|
|
7
|
+
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"tslib": "^2.6.2"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/n1ru4l/envelop.git",
|
|
15
|
+
"directory": "packages/plugins/response-cache-cloudflare-kv"
|
|
16
|
+
},
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Adishwar Rishi",
|
|
19
|
+
"url": "https://github.com/AdiRishi"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18.0.0"
|
|
24
|
+
},
|
|
25
|
+
"main": "cjs/index.js",
|
|
26
|
+
"module": "esm/index.js",
|
|
27
|
+
"typings": "typings/index.d.ts",
|
|
28
|
+
"typescript": {
|
|
29
|
+
"definition": "typings/index.d.ts"
|
|
30
|
+
},
|
|
31
|
+
"type": "module",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./typings/index.d.cts",
|
|
36
|
+
"default": "./cjs/index.js"
|
|
37
|
+
},
|
|
38
|
+
"import": {
|
|
39
|
+
"types": "./typings/index.d.ts",
|
|
40
|
+
"default": "./esm/index.js"
|
|
41
|
+
},
|
|
42
|
+
"default": {
|
|
43
|
+
"types": "./typings/index.d.ts",
|
|
44
|
+
"default": "./esm/index.js"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"./*": {
|
|
48
|
+
"require": {
|
|
49
|
+
"types": "./typings/*.d.cts",
|
|
50
|
+
"default": "./cjs/*.js"
|
|
51
|
+
},
|
|
52
|
+
"import": {
|
|
53
|
+
"types": "./typings/*.d.ts",
|
|
54
|
+
"default": "./esm/*.js"
|
|
55
|
+
},
|
|
56
|
+
"default": {
|
|
57
|
+
"types": "./typings/*.d.ts",
|
|
58
|
+
"default": "./esm/*.js"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"./package.json": "./package.json"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ExecutionContext, KVNamespace } from '@cloudflare/workers-types';
|
|
2
|
+
import type { Cache } from '@envelop/response-cache';
|
|
3
|
+
export type KvCacheConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* The Cloudflare KV namespace that should be used to store the cache
|
|
6
|
+
*/
|
|
7
|
+
KV: KVNamespace;
|
|
8
|
+
/**
|
|
9
|
+
* The Cloudflare worker execution context. Used to perform non-blocking actions like cache storage and invalidation.
|
|
10
|
+
*/
|
|
11
|
+
ctx: ExecutionContext;
|
|
12
|
+
/**
|
|
13
|
+
* Defines the length of time in milliseconds that a KV result is cached in the global network location it is accessed from.
|
|
14
|
+
*
|
|
15
|
+
* The cacheTTL parameter must be an integer greater than or equal to 60000 (60 seconds), which is the default.
|
|
16
|
+
*/
|
|
17
|
+
cacheReadTTL?: number;
|
|
18
|
+
/**
|
|
19
|
+
* A prefix that should be added to all cache keys
|
|
20
|
+
*/
|
|
21
|
+
keyPrefix?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Creates a cache object that uses Cloudflare KV to store GraphQL responses.
|
|
25
|
+
* This cache is optimized for Cloudflare workers and uses the `ctx.waitUntil` method to perform non-blocking actions where possible
|
|
26
|
+
*
|
|
27
|
+
* To find out more about how this cache is implemented see https://the-guild.dev/blog/graphql-response-caching-with-envelop
|
|
28
|
+
*
|
|
29
|
+
* @param config Modify the behavior of the cache as it pertains to Cloudflare KV
|
|
30
|
+
* @returns A cache object that can be passed to envelop's `useResponseCache` plugin
|
|
31
|
+
*/
|
|
32
|
+
export declare function createKvCache(config: KvCacheConfig): Cache;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ExecutionContext, KVNamespace } from '@cloudflare/workers-types';
|
|
2
|
+
import type { Cache } from '@envelop/response-cache';
|
|
3
|
+
export type KvCacheConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* The Cloudflare KV namespace that should be used to store the cache
|
|
6
|
+
*/
|
|
7
|
+
KV: KVNamespace;
|
|
8
|
+
/**
|
|
9
|
+
* The Cloudflare worker execution context. Used to perform non-blocking actions like cache storage and invalidation.
|
|
10
|
+
*/
|
|
11
|
+
ctx: ExecutionContext;
|
|
12
|
+
/**
|
|
13
|
+
* Defines the length of time in milliseconds that a KV result is cached in the global network location it is accessed from.
|
|
14
|
+
*
|
|
15
|
+
* The cacheTTL parameter must be an integer greater than or equal to 60000 (60 seconds), which is the default.
|
|
16
|
+
*/
|
|
17
|
+
cacheReadTTL?: number;
|
|
18
|
+
/**
|
|
19
|
+
* A prefix that should be added to all cache keys
|
|
20
|
+
*/
|
|
21
|
+
keyPrefix?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Creates a cache object that uses Cloudflare KV to store GraphQL responses.
|
|
25
|
+
* This cache is optimized for Cloudflare workers and uses the `ctx.waitUntil` method to perform non-blocking actions where possible
|
|
26
|
+
*
|
|
27
|
+
* To find out more about how this cache is implemented see https://the-guild.dev/blog/graphql-response-caching-with-envelop
|
|
28
|
+
*
|
|
29
|
+
* @param config Modify the behavior of the cache as it pertains to Cloudflare KV
|
|
30
|
+
* @returns A cache object that can be passed to envelop's `useResponseCache` plugin
|
|
31
|
+
*/
|
|
32
|
+
export declare function createKvCache(config: KvCacheConfig): Cache;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CacheEntityRecord } from '@envelop/response-cache';
|
|
2
|
+
import type { KvCacheConfig } from './index.cjs';
|
|
3
|
+
export declare function invalidate(entities: Iterable<CacheEntityRecord>, config: KvCacheConfig): Promise<void>;
|
|
4
|
+
export declare function invalidateCacheEntityRecord(entity: CacheEntityRecord,
|
|
5
|
+
/** Collect all inner promises to batch await all async operations outside the function */
|
|
6
|
+
kvPromiseCollection: Promise<unknown>[], config: KvCacheConfig): Promise<void>;
|
|
7
|
+
export declare function getAllKvKeysForPrefix(prefix: string, config: KvCacheConfig): AsyncGenerator<import("@cloudflare/workers-types").KVNamespaceListKey<{
|
|
8
|
+
operationKey: string;
|
|
9
|
+
}, string>, void, unknown>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CacheEntityRecord } from '@envelop/response-cache';
|
|
2
|
+
import type { KvCacheConfig } from './index.js';
|
|
3
|
+
export declare function invalidate(entities: Iterable<CacheEntityRecord>, config: KvCacheConfig): Promise<void>;
|
|
4
|
+
export declare function invalidateCacheEntityRecord(entity: CacheEntityRecord,
|
|
5
|
+
/** Collect all inner promises to batch await all async operations outside the function */
|
|
6
|
+
kvPromiseCollection: Promise<unknown>[], config: KvCacheConfig): Promise<void>;
|
|
7
|
+
export declare function getAllKvKeysForPrefix(prefix: string, config: KvCacheConfig): AsyncGenerator<import("@cloudflare/workers-types").KVNamespaceListKey<{
|
|
8
|
+
operationKey: string;
|
|
9
|
+
}, string>, void, unknown>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ExecutionResult } from 'graphql';
|
|
2
|
+
import type { CacheEntityRecord } from '@envelop/response-cache';
|
|
3
|
+
import type { KvCacheConfig } from './index.cjs';
|
|
4
|
+
export declare function set(
|
|
5
|
+
/** id/hash of the operation */
|
|
6
|
+
id: string,
|
|
7
|
+
/** the result that should be cached */
|
|
8
|
+
data: ExecutionResult,
|
|
9
|
+
/** array of entity records that were collected during execution */
|
|
10
|
+
entities: Iterable<CacheEntityRecord>,
|
|
11
|
+
/** how long the operation should be cached (in milliseconds) */
|
|
12
|
+
ttl: number, config: KvCacheConfig): Promise<void>;
|
package/typings/set.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ExecutionResult } from 'graphql';
|
|
2
|
+
import type { CacheEntityRecord } from '@envelop/response-cache';
|
|
3
|
+
import type { KvCacheConfig } from './index.js';
|
|
4
|
+
export declare function set(
|
|
5
|
+
/** id/hash of the operation */
|
|
6
|
+
id: string,
|
|
7
|
+
/** the result that should be cached */
|
|
8
|
+
data: ExecutionResult,
|
|
9
|
+
/** array of entity records that were collected during execution */
|
|
10
|
+
entities: Iterable<CacheEntityRecord>,
|
|
11
|
+
/** how long the operation should be cached (in milliseconds) */
|
|
12
|
+
ttl: number, config: KvCacheConfig): Promise<void>;
|