browser.js (1544B)
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 isFunction = require( '@stdlib/assert/is-function' ); 24 25 26 // MAIN // 27 28 /** 29 * Browser shim. 30 * 31 * @private 32 * @param {(string|null)} [encoding] - string encoding 33 * @param {Callback} clbk - callback to be invoked upon completion 34 * @throws {TypeError} callback argument must be a function 35 * 36 * @example 37 * function onRead( error ) { 38 * if ( error ) { 39 * return console.error( error.message ); 40 * } 41 * } 42 * 43 * stdin( onRead ); 44 */ 45 function stdin() { 46 var clbk = arguments[ arguments.length-1 ]; 47 if ( !isFunction( clbk ) ) { 48 throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + clbk + '`.' ); 49 } 50 setTimeout( onTimeout, 0 ); 51 52 /** 53 * Callback invoked upon a timeout. 54 * 55 * @private 56 */ 57 function onTimeout() { 58 clbk( new Error( 'invalid operation. The environment does not support reading from `stdin`.' ) ); 59 } 60 } 61 62 63 // EXPORTS // 64 65 module.exports = stdin;