diff --git a/README.md b/README.md index 1dd5513..6d24832 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/package-lock.json b/package-lock.json index 29092b8..e0c71f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "serverless-localstack", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "serverless-localstack", - "version": "1.2.0", + "version": "1.2.1", "license": "MIT", "dependencies": { "adm-zip": "^0.5.10", diff --git a/package.json b/package.json index fc4d5a8..a6ab713 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index 07efa9f..fec99f7 100644 --- a/src/index.js +++ b/src/index.js @@ -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( @@ -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(