@moinulmoin/eden-runtime-cloudflare 0.2.2 → 0.3.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.
@@ -1501,6 +1501,126 @@ var EVE_HOST_DEFAULTS = {
1501
1501
  instanceType: "basic",
1502
1502
  sleepAfter: "24h"
1503
1503
  };
1504
+ var EVE_SCHEDULE_WAKE_LEAD_MS = 24e4;
1505
+ var EVE_SCHEDULE_WAKE_GRACE_MS = 6e4;
1506
+ var MINUTE_MS = 6e4;
1507
+ var EVE_CRON_FIELD_BOUNDS = [
1508
+ { min: 0, max: 59 },
1509
+ // minute
1510
+ { min: 0, max: 23 },
1511
+ // hour
1512
+ { min: 1, max: 31 },
1513
+ // day of month
1514
+ { min: 1, max: 12 },
1515
+ // month
1516
+ { min: 0, max: 7 }
1517
+ // day of week (0 and 7 are Sunday)
1518
+ ];
1519
+ var EVE_CRON_MONTH_NAMES = {
1520
+ jan: 1,
1521
+ feb: 2,
1522
+ mar: 3,
1523
+ apr: 4,
1524
+ may: 5,
1525
+ jun: 6,
1526
+ jul: 7,
1527
+ aug: 8,
1528
+ sep: 9,
1529
+ oct: 10,
1530
+ nov: 11,
1531
+ dec: 12
1532
+ };
1533
+ var EVE_CRON_DAY_NAMES = {
1534
+ sun: 0,
1535
+ mon: 1,
1536
+ tue: 2,
1537
+ wed: 3,
1538
+ thu: 4,
1539
+ fri: 5,
1540
+ sat: 6
1541
+ };
1542
+ function parseCronValue(token, bounds, fieldIndex) {
1543
+ const names = fieldIndex === 3 ? EVE_CRON_MONTH_NAMES : fieldIndex === 4 ? EVE_CRON_DAY_NAMES : void 0;
1544
+ const named = names?.[token.toLowerCase()];
1545
+ const value = named !== void 0 ? named : /^[0-9]+$/u.test(token) ? Number.parseInt(token, 10) : Number.NaN;
1546
+ if (!Number.isInteger(value) || value < bounds.min || value > bounds.max) {
1547
+ return void 0;
1548
+ }
1549
+ return fieldIndex === 4 && value === 7 ? 0 : value;
1550
+ }
1551
+ function expandEveCronField(field, fieldIndex) {
1552
+ const bounds = EVE_CRON_FIELD_BOUNDS[fieldIndex];
1553
+ if (bounds === void 0) return void 0;
1554
+ const values = /* @__PURE__ */ new Set();
1555
+ for (const part of field.split(",")) {
1556
+ const stepMatch = /^(.+)\/([0-9]+)$/u.exec(part);
1557
+ const rangeText = stepMatch?.[1] ?? part;
1558
+ const step = stepMatch === null ? 1 : Number.parseInt(stepMatch[2] ?? "", 10);
1559
+ if (!Number.isInteger(step) || step < 1) return void 0;
1560
+ let low;
1561
+ let high;
1562
+ if (rangeText === "*" || rangeText === "?") {
1563
+ low = bounds.min;
1564
+ high = bounds.max;
1565
+ } else {
1566
+ const rangeMatch = /^([A-Za-z0-9]+)-([A-Za-z0-9]+)$/u.exec(rangeText);
1567
+ const lowToken = rangeMatch === null ? rangeText : rangeMatch[1];
1568
+ const lowValue = lowToken === void 0 ? void 0 : parseCronValue(lowToken, bounds, fieldIndex);
1569
+ if (lowValue === void 0) return void 0;
1570
+ low = lowValue;
1571
+ const highToken = rangeMatch?.[2];
1572
+ const highValue = highToken === void 0 ? void 0 : parseCronValue(highToken, bounds, fieldIndex);
1573
+ high = rangeMatch === null ? stepMatch === null ? lowValue : bounds.max : highValue ?? -1;
1574
+ if (high < low) return void 0;
1575
+ }
1576
+ for (let value = low; value <= high; value += step) {
1577
+ values.add(fieldIndex === 4 && value === 7 ? 0 : value);
1578
+ }
1579
+ }
1580
+ return values;
1581
+ }
1582
+ function parseEveScheduleCron(cron) {
1583
+ const fields = cron.trim().split(/\s+/u);
1584
+ const [minuteField, hourField, domField, monthField, dowField] = fields;
1585
+ if (fields.length !== 5 || minuteField === void 0 || hourField === void 0 || domField === void 0 || monthField === void 0 || dowField === void 0) return void 0;
1586
+ const minute = expandEveCronField(minuteField, 0);
1587
+ const hour = expandEveCronField(hourField, 1);
1588
+ const dayOfMonth = expandEveCronField(domField, 2);
1589
+ const month = expandEveCronField(monthField, 3);
1590
+ const dayOfWeek = expandEveCronField(dowField, 4);
1591
+ if (minute === void 0 || hour === void 0 || dayOfMonth === void 0 || month === void 0 || dayOfWeek === void 0) {
1592
+ return void 0;
1593
+ }
1594
+ return {
1595
+ minute,
1596
+ hour,
1597
+ dayOfMonth,
1598
+ month,
1599
+ dayOfWeek,
1600
+ domRestricted: domField !== "*",
1601
+ dowRestricted: dowField !== "*"
1602
+ };
1603
+ }
1604
+ function cronMatches(parsed, atMs) {
1605
+ const at = new Date(atMs);
1606
+ if (!parsed.minute.has(at.getUTCMinutes())) return false;
1607
+ if (!parsed.hour.has(at.getUTCHours())) return false;
1608
+ if (!parsed.month.has(at.getUTCMonth() + 1)) return false;
1609
+ const dom = parsed.dayOfMonth.has(at.getUTCDate());
1610
+ const dow = parsed.dayOfWeek.has(at.getUTCDay());
1611
+ if (parsed.domRestricted && parsed.dowRestricted) return dom || dow;
1612
+ return dom && dow;
1613
+ }
1614
+ function eveScheduleCronFiresWithin(cron, nowMs, leadMs = EVE_SCHEDULE_WAKE_LEAD_MS) {
1615
+ const parsed = parseEveScheduleCron(cron);
1616
+ if (parsed === void 0) return false;
1617
+ const firstCandidate = Math.floor((nowMs - EVE_SCHEDULE_WAKE_GRACE_MS) / MINUTE_MS) * MINUTE_MS;
1618
+ const lastCandidate = Math.floor((nowMs + leadMs) / MINUTE_MS) * MINUTE_MS;
1619
+ for (let candidate = firstCandidate; candidate <= lastCandidate; candidate += MINUTE_MS) {
1620
+ if (cronMatches(parsed, candidate)) return true;
1621
+ }
1622
+ return false;
1623
+ }
1504
1624
  function isEveWorkflowInternalRoute(pathname) {
1505
1625
  let decodedPath = pathname;
1506
1626
  try {
@@ -1924,8 +2044,8 @@ async function routeEveOutboundRequest(request, env) {
1924
2044
  }
1925
2045
  var EveHostContainer = class extends Container {
1926
2046
  defaultPort = EVE_HOST_DEFAULTS.internalPort;
1927
- requiredPorts = [EVE_HOST_DEFAULTS.internalPort];
1928
2047
  sleepAfter = EVE_HOST_DEFAULTS.sleepAfter;
2048
+ requiredPorts = [EVE_HOST_DEFAULTS.internalPort];
1929
2049
  interceptHttps = true;
1930
2050
  enableInternet = true;
1931
2051
  pingEndpoint = "localhost/eve/v1/health";
@@ -1963,6 +2083,10 @@ var EveHostContainer = class extends Container {
1963
2083
  NODE_EXTRA_CA_CERTS: EVE_CLOUDFLARE_CONTAINERS_CA_PATH,
1964
2084
  ...env.EDEN_EVE_RUNTIME_REVISION === void 0 ? {} : { EDEN_EVE_RUNTIME_REVISION: env.EDEN_EVE_RUNTIME_REVISION }
1965
2085
  };
2086
+ const sleepAfterOverride = env.EDEN_EVE_CONTAINER_SLEEP_AFTER;
2087
+ if (sleepAfterOverride !== void 0) {
2088
+ this.sleepAfter = sleepAfterOverride;
2089
+ }
1966
2090
  this.readiness = createEveReadinessGate({
1967
2091
  startAndWaitForPorts: (options) => this.startAndWaitForPorts(options),
1968
2092
  healthFetch: (request) => this.containerFetch(request, EVE_HOST_DEFAULTS.internalPort)
@@ -2026,21 +2150,23 @@ function createEveHostWorker(options) {
2026
2150
  "The Container binding name is not valid."
2027
2151
  );
2028
2152
  }
2153
+ const schedules = options.schedules ?? [];
2154
+ const resolveContainer = (env) => {
2155
+ const namespace = env[options.containerBindingName];
2156
+ if (namespace === void 0) {
2157
+ throw new EveHostError(
2158
+ "HOST_READINESS_UNPROVEN",
2159
+ "The configured Container binding is unavailable."
2160
+ );
2161
+ }
2162
+ return namespace.getByName(options.stableContainerInstanceName);
2163
+ };
2029
2164
  return {
2030
2165
  async fetch(request, env) {
2031
2166
  if (isEveWorkflowInternalRoute(new URL(request.url).pathname)) {
2032
2167
  return new Response(null, { status: 404 });
2033
2168
  }
2034
- const namespace = env[options.containerBindingName];
2035
- if (namespace === void 0) {
2036
- throw new EveHostError(
2037
- "HOST_READINESS_UNPROVEN",
2038
- "The configured Container binding is unavailable."
2039
- );
2040
- }
2041
- const container = namespace.getByName(
2042
- options.stableContainerInstanceName
2043
- );
2169
+ const container = resolveContainer(env);
2044
2170
  const forwarded = createTrustedEveRequest(request, {
2045
2171
  publicOrigin: options.publicOrigin,
2046
2172
  deploymentId: options.deploymentId,
@@ -2069,6 +2195,26 @@ function createEveHostWorker(options) {
2069
2195
  statusText: response.statusText,
2070
2196
  headers
2071
2197
  });
2198
+ },
2199
+ ...schedules.length === 0 ? {} : {
2200
+ scheduled(event, env, ctx) {
2201
+ const due = schedules.some((schedule) => eveScheduleCronFiresWithin(
2202
+ schedule.cron,
2203
+ event.scheduledTime,
2204
+ EVE_SCHEDULE_WAKE_LEAD_MS
2205
+ ));
2206
+ if (!due) return;
2207
+ ctx.waitUntil(
2208
+ resolveContainer(env).fetch(new Request(
2209
+ new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin)
2210
+ )).catch((error) => {
2211
+ console.error(
2212
+ "The scheduled wake request failed:",
2213
+ error instanceof Error ? error.message : "unknown"
2214
+ );
2215
+ })
2216
+ );
2217
+ }
2072
2218
  }
2073
2219
  };
2074
2220
  }
@@ -1,5 +1,5 @@
1
1
  import { Container, ContainerProxy } from "@cloudflare/containers";
2
- import { type EveHostContainerEnvironment, type EveHostForwardingMetadata, type EveHostReadinessEvidence } from "./eve-host.js";
2
+ import { type EveHostContainerEnvironment, type EveHostForwardingMetadata, type EveHostReadinessEvidence, type EveScheduleCronEntry } from "./eve-host.js";
3
3
  export { ContainerProxy };
4
4
  /**
5
5
  * Delivers the container's own HTTPS requests to its public workers.dev origin
@@ -20,8 +20,8 @@ export declare function routeEveOutboundRequest(request: Request, env: EveHostCo
20
20
  type EveHostContainerContext = ConstructorParameters<typeof Container>[0];
21
21
  export declare class EveHostContainer extends Container<EveHostContainerEnvironment> {
22
22
  defaultPort: 8080;
23
+ sleepAfter: string | number;
23
24
  requiredPorts: 8080[];
24
- sleepAfter: "24h";
25
25
  interceptHttps: boolean;
26
26
  enableInternet: boolean;
27
27
  pingEndpoint: string;
@@ -44,6 +44,7 @@ export interface EveHostWorkerOptions extends EveHostForwardingMetadata {
44
44
  readonly workerName: string;
45
45
  readonly containerBindingName: string;
46
46
  readonly stableContainerInstanceName: string;
47
+ readonly schedules?: readonly EveScheduleCronEntry[];
47
48
  }
48
49
  /**
49
50
  * Structural worker-handler shape. Declared locally so the emitted module
@@ -51,6 +52,12 @@ export interface EveHostWorkerOptions extends EveHostForwardingMetadata {
51
52
  */
52
53
  export interface EveHostWorkerHandler {
53
54
  fetch(request: Request, env: EveHostWorkerEnvironment): Promise<Response>;
55
+ scheduled?(event: {
56
+ readonly cron: string;
57
+ readonly scheduledTime: number;
58
+ }, env: EveHostWorkerEnvironment, ctx: {
59
+ waitUntil(task: Promise<unknown>): void;
60
+ }): void;
54
61
  }
55
62
  export declare function createEveHostWorker(options: EveHostWorkerOptions): EveHostWorkerHandler;
56
63
  //# sourceMappingURL=eve-host-runtime.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { Container, ContainerProxy } from "@cloudflare/containers";
2
- import { EVE_HOST_DEFAULTS, EveHostError, assertNonEmpty, assertStableOrigin, createEveHostLifecycleObserver, createEveReadinessGate, createTrustedEveRequest, IDENTIFIER_PATTERN, readProtectedRuntimeVariables, WORKER_NAME_PATTERN, isEveWorkflowInternalRoute, } from "./eve-host.js";
2
+ import { EVE_HOST_DEFAULTS, EVE_SCHEDULE_WAKE_LEAD_MS, EveHostError, assertNonEmpty, assertStableOrigin, createEveHostLifecycleObserver, createEveReadinessGate, createTrustedEveRequest, eveScheduleCronFiresWithin, IDENTIFIER_PATTERN, readProtectedRuntimeVariables, WORKER_NAME_PATTERN, isEveWorkflowInternalRoute, } from "./eve-host.js";
3
3
  export { ContainerProxy };
4
4
  const EVE_CLOUDFLARE_CONTAINERS_CA_PATH = "/etc/cloudflare/certs/cloudflare-containers-ca.crt";
5
5
  /**
@@ -37,8 +37,8 @@ export async function routeEveOutboundRequest(request, env) {
37
37
  }
38
38
  export class EveHostContainer extends Container {
39
39
  defaultPort = EVE_HOST_DEFAULTS.internalPort;
40
- requiredPorts = [EVE_HOST_DEFAULTS.internalPort];
41
40
  sleepAfter = EVE_HOST_DEFAULTS.sleepAfter;
41
+ requiredPorts = [EVE_HOST_DEFAULTS.internalPort];
42
42
  interceptHttps = true;
43
43
  enableInternet = true;
44
44
  pingEndpoint = "localhost/eve/v1/health";
@@ -77,6 +77,13 @@ export class EveHostContainer extends Container {
77
77
  ? {}
78
78
  : { EDEN_EVE_RUNTIME_REVISION: env.EDEN_EVE_RUNTIME_REVISION }),
79
79
  };
80
+ const sleepAfterOverride = env.EDEN_EVE_CONTAINER_SLEEP_AFTER;
81
+ if (sleepAfterOverride !== undefined) {
82
+ // `env` is only readable after `super()`, so the override is applied in
83
+ // the constructor body; the base class reads `this.sleepAfter` again on
84
+ // every subsequent activity renewal.
85
+ this.sleepAfter = sleepAfterOverride;
86
+ }
80
87
  this.readiness = createEveReadinessGate({
81
88
  startAndWaitForPorts: (options) => this.startAndWaitForPorts(options),
82
89
  healthFetch: (request) => this.containerFetch(request, EVE_HOST_DEFAULTS.internalPort),
@@ -128,6 +135,14 @@ export function createEveHostWorker(options) {
128
135
  if (!IDENTIFIER_PATTERN.test(options.containerBindingName)) {
129
136
  throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container binding name is not valid.");
130
137
  }
138
+ const schedules = options.schedules ?? [];
139
+ const resolveContainer = (env) => {
140
+ const namespace = env[options.containerBindingName];
141
+ if (namespace === undefined) {
142
+ throw new EveHostError("HOST_READINESS_UNPROVEN", "The configured Container binding is unavailable.");
143
+ }
144
+ return namespace.getByName(options.stableContainerInstanceName);
145
+ };
131
146
  return {
132
147
  async fetch(request, env) {
133
148
  // Queue deliveries to these endpoints arrive through the container's
@@ -136,11 +151,7 @@ export function createEveHostWorker(options) {
136
151
  if (isEveWorkflowInternalRoute(new URL(request.url).pathname)) {
137
152
  return new Response(null, { status: 404 });
138
153
  }
139
- const namespace = env[options.containerBindingName];
140
- if (namespace === undefined) {
141
- throw new EveHostError("HOST_READINESS_UNPROVEN", "The configured Container binding is unavailable.");
142
- }
143
- const container = namespace.getByName(options.stableContainerInstanceName);
154
+ const container = resolveContainer(env);
144
155
  const forwarded = createTrustedEveRequest(request, {
145
156
  publicOrigin: options.publicOrigin,
146
157
  deploymentId: options.deploymentId,
@@ -166,6 +177,22 @@ export function createEveHostWorker(options) {
166
177
  headers,
167
178
  });
168
179
  },
180
+ ...(schedules.length === 0
181
+ ? {}
182
+ : {
183
+ scheduled(event, env, ctx) {
184
+ const due = schedules.some((schedule) => eveScheduleCronFiresWithin(schedule.cron, event.scheduledTime, EVE_SCHEDULE_WAKE_LEAD_MS));
185
+ if (!due)
186
+ return;
187
+ // Wake only: the in-process Nitro runner inside the container
188
+ // fires the tick itself, so the schedule runs exactly once.
189
+ ctx.waitUntil(resolveContainer(env)
190
+ .fetch(new Request(new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin)))
191
+ .catch((error) => {
192
+ console.error("The scheduled wake request failed:", error instanceof Error ? error.message : "unknown");
193
+ }));
194
+ },
195
+ }),
169
196
  };
170
197
  }
171
198
  //# sourceMappingURL=eve-host-runtime.js.map
@@ -6,6 +6,49 @@ export declare const EVE_HOST_DEFAULTS: {
6
6
  readonly instanceType: "basic";
7
7
  readonly sleepAfter: "24h";
8
8
  };
9
+ export declare const EVE_SCHEDULE_WAKE_TRIGGER_CRON = "* * * * *";
10
+ /**
11
+ * How far ahead of a tick the every-minute wake trigger starts the Container.
12
+ * A sleeping Container cold-starts in well under a minute on a warm image; the
13
+ * wider window absorbs slow starts without missing the tick.
14
+ */
15
+ export declare const EVE_SCHEDULE_WAKE_LEAD_MS = 240000;
16
+ /**
17
+ * One authored Eve schedule reduced to the fields the host needs. `cron` is the
18
+ * 5-field expression from the compiled manifest.
19
+ */
20
+ export interface EveScheduleCronEntry {
21
+ readonly name: string;
22
+ readonly cron: string;
23
+ }
24
+ /**
25
+ * Expands one cron field (`*`, `*\/n`, `a-b/n`, `a,b,...`) into the set of
26
+ * matching unit values. Returns undefined when the field uses syntax outside
27
+ * the standard 5-field subset Eve documents (e.g. `?`, `L`, `W`, `#`).
28
+ */
29
+ export declare function expandEveCronField(field: string, fieldIndex: number): ReadonlySet<number> | undefined;
30
+ interface ParsedEveCron {
31
+ readonly minute: ReadonlySet<number>;
32
+ readonly hour: ReadonlySet<number>;
33
+ readonly dayOfMonth: ReadonlySet<number>;
34
+ readonly month: ReadonlySet<number>;
35
+ readonly dayOfWeek: ReadonlySet<number>;
36
+ readonly domRestricted: boolean;
37
+ readonly dowRestricted: boolean;
38
+ }
39
+ /**
40
+ * Parses a standard 5-field cron expression (minute granularity, UTC, Vixie
41
+ * day-of-month/day-of-week OR semantics — the same evaluation Vercel and
42
+ * croner use for Eve schedules). Returns undefined for anything else: named
43
+ * `@` schedules, seconds-prefixed 6-field expressions, or unsupported syntax.
44
+ */
45
+ export declare function parseEveScheduleCron(cron: string): ParsedEveCron | undefined;
46
+ /**
47
+ * True when the schedule's next tick lands inside the wake window: at or just
48
+ * after `nowMs` through `nowMs + leadMs` (Cloudflare Cron Triggers are
49
+ * minute-aligned UTC, matching Eve's minute-granularity schedules).
50
+ */
51
+ export declare function eveScheduleCronFiresWithin(cron: string, nowMs: number, leadMs?: number): boolean;
9
52
  /** Queue deliveries are private; webhook and manifest routes stay forwarded. */
10
53
  export declare function isEveWorkflowInternalRoute(pathname: string): boolean;
11
54
  export declare const EVE_HOST_OWNED_HEADERS: readonly ["forwarded", "x-forwarded-for", "x-forwarded-host", "x-forwarded-port", "x-forwarded-proto", "x-forwarded-server", "x-forwarded-scheme", "x-real-ip", "cf-connecting-ip", "cf-visitor", "true-client-ip", "x-original-host", "x-original-proto", "x-envoy-original-host", "x-envoy-original-proto", "x-proxy-host", "x-host", "x-eve-public-origin", "x-workflow-local-base-url", "x-eden-eve-callback-base", "x-eden-eve-deployment-id", "x-eden-eve-generation-id", "x-eden-eve-public-origin", "x-eden-eve-correlation-id", "x-eden-eve-container-name", "x-eden-eve-container-id", "x-eden-eve-runtime-revision", "x-eden-deployment-id", "x-eden-generation-id", "x-eden-container-name", "x-eden-container-id", "cf-container-target-port"];
@@ -37,6 +80,13 @@ export interface EveHostConfigRequest extends EveHostIdentity {
37
80
  readonly containerImageBuildContext?: string;
38
81
  readonly runtimeVariableNames?: readonly string[];
39
82
  readonly runtimeRevisionHandle?: string;
83
+ /**
84
+ * Compiled authored schedules. Entries whose cron cannot be expressed are
85
+ * rejected here; the caller reports that as a deploy warning.
86
+ */
87
+ readonly schedules?: readonly EveScheduleCronEntry[];
88
+ /** Overrides the Container `sleepAfter` (e.g. "30s") for testing. */
89
+ readonly containerSleepAfter?: string;
40
90
  }
41
91
  export interface EveHostWranglerConfig {
42
92
  readonly account_id?: string;
@@ -52,6 +102,10 @@ export interface EveHostWranglerConfig {
52
102
  readonly EDEN_EVE_GENERATION_ID: string;
53
103
  readonly EVE_RUNTIME_VARIABLE_NAMES: readonly string[];
54
104
  readonly EDEN_EVE_RUNTIME_REVISION?: string;
105
+ readonly EDEN_EVE_CONTAINER_SLEEP_AFTER?: string;
106
+ };
107
+ readonly triggers?: {
108
+ readonly crons: readonly string[];
55
109
  };
56
110
  readonly containers: readonly [
57
111
  {
@@ -88,6 +142,7 @@ export interface EveHostContainerConfig {
88
142
  readonly deploymentId: string;
89
143
  readonly generationId: string;
90
144
  readonly runtimeRevisionHandle?: string;
145
+ readonly schedules: readonly EveScheduleCronEntry[];
91
146
  }
92
147
  export interface EveHostConfig {
93
148
  readonly worker: EveHostWranglerConfig;
@@ -136,6 +191,7 @@ export interface EveHostContainerEnvironment {
136
191
  readonly EDEN_EVE_DEPLOYMENT_ID?: string;
137
192
  readonly EDEN_EVE_GENERATION_ID?: string;
138
193
  readonly EDEN_EVE_RUNTIME_REVISION?: string;
194
+ readonly EDEN_EVE_CONTAINER_SLEEP_AFTER?: string;
139
195
  }
140
196
  export interface EveHostReadinessEvidence {
141
197
  readonly healthPath: "/eve/v1/health";
@@ -168,4 +224,5 @@ export interface EveReadinessGate {
168
224
  }
169
225
  export declare function readProtectedRuntimeVariables(env: EveHostContainerEnvironment): Record<string, string>;
170
226
  export declare function createEveReadinessGate(options: EveHostReadinessOptions): EveReadinessGate;
227
+ export {};
171
228
  //# sourceMappingURL=eve-host.d.ts.map
package/dist/eve-host.js CHANGED
@@ -6,6 +6,155 @@ export const EVE_HOST_DEFAULTS = {
6
6
  instanceType: "basic",
7
7
  sleepAfter: "24h",
8
8
  };
9
+ export const EVE_SCHEDULE_WAKE_TRIGGER_CRON = "* * * * *";
10
+ /**
11
+ * How far ahead of a tick the every-minute wake trigger starts the Container.
12
+ * A sleeping Container cold-starts in well under a minute on a warm image; the
13
+ * wider window absorbs slow starts without missing the tick.
14
+ */
15
+ export const EVE_SCHEDULE_WAKE_LEAD_MS = 240_000;
16
+ const EVE_SCHEDULE_WAKE_GRACE_MS = 60_000;
17
+ const MINUTE_MS = 60_000;
18
+ const EVE_CRON_FIELD_BOUNDS = [
19
+ { min: 0, max: 59 }, // minute
20
+ { min: 0, max: 23 }, // hour
21
+ { min: 1, max: 31 }, // day of month
22
+ { min: 1, max: 12 }, // month
23
+ { min: 0, max: 7 }, // day of week (0 and 7 are Sunday)
24
+ ];
25
+ const EVE_CRON_MONTH_NAMES = {
26
+ jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6,
27
+ jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12,
28
+ };
29
+ const EVE_CRON_DAY_NAMES = {
30
+ sun: 0, mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6,
31
+ };
32
+ function parseCronValue(token, bounds, fieldIndex) {
33
+ const names = fieldIndex === 3
34
+ ? EVE_CRON_MONTH_NAMES
35
+ : fieldIndex === 4
36
+ ? EVE_CRON_DAY_NAMES
37
+ : undefined;
38
+ const named = names?.[token.toLowerCase()];
39
+ const value = named !== undefined
40
+ ? named
41
+ : /^[0-9]+$/u.test(token)
42
+ ? Number.parseInt(token, 10)
43
+ : Number.NaN;
44
+ if (!Number.isInteger(value) || value < bounds.min || value > bounds.max) {
45
+ return undefined;
46
+ }
47
+ return fieldIndex === 4 && value === 7 ? 0 : value;
48
+ }
49
+ /**
50
+ * Expands one cron field (`*`, `*\/n`, `a-b/n`, `a,b,...`) into the set of
51
+ * matching unit values. Returns undefined when the field uses syntax outside
52
+ * the standard 5-field subset Eve documents (e.g. `?`, `L`, `W`, `#`).
53
+ */
54
+ export function expandEveCronField(field, fieldIndex) {
55
+ const bounds = EVE_CRON_FIELD_BOUNDS[fieldIndex];
56
+ if (bounds === undefined)
57
+ return undefined;
58
+ const values = new Set();
59
+ for (const part of field.split(",")) {
60
+ const stepMatch = /^(.+)\/([0-9]+)$/u.exec(part);
61
+ const rangeText = stepMatch?.[1] ?? part;
62
+ const step = stepMatch === null ? 1 : Number.parseInt(stepMatch[2] ?? "", 10);
63
+ if (!Number.isInteger(step) || step < 1)
64
+ return undefined;
65
+ let low;
66
+ let high;
67
+ if (rangeText === "*" || rangeText === "?") {
68
+ low = bounds.min;
69
+ high = bounds.max;
70
+ }
71
+ else {
72
+ const rangeMatch = /^([A-Za-z0-9]+)-([A-Za-z0-9]+)$/u.exec(rangeText);
73
+ const lowToken = rangeMatch === null ? rangeText : rangeMatch[1];
74
+ const lowValue = lowToken === undefined
75
+ ? undefined
76
+ : parseCronValue(lowToken, bounds, fieldIndex);
77
+ if (lowValue === undefined)
78
+ return undefined;
79
+ low = lowValue;
80
+ const highToken = rangeMatch?.[2];
81
+ const highValue = highToken === undefined
82
+ ? undefined
83
+ : parseCronValue(highToken, bounds, fieldIndex);
84
+ high = rangeMatch === null
85
+ ? (stepMatch === null ? lowValue : bounds.max)
86
+ : highValue ?? -1;
87
+ if (high < low)
88
+ return undefined;
89
+ }
90
+ for (let value = low; value <= high; value += step) {
91
+ values.add(fieldIndex === 4 && value === 7 ? 0 : value);
92
+ }
93
+ }
94
+ return values;
95
+ }
96
+ /**
97
+ * Parses a standard 5-field cron expression (minute granularity, UTC, Vixie
98
+ * day-of-month/day-of-week OR semantics — the same evaluation Vercel and
99
+ * croner use for Eve schedules). Returns undefined for anything else: named
100
+ * `@` schedules, seconds-prefixed 6-field expressions, or unsupported syntax.
101
+ */
102
+ export function parseEveScheduleCron(cron) {
103
+ const fields = cron.trim().split(/\s+/u);
104
+ const [minuteField, hourField, domField, monthField, dowField] = fields;
105
+ if (fields.length !== 5 || minuteField === undefined || hourField === undefined ||
106
+ domField === undefined || monthField === undefined || dowField === undefined)
107
+ return undefined;
108
+ const minute = expandEveCronField(minuteField, 0);
109
+ const hour = expandEveCronField(hourField, 1);
110
+ const dayOfMonth = expandEveCronField(domField, 2);
111
+ const month = expandEveCronField(monthField, 3);
112
+ const dayOfWeek = expandEveCronField(dowField, 4);
113
+ if (minute === undefined || hour === undefined || dayOfMonth === undefined ||
114
+ month === undefined || dayOfWeek === undefined) {
115
+ return undefined;
116
+ }
117
+ return {
118
+ minute,
119
+ hour,
120
+ dayOfMonth,
121
+ month,
122
+ dayOfWeek,
123
+ domRestricted: domField !== "*",
124
+ dowRestricted: dowField !== "*",
125
+ };
126
+ }
127
+ function cronMatches(parsed, atMs) {
128
+ const at = new Date(atMs);
129
+ if (!parsed.minute.has(at.getUTCMinutes()))
130
+ return false;
131
+ if (!parsed.hour.has(at.getUTCHours()))
132
+ return false;
133
+ if (!parsed.month.has(at.getUTCMonth() + 1))
134
+ return false;
135
+ const dom = parsed.dayOfMonth.has(at.getUTCDate());
136
+ const dow = parsed.dayOfWeek.has(at.getUTCDay());
137
+ if (parsed.domRestricted && parsed.dowRestricted)
138
+ return dom || dow;
139
+ return dom && dow;
140
+ }
141
+ /**
142
+ * True when the schedule's next tick lands inside the wake window: at or just
143
+ * after `nowMs` through `nowMs + leadMs` (Cloudflare Cron Triggers are
144
+ * minute-aligned UTC, matching Eve's minute-granularity schedules).
145
+ */
146
+ export function eveScheduleCronFiresWithin(cron, nowMs, leadMs = EVE_SCHEDULE_WAKE_LEAD_MS) {
147
+ const parsed = parseEveScheduleCron(cron);
148
+ if (parsed === undefined)
149
+ return false;
150
+ const firstCandidate = Math.floor((nowMs - EVE_SCHEDULE_WAKE_GRACE_MS) / MINUTE_MS) * MINUTE_MS;
151
+ const lastCandidate = Math.floor((nowMs + leadMs) / MINUTE_MS) * MINUTE_MS;
152
+ for (let candidate = firstCandidate; candidate <= lastCandidate; candidate += MINUTE_MS) {
153
+ if (cronMatches(parsed, candidate))
154
+ return true;
155
+ }
156
+ return false;
157
+ }
9
158
  /** Queue deliveries are private; webhook and manifest routes stay forwarded. */
10
159
  export function isEveWorkflowInternalRoute(pathname) {
11
160
  let decodedPath = pathname;
@@ -137,6 +286,17 @@ export function createEveHostConfig(request) {
137
286
  throw new EveHostError("HOST_READINESS_UNPROVEN", `The protected runtime variable name ${name} is invalid or reserved.`);
138
287
  }
139
288
  }
289
+ const schedules = request.schedules ?? [];
290
+ for (const schedule of schedules) {
291
+ if (parseEveScheduleCron(schedule.cron) === undefined) {
292
+ throw new EveHostError("HOST_READINESS_UNPROVEN", `The schedule ${schedule.name} uses a cron expression the wake trigger cannot express.`);
293
+ }
294
+ }
295
+ if (request.containerSleepAfter !== undefined) {
296
+ if (!/^[0-9]+(?:ms|s|m|h|d)?$/u.test(request.containerSleepAfter)) {
297
+ throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container sleepAfter override is not a parseable duration.");
298
+ }
299
+ }
140
300
  const container = {
141
301
  name: request.containerApplicationName,
142
302
  class_name: request.containerClassName,
@@ -166,7 +326,13 @@ export function createEveHostConfig(request) {
166
326
  ...(request.runtimeRevisionHandle === undefined
167
327
  ? {}
168
328
  : { EDEN_EVE_RUNTIME_REVISION: request.runtimeRevisionHandle }),
329
+ ...(request.containerSleepAfter === undefined
330
+ ? {}
331
+ : { EDEN_EVE_CONTAINER_SLEEP_AFTER: request.containerSleepAfter }),
169
332
  },
333
+ ...(schedules.length === 0
334
+ ? {}
335
+ : { triggers: { crons: [EVE_SCHEDULE_WAKE_TRIGGER_CRON] } }),
170
336
  containers: [container],
171
337
  durable_objects: {
172
338
  bindings: [
@@ -195,6 +361,7 @@ export function createEveHostConfig(request) {
195
361
  ...(request.runtimeRevisionHandle === undefined
196
362
  ? {}
197
363
  : { runtimeRevisionHandle: request.runtimeRevisionHandle }),
364
+ schedules,
198
365
  },
199
366
  };
200
367
  }
@@ -212,6 +379,9 @@ export function generateEveHostWorkerSource(request) {
212
379
  : {
213
380
  runtimeRevisionHandle: request.config.container.runtimeRevisionHandle,
214
381
  }),
382
+ ...(request.config.container.schedules.length === 0
383
+ ? {}
384
+ : { schedules: request.config.container.schedules }),
215
385
  };
216
386
  return [
217
387
  `import { ContainerProxy, EveHostContainer, createEveHostWorker, routeEveOutboundRequest } from ${JSON.stringify(moduleSpecifier)};`,
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { EVE_HOST_DEFAULTS, EVE_HOST_OWNED_HEADERS, EveHostError, createEveHostConfig, createEveHostLifecycleObserver, createEveHostProxy, createEveReadinessGate, createTrustedEveRequest, generateEveHostWorkerSource, resolveStableWorkersDevOrigin, } from "./eve-host.js";
2
- export type { EveContainerTransport, EveHostConfig, EveHostConfigRequest, EveHostContainerConfig, EveHostContainerEnvironment, EveHostErrorCode, EveHostForwardingMetadata, EveHostIdentity, EveHostLifecycleEvent, EveHostLifecycleObserver, EveHostProxyOptions, EveHostReadinessEvidence, EveHostReadinessOptions, EveReadinessGate, EveHostWranglerConfig, EveGeneratedWorkerSourceRequest, StableWorkersDevOriginRequest, } from "./eve-host.js";
1
+ export { EVE_HOST_DEFAULTS, EVE_SCHEDULE_WAKE_LEAD_MS, EVE_SCHEDULE_WAKE_TRIGGER_CRON, eveScheduleCronFiresWithin, expandEveCronField, parseEveScheduleCron, EVE_HOST_OWNED_HEADERS, EveHostError, createEveHostConfig, createEveHostLifecycleObserver, createEveHostProxy, createEveReadinessGate, createTrustedEveRequest, generateEveHostWorkerSource, resolveStableWorkersDevOrigin, } from "./eve-host.js";
2
+ export type { EveContainerTransport, EveHostConfig, EveHostConfigRequest, EveHostContainerConfig, EveHostContainerEnvironment, EveHostErrorCode, EveScheduleCronEntry, EveHostForwardingMetadata, EveHostIdentity, EveHostLifecycleEvent, EveHostLifecycleObserver, EveHostProxyOptions, EveHostReadinessEvidence, EveHostReadinessOptions, EveReadinessGate, EveHostWranglerConfig, EveGeneratedWorkerSourceRequest, StableWorkersDevOriginRequest, } from "./eve-host.js";
3
3
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { EVE_HOST_DEFAULTS, EVE_HOST_OWNED_HEADERS, EveHostError, createEveHostConfig, createEveHostLifecycleObserver, createEveHostProxy, createEveReadinessGate, createTrustedEveRequest, generateEveHostWorkerSource, resolveStableWorkersDevOrigin, } from "./eve-host.js";
1
+ export { EVE_HOST_DEFAULTS, EVE_SCHEDULE_WAKE_LEAD_MS, EVE_SCHEDULE_WAKE_TRIGGER_CRON, eveScheduleCronFiresWithin, expandEveCronField, parseEveScheduleCron, EVE_HOST_OWNED_HEADERS, EveHostError, createEveHostConfig, createEveHostLifecycleObserver, createEveHostProxy, createEveReadinessGate, createTrustedEveRequest, generateEveHostWorkerSource, resolveStableWorkersDevOrigin, } from "./eve-host.js";
2
2
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moinulmoin/eden-runtime-cloudflare",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Cloudflare Worker/Container host runtime for Eden's Eve deployments.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",