-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.ts
More file actions
215 lines (191 loc) · 6.17 KB
/
Copy pathcodec.ts
File metadata and controls
215 lines (191 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { randomUUID } from "node:crypto";
import type { HasTraceId, PolyglotMessage } from "./contracts.js";
import { BabelQueueError } from "./errors.js";
/** The wire envelope schema version this core implements (versioned independently of the package version). */
export const SCHEMA_VERSION = 1;
/** Stamped into `meta.lang` for envelopes produced by this core. */
export const SOURCE_LANG = "node";
/** Immutable per-message metadata. */
export interface Meta {
id: string;
queue: string;
lang: string;
schema_version: number;
/** Unix milliseconds, UTC. */
created_at: number;
}
/** The additive block appended to an envelope when a message is dead-lettered. */
export interface DeadLetter {
reason: string;
error: string | null;
exception: string | null;
/** Unix milliseconds, UTC. */
failed_at: number;
original_queue: string;
attempts: number;
lang: string;
}
/**
* The canonical BabelQueue wire message: a strict, language-neutral JSON shape
* that every SDK produces and consumes identically. The property order here is
* significant — it matches the other cores so {@link EnvelopeCodec.encode} is
* byte-for-byte identical across the insertion-order languages (PHP/Python).
*/
export interface Envelope {
/** The message URN (never a class name). */
job: string;
/** Correlation id, preserved across every hop. */
trace_id: string;
/** The pure-JSON payload. */
data: Record<string, unknown>;
meta: Meta;
/** Top-level transport retry counter. */
attempts: number;
/** Present only once the message has been dead-lettered. */
dead_letter?: DeadLetter;
}
/**
* A decoded, not-yet-validated envelope. Fields are loosely typed because they
* come off the wire; `urn` is accepted as an inbound alias for `job`. Narrow it
* with {@link EnvelopeCodec.accepts} before trusting the contents.
*/
export interface IncomingEnvelope {
job?: string;
/** Inbound alias for `job`. */
urn?: string;
trace_id?: string;
data?: unknown;
meta?: unknown;
attempts?: unknown;
dead_letter?: unknown;
}
/** Options for {@link EnvelopeCodec.make}. */
export interface MakeOptions {
/** Logical queue name recorded in `meta.queue` (default `"default"`). */
queue?: string;
/** Reuse an existing trace id (trace continuation) instead of minting one. */
traceId?: string;
}
/**
* Builds, encodes and decodes the canonical envelope — the single Node/TypeScript
* implementation of the wire format.
*/
export const EnvelopeCodec = {
SCHEMA_VERSION,
SOURCE_LANG,
/**
* Build the canonical envelope for a `(urn, data)` pair. Mints a fresh trace id
* unless `options.traceId` is given, starts `attempts` at 0, and stamps `meta`.
* Throws {@link BabelQueueError} when the URN is blank.
*/
make(
urn: string,
data: Record<string, unknown>,
options: MakeOptions = {},
): Envelope {
const resolvedUrn = (urn ?? "").trim();
if (resolvedUrn === "") {
throw new BabelQueueError(
"A polyglot message must expose a stable, non-empty URN so consumers can identify it without any class name.",
);
}
const traceId = (options.traceId ?? "").trim() || randomUUID();
return {
job: resolvedUrn,
trace_id: traceId,
data: { ...data },
meta: {
id: randomUUID(),
queue: options.queue ?? "default",
lang: SOURCE_LANG,
schema_version: SCHEMA_VERSION,
created_at: Date.now(),
},
attempts: 0,
};
},
/**
* Build the envelope from a {@link PolyglotMessage}. If the message also
* implements {@link HasTraceId} and returns a non-empty value, that trace id is
* reused.
*/
fromMessage(
message: PolyglotMessage & Partial<HasTraceId>,
queue = "default",
): Envelope {
const traceId =
typeof message.getBabelTraceId === "function"
? (message.getBabelTraceId() ?? undefined)
: undefined;
return EnvelopeCodec.make(message.getBabelUrn(), message.toPayload(), {
queue,
traceId,
});
},
/**
* Encode the envelope as compact UTF-8 JSON. `JSON.stringify` already emits the
* canonical form — no spaces, and slashes/unicode/HTML left unescaped — matching
* the other SDK cores.
*/
encode(envelope: Envelope): string {
return JSON.stringify(envelope);
},
/**
* Parse a raw JSON body. Returns `{}` for malformed or non-object input (call
* {@link EnvelopeCodec.accepts} before trusting it). Resolves the `urn` inbound
* alias into `job`.
*/
decode(raw: string): IncomingEnvelope {
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
return {};
}
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
return {};
}
const envelope = parsed as IncomingEnvelope;
if (!envelope.job && typeof envelope.urn === "string") {
envelope.job = envelope.urn;
}
return envelope;
},
/** The message URN — canonical `job`, with `urn` accepted as an alias. */
urn(envelope: IncomingEnvelope): string {
const value = envelope?.job ?? envelope?.urn ?? "";
return typeof value === "string" ? value.trim() : "";
},
/**
* Whether a consumer should accept this envelope. Rejects a missing URN, an
* unsupported `meta.schema_version`, a non-object `data`, a non-integer
* `attempts`, or a blank `trace_id` — the consumer-side counterpart to the
* producer JSON Schema. Acts as a type guard that narrows to {@link Envelope}.
*/
accepts(envelope: IncomingEnvelope): envelope is Envelope {
if (EnvelopeCodec.urn(envelope) === "") {
return false;
}
const meta = envelope.meta;
if (
meta === null ||
typeof meta !== "object" ||
(meta as Meta).schema_version !== SCHEMA_VERSION
) {
return false;
}
const data = envelope.data;
if (data === null || typeof data !== "object" || Array.isArray(data)) {
return false;
}
const attempts = envelope.attempts;
if (typeof attempts !== "number" || !Number.isInteger(attempts)) {
return false;
}
const traceId = envelope.trace_id;
if (typeof traceId !== "string" || traceId.trim() === "") {
return false;
}
return true;
},
} as const;