@adobe/aio-lib-web 7.0.5 → 7.1.0
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 +1 -1
- package/lib/remote-storage.js +37 -13
- package/package.json +3 -3
- package/test/lib/remote-storage.test.js +94 -5
package/.eslintrc
CHANGED
package/lib/remote-storage.js
CHANGED
|
@@ -149,23 +149,39 @@ module.exports = class RemoteStorage {
|
|
|
149
149
|
}
|
|
150
150
|
const content = await fs.readFile(file)
|
|
151
151
|
const mimeType = mime.lookup(path.extname(file))
|
|
152
|
+
// first we will grab it from the global config: htmlCacheDuration, etc.
|
|
152
153
|
const cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
|
|
153
154
|
const uploadParams = {
|
|
154
155
|
Bucket: this.bucket,
|
|
155
156
|
Key: urlJoin(prefix, path.basename(file)),
|
|
156
|
-
Body: content
|
|
157
|
-
|
|
157
|
+
Body: content
|
|
158
|
+
}
|
|
159
|
+
// if we found it in the global config, we will use it ( for now )
|
|
160
|
+
if (cacheControlString) {
|
|
161
|
+
uploadParams.CacheControl = cacheControlString
|
|
158
162
|
}
|
|
159
163
|
// add response headers if specified in manifest
|
|
160
|
-
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig)
|
|
161
|
-
if
|
|
164
|
+
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig) ?? {}
|
|
165
|
+
// here we allow overriding the cache control if specified in response headers
|
|
166
|
+
// this is considered more specific than the general cache control config
|
|
167
|
+
// ideally we deprecate cache control config in favor of response headers directly
|
|
168
|
+
if (responseHeaders?.['adp-cache-control']) {
|
|
169
|
+
uploadParams.CacheControl = responseHeaders['adp-cache-control']
|
|
170
|
+
delete responseHeaders['adp-cache-control']
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (appConfig.auditUserId) {
|
|
174
|
+
responseHeaders['adp-AuditUserId'] = appConfig.auditUserId
|
|
175
|
+
}
|
|
176
|
+
// we only set metadata if we have added anything to responseHeaders object
|
|
177
|
+
// it is not null, but could be empty
|
|
178
|
+
if (Object.keys(responseHeaders).length > 0) {
|
|
162
179
|
uploadParams.Metadata = responseHeaders
|
|
163
180
|
}
|
|
164
181
|
// s3 misses some mime types like for css files
|
|
165
182
|
if (mimeType) {
|
|
166
183
|
uploadParams.ContentType = mimeType
|
|
167
184
|
}
|
|
168
|
-
|
|
169
185
|
// 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
|
|
170
186
|
// 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
|
|
171
187
|
return this.s3.putObject(uploadParams)
|
|
@@ -190,6 +206,13 @@ module.exports = class RemoteStorage {
|
|
|
190
206
|
return responseHeaders
|
|
191
207
|
}
|
|
192
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Checks if a header can be added to a file based on the rule
|
|
211
|
+
* @param {string} file - file path
|
|
212
|
+
* @param {string} distRoot - distribution root
|
|
213
|
+
* @param {string} rule - rule to check
|
|
214
|
+
* @returns {boolean} true if header can be added, false otherwise
|
|
215
|
+
*/
|
|
193
216
|
canAddHeader (file, distRoot, rule) {
|
|
194
217
|
const filePath = path.parse(file)
|
|
195
218
|
const normalisedRule = rule.replace(/\//g, path.sep)
|
|
@@ -198,7 +221,6 @@ module.exports = class RemoteStorage {
|
|
|
198
221
|
if (folderPathToMatch.endsWith(path.sep)) {
|
|
199
222
|
folderPathToMatch = folderPathToMatch.substring(0, folderPathToMatch.length - 1) // remove any trailing path separator
|
|
200
223
|
}
|
|
201
|
-
|
|
202
224
|
if (rule === '/*') { // all content
|
|
203
225
|
return true
|
|
204
226
|
} else if (rule.endsWith('/*')) { // all content in a folder ex. /test/*
|
|
@@ -293,17 +315,19 @@ module.exports = class RemoteStorage {
|
|
|
293
315
|
* @param {Object} appConfig - application config
|
|
294
316
|
*/
|
|
295
317
|
_getCacheControlConfig (mimeType, appConfig) {
|
|
296
|
-
const cacheControlStr = 's-maxage=
|
|
318
|
+
const cacheControlStr = 's-maxage=60'
|
|
297
319
|
if (!mimeType) {
|
|
298
|
-
return
|
|
320
|
+
return null
|
|
299
321
|
} else if (mimeType === mime.lookup('html')) {
|
|
300
|
-
return cacheControlStr
|
|
322
|
+
return `${cacheControlStr}, max-age=${appConfig.htmlCacheDuration}`
|
|
301
323
|
} else if (mimeType === mime.lookup('js')) {
|
|
302
|
-
return cacheControlStr
|
|
324
|
+
return `${cacheControlStr}, max-age=${appConfig.jsCacheDuration}`
|
|
303
325
|
} else if (mimeType === mime.lookup('css')) {
|
|
304
|
-
return cacheControlStr
|
|
326
|
+
return `${cacheControlStr}, max-age=${appConfig.cssCacheDuration}`
|
|
305
327
|
} else if (mimeType.startsWith('image')) {
|
|
306
|
-
return cacheControlStr
|
|
307
|
-
} else {
|
|
328
|
+
return `${cacheControlStr}, max-age=${appConfig.imageCacheDuration}`
|
|
329
|
+
} else {
|
|
330
|
+
return null
|
|
331
|
+
}
|
|
308
332
|
}
|
|
309
333
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/aio-lib-web",
|
|
3
|
-
"version": "7.0
|
|
3
|
+
"version": "7.1.0",
|
|
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": {
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
"@adobe/aio-lib-core-logging": "^3",
|
|
34
34
|
"@adobe/aio-lib-core-tvm": "^4",
|
|
35
35
|
"@aws-sdk/client-s3": "^3.624.0",
|
|
36
|
-
"@smithy/node-http-handler": "^
|
|
36
|
+
"@smithy/node-http-handler": "^4.0.2",
|
|
37
37
|
"core-js": "^3.25.1",
|
|
38
38
|
"fs-extra": "^11",
|
|
39
39
|
"joi": "^17.2.1",
|
|
40
40
|
"klaw": "^4",
|
|
41
41
|
"lodash.clonedeep": "^4.5.0",
|
|
42
42
|
"mime-types": "^2.1.24",
|
|
43
|
-
"parcel": "^2.
|
|
43
|
+
"parcel": "^2.15.4",
|
|
44
44
|
"proxy-agent": "^6.3.0",
|
|
45
45
|
"regenerator-runtime": "^0.13.7"
|
|
46
46
|
},
|
|
@@ -300,31 +300,31 @@ describe('RemoteStorage', () => {
|
|
|
300
300
|
test('cachecontrol string for html', async () => {
|
|
301
301
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
302
302
|
const response = rs._getCacheControlConfig('text/html', global.fakeConfig.app)
|
|
303
|
-
expect(response).toBe('s-maxage=
|
|
303
|
+
expect(response).toBe('s-maxage=60, max-age=60')
|
|
304
304
|
})
|
|
305
305
|
|
|
306
306
|
test('cachecontrol string for JS', async () => {
|
|
307
307
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
308
308
|
const response = rs._getCacheControlConfig('application/javascript', global.fakeConfig.app)
|
|
309
|
-
expect(response).toBe('s-maxage=
|
|
309
|
+
expect(response).toBe('s-maxage=60, max-age=604800')
|
|
310
310
|
})
|
|
311
311
|
|
|
312
312
|
test('cachecontrol string for CSS', async () => {
|
|
313
313
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
314
314
|
const response = rs._getCacheControlConfig('text/css', global.fakeConfig.app)
|
|
315
|
-
expect(response).toBe('s-maxage=
|
|
315
|
+
expect(response).toBe('s-maxage=60, max-age=604800')
|
|
316
316
|
})
|
|
317
317
|
|
|
318
318
|
test('cachecontrol string for Image', async () => {
|
|
319
319
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
320
320
|
const response = rs._getCacheControlConfig('image/jpeg', global.fakeConfig.app)
|
|
321
|
-
expect(response).toBe('s-maxage=
|
|
321
|
+
expect(response).toBe('s-maxage=60, max-age=604800')
|
|
322
322
|
})
|
|
323
323
|
|
|
324
324
|
test('cachecontrol string for default', async () => {
|
|
325
325
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
326
326
|
const response = rs._getCacheControlConfig('application/pdf', global.fakeConfig.app)
|
|
327
|
-
expect(response).toBe(
|
|
327
|
+
expect(response).toBe(null)
|
|
328
328
|
})
|
|
329
329
|
|
|
330
330
|
// response header tests
|
|
@@ -573,4 +573,93 @@ describe('RemoteStorage', () => {
|
|
|
573
573
|
}
|
|
574
574
|
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining(expected))
|
|
575
575
|
})
|
|
576
|
+
|
|
577
|
+
test('Cache control override from response headers', async () => {
|
|
578
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.html': 'fake content' })
|
|
579
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
580
|
+
const files = await rs.walkDir('fakeDir')
|
|
581
|
+
const fakeDistRoot = path.parse(files[0]).dir
|
|
582
|
+
const filePath = files[0] // Use absolute path from walkDir
|
|
583
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
584
|
+
'response-headers': {
|
|
585
|
+
'/*.html': {
|
|
586
|
+
'cache-control': 'max-age=3600, s-maxage=7200',
|
|
587
|
+
testHeader: 'generic-header'
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
})
|
|
591
|
+
await rs.uploadFile(filePath, 'fakeprefix', newConfig, fakeDistRoot)
|
|
592
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
593
|
+
const expected = {
|
|
594
|
+
Bucket: 'fake-bucket',
|
|
595
|
+
Key: 'fakeprefix/index.html',
|
|
596
|
+
Body: body,
|
|
597
|
+
ContentType: 'text/html',
|
|
598
|
+
CacheControl: 'max-age=3600, s-maxage=7200',
|
|
599
|
+
Metadata: {
|
|
600
|
+
'adp-testHeader': 'generic-header'
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining(expected))
|
|
604
|
+
// Verify that adp-cache-control was removed from metadata
|
|
605
|
+
const putObjectCall = mockS3.putObject.mock.calls[0][0]
|
|
606
|
+
expect(putObjectCall.Metadata).not.toHaveProperty('adp-cache-control')
|
|
607
|
+
})
|
|
608
|
+
|
|
609
|
+
test('uploadFile includes auditUserId in metadata when set', async () => {
|
|
610
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
611
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
612
|
+
const fakeConfig = { ...global.fakeConfig, auditUserId: 'test-user-123' }
|
|
613
|
+
await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig, 'fakeDir')
|
|
614
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
615
|
+
const expected = {
|
|
616
|
+
Bucket: 'fake-bucket',
|
|
617
|
+
Key: 'fakeprefix/index.js',
|
|
618
|
+
Body: body,
|
|
619
|
+
ContentType: 'application/javascript',
|
|
620
|
+
Metadata: expect.objectContaining({
|
|
621
|
+
'adp-AuditUserId': 'test-user-123'
|
|
622
|
+
})
|
|
623
|
+
}
|
|
624
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining(expected))
|
|
625
|
+
})
|
|
626
|
+
|
|
627
|
+
test('uploadFile does not set Metadata when responseHeaders is empty and auditUserId is not set', async () => {
|
|
628
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
629
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
630
|
+
const fakeConfig = {
|
|
631
|
+
app: global.fakeConfig.app
|
|
632
|
+
// No web.response-headers and no auditUserId
|
|
633
|
+
}
|
|
634
|
+
await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig, 'fakeDir')
|
|
635
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
636
|
+
const putObjectCall = mockS3.putObject.mock.calls[0][0]
|
|
637
|
+
expect(putObjectCall).not.toHaveProperty('Metadata')
|
|
638
|
+
expect(putObjectCall).toMatchObject({
|
|
639
|
+
Bucket: 'fake-bucket',
|
|
640
|
+
Key: 'fakeprefix/index.js',
|
|
641
|
+
Body: body,
|
|
642
|
+
ContentType: 'application/javascript'
|
|
643
|
+
})
|
|
644
|
+
})
|
|
645
|
+
|
|
646
|
+
test('uploadFile sets CacheControl even when responseHeaders is empty and auditUserId is not set', async () => {
|
|
647
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.html': 'fake content' })
|
|
648
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
649
|
+
const fakeConfig = {
|
|
650
|
+
app: global.fakeConfig.app
|
|
651
|
+
// No web.response-headers and no auditUserId
|
|
652
|
+
}
|
|
653
|
+
await rs.uploadFile('fakeDir/index.html', 'fakeprefix', fakeConfig, 'fakeDir')
|
|
654
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
655
|
+
const putObjectCall = mockS3.putObject.mock.calls[0][0]
|
|
656
|
+
expect(putObjectCall).not.toHaveProperty('Metadata')
|
|
657
|
+
expect(putObjectCall).toMatchObject({
|
|
658
|
+
Bucket: 'fake-bucket',
|
|
659
|
+
Key: 'fakeprefix/index.html',
|
|
660
|
+
Body: body,
|
|
661
|
+
ContentType: 'text/html',
|
|
662
|
+
CacheControl: 's-maxage=60, max-age=60'
|
|
663
|
+
})
|
|
664
|
+
})
|
|
576
665
|
})
|