@supacloud/elysia 0.41.0 → 0.42.1
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 +117 -0
- package/dist/fixtures/webhook-generated/application.d.ts +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +118 -0
- package/dist/worker.d.ts +33 -0
- package/package.json +8 -8
package/dist/bun-aot.js
CHANGED
|
@@ -1691,6 +1691,116 @@ 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 closing;
|
|
1709
|
+
let closed = false;
|
|
1710
|
+
const deliver = (message) => {
|
|
1711
|
+
if (message === null)
|
|
1712
|
+
return;
|
|
1713
|
+
if (closed) {
|
|
1714
|
+
options.channel.nack(message, false, true);
|
|
1715
|
+
return;
|
|
1716
|
+
}
|
|
1717
|
+
const id = String(message.fields.deliveryTag);
|
|
1718
|
+
let payload;
|
|
1719
|
+
try {
|
|
1720
|
+
payload = JSON.parse(new TextDecoder().decode(message.content));
|
|
1721
|
+
} catch {
|
|
1722
|
+
options.channel.nack(message, false, false);
|
|
1723
|
+
return;
|
|
1724
|
+
}
|
|
1725
|
+
const value = { id, payload };
|
|
1726
|
+
deliveries.set(id, message);
|
|
1727
|
+
const waiter = pending.shift();
|
|
1728
|
+
if (waiter)
|
|
1729
|
+
waiter(value);
|
|
1730
|
+
else
|
|
1731
|
+
buffered.push({ message, value });
|
|
1732
|
+
};
|
|
1733
|
+
const start = async () => {
|
|
1734
|
+
if (closed)
|
|
1735
|
+
throw new Error("RABBITMQ_QUEUE_CLOSED");
|
|
1736
|
+
if (!starting) {
|
|
1737
|
+
starting = Promise.resolve(options.channel.prefetch(prefetch)).then(() => options.channel.consume(options.queue, deliver, { noAck: false })).then((value) => {
|
|
1738
|
+
if (closed) {
|
|
1739
|
+
return Promise.resolve(options.channel.cancel(value.consumerTag)).then(() => {
|
|
1740
|
+
return;
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
consumer = value;
|
|
1744
|
+
});
|
|
1745
|
+
}
|
|
1746
|
+
await starting;
|
|
1747
|
+
if (closed)
|
|
1748
|
+
throw new Error("RABBITMQ_QUEUE_CLOSED");
|
|
1749
|
+
};
|
|
1750
|
+
const receive = async () => {
|
|
1751
|
+
await start();
|
|
1752
|
+
const item = buffered.shift();
|
|
1753
|
+
if (item)
|
|
1754
|
+
return item.value;
|
|
1755
|
+
return new Promise((resolve) => {
|
|
1756
|
+
const timer = setTimeout(() => {
|
|
1757
|
+
const index = pending.indexOf(resolve);
|
|
1758
|
+
if (index >= 0)
|
|
1759
|
+
pending.splice(index, 1);
|
|
1760
|
+
resolve(null);
|
|
1761
|
+
}, timeoutMs);
|
|
1762
|
+
pending.push((value) => {
|
|
1763
|
+
clearTimeout(timer);
|
|
1764
|
+
resolve(value);
|
|
1765
|
+
});
|
|
1766
|
+
});
|
|
1767
|
+
};
|
|
1768
|
+
return {
|
|
1769
|
+
receive,
|
|
1770
|
+
ack(messageId) {
|
|
1771
|
+
const message = deliveries.get(String(messageId));
|
|
1772
|
+
if (!message)
|
|
1773
|
+
throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
|
|
1774
|
+
deliveries.delete(String(messageId));
|
|
1775
|
+
options.channel.ack(message);
|
|
1776
|
+
},
|
|
1777
|
+
fail(messageId, failure) {
|
|
1778
|
+
const message = deliveries.get(String(messageId));
|
|
1779
|
+
if (!message)
|
|
1780
|
+
throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
|
|
1781
|
+
deliveries.delete(String(messageId));
|
|
1782
|
+
options.channel.nack(message, false, failure?.deadLetter !== true);
|
|
1783
|
+
},
|
|
1784
|
+
async close() {
|
|
1785
|
+
if (closing)
|
|
1786
|
+
return closing;
|
|
1787
|
+
closed = true;
|
|
1788
|
+
for (const resolve of pending.splice(0))
|
|
1789
|
+
resolve(null);
|
|
1790
|
+
const inFlight = starting;
|
|
1791
|
+
closing = (async () => {
|
|
1792
|
+
try {
|
|
1793
|
+
await inFlight;
|
|
1794
|
+
} catch {}
|
|
1795
|
+
if (consumer)
|
|
1796
|
+
await options.channel.cancel(consumer.consumerTag);
|
|
1797
|
+
consumer = undefined;
|
|
1798
|
+
starting = undefined;
|
|
1799
|
+
})();
|
|
1800
|
+
return closing;
|
|
1801
|
+
}
|
|
1802
|
+
};
|
|
1803
|
+
}
|
|
1694
1804
|
function workerFailureMessage(error) {
|
|
1695
1805
|
const message = error instanceof Error ? error.message : typeof error === "string" ? error : "Worker job failed";
|
|
1696
1806
|
const normalized = message.replace(/[\u0000-\u001f\u007f]/g, " ").trim();
|
|
@@ -2362,6 +2472,8 @@ function selectCommandBranch(command, input) {
|
|
|
2362
2472
|
};
|
|
2363
2473
|
}
|
|
2364
2474
|
}
|
|
2475
|
+
if (command.branchesFallback === "command")
|
|
2476
|
+
return command;
|
|
2365
2477
|
throw new ApplicationError(`Command "${command.name}" has no matching RPC branch`, {
|
|
2366
2478
|
status: 422,
|
|
2367
2479
|
code: "COMMAND_BRANCH_UNMATCHED"
|
|
@@ -2577,6 +2689,11 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
|
|
|
2577
2689
|
});
|
|
2578
2690
|
}
|
|
2579
2691
|
for (const command of compiled.commands ?? []) {
|
|
2692
|
+
if (command.branchesFallback !== undefined && command.branchesFallback !== "reject" && command.branchesFallback !== "command") {
|
|
2693
|
+
throw new ApplicationError(`Command "${command.name}" has an invalid branches fallback`, {
|
|
2694
|
+
code: "COMMAND_BRANCH_FALLBACK_INVALID"
|
|
2695
|
+
});
|
|
2696
|
+
}
|
|
2580
2697
|
if (command.rpc !== undefined && command.branches !== undefined) {
|
|
2581
2698
|
throw new ApplicationError(`Command "${command.name}" declares both rpc and branches`, {
|
|
2582
2699
|
code: "COMMAND_BRANCHES_CONFLICT"
|
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,116 @@ 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 closing;
|
|
1709
|
+
let closed = false;
|
|
1710
|
+
const deliver = (message) => {
|
|
1711
|
+
if (message === null)
|
|
1712
|
+
return;
|
|
1713
|
+
if (closed) {
|
|
1714
|
+
options.channel.nack(message, false, true);
|
|
1715
|
+
return;
|
|
1716
|
+
}
|
|
1717
|
+
const id = String(message.fields.deliveryTag);
|
|
1718
|
+
let payload;
|
|
1719
|
+
try {
|
|
1720
|
+
payload = JSON.parse(new TextDecoder().decode(message.content));
|
|
1721
|
+
} catch {
|
|
1722
|
+
options.channel.nack(message, false, false);
|
|
1723
|
+
return;
|
|
1724
|
+
}
|
|
1725
|
+
const value = { id, payload };
|
|
1726
|
+
deliveries.set(id, message);
|
|
1727
|
+
const waiter = pending.shift();
|
|
1728
|
+
if (waiter)
|
|
1729
|
+
waiter(value);
|
|
1730
|
+
else
|
|
1731
|
+
buffered.push({ message, value });
|
|
1732
|
+
};
|
|
1733
|
+
const start = async () => {
|
|
1734
|
+
if (closed)
|
|
1735
|
+
throw new Error("RABBITMQ_QUEUE_CLOSED");
|
|
1736
|
+
if (!starting) {
|
|
1737
|
+
starting = Promise.resolve(options.channel.prefetch(prefetch)).then(() => options.channel.consume(options.queue, deliver, { noAck: false })).then((value) => {
|
|
1738
|
+
if (closed) {
|
|
1739
|
+
return Promise.resolve(options.channel.cancel(value.consumerTag)).then(() => {
|
|
1740
|
+
return;
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
consumer = value;
|
|
1744
|
+
});
|
|
1745
|
+
}
|
|
1746
|
+
await starting;
|
|
1747
|
+
if (closed)
|
|
1748
|
+
throw new Error("RABBITMQ_QUEUE_CLOSED");
|
|
1749
|
+
};
|
|
1750
|
+
const receive = async () => {
|
|
1751
|
+
await start();
|
|
1752
|
+
const item = buffered.shift();
|
|
1753
|
+
if (item)
|
|
1754
|
+
return item.value;
|
|
1755
|
+
return new Promise((resolve) => {
|
|
1756
|
+
const timer = setTimeout(() => {
|
|
1757
|
+
const index = pending.indexOf(resolve);
|
|
1758
|
+
if (index >= 0)
|
|
1759
|
+
pending.splice(index, 1);
|
|
1760
|
+
resolve(null);
|
|
1761
|
+
}, timeoutMs);
|
|
1762
|
+
pending.push((value) => {
|
|
1763
|
+
clearTimeout(timer);
|
|
1764
|
+
resolve(value);
|
|
1765
|
+
});
|
|
1766
|
+
});
|
|
1767
|
+
};
|
|
1768
|
+
return {
|
|
1769
|
+
receive,
|
|
1770
|
+
ack(messageId) {
|
|
1771
|
+
const message = deliveries.get(String(messageId));
|
|
1772
|
+
if (!message)
|
|
1773
|
+
throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
|
|
1774
|
+
deliveries.delete(String(messageId));
|
|
1775
|
+
options.channel.ack(message);
|
|
1776
|
+
},
|
|
1777
|
+
fail(messageId, failure) {
|
|
1778
|
+
const message = deliveries.get(String(messageId));
|
|
1779
|
+
if (!message)
|
|
1780
|
+
throw new Error("RABBITMQ_DELIVERY_UNKNOWN");
|
|
1781
|
+
deliveries.delete(String(messageId));
|
|
1782
|
+
options.channel.nack(message, false, failure?.deadLetter !== true);
|
|
1783
|
+
},
|
|
1784
|
+
async close() {
|
|
1785
|
+
if (closing)
|
|
1786
|
+
return closing;
|
|
1787
|
+
closed = true;
|
|
1788
|
+
for (const resolve of pending.splice(0))
|
|
1789
|
+
resolve(null);
|
|
1790
|
+
const inFlight = starting;
|
|
1791
|
+
closing = (async () => {
|
|
1792
|
+
try {
|
|
1793
|
+
await inFlight;
|
|
1794
|
+
} catch {}
|
|
1795
|
+
if (consumer)
|
|
1796
|
+
await options.channel.cancel(consumer.consumerTag);
|
|
1797
|
+
consumer = undefined;
|
|
1798
|
+
starting = undefined;
|
|
1799
|
+
})();
|
|
1800
|
+
return closing;
|
|
1801
|
+
}
|
|
1802
|
+
};
|
|
1803
|
+
}
|
|
1694
1804
|
function workerFailureMessage(error) {
|
|
1695
1805
|
const message = error instanceof Error ? error.message : typeof error === "string" ? error : "Worker job failed";
|
|
1696
1806
|
const normalized = message.replace(/[\u0000-\u001f\u007f]/g, " ").trim();
|
|
@@ -2362,6 +2472,8 @@ function selectCommandBranch(command, input) {
|
|
|
2362
2472
|
};
|
|
2363
2473
|
}
|
|
2364
2474
|
}
|
|
2475
|
+
if (command.branchesFallback === "command")
|
|
2476
|
+
return command;
|
|
2365
2477
|
throw new ApplicationError(`Command "${command.name}" has no matching RPC branch`, {
|
|
2366
2478
|
status: 422,
|
|
2367
2479
|
code: "COMMAND_BRANCH_UNMATCHED"
|
|
@@ -2577,6 +2689,11 @@ function assertCommandGovernanceReady(compiled, governance, executor) {
|
|
|
2577
2689
|
});
|
|
2578
2690
|
}
|
|
2579
2691
|
for (const command of compiled.commands ?? []) {
|
|
2692
|
+
if (command.branchesFallback !== undefined && command.branchesFallback !== "reject" && command.branchesFallback !== "command") {
|
|
2693
|
+
throw new ApplicationError(`Command "${command.name}" has an invalid branches fallback`, {
|
|
2694
|
+
code: "COMMAND_BRANCH_FALLBACK_INVALID"
|
|
2695
|
+
});
|
|
2696
|
+
}
|
|
2580
2697
|
if (command.rpc !== undefined && command.branches !== undefined) {
|
|
2581
2698
|
throw new ApplicationError(`Command "${command.name}" declares both rpc and branches`, {
|
|
2582
2699
|
code: "COMMAND_BRANCHES_CONFLICT"
|
|
@@ -3320,6 +3437,7 @@ export {
|
|
|
3320
3437
|
createPersistentCommandAdapter,
|
|
3321
3438
|
createPostgresHttpPolicyStores,
|
|
3322
3439
|
createQueueWorkerTransport,
|
|
3440
|
+
createRabbitMqQueuePort,
|
|
3323
3441
|
createSchemaDecoder,
|
|
3324
3442
|
createSupAuthRequestContext,
|
|
3325
3443
|
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.
|
|
3
|
+
"version": "0.42.1",
|
|
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,17 +54,17 @@
|
|
|
54
54
|
"directory": "packages/elysia"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@supacloud/app": "0.39.
|
|
57
|
+
"@supacloud/app": "0.39.1",
|
|
58
58
|
"@supacloud/commands": "0.12.0",
|
|
59
|
-
"@supacloud/contracts": "0.16.
|
|
59
|
+
"@supacloud/contracts": "0.16.1",
|
|
60
60
|
"effect": "4.0.5",
|
|
61
61
|
"exact-mirror": "1.2.6",
|
|
62
62
|
"jose": "^6.2.13",
|
|
63
63
|
"typebox": "1.4.3"
|
|
64
64
|
},
|
|
65
65
|
"overrides": {
|
|
66
|
-
"@supacloud/delivery": "0.
|
|
67
|
-
"@supacloud/contracts": "0.16.
|
|
66
|
+
"@supacloud/delivery": "0.26.0",
|
|
67
|
+
"@supacloud/contracts": "0.16.1",
|
|
68
68
|
"@supacloud/commands": "0.12.0",
|
|
69
69
|
"@graphql-tools/utils": "^12.0.4",
|
|
70
70
|
"deepmerge-ts": "8.0.2",
|
|
@@ -74,9 +74,9 @@
|
|
|
74
74
|
"elysia": "2.0.0-rc.1"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
|
-
"@supacloud/compiler": "0.
|
|
78
|
-
"@supacloud/delivery": "0.
|
|
79
|
-
"@supacloud/db": "0.
|
|
77
|
+
"@supacloud/compiler": "0.60.0",
|
|
78
|
+
"@supacloud/delivery": "0.26.0",
|
|
79
|
+
"@supacloud/db": "0.24.0",
|
|
80
80
|
"@types/bun": "^1.4.4",
|
|
81
81
|
"elysia": "2.0.0-rc.1",
|
|
82
82
|
"typescript": "7.1.0-beta"
|