@adobe/aio-lib-web 6.0.2 → 6.1.1-pre.2024-01-08.20eab078

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.
@@ -10,7 +10,7 @@ jobs:
10
10
  runs-on: ${{ matrix.os }}
11
11
  strategy:
12
12
  matrix:
13
- node: [16]
13
+ node-version: [18]
14
14
  os: [ubuntu-latest]
15
15
 
16
16
  steps:
@@ -29,6 +29,6 @@ jobs:
29
29
  env:
30
30
  SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
31
31
  SLACK_TITLE: 'Node version'
32
- SLACK_MESSAGE: ${{ matrix.node }}
32
+ SLACK_MESSAGE: ${{ matrix.node-version }}
33
33
  SLACK_COLOR: ${{ job.status == 'success' && 'good' || job.status == 'cancelled' && '#808080' || 'danger' }}
34
34
 
@@ -14,7 +14,7 @@ jobs:
14
14
  runs-on: ${{ matrix.os }}
15
15
  strategy:
16
16
  matrix:
17
- node-version: [14.x, 16.x]
17
+ node-version: [18.x, 20.x]
18
18
  os: [ubuntu-latest, windows-latest]
19
19
 
20
20
  steps:
@@ -12,7 +12,7 @@ jobs:
12
12
  - uses: actions/checkout@v2
13
13
  - uses: actions/setup-node@v1
14
14
  with:
15
- node-version: 16
15
+ node-version: 18
16
16
  - run: npm install
17
17
  - run: npm test
18
18
  - uses: JS-DevTools/npm-publish@v1
@@ -26,7 +26,7 @@ jobs:
26
26
  git config user.email github-actions@github.com
27
27
  - uses: actions/setup-node@v1
28
28
  with:
29
- node-version: 16
29
+ node-version: 18
30
30
  - run: |
31
31
  npm install
32
32
  npm test
@@ -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
+ }
@@ -16,6 +16,10 @@ 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
@@ -122,20 +126,26 @@ module.exports = class RemoteStorage {
122
126
  * @param {string} file
123
127
  * @param {string} prefix - prefix to upload the file to
124
128
  * @param {Object} appConfig - application config
129
+ * @param {string} distRoot - Distribution root dir
125
130
  */
126
- async uploadFile (file, prefix, appConfig) {
131
+ async uploadFile (file, prefix, appConfig, distRoot) {
127
132
  if (typeof prefix !== 'string') {
128
133
  throw new Error('prefix must be a valid string')
129
134
  }
130
135
  const content = await fs.readFile(file)
131
136
  const mimeType = mime.lookup(path.extname(file))
132
- const cacheControlString = this._getCacheControlConfig(mimeType, appConfig)
137
+ const cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
133
138
  const uploadParams = {
134
139
  Bucket: this.bucket,
135
140
  Key: urlJoin(prefix, path.basename(file)),
136
141
  Body: content,
137
142
  CacheControl: cacheControlString
138
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
+ }
139
149
  // s3 misses some mime types like for css files
140
150
  if (mimeType) {
141
151
  uploadParams.ContentType = mimeType
@@ -146,6 +156,68 @@ module.exports = class RemoteStorage {
146
156
  return this.s3.putObject(uploadParams)
147
157
  }
148
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
+ }
219
+ }
220
+
149
221
  async walkDir (dir) {
150
222
  return new Promise((resolve, reject) => {
151
223
  const items = []
@@ -181,7 +253,7 @@ module.exports = class RemoteStorage {
181
253
  prefixDirectory = prefixDirectory === '.' ? '' : prefixDirectory
182
254
  // newPrefix is now the initial prefix plus the files relative directory path.
183
255
  const newPrefix = urlJoin(prefix, prefixDirectory)
184
- const s3Res = await this.uploadFile(f, newPrefix, appConfig)
256
+ const s3Res = await this.uploadFile(f, newPrefix, appConfig, dir)
185
257
 
186
258
  if (postFileUploadCallback) {
187
259
  postFileUploadCallback(f)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/aio-lib-web",
3
- "version": "6.0.2",
3
+ "version": "6.1.1-pre.2024-01-08.20eab078",
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": {
@@ -10,7 +10,7 @@
10
10
  "author": "Adobe Inc.",
11
11
  "license": "Apache-2.0",
12
12
  "engines": {
13
- "node": "^14.18 || ^16.13 || >=18"
13
+ "node": ">=18"
14
14
  },
15
15
  "repository": "adobe/aio-lib-web",
16
16
  "homepage": "https://github.com/adobe/aio-lib-web",
@@ -29,12 +29,12 @@
29
29
  "e2e": "jest --config='./e2e/jest.config.js' --testRegex './e2e/e2e.js'"
30
30
  },
31
31
  "dependencies": {
32
- "@adobe/aio-lib-core-config": "^3.0.0",
33
- "@adobe/aio-lib-core-logging": "^2.0.0",
34
- "@adobe/aio-lib-core-tvm": "^3.0.0",
32
+ "@adobe/aio-lib-core-config": "next",
33
+ "@adobe/aio-lib-core-logging": "next",
34
+ "@adobe/aio-lib-core-tvm": "next",
35
35
  "@aws-sdk/client-s3": "^3.276.0",
36
36
  "core-js": "^3.25.1",
37
- "fs-extra": "^10",
37
+ "fs-extra": "^11",
38
38
  "joi": "^17.2.1",
39
39
  "klaw": "^4",
40
40
  "lodash.clonedeep": "^4.5.0",
@@ -54,8 +54,9 @@
54
54
  "eslint-plugin-promise": "^6",
55
55
  "eslint-plugin-standard": "^5",
56
56
  "jest": "^29",
57
- "jsdoc-to-markdown": "^7",
57
+ "jsdoc-to-markdown": "^8",
58
58
  "memfs": "^3.4.7",
59
59
  "node-fetch": "^2.6.7"
60
- }
61
- }
60
+ },
61
+ "prereleaseSha": "20eab07837ed3c5f32dacfcb60cf55fe40756e07"
62
+ }
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.app, _log)
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
@@ -22,11 +22,11 @@ const undeployWeb = async (config) => {
22
22
 
23
23
  const remoteStorage = new RemoteStorage(creds)
24
24
 
25
- if (!(await remoteStorage.folderExists(config.s3.folder))) {
25
+ if (!(await remoteStorage.folderExists(config.s3.folder + '/'))) {
26
26
  throw new Error(`cannot undeploy static files, there is no deployment for ${config.s3.folder}`)
27
27
  }
28
28
 
29
- await remoteStorage.emptyFolder(config.s3.folder)
29
+ await remoteStorage.emptyFolder(config.s3.folder + '/')
30
30
  }
31
31
 
32
32
  module.exports = undeployWeb
@@ -1,4 +1,3 @@
1
-
2
1
  const mockTvm = {
3
2
  init: jest.fn(async () => {
4
3
  return {
@@ -112,6 +112,12 @@ global.configWithMissing = (config, members) => {
112
112
  return config
113
113
  }
114
114
 
115
+ global.configWithModifiedWeb = (config, newWebConfig) => {
116
+ config = cloneDeep(config)
117
+ config.web = newWebConfig
118
+ return config
119
+ }
120
+
115
121
  global.fakeS3Bucket = 'fake-bucket'
116
122
  global.fakeConfig = {
117
123
  tvm: {
@@ -139,11 +145,18 @@ global.fakeConfig = {
139
145
  awssecretaccesskey: 'fakeAwsSecretKey'
140
146
  }
141
147
  },
142
- cna: {
148
+ app: {
143
149
  htmlCacheDuration: 60,
144
150
  jsCacheDuration: 604800,
145
151
  cssCacheDuration: 604800,
146
152
  imageCacheDuration: 604800
153
+ },
154
+ web: {
155
+ 'response-headers': {
156
+ '/*': {
157
+ testHeader: 'foo'
158
+ }
159
+ }
147
160
  }
148
161
  }
149
162
 
@@ -20,6 +20,7 @@ jest.mock('@aws-sdk/client-s3', () => Object({ S3: jest.fn(() => { return mockS3
20
20
 
21
21
  const { S3 } = require('@aws-sdk/client-s3')
22
22
  const { vol } = global.mockFs()
23
+ const path = require('path')
23
24
 
24
25
  const RemoteStorage = require('../../lib/remote-storage')
25
26
 
@@ -145,8 +146,8 @@ describe('RemoteStorage', () => {
145
146
  test('uploadFile should call S3#upload with the correct parameters', async () => {
146
147
  global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
147
148
  const rs = new RemoteStorage(global.fakeTVMResponse)
148
- const fakeConfig = {}
149
- await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig)
149
+ const fakeConfig = global.fakeConfig
150
+ await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig, 'fakeDir')
150
151
  const body = Buffer.from('fake content', 'utf8')
151
152
  expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: 'fakeprefix/index.js', Body: body, ContentType: 'application/javascript' }))
152
153
  })
@@ -154,8 +155,8 @@ describe('RemoteStorage', () => {
154
155
  test('uploadFile should call S3#upload with the correct parameters and slash-prefix', async () => {
155
156
  global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
156
157
  const rs = new RemoteStorage(global.fakeTVMResponse)
157
- const fakeConfig = {}
158
- 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')
159
160
  const body = Buffer.from('fake content', 'utf8')
160
161
  expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: '/slash-prefix/index.js', Body: body, ContentType: 'application/javascript' }))
161
162
  })
@@ -164,7 +165,7 @@ describe('RemoteStorage', () => {
164
165
  global.addFakeFiles(vol, 'fakeDir', { 'index.mst': 'fake content' })
165
166
  const rs = new RemoteStorage(global.fakeTVMResponse)
166
167
  const fakeConfig = {}
167
- await rs.uploadFile('fakeDir/index.mst', 'fakeprefix', fakeConfig)
168
+ await rs.uploadFile('fakeDir/index.mst', 'fakeprefix', fakeConfig, 'fakeDir')
168
169
  const body = Buffer.from('fake content', 'utf8')
169
170
  expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining({ Bucket: 'fake-bucket', Key: 'fakeprefix/index.mst', Body: body }))
170
171
  expect(mockS3.putObject.mock.calls[0][0]).not.toHaveProperty('ContentType')
@@ -173,7 +174,7 @@ describe('RemoteStorage', () => {
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
176
  const rs = new RemoteStorage(global.fakeTVMResponse)
176
- await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig.creds.cna)
177
+ await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig)
177
178
  expect(mockS3.putObject).toHaveBeenCalledTimes(3)
178
179
  })
179
180
 
@@ -182,37 +183,284 @@ describe('RemoteStorage', () => {
182
183
  const cbMock = jest.fn()
183
184
  const rs = new RemoteStorage(global.fakeTVMResponse)
184
185
 
185
- await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig.cna, cbMock)
186
+ await rs.uploadDir('fakeDir', 'fakeprefix', global.fakeConfig, cbMock)
186
187
  expect(cbMock).toHaveBeenCalledTimes(4)
187
188
  })
188
189
 
189
190
  test('cachecontrol string for html', async () => {
190
191
  const rs = new RemoteStorage(global.fakeTVMResponse)
191
- const response = rs._getCacheControlConfig('text/html', global.fakeConfig.cna)
192
+ const response = rs._getCacheControlConfig('text/html', global.fakeConfig.app)
192
193
  expect(response).toBe('s-maxage=0, max-age=60')
193
194
  })
194
195
 
195
196
  test('cachecontrol string for JS', async () => {
196
197
  const rs = new RemoteStorage(global.fakeTVMResponse)
197
- const response = rs._getCacheControlConfig('application/javascript', global.fakeConfig.cna)
198
+ const response = rs._getCacheControlConfig('application/javascript', global.fakeConfig.app)
198
199
  expect(response).toBe('s-maxage=0, max-age=604800')
199
200
  })
200
201
 
201
202
  test('cachecontrol string for CSS', async () => {
202
203
  const rs = new RemoteStorage(global.fakeTVMResponse)
203
- const response = rs._getCacheControlConfig('text/css', global.fakeConfig.cna)
204
+ const response = rs._getCacheControlConfig('text/css', global.fakeConfig.app)
204
205
  expect(response).toBe('s-maxage=0, max-age=604800')
205
206
  })
206
207
 
207
208
  test('cachecontrol string for Image', async () => {
208
209
  const rs = new RemoteStorage(global.fakeTVMResponse)
209
- const response = rs._getCacheControlConfig('image/jpeg', global.fakeConfig.cna)
210
+ const response = rs._getCacheControlConfig('image/jpeg', global.fakeConfig.app)
210
211
  expect(response).toBe('s-maxage=0, max-age=604800')
211
212
  })
212
213
 
213
214
  test('cachecontrol string for default', async () => {
214
215
  const rs = new RemoteStorage(global.fakeTVMResponse)
215
- const response = rs._getCacheControlConfig('application/pdf', global.fakeConfig.cna)
216
+ const response = rs._getCacheControlConfig('application/pdf', global.fakeConfig.app)
216
217
  expect(response).toBe('s-maxage=0')
217
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
+ })
218
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.app, null)
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.app, expect.any(Function))
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.app, expect.any(Function))
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.app, null)
225
+ expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, null)
226
226
  // empty dir!
227
227
  expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('somefolder/')
228
228
  })
@@ -64,8 +64,8 @@ describe('undeploy-web', () => {
64
64
  await undeployWeb(config)
65
65
  expect(getS3Credentials).toHaveBeenCalledWith(config)
66
66
  expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
67
- expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder')
68
- expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('somefolder')
67
+ expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
68
+ expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('somefolder/')
69
69
  })
70
70
 
71
71
  test('throws if remoteStorage folder does not exist', async () => {
@@ -89,7 +89,7 @@ describe('undeploy-web', () => {
89
89
  await expect(undeployWeb(config)).rejects.toThrow('cannot undeploy static files')
90
90
  expect(getS3Credentials).toHaveBeenCalledWith(config)
91
91
  expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
92
- expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder')
92
+ expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
93
93
  expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
94
94
  })
95
95
  })