eventsourcingdb 1.8.1 → 1.8.3
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/Bound.d.ts +6 -0
- package/dist/Bound.js +1 -0
- package/dist/Client.d.ts +21 -0
- package/dist/Client.js +484 -0
- package/dist/CloudEvent.d.ts +16 -0
- package/dist/CloudEvent.js +1 -0
- package/dist/Container.d.ts +20 -0
- package/dist/Container.js +108 -0
- package/dist/Event.d.ts +36 -0
- package/dist/Event.js +63 -0
- package/dist/EventCandidate.d.ts +9 -0
- package/dist/EventCandidate.js +1 -0
- package/dist/EventType.d.ts +6 -0
- package/dist/EventType.js +1 -0
- package/dist/ObserveEventsOptions.d.ts +13 -0
- package/dist/ObserveEventsOptions.js +1 -0
- package/dist/Precondition.d.ts +27 -0
- package/dist/Precondition.js +1 -0
- package/dist/ReadEventsOptions.d.ts +16 -0
- package/dist/ReadEventsOptions.js +1 -0
- package/dist/convertCloudEventToEvent.d.ts +4 -0
- package/dist/convertCloudEventToEvent.js +18 -0
- package/dist/getImageVersionFromDockerfile.d.ts +2 -0
- package/dist/getImageVersionFromDockerfile.js +14 -0
- package/dist/index.d.ts +14 -154
- package/dist/index.js +7 -2
- package/dist/isCloudEvent.d.ts +3 -0
- package/dist/isCloudEvent.js +21 -0
- package/dist/isEventQlQueryTrue.d.ts +3 -0
- package/dist/isEventQlQueryTrue.js +5 -0
- package/dist/isSubjectOnEventId.d.ts +3 -0
- package/dist/isSubjectOnEventId.js +5 -0
- package/dist/isSubjectPopulated.d.ts +3 -0
- package/dist/isSubjectPopulated.js +5 -0
- package/dist/isSubjectPristine.d.ts +3 -0
- package/dist/isSubjectPristine.js +5 -0
- package/dist/isValidServerHeader.d.ts +2 -0
- package/dist/isValidServerHeader.js +11 -0
- package/dist/ndjson/readNdJsonStream.d.ts +2 -0
- package/dist/ndjson/readNdJsonStream.js +43 -0
- package/dist/stream/StreamCloudEvent.d.ts +19 -0
- package/dist/stream/StreamCloudEvent.js +1 -0
- package/dist/stream/StreamError.d.ts +7 -0
- package/dist/stream/StreamError.js +1 -0
- package/dist/stream/StreamEventType.d.ts +9 -0
- package/dist/stream/StreamEventType.js +1 -0
- package/dist/stream/StreamHeartbeat.d.ts +4 -0
- package/dist/stream/StreamHeartbeat.js +1 -0
- package/dist/stream/StreamRow.d.ts +5 -0
- package/dist/stream/StreamRow.js +1 -0
- package/dist/stream/StreamSubject.d.ts +7 -0
- package/dist/stream/StreamSubject.js +1 -0
- package/dist/stream/isStreamCloudEvent.d.ts +3 -0
- package/dist/stream/isStreamCloudEvent.js +24 -0
- package/dist/stream/isStreamError.d.ts +3 -0
- package/dist/stream/isStreamError.js +9 -0
- package/dist/stream/isStreamEventType.d.ts +3 -0
- package/dist/stream/isStreamEventType.js +11 -0
- package/dist/stream/isStreamHeartbeat.d.ts +3 -0
- package/dist/stream/isStreamHeartbeat.js +5 -0
- package/dist/stream/isStreamRow.d.ts +3 -0
- package/dist/stream/isStreamRow.js +6 -0
- package/dist/stream/isStreamSubject.d.ts +3 -0
- package/dist/stream/isStreamSubject.js +9 -0
- package/dist/types/hasShapeOf.d.ts +6 -0
- package/dist/types/hasShapeOf.js +25 -0
- package/dist/types/isBoolean.d.ts +2 -0
- package/dist/types/isBoolean.js +7 -0
- package/dist/types/isRecord.d.ts +2 -0
- package/dist/types/isRecord.js +13 -0
- package/dist/types/isString.d.ts +2 -0
- package/dist/types/isString.js +7 -0
- package/dist/types/isStringOrNull.d.ts +2 -0
- package/dist/types/isStringOrNull.js +10 -0
- package/dist/types/isStringOrUndefined.d.ts +2 -0
- package/dist/types/isStringOrUndefined.js +10 -0
- package/package.json +14 -11
- package/.editorconfig +0 -22
- package/dist/index.d.mts +0 -154
- package/dist/index.mjs +0 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { hasShapeOf } from '../types/hasShapeOf.js';
|
|
2
|
+
import { isRecord } from '../types/isRecord.js';
|
|
3
|
+
import { isString } from '../types/isString.js';
|
|
4
|
+
import { isStringOrNull } from '../types/isStringOrNull.js';
|
|
5
|
+
import { isStringOrUndefined } from '../types/isStringOrUndefined.js';
|
|
6
|
+
const isStreamCloudEvent = (line) => hasShapeOf(line, {
|
|
7
|
+
type: (value) => value === 'event',
|
|
8
|
+
payload: {
|
|
9
|
+
specversion: isString,
|
|
10
|
+
id: isString,
|
|
11
|
+
time: isString,
|
|
12
|
+
source: isString,
|
|
13
|
+
subject: isString,
|
|
14
|
+
type: isString,
|
|
15
|
+
datacontenttype: isString,
|
|
16
|
+
data: isRecord,
|
|
17
|
+
hash: isString,
|
|
18
|
+
predecessorhash: isString,
|
|
19
|
+
traceparent: isStringOrUndefined,
|
|
20
|
+
tracestate: isStringOrUndefined,
|
|
21
|
+
signature: isStringOrNull,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
export { isStreamCloudEvent };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { hasShapeOf } from '../types/hasShapeOf.js';
|
|
2
|
+
import { isString } from '../types/isString.js';
|
|
3
|
+
const isStreamError = (line) => hasShapeOf(line, {
|
|
4
|
+
type: (value) => value === 'error',
|
|
5
|
+
payload: {
|
|
6
|
+
error: isString,
|
|
7
|
+
},
|
|
8
|
+
});
|
|
9
|
+
export { isStreamError };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { hasShapeOf } from '../types/hasShapeOf.js';
|
|
2
|
+
import { isBoolean } from '../types/isBoolean.js';
|
|
3
|
+
import { isString } from '../types/isString.js';
|
|
4
|
+
const isStreamEventType = (line) => hasShapeOf(line, {
|
|
5
|
+
type: (value) => value === 'eventType',
|
|
6
|
+
payload: {
|
|
7
|
+
eventType: isString,
|
|
8
|
+
isPhantom: isBoolean,
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
export { isStreamEventType };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { hasShapeOf } from '../types/hasShapeOf.js';
|
|
2
|
+
import { isString } from '../types/isString.js';
|
|
3
|
+
const isStreamSubject = (line) => hasShapeOf(line, {
|
|
4
|
+
type: (value) => value === 'subject',
|
|
5
|
+
payload: {
|
|
6
|
+
subject: isString,
|
|
7
|
+
},
|
|
8
|
+
});
|
|
9
|
+
export { isStreamSubject };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isRecord } from './isRecord.js';
|
|
2
|
+
const hasShapeOf = (value, shape) => {
|
|
3
|
+
if (!isRecord(value)) {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
for (const key of Object.keys(shape)) {
|
|
7
|
+
const spec = shape[key];
|
|
8
|
+
const val = value[key];
|
|
9
|
+
if (typeof spec === 'function') {
|
|
10
|
+
const guard = spec;
|
|
11
|
+
if (!guard(val)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!isRecord(val)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
if (!hasShapeOf(val, spec)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
};
|
|
25
|
+
export { hasShapeOf };
|
package/package.json
CHANGED
|
@@ -1,32 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eventsourcingdb",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.3",
|
|
4
4
|
"description": "The official JavaScript client SDK for EventSourcingDB.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "the native web GmbH",
|
|
7
7
|
"email": "hello@thenativeweb.io"
|
|
8
8
|
},
|
|
9
|
+
"type": "module",
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
13
14
|
}
|
|
14
15
|
},
|
|
15
16
|
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
16
20
|
"dependencies": {
|
|
17
|
-
"testcontainers": "
|
|
21
|
+
"testcontainers": "12.0.4"
|
|
18
22
|
},
|
|
19
23
|
"devDependencies": {
|
|
20
|
-
"@biomejs/biome": "2.
|
|
21
|
-
"@types/node": "
|
|
22
|
-
"get-port": "7.
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"typescript": "5.9.3"
|
|
24
|
+
"@biomejs/biome": "2.5.5",
|
|
25
|
+
"@types/node": "26.1.1",
|
|
26
|
+
"get-port": "7.2.0",
|
|
27
|
+
"tsx": "4.23.1",
|
|
28
|
+
"typescript": "7.0.2"
|
|
26
29
|
},
|
|
27
30
|
"scripts": {
|
|
28
31
|
"analyze": "npx tsc --noEmit && npx biome check --error-on-warnings .",
|
|
29
|
-
"build": "npx tsc
|
|
32
|
+
"build": "npx tsc -p tsconfig.build.json",
|
|
30
33
|
"format": "npx biome check --write .",
|
|
31
34
|
"qa": "npm run analyze && npm run test",
|
|
32
35
|
"test": "node --test --import tsx \"./src/**/*.test.ts\""
|
package/.editorconfig
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# top-most EditorConfig file
|
|
2
|
-
root = true
|
|
3
|
-
|
|
4
|
-
[*]
|
|
5
|
-
charset = utf-8
|
|
6
|
-
end_of_line = lf
|
|
7
|
-
indent_size = 2
|
|
8
|
-
indent_style = tab
|
|
9
|
-
insert_final_newline = true
|
|
10
|
-
trim_trailing_whitespace = true
|
|
11
|
-
|
|
12
|
-
[*.json]
|
|
13
|
-
indent_size = 2
|
|
14
|
-
indent_style = space
|
|
15
|
-
|
|
16
|
-
[*.md]
|
|
17
|
-
indent_size = 2
|
|
18
|
-
indent_style = space
|
|
19
|
-
|
|
20
|
-
[*.yml]
|
|
21
|
-
indent_size = 2
|
|
22
|
-
indent_style = space
|
package/dist/index.d.mts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import crypto from 'node:crypto';
|
|
2
|
-
|
|
3
|
-
declare class Event {
|
|
4
|
-
#private;
|
|
5
|
-
specversion: string;
|
|
6
|
-
id: string;
|
|
7
|
-
time: Date;
|
|
8
|
-
source: string;
|
|
9
|
-
subject: string;
|
|
10
|
-
type: string;
|
|
11
|
-
datacontenttype: string;
|
|
12
|
-
data: Record<string, unknown>;
|
|
13
|
-
hash: string;
|
|
14
|
-
predecessorhash: string;
|
|
15
|
-
traceparent?: string;
|
|
16
|
-
tracestate?: string;
|
|
17
|
-
signature: string | null;
|
|
18
|
-
constructor({ specversion, id, time, timeFromServer, source, subject, type, datacontenttype, data, hash, predecessorhash, traceparent, tracestate, signature, }: {
|
|
19
|
-
specversion: string;
|
|
20
|
-
id: string;
|
|
21
|
-
time: Date;
|
|
22
|
-
timeFromServer: string;
|
|
23
|
-
source: string;
|
|
24
|
-
subject: string;
|
|
25
|
-
type: string;
|
|
26
|
-
datacontenttype: string;
|
|
27
|
-
data: Record<string, unknown>;
|
|
28
|
-
hash: string;
|
|
29
|
-
predecessorhash: string;
|
|
30
|
-
traceparent?: string;
|
|
31
|
-
tracestate?: string;
|
|
32
|
-
signature: string | null;
|
|
33
|
-
});
|
|
34
|
-
verifyHash(): void;
|
|
35
|
-
verifySignature(verificationKey: crypto.KeyObject): void;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface EventCandidate {
|
|
39
|
-
source: string;
|
|
40
|
-
subject: string;
|
|
41
|
-
type: string;
|
|
42
|
-
data: Record<string, unknown>;
|
|
43
|
-
traceparent?: string;
|
|
44
|
-
tracestate?: string;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
interface EventType {
|
|
48
|
-
eventType: string;
|
|
49
|
-
isPhantom: boolean;
|
|
50
|
-
schema?: Record<string, unknown>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
type BoundType = 'inclusive' | 'exclusive';
|
|
54
|
-
interface Bound {
|
|
55
|
-
id: string;
|
|
56
|
-
type: BoundType;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
type ObserveIfEventIsMissing = 'read-everything' | 'wait-for-event';
|
|
60
|
-
interface ObserveFromLatestEvent {
|
|
61
|
-
subject: string;
|
|
62
|
-
type: string;
|
|
63
|
-
ifEventIsMissing: ObserveIfEventIsMissing;
|
|
64
|
-
}
|
|
65
|
-
interface ObserveEventsOptions {
|
|
66
|
-
recursive: boolean;
|
|
67
|
-
lowerBound?: Bound;
|
|
68
|
-
fromLatestEvent?: ObserveFromLatestEvent;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
interface IsSubjectPristinePrecondition {
|
|
72
|
-
subject: string;
|
|
73
|
-
}
|
|
74
|
-
interface IsSubjectPopulatedPrecondition {
|
|
75
|
-
subject: string;
|
|
76
|
-
}
|
|
77
|
-
interface IsSubjectOnEventIdPrecondition {
|
|
78
|
-
subject: string;
|
|
79
|
-
eventId: string;
|
|
80
|
-
}
|
|
81
|
-
interface IsEventQlQueryTruePrecondition {
|
|
82
|
-
query: string;
|
|
83
|
-
}
|
|
84
|
-
type Precondition = {
|
|
85
|
-
type: 'isSubjectPristine';
|
|
86
|
-
payload: IsSubjectPristinePrecondition;
|
|
87
|
-
} | {
|
|
88
|
-
type: 'isSubjectOnEventId';
|
|
89
|
-
payload: IsSubjectOnEventIdPrecondition;
|
|
90
|
-
} | {
|
|
91
|
-
type: 'isSubjectPopulated';
|
|
92
|
-
payload: IsSubjectPopulatedPrecondition;
|
|
93
|
-
} | {
|
|
94
|
-
type: 'isEventQlQueryTrue';
|
|
95
|
-
payload: IsEventQlQueryTruePrecondition;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
type Order = 'chronological' | 'antichronological';
|
|
99
|
-
type ReadIfEventIsMissing = 'read-nothing' | 'read-everything';
|
|
100
|
-
interface ReadFromLatestEvent {
|
|
101
|
-
subject: string;
|
|
102
|
-
type: string;
|
|
103
|
-
ifEventIsMissing: ReadIfEventIsMissing;
|
|
104
|
-
}
|
|
105
|
-
interface ReadEventsOptions {
|
|
106
|
-
recursive: boolean;
|
|
107
|
-
order?: Order;
|
|
108
|
-
lowerBound?: Bound;
|
|
109
|
-
upperBound?: Bound;
|
|
110
|
-
fromLatestEvent?: ReadFromLatestEvent;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
declare class Client {
|
|
114
|
-
#private;
|
|
115
|
-
constructor(url: URL, apiToken: string);
|
|
116
|
-
ping(): Promise<void>;
|
|
117
|
-
verifyApiToken(): Promise<void>;
|
|
118
|
-
writeEvents(events: EventCandidate[], preconditions?: Precondition[]): Promise<Event[]>;
|
|
119
|
-
readEvents(subject: string, options: ReadEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
|
|
120
|
-
runEventQlQuery(query: string, signal?: AbortSignal): AsyncGenerator<unknown, void, void>;
|
|
121
|
-
observeEvents(subject: string, options: ObserveEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
|
|
122
|
-
registerEventSchema(eventType: string, schema: Record<string, unknown>): Promise<void>;
|
|
123
|
-
readSubjects(baseSubject: string, signal?: AbortSignal): AsyncGenerator<string, void, void>;
|
|
124
|
-
readEventType(eventType: string): Promise<EventType>;
|
|
125
|
-
readEventTypes(signal?: AbortSignal): AsyncGenerator<EventType, void, void>;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
declare class Container {
|
|
129
|
-
#private;
|
|
130
|
-
withImageTag(tag: string): this;
|
|
131
|
-
withApiToken(token: string): this;
|
|
132
|
-
withSigningKey(): this;
|
|
133
|
-
withPort(port: number): this;
|
|
134
|
-
start(): Promise<void>;
|
|
135
|
-
getHost(): string;
|
|
136
|
-
getMappedPort(): number;
|
|
137
|
-
getBaseUrl(): URL;
|
|
138
|
-
getApiToken(): string;
|
|
139
|
-
getSigningKey(): crypto.KeyObject;
|
|
140
|
-
getVerificationKey(): crypto.KeyObject;
|
|
141
|
-
isRunning(): boolean;
|
|
142
|
-
stop(): Promise<void>;
|
|
143
|
-
getClient(): Client;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
declare const isEventQlQueryTrue: (query: string) => Precondition;
|
|
147
|
-
|
|
148
|
-
declare const isSubjectOnEventId: (subject: string, eventId: string) => Precondition;
|
|
149
|
-
|
|
150
|
-
declare const isSubjectPopulated: (subject: string) => Precondition;
|
|
151
|
-
|
|
152
|
-
declare const isSubjectPristine: (subject: string) => Precondition;
|
|
153
|
-
|
|
154
|
-
export { Client, Container, Event, type EventCandidate, type EventType, type ObserveEventsOptions, type Precondition, type ReadEventsOptions, isEventQlQueryTrue, isSubjectOnEventId, isSubjectPopulated, isSubjectPristine };
|
package/dist/index.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import g from"crypto";var S=class{specversion;id;time;#n;source;subject;type;datacontenttype;data;hash;predecessorhash;traceparent;tracestate;signature;constructor({specversion:e,id:r,time:o,timeFromServer:s,source:c,subject:d,type:u,datacontenttype:a,data:n,hash:i,predecessorhash:h,traceparent:A,tracestate:H,signature:F}){this.specversion=e,this.id=r,this.time=o,this.#n=s,this.source=c,this.subject=d,this.type=u,this.datacontenttype=a,this.data=n,this.hash=i,this.predecessorhash=h,this.traceparent=A,this.tracestate=H,this.signature=F}verifyHash(){let e=`${this.specversion}|${this.id}|${this.predecessorhash}|${this.#n}|${this.source}|${this.subject}|${this.type}|${this.datacontenttype}`,r=g.createHash("sha256").update(e).digest("hex"),o=g.createHash("sha256").update(JSON.stringify(this.data)).digest("hex");if(g.createHash("sha256").update(`${r}${o}`).digest("hex")!==this.hash)throw new Error("Failed to verify hash.")}verifySignature(e){if(this.signature===null)throw new Error("Signature must not be null.");this.verifyHash();let r="esdb:signature:v1:";if(!this.signature.startsWith(r))throw new Error(`Signature must start with '${r}'`);let o=this.signature.slice(r.length),s=Buffer.from(o,"hex"),c=Buffer.from(this.hash,"utf8");if(!g.verify(null,c,e,s))throw new Error("Signature verification failed.")}};var E=t=>new S({specversion:t.specversion,id:t.id,time:new Date(t.time),timeFromServer:t.time,source:t.source,subject:t.subject,type:t.type,datacontenttype:t.datacontenttype,data:t.data,hash:t.hash,predecessorhash:t.predecessorhash,traceparent:t.traceparent,tracestate:t.tracestate,signature:t.signature});var y=t=>!(t===null||typeof t!="object"||Array.isArray(t));var f=(t,e)=>{if(!y(t))return!1;for(let r of Object.keys(e)){let o=e[r],s=t[r];if(typeof o=="function"){if(!o(s))return!1;continue}if(!y(s)||!f(s,o))return!1}return!0};var p=t=>typeof t=="string";var T=t=>t===null||typeof t=="string";var l=t=>t===void 0||typeof t=="string";var O=t=>f(t,{specversion:p,id:p,time:p,source:p,subject:p,type:p,datacontenttype:p,data:y,hash:p,predecessorhash:p,traceparent:l,tracestate:l,signature:T});var m=t=>{let e=t.headers.get("Server");return!(!e||!e.startsWith("EventSourcingDB/"))};var v=async function*(t,e){let r=t.getReader(),o=new TextDecoder("utf-8"),s="",c=()=>{r.cancel().catch(()=>{})};if(e.aborted){await r.cancel().catch(()=>{});return}e.addEventListener("abort",c);try{for(;!e.aborted;){let{done:d,value:u}=await r.read();if(d)break;s+=o.decode(u,{stream:!0});let a;for(;a=s.indexOf(`
|
|
2
|
-
`),a!==-1;){let n=s.slice(0,a).trim();s=s.slice(a+1),n&&(yield JSON.parse(n))}}}finally{e.removeEventListener("abort",c),await r.cancel().catch(()=>{})}};var P=t=>f(t,{type:e=>e==="event",payload:{specversion:p,id:p,time:p,source:p,subject:p,type:p,datacontenttype:p,data:y,hash:p,predecessorhash:p,traceparent:l,tracestate:l,signature:T}});var b=t=>f(t,{type:e=>e==="error",payload:{error:p}});var j=t=>typeof t=="boolean";var C=t=>f(t,{type:e=>e==="eventType",payload:{eventType:p,isPhantom:j}});var k=t=>f(t,{type:e=>e==="heartbeat"});var B=t=>f(t,{type:e=>e==="row",payload:e=>e!==void 0});var $=t=>f(t,{type:e=>e==="subject",payload:{subject:p}});var w=class{#n;#t;#e(e){return new URL(e,this.#n).toString()}constructor(e,r){this.#n=e,this.#t=r}async ping(){let e=this.#e("/api/v1/ping"),r=await fetch(e,{method:"get"});if(!m(r))throw new Error("Server must be EventSourcingDB.");if(r.status!==200)throw new Error(`Failed to ping, got HTTP status code '${r.status}', expected '200'.`);let o=await r.json();if(!f(o,{type:p}))throw new Error("Failed to parse response.");if(o.type!=="io.eventsourcingdb.api.ping-received")throw new Error("Failed to ping.")}async verifyApiToken(){let e=this.#e("/api/v1/verify-api-token"),r=await fetch(e,{method:"post",headers:{authorization:`Bearer ${this.#t}`}});if(!m(r))throw new Error("Server must be EventSourcingDB.");if(r.status!==200)throw new Error(`Failed to verify API token, got HTTP status code '${r.status}', expected '200'.`);let o=await r.json();if(!f(o,{type:p}))throw new Error("Failed to parse response.");if(o.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(e,r=[]){let o=this.#e("/api/v1/write-events"),s=await fetch(o,{method:"post",headers:{authorization:`Bearer ${this.#t}`,"content-type":"application/json"},body:JSON.stringify({events:e,preconditions:r})});if(!m(s))throw new Error("Server must be EventSourcingDB.");if(s.status!==200)throw new Error(`Failed to write events, got HTTP status code '${s.status}', expected '200'.`);let c=await s.json();if(!Array.isArray(c))throw new Error("Failed to parse response.");return c.map(u=>{if(!O(u))throw new Error("Failed to parse response item.");return E(u)})}readEvents(e,r,o){let s=this.#e("/api/v1/read-events"),c=this.#t;return(async function*(){let d=new AbortController,u=o??d.signal,a=!o,n;if(o&&!o.aborted){let i=()=>d.abort();o.addEventListener("abort",i,{once:!0}),n=()=>o.removeEventListener("abort",i)}try{let i=await fetch(s,{method:"post",headers:{authorization:`Bearer ${c}`,"content-type":"application/json"},body:JSON.stringify({subject:e,options:r}),signal:u});if(!m(i)){let h=i.headers.get("Server");throw h?new Error(`Server must be EventSourcingDB, got Server header: '${h}'.`):new Error("Server must be EventSourcingDB, but Server header is missing.")}if(i.status!==200)throw new Error(`Failed to read events, got HTTP status code '${i.status}', expected '200'.`);if(!i.body)throw new Error("Failed to read events.");for await(let h of v(i.body,u)){if(b(h))throw new Error(`${h.payload.error}.`);if(P(h)){yield E(h.payload);continue}throw new Error("Failed to read events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{n&&n(),a&&d.abort()}})()}runEventQlQuery(e,r){let o=this.#e("/api/v1/run-eventql-query"),s=this.#t;return(async function*(){let c=new AbortController,d=r??c.signal,u=!r,a;if(r&&!r.aborted){let n=()=>c.abort();r.addEventListener("abort",n,{once:!0}),a=()=>r.removeEventListener("abort",n)}try{let n=await fetch(o,{method:"post",headers:{authorization:`Bearer ${s}`,"content-type":"application/json"},body:JSON.stringify({query:e}),signal:d});if(!m(n)){let i=n.headers.get("Server");throw i?new Error(`Server must be EventSourcingDB, got Server header: '${i}'.`):new Error("Server must be EventSourcingDB, but Server header is missing.")}if(n.status!==200)throw new Error(`Failed to run EventQL query, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw new Error("Failed to run EventQL query.");for await(let i of v(n.body,d))if(!k(i)){if(b(i))throw new Error(`${i.payload.error}.`);if(B(i)){yield i.payload;continue}throw new Error("Failed to run EventQL query.")}}catch(n){if(n instanceof DOMException&&n.name==="AbortError")return;throw n}finally{a&&a(),u&&c.abort()}})()}observeEvents(e,r,o){let s=this.#e("/api/v1/observe-events"),c=this.#t;return(async function*(){let d=new AbortController,u=o??d.signal,a=!o,n;if(o&&!o.aborted){let i=()=>d.abort();o.addEventListener("abort",i,{once:!0}),n=()=>o.removeEventListener("abort",i)}try{let i=await fetch(s,{method:"post",headers:{authorization:`Bearer ${c}`,"content-type":"application/json"},body:JSON.stringify({subject:e,options:r}),signal:u});if(!m(i)){let h=i.headers.get("Server");throw h?new Error(`Server must be EventSourcingDB, got Server header: '${h}'.`):new Error("Server must be EventSourcingDB, but Server header is missing.")}if(i.status!==200)throw new Error(`Failed to observe events, got HTTP status code '${i.status}', expected '200'.`);if(!i.body)throw new Error("Failed to observe events.");for await(let h of v(i.body,u))if(!k(h)){if(b(h))throw new Error(`${h.payload.error}.`);if(P(h)){yield E(h.payload);continue}throw new Error("Failed to observe events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{n&&n(),a&&d.abort()}})()}async registerEventSchema(e,r){let o=this.#e("/api/v1/register-event-schema"),s=await fetch(o,{method:"post",headers:{authorization:`Bearer ${this.#t}`,"content-type":"application/json"},body:JSON.stringify({eventType:e,schema:r})});if(!m(s))throw new Error("Server must be EventSourcingDB.");if(s.status!==200)throw new Error(`Failed to register event schema, got HTTP status code '${s.status}', expected '200'.`)}readSubjects(e,r){let o=this.#e("/api/v1/read-subjects"),s=this.#t;return(async function*(){let c=new AbortController,d=r??c.signal,u=!r,a;if(r&&!r.aborted){let n=()=>c.abort();r.addEventListener("abort",n,{once:!0}),a=()=>r.removeEventListener("abort",n)}try{let n=await fetch(o,{method:"post",headers:{authorization:`Bearer ${s}`,"content-type":"application/json"},body:JSON.stringify({baseSubject:e}),signal:d});if(!m(n)){let i=n.headers.get("Server");throw i?new Error(`Server must be EventSourcingDB, got Server header: '${i}'.`):new Error("Server must be EventSourcingDB, but Server header is missing.")}if(n.status!==200)throw new Error(`Failed to read subjects, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw new Error("Failed to read subjects.");for await(let i of v(n.body,d)){if(b(i))throw new Error(`${i.payload.error}.`);if($(i)){yield i.payload.subject;continue}throw new Error("Failed to read subjects.")}}catch(n){if(n instanceof DOMException&&n.name==="AbortError")return;throw n}finally{a&&a(),u&&c.abort()}})()}async readEventType(e){let r=this.#e("/api/v1/read-event-type"),o=await fetch(r,{method:"post",headers:{authorization:`Bearer ${this.#t}`,"content-type":"application/json"},body:JSON.stringify({eventType:e})});if(!m(o))throw new Error("Server must be EventSourcingDB.");if(o.status!==200)throw new Error(`Failed to read event type, got HTTP status code '${o.status}', expected '200'.`);let s=await o.json();if(!f(s,{eventType:p,isPhantom:j}))throw new Error("Failed to parse response.");return s}readEventTypes(e){let r=this.#e("/api/v1/read-event-types"),o=this.#t;return(async function*(){let s=new AbortController,c=e??s.signal,d=!e,u;if(e&&!e.aborted){let a=()=>s.abort();e.addEventListener("abort",a,{once:!0}),u=()=>e.removeEventListener("abort",a)}try{let a=await fetch(r,{method:"post",headers:{authorization:`Bearer ${o}`},signal:c});if(!m(a)){let n=a.headers.get("Server");throw n?new Error(`Server must be EventSourcingDB, got Server header: '${n}'.`):new Error("Server must be EventSourcingDB, but Server header is missing.")}if(a.status!==200)throw new Error(`Failed to read event types, got HTTP status code '${a.status}', expected '200'.`);if(!a.body)throw new Error("Failed to read event types.");for await(let n of v(a.body,c))if(!k(n)){if(b(n))throw new Error(`${n.payload.error}.`);if(C(n)){yield n.payload;continue}throw new Error("Failed to read event types.")}}catch(a){if(a instanceof DOMException&&a.name==="AbortError")return;throw a}finally{u&&u(),d&&s.abort()}})()}};import R from"crypto";import{GenericContainer as D,Wait as L}from"testcontainers";var x=class{#n="thenativeweb/eventsourcingdb";#t="latest";#e=3e3;#i="secret";#o;#r;withImageTag(e){return this.#t=e,this}withApiToken(e){return this.#i=e,this}withSigningKey(){let{privateKey:e}=R.generateKeyPairSync("ed25519");return this.#o=e,this}withPort(e){return this.#e=e,this}async start(){let e=["run","--api-token",this.#i,"--data-directory-temporary","--http-enabled","--https-enabled=false"],r=[];this.#o!==void 0&&(e.push("--signing-key-file"),e.push("/etc/esdb/signing-key.pem"),r.push({content:this.#o.export({format:"pem",type:"pkcs8"}),target:"/etc/esdb/signing-key.pem",mode:511})),this.#r=await new D(`${this.#n}:${this.#t}`).withExposedPorts(this.#e).withCommand(e).withCopyContentToContainer(r).withWaitStrategy(L.forHttp("/api/v1/ping",this.#e).withStartupTimeout(1e4)).start()}getHost(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getHost()}getMappedPort(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getMappedPort(this.#e)}getBaseUrl(){if(this.#r===void 0)throw new Error("Container must be running.");let e=this.getHost(),r=this.getMappedPort();return new URL(`http://${e}:${r}`)}getApiToken(){return this.#i}getSigningKey(){if(this.#o===void 0)throw new Error("Signing key not set.");return this.#o}getVerificationKey(){if(this.#o===void 0)throw new Error("Signing key not set.");return R.createPublicKey(this.#o)}isRunning(){return this.#r!==void 0}async stop(){this.#r!==void 0&&(await this.#r.stop(),this.#r=void 0)}getClient(){return new w(this.getBaseUrl(),this.getApiToken())}};var K=t=>({type:"isEventQlQueryTrue",payload:{query:t}});var N=(t,e)=>({type:"isSubjectOnEventId",payload:{subject:t,eventId:e}});var U=t=>({type:"isSubjectPopulated",payload:{subject:t}});var G=t=>({type:"isSubjectPristine",payload:{subject:t}});export{w as Client,x as Container,K as isEventQlQueryTrue,N as isSubjectOnEventId,U as isSubjectPopulated,G as isSubjectPristine};
|