File size: 2,831 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Client-side trigger script loading for cron create/edit commands.
import { createReadStream } from "node:fs";
import {
  readByteStreamWithLimit,
  type ReadByteStreamWithLimitOptions,
} from "@openclaw/media-core/read-byte-stream-with-limit";
import { CRON_JOB_SCRATCH_MAX_BYTES } from "../../cron/scratch-contract.js";
import { isErrno } from "../../infra/errno.js";
import { CronCliError } from "./cron-cli-error.js";

const MAX_CRON_TRIGGER_SCRIPT_BYTES = 65_536;

async function readCronInput(
  source: string,
  stdin: AsyncIterable<unknown> | undefined,
  options: ReadByteStreamWithLimitOptions,
): Promise<Buffer> {
  const stream = source === "-" ? (stdin ?? process.stdin) : createReadStream(source);
  try {
    return await readByteStreamWithLimit(stream, options);
  } catch (error) {
    // Filesystem read failures are operator input errors; byte-stream contract
    // violations and other unexpected exceptions keep their original identity.
    if (
      error instanceof Error &&
      isErrno(error) &&
      typeof error.errno === "number" &&
      typeof error.syscall === "string"
    ) {
      throw new CronCliError(error);
    }
    throw error;
  }
}

async function readScriptStream(
  source: string,
  stdin: AsyncIterable<unknown> | undefined,
  label: string,
): Promise<string> {
  const bytes = await readCronInput(source, stdin, {
    maxBytes: MAX_CRON_TRIGGER_SCRIPT_BYTES,
    onOverflow: () => new CronCliError(`${label} exceeds ${MAX_CRON_TRIGGER_SCRIPT_BYTES} bytes`),
  });
  return bytes.toString("utf8");
}

/** Reads a trigger script locally before sending the cron RPC. */
export async function readCronTriggerScript(
  source: string,
  deps?: {
    stdin?: AsyncIterable<unknown>;
  },
): Promise<string> {
  const raw = await readScriptStream(source, deps?.stdin, "Trigger script");
  const script = raw.trim();
  if (!script) {
    throw new CronCliError("Trigger script must not be empty");
  }
  return script;
}

/** Reads a script payload locally before sending the cron RPC. */
export async function readCronPayloadScript(
  source: string,
  deps?: { stdin?: AsyncIterable<unknown> },
): Promise<string> {
  const raw = await readScriptStream(source, deps?.stdin, "Script payload");
  const script = raw.trim();
  if (!script) {
    throw new CronCliError("Script payload must not be empty");
  }
  return script;
}

/** Reads exact scratch content locally; empty content is a meaningful value. */
export async function readCronScratchContent(
  source: string,
  deps?: { stdin?: AsyncIterable<unknown> },
): Promise<string> {
  const bytes = await readCronInput(source, deps?.stdin, {
    maxBytes: CRON_JOB_SCRATCH_MAX_BYTES,
    onOverflow: () => new CronCliError(`Cron scratch exceeds ${CRON_JOB_SCRATCH_MAX_BYTES} bytes`),
  });
  return bytes.toString("utf8");
}