@projected/messaging 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/commands.d.ts +62 -0
- package/dist/commands.d.ts.map +1 -0
- package/dist/commands.js +119 -0
- package/dist/commands.js.map +1 -0
- package/dist/events.d.ts +44 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +67 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/memory.d.ts +59 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +227 -0
- package/dist/memory.js.map +1 -0
- package/dist/replay.d.ts +26 -0
- package/dist/replay.d.ts.map +1 -0
- package/dist/replay.js +51 -0
- package/dist/replay.js.map +1 -0
- package/dist/timing.d.ts +8 -0
- package/dist/timing.d.ts.map +1 -0
- package/dist/timing.js +17 -0
- package/dist/timing.js.map +1 -0
- package/dist/transport.d.ts +61 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +16 -0
- package/dist/transport.js.map +1 -0
- package/package.json +36 -3
- package/src/commands.ts +188 -0
- package/src/events.ts +103 -0
- package/src/index.ts +21 -0
- package/src/memory.ts +298 -0
- package/src/replay.ts +68 -0
- package/src/timing.ts +21 -0
- package/src/transport.ts +77 -0
- package/README.md +0 -3
package/src/timing.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Wait `ms` milliseconds. */
|
|
2
|
+
export const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `work`, or a rejection once `ms` have passed without an answer: for a transport's `ping()`,
|
|
6
|
+
* which must answer promptly even when its broker never will.
|
|
7
|
+
*/
|
|
8
|
+
export const withTimeout = <T>(ms: number, work: Promise<T>): Promise<T> =>
|
|
9
|
+
new Promise((resolve, reject) => {
|
|
10
|
+
const timer = setTimeout(() => reject(new Error(`no answer within ${ms} ms`)), ms);
|
|
11
|
+
work.then(
|
|
12
|
+
(value) => {
|
|
13
|
+
clearTimeout(timer);
|
|
14
|
+
resolve(value);
|
|
15
|
+
},
|
|
16
|
+
(error: unknown) => {
|
|
17
|
+
clearTimeout(timer);
|
|
18
|
+
reject(error);
|
|
19
|
+
},
|
|
20
|
+
);
|
|
21
|
+
});
|
package/src/transport.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Result, type DependencyUnavailable, type ReadinessCheck } from "@projected/application";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The transport SPI: the smallest surface a broker adapter (Kafka, SQS, NATS, RabbitMQ, ...) must
|
|
5
|
+
* implement. Delivery is at-least-once; consumers decide what happens to each message.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface OutgoingMessage {
|
|
9
|
+
/** Unique, stable id — the deduplication key for consumers. */
|
|
10
|
+
readonly id: string;
|
|
11
|
+
/**
|
|
12
|
+
* Partition or ordering key. Brokers that partition (Kafka) keep messages with the same key in
|
|
13
|
+
* order; relayed events use their stream name, so one aggregate's events stay ordered.
|
|
14
|
+
* Defaults to the id.
|
|
15
|
+
*/
|
|
16
|
+
readonly key?: string;
|
|
17
|
+
/** JSON-serializable body. */
|
|
18
|
+
readonly body: unknown;
|
|
19
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface Message extends OutgoingMessage {
|
|
23
|
+
readonly topic: string;
|
|
24
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
25
|
+
/** Delivery attempt, starting at 1. */
|
|
26
|
+
readonly attempt: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What to do with a delivered message. */
|
|
30
|
+
export type Disposition = "ack" | "retry" | "dead-letter";
|
|
31
|
+
|
|
32
|
+
export interface Subscription {
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface MessageTransport {
|
|
37
|
+
publish(topic: string, message: OutgoingMessage): Promise<Result<void, DependencyUnavailable>>;
|
|
38
|
+
/** Resolves once the subscription is receiving. `group` names a set of competing consumers. */
|
|
39
|
+
subscribe(
|
|
40
|
+
topic: string,
|
|
41
|
+
handler: (message: Message) => Promise<Disposition>,
|
|
42
|
+
options?: SubscribeOptions,
|
|
43
|
+
): Promise<Subscription>;
|
|
44
|
+
/**
|
|
45
|
+
* Whether the broker answers now — for readiness probes ({@link transportReadiness}). Must
|
|
46
|
+
* answer promptly, unavailable rather than hanging when the broker is unreachable.
|
|
47
|
+
*/
|
|
48
|
+
ping(): Promise<Result<void, DependencyUnavailable>>;
|
|
49
|
+
/** Release connections. */
|
|
50
|
+
close?(): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A transport as a readiness check: the service is not ready while its broker is unreachable. */
|
|
54
|
+
export function transportReadiness(name: string, transport: MessageTransport): ReadinessCheck {
|
|
55
|
+
return { name, check: () => transport.ping() };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface SubscribeOptions {
|
|
59
|
+
/**
|
|
60
|
+
* Consumers in the same group share the topic's messages; each group gets every message.
|
|
61
|
+
* Defaults to one group per topic. Name it after the consuming service.
|
|
62
|
+
*/
|
|
63
|
+
readonly group?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Name of the topic that receives a topic's dead letters. The same convention on every transport. */
|
|
67
|
+
export const deadLetterTopic = (topic: string) => `${topic}.dead-letter`;
|
|
68
|
+
|
|
69
|
+
/** Header on a dead letter saying why it was dead-lettered. */
|
|
70
|
+
export const DEAD_LETTER_REASON_HEADER = "projected-dead-letter-reason";
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Header on a message published again on purpose — by a dead-letter replay — under the id it had.
|
|
74
|
+
* A broker that drops repeated publishes of one id (NATS JetStream's deduplication) must count
|
|
75
|
+
* each replay as a new publish; consumers still see the original id.
|
|
76
|
+
*/
|
|
77
|
+
export const REPLAY_HEADER = "projected-replay";
|
package/README.md
DELETED