@cloudamqp/amqp-client
Version:
AMQP 0-9-1 client, both for browsers (WebSocket) and node (TCP Socket)
138 lines • 4.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.builtinCoders = exports.builtinParsers = exports.deflateRawCoder = void 0;
exports.serializeAndEncode = serializeAndEncode;
exports.decodeAndParse = decodeAndParse;
exports.decodeMessage = decodeMessage;
const amqp_publisher_js_1 = require("./amqp-publisher.js");
function toBytes(data) {
if (data === null)
return new Uint8Array(0);
if (data instanceof Uint8Array)
return data;
if (data instanceof ArrayBuffer)
return new Uint8Array(data);
if (typeof data === "string")
return new TextEncoder().encode(data);
return new Uint8Array(data);
}
const PlainParser = {
serialize(body) {
return new TextEncoder().encode(String(body));
},
parse(body) {
return new TextDecoder().decode(body);
},
};
const JSONParser = {
serialize(body) {
return new TextEncoder().encode(JSON.stringify(body));
},
parse(body) {
return JSON.parse(new TextDecoder().decode(body));
},
};
async function compressWithStream(data, format) {
const stream = new Blob([data]).stream().pipeThrough(new CompressionStream(format));
return new Uint8Array(await new Response(stream).arrayBuffer());
}
async function decompressWithStream(data, format) {
const stream = new Blob([data]).stream().pipeThrough(new DecompressionStream(format));
return new Uint8Array(await new Response(stream).arrayBuffer());
}
const GzipCoder = {
encode(body) {
return compressWithStream(body, "gzip");
},
decode(body) {
return decompressWithStream(body, "gzip");
},
};
const DeflateCoder = {
encode(body) {
return compressWithStream(body, "deflate");
},
decode(body) {
return decompressWithStream(body, "deflate");
},
};
exports.deflateRawCoder = {
encode(body) {
return compressWithStream(body, "deflate-raw");
},
decode(body) {
return decompressWithStream(body, "deflate-raw");
},
};
exports.builtinParsers = {
"text/plain": PlainParser,
"application/json": JSONParser,
};
exports.builtinCoders = {
gzip: GzipCoder,
deflate: DeflateCoder,
};
function serializeAndEncode(parsers, coders, body, properties, defaults) {
const props = { ...properties };
if (defaults?.contentType && !props.contentType)
props.contentType = defaults.contentType;
if (defaults?.contentEncoding && !props.contentEncoding)
props.contentEncoding = defaults.contentEncoding;
let bytes;
if (props.contentType) {
const parser = parsers[props.contentType];
if (parser) {
bytes = parser.serialize(body, props);
}
else if ((0, amqp_publisher_js_1.isPlainBody)(body)) {
bytes = toBytes(body);
}
else {
throw new Error(`No parser registered for content-type "${props.contentType}" and body is not a string/Buffer/Uint8Array.`);
}
}
else if ((0, amqp_publisher_js_1.isPlainBody)(body)) {
bytes = toBytes(body);
}
else {
throw new Error("Cannot serialize body: no contentType specified and body is not a string/Buffer/Uint8Array. " +
"Set contentType or configure a defaultContentType on the session.");
}
if (props.contentEncoding) {
const coder = coders[props.contentEncoding];
if (!coder) {
throw new Error(`No coder registered for content-encoding "${props.contentEncoding}".`);
}
return coder.encode(bytes, props).then((encoded) => ({ body: encoded, properties: props }));
}
return { body: bytes, properties: props };
}
function decodeAndParse(parsers, coders, body, properties) {
if (properties.contentEncoding) {
const coder = coders[properties.contentEncoding];
if (!coder) {
throw new Error(`No coder registered for content-encoding "${properties.contentEncoding}".`);
}
return coder.decode(body, properties).then((decoded) => {
if (properties.contentType) {
const parser = parsers[properties.contentType];
if (parser)
return parser.parse(decoded, properties);
}
return decoded;
});
}
if (properties.contentType) {
const parser = parsers[properties.contentType];
if (parser)
return parser.parse(body, properties);
}
return body;
}
async function decodeMessage(msg, parsers, coders) {
if (msg._rawBytes) {
;
msg.body = await decodeAndParse(parsers, coders, msg._rawBytes, msg.properties);
}
}
//# sourceMappingURL=amqp-codec-registry.js.map