IsDetachedBuffer.js 783 B

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