MCPcopy Create free account
hub / github.com/pollinations/pollinations / createTestR2Bucket

Function createTestR2Bucket

shared/test/mocks/r2.ts:16–71  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14};
15
16export function createTestR2Bucket(): TestR2Bucket {
17 const objects = new Map<string, StoredR2Object>();
18 let putCount = 0;
19 let uploadTime = 0;
20
21 function createR2Object(key: string, object: StoredR2Object): R2Object {
22 return {
23 key,
24 version: "test",
25 size: object.body.byteLength,
26 etag: "test",
27 httpEtag: '"test"',
28 uploaded: object.uploaded,
29 httpMetadata: object.httpMetadata,
30 customMetadata: object.customMetadata,
31 storageClass: object.storageClass,
32 checksums: {},
33 } as unknown as R2Object;
34 }
35
36 return {
37 head: async (key: string) => {
38 const object = objects.get(key);
39 return object ? createR2Object(key, object) : null;
40 },
41 get: async (key: string) => {
42 const object = objects.get(key);
43 if (!object) return null;
44 return {
45 ...createR2Object(key, object),
46 body: new Response(object.body.slice()).body,
47 };
48 },
49 put: async (key: string, value: BodyInit, options?: R2PutOptions) => {
50 const httpMetadata =
51 options?.httpMetadata instanceof Headers
52 ? undefined
53 : options?.httpMetadata;
54
55 putCount += 1;
56 uploadTime += 1;
57 objects.set(key, {
58 body: new Uint8Array(await new Response(value).arrayBuffer()),
59 httpMetadata,
60 customMetadata: options?.customMetadata,
61 storageClass: options?.storageClass,
62 uploaded: new Date(uploadTime),
63 });
64 return null;
65 },
66 getObject: (key: string) => objects.get(key),
67 get putCount() {
68 return putCount;
69 },
70 } as unknown as TestR2Bucket;
71}

Callers 7

createMediaEnvFunction · 0.90
createTextCacheEnvFunction · 0.90
text-cache.test.tsFile · 0.90
r2-storage.test.tsFile · 0.90
createMediaCacheEnvFunction · 0.90

Calls 5

createR2ObjectFunction · 0.85
sliceMethod · 0.80
arrayBufferMethod · 0.80
getMethod · 0.65
setMethod · 0.65

Tested by 3

createMediaEnvFunction · 0.72
createTextCacheEnvFunction · 0.72
createMediaCacheEnvFunction · 0.72