@lvce-editor/search-process 12.0.0 → 12.1.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.
Files changed (2) hide show
  1. package/dist/index.js +243 -64
  2. package/package.json +2 -1
package/dist/index.js CHANGED
@@ -9,20 +9,40 @@ import { Writable } from 'node:stream';
9
9
  import { pipeline } from 'node:stream/promises';
10
10
  import { Buffer as Buffer$1 } from 'node:buffer';
11
11
 
12
- const searches = Object.create(null);
13
- const get$1 = id => {
14
- return searches[id];
12
+ const searches$1 = Object.create(null);
13
+ const get$3 = id => {
14
+ return searches$1[id];
15
15
  };
16
- const set$1 = (id, search) => {
17
- searches[id] = search;
16
+ const set$3 = (id, search) => {
17
+ searches$1[id] = search;
18
18
  };
19
19
 
20
+ // @deprecated use TextSearch.getPullResults instead
20
21
  const getIncrementalResults = (id, minLineY, maxLineY) => {
21
- const item = get$1(id);
22
+ const item = get$3(id);
22
23
  const visibleItems = item.getItems(minLineY, maxLineY);
23
24
  return visibleItems;
24
25
  };
25
26
 
27
+ const searches = Object.create(null);
28
+ const get$2 = id => {
29
+ return searches[id];
30
+ };
31
+ const set$2 = (id, search) => {
32
+ searches[id] = search;
33
+ };
34
+
35
+ const getPullProgress = id => {
36
+ const item = get$2(id);
37
+ return item.getProgress();
38
+ };
39
+
40
+ const getPullResults = id => {
41
+ const item = get$2(id);
42
+ const newItems = item.getNewItems();
43
+ return newItems;
44
+ };
45
+
26
46
  const normalizeLine = line => {
27
47
  if (line.startsWith('Error: ')) {
28
48
  return line.slice('Error: '.length);
@@ -779,10 +799,10 @@ const create$4 = (method, params) => {
779
799
  };
780
800
  };
781
801
  const callbacks = Object.create(null);
782
- const set = (id, fn) => {
802
+ const set$1 = (id, fn) => {
783
803
  callbacks[id] = fn;
784
804
  };
785
- const get = id => {
805
+ const get$1 = id => {
786
806
  return callbacks[id];
787
807
  };
788
808
  const remove = id => {
@@ -798,7 +818,7 @@ const registerPromise = () => {
798
818
  resolve,
799
819
  promise
800
820
  } = Promise.withResolvers();
801
- set(id, resolve);
821
+ set$1(id, resolve);
802
822
  return {
803
823
  id,
804
824
  promise
@@ -963,7 +983,7 @@ const warn = (...args) => {
963
983
  console.warn(...args);
964
984
  };
965
985
  const resolve = (id, response) => {
966
- const fn = get(id);
986
+ const fn = get$1(id);
967
987
  if (!fn) {
968
988
  console.log(response);
969
989
  warn(`callback ${id} may already be disposed`);
@@ -1420,12 +1440,12 @@ const exec$1 = async (command, args, options) => {
1420
1440
  array(args);
1421
1441
  object(options);
1422
1442
  const {
1423
- stdout,
1424
- stderr
1443
+ stderr,
1444
+ stdout
1425
1445
  } = await execa(command, args, options);
1426
1446
  return {
1427
- stdout,
1428
- stderr
1447
+ stderr,
1448
+ stdout
1429
1449
  };
1430
1450
  };
1431
1451
 
@@ -1433,12 +1453,12 @@ const execRipGrep = async (args, options) => {
1433
1453
  array(args);
1434
1454
  object(options);
1435
1455
  const {
1436
- stdout,
1437
- stderr
1456
+ stderr,
1457
+ stdout
1438
1458
  } = await exec$1(ripGrepPath, args, options);
1439
1459
  return {
1440
- stdout,
1441
- stderr
1460
+ stderr,
1461
+ stdout
1442
1462
  };
1443
1463
  };
1444
1464
 
@@ -1449,20 +1469,20 @@ const spawn = (args, options) => {
1449
1469
  throw new Error('stdout missing');
1450
1470
  }
1451
1471
  return {
1452
- on(event, listener) {
1453
- childProcess.on(event, listener);
1472
+ kill() {
1473
+ childProcess.kill();
1454
1474
  },
1455
1475
  off(event, listener) {
1456
1476
  childProcess.off(event, listener);
1457
1477
  },
1478
+ on(event, listener) {
1479
+ childProcess.on(event, listener);
1480
+ },
1458
1481
  once(event, listener) {
1459
1482
  childProcess.once(event, listener);
1460
1483
  },
1461
- stdout: childProcess.stdout,
1462
1484
  stderr: childProcess.stderr,
1463
- kill() {
1464
- childProcess.kill();
1465
- }
1485
+ stdout: childProcess.stdout
1466
1486
  };
1467
1487
  } catch (error) {
1468
1488
  throw new VError(error, `Failed to spawn ripgrep`);
@@ -1477,9 +1497,9 @@ const exec = execRipGrep;
1477
1497
  // but the second time only a few hundred bytes of changes
1478
1498
 
1479
1499
  const searchFile = async ({
1480
- searchPath = '',
1481
1500
  limit = 100,
1482
- ripGrepArgs = []
1501
+ ripGrepArgs = [],
1502
+ searchPath = ''
1483
1503
  }) => {
1484
1504
  try {
1485
1505
  string(searchPath);
@@ -1518,10 +1538,10 @@ const formatFile = text => {
1518
1538
 
1519
1539
  const processData = async (stdout, handleData) => {
1520
1540
  await pipeline(stdout, new Writable({
1521
- decodeStrings: false,
1522
1541
  construct(callback) {
1523
1542
  callback();
1524
1543
  },
1544
+ decodeStrings: false,
1525
1545
  write(chunk, encoding, callback) {
1526
1546
  try {
1527
1547
  handleData(chunk);
@@ -1565,11 +1585,11 @@ const toTextSearchResult = (parsedLine, remaining, charsBefore, charsAfter) => {
1565
1585
  const previewEnd = Math.min(submatch.end + charsAfter, linesLength);
1566
1586
  const previewText = lines.slice(actualStart, previewEnd);
1567
1587
  results.push({
1568
- type: Match,
1569
- start: submatch.start - actualStart,
1570
1588
  end: submatch.end - actualStart,
1571
1589
  lineNumber,
1572
- text: previewText
1590
+ start: submatch.start - actualStart,
1591
+ text: previewText,
1592
+ type: Match
1573
1593
  });
1574
1594
  }
1575
1595
  return results;
@@ -1596,17 +1616,17 @@ const collectStdout = async (stdout, kill, maxSearchResults, charsBefore, charsA
1596
1616
  const handleLine = line => {
1597
1617
  const parsedLine = JSON.parse(line);
1598
1618
  const {
1599
- type,
1600
- data
1619
+ data,
1620
+ type
1601
1621
  } = parsedLine;
1602
1622
  switch (type) {
1603
1623
  case Begin:
1604
1624
  allSearchResults[data.path.text] = [{
1605
- type: File,
1606
- start: 0,
1607
1625
  end: 0,
1608
1626
  lineNumber: 0,
1609
- text: formatFile(data.path.text)
1627
+ start: 0,
1628
+ text: formatFile(data.path.text),
1629
+ type: File
1610
1630
  }];
1611
1631
  break;
1612
1632
  case Match$1:
@@ -1644,9 +1664,9 @@ const collectStdout = async (stdout, kill, maxSearchResults, charsBefore, charsA
1644
1664
  await processData(stdout, handleData);
1645
1665
  const results = Object.values(allSearchResults).flat();
1646
1666
  return {
1667
+ limitHit,
1647
1668
  results,
1648
- stats,
1649
- limitHit
1669
+ stats
1650
1670
  };
1651
1671
  };
1652
1672
 
@@ -1671,8 +1691,8 @@ class TextSearchError extends Error {
1671
1691
 
1672
1692
  const getFirstEvent = (eventEmitter, eventMap) => {
1673
1693
  const {
1674
- resolve,
1675
- promise
1694
+ promise,
1695
+ resolve
1676
1696
  } = Promise.withResolvers();
1677
1697
  const listenerMap = Object.create(null);
1678
1698
  const cleanup = value => {
@@ -1684,8 +1704,8 @@ const getFirstEvent = (eventEmitter, eventMap) => {
1684
1704
  for (const [event, type] of Object.entries(eventMap)) {
1685
1705
  const listener = event => {
1686
1706
  cleanup({
1687
- type,
1688
- event
1707
+ event,
1708
+ type
1689
1709
  });
1690
1710
  };
1691
1711
  eventEmitter.on(event, listener);
@@ -1696,8 +1716,8 @@ const getFirstEvent = (eventEmitter, eventMap) => {
1696
1716
 
1697
1717
  const waitForProcessToExit = childProcess => {
1698
1718
  return getFirstEvent(childProcess, {
1699
- error: Error$1,
1700
- close: Exit
1719
+ close: Exit,
1720
+ error: Error$1
1701
1721
  });
1702
1722
  };
1703
1723
 
@@ -1713,9 +1733,9 @@ const waitForProcessToExit = childProcess => {
1713
1733
  // TODO not always run nice, maybe configure nice via flag/options
1714
1734
 
1715
1735
  const search = async ({
1716
- searchDir = '',
1717
1736
  maxSearchResults = 20_000,
1718
- ripGrepArgs = []
1737
+ ripGrepArgs = [],
1738
+ searchDir = ''
1719
1739
  } = {}) => {
1720
1740
  const charsBefore = 26;
1721
1741
  const charsAfter = 50; // TODO pass these also in as parameters
@@ -1745,30 +1765,30 @@ const collectStdoutIncremental = async (id, stdout, kill, maxSearchResults, char
1745
1765
  dispose() {
1746
1766
  kill();
1747
1767
  },
1748
- getResultCount() {
1749
- return numberOfResults;
1750
- },
1751
1768
  getItems(minLineY, maxLineY) {
1752
1769
  const results = Object.values(allSearchResults).flat();
1753
1770
  return results.slice(minLineY, maxLineY);
1771
+ },
1772
+ getResultCount() {
1773
+ return numberOfResults;
1754
1774
  }
1755
1775
  };
1756
- set$1(id, search);
1776
+ set$3(id, search);
1757
1777
  stdout.setEncoding(Utf8);
1758
1778
  const handleLine = line => {
1759
1779
  const parsedLine = JSON.parse(line);
1760
1780
  const {
1761
- type,
1762
- data
1781
+ data,
1782
+ type
1763
1783
  } = parsedLine;
1764
1784
  switch (type) {
1765
1785
  case Begin:
1766
1786
  allSearchResults[data.path.text] = [{
1767
- type: File,
1768
- start: 0,
1769
1787
  end: 0,
1770
1788
  lineNumber: 0,
1771
- text: data.path.text
1789
+ start: 0,
1790
+ text: data.path.text,
1791
+ type: File
1772
1792
  }];
1773
1793
  break;
1774
1794
  case Match$1:
@@ -1805,8 +1825,8 @@ const collectStdoutIncremental = async (id, stdout, kill, maxSearchResults, char
1805
1825
  };
1806
1826
  await processData(stdout, handleData);
1807
1827
  return {
1808
- stats,
1809
- limitHit
1828
+ limitHit,
1829
+ stats
1810
1830
  };
1811
1831
  };
1812
1832
 
@@ -1815,11 +1835,12 @@ const collectStdoutIncremental = async (id, stdout, kill, maxSearchResults, char
1815
1835
  // text and file search would be more independent
1816
1836
  // only the logic for ripgrep path and websocket / messageport handling
1817
1837
  // would need to be the same
1838
+ // @deprecated use TextSearch.searchPull instead
1818
1839
  const textSearchIncremental = async ({
1819
- searchDir = '',
1840
+ id,
1820
1841
  maxSearchResults = 20_000,
1821
1842
  ripGrepArgs = [],
1822
- id
1843
+ searchDir = ''
1823
1844
  }) => {
1824
1845
  // TODO options
1825
1846
  const charsBefore = 26;
@@ -1839,13 +1860,170 @@ const textSearchIncremental = async ({
1839
1860
  return pipeLineResult;
1840
1861
  };
1841
1862
 
1863
+ const collectStdoutPull = async (id, stdout, kill, maxSearchResults, charsBefore, charsAfter, notifyResultsFound) => {
1864
+ const allSearchResults = [];
1865
+ let buffer = '';
1866
+ let stats = {};
1867
+ let limitHit = false;
1868
+ let done = false;
1869
+ let nextResultIndex = 0;
1870
+ let hasUnfetchedResults = false;
1871
+ let numberOfResults = 0;
1872
+ const maybeNotifyResultsFound = () => {
1873
+ if (hasUnfetchedResults) {
1874
+ return;
1875
+ }
1876
+ hasUnfetchedResults = true;
1877
+ notifyResultsFound();
1878
+ };
1879
+ const search = {
1880
+ dispose() {
1881
+ kill();
1882
+ },
1883
+ getNewItems() {
1884
+ const newItems = allSearchResults.slice(nextResultIndex);
1885
+ nextResultIndex = allSearchResults.length;
1886
+ hasUnfetchedResults = false;
1887
+ return newItems;
1888
+ },
1889
+ getProgress() {
1890
+ return {
1891
+ done,
1892
+ limitHit,
1893
+ resultCount: numberOfResults,
1894
+ stats
1895
+ };
1896
+ }
1897
+ };
1898
+ set$2(id, search);
1899
+ stdout.setEncoding(Utf8);
1900
+ const handleLine = line => {
1901
+ const parsedLine = JSON.parse(line);
1902
+ const {
1903
+ data,
1904
+ type
1905
+ } = parsedLine;
1906
+ switch (type) {
1907
+ case Begin:
1908
+ allSearchResults.push({
1909
+ end: 0,
1910
+ lineNumber: 0,
1911
+ start: 0,
1912
+ text: data.path.text,
1913
+ type: File
1914
+ });
1915
+ maybeNotifyResultsFound();
1916
+ break;
1917
+ case Match$1:
1918
+ const remaining = maxSearchResults - numberOfResults;
1919
+ const matches = toTextSearchResult(parsedLine, remaining, charsBefore, charsAfter);
1920
+ numberOfResults += matches.length;
1921
+ if (matches.length > 0) {
1922
+ allSearchResults.push(...matches);
1923
+ maybeNotifyResultsFound();
1924
+ }
1925
+ break;
1926
+ case Summary:
1927
+ stats = data;
1928
+ break;
1929
+ }
1930
+ };
1931
+ const handleData = chunk => {
1932
+ let newLineIndex = getNewLineIndex(chunk);
1933
+ const dataString = buffer + chunk;
1934
+ if (newLineIndex === -1) {
1935
+ buffer = dataString;
1936
+ return;
1937
+ }
1938
+ newLineIndex += buffer.length;
1939
+ let previousIndex = 0;
1940
+ while (newLineIndex >= 0) {
1941
+ const line = dataString.slice(previousIndex, newLineIndex);
1942
+ handleLine(line);
1943
+ previousIndex = newLineIndex + 1;
1944
+ newLineIndex = getNewLineIndex(dataString, previousIndex);
1945
+ }
1946
+ buffer = dataString.slice(previousIndex);
1947
+ if (numberOfResults > maxSearchResults) {
1948
+ limitHit = true;
1949
+ kill();
1950
+ }
1951
+ };
1952
+ await processData(stdout, handleData);
1953
+ done = true;
1954
+ return {
1955
+ limitHit,
1956
+ stats
1957
+ };
1958
+ };
1959
+
1960
+ let rpc = undefined;
1961
+ const rpcMap = new Map();
1962
+ const get = () => {
1963
+ return rpc;
1964
+ };
1965
+ const set = value => {
1966
+ rpc = value;
1967
+ };
1968
+ const getById = id => {
1969
+ return rpcMap.get(id);
1970
+ };
1971
+ const setById = (id, value) => {
1972
+ rpcMap.set(id, value);
1973
+ };
1974
+ const removeById = id => {
1975
+ rpcMap.delete(id);
1976
+ };
1977
+
1978
+ const textSearchPull = async ({
1979
+ id,
1980
+ maxSearchResults = 20_000,
1981
+ resultsFoundMethod,
1982
+ ripGrepArgs = [],
1983
+ searchDir = ''
1984
+ }) => {
1985
+ const charsBefore = 26;
1986
+ const charsAfter = 50;
1987
+ const initialRpc = get();
1988
+ if (initialRpc) {
1989
+ setById(id, initialRpc);
1990
+ }
1991
+ const childProcess = spawn(ripGrepArgs, {
1992
+ cwd: searchDir
1993
+ });
1994
+ const notifyResultsFound = () => {
1995
+ const rpc = getById(id);
1996
+ if (!rpc) {
1997
+ return;
1998
+ }
1999
+ rpc.send(resultsFoundMethod, id);
2000
+ };
2001
+ try {
2002
+ const pipeLinePromise = collectStdoutPull(id, childProcess.stdout, childProcess.kill, maxSearchResults, charsBefore, charsAfter, notifyResultsFound);
2003
+ const closePromise = waitForProcessToExit(childProcess);
2004
+ const [pipeLineResult, exitResult] = await Promise.all([pipeLinePromise, closePromise]);
2005
+ if (exitResult.type === Error$1) {
2006
+ if (isEnoentError(exitResult.event)) {
2007
+ throw new RipGrepNotFoundError(exitResult.event);
2008
+ }
2009
+ throw new TextSearchError(exitResult.event);
2010
+ }
2011
+ return pipeLineResult;
2012
+ } finally {
2013
+ removeById(id);
2014
+ }
2015
+ };
2016
+
1842
2017
  const commandMap = {
1843
2018
  'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort,
1844
2019
  'HandleWebSocket.handleWebSocket': handleWebSocket,
1845
2020
  'SearchFile.searchFile': searchFile,
2021
+ 'TextSearch.getIncrementalResults': getIncrementalResults,
2022
+ 'TextSearch.getPullProgress': getPullProgress,
2023
+ 'TextSearch.getPullResults': getPullResults,
1846
2024
  'TextSearch.search': search,
1847
2025
  'TextSearch.searchIncremental': textSearchIncremental,
1848
- 'TextSearch.getIncrementalResults': getIncrementalResults
2026
+ 'TextSearch.searchPull': textSearchPull
1849
2027
  };
1850
2028
 
1851
2029
  const NodeWorker = 1;
@@ -1868,14 +2046,14 @@ const Auto = argv => {
1868
2046
 
1869
2047
  const getModule = method => {
1870
2048
  switch (method) {
2049
+ case ElectronMessagePort:
2050
+ return ElectronMessagePortRpcClient.create;
2051
+ case ElectronUtilityProcess:
2052
+ return ElectronUtilityProcessRpcClient.create;
1871
2053
  case NodeForkedProcess:
1872
2054
  return NodeForkedProcessRpcClient.create;
1873
2055
  case NodeWorker:
1874
2056
  return NodeWorkerRpcClient.create;
1875
- case ElectronUtilityProcess:
1876
- return ElectronUtilityProcessRpcClient.create;
1877
- case ElectronMessagePort:
1878
- return ElectronMessagePortRpcClient.create;
1879
2057
  case WebSocket:
1880
2058
  return NodeWebSocketRpcClient.create;
1881
2059
  default:
@@ -1886,9 +2064,10 @@ const getModule = method => {
1886
2064
  const listen = async argv => {
1887
2065
  const method = Auto(argv);
1888
2066
  const create = getModule(method);
1889
- await create({
2067
+ const rpc = await create({
1890
2068
  commandMap: commandMap
1891
2069
  });
2070
+ set(rpc);
1892
2071
  };
1893
2072
 
1894
2073
  const main = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/search-process",
3
- "version": "12.0.0",
3
+ "version": "12.1.0",
4
4
  "description": "Search Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",
@@ -18,6 +18,7 @@
18
18
  "search-process": "bin/searchProcess.js"
19
19
  },
20
20
  "dependencies": {
21
+ "@lvce-editor/constants": "^3.7.0",
21
22
  "execa": "^9.6.0",
22
23
  "ws": "^8.18.3"
23
24
  },