main.js (1635B)
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 isBuffer = require( './../../is-buffer' ); 24 var GlobalBuffer = require( './buffer.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Tests for native `Buffer` support. 31 * 32 * @returns {boolean} boolean indicating if an environment has `Buffer` support 33 * 34 * @example 35 * var bool = hasNodeBufferSupport(); 36 * // returns <boolean> 37 */ 38 function hasNodeBufferSupport() { 39 var bool; 40 var b; 41 42 if ( typeof GlobalBuffer !== 'function' ) { 43 return false; 44 } 45 // Test basic support... 46 try { 47 if ( typeof GlobalBuffer.from === 'function' ) { 48 b = GlobalBuffer.from( [ 1, 2, 3, 4 ] ); 49 } else { 50 b = new GlobalBuffer( [ 1, 2, 3, 4 ] ); // Note: this is deprecated behavior starting in Node v6 (see https://nodejs.org/api/buffer.html#buffer_new_buffer_array) 51 } 52 bool = ( 53 isBuffer( b ) && 54 b[ 0 ] === 1 && 55 b[ 1 ] === 2 && 56 b[ 2 ] === 3 && 57 b[ 3 ] === 4 58 ); 59 } catch ( err ) { // eslint-disable-line no-unused-vars 60 bool = false; 61 } 62 return bool; 63 } 64 65 66 // EXPORTS // 67 68 module.exports = hasNodeBufferSupport;