@nimbus-cqrs/eventsourcingdb 2.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +85 -0
- package/README.md +26 -0
- package/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +57 -0
- package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts +700 -0
- package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.js +903 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts +14 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.js +65 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts +20 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.js +43 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts +44 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.js +47 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts +44 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.js +51 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts +31 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.js +37 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +5 -0
- package/esm/lib/client.d.ts +90 -0
- package/esm/lib/client.d.ts.map +1 -0
- package/esm/lib/client.js +112 -0
- package/esm/lib/eventMapping.d.ts +51 -0
- package/esm/lib/eventMapping.d.ts.map +1 -0
- package/esm/lib/eventMapping.js +71 -0
- package/esm/lib/eventObserver.d.ts +85 -0
- package/esm/lib/eventObserver.d.ts.map +1 -0
- package/esm/lib/eventObserver.js +154 -0
- package/esm/lib/readEvents.d.ts +16 -0
- package/esm/lib/readEvents.d.ts.map +1 -0
- package/esm/lib/readEvents.js +21 -0
- package/esm/lib/tracing.d.ts +37 -0
- package/esm/lib/tracing.d.ts.map +1 -0
- package/esm/lib/tracing.js +114 -0
- package/esm/lib/writeEvents.d.ts +11 -0
- package/esm/lib/writeEvents.d.ts.map +1 -0
- package/esm/lib/writeEvents.js +20 -0
- package/esm/package.json +3 -0
- package/package.json +35 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Type for a ULID generator function. */
|
|
2
|
+
export type ULID = (seedTime?: number) => string;
|
|
3
|
+
export declare const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
4
|
+
export declare const ENCODING_LEN: number;
|
|
5
|
+
export declare const TIME_MAX: number;
|
|
6
|
+
export declare const TIME_LEN = 10;
|
|
7
|
+
export declare const RANDOM_LEN = 16;
|
|
8
|
+
export declare const ULID_LEN: number;
|
|
9
|
+
export declare function encodeTime(timestamp: number): string;
|
|
10
|
+
export declare function encodeRandom(): string;
|
|
11
|
+
export declare function incrementBase32(str: string): string;
|
|
12
|
+
/** Generates a monotonically increasing ULID. */
|
|
13
|
+
export declare function monotonicFactory(encodeRand?: typeof encodeRandom): ULID;
|
|
14
|
+
//# sourceMappingURL=_util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_util.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/ulid/1.0.0/_util.ts"],"names":[],"mappings":"AAEA,0CAA0C;AAC1C,MAAM,MAAM,IAAI,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;AAIjD,eAAO,MAAM,QAAQ,qCAAqC,CAAC;AAC3D,eAAO,MAAM,YAAY,QAAkB,CAAC;AAC5C,eAAO,MAAM,QAAQ,QAAsB,CAAC;AAC5C,eAAO,MAAM,QAAQ,KAAK,CAAC;AAC3B,eAAO,MAAM,UAAU,KAAK,CAAC;AAC7B,eAAO,MAAM,QAAQ,QAAwB,CAAC;AAM9C,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAapD;AAED,wBAAgB,YAAY,IAAI,MAAM,CAOrC;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAkBnD;AAED,iDAAiD;AACjD,wBAAgB,gBAAgB,CAAC,UAAU,sBAAe,GAAG,IAAI,CAYhE"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
// These values should NEVER change. If
|
|
3
|
+
// they do, we're no longer making ulids!
|
|
4
|
+
export const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
|
|
5
|
+
export const ENCODING_LEN = ENCODING.length;
|
|
6
|
+
export const TIME_MAX = Math.pow(2, 48) - 1;
|
|
7
|
+
export const TIME_LEN = 10;
|
|
8
|
+
export const RANDOM_LEN = 16;
|
|
9
|
+
export const ULID_LEN = TIME_LEN + RANDOM_LEN;
|
|
10
|
+
function replaceCharAt(str, index, char) {
|
|
11
|
+
return str.substring(0, index) + char + str.substring(index + 1);
|
|
12
|
+
}
|
|
13
|
+
export function encodeTime(timestamp) {
|
|
14
|
+
if (!Number.isInteger(timestamp) || timestamp < 0 || timestamp > TIME_MAX) {
|
|
15
|
+
throw new RangeError(`Time must be a positive integer less than ${TIME_MAX}`);
|
|
16
|
+
}
|
|
17
|
+
let str = "";
|
|
18
|
+
for (let len = TIME_LEN; len > 0; len--) {
|
|
19
|
+
const mod = timestamp % ENCODING_LEN;
|
|
20
|
+
str = ENCODING[mod] + str;
|
|
21
|
+
timestamp = Math.floor(timestamp / ENCODING_LEN);
|
|
22
|
+
}
|
|
23
|
+
return str;
|
|
24
|
+
}
|
|
25
|
+
export function encodeRandom() {
|
|
26
|
+
let str = "";
|
|
27
|
+
const bytes = crypto.getRandomValues(new Uint8Array(RANDOM_LEN));
|
|
28
|
+
for (const byte of bytes) {
|
|
29
|
+
str += ENCODING[byte % ENCODING_LEN];
|
|
30
|
+
}
|
|
31
|
+
return str;
|
|
32
|
+
}
|
|
33
|
+
export function incrementBase32(str) {
|
|
34
|
+
let index = str.length;
|
|
35
|
+
let char;
|
|
36
|
+
let charIndex;
|
|
37
|
+
const maxCharIndex = ENCODING_LEN - 1;
|
|
38
|
+
while (--index >= 0) {
|
|
39
|
+
char = str[index];
|
|
40
|
+
charIndex = ENCODING.indexOf(char);
|
|
41
|
+
if (charIndex === -1) {
|
|
42
|
+
throw new TypeError("Incorrectly encoded string");
|
|
43
|
+
}
|
|
44
|
+
if (charIndex === maxCharIndex) {
|
|
45
|
+
str = replaceCharAt(str, index, ENCODING[0]);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
return replaceCharAt(str, index, ENCODING[charIndex + 1]);
|
|
49
|
+
}
|
|
50
|
+
throw new Error("Cannot increment this string");
|
|
51
|
+
}
|
|
52
|
+
/** Generates a monotonically increasing ULID. */
|
|
53
|
+
export function monotonicFactory(encodeRand = encodeRandom) {
|
|
54
|
+
let lastTime = 0;
|
|
55
|
+
let lastRandom;
|
|
56
|
+
return function ulid(seedTime = Date.now()) {
|
|
57
|
+
if (seedTime <= lastTime) {
|
|
58
|
+
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
|
|
59
|
+
return encodeTime(lastTime) + incrementedRandom;
|
|
60
|
+
}
|
|
61
|
+
lastTime = seedTime;
|
|
62
|
+
const newRandom = (lastRandom = encodeRand());
|
|
63
|
+
return encodeTime(seedTime) + newRandom;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the number of milliseconds since the Unix epoch that had passed when
|
|
3
|
+
* the ULID was generated. If the ULID is malformed, an error will be thrown.
|
|
4
|
+
*
|
|
5
|
+
* @example Decode the time from a ULID
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { decodeTime, ulid } from "@std/ulid";
|
|
8
|
+
* import { assertEquals } from "@std/assert";
|
|
9
|
+
*
|
|
10
|
+
* const timestamp = 150_000;
|
|
11
|
+
* const ulidString = ulid(timestamp);
|
|
12
|
+
*
|
|
13
|
+
* assertEquals(decodeTime(ulidString), timestamp);
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param ulid The ULID to extract the timestamp from.
|
|
17
|
+
* @returns The number of milliseconds since the Unix epoch that had passed when the ULID was generated.
|
|
18
|
+
*/
|
|
19
|
+
export declare function decodeTime(ulid: string): number;
|
|
20
|
+
//# sourceMappingURL=decode_time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode_time.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/ulid/1.0.0/decode_time.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAqB/C"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
// Copyright 2023 Yoshiya Hinosawa. All rights reserved. MIT license.
|
|
3
|
+
// Copyright 2017 Alizain Feerasta. All rights reserved. MIT license.
|
|
4
|
+
// This module is browser compatible.
|
|
5
|
+
import { ENCODING, ENCODING_LEN, TIME_LEN, TIME_MAX, ULID_LEN, } from "./_util.js";
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the number of milliseconds since the Unix epoch that had passed when
|
|
8
|
+
* the ULID was generated. If the ULID is malformed, an error will be thrown.
|
|
9
|
+
*
|
|
10
|
+
* @example Decode the time from a ULID
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { decodeTime, ulid } from "@std/ulid";
|
|
13
|
+
* import { assertEquals } from "@std/assert";
|
|
14
|
+
*
|
|
15
|
+
* const timestamp = 150_000;
|
|
16
|
+
* const ulidString = ulid(timestamp);
|
|
17
|
+
*
|
|
18
|
+
* assertEquals(decodeTime(ulidString), timestamp);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param ulid The ULID to extract the timestamp from.
|
|
22
|
+
* @returns The number of milliseconds since the Unix epoch that had passed when the ULID was generated.
|
|
23
|
+
*/
|
|
24
|
+
export function decodeTime(ulid) {
|
|
25
|
+
if (ulid.length !== ULID_LEN) {
|
|
26
|
+
throw new Error(`ULID must be exactly ${ULID_LEN} characters long`);
|
|
27
|
+
}
|
|
28
|
+
const time = ulid
|
|
29
|
+
.substring(0, TIME_LEN)
|
|
30
|
+
.split("")
|
|
31
|
+
.reverse()
|
|
32
|
+
.reduce((carry, char, index) => {
|
|
33
|
+
const encodingIndex = ENCODING.indexOf(char);
|
|
34
|
+
if (encodingIndex === -1) {
|
|
35
|
+
throw new Error(`Invalid ULID character found: ${char}`);
|
|
36
|
+
}
|
|
37
|
+
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
|
|
38
|
+
}, 0);
|
|
39
|
+
if (time > TIME_MAX) {
|
|
40
|
+
throw new RangeError(`ULID timestamp component exceeds maximum value of ${TIME_MAX}`);
|
|
41
|
+
}
|
|
42
|
+
return time;
|
|
43
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for generating and working with
|
|
3
|
+
* {@link https://github.com/ulid/spec | Universally Unique Lexicographically Sortable Identifiers (ULIDs)}.
|
|
4
|
+
*
|
|
5
|
+
* To generate a ULID use the {@linkcode ulid} function. This will generate a
|
|
6
|
+
* ULID based on the current time.
|
|
7
|
+
*
|
|
8
|
+
* ```ts no-assert
|
|
9
|
+
* import { ulid } from "@std/ulid";
|
|
10
|
+
*
|
|
11
|
+
* ulid(); // 01HYFKMDF3HVJ4J3JZW8KXPVTY
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* {@linkcode ulid} does not guarantee that the ULIDs will be strictly
|
|
15
|
+
* increasing for the same current time. If you need to guarantee that the ULIDs
|
|
16
|
+
* will be strictly increasing, even for the same current time, use the
|
|
17
|
+
* {@linkcode monotonicUlid} function.
|
|
18
|
+
*
|
|
19
|
+
* ```ts no-assert
|
|
20
|
+
* import { monotonicUlid } from "@std/ulid";
|
|
21
|
+
*
|
|
22
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAS
|
|
23
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAT
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Because each ULID encodes the time it was generated, you can extract the
|
|
27
|
+
* timestamp from a ULID using the {@linkcode decodeTime} function.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { decodeTime, ulid } from "@std/ulid";
|
|
31
|
+
* import { assertEquals } from "@std/assert";
|
|
32
|
+
*
|
|
33
|
+
* const timestamp = 150_000;
|
|
34
|
+
* const ulidString = ulid(timestamp);
|
|
35
|
+
*
|
|
36
|
+
* assertEquals(decodeTime(ulidString), timestamp);
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @module
|
|
40
|
+
*/
|
|
41
|
+
export * from "./decode_time.js";
|
|
42
|
+
export * from "./monotonic_ulid.js";
|
|
43
|
+
export * from "./ulid.js";
|
|
44
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/ulid/1.0.0/mod.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
// Copyright 2023 Yoshiya Hinosawa. All rights reserved. MIT license.
|
|
3
|
+
// Copyright 2017 Alizain Feerasta. All rights reserved. MIT license.
|
|
4
|
+
// This module is browser compatible.
|
|
5
|
+
/**
|
|
6
|
+
* Utilities for generating and working with
|
|
7
|
+
* {@link https://github.com/ulid/spec | Universally Unique Lexicographically Sortable Identifiers (ULIDs)}.
|
|
8
|
+
*
|
|
9
|
+
* To generate a ULID use the {@linkcode ulid} function. This will generate a
|
|
10
|
+
* ULID based on the current time.
|
|
11
|
+
*
|
|
12
|
+
* ```ts no-assert
|
|
13
|
+
* import { ulid } from "@std/ulid";
|
|
14
|
+
*
|
|
15
|
+
* ulid(); // 01HYFKMDF3HVJ4J3JZW8KXPVTY
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* {@linkcode ulid} does not guarantee that the ULIDs will be strictly
|
|
19
|
+
* increasing for the same current time. If you need to guarantee that the ULIDs
|
|
20
|
+
* will be strictly increasing, even for the same current time, use the
|
|
21
|
+
* {@linkcode monotonicUlid} function.
|
|
22
|
+
*
|
|
23
|
+
* ```ts no-assert
|
|
24
|
+
* import { monotonicUlid } from "@std/ulid";
|
|
25
|
+
*
|
|
26
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAS
|
|
27
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAT
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Because each ULID encodes the time it was generated, you can extract the
|
|
31
|
+
* timestamp from a ULID using the {@linkcode decodeTime} function.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { decodeTime, ulid } from "@std/ulid";
|
|
35
|
+
* import { assertEquals } from "@std/assert";
|
|
36
|
+
*
|
|
37
|
+
* const timestamp = 150_000;
|
|
38
|
+
* const ulidString = ulid(timestamp);
|
|
39
|
+
*
|
|
40
|
+
* assertEquals(decodeTime(ulidString), timestamp);
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @module
|
|
44
|
+
*/
|
|
45
|
+
export * from "./decode_time.js";
|
|
46
|
+
export * from "./monotonic_ulid.js";
|
|
47
|
+
export * from "./ulid.js";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a ULID that monotonically increases even for the same millisecond,
|
|
3
|
+
* optionally passing the current time. If the current time is not passed, it
|
|
4
|
+
* will default to `Date.now()`.
|
|
5
|
+
*
|
|
6
|
+
* Unlike the {@linkcode ulid} function, this function is guaranteed to return
|
|
7
|
+
* strictly increasing ULIDs, even for the same seed time, but only if the seed
|
|
8
|
+
* time only ever increases. If the seed time ever goes backwards, the ULID will
|
|
9
|
+
* still be generated, but it will not be guaranteed to be monotonic with
|
|
10
|
+
* previous ULIDs for that same seed time.
|
|
11
|
+
*
|
|
12
|
+
* @example Generate a monotonic ULID
|
|
13
|
+
* ```ts no-assert
|
|
14
|
+
* import { monotonicUlid } from "@std/ulid";
|
|
15
|
+
*
|
|
16
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAS
|
|
17
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAT
|
|
18
|
+
* monotonicUlid(); // 01HYFKHHX8H4BRY8BYHAV1BZ2T
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example Generate a monotonic ULID with a seed time
|
|
22
|
+
* ```ts no-assert
|
|
23
|
+
* import { monotonicUlid } from "@std/ulid";
|
|
24
|
+
*
|
|
25
|
+
* // Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
|
|
26
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1Q
|
|
27
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1R
|
|
28
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1S
|
|
29
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1T
|
|
30
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1U
|
|
31
|
+
*
|
|
32
|
+
* // A different timestamp will reset the random bits
|
|
33
|
+
* monotonicUlid(150001); // 0000004JFHJJ2Z7X64FN2B4F1P
|
|
34
|
+
*
|
|
35
|
+
* // A previous seed time will not guarantee ordering, and may result in a
|
|
36
|
+
* // ULID lower than one with the same seed time generated previously
|
|
37
|
+
* monotonicUlid(150000); // 0000004JFJ7XF6D76ES95SZR0X
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @param seedTime The time to base the ULID on, in milliseconds since the Unix epoch. Defaults to `Date.now()`.
|
|
41
|
+
* @returns A ULID that is guaranteed to be strictly increasing for the same seed time.
|
|
42
|
+
*/
|
|
43
|
+
export declare function monotonicUlid(seedTime?: number): string;
|
|
44
|
+
//# sourceMappingURL=monotonic_ulid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"monotonic_ulid.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,aAAa,CAAC,QAAQ,GAAE,MAAmB,GAAG,MAAM,CAEnE"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
// Copyright 2023 Yoshiya Hinosawa. All rights reserved. MIT license.
|
|
3
|
+
// Copyright 2017 Alizain Feerasta. All rights reserved. MIT license.
|
|
4
|
+
// This module is browser compatible.
|
|
5
|
+
import { monotonicFactory } from "./_util.js";
|
|
6
|
+
const defaultMonotonicUlid = monotonicFactory();
|
|
7
|
+
/**
|
|
8
|
+
* Generate a ULID that monotonically increases even for the same millisecond,
|
|
9
|
+
* optionally passing the current time. If the current time is not passed, it
|
|
10
|
+
* will default to `Date.now()`.
|
|
11
|
+
*
|
|
12
|
+
* Unlike the {@linkcode ulid} function, this function is guaranteed to return
|
|
13
|
+
* strictly increasing ULIDs, even for the same seed time, but only if the seed
|
|
14
|
+
* time only ever increases. If the seed time ever goes backwards, the ULID will
|
|
15
|
+
* still be generated, but it will not be guaranteed to be monotonic with
|
|
16
|
+
* previous ULIDs for that same seed time.
|
|
17
|
+
*
|
|
18
|
+
* @example Generate a monotonic ULID
|
|
19
|
+
* ```ts no-assert
|
|
20
|
+
* import { monotonicUlid } from "@std/ulid";
|
|
21
|
+
*
|
|
22
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAS
|
|
23
|
+
* monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAT
|
|
24
|
+
* monotonicUlid(); // 01HYFKHHX8H4BRY8BYHAV1BZ2T
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example Generate a monotonic ULID with a seed time
|
|
28
|
+
* ```ts no-assert
|
|
29
|
+
* import { monotonicUlid } from "@std/ulid";
|
|
30
|
+
*
|
|
31
|
+
* // Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
|
|
32
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1Q
|
|
33
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1R
|
|
34
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1S
|
|
35
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1T
|
|
36
|
+
* monotonicUlid(150000); // 0000004JFHJJ2Z7X64FN2B4F1U
|
|
37
|
+
*
|
|
38
|
+
* // A different timestamp will reset the random bits
|
|
39
|
+
* monotonicUlid(150001); // 0000004JFHJJ2Z7X64FN2B4F1P
|
|
40
|
+
*
|
|
41
|
+
* // A previous seed time will not guarantee ordering, and may result in a
|
|
42
|
+
* // ULID lower than one with the same seed time generated previously
|
|
43
|
+
* monotonicUlid(150000); // 0000004JFJ7XF6D76ES95SZR0X
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param seedTime The time to base the ULID on, in milliseconds since the Unix epoch. Defaults to `Date.now()`.
|
|
47
|
+
* @returns A ULID that is guaranteed to be strictly increasing for the same seed time.
|
|
48
|
+
*/
|
|
49
|
+
export function monotonicUlid(seedTime = Date.now()) {
|
|
50
|
+
return defaultMonotonicUlid(seedTime);
|
|
51
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a ULID, optionally based on a given timestamp. If the timestamp is
|
|
3
|
+
* not passed, it will default to `Date.now()`.
|
|
4
|
+
*
|
|
5
|
+
* Multiple calls to this function with the same seed time will not guarantee
|
|
6
|
+
* that the ULIDs will be strictly increasing, even if the seed time is the
|
|
7
|
+
* same. For that, use the {@linkcode monotonicUlid} function.
|
|
8
|
+
*
|
|
9
|
+
* @example Generate a ULID
|
|
10
|
+
* ```ts no-assert
|
|
11
|
+
* import { ulid } from "@std/ulid";
|
|
12
|
+
*
|
|
13
|
+
* ulid(); // 01HYFKMDF3HVJ4J3JZW8KXPVTY
|
|
14
|
+
* ulid(); // 01HYFKMDF3D2P7G502B9Z2VKV0
|
|
15
|
+
* ulid(); // 01HYFKMDZQ7JD17CRKDXQSZ3Z4
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example Generate a ULID with a seed time
|
|
19
|
+
* ```ts no-assert
|
|
20
|
+
* import { ulid } from "@std/ulid";
|
|
21
|
+
*
|
|
22
|
+
* ulid(150000); // 0000004JFG3EKDRE04TVVDJW7K
|
|
23
|
+
* ulid(150000); // 0000004JFGN0KHBH0447AK895X
|
|
24
|
+
* ulid(150000); // 0000004JFGMRDH0PN7SM8BZN06
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param seedTime The time to base the ULID on, in milliseconds since the Unix epoch. Defaults to `Date.now()`.
|
|
28
|
+
* @returns A ULID.
|
|
29
|
+
*/
|
|
30
|
+
export declare function ulid(seedTime?: number): string;
|
|
31
|
+
//# sourceMappingURL=ulid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ulid.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/ulid/1.0.0/ulid.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,IAAI,CAAC,QAAQ,GAAE,MAAmB,GAAG,MAAM,CAE1D"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
// Copyright 2023 Yoshiya Hinosawa. All rights reserved. MIT license.
|
|
3
|
+
// Copyright 2017 Alizain Feerasta. All rights reserved. MIT license.
|
|
4
|
+
// This module is browser compatible.
|
|
5
|
+
import { encodeRandom, encodeTime } from "./_util.js";
|
|
6
|
+
/**
|
|
7
|
+
* Generate a ULID, optionally based on a given timestamp. If the timestamp is
|
|
8
|
+
* not passed, it will default to `Date.now()`.
|
|
9
|
+
*
|
|
10
|
+
* Multiple calls to this function with the same seed time will not guarantee
|
|
11
|
+
* that the ULIDs will be strictly increasing, even if the seed time is the
|
|
12
|
+
* same. For that, use the {@linkcode monotonicUlid} function.
|
|
13
|
+
*
|
|
14
|
+
* @example Generate a ULID
|
|
15
|
+
* ```ts no-assert
|
|
16
|
+
* import { ulid } from "@std/ulid";
|
|
17
|
+
*
|
|
18
|
+
* ulid(); // 01HYFKMDF3HVJ4J3JZW8KXPVTY
|
|
19
|
+
* ulid(); // 01HYFKMDF3D2P7G502B9Z2VKV0
|
|
20
|
+
* ulid(); // 01HYFKMDZQ7JD17CRKDXQSZ3Z4
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example Generate a ULID with a seed time
|
|
24
|
+
* ```ts no-assert
|
|
25
|
+
* import { ulid } from "@std/ulid";
|
|
26
|
+
*
|
|
27
|
+
* ulid(150000); // 0000004JFG3EKDRE04TVVDJW7K
|
|
28
|
+
* ulid(150000); // 0000004JFGN0KHBH0447AK895X
|
|
29
|
+
* ulid(150000); // 0000004JFGMRDH0PN7SM8BZN06
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @param seedTime The time to base the ULID on, in milliseconds since the Unix epoch. Defaults to `Date.now()`.
|
|
33
|
+
* @returns A ULID.
|
|
34
|
+
*/
|
|
35
|
+
export function ulid(seedTime = Date.now()) {
|
|
36
|
+
return encodeTime(seedTime) + encodeRandom();
|
|
37
|
+
}
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Client } from 'eventsourcingdb';
|
|
2
|
+
import { type EventObserver } from './eventObserver.js';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for setting up the EventSourcingDB client.
|
|
5
|
+
*/
|
|
6
|
+
export type SetupEventSourcingDBClientInput = {
|
|
7
|
+
/**
|
|
8
|
+
* The URL of the EventSourcingDB server.
|
|
9
|
+
*/
|
|
10
|
+
url: URL;
|
|
11
|
+
/**
|
|
12
|
+
* The API token for authenticating with EventSourcingDB.
|
|
13
|
+
*/
|
|
14
|
+
apiToken: string;
|
|
15
|
+
/**
|
|
16
|
+
* An optional array of event observers to observe events.
|
|
17
|
+
*/
|
|
18
|
+
eventObservers?: EventObserver[];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Initialize and configure the EventSourcingDB client.
|
|
22
|
+
*
|
|
23
|
+
* This function creates a singleton client instance, verifies connectivity by pinging
|
|
24
|
+
* the server, and validates the provided API token. It should be called once at
|
|
25
|
+
* application startup before using {@link getEventSourcingDBClient}.
|
|
26
|
+
*
|
|
27
|
+
* Optionally, you can provide event observers that will start observing events
|
|
28
|
+
* in the background after the client is initialized.
|
|
29
|
+
*
|
|
30
|
+
* @param {SetupEventSourcingDBClientInput} options - The configuration options
|
|
31
|
+
* @param {URL} options.url - The URL of the EventSourcingDB server
|
|
32
|
+
* @param {string} options.apiToken - The API token for authentication
|
|
33
|
+
* @param {EventObserver[]} [options.eventObservers] - Optional array of event observers
|
|
34
|
+
*
|
|
35
|
+
* @throws {GenericException} If the connection to EventSourcingDB fails
|
|
36
|
+
* @throws {GenericException} If the API token is invalid
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { setupEventSourcingDBClient } from '@nimbus/eventsourcingdb';
|
|
41
|
+
* import type { Event } from 'eventsourcingdb';
|
|
42
|
+
*
|
|
43
|
+
* await setupEventSourcingDBClient({
|
|
44
|
+
* url: new URL(process.env.ESDB_URL ?? ''),
|
|
45
|
+
* apiToken: process.env.ESDB_API_TOKEN ?? '',
|
|
46
|
+
* eventObservers: [
|
|
47
|
+
* {
|
|
48
|
+
* subject: '/users',
|
|
49
|
+
* recursive: true,
|
|
50
|
+
* lowerBound: {
|
|
51
|
+
* id: 'last-processed-event-id',
|
|
52
|
+
* type: 'exclusive',
|
|
53
|
+
* },
|
|
54
|
+
* fromLatestEvent: {
|
|
55
|
+
* subject: '/users',
|
|
56
|
+
* type: 'io.nimbus.users.invited',
|
|
57
|
+
* ifEventIsMissing: 'read-everything',
|
|
58
|
+
* },
|
|
59
|
+
* eventHandler: async (event: Event) => {
|
|
60
|
+
* console.log('Received event:', event);
|
|
61
|
+
* },
|
|
62
|
+
* retryOptions: {
|
|
63
|
+
* maxRetries: 3,
|
|
64
|
+
* initialRetryDelayMs: 3000,
|
|
65
|
+
* },
|
|
66
|
+
* },
|
|
67
|
+
* ],
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare const setupEventSourcingDBClient: ({ url, apiToken, eventObservers }: SetupEventSourcingDBClientInput) => Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Get the EventSourcingDB client instance.
|
|
74
|
+
*
|
|
75
|
+
* Returns the singleton client instance that was created by {@link setupEventSourcingDBClient}.
|
|
76
|
+
* This function must be called after the client has been initialized.
|
|
77
|
+
*
|
|
78
|
+
* @returns {Client} The EventSourcingDB client instance
|
|
79
|
+
*
|
|
80
|
+
* @throws {GenericException} If the client has not been initialized via setupEventSourcingDBClient
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { getEventSourcingDBClient } from '@nimbus/eventsourcingdb';
|
|
85
|
+
*
|
|
86
|
+
* const client = getEventSourcingDBClient();
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare const getEventSourcingDBClient: () => Client;
|
|
90
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,KAAK,aAAa,EAAqB,MAAM,oBAAoB,CAAC;AAI3E;;GAEG;AACH,MAAM,MAAM,+BAA+B,GAAG;IAC1C;;OAEG;IACH,GAAG,EAAE,GAAG,CAAC;IACT;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,cAAc,CAAC,EAAE,aAAa,EAAE,CAAC;CACpC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,0BAA0B,GACnC,mCAAmC,+BAA+B,KACnE,OAAO,CAAC,IAAI,CA0Cd,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,wBAAwB,QAAO,MAQ3C,CAAC"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { GenericException, getLogger } from '../deps/jsr.io/@nimbus/core/2.0.0/src/index.js';
|
|
2
|
+
import { Client } from 'eventsourcingdb';
|
|
3
|
+
import { initEventObserver } from './eventObserver.js';
|
|
4
|
+
let eventSourcingDBClient = null;
|
|
5
|
+
/**
|
|
6
|
+
* Initialize and configure the EventSourcingDB client.
|
|
7
|
+
*
|
|
8
|
+
* This function creates a singleton client instance, verifies connectivity by pinging
|
|
9
|
+
* the server, and validates the provided API token. It should be called once at
|
|
10
|
+
* application startup before using {@link getEventSourcingDBClient}.
|
|
11
|
+
*
|
|
12
|
+
* Optionally, you can provide event observers that will start observing events
|
|
13
|
+
* in the background after the client is initialized.
|
|
14
|
+
*
|
|
15
|
+
* @param {SetupEventSourcingDBClientInput} options - The configuration options
|
|
16
|
+
* @param {URL} options.url - The URL of the EventSourcingDB server
|
|
17
|
+
* @param {string} options.apiToken - The API token for authentication
|
|
18
|
+
* @param {EventObserver[]} [options.eventObservers] - Optional array of event observers
|
|
19
|
+
*
|
|
20
|
+
* @throws {GenericException} If the connection to EventSourcingDB fails
|
|
21
|
+
* @throws {GenericException} If the API token is invalid
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { setupEventSourcingDBClient } from '@nimbus/eventsourcingdb';
|
|
26
|
+
* import type { Event } from 'eventsourcingdb';
|
|
27
|
+
*
|
|
28
|
+
* await setupEventSourcingDBClient({
|
|
29
|
+
* url: new URL(process.env.ESDB_URL ?? ''),
|
|
30
|
+
* apiToken: process.env.ESDB_API_TOKEN ?? '',
|
|
31
|
+
* eventObservers: [
|
|
32
|
+
* {
|
|
33
|
+
* subject: '/users',
|
|
34
|
+
* recursive: true,
|
|
35
|
+
* lowerBound: {
|
|
36
|
+
* id: 'last-processed-event-id',
|
|
37
|
+
* type: 'exclusive',
|
|
38
|
+
* },
|
|
39
|
+
* fromLatestEvent: {
|
|
40
|
+
* subject: '/users',
|
|
41
|
+
* type: 'io.nimbus.users.invited',
|
|
42
|
+
* ifEventIsMissing: 'read-everything',
|
|
43
|
+
* },
|
|
44
|
+
* eventHandler: async (event: Event) => {
|
|
45
|
+
* console.log('Received event:', event);
|
|
46
|
+
* },
|
|
47
|
+
* retryOptions: {
|
|
48
|
+
* maxRetries: 3,
|
|
49
|
+
* initialRetryDelayMs: 3000,
|
|
50
|
+
* },
|
|
51
|
+
* },
|
|
52
|
+
* ],
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const setupEventSourcingDBClient = async ({ url, apiToken, eventObservers }) => {
|
|
57
|
+
eventSourcingDBClient = new Client(url, apiToken);
|
|
58
|
+
try {
|
|
59
|
+
await eventSourcingDBClient.ping();
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
getLogger().critical({
|
|
63
|
+
category: 'Nimbus',
|
|
64
|
+
message: 'Could not connect to EventSourcingDB',
|
|
65
|
+
error: error,
|
|
66
|
+
});
|
|
67
|
+
throw new GenericException('Could not connect to EventSourcingDB');
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
await eventSourcingDBClient.verifyApiToken();
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
getLogger().error({
|
|
74
|
+
category: 'Nimbus',
|
|
75
|
+
message: 'Invalid API token. Please check your API token.',
|
|
76
|
+
error: error,
|
|
77
|
+
});
|
|
78
|
+
throw new GenericException('Invalid API token. Please check your API token.');
|
|
79
|
+
}
|
|
80
|
+
getLogger().info({
|
|
81
|
+
category: 'Nimbus',
|
|
82
|
+
message: 'EventSourcingDB client initialized successfully',
|
|
83
|
+
});
|
|
84
|
+
if (eventObservers?.length) {
|
|
85
|
+
for (const eventObserver of eventObservers) {
|
|
86
|
+
initEventObserver(eventObserver);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Get the EventSourcingDB client instance.
|
|
92
|
+
*
|
|
93
|
+
* Returns the singleton client instance that was created by {@link setupEventSourcingDBClient}.
|
|
94
|
+
* This function must be called after the client has been initialized.
|
|
95
|
+
*
|
|
96
|
+
* @returns {Client} The EventSourcingDB client instance
|
|
97
|
+
*
|
|
98
|
+
* @throws {GenericException} If the client has not been initialized via setupEventSourcingDBClient
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { getEventSourcingDBClient } from '@nimbus/eventsourcingdb';
|
|
103
|
+
*
|
|
104
|
+
* const client = getEventSourcingDBClient();
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export const getEventSourcingDBClient = () => {
|
|
108
|
+
if (!eventSourcingDBClient) {
|
|
109
|
+
throw new GenericException('EventSourcingDB client not yet initialized. Please call setupEventSourcingDBClient() first.');
|
|
110
|
+
}
|
|
111
|
+
return eventSourcingDBClient;
|
|
112
|
+
};
|