|
| 1 | +import { GetObjectCommand, HeadObjectCommand, S3Client } from '@aws-sdk/client-s3' |
| 2 | +import { describe, test } from '@jest/globals' |
| 3 | +import { S3ObjectCache } from './s3ObjectCache' |
| 4 | + |
| 5 | +const maybe = process.env.BUCKET_NAME && process.env.AWS_REGION && process.env.ACCESS_KEY && process.env.SECRET ? describe : describe.skip |
| 6 | + |
| 7 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 8 | +const zlib = require('zlib') |
| 9 | + |
| 10 | +maybe('S3ObjectCache', () => { |
| 11 | + const s3Client = new S3Client({ |
| 12 | + region: process.env.AWS_REGION, |
| 13 | + credentials: { |
| 14 | + accessKeyId: process.env.ACCESS_KEY!, |
| 15 | + secretAccessKey: process.env.SECRET!, |
| 16 | + }, |
| 17 | + }) |
| 18 | + const bucketName = process.env.BUCKET_NAME |
| 19 | + |
| 20 | + const checkFileExist = async (key: string) => { |
| 21 | + const bucketAndKey = { |
| 22 | + Bucket: bucketName, |
| 23 | + Key: key, |
| 24 | + } |
| 25 | + |
| 26 | + const fileExist = await s3Client |
| 27 | + .send(new HeadObjectCommand(bucketAndKey)) |
| 28 | + .then(() => true) |
| 29 | + .catch(() => false) |
| 30 | + |
| 31 | + return fileExist |
| 32 | + } |
| 33 | + |
| 34 | + test('Puts an object in cache as json', async () => { |
| 35 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 36 | + await cache.put('test', { test: 1 }) |
| 37 | + |
| 38 | + const bucketAndKey = { |
| 39 | + Bucket: bucketName, |
| 40 | + Key: 'test.json.gz', |
| 41 | + } |
| 42 | + |
| 43 | + const data = await s3Client |
| 44 | + .send(new GetObjectCommand(bucketAndKey)) |
| 45 | + .then(async (x) => ({ Body: await x.Body!.transformToByteArray(), LastModified: x.LastModified, ContentType: x.ContentType })) |
| 46 | + .catch(() => undefined) |
| 47 | + |
| 48 | + const json = data ? zlib.gunzipSync(data.Body!).toString('utf-8') : undefined |
| 49 | + expect(json).toMatchInlineSnapshot(` |
| 50 | + "{ |
| 51 | + "test": 1 |
| 52 | + }" |
| 53 | + `) |
| 54 | + }) |
| 55 | + |
| 56 | + test('Clear json file cache', async () => { |
| 57 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 58 | + const key = 'test.json.gz' |
| 59 | + |
| 60 | + await cache.put('test', { test: 1 }) |
| 61 | + let fileExist = await checkFileExist(key) |
| 62 | + expect(fileExist).toBe(true) |
| 63 | + |
| 64 | + await cache.clearCache('test') |
| 65 | + fileExist = await checkFileExist(key) |
| 66 | + expect(fileExist).toBe(false) |
| 67 | + }) |
| 68 | + |
| 69 | + test('Clear binary cache', async () => { |
| 70 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 71 | + const fileData = Uint8Array.from(atob('test'), (c) => c.charCodeAt(0)) |
| 72 | + const key = 'test.gz' |
| 73 | + |
| 74 | + await cache.put('test', fileData) |
| 75 | + let fileExist = await checkFileExist(key) |
| 76 | + expect(fileExist).toBe(true) |
| 77 | + |
| 78 | + await cache.clearCache('test') |
| 79 | + fileExist = await checkFileExist(key) |
| 80 | + expect(fileExist).toBe(false) |
| 81 | + }) |
| 82 | + |
| 83 | + test("Get an object from cache if it didn't exist", async () => { |
| 84 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 85 | + |
| 86 | + const cached = await cache.getAndCache<{ test: number }>('test', async (_) => ({ |
| 87 | + test: 1, |
| 88 | + })) |
| 89 | + |
| 90 | + expect(cached.test).toBe(1) |
| 91 | + }) |
| 92 | + |
| 93 | + test('Get an object from cache if it did exist', async () => { |
| 94 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 95 | + await cache.put('test', { test: 1 }) |
| 96 | + |
| 97 | + const cached = await cache.getAndCache<{ test: number }>('test', async (_) => ({ |
| 98 | + test: 2, |
| 99 | + })) |
| 100 | + |
| 101 | + expect(cached.test).toBe(1) |
| 102 | + }) |
| 103 | + |
| 104 | + test('Get an object from cache if it is not yet stale', async () => { |
| 105 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 106 | + await cache.put('test', { test: 1 }) |
| 107 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 108 | + |
| 109 | + const cached = await cache.getAndCache<{ test: number }>( |
| 110 | + 'test', |
| 111 | + async (_) => ({ |
| 112 | + test: 2, |
| 113 | + }), |
| 114 | + { staleAfterSeconds: 2 }, |
| 115 | + ) |
| 116 | + |
| 117 | + expect(cached.test).toBe(1) |
| 118 | + }) |
| 119 | + |
| 120 | + test('Get a fresh object if it is stale', async () => { |
| 121 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 122 | + await cache.put('test', { test: 1 }) |
| 123 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 124 | + |
| 125 | + const cached = await cache.getAndCache<{ test: number }>( |
| 126 | + 'test', |
| 127 | + async (_) => ({ |
| 128 | + test: 2, |
| 129 | + }), |
| 130 | + { staleAfterSeconds: 0 }, |
| 131 | + ) |
| 132 | + |
| 133 | + expect(cached.test).toBe(2) |
| 134 | + }) |
| 135 | + |
| 136 | + test('Get an object from cache if it is stale but there is an error and returnStaleOnError is set', async () => { |
| 137 | + const cache = new S3ObjectCache(s3Client, bucketName!) |
| 138 | + await cache.put('test', { test: 1 }) |
| 139 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 140 | + |
| 141 | + const cached = await cache.getAndCache<{ test: number }>( |
| 142 | + 'test', |
| 143 | + async (_) => { |
| 144 | + throw new Error('error') |
| 145 | + }, |
| 146 | + { |
| 147 | + staleAfterSeconds: 0, |
| 148 | + returnStaleResultOnError: true, |
| 149 | + }, |
| 150 | + ) |
| 151 | + |
| 152 | + expect(cached.test).toBe(1) |
| 153 | + }) |
| 154 | +}) |
0 commit comments