@faable/faable 4.37.0 → 4.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -495,8 +495,10 @@ class FaableApi {
495
495
  headers: projectHeader(team)
496
496
  }));
497
497
  }
498
- // Relink a domain to another app of its project (or `null` to unlink it).
499
- // Keeps the row: same id, same CNAME target, same certificate.
498
+ // Relink a domain to another app of its project (`app_id`, or `null` to
499
+ // unlink it), or make it redirect to another domain of the project
500
+ // (`redirect`, `null` to serve again). Keeps the row: same id, same CNAME
501
+ // target, same certificate.
500
502
  async updateDomain(domain_id, team, params) {
501
503
  return data(this.client.post(`/domain/${domain_id}`, params, {
502
504
  headers: projectHeader(team)
@@ -1,7 +1,7 @@
1
1
  import { withAuthHints, requireAuthAdmin } from '../../../api/auth_admin.js';
2
+ import { json_option, fetch_page, from_paginator, print_json } from '../../../lib/listing.js';
2
3
  import { log } from '../../../log.js';
3
4
  import { tenant_options } from '../options.js';
4
- import { json_option, fetch_page, from_paginator, print_json } from '../../../lib/listing.js';
5
5
  import { yes_no, when } from '../render.js';
6
6
  import { resolve_user_ref } from './resolve.js';
7
7
 
@@ -11,6 +11,20 @@ const sanitize_identity = (i) => {
11
11
  const { access_token: _a, access_token_secret: _s, refresh_token: _r, ...safe } = i;
12
12
  return safe;
13
13
  };
14
+ // An expanded `connection` ({ id, connection_name, … }) — or a bare id when
15
+ // the server did not expand it — as the id plus a readable name.
16
+ const connection_ref = (c) => {
17
+ if (c && typeof c === 'object') {
18
+ const o = c;
19
+ const id = String(o.id ?? '');
20
+ return {
21
+ connection: id,
22
+ connection_name: o.connection_name ?? o.connection_type ?? id
23
+ };
24
+ }
25
+ const id = String(c ?? '');
26
+ return { connection: id, connection_name: id };
27
+ };
14
28
  const users_get = {
15
29
  command: 'get <user_id>',
16
30
  describe: 'Show a user (by id or email), their federated identities and their database login identifiers',
@@ -28,42 +42,32 @@ const users_get = {
28
42
  // Federated identities (GitHub, Google…): who this user IS at the
29
43
  // provider — the piece the abuse playbook keeps needing (a Faable email
30
44
  // rarely matches the GitHub handle that owns the deployed repos).
45
+ // `expand=connection` brings the connection's name in the same call.
31
46
  // Best-effort: a tenant where the session can't read /identity still
32
47
  // renders the user card.
33
- const identities = await fetch_page(from_paginator(api.identityList({ query: `user:${user_id}` })), { limit: 200, all: true })
34
- .then(r => r.data.map(i => sanitize_identity(i)))
48
+ const identities = await fetch_page(from_paginator(api.identityList({ query: `user:${user_id}`, expand: ['connection'] })), { limit: 200, all: true })
49
+ .then(r => r.data.map(i => {
50
+ const safe = sanitize_identity(i);
51
+ return {
52
+ ...safe,
53
+ ...connection_ref(safe.connection)
54
+ };
55
+ }))
35
56
  .catch(() => []);
36
57
  // Database-connection credentials: the email/username the user actually
37
58
  // TYPES to log in. Distinct from `user.email` (the profile email), which
38
59
  // is NOT what the password login matches — the gap that otherwise sends
39
60
  // you to the dashboard to find out why a login keeps failing. Best-effort.
40
- const credentials = await fetch_page(from_paginator(api.credentialsList({ user: user_id })), { limit: 200, all: true })
61
+ const credentials = await fetch_page(from_paginator(api.credentialsList({ user: user_id, expand: ['connection'] })), { limit: 200, all: true })
41
62
  .then(r => r.data.map(c => ({
42
63
  id: String(c.id ?? ''),
43
- connection: String(c.connection ?? ''),
64
+ ...connection_ref(c.connection),
44
65
  email: c.email,
45
66
  username: c.username
46
67
  })))
47
68
  .catch(() => []);
48
- // Resolve connection ids to names ("github", "default") once per distinct
49
- // id — identities and credentials alike.
50
- const connection_names = new Map();
51
- for (const id of new Set([
52
- ...identities.map(i => String(i.connection ?? '')),
53
- ...credentials.map(c => c.connection)
54
- ].filter(Boolean))) {
55
- const conn = (await api.connectionGet(id).catch(() => null));
56
- connection_names.set(id, conn?.connection_name ?? conn?.connection_type ?? id);
57
- }
58
69
  if (args.json)
59
- return print_json({
60
- ...user,
61
- identities,
62
- credentials: credentials.map(c => ({
63
- ...c,
64
- connection_name: connection_names.get(c.connection) ?? c.connection
65
- }))
66
- });
70
+ return print_json({ ...user, identities, credentials });
67
71
  log.info(`👤 ${user.id}`);
68
72
  log.info(` Email: ${user.email ?? '-'} (verified: ${yes_no(user.email_verified)})`);
69
73
  log.info(` Name: ${user.name ?? '-'}`);
@@ -72,7 +76,7 @@ const users_get = {
72
76
  // profile email above, in which case that mismatch is the bug.
73
77
  const ids = credentials
74
78
  .map(c => {
75
- const conn = connection_names.get(c.connection) ?? (c.connection || 'default');
79
+ const conn = c.connection_name || 'default';
76
80
  return `${c.email ?? c.username ?? '-'} (${conn})`;
77
81
  })
78
82
  .join(', ');
@@ -96,7 +100,7 @@ const users_get = {
96
100
  if (identities.length > 0) {
97
101
  log.info(` Identities:`);
98
102
  for (const identity of identities) {
99
- const conn = connection_names.get(String(identity.connection ?? '')) ?? '-';
103
+ const conn = String(identity.connection_name ?? '-');
100
104
  const pd = (identity.profile_data ?? {});
101
105
  const bits = [
102
106
  pd.login ?? String(identity.identity_id ?? '-'),
@@ -104,7 +108,9 @@ const users_get = {
104
108
  // Provider-account age is triage gold: a repo pushed from a
105
109
  // years-old GitHub account reads differently than one created
106
110
  // this morning.
107
- pd.created_at ? `account since ${String(pd.created_at).slice(0, 10)}` : undefined,
111
+ pd.created_at
112
+ ? `account since ${String(pd.created_at).slice(0, 10)}`
113
+ : undefined,
108
114
  pd.email && pd.email !== user.email ? `email ${pd.email}` : undefined,
109
115
  pd.location ? `location ${pd.location}` : undefined
110
116
  ].filter(Boolean);
@@ -36,6 +36,9 @@ const domains_check = {
36
36
  if (!domain.app_id) {
37
37
  log.warn(` Linked to no app: it answers DOMAIN_NOT_LINKED. Serve it with: faable deploy domains attach ${domain.fqdn} --app <app>`);
38
38
  }
39
+ if (domain.redirect) {
40
+ log.info(` Redirects to: https://${domain.redirect} (path and query kept). Serve it again with: faable deploy domains redirect ${domain.fqdn} --off`);
41
+ }
39
42
  log.info(` Expected CNAME: ${domain.fqdn} → ${cname_target(domain)}`);
40
43
  if (status?.dns_observed?.length) {
41
44
  log.info(` Observed: ${status.dns_observed.join(', ')}`);
@@ -47,5 +47,13 @@ const find_by_fqdn = (domains, fqdn) => domains.find(d => d.fqdn.toLowerCase() =
47
47
  * the counterpart to look it up.
48
48
  */
49
49
  const pair_fqdn = (fqdn) => fqdn.startsWith('www.') ? fqdn.slice(4) : `www.${fqdn}`;
50
+ /** ` ↪ example.com` for a domain that redirects, '' for one that serves. */
51
+ const redirect_badge = (domain) => domain.redirect ? `↪ ${domain.redirect}` : '';
52
+ /**
53
+ * The domains of `all` that redirect to `domain`. They are part of it: deleting
54
+ * `domain` deletes them too (the api cascades; same project only).
55
+ */
56
+ const redirects_to = (all, domain) => all.filter(d => d.id !== domain.id &&
57
+ d.redirect?.toLowerCase() === domain.fqdn.toLowerCase());
50
58
 
51
- export { acme_record, cname_target, dns_badge, find_by_fqdn, pair_fqdn, tls_line };
59
+ export { acme_record, cname_target, dns_badge, find_by_fqdn, pair_fqdn, redirect_badge, redirects_to, tls_line };
@@ -2,6 +2,7 @@ import { domains_add } from './add.js';
2
2
  import { domains_attach } from './attach.js';
3
3
  import { domains_check } from './check.js';
4
4
  import { domains_list } from './list.js';
5
+ import { domains_redirect } from './redirect.js';
5
6
  import { domains_rm } from './rm.js';
6
7
 
7
8
  const domains = {
@@ -12,8 +13,9 @@ const domains = {
12
13
  .command(domains_add)
13
14
  .command(domains_attach)
14
15
  .command(domains_check)
16
+ .command(domains_redirect)
15
17
  .command(domains_rm)
16
- .demandCommand(1, 'Specify a domains command: list, add, attach, check or rm'),
18
+ .demandCommand(1, 'Specify a domains command: list, add, attach, check, redirect or rm'),
17
19
  handler: () => {
18
20
  // Unreachable: demandCommand(1) either routes to a subcommand or fails
19
21
  // through the global .fail() in src/index.ts.
@@ -3,7 +3,7 @@ import { fetch_page, print_json, print, more_hint, list_options } from '../../..
3
3
  import { log } from '../../../log.js';
4
4
  import { requireProject } from '../../../api/project.js';
5
5
  import { find_app_id } from '../resolve_app_id.js';
6
- import { dns_badge, cname_target } from './format.js';
6
+ import { dns_badge, redirect_badge, cname_target } from './format.js';
7
7
 
8
8
  const domains_list = {
9
9
  command: 'list',
@@ -35,7 +35,7 @@ const domains_list = {
35
35
  const width = Math.max(...domains.map(d => d.fqdn.length));
36
36
  for (const domain of domains) {
37
37
  const tls = domain.tls ? 'tls' : 'no-tls';
38
- print(` ${domain.fqdn.padEnd(width)} ${dns_badge(domain)} (${tls})`);
38
+ print(` ${domain.fqdn.padEnd(width)} ${dns_badge(domain)} (${tls}) ${redirect_badge(domain)}`.trimEnd());
39
39
  }
40
40
  const hint = more_hint(page);
41
41
  if (hint)
@@ -75,7 +75,7 @@ const list_project_domains = async (api, args) => {
75
75
  const app_width = Math.max(...domains.map(d => label(d).length));
76
76
  for (const domain of domains) {
77
77
  const tls = domain.tls ? 'tls' : 'no-tls';
78
- print(` ${domain.fqdn.padEnd(width)} ${label(domain).padEnd(app_width)} ${dns_badge(domain)} (${tls})`);
78
+ print(` ${domain.fqdn.padEnd(width)} ${label(domain).padEnd(app_width)} ${dns_badge(domain)} (${tls}) ${redirect_badge(domain)}`.trimEnd());
79
79
  }
80
80
  const hint = more_hint(page);
81
81
  if (hint)
@@ -0,0 +1,77 @@
1
+ import { requireApi } from '../../../api/context.js';
2
+ import { CliError } from '../../../lib/errors.js';
3
+ import { print_json, json_option } from '../../../lib/listing.js';
4
+ import { log } from '../../../log.js';
5
+ import { find_by_fqdn } from './format.js';
6
+ import { domain_scope, project_domains, not_in_project } from './locate.js';
7
+
8
+ const domains_redirect = {
9
+ command: 'redirect <fqdn> [target]',
10
+ describe: 'Make a domain redirect every request to another domain of the project (or serve again with --off)',
11
+ builder: yargs => json_option(yargs)
12
+ .positional('fqdn', {
13
+ type: 'string',
14
+ demandOption: true,
15
+ description: 'The domain that redirects'
16
+ })
17
+ .positional('target', {
18
+ type: 'string',
19
+ description: 'The domain it redirects to: same project, and one that serves'
20
+ })
21
+ .option('off', {
22
+ type: 'boolean',
23
+ default: false,
24
+ description: 'Stop redirecting: the domain serves its app again'
25
+ })
26
+ .option('app', {
27
+ alias: 'a',
28
+ type: 'string',
29
+ description: 'App id, name or slug. Optional: the domain is looked up in the project'
30
+ })
31
+ .example('$0 deploy domains redirect www.example.com example.com', 'www.example.com/path?q → https://example.com/path?q')
32
+ .example('$0 deploy domains redirect www.example.com --off', 'www.example.com serves its app again')
33
+ .epilog('GET gets a 301, every other method (HEAD included) a 308: the path and the query are kept. A domain redirects to the one it names, never further: no chains. Removing the target removes the domains that redirect to it.')
34
+ .showHelpOnFail(false),
35
+ handler: async (args) => {
36
+ if (!args.off && !args.target) {
37
+ throw new CliError('usage', 'Name the domain to redirect to, or pass --off to stop redirecting.');
38
+ }
39
+ if (args.off && args.target) {
40
+ throw new CliError('usage', 'Pass either a target or --off, not both.');
41
+ }
42
+ const ctx = await requireApi();
43
+ const scope = await domain_scope(ctx.api, args.app, ctx.appId);
44
+ const all = await project_domains(ctx.api, scope.team);
45
+ const domain = find_by_fqdn(all, args.fqdn);
46
+ if (!domain)
47
+ throw not_in_project(args.fqdn, scope.team);
48
+ const target = args.off ? null : args.target.toLowerCase();
49
+ if ((domain.redirect ?? null) === target) {
50
+ if (args.json)
51
+ return print_json(domain);
52
+ log.info(target
53
+ ? `↪ ${domain.fqdn} already redirects to ${target}.`
54
+ : `🌐 ${domain.fqdn} already serves its app: it redirects nowhere.`);
55
+ return;
56
+ }
57
+ // The api checks the target (same project, serving, no chains) and says
58
+ // why in its error; this only spares the round trip for the plain typo.
59
+ if (target && !find_by_fqdn(all, target)) {
60
+ throw new CliError('domain_redirect_invalid', `${target} is not a domain of this project. Add it first with "faable deploy domains add ${target}": a domain can only redirect to another domain of the same project.`);
61
+ }
62
+ const updated = await ctx.api.updateDomain(domain.id, scope.team, {
63
+ redirect: target
64
+ });
65
+ if (args.json)
66
+ return print_json(updated);
67
+ if (target) {
68
+ log.info(`↪ ${updated.fqdn} now redirects to ${target}: GET → 301, other methods → 308, path and query kept.`);
69
+ log.info(`Removing ${target} will remove ${updated.fqdn} too. Its CNAME and certificate stay as they are.`);
70
+ }
71
+ else {
72
+ log.info(`🌐 ${updated.fqdn} no longer redirects: it serves ${updated.app_id ?? 'no app (link one with "faable deploy domains attach")'}.`);
73
+ }
74
+ }
75
+ };
76
+
77
+ export { domains_redirect };
@@ -2,7 +2,8 @@ import { requireApi } from '../../../api/context.js';
2
2
  import { confirm } from '../../../lib/interactive.js';
3
3
  import { print_json, json_option } from '../../../lib/listing.js';
4
4
  import { log } from '../../../log.js';
5
- import { domain_scope, locate_domain } from './locate.js';
5
+ import { redirects_to } from './format.js';
6
+ import { domain_scope, locate_domain, project_domains } from './locate.js';
6
7
 
7
8
  const domains_rm = {
8
9
  command: 'rm <fqdn>',
@@ -36,10 +37,16 @@ const domains_rm = {
36
37
  ? `${scope.app.name} (${domain.app_id})`
37
38
  : domain.app_id
38
39
  : null;
40
+ // The domains that redirect to it go with it (the api cascades): say so
41
+ // before, not after.
42
+ const dependents = redirects_to(await project_domains(ctx.api, scope.team), domain);
43
+ const also = dependents.length
44
+ ? ` ${dependents.map(d => d.fqdn).join(', ')} redirect${dependents.length === 1 ? 's' : ''} to it and will be removed too.`
45
+ : '';
39
46
  const go = await confirm({
40
47
  message: serves
41
- ? `Remove domain "${domain.fqdn}" from ${serves}? Traffic to it will stop being served.`
42
- : `Remove domain "${domain.fqdn}"? It is linked to no app. To serve it again with the same CNAME and certificate, use "faable deploy domains attach" instead.`,
48
+ ? `Remove domain "${domain.fqdn}" from ${serves}? Traffic to it will stop being served.${also}`
49
+ : `Remove domain "${domain.fqdn}"? It is linked to no app. To serve it again with the same CNAME and certificate, use "faable deploy domains attach" instead.${also}`,
43
50
  yes: args.yes
44
51
  });
45
52
  if (!go) {
@@ -48,10 +55,15 @@ const domains_rm = {
48
55
  }
49
56
  await ctx.api.deleteDomain(domain.id, scope.team);
50
57
  if (args.json) {
51
- return print_json({ deleted: true, id: domain.id, fqdn: domain.fqdn });
58
+ return print_json({
59
+ deleted: true,
60
+ id: domain.id,
61
+ fqdn: domain.fqdn,
62
+ also_deleted: dependents.map(d => ({ id: d.id, fqdn: d.fqdn }))
63
+ });
52
64
  }
53
- log.info(`🗑️ Removed domain ${domain.fqdn}${serves ? ` from ${serves}` : ''}.`);
54
- log.info(`Remember to delete the CNAME at your DNS provider.`);
65
+ log.info(`🗑️ Removed domain ${domain.fqdn}${serves ? ` from ${serves}` : ''}${dependents.length ? `, and ${dependents.map(d => d.fqdn).join(', ')} that redirected to it` : ''}.`);
66
+ log.info(`Remember to delete the CNAME${dependents.length ? 's' : ''} at your DNS provider.`);
55
67
  }
56
68
  };
57
69
 
package/dist/mcp/tools.js CHANGED
@@ -420,7 +420,7 @@ const TOOLS = [
420
420
  {
421
421
  name: 'list_domains',
422
422
  title: 'List custom domains',
423
- description: 'Custom domains of an app and whether their DNS is verified. Without `app`, every domain of the project with the app it serves: `app_id: null` is a domain linked to no app (its app was deleted), which answers DOMAIN_NOT_LINKED until attach_domain links it.',
423
+ description: 'Custom domains of an app and whether their DNS is verified. Without `app`, every domain of the project with the app it serves: `app_id: null` is a domain linked to no app (its app was deleted), which answers DOMAIN_NOT_LINKED until attach_domain links it. `redirect` names the domain it sends every request to (the www half of a pair, or an alias); null when it serves its app.',
424
424
  input: { app: optional_app, project },
425
425
  command: ['deploy', 'domains', 'list'],
426
426
  flags: app_or_project,
@@ -430,6 +430,7 @@ const TOOLS = [
430
430
  app_id: d.app_id ?? null,
431
431
  verified: d.verified,
432
432
  tls: d.tls,
433
+ redirect: d.redirect ?? null,
433
434
  dns_message: d.status?.dns_message
434
435
  })),
435
436
  annotations: READ,
@@ -450,6 +451,7 @@ const TOOLS = [
450
451
  fqdn: d.fqdn,
451
452
  app_id: d.app_id ?? null,
452
453
  verified: d.verified,
454
+ redirect: d.redirect ?? null,
453
455
  expected_cname: d.expected_cname,
454
456
  observed: d.status?.dns_observed ?? [],
455
457
  diagnostic: d.status?.dns_message,
@@ -625,9 +627,15 @@ const TOOLS = [
625
627
  ...user_row(u),
626
628
  phone: u.phone,
627
629
  last_ip: u.last_ip,
630
+ // `auth users get --json` rows: { connection, connection_name,
631
+ // identity_id, profile_data } — the provider account behind the user.
628
632
  identities: (u.identities ?? []).map((i) => ({
629
- provider: i.provider ?? i.connection_type,
630
- user_id: i.user_id ?? i.provider_user_id
633
+ connection: i.connection_name ?? i.connection,
634
+ identity_id: i.identity_id,
635
+ login: i.profile_data?.login,
636
+ profile_url: i.profile_data?.html_url,
637
+ account_created_at: i.profile_data?.created_at,
638
+ provider_email: i.profile_data?.email
631
639
  })),
632
640
  credentials: (u.credentials ?? []).map((c) => ({
633
641
  connection: c.connection_name ?? c.connection,
@@ -1047,6 +1055,40 @@ const TOOLS = [
1047
1055
  },
1048
1056
  example: { app: 'web', fqdn: 'app.example.com' }
1049
1057
  },
1058
+ {
1059
+ name: 'set_domain_redirect',
1060
+ title: 'Redirect a domain to another',
1061
+ description: 'Make a custom domain send every request to another domain of the same project (GET → 301, other methods → 308, path and query kept), e.g. www.example.com to example.com, or an old name to the new one. `target: null` stops it: the domain serves its app again. The target must be a domain of the project that serves (no chains). Deleting the target later deletes the domains that redirect to it. DNS and certificate do not change.',
1062
+ input: {
1063
+ app: optional_app,
1064
+ project,
1065
+ fqdn: z.string().describe('The domain that redirects, e.g. www.example.com'),
1066
+ target: z
1067
+ .string()
1068
+ .nullable()
1069
+ .describe('The domain it redirects to, e.g. example.com; null to stop redirecting')
1070
+ },
1071
+ command: ['deploy', 'domains', 'redirect'],
1072
+ flags: a => [
1073
+ a.fqdn,
1074
+ ...(a.target ? [a.target] : ['--off']),
1075
+ ...app_or_project(a)
1076
+ ],
1077
+ write: true,
1078
+ shape: (d) => ({
1079
+ id: d.id,
1080
+ fqdn: d.fqdn,
1081
+ app_id: d.app_id ?? null,
1082
+ redirect: d.redirect ?? null
1083
+ }),
1084
+ annotations: {
1085
+ readOnlyHint: false,
1086
+ destructiveHint: false,
1087
+ idempotentHint: true,
1088
+ openWorldHint: true
1089
+ },
1090
+ example: { fqdn: 'www.example.com', target: 'example.com' }
1091
+ },
1050
1092
  {
1051
1093
  name: 'configure_repo',
1052
1094
  title: 'Configure how an app deploys',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faable/faable",
3
- "version": "4.37.0",
3
+ "version": "4.38.1",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Marc Pomar <marc@faable.com>",