1 | "use strict";
|
2 | var __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 | }));
|
9 | var __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 | });
|
14 | var __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 | };
|
21 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
22 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
23 | };
|
24 | Object.defineProperty(exports, "__esModule", { value: true });
|
25 | exports.detectFileSync = exports.detectFile = exports.analyse = exports.detect = void 0;
|
26 | const node_1 = __importDefault(require("./fs/node"));
|
27 | const utf8_1 = __importDefault(require("./encoding/utf8"));
|
28 | const unicode = __importStar(require("./encoding/unicode"));
|
29 | const mbcs = __importStar(require("./encoding/mbcs"));
|
30 | const sbcs = __importStar(require("./encoding/sbcs"));
|
31 | const iso2022 = __importStar(require("./encoding/iso2022"));
|
32 | const 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 | ];
|
57 | const detect = (buffer) => {
|
58 | const matches = (0, exports.analyse)(buffer);
|
59 | return matches.length > 0 ? matches[0].name : null;
|
60 | };
|
61 | exports.detect = detect;
|
62 | const 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 | };
|
95 | exports.analyse = analyse;
|
96 | const 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 | });
|
120 | exports.detectFile = detectFile;
|
121 | const 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 | };
|
132 | exports.detectFileSync = detectFileSync;
|
133 | exports.default = {
|
134 | analyse: exports.analyse,
|
135 | detect: exports.detect,
|
136 | detectFileSync: exports.detectFileSync,
|
137 | detectFile: exports.detectFile,
|
138 | };
|
139 |
|
\ | No newline at end of file |