polyfill.js (4090B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); 24 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 25 var Buffer = require( './../../ctor' ); 26 var Uint8Array = require( '@stdlib/array/uint8' ); 27 var fromArray = require( './../../from-array' ); 28 var version = require( './node_version.js' ); 29 30 31 // MAIN // 32 33 /** 34 * Allocates a buffer from an `ArrayBuffer`. 35 * 36 * ## Notes 37 * 38 * The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs: 39 * 40 * - `<3.0.0`: the function copies ArrayBuffer bytes to a new Buffer instance. 41 * - `>=3.0.0`: if provided a byte offset, the function copies `ArrayBuffer` bytes to a new `Buffer` instance; otherwise, the function returns a view of an `ArrayBuffer` without copying the underlying memory. 42 * - `<6.0.0`: if provided an empty ArrayBuffer, the function returns an empty Buffer which is **not** an ArrayBuffer view. 43 * 44 * 45 * @param {ArrayBuffer} buf - ArrayBuffer instance 46 * @param {NonNegativeInteger} [byteOffset=0] - index specifying the location of the first buffer byte 47 * @param {NonNegativeInteger} [length=buf.byteLength] - number of buffer bytes 48 * @throws {TypeError} first argument must be an ArrayBuffer 49 * @throws {TypeError} second argument must be a nonnegative integer 50 * @throws {RangeError} second argument must not exceed number of bytes in input ArrayBuffer 51 * @throws {TypeError} last argument must be a nonnegative integer 52 * @throws {RangeError} last argument must not exceed number of bytes in input ArrayBuffer 53 * @returns {Buffer} new `Buffer` instance 54 * 55 * @example 56 * var ArrayBuffer = require( '@stdlib/array/buffer' ); 57 * var ab = new ArrayBuffer( 10 ); 58 * 59 * var buf = fromArrayBuffer( ab ); 60 * // returns <Buffer> 61 * 62 * @example 63 * var ArrayBuffer = require( '@stdlib/array/buffer' ); 64 * var ab = new ArrayBuffer( 10 ); 65 * 66 * var buf = fromArrayBuffer( ab, 2, 4 ); 67 * // returns <Buffer> 68 */ 69 function fromArrayBuffer( buf, byteOffset, length ) { 70 var len; 71 if ( !isArrayBuffer( buf ) ) { 72 throw new TypeError( 'invalid argument. First argument must be an ArrayBuffer. Value: `' + buf + '`' ); 73 } 74 if ( arguments.length > 1 ) { 75 if ( !isNonNegativeInteger( byteOffset ) ) { 76 throw new TypeError( 'invalid argument. Second argument must be a nonnegative integer. Value: `' + byteOffset + '`.' ); 77 } 78 if ( byteOffset > buf.byteLength ) { 79 throw new RangeError( 'invalid argument. Second argument must not exceed the number of bytes in the input ArrayBuffer. Value: `' + byteOffset + '`.' ); 80 } 81 if ( arguments.length > 2 ) { 82 if ( !isNonNegativeInteger( length ) ) { 83 throw new TypeError( 'invalid argument. Last argument must be a nonnegative integer. Value: `' + length + '`.' ); 84 } 85 if ( length > buf.byteLength ) { 86 throw new RangeError( 'invalid argument. Last argument must not exceed the number of bytes in the input ArrayBuffer. Value: `' + length + '`.' ); 87 } 88 len = length; 89 } else { 90 len = buf.byteLength - byteOffset; 91 } 92 return fromArray( new Uint8Array( buf, byteOffset, len ) ); 93 } 94 if ( version < 3 ) { 95 return fromArray( new Uint8Array( buf ) ); 96 } 97 // Address Node <= v5.x where providing an empty ArrayBuffer throws an error: 98 if ( buf.byteLength === 0 ) { 99 return new Buffer( [] ); // eslint-disable-line no-buffer-constructor 100 } 101 return new Buffer( buf ); // eslint-disable-line no-buffer-constructor 102 } 103 104 105 // EXPORTS // 106 107 module.exports = fromArrayBuffer;