async.js (2852B)
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 isObject = require( '@stdlib/assert/is-plain-object' ); 24 var isFunction = require( '@stdlib/assert/is-function' ); 25 var isUint8Array = require( '@stdlib/assert/is-uint8array' ); 26 var readFile = require( './../../read-file' ); 27 var Uint8Array = require( '@stdlib/array/uint8' ); 28 29 30 // MAIN // 31 32 /** 33 * Reads the entire contents of a WebAssembly file. 34 * 35 * @param {(string|Buffer|integer)} file - file path or file descriptor 36 * @param {Options} [options] - options 37 * @param {string} [options.flag] - file status flag 38 * @param {Callback} clbk - callback to invoke after reading a file 39 * @throws {TypeError} options argument must be an object 40 * @throws {TypeError} callback argument must be a function 41 * 42 * @example 43 * var join = require( 'path' ).join; 44 * var instanceOf = require( '@stdlib/assert/instance-of' ); 45 * 46 * var fpath = join( __dirname, 'foo.wasm' ); 47 * readWASM( fpath, onRead ); 48 * 49 * function onRead( error, buf ) { 50 * if ( error ) { 51 * throw error; 52 * } 53 * console.log( buf ); 54 * } 55 */ 56 function readWASM( file, options, clbk ) { 57 var opts; 58 var done; 59 if ( arguments.length < 3 ) { 60 opts = {}; 61 done = options; 62 } else { 63 if ( !isObject( options ) ) { 64 throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 65 } 66 opts = options; 67 done = clbk; 68 } 69 if ( !isFunction( done ) ) { 70 throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + done + '`.' ); 71 } 72 // Always override setting the encoding option, as wasm is a binary file format: 73 opts.encoding = null; 74 readFile( file, opts, onRead ); 75 76 /** 77 * Callback invoked upon reading a file. 78 * 79 * @private 80 * @param {(Error|null)} error - error object 81 * @param {(Buffer|string)} file - file contents 82 * @returns {void} 83 */ 84 function onRead( error, file ) { 85 var out; 86 var i; 87 if ( error ) { 88 return done( error ); 89 } 90 if ( isUint8Array( file ) ) { 91 return done( null, file ); 92 } 93 // Handle older Node.js environments where Buffer objects are not Uint8Arrays... 94 out = new Uint8Array( file.length ); 95 for ( i = 0; i < file.length; i++ ) { 96 out[ i ] = file[ i ]; 97 } 98 done( null, out ); 99 } 100 } 101 102 103 // EXPORTS // 104 105 module.exports = readWASM;