From 502a1ae13d19a66294041da83f07113483613c35 Mon Sep 17 00:00:00 2001 From: David Barr <38654497+davebarrau@users.noreply.github.com> Date: Wed, 9 Jun 2021 22:43:58 +1000 Subject: [PATCH] Add support for zoneless deployments (workers.dev only) --- deploy/lib/multiscript.js | 9 ++++++++- shared/accountType.js | 3 ++- shared/duplicate.js | 15 ++++++++++++++- shared/multiscript.js | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/deploy/lib/multiscript.js b/deploy/lib/multiscript.js index 36632c5..9405540 100644 --- a/deploy/lib/multiscript.js +++ b/deploy/lib/multiscript.js @@ -34,10 +34,17 @@ module.exports = { const workerScriptResponse = await ms.deployWorker(this.provider.config.accountId, this.serverless, functionObject); const routesResponse = await ms.deployRoutes(this.provider.config.zoneId, functionObject); + let zonelessResponse; + + if (functionObject.zoneless) { + zonelessResponse = await ms.deployZoneless(this.provider.config.accountId, functionObject); + } + return { workerScriptResponse, routesResponse, - namespaceResponse + namespaceResponse, + zonelessResponse }; }); }, diff --git a/shared/accountType.js b/shared/accountType.js index e952562..b523213 100644 --- a/shared/accountType.js +++ b/shared/accountType.js @@ -22,11 +22,12 @@ const cf = require("cloudflare-workers-toolkit"); module.exports = { async checkAccountType() { + const accountId = this.provider.config.accountId; const zoneId = this.provider.config.zoneId; return await BB.bind(this) .then(this.checkAllEnvironmentVariables) .then(function() { - return cf.workers.getSettings({zoneId}); + return cf.workers.getSettings({zoneId, accountId}); }) .then(this.checkIfMultiScript) }, diff --git a/shared/duplicate.js b/shared/duplicate.js index 9ef662a..0a0530a 100644 --- a/shared/duplicate.js +++ b/shared/duplicate.js @@ -33,6 +33,20 @@ module.exports = { const { zoneId } = provider.config; + const functions = serverless.service.getAllFunctions(); + + const hasRoutes = functions.some(scriptName => { + const functionObject = serverless.service.getFunction(scriptName); + + if (functionObject.events && functionObject.events.length) { + return true; + } + }); + + if (!hasRoutes) { + return false; + } + if (!zoneId) { throw("You must specify a Zone ID CLOUDFLARE_ZONE_ID"); } @@ -43,7 +57,6 @@ module.exports = { const foundDuplicate = result.some(filters => { const { pattern, script } = filters; - const functions = serverless.service.getAllFunctions(); for (const scriptName of functions) { const functionObject = serverless.service.getFunction(scriptName); const routes = functionObject.events.map(function(event) { diff --git a/shared/multiscript.js b/shared/multiscript.js index 5527cb5..52429fe 100644 --- a/shared/multiscript.js +++ b/shared/multiscript.js @@ -17,6 +17,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ const cf = require("cloudflare-workers-toolkit"); +const sdk = require("../provider/sdk"); const path = require("path"); const { generateCode, generateWASM } = require("../deploy/lib/workerScript"); @@ -145,5 +146,18 @@ module.exports = { } return routeResponses; + }, + + async deployZoneless(accountId, functionObject) { + const name = functionObject.name; + + const response = await sdk.cfApiCall({ + url: `/accounts/${accountId}/workers/scripts/${name}/subdomain`, + method: `POST`, + contentType: 'application/json', + body: JSON.stringify({enabled: true}) + }); + + return response; } } \ No newline at end of file