@muspellheim/shared 0.6.1 → 0.8.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.
Files changed (46) hide show
  1. package/LICENSE.txt +1 -1
  2. package/README.md +11 -13
  3. package/dist/shared.d.ts +423 -0
  4. package/dist/shared.js +535 -0
  5. package/dist/shared.umd.cjs +1 -0
  6. package/package.json +27 -23
  7. package/.prettierignore +0 -3
  8. package/.prettierrc +0 -5
  9. package/deno.json +0 -15
  10. package/deno.mk +0 -68
  11. package/eslint.config.js +0 -23
  12. package/lib/assert.js +0 -15
  13. package/lib/browser/components.js +0 -165
  14. package/lib/browser/index.js +0 -3
  15. package/lib/color.js +0 -137
  16. package/lib/configurable-responses.js +0 -69
  17. package/lib/feature-toggle.js +0 -9
  18. package/lib/health.js +0 -510
  19. package/lib/index.js +0 -23
  20. package/lib/lang.js +0 -100
  21. package/lib/logging.js +0 -599
  22. package/lib/long-polling-client.js +0 -186
  23. package/lib/message-client.js +0 -68
  24. package/lib/messages.js +0 -68
  25. package/lib/metrics.js +0 -120
  26. package/lib/node/actuator-controller.js +0 -102
  27. package/lib/node/configuration-properties.js +0 -291
  28. package/lib/node/handler.js +0 -25
  29. package/lib/node/index.js +0 -9
  30. package/lib/node/logging.js +0 -60
  31. package/lib/node/long-polling.js +0 -83
  32. package/lib/node/sse-emitter.js +0 -104
  33. package/lib/node/static-files-controller.js +0 -15
  34. package/lib/output-tracker.js +0 -89
  35. package/lib/service-locator.js +0 -44
  36. package/lib/sse-client.js +0 -163
  37. package/lib/stop-watch.js +0 -54
  38. package/lib/store.js +0 -129
  39. package/lib/time.js +0 -445
  40. package/lib/util.js +0 -380
  41. package/lib/validation.js +0 -290
  42. package/lib/vector.js +0 -194
  43. package/lib/vitest/equality-testers.js +0 -19
  44. package/lib/vitest/index.js +0 -1
  45. package/lib/web-socket-client.js +0 -262
  46. package/tsconfig.json +0 -13
package/lib/vector.js DELETED
@@ -1,194 +0,0 @@
1
- // Copyright (c) 2023-2024 Falko Schumann. All rights reserved. MIT license.
2
-
3
- /**
4
- * Calculate with vectors in a two-dimensional space.
5
- *
6
- * @module
7
- */
8
-
9
- /**
10
- * A vector in a two-dimensional space.
11
- */
12
- export class Vector2D {
13
- /**
14
- * Creates a vector from 2 points.
15
- *
16
- * @param {Vector2D} a The first point.
17
- * @param {Vector2D} b The second point.
18
- * @return {Vector2D} The vector from a to b.
19
- */
20
- static fromPoints(a, b) {
21
- return new Vector2D(b.x - a.x, b.y - a.y);
22
- }
23
-
24
- /**
25
- * Creates a new vector.
26
- *
27
- * Examples:
28
- *
29
- * ```java
30
- * new Vector2D(1, 2)
31
- * new Vector2D([1, 2])
32
- * new Vector2D({ x: 1, y: 2 })
33
- * ```
34
- *
35
- * @param {number|Array<number>|Vector2D} [x=0] The x coordinate or an array
36
- * or another vector.
37
- * @param {number} [y=0] The y coordinate or undefined if x is an array or
38
- * another vector.
39
- */
40
- constructor(x = 0, y = 0) {
41
- if (Array.isArray(x)) {
42
- this.x = Number(x[0]);
43
- this.y = Number(x[1]);
44
- } else if (typeof x === 'object' && 'x' in x && 'y' in x) {
45
- this.x = Number(x.x);
46
- this.y = Number(x.y);
47
- } else {
48
- this.x = Number(x);
49
- this.y = Number(y);
50
- }
51
- }
52
-
53
- /**
54
- * Returns the length of the vector.
55
- *
56
- * @return {number} The length of the vector.
57
- */
58
- length() {
59
- return Math.hypot(this.x, this.y);
60
- }
61
-
62
- /**
63
- * Adds another vector to this vector and return the new vector.
64
- *
65
- * @param {Vector2D} v The vector to add.
66
- * @return {Vector2D} The new vector.
67
- */
68
- add(v) {
69
- v = new Vector2D(v);
70
- return new Vector2D(this.x + v.x, this.y + v.y);
71
- }
72
-
73
- /**
74
- * Subtracts another vector from this vector and return the new vector.
75
- *
76
- * @param {Vector2D} v The vector to subtract.
77
- * @return {Vector2D} The new vector.
78
- */
79
- subtract(v) {
80
- v = new Vector2D(v);
81
- return new Vector2D(this.x - v.x, this.y - v.y);
82
- }
83
-
84
- /**
85
- * Multiplies the vector with a scalar and returns the new vector.
86
- *
87
- * @param {number} scalar The scalar to multiply with.
88
- * @return {Vector2D} The new vector.
89
- */
90
- scale(scalar) {
91
- return new Vector2D(this.x * scalar, this.y * scalar);
92
- }
93
-
94
- /**
95
- * Multiplies the vector with another vector and returns the scalar.
96
- *
97
- * @param {Vector2D} v The vector to multiply with.
98
- * @return {number} The scalar.
99
- */
100
- dot(v) {
101
- v = new Vector2D(v);
102
- return this.x * v.x + this.y * v.y;
103
- }
104
-
105
- /**
106
- * Returns the distance between this vector and another vector.
107
- *
108
- * @param {Vector2D} v The other vector.
109
- * @return {number} The distance.
110
- */
111
- distance(v) {
112
- v = new Vector2D(v);
113
- return Vector2D.fromPoints(this, v).length();
114
- }
115
-
116
- /**
117
- * Returns the rotated vector by the given angle in radians.
118
- *
119
- * @param {number} angle The angle in radians.
120
- * @return {Vector2D} The rotated vector.
121
- */
122
- rotate(angle) {
123
- const cos = Math.cos(angle);
124
- const sin = Math.sin(angle);
125
- return new Vector2D(
126
- this.x * cos - this.y * sin,
127
- this.x * sin + this.y * cos,
128
- );
129
- }
130
-
131
- /**
132
- * Returns the unit vector of this vector.
133
- *
134
- * @return {Vector2D} The unit vector.
135
- */
136
- normalize() {
137
- return this.scale(1 / this.length());
138
- }
139
- }
140
-
141
- /**
142
- * A line in a two-dimensional space.
143
- */
144
- export class Line2D {
145
- /**
146
- * Creates a line from 2 points.
147
- *
148
- * @param {Vector2D} a The first point.
149
- * @param {Vector2D} b The second point.
150
- * @return {Line2D} The line from a to b.
151
- */
152
- static fromPoints(a, b) {
153
- return new Line2D(a, Vector2D.fromPoints(a, b));
154
- }
155
-
156
- /**
157
- * Creates a new line.
158
- *
159
- * @param {Vector2D} point A point on the line.
160
- * @param {Vector2D} direction The direction of the line.
161
- */
162
- constructor(point, direction) {
163
- this.point = new Vector2D(point);
164
- this.direction = new Vector2D(direction);
165
- }
166
-
167
- /**
168
- * Returns the perpendicular of a point on this line.
169
- *
170
- * @param {Vector2D} point A point.
171
- * @return {{foot: Vector2D, scalar: number}} The `foot` and the `scalar`.
172
- */
173
- perpendicular(point) {
174
- // dissolve after r: (line.position + r * line.direction - point) * line.direction = 0
175
- const a = this.point.subtract(point);
176
- const b = a.dot(this.direction);
177
- const c = this.direction.dot(this.direction);
178
- const r = b !== 0 ? -b / c : 0;
179
-
180
- // solve with r: line.position + r * line.direction = foot
181
- const foot = this.point.add(this.direction.scale(r));
182
-
183
- let scalar;
184
- if (this.direction.x !== 0.0) {
185
- scalar = (foot.x - this.point.x) / this.direction.x;
186
- } else if (this.direction.y !== 0.0) {
187
- scalar = (foot.y - this.point.y) / this.direction.y;
188
- } else {
189
- scalar = Number.NaN;
190
- }
191
-
192
- return { foot, scalar };
193
- }
194
- }
@@ -1,19 +0,0 @@
1
- import { expect } from 'vitest';
2
-
3
- function isEquatable(a) {
4
- return a != null && typeof a.equals === 'function';
5
- }
6
-
7
- function testEquatable(a, b) {
8
- const isAEquatable = isEquatable(a);
9
- const isBEquatable = isEquatable(b);
10
- if (isAEquatable && isBEquatable) {
11
- return a.equals(b);
12
- } else if (isAEquatable === isBEquatable) {
13
- return undefined;
14
- } else {
15
- return false;
16
- }
17
- }
18
-
19
- expect.addEqualityTesters([testEquatable]);
@@ -1 +0,0 @@
1
- export * from './equality-testers.js';
@@ -1,262 +0,0 @@
1
- // Copyright (c) 2023-2024 Falko Schumann. All rights reserved. MIT license.
2
-
3
- import { MessageClient } from './message-client.js';
4
- import { OutputTracker } from './output-tracker.js';
5
- import { Timer, TimerTask } from './util.js';
6
-
7
- /**
8
- * @ignore @typedef {typeof WebSocket} WebSocketConstructor
9
- */
10
-
11
- export const HEARTBEAT_TYPE = 'heartbeat';
12
-
13
- const MESSAGE_SENT_EVENT = 'message-sent';
14
-
15
- /**
16
- * A client for the WebSocket protocol.
17
- *
18
- * Emits the following events:
19
- *
20
- * - open, {@link Event}
21
- * - message, {@link MessageEvent}
22
- * - error, {@link Event}
23
- * - close, {@link CloseEvent}
24
- *
25
- * @implements {MessageClient}
26
- */
27
- export class WebSocketClient extends MessageClient {
28
- // TODO Recover connection with timeout after an error event.
29
-
30
- /**
31
- * Creates a WebSocket client.
32
- *
33
- * @param {object} options
34
- * @param {number} [options.heartbeat=30000] The heartbeat interval i
35
- * milliseconds. A value <= 0 disables the heartbeat.
36
- * @return {WebSocketClient} A new WebSocket client.
37
- */
38
- static create({ heartbeat = 30000 } = {}) {
39
- return new WebSocketClient(heartbeat, Timer.create(), WebSocket);
40
- }
41
-
42
- /**
43
- * Creates a nulled WebSocket client.
44
- *
45
- * @param {object} options
46
- * @param {number} [options.heartbeat=-1] The heartbeat interval in
47
- * milliseconds. A value <= 0 disables the heartbeat.
48
- * @return {WebSocketClient} A new nulled WebSocket client.
49
- */
50
- static createNull({ heartbeat = -1 } = {}) {
51
- return new WebSocketClient(heartbeat, Timer.createNull(), WebSocketStub);
52
- }
53
-
54
- #heartbeat;
55
- #timer;
56
- #webSocketConstructor;
57
- /** @type {WebSocket} */ #webSocket;
58
-
59
- /**
60
- * The constructor is for internal use. Use the factory methods instead.
61
- *
62
- * @see WebSocketClient.create
63
- * @see WebSocketClient.createNull
64
- */
65
- constructor(
66
- /** @type {number} */ heartbeat,
67
- /** @type {Timer} */ timer,
68
- /** @type {WebSocketConstructor} */ webSocketConstructor,
69
- ) {
70
- super();
71
- this.#heartbeat = heartbeat;
72
- this.#timer = timer;
73
- this.#webSocketConstructor = webSocketConstructor;
74
- }
75
-
76
- /**
77
- * @override
78
- */
79
- get isConnected() {
80
- return this.#webSocket?.readyState === WebSocket.OPEN;
81
- }
82
-
83
- /**
84
- * @override
85
- */
86
- get url() {
87
- return this.#webSocket?.url;
88
- }
89
-
90
- /**
91
- * @override
92
- */
93
- async connect(/** @type {string | URL} */ url) {
94
- await new Promise((resolve, reject) => {
95
- if (this.isConnected) {
96
- reject(new Error('Already connected.'));
97
- return;
98
- }
99
-
100
- try {
101
- this.#webSocket = new this.#webSocketConstructor(url);
102
- this.#webSocket.addEventListener('open', (e) => {
103
- this.#handleOpen(e);
104
- resolve();
105
- });
106
- this.#webSocket.addEventListener('message', (e) =>
107
- this.#handleMessage(e),
108
- );
109
- this.#webSocket.addEventListener('close', (e) => this.#handleClose(e));
110
- this.#webSocket.addEventListener('error', (e) => this.#handleError(e));
111
- } catch (error) {
112
- reject(error);
113
- }
114
- });
115
- }
116
-
117
- /**
118
- * Sends a message to the server.
119
- * @param {string} message The message to send.
120
- * @override
121
- */
122
- async send(message) {
123
- this.#webSocket.send(message);
124
- this.dispatchEvent(
125
- new CustomEvent(MESSAGE_SENT_EVENT, { detail: message }),
126
- );
127
- await Promise.resolve();
128
- }
129
-
130
- /**
131
- * Returns a tracker for messages sent.
132
- *
133
- * @return {OutputTracker} A new output tracker.
134
- */
135
- trackMessageSent() {
136
- return OutputTracker.create(this, MESSAGE_SENT_EVENT);
137
- }
138
-
139
- /**
140
- * Closes the connection.
141
- *
142
- * If a code is provided, also a reason should be provided.
143
- * @param {number} [code] An optional code.
144
- * @param {string} [reason] An optional reason.
145
- * @override
146
- */
147
- async close(code, reason) {
148
- await new Promise((resolve) => {
149
- if (!this.isConnected) {
150
- resolve();
151
- return;
152
- }
153
-
154
- this.#webSocket.addEventListener('close', () => resolve());
155
- this.#webSocket.close(code, reason);
156
- });
157
- }
158
-
159
- /**
160
- * Simulates a message event from the server.
161
- *
162
- * @param {string} message The message to receive.
163
- */
164
- simulateMessage(message) {
165
- this.#handleMessage(new MessageEvent('message', { data: message }));
166
- }
167
-
168
- /**
169
- * Simulates a heartbeat.
170
- */
171
- simulateHeartbeat() {
172
- this.#timer.simulateTaskExecution({ ticks: this.#heartbeat });
173
- }
174
-
175
- /**
176
- * Simulates a close event.
177
- *
178
- * @param {number} code An optional code.
179
- * @param {string} reason An optional reason.
180
- */
181
- simulateClose(code, reason) {
182
- this.#handleClose(new CloseEvent('close', { code, reason }));
183
- }
184
-
185
- /**
186
- * Simulates an error event.
187
- */
188
- simulateError() {
189
- this.#handleError(new Event('error'));
190
- }
191
-
192
- #handleOpen(event) {
193
- this.dispatchEvent(new event.constructor(event.type, event));
194
- this.#startHeartbeat();
195
- }
196
-
197
- #handleMessage(event) {
198
- this.dispatchEvent(new event.constructor(event.type, event));
199
- }
200
-
201
- #handleClose(event) {
202
- this.#stopHeartbeat();
203
- this.dispatchEvent(new event.constructor(event.type, event));
204
- }
205
-
206
- #handleError(event) {
207
- this.dispatchEvent(new event.constructor(event.type, event));
208
- }
209
-
210
- #startHeartbeat() {
211
- if (this.#heartbeat <= 0) {
212
- return;
213
- }
214
-
215
- this.#timer.scheduleAtFixedRate(
216
- new HeartbeatTask(this),
217
- this.#heartbeat,
218
- this.#heartbeat,
219
- );
220
- }
221
-
222
- #stopHeartbeat() {
223
- this.#timer.cancel();
224
- }
225
- }
226
-
227
- class HeartbeatTask extends TimerTask {
228
- #client;
229
-
230
- constructor(/** @type {WebSocketClient} */ client) {
231
- super();
232
- this.#client = client;
233
- }
234
-
235
- /**
236
- * @override
237
- */
238
- run() {
239
- this.#client.send(HEARTBEAT_TYPE);
240
- }
241
- }
242
-
243
- class WebSocketStub extends EventTarget {
244
- /** @type {number} */
245
- readyState = WebSocket.CONNECTING;
246
-
247
- constructor(url) {
248
- super();
249
- this.url = url;
250
- setTimeout(() => {
251
- this.readyState = WebSocket.OPEN;
252
- this.dispatchEvent(new Event('open'));
253
- }, 0);
254
- }
255
-
256
- send() {}
257
-
258
- close() {
259
- this.readyState = WebSocket.CLOSED;
260
- this.dispatchEvent(new Event('close'));
261
- }
262
- }
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "nodenext",
6
- "lib": ["es2022", "dom"],
7
- "allowJs": true,
8
- "checkJs": false,
9
- "noImplicitAny": false,
10
- "strictNullChecks": false
11
- },
12
- "exclude": ["coverage", "dist", "node_modules"]
13
- }