@event-driven-io/emmett-expressjs 0.36.0 → 0.38.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -8,15 +8,27 @@ import http from "http";
8
8
 
9
9
  // ../emmett/dist/chunk-BQYVGGNE.js
10
10
  var isNumber = (val) => typeof val === "number" && val === val;
11
+ var isString = (val) => typeof val === "string";
12
+ var EmmettError = class _EmmettError extends Error {
13
+ errorCode;
14
+ constructor(options) {
15
+ const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : 500;
16
+ const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Emmett processing`;
17
+ super(message);
18
+ this.errorCode = errorCode;
19
+ Object.setPrototypeOf(this, _EmmettError.prototype);
20
+ }
21
+ };
11
22
 
12
23
  // ../emmett/dist/index.js
13
- import { v4 as uuid3 } from "uuid";
24
+ import { v4 as uuid5 } from "uuid";
14
25
  import { TransformStream } from "web-streams-polyfill";
15
26
  import { v4 as uuid2 } from "uuid";
16
27
  import { v4 as uuid } from "uuid";
17
28
  import { TransformStream as TransformStream2 } from "web-streams-polyfill";
29
+ import { v7 as uuid3 } from "uuid";
18
30
  import retry from "async-retry";
19
- import { v7 as uuid4 } from "uuid";
31
+ import { v4 as uuid4 } from "uuid";
20
32
  import { ReadableStream } from "web-streams-polyfill";
21
33
  import "web-streams-polyfill";
22
34
  import { TransformStream as TransformStream3 } from "web-streams-polyfill";
@@ -68,22 +80,6 @@ var NotifyAboutNoActiveReadersStream = class extends TransformStream2 {
68
80
  }
69
81
  }
70
82
  };
71
- var asyncRetry = async (fn, opts) => {
72
- if (opts === void 0 || opts.retries === 0) return fn();
73
- return retry(
74
- async (bail) => {
75
- try {
76
- return await fn();
77
- } catch (error2) {
78
- if (opts?.shouldRetryError && !opts.shouldRetryError(error2)) {
79
- bail(error2);
80
- }
81
- throw error2;
82
- }
83
- },
84
- opts ?? { retries: 0 }
85
- );
86
- };
87
83
  var ParseError = class extends Error {
88
84
  constructor(text) {
89
85
  super(`Cannot parse! ${text}`);
@@ -105,6 +101,107 @@ var JSONParser = {
105
101
  return options?.map ? options.map(parsed) : parsed;
106
102
  }
107
103
  };
104
+ var asyncRetry = async (fn, opts) => {
105
+ if (opts === void 0 || opts.retries === 0) return fn();
106
+ return retry(
107
+ async (bail) => {
108
+ try {
109
+ const result = await fn();
110
+ if (opts?.shouldRetryResult && opts.shouldRetryResult(result)) {
111
+ throw new EmmettError(
112
+ `Retrying because of result: ${JSONParser.stringify(result)}`
113
+ );
114
+ }
115
+ return result;
116
+ } catch (error2) {
117
+ if (opts?.shouldRetryError && !opts.shouldRetryError(error2)) {
118
+ bail(error2);
119
+ }
120
+ throw error2;
121
+ }
122
+ },
123
+ opts ?? { retries: 0 }
124
+ );
125
+ };
126
+ var AssertionError = class extends Error {
127
+ constructor(message2) {
128
+ super(message2);
129
+ }
130
+ };
131
+ var isSubset = (superObj, subObj) => {
132
+ const sup = superObj;
133
+ const sub = subObj;
134
+ assertOk(sup);
135
+ assertOk(sub);
136
+ return Object.keys(sub).every((ele) => {
137
+ if (typeof sub[ele] == "object") {
138
+ return isSubset(sup[ele], sub[ele]);
139
+ }
140
+ return sub[ele] === sup[ele];
141
+ });
142
+ };
143
+ var assertFails = (message2) => {
144
+ throw new AssertionError(message2 ?? "That should not ever happened, right?");
145
+ };
146
+ var assertMatches = (actual, expected, message2) => {
147
+ if (!isSubset(actual, expected))
148
+ throw new AssertionError(
149
+ message2 ?? `subObj:
150
+ ${JSONParser.stringify(expected)}
151
+ is not subset of
152
+ ${JSONParser.stringify(actual)}`
153
+ );
154
+ };
155
+ function assertOk(obj, message2) {
156
+ if (!obj) throw new AssertionError(message2 ?? `Condition is not truthy`);
157
+ }
158
+ function assertEqual(expected, actual, message2) {
159
+ if (expected !== actual)
160
+ throw new AssertionError(
161
+ `${message2 ?? "Objects are not equal"}:
162
+ Expected: ${JSONParser.stringify(expected)}
163
+ Actual: ${JSONParser.stringify(actual)}`
164
+ );
165
+ }
166
+ var WrapEventStore = (eventStore) => {
167
+ const appendedEvents = /* @__PURE__ */ new Map();
168
+ const wrapped = {
169
+ ...eventStore,
170
+ aggregateStream(streamName, options) {
171
+ return eventStore.aggregateStream(streamName, options);
172
+ },
173
+ async readStream(streamName, options) {
174
+ return await eventStore.readStream(
175
+ streamName,
176
+ options
177
+ );
178
+ },
179
+ appendToStream: async (streamName, events, options) => {
180
+ const result = await eventStore.appendToStream(
181
+ streamName,
182
+ events,
183
+ options
184
+ );
185
+ const currentStream = appendedEvents.get(streamName) ?? [streamName, []];
186
+ appendedEvents.set(streamName, [
187
+ streamName,
188
+ [...currentStream[1], ...events]
189
+ ]);
190
+ return result;
191
+ },
192
+ appendedEvents,
193
+ setup: async (streamName, events) => {
194
+ return eventStore.appendToStream(streamName, events);
195
+ }
196
+ // streamEvents: (): ReadableStream<
197
+ // // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
198
+ // ReadEvent<Event, ReadEventMetadataType> | GlobalSubscriptionEvent
199
+ // > => {
200
+ // return eventStore.streamEvents();
201
+ // },
202
+ };
203
+ return wrapped;
204
+ };
108
205
  var filter = (filter2) => new TransformStream3({
109
206
  transform(chunk, controller) {
110
207
  if (filter2(chunk)) {
@@ -246,85 +343,6 @@ var streamTransformations = {
246
343
  waitAtMost
247
344
  };
248
345
  var { retry: retry2 } = streamTransformations;
249
- var AssertionError = class extends Error {
250
- constructor(message2) {
251
- super(message2);
252
- }
253
- };
254
- var isSubset = (superObj, subObj) => {
255
- const sup = superObj;
256
- const sub = subObj;
257
- assertOk(sup);
258
- assertOk(sub);
259
- return Object.keys(sub).every((ele) => {
260
- if (typeof sub[ele] == "object") {
261
- return isSubset(sup[ele], sub[ele]);
262
- }
263
- return sub[ele] === sup[ele];
264
- });
265
- };
266
- var assertFails = (message2) => {
267
- throw new AssertionError(message2 ?? "That should not ever happened, right?");
268
- };
269
- var assertMatches = (actual, expected, message2) => {
270
- if (!isSubset(actual, expected))
271
- throw new AssertionError(
272
- message2 ?? `subObj:
273
- ${JSONParser.stringify(expected)}
274
- is not subset of
275
- ${JSONParser.stringify(actual)}`
276
- );
277
- };
278
- function assertOk(obj, message2) {
279
- if (!obj) throw new AssertionError(message2 ?? `Condition is not truthy`);
280
- }
281
- function assertEqual(expected, actual, message2) {
282
- if (expected !== actual)
283
- throw new AssertionError(
284
- `${message2 ?? "Objects are not equal"}:
285
- Expected: ${JSONParser.stringify(expected)}
286
- Actual: ${JSONParser.stringify(actual)}`
287
- );
288
- }
289
- var WrapEventStore = (eventStore) => {
290
- const appendedEvents = /* @__PURE__ */ new Map();
291
- const wrapped = {
292
- ...eventStore,
293
- aggregateStream(streamName, options) {
294
- return eventStore.aggregateStream(streamName, options);
295
- },
296
- async readStream(streamName, options) {
297
- return await eventStore.readStream(
298
- streamName,
299
- options
300
- );
301
- },
302
- appendToStream: async (streamName, events, options) => {
303
- const result = await eventStore.appendToStream(
304
- streamName,
305
- events,
306
- options
307
- );
308
- const currentStream = appendedEvents.get(streamName) ?? [streamName, []];
309
- appendedEvents.set(streamName, [
310
- streamName,
311
- [...currentStream[1], ...events]
312
- ]);
313
- return result;
314
- },
315
- appendedEvents,
316
- setup: async (streamName, events) => {
317
- return eventStore.appendToStream(streamName, events);
318
- }
319
- // streamEvents: (): ReadableStream<
320
- // // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
321
- // ReadEvent<Event, ReadEventMetadataType> | GlobalSubscriptionEvent
322
- // > => {
323
- // return eventStore.streamEvents();
324
- // },
325
- };
326
- return wrapped;
327
- };
328
346
 
329
347
  // src/middlewares/problemDetailsMiddleware.ts
330
348
  import { ProblemDocument } from "http-problem-details";