@lvce-editor/file-search-worker 1.0.0 → 1.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/fileSearchWorkerMain.js +236 -3
- package/package.json +1 -1
|
@@ -18,12 +18,26 @@ const execute = (command, ...args) => {
|
|
|
18
18
|
return fn(...args);
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
const searchFile = async (workspace, root) => {
|
|
21
|
+
const searchFile$1 = async (workspace, root) => {
|
|
22
22
|
return [];
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
const
|
|
26
|
-
'
|
|
25
|
+
const getFileSearchRipGrepArgs = () => {
|
|
26
|
+
const ripGrepArgs = ['--files', '--sort-files'];
|
|
27
|
+
return ripGrepArgs;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const state$2 = {
|
|
31
|
+
/**
|
|
32
|
+
* @type {any}
|
|
33
|
+
*/
|
|
34
|
+
ipc: undefined
|
|
35
|
+
};
|
|
36
|
+
const get$1 = () => {
|
|
37
|
+
return state$2.ipc;
|
|
38
|
+
};
|
|
39
|
+
const set$1 = ipc => {
|
|
40
|
+
state$2.ipc = ipc;
|
|
27
41
|
};
|
|
28
42
|
|
|
29
43
|
const Two = '2.0';
|
|
@@ -64,15 +78,49 @@ const number = value => {
|
|
|
64
78
|
const state$1 = {
|
|
65
79
|
callbacks: Object.create(null)
|
|
66
80
|
};
|
|
81
|
+
const set = (id, fn) => {
|
|
82
|
+
state$1.callbacks[id] = fn;
|
|
83
|
+
};
|
|
67
84
|
const get = id => {
|
|
68
85
|
return state$1.callbacks[id];
|
|
69
86
|
};
|
|
70
87
|
const remove = id => {
|
|
71
88
|
delete state$1.callbacks[id];
|
|
72
89
|
};
|
|
90
|
+
const state = {
|
|
91
|
+
id: 0
|
|
92
|
+
};
|
|
93
|
+
const create$3 = () => {
|
|
94
|
+
return ++state.id;
|
|
95
|
+
};
|
|
73
96
|
const warn = (...args) => {
|
|
74
97
|
console.warn(...args);
|
|
75
98
|
};
|
|
99
|
+
const withResolvers$1 = () => {
|
|
100
|
+
/**
|
|
101
|
+
* @type {any}
|
|
102
|
+
*/
|
|
103
|
+
let _resolve;
|
|
104
|
+
const promise = new Promise(resolve => {
|
|
105
|
+
_resolve = resolve;
|
|
106
|
+
});
|
|
107
|
+
return {
|
|
108
|
+
resolve: _resolve,
|
|
109
|
+
promise
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
const registerPromise = () => {
|
|
113
|
+
const id = create$3();
|
|
114
|
+
const {
|
|
115
|
+
resolve,
|
|
116
|
+
promise
|
|
117
|
+
} = withResolvers$1();
|
|
118
|
+
set(id, resolve);
|
|
119
|
+
return {
|
|
120
|
+
id,
|
|
121
|
+
promise
|
|
122
|
+
};
|
|
123
|
+
};
|
|
76
124
|
const resolve = (id, args) => {
|
|
77
125
|
number(id);
|
|
78
126
|
const fn = get(id);
|
|
@@ -84,14 +132,153 @@ const resolve = (id, args) => {
|
|
|
84
132
|
fn(args);
|
|
85
133
|
remove(id);
|
|
86
134
|
};
|
|
135
|
+
const create$2 = (method, params) => {
|
|
136
|
+
const {
|
|
137
|
+
id,
|
|
138
|
+
promise
|
|
139
|
+
} = registerPromise();
|
|
140
|
+
const message = {
|
|
141
|
+
jsonrpc: Two,
|
|
142
|
+
method,
|
|
143
|
+
params,
|
|
144
|
+
id
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
message,
|
|
148
|
+
promise
|
|
149
|
+
};
|
|
150
|
+
};
|
|
87
151
|
class JsonRpcError extends Error {
|
|
88
152
|
constructor(message) {
|
|
89
153
|
super(message);
|
|
90
154
|
this.name = 'JsonRpcError';
|
|
91
155
|
}
|
|
92
156
|
}
|
|
157
|
+
const NewLine$2 = '\n';
|
|
158
|
+
const DomException = 'DOMException';
|
|
159
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
160
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
161
|
+
const TypeError$1 = 'TypeError';
|
|
162
|
+
const getErrorConstructor = (message, type) => {
|
|
163
|
+
if (type) {
|
|
164
|
+
switch (type) {
|
|
165
|
+
case DomException:
|
|
166
|
+
return DOMException;
|
|
167
|
+
case TypeError$1:
|
|
168
|
+
return TypeError;
|
|
169
|
+
case SyntaxError$1:
|
|
170
|
+
return SyntaxError;
|
|
171
|
+
case ReferenceError$1:
|
|
172
|
+
return ReferenceError;
|
|
173
|
+
default:
|
|
174
|
+
return Error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (message.startsWith('TypeError: ')) {
|
|
178
|
+
return TypeError;
|
|
179
|
+
}
|
|
180
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
181
|
+
return SyntaxError;
|
|
182
|
+
}
|
|
183
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
184
|
+
return ReferenceError;
|
|
185
|
+
}
|
|
186
|
+
return Error;
|
|
187
|
+
};
|
|
188
|
+
const constructError = (message, type, name) => {
|
|
189
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
190
|
+
if (ErrorConstructor === DOMException && name) {
|
|
191
|
+
return new ErrorConstructor(message, name);
|
|
192
|
+
}
|
|
193
|
+
if (ErrorConstructor === Error) {
|
|
194
|
+
const error = new Error(message);
|
|
195
|
+
if (name && name !== 'VError') {
|
|
196
|
+
error.name = name;
|
|
197
|
+
}
|
|
198
|
+
return error;
|
|
199
|
+
}
|
|
200
|
+
return new ErrorConstructor(message);
|
|
201
|
+
};
|
|
202
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
203
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
204
|
+
};
|
|
205
|
+
const getParentStack = error => {
|
|
206
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
207
|
+
if (parentStack.startsWith(' at')) {
|
|
208
|
+
parentStack = error.message + NewLine$2 + parentStack;
|
|
209
|
+
}
|
|
210
|
+
return parentStack;
|
|
211
|
+
};
|
|
212
|
+
const joinLines$1 = lines => {
|
|
213
|
+
return lines.join(NewLine$2);
|
|
214
|
+
};
|
|
93
215
|
const MethodNotFound = -32601;
|
|
94
216
|
const Custom = -32001;
|
|
217
|
+
const splitLines$2 = lines => {
|
|
218
|
+
return lines.split(NewLine$2);
|
|
219
|
+
};
|
|
220
|
+
const restoreJsonRpcError = error => {
|
|
221
|
+
if (error && error instanceof Error) {
|
|
222
|
+
return error;
|
|
223
|
+
}
|
|
224
|
+
const currentStack = joinLines$1(splitLines$2(new Error().stack || '').slice(1));
|
|
225
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
226
|
+
const restoredError = new JsonRpcError(error.message);
|
|
227
|
+
const parentStack = getParentStack(error);
|
|
228
|
+
restoredError.stack = parentStack + NewLine$2 + currentStack;
|
|
229
|
+
return restoredError;
|
|
230
|
+
}
|
|
231
|
+
if (error && error.message) {
|
|
232
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
233
|
+
if (error.data) {
|
|
234
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
235
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine$2 + error.data.stack + NewLine$2 + currentStack;
|
|
236
|
+
} else if (error.data.stack) {
|
|
237
|
+
restoredError.stack = error.data.stack;
|
|
238
|
+
}
|
|
239
|
+
if (error.data.codeFrame) {
|
|
240
|
+
// @ts-ignore
|
|
241
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
242
|
+
}
|
|
243
|
+
if (error.data.code) {
|
|
244
|
+
// @ts-ignore
|
|
245
|
+
restoredError.code = error.data.code;
|
|
246
|
+
}
|
|
247
|
+
if (error.data.type) {
|
|
248
|
+
// @ts-ignore
|
|
249
|
+
restoredError.name = error.data.type;
|
|
250
|
+
}
|
|
251
|
+
} else {
|
|
252
|
+
if (error.stack) {
|
|
253
|
+
const lowerStack = restoredError.stack || '';
|
|
254
|
+
// @ts-ignore
|
|
255
|
+
const indexNewLine = getNewLineIndex$1(lowerStack);
|
|
256
|
+
const parentStack = getParentStack(error);
|
|
257
|
+
// @ts-ignore
|
|
258
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
259
|
+
}
|
|
260
|
+
if (error.codeFrame) {
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
restoredError.codeFrame = error.codeFrame;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return restoredError;
|
|
266
|
+
}
|
|
267
|
+
if (typeof error === 'string') {
|
|
268
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
269
|
+
}
|
|
270
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
271
|
+
};
|
|
272
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
273
|
+
if ('error' in responseMessage) {
|
|
274
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
275
|
+
throw restoredError;
|
|
276
|
+
}
|
|
277
|
+
if ('result' in responseMessage) {
|
|
278
|
+
return responseMessage.result;
|
|
279
|
+
}
|
|
280
|
+
throw new JsonRpcError('unexpected response message');
|
|
281
|
+
};
|
|
95
282
|
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
96
283
|
const getType = prettyError => {
|
|
97
284
|
if (prettyError && prettyError.type) {
|
|
@@ -210,6 +397,51 @@ const handleJsonRpcMessage = async (...args) => {
|
|
|
210
397
|
}
|
|
211
398
|
throw new JsonRpcError('unexpected message');
|
|
212
399
|
};
|
|
400
|
+
const invoke$2 = async (ipc, method, ...params) => {
|
|
401
|
+
const {
|
|
402
|
+
message,
|
|
403
|
+
promise
|
|
404
|
+
} = create$2(method, params);
|
|
405
|
+
ipc.send(message);
|
|
406
|
+
const responseMessage = await promise;
|
|
407
|
+
const result = unwrapJsonRpcResult(responseMessage);
|
|
408
|
+
return result;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const invoke$1 = (method, ...params) => {
|
|
412
|
+
const ipc = get$1();
|
|
413
|
+
return invoke$2(ipc, method, ...params);
|
|
414
|
+
};
|
|
415
|
+
const listen$2 = ipc => {
|
|
416
|
+
set$1(ipc);
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
const invoke = (method, ...params) => {
|
|
420
|
+
return invoke$1('SearchProcess.invoke', method, ...params);
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
const splitLines$1 = lines => {
|
|
424
|
+
return lines.split('\n');
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
// TODO create direct connection from electron to file search worker using message ports
|
|
428
|
+
|
|
429
|
+
const searchFile = async (path, value) => {
|
|
430
|
+
const ripGrepArgs = getFileSearchRipGrepArgs();
|
|
431
|
+
const options = {
|
|
432
|
+
ripGrepArgs,
|
|
433
|
+
searchPath: path,
|
|
434
|
+
limit: 9999999
|
|
435
|
+
};
|
|
436
|
+
const stdout = await invoke('SearchFile.searchFile', options);
|
|
437
|
+
const lines = splitLines$1(stdout);
|
|
438
|
+
return lines;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
const commandMap = {
|
|
442
|
+
'SearchFile.searchFile': searchFile$1,
|
|
443
|
+
'SearchFile.searchFileWithRipGrep': searchFile
|
|
444
|
+
};
|
|
213
445
|
|
|
214
446
|
const requiresSocket = () => {
|
|
215
447
|
return false;
|
|
@@ -662,6 +894,7 @@ const listen = async () => {
|
|
|
662
894
|
method: Auto()
|
|
663
895
|
});
|
|
664
896
|
handleIpc(ipc);
|
|
897
|
+
listen$2(ipc);
|
|
665
898
|
};
|
|
666
899
|
|
|
667
900
|
const main = async () => {
|