bdy 1.25.41-dev → 1.25.42-stage

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.
Files changed (34) hide show
  1. package/distTs/package.json +1 -1
  2. package/distTs/src/api/client.js +59 -1
  3. package/distTs/src/command/agent/debug.js +22 -1
  4. package/distTs/src/command/agent/disable.js +21 -2
  5. package/distTs/src/command/agent/enable.js +21 -2
  6. package/distTs/src/command/agent/proxy/disable.js +16 -2
  7. package/distTs/src/command/agent/proxy/enable.js +16 -2
  8. package/distTs/src/command/agent/proxy/status.js +32 -13
  9. package/distTs/src/command/agent/remote.js +219 -0
  10. package/distTs/src/command/agent/status.js +78 -48
  11. package/distTs/src/command/agent/tags/get.js +37 -13
  12. package/distTs/src/command/agent/tags/set.js +30 -9
  13. package/distTs/src/command/agent/target/disable.js +16 -2
  14. package/distTs/src/command/agent/target/enable.js +16 -2
  15. package/distTs/src/command/agent/target/status.js +32 -13
  16. package/distTs/src/command/agent/tunnel/disable.js +16 -2
  17. package/distTs/src/command/agent/tunnel/enable.js +16 -2
  18. package/distTs/src/command/agent/tunnel/http.js +21 -0
  19. package/distTs/src/command/agent/tunnel/list.js +28 -6
  20. package/distTs/src/command/agent/tunnel/remove.js +29 -1
  21. package/distTs/src/command/agent/tunnel/start.js +23 -0
  22. package/distTs/src/command/agent/tunnel/status.js +50 -13
  23. package/distTs/src/command/agent/tunnel/tcp.js +21 -0
  24. package/distTs/src/command/agent/tunnel/tls.js +21 -0
  25. package/distTs/src/command/agent/update.js +17 -1
  26. package/distTs/src/command/agent/version.js +28 -5
  27. package/distTs/src/command/pre.js +2 -1
  28. package/distTs/src/format.js +6 -0
  29. package/distTs/src/output.js +16 -0
  30. package/distTs/src/texts.js +231 -31
  31. package/distTs/src/tunnel/output/interactive/tunnel.js +67 -8
  32. package/distTs/src/tunnel/output/noninteractive/tunnel.js +8 -0
  33. package/distTs/src/types/serverAgent.js +2 -0
  34. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.25.41-dev",
4
+ "version": "1.25.42-stage",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "homepage": "https://buddy.works/docs/cli",
@@ -216,7 +216,7 @@ class ApiClient {
216
216
  }
217
217
  throw new cliError_1.default('unauthorized', texts_1.ERR_REST_API_UNAUTHORIZED, texts_1.TXT_CLI_LOGIN_HINT);
218
218
  }
219
- if ([200, 201].includes(status)) {
219
+ if ([200, 201, 202].includes(status)) {
220
220
  if (parseResponseBody) {
221
221
  try {
222
222
  const b = await responseBody.json();
@@ -656,6 +656,64 @@ class ApiClient {
656
656
  q.environment = line.environment;
657
657
  return await this.getResourceByIdentifier(workspace, q);
658
658
  }
659
+ // Server agent methods (REST API; the local agent is reached through
660
+ // tunnel/api/agent.ts)
661
+ async getServerAgent(workspace, agentId) {
662
+ return await this.request({
663
+ method: 'GET',
664
+ path: `/workspaces/${encodeURIComponent(workspace)}/server-agents/${encodeURIComponent(agentId)}`,
665
+ parseResponseBody: true,
666
+ });
667
+ }
668
+ // Partial: an omitted field is left unchanged.
669
+ async updateServerAgent(workspace, agentId, body) {
670
+ return await this.request({
671
+ method: 'PATCH',
672
+ path: `/workspaces/${encodeURIComponent(workspace)}/server-agents/${encodeURIComponent(agentId)}`,
673
+ body,
674
+ parseResponseBody: true,
675
+ });
676
+ }
677
+ // The predefined SSH tunnel of the agent is not listed.
678
+ async getServerAgentTunnels(workspace, agentId) {
679
+ return await this.request({
680
+ method: 'GET',
681
+ path: `/workspaces/${encodeURIComponent(workspace)}/server-agents/${encodeURIComponent(agentId)}/tunnels`,
682
+ parseResponseBody: true,
683
+ });
684
+ }
685
+ async getServerAgentTunnel(workspace, agentId, tunnelId) {
686
+ return await this.request({
687
+ method: 'GET',
688
+ path: `/workspaces/${encodeURIComponent(workspace)}/server-agents/${encodeURIComponent(agentId)}/tunnels/${encodeURIComponent(tunnelId)}`,
689
+ parseResponseBody: true,
690
+ });
691
+ }
692
+ // Answers the new tunnel with its domain, subdomain and endpoint_url.
693
+ async createServerAgentTunnel(workspace, agentId, body) {
694
+ return await this.request({
695
+ method: 'POST',
696
+ path: `/workspaces/${encodeURIComponent(workspace)}/server-agents/${encodeURIComponent(agentId)}/tunnels`,
697
+ body,
698
+ parseResponseBody: true,
699
+ });
700
+ }
701
+ // The predefined SSH tunnel cannot be deleted (400).
702
+ async deleteServerAgentTunnel(workspace, agentId, tunnelId) {
703
+ return await this.request({
704
+ method: 'DELETE',
705
+ path: `/workspaces/${encodeURIComponent(workspace)}/server-agents/${encodeURIComponent(agentId)}/tunnels/${encodeURIComponent(tunnelId)}`,
706
+ });
707
+ }
708
+ // 202 with the agent once queued; 409 while another action is pending.
709
+ async createServerAgentAction(workspace, agentId, type) {
710
+ return await this.request({
711
+ method: 'POST',
712
+ path: `/workspaces/${encodeURIComponent(workspace)}/server-agents/${encodeURIComponent(agentId)}/actions`,
713
+ body: { type },
714
+ parseResponseBody: true,
715
+ });
716
+ }
659
717
  // Target methods
660
718
  targetScopeQuery(scope) {
661
719
  const query = {};
@@ -6,10 +6,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const utils_1 = require("../../utils");
7
7
  const texts_1 = require("../../texts");
8
8
  const output_1 = __importDefault(require("../../output"));
9
- const commandAgentDebug = (0, utils_1.newCommand)('debug', texts_1.DESC_COMMAND_AGENT_DEBUG, { requiredScopes: [] });
9
+ const cliError_1 = __importDefault(require("../../cliError"));
10
+ const remote_1 = require("./remote");
11
+ const commandAgentDebug = (0, utils_1.newCommand)('debug', texts_1.DESC_COMMAND_AGENT_DEBUG, { requiredScopes: [], examples: texts_1.EXAMPLE_AGENT_DEBUG });
10
12
  commandAgentDebug.argument('<on/off>');
11
13
  commandAgentDebug.option('--format <text|json>', texts_1.OPTION_FORMAT);
14
+ (0, remote_1.addRemoteAgentOptions)(commandAgentDebug);
12
15
  commandAgentDebug.action(async (debug, options) => {
16
+ // anything but on/off is a typo, not "off": with --agent it would queue an
17
+ // action that blocks the next one (409) until the agent picks it up
18
+ if (debug !== 'on' && debug !== 'off') {
19
+ output_1.default.exitError(new cliError_1.default('usage_error', (0, texts_1.ERR_AGENT_DEBUG_INVALID)(debug)));
20
+ }
21
+ // --agent: requested through the REST API; the agent switches its log level
22
+ // (and saves it) when it gets the action, so no rights are needed here
23
+ let requested = null;
24
+ try {
25
+ requested = await (0, remote_1.remoteAgentAction)(options, debug === 'on' ? 'DEBUG_ON' : 'DEBUG_OFF');
26
+ }
27
+ catch (err) {
28
+ output_1.default.exitError(err);
29
+ }
30
+ if (requested) {
31
+ output_1.default.exitResult(options.format === 'json', { debug: debug === 'on' }, debug === 'on' ? texts_1.TXT_AGENT_DEBUG_ON_REQUESTED : texts_1.TXT_AGENT_DEBUG_OFF_REQUESTED);
32
+ return;
33
+ }
13
34
  const AgentManager = require('../../agent/manager').default;
14
35
  const ApiAgent = require('../../tunnel/api/agent').default;
15
36
  const hasRights = await AgentManager.system.hasRightsToManage();
@@ -6,9 +6,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const utils_1 = require("../../utils");
7
7
  const texts_1 = require("../../texts");
8
8
  const output_1 = __importDefault(require("../../output"));
9
- const commandAgentDisable = (0, utils_1.newCommand)('disable', texts_1.DESC_COMMAND_AGENT_DISABLE, { requiredScopes: [] });
9
+ const remote_1 = require("./remote");
10
+ const commandAgentDisable = (0, utils_1.newCommand)('disable', texts_1.DESC_COMMAND_AGENT_DISABLE, { requiredScopes: [], examples: texts_1.EXAMPLE_AGENT_DISABLE });
10
11
  commandAgentDisable.option('--format <text|json>', texts_1.OPTION_FORMAT);
12
+ (0, remote_1.addRemoteAgentOptions)(commandAgentDisable);
11
13
  commandAgentDisable.action(async (options) => {
14
+ const json = options.format === 'json';
15
+ let enabled = null;
16
+ try {
17
+ const remote = await (0, remote_1.resolveRemoteAgent)(options);
18
+ if (remote) {
19
+ // the agent gets the change pushed, or on its next connection
20
+ const agent = await remote.client.updateServerAgent(remote.workspace, remote.agentId, { disabled: true });
21
+ enabled = !agent.disabled;
22
+ }
23
+ }
24
+ catch (err) {
25
+ output_1.default.exitError(err);
26
+ }
27
+ if (enabled !== null) {
28
+ output_1.default.exitResult(json, { enabled }, texts_1.TXT_AGENT_REMOTE_DISABLED);
29
+ return;
30
+ }
12
31
  const ApiAgent = require('../../tunnel/api/agent').default;
13
32
  if (!commandAgentDisable.agentInstalled) {
14
33
  output_1.default.exitError(texts_1.ERR_AGENT_NOT_INSTALLED);
@@ -19,7 +38,7 @@ commandAgentDisable.action(async (options) => {
19
38
  try {
20
39
  const api = new ApiAgent(commandAgentDisable.agentPort || 0);
21
40
  await api.disableAgent();
22
- output_1.default.exitResult(options.format === 'json', { enabled: false }, texts_1.TXT_AGENT_STOPPED);
41
+ output_1.default.exitResult(json, { enabled: false }, texts_1.TXT_AGENT_STOPPED);
23
42
  }
24
43
  catch (err) {
25
44
  output_1.default.exitError(err);
@@ -6,9 +6,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const utils_1 = require("../../utils");
7
7
  const texts_1 = require("../../texts");
8
8
  const output_1 = __importDefault(require("../../output"));
9
- const commandAgentEnable = (0, utils_1.newCommand)('enable', texts_1.DESC_COMMAND_AGENT_ENABLE, { requiredScopes: [] });
9
+ const remote_1 = require("./remote");
10
+ const commandAgentEnable = (0, utils_1.newCommand)('enable', texts_1.DESC_COMMAND_AGENT_ENABLE, { requiredScopes: [], examples: texts_1.EXAMPLE_AGENT_ENABLE });
10
11
  commandAgentEnable.option('--format <text|json>', texts_1.OPTION_FORMAT);
12
+ (0, remote_1.addRemoteAgentOptions)(commandAgentEnable);
11
13
  commandAgentEnable.action(async (options) => {
14
+ const json = options.format === 'json';
15
+ let enabled = null;
16
+ try {
17
+ const remote = await (0, remote_1.resolveRemoteAgent)(options);
18
+ if (remote) {
19
+ // the agent gets the change pushed, or on its next connection
20
+ const agent = await remote.client.updateServerAgent(remote.workspace, remote.agentId, { disabled: false });
21
+ enabled = !agent.disabled;
22
+ }
23
+ }
24
+ catch (err) {
25
+ output_1.default.exitError(err);
26
+ }
27
+ if (enabled !== null) {
28
+ output_1.default.exitResult(json, { enabled }, texts_1.TXT_AGENT_REMOTE_ENABLED);
29
+ return;
30
+ }
12
31
  const ApiAgent = require('../../tunnel/api/agent').default;
13
32
  if (!commandAgentEnable.agentInstalled) {
14
33
  output_1.default.exitError(texts_1.ERR_AGENT_NOT_INSTALLED);
@@ -19,7 +38,7 @@ commandAgentEnable.action(async (options) => {
19
38
  try {
20
39
  const api = new ApiAgent(commandAgentEnable.agentPort || 0);
21
40
  await api.enableAgent();
22
- output_1.default.exitResult(options.format === 'json', { enabled: true }, texts_1.TXT_AGENT_STARTED);
41
+ output_1.default.exitResult(json, { enabled: true }, texts_1.TXT_AGENT_STARTED);
23
42
  }
24
43
  catch (err) {
25
44
  output_1.default.exitError(err);
@@ -6,9 +6,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const utils_1 = require("../../../utils");
7
7
  const texts_1 = require("../../../texts");
8
8
  const output_1 = __importDefault(require("../../../output"));
9
- const commandAgentProxyDisable = (0, utils_1.newCommand)('disable', texts_1.DESC_COMMAND_AGENT_PROXY_DISABLE, { requiredScopes: [] });
9
+ const remote_1 = require("../remote");
10
+ const commandAgentProxyDisable = (0, utils_1.newCommand)('disable', texts_1.DESC_COMMAND_AGENT_PROXY_DISABLE, { requiredScopes: [], examples: texts_1.EXAMPLE_AGENT_PROXY_DISABLE });
10
11
  commandAgentProxyDisable.option('--format <text|json>', texts_1.OPTION_FORMAT);
12
+ (0, remote_1.addRemoteAgentOptions)(commandAgentProxyDisable);
11
13
  commandAgentProxyDisable.action(async (options) => {
14
+ const json = options.format === 'json';
15
+ let remote = null;
16
+ try {
17
+ remote = await (0, remote_1.remoteAgentFlag)(options, 'proxy', false);
18
+ }
19
+ catch (err) {
20
+ output_1.default.exitError(err);
21
+ }
22
+ if (remote !== null) {
23
+ output_1.default.exitResult(json, { proxy: remote }, texts_1.TXT_AGENT_PROXY_DISABLED);
24
+ return;
25
+ }
12
26
  const ApiAgent = require('../../../tunnel/api/agent').default;
13
27
  if (!commandAgentProxyDisable.agentInstalled) {
14
28
  output_1.default.exitError(texts_1.ERR_AGENT_NOT_INSTALLED);
@@ -19,7 +33,7 @@ commandAgentProxyDisable.action(async (options) => {
19
33
  try {
20
34
  const api = new ApiAgent(commandAgentProxyDisable.agentPort || 0);
21
35
  await api.disableAgentProxy();
22
- output_1.default.exitResult(options.format === 'json', { proxy: false }, texts_1.TXT_AGENT_PROXY_DISABLED);
36
+ output_1.default.exitResult(json, { proxy: false }, texts_1.TXT_AGENT_PROXY_DISABLED);
23
37
  }
24
38
  catch (err) {
25
39
  output_1.default.exitError(err);
@@ -6,9 +6,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const utils_1 = require("../../../utils");
7
7
  const texts_1 = require("../../../texts");
8
8
  const output_1 = __importDefault(require("../../../output"));
9
- const commandAgentProxyEnable = (0, utils_1.newCommand)('enable', texts_1.DESC_COMMAND_AGENT_PROXY_ENABLE, { requiredScopes: [] });
9
+ const remote_1 = require("../remote");
10
+ const commandAgentProxyEnable = (0, utils_1.newCommand)('enable', texts_1.DESC_COMMAND_AGENT_PROXY_ENABLE, { requiredScopes: [], examples: texts_1.EXAMPLE_AGENT_PROXY_ENABLE });
10
11
  commandAgentProxyEnable.option('--format <text|json>', texts_1.OPTION_FORMAT);
12
+ (0, remote_1.addRemoteAgentOptions)(commandAgentProxyEnable);
11
13
  commandAgentProxyEnable.action(async (options) => {
14
+ const json = options.format === 'json';
15
+ let remote = null;
16
+ try {
17
+ remote = await (0, remote_1.remoteAgentFlag)(options, 'proxy', true);
18
+ }
19
+ catch (err) {
20
+ output_1.default.exitError(err);
21
+ }
22
+ if (remote !== null) {
23
+ output_1.default.exitResult(json, { proxy: remote }, texts_1.TXT_AGENT_PROXY_ENABLED);
24
+ return;
25
+ }
12
26
  const ApiAgent = require('../../../tunnel/api/agent').default;
13
27
  if (!commandAgentProxyEnable.agentInstalled) {
14
28
  output_1.default.exitError(texts_1.ERR_AGENT_NOT_INSTALLED);
@@ -19,7 +33,7 @@ commandAgentProxyEnable.action(async (options) => {
19
33
  try {
20
34
  const api = new ApiAgent(commandAgentProxyEnable.agentPort || 0);
21
35
  await api.enableAgentProxy();
22
- output_1.default.exitResult(options.format === 'json', { proxy: true }, texts_1.TXT_AGENT_PROXY_ENABLED);
36
+ output_1.default.exitResult(json, { proxy: true }, texts_1.TXT_AGENT_PROXY_ENABLED);
23
37
  }
24
38
  catch (err) {
25
39
  output_1.default.exitError(err);
@@ -6,9 +6,38 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const utils_1 = require("../../../utils");
7
7
  const texts_1 = require("../../../texts");
8
8
  const output_1 = __importDefault(require("../../../output"));
9
+ const remote_1 = require("../remote");
9
10
  const commandAgentProxyStatus = (0, utils_1.newCommand)('status', texts_1.DESC_COMMAND_AGENT_PROXY_STATUS, { requiredScopes: [], examples: texts_1.EXAMPLE_AGENT_PROXY_STATUS });
10
11
  commandAgentProxyStatus.option('--format <text|json>', texts_1.OPTION_FORMAT);
12
+ (0, remote_1.addRemoteAgentOptions)(commandAgentProxyStatus);
13
+ // a disabled proxy is a non-zero exit, in text and JSON alike
14
+ const output = (data, json) => {
15
+ if (json) {
16
+ output_1.default.json(data);
17
+ if (!data.proxy)
18
+ output_1.default.exitError(texts_1.TXT_AGENT_PROXY_DISABLED);
19
+ output_1.default.exitNormal();
20
+ }
21
+ if (data.proxy) {
22
+ output_1.default.exitSuccess(texts_1.TXT_AGENT_PROXY_ENABLED);
23
+ }
24
+ else {
25
+ output_1.default.exitError(texts_1.TXT_AGENT_PROXY_DISABLED);
26
+ }
27
+ };
11
28
  commandAgentProxyStatus.action(async (options) => {
29
+ const json = options.format === 'json';
30
+ let remote = null;
31
+ try {
32
+ remote = await (0, remote_1.remoteAgentFlag)(options, 'proxy');
33
+ }
34
+ catch (err) {
35
+ output_1.default.exitError(err);
36
+ }
37
+ if (remote !== null) {
38
+ output({ proxy: remote }, json);
39
+ return;
40
+ }
12
41
  const ApiAgent = require('../../../tunnel/api/agent').default;
13
42
  if (!commandAgentProxyStatus.agentInstalled) {
14
43
  output_1.default.exitError(texts_1.ERR_AGENT_NOT_INSTALLED);
@@ -16,24 +45,14 @@ commandAgentProxyStatus.action(async (options) => {
16
45
  if (!commandAgentProxyStatus.agentStatus) {
17
46
  output_1.default.exitError(texts_1.ERR_AGENT_NOT_RUNNING);
18
47
  }
48
+ let data;
19
49
  try {
20
50
  const api = new ApiAgent(commandAgentProxyStatus.agentPort || 0);
21
- const data = await api.fetchAgentProxy();
22
- if (options.format === 'json') {
23
- output_1.default.json(data);
24
- if (!data.proxy)
25
- output_1.default.exitError(texts_1.TXT_AGENT_PROXY_DISABLED);
26
- output_1.default.exitNormal();
27
- }
28
- if (data.proxy) {
29
- output_1.default.exitSuccess(texts_1.TXT_AGENT_PROXY_ENABLED);
30
- }
31
- else {
32
- output_1.default.exitError(texts_1.TXT_AGENT_PROXY_DISABLED);
33
- }
51
+ data = await api.fetchAgentProxy();
34
52
  }
35
53
  catch (err) {
36
54
  output_1.default.exitError(err);
37
55
  }
56
+ output(data, json);
38
57
  });
39
58
  exports.default = commandAgentProxyStatus;
@@ -0,0 +1,219 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.remoteAgentAction = exports.remoteTunnelCreate = exports.remoteTunnelView = exports.remoteTunnelStatus = exports.remoteTunnelListItem = exports.remoteAgentFlag = exports.remoteAgentStatus = exports.remoteAgentApiStatus = exports.resolveRemoteAgent = exports.addRemoteAgentOptions = void 0;
7
+ const texts_1 = require("../../texts");
8
+ const input_1 = __importDefault(require("../../input"));
9
+ const cliError_1 = __importDefault(require("../../cliError"));
10
+ const tunnel_1 = require("../../types/tunnel");
11
+ // The options of the remote mode. The tunnel commands already use -w for
12
+ // --whitelist, so there the workspace has no short flag.
13
+ const addRemoteAgentOptions = (command, workspaceShort = true) => {
14
+ command.option('--agent <identifier>', texts_1.OPTION_AGENT_REMOTE);
15
+ command.option(workspaceShort ? '-w, --workspace <domain>' : '--workspace <domain>', texts_1.OPTION_REST_API_WORKSPACE);
16
+ command.option('-p, --project <name>', texts_1.OPTION_REST_API_PROJECT);
17
+ };
18
+ exports.addRemoteAgentOptions = addRemoteAgentOptions;
19
+ const notFound = () => new cliError_1.default('not_found', texts_1.ERR_AGENT_NOT_FOUND, texts_1.HINT_AGENT_REMOTE);
20
+ // null without --agent: the command goes to the local agent as before.
21
+ //
22
+ // --agent takes the agent's identifier or its hash id. With a project
23
+ // (--project, BUDDY_PROJECT, linked project) /identifiers looks among the
24
+ // agents of the project and of the workspace both (the backend's scope for
25
+ // agents, unlike the exact scope of the list), so a workspace agent is found
26
+ // from a linked directory too. /identifiers resolves an identifier or a
27
+ // numeric id only, so a value it does not know is tried as a hash id.
28
+ const resolveRemoteAgent = async (options) => {
29
+ if (!options.agent)
30
+ return null;
31
+ const workspace = input_1.default.restApiWorkspace(options.workspace);
32
+ const project = input_1.default.restApiProject(options.project, true);
33
+ const client = input_1.default.restApiTokenClient();
34
+ const resolved = await client.getResourceByIdentifier(workspace, {
35
+ project: project || undefined,
36
+ server_agent: options.agent,
37
+ });
38
+ if (resolved.server_agent_id) {
39
+ return { client, workspace, agentId: resolved.server_agent_id };
40
+ }
41
+ try {
42
+ const agent = await client.getServerAgent(workspace, options.agent);
43
+ return { client, workspace, agentId: agent.id };
44
+ }
45
+ catch (err) {
46
+ // a value that is not a hash id at all answers 400
47
+ if (err instanceof cliError_1.default && ['not_found', 'usage_error'].includes(err.code)) {
48
+ throw notFound();
49
+ }
50
+ throw err;
51
+ }
52
+ };
53
+ exports.resolveRemoteAgent = resolveRemoteAgent;
54
+ // The local agent's `/status` answer rebuilt from the REST view, so the JSON
55
+ // of `agent version` and `agent status` keeps one shape. The connection detail
56
+ // of the local status (fetch failed, initializing) is not reported over REST.
57
+ const remoteAgentApiStatus = (agent) => ({
58
+ id: agent.id,
59
+ status: agent.disabled ? tunnel_1.TUNNEL_AGENT_STATUS.DISABLED : tunnel_1.TUNNEL_AGENT_STATUS.ENABLED,
60
+ version: agent.version || null,
61
+ updating: agent.action === tunnel_1.TUNNEL_AGENT_ACTION.UPDATE ||
62
+ agent.action === tunnel_1.TUNNEL_AGENT_UPDATE_STATUS.STARTED,
63
+ enabled: !agent.disabled,
64
+ target: !!agent.target,
65
+ tunneling: !!agent.tunneling,
66
+ proxy: !!agent.proxy,
67
+ });
68
+ exports.remoteAgentApiStatus = remoteAgentApiStatus;
69
+ // `agent status`: the API status plus what the local command adds about the
70
+ // installation - here only what the REST view knows.
71
+ const remoteAgentStatus = (agent) => ({
72
+ ...(0, exports.remoteAgentApiStatus)(agent),
73
+ installed: true,
74
+ standalone: !agent.service,
75
+ running: !!agent.active,
76
+ orphaned_config: null,
77
+ });
78
+ exports.remoteAgentStatus = remoteAgentStatus;
79
+ // `agent target|proxy|tunnel ...` with --agent: reads the flag (no value) or
80
+ // sets it with a PATCH, which the backend pushes to the agent, and returns the
81
+ // stored state. null without --agent.
82
+ const remoteAgentFlag = async (options, flag, value) => {
83
+ const remote = await (0, exports.resolveRemoteAgent)(options);
84
+ if (!remote)
85
+ return null;
86
+ const agent = value === undefined
87
+ ? await remote.client.getServerAgent(remote.workspace, remote.agentId)
88
+ : await remote.client.updateServerAgent(remote.workspace, remote.agentId, {
89
+ [flag]: value,
90
+ });
91
+ return !!agent[flag];
92
+ };
93
+ exports.remoteAgentFlag = remoteAgentFlag;
94
+ // A latency the agent has not reported is unreachable, as the local agent says
95
+ const latency = (value) => value === undefined || value === null ? -1 : value;
96
+ // `agent tunnel list --agent`: one item of the local agent's `/tunnels`
97
+ // answer rebuilt from the REST view (the SSH tunnel is left out by both).
98
+ const remoteTunnelListItem = (tunnel) => ({
99
+ id: tunnel.id,
100
+ type: tunnel.type,
101
+ target: tunnel.endpoint,
102
+ domain: tunnel.domain,
103
+ subdomain: tunnel.subdomain,
104
+ serve: tunnel.http?.serve_path,
105
+ status: tunnel.active ? tunnel_1.TUNNEL_STATUS.OPEN : tunnel_1.TUNNEL_STATUS.CLOSED,
106
+ region: tunnel.region,
107
+ sshId: tunnel.ssh?.ssh_id,
108
+ sshForwardPort: tunnel.ssh?.forward_port,
109
+ regionLatency: latency(tunnel.region_latency),
110
+ targetLatency: latency(tunnel.target_latency),
111
+ });
112
+ exports.remoteTunnelListItem = remoteTunnelListItem;
113
+ // `agent tunnel status <id> --agent`: the status message the local agent
114
+ // streams for a tunnel, rebuilt from the REST view. Left out: `connections`
115
+ // (the agent's open connections - the REST view has only their count) and
116
+ // `dns` (the local check of the entry host).
117
+ const remoteTunnelStatus = (tunnel) => ({
118
+ regionLatency: { latency: latency(tunnel.region_latency) },
119
+ targetLatency: { latency: latency(tunnel.target_latency) },
120
+ identify: tunnel.http_identified ? { type: tunnel.http_identified } : null,
121
+ totalConnections: tunnel.total_connections ?? 0,
122
+ serve: tunnel.http?.serve_path,
123
+ log: !!tunnel.http?.log_requests,
124
+ status: tunnel.active ? tunnel_1.TUNNEL_STATUS.OPEN : tunnel_1.TUNNEL_STATUS.CLOSED,
125
+ region: tunnel.region,
126
+ type: tunnel.type,
127
+ target: tunnel.endpoint,
128
+ terminate: tunnel.tls?.terminate_at,
129
+ id: tunnel.id,
130
+ subdomain: tunnel.subdomain,
131
+ domain: tunnel.domain,
132
+ sshId: tunnel.ssh?.ssh_id,
133
+ sshForwardPort: tunnel.ssh?.forward_port,
134
+ });
135
+ exports.remoteTunnelStatus = remoteTunnelStatus;
136
+ // What the tunnel views read on top of the status message: the note and the
137
+ // open connections, of which the REST view knows the count only.
138
+ const remoteTunnelView = (tunnel) => {
139
+ const connections = {};
140
+ for (let i = 0; i < (tunnel.current_connections || 0); i += 1) {
141
+ connections[String(i)] = true;
142
+ }
143
+ return { ...(0, exports.remoteTunnelStatus)(tunnel), note: tunnel.note, connections };
144
+ };
145
+ exports.remoteTunnelView = remoteTunnelView;
146
+ // The CLI keeps a fraction, the API a whole percentage (an integer 0-100): a
147
+ // threshold with a fraction of a percent is refused rather than rounded into
148
+ // another value than the one the local agent would use.
149
+ const remoteCircuitBreaker = (fraction) => {
150
+ if (fraction === undefined || fraction === null)
151
+ return undefined;
152
+ const percent = fraction * 100;
153
+ const whole = Math.round(percent);
154
+ // the float product of a whole percentage is off by an epsilon (0.07 * 100)
155
+ if (Math.abs(percent - whole) > 1e-9) {
156
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_CB_THRESHOLD_NOT_INTEGER_REMOTE)(parseFloat(percent.toFixed(6))));
157
+ }
158
+ return whole;
159
+ };
160
+ const defined = (body) => {
161
+ Object.keys(body).forEach((key) => body[key] === undefined && delete body[key]);
162
+ return body;
163
+ };
164
+ // A tunnel definition (`Cfg.prepareTunnel`: the options of `agent tunnel
165
+ // tcp|tls|http`, or a tunnel of the config for `agent tunnel start`) as the
166
+ // body of POST /server-agents/{id}/tunnels. What the definition leaves out the
167
+ // backend fills with the agent's defaults (its region, timeout 30, no
168
+ // whitelist): the config defaults of this machine are no business of another.
169
+ const remoteTunnelCreate = (tunnel) => {
170
+ const body = defined({
171
+ type: tunnel.type,
172
+ endpoint: tunnel.target,
173
+ name: tunnel.name,
174
+ region: tunnel.region,
175
+ whitelist: tunnel.whitelist,
176
+ timeout: tunnel.timeout,
177
+ note: tunnel.note,
178
+ agent_note: tunnel.agentNote,
179
+ });
180
+ if (tunnel.type === tunnel_1.TUNNEL_TYPE.HTTP) {
181
+ body.http = defined({
182
+ auth_type: tunnel.authType,
183
+ login: tunnel.login,
184
+ password: tunnel.password,
185
+ tls_ca: tunnel.ca,
186
+ verify_certificate: tunnel.verify,
187
+ compression: tunnel.compression,
188
+ http2: tunnel.http2,
189
+ log_requests: tunnel.log,
190
+ request_headers: tunnel.headers,
191
+ response_headers: tunnel.responseHeaders,
192
+ whitelist_user_agents: tunnel.useragents,
193
+ rewrite_host_header: tunnel.host,
194
+ serve_path: tunnel.serve,
195
+ circuit_breaker: remoteCircuitBreaker(tunnel.circuitBreaker),
196
+ });
197
+ }
198
+ else if (tunnel.type === tunnel_1.TUNNEL_TYPE.TLS) {
199
+ body.tls = defined({
200
+ terminate_at: tunnel.terminate,
201
+ private_key: tunnel.key,
202
+ certificate: tunnel.cert,
203
+ ca_certificate: tunnel.ca,
204
+ });
205
+ }
206
+ return body;
207
+ };
208
+ exports.remoteTunnelCreate = remoteTunnelCreate;
209
+ // `agent update` / `agent debug` with --agent: the action is queued on the
210
+ // agent and done when the agent gets it - nothing is awaited. true once
211
+ // requested, null without --agent.
212
+ const remoteAgentAction = async (options, type) => {
213
+ const remote = await (0, exports.resolveRemoteAgent)(options);
214
+ if (!remote)
215
+ return null;
216
+ await remote.client.createServerAgentAction(remote.workspace, remote.agentId, type);
217
+ return true;
218
+ };
219
+ exports.remoteAgentAction = remoteAgentAction;