Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new custom-resource patching mechanism for serverless>3.39.0 #254

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ custom:
```

## Change Log
* v1.2.1: Fix custom-resource bucket compatibility with serverless >3.39.0, continue improving support for `AWS_ENDPOINT_URL`
* v1.2.0: Add docker-compose config and fix autostart when plugin is not active
* v1.1.3: Fix replacing host from environment variable `AWS_ENDPOINT_URL`
* v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL`
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-localstack",
"version": "1.2.0",
"version": "1.2.1",
"description": "Connect Serverless to LocalStack!",
"main": "src/index.js",
"scripts": {
Expand Down
58 changes: 47 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,50 @@ class LocalstackPlugin {
return;
}
}

function patchPreV3() {
const utilFile = customResources.getEntry('utils.js');
if (utilFile == null) return;
const data = utilFile.getData().toString();
const legacyPatch = 'AWS.config.s3ForcePathStyle = true;';
if (data.includes(legacyPatch)) {
createPatchMarker();
return true;
}
const patchIndex = data.indexOf('AWS.config.logger = console;');
if (patchIndex === -1) {
return false;
}
const newData =
data.slice(0, patchIndex) + legacyPatch + '\n' + data.slice(patchIndex);
utilFile.setData(newData);
return true;
}

function patchV3() {
this.debug(
'serverless-localstack: Patching V3',
);
const customResourcesBucketFile = customResources.getEntry('s3/lib/bucket.js');
if (customResourcesBucketFile == null) {
// TODO debugging, remove
this.log(
'serverless-localstack: Could not find file s3/lib/bucket.js to patch.',
);
return;
}
const data = customResourcesBucketFile.getData().toString();
const oldClientCreation = 'S3Client({ maxAttempts: MAX_AWS_REQUEST_TRY });';
const newClientCreation = 'S3Client({ maxAttempts: MAX_AWS_REQUEST_TRY, forcePathStyle: true });';
if (data.includes(newClientCreation)) {
// patch already done
createPatchMarker();
return;
}
const newData = data.replace(oldClientCreation, newClientCreation);

customResourcesBucketFile.setData(newData);
}

if (fileExists(patchMarker)) {
this.debug(
Expand All @@ -1000,18 +1044,10 @@ class LocalstackPlugin {
}

const customResources = new AdmZip(zipFilePath);
const utilFile = customResources.getEntry('utils.js');
if (utilFile == null) return;
const data = utilFile.getData().toString();
const patch = 'AWS.config.s3ForcePathStyle = true;';
if (data.includes(patch)) {
createPatchMarker();
return;

if (!patchPreV3.call(this)) {
patchV3.call(this);
}
const indexPatch = data.indexOf('AWS.config.logger = console;');
const newData =
data.slice(0, indexPatch) + patch + '\n' + data.slice(indexPatch);
utilFile.setData(newData);
customResources.writeZip();
createPatchMarker();
this.debug(
Expand Down
Loading