@streamsy/core 0.0.1
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/dist/index.d.ts +421 -0
- package/dist/index.js +1831 -0
- package/package.json +41 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
//#region src/types/storage.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Simplified storage boundary for Streamsy.
|
|
4
|
+
*
|
|
5
|
+
* Adapters persist facts and may provide runtime capabilities (locks,
|
|
6
|
+
* notification, expiration scheduling). Protocol/lifecycle policy lives in core.
|
|
7
|
+
*/
|
|
8
|
+
type StreamId = string;
|
|
9
|
+
type Offset = string;
|
|
10
|
+
interface StoredMessage {
|
|
11
|
+
data: Uint8Array;
|
|
12
|
+
offset: Offset;
|
|
13
|
+
timestamp: number;
|
|
14
|
+
}
|
|
15
|
+
interface StreamConfig {
|
|
16
|
+
contentType: string;
|
|
17
|
+
ttlSeconds?: number;
|
|
18
|
+
expiresAt?: string;
|
|
19
|
+
createdAt: number;
|
|
20
|
+
}
|
|
21
|
+
interface StreamLifecycleState {
|
|
22
|
+
lastSeq?: string;
|
|
23
|
+
closed?: boolean;
|
|
24
|
+
closedAt?: number;
|
|
25
|
+
forkedFrom?: StreamId;
|
|
26
|
+
forkOffset?: Offset;
|
|
27
|
+
childRefCount: number;
|
|
28
|
+
softDeleted?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Effective expiration deadline in epoch ms. Core updates this on create
|
|
31
|
+
* and on TTL touches; lazy `expireIfNeeded()` reads it. Absent when the
|
|
32
|
+
* stream has neither `ttlSeconds` nor `expiresAt`.
|
|
33
|
+
*/
|
|
34
|
+
expiresAtMs?: number;
|
|
35
|
+
}
|
|
36
|
+
interface StreamRecord {
|
|
37
|
+
id: StreamId;
|
|
38
|
+
config: StreamConfig;
|
|
39
|
+
lifecycle: StreamLifecycleState;
|
|
40
|
+
currentOffset: Offset;
|
|
41
|
+
counter: number;
|
|
42
|
+
}
|
|
43
|
+
interface StreamRecordPatch {
|
|
44
|
+
config?: Partial<StreamConfig>;
|
|
45
|
+
lifecycle?: Partial<StreamLifecycleState>;
|
|
46
|
+
currentOffset?: Offset;
|
|
47
|
+
counter?: number;
|
|
48
|
+
}
|
|
49
|
+
type CreateStreamRecordResult = {
|
|
50
|
+
status: "created";
|
|
51
|
+
} | {
|
|
52
|
+
status: "exists";
|
|
53
|
+
record: StreamRecord;
|
|
54
|
+
};
|
|
55
|
+
interface ProducerState {
|
|
56
|
+
epoch: number;
|
|
57
|
+
lastSeq: number;
|
|
58
|
+
}
|
|
59
|
+
interface ListMessagesOptions {
|
|
60
|
+
after?: Offset;
|
|
61
|
+
until?: Offset;
|
|
62
|
+
limit?: number;
|
|
63
|
+
}
|
|
64
|
+
type StreamEventType = "message" | "closed" | "deleted" | "soft-deleted";
|
|
65
|
+
interface WaitForEventOptions {
|
|
66
|
+
timeoutMs: number;
|
|
67
|
+
signal?: AbortSignal;
|
|
68
|
+
}
|
|
69
|
+
type WaitForEventResult = {
|
|
70
|
+
status: "notified";
|
|
71
|
+
type?: StreamEventType;
|
|
72
|
+
} | {
|
|
73
|
+
status: "timeout";
|
|
74
|
+
} | {
|
|
75
|
+
status: "aborted";
|
|
76
|
+
};
|
|
77
|
+
interface Clock {
|
|
78
|
+
now(): number;
|
|
79
|
+
date(value?: number | string): Date;
|
|
80
|
+
} //#endregion
|
|
81
|
+
//#region src/types/factory.d.ts
|
|
82
|
+
/** Bound record store: every method targets the single stream this store represents. */
|
|
83
|
+
interface StreamRecordStore {
|
|
84
|
+
getRecord(): Promise<StreamRecord | null>;
|
|
85
|
+
createRecord(record: StreamRecord): Promise<CreateStreamRecordResult>;
|
|
86
|
+
updateRecord(patch: StreamRecordPatch): Promise<StreamRecord>;
|
|
87
|
+
deleteRecord(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
/** Bound message store for a single stream. */
|
|
90
|
+
interface StreamMessageStore {
|
|
91
|
+
appendMessages(messages: StoredMessage[]): Promise<void>;
|
|
92
|
+
listMessages(options?: ListMessagesOptions): Promise<StoredMessage[]>;
|
|
93
|
+
deleteMessages(): Promise<void>;
|
|
94
|
+
}
|
|
95
|
+
/** Bound producer-state store for a single stream. */
|
|
96
|
+
interface StreamProducerStore {
|
|
97
|
+
getProducerState(producerId: string): Promise<ProducerState | undefined>;
|
|
98
|
+
setProducerState(producerId: string, state: ProducerState): Promise<void>;
|
|
99
|
+
deleteProducerStates(): Promise<void>;
|
|
100
|
+
}
|
|
101
|
+
/** Bound parent/child reference tracker for a single stream. */
|
|
102
|
+
interface StreamReferenceTracker {
|
|
103
|
+
incrementChildRefCount(): Promise<number>;
|
|
104
|
+
decrementChildRefCount(): Promise<number>;
|
|
105
|
+
}
|
|
106
|
+
/** Per-stream mutation coordinator (lock-like). */
|
|
107
|
+
interface StreamMutationCoordinator {
|
|
108
|
+
withMutationLock<T>(fn: () => Promise<T>): Promise<T>;
|
|
109
|
+
}
|
|
110
|
+
/** Per-stream live-read notification hub. */
|
|
111
|
+
interface StreamEventHub {
|
|
112
|
+
waitForEvent(options: WaitForEventOptions): Promise<WaitForEventResult>;
|
|
113
|
+
notify(type: StreamEventType): Promise<void> | void;
|
|
114
|
+
}
|
|
115
|
+
/** Per-stream active expiry scheduler. */
|
|
116
|
+
interface StreamExpiryScheduler {
|
|
117
|
+
scheduleExpiry(at: number, callback?: () => Promise<void>): Promise<void> | void;
|
|
118
|
+
cancelExpiry(): Promise<void> | void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Protocol-facing stream. Represents one stream identified by `id` and exposes
|
|
122
|
+
* all protocol storage behavior as direct methods. Adapter-private store
|
|
123
|
+
* composition must stay behind this object.
|
|
124
|
+
*/
|
|
125
|
+
interface Stream extends StreamRecordStore, StreamMessageStore, StreamProducerStore, StreamReferenceTracker, StreamMutationCoordinator, StreamEventHub, StreamExpiryScheduler {
|
|
126
|
+
readonly id: StreamId;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Storage factory. Maps a public stream id to a storage-bound `Stream`.
|
|
130
|
+
* Lookup, routing, and dependency composition are factory-owned concerns
|
|
131
|
+
* and are not exposed on the returned `Stream`.
|
|
132
|
+
*/
|
|
133
|
+
interface StreamFactory {
|
|
134
|
+
getStream(streamId: StreamId): Promise<Stream>;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Structured "feature not supported" protocol result. Protocol methods map
|
|
138
|
+
* typed storage unsupported behavior to this public result shape.
|
|
139
|
+
*/
|
|
140
|
+
interface NotSupportedResult {
|
|
141
|
+
status: "not-supported";
|
|
142
|
+
feature: string;
|
|
143
|
+
message?: string;
|
|
144
|
+
}
|
|
145
|
+
/** Internal/storage-level unsupported-feature error. */
|
|
146
|
+
declare class NotSupportedError extends Error {
|
|
147
|
+
readonly feature: string;
|
|
148
|
+
constructor(feature: string, message?: string);
|
|
149
|
+
}
|
|
150
|
+
/** Convenience constructor for a {@link NotSupportedResult}. */
|
|
151
|
+
declare function notSupported(feature: string, message?: string): NotSupportedResult;
|
|
152
|
+
/** Convenience constructor for a storage-level unsupported-feature error. */
|
|
153
|
+
declare function unsupported(feature: string, message?: string): NotSupportedError;
|
|
154
|
+
/** Type guard for storage-level unsupported-feature errors. */
|
|
155
|
+
declare function isNotSupportedError(value: unknown): value is NotSupportedError;
|
|
156
|
+
/** Convert storage-level unsupported-feature errors to public protocol results. */
|
|
157
|
+
declare function notSupportedFromError(error: NotSupportedError): NotSupportedResult;
|
|
158
|
+
/** Type guard for protocol results that may be a {@link NotSupportedResult}. */
|
|
159
|
+
declare function isNotSupported(value: unknown): value is NotSupportedResult;
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/types/protocol.d.ts
|
|
163
|
+
interface CreateOptions {
|
|
164
|
+
contentType?: string;
|
|
165
|
+
ttlSeconds?: number;
|
|
166
|
+
expiresAt?: string;
|
|
167
|
+
initialData?: Uint8Array;
|
|
168
|
+
closed?: boolean;
|
|
169
|
+
forkedFrom?: string;
|
|
170
|
+
forkOffset?: string;
|
|
171
|
+
}
|
|
172
|
+
interface ProducerOptions {
|
|
173
|
+
producerId: string;
|
|
174
|
+
producerEpoch: number;
|
|
175
|
+
producerSeq: number;
|
|
176
|
+
}
|
|
177
|
+
interface AppendOptions {
|
|
178
|
+
data: Uint8Array;
|
|
179
|
+
contentType: string;
|
|
180
|
+
seq?: string;
|
|
181
|
+
producer?: ProducerOptions;
|
|
182
|
+
close?: boolean;
|
|
183
|
+
}
|
|
184
|
+
interface ReadOptions {
|
|
185
|
+
offset?: string;
|
|
186
|
+
}
|
|
187
|
+
interface ReadLiveOptions {
|
|
188
|
+
offset: string;
|
|
189
|
+
mode: "long-poll" | "sse";
|
|
190
|
+
cursor?: string;
|
|
191
|
+
signal?: AbortSignal;
|
|
192
|
+
}
|
|
193
|
+
type CreateConflictReason = "config-mismatch" | "soft-deleted" | "fork-content-type" | "fork-source-soft-deleted";
|
|
194
|
+
type CreateFailureResult = {
|
|
195
|
+
status: "conflict";
|
|
196
|
+
nextOffset: string;
|
|
197
|
+
contentType: string;
|
|
198
|
+
conflictReason?: CreateConflictReason;
|
|
199
|
+
errorMessage?: string;
|
|
200
|
+
} | {
|
|
201
|
+
status: "not-found" | "bad-request";
|
|
202
|
+
nextOffset: string;
|
|
203
|
+
contentType: string;
|
|
204
|
+
errorMessage?: string;
|
|
205
|
+
} | NotSupportedResult;
|
|
206
|
+
/**
|
|
207
|
+
* Create/fork result produced by the protocol services, before the bound
|
|
208
|
+
* protocol stream is attached. The protocol factory grafts the resolved
|
|
209
|
+
* `stream` onto success outcomes to produce the public {@link CreateResult}.
|
|
210
|
+
*
|
|
211
|
+
* `created` and `exists` are kept as separate members (rather than a combined
|
|
212
|
+
* `"created" | "exists"` discriminant) so callers can narrow either status and
|
|
213
|
+
* still eliminate the other union members.
|
|
214
|
+
*/
|
|
215
|
+
|
|
216
|
+
type CreateResult = {
|
|
217
|
+
status: "created";
|
|
218
|
+
stream: ProtocolStream;
|
|
219
|
+
nextOffset: string;
|
|
220
|
+
contentType: string;
|
|
221
|
+
closed?: boolean;
|
|
222
|
+
} | {
|
|
223
|
+
status: "exists";
|
|
224
|
+
stream: ProtocolStream;
|
|
225
|
+
nextOffset: string;
|
|
226
|
+
contentType: string;
|
|
227
|
+
closed?: boolean;
|
|
228
|
+
} | CreateFailureResult;
|
|
229
|
+
type AppendConflictReason = "content-type" | "sequence" | "closed";
|
|
230
|
+
type AppendResult = {
|
|
231
|
+
status: "appended";
|
|
232
|
+
nextOffset: string;
|
|
233
|
+
producerEpoch?: number;
|
|
234
|
+
producerSeq?: number;
|
|
235
|
+
closed?: boolean;
|
|
236
|
+
} | {
|
|
237
|
+
status: "duplicate";
|
|
238
|
+
nextOffset: string;
|
|
239
|
+
producerEpoch: number;
|
|
240
|
+
producerSeq: number;
|
|
241
|
+
closed?: boolean;
|
|
242
|
+
} | {
|
|
243
|
+
status: "not-found";
|
|
244
|
+
} | {
|
|
245
|
+
status: "gone";
|
|
246
|
+
} | {
|
|
247
|
+
status: "conflict";
|
|
248
|
+
conflictReason: "closed";
|
|
249
|
+
nextOffset: string;
|
|
250
|
+
closed: true;
|
|
251
|
+
} | {
|
|
252
|
+
status: "conflict";
|
|
253
|
+
conflictReason: "content-type" | "sequence";
|
|
254
|
+
} | {
|
|
255
|
+
status: "stale-epoch";
|
|
256
|
+
currentEpoch: number;
|
|
257
|
+
} | {
|
|
258
|
+
status: "producer-gap";
|
|
259
|
+
expectedSeq: number;
|
|
260
|
+
receivedSeq: number;
|
|
261
|
+
} | {
|
|
262
|
+
status: "invalid-epoch-seq";
|
|
263
|
+
} | NotSupportedResult;
|
|
264
|
+
type ReadResult = {
|
|
265
|
+
status: "ok";
|
|
266
|
+
messages: StoredMessage[];
|
|
267
|
+
nextOffset: string;
|
|
268
|
+
upToDate: boolean;
|
|
269
|
+
closed?: boolean;
|
|
270
|
+
} | {
|
|
271
|
+
status: "not-found";
|
|
272
|
+
} | {
|
|
273
|
+
status: "gone";
|
|
274
|
+
};
|
|
275
|
+
type ReadLiveResult = {
|
|
276
|
+
status: "ok" | "timeout" | "not-found" | "gone";
|
|
277
|
+
messages: StoredMessage[];
|
|
278
|
+
nextOffset: string;
|
|
279
|
+
upToDate: boolean;
|
|
280
|
+
cursor: string;
|
|
281
|
+
closed?: boolean;
|
|
282
|
+
} | NotSupportedResult;
|
|
283
|
+
type MetadataResult = {
|
|
284
|
+
status: "ok";
|
|
285
|
+
contentType: string;
|
|
286
|
+
nextOffset: string;
|
|
287
|
+
ttlSeconds?: number;
|
|
288
|
+
expiresAt?: string;
|
|
289
|
+
closed?: boolean;
|
|
290
|
+
} | {
|
|
291
|
+
status: "not-found";
|
|
292
|
+
} | {
|
|
293
|
+
status: "gone";
|
|
294
|
+
};
|
|
295
|
+
type DeleteResult = {
|
|
296
|
+
status: "ok";
|
|
297
|
+
} | {
|
|
298
|
+
status: "not-found";
|
|
299
|
+
} | {
|
|
300
|
+
status: "gone";
|
|
301
|
+
};
|
|
302
|
+
interface ProtocolStream {
|
|
303
|
+
readonly id: StreamId;
|
|
304
|
+
append(options: AppendOptions): Promise<AppendResult>;
|
|
305
|
+
read(options: ReadOptions): Promise<ReadResult>;
|
|
306
|
+
readLive(options: ReadLiveOptions): Promise<ReadLiveResult>;
|
|
307
|
+
metadata(): Promise<MetadataResult>;
|
|
308
|
+
delete(): Promise<DeleteResult>;
|
|
309
|
+
}
|
|
310
|
+
type ProtocolGetResult = {
|
|
311
|
+
status: "ok";
|
|
312
|
+
stream: ProtocolStream;
|
|
313
|
+
} | {
|
|
314
|
+
status: "not-found";
|
|
315
|
+
} | {
|
|
316
|
+
status: "gone";
|
|
317
|
+
} | NotSupportedResult;
|
|
318
|
+
interface StreamProtocolFactory {
|
|
319
|
+
create(streamId: string, options: CreateOptions): Promise<CreateResult>;
|
|
320
|
+
get(streamId: string): Promise<ProtocolGetResult>;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
//#endregion
|
|
324
|
+
//#region src/protocol/helpers/offset-generator.d.ts
|
|
325
|
+
declare const ZERO_OFFSET: Offset;
|
|
326
|
+
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region src/protocol.d.ts
|
|
329
|
+
interface StreamProtocolOptions {
|
|
330
|
+
clock?: Clock;
|
|
331
|
+
longPollTimeoutMs?: number;
|
|
332
|
+
}
|
|
333
|
+
interface StreamProtocolDeps extends StreamProtocolOptions {
|
|
334
|
+
storage: {
|
|
335
|
+
factory: StreamFactory;
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
declare function createStreamProtocol(deps: StreamProtocolDeps): StreamProtocolFactory;
|
|
339
|
+
declare class StreamProtocol implements StreamProtocolFactory {
|
|
340
|
+
private deps;
|
|
341
|
+
private clock;
|
|
342
|
+
private longPollTimeoutMs;
|
|
343
|
+
private expiryPolicy;
|
|
344
|
+
private gcService;
|
|
345
|
+
private recordFactory;
|
|
346
|
+
constructor(deps: StreamProtocolDeps);
|
|
347
|
+
create(streamId: string, options: CreateOptions): Promise<CreateResult>;
|
|
348
|
+
get(streamId: string): Promise<ProtocolGetResult>;
|
|
349
|
+
/** Called by adapter schedulers/alarms; core decides soft-delete vs purge. */
|
|
350
|
+
handleScheduledExpiry(streamId: string): Promise<void>;
|
|
351
|
+
private boundProtocolStream;
|
|
352
|
+
private storageStream;
|
|
353
|
+
private withStreamMutationLock;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
//#endregion
|
|
357
|
+
//#region src/http/types.d.ts
|
|
358
|
+
interface HttpHandlerOptions {
|
|
359
|
+
protocol: StreamProtocolFactory;
|
|
360
|
+
pathPrefix?: string;
|
|
361
|
+
maxMessageSize?: number;
|
|
362
|
+
}
|
|
363
|
+
interface ReadOnlyHttpHandlerOptions {
|
|
364
|
+
protocol: StreamProtocolFactory;
|
|
365
|
+
pathPrefix?: string;
|
|
366
|
+
}
|
|
367
|
+
interface HttpHandlerInterface {
|
|
368
|
+
fetch(request: Request): Promise<Response>;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/http.d.ts
|
|
373
|
+
declare function createHttpHandler(options: HttpHandlerOptions): HttpHandlerInterface;
|
|
374
|
+
declare class HttpHandler implements HttpHandlerInterface {
|
|
375
|
+
private dispatch;
|
|
376
|
+
constructor(options: HttpHandlerOptions);
|
|
377
|
+
fetch(request: Request): Promise<Response>;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
//#endregion
|
|
381
|
+
//#region src/read-only-http.d.ts
|
|
382
|
+
declare function createReadOnlyHttpHandler(options: ReadOnlyHttpHandlerOptions): HttpHandlerInterface;
|
|
383
|
+
declare class ReadOnlyHttpHandler implements HttpHandlerInterface {
|
|
384
|
+
private dispatch;
|
|
385
|
+
constructor(options: ReadOnlyHttpHandlerOptions);
|
|
386
|
+
fetch(request: Request): Promise<Response>;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/http/responses.d.ts
|
|
391
|
+
declare class HttpResponseFactory {
|
|
392
|
+
secure(response: Response): Response;
|
|
393
|
+
text(message: string | null, status: number, headers?: HeadersInit): Response;
|
|
394
|
+
empty(status: number, headers?: HeadersInit): Response;
|
|
395
|
+
badRequest(message: string): Response;
|
|
396
|
+
notFound(message?: string): Response;
|
|
397
|
+
gone(message?: string): Response;
|
|
398
|
+
conflict(message: string | null, headers?: HeadersInit): Response;
|
|
399
|
+
payloadTooLarge(): Response;
|
|
400
|
+
invalidJson(): Response;
|
|
401
|
+
methodNotAllowed(): Response;
|
|
402
|
+
internalError(): Response;
|
|
403
|
+
/**
|
|
404
|
+
* Wrap a response with the standard security headers used across every
|
|
405
|
+
* route. Existing values for the same header names are preserved.
|
|
406
|
+
*/
|
|
407
|
+
withSecurityHeaders(response: Response): Response;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
//#endregion
|
|
411
|
+
//#region src/http/not-supported.d.ts
|
|
412
|
+
declare function notSupportedResponse(result: NotSupportedResult, responses: HttpResponseFactory): Response;
|
|
413
|
+
/**
|
|
414
|
+
* Convenience for use in `case`-style handlers that may want to short
|
|
415
|
+
* circuit on a `not-supported` result before pattern-matching the rest of a
|
|
416
|
+
* union.
|
|
417
|
+
*/
|
|
418
|
+
declare function maybeNotSupportedResponse<T>(result: T | NotSupportedResult, responses: HttpResponseFactory): Response | null;
|
|
419
|
+
|
|
420
|
+
//#endregion
|
|
421
|
+
export { AppendConflictReason, AppendOptions, AppendResult, Clock, CreateConflictReason, CreateOptions, CreateResult, CreateStreamRecordResult, DeleteResult, HttpHandler, HttpHandlerInterface, HttpHandlerOptions, ListMessagesOptions, MetadataResult, NotSupportedError, NotSupportedResult, Offset, ProducerOptions, ProducerState, ProtocolGetResult, ProtocolStream, ReadLiveOptions, ReadLiveResult, ReadOnlyHttpHandler, ReadOnlyHttpHandlerOptions, ReadOptions, ReadResult, StoredMessage, Stream, StreamConfig, StreamEventHub, StreamEventType, StreamExpiryScheduler, StreamFactory, StreamId, StreamLifecycleState, StreamMessageStore, StreamMutationCoordinator, StreamProducerStore, StreamProtocol, StreamProtocolDeps, StreamProtocolFactory, StreamProtocolOptions, StreamRecord, StreamRecordPatch, StreamRecordStore, StreamReferenceTracker, WaitForEventOptions, WaitForEventResult, ZERO_OFFSET, createHttpHandler, createReadOnlyHttpHandler, createStreamProtocol, isNotSupported, isNotSupportedError, maybeNotSupportedResponse, notSupported, notSupportedFromError, notSupportedResponse, unsupported };
|