forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-cache-test-utils.ts
More file actions
71 lines (66 loc) · 1.57 KB
/
Copy pathgithub-cache-test-utils.ts
File metadata and controls
71 lines (66 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
export function createMockR2Bucket() {
const objects = new Map<
string,
{
customMetadata?: Record<string, string>
uploaded: Date
value: string
}
>()
const bucket = {
async delete(keys: string | Array<string>) {
for (const key of Array.isArray(keys) ? keys : [keys]) {
objects.delete(key)
}
},
async get(key: string) {
const object = objects.get(key)
if (!object) {
return null
}
return {
key,
customMetadata: object.customMetadata,
text: async () => object.value,
uploaded: object.uploaded,
}
},
async list(options?: {
cursor?: string
include?: Array<'customMetadata'>
limit?: number
prefix?: string
}) {
const prefix = options?.prefix ?? ''
const limit = options?.limit ?? 1000
const entries = Array.from(objects.entries()).filter(([key]) =>
key.startsWith(prefix),
)
return {
objects: entries.slice(0, limit).map(([key, object]) => ({
key,
customMetadata: object.customMetadata,
uploaded: object.uploaded,
})),
truncated: entries.length > limit,
}
},
async put(
key: string,
value: string,
options?: {
customMetadata?: Record<string, string>
httpMetadata?: {
contentType?: string
}
},
) {
objects.set(key, {
customMetadata: options?.customMetadata,
uploaded: new Date(),
value,
})
},
}
return { bucket, objects }
}