Skip to content

Commit

Permalink
Merge branch 'k1LoW:master' into allow-disabled-syncs
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualVirtuoso authored Jan 27, 2024
2 parents 30e9562 + 11c7c56 commit 34a623b
Show file tree
Hide file tree
Showing 3 changed files with 4,840 additions and 135 deletions.
38 changes: 35 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ const child_process = require('child_process');

const toS3Path = (osPath) => osPath.replace(new RegExp(`\\${path.sep}`, 'g'), '/');

/*
From @auth0/s3/lib/index.js - used when uploading the file in the first place
- added the + character to the set that are escaped.
Using is is needed to update the meta data of keys that contain spaces, +, etc...
to avoid a Key not found exception.
*/
function encodeSpecialCharacters(filename) {
// Note: these characters are valid in URIs, but S3 does not like them for
// some reason.
return encodeURI(filename).replace(/[+!'()* ]/g, function (char) {
return '%' + char.charCodeAt(0).toString(16);
});
}

class ServerlessS3Sync {
constructor(serverless, options, logging) {
this.serverless = serverless;
Expand Down Expand Up @@ -61,6 +75,24 @@ class ServerlessS3Sync {
usage: 'Disable sync to S3 during remove'
}
}
},
offline: {
options: {
nos3sync: {
type: 'boolean',
usage: 'Disable sync to S3 for serverless offline'
}
},
commands: {
start: {
options: {
nos3sync: {
type: 'boolean',
usage: 'Disable sync to S3 for serverless offline start command'
}
}
}
}
}
};

Expand Down Expand Up @@ -345,7 +377,7 @@ class ServerlessS3Sync {
}
const localDir = path.join(servicePath, s.localDir);
let filesToSync = [];
let ignoreFiles = [];
let ignoreFiles = ['.DS_Store'];
if(Array.isArray(s.params)) {
s.params.forEach((param) => {
const glob = Object.keys(param)[0];
Expand Down Expand Up @@ -390,8 +422,8 @@ class ServerlessS3Sync {
...contentTypeObject,
...file.params,
...{
CopySource: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, bucketDir)),
Key: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, `${bucketPrefix ? bucketPrefix.replace(/^\//, '') + '/' : ''}`)),
CopySource: encodeSpecialCharacters(toS3Path(file.name.replace(path.resolve(localDir) + path.sep, bucketDir))),
Key: encodeSpecialCharacters(toS3Path(file.name.replace(path.resolve(localDir) + path.sep, `${bucketPrefix ? bucketPrefix.replace(/^\//, '') + '/' : ''}`))),
Bucket: bucketName,
ACL: acl,
MetadataDirective: 'REPLACE'
Expand Down
Loading

0 comments on commit 34a623b

Please sign in to comment.