@adonix.org/cloud-spark 0.0.184 → 0.0.185
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/README.md +145 -66
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -9,20 +9,11 @@
|
|
|
9
9
|
|
|
10
10
|
**_Ignite_** your Cloudflare Workers with a type-safe library for rapid development.
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
CloudSpark provides a logical foundation for building Cloudflare Workers. It works well for simple workers or projects that grow in complexity, helping keep code organized and functionality scalable. It is lightweight and designed to let you focus on the logic that powers your worker.
|
|
13
13
|
|
|
14
|
-
-
|
|
15
|
-
- CORS
|
|
16
|
-
- Caching
|
|
17
|
-
- WebSockets
|
|
14
|
+
:bulb: If you are new to _Cloudflare Workers_, create a free [Cloudflare account](https://dash.cloudflare.com/sign-up) and install their command line interface [Wrangler](#cowboy_hat_face-wrangler).
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- Custom Middleware
|
|
22
|
-
- Nested Workers
|
|
23
|
-
- Advanced Routing
|
|
24
|
-
|
|
25
|
-
:bulb: If you are new to _Cloudflare Workers_, create a free [Cloudflare account](https://dash.cloudflare.com/sign-up) and install their command line interface [Wrangler](#cowboy_hat_face-wrangler). Detailed worker documentation can also be found [here](https://developers.cloudflare.com/workers/).
|
|
16
|
+
Detailed worker documentation can also be found [here](https://developers.cloudflare.com/workers/).
|
|
26
17
|
|
|
27
18
|
<br>
|
|
28
19
|
|
|
@@ -36,22 +27,16 @@ npm install @adonix.org/cloud-spark
|
|
|
36
27
|
|
|
37
28
|
## :rocket: Quickstart
|
|
38
29
|
|
|
39
|
-
:page_facing_up:
|
|
30
|
+
:page_facing_up: index.ts
|
|
40
31
|
|
|
41
32
|
```ts
|
|
42
33
|
import { BasicWorker, TextResponse } from "@adonix.org/cloud-spark";
|
|
43
34
|
|
|
44
|
-
|
|
35
|
+
class HelloWorld extends BasicWorker {
|
|
45
36
|
get() {
|
|
46
37
|
return this.response(TextResponse, "Hi from Cloud Spark!");
|
|
47
38
|
}
|
|
48
39
|
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
:page_facing_up: index.ts
|
|
52
|
-
|
|
53
|
-
```ts
|
|
54
|
-
import { HelloWorld } from "./hello-world";
|
|
55
40
|
|
|
56
41
|
export default HelloWorld.ignite();
|
|
57
42
|
```
|
|
@@ -68,76 +53,174 @@ And it's ready on http://localhost:8787
|
|
|
68
53
|
|
|
69
54
|
## :arrow_right: Basic Worker
|
|
70
55
|
|
|
71
|
-
As shown in the [Quickstart](#rocket-quickstart), BasicWorker is the base class for building Cloudflare Workers with
|
|
56
|
+
As shown in the [Quickstart](#rocket-quickstart), BasicWorker is the base class for building Cloudflare Workers with CloudSpark. It handles common tasks, including:
|
|
72
57
|
|
|
73
58
|
- Dispatching incoming HTTP requests to the corresponding handler (GET, POST, PUT, etc.).
|
|
74
|
-
- Catching unhandled errors and returning a structured 500 InternalServerError.
|
|
75
59
|
- Providing defaults for standard HTTP behavior, such as HEAD requests and OPTIONS responses.
|
|
76
60
|
- Ensuring type safety and consistent response formatting.
|
|
61
|
+
- Support for built-in and custom middleware.
|
|
62
|
+
- Catching unhandled errors.
|
|
77
63
|
|
|
78
|
-
Subclasses only need to implement the HTTP methods that their Worker will handle. Each method can be overridden independently, and additional functionality such as middleware can be added as needed.
|
|
79
|
-
|
|
80
|
-
## :hammer_and_wrench: Basic Worker API
|
|
81
|
-
|
|
82
|
-
#### `getAllowedMethods(): Method[]`
|
|
64
|
+
Subclasses only need to implement the HTTP methods that their Worker will handle. Each method can be overridden independently, and additional functionality such as [middleware](#gear-middleware) can be added as needed.
|
|
83
65
|
|
|
84
|
-
|
|
66
|
+
Building on the [Quickstart](#rocket-quickstart), what follows is a more complete example:
|
|
85
67
|
|
|
86
|
-
|
|
87
|
-
- **Notes:** Subclasses must override to allow additional methods. Any request using a method not listed will automatically return a `405 Method Not Allowed` response.
|
|
88
|
-
|
|
89
|
-
#### `get(): Promise<Response>`
|
|
90
|
-
|
|
91
|
-
Override this method to handle `GET` requests.
|
|
68
|
+
:page_facing_up: index.ts
|
|
92
69
|
|
|
93
|
-
|
|
94
|
-
|
|
70
|
+
```ts
|
|
71
|
+
import { BasicWorker, JsonResponse, Method, POST, TextResponse } from "@adonix.org/cloud-spark";
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* To access the Cloudflare runtime properties:
|
|
75
|
+
* • this.request — the incoming Request
|
|
76
|
+
* • this.env — environment bindings (KV, R2, etc.)
|
|
77
|
+
* • this.ctx — the execution context for background tasks
|
|
78
|
+
*/
|
|
79
|
+
export class MyWorker extends BasicWorker {
|
|
80
|
+
/**
|
|
81
|
+
* Override to allow additional method support for the worker.
|
|
82
|
+
* GET and HEAD requests are always allowed.
|
|
83
|
+
*
|
|
84
|
+
* Default: GET, HEAD, OPTIONS
|
|
85
|
+
*
|
|
86
|
+
* For OPTIONS requests, the default response is:
|
|
87
|
+
* 204 No Content
|
|
88
|
+
* "Allow" response header contains all allowed methods.
|
|
89
|
+
*
|
|
90
|
+
* If a requested method is not listed, the response is:
|
|
91
|
+
* 405 Method Not Allowed
|
|
92
|
+
*
|
|
93
|
+
* If an allowed method isn’t implemented, the response is:
|
|
94
|
+
* GET or HEAD: 404 Not Found
|
|
95
|
+
* All other methods: 501 Not Implemented
|
|
96
|
+
*
|
|
97
|
+
* This example adds POST method support to the defaults.
|
|
98
|
+
*/
|
|
99
|
+
public override getAllowedMethods(): Method[] {
|
|
100
|
+
return [...super.getAllowedMethods(), POST];
|
|
101
|
+
}
|
|
95
102
|
|
|
96
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Example handler for GET requests that returns a simple
|
|
105
|
+
* text response.
|
|
106
|
+
*/
|
|
107
|
+
protected override get(): Promise<Response> {
|
|
108
|
+
return this.response(TextResponse, "Hello from Cloud Spark!");
|
|
109
|
+
}
|
|
97
110
|
|
|
98
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Example handler for POST requests that echoes the
|
|
113
|
+
* incoming JSON.
|
|
114
|
+
*/
|
|
115
|
+
protected override async post(): Promise<Response> {
|
|
116
|
+
const json = await this.request.json();
|
|
117
|
+
// Do something with the request JSON.
|
|
99
118
|
|
|
100
|
-
|
|
101
|
-
|
|
119
|
+
return this.response(JsonResponse, json);
|
|
120
|
+
}
|
|
102
121
|
|
|
103
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Supported BasicWorker request methods:
|
|
124
|
+
* protected override get(): Promise<Response>
|
|
125
|
+
* protected override put(): Promise<Response>
|
|
126
|
+
* protected override post(): Promise<Response>
|
|
127
|
+
* protected override patch(): Promise<Response>
|
|
128
|
+
* protected override delete(): Promise<Response>
|
|
129
|
+
*
|
|
130
|
+
* Implementations are provided but can be overridden for:
|
|
131
|
+
* protected override head(): Promise<Response>
|
|
132
|
+
* protected override options(): Promise<Response>
|
|
133
|
+
*/
|
|
134
|
+
}
|
|
104
135
|
|
|
105
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Connects this worker to the Cloudflare runtime.
|
|
138
|
+
*/
|
|
139
|
+
export default MyWorker.ignite();
|
|
140
|
+
```
|
|
106
141
|
|
|
107
|
-
|
|
108
|
-
- **Notes:** Ideal for form submissions, JSON payloads, or resource creation.
|
|
142
|
+
<br>
|
|
109
143
|
|
|
110
|
-
|
|
144
|
+
## :twisted_rightwards_arrows: Route Worker
|
|
111
145
|
|
|
112
|
-
|
|
146
|
+
RouteWorker extends [BasicWorker](#arrow_right-basic-worker) to provide route-based request handling making it easy to define multiple endpoints in a single Worker. It provides:
|
|
113
147
|
|
|
114
|
-
-
|
|
115
|
-
-
|
|
148
|
+
- Registering routes individually or in bulk.
|
|
149
|
+
- Matching incoming requests to registered routes by HTTP method and path.
|
|
150
|
+
- Support for URL path patterns using [path-to-regex](https://github.com/pillarjs/path-to-regexp) syntax.
|
|
151
|
+
- Dispatching requests to either a callback function or another Worker.
|
|
116
152
|
|
|
117
|
-
|
|
153
|
+
Example:
|
|
118
154
|
|
|
119
|
-
|
|
155
|
+
:page_facing_up: index.ts
|
|
120
156
|
|
|
121
|
-
|
|
122
|
-
|
|
157
|
+
```ts
|
|
158
|
+
import { BasicWorker, GET, PathParams, RouteWorker, TextResponse } from "@adonix.org/cloud-spark";
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* An example worker with path routing.
|
|
162
|
+
*/
|
|
163
|
+
class GreetingWorker extends RouteWorker {
|
|
164
|
+
/**
|
|
165
|
+
* Called before request processing to enable worker
|
|
166
|
+
* initialization without overriding the constructor.
|
|
167
|
+
*/
|
|
168
|
+
protected override init(): void {
|
|
169
|
+
/**
|
|
170
|
+
* Example of path-to-regex and local method routing.
|
|
171
|
+
*/
|
|
172
|
+
this.route(GET, "/hello/:name", this.hello);
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Example of simple path to a nested worker.
|
|
176
|
+
*/
|
|
177
|
+
this.route(GET, "/goodbye", GoodbyeWorker);
|
|
178
|
+
}
|
|
123
179
|
|
|
124
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Path parameters are provided via path-to-regex parsing
|
|
182
|
+
* of the request path.
|
|
183
|
+
*
|
|
184
|
+
* For example, http://localhost:8787/hello/Inigo will yield
|
|
185
|
+
* the text response "Hello Inigo!"
|
|
186
|
+
*/
|
|
187
|
+
protected hello(params: PathParams): Promise<Response> {
|
|
188
|
+
return this.response(TextResponse, `Hello ${params["name"]}!`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
125
191
|
|
|
126
|
-
|
|
192
|
+
/**
|
|
193
|
+
* An example nested BasicWorker.
|
|
194
|
+
*
|
|
195
|
+
* The original request, env, and ctx are passed to the nested
|
|
196
|
+
* worker via the constructor.
|
|
197
|
+
*
|
|
198
|
+
* RouteWorkers may also be nested to access path parameters.
|
|
199
|
+
*/
|
|
200
|
+
class GoodbyeWorker extends BasicWorker {
|
|
201
|
+
/**
|
|
202
|
+
* GET handler for the "/goodbye" path.
|
|
203
|
+
*/
|
|
204
|
+
protected override get(): Promise<Response> {
|
|
205
|
+
return this.response(TextResponse, "Goodbye!");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
127
208
|
|
|
128
|
-
|
|
129
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Connects GreetingWorker to the Cloudflare runtime.
|
|
211
|
+
*/
|
|
212
|
+
export default GreetingWorker.ignite();
|
|
213
|
+
```
|
|
130
214
|
|
|
131
|
-
|
|
215
|
+
<br>
|
|
132
216
|
|
|
133
|
-
|
|
217
|
+
## :gear: Middleware
|
|
134
218
|
|
|
135
|
-
|
|
136
|
-
- **Notes:** `GET`, `HEAD`, and `OPTIONS` are included by default.
|
|
219
|
+
### CORS
|
|
137
220
|
|
|
138
|
-
|
|
221
|
+
### Cache
|
|
139
222
|
|
|
140
|
-
|
|
223
|
+
### Custom
|
|
141
224
|
|
|
142
225
|
<br>
|
|
143
226
|
|
|
@@ -145,10 +228,6 @@ Override to handle `OPTIONS` requests.
|
|
|
145
228
|
|
|
146
229
|
<br>
|
|
147
230
|
|
|
148
|
-
## :gear: Middleware
|
|
149
|
-
|
|
150
|
-
<br>
|
|
151
|
-
|
|
152
231
|
## :cowboy_hat_face: Wrangler
|
|
153
232
|
|
|
154
233
|
First, create a **FREE** [Cloudflare account](https://dash.cloudflare.com/sign-up).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonix.org/cloud-spark",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.185",
|
|
4
4
|
"description": "Ignite your Cloudflare Workers with a type-safe library for rapid development.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -51,21 +51,21 @@
|
|
|
51
51
|
"lint": "eslint ./src"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@eslint/js": "^9.39.
|
|
55
|
-
"@types/node": "^24.
|
|
54
|
+
"@eslint/js": "^9.39.1",
|
|
55
|
+
"@types/node": "^24.10.0",
|
|
56
56
|
"@typescript-eslint/eslint-plugin": "^8.46.2",
|
|
57
|
-
"@typescript-eslint/parser": "^8.46.
|
|
58
|
-
"@vitest/coverage-v8": "^4.0.
|
|
59
|
-
"eslint": "^9.39.
|
|
57
|
+
"@typescript-eslint/parser": "^8.46.3",
|
|
58
|
+
"@vitest/coverage-v8": "^4.0.8",
|
|
59
|
+
"eslint": "^9.39.1",
|
|
60
60
|
"eslint-plugin-import": "^2.32.0",
|
|
61
61
|
"globals": "^16.5.0",
|
|
62
62
|
"jiti": "^2.6.1",
|
|
63
63
|
"prettier": "^3.6.2",
|
|
64
64
|
"tsup": "^8.5.0",
|
|
65
65
|
"typescript": "^5.9.3",
|
|
66
|
-
"typescript-eslint": "^8.46.
|
|
67
|
-
"vitest": "^4.0.
|
|
68
|
-
"wrangler": "^4.
|
|
66
|
+
"typescript-eslint": "^8.46.3",
|
|
67
|
+
"vitest": "^4.0.8",
|
|
68
|
+
"wrangler": "^4.47.0"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"cache-control-parser": "^2.0.6",
|