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AQ,IAAI,kBAAoB,UAC3C,QAAS,KAAK,UAAU,CACpB,YAAa,KAAK,WAAW,QAAQ,QACrC,gBAAiB,KAAK,WAAW,QAAQ,SAAS,MAClD,uBAAwB,KAAK,WAAW,QAAQ,SAAS,iBACzD,0BAA2B,EAC3B,aAAc,KAItB,OAAO,KAAK,SAAS,QAAQ,SAAU,SAAU,GAC5C,MAAK,IACF,IAAI,EAAU,EAAuB,QACjC,EAAqB,KAAK,MAAM,GAEpC,GAAI,EAAuB,cACvB,MAAM,IAAI,MAAM,EAAmB,cAGvC,OAAO,EAAmB,WAAW,GAEjD,CAMA,mBAAA,CAAoB,GAEhB,IAAI,EAAY,KAAK,WAAW,QAAQ,UAExC,MAAM,EADc,OAAO,KAAK,GACD,KAAI,IAC/B,MAEM,EAAS,CACX,aAHc,EAAU,GAGA,KACxB,SAAU,KAAK,WAAW,QAAQ,SAAS,iBAC3C,MAAO,GAGX,OAAO,KAAK,SAAS,QAAQ,SAAU,qBAAsB,EAAM,IAGvE,OAAO,UAAU,IAAI,EACzB,EAIJ,OAAO,QAAU","file":"/npm/serverless-plugin-fastdeploy@0.0.4/src/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/serverless-plugin-fastdeploy@0.0.4/src/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","\"use strict\";\n\nconst archiver = require(\"archiver\");\nconst BbPromise = require(\"bluebird\");\nconst fs = require(\"fs-extra\");\nconst path = require(\"path\");\n\n/**\n * @classdesc   Fast Serverless deployments for large packages\n * @class       ServerlessPluginFastDeploy\n */\nclass ServerlessPluginFastDeploy {\n\n    /**\n     * @constructor\n     *\n     * @param serverless\n     * @param options\n     */\n    constructor(serverless, options) {\n        this.serverless = serverless;\n        this.options = options;\n        this.provider = serverless.getProvider(\"aws\");\n        this.custom = this.serverless.service.custom;\n\n        this.commands = {\n            fastdeploy: {\n                usage: \"Perform Lightning Fast Deployment\",\n                lifecycleEvents: [\"fastdeploy\"]\n            }\n        };\n\n        this.hooks = {\n            \"fastdeploy:fastdeploy\": this.performFastDeploy.bind(this),\n            \"after:package:initialize\": this.afterPackageInitialize.bind(this),\n            \"after:package:createDeploymentArtifacts\": this.afterCreateDeploymentArtifacts.bind(this),\n            \"after:deploy:deploy\": this.afterDeployFunctions.bind(this)\n        };\n    }\n\n    performFastDeploy() {\n\n        this.configPlugin();\n\n        return this.createFastDeployUpdateArtifact()\n            .then(this.invokeFastDeployFunction.bind(this))\n            .then(this.updateFunctionsCode.bind(this));\n    }\n\n    /**\n     * @description After package initialize hook. Create FastDeploy function and add it to the service.\n     *\n     * @fulfil {} — Warm up set\n     * @reject {Error} Warm up error\n     *\n     * @return {(boolean|Promise)}\n     * */\n    afterPackageInitialize() {\n        // See https://github.com/serverless/serverless/issues/2631\n        this.options.stage = this.options.stage\n            || this.serverless.service.provider.stage\n            || (this.serverless.service.defaults && this.serverless.service.defaults.stage)\n            || \"dev\";\n        this.options.region = this.options.region\n            || this.serverless.service.provider.region\n            || (this.serverless.service.defaults && this.serverless.service.defaults.region)\n            || \"us-east-1\";\n\n        return this.createFastDeployFunctionArtifact()\n            .then(() => this.addFastDeployFunctionToService());\n    }\n\n    /**\n     * @description After create deployment artifacts. Clean prefix folder.\n     *\n     * @fulfil {} — Optimization finished\n     * @reject {Error} Optimization error\n     *\n     * @return {Promise}\n     * */\n    afterCreateDeploymentArtifacts() {\n        return this.cleanFolder()\n    }\n\n    /**\n     * @description After deploy functions hooks\n     *\n     * @fulfil {} — Functions warmed up sucessfuly\n     * @reject {Error} Functions couldn\"t be warmed up\n     *\n     * @return {Promise}\n     * */\n    afterDeployFunctions() {\n        this.configPlugin();\n    }\n\n    configPlugin() {\n        /** Set warm up folder, file and handler paths */\n        this.folderName = \"_fastdeploy\";\n        if (this.custom && this.custom.fastDeploy && typeof this.custom.fastDeploy.folderName === \"string\") {\n            this.folderName = this.custom.fastDeploy.folderName\n        }\n        this.pathFolder = path.join(this.serverless.config.servicePath, this.folderName);\n        this.pathFile = path.join(this.pathFolder, \"fast_deploy.py\");\n        this.pathHandler = `${this.folderName}/fast_deploy.handle`;\n\n        this.artifactFilePath = path.join(this.serverless.config.servicePath,\n            \".serverless\",\n            `${this.serverless.service.service}-FastDeployUpdate.zip`\n        );\n\n        /** Default options */\n        this.fastDeploy = {\n            cleanFolder: true,\n            memorySize: 512,\n            name: this.serverless.service.service + \"-\" + this.options.stage + \"-FastDeploy\",\n            timeout: 30,\n            includePatterns: []\n        };\n\n        /** Set global custom options */\n        if (!this.custom || !this.custom.fastDeploy) {\n            return\n        }\n\n        /** Clean folder */\n        if (typeof this.custom.fastDeploy.cleanFolder === \"boolean\") {\n            this.fastDeploy.cleanFolder = this.custom.fastDeploy.cleanFolder\n        }\n\n        /** Memory size */\n        if (typeof this.custom.fastDeploy.memorySize === \"number\") {\n            this.fastDeploy.memorySize = this.custom.fastDeploy.memorySize\n        }\n\n        /** Function name */\n        if (typeof this.custom.fastDeploy.name === \"string\") {\n            this.fastDeploy.name = this.custom.fastDeploy.name\n        }\n\n        /** Role */\n        if (typeof this.custom.fastDeploy.role === \"string\") {\n            this.fastDeploy.role = this.custom.fastDeploy.role\n        }\n\n        /** Tags */\n        if (typeof this.custom.fastDeploy.tags === \"object\") {\n            this.fastDeploy.tags = this.custom.fastDeploy.tags\n        }\n\n        /** Timeout */\n        if (typeof this.custom.fastDeploy.timeout === \"number\") {\n            this.fastDeploy.timeout = this.custom.fastDeploy.timeout\n        }\n\n        /** Include Pattern */\n        if (typeof this.custom.fastDeploy.include === \"object\") {\n            this.fastDeploy.include = this.custom.fastDeploy.include\n        }\n    }\n\n    cleanFolder() {\n        if (fs.existsSync(this.pathFolder)) {\n            return fs.remove(this.pathFolder)\n        }\n\n        return BbPromise.resolve();\n    }\n\n    /**\n     * Create Update Artifact\n     *\n     * @returns {*|{lifecycleEvents}|void}\n     */\n    createFastDeployUpdateArtifact() {\n        const output = fs.createWriteStream(this.artifactFilePath);\n        const zip = archiver.create(\"zip\");\n\n        zip.pipe(output);\n\n        const globalGlobOptions = {\n            cwd: this.serverless.config.servicePath,\n            silent: true,\n            follow: true,\n            nodir: true\n        };\n\n        if (Array.isArray(this.fastDeploy.include)) {\n            this.fastDeploy.include.forEach(pattern => zip.glob(pattern, globalGlobOptions));\n        }\n        else {\n            Object.keys(this.fastDeploy.include)\n                .forEach(relativePath => {\n                    const localGlobOptions = Object.assign({}, globalGlobOptions);\n                    localGlobOptions.cwd = path.join(this.serverless.config.servicePath, relativePath);\n                    const pattern = this.fastDeploy.include[relativePath];\n                    zip.glob(pattern, localGlobOptions)\n                });\n        }\n\n        zip.finalize();\n\n        return new Promise((resolve, reject) => {\n            let errored;\n\n            output.on(\"close\", () => {\n                if (!errored) resolve()\n            });\n\n            output.on(\"error\", err => {\n                errored = true;\n                reject(err);\n            })\n        })\n    }\n\n    createFastDeployFunctionArtifact() {\n        this.configPlugin();\n\n        return this.cleanFolder()\n            .then(() => fs.mkdirs(this.pathFolder))\n            .then(() => fs.createReadStream(__dirname + \"/fast_deploy.py\").pipe(fs.createWriteStream(this.pathFile)));\n    }\n\n\n    addFastDeployFunctionToService() {\n        /** Serverless FastDeploy function */\n        this.serverless.service.functions.FastDeploy = {\n            description: \"Serverless Fast Deploy Plugin\",\n            events: [],\n            handler: this.pathHandler,\n            memorySize: this.fastDeploy.memorySize,\n            name: this.fastDeploy.name,\n            runtime: \"python3.9\",\n            package: {\n                individually: true,\n                exclude: [\"**\"],\n                include: [this.folderName + \"/**\"]\n            },\n            timeout: this.fastDeploy.timeout\n        };\n\n        if (this.fastDeploy.role) {\n            this.serverless.service.functions.fastDeploy.role = this.fastDeploy.role\n        }\n\n        if (this.fastDeploy.tags) {\n            this.serverless.service.functions.fastDeploy.tags = this.fastDeploy.tags\n        }\n\n        /** Return service function object */\n        return this.serverless.service.functions.FastDeploy\n    }\n\n    /**\n     * Invoke Fast Deploy Function\n     * @returns {*}\n     */\n    invokeFastDeployFunction() {\n        const base64Data = fs.readFileSync(this.artifactFilePath, \"base64\");\n\n        const globPatterns = Array.isArray(this.fastDeploy.include) ?\n            this.fastDeploy.include :\n            Object.values(this.fastDeploy.include);\n\n        const params = {\n            FunctionName: this.fastDeploy.name,\n            InvocationType: \"RequestResponse\",\n            LogType: \"None\",\n            Qualifier: process.env.SERVERLESS_ALIAS || \"$LATEST\",\n            Payload: JSON.stringify({\n                serviceName: this.serverless.service.service,\n                deploymentStage: this.serverless.service.provider.stage,\n                deploymentS3BucketName: this.serverless.service.provider.deploymentBucket,\n                base64EncodedZipFileBytes: base64Data,\n                globPatterns: globPatterns,\n            })\n        };\n\n        return this.provider.request(\"Lambda\", \"invoke\", params)\n            .then(invokeFunctionResponse => {\n                let payload = invokeFunctionResponse.Payload;\n                let fastDeployResponse = JSON.parse(payload);\n\n                if (invokeFunctionResponse.FunctionError) {\n                    throw new Error(fastDeployResponse.errorMessage)\n                }\n\n                return fastDeployResponse.s3ObjectKey;\n            })\n    }\n\n    /**\n     * Invoke Fast Deploy Function\n     * @returns {*}\n     */\n    updateFunctionsCode(s3ObjectKey) {\n\n        let functions = this.serverless.service.functions;\n        let functionNames = Object.keys(functions);\n        const promises = functionNames.map(functionName => {\n            const function_ = functions[functionName];\n\n            const params = {\n                FunctionName: function_.name,\n                S3Bucket: this.serverless.service.provider.deploymentBucket,\n                S3Key: s3ObjectKey,\n            };\n\n            return this.provider.request(\"Lambda\", \"updateFunctionCode\", params)\n        });\n\n        return BbPromise.all(promises);\n    }\n}\n\n/** Export ServerlessPluginFastDeploy class */\nmodule.exports = ServerlessPluginFastDeploy;\n"]}