Skip to content

Commit

Permalink
Add KVGateway tests, fix cacheTtl validation and last cursor (#416
Browse files Browse the repository at this point in the history
)

- Upgrade to `ava@5`
- Port `KVNamespace` tests from Miniflare 2
- Only throw if `cacheTtl` is invalid, not just specified
- Return `cursor: undefined` on the last page to match the behaviour
  of the Worker's runtime
  • Loading branch information
mrbbot committed Oct 31, 2023
1 parent caf9de0 commit 83671be
Show file tree
Hide file tree
Showing 4 changed files with 533 additions and 6 deletions.
12 changes: 8 additions & 4 deletions packages/miniflare/src/plugins/kv/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface KVGatewayListOptions {
}
export interface KVGatewayListResult<Meta = unknown> {
keys: StoredKeyMeta<Meta>[];
cursor: string;
cursor?: string;
list_complete: boolean;
}

Expand All @@ -81,10 +81,14 @@ export class KVGateway {
validateKey(key);
// Validate cacheTtl, but ignore it as there's only one "edge location":
// the user's computer
if (options?.cacheTtl !== undefined) {
const cacheTtl = options?.cacheTtl;
if (
cacheTtl !== undefined &&
(isNaN(cacheTtl) || cacheTtl < MIN_CACHE_TTL)
) {
throw new KVError(
400,
`Invalid ${PARAM_CACHE_TTL} of ${options.cacheTtl}. Cache TTL must be at least ${MIN_CACHE_TTL}.`
`Invalid ${PARAM_CACHE_TTL} of ${cacheTtl}. Cache TTL must be at least ${MIN_CACHE_TTL}.`
);
}
return this.storage.get(key);
Expand Down Expand Up @@ -184,7 +188,7 @@ export class KVGateway {
const res = await this.storage.list({ limit, prefix, cursor });
return {
keys: res.keys,
cursor: res.cursor,
cursor: res.cursor === "" ? undefined : res.cursor,
list_complete: res.cursor === "",
};
}
Expand Down
Loading

0 comments on commit 83671be

Please sign in to comment.