Ejemplo n.º 1
0
/**
 * Is Text (Synchronous)
 * Determine whether or not a file is a text or binary file.
 * Determined by extension checks first, then if unknown extension, will fallback on encoding detection.
 * We do that as encoding detection cannot guarantee everything, especially for chars between utf8 and utf16
 * @param {?string} filename - the filename for the file/buffer if available
 * @param {?Buffer} buffer - the buffer for the file if available
 * @returns {Error|boolean}
 */
function isTextSync (filename, buffer) {
	// Prepare
	let isText = null

	// Test extensions
	if ( filename ) {
		// Extract filename
		const parts = pathUtil.basename(filename).split('.').reverse()

		// Cycle extensions
		for ( const extension of parts ) {
			if ( textExtensions.indexOf(extension) !== -1 ) {
				isText = true
				break
			}
			if ( binaryExtensions.indexOf(extension) !== -1 ) {
				isText = false
				break
			}
		}
	}

	// Fallback to encoding if extension check was not enough
	if ( buffer && isText === null ) {
		isText = getEncodingSync(buffer) === 'utf8'
	}

	// Return our result
	return isText
}
Ejemplo n.º 2
0
/**
 * WIll be `null` if `buffer` was not provided. Otherwise will be either `'utf8'` or `'binary'`.
 * @typedef {'utf8'|'binary'|null} EncodingResult
 */

/**
 * WIll be `null` if neither `filename` nor `buffer` were provided. Otherwise will be a boolean value with the detection result.
 * @typedef {boolean|null} TextOrBinaryResult
 */

/**
 * @typedef {Object} EncodingOpts
 * @property {number} [chunkLength = 24]
 * @property {number} [chunkBegin = 0]
 */

/**
 * @callback IsTextCallback
 * @param {Error?} error
 * @param {TextOrBinaryResult} [isTextResult]
 */

/**
 * @callback IsBinaryCallback
 * @param {Error?} error
 * @param {TextOrBinaryResult} [isBinaryResult]
 */

/**
 * @callback GetEncodingCallback
 * @param {Error?} error
 * @param {EncodingResult} [encoding]
 */

/**
 * Determine if the filename and/or buffer is text.
 * Determined by extension checks first (if filename is available), otherwise if unknown extension or no filename, will perform a slower buffer encoding detection.
 * This order is done, as extension checks are quicker, and also because encoding checks cannot guarantee accuracy for chars between utf8 and utf16.
 * The extension checks are performed using the resources https://github.com/bevry/textextensions and https://github.com/bevry/binaryextensions
 * In a later major release, this function will become {@link isText} so you should use that instead.
 * @param {string} [filename] The filename for the file/buffer if available
 * @param {Buffer} [buffer] The buffer for the file if available
 * @returns {TextOrBinaryResult}
 */
function isTextSync(filename, buffer) {
	// Test extensions
	if (filename) {
		// Extract filename
		const parts = pathUtil
			.basename(filename)
			.split('.')
			.reverse()

		// Cycle extensions
		for (const extension of parts) {
			if (textExtensions.indexOf(extension) !== -1) {
				return true
			}
			if (binaryExtensions.indexOf(extension) !== -1) {
				return false
			}
		}
	}

	// Fallback to encoding if extension check was not enough
	if (buffer) {
		return getEncodingSync(buffer) === 'utf8'
	}

	// No buffer was provided
	return null
}
Ejemplo n.º 3
0
/**
 * Is Text (Synchronous)
 * Determine whether or not a file is a text or binary file.
 * Determined by extension checks first, then if unknown extension, will fallback on encoding detection.
 * We do that as encoding detection cannot guarantee everything, especially for chars between utf8 and utf16
 * @param {?string} filename - the filename for the file/buffer if available
 * @param {?Buffer} buffer - the buffer for the file if available
 * @returns {Error|boolean}
 */
function isTextSync(filename, buffer) {
	// Prepare
	var isText = null;

	// Test extensions
	if (filename) {
		// Extract filename
		var parts = pathUtil.basename(filename).split('.').reverse();

		// Cycle extensions
		var _iteratorNormalCompletion = true;
		var _didIteratorError = false;
		var _iteratorError = undefined;

		try {
			for (var _iterator = parts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
				var extension = _step.value;

				if (textExtensions.indexOf(extension) !== -1) {
					isText = true;
					break;
				}
				if (binaryExtensions.indexOf(extension) !== -1) {
					isText = false;
					break;
				}
			}
		} catch (err) {
			_didIteratorError = true;
			_iteratorError = err;
		} finally {
			try {
				if (!_iteratorNormalCompletion && _iterator.return) {
					_iterator.return();
				}
			} finally {
				if (_didIteratorError) {
					throw _iteratorError;
				}
			}
		}
	}

	// Fallback to encoding if extension check was not enough
	if (buffer && isText === null) {
		isText = getEncodingSync(buffer) === 'utf8';
	}

	// Return our result
	return isText;
}