@lvce-editor/text-search-worker 1.0.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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/textSearchWorkerMain.js +1338 -0
- package/package.json +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lvce Editor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,1338 @@
|
|
|
1
|
+
const commands = Object.create(null);
|
|
2
|
+
const registerCommand = (key, fn) => {
|
|
3
|
+
commands[key] = fn;
|
|
4
|
+
};
|
|
5
|
+
const register = commandMap => {
|
|
6
|
+
for (const [key, value] of Object.entries(commandMap)) {
|
|
7
|
+
registerCommand(key, value);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
const getCommand = key => {
|
|
11
|
+
return commands[key];
|
|
12
|
+
};
|
|
13
|
+
const execute = (command, ...args) => {
|
|
14
|
+
const fn = getCommand(command);
|
|
15
|
+
if (!fn) {
|
|
16
|
+
throw new Error(`command not found ${command}`);
|
|
17
|
+
}
|
|
18
|
+
return fn(...args);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
let AssertionError$1 = class AssertionError extends Error {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = 'AssertionError';
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const getType$2 = value => {
|
|
28
|
+
switch (typeof value) {
|
|
29
|
+
case 'number':
|
|
30
|
+
return 'number';
|
|
31
|
+
case 'function':
|
|
32
|
+
return 'function';
|
|
33
|
+
case 'string':
|
|
34
|
+
return 'string';
|
|
35
|
+
case 'object':
|
|
36
|
+
if (value === null) {
|
|
37
|
+
return 'null';
|
|
38
|
+
}
|
|
39
|
+
if (Array.isArray(value)) {
|
|
40
|
+
return 'array';
|
|
41
|
+
}
|
|
42
|
+
return 'object';
|
|
43
|
+
case 'boolean':
|
|
44
|
+
return 'boolean';
|
|
45
|
+
default:
|
|
46
|
+
return 'unknown';
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const string = value => {
|
|
50
|
+
const type = getType$2(value);
|
|
51
|
+
if (type !== 'string') {
|
|
52
|
+
throw new AssertionError$1('expected value to be of type string');
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const RE_PROTOCOL = /^([a-z\-]+):\/\//;
|
|
57
|
+
const getProtocol = uri => {
|
|
58
|
+
const protocolMatch = uri.match(RE_PROTOCOL);
|
|
59
|
+
if (protocolMatch) {
|
|
60
|
+
return protocolMatch[1];
|
|
61
|
+
}
|
|
62
|
+
return '';
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const textSearch$5 = async (scheme, root, query) => {
|
|
66
|
+
string(scheme);
|
|
67
|
+
string(query);
|
|
68
|
+
throw new Error('not implemented');
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const assetDir = '';
|
|
72
|
+
|
|
73
|
+
const splitLines$1 = lines => {
|
|
74
|
+
return lines.split('\n');
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const File$1 = 1;
|
|
78
|
+
const Match = 2;
|
|
79
|
+
|
|
80
|
+
const textSearchInFile$1 = (file, content, query) => {
|
|
81
|
+
const results = [];
|
|
82
|
+
const lines = splitLines$1(content);
|
|
83
|
+
for (let i = 0; i < lines.length; i++) {
|
|
84
|
+
const line = lines[i];
|
|
85
|
+
const index = line.indexOf(query);
|
|
86
|
+
if (index !== -1) {
|
|
87
|
+
results.push({
|
|
88
|
+
type: Match,
|
|
89
|
+
text: line,
|
|
90
|
+
start: index,
|
|
91
|
+
end: index + query.length,
|
|
92
|
+
lineNumber: i
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (results.length > 0) {
|
|
97
|
+
results.unshift({
|
|
98
|
+
type: File$1,
|
|
99
|
+
text: file,
|
|
100
|
+
start: 0,
|
|
101
|
+
end: 0,
|
|
102
|
+
lineNumber: 0
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return results;
|
|
106
|
+
};
|
|
107
|
+
const textSearch$4 = async (scheme, root, query, options) => {
|
|
108
|
+
string(scheme);
|
|
109
|
+
string(root);
|
|
110
|
+
string(query);
|
|
111
|
+
const fetchUri = `${assetDir}/config/fileMap.json`;
|
|
112
|
+
const fileList = await execute('Ajax.getJson', fetchUri);
|
|
113
|
+
const allResults = [];
|
|
114
|
+
const relativeRoot = root.slice('fetch://'.length);
|
|
115
|
+
for (const uri of fileList) {
|
|
116
|
+
const fetchUri = `${assetDir}${uri}`;
|
|
117
|
+
const content = await execute('Ajax.getText', fetchUri);
|
|
118
|
+
const relativeUri = uri.slice(relativeRoot.length + 1);
|
|
119
|
+
const results = textSearchInFile$1(relativeUri, content, query);
|
|
120
|
+
allResults.push(...results);
|
|
121
|
+
}
|
|
122
|
+
return allResults;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// based on https://github.com/microsoft/vscode/blob/c0769274fa136b45799edeccc0d0a2f645b75caf/src/vs/base/common/arrays.ts#L625 (License MIT)
|
|
126
|
+
|
|
127
|
+
const insertInto = (array, start, newItems) => {
|
|
128
|
+
const originalLength = array.length;
|
|
129
|
+
const newItemsLength = newItems.length;
|
|
130
|
+
array.length = originalLength + newItemsLength;
|
|
131
|
+
// Move the items after the start index, start from the end so that we don't overwrite any value.
|
|
132
|
+
for (let i = originalLength - 1; i >= start; i--) {
|
|
133
|
+
array[i + newItemsLength] = array[i];
|
|
134
|
+
}
|
|
135
|
+
for (let i = 0; i < newItemsLength; i++) {
|
|
136
|
+
array[i + start] = newItems[i];
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const push = (array, newItems) => {
|
|
140
|
+
insertInto(array, array.length, newItems);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const NotReadableError = 'NotReadableError';
|
|
144
|
+
|
|
145
|
+
const isNotReadableError = error => {
|
|
146
|
+
return error && error.name === NotReadableError;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const Directory = 'directory';
|
|
150
|
+
const File = 'file';
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
*
|
|
154
|
+
* @param {FileSystemFileHandle} handle
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
const getFile = handle => {
|
|
158
|
+
return handle.getFile();
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const dirname = (pathSeparator, path) => {
|
|
162
|
+
const index = path.lastIndexOf(pathSeparator);
|
|
163
|
+
if (index === -1) {
|
|
164
|
+
return path;
|
|
165
|
+
}
|
|
166
|
+
return path.slice(0, index);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const instanceOfAny = (object, constructors) => constructors.some(c => object instanceof c);
|
|
170
|
+
let idbProxyableTypes;
|
|
171
|
+
let cursorAdvanceMethods;
|
|
172
|
+
// This is a function to prevent it throwing up in node environments.
|
|
173
|
+
function getIdbProxyableTypes() {
|
|
174
|
+
return idbProxyableTypes || (idbProxyableTypes = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction]);
|
|
175
|
+
}
|
|
176
|
+
// This is a function to prevent it throwing up in node environments.
|
|
177
|
+
function getCursorAdvanceMethods() {
|
|
178
|
+
return cursorAdvanceMethods || (cursorAdvanceMethods = [IDBCursor.prototype.advance, IDBCursor.prototype.continue, IDBCursor.prototype.continuePrimaryKey]);
|
|
179
|
+
}
|
|
180
|
+
const transactionDoneMap = new WeakMap();
|
|
181
|
+
const transformCache = new WeakMap();
|
|
182
|
+
const reverseTransformCache = new WeakMap();
|
|
183
|
+
function promisifyRequest(request) {
|
|
184
|
+
const promise = new Promise((resolve, reject) => {
|
|
185
|
+
const unlisten = () => {
|
|
186
|
+
request.removeEventListener('success', success);
|
|
187
|
+
request.removeEventListener('error', error);
|
|
188
|
+
};
|
|
189
|
+
const success = () => {
|
|
190
|
+
resolve(wrap(request.result));
|
|
191
|
+
unlisten();
|
|
192
|
+
};
|
|
193
|
+
const error = () => {
|
|
194
|
+
reject(request.error);
|
|
195
|
+
unlisten();
|
|
196
|
+
};
|
|
197
|
+
request.addEventListener('success', success);
|
|
198
|
+
request.addEventListener('error', error);
|
|
199
|
+
});
|
|
200
|
+
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
|
|
201
|
+
// is because we create many promises from a single IDBRequest.
|
|
202
|
+
reverseTransformCache.set(promise, request);
|
|
203
|
+
return promise;
|
|
204
|
+
}
|
|
205
|
+
function cacheDonePromiseForTransaction(tx) {
|
|
206
|
+
// Early bail if we've already created a done promise for this transaction.
|
|
207
|
+
if (transactionDoneMap.has(tx)) return;
|
|
208
|
+
const done = new Promise((resolve, reject) => {
|
|
209
|
+
const unlisten = () => {
|
|
210
|
+
tx.removeEventListener('complete', complete);
|
|
211
|
+
tx.removeEventListener('error', error);
|
|
212
|
+
tx.removeEventListener('abort', error);
|
|
213
|
+
};
|
|
214
|
+
const complete = () => {
|
|
215
|
+
resolve();
|
|
216
|
+
unlisten();
|
|
217
|
+
};
|
|
218
|
+
const error = () => {
|
|
219
|
+
reject(tx.error || new DOMException('AbortError', 'AbortError'));
|
|
220
|
+
unlisten();
|
|
221
|
+
};
|
|
222
|
+
tx.addEventListener('complete', complete);
|
|
223
|
+
tx.addEventListener('error', error);
|
|
224
|
+
tx.addEventListener('abort', error);
|
|
225
|
+
});
|
|
226
|
+
// Cache it for later retrieval.
|
|
227
|
+
transactionDoneMap.set(tx, done);
|
|
228
|
+
}
|
|
229
|
+
let idbProxyTraps = {
|
|
230
|
+
get(target, prop, receiver) {
|
|
231
|
+
if (target instanceof IDBTransaction) {
|
|
232
|
+
// Special handling for transaction.done.
|
|
233
|
+
if (prop === 'done') return transactionDoneMap.get(target);
|
|
234
|
+
// Make tx.store return the only store in the transaction, or undefined if there are many.
|
|
235
|
+
if (prop === 'store') {
|
|
236
|
+
return receiver.objectStoreNames[1] ? undefined : receiver.objectStore(receiver.objectStoreNames[0]);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// Else transform whatever we get back.
|
|
240
|
+
return wrap(target[prop]);
|
|
241
|
+
},
|
|
242
|
+
set(target, prop, value) {
|
|
243
|
+
target[prop] = value;
|
|
244
|
+
return true;
|
|
245
|
+
},
|
|
246
|
+
has(target, prop) {
|
|
247
|
+
if (target instanceof IDBTransaction && (prop === 'done' || prop === 'store')) {
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
return prop in target;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
function replaceTraps(callback) {
|
|
254
|
+
idbProxyTraps = callback(idbProxyTraps);
|
|
255
|
+
}
|
|
256
|
+
function wrapFunction(func) {
|
|
257
|
+
// Due to expected object equality (which is enforced by the caching in `wrap`), we
|
|
258
|
+
// only create one new func per func.
|
|
259
|
+
// Cursor methods are special, as the behaviour is a little more different to standard IDB. In
|
|
260
|
+
// IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
|
|
261
|
+
// cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
|
|
262
|
+
// with real promises, so each advance methods returns a new promise for the cursor object, or
|
|
263
|
+
// undefined if the end of the cursor has been reached.
|
|
264
|
+
if (getCursorAdvanceMethods().includes(func)) {
|
|
265
|
+
return function (...args) {
|
|
266
|
+
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
|
|
267
|
+
// the original object.
|
|
268
|
+
func.apply(unwrap(this), args);
|
|
269
|
+
return wrap(this.request);
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
return function (...args) {
|
|
273
|
+
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
|
|
274
|
+
// the original object.
|
|
275
|
+
return wrap(func.apply(unwrap(this), args));
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function transformCachableValue(value) {
|
|
279
|
+
if (typeof value === 'function') return wrapFunction(value);
|
|
280
|
+
// This doesn't return, it just creates a 'done' promise for the transaction,
|
|
281
|
+
// which is later returned for transaction.done (see idbObjectHandler).
|
|
282
|
+
if (value instanceof IDBTransaction) cacheDonePromiseForTransaction(value);
|
|
283
|
+
if (instanceOfAny(value, getIdbProxyableTypes())) return new Proxy(value, idbProxyTraps);
|
|
284
|
+
// Return the same value back if we're not going to transform it.
|
|
285
|
+
return value;
|
|
286
|
+
}
|
|
287
|
+
function wrap(value) {
|
|
288
|
+
// We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
|
|
289
|
+
// IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
|
|
290
|
+
if (value instanceof IDBRequest) return promisifyRequest(value);
|
|
291
|
+
// If we've already transformed this value before, reuse the transformed value.
|
|
292
|
+
// This is faster, but it also provides object equality.
|
|
293
|
+
if (transformCache.has(value)) return transformCache.get(value);
|
|
294
|
+
const newValue = transformCachableValue(value);
|
|
295
|
+
// Not all types are transformed.
|
|
296
|
+
// These may be primitive types, so they can't be WeakMap keys.
|
|
297
|
+
if (newValue !== value) {
|
|
298
|
+
transformCache.set(value, newValue);
|
|
299
|
+
reverseTransformCache.set(newValue, value);
|
|
300
|
+
}
|
|
301
|
+
return newValue;
|
|
302
|
+
}
|
|
303
|
+
const unwrap = value => reverseTransformCache.get(value);
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Open a database.
|
|
307
|
+
*
|
|
308
|
+
* @param name Name of the database.
|
|
309
|
+
* @param version Schema version.
|
|
310
|
+
* @param callbacks Additional callbacks.
|
|
311
|
+
*/
|
|
312
|
+
function openDB(name, version, {
|
|
313
|
+
blocked,
|
|
314
|
+
upgrade,
|
|
315
|
+
blocking,
|
|
316
|
+
terminated
|
|
317
|
+
} = {}) {
|
|
318
|
+
const request = indexedDB.open(name, version);
|
|
319
|
+
const openPromise = wrap(request);
|
|
320
|
+
if (upgrade) {
|
|
321
|
+
request.addEventListener('upgradeneeded', event => {
|
|
322
|
+
upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
if (blocked) {
|
|
326
|
+
request.addEventListener('blocked', event => blocked(
|
|
327
|
+
// Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
|
|
328
|
+
event.oldVersion, event.newVersion, event));
|
|
329
|
+
}
|
|
330
|
+
openPromise.then(db => {
|
|
331
|
+
if (terminated) db.addEventListener('close', () => terminated());
|
|
332
|
+
if (blocking) {
|
|
333
|
+
db.addEventListener('versionchange', event => blocking(event.oldVersion, event.newVersion, event));
|
|
334
|
+
}
|
|
335
|
+
}).catch(() => {});
|
|
336
|
+
return openPromise;
|
|
337
|
+
}
|
|
338
|
+
const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];
|
|
339
|
+
const writeMethods = ['put', 'add', 'delete', 'clear'];
|
|
340
|
+
const cachedMethods = new Map();
|
|
341
|
+
function getMethod(target, prop) {
|
|
342
|
+
if (!(target instanceof IDBDatabase && !(prop in target) && typeof prop === 'string')) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (cachedMethods.get(prop)) return cachedMethods.get(prop);
|
|
346
|
+
const targetFuncName = prop.replace(/FromIndex$/, '');
|
|
347
|
+
const useIndex = prop !== targetFuncName;
|
|
348
|
+
const isWrite = writeMethods.includes(targetFuncName);
|
|
349
|
+
if (
|
|
350
|
+
// Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
|
|
351
|
+
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) || !(isWrite || readMethods.includes(targetFuncName))) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const method = async function (storeName, ...args) {
|
|
355
|
+
// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
|
|
356
|
+
const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
|
|
357
|
+
let target = tx.store;
|
|
358
|
+
if (useIndex) target = target.index(args.shift());
|
|
359
|
+
// Must reject if op rejects.
|
|
360
|
+
// If it's a write operation, must reject if tx.done rejects.
|
|
361
|
+
// Must reject with op rejection first.
|
|
362
|
+
// Must resolve with op value.
|
|
363
|
+
// Must handle both promises (no unhandled rejections)
|
|
364
|
+
return (await Promise.all([target[targetFuncName](...args), isWrite && tx.done]))[0];
|
|
365
|
+
};
|
|
366
|
+
cachedMethods.set(prop, method);
|
|
367
|
+
return method;
|
|
368
|
+
}
|
|
369
|
+
replaceTraps(oldTraps => ({
|
|
370
|
+
...oldTraps,
|
|
371
|
+
get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
|
|
372
|
+
has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop)
|
|
373
|
+
}));
|
|
374
|
+
const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
|
|
375
|
+
const methodMap = {};
|
|
376
|
+
const advanceResults = new WeakMap();
|
|
377
|
+
const ittrProxiedCursorToOriginalProxy = new WeakMap();
|
|
378
|
+
const cursorIteratorTraps = {
|
|
379
|
+
get(target, prop) {
|
|
380
|
+
if (!advanceMethodProps.includes(prop)) return target[prop];
|
|
381
|
+
let cachedFunc = methodMap[prop];
|
|
382
|
+
if (!cachedFunc) {
|
|
383
|
+
cachedFunc = methodMap[prop] = function (...args) {
|
|
384
|
+
advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
return cachedFunc;
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
async function* iterate(...args) {
|
|
391
|
+
// tslint:disable-next-line:no-this-assignment
|
|
392
|
+
let cursor = this;
|
|
393
|
+
if (!(cursor instanceof IDBCursor)) {
|
|
394
|
+
cursor = await cursor.openCursor(...args);
|
|
395
|
+
}
|
|
396
|
+
if (!cursor) return;
|
|
397
|
+
cursor = cursor;
|
|
398
|
+
const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
|
|
399
|
+
ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
|
|
400
|
+
// Map this double-proxy back to the original, so other cursor methods work.
|
|
401
|
+
reverseTransformCache.set(proxiedCursor, unwrap(cursor));
|
|
402
|
+
while (cursor) {
|
|
403
|
+
yield proxiedCursor;
|
|
404
|
+
// If one of the advancing methods was not called, call continue().
|
|
405
|
+
cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
|
|
406
|
+
advanceResults.delete(proxiedCursor);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function isIteratorProp(target, prop) {
|
|
410
|
+
return prop === Symbol.asyncIterator && instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor]) || prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore]);
|
|
411
|
+
}
|
|
412
|
+
replaceTraps(oldTraps => ({
|
|
413
|
+
...oldTraps,
|
|
414
|
+
get(target, prop, receiver) {
|
|
415
|
+
if (isIteratorProp(target, prop)) return iterate;
|
|
416
|
+
return oldTraps.get(target, prop, receiver);
|
|
417
|
+
},
|
|
418
|
+
has(target, prop) {
|
|
419
|
+
return isIteratorProp(target, prop) || oldTraps.has(target, prop);
|
|
420
|
+
}
|
|
421
|
+
}));
|
|
422
|
+
|
|
423
|
+
const normalizeLine$1 = line => {
|
|
424
|
+
if (line.startsWith('Error: ')) {
|
|
425
|
+
return line.slice(`Error: `.length);
|
|
426
|
+
}
|
|
427
|
+
if (line.startsWith('VError: ')) {
|
|
428
|
+
return line.slice(`VError: `.length);
|
|
429
|
+
}
|
|
430
|
+
return line;
|
|
431
|
+
};
|
|
432
|
+
const getCombinedMessage$1 = (error, message) => {
|
|
433
|
+
const stringifiedError = normalizeLine$1(`${error}`);
|
|
434
|
+
if (message) {
|
|
435
|
+
return `${message}: ${stringifiedError}`;
|
|
436
|
+
}
|
|
437
|
+
return stringifiedError;
|
|
438
|
+
};
|
|
439
|
+
const NewLine$2 = '\n';
|
|
440
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
441
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
442
|
+
};
|
|
443
|
+
const mergeStacks$1 = (parent, child) => {
|
|
444
|
+
if (!child) {
|
|
445
|
+
return parent;
|
|
446
|
+
}
|
|
447
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
|
448
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
|
449
|
+
if (childNewLineIndex === -1) {
|
|
450
|
+
return parent;
|
|
451
|
+
}
|
|
452
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
453
|
+
const childRest = child.slice(childNewLineIndex);
|
|
454
|
+
const childFirstLine = normalizeLine$1(child.slice(0, childNewLineIndex));
|
|
455
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
456
|
+
return parentFirstLine + childRest;
|
|
457
|
+
}
|
|
458
|
+
return child;
|
|
459
|
+
};
|
|
460
|
+
let VError$1 = class VError extends Error {
|
|
461
|
+
constructor(error, message) {
|
|
462
|
+
const combinedMessage = getCombinedMessage$1(error, message);
|
|
463
|
+
super(combinedMessage);
|
|
464
|
+
this.name = 'VError';
|
|
465
|
+
if (error instanceof Error) {
|
|
466
|
+
this.stack = mergeStacks$1(this.stack, error.stack);
|
|
467
|
+
}
|
|
468
|
+
if (error.codeFrame) {
|
|
469
|
+
// @ts-ignore
|
|
470
|
+
this.codeFrame = error.codeFrame;
|
|
471
|
+
}
|
|
472
|
+
if (error.code) {
|
|
473
|
+
// @ts-ignore
|
|
474
|
+
this.code = error.code;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const state = {
|
|
480
|
+
databases: Object.create(null),
|
|
481
|
+
eventId: 0,
|
|
482
|
+
dbVersion: 1,
|
|
483
|
+
cachedDb: undefined
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
// TODO high memory usage in idb because of transactionDoneMap
|
|
487
|
+
|
|
488
|
+
const getHandleDb = async () => {
|
|
489
|
+
// @ts-ignore
|
|
490
|
+
const db = await openDB('handle', state.dbVersion, {
|
|
491
|
+
async upgrade(db, oldVersion) {
|
|
492
|
+
if (!db.objectStoreNames.contains('file-handles-store')) {
|
|
493
|
+
// @ts-ignore
|
|
494
|
+
await db.createObjectStore('file-handles-store', {});
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
return db;
|
|
499
|
+
};
|
|
500
|
+
const getHandle$1 = async uri => {
|
|
501
|
+
const handleDb = await getHandleDb();
|
|
502
|
+
const handle = await handleDb.get('file-handles-store', uri);
|
|
503
|
+
return handle;
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
const getHandle = async uri => {
|
|
507
|
+
try {
|
|
508
|
+
// TODO retrieve handle from state or from indexeddb
|
|
509
|
+
// TODO if not found, throw error
|
|
510
|
+
const handle = await getHandle$1(uri);
|
|
511
|
+
return handle;
|
|
512
|
+
} catch (error) {
|
|
513
|
+
throw new VError$1(error, 'Failed to get handle');
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
const getDirectoryHandle = async uri => {
|
|
518
|
+
const handle = await getHandle(uri);
|
|
519
|
+
if (handle) {
|
|
520
|
+
return handle;
|
|
521
|
+
}
|
|
522
|
+
const dirname$1 = dirname('/', uri);
|
|
523
|
+
if (uri === dirname$1) {
|
|
524
|
+
return undefined;
|
|
525
|
+
}
|
|
526
|
+
return getDirectoryHandle(dirname$1);
|
|
527
|
+
};
|
|
528
|
+
const textSearchInText = (file, content, query) => {
|
|
529
|
+
const results = [];
|
|
530
|
+
const lines = splitLines$1(content);
|
|
531
|
+
for (let i = 0; i < lines.length; i++) {
|
|
532
|
+
const line = lines[i];
|
|
533
|
+
const index = line.indexOf(query);
|
|
534
|
+
if (index !== -1) {
|
|
535
|
+
results.push({
|
|
536
|
+
type: Match,
|
|
537
|
+
text: line,
|
|
538
|
+
start: index,
|
|
539
|
+
end: index + query.length,
|
|
540
|
+
lineNumber: i
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (results.length > 0) {
|
|
545
|
+
results.unshift({
|
|
546
|
+
type: File$1,
|
|
547
|
+
text: file,
|
|
548
|
+
start: 0,
|
|
549
|
+
end: 0,
|
|
550
|
+
lineNumber: 0
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
return results;
|
|
554
|
+
};
|
|
555
|
+
const textSearchInFile = async (all, handle, absolutePath, query) => {
|
|
556
|
+
try {
|
|
557
|
+
const file = await getFile(handle);
|
|
558
|
+
const content = await file.text();
|
|
559
|
+
const results = textSearchInText(absolutePath, content, query);
|
|
560
|
+
push(all, results);
|
|
561
|
+
} catch (error) {
|
|
562
|
+
if (isNotReadableError(error)) {
|
|
563
|
+
// ignore
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
throw error;
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
const textSearchRecursively = async (all, parent, handle, query) => {
|
|
570
|
+
const childHandles = [];
|
|
571
|
+
// TODO
|
|
572
|
+
// await FileSystemHtml.getChildHandles(handle)
|
|
573
|
+
const promises = [];
|
|
574
|
+
for (const childHandle of childHandles) {
|
|
575
|
+
const absolutePath = parent + '/' + childHandle.name;
|
|
576
|
+
switch (childHandle.kind) {
|
|
577
|
+
case Directory:
|
|
578
|
+
promises.push(textSearchRecursively(all, absolutePath, childHandle, query));
|
|
579
|
+
break;
|
|
580
|
+
case File:
|
|
581
|
+
promises.push(textSearchInFile(all, childHandle, absolutePath, query));
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
await Promise.all(promises);
|
|
586
|
+
};
|
|
587
|
+
const textSearch$3 = async (scheme, root, query) => {
|
|
588
|
+
string(scheme);
|
|
589
|
+
string(root);
|
|
590
|
+
string(query);
|
|
591
|
+
const relativeRoot = root.slice('html://'.length);
|
|
592
|
+
const handle = await getDirectoryHandle(relativeRoot);
|
|
593
|
+
if (!handle) {
|
|
594
|
+
// @ts-ignore
|
|
595
|
+
throw new VError$1(`Folder not found ${relativeRoot}`);
|
|
596
|
+
}
|
|
597
|
+
const all = [];
|
|
598
|
+
await textSearchRecursively(all, '', handle, query);
|
|
599
|
+
return all;
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
const getRipGrepArgs = ({
|
|
603
|
+
threads,
|
|
604
|
+
isCaseSensitive,
|
|
605
|
+
searchString,
|
|
606
|
+
useRegularExpression
|
|
607
|
+
}) => {
|
|
608
|
+
const ripGrepArgs = ['--hidden', '--no-require-git', '--smart-case', '--stats', '--json'];
|
|
609
|
+
ripGrepArgs.push('--threads', `${threads}`);
|
|
610
|
+
if (isCaseSensitive) {
|
|
611
|
+
ripGrepArgs.push('--case-sensitive');
|
|
612
|
+
} else {
|
|
613
|
+
ripGrepArgs.push('--ignore-case');
|
|
614
|
+
}
|
|
615
|
+
if (useRegularExpression) {
|
|
616
|
+
ripGrepArgs.push('--regexp', searchString);
|
|
617
|
+
} else {
|
|
618
|
+
ripGrepArgs.push('--fixed-strings');
|
|
619
|
+
ripGrepArgs.push('--');
|
|
620
|
+
ripGrepArgs.push(searchString);
|
|
621
|
+
}
|
|
622
|
+
ripGrepArgs.push('.');
|
|
623
|
+
return ripGrepArgs;
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
// import * as SearchProcess from '../SearchProcess/SearchProcess.js'
|
|
627
|
+
// import * as SharedProcessCommandType from '../SharedProcessCommandType/SharedProcessCommandType.js'
|
|
628
|
+
|
|
629
|
+
const SearchProcess = {
|
|
630
|
+
// TODO connect to search process
|
|
631
|
+
invoke() {
|
|
632
|
+
return {
|
|
633
|
+
results: []
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
const textSearch$2 = async (scheme, root, query, options) => {
|
|
638
|
+
const ripGrepArgs = getRipGrepArgs({
|
|
639
|
+
...options,
|
|
640
|
+
searchString: query
|
|
641
|
+
});
|
|
642
|
+
const actualOptions = {
|
|
643
|
+
ripGrepArgs,
|
|
644
|
+
searchDir: root
|
|
645
|
+
};
|
|
646
|
+
// @ts-ignore
|
|
647
|
+
const result = await SearchProcess.invoke(SharedProcessCommandType.TextSearchSearch, actualOptions);
|
|
648
|
+
// TODO api is weird
|
|
649
|
+
return result.results;
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
const textSearch$1 = (scheme, root, query) => {
|
|
653
|
+
// TODO ask renderer worker for files
|
|
654
|
+
const entries = Object.entries({});
|
|
655
|
+
const results = [];
|
|
656
|
+
for (const [key, value] of entries) {
|
|
657
|
+
// @ts-ignore
|
|
658
|
+
if (value.includes(query)) {
|
|
659
|
+
results.push([key, [{
|
|
660
|
+
absoluteOffset: 0,
|
|
661
|
+
preview: value
|
|
662
|
+
}]]);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return results;
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
const getProvider = scheme => {
|
|
669
|
+
switch (scheme) {
|
|
670
|
+
case '':
|
|
671
|
+
return textSearch$2;
|
|
672
|
+
case 'web':
|
|
673
|
+
return textSearch$1;
|
|
674
|
+
case 'fetch':
|
|
675
|
+
return textSearch$4;
|
|
676
|
+
case 'html':
|
|
677
|
+
return textSearch$3;
|
|
678
|
+
default:
|
|
679
|
+
return textSearch$5;
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
const textSearch = async (root, query, options) => {
|
|
684
|
+
string(root);
|
|
685
|
+
string(query);
|
|
686
|
+
const scheme = getProtocol(root);
|
|
687
|
+
const provider = getProvider(scheme);
|
|
688
|
+
const results = await provider(scheme, root, query, options);
|
|
689
|
+
return results;
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
const commandMap = {
|
|
693
|
+
'TextSearch.textSearch': textSearch
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
const Two = '2.0';
|
|
697
|
+
class AssertionError extends Error {
|
|
698
|
+
constructor(message) {
|
|
699
|
+
super(message);
|
|
700
|
+
this.name = 'AssertionError';
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
const getType$1 = value => {
|
|
704
|
+
switch (typeof value) {
|
|
705
|
+
case 'number':
|
|
706
|
+
return 'number';
|
|
707
|
+
case 'function':
|
|
708
|
+
return 'function';
|
|
709
|
+
case 'string':
|
|
710
|
+
return 'string';
|
|
711
|
+
case 'object':
|
|
712
|
+
if (value === null) {
|
|
713
|
+
return 'null';
|
|
714
|
+
}
|
|
715
|
+
if (Array.isArray(value)) {
|
|
716
|
+
return 'array';
|
|
717
|
+
}
|
|
718
|
+
return 'object';
|
|
719
|
+
case 'boolean':
|
|
720
|
+
return 'boolean';
|
|
721
|
+
default:
|
|
722
|
+
return 'unknown';
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
const number = value => {
|
|
726
|
+
const type = getType$1(value);
|
|
727
|
+
if (type !== 'number') {
|
|
728
|
+
throw new AssertionError('expected value to be of type number');
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
const state$1 = {
|
|
732
|
+
callbacks: Object.create(null)
|
|
733
|
+
};
|
|
734
|
+
const get = id => {
|
|
735
|
+
return state$1.callbacks[id];
|
|
736
|
+
};
|
|
737
|
+
const remove = id => {
|
|
738
|
+
delete state$1.callbacks[id];
|
|
739
|
+
};
|
|
740
|
+
const warn = (...args) => {
|
|
741
|
+
console.warn(...args);
|
|
742
|
+
};
|
|
743
|
+
const resolve = (id, args) => {
|
|
744
|
+
number(id);
|
|
745
|
+
const fn = get(id);
|
|
746
|
+
if (!fn) {
|
|
747
|
+
console.log(args);
|
|
748
|
+
warn(`callback ${id} may already be disposed`);
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
fn(args);
|
|
752
|
+
remove(id);
|
|
753
|
+
};
|
|
754
|
+
class JsonRpcError extends Error {
|
|
755
|
+
constructor(message) {
|
|
756
|
+
super(message);
|
|
757
|
+
this.name = 'JsonRpcError';
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
const MethodNotFound = -32601;
|
|
761
|
+
const Custom = -32001;
|
|
762
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
763
|
+
const getType = prettyError => {
|
|
764
|
+
if (prettyError && prettyError.type) {
|
|
765
|
+
return prettyError.type;
|
|
766
|
+
}
|
|
767
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
768
|
+
return prettyError.constructor.name;
|
|
769
|
+
}
|
|
770
|
+
return undefined;
|
|
771
|
+
};
|
|
772
|
+
const getErrorProperty = (error, prettyError) => {
|
|
773
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
774
|
+
return {
|
|
775
|
+
code: MethodNotFound,
|
|
776
|
+
message: error.message,
|
|
777
|
+
data: error.stack
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
return {
|
|
781
|
+
code: Custom,
|
|
782
|
+
message: prettyError.message,
|
|
783
|
+
data: {
|
|
784
|
+
stack: prettyError.stack,
|
|
785
|
+
codeFrame: prettyError.codeFrame,
|
|
786
|
+
type: getType(prettyError),
|
|
787
|
+
code: prettyError.code,
|
|
788
|
+
name: prettyError.name
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
const create$1 = (message, error) => {
|
|
793
|
+
return {
|
|
794
|
+
jsonrpc: Two,
|
|
795
|
+
id: message.id,
|
|
796
|
+
error
|
|
797
|
+
};
|
|
798
|
+
};
|
|
799
|
+
const getErrorResponse = (message, error, preparePrettyError, logError) => {
|
|
800
|
+
const prettyError = preparePrettyError(error);
|
|
801
|
+
logError(error, prettyError);
|
|
802
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
803
|
+
return create$1(message, errorProperty);
|
|
804
|
+
};
|
|
805
|
+
const create = (message, result) => {
|
|
806
|
+
return {
|
|
807
|
+
jsonrpc: Two,
|
|
808
|
+
id: message.id,
|
|
809
|
+
result: result ?? null
|
|
810
|
+
};
|
|
811
|
+
};
|
|
812
|
+
const getSuccessResponse = (message, result) => {
|
|
813
|
+
const resultProperty = result ?? null;
|
|
814
|
+
return create(message, resultProperty);
|
|
815
|
+
};
|
|
816
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
817
|
+
try {
|
|
818
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
819
|
+
return getSuccessResponse(message, result);
|
|
820
|
+
} catch (error) {
|
|
821
|
+
return getErrorResponse(message, error, preparePrettyError, logError);
|
|
822
|
+
}
|
|
823
|
+
};
|
|
824
|
+
const defaultPreparePrettyError = error => {
|
|
825
|
+
return error;
|
|
826
|
+
};
|
|
827
|
+
const defaultLogError = () => {
|
|
828
|
+
// ignore
|
|
829
|
+
};
|
|
830
|
+
const defaultRequiresSocket = () => {
|
|
831
|
+
return false;
|
|
832
|
+
};
|
|
833
|
+
const defaultResolve = resolve;
|
|
834
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
835
|
+
let message;
|
|
836
|
+
let ipc;
|
|
837
|
+
let execute;
|
|
838
|
+
let preparePrettyError;
|
|
839
|
+
let logError;
|
|
840
|
+
let resolve;
|
|
841
|
+
let requiresSocket;
|
|
842
|
+
if (args.length === 1) {
|
|
843
|
+
const arg = args[0];
|
|
844
|
+
message = arg.message;
|
|
845
|
+
ipc = arg.ipc;
|
|
846
|
+
execute = arg.execute;
|
|
847
|
+
preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
|
|
848
|
+
logError = arg.logError || defaultLogError;
|
|
849
|
+
requiresSocket = arg.requiresSocket || defaultRequiresSocket;
|
|
850
|
+
resolve = arg.resolve || defaultResolve;
|
|
851
|
+
} else {
|
|
852
|
+
ipc = args[0];
|
|
853
|
+
message = args[1];
|
|
854
|
+
execute = args[2];
|
|
855
|
+
resolve = args[3];
|
|
856
|
+
preparePrettyError = args[4];
|
|
857
|
+
logError = args[5];
|
|
858
|
+
requiresSocket = args[6];
|
|
859
|
+
}
|
|
860
|
+
if ('id' in message) {
|
|
861
|
+
if ('method' in message) {
|
|
862
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
863
|
+
try {
|
|
864
|
+
ipc.send(response);
|
|
865
|
+
} catch (error) {
|
|
866
|
+
const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
|
|
867
|
+
ipc.send(errorResponse);
|
|
868
|
+
}
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
resolve(message.id, message);
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
if ('method' in message) {
|
|
875
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
throw new JsonRpcError('unexpected message');
|
|
879
|
+
};
|
|
880
|
+
|
|
881
|
+
const requiresSocket = () => {
|
|
882
|
+
return false;
|
|
883
|
+
};
|
|
884
|
+
const preparePrettyError = error => {
|
|
885
|
+
return error;
|
|
886
|
+
};
|
|
887
|
+
const logError = error => {
|
|
888
|
+
// handled by renderer worker
|
|
889
|
+
};
|
|
890
|
+
const handleMessage = event => {
|
|
891
|
+
return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
const handleIpc = ipc => {
|
|
895
|
+
ipc.addEventListener('message', handleMessage);
|
|
896
|
+
};
|
|
897
|
+
|
|
898
|
+
const MessagePort$1 = 1;
|
|
899
|
+
const ModuleWorker = 2;
|
|
900
|
+
const ReferencePort = 3;
|
|
901
|
+
const ModuleWorkerAndMessagePort = 8;
|
|
902
|
+
const Auto = () => {
|
|
903
|
+
// @ts-ignore
|
|
904
|
+
if (globalThis.acceptPort) {
|
|
905
|
+
return MessagePort$1;
|
|
906
|
+
}
|
|
907
|
+
// @ts-ignore
|
|
908
|
+
if (globalThis.acceptReferencePort) {
|
|
909
|
+
return ReferencePort;
|
|
910
|
+
}
|
|
911
|
+
return ModuleWorkerAndMessagePort;
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
const getData$1 = event => {
|
|
915
|
+
return event.data;
|
|
916
|
+
};
|
|
917
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
918
|
+
if (!value) {
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
if (isTransferrable(value)) {
|
|
922
|
+
transferrables.push(value);
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
if (Array.isArray(value)) {
|
|
926
|
+
for (const item of value) {
|
|
927
|
+
walkValue(item, transferrables, isTransferrable);
|
|
928
|
+
}
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
if (typeof value === 'object') {
|
|
932
|
+
for (const property of Object.values(value)) {
|
|
933
|
+
walkValue(property, transferrables, isTransferrable);
|
|
934
|
+
}
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
const isMessagePort = value => {
|
|
939
|
+
return value && value instanceof MessagePort;
|
|
940
|
+
};
|
|
941
|
+
const isMessagePortMain = value => {
|
|
942
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
943
|
+
};
|
|
944
|
+
const isOffscreenCanvas = value => {
|
|
945
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
946
|
+
};
|
|
947
|
+
const isInstanceOf = (value, constructorName) => {
|
|
948
|
+
return value?.constructor?.name === constructorName;
|
|
949
|
+
};
|
|
950
|
+
const isSocket = value => {
|
|
951
|
+
return isInstanceOf(value, 'Socket');
|
|
952
|
+
};
|
|
953
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
954
|
+
const isTransferrable = value => {
|
|
955
|
+
for (const fn of transferrables) {
|
|
956
|
+
if (fn(value)) {
|
|
957
|
+
return true;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
return false;
|
|
961
|
+
};
|
|
962
|
+
const getTransferrables = value => {
|
|
963
|
+
const transferrables = [];
|
|
964
|
+
walkValue(value, transferrables, isTransferrable);
|
|
965
|
+
return transferrables;
|
|
966
|
+
};
|
|
967
|
+
const attachEvents = that => {
|
|
968
|
+
const handleMessage = (...args) => {
|
|
969
|
+
const data = that.getData(...args);
|
|
970
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
971
|
+
data
|
|
972
|
+
}));
|
|
973
|
+
};
|
|
974
|
+
that.onMessage(handleMessage);
|
|
975
|
+
const handleClose = event => {
|
|
976
|
+
that.dispatchEvent(new Event('close'));
|
|
977
|
+
};
|
|
978
|
+
that.onClose(handleClose);
|
|
979
|
+
};
|
|
980
|
+
class Ipc extends EventTarget {
|
|
981
|
+
constructor(rawIpc) {
|
|
982
|
+
super();
|
|
983
|
+
this._rawIpc = rawIpc;
|
|
984
|
+
attachEvents(this);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
const readyMessage = 'ready';
|
|
988
|
+
const listen$4 = () => {
|
|
989
|
+
// @ts-ignore
|
|
990
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
991
|
+
throw new TypeError('module is not in web worker scope');
|
|
992
|
+
}
|
|
993
|
+
return globalThis;
|
|
994
|
+
};
|
|
995
|
+
const signal$3 = global => {
|
|
996
|
+
global.postMessage(readyMessage);
|
|
997
|
+
};
|
|
998
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
999
|
+
getData(event) {
|
|
1000
|
+
return getData$1(event);
|
|
1001
|
+
}
|
|
1002
|
+
send(message) {
|
|
1003
|
+
// @ts-ignore
|
|
1004
|
+
this._rawIpc.postMessage(message);
|
|
1005
|
+
}
|
|
1006
|
+
sendAndTransfer(message) {
|
|
1007
|
+
const transfer = getTransferrables(message);
|
|
1008
|
+
// @ts-ignore
|
|
1009
|
+
this._rawIpc.postMessage(message, transfer);
|
|
1010
|
+
}
|
|
1011
|
+
dispose() {
|
|
1012
|
+
// ignore
|
|
1013
|
+
}
|
|
1014
|
+
onClose(callback) {
|
|
1015
|
+
// ignore
|
|
1016
|
+
}
|
|
1017
|
+
onMessage(callback) {
|
|
1018
|
+
this._rawIpc.addEventListener('message', callback);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
const wrap$6 = global => {
|
|
1022
|
+
return new IpcChildWithModuleWorker(global);
|
|
1023
|
+
};
|
|
1024
|
+
const IpcChildWithModuleWorker$1 = {
|
|
1025
|
+
__proto__: null,
|
|
1026
|
+
listen: listen$4,
|
|
1027
|
+
signal: signal$3,
|
|
1028
|
+
wrap: wrap$6
|
|
1029
|
+
};
|
|
1030
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
1031
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
1032
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
1033
|
+
const NewLine$1 = '\n';
|
|
1034
|
+
const joinLines = lines => {
|
|
1035
|
+
return lines.join(NewLine$1);
|
|
1036
|
+
};
|
|
1037
|
+
const splitLines = lines => {
|
|
1038
|
+
return lines.split(NewLine$1);
|
|
1039
|
+
};
|
|
1040
|
+
const isModuleNotFoundMessage = line => {
|
|
1041
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
1042
|
+
};
|
|
1043
|
+
const getModuleNotFoundError = stderr => {
|
|
1044
|
+
const lines = splitLines(stderr);
|
|
1045
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
1046
|
+
const message = lines[messageIndex];
|
|
1047
|
+
return {
|
|
1048
|
+
message,
|
|
1049
|
+
code: ERR_MODULE_NOT_FOUND
|
|
1050
|
+
};
|
|
1051
|
+
};
|
|
1052
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
1053
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
1054
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
1055
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
1056
|
+
const RE_AT = /^\s+at/;
|
|
1057
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
1058
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
1059
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
1060
|
+
};
|
|
1061
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
1062
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
1063
|
+
};
|
|
1064
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
1065
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
1066
|
+
};
|
|
1067
|
+
const getMessageCodeBlock = stderr => {
|
|
1068
|
+
const lines = splitLines(stderr);
|
|
1069
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
1070
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
1071
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
1072
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
1073
|
+
return relevantMessage;
|
|
1074
|
+
};
|
|
1075
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
1076
|
+
const message = getMessageCodeBlock(stderr);
|
|
1077
|
+
return {
|
|
1078
|
+
message: `Incompatible native node module: ${message}`,
|
|
1079
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE
|
|
1080
|
+
};
|
|
1081
|
+
};
|
|
1082
|
+
const isModulesSyntaxError = stderr => {
|
|
1083
|
+
if (!stderr) {
|
|
1084
|
+
return false;
|
|
1085
|
+
}
|
|
1086
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
1087
|
+
};
|
|
1088
|
+
const getModuleSyntaxError = () => {
|
|
1089
|
+
return {
|
|
1090
|
+
message: `ES Modules are not supported in electron`,
|
|
1091
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
|
|
1092
|
+
};
|
|
1093
|
+
};
|
|
1094
|
+
const isModuleNotFoundError = stderr => {
|
|
1095
|
+
if (!stderr) {
|
|
1096
|
+
return false;
|
|
1097
|
+
}
|
|
1098
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
1099
|
+
};
|
|
1100
|
+
const isNormalStackLine = line => {
|
|
1101
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
1102
|
+
};
|
|
1103
|
+
const getDetails = lines => {
|
|
1104
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
1105
|
+
if (index === -1) {
|
|
1106
|
+
return {
|
|
1107
|
+
actualMessage: joinLines(lines),
|
|
1108
|
+
rest: []
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
let lastIndex = index - 1;
|
|
1112
|
+
while (++lastIndex < lines.length) {
|
|
1113
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
1114
|
+
break;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
return {
|
|
1118
|
+
actualMessage: lines[index - 1],
|
|
1119
|
+
rest: lines.slice(index, lastIndex)
|
|
1120
|
+
};
|
|
1121
|
+
};
|
|
1122
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
1123
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
1124
|
+
return getNativeModuleErrorMessage(stderr);
|
|
1125
|
+
}
|
|
1126
|
+
if (isModulesSyntaxError(stderr)) {
|
|
1127
|
+
return getModuleSyntaxError();
|
|
1128
|
+
}
|
|
1129
|
+
if (isModuleNotFoundError(stderr)) {
|
|
1130
|
+
return getModuleNotFoundError(stderr);
|
|
1131
|
+
}
|
|
1132
|
+
const lines = splitLines(stderr);
|
|
1133
|
+
const {
|
|
1134
|
+
actualMessage,
|
|
1135
|
+
rest
|
|
1136
|
+
} = getDetails(lines);
|
|
1137
|
+
return {
|
|
1138
|
+
message: `${actualMessage}`,
|
|
1139
|
+
code: '',
|
|
1140
|
+
stack: rest
|
|
1141
|
+
};
|
|
1142
|
+
};
|
|
1143
|
+
const normalizeLine = line => {
|
|
1144
|
+
if (line.startsWith('Error: ')) {
|
|
1145
|
+
return line.slice(`Error: `.length);
|
|
1146
|
+
}
|
|
1147
|
+
if (line.startsWith('VError: ')) {
|
|
1148
|
+
return line.slice(`VError: `.length);
|
|
1149
|
+
}
|
|
1150
|
+
return line;
|
|
1151
|
+
};
|
|
1152
|
+
const getCombinedMessage = (error, message) => {
|
|
1153
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
1154
|
+
if (message) {
|
|
1155
|
+
return `${message}: ${stringifiedError}`;
|
|
1156
|
+
}
|
|
1157
|
+
return stringifiedError;
|
|
1158
|
+
};
|
|
1159
|
+
const NewLine = '\n';
|
|
1160
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
1161
|
+
return string.indexOf(NewLine, startIndex);
|
|
1162
|
+
};
|
|
1163
|
+
const mergeStacks = (parent, child) => {
|
|
1164
|
+
if (!child) {
|
|
1165
|
+
return parent;
|
|
1166
|
+
}
|
|
1167
|
+
const parentNewLineIndex = getNewLineIndex(parent);
|
|
1168
|
+
const childNewLineIndex = getNewLineIndex(child);
|
|
1169
|
+
if (childNewLineIndex === -1) {
|
|
1170
|
+
return parent;
|
|
1171
|
+
}
|
|
1172
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
1173
|
+
const childRest = child.slice(childNewLineIndex);
|
|
1174
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
1175
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
1176
|
+
return parentFirstLine + childRest;
|
|
1177
|
+
}
|
|
1178
|
+
return child;
|
|
1179
|
+
};
|
|
1180
|
+
class VError extends Error {
|
|
1181
|
+
constructor(error, message) {
|
|
1182
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
1183
|
+
super(combinedMessage);
|
|
1184
|
+
this.name = 'VError';
|
|
1185
|
+
if (error instanceof Error) {
|
|
1186
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
1187
|
+
}
|
|
1188
|
+
if (error.codeFrame) {
|
|
1189
|
+
// @ts-ignore
|
|
1190
|
+
this.codeFrame = error.codeFrame;
|
|
1191
|
+
}
|
|
1192
|
+
if (error.code) {
|
|
1193
|
+
// @ts-ignore
|
|
1194
|
+
this.code = error.code;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
class IpcError extends VError {
|
|
1199
|
+
// @ts-ignore
|
|
1200
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
1201
|
+
if (stdout || stderr) {
|
|
1202
|
+
// @ts-ignore
|
|
1203
|
+
const {
|
|
1204
|
+
message,
|
|
1205
|
+
code,
|
|
1206
|
+
stack
|
|
1207
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
1208
|
+
const cause = new Error(message);
|
|
1209
|
+
// @ts-ignore
|
|
1210
|
+
cause.code = code;
|
|
1211
|
+
cause.stack = stack;
|
|
1212
|
+
super(cause, betterMessage);
|
|
1213
|
+
} else {
|
|
1214
|
+
super(betterMessage);
|
|
1215
|
+
}
|
|
1216
|
+
// @ts-ignore
|
|
1217
|
+
this.name = 'IpcError';
|
|
1218
|
+
// @ts-ignore
|
|
1219
|
+
this.stdout = stdout;
|
|
1220
|
+
// @ts-ignore
|
|
1221
|
+
this.stderr = stderr;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
const withResolvers = () => {
|
|
1225
|
+
let _resolve;
|
|
1226
|
+
const promise = new Promise(resolve => {
|
|
1227
|
+
_resolve = resolve;
|
|
1228
|
+
});
|
|
1229
|
+
return {
|
|
1230
|
+
resolve: _resolve,
|
|
1231
|
+
promise
|
|
1232
|
+
};
|
|
1233
|
+
};
|
|
1234
|
+
const waitForFirstMessage = async port => {
|
|
1235
|
+
const {
|
|
1236
|
+
resolve,
|
|
1237
|
+
promise
|
|
1238
|
+
} = withResolvers();
|
|
1239
|
+
port.addEventListener('message', resolve, {
|
|
1240
|
+
once: true
|
|
1241
|
+
});
|
|
1242
|
+
const event = await promise;
|
|
1243
|
+
// @ts-ignore
|
|
1244
|
+
return event.data;
|
|
1245
|
+
};
|
|
1246
|
+
const listen$3 = async () => {
|
|
1247
|
+
const parentIpcRaw = listen$4();
|
|
1248
|
+
signal$3(parentIpcRaw);
|
|
1249
|
+
const parentIpc = wrap$6(parentIpcRaw);
|
|
1250
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
1251
|
+
if (firstMessage.method !== 'initialize') {
|
|
1252
|
+
throw new IpcError('unexpected first message');
|
|
1253
|
+
}
|
|
1254
|
+
const type = firstMessage.params[0];
|
|
1255
|
+
if (type === 'message-port') {
|
|
1256
|
+
parentIpc.send({
|
|
1257
|
+
jsonrpc: '2.0',
|
|
1258
|
+
id: firstMessage.id,
|
|
1259
|
+
result: null
|
|
1260
|
+
});
|
|
1261
|
+
parentIpc.dispose();
|
|
1262
|
+
const port = firstMessage.params[1];
|
|
1263
|
+
return port;
|
|
1264
|
+
}
|
|
1265
|
+
return globalThis;
|
|
1266
|
+
};
|
|
1267
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
1268
|
+
constructor(port) {
|
|
1269
|
+
super(port);
|
|
1270
|
+
}
|
|
1271
|
+
getData(event) {
|
|
1272
|
+
return getData$1(event);
|
|
1273
|
+
}
|
|
1274
|
+
send(message) {
|
|
1275
|
+
this._rawIpc.postMessage(message);
|
|
1276
|
+
}
|
|
1277
|
+
sendAndTransfer(message) {
|
|
1278
|
+
const transfer = getTransferrables(message);
|
|
1279
|
+
this._rawIpc.postMessage(message, transfer);
|
|
1280
|
+
}
|
|
1281
|
+
dispose() {
|
|
1282
|
+
if (this._rawIpc.close) {
|
|
1283
|
+
this._rawIpc.close();
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
onClose(callback) {
|
|
1287
|
+
// ignore
|
|
1288
|
+
}
|
|
1289
|
+
onMessage(callback) {
|
|
1290
|
+
this._rawIpc.addEventListener('message', callback);
|
|
1291
|
+
this._rawIpc.start();
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
const wrap$5 = port => {
|
|
1295
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
1296
|
+
};
|
|
1297
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
1298
|
+
__proto__: null,
|
|
1299
|
+
listen: listen$3,
|
|
1300
|
+
wrap: wrap$5
|
|
1301
|
+
};
|
|
1302
|
+
|
|
1303
|
+
const getModule = method => {
|
|
1304
|
+
switch (method) {
|
|
1305
|
+
case ModuleWorker:
|
|
1306
|
+
return IpcChildWithModuleWorker$1;
|
|
1307
|
+
case ModuleWorkerAndMessagePort:
|
|
1308
|
+
return IpcChildWithModuleWorkerAndMessagePort$1;
|
|
1309
|
+
default:
|
|
1310
|
+
throw new Error('unexpected ipc type');
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
|
|
1314
|
+
const listen$1 = async ({
|
|
1315
|
+
method
|
|
1316
|
+
}) => {
|
|
1317
|
+
const module = await getModule(method);
|
|
1318
|
+
const rawIpc = await module.listen();
|
|
1319
|
+
if (module.signal) {
|
|
1320
|
+
module.signal(rawIpc);
|
|
1321
|
+
}
|
|
1322
|
+
const ipc = module.wrap(rawIpc);
|
|
1323
|
+
return ipc;
|
|
1324
|
+
};
|
|
1325
|
+
|
|
1326
|
+
const listen = async () => {
|
|
1327
|
+
register(commandMap);
|
|
1328
|
+
const ipc = await listen$1({
|
|
1329
|
+
method: Auto()
|
|
1330
|
+
});
|
|
1331
|
+
handleIpc(ipc);
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1334
|
+
const main = async () => {
|
|
1335
|
+
await listen();
|
|
1336
|
+
};
|
|
1337
|
+
|
|
1338
|
+
main();
|