@velajs/cloudflare 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.
Files changed (52) hide show
  1. package/README.md +372 -0
  2. package/dist/binding-ref.d.ts +15 -0
  3. package/dist/binding-ref.js +23 -0
  4. package/dist/cloudflare-application.d.ts +51 -0
  5. package/dist/cloudflare-application.js +77 -0
  6. package/dist/cloudflare-factory.d.ts +18 -0
  7. package/dist/cloudflare-factory.js +42 -0
  8. package/dist/decorators/env.d.ts +16 -0
  9. package/dist/decorators/env.js +22 -0
  10. package/dist/decorators/queue-consumer.d.ts +23 -0
  11. package/dist/decorators/queue-consumer.js +32 -0
  12. package/dist/decorators/scheduled.d.ts +20 -0
  13. package/dist/decorators/scheduled.js +29 -0
  14. package/dist/index.d.ts +27 -0
  15. package/dist/index.js +29 -0
  16. package/dist/modules/ai.module.d.ts +6 -0
  17. package/dist/modules/ai.module.js +31 -0
  18. package/dist/modules/d1.module.d.ts +6 -0
  19. package/dist/modules/d1.module.js +31 -0
  20. package/dist/modules/durable-object.module.d.ts +6 -0
  21. package/dist/modules/durable-object.module.js +31 -0
  22. package/dist/modules/hyperdrive.module.d.ts +6 -0
  23. package/dist/modules/hyperdrive.module.js +31 -0
  24. package/dist/modules/kv.module.d.ts +6 -0
  25. package/dist/modules/kv.module.js +31 -0
  26. package/dist/modules/queue.module.d.ts +6 -0
  27. package/dist/modules/queue.module.js +31 -0
  28. package/dist/modules/r2.module.d.ts +6 -0
  29. package/dist/modules/r2.module.js +31 -0
  30. package/dist/modules/vectorize.module.d.ts +6 -0
  31. package/dist/modules/vectorize.module.js +31 -0
  32. package/dist/services/ai.service.d.ts +16 -0
  33. package/dist/services/ai.service.js +36 -0
  34. package/dist/services/d1.service.d.ts +22 -0
  35. package/dist/services/d1.service.js +47 -0
  36. package/dist/services/durable-object.service.d.ts +24 -0
  37. package/dist/services/durable-object.service.js +48 -0
  38. package/dist/services/hyperdrive.service.d.ts +26 -0
  39. package/dist/services/hyperdrive.service.js +51 -0
  40. package/dist/services/kv.service.d.ts +24 -0
  41. package/dist/services/kv.service.js +48 -0
  42. package/dist/services/queue.service.d.ts +20 -0
  43. package/dist/services/queue.service.js +39 -0
  44. package/dist/services/r2.service.d.ts +18 -0
  45. package/dist/services/r2.service.js +48 -0
  46. package/dist/services/vectorize.service.d.ts +26 -0
  47. package/dist/services/vectorize.service.js +51 -0
  48. package/dist/tokens.d.ts +17 -0
  49. package/dist/tokens.js +18 -0
  50. package/dist/types.d.ts +13 -0
  51. package/dist/types.js +1 -0
  52. package/package.json +69 -0
@@ -0,0 +1,51 @@
1
+ function _ts_decorate(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
+ function _ts_metadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+ function _ts_param(paramIndex, decorator) {
11
+ return function(target, key) {
12
+ decorator(target, key, paramIndex);
13
+ };
14
+ }
15
+ import { Injectable, Inject } from "@velajs/vela";
16
+ import { HYPERDRIVE_BINDING_REF } from "../tokens.js";
17
+ export class HyperdriveService {
18
+ ref;
19
+ constructor(ref){
20
+ this.ref = ref;
21
+ }
22
+ /** Access the raw Hyperdrive binding directly. */ get binding() {
23
+ return this.ref.value;
24
+ }
25
+ get connectionString() {
26
+ return this.binding.connectionString;
27
+ }
28
+ get host() {
29
+ return this.binding.host;
30
+ }
31
+ get port() {
32
+ return this.binding.port;
33
+ }
34
+ get user() {
35
+ return this.binding.user;
36
+ }
37
+ get password() {
38
+ return this.binding.password;
39
+ }
40
+ get database() {
41
+ return this.binding.database;
42
+ }
43
+ }
44
+ HyperdriveService = _ts_decorate([
45
+ Injectable(),
46
+ _ts_param(0, Inject(HYPERDRIVE_BINDING_REF)),
47
+ _ts_metadata("design:type", Function),
48
+ _ts_metadata("design:paramtypes", [
49
+ typeof BindingRef === "undefined" ? Object : BindingRef
50
+ ])
51
+ ], HyperdriveService);
@@ -0,0 +1,24 @@
1
+ import type { BindingRef } from '../binding-ref';
2
+ type KVBinding = {
3
+ get: Function;
4
+ getWithMetadata: Function;
5
+ put: Function;
6
+ delete: Function;
7
+ list: Function;
8
+ } & Record<string, any>;
9
+ /**
10
+ * Wrapper around Cloudflare KV Namespace.
11
+ * Injected via `KVModule.forRoot({ binding: 'MY_KV' })`.
12
+ */
13
+ export declare class KVService {
14
+ private ref;
15
+ constructor(ref: BindingRef);
16
+ /** Access the raw KVNamespace binding directly. */
17
+ get namespace(): KVBinding;
18
+ get(key: string, typeOrOptions?: string | Record<string, unknown>): Promise<unknown>;
19
+ getWithMetadata(key: string, typeOrOptions?: string | Record<string, unknown>): Promise<unknown>;
20
+ put(key: string, value: string | ArrayBuffer | ReadableStream, options?: Record<string, unknown>): Promise<void>;
21
+ delete(key: string): Promise<void>;
22
+ list(options?: Record<string, unknown>): Promise<unknown>;
23
+ }
24
+ export {};
@@ -0,0 +1,48 @@
1
+ function _ts_decorate(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
+ function _ts_metadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+ function _ts_param(paramIndex, decorator) {
11
+ return function(target, key) {
12
+ decorator(target, key, paramIndex);
13
+ };
14
+ }
15
+ import { Injectable, Inject } from "@velajs/vela";
16
+ import { KV_BINDING_REF } from "../tokens.js";
17
+ export class KVService {
18
+ ref;
19
+ constructor(ref){
20
+ this.ref = ref;
21
+ }
22
+ /** Access the raw KVNamespace binding directly. */ get namespace() {
23
+ return this.ref.value;
24
+ }
25
+ get(key, typeOrOptions) {
26
+ return this.namespace.get(key, typeOrOptions);
27
+ }
28
+ getWithMetadata(key, typeOrOptions) {
29
+ return this.namespace.getWithMetadata(key, typeOrOptions);
30
+ }
31
+ put(key, value, options) {
32
+ return this.namespace.put(key, value, options);
33
+ }
34
+ delete(key) {
35
+ return this.namespace.delete(key);
36
+ }
37
+ list(options) {
38
+ return this.namespace.list(options);
39
+ }
40
+ }
41
+ KVService = _ts_decorate([
42
+ Injectable(),
43
+ _ts_param(0, Inject(KV_BINDING_REF)),
44
+ _ts_metadata("design:type", Function),
45
+ _ts_metadata("design:paramtypes", [
46
+ typeof BindingRef === "undefined" ? Object : BindingRef
47
+ ])
48
+ ], KVService);
@@ -0,0 +1,20 @@
1
+ import type { BindingRef } from '../binding-ref';
2
+ type QueueBinding = Record<string, any>;
3
+ /**
4
+ * Wrapper around Cloudflare Queue (producer side).
5
+ * Injected via `QueueModule.forRoot({ binding: 'MY_QUEUE' })`.
6
+ *
7
+ * For consuming messages, use the `@QueueConsumer()` decorator.
8
+ */
9
+ export declare class QueueService {
10
+ private ref;
11
+ constructor(ref: BindingRef);
12
+ /** Access the raw Queue binding directly. */
13
+ get queue(): QueueBinding;
14
+ send(message: unknown, options?: Record<string, unknown>): Promise<void>;
15
+ sendBatch(messages: Iterable<{
16
+ body: unknown;
17
+ [key: string]: unknown;
18
+ }>, options?: Record<string, unknown>): Promise<void>;
19
+ }
20
+ export {};
@@ -0,0 +1,39 @@
1
+ function _ts_decorate(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
+ function _ts_metadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+ function _ts_param(paramIndex, decorator) {
11
+ return function(target, key) {
12
+ decorator(target, key, paramIndex);
13
+ };
14
+ }
15
+ import { Injectable, Inject } from "@velajs/vela";
16
+ import { QUEUE_BINDING_REF } from "../tokens.js";
17
+ export class QueueService {
18
+ ref;
19
+ constructor(ref){
20
+ this.ref = ref;
21
+ }
22
+ /** Access the raw Queue binding directly. */ get queue() {
23
+ return this.ref.value;
24
+ }
25
+ send(message, options) {
26
+ return this.queue.send(message, options);
27
+ }
28
+ sendBatch(messages, options) {
29
+ return this.queue.sendBatch(messages, options);
30
+ }
31
+ }
32
+ QueueService = _ts_decorate([
33
+ Injectable(),
34
+ _ts_param(0, Inject(QUEUE_BINDING_REF)),
35
+ _ts_metadata("design:type", Function),
36
+ _ts_metadata("design:paramtypes", [
37
+ typeof BindingRef === "undefined" ? Object : BindingRef
38
+ ])
39
+ ], QueueService);
@@ -0,0 +1,18 @@
1
+ import type { BindingRef } from '../binding-ref';
2
+ type R2Binding = Record<string, any>;
3
+ /**
4
+ * Wrapper around Cloudflare R2 Bucket.
5
+ * Injected via `R2Module.forRoot({ binding: 'ASSETS' })`.
6
+ */
7
+ export declare class R2Service {
8
+ private ref;
9
+ constructor(ref: BindingRef);
10
+ /** Access the raw R2Bucket binding directly. */
11
+ get bucket(): R2Binding;
12
+ get(key: string, options?: Record<string, unknown>): Promise<unknown>;
13
+ head(key: string): Promise<unknown>;
14
+ put(key: string, value: ReadableStream | ArrayBuffer | ArrayBufferView | string | null | Blob, options?: Record<string, unknown>): Promise<unknown>;
15
+ delete(keys: string | string[]): Promise<void>;
16
+ list(options?: Record<string, unknown>): Promise<unknown>;
17
+ }
18
+ export {};
@@ -0,0 +1,48 @@
1
+ function _ts_decorate(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
+ function _ts_metadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+ function _ts_param(paramIndex, decorator) {
11
+ return function(target, key) {
12
+ decorator(target, key, paramIndex);
13
+ };
14
+ }
15
+ import { Injectable, Inject } from "@velajs/vela";
16
+ import { R2_BINDING_REF } from "../tokens.js";
17
+ export class R2Service {
18
+ ref;
19
+ constructor(ref){
20
+ this.ref = ref;
21
+ }
22
+ /** Access the raw R2Bucket binding directly. */ get bucket() {
23
+ return this.ref.value;
24
+ }
25
+ get(key, options) {
26
+ return this.bucket.get(key, options);
27
+ }
28
+ head(key) {
29
+ return this.bucket.head(key);
30
+ }
31
+ put(key, value, options) {
32
+ return this.bucket.put(key, value, options);
33
+ }
34
+ delete(keys) {
35
+ return this.bucket.delete(keys);
36
+ }
37
+ list(options) {
38
+ return this.bucket.list(options);
39
+ }
40
+ }
41
+ R2Service = _ts_decorate([
42
+ Injectable(),
43
+ _ts_param(0, Inject(R2_BINDING_REF)),
44
+ _ts_metadata("design:type", Function),
45
+ _ts_metadata("design:paramtypes", [
46
+ typeof BindingRef === "undefined" ? Object : BindingRef
47
+ ])
48
+ ], R2Service);
@@ -0,0 +1,26 @@
1
+ import type { BindingRef } from '../binding-ref';
2
+ type VectorizeBinding = {
3
+ query: Function;
4
+ insert: Function;
5
+ upsert: Function;
6
+ getByIds: Function;
7
+ deleteByIds: Function;
8
+ describe: Function;
9
+ } & Record<string, any>;
10
+ /**
11
+ * Wrapper around Cloudflare Vectorize Index.
12
+ * Injected via `VectorizeModule.forRoot({ binding: 'EMBEDDINGS' })`.
13
+ */
14
+ export declare class VectorizeService {
15
+ private ref;
16
+ constructor(ref: BindingRef);
17
+ /** Access the raw VectorizeIndex binding directly. */
18
+ get index(): VectorizeBinding;
19
+ query(vector: number[], options?: Record<string, unknown>): Promise<unknown>;
20
+ insert(vectors: unknown[]): Promise<unknown>;
21
+ upsert(vectors: unknown[]): Promise<unknown>;
22
+ getByIds(ids: string[]): Promise<unknown>;
23
+ deleteByIds(ids: string[]): Promise<unknown>;
24
+ describe(): Promise<unknown>;
25
+ }
26
+ export {};
@@ -0,0 +1,51 @@
1
+ function _ts_decorate(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
+ function _ts_metadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+ function _ts_param(paramIndex, decorator) {
11
+ return function(target, key) {
12
+ decorator(target, key, paramIndex);
13
+ };
14
+ }
15
+ import { Injectable, Inject } from "@velajs/vela";
16
+ import { VECTORIZE_BINDING_REF } from "../tokens.js";
17
+ export class VectorizeService {
18
+ ref;
19
+ constructor(ref){
20
+ this.ref = ref;
21
+ }
22
+ /** Access the raw VectorizeIndex binding directly. */ get index() {
23
+ return this.ref.value;
24
+ }
25
+ query(vector, options) {
26
+ return this.index.query(vector, options);
27
+ }
28
+ insert(vectors) {
29
+ return this.index.insert(vectors);
30
+ }
31
+ upsert(vectors) {
32
+ return this.index.upsert(vectors);
33
+ }
34
+ getByIds(ids) {
35
+ return this.index.getByIds(ids);
36
+ }
37
+ deleteByIds(ids) {
38
+ return this.index.deleteByIds(ids);
39
+ }
40
+ describe() {
41
+ return this.index.describe();
42
+ }
43
+ }
44
+ VectorizeService = _ts_decorate([
45
+ Injectable(),
46
+ _ts_param(0, Inject(VECTORIZE_BINDING_REF)),
47
+ _ts_metadata("design:type", Function),
48
+ _ts_metadata("design:paramtypes", [
49
+ typeof BindingRef === "undefined" ? Object : BindingRef
50
+ ])
51
+ ], VectorizeService);
@@ -0,0 +1,17 @@
1
+ import { InjectionToken } from '@velajs/vela';
2
+ import type { BindingRef } from './binding-ref';
3
+ export declare const KV_BINDING_REF: InjectionToken<BindingRef<unknown>>;
4
+ export declare const D1_BINDING_REF: InjectionToken<BindingRef<unknown>>;
5
+ export declare const R2_BINDING_REF: InjectionToken<BindingRef<unknown>>;
6
+ export declare const QUEUE_BINDING_REF: InjectionToken<BindingRef<unknown>>;
7
+ export declare const DO_BINDING_REF: InjectionToken<BindingRef<unknown>>;
8
+ export declare const AI_BINDING_REF: InjectionToken<BindingRef<unknown>>;
9
+ export declare const VECTORIZE_BINDING_REF: InjectionToken<BindingRef<unknown>>;
10
+ export declare const HYPERDRIVE_BINDING_REF: InjectionToken<BindingRef<unknown>>;
11
+ /**
12
+ * Global registry of binding refs to initialize on first request.
13
+ * Each module's `forRoot()` pushes its BindingRef here.
14
+ * CloudflareFactory reads from this to populate bindings.
15
+ */
16
+ export declare const bindingsRegistry: BindingRef[];
17
+ export declare function clearBindingsRegistry(): void;
package/dist/tokens.js ADDED
@@ -0,0 +1,18 @@
1
+ import { InjectionToken } from "@velajs/vela";
2
+ // Internal tokens for binding refs — used by services
3
+ export const KV_BINDING_REF = new InjectionToken('CF_KV_BINDING_REF');
4
+ export const D1_BINDING_REF = new InjectionToken('CF_D1_BINDING_REF');
5
+ export const R2_BINDING_REF = new InjectionToken('CF_R2_BINDING_REF');
6
+ export const QUEUE_BINDING_REF = new InjectionToken('CF_QUEUE_BINDING_REF');
7
+ export const DO_BINDING_REF = new InjectionToken('CF_DO_BINDING_REF');
8
+ export const AI_BINDING_REF = new InjectionToken('CF_AI_BINDING_REF');
9
+ export const VECTORIZE_BINDING_REF = new InjectionToken('CF_VECTORIZE_BINDING_REF');
10
+ export const HYPERDRIVE_BINDING_REF = new InjectionToken('CF_HYPERDRIVE_BINDING_REF');
11
+ /**
12
+ * Global registry of binding refs to initialize on first request.
13
+ * Each module's `forRoot()` pushes its BindingRef here.
14
+ * CloudflareFactory reads from this to populate bindings.
15
+ */ export const bindingsRegistry = [];
16
+ export function clearBindingsRegistry() {
17
+ bindingsRegistry.length = 0;
18
+ }
@@ -0,0 +1,13 @@
1
+ export interface CloudflareEnv {
2
+ [key: string]: unknown;
3
+ }
4
+ export interface ScheduledRegistration {
5
+ instance: unknown;
6
+ methodName: string;
7
+ cron: string;
8
+ }
9
+ export interface QueueRegistration {
10
+ instance: unknown;
11
+ methodName: string;
12
+ queueName: string;
13
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export { };
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@velajs/cloudflare",
3
+ "version": "0.1.0",
4
+ "description": "Cloudflare Workers integration for Vela framework",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md",
17
+ "LICENSE",
18
+ "CHANGELOG.md"
19
+ ],
20
+ "sideEffects": false,
21
+ "keywords": [
22
+ "vela",
23
+ "cloudflare",
24
+ "cloudflare-workers",
25
+ "kv",
26
+ "d1",
27
+ "r2",
28
+ "queues",
29
+ "edge",
30
+ "framework",
31
+ "decorators"
32
+ ],
33
+ "author": "ksh",
34
+ "license": "MIT",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/velajs/cloudflare.git"
38
+ },
39
+ "homepage": "https://github.com/velajs/cloudflare#readme",
40
+ "bugs": {
41
+ "url": "https://github.com/velajs/cloudflare/issues"
42
+ },
43
+ "engines": {
44
+ "node": ">=20"
45
+ },
46
+ "peerDependencies": {
47
+ "@velajs/vela": ">=0.1.0",
48
+ "hono": ">=4"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "@velajs/vela": {
52
+ "optional": true
53
+ }
54
+ },
55
+ "devDependencies": {
56
+ "@swc/cli": "^0.8.0",
57
+ "@swc/core": "^1.15.11",
58
+ "@velajs/vela": "^0.2.0",
59
+ "hono": "^4",
60
+ "typescript": "^5",
61
+ "unplugin-swc": "^1.5.9",
62
+ "vitest": "^4.0.18"
63
+ },
64
+ "scripts": {
65
+ "build": "rm -rf dist && swc src -d dist --strip-leading-paths && tsc --emitDeclarationOnly",
66
+ "test": "vitest run",
67
+ "typecheck": "tsc --noEmit"
68
+ }
69
+ }