@vite-hub/schedule 0.4.1 → 0.5.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.
Files changed (44) hide show
  1. package/README.md +12 -7
  2. package/dist/{console-wEhr0LWn.d.ts → console-DueeEBcM.d.ts} +2 -0
  3. package/dist/cron-C1l3TquN.js +70 -0
  4. package/dist/definition.js +1 -2
  5. package/dist/{due-sOJvF_6Z.js → due-n120-_nz.js} +22 -7
  6. package/dist/durability.d.ts +2 -2
  7. package/dist/durability.js +5 -5
  8. package/dist/{error-diagnostics-CQ4BUqRe.js → error-diagnostics-C0KyI408.js} +5 -1
  9. package/dist/{error-diagnostics-BBtYYl8j.js → error-diagnostics-DTmP6baC.js} +1 -5
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.js +3 -4
  12. package/dist/{started-name-ruCM1EtQ.js → inspect-DTfAo5gS.js} +1 -5
  13. package/dist/{operation-definition-nmRu_O3V.d.ts → operation-definition-CI2PJGqu.d.ts} +8 -2
  14. package/dist/{operations-DqwmJ8MN.js → operations-DlrK9mlH.js} +6 -9
  15. package/dist/operations.d.ts +1 -1
  16. package/dist/operations.js +5 -5
  17. package/dist/os.d.ts +0 -1
  18. package/dist/os.js +12 -84
  19. package/dist/out-BbXG-yVX.js +21755 -0
  20. package/dist/rolldown-runtime-CMFfr-1z.js +26 -0
  21. package/dist/runtime/console.d.ts +1 -1
  22. package/dist/runtime/console.js +2 -2
  23. package/dist/runtime/process.js +24 -45
  24. package/dist/runtime/state.d.ts +1 -1
  25. package/dist/runtime/state.js +1 -1
  26. package/dist/runtime/static.d.ts +2 -73
  27. package/dist/runtime/static.js +3 -2
  28. package/dist/runtime.d.ts +3 -3
  29. package/dist/runtime.js +4 -4
  30. package/dist/{started-B0pc3JY0.js → started-BWWr7Ufw.js} +3 -4
  31. package/dist/{started-Bk2Icknk.js → started-BXdWMdji.js} +24 -6
  32. package/dist/{started-errors-DEmYH-vp.js → started-errors-BMg04lL3.js} +45 -5
  33. package/dist/started-name-DudQ0fsR.js +5 -0
  34. package/dist/{state-D4SZH6zk.js → state-BjWnxyhv.js} +70 -117
  35. package/dist/{state-CPd10SXq.d.ts → state-CJKAhskJ.d.ts} +2 -1
  36. package/dist/static-BgoOCoe5.js +610 -0
  37. package/dist/static-CcBWx1kU.d.ts +96 -0
  38. package/dist/{store-DeMojnXM.d.ts → store-CZHZfttA.d.ts} +2 -6
  39. package/dist/vite.d.ts +5 -3
  40. package/dist/vite.js +2251 -130
  41. package/package.json +4 -4
  42. package/dist/invariant-DolT9EEx.js +0 -22
  43. package/dist/static-7JCga3Iu.js +0 -281
  44. package/dist/wait-until-Dy4Bn3ZN.js +0 -112
package/dist/os.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { t as isIanaTimeZone } from "./time-zone-SBldrUgt.js";
2
2
  import { t as isRecord } from "./object-Da1zyPCR.js";
3
- import { n as invariantError } from "./invariant-DolT9EEx.js";
4
- import { r as timeZoneOffsetMinutes } from "./due-sOJvF_6Z.js";
3
+ import { i as timeZoneOffsetMinutes } from "./due-n120-_nz.js";
4
+ import { a as parseCronFields, c as timesOfDay, i as isWeekdayRestricted, n as isDayRestricted, o as range, r as isMonthRestricted, s as runsEveryDay, t as arithmeticStep, u as invariantError } from "./cron-C1l3TquN.js";
5
5
  import { defineErrors } from "@vite-hub/runtime";
6
- import { parseCronExpression } from "cron-schedule";
7
6
  import { homedir, userInfo } from "node:os";
8
7
  import { basename, dirname, join, posix, win32 } from "node:path";
9
8
  import { mkdir, readFile, realpath, rm, rmdir, writeFile } from "node:fs/promises";
@@ -113,53 +112,6 @@ function createNodeHost() {
113
112
  };
114
113
  }
115
114
  //#endregion
116
- //#region src/os/cron.ts
117
- const allMonths = range(1, 12);
118
- const allWeekdays = range(0, 6);
119
- function range(first, last) {
120
- return Array.from({ length: last - first + 1 }, (_, index) => first + index);
121
- }
122
- /** Reads a five-field cron. Returns `undefined` for anything else. */
123
- function parseCronFields(cron) {
124
- if (cron.trim() !== cron || cron.split(/\s+/).length !== 5) return void 0;
125
- try {
126
- const parsed = parseCronExpression(cron);
127
- const sorted = (values) => [...new Set(values)].sort((left, right) => left - right);
128
- return {
129
- minutes: sorted(parsed.minutes),
130
- hours: sorted(parsed.hours),
131
- days: sorted(parsed.days),
132
- months: sorted(parsed.months.map((month) => month + 1)),
133
- weekdays: sorted(parsed.weekdays.map((weekday) => weekday % 7))
134
- };
135
- } catch {
136
- return;
137
- }
138
- }
139
- function isDayRestricted(fields) {
140
- return fields.days.length !== 31;
141
- }
142
- function isWeekdayRestricted(fields) {
143
- return fields.weekdays.length !== allWeekdays.length;
144
- }
145
- function isMonthRestricted(fields) {
146
- return fields.months.length !== allMonths.length;
147
- }
148
- /** True when every day field holds every value, so the cron runs on every day. */
149
- function runsEveryDay(fields) {
150
- return !isDayRestricted(fields) && !isWeekdayRestricted(fields) && !isMonthRestricted(fields);
151
- }
152
- /** Minutes of the day when the cron runs, sorted. */
153
- function timesOfDay(fields) {
154
- return fields.hours.flatMap((hour) => fields.minutes.map((minute) => hour * 60 + minute));
155
- }
156
- /** The step of a sorted list of two or more values with equal gaps, else `undefined`. One value has step 0. */
157
- function arithmeticStep(values) {
158
- if (values.length < 2) return 0;
159
- const step = values[1] - values[0];
160
- return values.every((value, index) => index === 0 || value - values[index - 1] === step) ? step : void 0;
161
- }
162
- //#endregion
163
115
  //#region src/os/errors.ts
164
116
  const platformNames = {
165
117
  darwin: "launchd",
@@ -220,7 +172,6 @@ const sampledDays = 732;
220
172
  function modulo(value, size) {
221
173
  return (value % size + size) % size;
222
174
  }
223
- /** The minutes, from `from` to `to`, where the offset of `timeZone` changes. It assumes at most one change a day. */
224
175
  function offsetChanges(timeZone, from, to) {
225
176
  const offset = (time) => timeZoneOffsetMinutes(new Date(time), timeZone);
226
177
  const changes = [];
@@ -268,7 +219,6 @@ function clockChanges(timeZone, from) {
268
219
  }
269
220
  return changes;
270
221
  }
271
- /** Every value of `offset(timeZone) - offset(machineTimeZone)`, in minutes, in the two years from `from`. */
272
222
  function offsetDifferences(timeZone, machineTimeZone, from) {
273
223
  const start = Math.floor(from.getTime() / minuteMs) * minuteMs;
274
224
  const end = start + sampledDays * dayMs$1;
@@ -279,7 +229,6 @@ function offsetDifferences(timeZone, machineTimeZone, from) {
279
229
  ];
280
230
  return [...new Set(instants.map((time) => timeZoneOffsetMinutes(new Date(time), timeZone) - timeZoneOffsetMinutes(new Date(time), machineTimeZone)))].sort((left, right) => left - right);
281
231
  }
282
- /** Hours and minutes whose product is exactly `times` (minutes of the day), else `undefined`. */
283
232
  function asProduct(times) {
284
233
  const unique = new Set(times);
285
234
  const hours = [...new Set([...unique].map((time) => Math.floor(time / 60)))].sort((left, right) => left - right);
@@ -289,7 +238,6 @@ function asProduct(times) {
289
238
  minutes
290
239
  } : void 0;
291
240
  }
292
- /** The cron moved by a constant `difference`: the machine time is the Schedule time minus `difference` minutes. */
293
241
  function shiftFields(fields, difference) {
294
242
  const shifted = timesOfDay(fields).map((time) => time - difference);
295
243
  const product = asProduct(shifted.map((time) => modulo(time, dayMinutes)));
@@ -441,7 +389,6 @@ function parseLaunchdPlist(plist) {
441
389
  ...logFile === void 0 ? {} : { logFile: unxml(logFile) }
442
390
  };
443
391
  }
444
- /** Reads `launchctl print` output: the `state` and `last exit code` lines of the service, one tab deep. */
445
392
  function parseLaunchctlPrint(output) {
446
393
  const state = output.match(/^\tstate = (.+)$/m)?.[1]?.trim();
447
394
  const lastExit = output.match(/^\tlast exit code = (-?\d+)/m)?.[1];
@@ -451,7 +398,6 @@ function parseLaunchctlPrint(output) {
451
398
  };
452
399
  }
453
400
  const dayMs = 864e5;
454
- /** UTC instant of a wall-clock time in `timeZone`. A time in a daylight saving gap moves forward by the gap. */
455
401
  function zonedInstant(year, month, day, minuteOfDay, timeZone) {
456
402
  const local = Date.UTC(year, month - 1, day, 0, minuteOfDay);
457
403
  const guess = local - timeZoneOffsetMinutes(new Date(local), timeZone) * 6e4;
@@ -495,7 +441,6 @@ function createLaunchdDriver(host, scope) {
495
441
  output: commandOutput(result)
496
442
  }), void 0];
497
443
  }
498
- /** The domain that has the job loaded, and its `launchctl print` output. */
499
444
  async function loaded(name) {
500
445
  for (const domain of domains) {
501
446
  const result = await host.exec("launchctl", ["print", `${domain}/${name}`]);
@@ -505,10 +450,6 @@ function createLaunchdDriver(host, scope) {
505
450
  };
506
451
  }
507
452
  }
508
- /**
509
- * The plist and the loaded job of `name`, and the resource that holds the name when ViteHub did not install it: a
510
- * plist at our path without our header, or a job with this label that launchd loaded from another plist.
511
- */
512
453
  async function owner(name) {
513
454
  const path = plistPath(name);
514
455
  const plist = await host.fs.readFile(path);
@@ -683,7 +624,6 @@ function lineBreak() {
683
624
  function pad$1(value) {
684
625
  return String(value).padStart(2, "0");
685
626
  }
686
- /** `1..5`, `0,15,30,45`, or `*` when the list holds every value. Runs of three or more become ranges. */
687
627
  function calendarList(values, size, format = pad$1) {
688
628
  if (values.length === size) return "*";
689
629
  const parts = [];
@@ -746,7 +686,6 @@ function renderSystemdTimer(spec) {
746
686
  ""
747
687
  ].join("\n");
748
688
  }
749
- /** Reads `systemctl show` output: blocks of `Key=value` lines, one block per unit, keyed by `Id`. */
750
689
  function parseSystemctlShow(output) {
751
690
  const units = /* @__PURE__ */ new Map();
752
691
  for (const block of output.split(/\n\s*\n/)) {
@@ -760,7 +699,6 @@ function parseSystemctlShow(output) {
760
699
  }
761
700
  return units;
762
701
  }
763
- /** `@1794841368` (`--timestamp=unix`) as a Date. Empty or `0` means never. */
764
702
  function unixTimestamp(value) {
765
703
  const seconds = Number(value?.replace(/^@/, ""));
766
704
  return value && Number.isFinite(seconds) && seconds > 0 ? /* @__PURE__ */ new Date(seconds * 1e3) : void 0;
@@ -781,10 +719,6 @@ const statusProperties = [
781
719
  ];
782
720
  const exitedCode = "1";
783
721
  const marker = `# ${generatedHeader}`;
784
- /**
785
- * The unit or file that holds `name` when ViteHub did not install it: a file at our path without our header, or a
786
- * loaded unit that systemd reads from another file, such as /usr/lib/systemd/user/<name>.service or a transient unit.
787
- */
788
722
  async function foreignResource(host, paths, texts, units, name) {
789
723
  for (const kind of ["service", "timer"]) {
790
724
  const text = texts[kind];
@@ -865,7 +799,6 @@ function createSystemdDriver(host, scope) {
865
799
  ...timer === void 0 ? {} : { timer }
866
800
  };
867
801
  }
868
- /** The resource that holds `name` when ViteHub did not install it in this scope. */
869
802
  async function foreign(name, files, texts) {
870
803
  const fileOwner = await foreignResource(host, files, texts, /* @__PURE__ */ new Map(), name);
871
804
  if (fileOwner) return [void 0, fileOwner];
@@ -1041,14 +974,12 @@ const monthNames = [
1041
974
  const taskNeverRan = 267011;
1042
975
  const taskRunning = 267009;
1043
976
  const { join: join$1 } = pathsFor("win32");
1044
- /** An XML duration, such as `PT23H52M30S`. */
1045
977
  function taskDuration(seconds) {
1046
978
  const hours = Math.floor(seconds / 3600);
1047
979
  const minutes = Math.floor(seconds % 3600 / 60);
1048
980
  const rest = seconds % 60;
1049
981
  return `PT${hours ? `${hours}H` : ""}${minutes ? `${minutes}M` : ""}${rest || !seconds ? `${rest}S` : ""}`;
1050
982
  }
1051
- /** One trigger for the runs. The duration ends half a step after the last run. */
1052
983
  function trigger({ start, step, count }) {
1053
984
  if (count < 2) return { start };
1054
985
  return {
@@ -1059,15 +990,10 @@ function trigger({ start, step, count }) {
1059
990
  }
1060
991
  };
1061
992
  }
1062
- /**
1063
- * Task Scheduler repeats a trigger in elapsed time. When the clocks change between the first and the last run, the
1064
- * later runs move by the change, and the duration ends at another time of day. Such runs need one trigger per time.
1065
- */
1066
993
  function crossesChange(runs, changes) {
1067
994
  const last = runs.start + (runs.count - 1) * runs.step;
1068
995
  return runs.count > 1 && changes.some((change) => runs.start < change.end && last >= change.start);
1069
996
  }
1070
- /** The fewest triggers that start at every time of the day of the cron, and keep their times across `changes`. */
1071
997
  function timeTriggers(fields, changes) {
1072
998
  const times = timesOfDay(fields);
1073
999
  const single = (start) => ({
@@ -1156,7 +1082,6 @@ function taskTriggers(fields, clock = {
1156
1082
  function xml(value) {
1157
1083
  return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;");
1158
1084
  }
1159
- /** A PowerShell single-quoted string. PowerShell also reads the typographic single quotes as quotes. */
1160
1085
  function powerShellString(value) {
1161
1086
  return `'${value.replace(/['\u2018\u2019\u201A\u201B]/g, "$&$&")}'`;
1162
1087
  }
@@ -1208,6 +1133,15 @@ function renderTaskWrapper(spec, logFile) {
1208
1133
  function parseTaskWrapperLog(wrapper) {
1209
1134
  return wrapper.match(/^\$log = '((?:[^']|'')*)'\r?$/m)?.[1]?.replaceAll("''", "'");
1210
1135
  }
1136
+ /**
1137
+ * The base64 PowerShell command that runs the wrapper and exits with its exit code. Task Scheduler expands `%NAME%` in
1138
+ * the arguments of a task and has no escape for `%`, so a wrapper path in the arguments breaks under a folder such as
1139
+ * `C:\Users\pct %PATH%`. Base64 has no `%`.
1140
+ */
1141
+ function runWrapperCommand(wrapper) {
1142
+ const script = `$global:LASTEXITCODE = 1\n& ${powerShellString(wrapper)}\nexit $global:LASTEXITCODE`;
1143
+ return Buffer.from(script, "utf16le").toString("base64");
1144
+ }
1211
1145
  /** The task XML. The description ends with a hash of the XML and the wrapper, which a later install compares. */
1212
1146
  function renderTaskXml(spec, triggers, options) {
1213
1147
  const render = (description) => [
@@ -1255,7 +1189,7 @@ function renderTaskXml(spec, triggers, options) {
1255
1189
  " <Actions Context=\"Author\">",
1256
1190
  " <Exec>",
1257
1191
  " <Command>powershell.exe</Command>",
1258
- ` <Arguments>${xml(`-NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File "${options.wrapper}"`)}</Arguments>`,
1192
+ ` <Arguments>${xml(`-NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -EncodedCommand ${runWrapperCommand(options.wrapper)}`)}</Arguments>`,
1259
1193
  " </Exec>",
1260
1194
  " </Actions>",
1261
1195
  "</Task>",
@@ -1273,16 +1207,11 @@ function taskOwnerScope(description) {
1273
1207
  function taskDescriptionHash(description) {
1274
1208
  return description?.match(/sha256:([0-9a-f]{64})/)?.[1];
1275
1209
  }
1276
- /**
1277
- * Windows PowerShell 5.1 joins native arguments without escaping them, so a quote, an empty argument, or a trailing
1278
- * backslash in an argument with spaces reaches the program changed.
1279
- */
1280
1210
  function unsafeWindowsArgument(argument) {
1281
1211
  if (argument === "") return "an empty argument";
1282
1212
  if (argument.includes("\"")) return "an argument with a double quote";
1283
1213
  if (/\s/.test(argument) && argument.endsWith("\\")) return "an argument with spaces that ends with a backslash";
1284
1214
  }
1285
- /** Reads the JSON of the status script. */
1286
1215
  function parseTaskInfo(output) {
1287
1216
  const value = JSON.parse(output.trim() || "{}");
1288
1217
  if (typeof value !== "object" || value === null || !("installed" in value)) return { installed: false };
@@ -1304,7 +1233,6 @@ function taskInfoScript(name) {
1304
1233
  }
1305
1234
  /** Task Scheduler tasks in the root folder, run as the current user or as SYSTEM. */
1306
1235
  function createTaskSchedulerDriver(host, scope) {
1307
- /** A task of this name that ViteHub did not install in this scope. */
1308
1236
  const foreign = (task) => task.installed && taskOwnerScope(task.description) !== scope;
1309
1237
  const taskResource = (name, task) => {
1310
1238
  const owner = taskOwnerScope(task.description);