@adobe/aio-lib-web 7.0.3 → 7.0.5
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/lib/remote-storage.js +41 -16
- package/package.json +12 -8
- package/test/lib/remote-storage.test.js +113 -3
package/lib/remote-storage.js
CHANGED
|
@@ -17,6 +17,9 @@ const fs = require('fs-extra')
|
|
|
17
17
|
const joi = require('joi')
|
|
18
18
|
const klaw = require('klaw')
|
|
19
19
|
const http = require('http')
|
|
20
|
+
// Proxy support for AWS SDK v3 (inspired by PR #224 by pat-lego, with compatibility fixes)
|
|
21
|
+
const { NodeHttpHandler } = require('@smithy/node-http-handler')
|
|
22
|
+
const { ProxyAgent } = require('proxy-agent')
|
|
20
23
|
const { codes, logAndThrow } = require('./StorageError')
|
|
21
24
|
|
|
22
25
|
const fileExtensionPattern = /\*\.[0-9a-zA-Z]+$/
|
|
@@ -71,8 +74,20 @@ module.exports = class RemoteStorage {
|
|
|
71
74
|
}
|
|
72
75
|
this.bucket = creds.params.Bucket
|
|
73
76
|
|
|
77
|
+
// Configure proxy support for AWS SDK v3
|
|
78
|
+
// ProxyAgent automatically handles proxy environment variables via proxy-from-env
|
|
79
|
+
const agent = new ProxyAgent()
|
|
80
|
+
const s3Config = {
|
|
81
|
+
credentials,
|
|
82
|
+
region,
|
|
83
|
+
requestHandler: new NodeHttpHandler({
|
|
84
|
+
httpAgent: agent,
|
|
85
|
+
httpsAgent: agent
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
74
89
|
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3.html#constructor
|
|
75
|
-
this.s3 = new S3(
|
|
90
|
+
this.s3 = new S3(s3Config)
|
|
76
91
|
}
|
|
77
92
|
|
|
78
93
|
/**
|
|
@@ -245,21 +260,31 @@ module.exports = class RemoteStorage {
|
|
|
245
260
|
// walk the whole directory recursively using klaw.
|
|
246
261
|
const files = await this.walkDir(dir)
|
|
247
262
|
|
|
248
|
-
//
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
+
// we will upload files in batches of 50 to prevent warnings about enqued tasks.
|
|
264
|
+
// this happens based on the default maxSockets value of 50 in node.js but we cannot change
|
|
265
|
+
// the user's default value.
|
|
266
|
+
// less than 1% of users will see this warning, but it is better to prevent it.
|
|
267
|
+
const batchSize = 50
|
|
268
|
+
let fileBatch = files.splice(0, batchSize)
|
|
269
|
+
const allResults = []
|
|
270
|
+
while (fileBatch.length > 0) {
|
|
271
|
+
const res = await Promise.all(fileBatch.map(async f => {
|
|
272
|
+
// get file's relative folder to the base directory.
|
|
273
|
+
let prefixDirectory = path.dirname(path.relative(dir, f))
|
|
274
|
+
// base directory returns ".", ignore that.
|
|
275
|
+
prefixDirectory = prefixDirectory === '.' ? '' : prefixDirectory
|
|
276
|
+
// newPrefix is now the initial prefix plus the files relative directory path.
|
|
277
|
+
const newPrefix = urlJoin(prefix, prefixDirectory)
|
|
278
|
+
const s3Res = await this.uploadFile(f, newPrefix, appConfig, dir)
|
|
279
|
+
if (postFileUploadCallback) {
|
|
280
|
+
postFileUploadCallback(f)
|
|
281
|
+
}
|
|
282
|
+
return s3Res
|
|
283
|
+
}))
|
|
284
|
+
allResults.push(res)
|
|
285
|
+
fileBatch = files.splice(0, batchSize)
|
|
286
|
+
}
|
|
287
|
+
return allResults
|
|
263
288
|
}
|
|
264
289
|
|
|
265
290
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/aio-lib-web",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.5",
|
|
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,6 +33,7 @@
|
|
|
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": "^3.0.0",
|
|
36
37
|
"core-js": "^3.25.1",
|
|
37
38
|
"fs-extra": "^11",
|
|
38
39
|
"joi": "^17.2.1",
|
|
@@ -40,18 +41,21 @@
|
|
|
40
41
|
"lodash.clonedeep": "^4.5.0",
|
|
41
42
|
"mime-types": "^2.1.24",
|
|
42
43
|
"parcel": "^2.7.0",
|
|
44
|
+
"proxy-agent": "^6.3.0",
|
|
43
45
|
"regenerator-runtime": "^0.13.7"
|
|
44
46
|
},
|
|
45
47
|
"devDependencies": {
|
|
48
|
+
"@adobe/eslint-config-aio-lib-config": "^4.0.0",
|
|
46
49
|
"@types/hapi__joi": "^17",
|
|
47
50
|
"@types/jest": "^29",
|
|
48
|
-
"eslint": "^8",
|
|
49
|
-
"eslint-config-standard": "^17",
|
|
50
|
-
"eslint-plugin-import": "^2.
|
|
51
|
-
"eslint-plugin-jest": "^27.0
|
|
52
|
-
"eslint-plugin-
|
|
53
|
-
"eslint-plugin-
|
|
54
|
-
"eslint-plugin-
|
|
51
|
+
"eslint": "^8.57.1",
|
|
52
|
+
"eslint-config-standard": "^17.1.0",
|
|
53
|
+
"eslint-plugin-import": "^2.31.0",
|
|
54
|
+
"eslint-plugin-jest": "^27.9.0",
|
|
55
|
+
"eslint-plugin-jsdoc": "^48.11.0",
|
|
56
|
+
"eslint-plugin-n": "^15.7.0",
|
|
57
|
+
"eslint-plugin-node": "^11.1.0",
|
|
58
|
+
"eslint-plugin-promise": "^6.6.0",
|
|
55
59
|
"eslint-plugin-standard": "^5",
|
|
56
60
|
"jest": "^29",
|
|
57
61
|
"jsdoc-to-markdown": "^8",
|
|
@@ -49,7 +49,8 @@ describe('RemoteStorage', () => {
|
|
|
49
49
|
sessionToken: global.fakeTVMResponse.sessionToken,
|
|
50
50
|
expiration: new Date(global.fakeTVMResponse.expiration)
|
|
51
51
|
},
|
|
52
|
-
region: 'us-east-1'
|
|
52
|
+
region: 'us-east-1',
|
|
53
|
+
requestHandler: expect.any(Object)
|
|
53
54
|
})
|
|
54
55
|
rs.bucket = global.fakeTVMResponse.Bucket
|
|
55
56
|
})
|
|
@@ -59,13 +60,122 @@ describe('RemoteStorage', () => {
|
|
|
59
60
|
expect(S3).toHaveBeenCalledWith({
|
|
60
61
|
credentials: {
|
|
61
62
|
accessKeyId: global.fakeTVMResponse.accessKeyId,
|
|
62
|
-
secretAccessKey: global.fakeTVMResponse.secretAccessKey
|
|
63
|
+
secretAccessKey: global.fakeTVMResponse.secretAccessKey,
|
|
64
|
+
sessionToken: undefined,
|
|
65
|
+
expiration: undefined
|
|
63
66
|
},
|
|
64
|
-
region: 'us-east-1'
|
|
67
|
+
region: 'us-east-1',
|
|
68
|
+
requestHandler: expect.any(Object)
|
|
65
69
|
})
|
|
66
70
|
rs.bucket = global.fakeTVMResponse.Bucket
|
|
67
71
|
})
|
|
68
72
|
|
|
73
|
+
describe('Proxy configuration', () => {
|
|
74
|
+
const originalEnv = process.env
|
|
75
|
+
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
// Clear environment variables before each test
|
|
78
|
+
delete process.env.https_proxy
|
|
79
|
+
delete process.env.HTTPS_PROXY
|
|
80
|
+
delete process.env.http_proxy
|
|
81
|
+
delete process.env.HTTP_PROXY
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
afterAll(() => {
|
|
85
|
+
// Restore original environment
|
|
86
|
+
process.env = originalEnv
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('Constructor uses HTTPS_PROXY when set (uppercase)', async () => {
|
|
90
|
+
process.env.HTTPS_PROXY = 'http://proxy.example.com:8080'
|
|
91
|
+
// eslint-disable-next-line no-new
|
|
92
|
+
new RemoteStorage(global.fakeTVMResponse)
|
|
93
|
+
|
|
94
|
+
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
|
|
95
|
+
requestHandler: expect.any(Object),
|
|
96
|
+
credentials: expect.any(Object),
|
|
97
|
+
region: 'us-east-1'
|
|
98
|
+
}))
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('Constructor uses https_proxy when set (lowercase)', async () => {
|
|
102
|
+
process.env.https_proxy = 'http://proxy.example.com:3128'
|
|
103
|
+
// eslint-disable-next-line no-new
|
|
104
|
+
new RemoteStorage(global.fakeTVMResponse)
|
|
105
|
+
|
|
106
|
+
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
|
|
107
|
+
requestHandler: expect.any(Object),
|
|
108
|
+
credentials: expect.any(Object),
|
|
109
|
+
region: 'us-east-1'
|
|
110
|
+
}))
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('Constructor uses HTTP_PROXY when HTTPS_PROXY not set', async () => {
|
|
114
|
+
process.env.HTTP_PROXY = 'http://proxy.example.com:8080'
|
|
115
|
+
// eslint-disable-next-line no-new
|
|
116
|
+
new RemoteStorage(global.fakeTVMResponse)
|
|
117
|
+
|
|
118
|
+
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
|
|
119
|
+
requestHandler: expect.any(Object),
|
|
120
|
+
credentials: expect.any(Object),
|
|
121
|
+
region: 'us-east-1'
|
|
122
|
+
}))
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('Constructor uses http_proxy when other proxy vars not set', async () => {
|
|
126
|
+
process.env.http_proxy = 'http://proxy.example.com:3128'
|
|
127
|
+
// eslint-disable-next-line no-new
|
|
128
|
+
new RemoteStorage(global.fakeTVMResponse)
|
|
129
|
+
|
|
130
|
+
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
|
|
131
|
+
requestHandler: expect.any(Object),
|
|
132
|
+
credentials: expect.any(Object),
|
|
133
|
+
region: 'us-east-1'
|
|
134
|
+
}))
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('Constructor prioritizes HTTPS_PROXY over HTTP_PROXY', async () => {
|
|
138
|
+
process.env.HTTPS_PROXY = 'http://https-proxy.example.com:8080'
|
|
139
|
+
process.env.HTTP_PROXY = 'http://http-proxy.example.com:8080'
|
|
140
|
+
// eslint-disable-next-line no-new
|
|
141
|
+
new RemoteStorage(global.fakeTVMResponse)
|
|
142
|
+
|
|
143
|
+
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
|
|
144
|
+
requestHandler: expect.any(Object),
|
|
145
|
+
credentials: expect.any(Object),
|
|
146
|
+
region: 'us-east-1'
|
|
147
|
+
}))
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('Constructor prioritizes https_proxy over HTTP_PROXY', async () => {
|
|
151
|
+
process.env.https_proxy = 'http://https-proxy.example.com:3128'
|
|
152
|
+
process.env.HTTP_PROXY = 'http://http-proxy.example.com:8080'
|
|
153
|
+
// eslint-disable-next-line no-new
|
|
154
|
+
new RemoteStorage(global.fakeTVMResponse)
|
|
155
|
+
|
|
156
|
+
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
|
|
157
|
+
requestHandler: expect.any(Object),
|
|
158
|
+
credentials: expect.any(Object),
|
|
159
|
+
region: 'us-east-1'
|
|
160
|
+
}))
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test('Constructor always includes requestHandler with ProxyAgent', async () => {
|
|
164
|
+
// eslint-disable-next-line no-new
|
|
165
|
+
new RemoteStorage(global.fakeTVMResponse)
|
|
166
|
+
|
|
167
|
+
expect(S3).toHaveBeenCalledWith({
|
|
168
|
+
credentials: expect.any(Object),
|
|
169
|
+
region: 'us-east-1',
|
|
170
|
+
requestHandler: expect.any(Object)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
// ProxyAgent handles proxy detection automatically via proxy-from-env
|
|
174
|
+
const s3CallArgs = S3.mock.calls[S3.mock.calls.length - 1][0]
|
|
175
|
+
expect(s3CallArgs).toHaveProperty('requestHandler')
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
69
179
|
test('folderExists missing prefix', async () => {
|
|
70
180
|
const rs = new RemoteStorage(global.fakeTVMResponse)
|
|
71
181
|
await expect(rs.folderExists()).rejects.toEqual(expect.objectContaining({ message: 'prefix must be a valid string' }))
|