@streamsy/core 0.16.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.
@@ -1,193 +0,0 @@
1
- import { Context, Data, Effect, Option, Predicate, Record, Schema } from "effect";
2
- // src/fault.ts
3
- var StorageFault = class extends Schema.TaggedError()("StorageFault", {
4
- operation: Schema.String,
5
- message: Schema.String,
6
- retryable: Schema.Boolean,
7
- cause: Schema.optionalKey(Schema.Defect())
8
- }) {};
9
- var EncodeFault = class extends Schema.TaggedError()("EncodeFault", {
10
- message: Schema.String,
11
- cause: Schema.optionalKey(Schema.Defect())
12
- }) {};
13
- var DecodeFault = class extends Schema.TaggedError()("DecodeFault", {
14
- message: Schema.String,
15
- cause: Schema.optionalKey(Schema.Defect())
16
- }) {};
17
- /** Failures of the remote boundary; protocol rejections have their own typed errors. */
18
- var TransportFault = class extends Schema.TaggedError()("TransportFault", {
19
- operation: Schema.String,
20
- reason: Schema.Literals([
21
- "request",
22
- "response",
23
- "decode",
24
- "configuration"
25
- ]),
26
- /** HTTP status of the response, when available. */
27
- status: Schema.optionalKey(Schema.Int),
28
- message: Schema.String,
29
- cause: Schema.optionalKey(Schema.Defect())
30
- }) {
31
- /** Failed requests, 429s and 5xx responses can succeed later. */
32
- get retryable() {
33
- return this.reason === "request" || this.status !== void 0 && (this.status === 429 || this.status >= 500);
34
- }
35
- };
36
- const StreamsFault = Schema.Union([StorageFault, TransportFault]);
37
- /** A storage or transport failure, as opposed to a protocol rejection. */
38
- const isStreamsFault = Schema.is(StreamsFault);
39
- /** A storage or transport failure that can succeed later. */
40
- const isRetryableFault = (error) => isStreamsFault(error) && error.retryable;
41
- // src/internal/owner.ts
42
- /** Private bridge: a stream guard dies with this sentinel; the atomic runner translates it. */
43
- const ForeignOwnerType = Symbol.for("@streamsy/core/internal/ForeignOwner");
44
- var ForeignOwner = class extends Data.TaggedError("ForeignOwner") {
45
- stream;
46
- [ForeignOwnerType] = true;
47
- constructor(stream, knownOwner) {
48
- super({ message: knownOwner ? `Stream write to ${stream} does not share the checkpoint owner` : `Cannot establish a storage owner for ${stream}; the exposed Storage does not own the protocol wires` });
49
- this.stream = stream;
50
- }
51
- };
52
- const isForeignOwner = (value) => Predicate.hasProperty(value, ForeignOwnerType) && value[ForeignOwnerType] === true;
53
- const OwnerMetadata = Symbol.for("@streamsy/core/internal/CommitOwner");
54
- const attachOwner = (value, owner) => Object.defineProperty(value, OwnerMetadata, {
55
- value: owner,
56
- enumerable: false
57
- });
58
- const ownerOf = (value) => {
59
- if (!(OwnerMetadata in value)) return value;
60
- const owner = value[OwnerMetadata];
61
- return Predicate.isObjectKeyword(owner) ? owner : value;
62
- };
63
- const CurrentOwner = Context.Reference("@streamsy/core/internal/CurrentOwner", { defaultValue: () => void 0 });
64
- const checkWriteOwner = (owner, id, knownOwner) => Effect.flatMap(CurrentOwner, (expected) => expected === void 0 || expected === owner ? Effect.void : Effect.die(new ForeignOwner(id, knownOwner)));
65
- const Metadata = Symbol.for("@streamsy/core/internal/StorageOwner");
66
- const attachStorage = (wire, storage) => Object.defineProperty(wire, Metadata, {
67
- value: storage,
68
- enumerable: false
69
- });
70
- const storageOf = (wire) => {
71
- if (!(Metadata in wire)) return void 0;
72
- return wire[Metadata];
73
- };
74
- // src/toolkit/route-internal.ts
75
- /** Ids are canonical relative paths, the same rule the remote Layer enforces. */
76
- function isCanonicalIdSegment(value) {
77
- if (value.length === 0 || value === "." || value === "..") return false;
78
- return !/[%?#/\\]/.test(value);
79
- }
80
- function compileSegments(template) {
81
- if (template.length === 0) throw new RangeError("a stream route template must name at least one segment");
82
- if (template.startsWith("/")) throw new RangeError("a stream route template is relative and cannot start with a slash");
83
- if (/[?#*]/.test(template)) throw new RangeError("a stream route template cannot contain a query, fragment, or wildcard");
84
- const names = [];
85
- const segments = [];
86
- for (const part of template.split("/")) {
87
- if (part.length === 0) throw new RangeError("a stream route template cannot contain an empty segment");
88
- if (!part.startsWith(":")) {
89
- if (!isCanonicalIdSegment(part)) throw new RangeError(`a stream route literal segment is not a canonical id segment: ${part}`);
90
- segments.push({
91
- kind: "literal",
92
- value: part
93
- });
94
- continue;
95
- }
96
- const name = part.slice(1);
97
- if (name.length === 0) throw new RangeError("a stream route parameter needs a name");
98
- if (names.includes(name)) throw new RangeError(`duplicate stream route parameter: ${name}`);
99
- names.push(name);
100
- segments.push({
101
- kind: "parameter",
102
- name
103
- });
104
- }
105
- return {
106
- template,
107
- segments: Object.freeze(segments),
108
- names: Object.freeze(names)
109
- };
110
- }
111
- /**
112
- * True when two template routes can both match one id.
113
- *
114
- * Lengths must agree, and every position must be either an equal literal pair
115
- * or a parameter on at least one side. A parameter is treated as matching any
116
- * segment, so a codec-constrained parameter can report an overlap it would
117
- * never actually match. A custom route carries no template and is never
118
- * checked; HTTP dispatch uses declaration order and the first match wins.
119
- */
120
- function templatesOverlap(left, right) {
121
- if (left.length === 0 || right.length === 0) return false;
122
- const a = compileSegments(left).segments;
123
- const b = compileSegments(right).segments;
124
- if (a.length !== b.length) return false;
125
- for (const [index, segment] of a.entries()) {
126
- const other = b[index];
127
- if (!other || segment.kind === "parameter" || other.kind === "parameter") continue;
128
- if (segment.value !== other.value) return false;
129
- }
130
- return true;
131
- }
132
- // src/toolkit/state-wire.ts
133
- const upsertOperation = Schema.Union([
134
- Schema.Literal("upsert"),
135
- Schema.Literal("insert").transform("upsert"),
136
- Schema.Literal("update").transform("upsert")
137
- ]);
138
- const headers = {
139
- offset: Schema.optionalKey(Schema.String),
140
- txid: Schema.optionalKey(Schema.String),
141
- timestamp: Schema.optionalKey(Schema.String),
142
- from: Schema.optionalKey(Schema.String)
143
- };
144
- const changeCodec = (type, value) => Schema.Union([Schema.Struct({
145
- type,
146
- key: Schema.String,
147
- value,
148
- headers: Schema.Struct({
149
- operation: upsertOperation,
150
- ...headers
151
- })
152
- }), Schema.Struct({
153
- type,
154
- key: Schema.String,
155
- old_value: Schema.optionalKey(value),
156
- headers: Schema.Struct({
157
- operation: Schema.Literal("delete"),
158
- ...headers
159
- })
160
- })]);
161
- const controlCodec = Schema.Struct({ headers: Schema.Struct({
162
- control: Schema.Literals([
163
- "snapshot-start",
164
- "snapshot-end",
165
- "reset"
166
- ]),
167
- offset: Schema.optionalKey(Schema.String)
168
- }) });
169
- const rawChange = changeCodec(Schema.String, Schema.Unknown);
170
- /** Decode the State envelope while keeping row values in their wire form. */
171
- const decodeStateWire = Schema.decodeEffect(Schema.fromJsonString(Schema.Union([controlCodec, rawChange])));
172
- // src/toolkit/read-message.ts
173
- const parseJson = Schema.decodeOption(Schema.fromJsonString(Schema.Unknown));
174
- const decodeChange = Schema.decodeUnknownOption(rawChange);
175
- const isUndeclaredChange = (address, json) => {
176
- if (!("collections" in address) || !Predicate.isObject(address.collections)) return false;
177
- if ("strict" in address && address.strict === true) return false;
178
- const parsed = parseJson(json);
179
- if (Option.isNone(parsed)) return false;
180
- const value = parsed.value;
181
- if (Predicate.hasProperty(value, "headers") && Predicate.hasProperty(value.headers, "control")) return false;
182
- const change = decodeChange(value);
183
- return Option.isSome(change) && !Record.has(address.collections, change.value.type);
184
- };
185
- /** A decoded message, or None for a change outside a non-strict State address's collections. */
186
- const decodeMessage = (address, message, index) => {
187
- const wire = Predicate.isTagged(address, "Json") ? new TextDecoder().decode(message.data) : message.data;
188
- return Schema.decodeEffect(address.codec)(wire).pipe(Effect.asSome, Effect.catch((cause) => Predicate.isString(wire) && isUndeclaredChange(address, wire) ? Effect.succeedNone : Effect.fail(new DecodeFault({
189
- message: `Cannot decode ${address.id} at read message ${index}`,
190
- cause
191
- }))));
192
- };
193
- export { StorageFault as _, compileSegments as a, isRetryableFault as b, CurrentOwner as c, checkWriteOwner as d, isForeignOwner as f, EncodeFault as g, DecodeFault as h, decodeStateWire as i, attachOwner as l, storageOf as m, changeCodec as n, isCanonicalIdSegment as o, ownerOf as p, controlCodec as r, templatesOverlap as s, decodeMessage as t, attachStorage as u, StreamsFault as v, isStreamsFault as x, TransportFault as y };