@@ -2,12 +2,12 @@ import type { AMQPMessage } from "./amqp-message.js"
22import type { AMQPProperties } from "./amqp-properties.js"
33import { isPlainBody } from "./amqp-publisher.js"
44
5- // 1. Define a type that represents a map of different Parsers
5+ /** Map of content- type → parser. */
66export type ParserMap = {
77 [ K : string ] : AMQPParser < unknown , unknown >
88}
99
10- // 2. The ParserRegistry type uses a Mapped Type to preserve the unique In/Out of each key
10+ /** Readonly view of a { @link ParserMap}; returned by helpers. */
1111export type ParserRegistry < T extends ParserMap > = {
1212 readonly [ K in keyof T & string ] : T [ K ]
1313}
@@ -25,53 +25,16 @@ type BuiltinParsers = {
2525 "application/json" : AMQPParser < JsonSerializable , unknown >
2626}
2727
28- // 3. The factory function
29- export function createParserRegistry < T extends ParserMap > ( parsers : T , useDefaultParsers ?: false ) : ParserRegistry < T >
30- export function createParserRegistry < T extends ParserMap > (
31- parsers : T ,
32- useDefaultParsers : true ,
33- ) : ParserRegistry < T & BuiltinParsers >
34- export function createParserRegistry < T extends ParserMap > (
35- parsers : T ,
36- useDefaultParsers ?: boolean ,
37- ) : ParserRegistry < T & BuiltinParsers > | ParserRegistry < T > {
38- if ( useDefaultParsers ) {
39- return { "text/plain" : PlainParser , "application/json" : JSONParser , ...parsers }
40- }
41- return parsers
42- }
43-
4428export type InferParserInput < P > = P extends AMQPParser < infer TInput , unknown > ? TInput : never
4529export type InferParserOutput < P > = P extends AMQPParser < never , infer Out > ? Out : never
4630
31+ /** Map of content-encoding → coder. */
4732export type CoderMap = { [ K : string ] : AMQPCoder }
33+ /** Readonly view of a {@link CoderMap}. */
4834export type CoderRegistry < T extends CoderMap > = { readonly [ K in keyof T & string ] : T [ K ] }
4935
5036type BuiltinCoders = { gzip : AMQPCoder ; deflate : AMQPCoder }
5137
52- export function createCoderRegistry < T extends CoderMap > ( coders : T , useDefaults ?: false ) : CoderRegistry < T >
53- export function createCoderRegistry < T extends CoderMap > ( coders : T , useDefaults : true ) : CoderRegistry < T & BuiltinCoders >
54- export function createCoderRegistry < T extends CoderMap > (
55- coders : T ,
56- useDefaults ?: boolean ,
57- ) : CoderRegistry < T & BuiltinCoders > | CoderRegistry < T > {
58- if ( useDefaults ) {
59- if (
60- typeof CompressionStream === "undefined" ||
61- typeof DecompressionStream === "undefined" ||
62- typeof Blob === "undefined" ||
63- typeof Response === "undefined"
64- ) {
65- throw new Error (
66- "Built-in coders require CompressionStream, DecompressionStream, Blob, and Response " +
67- "(Node 18+, modern browsers). Register custom coders via createCoderRegistry() instead." ,
68- )
69- }
70- return { gzip : GzipCoder , deflate : DeflateCoder , ...coders }
71- }
72- return coders
73- }
74-
7538/** Handles serialization/deserialization based on content-type. */
7639export interface AMQPParser < In = unknown , Out = unknown > {
7740 serialize ( body : In , properties : AMQPProperties ) : Uint8Array
@@ -138,6 +101,25 @@ const DeflateCoder: AMQPCoder = {
138101 } ,
139102}
140103
104+ /**
105+ * Built-in parsers for `text/plain` and `application/json`.
106+ * Use directly, or merge with custom parsers via spread:
107+ * `{ ...builtinParsers, "text/csv": csvParser }`.
108+ */
109+ export const builtinParsers : ParserRegistry < BuiltinParsers > = {
110+ "text/plain" : PlainParser ,
111+ "application/json" : JSONParser ,
112+ }
113+
114+ /**
115+ * Built-in coders for `gzip` and `deflate`.
116+ * Uses `CompressionStream`/`DecompressionStream` — requires Node 18+ or a modern browser.
117+ */
118+ export const builtinCoders : CoderRegistry < BuiltinCoders > = {
119+ gzip : GzipCoder ,
120+ deflate : DeflateCoder ,
121+ }
122+
141123export function serializeAndEncode (
142124 parsers : ParserMap ,
143125 coders : CoderMap ,
0 commit comments