UNPKG

4.96 kBJavaScriptView Raw
1"use strict";
2var __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}));
13var __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});
18var __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};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.detectFileSync = exports.detectFile = exports.analyse = exports.detect = void 0;
30const node_1 = __importDefault(require("./fs/node"));
31const ascii_1 = __importDefault(require("./encoding/ascii"));
32const utf8_1 = __importDefault(require("./encoding/utf8"));
33const unicode = __importStar(require("./encoding/unicode"));
34const mbcs = __importStar(require("./encoding/mbcs"));
35const sbcs = __importStar(require("./encoding/sbcs"));
36const iso2022 = __importStar(require("./encoding/iso2022"));
37const utils_1 = require("./utils");
38const 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];
64const detect = (buffer) => {
65 const matches = (0, exports.analyse)(buffer);
66 return matches.length > 0 ? matches[0].name : null;
67};
68exports.detect = detect;
69const 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};
105exports.analyse = analyse;
106const 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});
130exports.detectFile = detectFile;
131const 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};
142exports.detectFileSync = detectFileSync;
143exports.default = {
144 analyse: exports.analyse,
145 detect: exports.detect,
146 detectFileSync: exports.detectFileSync,
147 detectFile: exports.detectFile,
148};
149//# sourceMappingURL=index.js.map
\No newline at end of file