| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /*! |
| 2 | * remove-bom-buffer <https://github.com/jonschlinkert/remove-bom-buffer> |
| 3 | * |
| 4 | * Copyright (c) 2015-2017, Jon Schlinkert. |
| 5 | * Released under the MIT License. |
| 6 | */ |
| 7 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | var isUTF8 = require('is-utf8'); |
| 11 | var isBuffer = require('is-buffer'); |
| 12 | |
| 13 | function matchBOM(buf) { |
| 14 | return buf[0] === 0xEF && buf[1] === 0xBB && buf[2] === 0xBF; |
| 15 | } |
| 16 | |
| 17 | function maybeUTF8(buf) { |
| 18 | // Only "maybe" because we aren't sniffing the whole buffer |
| 19 | return isUTF8(buf.slice(3, 7)); |
| 20 | } |
| 21 | |
| 22 | module.exports = function(buf) { |
| 23 | if (isBuffer(buf) && matchBOM(buf) && maybeUTF8(buf)) { |
| 24 | return buf.slice(3); |
| 25 | } |
| 26 | return buf; |
| 27 | }; |