time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (1826B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 isUint8Array = require( './../../is-uint8array' );
     24 var isBuffer = require( './../../is-buffer' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Tests if a value is a gzip buffer (or Uint8Array).
     31 *
     32 * @param {*} value - value to test
     33 * @returns {boolean} boolean indicating whether a value is a gzip buffer
     34 *
     35 * @example
     36 * var Uint8Array = require( '@stdlib/array/uint8' );
     37 *
     38 * var buf = new Uint8Array( 20 );
     39 * buf[ 0 ] = 31;  // 0x1f => magic number
     40 * buf[ 1 ] = 139; // 0x8b
     41 * buf[ 2 ] = 8;   // 0x08 => compression method
     42 *
     43 * var bool = isgzipBuffer( buf );
     44 * // returns true
     45 *
     46 * @example
     47 * var Uint8Array = require( '@stdlib/array/uint8' );
     48 *
     49 * var bool = isgzipBuffer( new Uint8Array( 20 ) );
     50 * // returns false
     51 *
     52 * @example
     53 * var bool = isgzipBuffer( [] );
     54 * // returns false
     55 */
     56 function isgzipBuffer( value ) {
     57 	if ( !isUint8Array( value ) && !isBuffer( value ) ) {
     58 		return false;
     59 	}
     60 	if ( value.length < 19 ) { // 10-byte header + 8-byte footer + payload
     61 		return false;
     62 	}
     63 	return (
     64 		// Check for expected magic number:
     65 		value[ 0 ] === 0x1F &&
     66 		value[ 1 ] === 0x8B &&
     67 
     68 		// Check for expected compression method:
     69 		value[ 2 ] === 0x08
     70 	);
     71 }
     72 
     73 
     74 // EXPORTS //
     75 
     76 module.exports = isgzipBuffer;