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c,KAAO,IAEnE,MAAK,IAEJ,EAAS,SAAQ,IAGb,EAAO,SAAS,KAAK,GACrB,EAAO,KAAK,iBAAkB,EAAQ,GAExC,IAEH,MAAM,EAAY,IAEtB,MAAM,EAEb,IAiBA,QAAQ,GAAG,SAAU,GAGrB,QAAQ,GAAG,UAAW,EAE1B,CACA,aAAA,CAAc,GAEV,MAAM,EAAS,KACT,EAAQ,EAAO,SAAS,QAAQ,GAEtC,OADA,EAAO,SAAS,OAAO,EAAO,GACvB,EAAQ,SAAS,MAAK,KAEzB,EAAO,KAAK,iBAAkB,GACvB,QAAQ,QAAQ,KAI/B,CACA,MAAA,CAAO,EAAM,EAAU,EAAS,GAE5B,EAAO,GAAQ,KAAK,QAAQ,MAAQ,KAAK,QAAQ,MAAM,MAAQ,EAE/D,MAAM,OAAO,EAAM,EAAU,EAAS,EAE1C,CACA,KAAA,CAAM,GAGF,MAAM,EAAO,KAAK,SAAS,KAAI,GAAW,EAAQ,MAGlD,QAAQ,IACJ,KAAK,SAAS,KAAI,GAAW,KAAK,cAAc,MAClD,MAAK,IAGI,QAAQ,IAAI,EAAK,KAAI,GAAO,MAAM,WAAW,QAErD,MAAK,IAGG,MAAM,SAEd,MAAK,KAGJ,MAAM,MAAM,EAAS,IAEtB,OAAM,IAEL,EAAS,EAAI,GAIrB,EAIJ,QAAQ,OAAS,wBAEjB,QAAQ,aAAe,SAAS,EAAM,GAElC,OAAO,IAAI,wBAAwB,EAAM,EAE7C","file":"/npm/contentful-webhook-tunnel@2.0.6/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/contentful-webhook-tunnel@2.0.6/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 listener = require(\"contentful-webhook-listener\");\nconst ContentfulWebhookListener = listener.Server;\nconst ngrok = require(\"ngrok\");\nconst contentfulManagement = require(\"contentful-management\");\nconst os = require(\"os\");\nconst crypto = require(\"crypto\");\nconst HttpsProxyAgent = require(\"https-proxy-agent\");\n\nvar agent;\nconst proxy = process.env.npm_config_https_proxy || process.env.HTTPS_PROXY;\nif (proxy) {\n    agent = new HttpsProxyAgent(proxy);\n}\n\n\n/**\n * generate a random string for HTTP Basic Authentication\n * @returns {String} auth string of 32 random characters\n */\nfunction randomAuth() {\n\n    // note, each byte encoded to hex is two characters\n    let username = crypto.randomBytes(8).toString(\"hex\");\n    let password = crypto.randomBytes(8).toString(\"hex\");\n\n    // insert an \":\" in the middle to make this an auth string\n    return `${username}:${password}`;\n\n}\n\nclass ContentfulWebhookTunnel extends ContentfulWebhookListener {\n    constructor (opts, requestListener) {\n\n        // must set opts.auth before calling parent constructor\n        if (process.env.NGROK_AUTH_TOKEN) {\n\n            // this option requires an AUTH TOKEN\n            opts.auth = opts.auth || randomAuth();\n\n        }\n\n        super(opts, requestListener);\n        this.options = this.options || {};\n        this.options.auth = opts.auth;\n        this.options.port = opts.port || 0;\n        this.options.spaces = opts.spaces || [];\n\n        // define our default ngrok options\n        this.options.ngrok = {\n            \"proto\": \"http\",\n            \"bind_tls\": true,\n            \"region\": \"us\"\n        };\n\n        // allow options passed to createServer() to override the defaults\n        Object.assign(this.options.ngrok, opts.ngrok);\n\n        // environment variables override defaults and values from createServer\n        if (process.env.NGROK_REGION) {\n\n            this.options.ngrok.region = process.env.NGROK_REGION;\n\n        }\n        if (process.env.NGROK_AUTH_TOKEN) {\n\n            this.options.ngrok.authtoken = process.env.NGROK_AUTH_TOKEN;\n\n        }\n        if (process.env.NGROK_SUBDOMAIN && process.env.NGROK_AUTH_TOKEN) {\n\n            // this option requires an AUTH TOKEN\n            this.options.ngrok.subdomain = process.env.NGROK_SUBDOMAIN;\n\n        }\n        if (opts.auth && process.env.NGROK_AUTH_TOKEN) {\n\n            // this option requires an AUTH TOKEN\n            this.options.ngrok.auth = opts.auth;\n\n        }\n\n        let server = this;\n\n        function handleError (err) {\n\n            server.emit(\"error\", err);\n            server.close();\n\n        }\n\n        // a place to save a list of registered webhooks\n        server.webhooks = [];\n\n        server.on(\"listening\", function () {\n\n            let port = server.address().port;\n\n            // tell ngrok which port we are listening on\n            this.options.ngrok.addr = port;\n\n            if (!process.env.CONTENTFUL_MANAGEMENT_ACCESS_TOKEN) {\n\n                handleError(new Error(\"CONTENTFUL_MANAGEMENT_ACCESS_TOKEN is undefined or invalid.\"));\n                return;\n\n            }\n\n            let client = contentfulManagement.createClient({\n                \"accessToken\": process.env.CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,\n                \"httpsAgent\": agent\n            });\n\n            let hostname = os.hostname();\n\n            const p1 = ngrok.connect(server.options.ngrok);\n\n            // connect ngrok and contentful\n            p1.then(url => {\n\n                server.emit(\"ngrokConnect\", url, null, port);\n\n                // find and remove webhooks with the same name before re-connecting\n                Promise.all(server.options.spaces.map(spaceId => {\n\n                    // use contentful API to get each space\n                    return client.getSpace(spaceId);\n\n                })).then(spaces => {\n\n                    // get all the webhooks for each space\n                    return Promise.all(spaces.map(space => space.getWebhooks()));\n\n                }).then(responses => {\n\n                    let webhooks = responses.filter(response => response.sys.type === \"Array\");\n\n                    webhooks = webhooks.reduce((acc, v) => acc.concat(v.items), []);\n\n                    // find matching webhooks\n                    let matches = webhooks.filter(webhook => {\n\n                        return webhook.name === `Tunnel to ${hostname}`;\n\n                    });\n\n                    // remove matching webhooks\n                    return Promise.all(matches.map(webhook => webhook.delete()));\n\n                }).then(() => {\n\n                    return Promise.all(server.options.spaces.map(spaceId => {\n\n                        // use contentful API to get each space\n                        return client.getSpace(spaceId);\n\n                    }));\n\n                }).then(spaces => {\n\n                    // use contentful API to create/update webhook\n                    let now = (new Date()).toGMTString();\n                    let data = {\n                        \"name\": `Tunnel to ${hostname}`,\n                        \"url\": url,\n                        \"headers\": [{\n                            \"key\": \"X-Date-Created\",\n                            \"value\": `${now}`\n                        }],\n                        \"topics\": [ \"*.*\" ]\n                    };\n\n                    // basic auth is optional\n                    if (server.options.auth) {\n\n                        let [username, password] = server.options.auth.split(\":\");\n                        data.httpBasicUsername = username;\n                        data.httpBasicPassword = password;\n\n                    }\n\n                    return Promise.all(spaces.map(space => space.createWebhook(data)));\n\n                }).then(webhooks => {\n\n                    webhooks.forEach(webhook => {\n\n                        // add to this server's list of registered webhooks\n                        server.webhooks.push(webhook);\n                        server.emit(\"webhookCreated\", webhook);\n\n                    });\n\n                }).catch(handleError);\n\n            }).catch(handleError);\n\n        });\n\n\n        // delete all webhooks when server closes\n        // TODO needed?\n\n        function handleSignal (signal) {\n\n            Promise.all(\n                server.webhooks.map(webhook => server.deleteWebhook(webhook))\n            ).then(() => {\n                process.exit(128 + signal);\n            });\n\n        }\n\n        // delete all webhooks when process is interrupted\n        process.on(\"SIGINT\", handleSignal);\n\n        // delete all webhook when process is terminated\n        process.on(\"SIGTERM\", handleSignal);\n\n    }\n    deleteWebhook(webhook) {\n\n        const server = this;\n        const index = server.webhooks.indexOf(webhook);\n        server.webhooks.splice(index, 1);\n        return webhook.delete().then(() => {\n\n            server.emit(\"webhookDeleted\", webhook);\n            return Promise.resolve(webhook);\n\n        });\n\n    }\n    listen(port, hostname, backlog, callback) {\n\n        port = port || this.options.port || this.options.ngrok.addr || 0;\n\n        super.listen(port, hostname, backlog, callback);\n\n    }\n    close(callback) {\n\n        // collect ALL the webhook urls\n        const urls = this.webhooks.map(webhook => webhook.url);\n\n        // delete webhook records from Contentful\n        Promise.all(\n            this.webhooks.map(webhook => this.deleteWebhook(webhook))\n        ).then(() => {\n\n            // close all the ngrok tunnels\n            return Promise.all(urls.map(url => ngrok.disconnect(url)));\n\n        }).then(() => {\n\n            // kills ngrok proess\n            return ngrok.kill();\n\n        }).then(() => {\n\n            // then close the server\n            super.close(callback);\n\n        }).catch(err => {\n\n            callback(err);\n\n        });\n\n    }\n\n}\n\nexports.Server = ContentfulWebhookTunnel;\n\nexports.createServer = function(opts, requestListener) {\n\n    return new ContentfulWebhookTunnel(opts, requestListener);\n\n};\n"]}