Skip to content

Commit 811f520

Browse files
authored
Add new custom-resource patching mechanism for serverless>3.39.0 (#254)
1 parent 9436003 commit 811f520

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ custom:
208208
```
209209
210210
## Change Log
211+
* v1.2.1: Fix custom-resource bucket compatibility with serverless >3.39.0, continue improving support for `AWS_ENDPOINT_URL`
211212
* v1.2.0: Add docker-compose config and fix autostart when plugin is not active
212213
* v1.1.3: Fix replacing host from environment variable `AWS_ENDPOINT_URL`
213214
* v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL`

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-localstack",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Connect Serverless to LocalStack!",
55
"main": "src/index.js",
66
"scripts": {

src/index.js

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,50 @@ class LocalstackPlugin {
985985
return;
986986
}
987987
}
988+
989+
function patchPreV3() {
990+
const utilFile = customResources.getEntry('utils.js');
991+
if (utilFile == null) return;
992+
const data = utilFile.getData().toString();
993+
const legacyPatch = 'AWS.config.s3ForcePathStyle = true;';
994+
if (data.includes(legacyPatch)) {
995+
createPatchMarker();
996+
return true;
997+
}
998+
const patchIndex = data.indexOf('AWS.config.logger = console;');
999+
if (patchIndex === -1) {
1000+
return false;
1001+
}
1002+
const newData =
1003+
data.slice(0, patchIndex) + legacyPatch + '\n' + data.slice(patchIndex);
1004+
utilFile.setData(newData);
1005+
return true;
1006+
}
1007+
1008+
function patchV3() {
1009+
this.debug(
1010+
'serverless-localstack: Patching V3',
1011+
);
1012+
const customResourcesBucketFile = customResources.getEntry('s3/lib/bucket.js');
1013+
if (customResourcesBucketFile == null) {
1014+
// TODO debugging, remove
1015+
this.log(
1016+
'serverless-localstack: Could not find file s3/lib/bucket.js to patch.',
1017+
);
1018+
return;
1019+
}
1020+
const data = customResourcesBucketFile.getData().toString();
1021+
const oldClientCreation = 'S3Client({ maxAttempts: MAX_AWS_REQUEST_TRY });';
1022+
const newClientCreation = 'S3Client({ maxAttempts: MAX_AWS_REQUEST_TRY, forcePathStyle: true });';
1023+
if (data.includes(newClientCreation)) {
1024+
// patch already done
1025+
createPatchMarker();
1026+
return;
1027+
}
1028+
const newData = data.replace(oldClientCreation, newClientCreation);
1029+
1030+
customResourcesBucketFile.setData(newData);
1031+
}
9881032

9891033
if (fileExists(patchMarker)) {
9901034
this.debug(
@@ -1000,18 +1044,10 @@ class LocalstackPlugin {
10001044
}
10011045

10021046
const customResources = new AdmZip(zipFilePath);
1003-
const utilFile = customResources.getEntry('utils.js');
1004-
if (utilFile == null) return;
1005-
const data = utilFile.getData().toString();
1006-
const patch = 'AWS.config.s3ForcePathStyle = true;';
1007-
if (data.includes(patch)) {
1008-
createPatchMarker();
1009-
return;
1047+
1048+
if (!patchPreV3.call(this)) {
1049+
patchV3.call(this);
10101050
}
1011-
const indexPatch = data.indexOf('AWS.config.logger = console;');
1012-
const newData =
1013-
data.slice(0, indexPatch) + patch + '\n' + data.slice(indexPatch);
1014-
utilFile.setData(newData);
10151051
customResources.writeZip();
10161052
createPatchMarker();
10171053
this.debug(

0 commit comments

Comments
 (0)