This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.js
More file actions
82 lines (73 loc) · 2.14 KB
/
utils.js
File metadata and controls
82 lines (73 loc) · 2.14 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
72
73
74
75
76
77
78
79
80
81
82
import fsBlobStore from 'fs-blob-store'
import s3BlobStore from 's3-blob-store'
import aws from 'aws-sdk'
import path from 'path'
import randomstring from 'randomstring'
import rimraf from 'rimraf'
import { mkdirSync } from 'fs'
import test from 'blue-tape'
import cloudCache from '../src'
// Clean .minio directory
/*
rimraf.sync(minioDir)
mkdirSync(minioDir)
*/
const storeInit = {
fs: () => {
const tmpDir = path.join(__dirname, '.tmp')
// const dir = path.join(tmpDir, name)
const blobStore = fsBlobStore(tmpDir)
const resetStore = () => {
// Clean tmp directory
rimraf.sync(tmpDir)
mkdirSync(tmpDir)
return Promise.resolve()
}
return { resetStore, blobStore }
},
s3: () => {
const accessKeyId = process.env.S3_ACCESS_KEY
const secretAccessKey = process.env.S3_SECRET_KEY
const bucket = 'cloudcache-test'
const client = new aws.S3({
accessKeyId,
secretAccessKey,
endpoint: new aws.Endpoint(process.env.S3_ENDPOINT),
s3ForcePathStyle: true, // needed to use minio?
signatureVersion: 'v4',
})
const blobStore = s3BlobStore({ client, bucket })
const resetStore = () => {
const params = { Bucket: bucket }
const clearBucket = () => (
client
.listObjects(params)
.promise()
.then(({ Contents }) => {
const tasks = Contents.map(({ Key }) => (
client.deleteObject({ ...params, Key }).promise()
))
return Promise.all(tasks)
})
)
return clearBucket()
.then(() => (
client.deleteBucket(params).promise()
))
.catch((err) => {
if (err.code === 'NoSuchBucket') return
throw err
})
.then(() => (
client.createBucket(params).promise()
))
}
return { resetStore, blobStore }
},
}
const { resetStore, blobStore } = storeInit[process.env.STORE || 'fs']()
// @hack... this should be executed once, before any other test
test('reset store', () => resetStore())
export default (name = randomstring.generate(7)) => (
cloudCache(blobStore, { keyPrefix: `${name}/` })
)