eventsourcingdb 1.8.2 → 1.8.4
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 -0
- package/dist/index.js +7 -0
- 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 -17
- package/.editorconfig +0 -22
- package/.nvmrc +0 -1
- package/dist/index.cjs +0 -3
- package/dist/index.d.cts +0 -182
- package/dist/index.d.mts +0 -182
- package/dist/index.mjs +0 -3
package/package.json
CHANGED
|
@@ -1,38 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eventsourcingdb",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.4",
|
|
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
|
-
|
|
13
|
-
"default": "./dist/index.mjs"
|
|
14
|
-
},
|
|
15
|
-
"require": {
|
|
16
|
-
"types": "./dist/index.d.cts",
|
|
17
|
-
"default": "./dist/index.cjs"
|
|
18
|
-
}
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
19
14
|
}
|
|
20
15
|
},
|
|
21
|
-
"types": "./dist/index.d.
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
22
20
|
"dependencies": {
|
|
23
|
-
"testcontainers": "12.0
|
|
21
|
+
"testcontainers": "12.1.0"
|
|
24
22
|
},
|
|
25
23
|
"devDependencies": {
|
|
26
|
-
"@biomejs/biome": "2.5.
|
|
27
|
-
"@types/node": "
|
|
24
|
+
"@biomejs/biome": "2.5.5",
|
|
25
|
+
"@types/node": "26.1.1",
|
|
28
26
|
"get-port": "7.2.0",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"typescript": "6.0.3"
|
|
27
|
+
"tsx": "4.23.1",
|
|
28
|
+
"typescript": "7.0.2"
|
|
32
29
|
},
|
|
33
30
|
"scripts": {
|
|
34
31
|
"analyze": "npx tsc --noEmit && npx biome check --error-on-warnings .",
|
|
35
|
-
"build": "npx tsc
|
|
32
|
+
"build": "npx tsc -p tsconfig.build.json",
|
|
36
33
|
"format": "npx biome check --write .",
|
|
37
34
|
"qa": "npm run analyze && npm run test",
|
|
38
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/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
26.3.1
|
package/dist/index.cjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require("node:crypto");c=s(c);let l=require("testcontainers");var u=class{specversion;id;time;#e;source;subject;type;datacontenttype;data;hash;predecessorhash;traceparent;tracestate;signature;constructor({specversion:e,id:t,time:n,timeFromServer:r,source:i,subject:a,type:o,datacontenttype:s,data:c,hash:l,predecessorhash:u,traceparent:d,tracestate:f,signature:p}){this.specversion=e,this.id=t,this.time=n,this.#e=r,this.source=i,this.subject=a,this.type=o,this.datacontenttype=s,this.data=c,this.hash=l,this.predecessorhash=u,this.traceparent=d,this.tracestate=f,this.signature=p}verifyHash(){let e=`${this.specversion}|${this.id}|${this.predecessorhash}|${this.#e}|${this.source}|${this.subject}|${this.type}|${this.datacontenttype}`,t=c.default.createHash(`sha256`).update(e).digest(`hex`),n=c.default.createHash(`sha256`).update(JSON.stringify(this.data)).digest(`hex`);if(c.default.createHash(`sha256`).update(`${t}${n}`).digest(`hex`)!==this.hash)throw Error(`Failed to verify hash.`)}verifySignature(e){if(this.signature===null)throw Error(`Signature must not be null.`);this.verifyHash();let t=`esdb:signature:v1:`;if(!this.signature.startsWith(t))throw Error(`Signature must start with '${t}'`);let n=this.signature.slice(18),r=Buffer.from(n,`hex`),i=Buffer.from(this.hash,`utf8`);if(!c.default.verify(null,i,e,r))throw Error(`Signature verification failed.`)}};const d=e=>new u({specversion:e.specversion,id:e.id,time:new Date(e.time),timeFromServer:e.time,source:e.source,subject:e.subject,type:e.type,datacontenttype:e.datacontenttype,data:e.data,hash:e.hash,predecessorhash:e.predecessorhash,traceparent:e.traceparent,tracestate:e.tracestate,signature:e.signature}),f=e=>!(typeof e!=`object`||!e||Array.isArray(e)),p=(e,t)=>{if(!f(e))return!1;for(let n of Object.keys(t)){let r=t[n],i=e[n];if(typeof r==`function`){if(!r(i))return!1;continue}if(!f(i)||!p(i,r))return!1}return!0},m=e=>typeof e==`string`,h=e=>e===null||typeof e==`string`,g=e=>e===void 0||typeof e==`string`,_=e=>p(e,{specversion:m,id:m,time:m,source:m,subject:m,type:m,datacontenttype:m,data:f,hash:m,predecessorhash:m,traceparent:g,tracestate:g,signature:h}),v=e=>{let t=e.headers.get(`Server`);return!(!t||!t.startsWith(`EventSourcingDB/`))},y=async function*(e,t){let n=e.getReader(),r=new TextDecoder(`utf-8`),i=``,a=()=>{n.cancel().catch(()=>{})};if(t.aborted){await n.cancel().catch(()=>{});return}t.addEventListener(`abort`,a);try{for(;!t.aborted;){let{done:e,value:t}=await n.read();if(e)break;i+=r.decode(t,{stream:!0});let a=i.indexOf(`
|
|
2
|
-
`);for(;a!==-1;){let e=i.slice(0,a).trim();i=i.slice(a+1),e&&(yield JSON.parse(e)),a=i.indexOf(`
|
|
3
|
-
`)}}}finally{t.removeEventListener(`abort`,a),await n.cancel().catch(()=>{})}},b=e=>p(e,{type:e=>e===`event`,payload:{specversion:m,id:m,time:m,source:m,subject:m,type:m,datacontenttype:m,data:f,hash:m,predecessorhash:m,traceparent:g,tracestate:g,signature:h}}),x=e=>p(e,{type:e=>e===`error`,payload:{error:m}}),S=e=>typeof e==`boolean`,C=e=>p(e,{type:e=>e===`eventType`,payload:{eventType:m,isPhantom:S}}),w=e=>p(e,{type:e=>e===`heartbeat`}),T=e=>p(e,{type:e=>e===`row`,payload:e=>e!==void 0}),E=e=>p(e,{type:e=>e===`subject`,payload:{subject:m}});var D=class{#e;#t;#n(e){return new URL(e,this.#e).toString()}constructor(e,t){this.#e=e,this.#t=t}async ping(){let e=this.#n(`/api/v1/ping`),t=await fetch(e,{method:`get`});if(!v(t))throw Error(`Server must be EventSourcingDB.`);if(t.status!==200)throw Error(`Failed to ping, got HTTP status code '${t.status}', expected '200'.`);let n=await t.json();if(!p(n,{type:m}))throw Error(`Failed to parse response.`);if(n.type!==`io.eventsourcingdb.api.ping-received`)throw Error(`Failed to ping.`)}async verifyApiToken(){let e=this.#n(`/api/v1/verify-api-token`),t=await fetch(e,{method:`post`,headers:{authorization:`Bearer ${this.#t}`}});if(!v(t))throw Error(`Server must be EventSourcingDB.`);if(t.status!==200)throw Error(`Failed to verify API token, got HTTP status code '${t.status}', expected '200'.`);let n=await t.json();if(!p(n,{type:m}))throw Error(`Failed to parse response.`);if(n.type!==`io.eventsourcingdb.api.api-token-verified`)throw Error(`Failed to verify API token.`)}async writeEvents(e,t=[]){let n=this.#n(`/api/v1/write-events`),r=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${this.#t}`,"content-type":`application/json`},body:JSON.stringify({events:e,preconditions:t})});if(!v(r))throw Error(`Server must be EventSourcingDB.`);if(r.status!==200)throw Error(`Failed to write events, got HTTP status code '${r.status}', expected '200'.`);let i=await r.json();if(!Array.isArray(i))throw Error(`Failed to parse response.`);return i.map(e=>{if(!_(e))throw Error(`Failed to parse response item.`);return d(e)})}readEvents(e,t,n){let r=this.#n(`/api/v1/read-events`),i=this.#t;return(async function*(){let a=new AbortController,o=n??a.signal,s=!n,c;if(n&&!n.aborted){let e=()=>a.abort();n.addEventListener(`abort`,e,{once:!0}),c=()=>n.removeEventListener(`abort`,e)}try{let n=await fetch(r,{method:`post`,headers:{authorization:`Bearer ${i}`,"content-type":`application/json`},body:JSON.stringify({subject:e,options:t}),signal:o});if(!v(n)){let e=n.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(n.status!==200)throw Error(`Failed to read events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw Error(`Failed to read events.`);for await(let e of y(n.body,o)){if(x(e))throw Error(`${e.payload.error}.`);if(b(e)){yield d(e.payload);continue}throw Error(`Failed to read events.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{c&&c(),s&&a.abort()}})()}runEventQlQuery(e,t){let n=this.#n(`/api/v1/run-eventql-query`),r=this.#t;return(async function*(){let i=new AbortController,a=t??i.signal,o=!t,s;if(t&&!t.aborted){let e=()=>i.abort();t.addEventListener(`abort`,e,{once:!0}),s=()=>t.removeEventListener(`abort`,e)}try{let t=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${r}`,"content-type":`application/json`},body:JSON.stringify({query:e}),signal:a});if(!v(t)){let e=t.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(t.status!==200)throw Error(`Failed to run EventQL query, got HTTP status code '${t.status}', expected '200'.`);if(!t.body)throw Error(`Failed to run EventQL query.`);for await(let e of y(t.body,a))if(!w(e)){if(x(e))throw Error(`${e.payload.error}.`);if(T(e)){yield e.payload;continue}throw Error(`Failed to run EventQL query.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{s&&s(),o&&i.abort()}})()}observeEvents(e,t,n){let r=this.#n(`/api/v1/observe-events`),i=this.#t;return(async function*(){let a=new AbortController,o=n??a.signal,s=!n,c;if(n&&!n.aborted){let e=()=>a.abort();n.addEventListener(`abort`,e,{once:!0}),c=()=>n.removeEventListener(`abort`,e)}try{let n=await fetch(r,{method:`post`,headers:{authorization:`Bearer ${i}`,"content-type":`application/json`},body:JSON.stringify({subject:e,options:t}),signal:o});if(!v(n)){let e=n.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(n.status!==200)throw Error(`Failed to observe events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw Error(`Failed to observe events.`);for await(let e of y(n.body,o))if(!w(e)){if(x(e))throw Error(`${e.payload.error}.`);if(b(e)){yield d(e.payload);continue}throw Error(`Failed to observe events.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{c&&c(),s&&a.abort()}})()}async registerEventSchema(e,t){let n=this.#n(`/api/v1/register-event-schema`),r=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${this.#t}`,"content-type":`application/json`},body:JSON.stringify({eventType:e,schema:t})});if(!v(r))throw Error(`Server must be EventSourcingDB.`);if(r.status!==200)throw Error(`Failed to register event schema, got HTTP status code '${r.status}', expected '200'.`)}readSubjects(e,t){let n=this.#n(`/api/v1/read-subjects`),r=this.#t;return(async function*(){let i=new AbortController,a=t??i.signal,o=!t,s;if(t&&!t.aborted){let e=()=>i.abort();t.addEventListener(`abort`,e,{once:!0}),s=()=>t.removeEventListener(`abort`,e)}try{let t=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${r}`,"content-type":`application/json`},body:JSON.stringify({baseSubject:e}),signal:a});if(!v(t)){let e=t.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(t.status!==200)throw Error(`Failed to read subjects, got HTTP status code '${t.status}', expected '200'.`);if(!t.body)throw Error(`Failed to read subjects.`);for await(let e of y(t.body,a)){if(x(e))throw Error(`${e.payload.error}.`);if(E(e)){yield e.payload.subject;continue}throw Error(`Failed to read subjects.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{s&&s(),o&&i.abort()}})()}async readEventType(e){let t=this.#n(`/api/v1/read-event-type`),n=await fetch(t,{method:`post`,headers:{authorization:`Bearer ${this.#t}`,"content-type":`application/json`},body:JSON.stringify({eventType:e})});if(!v(n))throw Error(`Server must be EventSourcingDB.`);if(n.status!==200)throw Error(`Failed to read event type, got HTTP status code '${n.status}', expected '200'.`);let r=await n.json();if(!p(r,{eventType:m,isPhantom:S}))throw Error(`Failed to parse response.`);return r}readEventTypes(e){let t=this.#n(`/api/v1/read-event-types`),n=this.#t;return(async function*(){let r=new AbortController,i=e??r.signal,a=!e,o;if(e&&!e.aborted){let t=()=>r.abort();e.addEventListener(`abort`,t,{once:!0}),o=()=>e.removeEventListener(`abort`,t)}try{let e=await fetch(t,{method:`post`,headers:{authorization:`Bearer ${n}`},signal:i});if(!v(e)){let t=e.headers.get(`Server`);throw Error(t?`Server must be EventSourcingDB, got Server header: '${t}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(e.status!==200)throw Error(`Failed to read event types, got HTTP status code '${e.status}', expected '200'.`);if(!e.body)throw Error(`Failed to read event types.`);for await(let t of y(e.body,i))if(!w(t)){if(x(t))throw Error(`${t.payload.error}.`);if(C(t)){yield t.payload;continue}throw Error(`Failed to read event types.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{o&&o(),a&&r.abort()}})()}},O=class{#e=`thenativeweb/eventsourcingdb`;#t=`latest`;#n=3e3;#r=`secret`;#i;#a;withImageTag(e){return this.#t=e,this}withApiToken(e){return this.#r=e,this}withSigningKey(){let{privateKey:e}=c.default.generateKeyPairSync(`ed25519`);return this.#i=e,this}withPort(e){return this.#n=e,this}async start(){let e=[`run`,`--api-token`,this.#r,`--data-directory-temporary`,`--http-enabled`,`--https-enabled=false`],t=[];this.#i!==void 0&&(e.push(`--signing-key-file`),e.push(`/etc/esdb/signing-key.pem`),t.push({content:this.#i.export({format:`pem`,type:`pkcs8`}),target:`/etc/esdb/signing-key.pem`,mode:511})),this.#a=await new l.GenericContainer(`${this.#e}:${this.#t}`).withExposedPorts(this.#n).withCommand(e).withCopyContentToContainer(t).withWaitStrategy(l.Wait.forHttp(`/api/v1/ping`,this.#n).withStartupTimeout(1e4)).start()}getHost(){if(this.#a===void 0)throw Error(`Container must be running.`);return this.#a.getHost()}getMappedPort(){if(this.#a===void 0)throw Error(`Container must be running.`);return this.#a.getMappedPort(this.#n)}getBaseUrl(){if(this.#a===void 0)throw Error(`Container must be running.`);let e=this.getHost(),t=this.getMappedPort();return new URL(`http://${e}:${t}`)}getApiToken(){return this.#r}getSigningKey(){if(this.#i===void 0)throw Error(`Signing key not set.`);return this.#i}getVerificationKey(){if(this.#i===void 0)throw Error(`Signing key not set.`);return c.default.createPublicKey(this.#i)}isRunning(){return this.#a!==void 0}async stop(){this.#a!==void 0&&(await this.#a.stop(),this.#a=void 0)}getClient(){return new D(this.getBaseUrl(),this.getApiToken())}};const k=e=>({type:`isEventQlQueryTrue`,payload:{query:e}}),A=(e,t)=>({type:`isSubjectOnEventId`,payload:{subject:e,eventId:t}}),j=e=>({type:`isSubjectPopulated`,payload:{subject:e}}),M=e=>({type:`isSubjectPristine`,payload:{subject:e}});exports.Client=D,exports.Container=O,exports.isEventQlQueryTrue=k,exports.isSubjectOnEventId=A,exports.isSubjectPopulated=j,exports.isSubjectPristine=M;
|
package/dist/index.d.cts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
|
|
3
|
-
//#region src/Event.d.ts
|
|
4
|
-
declare class Event {
|
|
5
|
-
#private;
|
|
6
|
-
specversion: string;
|
|
7
|
-
id: string;
|
|
8
|
-
time: Date;
|
|
9
|
-
source: string;
|
|
10
|
-
subject: string;
|
|
11
|
-
type: string;
|
|
12
|
-
datacontenttype: string;
|
|
13
|
-
data: Record<string, unknown>;
|
|
14
|
-
hash: string;
|
|
15
|
-
predecessorhash: string;
|
|
16
|
-
traceparent?: string;
|
|
17
|
-
tracestate?: string;
|
|
18
|
-
signature: string | null;
|
|
19
|
-
constructor({
|
|
20
|
-
specversion,
|
|
21
|
-
id,
|
|
22
|
-
time,
|
|
23
|
-
timeFromServer,
|
|
24
|
-
source,
|
|
25
|
-
subject,
|
|
26
|
-
type,
|
|
27
|
-
datacontenttype,
|
|
28
|
-
data,
|
|
29
|
-
hash,
|
|
30
|
-
predecessorhash,
|
|
31
|
-
traceparent,
|
|
32
|
-
tracestate,
|
|
33
|
-
signature
|
|
34
|
-
}: {
|
|
35
|
-
specversion: string;
|
|
36
|
-
id: string;
|
|
37
|
-
time: Date;
|
|
38
|
-
timeFromServer: string;
|
|
39
|
-
source: string;
|
|
40
|
-
subject: string;
|
|
41
|
-
type: string;
|
|
42
|
-
datacontenttype: string;
|
|
43
|
-
data: Record<string, unknown>;
|
|
44
|
-
hash: string;
|
|
45
|
-
predecessorhash: string;
|
|
46
|
-
traceparent?: string;
|
|
47
|
-
tracestate?: string;
|
|
48
|
-
signature: string | null;
|
|
49
|
-
});
|
|
50
|
-
verifyHash(): void;
|
|
51
|
-
verifySignature(verificationKey: crypto.KeyObject): void;
|
|
52
|
-
}
|
|
53
|
-
//#endregion
|
|
54
|
-
//#region src/EventCandidate.d.ts
|
|
55
|
-
interface EventCandidate {
|
|
56
|
-
source: string;
|
|
57
|
-
subject: string;
|
|
58
|
-
type: string;
|
|
59
|
-
data: Record<string, unknown>;
|
|
60
|
-
traceparent?: string;
|
|
61
|
-
tracestate?: string;
|
|
62
|
-
}
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/EventType.d.ts
|
|
65
|
-
interface EventType {
|
|
66
|
-
eventType: string;
|
|
67
|
-
isPhantom: boolean;
|
|
68
|
-
schema?: Record<string, unknown>;
|
|
69
|
-
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/Bound.d.ts
|
|
72
|
-
type BoundType = 'inclusive' | 'exclusive';
|
|
73
|
-
interface Bound {
|
|
74
|
-
id: string;
|
|
75
|
-
type: BoundType;
|
|
76
|
-
}
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/ObserveEventsOptions.d.ts
|
|
79
|
-
type ObserveIfEventIsMissing = 'read-everything' | 'wait-for-event';
|
|
80
|
-
interface ObserveFromLatestEvent {
|
|
81
|
-
subject: string;
|
|
82
|
-
type: string;
|
|
83
|
-
ifEventIsMissing: ObserveIfEventIsMissing;
|
|
84
|
-
}
|
|
85
|
-
interface ObserveEventsOptions {
|
|
86
|
-
recursive: boolean;
|
|
87
|
-
lowerBound?: Bound;
|
|
88
|
-
fromLatestEvent?: ObserveFromLatestEvent;
|
|
89
|
-
}
|
|
90
|
-
//#endregion
|
|
91
|
-
//#region src/Precondition.d.ts
|
|
92
|
-
interface IsSubjectPristinePrecondition {
|
|
93
|
-
subject: string;
|
|
94
|
-
}
|
|
95
|
-
interface IsSubjectPopulatedPrecondition {
|
|
96
|
-
subject: string;
|
|
97
|
-
}
|
|
98
|
-
interface IsSubjectOnEventIdPrecondition {
|
|
99
|
-
subject: string;
|
|
100
|
-
eventId: string;
|
|
101
|
-
}
|
|
102
|
-
interface IsEventQlQueryTruePrecondition {
|
|
103
|
-
query: string;
|
|
104
|
-
}
|
|
105
|
-
type Precondition = {
|
|
106
|
-
type: 'isSubjectPristine';
|
|
107
|
-
payload: IsSubjectPristinePrecondition;
|
|
108
|
-
} | {
|
|
109
|
-
type: 'isSubjectOnEventId';
|
|
110
|
-
payload: IsSubjectOnEventIdPrecondition;
|
|
111
|
-
} | {
|
|
112
|
-
type: 'isSubjectPopulated';
|
|
113
|
-
payload: IsSubjectPopulatedPrecondition;
|
|
114
|
-
} | {
|
|
115
|
-
type: 'isEventQlQueryTrue';
|
|
116
|
-
payload: IsEventQlQueryTruePrecondition;
|
|
117
|
-
};
|
|
118
|
-
//#endregion
|
|
119
|
-
//#region src/ReadEventsOptions.d.ts
|
|
120
|
-
type Order = 'chronological' | 'antichronological';
|
|
121
|
-
type ReadIfEventIsMissing = 'read-nothing' | 'read-everything';
|
|
122
|
-
interface ReadFromLatestEvent {
|
|
123
|
-
subject: string;
|
|
124
|
-
type: string;
|
|
125
|
-
ifEventIsMissing: ReadIfEventIsMissing;
|
|
126
|
-
}
|
|
127
|
-
interface ReadEventsOptions {
|
|
128
|
-
recursive: boolean;
|
|
129
|
-
order?: Order;
|
|
130
|
-
lowerBound?: Bound;
|
|
131
|
-
upperBound?: Bound;
|
|
132
|
-
fromLatestEvent?: ReadFromLatestEvent;
|
|
133
|
-
}
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/Client.d.ts
|
|
136
|
-
declare class Client {
|
|
137
|
-
#private;
|
|
138
|
-
constructor(url: URL, apiToken: string);
|
|
139
|
-
ping(): Promise<void>;
|
|
140
|
-
verifyApiToken(): Promise<void>;
|
|
141
|
-
writeEvents(events: EventCandidate[], preconditions?: Precondition[]): Promise<Event[]>;
|
|
142
|
-
readEvents(subject: string, options: ReadEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
|
|
143
|
-
runEventQlQuery(query: string, signal?: AbortSignal): AsyncGenerator<unknown, void, void>;
|
|
144
|
-
observeEvents(subject: string, options: ObserveEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
|
|
145
|
-
registerEventSchema(eventType: string, schema: Record<string, unknown>): Promise<void>;
|
|
146
|
-
readSubjects(baseSubject: string, signal?: AbortSignal): AsyncGenerator<string, void, void>;
|
|
147
|
-
readEventType(eventType: string): Promise<EventType>;
|
|
148
|
-
readEventTypes(signal?: AbortSignal): AsyncGenerator<EventType, void, void>;
|
|
149
|
-
}
|
|
150
|
-
//#endregion
|
|
151
|
-
//#region src/Container.d.ts
|
|
152
|
-
declare class Container {
|
|
153
|
-
#private;
|
|
154
|
-
withImageTag(tag: string): this;
|
|
155
|
-
withApiToken(token: string): this;
|
|
156
|
-
withSigningKey(): this;
|
|
157
|
-
withPort(port: number): this;
|
|
158
|
-
start(): Promise<void>;
|
|
159
|
-
getHost(): string;
|
|
160
|
-
getMappedPort(): number;
|
|
161
|
-
getBaseUrl(): URL;
|
|
162
|
-
getApiToken(): string;
|
|
163
|
-
getSigningKey(): crypto.KeyObject;
|
|
164
|
-
getVerificationKey(): crypto.KeyObject;
|
|
165
|
-
isRunning(): boolean;
|
|
166
|
-
stop(): Promise<void>;
|
|
167
|
-
getClient(): Client;
|
|
168
|
-
}
|
|
169
|
-
//#endregion
|
|
170
|
-
//#region src/isEventQlQueryTrue.d.ts
|
|
171
|
-
declare const isEventQlQueryTrue: (query: string) => Precondition;
|
|
172
|
-
//#endregion
|
|
173
|
-
//#region src/isSubjectOnEventId.d.ts
|
|
174
|
-
declare const isSubjectOnEventId: (subject: string, eventId: string) => Precondition;
|
|
175
|
-
//#endregion
|
|
176
|
-
//#region src/isSubjectPopulated.d.ts
|
|
177
|
-
declare const isSubjectPopulated: (subject: string) => Precondition;
|
|
178
|
-
//#endregion
|
|
179
|
-
//#region src/isSubjectPristine.d.ts
|
|
180
|
-
declare const isSubjectPristine: (subject: string) => Precondition;
|
|
181
|
-
//#endregion
|
|
182
|
-
export { Client, Container, type Event, type EventCandidate, type EventType, type ObserveEventsOptions, type Precondition, type ReadEventsOptions, isEventQlQueryTrue, isSubjectOnEventId, isSubjectPopulated, isSubjectPristine };
|
package/dist/index.d.mts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
|
|
3
|
-
//#region src/Event.d.ts
|
|
4
|
-
declare class Event {
|
|
5
|
-
#private;
|
|
6
|
-
specversion: string;
|
|
7
|
-
id: string;
|
|
8
|
-
time: Date;
|
|
9
|
-
source: string;
|
|
10
|
-
subject: string;
|
|
11
|
-
type: string;
|
|
12
|
-
datacontenttype: string;
|
|
13
|
-
data: Record<string, unknown>;
|
|
14
|
-
hash: string;
|
|
15
|
-
predecessorhash: string;
|
|
16
|
-
traceparent?: string;
|
|
17
|
-
tracestate?: string;
|
|
18
|
-
signature: string | null;
|
|
19
|
-
constructor({
|
|
20
|
-
specversion,
|
|
21
|
-
id,
|
|
22
|
-
time,
|
|
23
|
-
timeFromServer,
|
|
24
|
-
source,
|
|
25
|
-
subject,
|
|
26
|
-
type,
|
|
27
|
-
datacontenttype,
|
|
28
|
-
data,
|
|
29
|
-
hash,
|
|
30
|
-
predecessorhash,
|
|
31
|
-
traceparent,
|
|
32
|
-
tracestate,
|
|
33
|
-
signature
|
|
34
|
-
}: {
|
|
35
|
-
specversion: string;
|
|
36
|
-
id: string;
|
|
37
|
-
time: Date;
|
|
38
|
-
timeFromServer: string;
|
|
39
|
-
source: string;
|
|
40
|
-
subject: string;
|
|
41
|
-
type: string;
|
|
42
|
-
datacontenttype: string;
|
|
43
|
-
data: Record<string, unknown>;
|
|
44
|
-
hash: string;
|
|
45
|
-
predecessorhash: string;
|
|
46
|
-
traceparent?: string;
|
|
47
|
-
tracestate?: string;
|
|
48
|
-
signature: string | null;
|
|
49
|
-
});
|
|
50
|
-
verifyHash(): void;
|
|
51
|
-
verifySignature(verificationKey: crypto.KeyObject): void;
|
|
52
|
-
}
|
|
53
|
-
//#endregion
|
|
54
|
-
//#region src/EventCandidate.d.ts
|
|
55
|
-
interface EventCandidate {
|
|
56
|
-
source: string;
|
|
57
|
-
subject: string;
|
|
58
|
-
type: string;
|
|
59
|
-
data: Record<string, unknown>;
|
|
60
|
-
traceparent?: string;
|
|
61
|
-
tracestate?: string;
|
|
62
|
-
}
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/EventType.d.ts
|
|
65
|
-
interface EventType {
|
|
66
|
-
eventType: string;
|
|
67
|
-
isPhantom: boolean;
|
|
68
|
-
schema?: Record<string, unknown>;
|
|
69
|
-
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/Bound.d.ts
|
|
72
|
-
type BoundType = 'inclusive' | 'exclusive';
|
|
73
|
-
interface Bound {
|
|
74
|
-
id: string;
|
|
75
|
-
type: BoundType;
|
|
76
|
-
}
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/ObserveEventsOptions.d.ts
|
|
79
|
-
type ObserveIfEventIsMissing = 'read-everything' | 'wait-for-event';
|
|
80
|
-
interface ObserveFromLatestEvent {
|
|
81
|
-
subject: string;
|
|
82
|
-
type: string;
|
|
83
|
-
ifEventIsMissing: ObserveIfEventIsMissing;
|
|
84
|
-
}
|
|
85
|
-
interface ObserveEventsOptions {
|
|
86
|
-
recursive: boolean;
|
|
87
|
-
lowerBound?: Bound;
|
|
88
|
-
fromLatestEvent?: ObserveFromLatestEvent;
|
|
89
|
-
}
|
|
90
|
-
//#endregion
|
|
91
|
-
//#region src/Precondition.d.ts
|
|
92
|
-
interface IsSubjectPristinePrecondition {
|
|
93
|
-
subject: string;
|
|
94
|
-
}
|
|
95
|
-
interface IsSubjectPopulatedPrecondition {
|
|
96
|
-
subject: string;
|
|
97
|
-
}
|
|
98
|
-
interface IsSubjectOnEventIdPrecondition {
|
|
99
|
-
subject: string;
|
|
100
|
-
eventId: string;
|
|
101
|
-
}
|
|
102
|
-
interface IsEventQlQueryTruePrecondition {
|
|
103
|
-
query: string;
|
|
104
|
-
}
|
|
105
|
-
type Precondition = {
|
|
106
|
-
type: 'isSubjectPristine';
|
|
107
|
-
payload: IsSubjectPristinePrecondition;
|
|
108
|
-
} | {
|
|
109
|
-
type: 'isSubjectOnEventId';
|
|
110
|
-
payload: IsSubjectOnEventIdPrecondition;
|
|
111
|
-
} | {
|
|
112
|
-
type: 'isSubjectPopulated';
|
|
113
|
-
payload: IsSubjectPopulatedPrecondition;
|
|
114
|
-
} | {
|
|
115
|
-
type: 'isEventQlQueryTrue';
|
|
116
|
-
payload: IsEventQlQueryTruePrecondition;
|
|
117
|
-
};
|
|
118
|
-
//#endregion
|
|
119
|
-
//#region src/ReadEventsOptions.d.ts
|
|
120
|
-
type Order = 'chronological' | 'antichronological';
|
|
121
|
-
type ReadIfEventIsMissing = 'read-nothing' | 'read-everything';
|
|
122
|
-
interface ReadFromLatestEvent {
|
|
123
|
-
subject: string;
|
|
124
|
-
type: string;
|
|
125
|
-
ifEventIsMissing: ReadIfEventIsMissing;
|
|
126
|
-
}
|
|
127
|
-
interface ReadEventsOptions {
|
|
128
|
-
recursive: boolean;
|
|
129
|
-
order?: Order;
|
|
130
|
-
lowerBound?: Bound;
|
|
131
|
-
upperBound?: Bound;
|
|
132
|
-
fromLatestEvent?: ReadFromLatestEvent;
|
|
133
|
-
}
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/Client.d.ts
|
|
136
|
-
declare class Client {
|
|
137
|
-
#private;
|
|
138
|
-
constructor(url: URL, apiToken: string);
|
|
139
|
-
ping(): Promise<void>;
|
|
140
|
-
verifyApiToken(): Promise<void>;
|
|
141
|
-
writeEvents(events: EventCandidate[], preconditions?: Precondition[]): Promise<Event[]>;
|
|
142
|
-
readEvents(subject: string, options: ReadEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
|
|
143
|
-
runEventQlQuery(query: string, signal?: AbortSignal): AsyncGenerator<unknown, void, void>;
|
|
144
|
-
observeEvents(subject: string, options: ObserveEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
|
|
145
|
-
registerEventSchema(eventType: string, schema: Record<string, unknown>): Promise<void>;
|
|
146
|
-
readSubjects(baseSubject: string, signal?: AbortSignal): AsyncGenerator<string, void, void>;
|
|
147
|
-
readEventType(eventType: string): Promise<EventType>;
|
|
148
|
-
readEventTypes(signal?: AbortSignal): AsyncGenerator<EventType, void, void>;
|
|
149
|
-
}
|
|
150
|
-
//#endregion
|
|
151
|
-
//#region src/Container.d.ts
|
|
152
|
-
declare class Container {
|
|
153
|
-
#private;
|
|
154
|
-
withImageTag(tag: string): this;
|
|
155
|
-
withApiToken(token: string): this;
|
|
156
|
-
withSigningKey(): this;
|
|
157
|
-
withPort(port: number): this;
|
|
158
|
-
start(): Promise<void>;
|
|
159
|
-
getHost(): string;
|
|
160
|
-
getMappedPort(): number;
|
|
161
|
-
getBaseUrl(): URL;
|
|
162
|
-
getApiToken(): string;
|
|
163
|
-
getSigningKey(): crypto.KeyObject;
|
|
164
|
-
getVerificationKey(): crypto.KeyObject;
|
|
165
|
-
isRunning(): boolean;
|
|
166
|
-
stop(): Promise<void>;
|
|
167
|
-
getClient(): Client;
|
|
168
|
-
}
|
|
169
|
-
//#endregion
|
|
170
|
-
//#region src/isEventQlQueryTrue.d.ts
|
|
171
|
-
declare const isEventQlQueryTrue: (query: string) => Precondition;
|
|
172
|
-
//#endregion
|
|
173
|
-
//#region src/isSubjectOnEventId.d.ts
|
|
174
|
-
declare const isSubjectOnEventId: (subject: string, eventId: string) => Precondition;
|
|
175
|
-
//#endregion
|
|
176
|
-
//#region src/isSubjectPopulated.d.ts
|
|
177
|
-
declare const isSubjectPopulated: (subject: string) => Precondition;
|
|
178
|
-
//#endregion
|
|
179
|
-
//#region src/isSubjectPristine.d.ts
|
|
180
|
-
declare const isSubjectPristine: (subject: string) => Precondition;
|
|
181
|
-
//#endregion
|
|
182
|
-
export { Client, Container, type Event, type EventCandidate, type EventType, type ObserveEventsOptions, type Precondition, type ReadEventsOptions, isEventQlQueryTrue, isSubjectOnEventId, isSubjectPopulated, isSubjectPristine };
|
package/dist/index.mjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import e from"node:crypto";import{GenericContainer as t,Wait as n}from"testcontainers";var r=class{specversion;id;time;#e;source;subject;type;datacontenttype;data;hash;predecessorhash;traceparent;tracestate;signature;constructor({specversion:e,id:t,time:n,timeFromServer:r,source:i,subject:a,type:o,datacontenttype:s,data:c,hash:l,predecessorhash:u,traceparent:d,tracestate:f,signature:p}){this.specversion=e,this.id=t,this.time=n,this.#e=r,this.source=i,this.subject=a,this.type=o,this.datacontenttype=s,this.data=c,this.hash=l,this.predecessorhash=u,this.traceparent=d,this.tracestate=f,this.signature=p}verifyHash(){let t=`${this.specversion}|${this.id}|${this.predecessorhash}|${this.#e}|${this.source}|${this.subject}|${this.type}|${this.datacontenttype}`,n=e.createHash(`sha256`).update(t).digest(`hex`),r=e.createHash(`sha256`).update(JSON.stringify(this.data)).digest(`hex`);if(e.createHash(`sha256`).update(`${n}${r}`).digest(`hex`)!==this.hash)throw Error(`Failed to verify hash.`)}verifySignature(t){if(this.signature===null)throw Error(`Signature must not be null.`);this.verifyHash();let n=`esdb:signature:v1:`;if(!this.signature.startsWith(n))throw Error(`Signature must start with '${n}'`);let r=this.signature.slice(18),i=Buffer.from(r,`hex`),a=Buffer.from(this.hash,`utf8`);if(!e.verify(null,a,t,i))throw Error(`Signature verification failed.`)}};const i=e=>new r({specversion:e.specversion,id:e.id,time:new Date(e.time),timeFromServer:e.time,source:e.source,subject:e.subject,type:e.type,datacontenttype:e.datacontenttype,data:e.data,hash:e.hash,predecessorhash:e.predecessorhash,traceparent:e.traceparent,tracestate:e.tracestate,signature:e.signature}),a=e=>!(typeof e!=`object`||!e||Array.isArray(e)),o=(e,t)=>{if(!a(e))return!1;for(let n of Object.keys(t)){let r=t[n],i=e[n];if(typeof r==`function`){if(!r(i))return!1;continue}if(!a(i)||!o(i,r))return!1}return!0},s=e=>typeof e==`string`,c=e=>e===null||typeof e==`string`,l=e=>e===void 0||typeof e==`string`,u=e=>o(e,{specversion:s,id:s,time:s,source:s,subject:s,type:s,datacontenttype:s,data:a,hash:s,predecessorhash:s,traceparent:l,tracestate:l,signature:c}),d=e=>{let t=e.headers.get(`Server`);return!(!t||!t.startsWith(`EventSourcingDB/`))},f=async function*(e,t){let n=e.getReader(),r=new TextDecoder(`utf-8`),i=``,a=()=>{n.cancel().catch(()=>{})};if(t.aborted){await n.cancel().catch(()=>{});return}t.addEventListener(`abort`,a);try{for(;!t.aborted;){let{done:e,value:t}=await n.read();if(e)break;i+=r.decode(t,{stream:!0});let a=i.indexOf(`
|
|
2
|
-
`);for(;a!==-1;){let e=i.slice(0,a).trim();i=i.slice(a+1),e&&(yield JSON.parse(e)),a=i.indexOf(`
|
|
3
|
-
`)}}}finally{t.removeEventListener(`abort`,a),await n.cancel().catch(()=>{})}},p=e=>o(e,{type:e=>e===`event`,payload:{specversion:s,id:s,time:s,source:s,subject:s,type:s,datacontenttype:s,data:a,hash:s,predecessorhash:s,traceparent:l,tracestate:l,signature:c}}),m=e=>o(e,{type:e=>e===`error`,payload:{error:s}}),h=e=>typeof e==`boolean`,g=e=>o(e,{type:e=>e===`eventType`,payload:{eventType:s,isPhantom:h}}),_=e=>o(e,{type:e=>e===`heartbeat`}),v=e=>o(e,{type:e=>e===`row`,payload:e=>e!==void 0}),y=e=>o(e,{type:e=>e===`subject`,payload:{subject:s}});var b=class{#e;#t;#n(e){return new URL(e,this.#e).toString()}constructor(e,t){this.#e=e,this.#t=t}async ping(){let e=this.#n(`/api/v1/ping`),t=await fetch(e,{method:`get`});if(!d(t))throw Error(`Server must be EventSourcingDB.`);if(t.status!==200)throw Error(`Failed to ping, got HTTP status code '${t.status}', expected '200'.`);let n=await t.json();if(!o(n,{type:s}))throw Error(`Failed to parse response.`);if(n.type!==`io.eventsourcingdb.api.ping-received`)throw Error(`Failed to ping.`)}async verifyApiToken(){let e=this.#n(`/api/v1/verify-api-token`),t=await fetch(e,{method:`post`,headers:{authorization:`Bearer ${this.#t}`}});if(!d(t))throw Error(`Server must be EventSourcingDB.`);if(t.status!==200)throw Error(`Failed to verify API token, got HTTP status code '${t.status}', expected '200'.`);let n=await t.json();if(!o(n,{type:s}))throw Error(`Failed to parse response.`);if(n.type!==`io.eventsourcingdb.api.api-token-verified`)throw Error(`Failed to verify API token.`)}async writeEvents(e,t=[]){let n=this.#n(`/api/v1/write-events`),r=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${this.#t}`,"content-type":`application/json`},body:JSON.stringify({events:e,preconditions:t})});if(!d(r))throw Error(`Server must be EventSourcingDB.`);if(r.status!==200)throw Error(`Failed to write events, got HTTP status code '${r.status}', expected '200'.`);let a=await r.json();if(!Array.isArray(a))throw Error(`Failed to parse response.`);return a.map(e=>{if(!u(e))throw Error(`Failed to parse response item.`);return i(e)})}readEvents(e,t,n){let r=this.#n(`/api/v1/read-events`),a=this.#t;return(async function*(){let o=new AbortController,s=n??o.signal,c=!n,l;if(n&&!n.aborted){let e=()=>o.abort();n.addEventListener(`abort`,e,{once:!0}),l=()=>n.removeEventListener(`abort`,e)}try{let n=await fetch(r,{method:`post`,headers:{authorization:`Bearer ${a}`,"content-type":`application/json`},body:JSON.stringify({subject:e,options:t}),signal:s});if(!d(n)){let e=n.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(n.status!==200)throw Error(`Failed to read events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw Error(`Failed to read events.`);for await(let e of f(n.body,s)){if(m(e))throw Error(`${e.payload.error}.`);if(p(e)){yield i(e.payload);continue}throw Error(`Failed to read events.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{l&&l(),c&&o.abort()}})()}runEventQlQuery(e,t){let n=this.#n(`/api/v1/run-eventql-query`),r=this.#t;return(async function*(){let i=new AbortController,a=t??i.signal,o=!t,s;if(t&&!t.aborted){let e=()=>i.abort();t.addEventListener(`abort`,e,{once:!0}),s=()=>t.removeEventListener(`abort`,e)}try{let t=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${r}`,"content-type":`application/json`},body:JSON.stringify({query:e}),signal:a});if(!d(t)){let e=t.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(t.status!==200)throw Error(`Failed to run EventQL query, got HTTP status code '${t.status}', expected '200'.`);if(!t.body)throw Error(`Failed to run EventQL query.`);for await(let e of f(t.body,a))if(!_(e)){if(m(e))throw Error(`${e.payload.error}.`);if(v(e)){yield e.payload;continue}throw Error(`Failed to run EventQL query.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{s&&s(),o&&i.abort()}})()}observeEvents(e,t,n){let r=this.#n(`/api/v1/observe-events`),a=this.#t;return(async function*(){let o=new AbortController,s=n??o.signal,c=!n,l;if(n&&!n.aborted){let e=()=>o.abort();n.addEventListener(`abort`,e,{once:!0}),l=()=>n.removeEventListener(`abort`,e)}try{let n=await fetch(r,{method:`post`,headers:{authorization:`Bearer ${a}`,"content-type":`application/json`},body:JSON.stringify({subject:e,options:t}),signal:s});if(!d(n)){let e=n.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(n.status!==200)throw Error(`Failed to observe events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw Error(`Failed to observe events.`);for await(let e of f(n.body,s))if(!_(e)){if(m(e))throw Error(`${e.payload.error}.`);if(p(e)){yield i(e.payload);continue}throw Error(`Failed to observe events.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{l&&l(),c&&o.abort()}})()}async registerEventSchema(e,t){let n=this.#n(`/api/v1/register-event-schema`),r=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${this.#t}`,"content-type":`application/json`},body:JSON.stringify({eventType:e,schema:t})});if(!d(r))throw Error(`Server must be EventSourcingDB.`);if(r.status!==200)throw Error(`Failed to register event schema, got HTTP status code '${r.status}', expected '200'.`)}readSubjects(e,t){let n=this.#n(`/api/v1/read-subjects`),r=this.#t;return(async function*(){let i=new AbortController,a=t??i.signal,o=!t,s;if(t&&!t.aborted){let e=()=>i.abort();t.addEventListener(`abort`,e,{once:!0}),s=()=>t.removeEventListener(`abort`,e)}try{let t=await fetch(n,{method:`post`,headers:{authorization:`Bearer ${r}`,"content-type":`application/json`},body:JSON.stringify({baseSubject:e}),signal:a});if(!d(t)){let e=t.headers.get(`Server`);throw Error(e?`Server must be EventSourcingDB, got Server header: '${e}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(t.status!==200)throw Error(`Failed to read subjects, got HTTP status code '${t.status}', expected '200'.`);if(!t.body)throw Error(`Failed to read subjects.`);for await(let e of f(t.body,a)){if(m(e))throw Error(`${e.payload.error}.`);if(y(e)){yield e.payload.subject;continue}throw Error(`Failed to read subjects.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{s&&s(),o&&i.abort()}})()}async readEventType(e){let t=this.#n(`/api/v1/read-event-type`),n=await fetch(t,{method:`post`,headers:{authorization:`Bearer ${this.#t}`,"content-type":`application/json`},body:JSON.stringify({eventType:e})});if(!d(n))throw Error(`Server must be EventSourcingDB.`);if(n.status!==200)throw Error(`Failed to read event type, got HTTP status code '${n.status}', expected '200'.`);let r=await n.json();if(!o(r,{eventType:s,isPhantom:h}))throw Error(`Failed to parse response.`);return r}readEventTypes(e){let t=this.#n(`/api/v1/read-event-types`),n=this.#t;return(async function*(){let r=new AbortController,i=e??r.signal,a=!e,o;if(e&&!e.aborted){let t=()=>r.abort();e.addEventListener(`abort`,t,{once:!0}),o=()=>e.removeEventListener(`abort`,t)}try{let e=await fetch(t,{method:`post`,headers:{authorization:`Bearer ${n}`},signal:i});if(!d(e)){let t=e.headers.get(`Server`);throw Error(t?`Server must be EventSourcingDB, got Server header: '${t}'.`:`Server must be EventSourcingDB, but Server header is missing.`)}if(e.status!==200)throw Error(`Failed to read event types, got HTTP status code '${e.status}', expected '200'.`);if(!e.body)throw Error(`Failed to read event types.`);for await(let t of f(e.body,i))if(!_(t)){if(m(t))throw Error(`${t.payload.error}.`);if(g(t)){yield t.payload;continue}throw Error(`Failed to read event types.`)}}catch(e){if(e instanceof DOMException&&e.name===`AbortError`)return;throw e}finally{o&&o(),a&&r.abort()}})()}},x=class{#e=`thenativeweb/eventsourcingdb`;#t=`latest`;#n=3e3;#r=`secret`;#i;#a;withImageTag(e){return this.#t=e,this}withApiToken(e){return this.#r=e,this}withSigningKey(){let{privateKey:t}=e.generateKeyPairSync(`ed25519`);return this.#i=t,this}withPort(e){return this.#n=e,this}async start(){let e=[`run`,`--api-token`,this.#r,`--data-directory-temporary`,`--http-enabled`,`--https-enabled=false`],r=[];this.#i!==void 0&&(e.push(`--signing-key-file`),e.push(`/etc/esdb/signing-key.pem`),r.push({content:this.#i.export({format:`pem`,type:`pkcs8`}),target:`/etc/esdb/signing-key.pem`,mode:511})),this.#a=await new t(`${this.#e}:${this.#t}`).withExposedPorts(this.#n).withCommand(e).withCopyContentToContainer(r).withWaitStrategy(n.forHttp(`/api/v1/ping`,this.#n).withStartupTimeout(1e4)).start()}getHost(){if(this.#a===void 0)throw Error(`Container must be running.`);return this.#a.getHost()}getMappedPort(){if(this.#a===void 0)throw Error(`Container must be running.`);return this.#a.getMappedPort(this.#n)}getBaseUrl(){if(this.#a===void 0)throw Error(`Container must be running.`);let e=this.getHost(),t=this.getMappedPort();return new URL(`http://${e}:${t}`)}getApiToken(){return this.#r}getSigningKey(){if(this.#i===void 0)throw Error(`Signing key not set.`);return this.#i}getVerificationKey(){if(this.#i===void 0)throw Error(`Signing key not set.`);return e.createPublicKey(this.#i)}isRunning(){return this.#a!==void 0}async stop(){this.#a!==void 0&&(await this.#a.stop(),this.#a=void 0)}getClient(){return new b(this.getBaseUrl(),this.getApiToken())}};const S=e=>({type:`isEventQlQueryTrue`,payload:{query:e}}),C=(e,t)=>({type:`isSubjectOnEventId`,payload:{subject:e,eventId:t}}),w=e=>({type:`isSubjectPopulated`,payload:{subject:e}}),T=e=>({type:`isSubjectPristine`,payload:{subject:e}});export{b as Client,x as Container,S as isEventQlQueryTrue,C as isSubjectOnEventId,w as isSubjectPopulated,T as isSubjectPristine};
|