@lvce-editor/search-process 9.1.0 → 9.3.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 +231 -68
  2. package/package.json +3 -3
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);
@@ -70,49 +84,57 @@ class AssertionError extends Error {
70
84
  this.name = 'AssertionError';
71
85
  }
72
86
  }
87
+ const Object$1 = 1;
88
+ const Number = 2;
89
+ const Array$1 = 3;
90
+ const String = 4;
91
+ const Boolean = 5;
92
+ const Function = 6;
93
+ const Null = 7;
94
+ const Unknown = 8;
73
95
  const getType = value => {
74
96
  switch (typeof value) {
75
97
  case 'number':
76
- return 'number';
98
+ return Number;
77
99
  case 'function':
78
- return 'function';
100
+ return Function;
79
101
  case 'string':
80
- return 'string';
102
+ return String;
81
103
  case 'object':
82
104
  if (value === null) {
83
- return 'null';
105
+ return Null;
84
106
  }
85
107
  if (Array.isArray(value)) {
86
- return 'array';
108
+ return Array$1;
87
109
  }
88
- return 'object';
110
+ return Object$1;
89
111
  case 'boolean':
90
- return 'boolean';
112
+ return Boolean;
91
113
  default:
92
- return 'unknown';
114
+ return Unknown;
93
115
  }
94
116
  };
95
117
  const object = value => {
96
118
  const type = getType(value);
97
- if (type !== 'object') {
119
+ if (type !== Object$1) {
98
120
  throw new AssertionError('expected value to be of type object');
99
121
  }
100
122
  };
101
123
  const number = value => {
102
124
  const type = getType(value);
103
- if (type !== 'number') {
125
+ if (type !== Number) {
104
126
  throw new AssertionError('expected value to be of type number');
105
127
  }
106
128
  };
107
129
  const array = value => {
108
130
  const type = getType(value);
109
- if (type !== 'array') {
131
+ if (type !== Array$1) {
110
132
  throw new AssertionError('expected value to be of type array');
111
133
  }
112
134
  };
113
135
  const string = value => {
114
136
  const type = getType(value);
115
- if (type !== 'string') {
137
+ if (type !== String) {
116
138
  throw new AssertionError('expected value to be of type string');
117
139
  }
118
140
  };
@@ -539,7 +561,7 @@ const listen$3 = async () => {
539
561
  parentPort
540
562
  } = await import('node:worker_threads');
541
563
  if (!parentPort) {
542
- throw new IpcError('parentPort is required');
564
+ throw new IpcError('parentPort is required for node worker threads ipc');
543
565
  }
544
566
  return parentPort;
545
567
  };
@@ -674,8 +696,6 @@ const handleUpgrade$1 = async (...args) => {
674
696
  // @ts-ignore
675
697
  return module.handleUpgrade(...args);
676
698
  };
677
-
678
- // @ts-ignore
679
699
  const listen$1$1 = async ({
680
700
  request,
681
701
  handle
@@ -696,8 +716,6 @@ const listen$1$1 = async ({
696
716
  const signal$4 = webSocket => {
697
717
  webSocket.resume();
698
718
  };
699
-
700
- // @ts-ignore
701
719
  const wrap$9 = webSocket => {
702
720
  return {
703
721
  webSocket,
@@ -752,7 +770,7 @@ const IpcChildWithWebSocket = {
752
770
  };
753
771
 
754
772
  const Two = '2.0';
755
- const create$4$1 = (method, params) => {
773
+ const create$4 = (method, params) => {
756
774
  return {
757
775
  jsonrpc: Two,
758
776
  method,
@@ -852,6 +870,16 @@ const constructError = (message, type, name) => {
852
870
  }
853
871
  return new ErrorConstructor(message);
854
872
  };
873
+ const joinLines = lines => {
874
+ return lines.join(NewLine$1);
875
+ };
876
+ const splitLines = lines => {
877
+ return lines.split(NewLine$1);
878
+ };
879
+ const getCurrentStack = () => {
880
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(2));
881
+ return currentStack;
882
+ };
855
883
  const getNewLineIndex$1 = (string, startIndex = undefined) => {
856
884
  return string.indexOf(NewLine$1, startIndex);
857
885
  };
@@ -862,19 +890,16 @@ const getParentStack = error => {
862
890
  }
863
891
  return parentStack;
864
892
  };
865
- const joinLines = lines => {
866
- return lines.join(NewLine$1);
867
- };
868
893
  const MethodNotFound = -32601;
869
894
  const Custom = -32001;
870
- const splitLines = lines => {
871
- return lines.split(NewLine$1);
872
- };
873
895
  const restoreJsonRpcError = error => {
896
+ const currentStack = getCurrentStack();
874
897
  if (error && error instanceof Error) {
898
+ if (typeof error.stack === 'string') {
899
+ error.stack = error.stack + NewLine$1 + currentStack;
900
+ }
875
901
  return error;
876
902
  }
877
- const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
878
903
  if (error && error.code && error.code === MethodNotFound) {
879
904
  const restoredError = new JsonRpcError(error.message);
880
905
  const parentStack = getParentStack(error);
@@ -955,6 +980,17 @@ const getErrorType = prettyError => {
955
980
  }
956
981
  return undefined;
957
982
  };
983
+ const isAlreadyStack = line => {
984
+ return line.trim().startsWith('at ');
985
+ };
986
+ const getStack = prettyError => {
987
+ const stackString = prettyError.stack || '';
988
+ const newLineIndex = stackString.indexOf('\n');
989
+ if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
990
+ return stackString.slice(newLineIndex + 1);
991
+ }
992
+ return stackString;
993
+ };
958
994
  const getErrorProperty = (error, prettyError) => {
959
995
  if (error && error.code === E_COMMAND_NOT_FOUND) {
960
996
  return {
@@ -967,7 +1003,7 @@ const getErrorProperty = (error, prettyError) => {
967
1003
  code: Custom,
968
1004
  message: prettyError.message,
969
1005
  data: {
970
- stack: prettyError.stack,
1006
+ stack: getStack(prettyError),
971
1007
  codeFrame: prettyError.codeFrame,
972
1008
  type: getErrorType(prettyError),
973
1009
  code: prettyError.code,
@@ -975,18 +1011,18 @@ const getErrorProperty = (error, prettyError) => {
975
1011
  }
976
1012
  };
977
1013
  };
978
- const create$1 = (message, error) => {
1014
+ const create$1 = (id, error) => {
979
1015
  return {
980
1016
  jsonrpc: Two,
981
- id: message.id,
1017
+ id,
982
1018
  error
983
1019
  };
984
1020
  };
985
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
1021
+ const getErrorResponse = (id, error, preparePrettyError, logError) => {
986
1022
  const prettyError = preparePrettyError(error);
987
1023
  logError(error, prettyError);
988
1024
  const errorProperty = getErrorProperty(error, prettyError);
989
- return create$1(message, errorProperty);
1025
+ return create$1(id, errorProperty);
990
1026
  };
991
1027
  const create = (message, result) => {
992
1028
  return {
@@ -999,12 +1035,27 @@ const getSuccessResponse = (message, result) => {
999
1035
  const resultProperty = result ?? null;
1000
1036
  return create(message, resultProperty);
1001
1037
  };
1038
+ const getErrorResponseSimple = (id, error) => {
1039
+ return {
1040
+ jsonrpc: Two,
1041
+ id,
1042
+ error: {
1043
+ code: Custom,
1044
+ // @ts-ignore
1045
+ message: error.message,
1046
+ data: error
1047
+ }
1048
+ };
1049
+ };
1002
1050
  const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
1003
1051
  try {
1004
1052
  const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
1005
1053
  return getSuccessResponse(message, result);
1006
1054
  } catch (error) {
1007
- return getErrorResponse(message, error, preparePrettyError, logError);
1055
+ if (ipc.canUseSimpleErrorResponse) {
1056
+ return getErrorResponseSimple(message.id, error);
1057
+ }
1058
+ return getErrorResponse(message.id, error, preparePrettyError, logError);
1008
1059
  }
1009
1060
  };
1010
1061
  const defaultPreparePrettyError = error => {
@@ -1059,7 +1110,7 @@ const handleJsonRpcMessage = async (...args) => {
1059
1110
  try {
1060
1111
  ipc.send(response);
1061
1112
  } catch (error) {
1062
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
1113
+ const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
1063
1114
  ipc.send(errorResponse);
1064
1115
  }
1065
1116
  return;
@@ -1087,7 +1138,7 @@ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
1087
1138
  return unwrapJsonRpcResult(responseMessage);
1088
1139
  };
1089
1140
  const send = (transport, method, ...params) => {
1090
- const message = create$4$1(method, params);
1141
+ const message = create$4(method, params);
1091
1142
  transport.send(message);
1092
1143
  };
1093
1144
  const invoke = (ipc, method, ...params) => {
@@ -1164,7 +1215,7 @@ const listen$1 = async (module, options) => {
1164
1215
  const ipc = module.wrap(rawIpc);
1165
1216
  return ipc;
1166
1217
  };
1167
- const create$e = async ({
1218
+ const create$j = async ({
1168
1219
  commandMap,
1169
1220
  messagePort,
1170
1221
  requiresSocket
@@ -1183,9 +1234,9 @@ const create$e = async ({
1183
1234
  };
1184
1235
  const ElectronMessagePortRpcClient = {
1185
1236
  __proto__: null,
1186
- create: create$e
1237
+ create: create$j
1187
1238
  };
1188
- const create$d = async ({
1239
+ const create$i = async ({
1189
1240
  commandMap
1190
1241
  }) => {
1191
1242
  // TODO create a commandMap per rpc instance
@@ -1197,9 +1248,9 @@ const create$d = async ({
1197
1248
  };
1198
1249
  const ElectronUtilityProcessRpcClient = {
1199
1250
  __proto__: null,
1200
- create: create$d
1251
+ create: create$i
1201
1252
  };
1202
- const create$7 = async ({
1253
+ const create$9 = async ({
1203
1254
  commandMap
1204
1255
  }) => {
1205
1256
  // TODO create a commandMap per rpc instance
@@ -1211,9 +1262,9 @@ const create$7 = async ({
1211
1262
  };
1212
1263
  const NodeForkedProcessRpcClient = {
1213
1264
  __proto__: null,
1214
- create: create$7
1265
+ create: create$9
1215
1266
  };
1216
- const create$5 = async ({
1267
+ const create$7 = async ({
1217
1268
  commandMap,
1218
1269
  request,
1219
1270
  handle,
@@ -1234,9 +1285,9 @@ const create$5 = async ({
1234
1285
  };
1235
1286
  const NodeWebSocketRpcClient = {
1236
1287
  __proto__: null,
1237
- create: create$5
1288
+ create: create$7
1238
1289
  };
1239
- const create$4 = async ({
1290
+ const create$6 = async ({
1240
1291
  commandMap
1241
1292
  }) => {
1242
1293
  // TODO create a commandMap per rpc instance
@@ -1248,7 +1299,7 @@ const create$4 = async ({
1248
1299
  };
1249
1300
  const NodeWorkerRpcClient = {
1250
1301
  __proto__: null,
1251
- create: create$4
1302
+ create: create$6
1252
1303
  };
1253
1304
 
1254
1305
  const handleElectronMessagePort = async messagePort => {
@@ -1379,21 +1430,23 @@ const execRipGrep = async (args, options) => {
1379
1430
  const spawn = (args, options) => {
1380
1431
  try {
1381
1432
  const childProcess = NodeChildProcess.spawn(ripGrepPath, args, options);
1433
+ if (!childProcess.stdout || !childProcess.stderr) {
1434
+ throw new Error('stdout missing');
1435
+ }
1382
1436
  return {
1383
- childProcess,
1384
1437
  on(event, listener) {
1385
- this.childProcess.on(event, listener);
1438
+ childProcess.on(event, listener);
1386
1439
  },
1387
1440
  off(event, listener) {
1388
- this.childProcess.off(event, listener);
1441
+ childProcess.off(event, listener);
1389
1442
  },
1390
1443
  once(event, listener) {
1391
- this.childProcess.once(event, listener);
1444
+ childProcess.once(event, listener);
1392
1445
  },
1393
1446
  stdout: childProcess.stdout,
1394
1447
  stderr: childProcess.stderr,
1395
1448
  kill() {
1396
- this.childProcess.kill();
1449
+ childProcess.kill();
1397
1450
  }
1398
1451
  };
1399
1452
  } catch (error) {
@@ -1440,6 +1493,23 @@ const searchFile = async ({
1440
1493
  const Utf8 = 'utf8';
1441
1494
  const Base64 = 'base64';
1442
1495
 
1496
+ const processData = async (stdout, handleData) => {
1497
+ await pipeline(stdout, new Writable({
1498
+ decodeStrings: false,
1499
+ construct(callback) {
1500
+ callback();
1501
+ },
1502
+ write(chunk, encoding, callback) {
1503
+ try {
1504
+ handleData(chunk);
1505
+ callback();
1506
+ } catch (error) {
1507
+ callback(error);
1508
+ }
1509
+ }
1510
+ }));
1511
+ };
1512
+
1443
1513
  const Begin = 'begin';
1444
1514
  const Match$1 = 'match';
1445
1515
  const Summary = 'summary';
@@ -1493,13 +1563,13 @@ const toTextSearchResult = (parsedLine, remaining, charsBefore, charsAfter) => {
1493
1563
  // TODO update client
1494
1564
  // TODO not always run nice, maybe configure nice via flag/options
1495
1565
 
1496
- const collectStdout = async (childProcess, maxSearchResults, charsBefore, charsAfter) => {
1566
+ const collectStdout = async (stdout, kill, maxSearchResults, charsBefore, charsAfter) => {
1497
1567
  const allSearchResults = Object.create(null);
1498
1568
  let buffer = '';
1499
1569
  let stats = {};
1500
1570
  let limitHit = false;
1501
1571
  let numberOfResults = 0;
1502
- childProcess.stdout.setEncoding(Utf8);
1572
+ stdout.setEncoding(Utf8);
1503
1573
  const handleLine = line => {
1504
1574
  const parsedLine = JSON.parse(line);
1505
1575
  const {
@@ -1545,23 +1615,10 @@ const collectStdout = async (childProcess, maxSearchResults, charsBefore, charsA
1545
1615
  buffer = dataString.slice(previousIndex);
1546
1616
  if (numberOfResults > maxSearchResults) {
1547
1617
  limitHit = true;
1548
- childProcess.kill();
1618
+ kill();
1549
1619
  }
1550
1620
  };
1551
- await pipeline(childProcess.stdout, new Writable({
1552
- decodeStrings: false,
1553
- construct(callback) {
1554
- callback();
1555
- },
1556
- write(chunk, encoding, callback) {
1557
- try {
1558
- handleData(chunk);
1559
- callback();
1560
- } catch (error) {
1561
- callback(error);
1562
- }
1563
- }
1564
- }));
1621
+ await processData(stdout, handleData);
1565
1622
  const results = Object.values(allSearchResults).flat();
1566
1623
  return {
1567
1624
  results,
@@ -1642,7 +1699,111 @@ const search = async ({
1642
1699
  const childProcess = spawn(ripGrepArgs, {
1643
1700
  cwd: searchDir
1644
1701
  });
1645
- const pipeLinePromise = collectStdout(childProcess, maxSearchResults, charsBefore, charsAfter);
1702
+ const pipeLinePromise = collectStdout(childProcess.stdout, childProcess.kill, maxSearchResults, charsBefore, charsAfter);
1703
+ const closePromise = waitForProcessToExit(childProcess);
1704
+ const [pipeLineResult, exitResult] = await Promise.all([pipeLinePromise, closePromise]);
1705
+ if (exitResult.type === Error$1) {
1706
+ if (isEnoentError(exitResult.event)) {
1707
+ throw new RipGrepNotFoundError(exitResult.event);
1708
+ }
1709
+ throw new TextSearchError(exitResult.event);
1710
+ }
1711
+ return pipeLineResult;
1712
+ };
1713
+
1714
+ const collectStdoutIncremental = async (id, stdout, kill, maxSearchResults, charsBefore, charsAfter) => {
1715
+ const allSearchResults = Object.create(null);
1716
+ let buffer = '';
1717
+ let stats = {};
1718
+ let limitHit = false;
1719
+ let numberOfResults = 0;
1720
+ const search = {
1721
+ dispose() {
1722
+ kill();
1723
+ },
1724
+ getResultCount() {
1725
+ return numberOfResults;
1726
+ },
1727
+ getItems(minLineY, maxLineY) {
1728
+ const results = Object.values(allSearchResults).flat();
1729
+ return results.slice(minLineY, maxLineY);
1730
+ }
1731
+ };
1732
+ set$1(id, search);
1733
+ stdout.setEncoding(Utf8);
1734
+ const handleLine = line => {
1735
+ const parsedLine = JSON.parse(line);
1736
+ const {
1737
+ type,
1738
+ data
1739
+ } = parsedLine;
1740
+ switch (type) {
1741
+ case Begin:
1742
+ allSearchResults[data.path.text] = [{
1743
+ type: File,
1744
+ start: 0,
1745
+ end: 0,
1746
+ lineNumber: 0,
1747
+ text: data.path.text
1748
+ }];
1749
+ break;
1750
+ case Match$1:
1751
+ const remaining = maxSearchResults - numberOfResults;
1752
+ const matches = toTextSearchResult(parsedLine, remaining, charsBefore, charsAfter);
1753
+ numberOfResults += matches.length;
1754
+ allSearchResults[data.path.text].push(...matches);
1755
+ break;
1756
+ case Summary:
1757
+ stats = data;
1758
+ break;
1759
+ }
1760
+ };
1761
+ const handleData = chunk => {
1762
+ let newLineIndex = getNewLineIndex(chunk);
1763
+ const dataString = buffer + chunk;
1764
+ if (newLineIndex === -1) {
1765
+ buffer = dataString;
1766
+ return;
1767
+ }
1768
+ newLineIndex += buffer.length;
1769
+ let previousIndex = 0;
1770
+ while (newLineIndex >= 0) {
1771
+ const line = dataString.slice(previousIndex, newLineIndex);
1772
+ handleLine(line);
1773
+ previousIndex = newLineIndex + 1;
1774
+ newLineIndex = getNewLineIndex(dataString, previousIndex);
1775
+ }
1776
+ buffer = dataString.slice(previousIndex);
1777
+ if (numberOfResults > maxSearchResults) {
1778
+ limitHit = true;
1779
+ kill();
1780
+ }
1781
+ };
1782
+ await processData(stdout, handleData);
1783
+ return {
1784
+ stats,
1785
+ limitHit
1786
+ };
1787
+ };
1788
+
1789
+ // TODO maybe split text search and file search into two processes,
1790
+ // so that long file text search cannot block file search and
1791
+ // text and file search would be more independent
1792
+ // only the logic for ripgrep path and websocket / messageport handling
1793
+ // would need to be the same
1794
+ const textSearchIncremental = async ({
1795
+ searchDir = '',
1796
+ maxSearchResults = 20_000,
1797
+ ripGrepArgs = [],
1798
+ id
1799
+ }) => {
1800
+ // TODO options
1801
+ const charsBefore = 26;
1802
+ const charsAfter = 50;
1803
+ const childProcess = spawn(ripGrepArgs, {
1804
+ cwd: searchDir
1805
+ });
1806
+ const pipeLinePromise = collectStdoutIncremental(id, childProcess.stdout, childProcess.kill, maxSearchResults, charsBefore, charsAfter);
1646
1807
  const closePromise = waitForProcessToExit(childProcess);
1647
1808
  const [pipeLineResult, exitResult] = await Promise.all([pipeLinePromise, closePromise]);
1648
1809
  if (exitResult.type === Error$1) {
@@ -1656,9 +1817,11 @@ const search = async ({
1656
1817
 
1657
1818
  const commandMap = {
1658
1819
  'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort,
1659
- 'SearchFile.searchFile': searchFile,
1660
1820
  'HandleWebSocket.handleWebSocket': handleWebSocket,
1661
- 'TextSearch.search': search
1821
+ 'SearchFile.searchFile': searchFile,
1822
+ 'TextSearch.search': search,
1823
+ 'TextSearch.searchIncremental': textSearchIncremental,
1824
+ 'TextSearch.getIncrementalResults': getIncrementalResults
1662
1825
  };
1663
1826
 
1664
1827
  const NodeWorker = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/search-process",
3
- "version": "9.1.0",
3
+ "version": "9.3.0",
4
4
  "description": "Search Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",
@@ -18,8 +18,8 @@
18
18
  "search-process": "bin/searchProcess.js"
19
19
  },
20
20
  "dependencies": {
21
- "execa": "^9.5.3",
22
- "ws": "^8.18.2"
21
+ "execa": "^9.6.0",
22
+ "ws": "^8.18.3"
23
23
  },
24
24
  "optionalDependencies": {
25
25
  "@lvce-editor/ripgrep": "^2.1.0"