@streamsy/core 0.2.0 → 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 +139 -0
- package/dist/cursor-generator-CGz1TO_N.js +13 -0
- package/dist/http/index.d.ts +2 -0
- package/dist/http/index.js +3 -0
- package/dist/http-EnNOUsIK.js +69 -0
- package/dist/index-CFv8vZvy.d.ts +513 -0
- package/dist/index.d.ts +3 -792
- package/dist/index.js +109 -3336
- package/dist/internal/index.d.ts +22 -0
- package/dist/internal/index.js +3 -0
- package/dist/layer-D8ybaCL2.js +340 -0
- package/dist/layer-KvknRFUT.js +656 -0
- package/dist/owner-BQzMpfC1.js +36 -0
- package/dist/owner-records-CX6yU28i.js +5 -0
- package/dist/owner-records-QDlssl__.d.ts +2963 -0
- package/dist/program-DJ8NpHIf.js +814 -0
- package/dist/protocol/index.d.ts +8 -0
- package/dist/protocol/index.js +16 -0
- package/dist/remote/index.d.ts +23 -0
- package/dist/remote/index.js +428 -0
- package/dist/rolldown-runtime-D7D4PA-g.js +13 -0
- package/dist/streamsy-OjRubIPC.js +829 -0
- package/dist/testing/index.d.ts +44 -0
- package/dist/testing/index.js +757 -0
- package/package.json +50 -13
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Gareth Andrew
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @streamsy/core
|
|
2
|
+
|
|
3
|
+
An Effect implementation of the [Durable Streams](https://durablestreams.com)
|
|
4
|
+
protocol. Typed stream declarations, stream services, an in-process memory Layer, an
|
|
5
|
+
HTTP app, and a client for remote hosts. Version 0.4.0 requires
|
|
6
|
+
`effect@4.0.1`.
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
bun add @streamsy/core effect
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { Effect, Schema, Stream } from "effect";
|
|
14
|
+
import { MemoryStorage, Streamsy } from "@streamsy/core";
|
|
15
|
+
|
|
16
|
+
class Events extends Streamsy.json<Events>()("Readme/Events", "events", {
|
|
17
|
+
params: {},
|
|
18
|
+
schema: Schema.String,
|
|
19
|
+
}) {}
|
|
20
|
+
const program = Effect.gen(function* () {
|
|
21
|
+
const events = yield* Events;
|
|
22
|
+
yield* events.create({});
|
|
23
|
+
yield* events.append({}, ["hello"]);
|
|
24
|
+
return yield* events.read({}).pipe(Stream.runCollect);
|
|
25
|
+
});
|
|
26
|
+
await Effect.runPromise(program.pipe(Effect.provide(MemoryStorage.layer(Streamsy.group(Events)))));
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Compiled source: [packages/core/examples/readme.ts](https://github.com/gingerhendrix/streamsy/blob/effect-first-live-perimeter/packages/core/examples/readme.ts).
|
|
30
|
+
|
|
31
|
+
## What you get
|
|
32
|
+
|
|
33
|
+
- `StreamAddress` names a stream with its content type and schema, including
|
|
34
|
+
`StreamAddress.state(id, { collections })` for a Durable State stream. One address
|
|
35
|
+
declares several collections keyed by their wire `type`. `Streamsy` declares
|
|
36
|
+
a named stream service from an id template.
|
|
37
|
+
- Stream services create, append, read, follow, and remove streams. Create and
|
|
38
|
+
append return `_tag` variants such as `Created` / `Exists` and
|
|
39
|
+
`Appended` / `Duplicate`. Protocol failures are tagged errors
|
|
40
|
+
(`StreamNotFound`, `StreamGone`, `OffsetMismatch`, `AppendConflict`) for
|
|
41
|
+
`Effect.catchTag`.
|
|
42
|
+
- `Streams.follow(address, { offset, retry })` catches up and then waits for new
|
|
43
|
+
items. When a read fails with a `TransportFault` or `StorageFault`, it tries
|
|
44
|
+
that read again from the same offset, so a server restart or network drop
|
|
45
|
+
yields each item once. The default `retry` schedule starts at 200 ms, adds
|
|
46
|
+
jitter, stops growing at 30 s and never gives up. Other failures end the
|
|
47
|
+
stream. One-shot reads are not retried.
|
|
48
|
+
- `Producer.Position` describes a producer tuple. `Producer.next` advances its sequence after acknowledgement.
|
|
49
|
+
- `Streamsy` and storage layers that take a group bind each stream to its backend.
|
|
50
|
+
|
|
51
|
+
### Producer restarts
|
|
52
|
+
|
|
53
|
+
The protocol does not allocate producer epochs. If a producer cannot recompute
|
|
54
|
+
a payload after restart, persist the payload together with its `producerId`,
|
|
55
|
+
`producerEpoch`, and `producerSeq` before appending. Re-send that same tuple and
|
|
56
|
+
payload after a restart; `Duplicate` then means the earlier send landed.
|
|
57
|
+
|
|
58
|
+
## Entries
|
|
59
|
+
|
|
60
|
+
| Entry | Contents |
|
|
61
|
+
| ------------------------- | ----------------------------------------------------------------------------------------- |
|
|
62
|
+
| `@streamsy/core` | Streamsy, MemoryStorage, StreamAddress, DurableState, Producer, storage contracts, errors |
|
|
63
|
+
| `@streamsy/core/remote` | `RemoteStreams.layer(streams, { baseUrl })`: the same stream services over HTTP |
|
|
64
|
+
| `@streamsy/core/http` | `Http.routes`, `Http.app`, `Http.read`, and `securityHeaders` |
|
|
65
|
+
| `@streamsy/core/protocol` | StreamsReader, StreamsWriter, Streams, Protocol, StreamRoute, and raw routes |
|
|
66
|
+
| `@streamsy/core/testing` | Vitest contract tests and fault injection for backends |
|
|
67
|
+
|
|
68
|
+
## Storage and hosts
|
|
69
|
+
|
|
70
|
+
The memory Layer is process-local and not persistent. Its commit boundary copies
|
|
71
|
+
the whole store for each mutation, so one append costs linear time in the number
|
|
72
|
+
of stored messages. It is a development host; for anything that grows, use
|
|
73
|
+
SQLite from [`@streamsy/storage`](https://www.npmjs.com/package/@streamsy/storage),
|
|
74
|
+
on Bun or in a Durable Object.
|
|
75
|
+
Serve `Http.routes` with Effect’s `HttpRouter.serve` or `HttpRouter.toWebHandler`.
|
|
76
|
+
`Http.read(id, options)` reads a resolved stream id; `securityHeaders` supplies the
|
|
77
|
+
shared response headers. [`@streamsy/serve`](https://www.npmjs.com/package/@streamsy/serve)
|
|
78
|
+
provides output routes and host glue.
|
|
79
|
+
|
|
80
|
+
Browsers use the official `@durable-streams/client` and
|
|
81
|
+
`@durable-streams/state` packages directly against any Streamsy host.
|
|
82
|
+
|
|
83
|
+
`MemoryStorage.layer(streams, { readLimit: 1000 })` sets the server catch-up page size.
|
|
84
|
+
Protocol adapters can use `Protocol.layer({ readLimit })` from `@streamsy/core/protocol`. The default is
|
|
85
|
+
1000 messages; `readNext` returns the whole available tail. Remote clients send
|
|
86
|
+
only an offset and accept the remote server’s page size. Both direct read
|
|
87
|
+
methods reject malformed offsets with `InvalidReadRequest`; accepted offsets
|
|
88
|
+
are canonical tokens, `-1`, and `now` (or an omitted catch-up offset).
|
|
89
|
+
|
|
90
|
+
Producer options and `Producer.Position` use
|
|
91
|
+
`{ producerId, producerEpoch, producerSeq }`. Append results carry
|
|
92
|
+
`producerEpoch` and `producerSeq` when the acknowledgement includes producer
|
|
93
|
+
state. A close-only producer write uses HTTP 204 and decodes as `Duplicate`
|
|
94
|
+
when that state is present. A close on an already closed stream with a fresh
|
|
95
|
+
tuple returns `Appended` without producer state, including retries of that
|
|
96
|
+
unpersisted tuple.
|
|
97
|
+
|
|
98
|
+
`Http.routes(streams, { access: "read", sseDeadlineMs })`, `Http.app`, and `Http.read` accept an SSE lifetime in
|
|
99
|
+
milliseconds. It defaults to 60,000 and closes the body normally at the
|
|
100
|
+
deadline. Each connection draws its own deadline from 90 to 110 percent of
|
|
101
|
+
this value, so connections opened together do not all close together. Each SSE
|
|
102
|
+
batch goes out as one chunk, so the body ends only after a whole batch and its
|
|
103
|
+
control event. Host-forced interruption and client cancellation remain owned by
|
|
104
|
+
the host. Read results include `contentType`; a remote long poll returning 204
|
|
105
|
+
without Content-Type resolves it with HEAD.
|
|
106
|
+
|
|
107
|
+
## Row changes
|
|
108
|
+
|
|
109
|
+
`DurableState` is exported from `@streamsy/core`. Direct writers and projection
|
|
110
|
+
outputs use it to make row changes for `Streamsy.state` routes.
|
|
111
|
+
|
|
112
|
+
| Change | Call |
|
|
113
|
+
| ------ | --------------------------------------------------------------------- |
|
|
114
|
+
| Upsert | `DurableState.upsert("card", row)` |
|
|
115
|
+
| Delete | `DurableState.remove("card", key)` |
|
|
116
|
+
| Delete | `DurableState.remove("card", key, { oldValue })` (writes `old_value`) |
|
|
117
|
+
|
|
118
|
+
`DurableState.Change<Collections>` checks the collection and row types. Each
|
|
119
|
+
change has a `_tag` of `"Upsert"` or `"Delete"`.
|
|
120
|
+
`DurableState.changes(address, { offset }, changes)` makes the wire events. With
|
|
121
|
+
an `offset`, each event gets that offset and a default `txid`. Projection outputs
|
|
122
|
+
have no source offset. The route's collection names and key fields define the
|
|
123
|
+
Durable State wire events.
|
|
124
|
+
|
|
125
|
+
## Documentation
|
|
126
|
+
|
|
127
|
+
- [Declare streams](https://streamsy.dev/docs/streams/declare)
|
|
128
|
+
- [Layers and storage](https://streamsy.dev/docs/hosting)
|
|
129
|
+
- [Protocol routes](https://streamsy.dev/docs/http/routes)
|
|
130
|
+
- [Remote client](https://streamsy.dev/docs/http/remote)
|
|
131
|
+
- [Write a storage adapter](https://streamsy.dev/docs/hosting/storage-adapter)
|
|
132
|
+
|
|
133
|
+
Source: https://github.com/gingerhendrix/streamsy
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
MIT
|
|
138
|
+
|
|
139
|
+
An address names one stream. It holds no data. It is not an Effect `Ref`.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DateTime } from "effect";
|
|
2
|
+
//#region src/policy/cursor-generator.ts
|
|
3
|
+
const CURSOR_EPOCH_MS = DateTime.toEpochMillis(DateTime.makeUnsafe("2024-10-09T00:00:00.000Z"));
|
|
4
|
+
const CURSOR_INTERVAL_MS = 2e4;
|
|
5
|
+
function generateCursor(clock, previous, random = Math.random) {
|
|
6
|
+
const currentInterval = Math.floor((clock.now() - CURSOR_EPOCH_MS) / CURSOR_INTERVAL_MS);
|
|
7
|
+
if (!previous) return String(currentInterval);
|
|
8
|
+
const previousInterval = parseInt(previous, 10);
|
|
9
|
+
if (Number.isNaN(previousInterval) || previousInterval < currentInterval) return String(currentInterval);
|
|
10
|
+
return String(previousInterval + Math.max(1, Math.floor(random() * 180)));
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { generateCursor as t };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { H as HttpOptions, U as app, V as streamPath, a as securityHeaders, c as read, d as routes, i as layerShutdownSignal, l as Access, n as ShutdownSignal, o as ReadOptions, r as ShutdownSignalService, s as checkReadOptions, u as RoutesOptions } from "../index-CFv8vZvy.js";
|
|
2
|
+
export { type Access, type HttpOptions, type ReadOptions, type RoutesOptions, ShutdownSignal, type ShutdownSignalService, app, checkReadOptions, layerShutdownSignal, read, routes, securityHeaders, streamPath };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as ShutdownSignal, c as streamPath, l as securityHeaders, n as checkReadOptions, o as layerShutdownSignal, r as read, t as app } from "../program-DJ8NpHIf.js";
|
|
2
|
+
import { n as routes } from "../http-EnNOUsIK.js";
|
|
3
|
+
export { ShutdownSignal, app, checkReadOptions, layerShutdownSignal, read, routes, securityHeaders, streamPath };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.js";
|
|
2
|
+
import { A as StreamsReader, i as checkStreams, j as StreamsWriter } from "./streamsy-OjRubIPC.js";
|
|
3
|
+
import { a as ShutdownSignal, c as streamPath, i as requestUrl, l as securityHeaders, n as checkReadOptions, o as layerShutdownSignal, r as read, s as discardRequestBody, t as app } from "./program-DJ8NpHIf.js";
|
|
4
|
+
import { Effect } from "effect";
|
|
5
|
+
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
6
|
+
//#region src/http/routes.ts
|
|
7
|
+
/** The `allow` header for each access level. */
|
|
8
|
+
const allowedMethods = {
|
|
9
|
+
read: "GET, HEAD, OPTIONS",
|
|
10
|
+
append: "GET, HEAD, POST, OPTIONS",
|
|
11
|
+
full: "GET, HEAD, PUT, POST, DELETE, OPTIONS"
|
|
12
|
+
};
|
|
13
|
+
function routes(group, options) {
|
|
14
|
+
const entries = group.streams.map((stream) => ({
|
|
15
|
+
stream,
|
|
16
|
+
access: typeof options.access === "function" ? options.access(stream) : options.access
|
|
17
|
+
}));
|
|
18
|
+
const settings = options;
|
|
19
|
+
const prefix = settings.prefix?.replace(/\/$/, "") || "/";
|
|
20
|
+
if (!prefix.startsWith("/") || /[?:#*]/.test(prefix) || prefix.includes("//")) throw new RangeError("HTTP prefix must be an absolute literal path");
|
|
21
|
+
const path = streamPath(prefix);
|
|
22
|
+
const program = app({
|
|
23
|
+
...settings,
|
|
24
|
+
pathPrefix: prefix
|
|
25
|
+
});
|
|
26
|
+
checkStreams(entries.map((entry) => entry.stream));
|
|
27
|
+
const handler = Effect.gen(function* () {
|
|
28
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
29
|
+
const url = requestUrl(request);
|
|
30
|
+
const id = url === void 0 ? void 0 : path.strip(url.pathname);
|
|
31
|
+
const entry = entries.find(({ stream }) => id !== void 0 && stream.match(id));
|
|
32
|
+
if (entry === void 0) {
|
|
33
|
+
yield* discardRequestBody(request);
|
|
34
|
+
return HttpServerResponse.empty({
|
|
35
|
+
status: 404,
|
|
36
|
+
headers: securityHeaders
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const allow = allowedMethods[entry.access];
|
|
40
|
+
if (entry.access !== "full" && !allow.split(", ").includes(request.method)) {
|
|
41
|
+
yield* discardRequestBody(request);
|
|
42
|
+
return HttpServerResponse.empty({
|
|
43
|
+
status: 405,
|
|
44
|
+
headers: {
|
|
45
|
+
...securityHeaders,
|
|
46
|
+
allow
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const service = yield* entry.stream;
|
|
51
|
+
const response = yield* program.pipe(Effect.provideService(StreamsReader, service.wire), Effect.provideService(StreamsWriter, service.wire));
|
|
52
|
+
return request.method === "OPTIONS" && entry.access !== "full" ? HttpServerResponse.setHeader(response, "access-control-allow-methods", allow) : response;
|
|
53
|
+
});
|
|
54
|
+
return HttpRouter.add("*", prefix === "/" ? "/*" : `/${prefix.slice(1)}/*`, handler);
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/http/index.ts
|
|
58
|
+
var http_exports = /* @__PURE__ */ __exportAll({
|
|
59
|
+
ShutdownSignal: () => ShutdownSignal,
|
|
60
|
+
app: () => app,
|
|
61
|
+
checkReadOptions: () => checkReadOptions,
|
|
62
|
+
layerShutdownSignal: () => layerShutdownSignal,
|
|
63
|
+
read: () => read,
|
|
64
|
+
routes: () => routes,
|
|
65
|
+
securityHeaders: () => securityHeaders,
|
|
66
|
+
streamPath: () => streamPath
|
|
67
|
+
});
|
|
68
|
+
//#endregion
|
|
69
|
+
export { routes as n, http_exports as t };
|