From 85a2eabcda8a272634f1ab20dead90e2fc4e141b Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Sat, 14 Aug 2021 10:45:27 -0700 Subject: [PATCH] feat: add `config.subdomain` option; - Closes #7 --- cfw.d.ts | 1 + src/@types/cloudflare.d.ts | 9 +++++++++ src/@types/index.d.ts | 25 +++---------------------- src/cloudflare/subdomains.ts | 25 +++++++++++++++++++++++++ src/commands/deploy.ts | 18 +++++++++++++++++- src/util.ts | 2 +- 6 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 src/cloudflare/subdomains.ts diff --git a/cfw.d.ts b/cfw.d.ts index 800d230..24d7467 100644 --- a/cfw.d.ts +++ b/cfw.d.ts @@ -12,6 +12,7 @@ export interface Config { entry?: string; zoneid?: string; profile?: string; + subdomain?: boolean; routes?: string[]; build?: Builder; globals?: Globals; diff --git a/src/@types/cloudflare.d.ts b/src/@types/cloudflare.d.ts index 393947e..50d2948 100644 --- a/src/@types/cloudflare.d.ts +++ b/src/@types/cloudflare.d.ts @@ -24,6 +24,15 @@ declare namespace Cloudflare { type DELETE = Result>; } + interface Subdomain { + subdomain: string; + } + + namespace Subdomain { + type GET = Result; + type TOGGLE = Result; + } + interface Script { etag: string; size: number; diff --git a/src/@types/index.d.ts b/src/@types/index.d.ts index 8362504..cae6623 100644 --- a/src/@types/index.d.ts +++ b/src/@types/index.d.ts @@ -3,29 +3,10 @@ type Nullable = T | null; // --- -type Builder = (config: import('esbuild').BuildOptions) => void; -type Globals = { - [name: string]: - | `KV:${string}` - | `ENV:${string}` - | `SECRET:${string}` - | `WASM:${string}` -} +type Builder = import('../..').Builder; +type Globals = import('../..').Globals; -interface Config { - name?: string; - entry?: string; - zoneid?: string; - profile?: string; - routes?: string[]; - build?: Builder; - globals?: Globals; - // should not exist - token?: string; - accountid?: string; - authkey?: string; - email?: string; -} +type Config = import('../..').Config; interface WorkerData { input: string; diff --git a/src/cloudflare/subdomains.ts b/src/cloudflare/subdomains.ts new file mode 100644 index 0000000..6a14ce4 --- /dev/null +++ b/src/cloudflare/subdomains.ts @@ -0,0 +1,25 @@ +import { error } from '../log'; +import { send, authorize } from './client'; + +// @see https://api.cloudflare.com/#worker-subdomain-get-subdomain +export function get(creds: Credentials): Promise { + return send('GET', `/accounts/${creds.accountid}/workers/subdomain`, { + headers: authorize(creds) + }).then(res => { + let subdomain = res.success && res.result.subdomain; + if (!subdomain) throw new Error('You must register a subdomain!'); + return `${subdomain}.workers.dev`; + }).catch(err => { + error(`Error fetching your workers.dev subdomain!\n${JSON.stringify(err.data || err.message, null, 2)}`); + }); +} + +export function toggle(creds: Credentials, worker: string, enabled: boolean) { + let path = `/accounts/${creds.accountid}/workers/scripts/${worker}/subdomain`; + return send('POST', path, { + headers: authorize(creds), + body: { enabled } + }).catch(err => { + error(`Error publishing "${worker}" to workers.dev subdomain!\n${JSON.stringify(err.data || err.message, null, 2)}`); + }); +} diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index 0121c55..3b29089 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -1,5 +1,6 @@ import colors from 'kleur'; import * as workers from '../cloudflare/workers'; +import * as subdomains from '../cloudflare/subdomains'; import * as globals from '../cloudflare/globals'; import * as utils from '../util'; import * as log from '../log'; @@ -9,6 +10,7 @@ export default async function (output: string | void, opts: Options) { let items = await utils.toWorkers(buildDir, opts); if (!items.length) return log.missing('Nothing to deploy!', opts); + let subdomain: string | void; let arrow = colors.cyan(log.ARROW); let count = colors.bold(items.length); let sfx = items.length === 1 ? '' : 's'; @@ -28,7 +30,15 @@ export default async function (output: string | void, opts: Options) { await workers.script(creds, name, filedata, metadata); console.log(arrow + name + log.time(Date.now() - now)); - if (cfw.routes) { + if (cfw.subdomain != null && !subdomain) { + subdomain = await subdomains.get(creds); + } + + if (cfw.subdomain) { + let t1 = Date.now(); + await subdomains.toggle(creds, name, true); + log.item(`https://${name}.${subdomain}/*`, Date.now() - t1, true); + } else if (cfw.routes) { await Promise.all( cfw.routes.map(str => { let iter = Date.now(); @@ -39,6 +49,12 @@ export default async function (output: string | void, opts: Options) { }); }) ); + + if (cfw.subdomain != null) { + let t1 = Date.now(); + await subdomains.toggle(creds, name, false); + log.item(`https://${name}.${subdomain}/*`, Date.now() - t1, false); + } } } diff --git a/src/util.ts b/src/util.ts index 22636a2..82b1a68 100644 --- a/src/util.ts +++ b/src/util.ts @@ -151,7 +151,7 @@ export async function toCredentials(def: Config, loose?: boolean): Promise