Projekt

Obecné

Profil

Stáhnout (881 Bajtů) Statistiky
| Větev: | Revize:
1
var Buffer = require('buffer').Buffer
2

    
3
module.exports = function (buf) {
4
	// If the buffer is backed by a Uint8Array, a faster version will work
5
	if (buf instanceof Uint8Array) {
6
		// If the buffer isn't a subarray, return the underlying ArrayBuffer
7
		if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
8
			return buf.buffer
9
		} else if (typeof buf.buffer.slice === 'function') {
10
			// Otherwise we need to get a proper copy
11
			return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
12
		}
13
	}
14

    
15
	if (Buffer.isBuffer(buf)) {
16
		// This is the slow version that will work with any Buffer
17
		// implementation (even in old browsers)
18
		var arrayCopy = new Uint8Array(buf.length)
19
		var len = buf.length
20
		for (var i = 0; i < len; i++) {
21
			arrayCopy[i] = buf[i]
22
		}
23
		return arrayCopy.buffer
24
	} else {
25
		throw new Error('Argument must be a Buffer')
26
	}
27
}
(6-6/8)