GatewayScript amqp module
The GatewayScript amqp module encapsulates the Red Hat Rhea library.
This module provides the APIs to encode and decode AMQP annotation headers and AMQP application
properties. You can use the APIs in this module to work with the full AMQP Type
system for the proper decoding and encoding of AMQP payloads.
This module includes the following APIs.
encode(data)- This API encodes the supplied JavaScript value into
a Buffer object with the encoded value according to the AMQP
Typesystem. decode(data)- This API decodes the supplied Buffer and returns the associated JavaScript value.
encodeAnnotations(data)- This API encodes a JavaScript object into a base-64 value that is suitable for the
decodeAnnotations(data)- This API decodes a base-64 encoded annotations data and returns a JavaScript object with the decoded value.
encodeProperties(data)- This API encodes a JavaScript object into a base-64 value that is suitable for the
AMQPApplicationPropertiesheaders decodeProperties(data)- This API decodes a base-64 encoded properties and returns a JavaScript object with the decoded value.
In addition to the previous high-level APIs, the Rhea types.js module is also exposed for precise control over encoding and decoding.
The following documentation provides examples that are not documented in the
types.js module.
Encoding and decoding of DataPower®
base-64 headers
- Encoding
-
var hm = require('header-metadata'); var amqp = require('amqp'); var annotations = { operation: "deposit", amount: 10 }; var hdrMessageAnnotations = amqp.encodeAnnotations(annotations); hm.response.set('AMQPMessageAnnotations', hdrMessageAnnotations); var properties = { type: "string", length: 128 }; var hdrApplicationProperties = amqp.encodeProperties(properties); hm.response.set('AMQPApplicationProperties', hdrApplicationProperties); - Decoding
-
var hm = require('header-metadata'); var amqp = require('amqp'); var hdrMessageAnnotations = hm.current.headers['AMQPMessageAnnotations']; var annotations = amqp.decodeAnnotations(hdrMessageAnnotations); var hdrApplicationProperties = hm.current.headers['AMQPApplicationProperties']; var properties = amqp.decodeProperties(hdrApplicationProperties);
Encoding and decoding of values to and from the AMQP Type system
- Encoding
-
var amqp = require('amqp'); session.input.readAsBuffer((error, data) => { if(error) throw error; var value = amqp.decode(data); // you can now use the value like a normal JavaScript object }); - Decoding
-
var amqp = require('amqp'); var obj = { a: 1, array: [ 0, 1, 2 ], color: "yellow" }; var encoded = amqp.encode(obj); // you can now use the encoded value as a payload when using amqp-value payload type
AMQP Described type encoding
- Encoding
-
var amqp = require('amqp'); var data = "http:/www.example.com/bank"; var value = amqp.types.wrap_described(data, "URL"); var writer = new amqp.types.Writer(); writer.write(value); var buffer = writer.toBuffer(); /// buffer can now be used wherever an AMQP type value is expected - Decoding
-
var amqp = require('amqp'); session.input.readAsBuffer((error, data) => { if(error) throw error; var reader = new amqp.types.Reader(data); var value = reader.read(); // you can now use the value.descriptor.name to access the descriptor name // to obtain the described value, you can just unwrap it: var actualValue = amqp.types.unwrap(value); });
APIs for low-level encoding
wrap_boolean(value)wrap_ulong(value)wrap_uint(value)wrap_ushort(value)wrap_ubyte(value)wrap_long(value)wrap_int(value)wrap_short(value)wrap_byte(value)wrap_float(value)wrap_double(value)wrap_timestamp(value)wrap_char(value)wrap_uuid(value)wrap_binary(value)wrap_string(value)wrap_symbol(value)wrap_list(value)wrap_map(value)wrap_symbolic_map(value)wrap_array(value, code, descriptor)wrap_described(value, descriptor)wrap_message_id(value)