UNPKG

4.51 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.detectFileSync = exports.detectFile = exports.analyse = exports.detect = void 0;
26const node_1 = __importDefault(require("./fs/node"));
27const utf8_1 = __importDefault(require("./encoding/utf8"));
28const unicode = __importStar(require("./encoding/unicode"));
29const mbcs = __importStar(require("./encoding/mbcs"));
30const sbcs = __importStar(require("./encoding/sbcs"));
31const iso2022 = __importStar(require("./encoding/iso2022"));
32const recognisers = [
33 new utf8_1.default(),
34 new unicode.UTF_16BE(),
35 new unicode.UTF_16LE(),
36 new unicode.UTF_32BE(),
37 new unicode.UTF_32LE(),
38 new mbcs.sjis(),
39 new mbcs.big5(),
40 new mbcs.euc_jp(),
41 new mbcs.euc_kr(),
42 new mbcs.gb_18030(),
43 new iso2022.ISO_2022_JP(),
44 new iso2022.ISO_2022_KR(),
45 new iso2022.ISO_2022_CN(),
46 new sbcs.ISO_8859_1(),
47 new sbcs.ISO_8859_2(),
48 new sbcs.ISO_8859_5(),
49 new sbcs.ISO_8859_6(),
50 new sbcs.ISO_8859_7(),
51 new sbcs.ISO_8859_8(),
52 new sbcs.ISO_8859_9(),
53 new sbcs.windows_1251(),
54 new sbcs.windows_1256(),
55 new sbcs.KOI8_R(),
56];
57const detect = (buffer) => {
58 const matches = (0, exports.analyse)(buffer);
59 return matches.length > 0 ? matches[0].name : null;
60};
61exports.detect = detect;
62const analyse = (buffer) => {
63 const byteStats = [];
64 for (let i = 0; i < 256; i++)
65 byteStats[i] = 0;
66 for (let i = buffer.length - 1; i >= 0; i--)
67 byteStats[buffer[i] & 0x00ff]++;
68 let c1Bytes = false;
69 for (let i = 0x80; i <= 0x9f; i += 1) {
70 if (byteStats[i] !== 0) {
71 c1Bytes = true;
72 break;
73 }
74 }
75 const context = {
76 byteStats,
77 c1Bytes,
78 rawInput: buffer,
79 rawLen: buffer.length,
80 inputBytes: buffer,
81 inputLen: buffer.length,
82 };
83 const matches = recognisers
84 .map((rec) => {
85 return rec.match(context);
86 })
87 .filter((match) => {
88 return !!match;
89 })
90 .sort((a, b) => {
91 return b.confidence - a.confidence;
92 });
93 return matches;
94};
95exports.analyse = analyse;
96const detectFile = (filepath, opts = {}) => new Promise((resolve, reject) => {
97 let fd;
98 const fs = (0, node_1.default)();
99 const handler = (err, buffer) => {
100 if (fd) {
101 fs.closeSync(fd);
102 }
103 if (err) {
104 reject(err);
105 }
106 else {
107 resolve((0, exports.detect)(buffer));
108 }
109 };
110 if (opts && opts.sampleSize) {
111 fd = fs.openSync(filepath, 'r');
112 const sample = Buffer.allocUnsafe(opts.sampleSize);
113 fs.read(fd, sample, 0, opts.sampleSize, null, (err) => {
114 handler(err, sample);
115 });
116 return;
117 }
118 fs.readFile(filepath, handler);
119});
120exports.detectFile = detectFile;
121const detectFileSync = (filepath, opts = {}) => {
122 const fs = (0, node_1.default)();
123 if (opts && opts.sampleSize) {
124 const fd = fs.openSync(filepath, 'r');
125 const sample = Buffer.allocUnsafe(opts.sampleSize);
126 fs.readSync(fd, sample, 0, opts.sampleSize);
127 fs.closeSync(fd);
128 return (0, exports.detect)(sample);
129 }
130 return (0, exports.detect)(fs.readFileSync(filepath));
131};
132exports.detectFileSync = detectFileSync;
133exports.default = {
134 analyse: exports.analyse,
135 detect: exports.detect,
136 detectFileSync: exports.detectFileSync,
137 detectFile: exports.detectFile,
138};
139//# sourceMappingURL=index.js.map
\No newline at end of file