Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config.subdomain option; #9

Merged
merged 1 commit into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cfw.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Config {
entry?: string;
zoneid?: string;
profile?: string;
subdomain?: boolean;
routes?: string[];
build?: Builder;
globals?: Globals;
Expand Down
9 changes: 9 additions & 0 deletions src/@types/cloudflare.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ declare namespace Cloudflare {
type DELETE = Result<Pick<Route, 'id'>>;
}

interface Subdomain {
subdomain: string;
}

namespace Subdomain {
type GET = Result<Subdomain>;
type TOGGLE = Result<null>;
}

interface Script {
etag: string;
size: number;
Expand Down
25 changes: 3 additions & 22 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,10 @@ type Nullable<T> = 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;
Expand Down
25 changes: 25 additions & 0 deletions src/cloudflare/subdomains.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
return send<Cloudflare.Worker.Subdomain.GET>('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<Cloudflare.Worker.Subdomain.TOGGLE>('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)}`);
});
}
18 changes: 17 additions & 1 deletion src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand All @@ -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();
Expand All @@ -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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export async function toCredentials(def: Config, loose?: boolean): Promise<Crede
});
}

assert(zoneid || loose, 'Missing Cloudflare "zoneid" value!');
assert(zoneid || loose || def.subdomain, 'Missing Cloudflare "zoneid" value!');
assert(accountid, 'Missing Cloudflare "accountid" value!');

if (token || (authkey && email)) {
Expand Down