@projected/messaging 0.0.0-stage → 0.1.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 +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/dist/memory.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Message, type MessageTransport } from "./transport.js";
|
|
2
|
+
/**
|
|
3
|
+
* When `publish` returns, and who delivers:
|
|
4
|
+
*
|
|
5
|
+
* - `"acknowledged"` — the transport delivers at once, and `publish` returns when every consumer
|
|
6
|
+
* group subscribed to the topic has handled the message. A relay or policy only moves its
|
|
7
|
+
* checkpoint past an event once its consumers are done, so a crash loses nothing: after a
|
|
8
|
+
* restart they publish it again. At-least-once, as with a broker.
|
|
9
|
+
* - `"async"` — the transport delivers at once, and `publish` returns as soon as the message is
|
|
10
|
+
* queued. Faster; a message not yet handled when the process dies is lost.
|
|
11
|
+
* - `"queued"` — nothing is delivered until {@link InMemoryTransport.drain}: for tests that step
|
|
12
|
+
* through.
|
|
13
|
+
*/
|
|
14
|
+
export type InMemoryDelivery = "acknowledged" | "async" | "queued";
|
|
15
|
+
export interface InMemoryTransportOptions {
|
|
16
|
+
/** Defaults to `"acknowledged"`. */
|
|
17
|
+
readonly delivery?: InMemoryDelivery;
|
|
18
|
+
/** Deliveries per message before it is dead-lettered. Defaults to 3. */
|
|
19
|
+
readonly maxAttempts?: number;
|
|
20
|
+
/** Wait before delivery `attempt + 1`, except when queued. Defaults to 50 ms doubling, at most 2 s. */
|
|
21
|
+
readonly retryDelayMs?: (attempt: number) => number;
|
|
22
|
+
/**
|
|
23
|
+
* Messages kept per topic for consumer groups that subscribe later, and at most waiting for a
|
|
24
|
+
* group with no subscriber; also the length of `published` and `deadLetters`. Defaults to 10 000.
|
|
25
|
+
*/
|
|
26
|
+
readonly retain?: number;
|
|
27
|
+
/** How long an acknowledged `publish` waits for its consumers before it fails. Defaults to 30 s. */
|
|
28
|
+
readonly ackTimeoutMs?: number;
|
|
29
|
+
/** A consumer that threw (its message is retried), or a message dropped for lack of room. */
|
|
30
|
+
readonly onError?: (error: unknown) => void;
|
|
31
|
+
/** A message set aside, and why. Dead letters live in memory: log or store them to keep them. */
|
|
32
|
+
readonly onDeadLetter?: (message: Message, reason: string) => void;
|
|
33
|
+
}
|
|
34
|
+
/** A {@link MessageTransport} within one process, with no broker. */
|
|
35
|
+
export interface InMemoryTransport extends MessageTransport {
|
|
36
|
+
/**
|
|
37
|
+
* Deliver what is waiting. Queued: deliver everything, retries included, and return the number
|
|
38
|
+
* of deliveries. Otherwise: wait until nothing is waiting or being delivered.
|
|
39
|
+
*/
|
|
40
|
+
drain(): Promise<number>;
|
|
41
|
+
/** Deliver what is waiting, then stop: later publishes fail, and so does `ping()`. */
|
|
42
|
+
close(): Promise<void>;
|
|
43
|
+
/** The latest messages published, oldest first. */
|
|
44
|
+
readonly published: readonly Message[];
|
|
45
|
+
/** The latest messages dead-lettered, oldest first. */
|
|
46
|
+
readonly deadLetters: readonly Message[];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create an {@link InMemoryTransport}: a broker's semantics within one process — consumer groups
|
|
50
|
+
* that each get every message, members of a group taking turns, per-key order, bounded
|
|
51
|
+
* redelivery and `<topic>.dead-letter` — so that `consumeCommands`, `consumeEvents` and
|
|
52
|
+
* `eventRelay` work the same with or without Kafka or NATS. Each group handles one message at a
|
|
53
|
+
* time; a message asking to be retried is redelivered before anything after it.
|
|
54
|
+
*
|
|
55
|
+
* With `"acknowledged"` delivery, subscribe consumers before relays start: a message published
|
|
56
|
+
* while a topic has no consumer group is kept only in memory, for groups that subscribe later.
|
|
57
|
+
*/
|
|
58
|
+
export declare function inMemoryTransport(options?: InMemoryTransportOptions): InMemoryTransport;
|
|
59
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../src/memory.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,OAAO,EACZ,KAAK,gBAAgB,EAGtB,MAAM,gBAAgB,CAAC;AAExB;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,gBAAgB,GAAG,cAAc,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEnE,MAAM,WAAW,wBAAwB;IACvC,oCAAoC;IACpC,QAAQ,CAAC,QAAQ,CAAC,EAAE,gBAAgB,CAAC;IACrC,wEAAwE;IACxE,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,uGAAuG;IACvG,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IACpD;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,oGAAoG;IACpG,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,6FAA6F;IAC7F,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IAC5C,iGAAiG;IACjG,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACpE;AAED,qEAAqE;AACrE,MAAM,WAAW,iBAAkB,SAAQ,gBAAgB;IACzD;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,sFAAsF;IACtF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,mDAAmD;IACnD,QAAQ,CAAC,SAAS,EAAE,SAAS,OAAO,EAAE,CAAC;IACvC,uDAAuD;IACvD,QAAQ,CAAC,WAAW,EAAE,SAAS,OAAO,EAAE,CAAC;CAC1C;AAoCD;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,GAAG,iBAAiB,CAiM3F"}
|
package/dist/memory.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { DependencyUnavailable, Result } from "@projected/application";
|
|
2
|
+
import { DEAD_LETTER_REASON_HEADER, deadLetterTopic, } from "./transport.js";
|
|
3
|
+
const defaultRetryDelay = (attempt) => Math.min(50 * 2 ** (attempt - 1), 2_000);
|
|
4
|
+
/** Tell the waiting publisher, once, whether its group handled the message. */
|
|
5
|
+
const settle = (pending, handled) => {
|
|
6
|
+
const waiting = pending.settle;
|
|
7
|
+
pending.settle = undefined;
|
|
8
|
+
waiting?.(handled);
|
|
9
|
+
};
|
|
10
|
+
/** A copy as a broker would give it back: through JSON. */
|
|
11
|
+
const asJson = (value) => (value === undefined ? value : JSON.parse(JSON.stringify(value)));
|
|
12
|
+
/**
|
|
13
|
+
* Create an {@link InMemoryTransport}: a broker's semantics within one process — consumer groups
|
|
14
|
+
* that each get every message, members of a group taking turns, per-key order, bounded
|
|
15
|
+
* redelivery and `<topic>.dead-letter` — so that `consumeCommands`, `consumeEvents` and
|
|
16
|
+
* `eventRelay` work the same with or without Kafka or NATS. Each group handles one message at a
|
|
17
|
+
* time; a message asking to be retried is redelivered before anything after it.
|
|
18
|
+
*
|
|
19
|
+
* With `"acknowledged"` delivery, subscribe consumers before relays start: a message published
|
|
20
|
+
* while a topic has no consumer group is kept only in memory, for groups that subscribe later.
|
|
21
|
+
*/
|
|
22
|
+
export function inMemoryTransport(options = {}) {
|
|
23
|
+
const delivery = options.delivery ?? "acknowledged";
|
|
24
|
+
const maxAttempts = options.maxAttempts ?? 3;
|
|
25
|
+
const retryDelay = options.retryDelayMs ?? defaultRetryDelay;
|
|
26
|
+
const retain = options.retain ?? 10_000;
|
|
27
|
+
const ackTimeoutMs = options.ackTimeoutMs ?? 30_000;
|
|
28
|
+
const onError = options.onError ?? (() => undefined);
|
|
29
|
+
const topics = new Map();
|
|
30
|
+
const published = [];
|
|
31
|
+
const deadLetters = [];
|
|
32
|
+
let closed = false;
|
|
33
|
+
let deliveries = 0;
|
|
34
|
+
const keepLast = (list, item, onDrop) => {
|
|
35
|
+
list.push(item);
|
|
36
|
+
while (list.length > retain) {
|
|
37
|
+
const dropped = list.shift();
|
|
38
|
+
if (dropped !== undefined)
|
|
39
|
+
onDrop?.(dropped);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const topicOf = (name) => {
|
|
43
|
+
let topic = topics.get(name);
|
|
44
|
+
if (topic === undefined)
|
|
45
|
+
topics.set(name, (topic = { log: [], groups: new Map() }));
|
|
46
|
+
return topic;
|
|
47
|
+
};
|
|
48
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
49
|
+
const deadLetter = (message, reason) => {
|
|
50
|
+
const dead = { ...message, topic: deadLetterTopic(message.topic), headers: { ...message.headers, [DEAD_LETTER_REASON_HEADER]: reason }, attempt: 1 };
|
|
51
|
+
keepLast(deadLetters, dead);
|
|
52
|
+
options.onDeadLetter?.(dead, reason);
|
|
53
|
+
enqueue(dead);
|
|
54
|
+
};
|
|
55
|
+
/** Hand `message` to every group of its topic, each telling `waitFor` how it went; returns how many groups. */
|
|
56
|
+
const enqueue = (message, waitFor) => {
|
|
57
|
+
const topic = topicOf(message.topic);
|
|
58
|
+
keepLast(topic.log, message);
|
|
59
|
+
let groups = 0;
|
|
60
|
+
for (const group of topic.groups.values()) {
|
|
61
|
+
groups++;
|
|
62
|
+
keepLast(group.pending, { message, ...(waitFor === undefined ? {} : { settle: waitFor }) }, (dropped) => {
|
|
63
|
+
settle(dropped, false);
|
|
64
|
+
onError(new Error(`in-memory transport: group ${group.name} dropped ${dropped.message.id} on ${message.topic}, more than ${retain} waiting`));
|
|
65
|
+
});
|
|
66
|
+
schedule(group);
|
|
67
|
+
}
|
|
68
|
+
return groups;
|
|
69
|
+
};
|
|
70
|
+
/** Deliver the group's head message once, and say what became of it. */
|
|
71
|
+
const deliverHead = async (group, retryAt) => {
|
|
72
|
+
const head = group.pending[0];
|
|
73
|
+
const member = group.members[group.next % Math.max(group.members.length, 1)];
|
|
74
|
+
if (head === undefined || member === undefined)
|
|
75
|
+
return "idle";
|
|
76
|
+
group.next++;
|
|
77
|
+
deliveries++;
|
|
78
|
+
let disposition;
|
|
79
|
+
try {
|
|
80
|
+
disposition = await member(head.message);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
onError(error);
|
|
84
|
+
disposition = "retry";
|
|
85
|
+
}
|
|
86
|
+
const { message } = head;
|
|
87
|
+
if (disposition === "retry" && message.attempt < maxAttempts) {
|
|
88
|
+
// In place: nothing after it reaches the group first, so per-key order holds.
|
|
89
|
+
group.pending[0] = { ...head, message: { ...message, attempt: message.attempt + 1 } };
|
|
90
|
+
if (retryAt === "later")
|
|
91
|
+
await wait(retryDelay(message.attempt));
|
|
92
|
+
return "retry";
|
|
93
|
+
}
|
|
94
|
+
group.pending.shift();
|
|
95
|
+
if (disposition !== "ack")
|
|
96
|
+
deadLetter(message, disposition === "dead-letter" ? "rejected" : `failed ${maxAttempts} deliveries`);
|
|
97
|
+
settle(head, true);
|
|
98
|
+
return "done";
|
|
99
|
+
};
|
|
100
|
+
/** Start the group's delivery loop on a later tick, never inside the publisher's call. */
|
|
101
|
+
const schedule = (group) => {
|
|
102
|
+
if (delivery === "queued" || group.running !== undefined || closed)
|
|
103
|
+
return;
|
|
104
|
+
group.running = (async () => {
|
|
105
|
+
await wait(0);
|
|
106
|
+
while (!closed && (await deliverHead(group, "later")) !== "idle")
|
|
107
|
+
;
|
|
108
|
+
})().finally(() => {
|
|
109
|
+
group.running = undefined;
|
|
110
|
+
if (!closed && group.pending.length > 0 && group.members.length > 0)
|
|
111
|
+
schedule(group);
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
const allGroups = () => [...topics.values()].flatMap((topic) => [...topic.groups.values()]);
|
|
115
|
+
const busy = () => allGroups().filter((group) => group.running !== undefined || (group.pending.length > 0 && group.members.length > 0));
|
|
116
|
+
const drain = async () => {
|
|
117
|
+
const before = deliveries;
|
|
118
|
+
if (closed)
|
|
119
|
+
return 0;
|
|
120
|
+
for (let groups = busy(); groups.length > 0; groups = busy()) {
|
|
121
|
+
if (delivery === "queued") {
|
|
122
|
+
for (const group of groups)
|
|
123
|
+
while ((await deliverHead(group, "now")) !== "idle")
|
|
124
|
+
;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
groups.forEach(schedule);
|
|
128
|
+
await Promise.all(groups.map((group) => group.running));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return deliveries - before;
|
|
132
|
+
};
|
|
133
|
+
const unavailable = (message) => Result.err(new DependencyUnavailable({ dependency: "in-memory transport", message: `in-memory transport: ${message}` }));
|
|
134
|
+
return {
|
|
135
|
+
get published() {
|
|
136
|
+
return [...published];
|
|
137
|
+
},
|
|
138
|
+
get deadLetters() {
|
|
139
|
+
return [...deadLetters];
|
|
140
|
+
},
|
|
141
|
+
ping: async () => (closed ? unavailable("closed") : Result.ok(undefined)),
|
|
142
|
+
publish: async (topicName, outgoing) => {
|
|
143
|
+
if (closed)
|
|
144
|
+
return unavailable(`closed, cannot publish to ${topicName}`);
|
|
145
|
+
let message;
|
|
146
|
+
try {
|
|
147
|
+
message = { id: outgoing.id, key: outgoing.key ?? outgoing.id, topic: topicName, body: asJson(outgoing.body), headers: asJson({ ...outgoing.headers }), attempt: 1 };
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// A broker would fail to serialize it too; nothing was sent.
|
|
151
|
+
return unavailable(`the body for ${topicName} is not JSON`);
|
|
152
|
+
}
|
|
153
|
+
keepLast(published, message);
|
|
154
|
+
if (delivery !== "acknowledged") {
|
|
155
|
+
enqueue(message);
|
|
156
|
+
return Result.ok(undefined);
|
|
157
|
+
}
|
|
158
|
+
// A group nobody listens to cannot acknowledge: it keeps the message, and the publisher hears
|
|
159
|
+
// at once that it is not done — rather than at its timeout, or never.
|
|
160
|
+
const absent = [...(topics.get(topicName)?.groups.values() ?? [])].filter((group) => group.members.length === 0).map((group) => group.name);
|
|
161
|
+
if (absent.length > 0) {
|
|
162
|
+
enqueue(message);
|
|
163
|
+
return unavailable(`no subscriber in group ${absent.join(", ")} for ${topicName}; kept for it`);
|
|
164
|
+
}
|
|
165
|
+
let outstanding = 0;
|
|
166
|
+
let failed = false;
|
|
167
|
+
let done = () => { };
|
|
168
|
+
const acknowledged = new Promise((resolve) => (done = resolve));
|
|
169
|
+
outstanding = enqueue(message, (handled) => {
|
|
170
|
+
failed ||= !handled;
|
|
171
|
+
if (--outstanding === 0)
|
|
172
|
+
done();
|
|
173
|
+
});
|
|
174
|
+
if (outstanding === 0)
|
|
175
|
+
return Result.ok(undefined);
|
|
176
|
+
let timer;
|
|
177
|
+
const timedOut = new Promise((resolve) => (timer = setTimeout(() => resolve("timeout"), ackTimeoutMs)));
|
|
178
|
+
const outcome = await Promise.race([acknowledged, timedOut]);
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
if (outcome === "timeout")
|
|
181
|
+
return unavailable(`no acknowledgement for ${message.id} on ${topicName} within ${ackTimeoutMs} ms`);
|
|
182
|
+
return failed ? unavailable(`${message.id} on ${topicName} was not acknowledged before its consumers stopped`) : Result.ok(undefined);
|
|
183
|
+
},
|
|
184
|
+
subscribe: async (topicName, handler, subscribeOptions = {}) => {
|
|
185
|
+
if (closed)
|
|
186
|
+
throw new Error(`in-memory transport: closed, cannot subscribe to ${topicName}`);
|
|
187
|
+
const topic = topicOf(topicName);
|
|
188
|
+
const name = subscribeOptions.group ?? "default";
|
|
189
|
+
let group = topic.groups.get(name);
|
|
190
|
+
if (group === undefined) {
|
|
191
|
+
group = { name, members: [], next: 0, pending: topic.log.map((message) => ({ message })) };
|
|
192
|
+
topic.groups.set(name, group);
|
|
193
|
+
}
|
|
194
|
+
const joined = group;
|
|
195
|
+
joined.members.push(handler);
|
|
196
|
+
schedule(joined);
|
|
197
|
+
return {
|
|
198
|
+
close: async () => {
|
|
199
|
+
const index = joined.members.indexOf(handler);
|
|
200
|
+
if (index === -1)
|
|
201
|
+
return;
|
|
202
|
+
joined.members.splice(index, 1);
|
|
203
|
+
if (joined.members.length > 0)
|
|
204
|
+
return;
|
|
205
|
+
await joined.running;
|
|
206
|
+
// Publishers waiting on this group learn now, rather than at their timeout.
|
|
207
|
+
for (const pending of joined.pending)
|
|
208
|
+
settle(pending, false);
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
},
|
|
212
|
+
drain,
|
|
213
|
+
close: async () => {
|
|
214
|
+
if (closed)
|
|
215
|
+
return;
|
|
216
|
+
if (delivery !== "queued")
|
|
217
|
+
await drain();
|
|
218
|
+
closed = true;
|
|
219
|
+
const groups = allGroups();
|
|
220
|
+
await Promise.all(groups.map((group) => group.running));
|
|
221
|
+
for (const group of groups)
|
|
222
|
+
for (const pending of group.pending)
|
|
223
|
+
settle(pending, false);
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../src/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EACL,yBAAyB,EACzB,eAAe,GAMhB,MAAM,gBAAgB,CAAC;AAyExB,MAAM,iBAAiB,GAAG,CAAC,OAAe,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAExF,+EAA+E;AAC/E,MAAM,MAAM,GAAG,CAAC,OAAgB,EAAE,OAAgB,EAAE,EAAE;IACpD,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAC/B,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAC3B,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC;AACrB,CAAC,CAAC;AAEF,2DAA2D;AAC3D,MAAM,MAAM,GAAG,CAAI,KAAQ,EAAK,EAAE,CAAC,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAO,CAAC,CAAC;AAE5G;;;;;;;;;GASG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAoC,EAAE;IACtE,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,cAAc,CAAC;IACpD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,CAAC,CAAC;IAC7C,MAAM,UAAU,GAAG,OAAO,CAAC,YAAY,IAAI,iBAAiB,CAAC;IAC7D,MAAM,MAAM,GAAG,OAA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AC,OAAO,CAAC,MAAM,GAAG,CAAC;wBAAE,OAAO;oBACtC,MAAM,MAAM,CAAC,OAAO,CAAC;oBACrB,4EAA4E;oBAC5E,KAAK,MAAM,OAAO,IAAI,MAAM,CAAC,OAAO;wBAAE,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;gBAC/D,CAAC;aACqB,CAAC;QAC3B,CAAC;QAED,KAAK;QAEL,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,IAAI,MAAM;gBAAE,OAAO;YACnB,IAAI,QAAQ,KAAK,QAAQ;gBAAE,MAAM,KAAK,EAAE,CAAC;YACzC,MAAM,GAAG,IAAI,CAAC;YACd,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;YAC3B,MAAM,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;YACxD,KAAK,MAAM,KAAK,IAAI,MAAM;gBAAE,KAAK,MAAM,OAAO,IAAI,KAAK,CAAC,OAAO;oBAAE,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAC1F,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/replay.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type OperatorTask } from "@projected/application";
|
|
2
|
+
import { type MessageTransport } from "./transport.js";
|
|
3
|
+
export interface DeadLetterReplayOptions {
|
|
4
|
+
/** The topics whose dead letters may be replayed: the ones this service consumes. */
|
|
5
|
+
readonly topics: readonly [string, ...string[]];
|
|
6
|
+
/**
|
|
7
|
+
* The consumer group reading the dead-letter topics. The broker keeps its position, so each
|
|
8
|
+
* dead letter is replayed once. Defaults to `projected-dead-letter-replay`.
|
|
9
|
+
*/
|
|
10
|
+
readonly group?: string;
|
|
11
|
+
/** Finish once no dead letter has arrived for this long. Defaults to 2 s. */
|
|
12
|
+
readonly idleMs?: number;
|
|
13
|
+
/** Finish after this long, whatever is still arriving; the rest waits for the next run. Defaults to 60 s. */
|
|
14
|
+
readonly maxMs?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The `replay-dead-letters` operator task: publish a topic's dead letters back to the topic, under
|
|
18
|
+
* their own ids, once whatever made them fail is fixed. Commands rerun (a dead-lettered command
|
|
19
|
+
* never completed, so its idempotency key is free) and events reach their consumers again.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* createApplication(contracts, { ..., operatorTasks: [deadLetterReplay(transport, { topics: ["billing.commands"] })] });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function deadLetterReplay(transport: MessageTransport, options: DeadLetterReplayOptions): OperatorTask;
|
|
26
|
+
//# sourceMappingURL=replay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6D,KAAK,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtH,OAAO,EAA6D,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElH,MAAM,WAAW,uBAAuB;IACtC,qFAAqF;IACrF,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAChD;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,6EAA6E;IAC7E,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,6GAA6G;IAC7G,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,gBAAgB,EAAE,OAAO,EAAE,uBAAuB,GAAG,YAAY,CAwC5G"}
|
package/dist/replay.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Result, defineOperatorTask, z } from "@projected/application";
|
|
2
|
+
import { sleep } from "./timing.js";
|
|
3
|
+
import { DEAD_LETTER_REASON_HEADER, REPLAY_HEADER, deadLetterTopic } from "./transport.js";
|
|
4
|
+
/**
|
|
5
|
+
* The `replay-dead-letters` operator task: publish a topic's dead letters back to the topic, under
|
|
6
|
+
* their own ids, once whatever made them fail is fixed. Commands rerun (a dead-lettered command
|
|
7
|
+
* never completed, so its idempotency key is free) and events reach their consumers again.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* createApplication(contracts, { ..., operatorTasks: [deadLetterReplay(transport, { topics: ["billing.commands"] })] });
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export function deadLetterReplay(transport, options) {
|
|
14
|
+
const group = options.group ?? "projected-dead-letter-replay";
|
|
15
|
+
const idleMs = options.idleMs ?? 2000;
|
|
16
|
+
const maxMs = options.maxMs ?? 60_000;
|
|
17
|
+
return defineOperatorTask({
|
|
18
|
+
name: "replay-dead-letters",
|
|
19
|
+
description: `Publish a topic's dead letters back to it, once what made them fail is fixed. Topics: ${options.topics.join(", ")}.`,
|
|
20
|
+
input: z.object({ topic: z.enum(options.topics) }),
|
|
21
|
+
run: async ({ topic }) => {
|
|
22
|
+
const replay = globalThis.crypto.randomUUID();
|
|
23
|
+
const started = Date.now();
|
|
24
|
+
let lastArrival = started;
|
|
25
|
+
let replayed = 0;
|
|
26
|
+
let failure;
|
|
27
|
+
const subscription = await transport.subscribe(deadLetterTopic(topic), async (message) => {
|
|
28
|
+
lastArrival = Date.now();
|
|
29
|
+
const { [DEAD_LETTER_REASON_HEADER]: _reason, ...headers } = message.headers;
|
|
30
|
+
const published = await transport.publish(topic, {
|
|
31
|
+
id: message.id,
|
|
32
|
+
key: message.key ?? message.id,
|
|
33
|
+
body: message.body,
|
|
34
|
+
headers: { ...headers, [REPLAY_HEADER]: replay },
|
|
35
|
+
});
|
|
36
|
+
if (published.isErr()) {
|
|
37
|
+
failure = published.error;
|
|
38
|
+
// Unacknowledged, so not lost: redelivered to the next run (or dead-lettered again once the transport gives up).
|
|
39
|
+
return "retry";
|
|
40
|
+
}
|
|
41
|
+
replayed++;
|
|
42
|
+
return "ack";
|
|
43
|
+
}, { group });
|
|
44
|
+
while (failure === undefined && Date.now() - lastArrival < idleMs && Date.now() - started < maxMs)
|
|
45
|
+
await sleep(Math.min(idleMs, 50));
|
|
46
|
+
await subscription.close();
|
|
47
|
+
return failure === undefined ? Result.ok({ topic, replayed }) : Result.err(failure);
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=replay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.js","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC,EAAiD,MAAM,wBAAwB,CAAC;AACtH,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,yBAAyB,EAAE,aAAa,EAAE,eAAe,EAAyB,MAAM,gBAAgB,CAAC;AAgBlH;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAA2B,EAAE,OAAgC;IAC5F,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,8BAA8B,CAAC;IAC9D,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC;IACtC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC;IACtC,OAAO,kBAAkB,CAAC;QACxB,IAAI,EAAE,qBAAqB;QAC3B,WAAW,EAAE,yFAAyF,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;QAClI,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,GAAG,EAAE,KAAK,EAAE,EAAE,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CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC;YACrI,MAAM,YAAY,CAAC,KAAK,EAAE,CAAC;YAC3B,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACtF,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
|
package/dist/timing.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Wait `ms` milliseconds. */
|
|
2
|
+
export declare const sleep: (ms: number) => Promise<void>;
|
|
3
|
+
/**
|
|
4
|
+
* `work`, or a rejection once `ms` have passed without an answer: for a transport's `ping()`,
|
|
5
|
+
* which must answer promptly even when its broker never will.
|
|
6
|
+
*/
|
|
7
|
+
export declare const withTimeout: <T>(ms: number, work: Promise<T>) => Promise<T>;
|
|
8
|
+
//# sourceMappingURL=timing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timing.d.ts","sourceRoot":"","sources":["../src/timing.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,IAAI,CAAsD,CAAC;AAEtG;;;GAGG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,IAAI,MAAM,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KAAG,OAAO,CAAC,CAAC,CAanE,CAAC"}
|
package/dist/timing.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Wait `ms` milliseconds. */
|
|
2
|
+
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
3
|
+
/**
|
|
4
|
+
* `work`, or a rejection once `ms` have passed without an answer: for a transport's `ping()`,
|
|
5
|
+
* which must answer promptly even when its broker never will.
|
|
6
|
+
*/
|
|
7
|
+
export const withTimeout = (ms, work) => new Promise((resolve, reject) => {
|
|
8
|
+
const timer = setTimeout(() => reject(new Error(`no answer within ${ms} ms`)), ms);
|
|
9
|
+
work.then((value) => {
|
|
10
|
+
clearTimeout(timer);
|
|
11
|
+
resolve(value);
|
|
12
|
+
}, (error) => {
|
|
13
|
+
clearTimeout(timer);
|
|
14
|
+
reject(error);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
//# sourceMappingURL=timing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timing.js","sourceRoot":"","sources":["../src/timing.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,EAAU,EAAiB,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAEtG;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAI,EAAU,EAAE,IAAgB,EAAc,EAAE,CACzE,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;IAC9B,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACnF,IAAI,CAAC,IAAI,CACP,CAAC,KAAK,EAAE,EAAE;QACR,YAAY,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;QACjB,YAAY,CAAC,KAAK,CAAC,CAAC;QACpB,MAAM,CAAC,KAAK,CAAC,CAAC;IAChB,CAAC,CACF,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Result, type DependencyUnavailable, type ReadinessCheck } from "@projected/application";
|
|
2
|
+
/**
|
|
3
|
+
* The transport SPI: the smallest surface a broker adapter (Kafka, SQS, NATS, RabbitMQ, ...) must
|
|
4
|
+
* implement. Delivery is at-least-once; consumers decide what happens to each message.
|
|
5
|
+
*/
|
|
6
|
+
export interface OutgoingMessage {
|
|
7
|
+
/** Unique, stable id — the deduplication key for consumers. */
|
|
8
|
+
readonly id: string;
|
|
9
|
+
/**
|
|
10
|
+
* Partition or ordering key. Brokers that partition (Kafka) keep messages with the same key in
|
|
11
|
+
* order; relayed events use their stream name, so one aggregate's events stay ordered.
|
|
12
|
+
* Defaults to the id.
|
|
13
|
+
*/
|
|
14
|
+
readonly key?: string;
|
|
15
|
+
/** JSON-serializable body. */
|
|
16
|
+
readonly body: unknown;
|
|
17
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
18
|
+
}
|
|
19
|
+
export interface Message extends OutgoingMessage {
|
|
20
|
+
readonly topic: string;
|
|
21
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
22
|
+
/** Delivery attempt, starting at 1. */
|
|
23
|
+
readonly attempt: number;
|
|
24
|
+
}
|
|
25
|
+
/** What to do with a delivered message. */
|
|
26
|
+
export type Disposition = "ack" | "retry" | "dead-letter";
|
|
27
|
+
export interface Subscription {
|
|
28
|
+
close(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
export interface MessageTransport {
|
|
31
|
+
publish(topic: string, message: OutgoingMessage): Promise<Result<void, DependencyUnavailable>>;
|
|
32
|
+
/** Resolves once the subscription is receiving. `group` names a set of competing consumers. */
|
|
33
|
+
subscribe(topic: string, handler: (message: Message) => Promise<Disposition>, options?: SubscribeOptions): Promise<Subscription>;
|
|
34
|
+
/**
|
|
35
|
+
* Whether the broker answers now — for readiness probes ({@link transportReadiness}). Must
|
|
36
|
+
* answer promptly, unavailable rather than hanging when the broker is unreachable.
|
|
37
|
+
*/
|
|
38
|
+
ping(): Promise<Result<void, DependencyUnavailable>>;
|
|
39
|
+
/** Release connections. */
|
|
40
|
+
close?(): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
/** A transport as a readiness check: the service is not ready while its broker is unreachable. */
|
|
43
|
+
export declare function transportReadiness(name: string, transport: MessageTransport): ReadinessCheck;
|
|
44
|
+
export interface SubscribeOptions {
|
|
45
|
+
/**
|
|
46
|
+
* Consumers in the same group share the topic's messages; each group gets every message.
|
|
47
|
+
* Defaults to one group per topic. Name it after the consuming service.
|
|
48
|
+
*/
|
|
49
|
+
readonly group?: string;
|
|
50
|
+
}
|
|
51
|
+
/** Name of the topic that receives a topic's dead letters. The same convention on every transport. */
|
|
52
|
+
export declare const deadLetterTopic: (topic: string) => string;
|
|
53
|
+
/** Header on a dead letter saying why it was dead-lettered. */
|
|
54
|
+
export declare const DEAD_LETTER_REASON_HEADER = "projected-dead-letter-reason";
|
|
55
|
+
/**
|
|
56
|
+
* Header on a message published again on purpose — by a dead-letter replay — under the id it had.
|
|
57
|
+
* A broker that drops repeated publishes of one id (NATS JetStream's deduplication) must count
|
|
58
|
+
* each replay as a new publish; consumers still see the original id.
|
|
59
|
+
*/
|
|
60
|
+
export declare const REPLAY_HEADER = "projected-replay";
|
|
61
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,qBAAqB,EAAE,KAAK,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAEjG;;;GAGG;AAEH,MAAM,WAAW,eAAe;IAC9B,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,8BAA8B;IAC9B,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC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O,MAAM,yBAAyB,iCAAiC,CAAC;AAExE;;;;GAIG;AACH,eAAO,MAAM,aAAa,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Result } from "@projected/application";
|
|
2
|
+
/** A transport as a readiness check: the service is not ready while its broker is unreachable. */
|
|
3
|
+
export function transportReadiness(name, transport) {
|
|
4
|
+
return { name, check: () => transport.ping() };
|
|
5
|
+
}
|
|
6
|
+
/** Name of the topic that receives a topic's dead letters. The same convention on every transport. */
|
|
7
|
+
export const deadLetterTopic = (topic) => `${topic}.dead-letter`;
|
|
8
|
+
/** Header on a dead letter saying why it was dead-lettered. */
|
|
9
|
+
export const DEAD_LETTER_REASON_HEADER = "projected-dead-letter-reason";
|
|
10
|
+
/**
|
|
11
|
+
* Header on a message published again on purpose — by a dead-letter replay — under the id it had.
|
|
12
|
+
* A broker that drops repeated publishes of one id (NATS JetStream's deduplication) must count
|
|
13
|
+
* each replay as a new publish; consumers still see the original id.
|
|
14
|
+
*/
|
|
15
|
+
export const REPLAY_HEADER = "projected-replay";
|
|
16
|
+
//# sourceMappingURL=transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAmD,MAAM,wBAAwB,CAAC;AAoDjG,kGAAkG;AAClG,MAAM,UAAU,kBAAkB,CAAC,IAAY,EAAE,SAA2B;IAC1E,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;AACjD,CAAC;AAUD,sGAAsG;AACtG,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,GAAG,KAAK,cAAc,CAAC;AAEzE,+DAA+D;AAC/D,MAAM,CAAC,MAAM,yBAAyB,GAAG,8BAA8B,CAAC;AAExE;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projected/messaging",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Asynchronous messaging adapters for projected: queued commands, an event relay (the event store is the outbox) and consumers, over a pluggable transport.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/martinzokov/projected.git",
|
|
9
|
+
"directory": "typescript/packages/messaging"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/martinzokov/projected#readme",
|
|
12
|
+
"bugs": "https://github.com/martinzokov/projected/issues",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"better-result": "^3.0.1",
|
|
27
|
+
"zod": "^4.6.7",
|
|
28
|
+
"@projected/application": "0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"ajv": "^8.20.0",
|
|
32
|
+
"ajv-formats": "^3.0.1",
|
|
33
|
+
"@projected/testing": "0.1.0",
|
|
34
|
+
"@projected/store-memory": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
6
39
|
}
|