Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
27e548c
7.1.1
purplecabbage Dec 8, 2025
e518504
beware the environment, no more S3 lib
purplecabbage Jan 17, 2026
04d92bc
no more S3 lib credentials needed
purplecabbage Jan 17, 2026
3f12bb8
no more s3
purplecabbage Jan 17, 2026
0ab2862
Revert "7.1.1"
purplecabbage Jan 20, 2026
ff79d76
refactor/added authToken to constructor for repeated use
purplecabbage Jan 20, 2026
425228c
deploy/undeploy files with deploy-service + tests
purplecabbage Jan 20, 2026
6ccfa8e
remove problematic #class-prop syntax
purplecabbage Jan 20, 2026
d371fb0
tests passing, env mocking
purplecabbage Jan 20, 2026
5a18204
clean up
purplecabbage Jan 23, 2026
1dd66b2
nit: remove unused param
purplecabbage Feb 6, 2026
14bb3c3
Merge branch 'master' into DeployService
purplecabbage Feb 6, 2026
2292765
fix: resolve merge issues, restore test coverage
purplecabbage Feb 6, 2026
906bb1b
mock+restore global.fetch
purplecabbage Feb 6, 2026
8885cb0
Merge branch 'master' into DeployService
purplecabbage May 19, 2026
bd946d2
error checking and tests
purplecabbage May 19, 2026
e738fe5
restore proxy support
purplecabbage May 20, 2026
466d529
restore overwrite warn log, throw specific errors
purplecabbage May 20, 2026
40a84a0
remove unused tvm lib, add back core errors which was a dep
purplecabbage May 20, 2026
bfd283e
comments, cleanup
purplecabbage May 20, 2026
83dcdab
added comment on sleep() removal
purplecabbage May 28, 2026
92d3acd
output config.ow.namespace, config.s3.folder could fail
purplecabbage May 28, 2026
80a282c
remove unused joi
purplecabbage May 28, 2026
a662565
Replaced the module-level deploymentServiceUrl block with getDeployme…
purplecabbage May 28, 2026
f01871f
rewire so RemoteStorage need not store authToken, validation is calle…
purplecabbage May 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions lib/getS3Creds.js

This file was deleted.

270 changes: 141 additions & 129 deletions lib/remote-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,162 +10,162 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { S3 } = require('@aws-sdk/client-s3')
const path = require('path')
const mime = require('mime-types')
const fs = require('fs-extra')
const joi = require('joi')
const klaw = require('klaw')
const http = require('http')
// Proxy support for AWS SDK v3 (inspired by PR #224 by pat-lego, with compatibility fixes)
const { NodeHttpHandler } = require('@smithy/node-http-handler')
const { ProxyAgent } = require('proxy-agent')
const { EnvHttpProxyAgent } = require('undici')
const { codes, logAndThrow } = require('./StorageError')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] Commented-out code and stale comments ('// dev deploy => ...', '// run local => ...') should be removed before merging.

Suggested change
const { codes, logAndThrow } = require('./StorageError')
const deploymentServiceUrl = process.env.AIO_DEPLOYMENT_SERVICE_URL ||
(getCliEnv() === PROD_ENV
? 'https://deploy-service.app-builder.adp.adobe.io'
: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io')

const aioLibEnv = require('@adobe/aio-lib-env')

Comment thread
purplecabbage marked this conversation as resolved.
Comment thread
purplecabbage marked this conversation as resolved.
const fileExtensionPattern = /\*\.[0-9a-zA-Z]+$/

module.exports = class RemoteStorage {
/**
* @param {object} creds
* @param {string} creds.accessKeyId
* @param {string} creds.secretAccessKey
* @param {string} creds.params.Bucket
* @param {string} [creds.sessionToken]
*/
constructor (creds) {
const res = joi.object().keys({
sessionToken: joi.string(),
accessKeyId: joi.string().required(),
secretAccessKey: joi.string().required(),
// hacky needs s3Bucket in creds.params.Bucket
params: joi.object().keys({ Bucket: joi.string().required() }).required()
}).unknown()
.validate(creds)
if (res.error) {
throw res.error
}

// the TVM response could be passed as is to the v2 client constructor, but the v3 client follows a different format
// see https://github.com/adobe/aio-tvm/issues/85
const region = creds.region || 'us-east-1'
// note this must supports TVM + BYO use cases
// see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/credentials.html
const credentials = {
accessKeyId: creds.accessKeyId,
secretAccessKey: creds.secretAccessKey,
sessionToken: creds.sessionToken,
expiration: creds.expiration ? new Date(creds.expiration) : undefined
}
this.bucket = creds.params.Bucket
// Set AIO_DEPLOYMENT_SERVICE_URL env var to override the default deploy-service url
// dev deploy => https://deploy-service.dev.app-builder.adp.adobe.io
// run local => http://localhost:3000
const getDeploymentServiceUrl = () => {
if (process.env.AIO_DEPLOYMENT_SERVICE_URL) {
return process.env.AIO_DEPLOYMENT_SERVICE_URL
}
return aioLibEnv.getCliEnv() === aioLibEnv.PROD_ENV
? 'https://deploy-service.app-builder.adp.adobe.io'
: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io'
}

// Configure proxy support for AWS SDK v3
// ProxyAgent automatically handles proxy environment variables via proxy-from-env
const agent = new ProxyAgent()
const s3Config = {
credentials,
region,
requestHandler: new NodeHttpHandler({
httpAgent: agent,
httpsAgent: agent
})
}
const fileExtensionPattern = /\*\.[0-9a-zA-Z]+$/

// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3.html#constructor
this.s3 = new S3(s3Config)
let fetchDispatcher
const getFetchDispatcher = () => {
if (!fetchDispatcher) {
fetchDispatcher = new EnvHttpProxyAgent()
}
return fetchDispatcher
}

const deployServiceFetch = (url, init) => {
Comment thread
purplecabbage marked this conversation as resolved.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] The deployServiceFetch function accesses the global fetch. If fetch is not available in the Node.js version being used, this will fail silently at runtime. Consider adding a guard or using undici's fetch directly.

Suggested change
const deployServiceFetch = (url, init) => {
const deployServiceFetch = (url, init) => {
const fetchFn = typeof fetch !== 'undefined' ? fetch : require('undici').fetch
return fetchFn(url, { ...init, dispatcher: getFetchDispatcher() })
}

return fetch(url, { ...init, dispatcher: getFetchDispatcher() })
}

module.exports = class RemoteStorage {
/**
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc comment for the constructor parameter should note that the token is stored in memory. For security, consider not storing the token as an instance variable directly if it could be logged or serialized, though this is a minor concern for a server-side Node.js module.

Suggested change
/**
/**
* Constructor for RemoteStorage
* @param {string} authToken - The authorization token (e.g. 'Bearer <token>') to use for the remote storage
* @private This token is stored in memory only and not serialized
*/

* Checks if prefix exists
* @param {string} prefix
* @returns {boolean}
* Checks if any files exist for the namespace
* @param {string} authToken - Authorization header value
* @param {string} prefix - unused, kept for API compatibility
* @param {Object} appConfig - application config
* @returns {Promise<boolean>} true if files exist, false otherwise
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fetchDispatcher is a module-level singleton. This means it persists across tests and potentially across different runtime configurations if the module is cached. While lazy initialization is fine, consider documenting that fetchDispatcher is intentionally a singleton and cannot be reset without clearing the module cache.

Suggested change
*/
// Module-level singleton: intentionally shared across all requests for connection reuse.
// To reset (e.g., in tests), clear the require cache for this module.
let fetchDispatcher

async folderExists (prefix) {
async folderExists (authToken, prefix, appConfig) {
if (typeof prefix !== 'string') {
throw new Error('prefix must be a valid string')
}
const listParams = {
Bucket: this.bucket,
Prefix: prefix
// Call the list files endpoint (GET /files) - there is no GET /files/:key route
const response = await deployServiceFetch(`${getDeploymentServiceUrl()}/cdn-api/namespaces/${appConfig.ow.namespace}/files`, {
method: 'GET',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] The deployServiceFetch function accesses the global fetch. If fetch is not available in the Node.js version being used, this will fail silently at runtime. Consider adding a guard or using undici's fetch directly.

Suggested change
method: 'GET',
const deployServiceFetch = (url, init) => {
const fetchFn = typeof fetch !== 'undefined' ? fetch : require('undici').fetch
return fetchFn(url, { ...init, dispatcher: getFetchDispatcher() })
}

headers: {
Authorization: authToken
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] emptyFolder returns response.ok (boolean false) on HTTP failure, but throws on auth failure. The behavior is inconsistent. Consider throwing on HTTP failure too for uniform error handling, since the caller in undeploy-web.js explicitly checks the return value and throws anyway.

Suggested change
}
if (!response.ok) {
throw new Error(`cannot empty folder, request failed: ${response.status} ${response.statusText}`)
}
return true

})
if (!response.ok) {
throw new Error(`cannot check if folder exists, request failed: ${response.status} ${response.statusText}`)
}
const listedObjects = await this.s3.listObjectsV2(listParams)

return listedObjects.KeyCount > 0
const files = await response.json()
// Check if there are any files (folder "exists" if it has content)
return Array.isArray(files) && files.length > 0
}

/**
* Deletes all files in a prefix location
* @param {string} prefix
* Empties all files for the namespace or deletes a specific file
* @param {string} authToken - Authorization header value
* @param {string} prefix - '/' to delete all files, or a specific file path
* @param {Object} appConfig - application config
* @returns {Promise<boolean>} true if the folder was emptied, false otherwise
*/
async emptyFolder (prefix) {
if (typeof prefix !== 'string') throw new Error('prefix must be a valid string')
const listParams = {
Bucket: this.bucket,
Prefix: prefix
async emptyFolder (authToken, prefix, appConfig) {
if (typeof prefix !== 'string') {
throw new Error('prefix must be a valid string')
}
const listedObjects = await this.s3.listObjectsV2(listParams)
// Server route is DELETE /files/:key
// When key='/' the server triggers emptyStorageForNamespace
// URL construction: /files/ (trailing slash makes :key = '/')
const deploymentServiceUrl = getDeploymentServiceUrl()
const url = prefix === '/'
? `${deploymentServiceUrl}/cdn-api/namespaces/${appConfig.ow.namespace}/files/`
: `${deploymentServiceUrl}/cdn-api/namespaces/${appConfig.ow.namespace}/files/${prefix}`

if (listedObjects.KeyCount < 1) {
return
}
const deleteParams = {
Bucket: this.bucket,
Delete: { Objects: [] }
}
listedObjects.Contents.forEach(({ Key }) => {
deleteParams.Delete.Objects.push({ Key })
const response = await deployServiceFetch(url, {
method: 'DELETE',
headers: {
Authorization: authToken
}
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emptyFolder method returns response.ok (a boolean) but the caller in undeploy-web.js checks if (!deleted) and throws. This is fine, but the method's JSDoc says it returns Promise<boolean> - however when auth check throws, it doesn't return false, it throws. The behavior is inconsistent: auth failure throws, but HTTP failure returns false. Consider making these consistent by throwing on HTTP failure too, or documenting the distinction.

Suggested change
})
if (!response.ok) {
throw new Error(`cannot empty folder, request failed: ${response.status} ${response.statusText}`)
}
return true

await this.s3.deleteObjects(deleteParams)
if (listedObjects.IsTruncated) {
await this.emptyFolder(prefix)
}
return response.ok
}

/**
* Uploads a file
* @param {string} file
* @param {string} prefix - prefix to upload the file to
* Uploads a file to the CDN API
* @param {string} authToken - Authorization header value
* @param {string} file - Full local file path
* @param {string} filePath - Path relative to namespace (e.g., 'images/photo.jpg' or 'index.html')
* This becomes file.name in the API request. The server will prepend the namespace.
* @param {Object} appConfig - application config
* @param {string} distRoot - Distribution root dir
* @param {string} distRoot - Distribution root dir (used for header matching)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] emptyFolder returns response.ok (boolean false) on HTTP failure, but throws on other errors. The behavior is inconsistent. Consider throwing on HTTP failure too for uniform error handling, since the caller in undeploy-web.js explicitly checks the return value and throws anyway.

Suggested change
* @param {string} distRoot - Distribution root dir (used for header matching)
if (!response.ok) {
throw new Error(`cannot empty folder, request failed: ${response.status} ${response.statusText}`)
}
return true

*/
async uploadFile (file, prefix, appConfig, distRoot) {
if (typeof prefix !== 'string') {
throw new Error('prefix must be a valid string')
async uploadFile (authToken, file, filePath, appConfig, distRoot) {
if (typeof filePath !== 'string') {
throw new Error('filePath must be a valid string')
}
const url = `${getDeploymentServiceUrl()}/cdn-api/namespaces/${appConfig.ow.namespace}/files`

const content = await fs.readFile(file)
const mimeType = mime.lookup(path.extname(file))
Comment thread
purplecabbage marked this conversation as resolved.
// first we will grab it from the global config: htmlCacheDuration, etc.
const cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
const uploadParams = {
Bucket: this.bucket,
Key: this._urlJoin(prefix, path.basename(file)),
Body: content
}
// if we found it in the global config, we will use it ( for now )
if (cacheControlString) {
uploadParams.CacheControl = cacheControlString
}
let cacheControlString = this._getCacheControlConfig(mimeType, appConfig.app)
// add response headers if specified in manifest
const responseHeaders = this.getResponseHeadersForFile(file, distRoot, appConfig) ?? {}
// here we allow overriding the cache control if specified in response headers
// this is considered more specific than the general cache control config
// ideally we deprecate cache control config in favor of response headers directly
if (responseHeaders?.['adp-cache-control']) {
uploadParams.CacheControl = responseHeaders['adp-cache-control']
cacheControlString = responseHeaders['adp-cache-control']
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File content is read with fs.readFile which returns a Buffer, then Buffer.from(content).toString('base64') is called. Since content is already a Buffer, Buffer.from(content) creates an unnecessary copy. Use content.toString('base64') directly.

Suggested change
cacheControlString = responseHeaders['adp-cache-control']
content: content.toString('base64')

delete responseHeaders['adp-cache-control']
}

// we only set metadata if we have added anything to responseHeaders object
// it is not null, but could be empty
if (Object.keys(responseHeaders).length > 0) {
uploadParams.Metadata = responseHeaders
// server expected body is: { contentType, cacheControl, customHeaders: {}, file: { name, content } }
// file.name is the path relative to namespace (e.g., 'images/photo.jpg' or 'index.html')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] File content is sent as base64-encoded string in a JSON body. For large static assets this doubles memory usage (Buffer + base64 string) and increases payload size by ~33%. Consider using multipart/form-data or a streaming upload approach for files above a certain size threshold.

// The server will prepend the namespace to create the S3 key: ${namespace}/${file.name}
const fileName = path.basename(file)
let relativeFilePath = filePath
if (relativeFilePath.startsWith('/')) {
relativeFilePath = relativeFilePath.substring(1)
}
// s3 misses some mime types like for css files
if (mimeType) {
uploadParams.ContentType = mimeType
// Use _urlJoin to normalize backslashes and construct the file path
const filePathForServer = this._urlJoin(relativeFilePath, fileName)
Comment thread
purplecabbage marked this conversation as resolved.
// NOTE: File content is base64-encoded in the JSON body
// which increases payload size by ~33%. Look at getting server to handle
// multipart uploads to avoid this.
const data = {
file: {
contentType: mimeType,
cacheControl: cacheControlString,
customHeaders: responseHeaders,
name: filePathForServer,
content: Buffer.from(content).toString('base64')
}
}
const response = await deployServiceFetch(url, {
method: 'PUT',
body: JSON.stringify(data),
headers: {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] Buffer.from(content) creates an unnecessary copy since content is already a Buffer returned by fs.readFile. Use content.toString('base64') directly.

Suggested change
headers: {
content: content.toString('base64')

'Content-Type': 'application/json',
Authorization: authToken
}
}).catch(error => {
console.error('Error uploading file:', file)
throw error
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] Buffer.from(content) creates an unnecessary copy since content is already a Buffer returned by fs.readFile. Use content.toString('base64') directly.

Suggested change
})
content: content.toString('base64')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] uploadFile silently passes false as contentType to the server when the MIME type is unknown (mime-types returns false for unknown extensions). The server may not handle false gracefully. Consider using a fallback like 'application/octet-stream'.

Suggested change
})
contentType: mimeType || 'application/octet-stream',

if (!response.ok) {
console.error('Failed to upload file:', file)
throw new Error(`Failed to upload file: ${response.statusText}`)
}
// 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
// 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
return this.s3.putObject(uploadParams)
return response.status
}

getResponseHeadersForFile (file, distRoot, appConfig) {
Comment thread
purplecabbage marked this conversation as resolved.
Expand Down Expand Up @@ -250,15 +250,17 @@ module.exports = class RemoteStorage {
}

/**
* Uploads all files in a dir to - recursion is supported
* @param {string} dir - directory with files to upload
* @param {string} prefix - prefix to upload the dir to
* Uploads all files in a directory recursively to the CDN API
* @param {string} authToken - Authorization header value
* @param {string} dir - Local directory with files to upload
* @param {string} basePath - Base path prefix for all files (e.g., from config.s3.folder)
* This is combined with each file's relative directory path.
* @param {Object} appConfig - application config
* @param {function} [postFileUploadCallback] - called for each uploaded file
*/
async uploadDir (dir, prefix, appConfig, postFileUploadCallback) {
if (typeof prefix !== 'string') {
throw new Error('prefix must be a valid string')
async uploadDir (authToken, dir, basePath, appConfig, postFileUploadCallback) {
if (typeof basePath !== 'string') {
throw new Error('basePath must be a valid string')
}
// walk the whole directory recursively using klaw.
const files = await this.walkDir(dir)
Expand All @@ -271,18 +273,28 @@ module.exports = class RemoteStorage {
let fileBatch = files.splice(0, batchSize)
const allResults = []
while (fileBatch.length > 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] Commented-out sleep/rate-limiting code should be removed. If rate limiting is needed, implement it properly or open a tracked issue.

Suggested change
while (fileBatch.length > 0) {
const res = await Promise.all(fileBatch.map(async file => {

const res = await Promise.all(fileBatch.map(async f => {
// get file's relative folder to the base directory.
let prefixDirectory = path.dirname(path.relative(dir, f))
// base directory returns ".", ignore that.
prefixDirectory = prefixDirectory === '.' ? '' : prefixDirectory
// newPrefix is now the initial prefix plus the files relative directory path.
const newPrefix = this._urlJoin(prefix, prefixDirectory)
const s3Res = await this.uploadFile(f, newPrefix, appConfig, dir)
// this used to sleep, but I don't think we need it here.
// I am leaving it here in case we see issues with rate limiting.
// sleep for 100ms to prevent rate limiting
// await new Promise(resolve => setTimeout(resolve, 100))
const res = await Promise.all(fileBatch.map(async file => {
// Calculate the file's relative directory path from the base directory
// e.g., if dir='/dist' and file='/dist/images/photo.jpg', relativeDir='images'
let relativeDir = path.dirname(path.relative(dir, file))
// path.relative returns '.' for files in the root directory, normalize to empty string
relativeDir = relativeDir === '.' ? '' : relativeDir

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re-raised] Commented-out sleep/rate-limiting code should be removed or replaced with a real implementation if rate limiting is a concern.

Suggested change
const res = await Promise.all(fileBatch.map(async file => {

// Combine basePath with relativeDir to get the full file path relative to namespace
// e.g., basePath='' + relativeDir='images' = 'images'
Comment thread
purplecabbage marked this conversation as resolved.
// basePath='assets' + relativeDir='images' = 'assets/images'
const filePath = this._urlJoin(basePath, relativeDir)

// Upload file with the calculated filePath (server will prepend namespace)
const s3Result = await this.uploadFile(authToken, file, filePath, appConfig, dir)
if (postFileUploadCallback) {
postFileUploadCallback(f)
postFileUploadCallback(file)
}
return s3Res
return s3Result
}))
allResults.push(res)
fileBatch = files.splice(0, batchSize)
Expand Down
Loading
Loading