|
1 | 1 | 'use strict'; |
| 2 | +constAdmZip=require("adm-zip"); |
2 | 3 | constAWS=require('aws-sdk'); |
3 | 4 | constfs=require('fs'); |
4 | 5 | constpath=require('path'); |
@@ -49,6 +50,11 @@ class LocalstackPlugin { |
49 | 50 | // Add a hook to fix TypeError when accessing undefined state attribute |
50 | 51 | this.addHookInFirstPosition('before:aws:deploy:deploy:checkForChanges',this.beforeDeployCheckForChanges); |
51 | 52 |
|
| 53 | +constcompileEventsHooks=this.serverless.pluginManager.hooks['package:compileEvents']||[]; |
| 54 | +compileEventsHooks.push({ |
| 55 | +pluginName:'LocalstackPlugin',hook:this.patchCustomResourceLambdaS3ForcePathStyle.bind(this) |
| 56 | +}); |
| 57 | + |
52 | 58 | this.awsServices=[ |
53 | 59 | 'acm', |
54 | 60 | 'amplify', |
@@ -731,7 +737,63 @@ class LocalstackPlugin { |
731 | 737 | plugin.display=newDisplay; |
732 | 738 | } |
733 | 739 | } |
| 740 | +patchCustomResourceLambdaS3ForcePathStyle(){ |
| 741 | +constawsProvider=this.awsProvider; |
| 742 | +constpatchMarker=path.join( |
| 743 | +awsProvider.serverless.serviceDir, |
| 744 | +'.serverless', |
| 745 | +'.internal-custom-resources-patched' |
| 746 | +); |
| 747 | +constzipFilePath=path.join( |
| 748 | +awsProvider.serverless.serviceDir, |
| 749 | +'.serverless', |
| 750 | +awsProvider.naming.getCustomResourcesArtifactName() |
| 751 | +); |
| 752 | + |
| 753 | +functionfileExists(filePath){ |
| 754 | +try{ |
| 755 | +conststats=fs.statSync(filePath); |
| 756 | +returnstats.isFile(); |
| 757 | +}catch(e){ |
| 758 | +returnfalse; |
| 759 | +} |
| 760 | +} |
| 761 | + |
| 762 | +functioncreatePatchMarker(){ |
| 763 | +try{ |
| 764 | +fs.open(patchMarker,'a').close() |
| 765 | +}catch(err){ |
| 766 | +return; |
| 767 | +} |
| 768 | +} |
| 769 | + |
| 770 | +if(fileExists(patchMarker)){ |
| 771 | +this.debug("serverless-localstack: Serverless internal CustomResources already patched") |
| 772 | +return; |
| 773 | +} |
| 774 | + |
| 775 | +constcustomResourceZipExists=fileExists(zipFilePath) |
734 | 776 |
|
| 777 | +if(!customResourceZipExists){ |
| 778 | +return; |
| 779 | +} |
| 780 | + |
| 781 | +constcustomResources=newAdmZip(zipFilePath); |
| 782 | +constutilFile=customResources.getEntry('utils.js') |
| 783 | +if(utilFile==null)return; |
| 784 | +constdata=utilFile.getData().toString() |
| 785 | +constpatch="AWS.config.s3ForcePathStyle = true;" |
| 786 | +if(data.includes(patch)){ |
| 787 | +createPatchMarker() |
| 788 | +return; |
| 789 | +} |
| 790 | +constindexPatch=data.indexOf('AWS.config.logger = console;') |
| 791 | +constnewData=data.slice(0,indexPatch)+patch+'\n'+data.slice(indexPatch) |
| 792 | +utilFile.setData(newData) |
| 793 | +customResources.writeZip() |
| 794 | +createPatchMarker() |
| 795 | +this.debug('serverless-localstack: Serverless internal CustomResources patched') |
| 796 | +} |
735 | 797 | } |
736 | 798 |
|
737 | 799 | module.exports=LocalstackPlugin; |