# Buffer not defined anymore in the payload format's JavaScript?

**URL:** <https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168>\
**Category:** v2 Console\
**Created:** [September 23, 2019, 1:54pm UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168 "2019-09-23T13:54:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joscha](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/joscha/32/28912_2.png) [@joscha](https://www.thethingsnetwork.org/forum/u/joscha)\
**Post date:** [September 23, 2019, 1:54pm UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/1 "2019-09-23T13:54:18Z")

</div>

I created [https://github.com/thesolarnomad/lora-serialization](https://github.com/thesolarnomad/lora-serialization) a while back which utilizes the `Buffer` object from the JS spec. I got a question recently that was related to encoding messages ([https://github.com/thesolarnomad/lora-serialization/issues/24#issuecomment-534107728](https://github.com/thesolarnomad/lora-serialization/issues/24#issuecomment-534107728)) and it turns out that I now get `Internal error: Encoder threw error: ReferenceError("'Buffer' is not defined")` when using the library. Is there any documentation on why Buffer support was removed/restricted or a changelog saying when this happened?

---

<div class="post-metadata">

**Author:** ![coastalplain](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/coastalplain/32/46415_2.png) [@coastalplain](https://www.thethingsnetwork.org/forum/u/coastalplain)\
**Post date:** [September 27, 2019, 9:31pm UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/2 "2019-09-27T21:31:30Z")

</div>

Within Node.js [documentation](https://nodejs.org/api/buffer.html), I see that several `new Buffer(array)` classes are [deprecated](https://nodejs.org/api/buffer.html#buffer_new_buffer_array). Could that point to the issue?

---

<div class="post-metadata">

**Author:** ![joscha](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/joscha/32/28912_2.png) [@joscha](https://www.thethingsnetwork.org/forum/u/joscha)\
**Post date:** [January 9, 2020, 12:57am UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/3 "2020-01-09T00:57:27Z")

</div>

Last time I checked the console didn’t use node.js unfortunately, they used a custom JS implementation in Go: [https://github.com/robertkrimen/otto](https://github.com/robertkrimen/otto) - not sure if that changed.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [January 14, 2020, 1:37pm UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/4 "2020-01-14T13:37:20Z")

</div>

Are you sure Buffer was ever supported in payload formats?

The decoder part of the LoRa Serialization library doesn’t use it, and I wonder if anyone used the library in an encoder. Buffer is simply not part of EcmaScript 5, which is what TTN/Otto uses.

---

<div class="post-metadata">

**Author:** ![joscha](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/joscha/32/28912_2.png) [@joscha](https://www.thethingsnetwork.org/forum/u/joscha)\
**Post date:** [January 14, 2020, 8:17pm UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/5 "2020-01-14T20:17:44Z")

</div>

It worked when I wrote the library.

---

<div class="post-metadata">

**Author:** ![coastalplain](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/coastalplain/32/46415_2.png) [@coastalplain](https://www.thethingsnetwork.org/forum/u/coastalplain)\
**Post date:** [January 15, 2020, 3:23pm UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/6 "2020-01-15T15:23:35Z")

</div>

I’m not sure of the Buffer issue, or from where it originates (my lack of understanding/knowing the engine under the TTN hood and the languages), but clearly it was working in the past. I will look at [“Is there any documentation on payload functions?”](https://www.thethingsnetwork.org/forum/t/is-there-any-documentation-on-payload-functions/3441) and will see if I can find an answer there.

Below is the first block of code from [encoder.js](https://github.com/thesolarnomad/lora-serialization/blob/master/src/encoder.js) from lora-serialization with just the `unixtime` variable. Is the second line `Buffer(byteSize)` causing the issue, or is it behind the scenes in TTN?

```
var intToBytes = function(i, byteSize) {
  var buf = new Buffer(byteSize);
  for (var x = 0; x < byteSize; x++) {
    buf[x] = i >> (x * 8);
  }
  return buf;
};

var unixtime = function(i) {
  if (isNaN(i) || i < 0) {
    throw new Error('Unix time must be positive');
  }
  return intToBytes(i, unixtime.BYTES);
};
unixtime.BYTES = 4;

```

Using [lora-serialization](https://github.com/thesolarnomad/lora-serialization) alone would be great, but might decide to use it for decode with some of the other information you provide on the “[best practices](https://www.thethingsnetwork.org/forum/t/best-practices-when-sending-gps-location-data-howto/1242/41)” link as a work around for the encoder. Is it possible to use one for decoding and another for encoding, as long as the node is programmed for that?

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [January 15, 2020, 6:08pm UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/7 "2020-01-15T18:08:16Z")

</div>

Not what you’re asking, but when replacing `Buffer` with an untyped JavaScript `[]` array, one just needs to ensure that only bytes (range 0x00…0xFF) are put into such array. So, `intToBytes` would need an extra `& 0xFF` after shifting:

```javascript
var intToBytes = function(i, byteSize) {
  var buf = [];
  for (var x = 0; x < byteSize; x++) {
    buf[x] = i >> (x * 8) & 0xFF;
  }
  return buf;
};

```

So, it seems the following will do, without the need for `Buffer`:

```javascript
var intToBytes = function(i, byteSize) {
  var buf = [];
  for (var x = 0; x < byteSize; x++) {
    buf[x] = i >> (x * 8) & 0xFF;
  }
  return buf;
};

var unixtime = function(i) {
  if (isNaN(i) || i < 0) {
    throw new Error('Unix time must be positive');
  }
  return intToBytes(i, unixtime.BYTES);
};
unixtime.BYTES = 4;

var uint8 = function(i) {
  if (isNaN(i) || i < 0 || i > 255) {
    throw new Error('int be in range 0..255');
  }
  return intToBytes(i, uint8.BYTES);
};
uint8.BYTES = 1;

var uint16 = function(i) {
  if (isNaN(i) || i < 0 || i > 65535) {
    throw new Error('int be in range 0..65535');
  }
  return intToBytes(i, uint16.BYTES);
};
uint16.BYTES = 2;

var latLng = function(latitude, longitude) {
  if (isNaN(latitude) || latitude < -90 || latitude > 90) {
    throw new Error('Latitude must be between -90° and 90°');
  }
  if (isNaN(longitude) || longitude < -180 || longitude > 180) {
    throw new Error('Longitude must be between -180° and 180°');
  }

  return [].concat(
    intToBytes(~~(latitude * 1e6), latLng.BYTES / 2),
    intToBytes(~~(longitude * 1e6), latLng.BYTES / 2)
  );
};
latLng.BYTES = 8;

var temperature = function(i) {
  if (isNaN(i) || i < -327.68 || i > 327.67) {
    throw new Error('Temperature must be in range -327.68..327.67');
  }
  var t = ~~(Math.abs(i) * 1e2);
  var b = ('0000000000000000' + Number(t >>> 0).toString(2)).slice(-16);
  if (i < 0) {
    var arr = b.split('').map(function(x) { return !Number(x); });
    for (var o = arr.length - 1; o > 0; o--) {
      arr[o] = !arr[o];
      if (arr[o]) {
        break;
      }
    }
    b = arr.map(Number).join('');
  }
  return [
    parseInt(b.slice(-16, -8), 2),
    parseInt(b.slice(-8), 2)
  ];
};
temperature.BYTES = 2;

var humidity = function(i) {
  if (isNaN(i) || i < 0 || i > 100) {
    throw new Error('Humidity must be in range 0..100');
  }

  return intToBytes(i * 1e2, humidity.BYTES);
};
humidity.BYTES = 2;

var bitmap = function(a, b, c, d, e, f, g, h) { // eslint-disable-line no-unused-vars
  var base = [];
  for(var i = 0; i < 8; i++) {
    var bit = arguments[i];
    if (typeof bit === 'undefined') {
      base[i] = false;
    } else if (typeof bit !== 'boolean') {
      throw new TypeError('Arguments must be of type boolean');
    } else {
      base[i] = bit;
    }
  }
  var bm = parseInt(base.map(Number).join(''), 2);
  return intToBytes(bm, bitmap.BYTES);
};
bitmap.BYTES = 1;

var encode = function(values, mask) {
  if (!Array.isArray(values)) {
    throw new TypeError('Values must be an array');
  }
  if (!Array.isArray(mask)) {
    throw new TypeError('Mask must be an array');
  }
  if (values.length > mask.length) {
    throw new Error('Mask length is ' + mask.length + ' whereas input is ' + values.length);
  }

  return values
    .reduce(function(acc, args, i) {
      return acc.concat(mask[i].apply(null, Array.isArray(args) ? args : [args]));
    }, []);
};

if (typeof module === 'object' && typeof module.exports !== 'undefined') {
  module.exports = {
    unixtime: unixtime,
    uint8: uint8,
    uint16: uint16,
    temperature: temperature,
    humidity: humidity,
    latLng: latLng,
    bitmap: bitmap,
    encode: encode
  };
}

```

(I’ve created [a pull request](https://github.com/thesolarnomad/lora-serialization/pull/30), also including a fix for `LoraMessage.js`.)

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [January 16, 2020, 9:19am UTC](https://www.thethingsnetwork.org/forum/t/buffer-not-defined-anymore-in-the-payload-formats-javascript/30168/8 "2020-01-16T09:19:10Z")

</div>

2 posts were split to a new topic: [How to use “bitmap” in LoRa Serialization library?](https://www.thethingsnetwork.org/forum/t/how-to-use-bitmap-in-lora-serialization-library/33424)
