| 1 |
"use strict"; |
| 2 |
|
| 3 |
let BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i; |
| 4 |
|
| 5 |
module.exports = { |
| 6 |
/** |
| 7 |
* The order that this parser will run, in relation to other parsers. |
| 8 |
* |
| 9 |
* @type {number} |
| 10 |
*/ |
| 11 |
order: 400, |
| 12 |
|
| 13 |
/** |
| 14 |
* Whether to allow "empty" files (zero bytes). |
| 15 |
* |
| 16 |
* @type {boolean} |
| 17 |
*/ |
| 18 |
allowEmpty: true, |
| 19 |
|
| 20 |
/** |
| 21 |
* Determines whether this parser can parse a given file reference. |
| 22 |
* Parsers that return true will be tried, in order, until one successfully parses the file. |
| 23 |
* Parsers that return false will be skipped, UNLESS all parsers returned false, in which case |
| 24 |
* every parser will be tried. |
| 25 |
* |
| 26 |
* @param {object} file - An object containing information about the referenced file |
| 27 |
* @param {string} file.url - The full URL of the referenced file |
| 28 |
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.) |
| 29 |
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver |
| 30 |
* @returns {boolean} |
| 31 |
*/ |
| 32 |
canParse (file) { |
| 33 |
// Use this parser if the file is a Buffer, and has a known binary extension |
| 34 |
return Buffer.isBuffer(file.data) && BINARY_REGEXP.test(file.url); |
| 35 |
}, |
| 36 |
|
| 37 |
/** |
| 38 |
* Parses the given data as a Buffer (byte array). |
| 39 |
* |
| 40 |
* @param {object} file - An object containing information about the referenced file |
| 41 |
* @param {string} file.url - The full URL of the referenced file |
| 42 |
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.) |
| 43 |
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver |
| 44 |
* @returns {Buffer} |
| 45 |
*/ |
| 46 |
parse (file) { |
| 47 |
if (Buffer.isBuffer(file.data)) { |
| 48 |
return file.data; |
| 49 |
} |
| 50 |
else { |
| 51 |
// This will reject if data is anything other than a string or typed array |
| 52 |
return Buffer.from(file.data); |
| 53 |
} |
| 54 |
} |
| 55 |
}; |