@event-driven-io/emmett-expressjs 0.20.2-alpha.4 → 0.20.2-alpha.5
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 +258 -125
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +625 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -6,9 +6,8 @@ var _express = require('express'); var _express2 = _interopRequireDefault(_expre
|
|
|
6
6
|
|
|
7
7
|
var _http = require('http'); var _http2 = _interopRequireDefault(_http);
|
|
8
8
|
|
|
9
|
-
// ../emmett/dist/chunk-
|
|
10
|
-
var
|
|
11
|
-
var s = (t) => typeof t == "number" && t === t;
|
|
9
|
+
// ../emmett/dist/chunk-AEEEXE2R.js
|
|
10
|
+
var isNumber = (val) => typeof val === "number" && val === val;
|
|
12
11
|
|
|
13
12
|
// ../emmett/dist/index.js
|
|
14
13
|
var _uuid = require('uuid');
|
|
@@ -28,14 +27,21 @@ var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefa
|
|
|
28
27
|
|
|
29
28
|
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
var notifyAboutNoActiveReadersStream = (onNoActiveReaderCallback, options = {}) => new NotifyAboutNoActiveReadersStream(onNoActiveReaderCallback, options);
|
|
33
|
+
var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfill.TransformStream {
|
|
34
|
+
constructor(onNoActiveReaderCallback, options = {}) {
|
|
35
|
+
super({
|
|
36
|
+
cancel: (reason) => {
|
|
37
|
+
console.log("Stream was canceled. Reason:", reason);
|
|
38
|
+
this.stopChecking();
|
|
39
|
+
}
|
|
40
|
+
});_class.prototype.__init.call(this);_class.prototype.__init2.call(this);;
|
|
41
|
+
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
42
|
+
this.streamId = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _2 => _2.streamId]), () => ( _uuid.v4.call(void 0, )));
|
|
43
|
+
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
44
|
+
this.startChecking(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.intervalCheckInMs]), () => ( 20)));
|
|
39
45
|
}
|
|
40
46
|
__init() {this.checkInterval = null}
|
|
41
47
|
|
|
@@ -43,148 +49,275 @@ var S = (_class = class extends _webstreamspolyfill.TransformStream {
|
|
|
43
49
|
get hasActiveSubscribers() {
|
|
44
50
|
return !this._isStopped;
|
|
45
51
|
}
|
|
46
|
-
startChecking(
|
|
52
|
+
startChecking(interval) {
|
|
47
53
|
this.checkInterval = setInterval(() => {
|
|
48
54
|
this.checkNoActiveReader();
|
|
49
|
-
},
|
|
55
|
+
}, interval);
|
|
50
56
|
}
|
|
51
57
|
stopChecking() {
|
|
52
|
-
|
|
58
|
+
if (!this.checkInterval) return;
|
|
59
|
+
clearInterval(this.checkInterval);
|
|
60
|
+
this.checkInterval = null;
|
|
61
|
+
this._isStopped = true;
|
|
62
|
+
this.onNoActiveReaderCallback(this);
|
|
53
63
|
}
|
|
54
64
|
checkNoActiveReader() {
|
|
55
|
-
!this.readable.locked && !this._isStopped
|
|
65
|
+
if (!this.readable.locked && !this._isStopped) {
|
|
66
|
+
this.stopChecking();
|
|
67
|
+
}
|
|
56
68
|
}
|
|
57
69
|
}, _class);
|
|
58
|
-
var
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
var asyncRetry = async (fn, opts) => {
|
|
71
|
+
if (opts === void 0 || opts.retries === 0) return fn();
|
|
72
|
+
return _asyncretry2.default.call(void 0,
|
|
73
|
+
async (bail) => {
|
|
74
|
+
try {
|
|
75
|
+
return await fn();
|
|
76
|
+
} catch (error2) {
|
|
77
|
+
if (_optionalChain([opts, 'optionalAccess', _4 => _4.shouldRetryError]) && !opts.shouldRetryError(error2)) {
|
|
78
|
+
bail(error2);
|
|
79
|
+
}
|
|
80
|
+
throw error2;
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
_nullishCoalesce(opts, () => ( { retries: 0 }))
|
|
84
|
+
);
|
|
85
|
+
};
|
|
86
|
+
var ParseError = class extends Error {
|
|
87
|
+
constructor(text) {
|
|
88
|
+
super(`Cannot parse! ${text}`);
|
|
63
89
|
}
|
|
64
|
-
}
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
90
|
+
};
|
|
91
|
+
var JSONParser = {
|
|
92
|
+
stringify: (value, options) => {
|
|
93
|
+
return JSON.stringify(
|
|
94
|
+
_optionalChain([options, 'optionalAccess', _5 => _5.map]) ? options.map(value) : value,
|
|
95
|
+
//TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
97
|
+
(_, v) => typeof v === "bigint" ? v.toString() : v
|
|
98
|
+
);
|
|
99
|
+
},
|
|
100
|
+
parse: (text, options) => {
|
|
101
|
+
const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess', _6 => _6.reviver]));
|
|
102
|
+
if (_optionalChain([options, 'optionalAccess', _7 => _7.typeCheck]) && !_optionalChain([options, 'optionalAccess', _8 => _8.typeCheck, 'call', _9 => _9(parsed)]))
|
|
103
|
+
throw new ParseError(text);
|
|
104
|
+
return _optionalChain([options, 'optionalAccess', _10 => _10.map]) ? options.map(parsed) : parsed;
|
|
68
105
|
}
|
|
69
106
|
};
|
|
70
|
-
var
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
var
|
|
107
|
+
var filter = (filter2) => new (0, _webstreamspolyfill.TransformStream)({
|
|
108
|
+
transform(chunk, controller) {
|
|
109
|
+
if (filter2(chunk)) {
|
|
110
|
+
controller.enqueue(chunk);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
var map = (map2) => new (0, _webstreamspolyfill.TransformStream)({
|
|
115
|
+
transform(chunk, controller) {
|
|
116
|
+
controller.enqueue(map2(chunk));
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
var reduce = (reducer, initialValue) => new ReduceTransformStream(reducer, initialValue);
|
|
120
|
+
var ReduceTransformStream = class extends _webstreamspolyfill.TransformStream {
|
|
83
121
|
|
|
84
122
|
|
|
85
|
-
constructor(
|
|
86
|
-
super({
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
123
|
+
constructor(reducer, initialValue) {
|
|
124
|
+
super({
|
|
125
|
+
transform: (chunk) => {
|
|
126
|
+
this.accumulator = this.reducer(this.accumulator, chunk);
|
|
127
|
+
},
|
|
128
|
+
flush: (controller) => {
|
|
129
|
+
controller.enqueue(this.accumulator);
|
|
130
|
+
controller.terminate();
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
this.accumulator = initialValue;
|
|
134
|
+
this.reducer = reducer;
|
|
91
135
|
}
|
|
92
136
|
};
|
|
93
|
-
var
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
137
|
+
var retryStream = (createSourceStream, handleChunk2, retryOptions = { forever: true, minTimeout: 25 }) => new (0, _webstreamspolyfill.TransformStream)({
|
|
138
|
+
start(controller) {
|
|
139
|
+
asyncRetry(
|
|
140
|
+
() => onRestream(createSourceStream, handleChunk2, controller),
|
|
141
|
+
retryOptions
|
|
142
|
+
).catch((error2) => {
|
|
143
|
+
controller.error(error2);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
var onRestream = async (createSourceStream, handleChunk2, controller) => {
|
|
148
|
+
const sourceStream = createSourceStream();
|
|
149
|
+
const reader = sourceStream.getReader();
|
|
100
150
|
try {
|
|
101
|
-
let
|
|
151
|
+
let done;
|
|
102
152
|
do {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
153
|
+
const result = await reader.read();
|
|
154
|
+
done = result.done;
|
|
155
|
+
await handleChunk2(result, controller);
|
|
156
|
+
if (done) {
|
|
157
|
+
controller.terminate();
|
|
158
|
+
}
|
|
159
|
+
} while (!done);
|
|
106
160
|
} finally {
|
|
107
|
-
|
|
161
|
+
reader.releaseLock();
|
|
108
162
|
}
|
|
109
163
|
};
|
|
110
|
-
var
|
|
111
|
-
var
|
|
164
|
+
var skip = (limit) => new SkipTransformStream(limit);
|
|
165
|
+
var SkipTransformStream = (_class2 = class extends _webstreamspolyfill.TransformStream {
|
|
112
166
|
__init3() {this.count = 0}
|
|
113
167
|
|
|
114
|
-
constructor(
|
|
115
|
-
super({
|
|
116
|
-
|
|
117
|
-
|
|
168
|
+
constructor(skip2) {
|
|
169
|
+
super({
|
|
170
|
+
transform: (chunk, controller) => {
|
|
171
|
+
this.count++;
|
|
172
|
+
if (this.count > this.skip) {
|
|
173
|
+
controller.enqueue(chunk);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});_class2.prototype.__init3.call(this);;
|
|
177
|
+
this.skip = skip2;
|
|
118
178
|
}
|
|
119
179
|
}, _class2);
|
|
120
|
-
var
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
180
|
+
var stopAfter = (stopCondition) => new (0, _webstreamspolyfill.TransformStream)({
|
|
181
|
+
transform(chunk, controller) {
|
|
182
|
+
controller.enqueue(chunk);
|
|
183
|
+
if (stopCondition(chunk)) {
|
|
184
|
+
controller.terminate();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
var stopOn = (stopCondition) => new (0, _webstreamspolyfill.TransformStream)({
|
|
189
|
+
async transform(chunk, controller) {
|
|
190
|
+
if (!stopCondition(chunk)) {
|
|
191
|
+
controller.enqueue(chunk);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
await Promise.resolve();
|
|
195
|
+
controller.terminate();
|
|
127
196
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
var
|
|
131
|
-
var C = (_class3 = class extends _webstreamspolyfill.TransformStream {
|
|
197
|
+
});
|
|
198
|
+
var take = (limit) => new TakeTransformStream(limit);
|
|
199
|
+
var TakeTransformStream = (_class3 = class extends _webstreamspolyfill.TransformStream {
|
|
132
200
|
__init4() {this.count = 0}
|
|
133
201
|
|
|
134
|
-
constructor(
|
|
135
|
-
super({
|
|
136
|
-
|
|
137
|
-
|
|
202
|
+
constructor(limit) {
|
|
203
|
+
super({
|
|
204
|
+
transform: (chunk, controller) => {
|
|
205
|
+
if (this.count < this.limit) {
|
|
206
|
+
this.count++;
|
|
207
|
+
controller.enqueue(chunk);
|
|
208
|
+
} else {
|
|
209
|
+
controller.terminate();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});_class3.prototype.__init4.call(this);;
|
|
213
|
+
this.limit = limit;
|
|
138
214
|
}
|
|
139
215
|
}, _class3);
|
|
140
|
-
var
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
216
|
+
var waitAtMost = (waitTimeInMs) => new (0, _webstreamspolyfill.TransformStream)({
|
|
217
|
+
start(controller) {
|
|
218
|
+
const timeoutId = setTimeout(() => {
|
|
219
|
+
controller.terminate();
|
|
220
|
+
}, waitTimeInMs);
|
|
221
|
+
const originalTerminate = controller.terminate.bind(controller);
|
|
222
|
+
controller.terminate = () => {
|
|
223
|
+
clearTimeout(timeoutId);
|
|
224
|
+
originalTerminate();
|
|
225
|
+
};
|
|
226
|
+
},
|
|
227
|
+
transform(chunk, controller) {
|
|
228
|
+
controller.enqueue(chunk);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
var streamTransformations = {
|
|
232
|
+
filter,
|
|
233
|
+
take,
|
|
234
|
+
TakeTransformStream,
|
|
235
|
+
skip,
|
|
236
|
+
SkipTransformStream,
|
|
237
|
+
map,
|
|
238
|
+
notifyAboutNoActiveReadersStream,
|
|
239
|
+
NotifyAboutNoActiveReadersStream,
|
|
240
|
+
reduce,
|
|
241
|
+
ReduceTransformStream,
|
|
242
|
+
retry: retryStream,
|
|
243
|
+
stopAfter,
|
|
244
|
+
stopOn,
|
|
245
|
+
waitAtMost
|
|
246
|
+
};
|
|
247
|
+
var { retry: retry2 } = streamTransformations;
|
|
248
|
+
var AssertionError = class extends Error {
|
|
249
|
+
constructor(message) {
|
|
250
|
+
super(message);
|
|
155
251
|
}
|
|
156
252
|
};
|
|
157
|
-
var
|
|
158
|
-
|
|
159
|
-
|
|
253
|
+
var isSubset = (superObj, subObj) => {
|
|
254
|
+
const sup = superObj;
|
|
255
|
+
const sub = subObj;
|
|
256
|
+
assertOk(sup);
|
|
257
|
+
assertOk(sub);
|
|
258
|
+
return Object.keys(sub).every((ele) => {
|
|
259
|
+
if (typeof sub[ele] == "object") {
|
|
260
|
+
return isSubset(sup[ele], sub[ele]);
|
|
261
|
+
}
|
|
262
|
+
return sub[ele] === sup[ele];
|
|
263
|
+
});
|
|
160
264
|
};
|
|
161
|
-
var
|
|
162
|
-
throw new
|
|
265
|
+
var assertFails = (message) => {
|
|
266
|
+
throw new AssertionError(_nullishCoalesce(message, () => ( "That should not ever happened, right?")));
|
|
163
267
|
};
|
|
164
|
-
var
|
|
165
|
-
if (!
|
|
166
|
-
|
|
268
|
+
var assertMatches = (actual, expected, message) => {
|
|
269
|
+
if (!isSubset(actual, expected))
|
|
270
|
+
throw new AssertionError(
|
|
271
|
+
_nullishCoalesce(message, () => ( `subObj:
|
|
272
|
+
${JSONParser.stringify(expected)}
|
|
167
273
|
is not subset of
|
|
168
|
-
${
|
|
274
|
+
${JSONParser.stringify(actual)}`))
|
|
275
|
+
);
|
|
169
276
|
};
|
|
170
|
-
function
|
|
171
|
-
if (!
|
|
277
|
+
function assertOk(obj, message) {
|
|
278
|
+
if (!obj) throw new AssertionError(_nullishCoalesce(message, () => ( `Condition is not truthy`)));
|
|
172
279
|
}
|
|
173
|
-
function
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
|
|
280
|
+
function assertEqual(expected, actual, message) {
|
|
281
|
+
if (expected !== actual)
|
|
282
|
+
throw new AssertionError(
|
|
283
|
+
`${_nullishCoalesce(message, () => ( "Objects are not equal"))}:
|
|
284
|
+
Expected: ${JSONParser.stringify(expected)}
|
|
285
|
+
Actual:${JSONParser.stringify(actual)}`
|
|
286
|
+
);
|
|
177
287
|
}
|
|
178
|
-
var
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
288
|
+
var WrapEventStore = (eventStore) => {
|
|
289
|
+
const appendedEvents = /* @__PURE__ */ new Map();
|
|
290
|
+
return {
|
|
291
|
+
async aggregateStream(streamName, options) {
|
|
292
|
+
return eventStore.aggregateStream(streamName, options);
|
|
293
|
+
},
|
|
294
|
+
readStream(streamName, options) {
|
|
295
|
+
return eventStore.readStream(streamName, options);
|
|
296
|
+
},
|
|
297
|
+
appendToStream: async (streamName, events, options) => {
|
|
298
|
+
const result = await eventStore.appendToStream(
|
|
299
|
+
streamName,
|
|
300
|
+
events,
|
|
301
|
+
options
|
|
302
|
+
);
|
|
303
|
+
const currentStream = _nullishCoalesce(appendedEvents.get(streamName), () => ( [streamName, []]));
|
|
304
|
+
appendedEvents.set(streamName, [
|
|
305
|
+
streamName,
|
|
306
|
+
[...currentStream[1], ...events]
|
|
307
|
+
]);
|
|
308
|
+
return result;
|
|
309
|
+
},
|
|
310
|
+
appendedEvents,
|
|
311
|
+
setup: async (streamName, events) => {
|
|
312
|
+
return eventStore.appendToStream(streamName, events);
|
|
313
|
+
}
|
|
314
|
+
// streamEvents: (): ReadableStream<
|
|
315
|
+
// // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
316
|
+
// ReadEvent<Event, ReadEventMetadataType> | GlobalSubscriptionEvent
|
|
317
|
+
// > => {
|
|
318
|
+
// return eventStore.streamEvents();
|
|
319
|
+
// },
|
|
320
|
+
};
|
|
188
321
|
};
|
|
189
322
|
|
|
190
323
|
// src/middlewares/problemDetailsMiddleware.ts
|
|
@@ -197,7 +330,7 @@ var problemDetailsMiddleware = (mapError) => (error, request, response, _next) =
|
|
|
197
330
|
};
|
|
198
331
|
var defaulErrorToProblemDetailsMapping = (error) => {
|
|
199
332
|
let statusCode = 500;
|
|
200
|
-
if ("errorCode" in error &&
|
|
333
|
+
if ("errorCode" in error && isNumber(error.errorCode) && error.errorCode >= 100 && error.errorCode < 600) {
|
|
201
334
|
statusCode = error.errorCode;
|
|
202
335
|
}
|
|
203
336
|
return new (0, _httpproblemdetails.ProblemDocument)({
|
|
@@ -364,7 +497,7 @@ var ApiE2ESpecification = {
|
|
|
364
497
|
for: (getEventStore, getApplication2) => {
|
|
365
498
|
{
|
|
366
499
|
return (...givenRequests) => {
|
|
367
|
-
const eventStore =
|
|
500
|
+
const eventStore = WrapEventStore(getEventStore());
|
|
368
501
|
const application = getApplication2(eventStore);
|
|
369
502
|
return {
|
|
370
503
|
when: (setupRequest) => {
|
|
@@ -404,9 +537,9 @@ var expectNewEvents = (streamId, events) => {
|
|
|
404
537
|
};
|
|
405
538
|
var expectResponse = (statusCode, options) => (response) => {
|
|
406
539
|
const { body, headers } = _nullishCoalesce(options, () => ( {}));
|
|
407
|
-
|
|
408
|
-
if (body)
|
|
409
|
-
if (headers)
|
|
540
|
+
assertEqual(statusCode, response.statusCode, "Response code doesn't match");
|
|
541
|
+
if (body) assertMatches(response.body, body);
|
|
542
|
+
if (headers) assertMatches(response.headers, headers);
|
|
410
543
|
};
|
|
411
544
|
var expectError = (errorCode, problemDetails) => expectResponse(
|
|
412
545
|
errorCode,
|
|
@@ -416,7 +549,7 @@ var ApiSpecification = {
|
|
|
416
549
|
for: (getEventStore, getApplication2) => {
|
|
417
550
|
{
|
|
418
551
|
return (...givenStreams) => {
|
|
419
|
-
const eventStore =
|
|
552
|
+
const eventStore = WrapEventStore(getEventStore());
|
|
420
553
|
const application = getApplication2(eventStore);
|
|
421
554
|
return {
|
|
422
555
|
when: (setupRequest) => {
|
|
@@ -431,15 +564,15 @@ var ApiSpecification = {
|
|
|
431
564
|
const response = await handle();
|
|
432
565
|
if (typeof verify === "function") {
|
|
433
566
|
const succeeded = verify(response);
|
|
434
|
-
if (succeeded === false)
|
|
567
|
+
if (succeeded === false) assertFails();
|
|
435
568
|
} else if (Array.isArray(verify)) {
|
|
436
569
|
const [first, ...rest] = verify;
|
|
437
570
|
if (typeof first === "function") {
|
|
438
571
|
const succeeded = first(response);
|
|
439
|
-
if (succeeded === false)
|
|
572
|
+
if (succeeded === false) assertFails();
|
|
440
573
|
}
|
|
441
574
|
const events = typeof first === "function" ? rest : verify;
|
|
442
|
-
|
|
575
|
+
assertMatches(
|
|
443
576
|
Array.from(eventStore.appendedEvents.values()),
|
|
444
577
|
events
|
|
445
578
|
);
|