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