@supacloud/elysia 0.41.0 → 0.42.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.
package/dist/bun-aot.js CHANGED
@@ -1691,6 +1691,95 @@ function cloneValue(value) {
1691
1691
  }
1692
1692
  // src/worker.ts
1693
1693
  import { PendingWorkRegistry } from "@supacloud/app/runtime";
1694
+ function createRabbitMqQueuePort(options) {
1695
+ if (!options.channel || typeof options.channel.consume !== "function" || typeof options.channel.ack !== "function" || typeof options.channel.nack !== "function" || !/^[A-Za-z0-9_.:-]{1,255}$/.test(options.queue)) {
1696
+ throw new TypeError("RabbitMQ queue port options are invalid");
1697
+ }
1698
+ const prefetch = options.prefetch ?? 1;
1699
+ const timeoutMs = options.receiveTimeoutMs ?? 1000;
1700
+ if (!Number.isSafeInteger(prefetch) || prefetch < 1 || prefetch > 4096 || !Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 300000) {
1701
+ throw new TypeError("RabbitMQ queue port limits are invalid");
1702
+ }
1703
+ const buffered = [];
1704
+ const pending = [];
1705
+ const deliveries = new Map;
1706
+ let consumer;
1707
+ let starting;
1708
+ let closed = false;
1709
+ const deliver = (message) => {
1710
+ if (message === null)
1711
+ return;
1712
+ const id = String(message.fields.deliveryTag);
1713
+ let payload;
1714
+ try {
1715
+ payload = JSON.parse(new TextDecoder().decode(message.content));
1716
+ } catch {
1717
+ options.channel.nack(message, false, false);
1718
+ return;
1719
+ }
1720
+ const value = { id, payload };
1721
+ deliveries.set(id, message);
1722
+ const waiter = pending.shift();
1723
+ if (waiter)
1724
+ waiter(value);
1725
+ else
1726
+ buffered.push({ message, value });
1727
+ };
1728
+ const start = async () => {
1729
+ if (closed)
1730
+ throw new Error("RABBITMQ_QUEUE_CLOSED");
1731
+ if (!starting) {
1732
+ starting = Promise.resolve(options.channel.prefetch(prefetch)).then(() => options.channel.consume(options.queue, deliver, { noAck: false })).then((value) => {
1733
+ consumer = value;
1734
+ });
1735
+ }
1736
+ await starting;
1737
+ };
1738
+ const receive = async () => {
1739
+ await start();
1740
+ const item = buffered.shift();
1741
+ if (item)
1742
+ return item.value;
1743
+ return new Promise((resolve) => {
1744
+ const timer = setTimeout(() => {
1745
+ const index = pending.indexOf(resolve);
1746
+ if (index >= 0)
1747
+ pending.splice(index, 1);
1748
+ resolve(null);
1749
+ }, timeoutMs);
1750
+ pending.push((value) => {
1751
+ clearTimeout(timer);
1752
+ resolve(value);
1753
+ });
1754
+ });
1755
+ };
1756
+ return {
1757
+ receive,
1758
+ ack(messageId) {
1759
+ const message = deliveries.get(String(messageId));
1760
+ if (!message)
1761
+ throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
1762
+ deliveries.delete(String(messageId));
1763
+ options.channel.ack(message);
1764
+ },
1765
+ fail(messageId, failure) {
1766
+ const message = deliveries.get(String(messageId));
1767
+ if (!message)
1768
+ throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
1769
+ deliveries.delete(String(messageId));
1770
+ options.channel.nack(message, false, failure?.deadLetter !== true);
1771
+ },
1772
+ async close() {
1773
+ closed = true;
1774
+ for (const resolve of pending.splice(0))
1775
+ resolve(null);
1776
+ if (consumer)
1777
+ await options.channel.cancel(consumer.consumerTag);
1778
+ consumer = undefined;
1779
+ starting = undefined;
1780
+ }
1781
+ };
1782
+ }
1694
1783
  function workerFailureMessage(error) {
1695
1784
  const message = error instanceof Error ? error.message : typeof error === "string" ? error : "Worker job failed";
1696
1785
  const normalized = message.replace(/[\u0000-\u001f\u007f]/g, " ").trim();
@@ -2362,6 +2451,8 @@ function selectCommandBranch(command, input) {
2362
2451
  };
2363
2452
  }
2364
2453
  }
2454
+ if (command.branchesFallback === "command")
2455
+ return command;
2365
2456
  throw new ApplicationError(`Command "${command.name}" has no matching RPC branch`, {
2366
2457
  status: 422,
2367
2458
  code: "COMMAND_BRANCH_UNMATCHED"
@@ -2577,6 +2668,11 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
2577
2668
  });
2578
2669
  }
2579
2670
  for (const command of compiled.commands ?? []) {
2671
+ if (command.branchesFallback !== undefined && command.branchesFallback !== "reject" && command.branchesFallback !== "command") {
2672
+ throw new ApplicationError(`Command "${command.name}" has an invalid branches fallback`, {
2673
+ code: "COMMAND_BRANCH_FALLBACK_INVALID"
2674
+ });
2675
+ }
2580
2676
  if (command.rpc !== undefined && command.branches !== undefined) {
2581
2677
  throw new ApplicationError(`Command "${command.name}" declares both rpc and branches`, {
2582
2678
  code: "COMMAND_BRANCHES_CONFLICT"
@@ -67,6 +67,7 @@ export interface CompiledRoute {
67
67
  export interface CompiledCommand {
68
68
  rpc?: string;
69
69
  branches?: readonly CompiledCommandBranch[];
70
+ branchesFallback?: "reject" | "command";
70
71
  className: string;
71
72
  name: string;
72
73
  permission: string;
package/dist/index.d.ts CHANGED
@@ -97,6 +97,7 @@ export type CompiledControllerInvokerTarget = Record<string, (...args: unknown[]
97
97
  export interface CompiledCommand {
98
98
  rpc?: string;
99
99
  branches?: readonly CompiledCommandBranch[];
100
+ branchesFallback?: "reject" | "command";
100
101
  className: string;
101
102
  name: string;
102
103
  permission?: string;
@@ -504,5 +505,5 @@ export { createMemorySandbox, } from "./memory";
504
505
  export type { HandleLike as MemoryHandleLike, MemoryDatabase, MemorySandbox, MemorySandboxOptions, MemoryStorage, MemoryStorageObject, } from "./memory";
505
506
  export { createMemoryPolicy } from "./memory_policy";
506
507
  export type { MemoryPolicy } from "./memory_policy";
507
- export { createQueueWorkerTransport, createWorker, SupaCloudWorker, WorkerRegistrationError, WorkerReceiptUnconfirmedError, } from "./worker";
508
- export type { WorkerAcknowledge, WorkerClaim, WorkerFail, WorkerOptions, QueueWorkerTransportOptions, WorkerQueueFailureOptions, WorkerQueueMessage, WorkerQueuePort, WorkerQueueReceiveOptions, WorkerReceiptContext, WorkerReceiptOperation, WorkerRunResult, WorkerState, WorkerTransport, } from "./worker";
508
+ export { createQueueWorkerTransport, createRabbitMqQueuePort, createWorker, SupaCloudWorker, WorkerRegistrationError, WorkerReceiptUnconfirmedError, } from "./worker";
509
+ export type { WorkerAcknowledge, WorkerClaim, WorkerFail, WorkerOptions, QueueWorkerTransportOptions, WorkerQueueFailureOptions, WorkerQueueMessage, WorkerQueuePort, WorkerQueueReceiveOptions, RabbitMqChannel, RabbitMqMessage, RabbitMqQueuePortOptions, WorkerReceiptContext, WorkerReceiptOperation, WorkerRunResult, WorkerState, WorkerTransport, } from "./worker";
package/dist/index.js CHANGED
@@ -1691,6 +1691,95 @@ function cloneValue(value) {
1691
1691
  }
1692
1692
  // src/worker.ts
1693
1693
  import { PendingWorkRegistry } from "@supacloud/app/runtime";
1694
+ function createRabbitMqQueuePort(options) {
1695
+ if (!options.channel || typeof options.channel.consume !== "function" || typeof options.channel.ack !== "function" || typeof options.channel.nack !== "function" || !/^[A-Za-z0-9_.:-]{1,255}$/.test(options.queue)) {
1696
+ throw new TypeError("RabbitMQ queue port options are invalid");
1697
+ }
1698
+ const prefetch = options.prefetch ?? 1;
1699
+ const timeoutMs = options.receiveTimeoutMs ?? 1000;
1700
+ if (!Number.isSafeInteger(prefetch) || prefetch < 1 || prefetch > 4096 || !Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 300000) {
1701
+ throw new TypeError("RabbitMQ queue port limits are invalid");
1702
+ }
1703
+ const buffered = [];
1704
+ const pending = [];
1705
+ const deliveries = new Map;
1706
+ let consumer;
1707
+ let starting;
1708
+ let closed = false;
1709
+ const deliver = (message) => {
1710
+ if (message === null)
1711
+ return;
1712
+ const id = String(message.fields.deliveryTag);
1713
+ let payload;
1714
+ try {
1715
+ payload = JSON.parse(new TextDecoder().decode(message.content));
1716
+ } catch {
1717
+ options.channel.nack(message, false, false);
1718
+ return;
1719
+ }
1720
+ const value = { id, payload };
1721
+ deliveries.set(id, message);
1722
+ const waiter = pending.shift();
1723
+ if (waiter)
1724
+ waiter(value);
1725
+ else
1726
+ buffered.push({ message, value });
1727
+ };
1728
+ const start = async () => {
1729
+ if (closed)
1730
+ throw new Error("RABBITMQ_QUEUE_CLOSED");
1731
+ if (!starting) {
1732
+ starting = Promise.resolve(options.channel.prefetch(prefetch)).then(() => options.channel.consume(options.queue, deliver, { noAck: false })).then((value) => {
1733
+ consumer = value;
1734
+ });
1735
+ }
1736
+ await starting;
1737
+ };
1738
+ const receive = async () => {
1739
+ await start();
1740
+ const item = buffered.shift();
1741
+ if (item)
1742
+ return item.value;
1743
+ return new Promise((resolve) => {
1744
+ const timer = setTimeout(() => {
1745
+ const index = pending.indexOf(resolve);
1746
+ if (index >= 0)
1747
+ pending.splice(index, 1);
1748
+ resolve(null);
1749
+ }, timeoutMs);
1750
+ pending.push((value) => {
1751
+ clearTimeout(timer);
1752
+ resolve(value);
1753
+ });
1754
+ });
1755
+ };
1756
+ return {
1757
+ receive,
1758
+ ack(messageId) {
1759
+ const message = deliveries.get(String(messageId));
1760
+ if (!message)
1761
+ throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
1762
+ deliveries.delete(String(messageId));
1763
+ options.channel.ack(message);
1764
+ },
1765
+ fail(messageId, failure) {
1766
+ const message = deliveries.get(String(messageId));
1767
+ if (!message)
1768
+ throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
1769
+ deliveries.delete(String(messageId));
1770
+ options.channel.nack(message, false, failure?.deadLetter !== true);
1771
+ },
1772
+ async close() {
1773
+ closed = true;
1774
+ for (const resolve of pending.splice(0))
1775
+ resolve(null);
1776
+ if (consumer)
1777
+ await options.channel.cancel(consumer.consumerTag);
1778
+ consumer = undefined;
1779
+ starting = undefined;
1780
+ }
1781
+ };
1782
+ }
1694
1783
  function workerFailureMessage(error) {
1695
1784
  const message = error instanceof Error ? error.message : typeof error === "string" ? error : "Worker job failed";
1696
1785
  const normalized = message.replace(/[\u0000-\u001f\u007f]/g, " ").trim();
@@ -2362,6 +2451,8 @@ function selectCommandBranch(command, input) {
2362
2451
  };
2363
2452
  }
2364
2453
  }
2454
+ if (command.branchesFallback === "command")
2455
+ return command;
2365
2456
  throw new ApplicationError(`Command "${command.name}" has no matching RPC branch`, {
2366
2457
  status: 422,
2367
2458
  code: "COMMAND_BRANCH_UNMATCHED"
@@ -2577,6 +2668,11 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
2577
2668
  });
2578
2669
  }
2579
2670
  for (const command of compiled.commands ?? []) {
2671
+ if (command.branchesFallback !== undefined && command.branchesFallback !== "reject" && command.branchesFallback !== "command") {
2672
+ throw new ApplicationError(`Command "${command.name}" has an invalid branches fallback`, {
2673
+ code: "COMMAND_BRANCH_FALLBACK_INVALID"
2674
+ });
2675
+ }
2580
2676
  if (command.rpc !== undefined && command.branches !== undefined) {
2581
2677
  throw new ApplicationError(`Command "${command.name}" declares both rpc and branches`, {
2582
2678
  code: "COMMAND_BRANCHES_CONFLICT"
@@ -3320,6 +3416,7 @@ export {
3320
3416
  createPersistentCommandAdapter,
3321
3417
  createPostgresHttpPolicyStores,
3322
3418
  createQueueWorkerTransport,
3419
+ createRabbitMqQueuePort,
3323
3420
  createSchemaDecoder,
3324
3421
  createSupAuthRequestContext,
3325
3422
  createSupaCloudFramework,
package/dist/worker.d.ts CHANGED
@@ -31,6 +31,39 @@ export interface WorkerQueueFailureOptions {
31
31
  error?: string;
32
32
  deadLetter?: boolean;
33
33
  }
34
+ /** Minimal AMQP 0-9-1 channel shape; keeps RabbitMQ optional at package level. */
35
+ export interface RabbitMqChannel {
36
+ prefetch(count: number): Promise<void> | void;
37
+ consume(queue: string, onMessage: (message: RabbitMqMessage | null) => void, options?: {
38
+ noAck?: boolean;
39
+ }): Promise<{
40
+ consumerTag: string;
41
+ }>;
42
+ ack(message: RabbitMqMessage): void;
43
+ nack(message: RabbitMqMessage, allUpTo?: boolean, requeue?: boolean): void;
44
+ cancel(consumerTag: string): Promise<void> | void;
45
+ }
46
+ export interface RabbitMqMessage {
47
+ content: Uint8Array;
48
+ fields: {
49
+ deliveryTag: number;
50
+ };
51
+ properties?: Readonly<Record<string, unknown>>;
52
+ }
53
+ export interface RabbitMqQueuePortOptions {
54
+ queue: string;
55
+ channel: RabbitMqChannel;
56
+ prefetch?: number;
57
+ receiveTimeoutMs?: number;
58
+ }
59
+ /**
60
+ * Adapts an AMQP consumer to the same pull/ack/fail port used by the native
61
+ * SupaCloud queue client. The channel owns redelivery; the worker owns JSON
62
+ * decoding and business idempotency.
63
+ */
64
+ export declare function createRabbitMqQueuePort(options: RabbitMqQueuePortOptions): WorkerQueuePort & {
65
+ close(): Promise<void>;
66
+ };
34
67
  /** Structural port implemented by `client.queue(name)` without a package dependency. */
35
68
  export interface WorkerQueuePort<TMessage extends WorkerQueueMessage = WorkerQueueMessage, TReceipt = unknown> {
36
69
  receive(options?: WorkerQueueReceiveOptions): Promise<TMessage | null>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supacloud/elysia",
3
- "version": "0.41.0",
3
+ "version": "0.42.0",
4
4
  "description": "Elysia runtime adapter for SupaCloud compiled modules: application/request scopes, route registration and validation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -54,7 +54,7 @@
54
54
  "directory": "packages/elysia"
55
55
  },
56
56
  "dependencies": {
57
- "@supacloud/app": "0.39.0",
57
+ "@supacloud/app": "0.39.1",
58
58
  "@supacloud/commands": "0.12.0",
59
59
  "@supacloud/contracts": "0.16.0",
60
60
  "effect": "4.0.5",
@@ -63,7 +63,7 @@
63
63
  "typebox": "1.4.3"
64
64
  },
65
65
  "overrides": {
66
- "@supacloud/delivery": "0.25.0",
66
+ "@supacloud/delivery": "0.26.0",
67
67
  "@supacloud/contracts": "0.16.0",
68
68
  "@supacloud/commands": "0.12.0",
69
69
  "@graphql-tools/utils": "^12.0.4",
@@ -74,9 +74,9 @@
74
74
  "elysia": "2.0.0-rc.1"
75
75
  },
76
76
  "devDependencies": {
77
- "@supacloud/compiler": "0.58.0",
78
- "@supacloud/delivery": "0.25.0",
79
- "@supacloud/db": "0.22.0",
77
+ "@supacloud/compiler": "0.59.0",
78
+ "@supacloud/delivery": "0.26.0",
79
+ "@supacloud/db": "0.23.1",
80
80
  "@types/bun": "^1.4.4",
81
81
  "elysia": "2.0.0-rc.1",
82
82
  "typescript": "7.1.0-beta"