istextorbinary
Advanced tools
Comparing version 2.3.0 to 2.4.0
/* eslint no-use-before-define:0 */ | ||
'use strict'; | ||
'use strict'; // Import | ||
// Import | ||
var pathUtil = require('path'); | ||
var pathUtil = require('path'); | ||
var textExtensions = require('textextensions'); | ||
var binaryExtensions = require('binaryextensions'); | ||
/** | ||
* @typedef {'utf8'|'binary'} EncodingResult | ||
*/ | ||
/** | ||
* 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. | ||
* We use the extensions from https://github.com/bevry/textextensions and https://github.com/bevry/binaryextensions | ||
* @param {string?} filename - the filename for the file/buffer if available | ||
* @param {Buffer?} buffer - the buffer for the file if available | ||
* @returns {Error|boolean} | ||
* @typedef {Object} EncodingOpts | ||
* @property {number} [chunkLength = 24] | ||
* @property {number} [chunkBegin = 0] | ||
*/ | ||
/** | ||
* @callback IsTextCallback | ||
* @param {Error?} error | ||
* @param {boolean} [result] | ||
*/ | ||
/** | ||
* @callback IsBinaryCallback | ||
* @param {Error?} error | ||
* @param {boolean} [result] | ||
*/ | ||
/** | ||
* @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 | ||
* @param {string} [filename] The filename for the file/buffer if available | ||
* @param {Buffer} [buffer] The buffer for the file if available | ||
* @returns {boolean} | ||
*/ | ||
function isTextSync(filename, buffer) { | ||
// Prepare | ||
var isText = null; | ||
// Prepare | ||
var isText = null; // Test extensions | ||
// Test extensions | ||
if (filename) { | ||
// Extract filename | ||
var parts = pathUtil.basename(filename).split('.').reverse(); | ||
if (filename) { | ||
// Extract filename | ||
var parts = pathUtil.basename(filename).split('.').reverse(); // Cycle extensions | ||
// Cycle extensions | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
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; | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
if (textExtensions.indexOf(extension) !== -1) { | ||
isText = true; | ||
break; | ||
} | ||
// Fallback to encoding if extension check was not enough | ||
if (buffer && isText === null) { | ||
isText = getEncodingSync(buffer) === 'utf8'; | ||
} | ||
if (binaryExtensions.indexOf(extension) !== -1) { | ||
isText = false; | ||
break; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} // Fallback to encoding if extension check was not enough | ||
// Return our result | ||
return isText; | ||
if (buffer && isText === null) { | ||
isText = getEncodingSync(buffer) === 'utf8'; | ||
} // Return our result | ||
return isText; | ||
} | ||
/** | ||
* Is Text | ||
* Uses `isTextSync` behind the scenes. | ||
* @param {string?} filename - forwarded to `isTextSync` | ||
* @param {Buffer?} buffer - forwarded to `isTextSync` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Determine if the filename and/or buffer is text. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string?} filename Forwarded to `isTextSync` | ||
* @param {Buffer?} buffer Forwarded to `isTextSync` | ||
* @param {IsTextCallback} next | ||
* @returns {void} | ||
*/ | ||
function isText(filename, buffer, next) { | ||
var result = isTextSync(filename, buffer); | ||
if (result instanceof Error) { | ||
next(result); | ||
} else { | ||
next(null, result); | ||
} | ||
var result; | ||
try { | ||
result = isTextSync(filename, buffer); | ||
} catch (err) { | ||
next(err); | ||
} | ||
next(null, result); | ||
} | ||
/** | ||
* Is Binary (Synchronous) | ||
* Uses `isTextSync` behind the scenes. | ||
* @param {string?} filename - forwarded to `isTextSync` | ||
* @param {Buffer?} buffer - forwarded to `isTextSync` | ||
* @returns {Error|boolean} | ||
* Determine if the filename and/or buffer is binary. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string} [filename] Forwarded to `isTextSync` | ||
* @param {Buffer} [buffer] Forwarded to `isTextSync` | ||
* @returns {boolean} | ||
*/ | ||
function isBinarySync(filename, buffer) { | ||
// Handle | ||
var result = isTextSync(filename, buffer); | ||
return result instanceof Error ? result : !result; | ||
// Handle | ||
var result = isTextSync(filename, buffer); | ||
return !result; | ||
} | ||
/** | ||
* Is Binary | ||
* Uses `isText` behind the scenes. | ||
* @param {string?} filename - forwarded to `isText` | ||
* @param {Buffer?} buffer - forwarded to `isText` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Determine if the filename and/or buffer is binary. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string?} filename Forwarded to `isText` | ||
* @param {Buffer?} buffer Forwarded to `isText` | ||
* @param {IsBinaryCallback} next | ||
* @returns {void} | ||
*/ | ||
function isBinary(filename, buffer, next) { | ||
// Handle | ||
isText(filename, buffer, function (err, result) { | ||
if (err) return next(err); | ||
return next(null, !result); | ||
}); | ||
var result; | ||
try { | ||
result = isTextSync(filename, buffer); | ||
} catch (err) { | ||
next(err); | ||
} | ||
next(null, !result); | ||
} | ||
/** | ||
@@ -123,77 +159,95 @@ * Get the encoding of a buffer. | ||
* @param {Buffer} buffer | ||
* @param {Object?} [opts] | ||
* @param {number?} [opts.chunkLength = 24] | ||
* @param {number?} [opts.chunkBegin = 0] | ||
* @returns {Error|string} either an Error instance if something went wrong, or if successful "utf8" or "binary" | ||
* @param {EncodingOpts} [opts] | ||
* @returns {EncodingResult} | ||
*/ | ||
function getEncodingSync(buffer, opts) { | ||
// Prepare | ||
var textEncoding = 'utf8'; | ||
var binaryEncoding = 'binary'; | ||
// Prepare | ||
var textEncoding = 'utf8'; | ||
var binaryEncoding = 'binary'; // Discover | ||
// Discover | ||
if (opts == null) { | ||
// Start | ||
var chunkLength = 24; | ||
var encoding = getEncodingSync(buffer, { chunkLength: chunkLength }); | ||
if (encoding === textEncoding) { | ||
// Middle | ||
var chunkBegin = Math.max(0, Math.floor(buffer.length / 2) - chunkLength); | ||
encoding = getEncodingSync(buffer, { chunkLength: chunkLength, chunkBegin: chunkBegin }); | ||
if (encoding === textEncoding) { | ||
// End | ||
chunkBegin = Math.max(0, buffer.length - chunkLength); | ||
encoding = getEncodingSync(buffer, { chunkLength: chunkLength, chunkBegin: chunkBegin }); | ||
} | ||
} | ||
if (opts == null) { | ||
// Start | ||
var chunkLength = 24; | ||
var encoding = getEncodingSync(buffer, { | ||
chunkLength: chunkLength | ||
}); | ||
// Return | ||
return encoding; | ||
} else { | ||
// Extract | ||
var _opts$chunkLength = opts.chunkLength, | ||
_chunkLength = _opts$chunkLength === undefined ? 24 : _opts$chunkLength, | ||
_opts$chunkBegin = opts.chunkBegin, | ||
_chunkBegin = _opts$chunkBegin === undefined ? 0 : _opts$chunkBegin; | ||
if (encoding === textEncoding) { | ||
// Middle | ||
var chunkBegin = Math.max(0, Math.floor(buffer.length / 2) - chunkLength); | ||
encoding = getEncodingSync(buffer, { | ||
chunkLength: chunkLength, | ||
chunkBegin: chunkBegin | ||
}); | ||
var chunkEnd = Math.min(buffer.length, _chunkBegin + _chunkLength); | ||
var contentChunkUTF8 = buffer.toString(textEncoding, _chunkBegin, chunkEnd); | ||
var _encoding = textEncoding; | ||
if (encoding === textEncoding) { | ||
// End | ||
chunkBegin = Math.max(0, buffer.length - chunkLength); | ||
encoding = getEncodingSync(buffer, { | ||
chunkLength: chunkLength, | ||
chunkBegin: chunkBegin | ||
}); | ||
} | ||
} // Return | ||
// Detect encoding | ||
for (var i = 0; i < contentChunkUTF8.length; ++i) { | ||
var charCode = contentChunkUTF8.charCodeAt(i); | ||
if (charCode === 65533 || charCode <= 8) { | ||
// 8 and below are control characters (e.g. backspace, null, eof, etc.) | ||
// 65533 is the unknown character | ||
// console.log(charCode, contentChunkUTF8[i]) | ||
_encoding = binaryEncoding; | ||
break; | ||
} | ||
} | ||
// Return | ||
return _encoding; | ||
} | ||
return encoding; | ||
} else { | ||
// Extract | ||
var _opts$chunkLength = opts.chunkLength, | ||
_chunkLength = _opts$chunkLength === void 0 ? 24 : _opts$chunkLength, | ||
_opts$chunkBegin = opts.chunkBegin, | ||
_chunkBegin = _opts$chunkBegin === void 0 ? 0 : _opts$chunkBegin; | ||
var chunkEnd = Math.min(buffer.length, _chunkBegin + _chunkLength); | ||
var contentChunkUTF8 = buffer.toString(textEncoding, _chunkBegin, chunkEnd); // Detect encoding | ||
for (var i = 0; i < contentChunkUTF8.length; ++i) { | ||
var charCode = contentChunkUTF8.charCodeAt(i); | ||
if (charCode === 65533 || charCode <= 8) { | ||
// 8 and below are control characters (e.g. backspace, null, eof, etc.) | ||
// 65533 is the unknown character | ||
// console.log(charCode, contentChunkUTF8[i]) | ||
return binaryEncoding; | ||
} | ||
} // Return | ||
return textEncoding; | ||
} | ||
} | ||
/** | ||
* Get the encoding of a buffer | ||
* Uses `getEncodingSync` behind the scenes. | ||
* @param {Buffer} buffer - forwarded to `getEncodingSync` | ||
* @param {Object} opts - forwarded to `getEncodingSync` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Get the encoding of a buffer. | ||
* Uses {@link getEncodingSync} behind the scenes. | ||
* @param {Buffer} buffer Forwarded to `getEncodingSync` | ||
* @param {EncodingOpts} opts Forwarded to `getEncodingSync` | ||
* @param {GetEncodingCallback} next | ||
* @returns {void} | ||
*/ | ||
function getEncoding(buffer, opts, next) { | ||
// Fetch and wrap result | ||
var result = getEncodingSync(buffer, opts); | ||
if (result instanceof Error) { | ||
next(result); | ||
} else { | ||
next(null, result); | ||
} | ||
} | ||
/** @type {EncodingResult?} */ | ||
var result; | ||
// Export | ||
module.exports = { isTextSync: isTextSync, isText: isText, isBinarySync: isBinarySync, isBinary: isBinary, getEncodingSync: getEncodingSync, getEncoding: getEncoding }; | ||
try { | ||
result = getEncodingSync(buffer, opts); | ||
} catch (err) { | ||
next(err); | ||
} | ||
next(null, result); | ||
} // Export | ||
module.exports = { | ||
isTextSync: isTextSync, | ||
isText: isText, | ||
isBinarySync: isBinarySync, | ||
isBinary: isBinary, | ||
getEncodingSync: getEncodingSync, | ||
getEncoding: getEncoding | ||
}; |
/* eslint no-use-before-define:0 */ | ||
'use strict'; | ||
'use strict'; // Import | ||
// Import | ||
var pathUtil = require('path'); | ||
var pathUtil = require('path'); | ||
var textExtensions = require('textextensions'); | ||
var binaryExtensions = require('binaryextensions'); | ||
/** | ||
* @typedef {'utf8'|'binary'} EncodingResult | ||
*/ | ||
/** | ||
* 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. | ||
* We use the extensions from https://github.com/bevry/textextensions and https://github.com/bevry/binaryextensions | ||
* @param {string?} filename - the filename for the file/buffer if available | ||
* @param {Buffer?} buffer - the buffer for the file if available | ||
* @returns {Error|boolean} | ||
* @typedef {Object} EncodingOpts | ||
* @property {number} [chunkLength = 24] | ||
* @property {number} [chunkBegin = 0] | ||
*/ | ||
/** | ||
* @callback IsTextCallback | ||
* @param {Error?} error | ||
* @param {boolean} [result] | ||
*/ | ||
/** | ||
* @callback IsBinaryCallback | ||
* @param {Error?} error | ||
* @param {boolean} [result] | ||
*/ | ||
/** | ||
* @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 | ||
* @param {string} [filename] The filename for the file/buffer if available | ||
* @param {Buffer} [buffer] The buffer for the file if available | ||
* @returns {boolean} | ||
*/ | ||
function isTextSync(filename, buffer) { | ||
// Prepare | ||
var isText = null; | ||
// Prepare | ||
var isText = null; // Test extensions | ||
// Test extensions | ||
if (filename) { | ||
// Extract filename | ||
var parts = pathUtil.basename(filename).split('.').reverse(); | ||
if (filename) { | ||
// Extract filename | ||
var parts = pathUtil.basename(filename).split('.').reverse(); // Cycle extensions | ||
// Cycle extensions | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
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; | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
if (textExtensions.indexOf(extension) !== -1) { | ||
isText = true; | ||
break; | ||
} | ||
// Fallback to encoding if extension check was not enough | ||
if (buffer && isText === null) { | ||
isText = getEncodingSync(buffer) === 'utf8'; | ||
} | ||
if (binaryExtensions.indexOf(extension) !== -1) { | ||
isText = false; | ||
break; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} // Fallback to encoding if extension check was not enough | ||
// Return our result | ||
return isText; | ||
if (buffer && isText === null) { | ||
isText = getEncodingSync(buffer) === 'utf8'; | ||
} // Return our result | ||
return isText; | ||
} | ||
/** | ||
* Is Text | ||
* Uses `isTextSync` behind the scenes. | ||
* @param {string?} filename - forwarded to `isTextSync` | ||
* @param {Buffer?} buffer - forwarded to `isTextSync` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Determine if the filename and/or buffer is text. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string?} filename Forwarded to `isTextSync` | ||
* @param {Buffer?} buffer Forwarded to `isTextSync` | ||
* @param {IsTextCallback} next | ||
* @returns {void} | ||
*/ | ||
function isText(filename, buffer, next) { | ||
var result = isTextSync(filename, buffer); | ||
if (result instanceof Error) { | ||
next(result); | ||
} else { | ||
next(null, result); | ||
} | ||
var result; | ||
try { | ||
result = isTextSync(filename, buffer); | ||
} catch (err) { | ||
next(err); | ||
} | ||
next(null, result); | ||
} | ||
/** | ||
* Is Binary (Synchronous) | ||
* Uses `isTextSync` behind the scenes. | ||
* @param {string?} filename - forwarded to `isTextSync` | ||
* @param {Buffer?} buffer - forwarded to `isTextSync` | ||
* @returns {Error|boolean} | ||
* Determine if the filename and/or buffer is binary. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string} [filename] Forwarded to `isTextSync` | ||
* @param {Buffer} [buffer] Forwarded to `isTextSync` | ||
* @returns {boolean} | ||
*/ | ||
function isBinarySync(filename, buffer) { | ||
// Handle | ||
var result = isTextSync(filename, buffer); | ||
return result instanceof Error ? result : !result; | ||
// Handle | ||
var result = isTextSync(filename, buffer); | ||
return !result; | ||
} | ||
/** | ||
* Is Binary | ||
* Uses `isText` behind the scenes. | ||
* @param {string?} filename - forwarded to `isText` | ||
* @param {Buffer?} buffer - forwarded to `isText` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Determine if the filename and/or buffer is binary. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string?} filename Forwarded to `isText` | ||
* @param {Buffer?} buffer Forwarded to `isText` | ||
* @param {IsBinaryCallback} next | ||
* @returns {void} | ||
*/ | ||
function isBinary(filename, buffer, next) { | ||
// Handle | ||
isText(filename, buffer, function (err, result) { | ||
if (err) return next(err); | ||
return next(null, !result); | ||
}); | ||
var result; | ||
try { | ||
result = isTextSync(filename, buffer); | ||
} catch (err) { | ||
next(err); | ||
} | ||
next(null, !result); | ||
} | ||
/** | ||
@@ -123,77 +159,95 @@ * Get the encoding of a buffer. | ||
* @param {Buffer} buffer | ||
* @param {Object?} [opts] | ||
* @param {number?} [opts.chunkLength = 24] | ||
* @param {number?} [opts.chunkBegin = 0] | ||
* @returns {Error|string} either an Error instance if something went wrong, or if successful "utf8" or "binary" | ||
* @param {EncodingOpts} [opts] | ||
* @returns {EncodingResult} | ||
*/ | ||
function getEncodingSync(buffer, opts) { | ||
// Prepare | ||
var textEncoding = 'utf8'; | ||
var binaryEncoding = 'binary'; | ||
// Prepare | ||
var textEncoding = 'utf8'; | ||
var binaryEncoding = 'binary'; // Discover | ||
// Discover | ||
if (opts == null) { | ||
// Start | ||
var chunkLength = 24; | ||
var encoding = getEncodingSync(buffer, { chunkLength: chunkLength }); | ||
if (encoding === textEncoding) { | ||
// Middle | ||
var chunkBegin = Math.max(0, Math.floor(buffer.length / 2) - chunkLength); | ||
encoding = getEncodingSync(buffer, { chunkLength: chunkLength, chunkBegin: chunkBegin }); | ||
if (encoding === textEncoding) { | ||
// End | ||
chunkBegin = Math.max(0, buffer.length - chunkLength); | ||
encoding = getEncodingSync(buffer, { chunkLength: chunkLength, chunkBegin: chunkBegin }); | ||
} | ||
} | ||
if (opts == null) { | ||
// Start | ||
var chunkLength = 24; | ||
var encoding = getEncodingSync(buffer, { | ||
chunkLength: chunkLength | ||
}); | ||
// Return | ||
return encoding; | ||
} else { | ||
// Extract | ||
var _opts$chunkLength = opts.chunkLength, | ||
_chunkLength = _opts$chunkLength === undefined ? 24 : _opts$chunkLength, | ||
_opts$chunkBegin = opts.chunkBegin, | ||
_chunkBegin = _opts$chunkBegin === undefined ? 0 : _opts$chunkBegin; | ||
if (encoding === textEncoding) { | ||
// Middle | ||
var chunkBegin = Math.max(0, Math.floor(buffer.length / 2) - chunkLength); | ||
encoding = getEncodingSync(buffer, { | ||
chunkLength: chunkLength, | ||
chunkBegin: chunkBegin | ||
}); | ||
var chunkEnd = Math.min(buffer.length, _chunkBegin + _chunkLength); | ||
var contentChunkUTF8 = buffer.toString(textEncoding, _chunkBegin, chunkEnd); | ||
var _encoding = textEncoding; | ||
if (encoding === textEncoding) { | ||
// End | ||
chunkBegin = Math.max(0, buffer.length - chunkLength); | ||
encoding = getEncodingSync(buffer, { | ||
chunkLength: chunkLength, | ||
chunkBegin: chunkBegin | ||
}); | ||
} | ||
} // Return | ||
// Detect encoding | ||
for (var i = 0; i < contentChunkUTF8.length; ++i) { | ||
var charCode = contentChunkUTF8.charCodeAt(i); | ||
if (charCode === 65533 || charCode <= 8) { | ||
// 8 and below are control characters (e.g. backspace, null, eof, etc.) | ||
// 65533 is the unknown character | ||
// console.log(charCode, contentChunkUTF8[i]) | ||
_encoding = binaryEncoding; | ||
break; | ||
} | ||
} | ||
// Return | ||
return _encoding; | ||
} | ||
return encoding; | ||
} else { | ||
// Extract | ||
var _opts$chunkLength = opts.chunkLength, | ||
_chunkLength = _opts$chunkLength === void 0 ? 24 : _opts$chunkLength, | ||
_opts$chunkBegin = opts.chunkBegin, | ||
_chunkBegin = _opts$chunkBegin === void 0 ? 0 : _opts$chunkBegin; | ||
var chunkEnd = Math.min(buffer.length, _chunkBegin + _chunkLength); | ||
var contentChunkUTF8 = buffer.toString(textEncoding, _chunkBegin, chunkEnd); // Detect encoding | ||
for (var i = 0; i < contentChunkUTF8.length; ++i) { | ||
var charCode = contentChunkUTF8.charCodeAt(i); | ||
if (charCode === 65533 || charCode <= 8) { | ||
// 8 and below are control characters (e.g. backspace, null, eof, etc.) | ||
// 65533 is the unknown character | ||
// console.log(charCode, contentChunkUTF8[i]) | ||
return binaryEncoding; | ||
} | ||
} // Return | ||
return textEncoding; | ||
} | ||
} | ||
/** | ||
* Get the encoding of a buffer | ||
* Uses `getEncodingSync` behind the scenes. | ||
* @param {Buffer} buffer - forwarded to `getEncodingSync` | ||
* @param {Object} opts - forwarded to `getEncodingSync` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Get the encoding of a buffer. | ||
* Uses {@link getEncodingSync} behind the scenes. | ||
* @param {Buffer} buffer Forwarded to `getEncodingSync` | ||
* @param {EncodingOpts} opts Forwarded to `getEncodingSync` | ||
* @param {GetEncodingCallback} next | ||
* @returns {void} | ||
*/ | ||
function getEncoding(buffer, opts, next) { | ||
// Fetch and wrap result | ||
var result = getEncodingSync(buffer, opts); | ||
if (result instanceof Error) { | ||
next(result); | ||
} else { | ||
next(null, result); | ||
} | ||
} | ||
/** @type {EncodingResult?} */ | ||
var result; | ||
// Export | ||
module.exports = { isTextSync: isTextSync, isText: isText, isBinarySync: isBinarySync, isBinary: isBinary, getEncodingSync: getEncodingSync, getEncoding: getEncoding }; | ||
try { | ||
result = getEncodingSync(buffer, opts); | ||
} catch (err) { | ||
next(err); | ||
} | ||
next(null, result); | ||
} // Export | ||
module.exports = { | ||
isTextSync: isTextSync, | ||
isText: isText, | ||
isBinarySync: isBinarySync, | ||
isBinary: isBinary, | ||
getEncodingSync: getEncodingSync, | ||
getEncoding: getEncoding | ||
}; |
# History | ||
## v2.4.0 2019 January 20 | ||
- Asynchronous methods now `try...catch` the synchronous methods to ensure an error from invalid inputs would be given to the callback. | ||
- Before they would not do any `try...catch` so if invalid inputs were given, the error would throw. | ||
- The JSDoc documentation has been updated for accuracy. | ||
- It previously indicated that the return types of the sync methods could have been an error instance, this was incorrect, they would throw if received invalid inputs. | ||
- It previously indicated that the result for of the async `getEncoding` callback was a boolean, this was incorrect, it would be the string result of `getEncodingSync`. | ||
- Updated [base files](https://github.com/bevry/base) and [editions](https://editions.bevry.me) using [boundation](https://github.com/bevry/boundation) | ||
## v2.3.0 2018 November 7 | ||
- Ensure that [textextensions](https://github.com/bevry/textextensions) and [binaryextensions](https://github.com/bevry/binaryextensions) are the latest versions at the time of publishing | ||
- Updated [base files](https://github.com/bevry/base) and [editions](https://github.com/bevry/editions) using [boundation](https://github.com/bevry/boundation) | ||
- Ensure that [textextensions](https://github.com/bevry/textextensions) and [binaryextensions](https://github.com/bevry/binaryextensions) are the latest versions at the time of publishing | ||
- Updated [base files](https://github.com/bevry/base) and [editions](https://github.com/bevry/editions) using [boundation](https://github.com/bevry/boundation) | ||
## v2.2.1 2018 January 24 | ||
- Added missing development dependency | ||
- Added missing development dependency | ||
## v2.2.0 2018 January 24 | ||
- Fixed invalid `package.json` error | ||
- Thanks to [Sean](https://github.com/AlbinoDrought) for [pull request #8](https://github.com/bevry/istextorbinary/pull/8) | ||
- Updated base files | ||
- Fixed invalid `package.json` error | ||
- Thanks to [Sean](https://github.com/AlbinoDrought) for [pull request #8](https://github.com/bevry/istextorbinary/pull/8) | ||
- Updated base files | ||
## v2.1.0 2016 May 10 | ||
- Support v2 of [textextensions](https://github.com/bevry/textextensions) and [binaryextensions](https://github.com/bevry/binaryextensions) | ||
- Support v2 of [textextensions](https://github.com/bevry/textextensions) and [binaryextensions](https://github.com/bevry/binaryextensions) | ||
## v2.0.0 2016 May 2 | ||
- Converted from CoffeeScript to JavaScript | ||
- Fixed `getEncoding` and `isText` not handling errors correctly | ||
- Right-most extension takes preference, instead of left-most | ||
- Thanks to [Ian Sibner](https://github.com/sibnerian) for [pull request #5](https://github.com/bevry/istextorbinary/pull/5) | ||
- **This has bumped the major** as it changes the output result, which could potentially break some apps, despite the API remaining exactly the same | ||
- Converted from CoffeeScript to JavaScript | ||
- Fixed `getEncoding` and `isText` not handling errors correctly | ||
- Right-most extension takes preference, instead of left-most | ||
- Thanks to [Ian Sibner](https://github.com/sibnerian) for [pull request #5](https://github.com/bevry/istextorbinary/pull/5) | ||
- **This has bumped the major** as it changes the output result, which could potentially break some apps, despite the API remaining exactly the same | ||
## v1.0.2 2015 January 16 | ||
- Fixed build | ||
- Added test for text files | ||
- Fixed build | ||
- Added test for text files | ||
## v1.0.1 2015 January 16 | ||
- Cleaned up thanks to [Shunnosuke Watanabe](https://github.com/shinnn) for [pull request #2](https://github.com/bevry/istextorbinary/pull/2) | ||
- Cleaned up thanks to [Shunnosuke Watanabe](https://github.com/shinnn) for [pull request #2](https://github.com/bevry/istextorbinary/pull/2) | ||
## v1.0.0 2013 October 25 | ||
- Initial release extracted from [balupton/bal-util](https://github.com/balupton/bal-util/blob/6501d51bc0244fce3781fc0150136f7493099237/src/lib/paths.coffee#L100-L201) | ||
- Initial release extracted from [balupton/bal-util](https://github.com/balupton/bal-util/blob/6501d51bc0244fce3781fc0150136f7493099237/src/lib/paths.coffee#L100-L201) where it was introduced [2012 September 24](https://github.com/balupton/bal-util/blob/master/HISTORY.md#v1137-2012-september-24). |
{ | ||
"title": "Is Text or Binary?", | ||
"name": "istextorbinary", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Determines if a buffer is comprised of text or binary", | ||
@@ -80,3 +80,3 @@ "homepage": "https://github.com/bevry/istextorbinary", | ||
"entry": "index.js", | ||
"syntaxes": [ | ||
"tags": [ | ||
"javascript", | ||
@@ -87,3 +87,3 @@ "esnext", | ||
"engines": { | ||
"node": ">=6", | ||
"node": "6 || 8 || 10 || 11", | ||
"browsers": false | ||
@@ -96,3 +96,3 @@ } | ||
"entry": "index.js", | ||
"syntaxes": [ | ||
"tags": [ | ||
"javascript", | ||
@@ -107,6 +107,6 @@ "require" | ||
{ | ||
"description": "esnext compiled for node.js >=0.12 with require for modules", | ||
"description": "esnext compiled for node.js 0.12 with require for modules", | ||
"directory": "edition-node-0.12", | ||
"entry": "index.js", | ||
"syntaxes": [ | ||
"tags": [ | ||
"javascript", | ||
@@ -116,3 +116,3 @@ "require" | ||
"engines": { | ||
"node": "0.12 || 4 || 6 || 8 || 10", | ||
"node": "0.12 || 4 || 6 || 8 || 10 || 11", | ||
"browsers": false | ||
@@ -126,20 +126,25 @@ } | ||
"binaryextensions": "^2.1.2", | ||
"editions": "^2.0.2", | ||
"editions": "^2.1.3", | ||
"textextensions": "^2.4.0" | ||
}, | ||
"devDependencies": { | ||
"assert-helpers": "^4.5.1", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"documentation": "^8.1.2", | ||
"eslint": "^5.8.0", | ||
"joe": "^2.0.2", | ||
"joe-reporter-console": "^2.0.2", | ||
"projectz": "^1.4.0", | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.2.2", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.2.0", | ||
"@babel/preset-env": "^7.2.3", | ||
"assert-helpers": "^4.9.6", | ||
"eslint": "^5.12.1", | ||
"eslint-config-bevry": "^1.1.2", | ||
"eslint-config-prettier": "^3.6.0", | ||
"eslint-plugin-prettier": "^3.0.1", | ||
"jsdoc": "^3.5.5", | ||
"kava": "^3.1.0", | ||
"minami": "^1.2.3", | ||
"prettier": "^1.15.3", | ||
"projectz": "^1.7.4", | ||
"surge": "^0.20.1", | ||
"valid-directory": "^1.0.0" | ||
}, | ||
"optionalDependencies": {}, | ||
"scripts": { | ||
"our:clean": "rm -Rf ./docs ./edition* ./es2015 ./es5 ./out", | ||
"our:clean": "rm -Rf ./docs ./edition* ./es2015 ./es5 ./out ./.next", | ||
"our:compile": "npm run our:compile:edition-browsers && npm run our:compile:edition-node-0.12", | ||
@@ -150,3 +155,4 @@ "our:compile:edition-browsers": "env BABEL_ENV=edition-browsers babel --out-dir ./edition-browsers ./source", | ||
"our:meta": "npm run our:meta:docs && npm run our:meta:projectz", | ||
"our:meta:docs": "documentation build -f html -o ./docs -g --shallow ./source/**.js", | ||
"our:meta:docs": "npm run our:meta:docs:jsdoc", | ||
"our:meta:docs:jsdoc": "rm -Rf ./docs && jsdoc --recurse --pedantic --access all --destination ./docs --package ./package.json --readme ./README.md --template ./node_modules/minami ./source && mv ./docs/$npm_package_name/$npm_package_version/* ./docs/ && rm -Rf ./docs/$npm_package_name/$npm_package_version", | ||
"our:meta:projectz": "projectz compile", | ||
@@ -164,29 +170,46 @@ "our:release": "npm run our:release:prepare && npm run our:release:check-changelog && npm run our:release:check-dirty && npm run our:release:tag && npm run our:release:push", | ||
"our:verify:directory": "npx valid-directory", | ||
"our:verify:eslint": "eslint --fix ./source", | ||
"test": "node --harmony ./test.js --joe-reporter=console" | ||
"our:verify:eslint": "eslint --fix --ignore-pattern '**/*.d.ts' --ignore-pattern '**/vendor/' --ignore-pattern '**/node_modules/' --ext .mjs,.js,.jsx,.ts,.tsx ./source", | ||
"test": "node ./test.js" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"bevry" | ||
] | ||
}, | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true | ||
}, | ||
"babel": { | ||
"env": { | ||
"edition-browsers": { | ||
"sourceType": "script", | ||
"presets": [ | ||
[ | ||
"env", | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"browsers": "defaults" | ||
} | ||
"targets": "defaults", | ||
"modules": "commonjs" | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"@babel/proposal-object-rest-spread" | ||
] | ||
}, | ||
"edition-node-0.12": { | ||
"sourceType": "script", | ||
"presets": [ | ||
[ | ||
"env", | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "0.12" | ||
} | ||
}, | ||
"modules": "commonjs" | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"@babel/proposal-object-rest-spread" | ||
] | ||
@@ -193,0 +216,0 @@ } |
@@ -41,44 +41,44 @@ <!-- TITLE/ --> | ||
<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>NPM</h3></a><ul> | ||
<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>npm</h3></a> | ||
<ul> | ||
<li>Install: <code>npm install --save istextorbinary</code></li> | ||
<li>Module: <code>require('istextorbinary')</code></li></ul> | ||
<li>Require: <code>require('istextorbinary')</code></li> | ||
</ul> | ||
<a href="http://browserify.org" title="Browserify lets you require('modules') in the browser by bundling up all of your dependencies"><h3>Browserify</h3></a><ul> | ||
<li>Install: <code>npm install --save istextorbinary</code></li> | ||
<li>Module: <code>require('istextorbinary')</code></li> | ||
<li>CDN URL: <code>//wzrd.in/bundle/istextorbinary@2.3.0</code></li></ul> | ||
<a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a> | ||
<a href="http://enderjs.com" title="Ender is a full featured package manager for your browser"><h3>Ender</h3></a><ul> | ||
<li>Install: <code>ender add istextorbinary</code></li> | ||
<li>Module: <code>require('istextorbinary')</code></li></ul> | ||
``` html | ||
<script type="module"> | ||
import * as pkg from '//dev.jspm.io/istextorbinary' | ||
</script> | ||
``` | ||
<h3><a href="https://github.com/bevry/editions" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3> | ||
<h3><a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3> | ||
<p>This package is published with the following editions:</p> | ||
<ul><li><code>istextorbinary</code> aliases <code>istextorbinary/index.js</code> which uses <a href="https://github.com/bevry/editions" title="Editions are the best way to produce and consume packages you care about.">Editions</a> to automatically select the correct edition for the consumers environment</li> | ||
<ul><li><code>istextorbinary</code> aliases <code>istextorbinary/index.js</code> which uses <a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a> to automatically select the correct edition for the consumers environment</li> | ||
<li><code>istextorbinary/source/index.js</code> is esnext source code with require for modules</li> | ||
<li><code>istextorbinary/edition-browsers/index.js</code> is esnext compiled for browsers with require for modules</li> | ||
<li><code>istextorbinary/edition-node-0.12/index.js</code> is esnext compiled for node.js >=0.12 with require for modules</li></ul> | ||
<li><code>istextorbinary/edition-node-0.12/index.js</code> is esnext compiled for node.js 0.12 with require for modules</li></ul> | ||
<!-- /INSTALL --> | ||
<h3><a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a></h3> | ||
This project provides its type information via inline <a href="http://usejsdoc.org" title="JSDoc is an API documentation generator for JavaScript, similar to Javadoc or phpDocumentor">JSDoc Comments</a>. To make use of this in <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a>, set your <code>maxNodeModuleJsDepth</code> compiler option to `5` or thereabouts. You can accomlish this via your `tsconfig.json` file like so: | ||
## Usage | ||
``` json | ||
{ | ||
"compilerOptions": { | ||
"maxNodeModuleJsDepth": 5 | ||
} | ||
} | ||
``` | ||
``` javascript | ||
// Synchronous API | ||
var result = require('istextorbinary').isTextSync(filename, buffer) | ||
<!-- /INSTALL --> | ||
// Asynchronous API | ||
require('istextorbinary').isText(filename, buffer, function(err, result){ | ||
// ... | ||
}) | ||
// You can supply text or buffer, or both text and buffer, the more provided, the more accurate the result | ||
``` | ||
## Usage | ||
[API Documentation.](http://master.istextorbinary.bevry.surge.sh/docs/) | ||
<!-- HISTORY/ --> | ||
@@ -85,0 +85,0 @@ |
@@ -10,12 +10,39 @@ /* eslint no-use-before-define: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. | ||
* We use the extensions from https://github.com/bevry/textextensions and https://github.com/bevry/binaryextensions | ||
* @param {string?} filename - the filename for the file/buffer if available | ||
* @param {Buffer?} buffer - the buffer for the file if available | ||
* @returns {Error|boolean} | ||
* @typedef {'utf8'|'binary'} EncodingResult | ||
*/ | ||
function isTextSync (filename, buffer) { | ||
/** | ||
* @typedef {Object} EncodingOpts | ||
* @property {number} [chunkLength = 24] | ||
* @property {number} [chunkBegin = 0] | ||
*/ | ||
/** | ||
* @callback IsTextCallback | ||
* @param {Error?} error | ||
* @param {boolean} [result] | ||
*/ | ||
/** | ||
* @callback IsBinaryCallback | ||
* @param {Error?} error | ||
* @param {boolean} [result] | ||
*/ | ||
/** | ||
* @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 | ||
* @param {string} [filename] The filename for the file/buffer if available | ||
* @param {Buffer} [buffer] The buffer for the file if available | ||
* @returns {boolean} | ||
*/ | ||
function isTextSync(filename, buffer) { | ||
// Prepare | ||
@@ -27,3 +54,6 @@ let isText = null | ||
// Extract filename | ||
const parts = pathUtil.basename(filename).split('.').reverse() | ||
const parts = pathUtil | ||
.basename(filename) | ||
.split('.') | ||
.reverse() | ||
@@ -53,46 +83,48 @@ // Cycle extensions | ||
/** | ||
* Is Text | ||
* Uses `isTextSync` behind the scenes. | ||
* @param {string?} filename - forwarded to `isTextSync` | ||
* @param {Buffer?} buffer - forwarded to `isTextSync` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Determine if the filename and/or buffer is text. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string?} filename Forwarded to `isTextSync` | ||
* @param {Buffer?} buffer Forwarded to `isTextSync` | ||
* @param {IsTextCallback} next | ||
* @returns {void} | ||
*/ | ||
function isText (filename, buffer, next) { | ||
const result = isTextSync(filename, buffer) | ||
if (result instanceof Error) { | ||
next(result) | ||
function isText(filename, buffer, next) { | ||
let result | ||
try { | ||
result = isTextSync(filename, buffer) | ||
} catch (err) { | ||
next(err) | ||
} | ||
else { | ||
next(null, result) | ||
} | ||
next(null, result) | ||
} | ||
/** | ||
* Is Binary (Synchronous) | ||
* Uses `isTextSync` behind the scenes. | ||
* @param {string?} filename - forwarded to `isTextSync` | ||
* @param {Buffer?} buffer - forwarded to `isTextSync` | ||
* @returns {Error|boolean} | ||
* Determine if the filename and/or buffer is binary. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string} [filename] Forwarded to `isTextSync` | ||
* @param {Buffer} [buffer] Forwarded to `isTextSync` | ||
* @returns {boolean} | ||
*/ | ||
function isBinarySync (filename, buffer) { | ||
function isBinarySync(filename, buffer) { | ||
// Handle | ||
const result = isTextSync(filename, buffer) | ||
return result instanceof Error ? result : !result | ||
return !result | ||
} | ||
/** | ||
* Is Binary | ||
* Uses `isText` behind the scenes. | ||
* @param {string?} filename - forwarded to `isText` | ||
* @param {Buffer?} buffer - forwarded to `isText` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Determine if the filename and/or buffer is binary. | ||
* Uses {@link isTextSync} behind the scenes. | ||
* @param {string?} filename Forwarded to `isText` | ||
* @param {Buffer?} buffer Forwarded to `isText` | ||
* @param {IsBinaryCallback} next | ||
* @returns {void} | ||
*/ | ||
function isBinary (filename, buffer, next) { | ||
// Handle | ||
isText(filename, buffer, function (err, result) { | ||
if (err) return next(err) | ||
return next(null, !result) | ||
}) | ||
function isBinary(filename, buffer, next) { | ||
let result | ||
try { | ||
result = isTextSync(filename, buffer) | ||
} catch (err) { | ||
next(err) | ||
} | ||
next(null, !result) | ||
} | ||
@@ -105,8 +137,6 @@ | ||
* @param {Buffer} buffer | ||
* @param {Object?} [opts] | ||
* @param {number?} [opts.chunkLength = 24] | ||
* @param {number?} [opts.chunkBegin = 0] | ||
* @returns {Error|string} either an Error instance if something went wrong, or if successful "utf8" or "binary" | ||
* @param {EncodingOpts} [opts] | ||
* @returns {EncodingResult} | ||
*/ | ||
function getEncodingSync (buffer, opts) { | ||
function getEncodingSync(buffer, opts) { | ||
// Prepare | ||
@@ -134,4 +164,3 @@ const textEncoding = 'utf8' | ||
return encoding | ||
} | ||
else { | ||
} else { | ||
// Extract | ||
@@ -141,3 +170,2 @@ const { chunkLength = 24, chunkBegin = 0 } = opts | ||
const contentChunkUTF8 = buffer.toString(textEncoding, chunkBegin, chunkEnd) | ||
let encoding = textEncoding | ||
@@ -151,4 +179,3 @@ // Detect encoding | ||
// console.log(charCode, contentChunkUTF8[i]) | ||
encoding = binaryEncoding | ||
break | ||
return binaryEncoding | ||
} | ||
@@ -158,3 +185,3 @@ } | ||
// Return | ||
return encoding | ||
return textEncoding | ||
} | ||
@@ -164,21 +191,28 @@ } | ||
/** | ||
* Get the encoding of a buffer | ||
* Uses `getEncodingSync` behind the scenes. | ||
* @param {Buffer} buffer - forwarded to `getEncodingSync` | ||
* @param {Object} opts - forwarded to `getEncodingSync` | ||
* @param {Function} next - accepts arguments: (error: Error, result: Boolean) | ||
* @returns {nothing} | ||
* Get the encoding of a buffer. | ||
* Uses {@link getEncodingSync} behind the scenes. | ||
* @param {Buffer} buffer Forwarded to `getEncodingSync` | ||
* @param {EncodingOpts} opts Forwarded to `getEncodingSync` | ||
* @param {GetEncodingCallback} next | ||
* @returns {void} | ||
*/ | ||
function getEncoding (buffer, opts, next) { | ||
// Fetch and wrap result | ||
const result = getEncodingSync(buffer, opts) | ||
if (result instanceof Error) { | ||
next(result) | ||
function getEncoding(buffer, opts, next) { | ||
/** @type {EncodingResult?} */ | ||
let result | ||
try { | ||
result = getEncodingSync(buffer, opts) | ||
} catch (err) { | ||
next(err) | ||
} | ||
else { | ||
next(null, result) | ||
} | ||
next(null, result) | ||
} | ||
// Export | ||
module.exports = { isTextSync, isText, isBinarySync, isBinary, getEncodingSync, getEncoding } | ||
module.exports = { | ||
isTextSync, | ||
isText, | ||
isBinarySync, | ||
isBinary, | ||
getEncodingSync, | ||
getEncoding | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40913
599
16
Updatededitions@^2.1.3