UNPKG

2.7 kBJavaScriptView Raw
1/**
2 * Copyright 2013-2022 the PM2 project authors. All rights reserved.
3 * Use of this source code is governed by a license that
4 * can be found in the LICENSE file.
5 */
6var fs = require('fs');
7var max_bytes = 512;
8
9module.exports = function(bytes, size) {
10 // Read the file with no encoding for raw buffer access.
11 if (size === undefined) {
12 var file = bytes;
13 try {
14 if(!fs.statSync(file).isFile()) return false;
15 } catch (err) {
16 // otherwise continue on
17 }
18 var descriptor = fs.openSync(file, 'r');
19 try {
20 bytes = Buffer.alloc(max_bytes);
21 size = fs.readSync(descriptor, bytes, 0, bytes.length, 0);
22 } finally {
23 fs.closeSync(descriptor);
24 }
25 }
26 // async version has a function instead of a `size`
27 else if (typeof size === "function") {
28 var file = bytes, callback = size;
29 fs.stat(file, function(err, stat) {
30 if (err || !stat.isFile()) return callback(null, false);
31
32 fs.open(file, 'r', function(err, descriptor){
33 if (err) return callback(err);
34 var bytes = Buffer.alloc(max_bytes);
35 // Read the file with no encoding for raw buffer access.
36 fs.read(descriptor, bytes, 0, bytes.length, 0, function(err, size, bytes){
37 fs.close(descriptor, function(err2){
38 if (err || err2)
39 return callback(err || err2);
40 return callback(null, isBinaryCheck(bytes, size));
41 });
42 });
43 });
44 });
45 }
46
47 return isBinaryCheck(bytes, size);
48}
49
50function isBinaryCheck(bytes, size) {
51 if (size === 0)
52 return false;
53
54 var suspicious_bytes = 0;
55 var total_bytes = Math.min(size, max_bytes);
56
57 if (size >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
58 // UTF-8 BOM. This isn't binary.
59 return false;
60 }
61
62 for (var i = 0; i < total_bytes; i++) {
63 if (bytes[i] === 0) { // NULL byte--it's binary!
64 return true;
65 }
66 else if ((bytes[i] < 7 || bytes[i] > 14) && (bytes[i] < 32 || bytes[i] > 127)) {
67 // UTF-8 detection
68 if (bytes[i] > 193 && bytes[i] < 224 && i + 1 < total_bytes) {
69 i++;
70 if (bytes[i] > 127 && bytes[i] < 192) {
71 continue;
72 }
73 }
74 else if (bytes[i] > 223 && bytes[i] < 240 && i + 2 < total_bytes) {
75 i++;
76 if (bytes[i] > 127 && bytes[i] < 192 && bytes[i + 1] > 127 && bytes[i + 1] < 192) {
77 i++;
78 continue;
79 }
80 }
81 suspicious_bytes++;
82 // Read at least 32 bytes before making a decision
83 if (i > 32 && (suspicious_bytes * 100) / total_bytes > 10) {
84 return true;
85 }
86 }
87 }
88
89 if ((suspicious_bytes * 100) / total_bytes > 10) {
90 return true;
91 }
92
93 return false;
94}