@muspellheim/shared 0.6.1 → 0.7.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.txt +1 -1
- package/README.md +11 -13
- package/dist/shared.d.ts +423 -0
- package/dist/shared.js +535 -0
- package/dist/shared.umd.cjs +1 -0
- package/package.json +27 -23
- package/.prettierignore +0 -3
- package/.prettierrc +0 -5
- package/deno.json +0 -15
- package/deno.mk +0 -68
- package/eslint.config.js +0 -23
- package/lib/assert.js +0 -15
- package/lib/browser/components.js +0 -165
- package/lib/browser/index.js +0 -3
- package/lib/color.js +0 -137
- package/lib/configurable-responses.js +0 -69
- package/lib/feature-toggle.js +0 -9
- package/lib/health.js +0 -510
- package/lib/index.js +0 -23
- package/lib/lang.js +0 -100
- package/lib/logging.js +0 -599
- package/lib/long-polling-client.js +0 -186
- package/lib/message-client.js +0 -68
- package/lib/messages.js +0 -68
- package/lib/metrics.js +0 -120
- package/lib/node/actuator-controller.js +0 -102
- package/lib/node/configuration-properties.js +0 -291
- package/lib/node/handler.js +0 -25
- package/lib/node/index.js +0 -9
- package/lib/node/logging.js +0 -60
- package/lib/node/long-polling.js +0 -83
- package/lib/node/sse-emitter.js +0 -104
- package/lib/node/static-files-controller.js +0 -15
- package/lib/output-tracker.js +0 -89
- package/lib/service-locator.js +0 -44
- package/lib/sse-client.js +0 -163
- package/lib/stop-watch.js +0 -54
- package/lib/store.js +0 -129
- package/lib/time.js +0 -445
- package/lib/util.js +0 -380
- package/lib/validation.js +0 -290
- package/lib/vector.js +0 -194
- package/lib/vitest/equality-testers.js +0 -19
- package/lib/vitest/index.js +0 -1
- package/lib/web-socket-client.js +0 -262
- package/tsconfig.json +0 -13
package/dist/shared.js
ADDED
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
class c {
|
|
2
|
+
/**
|
|
3
|
+
* Creates a clock using system the clock.
|
|
4
|
+
*
|
|
5
|
+
* @return A clock that uses the system clock.
|
|
6
|
+
*/
|
|
7
|
+
static system() {
|
|
8
|
+
return new c();
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a clock using a fixed date.
|
|
12
|
+
*
|
|
13
|
+
* @param date The fixed date of the clock.
|
|
14
|
+
* @return A clock that always returns a fixed date.
|
|
15
|
+
*/
|
|
16
|
+
static fixed(t) {
|
|
17
|
+
return new c(t);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a clock that returns a fixed offset from the given clock.
|
|
21
|
+
*
|
|
22
|
+
* @param clock The clock to offset from.
|
|
23
|
+
* @param offsetMillis The offset in milliseconds.
|
|
24
|
+
* @return A clock that returns a fixed offset from the given clock.
|
|
25
|
+
*/
|
|
26
|
+
static offset(t, e) {
|
|
27
|
+
return new c(t.millis() + e);
|
|
28
|
+
}
|
|
29
|
+
#s;
|
|
30
|
+
constructor(t) {
|
|
31
|
+
this.#s = t ? new Date(t) : void 0;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns the current timestamp of the clock.
|
|
35
|
+
*
|
|
36
|
+
* @return The current timestamp.
|
|
37
|
+
*/
|
|
38
|
+
date() {
|
|
39
|
+
return this.#s ? new Date(this.#s) : /* @__PURE__ */ new Date();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the current timestamp of the clock in milliseconds.
|
|
43
|
+
*
|
|
44
|
+
* @return The current timestamp in milliseconds.
|
|
45
|
+
*/
|
|
46
|
+
millis() {
|
|
47
|
+
return this.date().getTime();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class h {
|
|
51
|
+
/**
|
|
52
|
+
* Create a list of responses (by providing an array), or a single repeating
|
|
53
|
+
* response (by providing any other type). 'Name' is optional and used in
|
|
54
|
+
* error messages.
|
|
55
|
+
*
|
|
56
|
+
* @param responses A single response or an array of responses.
|
|
57
|
+
* @param name An optional name for the responses.
|
|
58
|
+
*/
|
|
59
|
+
static create(t, e) {
|
|
60
|
+
return new h(t, e);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Convert all properties in an object into ConfigurableResponse instances.
|
|
64
|
+
* For example, { a: 1 } becomes { a: ConfigurableResponses.create(1) }.
|
|
65
|
+
* 'Name' is optional and used in error messages.
|
|
66
|
+
*
|
|
67
|
+
* @param responseObject An object with single response or an array of responses.
|
|
68
|
+
* @param name An optional name for the responses.
|
|
69
|
+
*/
|
|
70
|
+
static mapObject(t, e) {
|
|
71
|
+
const r = Object.entries(t).map(([n, w]) => {
|
|
72
|
+
const v = e === void 0 ? void 0 : `${e}: ${n}`;
|
|
73
|
+
return [n, h.create(w, v)];
|
|
74
|
+
});
|
|
75
|
+
return Object.fromEntries(r);
|
|
76
|
+
}
|
|
77
|
+
#s;
|
|
78
|
+
#e;
|
|
79
|
+
/**
|
|
80
|
+
* Create a list of responses (by providing an array), or a single repeating
|
|
81
|
+
* response (by providing any other type). 'Name' is optional and used in
|
|
82
|
+
* error messages.
|
|
83
|
+
*
|
|
84
|
+
* @param responses A single response or an array of responses.
|
|
85
|
+
* @param name An optional name for the responses.
|
|
86
|
+
*/
|
|
87
|
+
constructor(t, e) {
|
|
88
|
+
this.#s = e == null ? "" : ` in ${e}`, this.#e = Array.isArray(t) ? [...t] : t;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get the next configured response. Throws an error when configured with a list
|
|
92
|
+
* of responses and no more responses remain.
|
|
93
|
+
*
|
|
94
|
+
* @return The next response.
|
|
95
|
+
*/
|
|
96
|
+
next() {
|
|
97
|
+
const t = Array.isArray(this.#e) ? this.#e.shift() : this.#e;
|
|
98
|
+
if (t === void 0)
|
|
99
|
+
throw new Error(`No more responses configured${this.#s}.`);
|
|
100
|
+
return t;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
class u {
|
|
104
|
+
/**
|
|
105
|
+
* Creates a tracker for a specific event of an event target.
|
|
106
|
+
*
|
|
107
|
+
* @param eventTarget The target to track.
|
|
108
|
+
* @param event The event name to track.
|
|
109
|
+
*/
|
|
110
|
+
static create(t, e) {
|
|
111
|
+
return new u(t, e);
|
|
112
|
+
}
|
|
113
|
+
#s;
|
|
114
|
+
#e;
|
|
115
|
+
#t;
|
|
116
|
+
#r;
|
|
117
|
+
/**
|
|
118
|
+
* Creates a tracker for a specific event of an event target.
|
|
119
|
+
*
|
|
120
|
+
* @param eventTarget The target to track.
|
|
121
|
+
* @param event The event name to track.
|
|
122
|
+
*/
|
|
123
|
+
constructor(t, e) {
|
|
124
|
+
this.#s = t, this.#e = e, this.#t = [], this.#r = (s) => this.#t.push(s.detail), this.#s.addEventListener(this.#e, this.#r);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns the tracked data.
|
|
128
|
+
*
|
|
129
|
+
* @return The tracked data.
|
|
130
|
+
*/
|
|
131
|
+
get data() {
|
|
132
|
+
return this.#t;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Clears the tracked data and returns the cleared data.
|
|
136
|
+
*
|
|
137
|
+
* @return The cleared data.
|
|
138
|
+
*/
|
|
139
|
+
clear() {
|
|
140
|
+
const t = [...this.#t];
|
|
141
|
+
return this.#t.length = 0, t;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Stops tracking.
|
|
145
|
+
*/
|
|
146
|
+
stop() {
|
|
147
|
+
this.#s.removeEventListener(this.#e, this.#r);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
class y {
|
|
151
|
+
isSuccess = !0;
|
|
152
|
+
}
|
|
153
|
+
class S {
|
|
154
|
+
isSuccess = !1;
|
|
155
|
+
errorMessage;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a failed status.
|
|
158
|
+
*
|
|
159
|
+
* @param errorMessage
|
|
160
|
+
*/
|
|
161
|
+
constructor(t) {
|
|
162
|
+
this.errorMessage = t;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const i = "message";
|
|
166
|
+
class f extends EventTarget {
|
|
167
|
+
log(...t) {
|
|
168
|
+
this.dispatchEvent(
|
|
169
|
+
new CustomEvent(i, {
|
|
170
|
+
detail: { level: "log", message: t }
|
|
171
|
+
})
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
error(...t) {
|
|
175
|
+
this.dispatchEvent(
|
|
176
|
+
new CustomEvent(i, {
|
|
177
|
+
detail: { level: "error", message: t }
|
|
178
|
+
})
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
warn(...t) {
|
|
182
|
+
this.dispatchEvent(
|
|
183
|
+
new CustomEvent(i, {
|
|
184
|
+
detail: { level: "warn", message: t }
|
|
185
|
+
})
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
info(...t) {
|
|
189
|
+
this.dispatchEvent(
|
|
190
|
+
new CustomEvent(i, {
|
|
191
|
+
detail: { level: "info", message: t }
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
debug(...t) {
|
|
196
|
+
this.dispatchEvent(
|
|
197
|
+
new CustomEvent(i, {
|
|
198
|
+
detail: { level: "debug", message: t }
|
|
199
|
+
})
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
trace(...t) {
|
|
203
|
+
this.dispatchEvent(
|
|
204
|
+
new CustomEvent(i, {
|
|
205
|
+
detail: { level: "trace", message: t }
|
|
206
|
+
})
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Tracks console messages.
|
|
211
|
+
*/
|
|
212
|
+
trackMessages() {
|
|
213
|
+
return new u(this, i);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function b(a) {
|
|
217
|
+
const t = h.create(a);
|
|
218
|
+
return async function() {
|
|
219
|
+
const e = t.next();
|
|
220
|
+
if (e instanceof Error)
|
|
221
|
+
throw e;
|
|
222
|
+
return new p(e);
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
class p {
|
|
226
|
+
#s;
|
|
227
|
+
#e;
|
|
228
|
+
#t;
|
|
229
|
+
constructor({ status: t, statusText: e, body: s = null }) {
|
|
230
|
+
this.#s = t, this.#e = e, this.#t = s;
|
|
231
|
+
}
|
|
232
|
+
get ok() {
|
|
233
|
+
return this.status >= 200 && this.status < 300;
|
|
234
|
+
}
|
|
235
|
+
get status() {
|
|
236
|
+
return this.#s;
|
|
237
|
+
}
|
|
238
|
+
get statusText() {
|
|
239
|
+
return this.#e;
|
|
240
|
+
}
|
|
241
|
+
async blob() {
|
|
242
|
+
if (this.#t == null)
|
|
243
|
+
return null;
|
|
244
|
+
if (this.#t instanceof Blob)
|
|
245
|
+
return this.#t;
|
|
246
|
+
throw new TypeError("Body is not a Blob.");
|
|
247
|
+
}
|
|
248
|
+
async json() {
|
|
249
|
+
const t = typeof this.#t == "string" ? this.#t : JSON.stringify(this.#t);
|
|
250
|
+
return Promise.resolve(JSON.parse(t));
|
|
251
|
+
}
|
|
252
|
+
async text() {
|
|
253
|
+
return this.#t == null ? "" : String(this.#t);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
class d extends EventTarget {
|
|
257
|
+
/**
|
|
258
|
+
* Creates an SSE client.
|
|
259
|
+
*
|
|
260
|
+
* @return A new SSE client.
|
|
261
|
+
*/
|
|
262
|
+
static create() {
|
|
263
|
+
return new d(EventSource);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Creates a nulled SSE client.
|
|
267
|
+
*
|
|
268
|
+
* @return A new SSE client.
|
|
269
|
+
*/
|
|
270
|
+
static createNull() {
|
|
271
|
+
return new d(o);
|
|
272
|
+
}
|
|
273
|
+
#s;
|
|
274
|
+
#e;
|
|
275
|
+
constructor(t) {
|
|
276
|
+
super(), this.#s = t;
|
|
277
|
+
}
|
|
278
|
+
get isConnected() {
|
|
279
|
+
return this.#e?.readyState === this.#s.OPEN;
|
|
280
|
+
}
|
|
281
|
+
get url() {
|
|
282
|
+
return this.#e?.url;
|
|
283
|
+
}
|
|
284
|
+
async connect(t, e = "message") {
|
|
285
|
+
await new Promise((s, r) => {
|
|
286
|
+
if (this.isConnected) {
|
|
287
|
+
r(new Error("Already connected."));
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
this.#e = new this.#s(t), this.#e.addEventListener("open", (n) => {
|
|
292
|
+
this.#t(n), s();
|
|
293
|
+
}), this.#e.addEventListener(
|
|
294
|
+
e,
|
|
295
|
+
(n) => this.#r(n)
|
|
296
|
+
), this.#e.addEventListener(
|
|
297
|
+
"error",
|
|
298
|
+
(n) => this.#n(n)
|
|
299
|
+
);
|
|
300
|
+
} catch (n) {
|
|
301
|
+
r(n);
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
send(t, e) {
|
|
306
|
+
throw new Error("Method not implemented.");
|
|
307
|
+
}
|
|
308
|
+
async close() {
|
|
309
|
+
await new Promise((t, e) => {
|
|
310
|
+
if (!this.isConnected) {
|
|
311
|
+
t();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
this.#e.close(), t();
|
|
316
|
+
} catch (s) {
|
|
317
|
+
e(s);
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Simulates a message event from the server.
|
|
323
|
+
*
|
|
324
|
+
* @param message The message to receive.
|
|
325
|
+
* @param eventName The optional event type.
|
|
326
|
+
* @param lastEventId The optional last event ID.
|
|
327
|
+
*/
|
|
328
|
+
simulateMessage(t, e = "message", s) {
|
|
329
|
+
this.#r(
|
|
330
|
+
new MessageEvent(e, { data: t, lastEventId: s })
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Simulates an error event.
|
|
335
|
+
*/
|
|
336
|
+
simulateError() {
|
|
337
|
+
this.#n(new Event("error"));
|
|
338
|
+
}
|
|
339
|
+
#t(t) {
|
|
340
|
+
this.dispatchEvent(new t.constructor(t.type, t));
|
|
341
|
+
}
|
|
342
|
+
#r(t) {
|
|
343
|
+
this.dispatchEvent(new t.constructor(t.type, t));
|
|
344
|
+
}
|
|
345
|
+
#n(t) {
|
|
346
|
+
this.dispatchEvent(new t.constructor(t.type, t));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
class o extends EventTarget {
|
|
350
|
+
// The constants have to be defined here because Node.js support is currently
|
|
351
|
+
// experimental only.
|
|
352
|
+
static CONNECTING = 0;
|
|
353
|
+
static OPEN = 1;
|
|
354
|
+
static CLOSED = 2;
|
|
355
|
+
url;
|
|
356
|
+
readyState = o.CONNECTING;
|
|
357
|
+
constructor(t) {
|
|
358
|
+
super(), this.url = t.toString(), setTimeout(() => {
|
|
359
|
+
this.readyState = o.OPEN, this.dispatchEvent(new Event("open"));
|
|
360
|
+
}, 0);
|
|
361
|
+
}
|
|
362
|
+
close() {
|
|
363
|
+
this.readyState = o.CLOSED;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
const g = "heartbeat", E = "message-sent";
|
|
367
|
+
class l extends EventTarget {
|
|
368
|
+
// TODO Recover connection with timeout after an error event.
|
|
369
|
+
/**
|
|
370
|
+
* Creates a WebSocket client.
|
|
371
|
+
*
|
|
372
|
+
* @param options The options for the WebSocket client.
|
|
373
|
+
* @return A new WebSocket client.
|
|
374
|
+
*/
|
|
375
|
+
static create({ heartbeat: t = 3e4 } = {}) {
|
|
376
|
+
return new l(t, WebSocket);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Creates a nulled WebSocket client.
|
|
380
|
+
*
|
|
381
|
+
* @param options The options for the WebSocket client.
|
|
382
|
+
* @return A new nulled WebSocket client.
|
|
383
|
+
*/
|
|
384
|
+
static createNull({ heartbeat: t = -1 } = {}) {
|
|
385
|
+
return new l(
|
|
386
|
+
t,
|
|
387
|
+
m
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
#s;
|
|
391
|
+
#e;
|
|
392
|
+
#t;
|
|
393
|
+
#r;
|
|
394
|
+
constructor(t, e) {
|
|
395
|
+
super(), this.#s = t, this.#e = e;
|
|
396
|
+
}
|
|
397
|
+
get isConnected() {
|
|
398
|
+
return this.#t?.readyState === WebSocket.OPEN;
|
|
399
|
+
}
|
|
400
|
+
get url() {
|
|
401
|
+
return this.#t?.url;
|
|
402
|
+
}
|
|
403
|
+
async connect(t) {
|
|
404
|
+
await new Promise((e, s) => {
|
|
405
|
+
if (this.isConnected) {
|
|
406
|
+
s(new Error("Already connected."));
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
this.#t = new this.#e(t), this.#t.addEventListener("open", (r) => {
|
|
411
|
+
this.#n(r), e();
|
|
412
|
+
}), this.#t.addEventListener(
|
|
413
|
+
"message",
|
|
414
|
+
(r) => this.#i(r)
|
|
415
|
+
), this.#t.addEventListener("close", (r) => this.#a(r)), this.#t.addEventListener("error", (r) => this.#o(r));
|
|
416
|
+
} catch (r) {
|
|
417
|
+
s(r);
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
async send(t) {
|
|
422
|
+
if (!this.isConnected)
|
|
423
|
+
throw new Error("Not connected.");
|
|
424
|
+
this.#t.send(t), this.dispatchEvent(
|
|
425
|
+
new CustomEvent(E, { detail: t })
|
|
426
|
+
), await Promise.resolve();
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Returns a tracker for messages sent.
|
|
430
|
+
*
|
|
431
|
+
* @return A new output tracker.
|
|
432
|
+
*/
|
|
433
|
+
trackMessageSent() {
|
|
434
|
+
return u.create(this, E);
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Closes the connection.
|
|
438
|
+
*
|
|
439
|
+
* If a code is provided, also a reason should be provided.
|
|
440
|
+
*
|
|
441
|
+
* @param code An optional code.
|
|
442
|
+
* @param reason An optional reason.
|
|
443
|
+
*/
|
|
444
|
+
async close(t, e) {
|
|
445
|
+
await new Promise((s) => {
|
|
446
|
+
if (!this.isConnected) {
|
|
447
|
+
s();
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
this.#t.addEventListener("close", () => s()), this.#t.close(t, e);
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Simulates a message event from the server.
|
|
455
|
+
*
|
|
456
|
+
* @param message The message to receive.
|
|
457
|
+
*/
|
|
458
|
+
simulateMessage(t) {
|
|
459
|
+
this.#i(new MessageEvent("message", { data: t }));
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Simulates a heartbeat.
|
|
463
|
+
*/
|
|
464
|
+
simulateHeartbeat() {
|
|
465
|
+
this.#c();
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Simulates a close event.
|
|
469
|
+
*
|
|
470
|
+
* @param code An optional code.
|
|
471
|
+
* @param reason An optional reason.
|
|
472
|
+
*/
|
|
473
|
+
simulateClose(t, e) {
|
|
474
|
+
this.#a(new CloseEvent("close", { code: t, reason: e }));
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Simulates an error event.
|
|
478
|
+
*/
|
|
479
|
+
simulateError() {
|
|
480
|
+
this.#o(new Event("error"));
|
|
481
|
+
}
|
|
482
|
+
#n(t) {
|
|
483
|
+
this.dispatchEvent(new t.constructor(t.type, t)), this.#h();
|
|
484
|
+
}
|
|
485
|
+
#i(t) {
|
|
486
|
+
this.dispatchEvent(
|
|
487
|
+
// @ts-expect-error create copy of event
|
|
488
|
+
new t.constructor(t.type, t)
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
#a(t) {
|
|
492
|
+
this.#u(), this.dispatchEvent(new t.constructor(t.type, t));
|
|
493
|
+
}
|
|
494
|
+
#o(t) {
|
|
495
|
+
this.dispatchEvent(new t.constructor(t.type, t));
|
|
496
|
+
}
|
|
497
|
+
#h() {
|
|
498
|
+
this.#s <= 0 || (this.#r = setInterval(
|
|
499
|
+
() => this.#c(),
|
|
500
|
+
this.#s
|
|
501
|
+
));
|
|
502
|
+
}
|
|
503
|
+
#u() {
|
|
504
|
+
clearInterval(this.#r), this.#r = void 0;
|
|
505
|
+
}
|
|
506
|
+
#c() {
|
|
507
|
+
this.#r != null && this.send(g);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
class m extends EventTarget {
|
|
511
|
+
url;
|
|
512
|
+
readyState = WebSocket.CONNECTING;
|
|
513
|
+
constructor(t) {
|
|
514
|
+
super(), this.url = t.toString(), setTimeout(() => {
|
|
515
|
+
this.readyState = WebSocket.OPEN, this.dispatchEvent(new Event("open"));
|
|
516
|
+
}, 0);
|
|
517
|
+
}
|
|
518
|
+
send() {
|
|
519
|
+
}
|
|
520
|
+
close() {
|
|
521
|
+
this.readyState = WebSocket.CLOSED, this.dispatchEvent(new Event("close"));
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
export {
|
|
525
|
+
c as Clock,
|
|
526
|
+
h as ConfigurableResponses,
|
|
527
|
+
f as ConsoleStub,
|
|
528
|
+
S as Failure,
|
|
529
|
+
g as HEARTBEAT_TYPE,
|
|
530
|
+
u as OutputTracker,
|
|
531
|
+
d as SseClient,
|
|
532
|
+
y as Success,
|
|
533
|
+
l as WebSocketClient,
|
|
534
|
+
b as createFetchStub
|
|
535
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.Shared={}))})(this,(function(r){"use strict";class a{static system(){return new a}static fixed(t){return new a(t)}static offset(t,e){return new a(t.millis()+e)}#s;constructor(t){this.#s=t?new Date(t):void 0}date(){return this.#s?new Date(this.#s):new Date}millis(){return this.date().getTime()}}class h{static create(t,e){return new h(t,e)}static mapObject(t,e){const n=Object.entries(t).map(([i,b])=>{const C=e===void 0?void 0:`${e}: ${i}`;return[i,h.create(b,C)]});return Object.fromEntries(n)}#s;#e;constructor(t,e){this.#s=e==null?"":` in ${e}`,this.#e=Array.isArray(t)?[...t]:t}next(){const t=Array.isArray(this.#e)?this.#e.shift():this.#e;if(t===void 0)throw new Error(`No more responses configured${this.#s}.`);return t}}class u{static create(t,e){return new u(t,e)}#s;#e;#t;#r;constructor(t,e){this.#s=t,this.#e=e,this.#t=[],this.#r=s=>this.#t.push(s.detail),this.#s.addEventListener(this.#e,this.#r)}get data(){return this.#t}clear(){const t=[...this.#t];return this.#t.length=0,t}stop(){this.#s.removeEventListener(this.#e,this.#r)}}class g{isSuccess=!0}class p{isSuccess=!1;errorMessage;constructor(t){this.errorMessage=t}}const c="message";class m extends EventTarget{log(...t){this.dispatchEvent(new CustomEvent(c,{detail:{level:"log",message:t}}))}error(...t){this.dispatchEvent(new CustomEvent(c,{detail:{level:"error",message:t}}))}warn(...t){this.dispatchEvent(new CustomEvent(c,{detail:{level:"warn",message:t}}))}info(...t){this.dispatchEvent(new CustomEvent(c,{detail:{level:"info",message:t}}))}debug(...t){this.dispatchEvent(new CustomEvent(c,{detail:{level:"debug",message:t}}))}trace(...t){this.dispatchEvent(new CustomEvent(c,{detail:{level:"trace",message:t}}))}trackMessages(){return new u(this,c)}}function y(o){const t=h.create(o);return async function(){const e=t.next();if(e instanceof Error)throw e;return new S(e)}}class S{#s;#e;#t;constructor({status:t,statusText:e,body:s=null}){this.#s=t,this.#e=e,this.#t=s}get ok(){return this.status>=200&&this.status<300}get status(){return this.#s}get statusText(){return this.#e}async blob(){if(this.#t==null)return null;if(this.#t instanceof Blob)return this.#t;throw new TypeError("Body is not a Blob.")}async json(){const t=typeof this.#t=="string"?this.#t:JSON.stringify(this.#t);return Promise.resolve(JSON.parse(t))}async text(){return this.#t==null?"":String(this.#t)}}class l extends EventTarget{static create(){return new l(EventSource)}static createNull(){return new l(d)}#s;#e;constructor(t){super(),this.#s=t}get isConnected(){return this.#e?.readyState===this.#s.OPEN}get url(){return this.#e?.url}async connect(t,e="message"){await new Promise((s,n)=>{if(this.isConnected){n(new Error("Already connected."));return}try{this.#e=new this.#s(t),this.#e.addEventListener("open",i=>{this.#t(i),s()}),this.#e.addEventListener(e,i=>this.#r(i)),this.#e.addEventListener("error",i=>this.#n(i))}catch(i){n(i)}})}send(t,e){throw new Error("Method not implemented.")}async close(){await new Promise((t,e)=>{if(!this.isConnected){t();return}try{this.#e.close(),t()}catch(s){e(s)}})}simulateMessage(t,e="message",s){this.#r(new MessageEvent(e,{data:t,lastEventId:s}))}simulateError(){this.#n(new Event("error"))}#t(t){this.dispatchEvent(new t.constructor(t.type,t))}#r(t){this.dispatchEvent(new t.constructor(t.type,t))}#n(t){this.dispatchEvent(new t.constructor(t.type,t))}}class d extends EventTarget{static CONNECTING=0;static OPEN=1;static CLOSED=2;url;readyState=d.CONNECTING;constructor(t){super(),this.url=t.toString(),setTimeout(()=>{this.readyState=d.OPEN,this.dispatchEvent(new Event("open"))},0)}close(){this.readyState=d.CLOSED}}const w="heartbeat",v="message-sent";class E extends EventTarget{static create({heartbeat:t=3e4}={}){return new E(t,WebSocket)}static createNull({heartbeat:t=-1}={}){return new E(t,f)}#s;#e;#t;#r;constructor(t,e){super(),this.#s=t,this.#e=e}get isConnected(){return this.#t?.readyState===WebSocket.OPEN}get url(){return this.#t?.url}async connect(t){await new Promise((e,s)=>{if(this.isConnected){s(new Error("Already connected."));return}try{this.#t=new this.#e(t),this.#t.addEventListener("open",n=>{this.#n(n),e()}),this.#t.addEventListener("message",n=>this.#i(n)),this.#t.addEventListener("close",n=>this.#a(n)),this.#t.addEventListener("error",n=>this.#c(n))}catch(n){s(n)}})}async send(t){if(!this.isConnected)throw new Error("Not connected.");this.#t.send(t),this.dispatchEvent(new CustomEvent(v,{detail:t})),await Promise.resolve()}trackMessageSent(){return u.create(this,v)}async close(t,e){await new Promise(s=>{if(!this.isConnected){s();return}this.#t.addEventListener("close",()=>s()),this.#t.close(t,e)})}simulateMessage(t){this.#i(new MessageEvent("message",{data:t}))}simulateHeartbeat(){this.#o()}simulateClose(t,e){this.#a(new CloseEvent("close",{code:t,reason:e}))}simulateError(){this.#c(new Event("error"))}#n(t){this.dispatchEvent(new t.constructor(t.type,t)),this.#h()}#i(t){this.dispatchEvent(new t.constructor(t.type,t))}#a(t){this.#u(),this.dispatchEvent(new t.constructor(t.type,t))}#c(t){this.dispatchEvent(new t.constructor(t.type,t))}#h(){this.#s<=0||(this.#r=setInterval(()=>this.#o(),this.#s))}#u(){clearInterval(this.#r),this.#r=void 0}#o(){this.#r!=null&&this.send(w)}}class f extends EventTarget{url;readyState=WebSocket.CONNECTING;constructor(t){super(),this.url=t.toString(),setTimeout(()=>{this.readyState=WebSocket.OPEN,this.dispatchEvent(new Event("open"))},0)}send(){}close(){this.readyState=WebSocket.CLOSED,this.dispatchEvent(new Event("close"))}}r.Clock=a,r.ConfigurableResponses=h,r.ConsoleStub=m,r.Failure=p,r.HEARTBEAT_TYPE=w,r.OutputTracker=u,r.SseClient=l,r.Success=g,r.WebSocketClient=E,r.createFetchStub=y,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,38 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muspellheim/shared",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"author": "Falko Schumann",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
7
|
-
"
|
|
8
|
-
"node": ">=18.7.0"
|
|
7
|
+
"node": ">=20.0.0"
|
|
9
8
|
},
|
|
10
9
|
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"main": "./dist/shared.umd.cjs",
|
|
14
|
+
"module": "./dist/shared.js",
|
|
11
15
|
"exports": {
|
|
12
|
-
".":
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"./node/*.js": "./lib/node/*.js",
|
|
18
|
-
"./vitest": "./lib/vitest/index.js",
|
|
19
|
-
"./vitest/*.js": "./lib/vitest/*.js"
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/shared.d.ts",
|
|
18
|
+
"import": "./dist/shared.js",
|
|
19
|
+
"require": "./dist/shared.umd.cjs"
|
|
20
|
+
}
|
|
20
21
|
},
|
|
21
22
|
"scripts": {
|
|
23
|
+
"build": "vite build",
|
|
22
24
|
"test": "vitest"
|
|
23
25
|
},
|
|
24
|
-
"dependencies": {
|
|
25
|
-
"express": "^4.21.2",
|
|
26
|
-
"lit-html": "^3.1.0"
|
|
27
|
-
},
|
|
28
26
|
"devDependencies": {
|
|
29
|
-
"@eslint/js": "9.
|
|
30
|
-
"@
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"jsdom": "
|
|
35
|
-
"
|
|
36
|
-
"
|
|
27
|
+
"@eslint/js": "9.35.0",
|
|
28
|
+
"@types/node": "22.16.5",
|
|
29
|
+
"@vitest/coverage-v8": "3.2.4",
|
|
30
|
+
"eslint": "9.35.0",
|
|
31
|
+
"eslint-plugin-headers": "1.3.3",
|
|
32
|
+
"jsdom": "27.0.0",
|
|
33
|
+
"globals": "16.4.0",
|
|
34
|
+
"prettier": "3.6.2",
|
|
35
|
+
"typedoc": "0.28.12",
|
|
36
|
+
"typescript": "5.8.3",
|
|
37
|
+
"typescript-eslint": "8.43.0",
|
|
38
|
+
"vite": "7.1.5",
|
|
39
|
+
"vitest": "3.2.4",
|
|
40
|
+
"vite-plugin-dts": "4.5.4"
|
|
37
41
|
}
|
|
38
42
|
}
|
package/.prettierignore
DELETED
package/.prettierrc
DELETED
package/deno.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"lib": ["es2022", "dom"],
|
|
4
|
-
"checkJs": true,
|
|
5
|
-
"noImplicitAny": false,
|
|
6
|
-
"strictNullChecks": false
|
|
7
|
-
},
|
|
8
|
-
"fmt": {
|
|
9
|
-
"singleQuote": true,
|
|
10
|
-
"exclude": ["coverage", "dist", "docs"]
|
|
11
|
-
},
|
|
12
|
-
"lint": {
|
|
13
|
-
"exclude": ["coverage", "dist", "docs"]
|
|
14
|
-
}
|
|
15
|
-
}
|
package/deno.mk
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
# Possible values: major, minor, patch or concrete version
|
|
2
|
-
VERSION = minor
|
|
3
|
-
|
|
4
|
-
all: dist docs check
|
|
5
|
-
|
|
6
|
-
clean:
|
|
7
|
-
rm -rf coverage docs
|
|
8
|
-
|
|
9
|
-
distclean: clean
|
|
10
|
-
rm -rf dist
|
|
11
|
-
rm -rf node_modules
|
|
12
|
-
|
|
13
|
-
dist: build
|
|
14
|
-
|
|
15
|
-
release: all
|
|
16
|
-
npm version $(VERSION) -m "chore: create release v%s"
|
|
17
|
-
git push
|
|
18
|
-
git push --tags
|
|
19
|
-
|
|
20
|
-
publish: all
|
|
21
|
-
deno publish --dry-run
|
|
22
|
-
|
|
23
|
-
docs:
|
|
24
|
-
# FIXME deno doc --html --name="Muspellheim Shared" --lint lib
|
|
25
|
-
# FIXME deno doc --html --name="Muspellheim Shared" lib
|
|
26
|
-
|
|
27
|
-
check: test
|
|
28
|
-
deno run --allow-all npm:eslint lib test
|
|
29
|
-
deno run --allow-all npm:prettier . --check
|
|
30
|
-
# TODO deno fmt --check
|
|
31
|
-
deno lint
|
|
32
|
-
|
|
33
|
-
format:
|
|
34
|
-
deno run --allow-all npm:eslint --fix lib test
|
|
35
|
-
deno run --allow-all npm:prettier . --write
|
|
36
|
-
# TODO deno fmt
|
|
37
|
-
deno lint --fix
|
|
38
|
-
|
|
39
|
-
dev: build
|
|
40
|
-
deno run --allow-all npm:vitest
|
|
41
|
-
|
|
42
|
-
test: build
|
|
43
|
-
deno run --allow-all npm:vitest run
|
|
44
|
-
|
|
45
|
-
unit-tests: build
|
|
46
|
-
deno run --allow-all npm:vitest run --testPathPattern=".*\/unit\/.*"
|
|
47
|
-
|
|
48
|
-
integration-tests: build
|
|
49
|
-
deno run --allow-all npm:vitest run --testPathPattern=".*\/integration\/.*"
|
|
50
|
-
|
|
51
|
-
e2e-tests: build
|
|
52
|
-
deno run --allow-all npm:vitest run --testPathPattern=".*\/e2e\/.*"
|
|
53
|
-
|
|
54
|
-
coverage: build
|
|
55
|
-
deno run --allow-all npm:vitest run --coverage
|
|
56
|
-
|
|
57
|
-
build: prepare
|
|
58
|
-
|
|
59
|
-
prepare: version
|
|
60
|
-
deno install
|
|
61
|
-
|
|
62
|
-
version:
|
|
63
|
-
@echo "Use Deno $(shell deno --version)"
|
|
64
|
-
|
|
65
|
-
.PHONY: all clean distclean dist release publish docs \
|
|
66
|
-
check format \
|
|
67
|
-
dev test unit-tests integration-tests e2e-tests coverage \
|
|
68
|
-
build prepare version
|