_cloneBuffer.js 1.0KB

123456789101112131415161718192021222324252627282930313233343536
  1. var root = require('./_root');
  2. /** Detect free variable `exports`. */
  3. var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;
  4. /** Detect free variable `module`. */
  5. var freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;
  6. /** Detect the popular CommonJS extension `module.exports`. */
  7. var moduleExports = freeModule && freeModule.exports === freeExports;
  8. /** Built-in value references. */
  9. var Buffer = moduleExports ? root.Buffer : undefined,
  10. allocUnsafe = Buffer ? Buffer.allocUnsafe : undefined;
  11. /**
  12. * Creates a clone of `buffer`.
  13. *
  14. * @private
  15. * @param {Buffer} buffer The buffer to clone.
  16. * @param {boolean} [isDeep] Specify a deep clone.
  17. * @returns {Buffer} Returns the cloned buffer.
  18. */
  19. function cloneBuffer(buffer, isDeep) {
  20. if (isDeep) {
  21. return buffer.slice();
  22. }
  23. var length = buffer.length,
  24. result = allocUnsafe ? allocUnsafe(length) : new buffer.constructor(length);
  25. buffer.copy(result);
  26. return result;
  27. }
  28. module.exports = cloneBuffer;