IsDetachedBuffer.js 1007 B

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