|
1 | 1 | 'use strict';
|
| 2 | +const AdmZip = require("adm-zip"); |
2 | 3 | const AWS = require('aws-sdk');
|
3 | 4 | const fs = require('fs');
|
4 | 5 | const path = 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 | + const compileEventsHooks = 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 | + const awsProvider = this.awsProvider; |
| 742 | + const patchMarker = path.join( |
| 743 | + awsProvider.serverless.serviceDir, |
| 744 | + '.serverless', |
| 745 | + '.internal-custom-resources-patched' |
| 746 | + ); |
| 747 | + const zipFilePath = path.join( |
| 748 | + awsProvider.serverless.serviceDir, |
| 749 | + '.serverless', |
| 750 | + awsProvider.naming.getCustomResourcesArtifactName() |
| 751 | + ); |
| 752 | + |
| 753 | + function fileExists (filePath) { |
| 754 | + try { |
| 755 | + const stats = fs.statSync(filePath); |
| 756 | + return stats.isFile(); |
| 757 | + } catch (e) { |
| 758 | + return false; |
| 759 | + } |
| 760 | + } |
| 761 | + |
| 762 | + function createPatchMarker () { |
| 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 | + const customResourceZipExists = fileExists(zipFilePath) |
734 | 776 |
|
| 777 | + if (!customResourceZipExists) { |
| 778 | + return; |
| 779 | + } |
| 780 | + |
| 781 | + const customResources = new AdmZip(zipFilePath); |
| 782 | + const utilFile = customResources.getEntry('utils.js') |
| 783 | + if (utilFile == null) return; |
| 784 | + const data = utilFile.getData().toString() |
| 785 | + const patch = "AWS.config.s3ForcePathStyle = true;" |
| 786 | + if (data.includes(patch)) { |
| 787 | + createPatchMarker() |
| 788 | + return; |
| 789 | + } |
| 790 | + const indexPatch = data.indexOf('AWS.config.logger = console;') |
| 791 | + const newData = 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;
|
0 commit comments