@adobe/aio-lib-web 7.2.0 → 8.0.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/README.md +1 -1
- package/e2e/e2e.js +12 -4
- package/lib/remote-storage.js +219 -75
- package/package.json +5 -3
- package/src/build-web.js +11 -1
- package/src/bundle.js +28 -6
- package/src/constants.js +19 -0
- package/src/deploy-web.js +28 -9
- package/src/undeploy-web.js +18 -6
- package/test/jest.setup.js +28 -0
- package/test/lib/remote-storage.test.js +952 -554
- package/test/src/build-web.test.js +40 -1
- package/test/src/bundle.test.js +52 -0
- package/test/src/deploy-web.test.js +197 -79
- package/test/src/undeploy-web.test.js +88 -19
- package/lib/getS3Creds.js +0 -42
- package/test/lib/getS3Creds.test.js +0 -73
package/README.md
CHANGED
|
@@ -30,4 +30,4 @@ Contributions are welcomed! Read the [Contributing Guide](./.github/CONTRIBUTING
|
|
|
30
30
|
|
|
31
31
|
## Licensing
|
|
32
32
|
|
|
33
|
-
This project is licensed under the Apache V2 License. See [LICENSE](LICENSE) for more information.
|
|
33
|
+
This project is licensed under the Apache V2 License. See [LICENSE](LICENSE) for more information.
|
package/e2e/e2e.js
CHANGED
|
@@ -25,7 +25,9 @@ const undeployWeb = require('../src/undeploy-web')
|
|
|
25
25
|
const ENTRY_FILE = path.join(__dirname, 'sample-app/index.html')
|
|
26
26
|
const DEST_FOLDER = path.join(__dirname, 'dist/')
|
|
27
27
|
const CACHE_FILE = path.join(__dirname, '.cache')
|
|
28
|
-
|
|
28
|
+
|
|
29
|
+
// Regex to capture the `src` attribute of a `<script>` tag.
|
|
30
|
+
const SRC_REGEX = /<script.*src=["']?(?<src>[^"'\s>]+)["']?/
|
|
29
31
|
|
|
30
32
|
const config = {
|
|
31
33
|
app: {
|
|
@@ -97,15 +99,21 @@ describe('e2e', () => {
|
|
|
97
99
|
})
|
|
98
100
|
|
|
99
101
|
test('undeploy', async () => {
|
|
100
|
-
let error, response
|
|
102
|
+
let error, response, contents
|
|
101
103
|
try {
|
|
102
104
|
await undeployWeb(config)
|
|
103
|
-
|
|
105
|
+
// undeployWeb empties the S3 origin, but CloudFront may still serve the previously cached page on the canonical URL for its default TTL
|
|
106
|
+
// Adding a unique query string bypasses the edge cache so we read the current origin state.
|
|
107
|
+
response = await fetch(`${url}?nocache=${Date.now()}`)
|
|
108
|
+
contents = await response.text()
|
|
104
109
|
} catch (e) {
|
|
105
110
|
error = e
|
|
106
111
|
console.error(e)
|
|
107
112
|
}
|
|
108
113
|
expect(error).toBeUndefined()
|
|
109
|
-
|
|
114
|
+
// assert that CDN serves its not-found page.
|
|
115
|
+
expect(response.status).toEqual(200)
|
|
116
|
+
expect(contents).toContain('404 Not Found')
|
|
117
|
+
expect(contents).toContain('The page you requested cannot be found.')
|
|
110
118
|
})
|
|
111
119
|
})
|
package/lib/remote-storage.js
CHANGED
|
@@ -20,19 +20,52 @@ const http = require('http')
|
|
|
20
20
|
// Proxy support for AWS SDK v3 (inspired by PR #224 by pat-lego, with compatibility fixes)
|
|
21
21
|
const { NodeHttpHandler } = require('@smithy/node-http-handler')
|
|
22
22
|
const { ProxyAgent } = require('proxy-agent')
|
|
23
|
+
const { EnvHttpProxyAgent } = require('undici')
|
|
23
24
|
const { codes, logAndThrow } = require('./StorageError')
|
|
25
|
+
const aioLibEnv = require('@adobe/aio-lib-env')
|
|
26
|
+
|
|
27
|
+
// Set AIO_DEPLOYMENT_SERVICE_URL env var to override the default deploy-service url
|
|
28
|
+
// dev deploy => https://deploy-service.dev.app-builder.adp.adobe.io
|
|
29
|
+
// run local => http://localhost:3000
|
|
30
|
+
const getDeploymentServiceUrl = () => {
|
|
31
|
+
if (process.env.AIO_DEPLOYMENT_SERVICE_URL) {
|
|
32
|
+
return process.env.AIO_DEPLOYMENT_SERVICE_URL
|
|
33
|
+
}
|
|
34
|
+
return aioLibEnv.getCliEnv() === aioLibEnv.PROD_ENV
|
|
35
|
+
? 'https://deploy-service.app-builder.adp.adobe.io'
|
|
36
|
+
: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io'
|
|
37
|
+
}
|
|
24
38
|
|
|
25
39
|
const fileExtensionPattern = /\*\.[0-9a-zA-Z]+$/
|
|
26
40
|
|
|
41
|
+
let fetchDispatcher
|
|
42
|
+
const getFetchDispatcher = () => {
|
|
43
|
+
if (!fetchDispatcher) {
|
|
44
|
+
fetchDispatcher = new EnvHttpProxyAgent()
|
|
45
|
+
}
|
|
46
|
+
return fetchDispatcher
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const deployServiceFetch = (url, init) => {
|
|
50
|
+
return fetch(url, { ...init, dispatcher: getFetchDispatcher() })
|
|
51
|
+
}
|
|
52
|
+
|
|
27
53
|
module.exports = class RemoteStorage {
|
|
28
54
|
/**
|
|
29
|
-
*
|
|
55
|
+
* Without creds the deploy-service CDN API is used.
|
|
56
|
+
* With creds (BYO AWS S3) the S3 bucket is accessed directly.
|
|
57
|
+
*
|
|
58
|
+
* @param {object} [creds]
|
|
30
59
|
* @param {string} creds.accessKeyId
|
|
31
60
|
* @param {string} creds.secretAccessKey
|
|
32
61
|
* @param {string} creds.params.Bucket
|
|
33
62
|
* @param {string} [creds.sessionToken]
|
|
34
63
|
*/
|
|
35
64
|
constructor (creds) {
|
|
65
|
+
if (creds === undefined || creds === null) {
|
|
66
|
+
// deploy-service mode
|
|
67
|
+
return
|
|
68
|
+
}
|
|
36
69
|
const res = joi.object().keys({
|
|
37
70
|
sessionToken: joi.string(),
|
|
38
71
|
accessKeyId: joi.string().required(),
|
|
@@ -75,97 +108,196 @@ module.exports = class RemoteStorage {
|
|
|
75
108
|
}
|
|
76
109
|
|
|
77
110
|
/**
|
|
78
|
-
* Checks if
|
|
79
|
-
* @param {string}
|
|
80
|
-
* @
|
|
111
|
+
* Checks if any files exist for the namespace
|
|
112
|
+
* @param {string} authToken - Authorization header value (unused for BYO S3)
|
|
113
|
+
* @param {string} prefix - unused for deploy-service, S3 prefix for BYO S3
|
|
114
|
+
* @param {Object} appConfig - application config
|
|
115
|
+
* @returns {Promise<boolean>} true if files exist, false otherwise
|
|
81
116
|
*/
|
|
82
|
-
async folderExists (prefix) {
|
|
117
|
+
async folderExists (authToken, prefix, appConfig) {
|
|
83
118
|
if (typeof prefix !== 'string') {
|
|
84
119
|
throw new Error('prefix must be a valid string')
|
|
85
120
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
121
|
+
if (this.s3) {
|
|
122
|
+
const listParams = {
|
|
123
|
+
Bucket: this.bucket,
|
|
124
|
+
Prefix: prefix
|
|
125
|
+
}
|
|
126
|
+
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
91
127
|
|
|
92
|
-
|
|
128
|
+
return listedObjects.KeyCount > 0
|
|
129
|
+
}
|
|
130
|
+
// Call the list files endpoint (GET /files) - there is no GET /files/:key route
|
|
131
|
+
const response = await deployServiceFetch(`${getDeploymentServiceUrl()}/cdn-api/namespaces/${appConfig.ow.namespace}/files`, {
|
|
132
|
+
method: 'GET',
|
|
133
|
+
headers: {
|
|
134
|
+
Authorization: authToken
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
if (!response.ok) {
|
|
138
|
+
throw new Error(`cannot check if folder exists, request failed: ${response.status} ${response.statusText}`)
|
|
139
|
+
}
|
|
140
|
+
const files = await response.json()
|
|
141
|
+
// Check if there are any files (folder "exists" if it has content)
|
|
142
|
+
return Array.isArray(files) && files.length > 0
|
|
93
143
|
}
|
|
94
144
|
|
|
95
145
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @param
|
|
146
|
+
* Empties all files for the namespace or deletes a specific file
|
|
147
|
+
* @param {string} authToken - Authorization header value
|
|
148
|
+
* @param {string} prefix - '/' to delete all files, or a specific file path
|
|
149
|
+
* @param {Object} appConfig - application config
|
|
150
|
+
* @returns {Promise<boolean>} true if the folder was emptied, false otherwise
|
|
98
151
|
*/
|
|
99
|
-
async emptyFolder (prefix) {
|
|
100
|
-
if (typeof prefix !== 'string')
|
|
101
|
-
|
|
102
|
-
Bucket: this.bucket,
|
|
103
|
-
Prefix: prefix
|
|
152
|
+
async emptyFolder (authToken, prefix, appConfig) {
|
|
153
|
+
if (typeof prefix !== 'string') {
|
|
154
|
+
throw new Error('prefix must be a valid string')
|
|
104
155
|
}
|
|
105
|
-
|
|
156
|
+
if (this.s3) {
|
|
157
|
+
const listParams = {
|
|
158
|
+
Bucket: this.bucket,
|
|
159
|
+
Prefix: prefix
|
|
160
|
+
}
|
|
161
|
+
const listedObjects = await this.s3.listObjectsV2(listParams)
|
|
106
162
|
|
|
107
|
-
|
|
163
|
+
if (listedObjects.KeyCount < 1) {
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
const deleteParams = {
|
|
167
|
+
Bucket: this.bucket,
|
|
168
|
+
Delete: { Objects: [] }
|
|
169
|
+
}
|
|
170
|
+
listedObjects.Contents.forEach(({ Key }) => {
|
|
171
|
+
deleteParams.Delete.Objects.push({ Key })
|
|
172
|
+
})
|
|
173
|
+
await this.s3.deleteObjects(deleteParams)
|
|
174
|
+
if (listedObjects.IsTruncated) {
|
|
175
|
+
await this.emptyFolder(authToken, prefix, appConfig)
|
|
176
|
+
}
|
|
108
177
|
return
|
|
109
178
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
179
|
+
// Server route is DELETE /files/:key
|
|
180
|
+
// When key='/' the server triggers emptyStorageForNamespace
|
|
181
|
+
// URL construction: /files/ (trailing slash makes :key = '/')
|
|
182
|
+
const deploymentServiceUrl = getDeploymentServiceUrl()
|
|
183
|
+
const url = prefix === '/'
|
|
184
|
+
? `${deploymentServiceUrl}/cdn-api/namespaces/${appConfig.ow.namespace}/files/`
|
|
185
|
+
: `${deploymentServiceUrl}/cdn-api/namespaces/${appConfig.ow.namespace}/files/${prefix}`
|
|
186
|
+
|
|
187
|
+
const response = await deployServiceFetch(url, {
|
|
188
|
+
method: 'DELETE',
|
|
189
|
+
headers: {
|
|
190
|
+
Authorization: authToken
|
|
191
|
+
}
|
|
116
192
|
})
|
|
117
|
-
|
|
118
|
-
if (listedObjects.IsTruncated) {
|
|
119
|
-
await this.emptyFolder(prefix)
|
|
120
|
-
}
|
|
193
|
+
return response.ok
|
|
121
194
|
}
|
|
122
195
|
|
|
123
196
|
/**
|
|
124
|
-
* Uploads a file
|
|
125
|
-
* @param {string}
|
|
126
|
-
* @param {string}
|
|
197
|
+
* Uploads a file to the CDN API
|
|
198
|
+
* @param {string} authToken - Authorization header value
|
|
199
|
+
* @param {string} file - Full local file path
|
|
200
|
+
* @param {string} filePath - Path relative to namespace (e.g., 'images/photo.jpg' or 'index.html')
|
|
201
|
+
* This becomes file.name in the API request. The server will prepend the namespace.
|
|
127
202
|
* @param {Object} appConfig - application config
|
|
128
|
-
* @param {string} distRoot - Distribution root dir
|
|
203
|
+
* @param {string} distRoot - Distribution root dir (used for header matching)
|
|
129
204
|
*/
|
|
130
|
-
async uploadFile (file,
|
|
131
|
-
if (typeof
|
|
132
|
-
throw new Error('
|
|
205
|
+
async uploadFile (authToken, file, filePath, appConfig, distRoot) {
|
|
206
|
+
if (typeof filePath !== 'string') {
|
|
207
|
+
throw new Error('filePath must be a valid string')
|
|
208
|
+
}
|
|
209
|
+
if (this.s3) {
|
|
210
|
+
// BYO S3: filePath is the S3 prefix to upload the file to
|
|
211
|
+
const prefix = filePath
|
|
212
|
+
const content = await fs.readFile(file)
|
|
213
|
+
const mimeType = mime.lookup(path.extname(file))
|
|
214
|
+
// first we will grab it from the global config: htmlCacheDuration, etc.
|
|
215
|
+
const cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
|
|
216
|
+
const uploadParams = {
|
|
217
|
+
Bucket: this.bucket,
|
|
218
|
+
Key: this._urlJoin(prefix, path.basename(file)),
|
|
219
|
+
Body: content
|
|
220
|
+
}
|
|
221
|
+
// if we found it in the global config, we will use it ( for now )
|
|
222
|
+
if (cacheControlString) {
|
|
223
|
+
uploadParams.CacheControl = cacheControlString
|
|
224
|
+
}
|
|
225
|
+
// add response headers if specified in manifest
|
|
226
|
+
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig) ?? {}
|
|
227
|
+
// here we allow overriding the cache control if specified in response headers
|
|
228
|
+
// this is considered more specific than the general cache control config
|
|
229
|
+
// ideally we deprecate cache control config in favor of response headers directly
|
|
230
|
+
if (responseHeaders?.['adp-cache-control']) {
|
|
231
|
+
uploadParams.CacheControl = responseHeaders['adp-cache-control']
|
|
232
|
+
delete responseHeaders['adp-cache-control']
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// we only set metadata if we have added anything to responseHeaders object
|
|
236
|
+
// it is not null, but could be empty
|
|
237
|
+
if (Object.keys(responseHeaders).length > 0) {
|
|
238
|
+
uploadParams.Metadata = responseHeaders
|
|
239
|
+
}
|
|
240
|
+
// s3 misses some mime types like for css files
|
|
241
|
+
if (mimeType) {
|
|
242
|
+
uploadParams.ContentType = mimeType
|
|
243
|
+
}
|
|
244
|
+
// 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
|
|
245
|
+
// 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
|
|
246
|
+
return this.s3.putObject(uploadParams)
|
|
133
247
|
}
|
|
248
|
+
const url = `${getDeploymentServiceUrl()}/cdn-api/namespaces/${appConfig.ow.namespace}/files`
|
|
249
|
+
|
|
134
250
|
const content = await fs.readFile(file)
|
|
135
251
|
const mimeType = mime.lookup(path.extname(file))
|
|
136
252
|
// first we will grab it from the global config: htmlCacheDuration, etc.
|
|
137
|
-
|
|
138
|
-
const uploadParams = {
|
|
139
|
-
Bucket: this.bucket,
|
|
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
|
|
146
|
-
}
|
|
253
|
+
let cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
|
|
147
254
|
// add response headers if specified in manifest
|
|
148
255
|
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig) ?? {}
|
|
149
256
|
// here we allow overriding the cache control if specified in response headers
|
|
150
257
|
// this is considered more specific than the general cache control config
|
|
151
258
|
// ideally we deprecate cache control config in favor of response headers directly
|
|
152
259
|
if (responseHeaders?.['adp-cache-control']) {
|
|
153
|
-
|
|
260
|
+
cacheControlString = responseHeaders['adp-cache-control']
|
|
154
261
|
delete responseHeaders['adp-cache-control']
|
|
155
262
|
}
|
|
156
|
-
|
|
157
|
-
//
|
|
158
|
-
//
|
|
159
|
-
|
|
160
|
-
|
|
263
|
+
// server expected body is: { contentType, cacheControl, customHeaders: {}, file: { name, content } }
|
|
264
|
+
// file.name is the path relative to namespace (e.g., 'images/photo.jpg' or 'index.html')
|
|
265
|
+
// The server will prepend the namespace to create the S3 key: ${namespace}/${file.name}
|
|
266
|
+
const fileName = path.basename(file)
|
|
267
|
+
let relativeFilePath = filePath
|
|
268
|
+
if (relativeFilePath.startsWith('/')) {
|
|
269
|
+
relativeFilePath = relativeFilePath.substring(1)
|
|
270
|
+
}
|
|
271
|
+
// Use _urlJoin to normalize backslashes and construct the file path
|
|
272
|
+
const filePathForServer = this._urlJoin(relativeFilePath, fileName)
|
|
273
|
+
// NOTE: File content is base64-encoded in the JSON body
|
|
274
|
+
// which increases payload size by ~33%. Look at getting server to handle
|
|
275
|
+
// multipart uploads to avoid this.
|
|
276
|
+
const data = {
|
|
277
|
+
file: {
|
|
278
|
+
contentType: mimeType,
|
|
279
|
+
cacheControl: cacheControlString,
|
|
280
|
+
customHeaders: responseHeaders,
|
|
281
|
+
name: filePathForServer,
|
|
282
|
+
content: Buffer.from(content).toString('base64')
|
|
283
|
+
}
|
|
161
284
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
285
|
+
const response = await deployServiceFetch(url, {
|
|
286
|
+
method: 'PUT',
|
|
287
|
+
body: JSON.stringify(data),
|
|
288
|
+
headers: {
|
|
289
|
+
'Content-Type': 'application/json',
|
|
290
|
+
Authorization: authToken
|
|
291
|
+
}
|
|
292
|
+
}).catch(error => {
|
|
293
|
+
console.error('Error uploading file:', file)
|
|
294
|
+
throw error
|
|
295
|
+
})
|
|
296
|
+
if (!response.ok) {
|
|
297
|
+
console.error('Failed to upload file:', file)
|
|
298
|
+
throw new Error(`Failed to upload file: ${response.statusText}`)
|
|
165
299
|
}
|
|
166
|
-
|
|
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
|
|
168
|
-
return this.s3.putObject(uploadParams)
|
|
300
|
+
return response.status
|
|
169
301
|
}
|
|
170
302
|
|
|
171
303
|
getResponseHeadersForFile (file, distRoot, appConfig) {
|
|
@@ -250,15 +382,17 @@ module.exports = class RemoteStorage {
|
|
|
250
382
|
}
|
|
251
383
|
|
|
252
384
|
/**
|
|
253
|
-
* Uploads all files in a
|
|
254
|
-
* @param {string}
|
|
255
|
-
* @param {string}
|
|
385
|
+
* Uploads all files in a directory recursively to the CDN API
|
|
386
|
+
* @param {string} authToken - Authorization header value
|
|
387
|
+
* @param {string} dir - Local directory with files to upload
|
|
388
|
+
* @param {string} basePath - Base path prefix for all files (e.g., from config.s3.folder)
|
|
389
|
+
* This is combined with each file's relative directory path.
|
|
256
390
|
* @param {Object} appConfig - application config
|
|
257
391
|
* @param {function} [postFileUploadCallback] - called for each uploaded file
|
|
258
392
|
*/
|
|
259
|
-
async uploadDir (dir,
|
|
260
|
-
if (typeof
|
|
261
|
-
throw new Error('
|
|
393
|
+
async uploadDir (authToken, dir, basePath, appConfig, postFileUploadCallback) {
|
|
394
|
+
if (typeof basePath !== 'string') {
|
|
395
|
+
throw new Error('basePath must be a valid string')
|
|
262
396
|
}
|
|
263
397
|
// walk the whole directory recursively using klaw.
|
|
264
398
|
const files = await this.walkDir(dir)
|
|
@@ -271,18 +405,28 @@ module.exports = class RemoteStorage {
|
|
|
271
405
|
let fileBatch = files.splice(0, batchSize)
|
|
272
406
|
const allResults = []
|
|
273
407
|
while (fileBatch.length > 0) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
//
|
|
280
|
-
|
|
281
|
-
|
|
408
|
+
// this used to sleep, but I don't think we need it here.
|
|
409
|
+
// I am leaving it here in case we see issues with rate limiting.
|
|
410
|
+
// sleep for 100ms to prevent rate limiting
|
|
411
|
+
// await new Promise(resolve => setTimeout(resolve, 100))
|
|
412
|
+
const res = await Promise.all(fileBatch.map(async file => {
|
|
413
|
+
// Calculate the file's relative directory path from the base directory
|
|
414
|
+
// e.g., if dir='/dist' and file='/dist/images/photo.jpg', relativeDir='images'
|
|
415
|
+
let relativeDir = path.dirname(path.relative(dir, file))
|
|
416
|
+
// path.relative returns '.' for files in the root directory, normalize to empty string
|
|
417
|
+
relativeDir = relativeDir === '.' ? '' : relativeDir
|
|
418
|
+
|
|
419
|
+
// Combine basePath with relativeDir to get the full file path relative to namespace
|
|
420
|
+
// e.g., basePath='' + relativeDir='images' = 'images'
|
|
421
|
+
// basePath='assets' + relativeDir='images' = 'assets/images'
|
|
422
|
+
const filePath = this._urlJoin(basePath, relativeDir)
|
|
423
|
+
|
|
424
|
+
// Upload file with the calculated filePath (server will prepend namespace)
|
|
425
|
+
const s3Result = await this.uploadFile(authToken, file, filePath, appConfig, dir)
|
|
282
426
|
if (postFileUploadCallback) {
|
|
283
|
-
postFileUploadCallback(
|
|
427
|
+
postFileUploadCallback(file)
|
|
284
428
|
}
|
|
285
|
-
return
|
|
429
|
+
return s3Result
|
|
286
430
|
}))
|
|
287
431
|
allResults.push(res)
|
|
288
432
|
fileBatch = files.splice(0, batchSize)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/aio-lib-web",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.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": {
|
|
@@ -30,8 +30,9 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@adobe/aio-lib-core-config": "^5",
|
|
33
|
+
"@adobe/aio-lib-core-errors": "^4.0.1",
|
|
33
34
|
"@adobe/aio-lib-core-logging": "^3",
|
|
34
|
-
"@adobe/aio-lib-
|
|
35
|
+
"@adobe/aio-lib-env": "^3.0.1",
|
|
35
36
|
"@aws-sdk/client-s3": "^3.1045.0",
|
|
36
37
|
"@smithy/node-http-handler": "^4.0.2",
|
|
37
38
|
"core-js": "^3.25.1",
|
|
@@ -42,7 +43,8 @@
|
|
|
42
43
|
"mime-types": "^2.1.24",
|
|
43
44
|
"parcel": "^2.15.4",
|
|
44
45
|
"proxy-agent": "^6.3.0",
|
|
45
|
-
"regenerator-runtime": "^0.13.7"
|
|
46
|
+
"regenerator-runtime": "^0.13.7",
|
|
47
|
+
"undici": "^7.30.2"
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
50
|
"@adobe/eslint-config-aio-lib-config": "^4.0.0",
|
package/src/build-web.js
CHANGED
|
@@ -14,6 +14,8 @@ const fs = require('fs-extra')
|
|
|
14
14
|
const path = require('path')
|
|
15
15
|
const Bundler = require('@parcel/core').default
|
|
16
16
|
|
|
17
|
+
const { DEVELOPMENT_ENV, PRODUCTION_ENV } = require('./constants')
|
|
18
|
+
|
|
17
19
|
/**
|
|
18
20
|
* @deprecated since 4.1.0 ( January, 2021 ), use `bundle` instead
|
|
19
21
|
*/
|
|
@@ -25,6 +27,9 @@ const buildWeb = async (config, log) => {
|
|
|
25
27
|
const dist = config.web.distProd
|
|
26
28
|
const src = config.web.src
|
|
27
29
|
|
|
30
|
+
const nodeEnv = process.env.NODE_ENV || DEVELOPMENT_ENV
|
|
31
|
+
const mode = nodeEnv === PRODUCTION_ENV ? PRODUCTION_ENV : DEVELOPMENT_ENV
|
|
32
|
+
|
|
28
33
|
// clean/create needed dirs
|
|
29
34
|
await fs.emptyDir(dist)
|
|
30
35
|
|
|
@@ -32,10 +37,15 @@ const buildWeb = async (config, log) => {
|
|
|
32
37
|
const bundler = new Bundler({
|
|
33
38
|
entries: path.join(src, 'index.html'),
|
|
34
39
|
defaultConfig: require.resolve('@parcel/config-default'),
|
|
40
|
+
mode,
|
|
41
|
+
env: {
|
|
42
|
+
NODE_ENV: nodeEnv
|
|
43
|
+
},
|
|
35
44
|
shouldDisableCache: true,
|
|
36
45
|
defaultTargetOptions: {
|
|
37
46
|
distDir: dist,
|
|
38
|
-
publicUrl: './'
|
|
47
|
+
publicUrl: './',
|
|
48
|
+
shouldOptimize: mode === 'production'
|
|
39
49
|
},
|
|
40
50
|
logLevel: 'none',
|
|
41
51
|
shouldContentHash: true
|
package/src/bundle.js
CHANGED
|
@@ -13,6 +13,8 @@ governing permissions and limitations under the License.
|
|
|
13
13
|
const Bundler = require('@parcel/core').default
|
|
14
14
|
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web:bundle', { provider: 'debug' })
|
|
15
15
|
|
|
16
|
+
const { DEVELOPMENT_ENV, PRODUCTION_ENV } = require('./constants')
|
|
17
|
+
|
|
16
18
|
/**
|
|
17
19
|
* @typedef {object} BundleWebObject
|
|
18
20
|
* @property {object} the Parcel bundler object
|
|
@@ -26,6 +28,10 @@ const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web:bun
|
|
|
26
28
|
* @property {boolean} watch
|
|
27
29
|
* @property {boolean} minify
|
|
28
30
|
* @property {number} logLevel
|
|
31
|
+
* @property {'development'|'production'} mode
|
|
32
|
+
* @property {object} env
|
|
33
|
+
* @property {object} defaultTargetOptions
|
|
34
|
+
* @property {boolean} shouldOptimize
|
|
29
35
|
*/
|
|
30
36
|
|
|
31
37
|
/**
|
|
@@ -37,7 +43,7 @@ const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-web:bun
|
|
|
37
43
|
* @param {Function} [log] the app logger
|
|
38
44
|
* @returns {BundleWebObject} the BundleWebObject
|
|
39
45
|
*/
|
|
40
|
-
module.exports = async (entries, dest, options = {
|
|
46
|
+
module.exports = async (entries, dest, options = {}, log = () => {}) => {
|
|
41
47
|
aioLogger.debug(`bundle options: ${JSON.stringify(options, null, 2)}`)
|
|
42
48
|
|
|
43
49
|
if (!entries) {
|
|
@@ -47,6 +53,16 @@ module.exports = async (entries, dest, options = { shouldOptimize: false }, log
|
|
|
47
53
|
throw new Error('cannot build web, missing destination')
|
|
48
54
|
}
|
|
49
55
|
|
|
56
|
+
const {
|
|
57
|
+
defaultTargetOptions = {},
|
|
58
|
+
env = {},
|
|
59
|
+
shouldOptimize,
|
|
60
|
+
...parcelOptions
|
|
61
|
+
} = options
|
|
62
|
+
|
|
63
|
+
const nodeEnv = env.NODE_ENV || process.env.NODE_ENV || DEVELOPMENT_ENV
|
|
64
|
+
const mode = parcelOptions.mode || (nodeEnv === PRODUCTION_ENV ? PRODUCTION_ENV : DEVELOPMENT_ENV)
|
|
65
|
+
|
|
50
66
|
// set defaults, but allow override by passed in values
|
|
51
67
|
const parcelBundleOptions = {
|
|
52
68
|
entries,
|
|
@@ -58,14 +74,20 @@ module.exports = async (entries, dest, options = { shouldOptimize: false }, log
|
|
|
58
74
|
distDir: dest
|
|
59
75
|
}
|
|
60
76
|
},
|
|
61
|
-
defaultTargetOptions: {
|
|
62
|
-
distDir: dest,
|
|
63
|
-
shouldOptimize: options.shouldOptimize
|
|
64
|
-
},
|
|
65
77
|
shouldPatchConsole: false,
|
|
66
78
|
shouldContentHash: true,
|
|
67
79
|
logLevel: 'error',
|
|
68
|
-
...
|
|
80
|
+
...parcelOptions,
|
|
81
|
+
mode,
|
|
82
|
+
env: {
|
|
83
|
+
...env,
|
|
84
|
+
NODE_ENV: nodeEnv
|
|
85
|
+
},
|
|
86
|
+
defaultTargetOptions: {
|
|
87
|
+
distDir: dest,
|
|
88
|
+
...defaultTargetOptions,
|
|
89
|
+
shouldOptimize: defaultTargetOptions.shouldOptimize ?? shouldOptimize ?? mode === 'production'
|
|
90
|
+
}
|
|
69
91
|
}
|
|
70
92
|
|
|
71
93
|
aioLogger.debug(`bundle bundleOptions: ${JSON.stringify(parcelBundleOptions, null, 2)}`)
|
package/src/constants.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 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 DEVELOPMENT_ENV = 'development'
|
|
14
|
+
const PRODUCTION_ENV = 'production'
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
DEVELOPMENT_ENV,
|
|
18
|
+
PRODUCTION_ENV
|
|
19
|
+
}
|
package/src/deploy-web.js
CHANGED
|
@@ -11,7 +11,6 @@ governing permissions and limitations under the License.
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
const RemoteStorage = require('../lib/remote-storage')
|
|
14
|
-
const getS3Credentials = require('../lib/getS3Creds')
|
|
15
14
|
|
|
16
15
|
const fs = require('fs-extra')
|
|
17
16
|
const path = require('path')
|
|
@@ -21,6 +20,16 @@ const deployWeb = async (config, log) => {
|
|
|
21
20
|
throw new Error('cannot deploy web, app has no frontend or config is invalid')
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
// byo: user supplied AWS S3 credentials, files are uploaded directly to the bucket
|
|
24
|
+
const byoCreds = config.s3 && config.s3.creds
|
|
25
|
+
let bearerToken
|
|
26
|
+
if (!byoCreds) {
|
|
27
|
+
bearerToken = await config?.ow?.auth_handler?.getAuthHeader()
|
|
28
|
+
if (!bearerToken) {
|
|
29
|
+
throw new Error('cannot deploy web, Authorization is required')
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
24
33
|
/// build files
|
|
25
34
|
const dist = config.web.distProd
|
|
26
35
|
if (!fs.existsSync(dist) ||
|
|
@@ -30,19 +39,29 @@ const deployWeb = async (config, log) => {
|
|
|
30
39
|
throw new Error(`missing files in ${dist}, maybe you forgot to build your UI ?`)
|
|
31
40
|
}
|
|
32
41
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (
|
|
42
|
+
const remoteStorage = byoCreds ? new RemoteStorage(byoCreds) : new RemoteStorage()
|
|
43
|
+
// validate config.app.hostname and config.ow.namespace are not empty and are valid and do not contain invalid characters
|
|
44
|
+
if (!config.app.hostname || !config.ow.namespace) {
|
|
45
|
+
throw new Error('config.app.hostname and config.ow.namespace are required')
|
|
46
|
+
}
|
|
47
|
+
if (!config.app.hostname.match(/^[a-zA-Z0-9.-]+$/)) {
|
|
48
|
+
throw new Error('config.app.hostname is invalid')
|
|
49
|
+
}
|
|
50
|
+
if (!config.ow.namespace.match(/^[a-zA-Z0-9_-]+$/)) {
|
|
51
|
+
throw new Error('config.ow.namespace is invalid')
|
|
52
|
+
}
|
|
53
|
+
// byo uses the s3 folder as prefix in the bucket, deploy-service uses the namespace
|
|
54
|
+
const folderPrefix = byoCreds ? config.s3.folder + '/' : '/'
|
|
55
|
+
if (await remoteStorage.folderExists(bearerToken, folderPrefix, config)) {
|
|
39
56
|
if (log) {
|
|
40
57
|
log('warning: an existing deployment will be overwritten')
|
|
41
58
|
}
|
|
42
|
-
await remoteStorage.emptyFolder(
|
|
59
|
+
await remoteStorage.emptyFolder(bearerToken, folderPrefix, config)
|
|
43
60
|
}
|
|
44
61
|
const _log = log ? (f) => log(`deploying ${path.relative(dist, f)}`) : null
|
|
45
|
-
|
|
62
|
+
// deploy-service prepends `${namespace}/` to file.name, so do not send a folder prefix
|
|
63
|
+
// (config.s3.folder is the namespace and would duplicate it: <ns>/<ns>/...)
|
|
64
|
+
await remoteStorage.uploadDir(bearerToken, dist, byoCreds ? config.s3.folder : '', config, _log)
|
|
46
65
|
|
|
47
66
|
const url = `https://${config.ow.namespace}.${config.app.hostname}/index.html`
|
|
48
67
|
return url
|