hammerkit 2.1.0-next.15 → 2.1.0-next.17

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.
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /**
3
+ * The commands of a run in the runner of a sandbox (see kubernetes/sandbox-runner.ts), as the
4
+ * process of the run and the registry of the server know them. A command runs apart from the
5
+ * process that started it, with its output and exit code in a directory of the sandbox's
6
+ * files, so a run that resumes after a restart of the server takes it up again.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.isRunnerCommandsPath = exports.createRunnerCommands = exports.MAX_OUTPUT_COMMANDS = exports.COMMAND_ID_PATTERN = exports.RUNNER_COMMANDS_DIRECTORY = void 0;
10
+ const path_1 = require("path");
11
+ // Where the directories of the commands are, relative to the root of the sandbox's files:
12
+ // hammerkit's run state, which changes, pushes, the files API and the sources of tasks leave out
13
+ exports.RUNNER_COMMANDS_DIRECTORY = '.hammerkit/commands';
14
+ // the name of the directory of a run's commands, and of each command in it
15
+ exports.COMMAND_ID_PATTERN = /^[0-9a-f]{24}$/;
16
+ // the commands of a run the registry keeps a record of; past it, the records of the commands
17
+ // that ended make way
18
+ exports.MAX_OUTPUT_COMMANDS = 1000;
19
+ function createRunnerCommands(options = {}) {
20
+ // a task of a run that resumes goes on with the execution it was at: the one in flight when
21
+ // the server stopped, or the last one, which ended
22
+ const executions = new Map();
23
+ for (const record of Object.values(options.resumed ?? {})) {
24
+ executions.set(record.task, Math.max(executions.get(record.task) ?? 0, record.execution));
25
+ }
26
+ return { ...options, executions, inUse: new Set() };
27
+ }
28
+ exports.createRunnerCommands = createRunnerCommands;
29
+ // Whether a path is in the directory of the commands of the sandbox whose files are at `root`:
30
+ // only that one, at the root of the files, not a folder of the same name deeper in them
31
+ function isRunnerCommandsPath(path, root) {
32
+ const directory = (0, path_1.join)(root, ...exports.RUNNER_COMMANDS_DIRECTORY.split('/'));
33
+ return path === directory || path.startsWith(`${directory}${path_1.sep}`);
34
+ }
35
+ exports.isRunnerCommandsPath = isRunnerCommandsPath;
36
+ //# sourceMappingURL=runner-commands.js.map
@@ -103,6 +103,10 @@ class Reconnecting {
103
103
  if ('response' in attempt) {
104
104
  await attempt.response.body?.cancel().catch(() => undefined);
105
105
  }
106
+ if (timing.windowMs <= 0) {
107
+ // no window: the call is not waited for, so nothing is said about it either
108
+ throw fail(reason);
109
+ }
106
110
  const window = (0, units_1.formatDuration)(timing.windowMs);
107
111
  this.deadline ?? (this.deadline = started + timing.windowMs);
108
112
  if (!this.away) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.downloadRunArtifact = exports.getRunArtifacts = exports.getRun = exports.followRun = exports.getMoveTarget = exports.lookupChecks = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.getServerConnection = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
3
+ exports.request = exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.downloadRunArtifact = exports.getRunArtifacts = exports.getRun = exports.followRun = exports.getMoveTarget = exports.lookupChecks = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.getServerConnection = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
4
4
  const path_1 = require("path");
5
5
  const token_source_1 = require("../remote/token-source");
6
6
  const reconnect_1 = require("./reconnect");
@@ -659,7 +659,12 @@ exports.quotePosix = quotePosix;
659
659
  function json(response) {
660
660
  return response.json();
661
661
  }
662
- async function call(connection, method, path, options = {}) {
662
+ /**
663
+ * The answer to a call, whatever its status. A call the server is away for is sent again as
664
+ * `repeat` allows, within the window of the connection; a token the server does not accept is
665
+ * renewed once and the call sent again.
666
+ */
667
+ async function request(connection, method, path, options = {}) {
663
668
  const url = new URL(`${connection.url.replace(/\/$/, '')}${path}`);
664
669
  for (const [key, value] of Object.entries(options.query ?? {})) {
665
670
  if (value !== undefined) {
@@ -675,11 +680,11 @@ async function call(connection, method, path, options = {}) {
675
680
  timing: options.reconnectMs !== undefined ? { ...timing, windowMs: options.reconnectMs } : timing,
676
681
  say: connection.say,
677
682
  signal: stop,
678
- fail: (message) => new SandboxClientError(message),
683
+ fail: options.fail ?? ((message) => new SandboxClientError(message)),
679
684
  });
680
685
  const attempt = async () => {
681
- const token = await connection.token();
682
- if (!(0, token_source_1.isToken)(token)) {
686
+ const token = 'token' in connection ? await connection.token() : undefined;
687
+ if (token !== undefined && !(0, token_source_1.isToken)(token)) {
683
688
  // a header refuses such a value and would quote it whole in its error
684
689
  throw new SandboxClientError('the token is not a single line of visible characters');
685
690
  }
@@ -688,7 +693,7 @@ async function call(connection, method, path, options = {}) {
688
693
  return fetch(url, {
689
694
  method,
690
695
  headers: {
691
- authorization: `Bearer ${token}`,
696
+ ...(token !== undefined ? { authorization: `Bearer ${token}` } : {}),
692
697
  ...(options.body !== undefined ? { 'content-type': 'application/json' } : {}),
693
698
  ...(options.raw !== undefined ? { 'content-type': 'application/octet-stream' } : {}),
694
699
  },
@@ -700,19 +705,27 @@ async function call(connection, method, path, options = {}) {
700
705
  error: e,
701
706
  message: stop?.aborted
702
707
  ? `stopped while asking ${shown}`
703
- : `unable to reach ${shown}: ${redact(fetchFailure(e), token)}`,
708
+ : `unable to reach ${shown}: ${token === undefined ? fetchFailure(e) : redact(fetchFailure(e), token)}`,
704
709
  }));
705
710
  };
706
711
  const repeat = options.repeat ?? (method === 'GET' || method === 'PUT' ? 'always' : 'unsent');
707
712
  let response = await reconnecting.send(attempt, repeat);
708
713
  // a new token is asked for within the window the call has left, not after it ran out
709
- if (response.status === 401 && connection.renew && !reconnecting.exhausted) {
714
+ if (response.status === 401 && 'token' in connection && connection.renew && !reconnecting.exhausted) {
710
715
  await connection.renew();
711
716
  response = await reconnecting.send(attempt, repeat);
712
717
  }
718
+ return response;
719
+ }
720
+ exports.request = request;
721
+ // The body of a call that answers 2xx (or a 404 it was asked to take as an answer); any other
722
+ // status is an error, which says what the server answered.
723
+ async function call(connection, method, path, options = {}) {
724
+ const response = await request(connection, method, path, options);
713
725
  if (response.ok || (options.missing && response.status === 404)) {
714
726
  return response;
715
727
  }
728
+ const shown = (0, reconnect_1.showUrl)(connection.url);
716
729
  let said = '';
717
730
  try {
718
731
  const body = await json(response);