Skip to content

Commit 53fc476

Browse files
author
Guy Bedford
authored
docs: document new KVStore features, add some edge case tests (#1030)
1 parent 895ff12 commit 53fc476

File tree

7 files changed

+226
-9
lines changed

7 files changed

+226
-9
lines changed

documentation/docs/backend/Backend/prototype/tcpKeepalive.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ The read-only **`tcpKeepalive`** property of a `Backend` instance returns an obj
1111
the TCP keepalive configuration, if any, otherwise returning `null` if TCP keepalive is not enabled.
1212

1313
This object has the following properties:
14-
- `timeSecs` _: number or null.
14+
- `timeSecs` _: number or null._
1515
- Configure how long to wait after the last sent data over the TCP connection before starting to send TCP keepalive probes.
16-
- `intervalSecs` _: number or null.
16+
- `intervalSecs` _: number or null._
1717
- Configure how long to wait between each TCP keepalive probe sent to the backend to determine if it is still active.
18-
- `probes` _: number or null.
18+
- `probes` _: number or null._
1919
- Number of probes to send to the backend before it is considered dead.
2020

2121
## Value
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# KVStore.prototype.list
9+
10+
The **`list()`** can be used to list the keys of a store.
11+
12+
## Syntax
13+
14+
```js
15+
list(options?)
16+
```
17+
18+
### Parameters
19+
20+
- `options` _: object_ _**optional**_
21+
- List options supporting properties:
22+
- `cursor` _: string_ _**optional**_
23+
- The cursor used to pick up from a previous iteration.
24+
- `limit` _: number_ _**optional**_
25+
- The maximum number of keys to return.
26+
- `prefix` _: string_ _**optional**_
27+
- List only those keys that start with the given string prefix.
28+
- `noSync` _: boolean_ _**optional**_
29+
- Do not sync the key list first, instead provide a possibly out-of-date listing. May be faster but inconsistent.
30+
31+
### Return value
32+
33+
Returns a `Promise` which resolves with `{ list: string[], cursor: string | undefined }`.
34+
35+
## Example
36+
37+
In this example we list the keys of a KV Store named `'files'`, iterating 10 at a time, counting the total;
38+
39+
```js
40+
/// <reference types="@fastly/js-compute" />
41+
42+
import { KVStore } from 'fastly:kv-store';
43+
44+
async function app(event) {
45+
const files = new KVStore('files');
46+
47+
let cursor,
48+
list,
49+
total = 0;
50+
do {
51+
({ cursor, list } = await files.list({ limit: 10, cursor }));
52+
total += list?.length;
53+
} while (list);
54+
55+
return new Response(`Iterated ${total} entries`);
56+
}
57+
58+
addEventListener('fetch', (event) => event.respondWith(app(event)));
59+
```

documentation/docs/kv-store/KVStore/prototype/put.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The **`put()`** method stores a `value` into the KV store under a `key`.
1313
## Syntax
1414

1515
```js
16-
put(key, value)
16+
put(key, value, options?)
1717
```
1818
1919
### Parameters
@@ -22,6 +22,14 @@ put(key, value)
2222
- The key to store the supplied value under within the KV store.
2323
- `value` _: ArrayBuffer | TypedArray | DataView| ReadableStream | URLSearchParams | String | string literal_
2424
- The value to store within the KV store.
25+
- `options` _: object_ _**optional**_
26+
- An insert options parameter, supporting:
27+
- `metadata` _: ArrayBuffer | TypedArray | DataView_ _**optional**_
28+
- Binary metadata associated with the entry, may be up to 1000 bytes.
29+
- `ttl` _: number_ _**optional**_
30+
- TTL for the entry
31+
- `mode` _: 'overwrite' | 'add' | 'append' | 'prepend'_ _**optional**_
32+
- Insert mode, defaults to 'overwrite'
2533
2634
### Return value
2735
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# KVStoreEntry.metadata()
9+
10+
The `metadata()` method of the `KVStoreEntry` interface provides the binary metadata associated with the `KVStoreEntry`.
11+
12+
The metadata binary may be up to 1000 bytes, and is returned as a `Uint8Array` TypedArray buffer, if metadata is set, or null otherwise.
13+
14+
## Syntax
15+
16+
```js
17+
metadata()
18+
```
19+
20+
### Parameters
21+
22+
None.
23+
24+
### Return value
25+
26+
A `Promise` that resolves to a `Uint8Array` buffer object.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# KVStoreEntry.metadataText()
9+
10+
The `metadataText()` method of the `KVStoreEntry` interface provides a String interpretation of binary metadata associated with the `KVStoreEntry`.
11+
12+
If the binary data is not a valid string, an encoding error will be thrown.
13+
14+
## Syntax
15+
16+
```js
17+
metadataText()
18+
```
19+
20+
### Parameters
21+
22+
None.
23+
24+
### Return value
25+
26+
A `Promise` that resolves to a String.

integration-tests/js-compute/fixtures/module-mode/src/kv-store.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,19 @@ import { routes, isRunningLocally } from './routes.js';
5555
// TTL only supported on compute not viceroy
5656
if (!isRunningLocally()) {
5757
await store.put('t', 't', { ttl: 2 });
58-
strictEqual(await (await store.get('t')).text(), 't');
58+
const tEntry = await store.get('t');
59+
strictEqual(await tEntry.text(), 't');
60+
const metadata = await tEntry.metadata();
61+
strictEqual(metadata, null);
5962
await new Promise((resolve) => setTimeout(resolve, 2000));
6063
strictEqual(await store.get('t'), null);
6164
}
6265

66+
// bad cursor gives a "bad request" error
67+
assertRejects(async () => {
68+
await store.list({ cursor: 'boooo' });
69+
}, TypeError);
70+
6371
assertThrows(() => {
6472
store.list({ limit: 'booooo' });
6573
}, TypeError);
@@ -86,7 +94,6 @@ import { routes, isRunningLocally } from './routes.js';
8694
]);
8795

8896
({ list, cursor } = await store.list({ limit: 10, prefix: 'c', cursor }));
89-
9097
deepStrictEqual(list, [
9198
'c18',
9299
'c19',
@@ -99,6 +106,95 @@ import { routes, isRunningLocally } from './routes.js';
99106
'c25',
100107
'c26',
101108
]);
109+
110+
({ list, cursor } = await store.list({ limit: 70, prefix: 'c', cursor }));
111+
deepStrictEqual(list, [
112+
'c27',
113+
'c28',
114+
'c29',
115+
'c3',
116+
'c30',
117+
'c31',
118+
'c32',
119+
'c33',
120+
'c34',
121+
'c35',
122+
'c36',
123+
'c37',
124+
'c38',
125+
'c39',
126+
'c4',
127+
'c40',
128+
'c41',
129+
'c42',
130+
'c43',
131+
'c44',
132+
'c45',
133+
'c46',
134+
'c47',
135+
'c48',
136+
'c49',
137+
'c5',
138+
'c50',
139+
'c51',
140+
'c52',
141+
'c53',
142+
'c54',
143+
'c55',
144+
'c56',
145+
'c57',
146+
'c58',
147+
'c59',
148+
'c6',
149+
'c60',
150+
'c61',
151+
'c62',
152+
'c63',
153+
'c64',
154+
'c65',
155+
'c66',
156+
'c67',
157+
'c68',
158+
'c69',
159+
'c7',
160+
'c70',
161+
'c71',
162+
'c72',
163+
'c73',
164+
'c74',
165+
'c75',
166+
'c76',
167+
'c77',
168+
'c78',
169+
'c79',
170+
'c8',
171+
'c80',
172+
'c81',
173+
'c82',
174+
'c83',
175+
'c84',
176+
'c85',
177+
'c86',
178+
'c87',
179+
'c88',
180+
'c89',
181+
'c9',
182+
]);
183+
184+
({ list, cursor } = await store.list({ limit: 15, prefix: 'c', cursor }));
185+
deepStrictEqual(list, [
186+
'c90',
187+
'c91',
188+
'c92',
189+
'c93',
190+
'c94',
191+
'c95',
192+
'c96',
193+
'c97',
194+
'c98',
195+
'c99',
196+
]);
197+
strictEqual(cursor, undefined);
102198
});
103199
}
104200

types/kv-store.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ declare module 'fastly:kv-store' {
8484
*/
8585
metadata?: ArrayBufferView | ArrayBuffer | string;
8686
/**
87-
* TTL for the entry, defaults to 0.
87+
* TTL for the entry.
8888
*/
8989
ttl?: number;
9090
/**
@@ -120,9 +120,11 @@ declare module 'fastly:kv-store' {
120120
}): {
121121
list: string[];
122122
/**
123-
* Pass this base64 cursor into a subsequent list call to obtain the next listing
123+
* Pass this base64 cursor into a subsequent list call to obtain the next listing.
124+
*
125+
* The cursor is *undefined* when the end of the list is reached.
124126
*/
125-
cursor: string;
127+
cursor: string | undefined;
126128
};
127129
}
128130

0 commit comments

Comments
 (0)