hammerkit 2.1.0-next.19 → 2.1.0-next.20

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.
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.workerMain = exports.readInput = exports.ChangeQueue = exports.applyChanges = exports.isInside = exports.syncLine = exports.syncServices = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.relayStatus = exports.removeCommandsOfRun = exports.getRunnerCommands = exports.getLogEvent = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = void 0;
3
+ exports.workerMain = exports.readInput = exports.ChangeQueue = exports.applyChanges = exports.parseChanges = exports.syncLine = exports.syncServices = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.relayStatus = exports.removeCommandsOfRun = exports.getRunnerCommands = exports.getLogEvent = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = void 0;
4
4
  const readline_1 = require("readline");
5
- const promises_1 = require("fs/promises");
6
- const path_1 = require("path");
7
5
  const zod_1 = require("zod");
8
6
  const cli_1 = require("../cli");
9
7
  const get_file_context_1 = require("../file/get-file-context");
@@ -638,79 +636,59 @@ async function syncServices(workTree, up, changes) {
638
636
  }
639
637
  }
640
638
  exports.syncServices = syncServices;
641
- // The line `sync <json>` the server writes for a batch of changes: the folder the batch is
642
- // staged in (`staging`), which the batch folder must be inside to be removed after it
639
+ // The line `sync <json>` the server writes for a batch of changes: FileChanges, with its archive
640
+ // in base64
643
641
  exports.syncLine = zod_1.z
644
642
  .object({
645
- staging: zod_1.z.string(),
646
- changes: zod_1.z
647
- .object({
648
- dir: zod_1.z.string(),
649
- written: zod_1.z.array(zod_1.z.string()),
650
- deleted: zod_1.z.array(zod_1.z.string()),
651
- restart: zod_1.z.boolean(),
652
- })
653
- .strict(),
643
+ archive: zod_1.z.string(),
644
+ written: zod_1.z.array(zod_1.z.string()),
645
+ deleted: zod_1.z.array(zod_1.z.string()),
646
+ restart: zod_1.z.boolean(),
654
647
  })
655
648
  .strict();
656
- // The staging folder and the batch of a sync line, as far as they are named: what a refused line
657
- // still names, so its batch can be removed (see applyChanges)
658
- const batchOf = zod_1.z.object({ staging: zod_1.z.string(), changes: zod_1.z.object({ dir: zod_1.z.string() }) });
659
- // Whether `path` is a folder inside `root`, not `root` itself
660
- function isInside(root, path) {
661
- const rel = (0, path_1.relative)((0, path_1.resolve)(root), (0, path_1.resolve)(path));
662
- return rel !== '' && rel.split(path_1.sep)[0] !== '..' && !(0, path_1.isAbsolute)(rel);
663
- }
664
- exports.isInside = isInside;
665
- /**
666
- * Applies one batch of changes to the services that are up (`sync`). The batch folder goes
667
- * afterwards, whatever came of it, but only when it is inside the staging folder. A line that
668
- * does not parse is logged and nothing else is done.
669
- */
670
- async function applyChanges(request, sync, log) {
649
+ // The changes of a sync line, or undefined when the line does not match
650
+ function parseChanges(request) {
671
651
  let json;
672
652
  try {
673
653
  json = JSON.parse(request);
674
654
  }
675
655
  catch {
676
- json = undefined;
656
+ return undefined;
677
657
  }
678
658
  const parsed = exports.syncLine.safeParse(json);
679
659
  if (!parsed.success) {
660
+ return undefined;
661
+ }
662
+ return { ...parsed.data, archive: Buffer.from(parsed.data.archive, 'base64') };
663
+ }
664
+ exports.parseChanges = parseChanges;
665
+ /**
666
+ * Applies one batch of changes to the services that are up (`sync`). A line that does not parse
667
+ * is logged, and nothing else is done.
668
+ */
669
+ async function applyChanges(request, sync, log,
670
+ // told when the batch was not applied: the server then sends the full set again
671
+ failed = () => undefined) {
672
+ const changes = parseChanges(request);
673
+ if (!changes) {
680
674
  log('the changed files of the sandbox could not be read');
681
- // the batch is still the server's to see gone: a line that does not match names it loosely
682
- const named = batchOf.safeParse(json);
683
- if (named.success) {
684
- await removeBatch(named.data.staging, (0, path_1.dirname)(named.data.changes.dir), log);
685
- }
675
+ failed();
676
+ return;
677
+ }
678
+ if (!sync) {
679
+ log('no service of the run is up to take the changed files of the sandbox');
680
+ failed();
686
681
  return;
687
682
  }
688
- const { staging, changes } = parsed.data;
689
- const batch = (0, path_1.dirname)(changes.dir);
690
683
  try {
691
- if (!sync) {
692
- log('no service of the run is up to take the changed files of the sandbox');
693
- return;
694
- }
695
684
  await sync(changes);
696
685
  }
697
686
  catch (e) {
698
687
  log(`the changed files of the sandbox could not be synced: ${(0, log_1.getErrorMessage)(e)}`);
699
- }
700
- finally {
701
- await removeBatch(staging, batch, log);
688
+ failed();
702
689
  }
703
690
  }
704
691
  exports.applyChanges = applyChanges;
705
- // Removes a batch folder once it was applied or refused, when it is inside the staging folder
706
- // of its run. Outside, it is left where it is: it is not the server's to remove.
707
- async function removeBatch(staging, batch, log) {
708
- if (!isInside(staging, batch)) {
709
- log(`${batch} is not in the staging folder of the run, it is left where it is`);
710
- return;
711
- }
712
- await (0, promises_1.rm)(batch, { recursive: true, force: true }).catch((e) => log(`${batch} could not be removed: ${(0, log_1.getErrorMessage)(e)}`));
713
- }
714
692
  /**
715
693
  * The batches of changes of a run, applied one after another in the order they came. A batch
716
694
  * that fails does not stop the ones after it.
@@ -792,7 +770,7 @@ async function workerMain(stdin, stdout, processEnvs, cwd, abortCtrl) {
792
770
  // the changes of the sandbox, one batch after the other, for the services that are up
793
771
  let sync;
794
772
  const logLine = (text) => say({ kind: 'event', event: { type: 'log', task: 'hammerkit', stream: 'stderr', text } });
795
- const batches = new ChangeQueue((request) => applyChanges(request, sync, logLine), (e) => logLine(`the changed files of the sandbox could not be synced: ${(0, log_1.getErrorMessage)(e)}`));
773
+ const batches = new ChangeQueue((request) => applyChanges(request, sync, logLine, () => say({ kind: 'sync-failed' })), (e) => logLine(`the changed files of the sandbox could not be synced: ${(0, log_1.getErrorMessage)(e)}`));
796
774
  const reader = readInput(stdin, stop, (request) => {
797
775
  void batches.push(request);
798
776
  });