@adobe/aio-lib-web 6.0.0 → 6.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +4 -1
- package/.github/dependabot.yml +11 -0
- package/.github/workflows/node.js.yml +3 -4
- package/lib/remote-storage.js +36 -10
- package/package.json +18 -17
- package/src/bundle.js +1 -1
- package/test/__fixtures__/named-package/actions/action-zip/index.js +1 -1
- package/test/__fixtures__/sample-app/actions/action-zip/index.js +1 -1
- package/test/index.test.js +1 -1
- package/test/jest.setup.js +8 -7
- package/test/lib/getS3Creds.test.js +3 -3
- package/test/lib/remote-storage.test.js +71 -79
package/.eslintrc
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
updates:
|
|
8
|
+
- package-ecosystem: "npm"
|
|
9
|
+
directory: "/"
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "weekly"
|
|
@@ -31,7 +31,6 @@ jobs:
|
|
|
31
31
|
- run: npm test
|
|
32
32
|
- name: upload coverage
|
|
33
33
|
if: success()
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
shell: bash
|
|
34
|
+
uses: codecov/codecov-action@v3.1.1
|
|
35
|
+
with:
|
|
36
|
+
name: ${{ runner.os }} node.js ${{ matrix.node-version }}
|
package/lib/remote-storage.js
CHANGED
|
@@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
|
|
|
10
10
|
governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
const
|
|
13
|
+
const { S3 } = require('@aws-sdk/client-s3')
|
|
14
14
|
const path = require('path')
|
|
15
15
|
const mime = require('mime-types')
|
|
16
16
|
const fs = require('fs-extra')
|
|
@@ -50,8 +50,25 @@ module.exports = class RemoteStorage {
|
|
|
50
50
|
params: joi.object().keys({ Bucket: joi.string().required() }).required()
|
|
51
51
|
}).unknown()
|
|
52
52
|
.validate(creds)
|
|
53
|
-
if (res.error)
|
|
54
|
-
|
|
53
|
+
if (res.error) {
|
|
54
|
+
throw res.error
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// the TVM response could be passed as is to the v2 client constructor, but the v3 client follows a different format
|
|
58
|
+
// see https://github.com/adobe/aio-tvm/issues/85
|
|
59
|
+
const region = creds.region || 'us-east-1'
|
|
60
|
+
// note this must supports TVM + BYO use cases
|
|
61
|
+
// see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/credentials.html
|
|
62
|
+
const credentials = {
|
|
63
|
+
accessKeyId: creds.accessKeyId,
|
|
64
|
+
secretAccessKey: creds.secretAccessKey,
|
|
65
|
+
sessionToken: creds.sessionToken,
|
|
66
|
+
expiration: creds.expiration
|
|
67
|
+
}
|
|
68
|
+
this.bucket = creds.params.Bucket
|
|
69
|
+
|
|
70
|
+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3.html#constructor
|
|
71
|
+
this.s3 = new S3({ credentials, region })
|
|
55
72
|
}
|
|
56
73
|
|
|
57
74
|
/**
|
|
@@ -64,10 +81,12 @@ module.exports = class RemoteStorage {
|
|
|
64
81
|
throw new Error('prefix must be a valid string')
|
|
65
82
|
}
|
|
66
83
|
const listParams = {
|
|
84
|
+
Bucket: this.bucket,
|
|
67
85
|
Prefix: prefix
|
|
68
86
|
}
|
|
69
|
-
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
70
|
-
|
|
87
|
+
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
88
|
+
|
|
89
|
+
return listedObjects.KeyCount > 0
|
|
71
90
|
}
|
|
72
91
|
|
|
73
92
|
/**
|
|
@@ -77,19 +96,22 @@ module.exports = class RemoteStorage {
|
|
|
77
96
|
async emptyFolder (prefix) {
|
|
78
97
|
if (typeof prefix !== 'string') throw new Error('prefix must be a valid string')
|
|
79
98
|
const listParams = {
|
|
99
|
+
Bucket: this.bucket,
|
|
80
100
|
Prefix: prefix
|
|
81
101
|
}
|
|
82
|
-
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
83
|
-
|
|
102
|
+
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
103
|
+
|
|
104
|
+
if (listedObjects.KeyCount < 1) {
|
|
84
105
|
return
|
|
85
106
|
}
|
|
86
107
|
const deleteParams = {
|
|
108
|
+
Bucket: this.bucket,
|
|
87
109
|
Delete: { Objects: [] }
|
|
88
110
|
}
|
|
89
111
|
listedObjects.Contents.forEach(({ Key }) => {
|
|
90
112
|
deleteParams.Delete.Objects.push({ Key })
|
|
91
113
|
})
|
|
92
|
-
await this.s3.deleteObjects(deleteParams)
|
|
114
|
+
await this.s3.deleteObjects(deleteParams)
|
|
93
115
|
if (listedObjects.IsTruncated) {
|
|
94
116
|
await this.emptyFolder(prefix)
|
|
95
117
|
}
|
|
@@ -109,6 +131,7 @@ module.exports = class RemoteStorage {
|
|
|
109
131
|
const mimeType = mime.lookup(path.extname(file))
|
|
110
132
|
const cacheControlString = this._getCacheControlConfig(mimeType, appConfig)
|
|
111
133
|
const uploadParams = {
|
|
134
|
+
Bucket: this.bucket,
|
|
112
135
|
Key: urlJoin(prefix, path.basename(file)),
|
|
113
136
|
Body: content,
|
|
114
137
|
CacheControl: cacheControlString
|
|
@@ -117,7 +140,10 @@ module.exports = class RemoteStorage {
|
|
|
117
140
|
if (mimeType) {
|
|
118
141
|
uploadParams.ContentType = mimeType
|
|
119
142
|
}
|
|
120
|
-
|
|
143
|
+
|
|
144
|
+
// Note: putObject is recommended for files < 100MB and has a limit of 5GB, which is ok for our use case of storing static web assets
|
|
145
|
+
// if we intend to store larger files, we should use multipart upload and https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_lib_storage.html
|
|
146
|
+
return this.s3.putObject(uploadParams)
|
|
121
147
|
}
|
|
122
148
|
|
|
123
149
|
async walkDir (dir) {
|
|
@@ -150,7 +176,7 @@ module.exports = class RemoteStorage {
|
|
|
150
176
|
// parallel upload
|
|
151
177
|
return Promise.all(files.map(async f => {
|
|
152
178
|
// get file's relative folder to the base directory.
|
|
153
|
-
|
|
179
|
+
let prefixDirectory = path.dirname(path.relative(dir, f))
|
|
154
180
|
// base directory returns ".", ignore that.
|
|
155
181
|
prefixDirectory = prefixDirectory === '.' ? '' : prefixDirectory
|
|
156
182
|
// newPrefix is now the initial prefix plus the files relative directory path.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/aio-lib-web",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.2",
|
|
4
4
|
"description": "Utility tooling library to build and deploy Adobe I/O Project Firefly app static sites to CDN",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -32,29 +32,30 @@
|
|
|
32
32
|
"@adobe/aio-lib-core-config": "^3.0.0",
|
|
33
33
|
"@adobe/aio-lib-core-logging": "^2.0.0",
|
|
34
34
|
"@adobe/aio-lib-core-tvm": "^3.0.0",
|
|
35
|
-
"aws-sdk": "^
|
|
36
|
-
"
|
|
35
|
+
"@aws-sdk/client-s3": "^3.276.0",
|
|
36
|
+
"core-js": "^3.25.1",
|
|
37
|
+
"fs-extra": "^10",
|
|
37
38
|
"joi": "^17.2.1",
|
|
38
|
-
"
|
|
39
|
-
"klaw": "^3.0.0",
|
|
39
|
+
"klaw": "^4",
|
|
40
40
|
"lodash.clonedeep": "^4.5.0",
|
|
41
41
|
"mime-types": "^2.1.24",
|
|
42
|
-
"parcel": "2.
|
|
42
|
+
"parcel": "^2.7.0",
|
|
43
43
|
"regenerator-runtime": "^0.13.7"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@types/hapi__joi": "^
|
|
47
|
-
"@types/jest": "^
|
|
48
|
-
"eslint": "^
|
|
49
|
-
"eslint-config-standard": "^
|
|
46
|
+
"@types/hapi__joi": "^17",
|
|
47
|
+
"@types/jest": "^29",
|
|
48
|
+
"eslint": "^8",
|
|
49
|
+
"eslint-config-standard": "^17",
|
|
50
50
|
"eslint-plugin-import": "^2.18.2",
|
|
51
|
-
"eslint-plugin-jest": "^
|
|
52
|
-
"eslint-plugin-
|
|
53
|
-
"eslint-plugin-
|
|
54
|
-
"eslint-plugin-
|
|
55
|
-
"
|
|
51
|
+
"eslint-plugin-jest": "^27.0.2",
|
|
52
|
+
"eslint-plugin-n": "^15.2.5",
|
|
53
|
+
"eslint-plugin-node": "^11",
|
|
54
|
+
"eslint-plugin-promise": "^6",
|
|
55
|
+
"eslint-plugin-standard": "^5",
|
|
56
|
+
"jest": "^29",
|
|
56
57
|
"jsdoc-to-markdown": "^7",
|
|
57
|
-
"memfs": "^3.
|
|
58
|
-
"node-fetch": "^2.6.
|
|
58
|
+
"memfs": "^3.4.7",
|
|
59
|
+
"node-fetch": "^2.6.7"
|
|
59
60
|
}
|
|
60
61
|
}
|
package/src/bundle.js
CHANGED
|
@@ -49,7 +49,7 @@ module.exports = async (entries, dest, options = { shouldOptimize: false }, log
|
|
|
49
49
|
|
|
50
50
|
// set defaults, but allow override by passed in values
|
|
51
51
|
const parcelBundleOptions = {
|
|
52
|
-
entries
|
|
52
|
+
entries,
|
|
53
53
|
defaultConfig: require.resolve('@parcel/config-default'),
|
|
54
54
|
shouldDisableCache: false,
|
|
55
55
|
targets: {
|
|
@@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
|
|
|
9
9
|
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
10
|
governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
|
-
require('
|
|
12
|
+
require('joi')
|
|
13
13
|
function main (args) {
|
|
14
14
|
return 'hello'
|
|
15
15
|
}
|
|
@@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
|
|
|
9
9
|
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
10
|
governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
|
-
require('
|
|
12
|
+
require('joi')
|
|
13
13
|
function main (args) {
|
|
14
14
|
return 'hello'
|
|
15
15
|
}
|
package/test/index.test.js
CHANGED
|
@@ -12,7 +12,7 @@ governing permissions and limitations under the License.
|
|
|
12
12
|
|
|
13
13
|
const AppScripts = require('../index')
|
|
14
14
|
|
|
15
|
-
describe('AppScripts has expected interface
|
|
15
|
+
describe('AppScripts has expected interface', () => {
|
|
16
16
|
test('exports functions', async () => {
|
|
17
17
|
expect(AppScripts.buildWeb).toBeDefined()
|
|
18
18
|
expect(typeof AppScripts.buildWeb).toBe('function')
|
package/test/jest.setup.js
CHANGED
|
@@ -19,11 +19,6 @@ process.on('unhandledRejection', error => {
|
|
|
19
19
|
throw error
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
beforeEach(() => {
|
|
23
|
-
// expect every test to have assertions
|
|
24
|
-
expect.hasAssertions()
|
|
25
|
-
})
|
|
26
|
-
|
|
27
22
|
const fixturePath = path.join(__dirname, '__fixtures__')
|
|
28
23
|
|
|
29
24
|
// quick normalization to test windows paths
|
|
@@ -45,7 +40,7 @@ global.r = p => path.resolve(p)
|
|
|
45
40
|
* @returns {object}
|
|
46
41
|
*/
|
|
47
42
|
function readFilesIntoObjectSync (filePath, toDir) {
|
|
48
|
-
const fsReal =
|
|
43
|
+
const fsReal = jest.requireActual('fs')
|
|
49
44
|
const flatObj = {}
|
|
50
45
|
|
|
51
46
|
function _readFilesIntoObjectSync (_filePathRec, _toDirRec, first = false) {
|
|
@@ -59,7 +54,7 @@ function readFilesIntoObjectSync (filePath, toDir) {
|
|
|
59
54
|
}
|
|
60
55
|
// is dir
|
|
61
56
|
const files = fsReal.readdirSync(_filePathRec)
|
|
62
|
-
files.
|
|
57
|
+
files.forEach(f => {
|
|
63
58
|
const fullPath = path.join(_filePathRec, f)
|
|
64
59
|
// skip first dir from path
|
|
65
60
|
_readFilesIntoObjectSync(fullPath, first ? _toDirRec : path.join(_toDirRec, path.basename(_filePathRec)))
|
|
@@ -160,6 +155,12 @@ global.fakeTVMResponse = {
|
|
|
160
155
|
params: { Bucket: global.fakeS3Bucket }
|
|
161
156
|
}
|
|
162
157
|
|
|
158
|
+
global.fakeBYOCredentials = {
|
|
159
|
+
accessKeyId: 'fake',
|
|
160
|
+
secretAccessKey: 'fake',
|
|
161
|
+
params: { Bucket: global.fakeS3Bucket }
|
|
162
|
+
}
|
|
163
|
+
|
|
163
164
|
global.expectedScripts = expect.objectContaining({
|
|
164
165
|
buildUI: expect.any(Function),
|
|
165
166
|
deployUI: expect.any(Function),
|
|
@@ -44,7 +44,7 @@ describe('getS3Credentials', () => {
|
|
|
44
44
|
test('gets credentials from tvm', async () => {
|
|
45
45
|
await expect(getS3Credentials({ ow: { namespace: 'ns', auth: 'auth' } }))
|
|
46
46
|
.resolves.toEqual(fakeReturnedTvmCreds)
|
|
47
|
-
expect(mockTVM.init).
|
|
47
|
+
expect(mockTVM.init).toHaveBeenCalledWith({
|
|
48
48
|
apiUrl: undefined,
|
|
49
49
|
cacheFile: undefined,
|
|
50
50
|
ow: { auth: 'auth', namespace: 'ns' }
|
|
@@ -54,7 +54,7 @@ describe('getS3Credentials', () => {
|
|
|
54
54
|
test('gets credentials from tvm with custom tvmurl', async () => {
|
|
55
55
|
await expect(getS3Credentials({ ow: { namespace: 'ns', auth: 'auth' }, s3: { tvmUrl: 'custom' } }))
|
|
56
56
|
.resolves.toEqual(fakeReturnedTvmCreds)
|
|
57
|
-
expect(mockTVM.init).
|
|
57
|
+
expect(mockTVM.init).toHaveBeenCalledWith({
|
|
58
58
|
apiUrl: 'custom',
|
|
59
59
|
cacheFile: undefined,
|
|
60
60
|
ow: { auth: 'auth', namespace: 'ns' }
|
|
@@ -64,7 +64,7 @@ describe('getS3Credentials', () => {
|
|
|
64
64
|
test('gets credentials from tvm with custom credsCacheFile', async () => {
|
|
65
65
|
await expect(getS3Credentials({ ow: { namespace: 'ns', auth: 'auth' }, s3: { credsCacheFile: 'custom' } }))
|
|
66
66
|
.resolves.toEqual(fakeReturnedTvmCreds)
|
|
67
|
-
expect(mockTVM.init).
|
|
67
|
+
expect(mockTVM.init).toHaveBeenCalledWith({
|
|
68
68
|
apiUrl: undefined,
|
|
69
69
|
cacheFile: 'custom',
|
|
70
70
|
ow: { auth: 'auth', namespace: 'ns' }
|
|
@@ -10,26 +10,27 @@ OF ANY KIND, either express or implied. See the License for the specific languag
|
|
|
10
10
|
governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
const mockS3 = {
|
|
14
|
+
listObjectsV2: jest.fn(),
|
|
15
|
+
deleteObjects: jest.fn(),
|
|
16
|
+
putObject: jest.fn()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
jest.mock('@aws-sdk/client-s3', () => Object({ S3: jest.fn(() => { return mockS3 }) }))
|
|
20
|
+
|
|
21
|
+
const { S3 } = require('@aws-sdk/client-s3')
|
|
13
22
|
const { vol } = global.mockFs()
|
|
14
23
|
|
|
15
24
|
const RemoteStorage = require('../../lib/remote-storage')
|
|
16
|
-
const aws = require('aws-sdk')
|
|
17
|
-
|
|
18
|
-
function spyS3 (funcs) {
|
|
19
|
-
const newFuncs = {}
|
|
20
|
-
// fake .promise() instead
|
|
21
|
-
Object.keys(funcs).forEach(name => {
|
|
22
|
-
newFuncs[name] = (...args) => Object({ promise: funcs[name].bind(null, ...args) })
|
|
23
|
-
})
|
|
24
|
-
return jest.spyOn(aws, 'S3').mockImplementation(() => {
|
|
25
|
-
return newFuncs
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
25
|
|
|
29
26
|
describe('RemoteStorage', () => {
|
|
30
27
|
beforeEach(() => {
|
|
31
|
-
//
|
|
32
|
-
|
|
28
|
+
// resets all mock s3 functions, do not use jest.resetAllMocks() as it also resets the s3 client constructor mock
|
|
29
|
+
mockS3.listObjectsV2.mockReset()
|
|
30
|
+
mockS3.deleteObjects.mockReset()
|
|
31
|
+
mockS3.putObject.mockReset()
|
|
32
|
+
S3.mockClear()
|
|
33
|
+
// resets the mock fs
|
|
33
34
|
global.cleanFs(vol)
|
|
34
35
|
})
|
|
35
36
|
|
|
@@ -38,6 +39,32 @@ describe('RemoteStorage', () => {
|
|
|
38
39
|
expect(instantiate.bind(this)).toThrowWithMessageContaining(['required'])
|
|
39
40
|
})
|
|
40
41
|
|
|
42
|
+
test('Constructor initializes the S3 constructor properly using tvm credentials', async () => {
|
|
43
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
44
|
+
expect(S3).toHaveBeenCalledWith({
|
|
45
|
+
credentials: {
|
|
46
|
+
accessKeyId: global.fakeTVMResponse.accessKeyId,
|
|
47
|
+
secretAccessKey: global.fakeTVMResponse.secretAccessKey,
|
|
48
|
+
sessionToken: global.fakeTVMResponse.sessionToken,
|
|
49
|
+
expiration: global.fakeTVMResponse.expiration
|
|
50
|
+
},
|
|
51
|
+
region: 'us-east-1'
|
|
52
|
+
})
|
|
53
|
+
rs.bucket = global.fakeTVMResponse.Bucket
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('Constructor initializes the S3 constructor properly using byo credentials', async () => {
|
|
57
|
+
const rs = new RemoteStorage(global.fakeBYOCredentials)
|
|
58
|
+
expect(S3).toHaveBeenCalledWith({
|
|
59
|
+
credentials: {
|
|
60
|
+
accessKeyId: global.fakeTVMResponse.accessKeyId,
|
|
61
|
+
secretAccessKey: global.fakeTVMResponse.secretAccessKey
|
|
62
|
+
},
|
|
63
|
+
region: 'us-east-1'
|
|
64
|
+
})
|
|
65
|
+
rs.bucket = global.fakeTVMResponse.Bucket
|
|
66
|
+
})
|
|
67
|
+
|
|
41
68
|
test('folderExists missing prefix', async () => {
|
|
42
69
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
43
70
|
await expect(rs.folderExists()).rejects.toEqual(expect.objectContaining({ message: 'prefix must be a valid string' }))
|
|
@@ -59,134 +86,99 @@ describe('RemoteStorage', () => {
|
|
|
59
86
|
})
|
|
60
87
|
|
|
61
88
|
test('folderExists should return false if there are no files', async () => {
|
|
62
|
-
|
|
89
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 0 })
|
|
63
90
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
64
91
|
expect((await rs.folderExists('fakeprefix'))).toBe(false)
|
|
92
|
+
expect(mockS3.listObjectsV2).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Prefix: 'fakeprefix' })
|
|
65
93
|
})
|
|
66
94
|
|
|
67
95
|
test('folderExists should return true if there are files', async () => {
|
|
68
|
-
|
|
96
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 1 })
|
|
69
97
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
70
98
|
expect((await rs.folderExists('fakeprefix'))).toBe(true)
|
|
71
99
|
})
|
|
72
100
|
|
|
73
101
|
test('emptyFolder should not throw if there are no files', async () => {
|
|
74
|
-
|
|
102
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 0 })
|
|
75
103
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
76
104
|
expect(rs.emptyFolder.bind(rs, 'fakeprefix')).not.toThrow()
|
|
77
105
|
})
|
|
78
106
|
|
|
79
107
|
test('emptyFolder should not call S3#deleteObjects if already empty', async () => {
|
|
80
|
-
|
|
81
|
-
spyS3({
|
|
82
|
-
listObjectsV2: () => Object({ Contents: [] }),
|
|
83
|
-
deleteObjects: deleteMock
|
|
84
|
-
})
|
|
108
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 0 })
|
|
85
109
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
86
110
|
await rs.emptyFolder('fakeprefix')
|
|
87
|
-
expect(
|
|
111
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledTimes(0)
|
|
88
112
|
})
|
|
89
113
|
|
|
90
114
|
test('emptyFolder should call S3#deleteObjects with correct parameters with one file', async () => {
|
|
91
|
-
const deleteMock = jest.fn()
|
|
92
115
|
const content = [{ Key: 'fakeprefix/index.html' }]
|
|
93
|
-
|
|
94
|
-
listObjectsV2: () => Object({ Contents: content }),
|
|
95
|
-
deleteObjects: deleteMock
|
|
96
|
-
})
|
|
116
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 1, Contents: content })
|
|
97
117
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
98
118
|
await rs.emptyFolder('fakeprefix')
|
|
99
|
-
expect(
|
|
119
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: content } })
|
|
100
120
|
})
|
|
101
121
|
|
|
102
122
|
test('emptyFolder should call S3#deleteObjects with correct parameters with multiple files', async () => {
|
|
103
|
-
const deleteMock = jest.fn()
|
|
104
123
|
const content = [{ Key: 'fakeprefix/index.html' }, { Key: 'fakeprefix/index.css' }, { Key: 'fakeprefix/index.css' }]
|
|
105
|
-
|
|
106
|
-
listObjectsV2: () => Object({ Contents: content }),
|
|
107
|
-
deleteObjects: deleteMock
|
|
108
|
-
})
|
|
124
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 3, Contents: content })
|
|
109
125
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
110
126
|
await rs.emptyFolder('fakeprefix')
|
|
111
|
-
expect(
|
|
127
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: content } })
|
|
112
128
|
})
|
|
113
129
|
|
|
114
130
|
test('emptyFolder should call S3#deleteObjects multiple time if listObjects is truncated', async () => {
|
|
115
|
-
const deleteMock = jest.fn()
|
|
116
131
|
const content = [{ Key: 'fakeprefix/index.html' }, { Key: 'fakeprefix/index.css' }, { Key: 'fakeprefix/index.js' }]
|
|
117
|
-
let iterations =
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return Object({ Contents: [content[iterations]], IsTruncated })
|
|
123
|
-
},
|
|
124
|
-
deleteObjects: deleteMock
|
|
132
|
+
let iterations = 2
|
|
133
|
+
mockS3.listObjectsV2.mockImplementation(() => {
|
|
134
|
+
const res = { Contents: [content[iterations]], IsTruncated: iterations > 0 }
|
|
135
|
+
iterations--
|
|
136
|
+
return Promise.resolve(res)
|
|
125
137
|
})
|
|
126
138
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
127
139
|
await rs.emptyFolder('fakeprefix')
|
|
128
|
-
expect(
|
|
129
|
-
expect(
|
|
130
|
-
expect(
|
|
140
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: [content[0]] } })
|
|
141
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: [content[1]] } })
|
|
142
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: [content[2]] } })
|
|
131
143
|
})
|
|
132
144
|
|
|
133
145
|
test('uploadFile should call S3#upload with the correct parameters', async () => {
|
|
134
146
|
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
135
|
-
const uploadMock = jest.fn()
|
|
136
|
-
spyS3({
|
|
137
|
-
upload: uploadMock
|
|
138
|
-
})
|
|
139
147
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
140
148
|
const fakeConfig = {}
|
|
141
149
|
await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig)
|
|
142
150
|
const body = Buffer.from('fake content', 'utf8')
|
|
143
|
-
expect(
|
|
151
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: 'fakeprefix/index.js', Body: body, ContentType: 'application/javascript' }))
|
|
144
152
|
})
|
|
145
153
|
|
|
146
154
|
test('uploadFile should call S3#upload with the correct parameters and slash-prefix', async () => {
|
|
147
155
|
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
148
|
-
const uploadMock = jest.fn()
|
|
149
|
-
spyS3({
|
|
150
|
-
upload: uploadMock
|
|
151
|
-
})
|
|
152
156
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
153
157
|
const fakeConfig = {}
|
|
154
158
|
await rs.uploadFile('fakeDir/index.js', '/slash-prefix', fakeConfig)
|
|
155
159
|
const body = Buffer.from('fake content', 'utf8')
|
|
156
|
-
expect(
|
|
160
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: '/slash-prefix/index.js', Body: body, ContentType: 'application/javascript' }))
|
|
157
161
|
})
|
|
158
162
|
|
|
159
|
-
test('uploadFile S3#upload with
|
|
163
|
+
test('uploadFile S3#upload with an unknown Content-Type', async () => {
|
|
160
164
|
global.addFakeFiles(vol, 'fakeDir', { 'index.mst': 'fake content' })
|
|
161
|
-
let uploadParams
|
|
162
|
-
const uploadMock = jest.fn((params) => { uploadParams = params })
|
|
163
|
-
spyS3({
|
|
164
|
-
upload: uploadMock
|
|
165
|
-
})
|
|
166
165
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
167
166
|
const fakeConfig = {}
|
|
168
167
|
await rs.uploadFile('fakeDir/index.mst', 'fakeprefix', fakeConfig)
|
|
169
|
-
|
|
170
|
-
expect(
|
|
168
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
169
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: 'fakeprefix/index.mst', Body: body }))
|
|
170
|
+
expect(mockS3.putObject.mock.calls[0][0]).not.toHaveProperty('ContentType')
|
|
171
171
|
})
|
|
172
172
|
|
|
173
173
|
test('uploadDir should call S3#upload one time per file', async () => {
|
|
174
174
|
await global.addFakeFiles(vol, 'fakeDir', ['index.js', 'index.css', 'index.html'])
|
|
175
|
-
const uploadMock = jest.fn()
|
|
176
|
-
spyS3({
|
|
177
|
-
upload: uploadMock
|
|
178
|
-
})
|
|
179
175
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
180
176
|
await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig.creds.cna)
|
|
181
|
-
expect(
|
|
177
|
+
expect(mockS3.putObject).toHaveBeenCalledTimes(3)
|
|
182
178
|
})
|
|
183
179
|
|
|
184
180
|
test('uploadDir should call a callback once per uploaded file', async () => {
|
|
185
181
|
await global.addFakeFiles(vol, 'fakeDir', ['index.js', 'index.css', 'index.html', 'test/i.js'])
|
|
186
|
-
const uploadMock = jest.fn()
|
|
187
|
-
spyS3({
|
|
188
|
-
upload: uploadMock
|
|
189
|
-
})
|
|
190
182
|
const cbMock = jest.fn()
|
|
191
183
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
192
184
|
|
|
@@ -194,31 +186,31 @@ describe('RemoteStorage', () => {
|
|
|
194
186
|
expect(cbMock).toHaveBeenCalledTimes(4)
|
|
195
187
|
})
|
|
196
188
|
|
|
197
|
-
test('
|
|
189
|
+
test('cachecontrol string for html', async () => {
|
|
198
190
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
199
191
|
const response = rs._getCacheControlConfig('text/html', global.fakeConfig.cna)
|
|
200
192
|
expect(response).toBe('s-maxage=0, max-age=60')
|
|
201
193
|
})
|
|
202
194
|
|
|
203
|
-
test('
|
|
195
|
+
test('cachecontrol string for JS', async () => {
|
|
204
196
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
205
197
|
const response = rs._getCacheControlConfig('application/javascript', global.fakeConfig.cna)
|
|
206
198
|
expect(response).toBe('s-maxage=0, max-age=604800')
|
|
207
199
|
})
|
|
208
200
|
|
|
209
|
-
test('
|
|
201
|
+
test('cachecontrol string for CSS', async () => {
|
|
210
202
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
211
203
|
const response = rs._getCacheControlConfig('text/css', global.fakeConfig.cna)
|
|
212
204
|
expect(response).toBe('s-maxage=0, max-age=604800')
|
|
213
205
|
})
|
|
214
206
|
|
|
215
|
-
test('
|
|
207
|
+
test('cachecontrol string for Image', async () => {
|
|
216
208
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
217
209
|
const response = rs._getCacheControlConfig('image/jpeg', global.fakeConfig.cna)
|
|
218
210
|
expect(response).toBe('s-maxage=0, max-age=604800')
|
|
219
211
|
})
|
|
220
212
|
|
|
221
|
-
test('
|
|
213
|
+
test('cachecontrol string for default', async () => {
|
|
222
214
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
223
215
|
const response = rs._getCacheControlConfig('application/pdf', global.fakeConfig.cna)
|
|
224
216
|
expect(response).toBe('s-maxage=0')
|