codec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. //
  3. //
  4. /*
  5. The AMQP 0-9-1 is a mess when it comes to the types that can be
  6. encoded on the wire.
  7. There are four encoding schemes, and three overlapping sets of types:
  8. frames, methods, (field-)tables, and properties.
  9. Each *frame type* has a set layout in which values of given types are
  10. concatenated along with sections of "raw binary" data.
  11. In frames there are `shortstr`s, that is length-prefixed strings of
  12. UTF8 chars, 8 bit unsigned integers (called `octet`), unsigned 16 bit
  13. integers (called `short` or `short-uint`), unsigned 32 bit integers
  14. (called `long` or `long-uint`), unsigned 64 bit integers (called
  15. `longlong` or `longlong-uint`), and flags (called `bit`).
  16. Methods are encoded as a frame giving a method ID and a sequence of
  17. arguments of known types. The encoded method argument values are
  18. concatenated (with some fun complications around "packing" consecutive
  19. bit values into bytes).
  20. Along with the types given in frames, method arguments may be long
  21. byte strings (`longstr`, not required to be UTF8) or 64 bit unsigned
  22. integers to be interpreted as timestamps (yeah I don't know why
  23. either), or arbitrary sets of key-value pairs (called `field-table`).
  24. Inside a field table the keys are `shortstr` and the values are
  25. prefixed with a byte tag giving the type. The types are any of the
  26. above except for bits (which are replaced by byte-wide `bool`), along
  27. with a NULL value `void`, a special fixed-precision number encoding
  28. (`decimal`), IEEE754 `float`s and `double`s, signed integers,
  29. `field-array` (a sequence of tagged values), and nested field-tables.
  30. RabbitMQ and QPid use a subset of the field-table types, and different
  31. value tags, established before the AMQP 0-9-1 specification was
  32. published. So far as I know, no-one uses the types and tags as
  33. published. http://www.rabbitmq.com/amqp-0-9-1-errata.html gives the
  34. list of field-table types.
  35. Lastly, there are (sets of) properties, only one of which is given in
  36. AMQP 0-9-1: `BasicProperties`. These are almost the same as methods,
  37. except that they appear in content header frames, which include a
  38. content size, and they carry a set of flags indicating which
  39. properties are present. This scheme can save ones of bytes per message
  40. (messages which take a minimum of three frames each to send).
  41. */
  42. 'use strict';
  43. var ints = require('buffer-more-ints');
  44. // JavaScript uses only doubles so what I'm testing for is whether
  45. // it's *better* to encode a number as a float or double. This really
  46. // just amounts to testing whether there's a fractional part to the
  47. // number, except that see below. NB I don't use bitwise operations to
  48. // do this 'efficiently' -- it would mask the number to 32 bits.
  49. //
  50. // At 2^50, doubles don't have sufficient precision to distinguish
  51. // between floating point and integer numbers (`Math.pow(2, 50) + 0.1
  52. // === Math.pow(2, 50)` (and, above 2^53, doubles cannot represent all
  53. // integers (`Math.pow(2, 53) + 1 === Math.pow(2, 53)`)). Hence
  54. // anything with a magnitude at or above 2^50 may as well be encoded
  55. // as a 64-bit integer. Except that only signed integers are supported
  56. // by RabbitMQ, so anything above 2^63 - 1 must be a double.
  57. function isFloatingPoint(n) {
  58. return n >= 0x8000000000000000 ||
  59. (Math.abs(n) < 0x4000000000000
  60. && Math.floor(n) !== n);
  61. }
  62. function encodeTable(buffer, val, offset) {
  63. var start = offset;
  64. offset += 4; // leave room for the table length
  65. for (var key in val) {
  66. if (val[key] !== undefined) {
  67. var len = Buffer.byteLength(key);
  68. buffer.writeUInt8(len, offset); offset++;
  69. buffer.write(key, offset, 'utf8'); offset += len;
  70. offset += encodeFieldValue(buffer, val[key], offset);
  71. }
  72. }
  73. var size = offset - start;
  74. buffer.writeUInt32BE(size - 4, start);
  75. return size;
  76. }
  77. function encodeArray(buffer, val, offset) {
  78. var start = offset;
  79. offset += 4;
  80. for (var i=0, num=val.length; i < num; i++) {
  81. offset += encodeFieldValue(buffer, val[i], offset);
  82. }
  83. var size = offset - start;
  84. buffer.writeUInt32BE(size - 4, start);
  85. return size;
  86. }
  87. function encodeFieldValue(buffer, value, offset) {
  88. var start = offset;
  89. var type = typeof value, val = value;
  90. // A trapdoor for specifying a type, e.g., timestamp
  91. if (value && type === 'object' && value.hasOwnProperty('!')) {
  92. val = value.value;
  93. type = value['!'];
  94. }
  95. // If it's a JS number, we'll have to guess what type to encode it
  96. // as.
  97. if (type == 'number') {
  98. // Making assumptions about the kind of number (floating point
  99. // v integer, signed, unsigned, size) desired is dangerous in
  100. // general; however, in practice RabbitMQ uses only
  101. // longstrings and unsigned integers in its arguments, and
  102. // other clients generally conflate number types anyway. So
  103. // the only distinction we care about is floating point vs
  104. // integers, preferring integers since those can be promoted
  105. // if necessary. If floating point is required, we may as well
  106. // use double precision.
  107. if (isFloatingPoint(val)) {
  108. type = 'double';
  109. }
  110. else { // only signed values are used in tables by
  111. // RabbitMQ. It *used* to (< v3.3.0) treat the byte 'b'
  112. // type as unsigned, but most clients (and the spec)
  113. // think it's signed, and now RabbitMQ does too.
  114. if (val < 128 && val >= -128) {
  115. type = 'byte';
  116. }
  117. else if (val >= -0x8000 && val < 0x8000) {
  118. type = 'short'
  119. }
  120. else if (val >= -0x80000000 && val < 0x80000000) {
  121. type = 'int';
  122. }
  123. else {
  124. type = 'long';
  125. }
  126. }
  127. }
  128. function tag(t) { buffer.write(t, offset); offset++; }
  129. switch (type) {
  130. case 'string': // no shortstr in field tables
  131. var len = Buffer.byteLength(val, 'utf8');
  132. tag('S');
  133. buffer.writeUInt32BE(len, offset); offset += 4;
  134. buffer.write(val, offset, 'utf8'); offset += len;
  135. break;
  136. case 'object':
  137. if (val === null) {
  138. tag('V');
  139. }
  140. else if (Array.isArray(val)) {
  141. tag('A');
  142. offset += encodeArray(buffer, val, offset);
  143. }
  144. else if (Buffer.isBuffer(val)) {
  145. tag('x');
  146. buffer.writeUInt32BE(val.length, offset); offset += 4;
  147. val.copy(buffer, offset); offset += val.length;
  148. }
  149. else {
  150. tag('F');
  151. offset += encodeTable(buffer, val, offset);
  152. }
  153. break;
  154. case 'boolean':
  155. tag('t');
  156. buffer.writeUInt8((val) ? 1 : 0, offset); offset++;
  157. break;
  158. // These are the types that are either guessed above, or
  159. // explicitly given using the {'!': type} notation.
  160. case 'double':
  161. case 'float64':
  162. tag('d');
  163. buffer.writeDoubleBE(val, offset);
  164. offset += 8;
  165. break;
  166. case 'byte':
  167. case 'int8':
  168. tag('b');
  169. buffer.writeInt8(val, offset); offset++;
  170. break;
  171. case 'unsignedbyte':
  172. case 'uint8':
  173. tag('B');
  174. buffer.writeUInt8(val, offset); offset++;
  175. break;
  176. case 'short':
  177. case 'int16':
  178. tag('s');
  179. buffer.writeInt16BE(val, offset); offset += 2;
  180. break;
  181. case 'unsignedshort':
  182. case 'uint16':
  183. tag('u');
  184. buffer.writeUInt16BE(val, offset); offset += 2;
  185. break;
  186. case 'int':
  187. case 'int32':
  188. tag('I');
  189. buffer.writeInt32BE(val, offset); offset += 4;
  190. break;
  191. case 'unsignedint':
  192. case 'uint32':
  193. tag('i');
  194. buffer.writeUInt32BE(val, offset); offset += 4;
  195. break;
  196. case 'long':
  197. case 'int64':
  198. tag('l');
  199. ints.writeInt64BE(buffer, val, offset); offset += 8;
  200. break;
  201. // Now for exotic types, those can _only_ be denoted by using
  202. // `{'!': type, value: val}
  203. case 'timestamp':
  204. tag('T');
  205. ints.writeUInt64BE(buffer, val, offset); offset += 8;
  206. break;
  207. case 'float':
  208. tag('f');
  209. buffer.writeFloatBE(val, offset); offset += 4;
  210. break;
  211. case 'decimal':
  212. tag('D');
  213. if (val.hasOwnProperty('places') && val.hasOwnProperty('digits')
  214. && val.places >= 0 && val.places < 256) {
  215. buffer[offset] = val.places; offset++;
  216. buffer.writeUInt32BE(val.digits, offset); offset += 4;
  217. }
  218. else throw new TypeError(
  219. "Decimal value must be {'places': 0..255, 'digits': uint32}, " +
  220. "got " + JSON.stringify(val));
  221. break;
  222. default:
  223. throw new TypeError('Unknown type to encode: ' + type);
  224. }
  225. return offset - start;
  226. }
  227. // Assume we're given a slice of the buffer that contains just the
  228. // fields.
  229. function decodeFields(slice) {
  230. var fields = {}, offset = 0, size = slice.length;
  231. var len, key, val;
  232. function decodeFieldValue() {
  233. var tag = String.fromCharCode(slice[offset]); offset++;
  234. switch (tag) {
  235. case 'b':
  236. val = slice.readInt8(offset); offset++;
  237. break;
  238. case 'B':
  239. val = slice.readUInt8(offset); offset++;
  240. break;
  241. case 'S':
  242. len = slice.readUInt32BE(offset); offset += 4;
  243. val = slice.toString('utf8', offset, offset + len);
  244. offset += len;
  245. break;
  246. case 'I':
  247. val = slice.readInt32BE(offset); offset += 4;
  248. break;
  249. case 'i':
  250. val = slice.readUInt32BE(offset); offset += 4;
  251. break;
  252. case 'D': // only positive decimals, apparently.
  253. var places = slice[offset]; offset++;
  254. var digits = slice.readUInt32BE(offset); offset += 4;
  255. val = {'!': 'decimal', value: {places: places, digits: digits}};
  256. break;
  257. case 'T':
  258. val = ints.readUInt64BE(slice, offset); offset += 8;
  259. val = {'!': 'timestamp', value: val};
  260. break;
  261. case 'F':
  262. len = slice.readUInt32BE(offset); offset += 4;
  263. val = decodeFields(slice.subarray(offset, offset + len));
  264. offset += len;
  265. break;
  266. case 'A':
  267. len = slice.readUInt32BE(offset); offset += 4;
  268. decodeArray(offset + len);
  269. // NB decodeArray will itself update offset and val
  270. break;
  271. case 'd':
  272. val = slice.readDoubleBE(offset); offset += 8;
  273. break;
  274. case 'f':
  275. val = slice.readFloatBE(offset); offset += 4;
  276. break;
  277. case 'l':
  278. val = ints.readInt64BE(slice, offset); offset += 8;
  279. break;
  280. case 's':
  281. val = slice.readInt16BE(offset); offset += 2;
  282. break;
  283. case 'u':
  284. val = slice.readUInt16BE(offset); offset += 2;
  285. break;
  286. case 't':
  287. val = slice[offset] != 0; offset++;
  288. break;
  289. case 'V':
  290. val = null;
  291. break;
  292. case 'x':
  293. len = slice.readUInt32BE(offset); offset += 4;
  294. val = slice.subarray(offset, offset + len);
  295. offset += len;
  296. break;
  297. default:
  298. throw new TypeError('Unexpected type tag "' + tag +'"');
  299. }
  300. }
  301. function decodeArray(until) {
  302. var vals = [];
  303. while (offset < until) {
  304. decodeFieldValue();
  305. vals.push(val);
  306. }
  307. val = vals;
  308. }
  309. while (offset < size) {
  310. len = slice.readUInt8(offset); offset++;
  311. key = slice.toString('utf8', offset, offset + len);
  312. offset += len;
  313. decodeFieldValue();
  314. fields[key] = val;
  315. }
  316. return fields;
  317. }
  318. module.exports.encodeTable = encodeTable;
  319. module.exports.decodeFields = decodeFields;