dead-claudia / thallium

A simple, extensible JavaScript test framework (work in progress)
18 stars 4 forks source link

ArrayBuffer issues with NodeJS 4.x #29

Closed ghost closed 7 years ago

ghost commented 7 years ago

Seems like there is an issue with NodejS 4.x and ArrayBuffer, so maybe you should check whether Buffer constructor accepts ArrayBuffer or not?

Something like this?

var toBuffer = (function () {
  // check whether Buffer constructor accepts ArrayBuffer or not
  function isBufferConstructorAcceptsArrayBuffer() {
    try {
      return typeof Uint8Array === 'function' && (new Buffer(new Uint8Array([1]).buffer)[0] === 1);
    } catch (e) {
      return false;
    }
  }
  if (isBufferConstructorAcceptsArrayBuffer()) {
    // Node 4.x
    return function (ab) {
      return new Buffer(ab);
    };
  } else {
    // Node 0.10.x and 0.12.x
    return function (ab) {
      var buffer = new Buffer(ab.byteLength);
      var view = new Uint8Array(ab);
      for (var i = 0; i < buffer.length; ++i) {
        buffer[i] = view[i];
      }
      return buffer;
    };
  }
})();
dead-claudia commented 7 years ago

My algorithm has no dependency on this fact at all. I just read it like any other indexed number-only collection.