source: trip-planner-front/node_modules/isbinaryfile/lib/index.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.4 KB
Line 
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.isBinaryFileSync = exports.isBinaryFile = void 0;
13const fs = require("fs");
14const util_1 = require("util");
15const statAsync = util_1.promisify(fs.stat);
16const openAsync = util_1.promisify(fs.open);
17const closeAsync = util_1.promisify(fs.close);
18const MAX_BYTES = 512;
19function isBinaryFile(file, size) {
20 return __awaiter(this, void 0, void 0, function* () {
21 if (isString(file)) {
22 const stat = yield statAsync(file);
23 isStatFile(stat);
24 const fileDescriptor = yield openAsync(file, 'r');
25 const allocBuffer = Buffer.alloc(MAX_BYTES);
26 // Read the file with no encoding for raw buffer access.
27 // NB: something is severely wrong with promisify, had to construct my own Promise
28 return new Promise((fulfill, reject) => {
29 fs.read(fileDescriptor, allocBuffer, 0, MAX_BYTES, 0, (err, bytesRead, _) => {
30 closeAsync(fileDescriptor);
31 if (err) {
32 reject(err);
33 }
34 else {
35 fulfill(isBinaryCheck(allocBuffer, bytesRead));
36 }
37 });
38 });
39 }
40 else {
41 if (size === undefined) {
42 size = file.length;
43 }
44 return isBinaryCheck(file, size);
45 }
46 });
47}
48exports.isBinaryFile = isBinaryFile;
49function isBinaryFileSync(file, size) {
50 if (isString(file)) {
51 const stat = fs.statSync(file);
52 isStatFile(stat);
53 const fileDescriptor = fs.openSync(file, 'r');
54 const allocBuffer = Buffer.alloc(MAX_BYTES);
55 const bytesRead = fs.readSync(fileDescriptor, allocBuffer, 0, MAX_BYTES, 0);
56 fs.closeSync(fileDescriptor);
57 return isBinaryCheck(allocBuffer, bytesRead);
58 }
59 else {
60 if (size === undefined) {
61 size = file.length;
62 }
63 return isBinaryCheck(file, size);
64 }
65}
66exports.isBinaryFileSync = isBinaryFileSync;
67function isBinaryCheck(fileBuffer, bytesRead) {
68 // empty file. no clue what it is.
69 if (bytesRead === 0) {
70 return false;
71 }
72 let suspiciousBytes = 0;
73 const totalBytes = Math.min(bytesRead, MAX_BYTES);
74 // UTF-8 BOM
75 if (bytesRead >= 3 && fileBuffer[0] === 0xef && fileBuffer[1] === 0xbb && fileBuffer[2] === 0xbf) {
76 return false;
77 }
78 // UTF-32 BOM
79 if (bytesRead >= 4 &&
80 fileBuffer[0] === 0x00 &&
81 fileBuffer[1] === 0x00 &&
82 fileBuffer[2] === 0xfe &&
83 fileBuffer[3] === 0xff) {
84 return false;
85 }
86 // UTF-32 LE BOM
87 if (bytesRead >= 4 &&
88 fileBuffer[0] === 0xff &&
89 fileBuffer[1] === 0xfe &&
90 fileBuffer[2] === 0x00 &&
91 fileBuffer[3] === 0x00) {
92 return false;
93 }
94 // GB BOM
95 if (bytesRead >= 4 &&
96 fileBuffer[0] === 0x84 &&
97 fileBuffer[1] === 0x31 &&
98 fileBuffer[2] === 0x95 &&
99 fileBuffer[3] === 0x33) {
100 return false;
101 }
102 if (totalBytes >= 5 && fileBuffer.slice(0, 5).toString() === '%PDF-') {
103 /* PDF. This is binary. */
104 return true;
105 }
106 // UTF-16 BE BOM
107 if (bytesRead >= 2 && fileBuffer[0] === 0xfe && fileBuffer[1] === 0xff) {
108 return false;
109 }
110 // UTF-16 LE BOM
111 if (bytesRead >= 2 && fileBuffer[0] === 0xff && fileBuffer[1] === 0xfe) {
112 return false;
113 }
114 for (let i = 0; i < totalBytes; i++) {
115 if (fileBuffer[i] === 0) {
116 // NULL byte--it's binary!
117 return true;
118 }
119 else if ((fileBuffer[i] < 7 || fileBuffer[i] > 14) && (fileBuffer[i] < 32 || fileBuffer[i] > 127)) {
120 // UTF-8 detection
121 if (fileBuffer[i] > 193 && fileBuffer[i] < 224 && i + 1 < totalBytes) {
122 i++;
123 if (fileBuffer[i] > 127 && fileBuffer[i] < 192) {
124 continue;
125 }
126 }
127 else if (fileBuffer[i] > 223 && fileBuffer[i] < 240 && i + 2 < totalBytes) {
128 i++;
129 if (fileBuffer[i] > 127 && fileBuffer[i] < 192 && fileBuffer[i + 1] > 127 && fileBuffer[i + 1] < 192) {
130 i++;
131 continue;
132 }
133 }
134 suspiciousBytes++;
135 // Read at least 32 fileBuffer before making a decision
136 if (i > 32 && (suspiciousBytes * 100) / totalBytes > 10) {
137 return true;
138 }
139 }
140 }
141 if ((suspiciousBytes * 100) / totalBytes > 10) {
142 return true;
143 }
144 return false;
145}
146function isString(x) {
147 return typeof x === 'string';
148}
149function isStatFile(stat) {
150 if (!stat.isFile()) {
151 throw new Error(`Path provided was not a file!`);
152 }
153}
Note: See TracBrowser for help on using the repository browser.