IsDetachedBuffer.js 735 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var $byteLength = require('array-buffer-byte-length');
  4. var isArrayBuffer = require('is-array-buffer');
  5. var availableTypedArrays = require('available-typed-arrays')();
  6. // https://262.ecma-international.org/6.0/#sec-isdetachedbuffer
  7. module.exports = function IsDetachedBuffer(arrayBuffer) {
  8. if (!isArrayBuffer(arrayBuffer)) {
  9. throw new $TypeError('Assertion failed: `arrayBuffer` must be an Object with an [[ArrayBufferData]] internal slot');
  10. }
  11. if ($byteLength(arrayBuffer) === 0) {
  12. try {
  13. new global[availableTypedArrays[0]](arrayBuffer); // eslint-disable-line no-new
  14. } catch (error) {
  15. return !!error && error.name === 'TypeError';
  16. }
  17. }
  18. return false;
  19. };