@faable/faable 4.23.1 → 4.25.0

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.
@@ -341,6 +341,17 @@ class FaableApi {
341
341
  // Rebuild a failed deployment from its recorded source (CAS manifest or
342
342
  // git ref). The API enforces the guards: failed phase only, never older
343
343
  // than what production serves.
344
+ // Tasks (one-off commands in a copy of the app's pod; api `task/`).
345
+ async createTask(team, body) {
346
+ return data(this.client.post(`/task`, body, {
347
+ headers: projectHeader(team)
348
+ }));
349
+ }
350
+ async getTask(task_id, team) {
351
+ return data(this.client.get(`/task/${task_id}`, {
352
+ headers: projectHeader(team)
353
+ }));
354
+ }
344
355
  async redeployDeployment(deployment_id, team) {
345
356
  return data(this.client.post(`/deployment/${deployment_id}/redeploy`, undefined, { headers: projectHeader(team) }));
346
357
  }
@@ -0,0 +1,12 @@
1
+ // What `--type` makes of a new client. `web` is the server-rendered app that
2
+ // keeps a secret; `spa` and `native` run where a secret cannot be kept, so they
3
+ // are public clients: PKCE, `token_endpoint_auth_method: none`, no secret. The
4
+ // create route takes neither field (auth ClientCreate), so the CLI sets them
5
+ // with an update right after creating the client.
6
+ const CLIENT_TYPES = ['web', 'spa', 'native'];
7
+ const is_public_client = (type) => type !== 'web';
8
+ const client_type_patch = (type) => is_public_client(type)
9
+ ? { application_type: type, token_endpoint_auth_method: 'none' }
10
+ : null;
11
+
12
+ export { CLIENT_TYPES, client_type_patch, is_public_client };
@@ -2,6 +2,7 @@ import { withAuthHints, requireAuthAdmin } from '../../../api/auth_admin.js';
2
2
  import { log } from '../../../log.js';
3
3
  import { tenant_options } from '../options.js';
4
4
  import { json_option, print_json } from '../../../lib/listing.js';
5
+ import { CLIENT_TYPES, client_type_patch, is_public_client } from './client_type.js';
5
6
 
6
7
  const clients_create = {
7
8
  command: 'create',
@@ -17,6 +18,12 @@ const clients_create = {
17
18
  alias: 'd',
18
19
  type: 'string',
19
20
  description: 'Client description'
21
+ })
22
+ .option('type', {
23
+ alias: 't',
24
+ choices: CLIENT_TYPES,
25
+ default: 'web',
26
+ description: 'web: a server app that keeps a secret. spa / native: a browser or mobile app, a public client with PKCE and no secret'
20
27
  })
21
28
  .option('callback', {
22
29
  type: 'string',
@@ -34,20 +41,36 @@ const clients_create = {
34
41
  description: 'Allowed web origin (CORS) — repeatable'
35
42
  })
36
43
  .example('$0 auth clients create -n my-app --callback https://app.example.com/callback', 'Create a client with one redirect URL')
44
+ .example('$0 auth clients create -n my-spa -t spa --callback https://app.example.com/ --web-origin https://app.example.com', 'Create a single-page app (public client, no secret)')
37
45
  .showHelpOnFail(false),
38
46
  handler: withAuthHints(async (args) => {
39
47
  const api = await requireAuthAdmin(args);
40
- const client = await api.clientCreate({
48
+ const created = await api.clientCreate({
41
49
  name: args.name,
42
50
  ...(args.description ? { description: args.description } : {}),
43
51
  ...(args.callback?.length ? { callbacks: args.callback } : {}),
44
52
  ...(args.logoutUrl?.length ? { logout_urls: args.logoutUrl } : {}),
45
53
  ...(args.webOrigin?.length ? { web_origins: args.webOrigin } : {})
46
54
  });
47
- if (args.json)
48
- return print_json(client);
55
+ const patch = client_type_patch(args.type);
56
+ const client = patch
57
+ ? { ...created, ...(await api.clientUpdate(created.id, patch)) }
58
+ : created;
59
+ const public_client = is_public_client(args.type);
60
+ // A public client never uses its secret: printing one would only invite
61
+ // someone to ship it in a bundle.
62
+ if (args.json) {
63
+ if (!public_client)
64
+ return print_json(client);
65
+ const { client_secret: _secret, ...rest } = client;
66
+ return print_json(rest);
67
+ }
49
68
  log.info(`✅ Created client "${client.name}" (${client.id})`);
50
69
  log.info(` Client ID: ${client.client_id}`);
70
+ if (public_client) {
71
+ log.info(` Type: ${args.type} — public client: PKCE, no secret (safe to ship in the app)`);
72
+ return;
73
+ }
51
74
  log.info(` Client secret: ${client.client_secret}`);
52
75
  log.info(' ⚠️ Store the secret now — treat it like a password.');
53
76
  })
@@ -1,7 +1,7 @@
1
- import { github } from './github/index.js';
2
1
  import { link } from '../link/index.js';
3
2
  import { apps, apps_list_legacy } from './apps/index.js';
4
3
  import { domains } from './domains/index.js';
4
+ import { github } from './github/index.js';
5
5
  import { capture_deploy_help } from './help.js';
6
6
  import { cancel } from './inspect/cancel.js';
7
7
  import { deployments } from './inspect/deployments.js';
@@ -9,6 +9,7 @@ import { inspect } from './inspect/inspect.js';
9
9
  import { logs } from './inspect/logs.js';
10
10
  import { open_app } from './inspect/open.js';
11
11
  import { redeploy } from './inspect/redeploy.js';
12
+ import { run } from './inspect/run.js';
12
13
  import { status } from './inspect/status.js';
13
14
  import { traffic } from './inspect/traffic.js';
14
15
  import { trigger } from './inspect/trigger.js';
@@ -43,6 +44,7 @@ const deploy = {
43
44
  .command(waf)
44
45
  .command(logs)
45
46
  .command(status)
47
+ .command(run)
46
48
  .command(traffic)
47
49
  .command(usage)
48
50
  .command(quota)
@@ -0,0 +1,79 @@
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 { resolve_app_id } from '../resolve_app_id.js';
6
+ import { waitForTask } from './task_wait.js';
7
+
8
+ const run = {
9
+ command: 'run',
10
+ // Hidden until the WordPress blueprint is published (api `/task` is
11
+ // internal: blueprint apps and staff only). Then: the describe below.
12
+ // 'Run a one-off command with the image, variables and volumes of the live deployment'
13
+ describe: false,
14
+ builder: yargs => json_option(yargs)
15
+ .parserConfiguration({ 'populate--': true })
16
+ .option('app', {
17
+ alias: 'a',
18
+ type: 'string',
19
+ description: 'App id, name or slug (defaults to the linked app)'
20
+ })
21
+ .option('wait', {
22
+ type: 'boolean',
23
+ default: true,
24
+ description: 'Wait for the result and print its output (--no-wait to only queue it)'
25
+ })
26
+ .option('timeout', {
27
+ type: 'number',
28
+ description: 'Seconds before the task is stopped (default 900, max 3600)'
29
+ })
30
+ .example('$0 deploy run -a my-blog -- wp plugin list', 'wp-cli on a WordPress app')
31
+ .example('$0 deploy run -- node scripts/migrate.js', 'A script of your own app')
32
+ .showHelpOnFail(false),
33
+ handler: async (args) => {
34
+ const argv = (args['--'] ?? []).map(String);
35
+ if (argv.length === 0) {
36
+ throw new CliError('usage', 'Say what to run after --, e.g. faable deploy run -- wp plugin list');
37
+ }
38
+ const ctx = await requireApi();
39
+ const app_id = await resolve_app_id(args.app, ctx.appId, ctx.api);
40
+ const app = await ctx.api.getApp(app_id);
41
+ const queued = await ctx.api.createTask(app.team, {
42
+ app_id,
43
+ argv,
44
+ ...(args.timeout ? { timeout_s: args.timeout } : {})
45
+ });
46
+ if (!args.wait) {
47
+ if (args.json)
48
+ return print_json(queued);
49
+ log.info(`🧰 Queued ${queued.id} on ${app.name}.`);
50
+ return;
51
+ }
52
+ if (!args.json)
53
+ log.info(`🧰 ${argv.join(' ')} on ${app.name} (${queued.id})`);
54
+ const done = await waitForTask(() => ctx.api.getTask(queued.id, app.team), {
55
+ onStatus: s => {
56
+ if (!args.json && s === 'running')
57
+ log.info('… running');
58
+ }
59
+ });
60
+ if (args.json) {
61
+ print_json(done);
62
+ }
63
+ else {
64
+ if (done.output_tail)
65
+ process.stdout.write(done.output_tail.replace(/\n?$/, '\n'));
66
+ const how = done.status === 'succeeded'
67
+ ? '✅ Done'
68
+ : done.status === 'timed_out'
69
+ ? '⏱️ Timed out'
70
+ : `❌ ${done.reason ?? 'Failed'}`;
71
+ log.info(`${how}${done.exit_code != null ? ` (exit ${done.exit_code})` : ''}`);
72
+ }
73
+ // The command's own exit code, so scripts can branch on it.
74
+ if (done.status !== 'succeeded')
75
+ process.exitCode = done.exit_code || 1;
76
+ }
77
+ };
78
+
79
+ export { run };
@@ -0,0 +1,18 @@
1
+ const DONE = new Set(['succeeded', 'failed', 'timed_out', 'canceled']);
2
+ const POLL_MS = 3000;
3
+ /** Polls a task until it is done (or the caller's patience runs out). */
4
+ const waitForTask = async (get, opts = {}) => {
5
+ const sleep = opts.sleep ?? (ms => new Promise(r => setTimeout(r, ms)));
6
+ let last = '';
7
+ for (;;) {
8
+ const t = await get();
9
+ if (t.status !== last)
10
+ opts.onStatus?.(t.status);
11
+ last = t.status;
12
+ if (DONE.has(t.status))
13
+ return t;
14
+ await sleep(POLL_MS);
15
+ }
16
+ };
17
+
18
+ export { waitForTask };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faable/faable",
3
- "version": "4.23.1",
3
+ "version": "4.25.0",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Marc Pomar <marc@faable.com>",