filecat 1.0.30 → 1.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __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
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const worker_threads_1 = require("worker_threads");
13
+ const fs = require("fs");
14
+ function copySubarray(buffer, start, end) {
15
+ const length = end - start;
16
+ const newBuffer = Buffer.alloc(length);
17
+ buffer.copy(newBuffer, 0, start, end);
18
+ return newBuffer;
19
+ }
20
+ function buildBadCharTable(pattern) {
21
+ const table = {};
22
+ const m = pattern.length;
23
+ for (let i = 0; i < m; i++) {
24
+ table[pattern[i]] = i;
25
+ }
26
+ return table;
27
+ }
28
+ function boyerMooreSearch(text, pattern, list) {
29
+ const badCharTable = buildBadCharTable(pattern);
30
+ const text_len = text.length;
31
+ const pattern_len = pattern.length;
32
+ const text_len_pattern_len = text_len - pattern_len;
33
+ let i = 0;
34
+ while (i <= text_len_pattern_len) {
35
+ let last_p_i = pattern_len - 1;
36
+ while (last_p_i >= 0 && text[i + last_p_i] === pattern[last_p_i]) {
37
+ last_p_i--;
38
+ }
39
+ if (last_p_i < 0) {
40
+ list.push(i);
41
+ i += (i + pattern_len < text_len) ?
42
+ pattern_len - badCharTable[text[i + pattern_len]] || pattern_len
43
+ : 1;
44
+ }
45
+ else {
46
+ const badCharLast = badCharTable[text[i + last_p_i]];
47
+ const badCharShift = badCharLast !== undefined
48
+ ? last_p_i - badCharLast
49
+ : last_p_i + 1;
50
+ i += Math.max(badCharShift, 1);
51
+ }
52
+ }
53
+ }
54
+ let done = false;
55
+ worker_threads_1.parentPort.on("message", (message) => __awaiter(void 0, void 0, void 0, function* () {
56
+ try {
57
+ switch (message.type) {
58
+ case 1:
59
+ {
60
+ const { start, end, file_path, query_text_buffer } = message;
61
+ let haveReadSize = 0;
62
+ let bufferContent = Buffer.alloc(0);
63
+ const fd = fs.openSync(file_path, "r");
64
+ const read_len = 1024 * 1024 * 2;
65
+ let text_start_index = 0;
66
+ while (haveReadSize < end) {
67
+ if (done)
68
+ break;
69
+ const buffer = Buffer.alloc(read_len);
70
+ let bytesRead = fs.readSync(fd, buffer, 0, buffer.length, haveReadSize);
71
+ if (bytesRead === 0)
72
+ break;
73
+ haveReadSize += bytesRead;
74
+ bufferContent = Buffer.concat([bufferContent, buffer.subarray(0, bytesRead)]);
75
+ worker_threads_1.parentPort.postMessage({ type: 5, progress: (haveReadSize * 100 / end).toFixed(0) });
76
+ const r_list = [];
77
+ boyerMooreSearch(bufferContent, query_text_buffer, r_list);
78
+ if (r_list.length > 0) {
79
+ for (let i = 0; i < r_list.length; i++) {
80
+ r_list[i] += text_start_index;
81
+ }
82
+ worker_threads_1.parentPort.postMessage({ type: 2, find_index: r_list });
83
+ }
84
+ text_start_index += (bufferContent.length - query_text_buffer.length);
85
+ bufferContent = copySubarray(bufferContent, bufferContent.length - query_text_buffer.length, bufferContent.length);
86
+ }
87
+ worker_threads_1.parentPort.postMessage({ type: 3 });
88
+ fs.closeSync(fd);
89
+ }
90
+ break;
91
+ case 4:
92
+ done = true;
93
+ break;
94
+ }
95
+ }
96
+ catch (e) {
97
+ console.log(e);
98
+ }
99
+ }));
100
+ //# sourceMappingURL=file.worker.js.map