@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.cjs CHANGED
@@ -8,6 +8,17 @@ var _http = require('http'); var _http2 = _interopRequireDefault(_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
+
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
24
  var _uuid = require('uuid');
@@ -15,6 +26,7 @@ var _webstreamspolyfill = require('web-streams-polyfill');
15
26
 
16
27
 
17
28
 
29
+
18
30
  var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefault(_asyncretry);
19
31
 
20
32
 
@@ -68,14 +80,41 @@ var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfi
68
80
  }
69
81
  }
70
82
  }, _class);
83
+ var ParseError = class extends Error {
84
+ constructor(text) {
85
+ super(`Cannot parse! ${text}`);
86
+ }
87
+ };
88
+ var JSONParser = {
89
+ stringify: (value, options) => {
90
+ return JSON.stringify(
91
+ _optionalChain([options, 'optionalAccess', _4 => _4.map]) ? options.map(value) : value,
92
+ //TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
93
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
94
+ (_, v) => typeof v === "bigint" ? v.toString() : v
95
+ );
96
+ },
97
+ parse: (text, options) => {
98
+ const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess', _5 => _5.reviver]));
99
+ if (_optionalChain([options, 'optionalAccess', _6 => _6.typeCheck]) && !_optionalChain([options, 'optionalAccess', _7 => _7.typeCheck, 'call', _8 => _8(parsed)]))
100
+ throw new ParseError(text);
101
+ return _optionalChain([options, 'optionalAccess', _9 => _9.map]) ? options.map(parsed) : parsed;
102
+ }
103
+ };
71
104
  var asyncRetry = async (fn, opts) => {
72
105
  if (opts === void 0 || opts.retries === 0) return fn();
73
106
  return _asyncretry2.default.call(void 0,
74
107
  async (bail) => {
75
108
  try {
76
- return await fn();
109
+ const result = await fn();
110
+ if (_optionalChain([opts, 'optionalAccess', _10 => _10.shouldRetryResult]) && opts.shouldRetryResult(result)) {
111
+ throw new EmmettError(
112
+ `Retrying because of result: ${JSONParser.stringify(result)}`
113
+ );
114
+ }
115
+ return result;
77
116
  } catch (error2) {
78
- if (_optionalChain([opts, 'optionalAccess', _4 => _4.shouldRetryError]) && !opts.shouldRetryError(error2)) {
117
+ if (_optionalChain([opts, 'optionalAccess', _11 => _11.shouldRetryError]) && !opts.shouldRetryError(error2)) {
79
118
  bail(error2);
80
119
  }
81
120
  throw error2;
@@ -84,26 +123,84 @@ var asyncRetry = async (fn, opts) => {
84
123
  _nullishCoalesce(opts, () => ( { retries: 0 }))
85
124
  );
86
125
  };
87
- var ParseError = class extends Error {
88
- constructor(text) {
89
- super(`Cannot parse! ${text}`);
126
+ var AssertionError = class extends Error {
127
+ constructor(message2) {
128
+ super(message2);
90
129
  }
91
130
  };
92
- var JSONParser = {
93
- stringify: (value, options) => {
94
- return JSON.stringify(
95
- _optionalChain([options, 'optionalAccess', _5 => _5.map]) ? options.map(value) : value,
96
- //TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
97
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
98
- (_, v) => typeof v === "bigint" ? v.toString() : v
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(_nullishCoalesce(message2, () => ( "That should not ever happened, right?")));
145
+ };
146
+ var assertMatches = (actual, expected, message2) => {
147
+ if (!isSubset(actual, expected))
148
+ throw new AssertionError(
149
+ _nullishCoalesce(message2, () => ( `subObj:
150
+ ${JSONParser.stringify(expected)}
151
+ is not subset of
152
+ ${JSONParser.stringify(actual)}`))
99
153
  );
100
- },
101
- parse: (text, options) => {
102
- const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess', _6 => _6.reviver]));
103
- if (_optionalChain([options, 'optionalAccess', _7 => _7.typeCheck]) && !_optionalChain([options, 'optionalAccess', _8 => _8.typeCheck, 'call', _9 => _9(parsed)]))
104
- throw new ParseError(text);
105
- return _optionalChain([options, 'optionalAccess', _10 => _10.map]) ? options.map(parsed) : parsed;
106
- }
154
+ };
155
+ function assertOk(obj, message2) {
156
+ if (!obj) throw new AssertionError(_nullishCoalesce(message2, () => ( `Condition is not truthy`)));
157
+ }
158
+ function assertEqual(expected, actual, message2) {
159
+ if (expected !== actual)
160
+ throw new AssertionError(
161
+ `${_nullishCoalesce(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 = _nullishCoalesce(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;
107
204
  };
108
205
  var filter = (filter2) => new (0, _webstreamspolyfill.TransformStream)({
109
206
  transform(chunk, controller) {
@@ -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(_nullishCoalesce(message2, () => ( "That should not ever happened, right?")));
268
- };
269
- var assertMatches = (actual, expected, message2) => {
270
- if (!isSubset(actual, expected))
271
- throw new AssertionError(
272
- _nullishCoalesce(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(_nullishCoalesce(message2, () => ( `Condition is not truthy`)));
280
- }
281
- function assertEqual(expected, actual, message2) {
282
- if (expected !== actual)
283
- throw new AssertionError(
284
- `${_nullishCoalesce(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 = _nullishCoalesce(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
  var _httpproblemdetails = require('http-problem-details');