@event-driven-io/emmett-testcontainers 0.43.0-beta.2 → 0.43.0-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,362 +1,109 @@
1
- // src/eventStore/index.ts
2
- import { EventStoreDBClient as EventStoreDBClient2 } from "@eventstore/db-client";
3
-
4
- // ../emmett/dist/chunk-AZDDB5SF.js
5
- var isNumber = (val) => typeof val === "number" && val === val;
6
- var isString = (val) => typeof val === "string";
7
- var EmmettError = class _EmmettError extends Error {
8
- static Codes = {
9
- ValidationError: 400,
10
- IllegalStateError: 403,
11
- NotFoundError: 404,
12
- ConcurrencyError: 412,
13
- InternalServerError: 500
14
- };
15
- errorCode;
16
- constructor(options) {
17
- const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : _EmmettError.Codes.InternalServerError;
18
- const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Emmett processing`;
19
- super(message);
20
- this.errorCode = errorCode;
21
- Object.setPrototypeOf(this, _EmmettError.prototype);
22
- }
23
- static mapFrom(error) {
24
- if (_EmmettError.isInstanceOf(error)) {
25
- return error;
26
- }
27
- return new _EmmettError({
28
- errorCode: "errorCode" in error && error.errorCode !== void 0 && error.errorCode !== null ? error.errorCode : _EmmettError.Codes.InternalServerError,
29
- message: error.message ?? "An unknown error occurred"
30
- });
31
- }
32
- static isInstanceOf(error, errorCode) {
33
- return typeof error === "object" && error !== null && "errorCode" in error && isNumber(error.errorCode) && (errorCode === void 0 || error.errorCode === errorCode);
34
- }
35
- };
36
-
37
- // ../emmett/dist/index.js
38
- import { v4 as uuid4 } from "uuid";
39
- import { v7 as uuid } from "uuid";
40
- import retry from "async-retry";
41
- import { v7 as uuid2 } from "uuid";
42
- import { v4 as uuid3 } from "uuid";
43
- import { v7 as uuid5 } from "uuid";
44
- var emmettPrefix = "emt";
45
- var defaultTag = `${emmettPrefix}:default`;
46
- var unknownTag = `${emmettPrefix}:unknown`;
47
- var TaskProcessor = class {
48
- constructor(options) {
49
- this.options = options;
50
- }
51
- queue = [];
52
- isProcessing = false;
53
- activeTasks = 0;
54
- activeGroups = /* @__PURE__ */ new Set();
55
- enqueue(task, options) {
56
- if (this.queue.length >= this.options.maxQueueSize) {
57
- return Promise.reject(
58
- new EmmettError(
59
- "Too many pending connections. Please try again later."
60
- )
61
- );
62
- }
63
- return this.schedule(task, options);
64
- }
65
- waitForEndOfProcessing() {
66
- return this.schedule(({ ack }) => Promise.resolve(ack()));
67
- }
68
- schedule(task, options) {
69
- return promiseWithDeadline(
70
- (resolve, reject) => {
71
- const taskWithContext = () => {
72
- return new Promise((resolveTask, failTask) => {
73
- const taskPromise = task({
74
- ack: resolveTask
75
- });
76
- taskPromise.then(resolve).catch((err) => {
77
- failTask(err);
78
- reject(err);
79
- });
80
- });
81
- };
82
- this.queue.push({ task: taskWithContext, options });
83
- if (!this.isProcessing) {
84
- this.ensureProcessing();
85
- }
86
- },
87
- { deadline: this.options.maxTaskIdleTime }
88
- );
89
- }
90
- ensureProcessing() {
91
- if (this.isProcessing) return;
92
- this.isProcessing = true;
93
- this.processQueue();
94
- }
95
- processQueue() {
96
- try {
97
- while (this.activeTasks < this.options.maxActiveTasks && this.queue.length > 0) {
98
- const item = this.takeFirstAvailableItem();
99
- if (item === null) return;
100
- const groupId = item.options?.taskGroupId;
101
- if (groupId) {
102
- this.activeGroups.add(groupId);
103
- }
104
- this.activeTasks++;
105
- void this.executeItem(item);
106
- }
107
- } catch (error) {
108
- console.error(error);
109
- throw error;
110
- } finally {
111
- this.isProcessing = false;
112
- if (this.hasItemsToProcess() && this.activeTasks < this.options.maxActiveTasks) {
113
- this.ensureProcessing();
114
- }
115
- }
116
- }
117
- async executeItem({ task, options }) {
118
- try {
119
- await task();
120
- } finally {
121
- this.activeTasks--;
122
- if (options && options.taskGroupId) {
123
- this.activeGroups.delete(options.taskGroupId);
124
- }
125
- this.ensureProcessing();
126
- }
127
- }
128
- takeFirstAvailableItem = () => {
129
- const taskIndex = this.queue.findIndex(
130
- (item2) => !item2.options?.taskGroupId || !this.activeGroups.has(item2.options.taskGroupId)
131
- );
132
- if (taskIndex === -1) {
133
- return null;
134
- }
135
- const [item] = this.queue.splice(taskIndex, 1);
136
- return item ?? null;
137
- };
138
- hasItemsToProcess = () => this.queue.findIndex(
139
- (item) => !item.options?.taskGroupId || !this.activeGroups.has(item.options.taskGroupId)
140
- ) !== -1;
141
- };
142
- var DEFAULT_PROMISE_DEADLINE = 2147483647;
143
- var promiseWithDeadline = (executor, options) => {
144
- return new Promise((resolve, reject) => {
145
- let taskStarted = false;
146
- const maxWaitingTime = options.deadline || DEFAULT_PROMISE_DEADLINE;
147
- let timeoutId = setTimeout(() => {
148
- if (!taskStarted) {
149
- reject(
150
- new Error("Task was not started within the maximum waiting time")
151
- );
152
- }
153
- }, maxWaitingTime);
154
- executor((value) => {
155
- taskStarted = true;
156
- if (timeoutId) {
157
- clearTimeout(timeoutId);
158
- }
159
- timeoutId = null;
160
- resolve(value);
161
- }, reject);
162
- });
163
- };
164
- var InProcessLock = () => {
165
- const taskProcessor = new TaskProcessor({
166
- maxActiveTasks: Number.MAX_VALUE,
167
- maxQueueSize: Number.MAX_VALUE
168
- });
169
- const locks = /* @__PURE__ */ new Map();
170
- return {
171
- async acquire({ lockId }) {
172
- await new Promise((resolve, reject) => {
173
- taskProcessor.enqueue(
174
- ({ ack }) => {
175
- locks.set(lockId, ack);
176
- resolve();
177
- return Promise.resolve();
178
- },
179
- { taskGroupId: lockId }
180
- ).catch(reject);
181
- });
182
- },
183
- async tryAcquire({ lockId }) {
184
- if (locks.has(lockId)) {
185
- return false;
186
- }
187
- await this.acquire({ lockId });
188
- return true;
189
- },
190
- release({ lockId }) {
191
- const ack = locks.get(lockId);
192
- if (ack === void 0) {
193
- return Promise.resolve(true);
194
- }
195
- locks.delete(lockId);
196
- ack();
197
- return Promise.resolve(true);
198
- },
199
- async withAcquire(handle, { lockId }) {
200
- return taskProcessor.enqueue(
201
- async ({ ack }) => {
202
- locks.set(lockId, ack);
203
- try {
204
- return await handle();
205
- } finally {
206
- locks.delete(lockId);
207
- ack();
208
- }
209
- },
210
- { taskGroupId: lockId }
211
- );
212
- }
213
- };
214
- };
215
- var textEncoder = new TextEncoder();
216
-
217
- // src/eventStore/eventStoreDBContainer.ts
218
1
  import { EventStoreDBClient } from "@eventstore/db-client";
219
- import {
220
- AbstractStartedContainer,
221
- GenericContainer,
222
- Wait
223
- } from "testcontainers";
224
- var EVENTSTOREDB_PORT = 2113;
225
- var EVENTSTOREDB_IMAGE_NAME = "eventstore/eventstore";
226
- var EVENTSTOREDB_IMAGE_TAG = "24.10.0-bookworm-slim";
227
- var EVENTSTOREDB_ARM64_IMAGE_TAG = "24.10.0-alpha-arm64v8";
228
- var EVENTSTOREDB_DEFAULT_IMAGE = `${EVENTSTOREDB_IMAGE_NAME}:${process.arch !== "arm64" ? EVENTSTOREDB_IMAGE_TAG : EVENTSTOREDB_ARM64_IMAGE_TAG}`;
229
- var defaultEventStoreDBContainerOptions = {
230
- disableProjections: false,
231
- isSecure: false,
232
- useFileStorage: false,
233
- withReuse: false
2
+ import { InProcessLock } from "@event-driven-io/emmett";
3
+ import { AbstractStartedContainer, GenericContainer, Wait } from "testcontainers";
4
+ import { MongoDBContainer } from "@testcontainers/mongodb";
5
+ import { PostgreSqlContainer } from "@testcontainers/postgresql";
6
+
7
+ //#region src/eventStore/eventStoreDBContainer.ts
8
+ const EVENTSTOREDB_PORT = 2113;
9
+ const EVENTSTOREDB_IMAGE_NAME = "eventstore/eventstore";
10
+ const EVENTSTOREDB_IMAGE_TAG = "24.10.0-bookworm-slim";
11
+ const EVENTSTOREDB_ARM64_IMAGE_TAG = "24.10.0-alpha-arm64v8";
12
+ const EVENTSTOREDB_DEFAULT_IMAGE = `${EVENTSTOREDB_IMAGE_NAME}:${process.arch !== "arm64" ? EVENTSTOREDB_IMAGE_TAG : EVENTSTOREDB_ARM64_IMAGE_TAG}`;
13
+ const defaultEventStoreDBContainerOptions = {
14
+ disableProjections: false,
15
+ isSecure: false,
16
+ useFileStorage: false,
17
+ withReuse: false
234
18
  };
235
19
  var EventStoreDBContainer = class extends GenericContainer {
236
- constructor(image = EVENTSTOREDB_DEFAULT_IMAGE, options = defaultEventStoreDBContainerOptions) {
237
- super(image);
238
- const environment = {
239
- ...!options.disableProjections ? {
240
- EVENTSTORE_RUN_PROJECTIONS: "ALL"
241
- } : {},
242
- ...!options.isSecure ? {
243
- EVENTSTORE_INSECURE: "true"
244
- } : {},
245
- ...options.useFileStorage ? {
246
- EVENTSTORE_MEM_DB: "false",
247
- EVENTSTORE_DB: "/data/integration-tests"
248
- } : {},
249
- EVENTSTORE_CLUSTER_SIZE: "1",
250
- EVENTSTORE_START_STANDARD_PROJECTIONS: "true",
251
- EVENTSTORE_NODE_PORT: `${EVENTSTOREDB_PORT}`,
252
- EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP: "true"
253
- };
254
- this.withEnvironment(environment).withExposedPorts(EVENTSTOREDB_PORT);
255
- if (options.withReuse) this.withReuse();
256
- this.withWaitStrategy(
257
- Wait.forAll([Wait.forHealthCheck(), Wait.forListeningPorts()])
258
- );
259
- }
260
- async start() {
261
- return new StartedEventStoreDBContainer(await super.start());
262
- }
20
+ constructor(image = EVENTSTOREDB_DEFAULT_IMAGE, options = defaultEventStoreDBContainerOptions) {
21
+ super(image);
22
+ const environment = {
23
+ ...!options.disableProjections ? { EVENTSTORE_RUN_PROJECTIONS: "ALL" } : {},
24
+ ...!options.isSecure ? { EVENTSTORE_INSECURE: "true" } : {},
25
+ ...options.useFileStorage ? {
26
+ EVENTSTORE_MEM_DB: "false",
27
+ EVENTSTORE_DB: "/data/integration-tests"
28
+ } : {},
29
+ EVENTSTORE_CLUSTER_SIZE: "1",
30
+ EVENTSTORE_START_STANDARD_PROJECTIONS: "true",
31
+ EVENTSTORE_NODE_PORT: `${EVENTSTOREDB_PORT}`,
32
+ EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP: "true"
33
+ };
34
+ this.withEnvironment(environment).withExposedPorts(EVENTSTOREDB_PORT);
35
+ if (options.withReuse) this.withReuse();
36
+ this.withWaitStrategy(Wait.forAll([Wait.forHealthCheck(), Wait.forListeningPorts()]));
37
+ }
38
+ async start() {
39
+ return new StartedEventStoreDBContainer(await super.start());
40
+ }
263
41
  };
264
42
  var StartedEventStoreDBContainer = class extends AbstractStartedContainer {
265
- constructor(container2) {
266
- super(container2);
267
- }
268
- getConnectionString() {
269
- return `esdb://${this.getHost()}:${this.getMappedPort(2113)}?tls=false`;
270
- }
271
- getClient() {
272
- return EventStoreDBClient.connectionString(this.getConnectionString());
273
- }
43
+ constructor(container) {
44
+ super(container);
45
+ }
46
+ getConnectionString() {
47
+ return `esdb://${this.getHost()}:${this.getMappedPort(2113)}?tls=false`;
48
+ }
49
+ getClient() {
50
+ return EventStoreDBClient.connectionString(this.getConnectionString());
51
+ }
274
52
  };
275
- var container = null;
276
- var startedContainer = null;
277
- var startedCount = 0;
278
- var lock = InProcessLock();
279
- var getSharedEventStoreDBTestContainer = () => lock.withAcquire(
280
- async () => {
281
- if (startedContainer) return startedContainer;
282
- if (!container)
283
- container = new EventStoreDBContainer(EVENTSTOREDB_DEFAULT_IMAGE);
284
- startedContainer = await container.start();
285
- startedCount++;
286
- container.withLogConsumer(
287
- (stream) => stream.on("data", (line) => console.log(line)).on("err", (line) => console.error(line)).on("end", () => console.log("Stream closed"))
288
- );
289
- return startedContainer;
290
- },
291
- { lockId: "SharedEventStoreDBTestContainer" }
292
- );
293
- var getSharedTestEventStoreDBClient = async () => {
294
- return (await getSharedEventStoreDBTestContainer()).getClient();
53
+ let container = null;
54
+ let startedContainer = null;
55
+ let startedCount = 0;
56
+ const lock = InProcessLock();
57
+ const getSharedEventStoreDBTestContainer = () => lock.withAcquire(async () => {
58
+ if (startedContainer) return startedContainer;
59
+ if (!container) container = new EventStoreDBContainer(EVENTSTOREDB_DEFAULT_IMAGE);
60
+ startedContainer = await container.start();
61
+ startedCount++;
62
+ container.withLogConsumer((stream) => stream.on("data", (line) => console.log(line)).on("err", (line) => console.error(line)).on("end", () => console.log("Stream closed")));
63
+ return startedContainer;
64
+ }, { lockId: "SharedEventStoreDBTestContainer" });
65
+ const getSharedTestEventStoreDBClient = async () => {
66
+ return (await getSharedEventStoreDBTestContainer()).getClient();
295
67
  };
296
- var releaseSharedEventStoreDBTestContainer = () => lock.withAcquire(
297
- async () => {
298
- const containerToStop = startedContainer;
299
- if (containerToStop && --startedCount === 0) {
300
- try {
301
- startedContainer = null;
302
- container = null;
303
- await containerToStop.stop();
304
- } catch {
305
- }
306
- }
307
- },
308
- { lockId: "SharedEventStoreDBTestContainer" }
309
- );
68
+ const releaseSharedEventStoreDBTestContainer = () => lock.withAcquire(async () => {
69
+ const containerToStop = startedContainer;
70
+ if (containerToStop && --startedCount === 0) try {
71
+ startedContainer = null;
72
+ container = null;
73
+ await containerToStop.stop();
74
+ } catch {}
75
+ }, { lockId: "SharedEventStoreDBTestContainer" });
310
76
 
311
- // src/eventStore/index.ts
312
- var esdbContainer;
313
- var getEventStoreDBTestClient = async (useTestContainers = false) => {
314
- let connectionString;
315
- if (useTestContainers) {
316
- if (!esdbContainer)
317
- esdbContainer = await new EventStoreDBContainer().start();
318
- connectionString = esdbContainer.getConnectionString();
319
- } else {
320
- connectionString = "esdb://localhost:2113?tls=false";
321
- }
322
- return EventStoreDBClient2.connectionString(connectionString);
77
+ //#endregion
78
+ //#region src/eventStore/index.ts
79
+ let esdbContainer;
80
+ const getEventStoreDBTestClient = async (useTestContainers = false) => {
81
+ let connectionString;
82
+ if (useTestContainers) {
83
+ if (!esdbContainer) esdbContainer = await new EventStoreDBContainer().start();
84
+ connectionString = esdbContainer.getConnectionString();
85
+ } else connectionString = "esdb://localhost:2113?tls=false";
86
+ return EventStoreDBClient.connectionString(connectionString);
323
87
  };
324
88
 
325
- // src/mongodb/mongoDBContainer.ts
326
- import { MongoDBContainer } from "@testcontainers/mongodb";
327
- var getMongoDBContainer = (options = { version: "6.0.1" }) => {
328
- return new MongoDBContainer(`mongo:${options.version}`);
89
+ //#endregion
90
+ //#region src/mongodb/mongoDBContainer.ts
91
+ const getMongoDBContainer = (options = { version: "6.0.1" }) => {
92
+ return new MongoDBContainer(`mongo:${options.version}`);
329
93
  };
330
- var getMongoDBStartedContainer = async (options = { version: "6.0.1" }) => {
331
- const container2 = getMongoDBContainer(options);
332
- return container2.start();
94
+ const getMongoDBStartedContainer = async (options = { version: "6.0.1" }) => {
95
+ return getMongoDBContainer(options).start();
333
96
  };
334
97
 
335
- // src/postgresql/postgreSQLContainer.ts
336
- import { PostgreSqlContainer } from "@testcontainers/postgresql";
337
- var getPostgreSQLContainer = (options = { version: "18.1" }) => {
338
- return new PostgreSqlContainer(`postgres:${options.version}`);
339
- };
340
- var getPostgreSQLStartedContainer = async (options = { version: "18.1" }) => {
341
- const container2 = getPostgreSQLContainer(options);
342
- return container2.start();
98
+ //#endregion
99
+ //#region src/postgresql/postgreSQLContainer.ts
100
+ const getPostgreSQLContainer = (options = { version: "18.1" }) => {
101
+ return new PostgreSqlContainer(`postgres:${options.version}`);
343
102
  };
344
- export {
345
- EVENTSTOREDB_ARM64_IMAGE_TAG,
346
- EVENTSTOREDB_DEFAULT_IMAGE,
347
- EVENTSTOREDB_IMAGE_NAME,
348
- EVENTSTOREDB_IMAGE_TAG,
349
- EVENTSTOREDB_PORT,
350
- EventStoreDBContainer,
351
- StartedEventStoreDBContainer,
352
- defaultEventStoreDBContainerOptions,
353
- getEventStoreDBTestClient,
354
- getMongoDBContainer,
355
- getMongoDBStartedContainer,
356
- getPostgreSQLContainer,
357
- getPostgreSQLStartedContainer,
358
- getSharedEventStoreDBTestContainer,
359
- getSharedTestEventStoreDBClient,
360
- releaseSharedEventStoreDBTestContainer
103
+ const getPostgreSQLStartedContainer = async (options = { version: "18.1" }) => {
104
+ return getPostgreSQLContainer(options).start();
361
105
  };
106
+
107
+ //#endregion
108
+ export { EVENTSTOREDB_ARM64_IMAGE_TAG, EVENTSTOREDB_DEFAULT_IMAGE, EVENTSTOREDB_IMAGE_NAME, EVENTSTOREDB_IMAGE_TAG, EVENTSTOREDB_PORT, EventStoreDBContainer, StartedEventStoreDBContainer, defaultEventStoreDBContainerOptions, getEventStoreDBTestClient, getMongoDBContainer, getMongoDBStartedContainer, getPostgreSQLContainer, getPostgreSQLStartedContainer, getSharedEventStoreDBTestContainer, getSharedTestEventStoreDBClient, releaseSharedEventStoreDBTestContainer };
362
109
  //# sourceMappingURL=index.js.map