@lvce-editor/search-process 9.0.0 → 9.2.0
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.
- package/dist/index.js +181 -37
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -8,6 +8,20 @@ import { Writable } from 'node:stream';
|
|
|
8
8
|
import { pipeline } from 'node:stream/promises';
|
|
9
9
|
import { Buffer as Buffer$1 } from 'node:buffer';
|
|
10
10
|
|
|
11
|
+
const searches = Object.create(null);
|
|
12
|
+
const get$1 = id => {
|
|
13
|
+
return searches[id];
|
|
14
|
+
};
|
|
15
|
+
const set$1 = (id, search) => {
|
|
16
|
+
searches[id] = search;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const getIncrementalResults = (id, minLineY, maxLineY) => {
|
|
20
|
+
const item = get$1(id);
|
|
21
|
+
const visibleItems = item.getItems(minLineY, maxLineY);
|
|
22
|
+
return visibleItems;
|
|
23
|
+
};
|
|
24
|
+
|
|
11
25
|
const normalizeLine = line => {
|
|
12
26
|
if (line.startsWith('Error: ')) {
|
|
13
27
|
return line.slice('Error: '.length);
|
|
@@ -674,8 +688,6 @@ const handleUpgrade$1 = async (...args) => {
|
|
|
674
688
|
// @ts-ignore
|
|
675
689
|
return module.handleUpgrade(...args);
|
|
676
690
|
};
|
|
677
|
-
|
|
678
|
-
// @ts-ignore
|
|
679
691
|
const listen$1$1 = async ({
|
|
680
692
|
request,
|
|
681
693
|
handle
|
|
@@ -696,8 +708,6 @@ const listen$1$1 = async ({
|
|
|
696
708
|
const signal$4 = webSocket => {
|
|
697
709
|
webSocket.resume();
|
|
698
710
|
};
|
|
699
|
-
|
|
700
|
-
// @ts-ignore
|
|
701
711
|
const wrap$9 = webSocket => {
|
|
702
712
|
return {
|
|
703
713
|
webSocket,
|
|
@@ -852,6 +862,16 @@ const constructError = (message, type, name) => {
|
|
|
852
862
|
}
|
|
853
863
|
return new ErrorConstructor(message);
|
|
854
864
|
};
|
|
865
|
+
const joinLines = lines => {
|
|
866
|
+
return lines.join(NewLine$1);
|
|
867
|
+
};
|
|
868
|
+
const splitLines = lines => {
|
|
869
|
+
return lines.split(NewLine$1);
|
|
870
|
+
};
|
|
871
|
+
const getCurrentStack = () => {
|
|
872
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(2));
|
|
873
|
+
return currentStack;
|
|
874
|
+
};
|
|
855
875
|
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
856
876
|
return string.indexOf(NewLine$1, startIndex);
|
|
857
877
|
};
|
|
@@ -862,19 +882,16 @@ const getParentStack = error => {
|
|
|
862
882
|
}
|
|
863
883
|
return parentStack;
|
|
864
884
|
};
|
|
865
|
-
const joinLines = lines => {
|
|
866
|
-
return lines.join(NewLine$1);
|
|
867
|
-
};
|
|
868
885
|
const MethodNotFound = -32601;
|
|
869
886
|
const Custom = -32001;
|
|
870
|
-
const splitLines = lines => {
|
|
871
|
-
return lines.split(NewLine$1);
|
|
872
|
-
};
|
|
873
887
|
const restoreJsonRpcError = error => {
|
|
888
|
+
const currentStack = getCurrentStack();
|
|
874
889
|
if (error && error instanceof Error) {
|
|
890
|
+
if (typeof error.stack === 'string') {
|
|
891
|
+
error.stack = error.stack + NewLine$1 + currentStack;
|
|
892
|
+
}
|
|
875
893
|
return error;
|
|
876
894
|
}
|
|
877
|
-
const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
|
|
878
895
|
if (error && error.code && error.code === MethodNotFound) {
|
|
879
896
|
const restoredError = new JsonRpcError(error.message);
|
|
880
897
|
const parentStack = getParentStack(error);
|
|
@@ -955,6 +972,17 @@ const getErrorType = prettyError => {
|
|
|
955
972
|
}
|
|
956
973
|
return undefined;
|
|
957
974
|
};
|
|
975
|
+
const isAlreadyStack = line => {
|
|
976
|
+
return line.trim().startsWith('at ');
|
|
977
|
+
};
|
|
978
|
+
const getStack = prettyError => {
|
|
979
|
+
const stackString = prettyError.stack || '';
|
|
980
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
981
|
+
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
982
|
+
return stackString.slice(newLineIndex + 1);
|
|
983
|
+
}
|
|
984
|
+
return stackString;
|
|
985
|
+
};
|
|
958
986
|
const getErrorProperty = (error, prettyError) => {
|
|
959
987
|
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
960
988
|
return {
|
|
@@ -967,7 +995,7 @@ const getErrorProperty = (error, prettyError) => {
|
|
|
967
995
|
code: Custom,
|
|
968
996
|
message: prettyError.message,
|
|
969
997
|
data: {
|
|
970
|
-
stack: prettyError
|
|
998
|
+
stack: getStack(prettyError),
|
|
971
999
|
codeFrame: prettyError.codeFrame,
|
|
972
1000
|
type: getErrorType(prettyError),
|
|
973
1001
|
code: prettyError.code,
|
|
@@ -1260,6 +1288,10 @@ const handleElectronMessagePort = async messagePort => {
|
|
|
1260
1288
|
};
|
|
1261
1289
|
|
|
1262
1290
|
const handleWebSocket = async (handle, request) => {
|
|
1291
|
+
if (!handle || !request) {
|
|
1292
|
+
// socket might have been closed during transfer
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1263
1295
|
object(handle);
|
|
1264
1296
|
object(request);
|
|
1265
1297
|
await NodeWebSocketRpcClient.create({
|
|
@@ -1375,21 +1407,23 @@ const execRipGrep = async (args, options) => {
|
|
|
1375
1407
|
const spawn = (args, options) => {
|
|
1376
1408
|
try {
|
|
1377
1409
|
const childProcess = NodeChildProcess.spawn(ripGrepPath, args, options);
|
|
1410
|
+
if (!childProcess.stdout || !childProcess.stderr) {
|
|
1411
|
+
throw new Error('stdout missing');
|
|
1412
|
+
}
|
|
1378
1413
|
return {
|
|
1379
|
-
childProcess,
|
|
1380
1414
|
on(event, listener) {
|
|
1381
|
-
|
|
1415
|
+
childProcess.on(event, listener);
|
|
1382
1416
|
},
|
|
1383
1417
|
off(event, listener) {
|
|
1384
|
-
|
|
1418
|
+
childProcess.off(event, listener);
|
|
1385
1419
|
},
|
|
1386
1420
|
once(event, listener) {
|
|
1387
|
-
|
|
1421
|
+
childProcess.once(event, listener);
|
|
1388
1422
|
},
|
|
1389
1423
|
stdout: childProcess.stdout,
|
|
1390
1424
|
stderr: childProcess.stderr,
|
|
1391
1425
|
kill() {
|
|
1392
|
-
|
|
1426
|
+
childProcess.kill();
|
|
1393
1427
|
}
|
|
1394
1428
|
};
|
|
1395
1429
|
} catch (error) {
|
|
@@ -1436,6 +1470,23 @@ const searchFile = async ({
|
|
|
1436
1470
|
const Utf8 = 'utf8';
|
|
1437
1471
|
const Base64 = 'base64';
|
|
1438
1472
|
|
|
1473
|
+
const processData = async (stdout, handleData) => {
|
|
1474
|
+
await pipeline(stdout, new Writable({
|
|
1475
|
+
decodeStrings: false,
|
|
1476
|
+
construct(callback) {
|
|
1477
|
+
callback();
|
|
1478
|
+
},
|
|
1479
|
+
write(chunk, encoding, callback) {
|
|
1480
|
+
try {
|
|
1481
|
+
handleData(chunk);
|
|
1482
|
+
callback();
|
|
1483
|
+
} catch (error) {
|
|
1484
|
+
callback(error);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
}));
|
|
1488
|
+
};
|
|
1489
|
+
|
|
1439
1490
|
const Begin = 'begin';
|
|
1440
1491
|
const Match$1 = 'match';
|
|
1441
1492
|
const Summary = 'summary';
|
|
@@ -1489,13 +1540,13 @@ const toTextSearchResult = (parsedLine, remaining, charsBefore, charsAfter) => {
|
|
|
1489
1540
|
// TODO update client
|
|
1490
1541
|
// TODO not always run nice, maybe configure nice via flag/options
|
|
1491
1542
|
|
|
1492
|
-
const collectStdout = async (
|
|
1543
|
+
const collectStdout = async (stdout, kill, maxSearchResults, charsBefore, charsAfter) => {
|
|
1493
1544
|
const allSearchResults = Object.create(null);
|
|
1494
1545
|
let buffer = '';
|
|
1495
1546
|
let stats = {};
|
|
1496
1547
|
let limitHit = false;
|
|
1497
1548
|
let numberOfResults = 0;
|
|
1498
|
-
|
|
1549
|
+
stdout.setEncoding(Utf8);
|
|
1499
1550
|
const handleLine = line => {
|
|
1500
1551
|
const parsedLine = JSON.parse(line);
|
|
1501
1552
|
const {
|
|
@@ -1541,23 +1592,10 @@ const collectStdout = async (childProcess, maxSearchResults, charsBefore, charsA
|
|
|
1541
1592
|
buffer = dataString.slice(previousIndex);
|
|
1542
1593
|
if (numberOfResults > maxSearchResults) {
|
|
1543
1594
|
limitHit = true;
|
|
1544
|
-
|
|
1595
|
+
kill();
|
|
1545
1596
|
}
|
|
1546
1597
|
};
|
|
1547
|
-
await
|
|
1548
|
-
decodeStrings: false,
|
|
1549
|
-
construct(callback) {
|
|
1550
|
-
callback();
|
|
1551
|
-
},
|
|
1552
|
-
write(chunk, encoding, callback) {
|
|
1553
|
-
try {
|
|
1554
|
-
handleData(chunk);
|
|
1555
|
-
callback();
|
|
1556
|
-
} catch (error) {
|
|
1557
|
-
callback(error);
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
}));
|
|
1598
|
+
await processData(stdout, handleData);
|
|
1561
1599
|
const results = Object.values(allSearchResults).flat();
|
|
1562
1600
|
return {
|
|
1563
1601
|
results,
|
|
@@ -1638,7 +1676,111 @@ const search = async ({
|
|
|
1638
1676
|
const childProcess = spawn(ripGrepArgs, {
|
|
1639
1677
|
cwd: searchDir
|
|
1640
1678
|
});
|
|
1641
|
-
const pipeLinePromise = collectStdout(childProcess, maxSearchResults, charsBefore, charsAfter);
|
|
1679
|
+
const pipeLinePromise = collectStdout(childProcess.stdout, childProcess.kill, maxSearchResults, charsBefore, charsAfter);
|
|
1680
|
+
const closePromise = waitForProcessToExit(childProcess);
|
|
1681
|
+
const [pipeLineResult, exitResult] = await Promise.all([pipeLinePromise, closePromise]);
|
|
1682
|
+
if (exitResult.type === Error$1) {
|
|
1683
|
+
if (isEnoentError(exitResult.event)) {
|
|
1684
|
+
throw new RipGrepNotFoundError(exitResult.event);
|
|
1685
|
+
}
|
|
1686
|
+
throw new TextSearchError(exitResult.event);
|
|
1687
|
+
}
|
|
1688
|
+
return pipeLineResult;
|
|
1689
|
+
};
|
|
1690
|
+
|
|
1691
|
+
const collectStdoutIncremental = async (id, stdout, kill, maxSearchResults, charsBefore, charsAfter) => {
|
|
1692
|
+
const allSearchResults = Object.create(null);
|
|
1693
|
+
let buffer = '';
|
|
1694
|
+
let stats = {};
|
|
1695
|
+
let limitHit = false;
|
|
1696
|
+
let numberOfResults = 0;
|
|
1697
|
+
const search = {
|
|
1698
|
+
dispose() {
|
|
1699
|
+
kill();
|
|
1700
|
+
},
|
|
1701
|
+
getResultCount() {
|
|
1702
|
+
return numberOfResults;
|
|
1703
|
+
},
|
|
1704
|
+
getItems(minLineY, maxLineY) {
|
|
1705
|
+
const results = Object.values(allSearchResults).flat();
|
|
1706
|
+
return results.slice(minLineY, maxLineY);
|
|
1707
|
+
}
|
|
1708
|
+
};
|
|
1709
|
+
set$1(id, search);
|
|
1710
|
+
stdout.setEncoding(Utf8);
|
|
1711
|
+
const handleLine = line => {
|
|
1712
|
+
const parsedLine = JSON.parse(line);
|
|
1713
|
+
const {
|
|
1714
|
+
type,
|
|
1715
|
+
data
|
|
1716
|
+
} = parsedLine;
|
|
1717
|
+
switch (type) {
|
|
1718
|
+
case Begin:
|
|
1719
|
+
allSearchResults[data.path.text] = [{
|
|
1720
|
+
type: File,
|
|
1721
|
+
start: 0,
|
|
1722
|
+
end: 0,
|
|
1723
|
+
lineNumber: 0,
|
|
1724
|
+
text: data.path.text
|
|
1725
|
+
}];
|
|
1726
|
+
break;
|
|
1727
|
+
case Match$1:
|
|
1728
|
+
const remaining = maxSearchResults - numberOfResults;
|
|
1729
|
+
const matches = toTextSearchResult(parsedLine, remaining, charsBefore, charsAfter);
|
|
1730
|
+
numberOfResults += matches.length;
|
|
1731
|
+
allSearchResults[data.path.text].push(...matches);
|
|
1732
|
+
break;
|
|
1733
|
+
case Summary:
|
|
1734
|
+
stats = data;
|
|
1735
|
+
break;
|
|
1736
|
+
}
|
|
1737
|
+
};
|
|
1738
|
+
const handleData = chunk => {
|
|
1739
|
+
let newLineIndex = getNewLineIndex(chunk);
|
|
1740
|
+
const dataString = buffer + chunk;
|
|
1741
|
+
if (newLineIndex === -1) {
|
|
1742
|
+
buffer = dataString;
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
newLineIndex += buffer.length;
|
|
1746
|
+
let previousIndex = 0;
|
|
1747
|
+
while (newLineIndex >= 0) {
|
|
1748
|
+
const line = dataString.slice(previousIndex, newLineIndex);
|
|
1749
|
+
handleLine(line);
|
|
1750
|
+
previousIndex = newLineIndex + 1;
|
|
1751
|
+
newLineIndex = getNewLineIndex(dataString, previousIndex);
|
|
1752
|
+
}
|
|
1753
|
+
buffer = dataString.slice(previousIndex);
|
|
1754
|
+
if (numberOfResults > maxSearchResults) {
|
|
1755
|
+
limitHit = true;
|
|
1756
|
+
kill();
|
|
1757
|
+
}
|
|
1758
|
+
};
|
|
1759
|
+
await processData(stdout, handleData);
|
|
1760
|
+
return {
|
|
1761
|
+
stats,
|
|
1762
|
+
limitHit
|
|
1763
|
+
};
|
|
1764
|
+
};
|
|
1765
|
+
|
|
1766
|
+
// TODO maybe split text search and file search into two processes,
|
|
1767
|
+
// so that long file text search cannot block file search and
|
|
1768
|
+
// text and file search would be more independent
|
|
1769
|
+
// only the logic for ripgrep path and websocket / messageport handling
|
|
1770
|
+
// would need to be the same
|
|
1771
|
+
const textSearchIncremental = async ({
|
|
1772
|
+
searchDir = '',
|
|
1773
|
+
maxSearchResults = 20_000,
|
|
1774
|
+
ripGrepArgs = [],
|
|
1775
|
+
id
|
|
1776
|
+
}) => {
|
|
1777
|
+
// TODO options
|
|
1778
|
+
const charsBefore = 26;
|
|
1779
|
+
const charsAfter = 50;
|
|
1780
|
+
const childProcess = spawn(ripGrepArgs, {
|
|
1781
|
+
cwd: searchDir
|
|
1782
|
+
});
|
|
1783
|
+
const pipeLinePromise = collectStdoutIncremental(id, childProcess.stdout, childProcess.kill, maxSearchResults, charsBefore, charsAfter);
|
|
1642
1784
|
const closePromise = waitForProcessToExit(childProcess);
|
|
1643
1785
|
const [pipeLineResult, exitResult] = await Promise.all([pipeLinePromise, closePromise]);
|
|
1644
1786
|
if (exitResult.type === Error$1) {
|
|
@@ -1652,9 +1794,11 @@ const search = async ({
|
|
|
1652
1794
|
|
|
1653
1795
|
const commandMap = {
|
|
1654
1796
|
'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort,
|
|
1655
|
-
'SearchFile.searchFile': searchFile,
|
|
1656
1797
|
'HandleWebSocket.handleWebSocket': handleWebSocket,
|
|
1657
|
-
'
|
|
1798
|
+
'SearchFile.searchFile': searchFile,
|
|
1799
|
+
'TextSearch.search': search,
|
|
1800
|
+
'TextSearch.searchIncremental': textSearchIncremental,
|
|
1801
|
+
'TextSearch.getIncrementalResults': getIncrementalResults
|
|
1658
1802
|
};
|
|
1659
1803
|
|
|
1660
1804
|
const NodeWorker = 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lvce-editor/search-process",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.2.0",
|
|
4
4
|
"description": "Search Process",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Lvce Editor",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"search-process": "bin/searchProcess.js"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"execa": "^9.
|
|
21
|
+
"execa": "^9.6.0",
|
|
22
22
|
"ws": "^8.18.2"
|
|
23
23
|
},
|
|
24
24
|
"optionalDependencies": {
|