effect-cf 0.15.0 → 0.17.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/CHANGELOG.md +12 -0
- package/README.md +124 -1
- package/dist/index.d.mts +333 -38
- package/dist/index.mjs +427 -49
- package/package.json +2 -1
- package/src/AnalyticsEngine.ts +846 -0
- package/src/ContainerNamespace.ts +337 -0
- package/src/index.ts +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# effect-cf
|
|
2
2
|
|
|
3
|
+
## 0.17.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#53](https://github.com/danieljvdm/effect-cf/pull/53) [`b36fc21`](https://github.com/danieljvdm/effect-cf/commit/b36fc2111670c6404a80d51a3c12b10080260256) Thanks [@danieljvdm](https://github.com/danieljvdm)! - Add `ContainerNamespace` services for named Cloudflare Container request forwarding and lifecycle operations.
|
|
8
|
+
|
|
9
|
+
## 0.16.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#51](https://github.com/danieljvdm/effect-cf/pull/51) [`9a6abd2`](https://github.com/danieljvdm/effect-cf/commit/9a6abd27366c71867a420f872459b3d5fc1b2143) Thanks [@owensbla](https://github.com/owensbla)! - Add Effect-native Cloudflare Analytics Engine helpers. `AnalyticsEngine.Tag(...)` now provides validated dataset writes, configurable invalid-write policy, and batch write helpers. `AnalyticsEngine.QueryTag(...)` / `makeQueryClient(...)` provide SQL API querying backed by Effect `HttpClient`, config and redacted API token support, typed result envelopes, and row decoding through Effect schemas.
|
|
14
|
+
|
|
3
15
|
## 0.15.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# effect-cf
|
|
2
2
|
|
|
3
|
-
Effect-native Cloudflare primitives for Workers, Durable Objects, bindings, KV, D1, Queues, Email, Workflows, and Durable Object storage.
|
|
3
|
+
Effect-native Cloudflare primitives for Workers, Durable Objects, Containers, bindings, KV, D1, Queues, Email, Analytics Engine, Workflows, and Durable Object storage.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -30,12 +30,14 @@ Runtime creation belongs at Cloudflare entrypoints, not inside binding helpers.
|
|
|
30
30
|
- `DurableObject` - Durable Object entrypoint factory and typed namespace helpers
|
|
31
31
|
- `DurableObjectState` / `DurableObjectStorage` - Effect wrappers for state, alarms, SQL, and embedded KV
|
|
32
32
|
- `DurableObjectWebSocket` - WebSocket upgrade helpers for Durable Objects
|
|
33
|
+
- `ContainerNamespace` - named Cloudflare Container instances with Effect-wrapped request and lifecycle operations
|
|
33
34
|
- `Kv` - typed KV namespace helper
|
|
34
35
|
- `D1` - typed D1 database binding helper with an `@effect/sql-d1` backed SQL layer
|
|
35
36
|
- `R2` - typed R2 bucket binding helper with Effect-wrapped object and multipart operations
|
|
36
37
|
- `Hyperdrive` - typed Hyperdrive binding helper for connection strings and optional Postgres SQL integration
|
|
37
38
|
- `Images` - typed Cloudflare Images binding helper with transformation APIs and optional hosted image operations
|
|
38
39
|
- `Email` - typed Cloudflare Send Email binding helper for `send_email` bindings
|
|
40
|
+
- `AnalyticsEngine` - typed Cloudflare Analytics Engine write bindings and SQL API query helpers
|
|
39
41
|
- `Queue` - typed Queue producer/consumer tags plus client and error types
|
|
40
42
|
- `Workflow` - typed Workflow entrypoints, steps, starter clients, and instance types
|
|
41
43
|
- `Rpc` - Cloudflare RPC type helpers and scoped disposal utilities
|
|
@@ -84,6 +86,46 @@ export const readCounter = Effect.gen(function* () {
|
|
|
84
86
|
|
|
85
87
|
Define Wrangler bindings and migrations in the consuming application. Durable Object namespace bindings are provided with `YourObject.layer({ binding })`, and consumers use `const namespace = yield* YourObject`.
|
|
86
88
|
|
|
89
|
+
## Container Example
|
|
90
|
+
|
|
91
|
+
Container entrypoints remain owned by `@cloudflare/containers`; `effect-cf`
|
|
92
|
+
wraps the Container namespace used by a calling Worker.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { Container, switchPort } from "@cloudflare/containers";
|
|
96
|
+
import { Effect, Layer } from "effect";
|
|
97
|
+
import { ContainerNamespace, Worker } from "effect-cf";
|
|
98
|
+
|
|
99
|
+
export class RendererContainer extends Container {
|
|
100
|
+
defaultPort = 8080;
|
|
101
|
+
sleepAfter = "10m";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
class Renderers extends ContainerNamespace.Tag<Renderers>()("Renderers") {}
|
|
105
|
+
|
|
106
|
+
const RenderersLive = Renderers.layer({ binding: "RENDERERS" });
|
|
107
|
+
|
|
108
|
+
export default Worker.make(RenderersLive, {
|
|
109
|
+
fetch: Effect.gen(function* () {
|
|
110
|
+
const request = yield* Worker.NativeRequest;
|
|
111
|
+
const renderer = Renderers.byName(new URL(request.url).pathname);
|
|
112
|
+
|
|
113
|
+
yield* renderer.startAndWaitForPorts({ ports: 8080 });
|
|
114
|
+
return yield* renderer.fetch(switchPort(request, 8080));
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The instance client exposes `state`, `fetch`, `start`,
|
|
120
|
+
`startAndWaitForPorts`, `stop`, `destroy`, and the Effect-valued native
|
|
121
|
+
`unsafeRaw` stub.
|
|
122
|
+
Responses are returned unchanged, including non-2xx and WebSocket upgrade
|
|
123
|
+
responses. If the Container uses outbound interception, also export
|
|
124
|
+
`ContainerProxy` from `@cloudflare/containers`.
|
|
125
|
+
|
|
126
|
+
See [`examples/containers/README.md`](../../examples/containers/README.md) for
|
|
127
|
+
the corresponding Wrangler configuration and entrypoint responsibilities.
|
|
128
|
+
|
|
87
129
|
## Queue Example
|
|
88
130
|
|
|
89
131
|
Queues define the message contract once. The same class is the producer service tag and the consumer Worker definition: use `YourQueue.layer({ binding })` to provide a Cloudflare producer binding from `WorkerEnvironment`, and use `.make(...)` for the consumer Worker entrypoint.
|
|
@@ -233,6 +275,87 @@ export const sendWelcomeEmail = (to: string) =>
|
|
|
233
275
|
});
|
|
234
276
|
```
|
|
235
277
|
|
|
278
|
+
## Analytics Engine Example
|
|
279
|
+
|
|
280
|
+
Analytics Engine tags expose Cloudflare dataset bindings as Effect-wrapped `writeDataPoint(...)` operations. Writes use Cloudflare's native non-blocking runtime API and are validated against Workers Analytics Engine limits before reaching the binding.
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
import { Effect } from "effect";
|
|
284
|
+
import { AnalyticsEngine } from "effect-cf";
|
|
285
|
+
|
|
286
|
+
class RequestAnalytics extends AnalyticsEngine.Tag<RequestAnalytics>()("RequestAnalytics") {}
|
|
287
|
+
|
|
288
|
+
export const RequestAnalyticsLayer = RequestAnalytics.layer({
|
|
289
|
+
binding: "REQUEST_ANALYTICS",
|
|
290
|
+
write: {
|
|
291
|
+
onInvalid: "error",
|
|
292
|
+
batchSize: 100,
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
export const recordPageView = (request: Request) =>
|
|
297
|
+
Effect.gen(function* () {
|
|
298
|
+
const analytics = yield* RequestAnalytics;
|
|
299
|
+
const url = new URL(request.url);
|
|
300
|
+
|
|
301
|
+
yield* analytics.writeDataPoint({
|
|
302
|
+
indexes: [url.hostname],
|
|
303
|
+
blobs: [url.pathname, request.headers.get("cf-connecting-country") ?? "unknown"],
|
|
304
|
+
doubles: [1],
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
yield* analytics.writeBatch(
|
|
308
|
+
[
|
|
309
|
+
{
|
|
310
|
+
indexes: [url.hostname],
|
|
311
|
+
blobs: [url.pathname, "page-view"],
|
|
312
|
+
doubles: [1],
|
|
313
|
+
},
|
|
314
|
+
],
|
|
315
|
+
{ onInvalid: "drop" },
|
|
316
|
+
);
|
|
317
|
+
});
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
Define the dataset binding in the consuming Worker's `wrangler.jsonc` with `analytics_engine_datasets`, then provide `RequestAnalytics.layer({ binding: "REQUEST_ANALYTICS" })` from the Worker layer. Invalid writes fail with `AnalyticsEngineWriteValidationError` by default. Use `write: { onInvalid: "drop" }` on the layer or `{ onInvalid: "drop" }` per call when dropping invalid points is preferable.
|
|
321
|
+
|
|
322
|
+
Analytics Engine query tags wrap Cloudflare's HTTP SQL API. Configuration can stay in Effect `Config`, including a redacted API token, the outbound transport is an Effect `HttpClient` dependency, and rows can be decoded with Effect schemas at the query boundary. Use `fetchLayerConfig(...)` as shorthand when the platform fetch-backed client is enough.
|
|
323
|
+
|
|
324
|
+
```ts
|
|
325
|
+
import { Effect, Layer, Schema as S } from "effect";
|
|
326
|
+
import { FetchHttpClient } from "effect/unstable/http";
|
|
327
|
+
import { AnalyticsEngine, WorkerConfig } from "effect-cf";
|
|
328
|
+
|
|
329
|
+
class AnalyticsQuery extends AnalyticsEngine.QueryTag<AnalyticsQuery>()("AnalyticsQuery") {}
|
|
330
|
+
|
|
331
|
+
export const AnalyticsQueryLayer = AnalyticsQuery.layerConfig(
|
|
332
|
+
AnalyticsEngine.queryConfig({
|
|
333
|
+
accountId: WorkerConfig.string("CLOUDFLARE_ACCOUNT_ID"),
|
|
334
|
+
apiToken: WorkerConfig.redacted("CLOUDFLARE_API_TOKEN"),
|
|
335
|
+
}),
|
|
336
|
+
).pipe(Layer.provide(FetchHttpClient.layer));
|
|
337
|
+
|
|
338
|
+
const PageView = S.Struct({
|
|
339
|
+
path: S.String,
|
|
340
|
+
views: S.Number,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
export const topPages = Effect.gen(function* () {
|
|
344
|
+
const analytics = yield* AnalyticsQuery;
|
|
345
|
+
|
|
346
|
+
return yield* analytics.queryRows(
|
|
347
|
+
PageView,
|
|
348
|
+
`
|
|
349
|
+
SELECT blob1 AS path, SUM(_sample_interval) AS views
|
|
350
|
+
FROM request_metrics
|
|
351
|
+
GROUP BY path
|
|
352
|
+
ORDER BY views DESC
|
|
353
|
+
LIMIT 10
|
|
354
|
+
`,
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
```
|
|
358
|
+
|
|
236
359
|
## Workflow Example
|
|
237
360
|
|
|
238
361
|
Workflow definitions type the payload and result. Runtime handlers can access `Workflow.WorkflowEvent`, use durable `Workflow.step(...)`, and use normal binding services inside steps.
|