@lunora/scheduler 0.0.0 → 1.0.0-alpha.2

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,20 @@
1
+ import { CronExpressionParser } from 'cron-parser';
2
+
3
+ const isValidCronExpression = (schedule) => {
4
+ if (typeof schedule !== "string" || schedule.trim() === "") {
5
+ return false;
6
+ }
7
+ try {
8
+ CronExpressionParser.parse(schedule.trim());
9
+ return true;
10
+ } catch {
11
+ return false;
12
+ }
13
+ };
14
+ const assertValidCronExpression = (schedule, context = "cron expression") => {
15
+ if (!isValidCronExpression(schedule)) {
16
+ throw new Error(`@lunora/scheduler: invalid ${context} "${schedule}" — expected a standard 5- or 6-field cron expression (e.g. "0 * * * *")`);
17
+ }
18
+ };
19
+
20
+ export { assertValidCronExpression, isValidCronExpression };
@@ -0,0 +1,27 @@
1
+ import { assertValidCronExpression } from './assertValidCronExpression-BLfrDgmK.mjs';
2
+
3
+ const createCronTrigger = (options) => {
4
+ if (!options.schedule || !options.fn) {
5
+ throw new Error("@lunora/scheduler: createCronTrigger() requires `schedule` and `fn`");
6
+ }
7
+ assertValidCronExpression(options.schedule);
8
+ const snippet = JSON.stringify(
9
+ {
10
+ triggers: {
11
+ crons: [options.schedule]
12
+ }
13
+ },
14
+ void 0,
15
+ 2
16
+ );
17
+ return {
18
+ crons: [options.schedule],
19
+ dispatcher: {
20
+ args: options.args ?? {},
21
+ functionPath: options.fn.__lunoraRef
22
+ },
23
+ wranglerJsonc: snippet
24
+ };
25
+ };
26
+
27
+ export { createCronTrigger };
@@ -0,0 +1,59 @@
1
+ const trimTrailingSlashes = (value) => {
2
+ let end = value.length;
3
+ while (end > 0 && value[end - 1] === "/") {
4
+ end -= 1;
5
+ }
6
+ return value.slice(0, end);
7
+ };
8
+ const createQueueWorkpool = (options) => {
9
+ if (!options.queue) {
10
+ throw new Error("@lunora/scheduler: `queue` (a Cloudflare Queue binding) is required");
11
+ }
12
+ const enqueue = async (function_, args, enqueueOptions = {}) => {
13
+ const job = { args, functionPath: function_.__lunoraRef, shardKey: enqueueOptions.shardKey };
14
+ const sendOptions = enqueueOptions.delaySeconds === void 0 ? void 0 : { delaySeconds: enqueueOptions.delaySeconds };
15
+ await options.queue.send(job, sendOptions);
16
+ };
17
+ const enqueueBatch = async (jobs, sendOptions) => {
18
+ const messages = jobs.map((job) => {
19
+ return { body: { args: job.args, functionPath: job.ref.__lunoraRef, shardKey: job.shardKey } };
20
+ });
21
+ await options.queue.sendBatch(messages, sendOptions);
22
+ };
23
+ return { enqueue, enqueueBatch };
24
+ };
25
+ const isQueueJob = (value) => typeof value === "object" && value !== null && typeof value.functionPath === "string";
26
+ const createQueueConsumer = (options) => async (batch) => {
27
+ await Promise.all(
28
+ batch.messages.map(async (message) => {
29
+ try {
30
+ if (!isQueueJob(message.body)) {
31
+ throw new Error("@lunora/scheduler: queue message body is not a QueueJob (missing functionPath)");
32
+ }
33
+ await options.dispatch(message.body);
34
+ message.ack();
35
+ } catch {
36
+ message.retry();
37
+ }
38
+ })
39
+ );
40
+ };
41
+ const httpDispatcher = (options) => {
42
+ const fetchImpl = options.fetchImpl ?? globalThis.fetch;
43
+ if (typeof fetchImpl !== "function") {
44
+ throw new TypeError("@lunora/scheduler: no fetch implementation available — pass fetchImpl or run on a platform with global fetch");
45
+ }
46
+ const url = `${trimTrailingSlashes(options.originUrl)}/_lunora/scheduler/dispatch`;
47
+ return async (job) => {
48
+ const response = await fetchImpl(url, {
49
+ body: JSON.stringify({ args: job.args ?? {}, functionPath: job.functionPath, shardKey: job.shardKey }),
50
+ headers: { authorization: `Bearer ${options.adminToken}`, "content-type": "application/json" },
51
+ method: "POST"
52
+ });
53
+ if (!response.ok) {
54
+ throw new Error(`@lunora/scheduler: queue dispatch failed (${response.status.toString()}): ${await response.text()}`);
55
+ }
56
+ };
57
+ };
58
+
59
+ export { createQueueConsumer, createQueueWorkpool, httpDispatcher };
@@ -0,0 +1,69 @@
1
+ import { a as applyJurisdiction } from './jurisdiction-CR2zC3Et.mjs';
2
+
3
+ const schedulerStub = (options) => {
4
+ const namespace = applyJurisdiction(options.namespace, options.jurisdiction);
5
+ return namespace.get(namespace.idFromName(options.instanceName ?? "default"));
6
+ };
7
+ const callDO = async (options, path, body) => {
8
+ const stub = schedulerStub(options);
9
+ const response = await stub.fetch(`https://scheduler.internal${path}`, {
10
+ body: JSON.stringify(body),
11
+ headers: { "content-type": "application/json" },
12
+ method: "POST"
13
+ });
14
+ if (!response.ok) {
15
+ const text = await response.text();
16
+ throw new Error(`@lunora/scheduler: SchedulerDO ${path} failed (${String(response.status)}): ${text}`);
17
+ }
18
+ return await response.json();
19
+ };
20
+ const getDO = async (options, path) => {
21
+ const stub = schedulerStub(options);
22
+ const response = await stub.fetch(`https://scheduler.internal${path}`, { method: "GET" });
23
+ if (!response.ok) {
24
+ const text = await response.text();
25
+ throw new Error(`@lunora/scheduler: SchedulerDO ${path} failed (${String(response.status)}): ${text}`);
26
+ }
27
+ return await response.json();
28
+ };
29
+ const createScheduler = (options) => {
30
+ if (!options.namespace) {
31
+ throw new Error("@lunora/scheduler: `namespace` (SchedulerDO binding) is required");
32
+ }
33
+ if (!options.originUrl) {
34
+ throw new Error("@lunora/scheduler: `originUrl` is required so the DO can dispatch back to the Worker");
35
+ }
36
+ const runAt = async (date, function_, args, options_ = {}) => {
37
+ const scheduledFor = date instanceof Date ? date.getTime() : date;
38
+ return callDO(options, "/schedule", {
39
+ args,
40
+ functionPath: function_.__lunoraRef,
41
+ originUrl: options.originUrl,
42
+ // Optional workpool / retry-policy passthrough. Absent for ordinary
43
+ // `runAfter`/`runAt` calls, which keeps the wire payload (and the
44
+ // DO's behaviour) identical to before this feature.
45
+ pool: options_.pool,
46
+ retry: options_.retry,
47
+ scheduledFor,
48
+ shardKey: options_.shardKey
49
+ });
50
+ };
51
+ const runAfter = async (delayMs, function_, args, options_ = {}) => {
52
+ if (!Number.isFinite(delayMs) || delayMs < 0) {
53
+ throw new Error("@lunora/scheduler: `delayMs` must be a non-negative finite number");
54
+ }
55
+ return runAt(Date.now() + delayMs, function_, args, options_);
56
+ };
57
+ const cancel = async (id) => callDO(options, "/cancel", { id });
58
+ const list = async () => {
59
+ const body = await getDO(options, "/list");
60
+ return Array.isArray(body.records) ? body.records : [];
61
+ };
62
+ const get = async (id) => {
63
+ const body = await getDO(options, `/get?id=${encodeURIComponent(id)}`);
64
+ return body.record ?? null;
65
+ };
66
+ return { cancel, get, list, runAfter, runAt };
67
+ };
68
+
69
+ export { createScheduler as default };
@@ -0,0 +1,62 @@
1
+ import { a as applyJurisdiction } from './jurisdiction-CR2zC3Et.mjs';
2
+
3
+ const workpoolStub = (options) => {
4
+ const namespace = applyJurisdiction(options.namespace, options.jurisdiction);
5
+ return namespace.get(namespace.idFromName(options.instanceName ?? "default"));
6
+ };
7
+ const callDO = async (options, path, body) => {
8
+ const stub = workpoolStub(options);
9
+ const response = await stub.fetch(`https://scheduler.internal${path}`, {
10
+ body: JSON.stringify(body),
11
+ headers: { "content-type": "application/json" },
12
+ method: "POST"
13
+ });
14
+ if (!response.ok) {
15
+ const text = await response.text();
16
+ throw new Error(`@lunora/scheduler: SchedulerDO ${path} failed (${String(response.status)}): ${text}`);
17
+ }
18
+ return await response.json();
19
+ };
20
+ const getDO = async (options, path) => {
21
+ const stub = workpoolStub(options);
22
+ const response = await stub.fetch(`https://scheduler.internal${path}`, { method: "GET" });
23
+ if (!response.ok) {
24
+ const text = await response.text();
25
+ throw new Error(`@lunora/scheduler: SchedulerDO ${path} failed (${String(response.status)}): ${text}`);
26
+ }
27
+ return await response.json();
28
+ };
29
+ const createWorkpool = (options) => {
30
+ if (!options.namespace) {
31
+ throw new Error("@lunora/scheduler: `namespace` (SchedulerDO binding) is required");
32
+ }
33
+ if (!options.originUrl) {
34
+ throw new Error("@lunora/scheduler: `originUrl` is required so the DO can dispatch back to the Worker");
35
+ }
36
+ if (!Number.isInteger(options.maxConcurrency) || options.maxConcurrency <= 0) {
37
+ throw new Error("@lunora/scheduler: `maxConcurrency` must be a positive integer");
38
+ }
39
+ const name = typeof options.name === "string" && options.name.length > 0 ? options.name : "default";
40
+ const enqueue = async (function_, args, options_ = {}) => {
41
+ const delayMs = options_.delayMs ?? 0;
42
+ if (!Number.isFinite(delayMs) || delayMs < 0) {
43
+ throw new Error("@lunora/scheduler: `delayMs` must be a non-negative finite number");
44
+ }
45
+ return callDO(options, "/schedule", {
46
+ args,
47
+ functionPath: function_.__lunoraRef,
48
+ instanceName: options.instanceName ?? "default",
49
+ maxConcurrency: options.maxConcurrency,
50
+ originUrl: options.originUrl,
51
+ pool: name,
52
+ retry: options_.retry,
53
+ scheduledFor: Date.now() + delayMs,
54
+ shardKey: options_.shardKey
55
+ });
56
+ };
57
+ const cancel = async (id) => callDO(options, "/cancel", { id });
58
+ const status = async () => getDO(options, `/pool?name=${encodeURIComponent(name)}`);
59
+ return { cancel, enqueue, name, status };
60
+ };
61
+
62
+ export { createWorkpool as default };
@@ -0,0 +1,3 @@
1
+ const isWorkflowReference = (target) => typeof target === "object" && target !== null && target.isLunoraWorkflow === true;
2
+
3
+ export { isWorkflowReference };
@@ -0,0 +1,13 @@
1
+ const applyJurisdiction = (namespace, jurisdiction) => {
2
+ if (jurisdiction === void 0) {
3
+ return namespace;
4
+ }
5
+ if (typeof namespace.jurisdiction !== "function") {
6
+ throw new TypeError(
7
+ `@lunora/scheduler: Durable Object namespace does not support jurisdiction("${jurisdiction}") — update @cloudflare/workers-types or remove the jurisdiction option`
8
+ );
9
+ }
10
+ return namespace.jurisdiction(jurisdiction);
11
+ };
12
+
13
+ export { applyJurisdiction as a };
package/package.json CHANGED
@@ -1,31 +1,54 @@
1
1
  {
2
2
  "name": "@lunora/scheduler",
3
- "version": "0.0.0",
3
+ "version": "1.0.0-alpha.2",
4
4
  "description": "Scheduling for Lunora: runAfter / runAt and Cron Triggers via SchedulerDO",
5
- "license": "FSL-1.1-Apache-2.0",
5
+ "keywords": [
6
+ "cloudflare",
7
+ "cron",
8
+ "durable-objects",
9
+ "lunora",
10
+ "queues",
11
+ "scheduler",
12
+ "workers",
13
+ "workpool"
14
+ ],
6
15
  "homepage": "https://lunora.sh",
16
+ "bugs": "https://github.com/anolilab/lunora/issues",
17
+ "license": "FSL-1.1-Apache-2.0",
18
+ "author": {
19
+ "name": "Daniel Bannert",
20
+ "email": "d.bannert@anolilab.de"
21
+ },
7
22
  "repository": {
8
23
  "type": "git",
9
24
  "url": "git+https://github.com/anolilab/lunora.git",
10
25
  "directory": "packages/scheduler"
11
26
  },
12
- "bugs": {
13
- "url": "https://github.com/anolilab/lunora/issues"
14
- },
15
- "keywords": [
16
- "lunora",
17
- "cloudflare",
18
- "workers",
19
- "durable-objects",
20
- "scheduler",
21
- "cron",
22
- "queues",
23
- "workpool"
27
+ "files": [
28
+ "dist",
29
+ "__assets__",
30
+ "README.md",
31
+ "LICENSE.md"
24
32
  ],
33
+ "type": "module",
34
+ "sideEffects": false,
35
+ "main": "./dist/index.mjs",
36
+ "module": "./dist/index.mjs",
37
+ "types": "./dist/index.d.ts",
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/index.d.ts",
41
+ "import": "./dist/index.mjs"
42
+ },
43
+ "./package.json": "./package.json"
44
+ },
25
45
  "publishConfig": {
26
46
  "access": "public"
27
47
  },
28
- "files": [
29
- "README.md"
30
- ]
48
+ "dependencies": {
49
+ "cron-parser": "5.5.0"
50
+ },
51
+ "engines": {
52
+ "node": "^22.15.0 || >=24.11.0"
53
+ }
31
54
  }