@muspellheim/shared 0.17.0 → 0.18.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.
Files changed (35) hide show
  1. package/dist/mod.cjs +556 -0
  2. package/dist/mod.js +524 -0
  3. package/dist/types/common/clock.d.ts +40 -0
  4. package/dist/types/common/event_tracker.d.ts +44 -0
  5. package/dist/types/common/log.d.ts +17 -0
  6. package/{src/common/mod.ts → dist/types/common/mod.d.ts} +0 -2
  7. package/{src/domain/messages.ts → dist/types/domain/messages.d.ts} +13 -24
  8. package/dist/types/domain/mod.d.ts +1 -0
  9. package/dist/types/infrastructure/configurable_responses.d.ts +58 -0
  10. package/dist/types/infrastructure/console_log.d.ts +34 -0
  11. package/dist/types/infrastructure/fetch_stub.d.ts +20 -0
  12. package/dist/types/infrastructure/message_client.d.ts +44 -0
  13. package/{src/infrastructure/mod.ts → dist/types/infrastructure/mod.d.ts} +0 -2
  14. package/dist/types/infrastructure/output_tracker.d.ts +60 -0
  15. package/dist/types/infrastructure/sse_client.d.ts +37 -0
  16. package/dist/types/infrastructure/web_socket_client.d.ts +79 -0
  17. package/{src/mod.ts → dist/types/mod.d.ts} +0 -2
  18. package/package.json +14 -23
  19. package/dist/shared.d.ts +0 -494
  20. package/dist/shared.js +0 -670
  21. package/dist/shared.js.map +0 -1
  22. package/dist/shared.umd.cjs +0 -2
  23. package/dist/shared.umd.cjs.map +0 -1
  24. package/src/common/clock.ts +0 -60
  25. package/src/common/event_tracker.ts +0 -94
  26. package/src/common/log.ts +0 -27
  27. package/src/domain/mod.ts +0 -3
  28. package/src/infrastructure/configurable_responses.ts +0 -90
  29. package/src/infrastructure/console_log.ts +0 -160
  30. package/src/infrastructure/fetch_stub.ts +0 -89
  31. package/src/infrastructure/message_client.ts +0 -50
  32. package/src/infrastructure/output_tracker.ts +0 -89
  33. package/src/infrastructure/sse_client.ts +0 -162
  34. package/src/infrastructure/web_socket_client.ts +0 -279
  35. package/src/vite-env.d.ts +0 -3
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Handle returning pre-configured responses.
3
+ *
4
+ * This is one of the nullability patterns from James Shore's article on
5
+ * [testing without mocks](https://www.jamesshore.com/v2/projects/nullables/testing-without-mocks#configurable-responses).
6
+ *
7
+ * Example usage for stubbing `fetch` function:
8
+ *
9
+ * ```javascript
10
+ * function createFetchStub(responses) {
11
+ * const configurableResponses = ConfigurableResponses.create(responses);
12
+ * return async function () {
13
+ * const response = configurableResponses.next();
14
+ * return {
15
+ * status: response.status,
16
+ * json: async () => response.body,
17
+ * };
18
+ * };
19
+ * }
20
+ * ```
21
+ */
22
+ export declare class ConfigurableResponses<T = unknown> {
23
+ #private;
24
+ /**
25
+ * Create a list of responses (by providing an array), or a single repeating
26
+ * response (by providing any other type). 'Name' is optional and used in
27
+ * error messages.
28
+ *
29
+ * @param responses A single response or an array of responses.
30
+ * @param name An optional name for the responses.
31
+ */
32
+ static create<T>(responses?: T | T[], name?: string): ConfigurableResponses<T>;
33
+ /**
34
+ * Convert all properties in an object into ConfigurableResponse instances.
35
+ * For example, { a: 1 } becomes { a: ConfigurableResponses.create(1) }.
36
+ * 'Name' is optional and used in error messages.
37
+ *
38
+ * @param responseObject An object with single response or an array of responses.
39
+ * @param name An optional name for the responses.
40
+ */
41
+ static mapObject<T extends Record<string, unknown>>(responseObject: T, name?: string): Record<string, ConfigurableResponses>;
42
+ /**
43
+ * Create a list of responses (by providing an array), or a single repeating
44
+ * response (by providing any other type). 'Name' is optional and used in
45
+ * error messages.
46
+ *
47
+ * @param responses A single response or an array of responses.
48
+ * @param name An optional name for the responses.
49
+ */
50
+ constructor(responses?: T | T[], name?: string);
51
+ /**
52
+ * Get the next configured response. Throws an error when configured with a list
53
+ * of responses and no more responses remain.
54
+ *
55
+ * @return The next response.
56
+ */
57
+ next(): T;
58
+ }
@@ -0,0 +1,34 @@
1
+ import type { Log, LogLevel } from "../common/mod";
2
+ import { OutputTracker } from "./output_tracker";
3
+ export interface ConsoleMessage {
4
+ level: LogLevel;
5
+ message: unknown[];
6
+ }
7
+ /**
8
+ * Wraps the console interface and allow setting the log level.
9
+ *
10
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Console_API
11
+ */
12
+ export declare class ConsoleLog extends EventTarget implements Log {
13
+ #private;
14
+ static create({ name }?: {
15
+ name?: string;
16
+ }): ConsoleLog;
17
+ static createNull({ name }?: {
18
+ name?: string;
19
+ }): ConsoleLog;
20
+ name?: string;
21
+ level: LogLevel;
22
+ private constructor();
23
+ log(...data: unknown[]): void;
24
+ error(...data: unknown[]): void;
25
+ warn(...data: unknown[]): void;
26
+ info(...data: unknown[]): void;
27
+ debug(...data: unknown[]): void;
28
+ trace(...data: unknown[]): void;
29
+ /**
30
+ * Track the console messages.
31
+ */
32
+ trackMessages(): OutputTracker<ConsoleMessage>;
33
+ isLoggable(level: LogLevel): boolean;
34
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * This data object configures the response of a fetch stub call.
3
+ */
4
+ export interface ResponseData {
5
+ /** The HTTP status code. */
6
+ status: number;
7
+ /** The HTTP status text. */
8
+ statusText: string;
9
+ /** The optional response body. */
10
+ body?: BodyInit | object;
11
+ }
12
+ /**
13
+ * Create a fetch stub.
14
+ *
15
+ * The stub returns a response from the provided response data or throws an provided error.
16
+ *
17
+ * @param responses A single response or an array of responses.
18
+ * @returns The fetch stub.
19
+ */
20
+ export declare function createFetchStub(responses?: ResponseData | Error | (ResponseData | Error)[]): () => Promise<Response>;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * An interface for a streaming message client.
3
+ *
4
+ * Emits the following events:
5
+ *
6
+ * - open, {@link Event}
7
+ * - message, {@link MessageEvent}
8
+ * - error, {@link Event}
9
+ * - close, optional {@link CloseEvent}
10
+ *
11
+ * It is used for wrappers around {@link EventSource} and {@link WebSocket}.
12
+ *
13
+ * @see {@link SseClient}
14
+ * @see {@link WebSocketClient}
15
+ */
16
+ export interface MessageClient extends EventTarget {
17
+ /**
18
+ * Return whether the client is connected.
19
+ */
20
+ get isConnected(): boolean;
21
+ /**
22
+ * Return the server URL.
23
+ */
24
+ get url(): string | undefined;
25
+ /**
26
+ * Connect to the server.
27
+ *
28
+ * @param url The server URL to connect to.
29
+ */
30
+ connect(url: string | URL): Promise<void>;
31
+ /**
32
+ * Send a message to the server.
33
+ *
34
+ * This is an optional method for streams with bidirectional communication.
35
+ *
36
+ * @param message The message to send.
37
+ * @param type The optional message type.
38
+ */
39
+ send(message: string, type?: string): Promise<void>;
40
+ /**
41
+ * Close the connection.
42
+ */
43
+ close(): Promise<void>;
44
+ }
@@ -1,5 +1,3 @@
1
- // Copyright (c) 2025 Falko Schumann. All rights reserved. MIT license.
2
-
3
1
  export * from "./configurable_responses";
4
2
  export * from "./console_log";
5
3
  export * from "./fetch_stub";
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Track output events.
3
+ *
4
+ * This is one of the nullability patterns from James Shore's article on
5
+ * [testing without mocks](https://www.jamesshore.com/v2/projects/nullables/testing-without-mocks#output-tracking).
6
+ *
7
+ * Example implementation of an event store:
8
+ *
9
+ * ```javascript
10
+ * async record(event) {
11
+ * // ...
12
+ * this.dispatchEvent(new CustomEvent("eventRecorded", { detail: event }));
13
+ * }
14
+ *
15
+ * trackEventsRecorded() {
16
+ * return new OutputTracker(this, "eventRecorded");
17
+ * }
18
+ * ```
19
+ *
20
+ * Example usage:
21
+ *
22
+ * ```javascript
23
+ * const eventsRecorded = eventStore.trackEventsRecorded();
24
+ * // ...
25
+ * const data = eventsRecorded.data(); // [event1, event2, ...]
26
+ * ```
27
+ */
28
+ export declare class OutputTracker<T = unknown> {
29
+ #private;
30
+ /**
31
+ * Create a tracker for a specific event of an event target.
32
+ *
33
+ * @param eventTarget The target to track.
34
+ * @param event The event name to track.
35
+ */
36
+ static create<T>(eventTarget: EventTarget, event: string): OutputTracker<T>;
37
+ /**
38
+ * Create a tracker for a specific event of an event target.
39
+ *
40
+ * @param eventTarget The target to track.
41
+ * @param event The event name to track.
42
+ */
43
+ constructor(eventTarget: EventTarget, event: string);
44
+ /**
45
+ * Return the tracked data.
46
+ *
47
+ * @return The tracked data.
48
+ */
49
+ get data(): T[];
50
+ /**
51
+ * Clear the tracked data and return the cleared data.
52
+ *
53
+ * @return The cleared data.
54
+ */
55
+ clear(): T[];
56
+ /**
57
+ * Stop tracking.
58
+ */
59
+ stop(): void;
60
+ }
@@ -0,0 +1,37 @@
1
+ import type { MessageClient } from "./message_client";
2
+ /**
3
+ * A client for the server-sent events protocol.
4
+ */
5
+ export declare class SseClient extends EventTarget implements MessageClient {
6
+ #private;
7
+ /**
8
+ * Create an SSE client.
9
+ *
10
+ * @return A new SSE client.
11
+ */
12
+ static create(): SseClient;
13
+ /**
14
+ * Create a nulled SSE client.
15
+ *
16
+ * @return A new SSE client.
17
+ */
18
+ static createNull(): SseClient;
19
+ private constructor();
20
+ get isConnected(): boolean;
21
+ get url(): string | undefined;
22
+ connect(url: string | URL, eventName?: string, ...otherEvents: string[]): Promise<void>;
23
+ send(_message: string, _type?: string): Promise<void>;
24
+ close(): Promise<void>;
25
+ /**
26
+ * Simulate a message event from the server.
27
+ *
28
+ * @param message The message to receive.
29
+ * @param eventName The optional event type.
30
+ * @param lastEventId The optional last event ID.
31
+ */
32
+ simulateMessage(message: string | number | boolean | object | null, eventName?: string, lastEventId?: string): void;
33
+ /**
34
+ * Simulate an error event.
35
+ */
36
+ simulateError(): void;
37
+ }
@@ -0,0 +1,79 @@
1
+ import { OutputTracker } from "./output_tracker";
2
+ import type { MessageClient } from "./message_client";
3
+ export declare const HEARTBEAT_TYPE = "heartbeat";
4
+ /**
5
+ * Options for the WebSocket client.
6
+ */
7
+ export interface WebSocketOptions {
8
+ /**
9
+ * The heartbeat interval in milliseconds. A value <= 0 disables the
10
+ * heartbeat.
11
+ */
12
+ heartbeat?: number;
13
+ /**
14
+ * The time in milliseconds to wait before retrying a connection after an
15
+ * error. A value <= 0 disables automatic retries.
16
+ */
17
+ retry?: number;
18
+ }
19
+ /**
20
+ * A client for the WebSocket protocol.
21
+ */
22
+ export declare class WebSocketClient extends EventTarget implements MessageClient {
23
+ #private;
24
+ /**
25
+ * Create a WebSocket client.
26
+ *
27
+ * @param options The options for the WebSocket client.
28
+ * @return A new WebSocket client.
29
+ */
30
+ static create({ heartbeat, retry, }?: WebSocketOptions): WebSocketClient;
31
+ /**
32
+ * Create a nulled WebSocket client.
33
+ *
34
+ * @param options The options for the WebSocket client.
35
+ * @return A new nulled WebSocket client.
36
+ */
37
+ static createNull({ heartbeat, retry }?: WebSocketOptions): WebSocketClient;
38
+ private constructor();
39
+ get isConnected(): boolean;
40
+ get url(): string | undefined;
41
+ connect(url: string | URL): Promise<void>;
42
+ send(message: string | ArrayBuffer | Blob | ArrayBufferView): Promise<void>;
43
+ /**
44
+ * Return a tracker for messages sent.
45
+ *
46
+ * @return A new output tracker.
47
+ */
48
+ trackMessageSent(): OutputTracker<string>;
49
+ /**
50
+ * Close the connection.
51
+ *
52
+ * If a code is provided, also a reason should be provided.
53
+ *
54
+ * @param code An optional code.
55
+ * @param reason An optional reason.
56
+ */
57
+ close(code?: number, reason?: string): Promise<void>;
58
+ /**
59
+ * Simulate a message event from the server.
60
+ *
61
+ * @param message The message to receive.
62
+ */
63
+ simulateMessage(message: string | number | boolean | object | null | Blob | ArrayBuffer): void;
64
+ /**
65
+ * Simulate a heartbeat.
66
+ */
67
+ simulateHeartbeat(): void;
68
+ /**
69
+ * Simulate a close event.
70
+ *
71
+ * @param code An optional code.
72
+ * @param reason An optional reason.
73
+ */
74
+ simulateClose(code?: number, reason?: string): void;
75
+ /**
76
+ * Simulate an error event.
77
+ */
78
+ simulateError(): void;
79
+ }
@@ -1,5 +1,3 @@
1
- // Copyright (c) 2025 Falko Schumann. All rights reserved. MIT license.
2
-
3
1
  export * from "./common/mod";
4
2
  export * from "./domain/mod";
5
3
  export * from "./infrastructure/mod";
package/package.json CHANGED
@@ -1,43 +1,34 @@
1
1
  {
2
2
  "name": "@muspellheim/shared",
3
- "version": "0.17.0",
3
+ "version": "0.18.1",
4
4
  "author": "Falko Schumann",
5
5
  "license": "MIT",
6
6
  "engines": {
7
- "node": ">=20.0.0"
7
+ "node": ">=20.0.0",
8
+ "bun": ">=1.0.0"
8
9
  },
9
10
  "type": "module",
10
11
  "files": [
11
- "dist",
12
- "src"
12
+ "dist"
13
13
  ],
14
- "main": "./dist/shared.umd.cjs",
15
- "module": "./dist/shared.js",
16
- "exports": {
17
- ".": {
18
- "types": "./dist/shared.d.ts",
19
- "import": "./dist/shared.js",
20
- "require": "./dist/shared.umd.cjs"
21
- }
22
- },
14
+ "main": "./dist/mod.cjs",
15
+ "module": "./dist/mod.js",
16
+ "types": "./dist/types/mod.d.ts",
23
17
  "scripts": {
24
- "build": "vite build",
18
+ "build": "tsc --project tsconfig.build.json && bun build src/mod.ts --outdir=dist",
25
19
  "test": "vitest"
26
20
  },
27
21
  "devDependencies": {
28
22
  "@eslint/js": "9.39.2",
29
- "@types/node": "20.19.28",
30
- "@vitest/coverage-v8": "4.0.16",
23
+ "@softarc/sheriff-core": "0.19.6",
24
+ "@vitest/coverage-istanbul": "4.0.18",
31
25
  "eslint": "9.39.2",
32
26
  "eslint-plugin-headers": "1.3.3",
33
- "jsdom": "27.4.0",
34
- "globals": "17.0.0",
35
- "prettier": "3.7.4",
27
+ "globals": "17.3.0",
28
+ "prettier": "3.8.1",
36
29
  "typedoc": "0.28.16",
37
30
  "typescript": "5.9.3",
38
- "typescript-eslint": "8.52.0",
39
- "vite": "7.3.1",
40
- "vitest": "4.0.16",
41
- "vite-plugin-dts": "4.5.4"
31
+ "typescript-eslint": "8.54.0",
32
+ "vitest": "4.0.18"
42
33
  }
43
34
  }