nestjs-cloudflare 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +583 -2
- package/dist/bindings/get-binding-token.d.ts +6 -0
- package/dist/bindings/get-binding-token.js +8 -0
- package/dist/cloudflare.module.d.ts +11 -0
- package/dist/cloudflare.module.js +55 -0
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +6 -0
- package/dist/decorators/inject-binding.decorator.d.ts +7 -0
- package/dist/decorators/inject-binding.decorator.js +10 -0
- package/dist/durable-objects/client.d.ts +13 -0
- package/dist/durable-objects/client.js +67 -0
- package/dist/durable-objects/constants.d.ts +1 -0
- package/dist/durable-objects/constants.js +1 -0
- package/dist/durable-objects/context.d.ts +18 -0
- package/dist/durable-objects/context.js +53 -0
- package/dist/durable-objects/context.transport.d.ts +9 -0
- package/dist/durable-objects/context.transport.js +13 -0
- package/dist/durable-objects/durable-object.d.ts +11 -0
- package/dist/durable-objects/durable-object.js +21 -0
- package/dist/durable-objects/index.d.ts +9 -0
- package/dist/durable-objects/index.js +7 -0
- package/dist/durable-objects/interfaces/client-options.interface.d.ts +15 -0
- package/dist/durable-objects/interfaces/client-options.interface.js +1 -0
- package/dist/durable-objects/interfaces/server-options.interface.d.ts +14 -0
- package/dist/durable-objects/interfaces/server-options.interface.js +1 -0
- package/dist/durable-objects/record-builder.d.ts +16 -0
- package/dist/durable-objects/record-builder.js +28 -0
- package/dist/durable-objects/record.serializer.d.ts +10 -0
- package/dist/durable-objects/record.serializer.js +10 -0
- package/dist/durable-objects/server.d.ts +16 -0
- package/dist/durable-objects/server.js +101 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +9 -0
- package/dist/interfaces/worker-options.interface.d.ts +45 -0
- package/dist/interfaces/worker-options.interface.js +1 -0
- package/dist/interfaces/worker.interface.d.ts +21 -0
- package/dist/interfaces/worker.interface.js +1 -0
- package/dist/queue/client.d.ts +19 -0
- package/dist/queue/client.js +44 -0
- package/dist/queue/constants.d.ts +6 -0
- package/dist/queue/constants.js +6 -0
- package/dist/queue/context.d.ts +25 -0
- package/dist/queue/context.js +27 -0
- package/dist/queue/index.d.ts +8 -0
- package/dist/queue/index.js +6 -0
- package/dist/queue/interfaces/client-options.interface.d.ts +14 -0
- package/dist/queue/interfaces/client-options.interface.js +1 -0
- package/dist/queue/interfaces/server-options.interface.d.ts +16 -0
- package/dist/queue/interfaces/server-options.interface.js +1 -0
- package/dist/queue/record-builder.d.ts +19 -0
- package/dist/queue/record-builder.js +32 -0
- package/dist/queue/record.serializer.d.ts +10 -0
- package/dist/queue/record.serializer.js +14 -0
- package/dist/queue/server.d.ts +28 -0
- package/dist/queue/server.js +102 -0
- package/dist/schedule/constants.d.ts +3 -0
- package/dist/schedule/constants.js +3 -0
- package/dist/schedule/cron.explorer.d.ts +20 -0
- package/dist/schedule/cron.explorer.js +121 -0
- package/dist/schedule/index.d.ts +1 -0
- package/dist/schedule/index.js +1 -0
- package/dist/schedule/schedule.module.d.ts +10 -0
- package/dist/schedule/schedule.module.js +33 -0
- package/dist/websockets/adapter.d.ts +76 -0
- package/dist/websockets/adapter.js +186 -0
- package/dist/websockets/context.d.ts +12 -0
- package/dist/websockets/context.js +15 -0
- package/dist/websockets/durable-object.d.ts +24 -0
- package/dist/websockets/durable-object.js +77 -0
- package/dist/websockets/gateway-durable-object.d.ts +17 -0
- package/dist/websockets/gateway-durable-object.js +82 -0
- package/dist/websockets/index.d.ts +11 -0
- package/dist/websockets/index.js +5 -0
- package/dist/websockets/interfaces/adapter-options.interface.d.ts +14 -0
- package/dist/websockets/interfaces/adapter-options.interface.js +1 -0
- package/dist/websockets/interfaces/durable-object-options.interface.d.ts +15 -0
- package/dist/websockets/interfaces/durable-object-options.interface.js +1 -0
- package/dist/websockets/interfaces/gateway-options.interface.d.ts +11 -0
- package/dist/websockets/interfaces/gateway-options.interface.js +1 -0
- package/dist/websockets/interfaces/message.interface.d.ts +11 -0
- package/dist/websockets/interfaces/message.interface.js +1 -0
- package/dist/websockets/interfaces/server.interface.d.ts +9 -0
- package/dist/websockets/interfaces/server.interface.js +1 -0
- package/dist/websockets/invalid-message.exception.d.ts +8 -0
- package/dist/websockets/invalid-message.exception.js +11 -0
- package/dist/websockets/message-dispatcher.d.ts +8 -0
- package/dist/websockets/message-dispatcher.js +26 -0
- package/dist/worker/create-worker.d.ts +11 -0
- package/dist/worker/create-worker.js +223 -0
- package/dist/worker/request-context.d.ts +19 -0
- package/dist/worker/request-context.js +38 -0
- package/dist/worker/websocket-routing.d.ts +3 -0
- package/dist/worker/websocket-routing.js +10 -0
- package/package.json +111 -4
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { INestApplication } from '@nestjs/common';
|
|
2
|
+
type WorkerExecutionContext = ExecutionContext;
|
|
3
|
+
type IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
|
|
4
|
+
/**
|
|
5
|
+
* Worker event handlers backed by one lazily initialized Nest application.
|
|
6
|
+
*
|
|
7
|
+
* @publicApi
|
|
8
|
+
*/
|
|
9
|
+
export interface CloudflareWorker<Env = Cloudflare.Env> {
|
|
10
|
+
/** Starts the application once and returns it to native Worker or Durable Object code. */
|
|
11
|
+
getApplication(): Promise<INestApplication>;
|
|
12
|
+
/** Dispatches a request to a configured Durable Object, Nest, or the native fallback. */
|
|
13
|
+
fetch(request: IncomingRequest, env: Env, ctx: WorkerExecutionContext): Promise<Response>;
|
|
14
|
+
/** Reports whether a configured WebSocket path or Express route owns the request. */
|
|
15
|
+
matches(request: IncomingRequest): Promise<boolean>;
|
|
16
|
+
/** Dispatches an event to registered Nest cron methods or the native fallback. */
|
|
17
|
+
scheduled(controller: ScheduledController, env: Env, ctx: WorkerExecutionContext): Promise<void>;
|
|
18
|
+
/** Dispatches a batch to the application's Cloudflare Queue transport. */
|
|
19
|
+
queue(batch: MessageBatch, env: Env, ctx: WorkerExecutionContext): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ClientProxy, type ReadPacket, type WritePacket } from '@nestjs/microservices';
|
|
2
|
+
import type { CloudflareQueueClientOptions } from './interfaces/client-options.interface.js';
|
|
3
|
+
/**
|
|
4
|
+
* Publishes Nest event packets through a Cloudflare Queue binding.
|
|
5
|
+
*
|
|
6
|
+
* @publicApi
|
|
7
|
+
*/
|
|
8
|
+
export declare class CloudflareQueueClient extends ClientProxy {
|
|
9
|
+
private readonly options;
|
|
10
|
+
constructor(options: CloudflareQueueClientOptions);
|
|
11
|
+
/** Returns the Queue binding without opening a broker connection. */
|
|
12
|
+
connect(): Promise<Queue>;
|
|
13
|
+
/** Releases no resources because Workers owns the Queue binding. */
|
|
14
|
+
close(): void;
|
|
15
|
+
/** Returns the underlying Queue binding. */
|
|
16
|
+
unwrap<T>(): T;
|
|
17
|
+
protected publish(_packet: ReadPacket, callback: (packet: WritePacket) => void): () => void;
|
|
18
|
+
protected dispatchEvent<T>(packet: ReadPacket): Promise<T>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ClientProxy, } from '@nestjs/microservices';
|
|
2
|
+
import { CloudflareQueueRecordSerializer } from './record.serializer.js';
|
|
3
|
+
/**
|
|
4
|
+
* Publishes Nest event packets through a Cloudflare Queue binding.
|
|
5
|
+
*
|
|
6
|
+
* @publicApi
|
|
7
|
+
*/
|
|
8
|
+
export class CloudflareQueueClient extends ClientProxy {
|
|
9
|
+
options;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super();
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.initializeSerializer({
|
|
14
|
+
...options,
|
|
15
|
+
serializer: options.serializer ?? new CloudflareQueueRecordSerializer(),
|
|
16
|
+
});
|
|
17
|
+
this.initializeDeserializer(options);
|
|
18
|
+
}
|
|
19
|
+
/** Returns the Queue binding without opening a broker connection. */
|
|
20
|
+
async connect() {
|
|
21
|
+
return this.options.queue;
|
|
22
|
+
}
|
|
23
|
+
/** Releases no resources because Workers owns the Queue binding. */
|
|
24
|
+
close() { }
|
|
25
|
+
/** Returns the underlying Queue binding. */
|
|
26
|
+
unwrap() {
|
|
27
|
+
return this.options.queue;
|
|
28
|
+
}
|
|
29
|
+
publish(_packet, callback) {
|
|
30
|
+
callback({
|
|
31
|
+
err: new Error('Cloudflare Queues supports emit(), not request-response send()'),
|
|
32
|
+
});
|
|
33
|
+
return () => { };
|
|
34
|
+
}
|
|
35
|
+
async dispatchEvent(packet) {
|
|
36
|
+
const pattern = this.normalizePattern(packet.pattern);
|
|
37
|
+
const { options, ...body } = await this.serializer.serialize({
|
|
38
|
+
...packet,
|
|
39
|
+
pattern,
|
|
40
|
+
});
|
|
41
|
+
await this.options.queue.send(body, options);
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { BaseRpcContext } from '@nestjs/microservices';
|
|
2
|
+
type WorkerExecutionContext = ExecutionContext;
|
|
3
|
+
type CloudflareQueueContextArgs = [
|
|
4
|
+
Message,
|
|
5
|
+
MessageBatch,
|
|
6
|
+
WorkerExecutionContext,
|
|
7
|
+
string
|
|
8
|
+
];
|
|
9
|
+
/**
|
|
10
|
+
* Exposes the native Queue event and matched Nest pattern to a handler.
|
|
11
|
+
*
|
|
12
|
+
* @publicApi
|
|
13
|
+
*/
|
|
14
|
+
export declare class CloudflareQueueContext extends BaseRpcContext<CloudflareQueueContextArgs> {
|
|
15
|
+
constructor(args: CloudflareQueueContextArgs);
|
|
16
|
+
/** Returns the message, including acknowledgement and retry methods. */
|
|
17
|
+
getMessage(): Message;
|
|
18
|
+
/** Returns the batch containing this message. */
|
|
19
|
+
getBatch(): MessageBatch;
|
|
20
|
+
/** Returns the Worker execution context for background work. */
|
|
21
|
+
getExecutionContext(): WorkerExecutionContext;
|
|
22
|
+
/** Returns the normalized Nest event pattern. */
|
|
23
|
+
getPattern(): string;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BaseRpcContext } from '@nestjs/microservices';
|
|
2
|
+
/**
|
|
3
|
+
* Exposes the native Queue event and matched Nest pattern to a handler.
|
|
4
|
+
*
|
|
5
|
+
* @publicApi
|
|
6
|
+
*/
|
|
7
|
+
export class CloudflareQueueContext extends BaseRpcContext {
|
|
8
|
+
constructor(args) {
|
|
9
|
+
super(args);
|
|
10
|
+
}
|
|
11
|
+
/** Returns the message, including acknowledgement and retry methods. */
|
|
12
|
+
getMessage() {
|
|
13
|
+
return this.args[0];
|
|
14
|
+
}
|
|
15
|
+
/** Returns the batch containing this message. */
|
|
16
|
+
getBatch() {
|
|
17
|
+
return this.args[1];
|
|
18
|
+
}
|
|
19
|
+
/** Returns the Worker execution context for background work. */
|
|
20
|
+
getExecutionContext() {
|
|
21
|
+
return this.args[2];
|
|
22
|
+
}
|
|
23
|
+
/** Returns the normalized Nest event pattern. */
|
|
24
|
+
getPattern() {
|
|
25
|
+
return this.args[3];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { CloudflareQueueClient } from './client.js';
|
|
2
|
+
export { CLOUDFLARE_QUEUE_TRANSPORT } from './constants.js';
|
|
3
|
+
export { CloudflareQueueContext } from './context.js';
|
|
4
|
+
export type { CloudflareQueueClientOptions } from './interfaces/client-options.interface.js';
|
|
5
|
+
export type { CloudflareQueueServerOptions } from './interfaces/server-options.interface.js';
|
|
6
|
+
export { CloudflareQueueRecord } from './record-builder.js';
|
|
7
|
+
export { CloudflareQueueRecordBuilder } from './record-builder.js';
|
|
8
|
+
export { CloudflareQueueServer } from './server.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { CloudflareQueueClient } from './client.js';
|
|
2
|
+
export { CLOUDFLARE_QUEUE_TRANSPORT } from './constants.js';
|
|
3
|
+
export { CloudflareQueueContext } from './context.js';
|
|
4
|
+
export { CloudflareQueueRecord } from './record-builder.js';
|
|
5
|
+
export { CloudflareQueueRecordBuilder } from './record-builder.js';
|
|
6
|
+
export { CloudflareQueueServer } from './server.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Deserializer, Serializer } from '@nestjs/microservices';
|
|
2
|
+
/**
|
|
3
|
+
* Configures a Queue client registered through Nest ClientsModule.
|
|
4
|
+
*
|
|
5
|
+
* @publicApi
|
|
6
|
+
*/
|
|
7
|
+
export interface CloudflareQueueClientOptions {
|
|
8
|
+
/** Queue binding used to publish Nest event packets. */
|
|
9
|
+
queue: Queue;
|
|
10
|
+
/** Converts outgoing packets into Queue message bodies. */
|
|
11
|
+
serializer?: Serializer;
|
|
12
|
+
/** Configures the Nest client deserialization extension point. */
|
|
13
|
+
deserializer?: Deserializer;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Deserializer, Serializer } from '@nestjs/microservices';
|
|
2
|
+
/**
|
|
3
|
+
* Configures queue acknowledgement and batch processing.
|
|
4
|
+
*
|
|
5
|
+
* @publicApi
|
|
6
|
+
*/
|
|
7
|
+
export interface CloudflareQueueServerOptions {
|
|
8
|
+
/** Acknowledges successful messages automatically unless false. */
|
|
9
|
+
noAck?: boolean;
|
|
10
|
+
/** Processes messages sequentially unless parallel is selected. */
|
|
11
|
+
concurrency?: 'sequential' | 'parallel';
|
|
12
|
+
/** Configures the Nest server serialization extension point. */
|
|
13
|
+
serializer?: Serializer;
|
|
14
|
+
/** Converts Queue message bodies into Nest event packets. */
|
|
15
|
+
deserializer?: Deserializer;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Carries a Queue payload and native send options through Nest emit().
|
|
2
|
+
* @publicApi
|
|
3
|
+
*/
|
|
4
|
+
export declare class CloudflareQueueRecord<TData = unknown> {
|
|
5
|
+
readonly data: TData;
|
|
6
|
+
readonly options?: QueueSendOptions | undefined;
|
|
7
|
+
constructor(data: TData, options?: QueueSendOptions | undefined);
|
|
8
|
+
}
|
|
9
|
+
/** Builds a Queue record using Nest's transport record convention.
|
|
10
|
+
* @publicApi
|
|
11
|
+
*/
|
|
12
|
+
export declare class CloudflareQueueRecordBuilder<TData = unknown> {
|
|
13
|
+
private data?;
|
|
14
|
+
private options?;
|
|
15
|
+
constructor(data?: TData | undefined);
|
|
16
|
+
setData(data: TData): this;
|
|
17
|
+
setOptions(options: QueueSendOptions): this;
|
|
18
|
+
build(): CloudflareQueueRecord<TData | undefined>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Carries a Queue payload and native send options through Nest emit().
|
|
2
|
+
* @publicApi
|
|
3
|
+
*/
|
|
4
|
+
export class CloudflareQueueRecord {
|
|
5
|
+
data;
|
|
6
|
+
options;
|
|
7
|
+
constructor(data, options) {
|
|
8
|
+
this.data = data;
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Builds a Queue record using Nest's transport record convention.
|
|
13
|
+
* @publicApi
|
|
14
|
+
*/
|
|
15
|
+
export class CloudflareQueueRecordBuilder {
|
|
16
|
+
data;
|
|
17
|
+
options;
|
|
18
|
+
constructor(data) {
|
|
19
|
+
this.data = data;
|
|
20
|
+
}
|
|
21
|
+
setData(data) {
|
|
22
|
+
this.data = data;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
setOptions(options) {
|
|
26
|
+
this.options = options;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
build() {
|
|
30
|
+
return new CloudflareQueueRecord(this.data, this.options);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ReadPacket, Serializer } from '@nestjs/microservices';
|
|
2
|
+
/** Unwraps transport records while preserving Nest packet metadata. */
|
|
3
|
+
export declare class CloudflareQueueRecordSerializer implements Serializer {
|
|
4
|
+
serialize(packet: ReadPacket): ReadPacket<any> | {
|
|
5
|
+
data: any;
|
|
6
|
+
options: QueueSendOptions | undefined;
|
|
7
|
+
pattern: any;
|
|
8
|
+
metadata?: import("@nestjs/microservices").PacketMetadata;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CloudflareQueueRecord } from './record-builder.js';
|
|
2
|
+
/** Unwraps transport records while preserving Nest packet metadata. */
|
|
3
|
+
export class CloudflareQueueRecordSerializer {
|
|
4
|
+
serialize(packet) {
|
|
5
|
+
if (packet.data instanceof CloudflareQueueRecord) {
|
|
6
|
+
return {
|
|
7
|
+
...packet,
|
|
8
|
+
data: packet.data.data,
|
|
9
|
+
options: packet.data.options,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
return packet;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type CustomTransportStrategy, type ReadPacket, Server } from '@nestjs/microservices';
|
|
2
|
+
import { CloudflareQueueContext } from './context.js';
|
|
3
|
+
import type { CloudflareQueueServerOptions } from './interfaces/server-options.interface.js';
|
|
4
|
+
type WorkerExecutionContext = ExecutionContext;
|
|
5
|
+
/**
|
|
6
|
+
* Delivers Cloudflare Queue packets through Nest event handlers.
|
|
7
|
+
*
|
|
8
|
+
* @publicApi
|
|
9
|
+
*/
|
|
10
|
+
export declare class CloudflareQueueServer extends Server implements CustomTransportStrategy {
|
|
11
|
+
private readonly options;
|
|
12
|
+
readonly propagatesEventHandlerErrors = true;
|
|
13
|
+
transportId: symbol;
|
|
14
|
+
constructor(options?: CloudflareQueueServerOptions);
|
|
15
|
+
/** Signals that the event transport is ready. */
|
|
16
|
+
listen(callback: () => void): void;
|
|
17
|
+
/** Releases no resources because Workers owns the queue connection. */
|
|
18
|
+
close(): void;
|
|
19
|
+
/** Rejects subscriptions to connection events, which Queues does not expose. */
|
|
20
|
+
on(): never;
|
|
21
|
+
/** Returns this event transport for Worker dispatch. */
|
|
22
|
+
unwrap<T>(): T;
|
|
23
|
+
/** Awaits handler streams so failures can trigger Queue retries. */
|
|
24
|
+
handleEvent(pattern: string, packet: ReadPacket, context: CloudflareQueueContext): Promise<void>;
|
|
25
|
+
/** Deserializes a batch and acknowledges successes or retries failed messages. */
|
|
26
|
+
consume(batch: MessageBatch, ctx: WorkerExecutionContext, fallback?: () => void | Promise<void>): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Server, } from '@nestjs/microservices';
|
|
2
|
+
import { finalize, isObservable, lastValueFrom } from 'rxjs';
|
|
3
|
+
import { CLOUDFLARE_QUEUE_TRANSPORT } from './constants.js';
|
|
4
|
+
import { CloudflareQueueContext } from './context.js';
|
|
5
|
+
/**
|
|
6
|
+
* Delivers Cloudflare Queue packets through Nest event handlers.
|
|
7
|
+
*
|
|
8
|
+
* @publicApi
|
|
9
|
+
*/
|
|
10
|
+
export class CloudflareQueueServer extends Server {
|
|
11
|
+
options;
|
|
12
|
+
propagatesEventHandlerErrors = true;
|
|
13
|
+
transportId = CLOUDFLARE_QUEUE_TRANSPORT;
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
super();
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.initializeSerializer(options);
|
|
18
|
+
this.initializeDeserializer(options);
|
|
19
|
+
}
|
|
20
|
+
/** Signals that the event transport is ready. */
|
|
21
|
+
listen(callback) {
|
|
22
|
+
callback();
|
|
23
|
+
}
|
|
24
|
+
/** Releases no resources because Workers owns the queue connection. */
|
|
25
|
+
close() { }
|
|
26
|
+
/** Rejects subscriptions to connection events, which Queues does not expose. */
|
|
27
|
+
on() {
|
|
28
|
+
throw new Error('Queue transport has no connection events');
|
|
29
|
+
}
|
|
30
|
+
/** Returns this event transport for Worker dispatch. */
|
|
31
|
+
unwrap() {
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
/** Awaits handler streams so failures can trigger Queue retries. */
|
|
35
|
+
async handleEvent(pattern, packet, context) {
|
|
36
|
+
const handler = this.getHandlerByPattern(pattern);
|
|
37
|
+
if (!handler?.isEventHandler) {
|
|
38
|
+
throw new Error(`No EventPattern handler for pattern: ${pattern}`);
|
|
39
|
+
}
|
|
40
|
+
await this.runWithProcessingHooks(context, async (end) => {
|
|
41
|
+
const result = await handler(packet.data, context);
|
|
42
|
+
if (isObservable(result)) {
|
|
43
|
+
await lastValueFrom(result.pipe(finalize(end)), {
|
|
44
|
+
defaultValue: undefined,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
end();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** Deserializes a batch and acknowledges successes or retries failed messages. */
|
|
53
|
+
async consume(batch, ctx, fallback) {
|
|
54
|
+
const packets = await Promise.all(batch.messages.map(async (message) => {
|
|
55
|
+
try {
|
|
56
|
+
const packet = await this.deserializer.deserialize(message.body, {
|
|
57
|
+
channel: batch.queue,
|
|
58
|
+
});
|
|
59
|
+
const pattern = this.normalizePattern(packet.pattern);
|
|
60
|
+
return { message, packet, pattern };
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
return { message, error };
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
if (fallback &&
|
|
67
|
+
packets.every(item => !('error' in item) &&
|
|
68
|
+
!this.getHandlerByPattern(item.pattern)?.isEventHandler)) {
|
|
69
|
+
await fallback();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const consumeMessage = async (item) => {
|
|
73
|
+
try {
|
|
74
|
+
if ('error' in item) {
|
|
75
|
+
throw item.error;
|
|
76
|
+
}
|
|
77
|
+
const context = new CloudflareQueueContext([
|
|
78
|
+
item.message,
|
|
79
|
+
batch,
|
|
80
|
+
ctx,
|
|
81
|
+
item.pattern,
|
|
82
|
+
]);
|
|
83
|
+
await this.handleEvent(item.pattern, item.packet, context);
|
|
84
|
+
if (this.options.noAck !== false) {
|
|
85
|
+
item.message.ack();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
this.logger.error(error);
|
|
90
|
+
item.message.retry();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
if (this.options.concurrency === 'parallel') {
|
|
94
|
+
await Promise.all(packets.map(consumeMessage));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
for (const item of packets) {
|
|
98
|
+
await consumeMessage(item);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
|
|
3
|
+
type WorkerExecutionContext = ExecutionContext;
|
|
4
|
+
/** Discovers singleton cron methods and dispatches matching scheduled events. */
|
|
5
|
+
export declare class CronExplorer implements OnModuleInit {
|
|
6
|
+
private readonly discovery;
|
|
7
|
+
private readonly scanner;
|
|
8
|
+
private readonly reflector;
|
|
9
|
+
private readonly logger;
|
|
10
|
+
private handlers;
|
|
11
|
+
constructor(discovery: DiscoveryService, scanner: MetadataScanner, reflector: Reflector);
|
|
12
|
+
/** Collects cron methods from providers and controllers. */
|
|
13
|
+
onModuleInit(): void;
|
|
14
|
+
/** Reports matched handler failures through the event execution context. */
|
|
15
|
+
dispatch(controller: ScheduledController, ctx: WorkerExecutionContext): boolean;
|
|
16
|
+
/** Runs matching handlers and resolves only after all handlers finish. */
|
|
17
|
+
dispatchAndWait(controller: ScheduledController, _ctx: WorkerExecutionContext): Promise<boolean>;
|
|
18
|
+
private execute;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var CronExplorer_1;
|
|
11
|
+
import { Injectable, Logger, Scope } from '@nestjs/common';
|
|
12
|
+
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
|
|
13
|
+
import { CRON_METADATA } from './constants.js';
|
|
14
|
+
/** Discovers singleton cron methods and dispatches matching scheduled events. */
|
|
15
|
+
let CronExplorer = CronExplorer_1 = class CronExplorer {
|
|
16
|
+
discovery;
|
|
17
|
+
scanner;
|
|
18
|
+
reflector;
|
|
19
|
+
logger = new Logger(CronExplorer_1.name);
|
|
20
|
+
handlers = [];
|
|
21
|
+
constructor(discovery, scanner, reflector) {
|
|
22
|
+
this.discovery = discovery;
|
|
23
|
+
this.scanner = scanner;
|
|
24
|
+
this.reflector = reflector;
|
|
25
|
+
}
|
|
26
|
+
/** Collects cron methods from providers and controllers. */
|
|
27
|
+
onModuleInit() {
|
|
28
|
+
this.handlers = [];
|
|
29
|
+
for (const provider of [
|
|
30
|
+
...this.discovery.getProviders(),
|
|
31
|
+
...this.discovery.getControllers(),
|
|
32
|
+
]) {
|
|
33
|
+
const instance = provider.instance;
|
|
34
|
+
if (!instance ||
|
|
35
|
+
(typeof instance !== 'object' && typeof instance !== 'function')) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
39
|
+
if (!prototype) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
for (const name of this.scanner.getAllMethodNames(prototype)) {
|
|
43
|
+
const method = Reflect.get(instance, name);
|
|
44
|
+
if (typeof method !== 'function') {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const metadata = this.reflector.get(CRON_METADATA, method);
|
|
48
|
+
if (metadata === undefined) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (provider.scope === Scope.TRANSIENT ||
|
|
52
|
+
!provider.isDependencyTreeStatic()) {
|
|
53
|
+
this.logger.warn(`Cannot register cron job "${provider.name}@${name}" because it is defined in a non static provider.`);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (typeof metadata.cronTime !== 'string' ||
|
|
57
|
+
metadata.cronTime.trim().split(/\s+/).length !== 5) {
|
|
58
|
+
this.logger.warn(`Cannot register cron job "${provider.name}@${name}" because Workers requires a five-field cron expression.`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
this.handlers.push({
|
|
62
|
+
expression: metadata.cronTime,
|
|
63
|
+
options: metadata,
|
|
64
|
+
running: false,
|
|
65
|
+
invoke: (controller, ctx) => method.call(instance, controller, ctx),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** Reports matched handler failures through the event execution context. */
|
|
71
|
+
dispatch(controller, ctx) {
|
|
72
|
+
const execution = this.execute(controller, ctx);
|
|
73
|
+
if (!execution) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
ctx.waitUntil(execution);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
/** Runs matching handlers and resolves only after all handlers finish. */
|
|
80
|
+
async dispatchAndWait(controller, _ctx) {
|
|
81
|
+
const execution = this.execute(controller, _ctx);
|
|
82
|
+
if (!execution) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
await execution;
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
execute(controller, ctx) {
|
|
89
|
+
const matches = this.handlers.filter(({ expression, options }) => !options.disabled && expression === controller.cron);
|
|
90
|
+
if (matches.length === 0) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
return Promise.allSettled(matches
|
|
94
|
+
.filter(handler => !handler.options.waitForCompletion || !handler.running)
|
|
95
|
+
.map(handler => {
|
|
96
|
+
handler.running = true;
|
|
97
|
+
return Promise.resolve()
|
|
98
|
+
.then(() => handler.invoke(controller, ctx))
|
|
99
|
+
.finally(() => {
|
|
100
|
+
handler.running = false;
|
|
101
|
+
});
|
|
102
|
+
})).then(results => {
|
|
103
|
+
const errors = results
|
|
104
|
+
.filter(result => result.status === 'rejected')
|
|
105
|
+
.map(result => result.reason);
|
|
106
|
+
if (errors.length > 0) {
|
|
107
|
+
for (const error of errors) {
|
|
108
|
+
this.logger.error(error);
|
|
109
|
+
}
|
|
110
|
+
throw new AggregateError(errors, 'Cron handlers failed');
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
CronExplorer = CronExplorer_1 = __decorate([
|
|
116
|
+
Injectable(),
|
|
117
|
+
__metadata("design:paramtypes", [DiscoveryService,
|
|
118
|
+
MetadataScanner,
|
|
119
|
+
Reflector])
|
|
120
|
+
], CronExplorer);
|
|
121
|
+
export { CronExplorer };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CloudflareScheduleModule } from './schedule.module.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CloudflareScheduleModule } from './schedule.module.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
/**
|
|
3
|
+
* Discovers Nest cron handlers for Cloudflare scheduled events.
|
|
4
|
+
*
|
|
5
|
+
* @publicApi
|
|
6
|
+
*/
|
|
7
|
+
export declare class CloudflareScheduleModule {
|
|
8
|
+
/** Registers scheduled event discovery without starting a timer scheduler. */
|
|
9
|
+
static forRoot(): DynamicModule;
|
|
10
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var CloudflareScheduleModule_1;
|
|
8
|
+
import { Module } from '@nestjs/common';
|
|
9
|
+
import { DiscoveryModule } from '@nestjs/core';
|
|
10
|
+
import { CRON_EXPLORER } from './constants.js';
|
|
11
|
+
import { CronExplorer } from './cron.explorer.js';
|
|
12
|
+
/**
|
|
13
|
+
* Discovers Nest cron handlers for Cloudflare scheduled events.
|
|
14
|
+
*
|
|
15
|
+
* @publicApi
|
|
16
|
+
*/
|
|
17
|
+
let CloudflareScheduleModule = CloudflareScheduleModule_1 = class CloudflareScheduleModule {
|
|
18
|
+
/** Registers scheduled event discovery without starting a timer scheduler. */
|
|
19
|
+
static forRoot() {
|
|
20
|
+
return {
|
|
21
|
+
module: CloudflareScheduleModule_1,
|
|
22
|
+
imports: [DiscoveryModule],
|
|
23
|
+
providers: [
|
|
24
|
+
CronExplorer,
|
|
25
|
+
{ provide: CRON_EXPLORER, useExisting: CronExplorer },
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
CloudflareScheduleModule = CloudflareScheduleModule_1 = __decorate([
|
|
31
|
+
Module({})
|
|
32
|
+
], CloudflareScheduleModule);
|
|
33
|
+
export { CloudflareScheduleModule };
|