nestjs-cloudflare 0.0.0-stage → 0.1.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 +317 -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 +10 -0
- package/dist/cloudflare.module.js +45 -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/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/interfaces/worker-options.interface.d.ts +14 -0
- package/dist/interfaces/worker-options.interface.js +1 -0
- package/dist/interfaces/worker.interface.d.ts +19 -0
- package/dist/interfaces/worker.interface.js +1 -0
- package/dist/queue/client.d.ts +19 -0
- package/dist/queue/client.js +36 -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 +6 -0
- package/dist/queue/index.js +4 -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/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 +17 -0
- package/dist/schedule/cron.explorer.js +105 -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 +69 -0
- package/dist/websockets/adapter.js +161 -0
- package/dist/websockets/durable-object.d.ts +21 -0
- package/dist/websockets/durable-object.js +70 -0
- package/dist/websockets/index.d.ts +7 -0
- package/dist/websockets/index.js +3 -0
- package/dist/websockets/interfaces/adapter-options.interface.d.ts +10 -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/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/worker/create-worker.d.ts +10 -0
- package/dist/worker/create-worker.js +99 -0
- package/package.json +107 -4
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DurableObject } from 'cloudflare:workers';
|
|
2
|
+
import { InvalidWebSocketMessageException } from './invalid-message.exception.js';
|
|
3
|
+
/**
|
|
4
|
+
* Hosts a Nest gateway using the Durable Object WebSocket Hibernation API.
|
|
5
|
+
*
|
|
6
|
+
* @publicApi
|
|
7
|
+
*/
|
|
8
|
+
export class CloudflareWebSocketDurableObject extends DurableObject {
|
|
9
|
+
options;
|
|
10
|
+
ready;
|
|
11
|
+
tag;
|
|
12
|
+
constructor(ctx, env, options) {
|
|
13
|
+
super(ctx, env);
|
|
14
|
+
this.options = options;
|
|
15
|
+
const tag = `nestjs:${options.path}`;
|
|
16
|
+
this.tag = tag;
|
|
17
|
+
this.ready = (async () => {
|
|
18
|
+
await options.getApplication();
|
|
19
|
+
for (const socket of ctx.getWebSockets(tag)) {
|
|
20
|
+
options.adapter.connect(options.path, socket);
|
|
21
|
+
}
|
|
22
|
+
})();
|
|
23
|
+
}
|
|
24
|
+
/** Accepts upgrades and registers connections for hibernation. */
|
|
25
|
+
async fetch(request) {
|
|
26
|
+
await this.ready;
|
|
27
|
+
if (new URL(request.url).pathname !== this.options.path) {
|
|
28
|
+
return new Response('Not found', { status: 404 });
|
|
29
|
+
}
|
|
30
|
+
if (request.headers.get('Upgrade')?.toLowerCase() !== 'websocket') {
|
|
31
|
+
return new Response('WebSocket upgrade required', { status: 426 });
|
|
32
|
+
}
|
|
33
|
+
const pair = new WebSocketPair();
|
|
34
|
+
this.ctx.acceptWebSocket(pair[1], [this.tag]);
|
|
35
|
+
this.options.adapter.connect(this.options.path, pair[1], request);
|
|
36
|
+
return new Response(null, { status: 101, webSocket: pair[0] });
|
|
37
|
+
}
|
|
38
|
+
/** Waits for Nest startup and gateway message handling. */
|
|
39
|
+
async webSocketMessage(socket, message) {
|
|
40
|
+
await this.ready;
|
|
41
|
+
try {
|
|
42
|
+
await this.options.adapter.message(socket, message);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (error instanceof InvalidWebSocketMessageException) {
|
|
46
|
+
socket.close(1007, error.message);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
this.options.adapter.error(socket, error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Completes the close handshake and dispatches disconnection hooks. */
|
|
53
|
+
async webSocketClose(socket, code, reason, _wasClean) {
|
|
54
|
+
await this.ready;
|
|
55
|
+
try {
|
|
56
|
+
if (socket.readyState === WebSocket.OPEN ||
|
|
57
|
+
socket.readyState === WebSocket.CLOSING) {
|
|
58
|
+
socket.close(code === 1005 || code === 1006 ? 1000 : code, reason);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
this.options.adapter.disconnect(socket, reason);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Reports native socket errors through the Nest adapter. */
|
|
66
|
+
async webSocketError(socket, error) {
|
|
67
|
+
await this.ready;
|
|
68
|
+
this.options.adapter.error(socket, error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { CloudflareWebSocketAdapter } from './adapter.js';
|
|
2
|
+
export { CloudflareWebSocketDurableObject } from './durable-object.js';
|
|
3
|
+
export type { CloudflareWebSocketAdapterOptions } from './interfaces/adapter-options.interface.js';
|
|
4
|
+
export type { CloudflareWebSocketDurableObjectOptions } from './interfaces/durable-object-options.interface.js';
|
|
5
|
+
export type { CloudflareWebSocketMessage } from './interfaces/message.interface.js';
|
|
6
|
+
export type { CloudflareWebSocketServer } from './interfaces/server.interface.js';
|
|
7
|
+
export { InvalidWebSocketMessageException } from './invalid-message.exception.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CloudflareWebSocketMessage } from './message.interface.js';
|
|
2
|
+
/**
|
|
3
|
+
* Configures parsing of native WebSocket frames before Nest dispatch.
|
|
4
|
+
*
|
|
5
|
+
* @publicApi
|
|
6
|
+
*/
|
|
7
|
+
export interface CloudflareWebSocketAdapterOptions {
|
|
8
|
+
/** Maps a frame to an event and payload, or returns void to ignore it. */
|
|
9
|
+
messageParser?: (data: string | ArrayBuffer) => CloudflareWebSocketMessage | void;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { INestApplication } from '@nestjs/common';
|
|
2
|
+
import type { CloudflareWebSocketAdapter } from '../adapter.js';
|
|
3
|
+
/**
|
|
4
|
+
* Selects the gateway and application used by a Durable Object.
|
|
5
|
+
*
|
|
6
|
+
* @publicApi
|
|
7
|
+
*/
|
|
8
|
+
export interface CloudflareWebSocketDurableObjectOptions {
|
|
9
|
+
/** Adapter installed on the Nest application. */
|
|
10
|
+
adapter: CloudflareWebSocketAdapter;
|
|
11
|
+
/** Path declared by the gateway's WebSocketGateway options. */
|
|
12
|
+
path: string;
|
|
13
|
+
/** Starts or retrieves the initialized Nest application. */
|
|
14
|
+
getApplication: () => Promise<INestApplication>;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A message mapped to a Nest gateway event.
|
|
3
|
+
*
|
|
4
|
+
* @publicApi
|
|
5
|
+
*/
|
|
6
|
+
export interface CloudflareWebSocketMessage {
|
|
7
|
+
/** Name matched against SubscribeMessage metadata. */
|
|
8
|
+
event: string;
|
|
9
|
+
/** Payload passed to the gateway's message handler. */
|
|
10
|
+
data: unknown;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifies a frame that cannot be decoded as an application message.
|
|
3
|
+
*
|
|
4
|
+
* @publicApi
|
|
5
|
+
*/
|
|
6
|
+
export class InvalidWebSocketMessageException extends Error {
|
|
7
|
+
constructor(message, options) {
|
|
8
|
+
super(message, options);
|
|
9
|
+
this.name = 'InvalidWebSocketMessageException';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { type INestApplication } from '@nestjs/common';
|
|
3
|
+
import type { CloudflareWorker } from '../interfaces/worker.interface.js';
|
|
4
|
+
import type { CloudflareWorkerOptions } from '../interfaces/worker-options.interface.js';
|
|
5
|
+
/**
|
|
6
|
+
* Defers Nest bootstrap until the first event and caches the initialized application.
|
|
7
|
+
*
|
|
8
|
+
* @publicApi
|
|
9
|
+
*/
|
|
10
|
+
export declare function createWorker<Env = Cloudflare.Env>(bootstrap: () => Promise<INestApplication>, options?: CloudflareWorkerOptions<Env>): CloudflareWorker<Env>;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { Logger } from '@nestjs/common';
|
|
3
|
+
import { httpServerHandler } from 'cloudflare:node';
|
|
4
|
+
import { CLOUDFLARE_ENV } from '../constants.js';
|
|
5
|
+
import { CLOUDFLARE_QUEUE_TRANSPORT } from '../queue/constants.js';
|
|
6
|
+
import { CRON_EXPLORER } from '../schedule/constants.js';
|
|
7
|
+
/**
|
|
8
|
+
* Defers Nest bootstrap until the first event and caches the initialized application.
|
|
9
|
+
*
|
|
10
|
+
* @publicApi
|
|
11
|
+
*/
|
|
12
|
+
export function createWorker(bootstrap, options = {}) {
|
|
13
|
+
const logger = new Logger('CloudflareWorker');
|
|
14
|
+
let application;
|
|
15
|
+
let http;
|
|
16
|
+
const getApplication = () => {
|
|
17
|
+
application ??= (async () => {
|
|
18
|
+
const app = await bootstrap();
|
|
19
|
+
try {
|
|
20
|
+
app.get(CLOUDFLARE_ENV);
|
|
21
|
+
}
|
|
22
|
+
catch (cause) {
|
|
23
|
+
await app.close();
|
|
24
|
+
throw new Error('Import CloudflareModule.forRoot() in the application root module', { cause });
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
await app.init();
|
|
28
|
+
await app.startAllMicroservices();
|
|
29
|
+
if (!app.getHttpServer().listening) {
|
|
30
|
+
await app.listen(0);
|
|
31
|
+
}
|
|
32
|
+
http = httpServerHandler(app.getHttpServer());
|
|
33
|
+
return app;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
await app.close();
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
})();
|
|
40
|
+
return application;
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
getApplication,
|
|
44
|
+
async fetch(request, env, ctx) {
|
|
45
|
+
await getApplication();
|
|
46
|
+
return http.fetch(request, env, ctx);
|
|
47
|
+
},
|
|
48
|
+
async scheduled(controller, env, ctx) {
|
|
49
|
+
const app = await getApplication();
|
|
50
|
+
let scheduled;
|
|
51
|
+
try {
|
|
52
|
+
scheduled = app.get(CRON_EXPLORER);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// The schedule integration is optional for applications without cron handlers.
|
|
56
|
+
}
|
|
57
|
+
if (scheduled?.dispatch(controller, ctx)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (options.fallback?.scheduled) {
|
|
61
|
+
await options.fallback.scheduled(controller, env, ctx);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
logger.warn(`No Cloudflare cron handler for expression: ${controller.cron}. Import CloudflareScheduleModule.forRoot() and declare a matching @Cron method`);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
async queue(batch, env, ctx) {
|
|
68
|
+
const app = await getApplication();
|
|
69
|
+
const transports = [];
|
|
70
|
+
for (const microservice of app.getMicroservices()) {
|
|
71
|
+
let transport;
|
|
72
|
+
try {
|
|
73
|
+
transport = microservice.unwrap();
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (typeof transport === 'object' &&
|
|
79
|
+
transport !== null &&
|
|
80
|
+
'transportId' in transport &&
|
|
81
|
+
transport.transportId === CLOUDFLARE_QUEUE_TRANSPORT) {
|
|
82
|
+
transports.push(transport);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (transports.length > 1) {
|
|
86
|
+
throw new Error('Connect exactly one CloudflareQueueServer to dispatch Worker queue events');
|
|
87
|
+
}
|
|
88
|
+
const fallback = options.fallback?.queue;
|
|
89
|
+
if (!transports[0]) {
|
|
90
|
+
if (fallback) {
|
|
91
|
+
await fallback(batch, env, ctx);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
throw new Error('Connect a CloudflareQueueServer with app.connectMicroservice() before handling queue events');
|
|
95
|
+
}
|
|
96
|
+
await transports[0].consume(batch, ctx, fallback ? () => fallback(batch, env, ctx) : undefined);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,109 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-cloudflare",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"./queue": {
|
|
11
|
+
"types": "./dist/queue/index.d.ts",
|
|
12
|
+
"import": "./dist/queue/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./schedule": {
|
|
15
|
+
"types": "./dist/schedule/index.d.ts",
|
|
16
|
+
"import": "./dist/schedule/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./websockets": {
|
|
19
|
+
"types": "./dist/websockets/index.d.ts",
|
|
20
|
+
"import": "./dist/websockets/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -p tsconfig.build.json",
|
|
28
|
+
"prepare": "tsc",
|
|
29
|
+
"build:example": "tsc -p tsconfig.example.json",
|
|
30
|
+
"test:types": "tsc -p tsconfig.test.json",
|
|
31
|
+
"lint": "eslint . && prettier --check .",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"format": "prettier --write .",
|
|
34
|
+
"prepublishOnly": "npm run build"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@cloudflare/vitest-plugin": "1.7.0",
|
|
38
|
+
"@cloudflare/workers-types": "5.20261110.1",
|
|
39
|
+
"@eslint/js": "^9.39.0",
|
|
40
|
+
"@nestjs/common": "12.4.2",
|
|
41
|
+
"@nestjs/core": "12.4.2",
|
|
42
|
+
"@nestjs/microservices": "12.4.2",
|
|
43
|
+
"@nestjs/platform-express": "12.4.2",
|
|
44
|
+
"@nestjs/schedule": "12.0.3",
|
|
45
|
+
"@nestjs/testing": "12.4.2",
|
|
46
|
+
"@nestjs/websockets": "12.4.2",
|
|
47
|
+
"@stylistic/eslint-plugin": "5.10.0",
|
|
48
|
+
"@vitest/eslint-plugin": "1.6.28",
|
|
49
|
+
"@types/node": "26.6.6",
|
|
50
|
+
"eslint": "^9.39.0",
|
|
51
|
+
"eslint-config-prettier": "^10.1.8",
|
|
52
|
+
"eslint-plugin-simple-import-sort": "14.0.0",
|
|
53
|
+
"prettier": "^3.9.10",
|
|
54
|
+
"reflect-metadata": "0.2.2",
|
|
55
|
+
"rxjs": "7.8.2",
|
|
56
|
+
"typescript": "^6.0.0",
|
|
57
|
+
"typescript-eslint": "^8.72.2",
|
|
58
|
+
"vite": "8.3.8",
|
|
59
|
+
"vitest": "4.1.0",
|
|
60
|
+
"wrangler": "4.159.0"
|
|
61
|
+
},
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/blockchainian/nestjs-cloudflare.git"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"@nestjs/common": "^12.0.0",
|
|
68
|
+
"@nestjs/core": "^12.0.0",
|
|
69
|
+
"@nestjs/microservices": "^12.0.0",
|
|
70
|
+
"reflect-metadata": "^0.2.2",
|
|
71
|
+
"rxjs": "^7.8.0",
|
|
72
|
+
"@nestjs/schedule": "^12.0.0",
|
|
73
|
+
"@nestjs/websockets": "^12.0.0",
|
|
74
|
+
"@cloudflare/workers-types": "5.20261110.1"
|
|
75
|
+
},
|
|
76
|
+
"peerDependenciesMeta": {
|
|
77
|
+
"@cloudflare/workers-types": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"@nestjs/microservices": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"@nestjs/schedule": {
|
|
84
|
+
"optional": true
|
|
85
|
+
},
|
|
86
|
+
"@nestjs/websockets": {
|
|
87
|
+
"optional": true
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"description": "NestJS integrations for Cloudflare Workers, Queues, scheduled events, and Durable Objects.",
|
|
91
|
+
"keywords": [
|
|
92
|
+
"nestjs",
|
|
93
|
+
"cloudflare",
|
|
94
|
+
"workers",
|
|
95
|
+
"queues",
|
|
96
|
+
"durable-objects"
|
|
97
|
+
],
|
|
98
|
+
"engines": {
|
|
99
|
+
"node": ">=22.12.0"
|
|
100
|
+
},
|
|
101
|
+
"license": "MIT",
|
|
102
|
+
"homepage": "https://github.com/blockchainian/nestjs-cloudflare#readme",
|
|
103
|
+
"bugs": {
|
|
104
|
+
"url": "https://github.com/blockchainian/nestjs-cloudflare/issues"
|
|
105
|
+
},
|
|
106
|
+
"publishConfig": {
|
|
107
|
+
"access": "public"
|
|
108
|
+
}
|
|
109
|
+
}
|