cloneBuffer.js 1.2 KB
Newer Older
1
import root from './root.js';
徐立's avatar
徐立 committed
2 3 4

/** Detect free variable `exports`. */
/** 检测自由变量“导出” */
5
const freeExports = typeof exports === 'object' && exports !== null && !exports.nodeType && exports;
徐立's avatar
徐立 committed
6 7 8

/** Detect free variable `module`. */
/** 检测自由变量“模块”。 */
9 10
const freeModule =
	freeExports && typeof module === 'object' && module !== null && !module.nodeType && module;
徐立's avatar
徐立 committed
11 12 13

/** Detect the popular CommonJS extension `module.exports`. */
/** 检测流行的CommonJS扩展“module.exports” */
14
const moduleExports = freeModule && freeModule.exports === freeExports;
徐立's avatar
徐立 committed
15 16 17

/** Built-in value references. */
/** 内置参考价值。 */
18 19
const Buffer = moduleExports ? root.Buffer : undefined,
	allocUnsafe = Buffer ? Buffer.allocUnsafe : undefined;
徐立's avatar
徐立 committed
20 21 22 23 24 25 26 27 28 29

/**
 * Creates a clone of `buffer`.
 * 创建一个“buffer”的克隆
 * @private
 * @param {Buffer} buffer 要克隆的缓冲区.
 * @param {boolean} [isDeep] 指定深度克隆.
 * @returns {Buffer} 返回克隆的缓冲区.
 */
function cloneBuffer(buffer, isDeep) {
30 31 32 33 34
	if (isDeep) {
		return buffer.slice();
	}
	const length = buffer.length;
	const result = allocUnsafe ? allocUnsafe(length) : new buffer.constructor(length);
徐立's avatar
徐立 committed
35

36 37
	buffer.copy(result);
	return result;
徐立's avatar
徐立 committed
38 39
}

40
export default cloneBuffer;