@adobe/aio-lib-web 7.0.6 → 7.1.1
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 +86 -35
- package/package.json +2 -2
- package/test/lib/remote-storage.test.js +381 -5
package/.eslintrc
CHANGED
package/lib/remote-storage.js
CHANGED
|
@@ -24,22 +24,6 @@ const { codes, logAndThrow } = require('./StorageError')
|
|
|
24
24
|
|
|
25
25
|
const fileExtensionPattern = /\*\.[0-9a-zA-Z]+$/
|
|
26
26
|
|
|
27
|
-
// /**
|
|
28
|
-
// * Joins url path parts
|
|
29
|
-
// * @param {...string} args url parts
|
|
30
|
-
// * @returns {string}
|
|
31
|
-
// */
|
|
32
|
-
function urlJoin (...args) {
|
|
33
|
-
let start = ''
|
|
34
|
-
if (args[0] &&
|
|
35
|
-
args[0].startsWith('/')) {
|
|
36
|
-
start = '/'
|
|
37
|
-
}
|
|
38
|
-
return start + args.map(a => a && a.replace(/(^\/|\/$)/g, ''))
|
|
39
|
-
.filter(a => a) // remove empty strings / nulls
|
|
40
|
-
.join('/')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
27
|
module.exports = class RemoteStorage {
|
|
44
28
|
/**
|
|
45
29
|
* @param {object} creds
|
|
@@ -149,23 +133,36 @@ module.exports = class RemoteStorage {
|
|
|
149
133
|
}
|
|
150
134
|
const content = await fs.readFile(file)
|
|
151
135
|
const mimeType = mime.lookup(path.extname(file))
|
|
136
|
+
// first we will grab it from the global config: htmlCacheDuration, etc.
|
|
152
137
|
const cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
|
|
153
138
|
const uploadParams = {
|
|
154
139
|
Bucket: this.bucket,
|
|
155
|
-
Key:
|
|
156
|
-
Body: content
|
|
157
|
-
|
|
140
|
+
Key: this._urlJoin(prefix, path.basename(file)),
|
|
141
|
+
Body: content
|
|
142
|
+
}
|
|
143
|
+
// if we found it in the global config, we will use it ( for now )
|
|
144
|
+
if (cacheControlString) {
|
|
145
|
+
uploadParams.CacheControl = cacheControlString
|
|
158
146
|
}
|
|
159
147
|
// add response headers if specified in manifest
|
|
160
|
-
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig)
|
|
161
|
-
if
|
|
148
|
+
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig) ?? {}
|
|
149
|
+
// here we allow overriding the cache control if specified in response headers
|
|
150
|
+
// this is considered more specific than the general cache control config
|
|
151
|
+
// ideally we deprecate cache control config in favor of response headers directly
|
|
152
|
+
if (responseHeaders?.['adp-cache-control']) {
|
|
153
|
+
uploadParams.CacheControl = responseHeaders['adp-cache-control']
|
|
154
|
+
delete responseHeaders['adp-cache-control']
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// we only set metadata if we have added anything to responseHeaders object
|
|
158
|
+
// it is not null, but could be empty
|
|
159
|
+
if (Object.keys(responseHeaders).length > 0) {
|
|
162
160
|
uploadParams.Metadata = responseHeaders
|
|
163
161
|
}
|
|
164
162
|
// s3 misses some mime types like for css files
|
|
165
163
|
if (mimeType) {
|
|
166
164
|
uploadParams.ContentType = mimeType
|
|
167
165
|
}
|
|
168
|
-
|
|
169
166
|
// 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
167
|
// 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
168
|
return this.s3.putObject(uploadParams)
|
|
@@ -190,6 +187,13 @@ module.exports = class RemoteStorage {
|
|
|
190
187
|
return responseHeaders
|
|
191
188
|
}
|
|
192
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Checks if a header can be added to a file based on the rule
|
|
192
|
+
* @param {string} file - file path
|
|
193
|
+
* @param {string} distRoot - distribution root
|
|
194
|
+
* @param {string} rule - rule to check
|
|
195
|
+
* @returns {boolean} true if header can be added, false otherwise
|
|
196
|
+
*/
|
|
193
197
|
canAddHeader (file, distRoot, rule) {
|
|
194
198
|
const filePath = path.parse(file)
|
|
195
199
|
const normalisedRule = rule.replace(/\//g, path.sep)
|
|
@@ -198,7 +202,6 @@ module.exports = class RemoteStorage {
|
|
|
198
202
|
if (folderPathToMatch.endsWith(path.sep)) {
|
|
199
203
|
folderPathToMatch = folderPathToMatch.substring(0, folderPathToMatch.length - 1) // remove any trailing path separator
|
|
200
204
|
}
|
|
201
|
-
|
|
202
205
|
if (rule === '/*') { // all content
|
|
203
206
|
return true
|
|
204
207
|
} else if (rule.endsWith('/*')) { // all content in a folder ex. /test/*
|
|
@@ -274,7 +277,7 @@ module.exports = class RemoteStorage {
|
|
|
274
277
|
// base directory returns ".", ignore that.
|
|
275
278
|
prefixDirectory = prefixDirectory === '.' ? '' : prefixDirectory
|
|
276
279
|
// newPrefix is now the initial prefix plus the files relative directory path.
|
|
277
|
-
const newPrefix =
|
|
280
|
+
const newPrefix = this._urlJoin(prefix, prefixDirectory)
|
|
278
281
|
const s3Res = await this.uploadFile(f, newPrefix, appConfig, dir)
|
|
279
282
|
if (postFileUploadCallback) {
|
|
280
283
|
postFileUploadCallback(f)
|
|
@@ -287,23 +290,71 @@ module.exports = class RemoteStorage {
|
|
|
287
290
|
return allResults
|
|
288
291
|
}
|
|
289
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Joins file path parts into a URL-friendly S3 key
|
|
295
|
+
* Normalizes paths by:
|
|
296
|
+
* - Preserving leading slash if first part starts with '/'
|
|
297
|
+
* - Removing leading/trailing slashes from each part
|
|
298
|
+
* - Converting Windows backslashes to forward slashes
|
|
299
|
+
* - Removing double slashes
|
|
300
|
+
* - Filtering out empty/null parts
|
|
301
|
+
* @param {...string} args - Path parts to join
|
|
302
|
+
* @returns {string} Normalized S3 key path
|
|
303
|
+
*/
|
|
304
|
+
_urlJoin (...args) {
|
|
305
|
+
// Preserve leading slash if first argument starts with one
|
|
306
|
+
const hasLeadingSlash = args[0] && args[0].startsWith('/')
|
|
307
|
+
|
|
308
|
+
// Normalize each path part:
|
|
309
|
+
// 1. Remove leading and trailing slashes (^\/ matches start, \/$ matches end)
|
|
310
|
+
// 2. Convert Windows backslashes to forward slashes
|
|
311
|
+
// 3. Filter out empty/null values
|
|
312
|
+
const normalizedParts = args
|
|
313
|
+
.map(part => {
|
|
314
|
+
if (!part) return null
|
|
315
|
+
// Remove leading/trailing slashes and convert backslashes to forward slashes
|
|
316
|
+
return part.replace(/(^\/|\/$)/g, '').replace(/\\/g, '/')
|
|
317
|
+
})
|
|
318
|
+
.filter(part => part) // Remove empty strings and nulls
|
|
319
|
+
|
|
320
|
+
// Join parts with '/' and remove any double slashes that may result
|
|
321
|
+
const joined = normalizedParts.join('/')
|
|
322
|
+
const withoutDoubleSlashes = joined.replace(/\/+/g, '/')
|
|
323
|
+
|
|
324
|
+
// Restore leading slash if original first part had one
|
|
325
|
+
return hasLeadingSlash ? '/' + withoutDoubleSlashes : withoutDoubleSlashes
|
|
326
|
+
}
|
|
327
|
+
|
|
290
328
|
/**
|
|
291
329
|
* Get cache control string based on mime type and config
|
|
292
330
|
* @param {string|boolean} mimeType - string if valid mimeType or false for unknown files
|
|
293
331
|
* @param {Object} appConfig - application config
|
|
294
332
|
*/
|
|
295
333
|
_getCacheControlConfig (mimeType, appConfig) {
|
|
296
|
-
const cacheControlStr = 's-maxage=
|
|
334
|
+
const cacheControlStr = 's-maxage=60'
|
|
297
335
|
if (!mimeType) {
|
|
298
|
-
return
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
336
|
+
return null
|
|
337
|
+
}
|
|
338
|
+
if (mimeType === mime.lookup('html')) {
|
|
339
|
+
if (appConfig.htmlCacheDuration) {
|
|
340
|
+
return `${cacheControlStr}, max-age=${appConfig.htmlCacheDuration}`
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (mimeType === mime.lookup('js')) {
|
|
344
|
+
if (appConfig.jsCacheDuration) {
|
|
345
|
+
return `${cacheControlStr}, max-age=${appConfig.jsCacheDuration}`
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (mimeType === mime.lookup('css')) {
|
|
349
|
+
if (appConfig.cssCacheDuration) {
|
|
350
|
+
return `${cacheControlStr}, max-age=${appConfig.cssCacheDuration}`
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (mimeType.startsWith('image')) {
|
|
354
|
+
if (appConfig.imageCacheDuration) {
|
|
355
|
+
return `${cacheControlStr}, max-age=${appConfig.imageCacheDuration}`
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return null
|
|
308
359
|
}
|
|
309
360
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/aio-lib-web",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.1",
|
|
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": {
|
|
@@ -40,7 +40,7 @@
|
|
|
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
|
},
|
|
@@ -176,6 +176,269 @@ describe('RemoteStorage', () => {
|
|
|
176
176
|
})
|
|
177
177
|
})
|
|
178
178
|
|
|
179
|
+
describe('_urlJoin', () => {
|
|
180
|
+
let rs
|
|
181
|
+
|
|
182
|
+
beforeEach(() => {
|
|
183
|
+
rs = new RemoteStorage(global.fakeTVMResponse)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
describe('basic path joining', () => {
|
|
187
|
+
test('joins simple path parts', () => {
|
|
188
|
+
expect(rs._urlJoin('prefix', 'file.js')).toBe('prefix/file.js')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('joins multiple path parts', () => {
|
|
192
|
+
expect(rs._urlJoin('prefix', 'deep', 'nested', 'file.js')).toBe('prefix/deep/nested/file.js')
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test('handles single part', () => {
|
|
196
|
+
expect(rs._urlJoin('file.js')).toBe('file.js')
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
test('handles empty string parts', () => {
|
|
200
|
+
expect(rs._urlJoin('prefix', '', 'file.js')).toBe('prefix/file.js')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('handles null parts', () => {
|
|
204
|
+
expect(rs._urlJoin('prefix', null, 'file.js')).toBe('prefix/file.js')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
test('handles undefined parts', () => {
|
|
208
|
+
expect(rs._urlJoin('prefix', undefined, 'file.js')).toBe('prefix/file.js')
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test('handles all empty/null parts', () => {
|
|
212
|
+
expect(rs._urlJoin('', null, undefined)).toBe('')
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
describe('leading slash preservation', () => {
|
|
217
|
+
test('preserves leading slash from first argument', () => {
|
|
218
|
+
expect(rs._urlJoin('/prefix', 'file.js')).toBe('/prefix/file.js')
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('preserves leading slash with multiple parts', () => {
|
|
222
|
+
expect(rs._urlJoin('/prefix', 'deep', 'file.js')).toBe('/prefix/deep/file.js')
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
test('does not add leading slash when first part does not have one', () => {
|
|
226
|
+
expect(rs._urlJoin('prefix', 'file.js')).toBe('prefix/file.js')
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('handles leading slash with single part', () => {
|
|
230
|
+
expect(rs._urlJoin('/file.js')).toBe('/file.js')
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
describe('trailing slash removal', () => {
|
|
235
|
+
test('removes trailing slash from parts', () => {
|
|
236
|
+
expect(rs._urlJoin('prefix/', 'file.js')).toBe('prefix/file.js')
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
test('removes trailing slash from multiple parts', () => {
|
|
240
|
+
expect(rs._urlJoin('prefix/', 'deep/', 'file.js')).toBe('prefix/deep/file.js')
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
test('removes trailing slash from last part', () => {
|
|
244
|
+
expect(rs._urlJoin('prefix', 'file.js/')).toBe('prefix/file.js')
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
test('removes trailing slash while preserving leading slash', () => {
|
|
248
|
+
expect(rs._urlJoin('/prefix/', 'file.js')).toBe('/prefix/file.js')
|
|
249
|
+
})
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
describe('leading slash removal', () => {
|
|
253
|
+
test('removes leading slash from non-first parts', () => {
|
|
254
|
+
expect(rs._urlJoin('prefix', '/deep', 'file.js')).toBe('prefix/deep/file.js')
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
test('removes leading slash from middle parts', () => {
|
|
258
|
+
expect(rs._urlJoin('prefix', '/deep', '/nested', 'file.js')).toBe('prefix/deep/nested/file.js')
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
test('preserves leading slash only on first part', () => {
|
|
262
|
+
expect(rs._urlJoin('/prefix', '/deep', '/file.js')).toBe('/prefix/deep/file.js')
|
|
263
|
+
})
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
describe('Windows backslash conversion', () => {
|
|
267
|
+
test('converts Windows backslashes to forward slashes', () => {
|
|
268
|
+
expect(rs._urlJoin('prefix\\deep', 'file.js')).toBe('prefix/deep/file.js')
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('converts backslashes in single part', () => {
|
|
272
|
+
expect(rs._urlJoin('prefix\\deep\\file.js')).toBe('prefix/deep/file.js')
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
test('converts backslashes in multiple parts', () => {
|
|
276
|
+
expect(rs._urlJoin('prefix\\deep', 'nested\\file.js')).toBe('prefix/deep/nested/file.js')
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
test('converts backslashes while preserving leading slash', () => {
|
|
280
|
+
expect(rs._urlJoin('/prefix\\deep', 'file.js')).toBe('/prefix/deep/file.js')
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
test('converts mixed backslashes and forward slashes', () => {
|
|
284
|
+
expect(rs._urlJoin('prefix\\deep', 'nested/file.js')).toBe('prefix/deep/nested/file.js')
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
test('handles Windows-style path with backslashes', () => {
|
|
288
|
+
expect(rs._urlJoin('fakeprefix\\deep\\dir', 'index.js')).toBe('fakeprefix/deep/dir/index.js')
|
|
289
|
+
})
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
describe('double slash removal', () => {
|
|
293
|
+
test('removes double slashes from joined path', () => {
|
|
294
|
+
expect(rs._urlJoin('prefix', '', 'file.js')).toBe('prefix/file.js')
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
test('removes multiple consecutive slashes', () => {
|
|
298
|
+
expect(rs._urlJoin('prefix//deep', 'file.js')).toBe('prefix/deep/file.js')
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
test('removes triple slashes', () => {
|
|
302
|
+
expect(rs._urlJoin('prefix///deep', 'file.js')).toBe('prefix/deep/file.js')
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
test('removes double slashes in middle of path', () => {
|
|
306
|
+
expect(rs._urlJoin('prefix', '//deep', 'file.js')).toBe('prefix/deep/file.js')
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
test('handles double slashes with leading slash', () => {
|
|
310
|
+
expect(rs._urlJoin('/prefix', '//deep', 'file.js')).toBe('/prefix/deep/file.js')
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
test('removes double slashes created by empty parts', () => {
|
|
314
|
+
expect(rs._urlJoin('prefix', '', '', 'file.js')).toBe('prefix/file.js')
|
|
315
|
+
})
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
describe('edge cases with slashes', () => {
|
|
319
|
+
test('handles part that is just a slash', () => {
|
|
320
|
+
expect(rs._urlJoin('prefix', '/', 'file.js')).toBe('prefix/file.js')
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
test('handles part that is just backslash', () => {
|
|
324
|
+
expect(rs._urlJoin('prefix', '\\', 'file.js')).toBe('prefix/file.js')
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
test('handles multiple slashes-only parts', () => {
|
|
328
|
+
expect(rs._urlJoin('prefix', '/', '/', 'file.js')).toBe('prefix/file.js')
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
test('handles leading slash with empty parts', () => {
|
|
332
|
+
expect(rs._urlJoin('/prefix', '', 'file.js')).toBe('/prefix/file.js')
|
|
333
|
+
})
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
describe('real-world S3 key scenarios', () => {
|
|
337
|
+
test('creates S3 key from prefix and filename', () => {
|
|
338
|
+
expect(rs._urlJoin('fakeprefix', 'index.js')).toBe('fakeprefix/index.js')
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
test('creates S3 key with nested directories', () => {
|
|
342
|
+
expect(rs._urlJoin('fakeprefix', 'deep', 'dir', 'index.js')).toBe('fakeprefix/deep/dir/index.js')
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
test('handles Windows-style prefix with trailing backslash', () => {
|
|
346
|
+
expect(rs._urlJoin('fakeprefix\\deep\\dir\\', 'index.js')).toBe('fakeprefix/deep/dir/index.js')
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
test('handles prefix with leading slash', () => {
|
|
350
|
+
expect(rs._urlJoin('/fakeprefix', 'index.js')).toBe('/fakeprefix/index.js')
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
test('handles relative directory path', () => {
|
|
354
|
+
expect(rs._urlJoin('deep', 'index.js')).toBe('deep/index.js')
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
test('handles empty prefix', () => {
|
|
358
|
+
expect(rs._urlJoin('', 'index.js')).toBe('index.js')
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
test('handles prefix with mixed separators', () => {
|
|
362
|
+
expect(rs._urlJoin('prefix\\deep', 'nested/file.js')).toBe('prefix/deep/nested/file.js')
|
|
363
|
+
})
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
describe('special characters and edge cases', () => {
|
|
367
|
+
test('handles file with no extension', () => {
|
|
368
|
+
expect(rs._urlJoin('prefix', 'file')).toBe('prefix/file')
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
test('handles file with multiple dots', () => {
|
|
372
|
+
expect(rs._urlJoin('prefix', 'file.min.js')).toBe('prefix/file.min.js')
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
test('handles path with spaces', () => {
|
|
376
|
+
expect(rs._urlJoin('prefix', 'file name.js')).toBe('prefix/file name.js')
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
test('handles path with special characters', () => {
|
|
380
|
+
expect(rs._urlJoin('prefix', 'file-name_123.js')).toBe('prefix/file-name_123.js')
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
test('handles very long path', () => {
|
|
384
|
+
const longPath = Array(10).fill('deep').join('/')
|
|
385
|
+
expect(rs._urlJoin('prefix', longPath, 'file.js')).toBe(`prefix/${longPath}/file.js`)
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
test('handles path starting with dot', () => {
|
|
389
|
+
expect(rs._urlJoin('prefix', '.hidden', 'file.js')).toBe('prefix/.hidden/file.js')
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
test('handles path with dot-dot', () => {
|
|
393
|
+
expect(rs._urlJoin('prefix', '..', 'file.js')).toBe('prefix/../file.js')
|
|
394
|
+
})
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
describe('empty and null handling', () => {
|
|
398
|
+
test('returns empty string for no arguments', () => {
|
|
399
|
+
expect(rs._urlJoin()).toBe('')
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
test('returns empty string for single empty string', () => {
|
|
403
|
+
expect(rs._urlJoin('')).toBe('')
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
test('filters out all empty parts', () => {
|
|
407
|
+
expect(rs._urlJoin('', '', '')).toBe('')
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
test('handles mix of valid and empty parts', () => {
|
|
411
|
+
expect(rs._urlJoin('', 'prefix', '', 'file.js', '')).toBe('prefix/file.js')
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
test('handles null in middle', () => {
|
|
415
|
+
expect(rs._urlJoin('prefix', null, 'file.js')).toBe('prefix/file.js')
|
|
416
|
+
})
|
|
417
|
+
|
|
418
|
+
test('handles undefined in middle', () => {
|
|
419
|
+
expect(rs._urlJoin('prefix', undefined, 'file.js')).toBe('prefix/file.js')
|
|
420
|
+
})
|
|
421
|
+
})
|
|
422
|
+
|
|
423
|
+
describe('complex combinations', () => {
|
|
424
|
+
test('combines all normalization features', () => {
|
|
425
|
+
expect(rs._urlJoin('/prefix\\deep/', '//nested/', '\\file.js')).toBe('/prefix/deep/nested/file.js')
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
test('handles Windows path with leading slash', () => {
|
|
429
|
+
expect(rs._urlJoin('/prefix\\deep\\dir', 'index.js')).toBe('/prefix/deep/dir/index.js')
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
test('handles multiple trailing slashes', () => {
|
|
433
|
+
expect(rs._urlJoin('prefix///', 'deep///', 'file.js')).toBe('prefix/deep/file.js')
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
test('handles mixed separators and slashes', () => {
|
|
437
|
+
expect(rs._urlJoin('prefix\\deep', '/nested', '\\file.js')).toBe('prefix/deep/nested/file.js')
|
|
438
|
+
})
|
|
439
|
+
})
|
|
440
|
+
})
|
|
441
|
+
|
|
179
442
|
test('folderExists missing prefix', async () => {
|
|
180
443
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
181
444
|
await expect(rs.folderExists()).rejects.toEqual(expect.objectContaining({ message: 'prefix must be a valid string' }))
|
|
@@ -191,6 +454,20 @@ describe('RemoteStorage', () => {
|
|
|
191
454
|
await expect(rs.uploadFile()).rejects.toEqual(expect.objectContaining({ message: 'prefix must be a valid string' }))
|
|
192
455
|
})
|
|
193
456
|
|
|
457
|
+
test('uploadFile with windows path', async () => {
|
|
458
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
459
|
+
// Create file using platform-agnostic paths
|
|
460
|
+
await global.addFakeFiles(vol, 'fakeDir', { 'deep/index.js': 'fake content' })
|
|
461
|
+
// Use platform-specific path for file reading (must match actual file system)
|
|
462
|
+
const filePath = path.join('fakeDir', 'deep', 'index.js')
|
|
463
|
+
// Test that Windows-style backslashes in prefix are normalized correctly
|
|
464
|
+
const prefixPath = 'fakeprefix\\deep\\dir\\'
|
|
465
|
+
await expect(rs.uploadFile(filePath, prefixPath, global.fakeConfig, 'fakeDir')).resolves.toBeUndefined()
|
|
466
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(
|
|
467
|
+
expect.objectContaining({ Bucket: 'fake-bucket', Key: 'fakeprefix/deep/dir/index.js' })
|
|
468
|
+
)
|
|
469
|
+
})
|
|
470
|
+
|
|
194
471
|
test('uploadDir missing prefix', async () => {
|
|
195
472
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
196
473
|
await expect(rs.uploadDir()).rejects.toEqual(expect.objectContaining({ message: 'prefix must be a valid string' }))
|
|
@@ -300,31 +577,59 @@ describe('RemoteStorage', () => {
|
|
|
300
577
|
test('cachecontrol string for html', async () => {
|
|
301
578
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
302
579
|
const response = rs._getCacheControlConfig('text/html', global.fakeConfig.app)
|
|
303
|
-
expect(response).toBe('s-maxage=
|
|
580
|
+
expect(response).toBe('s-maxage=60, max-age=60')
|
|
304
581
|
})
|
|
305
582
|
|
|
306
583
|
test('cachecontrol string for JS', async () => {
|
|
307
584
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
308
585
|
const response = rs._getCacheControlConfig('application/javascript', global.fakeConfig.app)
|
|
309
|
-
expect(response).toBe('s-maxage=
|
|
586
|
+
expect(response).toBe('s-maxage=60, max-age=604800')
|
|
310
587
|
})
|
|
311
588
|
|
|
312
589
|
test('cachecontrol string for CSS', async () => {
|
|
313
590
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
314
591
|
const response = rs._getCacheControlConfig('text/css', global.fakeConfig.app)
|
|
315
|
-
expect(response).toBe('s-maxage=
|
|
592
|
+
expect(response).toBe('s-maxage=60, max-age=604800')
|
|
316
593
|
})
|
|
317
594
|
|
|
318
595
|
test('cachecontrol string for Image', async () => {
|
|
319
596
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
320
597
|
const response = rs._getCacheControlConfig('image/jpeg', global.fakeConfig.app)
|
|
321
|
-
expect(response).toBe('s-maxage=
|
|
598
|
+
expect(response).toBe('s-maxage=60, max-age=604800')
|
|
322
599
|
})
|
|
323
600
|
|
|
324
601
|
test('cachecontrol string for default', async () => {
|
|
325
602
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
326
603
|
const response = rs._getCacheControlConfig('application/pdf', global.fakeConfig.app)
|
|
327
|
-
expect(response).toBe(
|
|
604
|
+
expect(response).toBe(null)
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
test('cachecontrol string for html when htmlCacheDuration is not defined', async () => {
|
|
608
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
609
|
+
const appConfigWithoutHtmlCache = global.configWithMissing(global.fakeConfig.app, 'htmlCacheDuration')
|
|
610
|
+
const response = rs._getCacheControlConfig('text/html', appConfigWithoutHtmlCache)
|
|
611
|
+
expect(response).toBe(null)
|
|
612
|
+
})
|
|
613
|
+
|
|
614
|
+
test('cachecontrol string for JS when jsCacheDuration is not defined', async () => {
|
|
615
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
616
|
+
const appConfigWithoutJsCache = global.configWithMissing(global.fakeConfig.app, 'jsCacheDuration')
|
|
617
|
+
const response = rs._getCacheControlConfig('application/javascript', appConfigWithoutJsCache)
|
|
618
|
+
expect(response).toBe(null)
|
|
619
|
+
})
|
|
620
|
+
|
|
621
|
+
test('cachecontrol string for CSS when cssCacheDuration is not defined', async () => {
|
|
622
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
623
|
+
const appConfigWithoutCssCache = global.configWithMissing(global.fakeConfig.app, 'cssCacheDuration')
|
|
624
|
+
const response = rs._getCacheControlConfig('text/css', appConfigWithoutCssCache)
|
|
625
|
+
expect(response).toBe(null)
|
|
626
|
+
})
|
|
627
|
+
|
|
628
|
+
test('cachecontrol string for image when imageCacheDuration is not defined', async () => {
|
|
629
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
630
|
+
const appConfigWithoutImageCache = global.configWithMissing(global.fakeConfig.app, 'imageCacheDuration')
|
|
631
|
+
const response = rs._getCacheControlConfig('image/jpeg', appConfigWithoutImageCache)
|
|
632
|
+
expect(response).toBe(null)
|
|
328
633
|
})
|
|
329
634
|
|
|
330
635
|
// response header tests
|
|
@@ -573,4 +878,75 @@ describe('RemoteStorage', () => {
|
|
|
573
878
|
}
|
|
574
879
|
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining(expected))
|
|
575
880
|
})
|
|
881
|
+
|
|
882
|
+
test('Cache control override from response headers', async () => {
|
|
883
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.html': 'fake content' })
|
|
884
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
885
|
+
const files = await rs.walkDir('fakeDir')
|
|
886
|
+
const fakeDistRoot = path.parse(files[0]).dir
|
|
887
|
+
const filePath = files[0] // Use absolute path from walkDir
|
|
888
|
+
const newConfig = global.configWithModifiedWeb(global.fakeConfig, {
|
|
889
|
+
'response-headers': {
|
|
890
|
+
'/*.html': {
|
|
891
|
+
'cache-control': 'max-age=3600, s-maxage=7200',
|
|
892
|
+
testHeader: 'generic-header'
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
})
|
|
896
|
+
await rs.uploadFile(filePath, 'fakeprefix', newConfig, fakeDistRoot)
|
|
897
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
898
|
+
const expected = {
|
|
899
|
+
Bucket: 'fake-bucket',
|
|
900
|
+
Key: 'fakeprefix/index.html',
|
|
901
|
+
Body: body,
|
|
902
|
+
ContentType: 'text/html',
|
|
903
|
+
CacheControl: 'max-age=3600, s-maxage=7200',
|
|
904
|
+
Metadata: {
|
|
905
|
+
'adp-testHeader': 'generic-header'
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
expect(mockS3.putObject).toHaveBeenCalledWith(expect.objectContaining(expected))
|
|
909
|
+
// Verify that adp-cache-control was removed from metadata
|
|
910
|
+
const putObjectCall = mockS3.putObject.mock.calls[0][0]
|
|
911
|
+
expect(putObjectCall.Metadata).not.toHaveProperty('adp-cache-control')
|
|
912
|
+
})
|
|
913
|
+
|
|
914
|
+
test('uploadFile does not set Metadata when responseHeaders is empty', async () => {
|
|
915
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.js': 'fake content' })
|
|
916
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
917
|
+
const fakeConfig = {
|
|
918
|
+
app: global.fakeConfig.app
|
|
919
|
+
// No web.response-headers
|
|
920
|
+
}
|
|
921
|
+
await rs.uploadFile('fakeDir/index.js', 'fakeprefix', fakeConfig, 'fakeDir')
|
|
922
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
923
|
+
const putObjectCall = mockS3.putObject.mock.calls[0][0]
|
|
924
|
+
expect(putObjectCall).not.toHaveProperty('Metadata')
|
|
925
|
+
expect(putObjectCall).toMatchObject({
|
|
926
|
+
Bucket: 'fake-bucket',
|
|
927
|
+
Key: 'fakeprefix/index.js',
|
|
928
|
+
Body: body,
|
|
929
|
+
ContentType: 'application/javascript'
|
|
930
|
+
})
|
|
931
|
+
})
|
|
932
|
+
|
|
933
|
+
test('uploadFile sets CacheControl even when responseHeaders is empty', async () => {
|
|
934
|
+
global.addFakeFiles(vol, 'fakeDir', { 'index.html': 'fake content' })
|
|
935
|
+
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
936
|
+
const fakeConfig = {
|
|
937
|
+
app: global.fakeConfig.app
|
|
938
|
+
// No web.response-headers
|
|
939
|
+
}
|
|
940
|
+
await rs.uploadFile('fakeDir/index.html', 'fakeprefix', fakeConfig, 'fakeDir')
|
|
941
|
+
const body = Buffer.from('fake content', 'utf8')
|
|
942
|
+
const putObjectCall = mockS3.putObject.mock.calls[0][0]
|
|
943
|
+
expect(putObjectCall).not.toHaveProperty('Metadata')
|
|
944
|
+
expect(putObjectCall).toMatchObject({
|
|
945
|
+
Bucket: 'fake-bucket',
|
|
946
|
+
Key: 'fakeprefix/index.html',
|
|
947
|
+
Body: body,
|
|
948
|
+
ContentType: 'text/html',
|
|
949
|
+
CacheControl: 's-maxage=60, max-age=60'
|
|
950
|
+
})
|
|
951
|
+
})
|
|
576
952
|
})
|