@adobe/aio-lib-web 6.0.0 → 6.0.2-pre.2023-03-31.0e449698
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/StorageError.js +42 -0
- package/lib/remote-storage.js +111 -13
- package/package.json +22 -20
- package/src/bundle.js +1 -1
- package/src/deploy-web.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 +22 -8
- package/test/lib/getS3Creds.test.js +3 -3
- package/test/lib/remote-storage.test.js +331 -91
- package/test/src/deploy-web.test.js +4 -4
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 }}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2023 Adobe. All rights reserved.
|
|
3
|
+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { ErrorWrapper, createUpdater } = require('@adobe/aio-lib-core-errors').AioCoreSDKErrorWrapper
|
|
14
|
+
const logger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web', { provider: 'debug' })
|
|
15
|
+
|
|
16
|
+
const codes = {}
|
|
17
|
+
const messages = new Map()
|
|
18
|
+
|
|
19
|
+
const Updater = createUpdater(
|
|
20
|
+
codes,
|
|
21
|
+
messages
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
const E = ErrorWrapper(
|
|
25
|
+
'WebStorageError',
|
|
26
|
+
'WebLib',
|
|
27
|
+
Updater
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
E('ERROR_INVALID_HEADER_NAME', '`%s` is not a valid response header name')
|
|
31
|
+
E('ERROR_INVALID_HEADER_VALUE', '`%s` is not a valid response header value for `%s`')
|
|
32
|
+
|
|
33
|
+
function logAndThrow (e) {
|
|
34
|
+
logger.error(JSON.stringify(e, null, 2))
|
|
35
|
+
throw e
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = {
|
|
39
|
+
codes,
|
|
40
|
+
messages,
|
|
41
|
+
logAndThrow
|
|
42
|
+
}
|
package/lib/remote-storage.js
CHANGED
|
@@ -10,12 +10,16 @@ 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')
|
|
17
17
|
const joi = require('joi')
|
|
18
18
|
const klaw = require('klaw')
|
|
19
|
+
const http = require('http')
|
|
20
|
+
const { codes, logAndThrow } = require('./StorageError')
|
|
21
|
+
|
|
22
|
+
const fileExtensionPattern = /\*\.[0-9a-zA-Z]+$/
|
|
19
23
|
|
|
20
24
|
// /**
|
|
21
25
|
// * Joins url path parts
|
|
@@ -50,8 +54,25 @@ module.exports = class RemoteStorage {
|
|
|
50
54
|
params: joi.object().keys({ Bucket: joi.string().required() }).required()
|
|
51
55
|
}).unknown()
|
|
52
56
|
.validate(creds)
|
|
53
|
-
if (res.error)
|
|
54
|
-
|
|
57
|
+
if (res.error) {
|
|
58
|
+
throw res.error
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// the TVM response could be passed as is to the v2 client constructor, but the v3 client follows a different format
|
|
62
|
+
// see https://github.com/adobe/aio-tvm/issues/85
|
|
63
|
+
const region = creds.region || 'us-east-1'
|
|
64
|
+
// note this must supports TVM + BYO use cases
|
|
65
|
+
// see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/credentials.html
|
|
66
|
+
const credentials = {
|
|
67
|
+
accessKeyId: creds.accessKeyId,
|
|
68
|
+
secretAccessKey: creds.secretAccessKey,
|
|
69
|
+
sessionToken: creds.sessionToken,
|
|
70
|
+
expiration: creds.expiration
|
|
71
|
+
}
|
|
72
|
+
this.bucket = creds.params.Bucket
|
|
73
|
+
|
|
74
|
+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3.html#constructor
|
|
75
|
+
this.s3 = new S3({ credentials, region })
|
|
55
76
|
}
|
|
56
77
|
|
|
57
78
|
/**
|
|
@@ -64,10 +85,12 @@ module.exports = class RemoteStorage {
|
|
|
64
85
|
throw new Error('prefix must be a valid string')
|
|
65
86
|
}
|
|
66
87
|
const listParams = {
|
|
88
|
+
Bucket: this.bucket,
|
|
67
89
|
Prefix: prefix
|
|
68
90
|
}
|
|
69
|
-
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
70
|
-
|
|
91
|
+
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
92
|
+
|
|
93
|
+
return listedObjects.KeyCount > 0
|
|
71
94
|
}
|
|
72
95
|
|
|
73
96
|
/**
|
|
@@ -77,19 +100,22 @@ module.exports = class RemoteStorage {
|
|
|
77
100
|
async emptyFolder (prefix) {
|
|
78
101
|
if (typeof prefix !== 'string') throw new Error('prefix must be a valid string')
|
|
79
102
|
const listParams = {
|
|
103
|
+
Bucket: this.bucket,
|
|
80
104
|
Prefix: prefix
|
|
81
105
|
}
|
|
82
|
-
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
83
|
-
|
|
106
|
+
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
107
|
+
|
|
108
|
+
if (listedObjects.KeyCount < 1) {
|
|
84
109
|
return
|
|
85
110
|
}
|
|
86
111
|
const deleteParams = {
|
|
112
|
+
Bucket: this.bucket,
|
|
87
113
|
Delete: { Objects: [] }
|
|
88
114
|
}
|
|
89
115
|
listedObjects.Contents.forEach(({ Key }) => {
|
|
90
116
|
deleteParams.Delete.Objects.push({ Key })
|
|
91
117
|
})
|
|
92
|
-
await this.s3.deleteObjects(deleteParams)
|
|
118
|
+
await this.s3.deleteObjects(deleteParams)
|
|
93
119
|
if (listedObjects.IsTruncated) {
|
|
94
120
|
await this.emptyFolder(prefix)
|
|
95
121
|
}
|
|
@@ -100,24 +126,96 @@ module.exports = class RemoteStorage {
|
|
|
100
126
|
* @param {string} file
|
|
101
127
|
* @param {string} prefix - prefix to upload the file to
|
|
102
128
|
* @param {Object} appConfig - application config
|
|
129
|
+
* @param {string} distRoot - Distribution root dir
|
|
103
130
|
*/
|
|
104
|
-
async uploadFile (file, prefix, appConfig) {
|
|
131
|
+
async uploadFile (file, prefix, appConfig, distRoot) {
|
|
105
132
|
if (typeof prefix !== 'string') {
|
|
106
133
|
throw new Error('prefix must be a valid string')
|
|
107
134
|
}
|
|
108
135
|
const content = await fs.readFile(file)
|
|
109
136
|
const mimeType = mime.lookup(path.extname(file))
|
|
110
|
-
const cacheControlString = this._getCacheControlConfig(mimeType, appConfig)
|
|
137
|
+
const cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
|
|
111
138
|
const uploadParams = {
|
|
139
|
+
Bucket: this.bucket,
|
|
112
140
|
Key: urlJoin(prefix, path.basename(file)),
|
|
113
141
|
Body: content,
|
|
114
142
|
CacheControl: cacheControlString
|
|
115
143
|
}
|
|
144
|
+
// add response headers if specified in manifest
|
|
145
|
+
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig)
|
|
146
|
+
if (responseHeaders) {
|
|
147
|
+
uploadParams.Metadata = responseHeaders
|
|
148
|
+
}
|
|
116
149
|
// s3 misses some mime types like for css files
|
|
117
150
|
if (mimeType) {
|
|
118
151
|
uploadParams.ContentType = mimeType
|
|
119
152
|
}
|
|
120
|
-
|
|
153
|
+
|
|
154
|
+
// 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
|
|
155
|
+
// 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
|
|
156
|
+
return this.s3.putObject(uploadParams)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
getResponseHeadersForFile (file, distRoot, appConfig) {
|
|
160
|
+
let responseHeaders
|
|
161
|
+
if (appConfig.web && appConfig.web['response-headers']) {
|
|
162
|
+
responseHeaders = {}
|
|
163
|
+
const cdnConfig = appConfig.web['response-headers']
|
|
164
|
+
const headerPrefix = 'adp-'
|
|
165
|
+
|
|
166
|
+
Object.keys(cdnConfig).forEach(rule => {
|
|
167
|
+
if (this.canAddHeader(file, distRoot, rule)) {
|
|
168
|
+
Object.keys(cdnConfig[rule]).forEach(header => {
|
|
169
|
+
this.validateHTTPHeader(header, cdnConfig[rule][header])
|
|
170
|
+
responseHeaders[headerPrefix + header] = cdnConfig[rule][header]
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
return responseHeaders
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
canAddHeader (file, distRoot, rule) {
|
|
179
|
+
const filePath = path.parse(file)
|
|
180
|
+
const normalisedRule = rule.replace(/\//g, path.sep)
|
|
181
|
+
const ruleFolderPath = path.parse(normalisedRule)
|
|
182
|
+
let folderPathToMatch = path.join(distRoot, ruleFolderPath.dir)
|
|
183
|
+
if (folderPathToMatch.endsWith(path.sep)) {
|
|
184
|
+
folderPathToMatch = folderPathToMatch.substring(0, folderPathToMatch.length - 1) // remove any trailing path separator
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (rule === '/*') { // all content
|
|
188
|
+
return true
|
|
189
|
+
} else if (rule.endsWith('/*')) { // all content in a folder ex. /test/*
|
|
190
|
+
if (filePath.dir.startsWith(folderPathToMatch)) { // matches with the folder
|
|
191
|
+
return true
|
|
192
|
+
}
|
|
193
|
+
} else if (fileExtensionPattern.test(rule)) { // all content with a given extension ex. /*.html or /test/*.js
|
|
194
|
+
// check file has same extension as specified in header
|
|
195
|
+
if ((filePath.ext === ruleFolderPath.ext) && (filePath.dir.startsWith(folderPathToMatch))) {
|
|
196
|
+
return true
|
|
197
|
+
}
|
|
198
|
+
} else { // specific file match ex. /test/foo.js
|
|
199
|
+
const uploadFilePath = path.join(distRoot, normalisedRule)
|
|
200
|
+
if (file === uploadFilePath) {
|
|
201
|
+
return true
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return false
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
validateHTTPHeader (headerName, value) {
|
|
208
|
+
try {
|
|
209
|
+
http.validateHeaderName(headerName)
|
|
210
|
+
} catch (e) {
|
|
211
|
+
logAndThrow(new codes.ERROR_INVALID_HEADER_NAME({ messageValues: [headerName], sdkDetails: {} }))
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
http.validateHeaderValue(headerName, value)
|
|
216
|
+
} catch (e) {
|
|
217
|
+
logAndThrow(new codes.ERROR_INVALID_HEADER_VALUE({ messageValues: [value, headerName], sdkDetails: {} }))
|
|
218
|
+
}
|
|
121
219
|
}
|
|
122
220
|
|
|
123
221
|
async walkDir (dir) {
|
|
@@ -150,12 +248,12 @@ module.exports = class RemoteStorage {
|
|
|
150
248
|
// parallel upload
|
|
151
249
|
return Promise.all(files.map(async f => {
|
|
152
250
|
// get file's relative folder to the base directory.
|
|
153
|
-
|
|
251
|
+
let prefixDirectory = path.dirname(path.relative(dir, f))
|
|
154
252
|
// base directory returns ".", ignore that.
|
|
155
253
|
prefixDirectory = prefixDirectory === '.' ? '' : prefixDirectory
|
|
156
254
|
// newPrefix is now the initial prefix plus the files relative directory path.
|
|
157
255
|
const newPrefix = urlJoin(prefix, prefixDirectory)
|
|
158
|
-
const s3Res = await this.uploadFile(f, newPrefix, appConfig)
|
|
256
|
+
const s3Res = await this.uploadFile(f, newPrefix, appConfig, dir)
|
|
159
257
|
|
|
160
258
|
if (postFileUploadCallback) {
|
|
161
259
|
postFileUploadCallback(f)
|
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-pre.2023-03-31.0e449698",
|
|
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": {
|
|
@@ -30,31 +30,33 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@adobe/aio-lib-core-config": "^3.0.0",
|
|
33
|
-
"@adobe/aio-lib-core-logging": "
|
|
33
|
+
"@adobe/aio-lib-core-logging": "next",
|
|
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.
|
|
59
|
-
}
|
|
60
|
-
|
|
58
|
+
"memfs": "^3.4.7",
|
|
59
|
+
"node-fetch": "^2.6.7"
|
|
60
|
+
},
|
|
61
|
+
"prereleaseSha": "0e449698f2951042566763b4669fe31a5601fcd5"
|
|
62
|
+
}
|
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: {
|
package/src/deploy-web.js
CHANGED
|
@@ -42,7 +42,7 @@ const deployWeb = async (config, log) => {
|
|
|
42
42
|
await remoteStorage.emptyFolder(config.s3.folder + '/')
|
|
43
43
|
}
|
|
44
44
|
const _log = log ? (f) => log(`deploying ${path.relative(dist, f)}`) : null
|
|
45
|
-
await remoteStorage.uploadDir(dist, config.s3.folder, config
|
|
45
|
+
await remoteStorage.uploadDir(dist, config.s3.folder, config, _log)
|
|
46
46
|
|
|
47
47
|
const url = `https://${config.ow.namespace}.${config.app.hostname}/index.html`
|
|
48
48
|
return url
|
|
@@ -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)))
|
|
@@ -117,6 +112,12 @@ global.configWithMissing = (config, members) => {
|
|
|
117
112
|
return config
|
|
118
113
|
}
|
|
119
114
|
|
|
115
|
+
global.configWithModifiedWeb = (config, newWebConfig) => {
|
|
116
|
+
config = cloneDeep(config)
|
|
117
|
+
config.web = newWebConfig
|
|
118
|
+
return config
|
|
119
|
+
}
|
|
120
|
+
|
|
120
121
|
global.fakeS3Bucket = 'fake-bucket'
|
|
121
122
|
global.fakeConfig = {
|
|
122
123
|
tvm: {
|
|
@@ -144,11 +145,18 @@ global.fakeConfig = {
|
|
|
144
145
|
awssecretaccesskey: 'fakeAwsSecretKey'
|
|
145
146
|
}
|
|
146
147
|
},
|
|
147
|
-
|
|
148
|
+
app: {
|
|
148
149
|
htmlCacheDuration: 60,
|
|
149
150
|
jsCacheDuration: 604800,
|
|
150
151
|
cssCacheDuration: 604800,
|
|
151
152
|
imageCacheDuration: 604800
|
|
153
|
+
},
|
|
154
|
+
web: {
|
|
155
|
+
'response-headers': {
|
|
156
|
+
'/*': {
|
|
157
|
+
testHeader: 'foo'
|
|
158
|
+
}
|
|
159
|
+
}
|
|
152
160
|
}
|
|
153
161
|
}
|
|
154
162
|
|
|
@@ -160,6 +168,12 @@ global.fakeTVMResponse = {
|
|
|
160
168
|
params: { Bucket: global.fakeS3Bucket }
|
|
161
169
|
}
|
|
162
170
|
|
|
171
|
+
global.fakeBYOCredentials = {
|
|
172
|
+
accessKeyId: 'fake',
|
|
173
|
+
secretAccessKey: 'fake',
|
|
174
|
+
params: { Bucket: global.fakeS3Bucket }
|
|
175
|
+
}
|
|
176
|
+
|
|
163
177
|
global.expectedScripts = expect.objectContaining({
|
|
164
178
|
buildUI: expect.any(Function),
|
|
165
179
|
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,28 @@ 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()
|
|
23
|
+
const path = require('path')
|
|
14
24
|
|
|
15
25
|
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
26
|
|
|
29
27
|
describe('RemoteStorage', () => {
|
|
30
28
|
beforeEach(() => {
|
|
31
|
-
//
|
|
32
|
-
|
|
29
|
+
// resets all mock s3 functions, do not use jest.resetAllMocks() as it also resets the s3 client constructor mock
|
|
30
|
+
mockS3.listObjectsV2.mockReset()
|
|
31
|
+
mockS3.deleteObjects.mockReset()
|
|
32
|
+
mockS3.putObject.mockReset()
|
|
33
|
+
S3.mockClear()
|
|
34
|
+
// resets the mock fs
|
|
33
35
|
global.cleanFs(vol)
|
|
34
36
|
})
|
|
35
37
|
|
|
@@ -38,6 +40,32 @@ describe('RemoteStorage', () => {
|
|
|
38
40
|
expect(instantiate.bind(this)).toThrowWithMessageContaining(['required'])
|
|
39
41
|
})
|
|
40
42
|
|
|
43
|
+
test('Constructor initializes the S3 constructor properly using tvm credentials', async () => {
|
|
44
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
45
|
+
expect(S3).toHaveBeenCalledWith({
|
|
46
|
+
credentials: {
|
|
47
|
+
accessKeyId: global.fakeTVMResponse.accessKeyId,
|
|
48
|
+
secretAccessKey: global.fakeTVMResponse.secretAccessKey,
|
|
49
|
+
sessionToken: global.fakeTVMResponse.sessionToken,
|
|
50
|
+
expiration: global.fakeTVMResponse.expiration
|
|
51
|
+
},
|
|
52
|
+
region: 'us-east-1'
|
|
53
|
+
})
|
|
54
|
+
rs.bucket = global.fakeTVMResponse.Bucket
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('Constructor initializes the S3 constructor properly using byo credentials', async () => {
|
|
58
|
+
const rs = new RemoteStorage(global.fakeBYOCredentials)
|
|
59
|
+
expect(S3).toHaveBeenCalledWith({
|
|
60
|
+
credentials: {
|
|
61
|
+
accessKeyId: global.fakeTVMResponse.accessKeyId,
|
|
62
|
+
secretAccessKey: global.fakeTVMResponse.secretAccessKey
|
|
63
|
+
},
|
|
64
|
+
region: 'us-east-1'
|
|
65
|
+
})
|
|
66
|
+
rs.bucket = global.fakeTVMResponse.Bucket
|
|
67
|
+
})
|
|
68
|
+
|
|
41
69
|
test('folderExists missing prefix', async () => {
|
|
42
70
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
43
71
|
await expect(rs.folderExists()).rejects.toEqual(expect.objectContaining({ message: 'prefix must be a valid string' }))
|
|
@@ -59,168 +87,380 @@ describe('RemoteStorage', () => {
|
|
|
59
87
|
})
|
|
60
88
|
|
|
61
89
|
test('folderExists should return false if there are no files', async () => {
|
|
62
|
-
|
|
90
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 0 })
|
|
63
91
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
64
92
|
expect((await rs.folderExists('fakeprefix'))).toBe(false)
|
|
93
|
+
expect(mockS3.listObjectsV2).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Prefix: 'fakeprefix' })
|
|
65
94
|
})
|
|
66
95
|
|
|
67
96
|
test('folderExists should return true if there are files', async () => {
|
|
68
|
-
|
|
97
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 1 })
|
|
69
98
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
70
99
|
expect((await rs.folderExists('fakeprefix'))).toBe(true)
|
|
71
100
|
})
|
|
72
101
|
|
|
73
102
|
test('emptyFolder should not throw if there are no files', async () => {
|
|
74
|
-
|
|
103
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 0 })
|
|
75
104
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
76
105
|
expect(rs.emptyFolder.bind(rs, 'fakeprefix')).not.toThrow()
|
|
77
106
|
})
|
|
78
107
|
|
|
79
108
|
test('emptyFolder should not call S3#deleteObjects if already empty', async () => {
|
|
80
|
-
|
|
81
|
-
spyS3({
|
|
82
|
-
listObjectsV2: () => Object({ Contents: [] }),
|
|
83
|
-
deleteObjects: deleteMock
|
|
84
|
-
})
|
|
109
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 0 })
|
|
85
110
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
86
111
|
await rs.emptyFolder('fakeprefix')
|
|
87
|
-
expect(
|
|
112
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledTimes(0)
|
|
88
113
|
})
|
|
89
114
|
|
|
90
115
|
test('emptyFolder should call S3#deleteObjects with correct parameters with one file', async () => {
|
|
91
|
-
const deleteMock = jest.fn()
|
|
92
116
|
const content = [{ Key: 'fakeprefix/index.html' }]
|
|
93
|
-
|
|
94
|
-
listObjectsV2: () => Object({ Contents: content }),
|
|
95
|
-
deleteObjects: deleteMock
|
|
96
|
-
})
|
|
117
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 1, Contents: content })
|
|
97
118
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
98
119
|
await rs.emptyFolder('fakeprefix')
|
|
99
|
-
expect(
|
|
120
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: content } })
|
|
100
121
|
})
|
|
101
122
|
|
|
102
123
|
test('emptyFolder should call S3#deleteObjects with correct parameters with multiple files', async () => {
|
|
103
|
-
const deleteMock = jest.fn()
|
|
104
124
|
const content = [{ Key: 'fakeprefix/index.html' }, { Key: 'fakeprefix/index.css' }, { Key: 'fakeprefix/index.css' }]
|
|
105
|
-
|
|
106
|
-
listObjectsV2: () => Object({ Contents: content }),
|
|
107
|
-
deleteObjects: deleteMock
|
|
108
|
-
})
|
|
125
|
+
mockS3.listObjectsV2.mockResolvedValue({ KeyCount: 3, Contents: content })
|
|
109
126
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
110
127
|
await rs.emptyFolder('fakeprefix')
|
|
111
|
-
expect(
|
|
128
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: content } })
|
|
112
129
|
})
|
|
113
130
|
|
|
114
131
|
test('emptyFolder should call S3#deleteObjects multiple time if listObjects is truncated', async () => {
|
|
115
|
-
const deleteMock = jest.fn()
|
|
116
132
|
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
|
|
133
|
+
let iterations = 2
|
|
134
|
+
mockS3.listObjectsV2.mockImplementation(() => {
|
|
135
|
+
const res = { Contents: [content[iterations]], IsTruncated: iterations > 0 }
|
|
136
|
+
iterations--
|
|
137
|
+
return Promise.resolve(res)
|
|
125
138
|
})
|
|
126
139
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
127
140
|
await rs.emptyFolder('fakeprefix')
|
|
128
|
-
expect(
|
|
129
|
-
expect(
|
|
130
|
-
expect(
|
|
141
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: [content[0]] } })
|
|
142
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: [content[1]] } })
|
|
143
|
+
expect(mockS3.deleteObjects).toHaveBeenCalledWith({ Bucket: 'fake-bucket', Delete: { Objects: [content[2]] } })
|
|
131
144
|
})
|
|
132
145
|
|
|
133
146
|
test('uploadFile should call S3#upload with the correct parameters', async () => {
|
|
134
147
|
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
135
|
-
const uploadMock = jest.fn()
|
|
136
|
-
spyS3({
|
|
137
|
-
upload: uploadMock
|
|
138
|
-
})
|
|
139
148
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
140
|
-
const fakeConfig =
|
|
141
|
-
await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig)
|
|
149
|
+
const fakeConfig = global.fakeConfig
|
|
150
|
+
await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig, 'fakeDir')
|
|
142
151
|
const body = Buffer.from('fake content', 'utf8')
|
|
143
|
-
expect(
|
|
152
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: 'fakeprefix/index.js', Body: body, ContentType: 'application/javascript' }))
|
|
144
153
|
})
|
|
145
154
|
|
|
146
155
|
test('uploadFile should call S3#upload with the correct parameters and slash-prefix', async () => {
|
|
147
156
|
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
148
|
-
const uploadMock = jest.fn()
|
|
149
|
-
spyS3({
|
|
150
|
-
upload: uploadMock
|
|
151
|
-
})
|
|
152
157
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
153
|
-
const fakeConfig =
|
|
154
|
-
await rs.uploadFile('fakeDir/index.js', '/slash-prefix', fakeConfig)
|
|
158
|
+
const fakeConfig = global.fakeConfig
|
|
159
|
+
await rs.uploadFile('fakeDir/index.js', '/slash-prefix', fakeConfig, 'fakeDir')
|
|
155
160
|
const body = Buffer.from('fake content', 'utf8')
|
|
156
|
-
expect(
|
|
161
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: '/slash-prefix/index.js', Body: body, ContentType: 'application/javascript' }))
|
|
157
162
|
})
|
|
158
163
|
|
|
159
|
-
test('uploadFile S3#upload with
|
|
164
|
+
test('uploadFile S3#upload with an unknown Content-Type', async () => {
|
|
160
165
|
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
166
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
167
167
|
const fakeConfig = {}
|
|
168
|
-
await rs.uploadFile('fakeDir/index.mst', 'fakeprefix', fakeConfig)
|
|
169
|
-
|
|
170
|
-
expect(
|
|
168
|
+
await rs.uploadFile('fakeDir/index.mst', 'fakeprefix', fakeConfig, 'fakeDir')
|
|
169
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
170
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: 'fakeprefix/index.mst', Body: body }))
|
|
171
|
+
expect(mockS3.putObject.mock.calls[0][0]).not.toHaveProperty('ContentType')
|
|
171
172
|
})
|
|
172
173
|
|
|
173
174
|
test('uploadDir should call S3#upload one time per file', async () => {
|
|
174
175
|
await global.addFakeFiles(vol, 'fakeDir', ['index.js', 'index.css', 'index.html'])
|
|
175
|
-
const uploadMock = jest.fn()
|
|
176
|
-
spyS3({
|
|
177
|
-
upload: uploadMock
|
|
178
|
-
})
|
|
179
176
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
180
|
-
await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig
|
|
181
|
-
expect(
|
|
177
|
+
await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig)
|
|
178
|
+
expect(mockS3.putObject).toHaveBeenCalledTimes(3)
|
|
182
179
|
})
|
|
183
180
|
|
|
184
181
|
test('uploadDir should call a callback once per uploaded file', async () => {
|
|
185
182
|
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
183
|
const cbMock = jest.fn()
|
|
191
184
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
192
185
|
|
|
193
|
-
await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig
|
|
186
|
+
await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig, cbMock)
|
|
194
187
|
expect(cbMock).toHaveBeenCalledTimes(4)
|
|
195
188
|
})
|
|
196
189
|
|
|
197
|
-
test('
|
|
190
|
+
test('cachecontrol string for html', async () => {
|
|
198
191
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
199
|
-
const response = rs._getCacheControlConfig('text/html', global.fakeConfig.
|
|
192
|
+
const response = rs._getCacheControlConfig('text/html', global.fakeConfig.app)
|
|
200
193
|
expect(response).toBe('s-maxage=0, max-age=60')
|
|
201
194
|
})
|
|
202
195
|
|
|
203
|
-
test('
|
|
196
|
+
test('cachecontrol string for JS', async () => {
|
|
204
197
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
205
|
-
const response = rs._getCacheControlConfig('application/javascript', global.fakeConfig.
|
|
198
|
+
const response = rs._getCacheControlConfig('application/javascript', global.fakeConfig.app)
|
|
206
199
|
expect(response).toBe('s-maxage=0, max-age=604800')
|
|
207
200
|
})
|
|
208
201
|
|
|
209
|
-
test('
|
|
202
|
+
test('cachecontrol string for CSS', async () => {
|
|
210
203
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
211
|
-
const response = rs._getCacheControlConfig('text/css', global.fakeConfig.
|
|
204
|
+
const response = rs._getCacheControlConfig('text/css', global.fakeConfig.app)
|
|
212
205
|
expect(response).toBe('s-maxage=0, max-age=604800')
|
|
213
206
|
})
|
|
214
207
|
|
|
215
|
-
test('
|
|
208
|
+
test('cachecontrol string for Image', async () => {
|
|
216
209
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
217
|
-
const response = rs._getCacheControlConfig('image/jpeg', global.fakeConfig.
|
|
210
|
+
const response = rs._getCacheControlConfig('image/jpeg', global.fakeConfig.app)
|
|
218
211
|
expect(response).toBe('s-maxage=0, max-age=604800')
|
|
219
212
|
})
|
|
220
213
|
|
|
221
|
-
test('
|
|
214
|
+
test('cachecontrol string for default', async () => {
|
|
222
215
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
223
|
-
const response = rs._getCacheControlConfig('application/pdf', global.fakeConfig.
|
|
216
|
+
const response = rs._getCacheControlConfig('application/pdf', global.fakeConfig.app)
|
|
224
217
|
expect(response).toBe('s-maxage=0')
|
|
225
218
|
})
|
|
219
|
+
|
|
220
|
+
// response header tests
|
|
221
|
+
test('get response header from config with multiple rules', async () => {
|
|
222
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
223
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
224
|
+
'response-headers': {
|
|
225
|
+
'/*': {
|
|
226
|
+
testHeader: 'generic-header'
|
|
227
|
+
},
|
|
228
|
+
'/testFolder/*': {
|
|
229
|
+
testHeader: 'folder-header'
|
|
230
|
+
},
|
|
231
|
+
'/testFolder/*.js': {
|
|
232
|
+
testHeader: 'all-js-file-in-folder-header'
|
|
233
|
+
},
|
|
234
|
+
'/test.js': {
|
|
235
|
+
testHeader: 'specific-file-header'
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
const folderPath1 = 'testFolder' + path.sep + 'index.html'
|
|
241
|
+
const folderPath2 = 'testFolder' + path.sep + 'test.js'
|
|
242
|
+
await global.addFakeFiles(vol, 'fakeDir', ['index.html', 'test.js', folderPath1, folderPath2])
|
|
243
|
+
const files = await rs.walkDir('fakeDir')
|
|
244
|
+
const fakeDistRoot = path.parse(files[0]).dir
|
|
245
|
+
|
|
246
|
+
const expectedValMap = {
|
|
247
|
+
'index.html': { 'adp-testHeader': 'generic-header' },
|
|
248
|
+
'test.js': { 'adp-testHeader': 'specific-file-header' }
|
|
249
|
+
}
|
|
250
|
+
expectedValMap[folderPath1] = { 'adp-testHeader': 'folder-header' }
|
|
251
|
+
expectedValMap[folderPath2] = { 'adp-testHeader': 'all-js-file-in-folder-header' }
|
|
252
|
+
|
|
253
|
+
files.forEach(f => {
|
|
254
|
+
const fileName = f.replace(path.join(fakeDistRoot, path.sep), '')
|
|
255
|
+
const response = rs.getResponseHeadersForFile(f, fakeDistRoot, newConfig)
|
|
256
|
+
const expected = expectedValMap[fileName]
|
|
257
|
+
expect(response).toStrictEqual(expected)
|
|
258
|
+
})
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
test('get response header for folder based path rules', async () => {
|
|
262
|
+
// setup files and paths
|
|
263
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
264
|
+
const folderPath1 = 'css' + path.sep + 'ui.css'
|
|
265
|
+
const folderPath2 = 'scripts' + path.sep + 'test.js'
|
|
266
|
+
const folderPath3 = 'images' + path.sep + 'image.png'
|
|
267
|
+
const folderPath4 = 'images' + path.sep + 'thumbnails' + path.sep + 'test.jpeg'
|
|
268
|
+
await global.addFakeFiles(vol, 'fakeDir', ['index.html', 'test.js', folderPath1, folderPath2, folderPath3, folderPath4])
|
|
269
|
+
const files = await rs.walkDir('fakeDir')
|
|
270
|
+
const fakeDistRoot = path.parse(files[0]).dir
|
|
271
|
+
|
|
272
|
+
// create a config of rules for files in specific folder
|
|
273
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
274
|
+
'response-headers': {
|
|
275
|
+
'/*': {
|
|
276
|
+
testHeader: 'generic-header'
|
|
277
|
+
},
|
|
278
|
+
'/css/*': {
|
|
279
|
+
testHeader: 'all-files-in-css-folder-header'
|
|
280
|
+
},
|
|
281
|
+
'/scripts/*': {
|
|
282
|
+
testHeader: 'all-files-in-js-folder-header'
|
|
283
|
+
},
|
|
284
|
+
'/images/*': {
|
|
285
|
+
testHeader: 'all-files-in-images-folder-header'
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
// set the expectation
|
|
291
|
+
const expectedValMap = {
|
|
292
|
+
'index.html': { 'adp-testHeader': 'generic-header' },
|
|
293
|
+
'test.js': { 'adp-testHeader': 'generic-header' }
|
|
294
|
+
}
|
|
295
|
+
expectedValMap[folderPath1] = { 'adp-testHeader': 'all-files-in-css-folder-header' }
|
|
296
|
+
expectedValMap[folderPath2] = { 'adp-testHeader': 'all-files-in-js-folder-header' }
|
|
297
|
+
expectedValMap[folderPath3] = { 'adp-testHeader': 'all-files-in-images-folder-header' }
|
|
298
|
+
expectedValMap[folderPath4] = { 'adp-testHeader': 'all-files-in-images-folder-header' }
|
|
299
|
+
|
|
300
|
+
// check header application per file
|
|
301
|
+
files.forEach(f => {
|
|
302
|
+
const fileName = f.replace(path.join(fakeDistRoot, path.sep), '')
|
|
303
|
+
const response = rs.getResponseHeadersForFile(f, fakeDistRoot, newConfig)
|
|
304
|
+
const expected = expectedValMap[fileName]
|
|
305
|
+
expect(response).toStrictEqual(expected)
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
test('get response header for specific file based path rules', async () => {
|
|
310
|
+
// setup files and paths
|
|
311
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
312
|
+
const folderPath1 = 'css' + path.sep + 'ui.css'
|
|
313
|
+
const folderPath2 = 'scripts' + path.sep + 'test.js'
|
|
314
|
+
const folderPath3 = 'images' + path.sep + 'image.png'
|
|
315
|
+
await global.addFakeFiles(vol, 'fakeDir', ['index.html', 'test.js', folderPath1, folderPath2, folderPath3])
|
|
316
|
+
const files = await rs.walkDir('fakeDir')
|
|
317
|
+
const fakeDistRoot = path.parse(files[0]).dir
|
|
318
|
+
|
|
319
|
+
// create a config of rules for spcefic files which overrider folder rules
|
|
320
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
321
|
+
'response-headers': {
|
|
322
|
+
'/*': {
|
|
323
|
+
testHeader: 'generic-header'
|
|
324
|
+
},
|
|
325
|
+
'/css/*': {
|
|
326
|
+
testHeader: 'all-files-in-css-folder-header'
|
|
327
|
+
},
|
|
328
|
+
'/css/ui.css': {
|
|
329
|
+
testHeader: 'specific-css-file-header' // overrides previous css folder rule
|
|
330
|
+
},
|
|
331
|
+
'/scripts/*': {
|
|
332
|
+
testHeader: 'all-files-in-js-folder-header'
|
|
333
|
+
},
|
|
334
|
+
'/scripts/test.js': {
|
|
335
|
+
testHeader: 'specific-js-file-header' // overrides previous js folder rule
|
|
336
|
+
},
|
|
337
|
+
'/images/*': {
|
|
338
|
+
testHeader: 'all-files-in-images-folder-header'
|
|
339
|
+
},
|
|
340
|
+
'/images/image.png': {
|
|
341
|
+
testHeader: 'specific-image-file-header' // overrides previous image folder rule
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
// set the expectation
|
|
347
|
+
const expectedValMap = {
|
|
348
|
+
'index.html': { 'adp-testHeader': 'generic-header' },
|
|
349
|
+
'test.js': { 'adp-testHeader': 'generic-header' }
|
|
350
|
+
}
|
|
351
|
+
expectedValMap[folderPath1] = { 'adp-testHeader': 'specific-css-file-header' }
|
|
352
|
+
expectedValMap[folderPath2] = { 'adp-testHeader': 'specific-js-file-header' }
|
|
353
|
+
expectedValMap[folderPath3] = { 'adp-testHeader': 'specific-image-file-header' }
|
|
354
|
+
|
|
355
|
+
// check header application per file
|
|
356
|
+
files.forEach(f => {
|
|
357
|
+
const fileName = f.replace(path.join(fakeDistRoot, path.sep), '')
|
|
358
|
+
const response = rs.getResponseHeadersForFile(f, fakeDistRoot, newConfig)
|
|
359
|
+
const expected = expectedValMap[fileName]
|
|
360
|
+
expect(response).toStrictEqual(expected)
|
|
361
|
+
})
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
test('get response header for file extension based path rules', async () => {
|
|
365
|
+
// setup files and paths
|
|
366
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
367
|
+
const folderPath1 = 'css' + path.sep + 'ui.css'
|
|
368
|
+
const folderPath2 = 'scripts' + path.sep + 'test.js'
|
|
369
|
+
const folderPath3 = 'images' + path.sep + 'image.png'
|
|
370
|
+
await global.addFakeFiles(vol, 'fakeDir', ['index.html', 'test.js', folderPath1, folderPath2, folderPath3])
|
|
371
|
+
const files = await rs.walkDir('fakeDir')
|
|
372
|
+
const fakeDistRoot = path.parse(files[0]).dir
|
|
373
|
+
|
|
374
|
+
// create a config of rules for spcefic files which overrider folder rules
|
|
375
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
376
|
+
'response-headers': {
|
|
377
|
+
'/*': {
|
|
378
|
+
testHeader: 'generic-header'
|
|
379
|
+
},
|
|
380
|
+
'/*.css': {
|
|
381
|
+
testHeader: 'all-css-files-header'
|
|
382
|
+
},
|
|
383
|
+
'/*.js': {
|
|
384
|
+
testHeader: 'all-js-files-header'
|
|
385
|
+
},
|
|
386
|
+
'/*.png': {
|
|
387
|
+
testHeader: 'all-png-files-header'
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
// set the expectation
|
|
393
|
+
const expectedValMap = {
|
|
394
|
+
'index.html': { 'adp-testHeader': 'generic-header' },
|
|
395
|
+
'test.js': { 'adp-testHeader': 'all-js-files-header' }
|
|
396
|
+
}
|
|
397
|
+
expectedValMap[folderPath1] = { 'adp-testHeader': 'all-css-files-header' }
|
|
398
|
+
expectedValMap[folderPath2] = { 'adp-testHeader': 'all-js-files-header' }
|
|
399
|
+
expectedValMap[folderPath3] = { 'adp-testHeader': 'all-png-files-header' }
|
|
400
|
+
|
|
401
|
+
// check header application per file
|
|
402
|
+
files.forEach(f => {
|
|
403
|
+
const fileName = f.replace(path.join(fakeDistRoot, path.sep), '')
|
|
404
|
+
const response = rs.getResponseHeadersForFile(f, fakeDistRoot, newConfig)
|
|
405
|
+
const expected = expectedValMap[fileName]
|
|
406
|
+
expect(response).toStrictEqual(expected)
|
|
407
|
+
})
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
test('get response header with invalid header name', async () => {
|
|
411
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
412
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
413
|
+
'response-headers': {
|
|
414
|
+
'/*': {
|
|
415
|
+
無効な名前: 'generic-header'
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
const fakeDistRoot = '/fake/web-prod/'
|
|
421
|
+
expect(() => rs.getResponseHeadersForFile(fakeDistRoot + 'index.html', fakeDistRoot, newConfig)).toThrowWithMessageContaining(
|
|
422
|
+
'[WebLib:ERROR_INVALID_HEADER_NAME] `無効な名前` is not a valid response header name')
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
test('get response header with invalid header value', async () => {
|
|
426
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
427
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
428
|
+
'response-headers': {
|
|
429
|
+
'/*': {
|
|
430
|
+
testHeader: '無効な値'
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
const fakeDistRoot = '/fake/web-prod/'
|
|
436
|
+
expect(() => rs.getResponseHeadersForFile(fakeDistRoot + 'index.html', fakeDistRoot, newConfig)).toThrowWithMessageContaining(
|
|
437
|
+
'[WebLib:ERROR_INVALID_HEADER_VALUE] `無効な値` is not a valid response header value for `testHeader`')
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
test('Metadata check for response headers', async () => {
|
|
441
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
442
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
443
|
+
const files = await rs.walkDir('fakeDir')
|
|
444
|
+
const fakeDistRoot = files[0].substring(0, files[0].indexOf('index.js'))
|
|
445
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
446
|
+
'response-headers': {
|
|
447
|
+
'/*': {
|
|
448
|
+
testHeader: 'generic-header'
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
})
|
|
452
|
+
// const fakeConfig = {}
|
|
453
|
+
await rs.uploadFile('fakeDir/index.js', 'fakeprefix', newConfig, fakeDistRoot)
|
|
454
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
455
|
+
const expected = {
|
|
456
|
+
Bucket: 'fake-bucket',
|
|
457
|
+
Key: 'fakeprefix/index.js',
|
|
458
|
+
Body: body,
|
|
459
|
+
ContentType: 'application/javascript',
|
|
460
|
+
Metadata: {
|
|
461
|
+
'adp-testHeader': 'generic-header'
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining(expected))
|
|
465
|
+
})
|
|
226
466
|
})
|
|
@@ -123,7 +123,7 @@ describe('deploy-web', () => {
|
|
|
123
123
|
await expect(deployWeb(config)).resolves.toEqual('https://ns.host/index.html')
|
|
124
124
|
expect(getS3Credentials).toHaveBeenCalledWith(config)
|
|
125
125
|
expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
|
|
126
|
-
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config
|
|
126
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, null)
|
|
127
127
|
expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
|
|
128
128
|
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
129
129
|
})
|
|
@@ -156,7 +156,7 @@ describe('deploy-web', () => {
|
|
|
156
156
|
await expect(deployWeb(config, mockLogger)).resolves.toEqual('https://ns.host/index.html')
|
|
157
157
|
expect(getS3Credentials).toHaveBeenCalledWith(config)
|
|
158
158
|
expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
|
|
159
|
-
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config
|
|
159
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, expect.any(Function))
|
|
160
160
|
expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
|
|
161
161
|
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
162
162
|
})
|
|
@@ -191,7 +191,7 @@ describe('deploy-web', () => {
|
|
|
191
191
|
expect(mockLogger).toHaveBeenCalledWith('warning: an existing deployment will be overwritten')
|
|
192
192
|
expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
|
|
193
193
|
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
194
|
-
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config
|
|
194
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, expect.any(Function))
|
|
195
195
|
// empty dir!
|
|
196
196
|
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('somefolder/')
|
|
197
197
|
})
|
|
@@ -222,7 +222,7 @@ describe('deploy-web', () => {
|
|
|
222
222
|
await expect(deployWeb(config)).resolves.toEqual('https://ns.host/index.html')
|
|
223
223
|
expect(getS3Credentials).toHaveBeenCalledWith(config)
|
|
224
224
|
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
225
|
-
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config
|
|
225
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, null)
|
|
226
226
|
// empty dir!
|
|
227
227
|
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('somefolder/')
|
|
228
228
|
})
|