@lvce-editor/text-search-worker 1.0.0 → 1.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.
@@ -70,7 +70,7 @@ const textSearch$5 = async (scheme, root, query) => {
70
70
 
71
71
  const assetDir = '';
72
72
 
73
- const splitLines$1 = lines => {
73
+ const splitLines$2 = lines => {
74
74
  return lines.split('\n');
75
75
  };
76
76
 
@@ -79,7 +79,7 @@ const Match = 2;
79
79
 
80
80
  const textSearchInFile$1 = (file, content, query) => {
81
81
  const results = [];
82
- const lines = splitLines$1(content);
82
+ const lines = splitLines$2(content);
83
83
  for (let i = 0; i < lines.length; i++) {
84
84
  const line = lines[i];
85
85
  const index = line.indexOf(query);
@@ -436,16 +436,16 @@ const getCombinedMessage$1 = (error, message) => {
436
436
  }
437
437
  return stringifiedError;
438
438
  };
439
- const NewLine$2 = '\n';
440
- const getNewLineIndex$1 = (string, startIndex = undefined) => {
441
- return string.indexOf(NewLine$2, startIndex);
439
+ const NewLine$3 = '\n';
440
+ const getNewLineIndex$2 = (string, startIndex = undefined) => {
441
+ return string.indexOf(NewLine$3, startIndex);
442
442
  };
443
443
  const mergeStacks$1 = (parent, child) => {
444
444
  if (!child) {
445
445
  return parent;
446
446
  }
447
- const parentNewLineIndex = getNewLineIndex$1(parent);
448
- const childNewLineIndex = getNewLineIndex$1(child);
447
+ const parentNewLineIndex = getNewLineIndex$2(parent);
448
+ const childNewLineIndex = getNewLineIndex$2(child);
449
449
  if (childNewLineIndex === -1) {
450
450
  return parent;
451
451
  }
@@ -476,7 +476,7 @@ let VError$1 = class VError extends Error {
476
476
  }
477
477
  };
478
478
 
479
- const state = {
479
+ const state$3 = {
480
480
  databases: Object.create(null),
481
481
  eventId: 0,
482
482
  dbVersion: 1,
@@ -487,7 +487,7 @@ const state = {
487
487
 
488
488
  const getHandleDb = async () => {
489
489
  // @ts-ignore
490
- const db = await openDB('handle', state.dbVersion, {
490
+ const db = await openDB('handle', state$3.dbVersion, {
491
491
  async upgrade(db, oldVersion) {
492
492
  if (!db.objectStoreNames.contains('file-handles-store')) {
493
493
  // @ts-ignore
@@ -527,7 +527,7 @@ const getDirectoryHandle = async uri => {
527
527
  };
528
528
  const textSearchInText = (file, content, query) => {
529
529
  const results = [];
530
- const lines = splitLines$1(content);
530
+ const lines = splitLines$2(content);
531
531
  for (let i = 0; i < lines.length; i++) {
532
532
  const line = lines[i];
533
533
  const index = line.indexOf(query);
@@ -623,74 +623,17 @@ const getRipGrepArgs = ({
623
623
  return ripGrepArgs;
624
624
  };
625
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
- }
626
+ const state$2 = {
627
+ /**
628
+ * @type {any}
629
+ */
630
+ ipc: undefined
636
631
  };
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;
632
+ const get$1 = () => {
633
+ return state$2.ipc;
650
634
  };
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
635
+ const set$1 = ipc => {
636
+ state$2.ipc = ipc;
694
637
  };
695
638
 
696
639
  const Two = '2.0';
@@ -731,15 +674,49 @@ const number = value => {
731
674
  const state$1 = {
732
675
  callbacks: Object.create(null)
733
676
  };
677
+ const set = (id, fn) => {
678
+ state$1.callbacks[id] = fn;
679
+ };
734
680
  const get = id => {
735
681
  return state$1.callbacks[id];
736
682
  };
737
683
  const remove = id => {
738
684
  delete state$1.callbacks[id];
739
685
  };
686
+ const state = {
687
+ id: 0
688
+ };
689
+ const create$3 = () => {
690
+ return ++state.id;
691
+ };
740
692
  const warn = (...args) => {
741
693
  console.warn(...args);
742
694
  };
695
+ const withResolvers$1 = () => {
696
+ /**
697
+ * @type {any}
698
+ */
699
+ let _resolve;
700
+ const promise = new Promise(resolve => {
701
+ _resolve = resolve;
702
+ });
703
+ return {
704
+ resolve: _resolve,
705
+ promise
706
+ };
707
+ };
708
+ const registerPromise = () => {
709
+ const id = create$3();
710
+ const {
711
+ resolve,
712
+ promise
713
+ } = withResolvers$1();
714
+ set(id, resolve);
715
+ return {
716
+ id,
717
+ promise
718
+ };
719
+ };
743
720
  const resolve = (id, args) => {
744
721
  number(id);
745
722
  const fn = get(id);
@@ -751,14 +728,153 @@ const resolve = (id, args) => {
751
728
  fn(args);
752
729
  remove(id);
753
730
  };
731
+ const create$2 = (method, params) => {
732
+ const {
733
+ id,
734
+ promise
735
+ } = registerPromise();
736
+ const message = {
737
+ jsonrpc: Two,
738
+ method,
739
+ params,
740
+ id
741
+ };
742
+ return {
743
+ message,
744
+ promise
745
+ };
746
+ };
754
747
  class JsonRpcError extends Error {
755
748
  constructor(message) {
756
749
  super(message);
757
750
  this.name = 'JsonRpcError';
758
751
  }
759
752
  }
753
+ const NewLine$2 = '\n';
754
+ const DomException = 'DOMException';
755
+ const ReferenceError$1 = 'ReferenceError';
756
+ const SyntaxError$1 = 'SyntaxError';
757
+ const TypeError$1 = 'TypeError';
758
+ const getErrorConstructor = (message, type) => {
759
+ if (type) {
760
+ switch (type) {
761
+ case DomException:
762
+ return DOMException;
763
+ case TypeError$1:
764
+ return TypeError;
765
+ case SyntaxError$1:
766
+ return SyntaxError;
767
+ case ReferenceError$1:
768
+ return ReferenceError;
769
+ default:
770
+ return Error;
771
+ }
772
+ }
773
+ if (message.startsWith('TypeError: ')) {
774
+ return TypeError;
775
+ }
776
+ if (message.startsWith('SyntaxError: ')) {
777
+ return SyntaxError;
778
+ }
779
+ if (message.startsWith('ReferenceError: ')) {
780
+ return ReferenceError;
781
+ }
782
+ return Error;
783
+ };
784
+ const constructError = (message, type, name) => {
785
+ const ErrorConstructor = getErrorConstructor(message, type);
786
+ if (ErrorConstructor === DOMException && name) {
787
+ return new ErrorConstructor(message, name);
788
+ }
789
+ if (ErrorConstructor === Error) {
790
+ const error = new Error(message);
791
+ if (name && name !== 'VError') {
792
+ error.name = name;
793
+ }
794
+ return error;
795
+ }
796
+ return new ErrorConstructor(message);
797
+ };
798
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
799
+ return string.indexOf(NewLine$2, startIndex);
800
+ };
801
+ const getParentStack = error => {
802
+ let parentStack = error.stack || error.data || error.message || '';
803
+ if (parentStack.startsWith(' at')) {
804
+ parentStack = error.message + NewLine$2 + parentStack;
805
+ }
806
+ return parentStack;
807
+ };
808
+ const joinLines$1 = lines => {
809
+ return lines.join(NewLine$2);
810
+ };
760
811
  const MethodNotFound = -32601;
761
812
  const Custom = -32001;
813
+ const splitLines$1 = lines => {
814
+ return lines.split(NewLine$2);
815
+ };
816
+ const restoreJsonRpcError = error => {
817
+ if (error && error instanceof Error) {
818
+ return error;
819
+ }
820
+ const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
821
+ if (error && error.code && error.code === MethodNotFound) {
822
+ const restoredError = new JsonRpcError(error.message);
823
+ const parentStack = getParentStack(error);
824
+ restoredError.stack = parentStack + NewLine$2 + currentStack;
825
+ return restoredError;
826
+ }
827
+ if (error && error.message) {
828
+ const restoredError = constructError(error.message, error.type, error.name);
829
+ if (error.data) {
830
+ if (error.data.stack && error.data.type && error.message) {
831
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine$2 + error.data.stack + NewLine$2 + currentStack;
832
+ } else if (error.data.stack) {
833
+ restoredError.stack = error.data.stack;
834
+ }
835
+ if (error.data.codeFrame) {
836
+ // @ts-ignore
837
+ restoredError.codeFrame = error.data.codeFrame;
838
+ }
839
+ if (error.data.code) {
840
+ // @ts-ignore
841
+ restoredError.code = error.data.code;
842
+ }
843
+ if (error.data.type) {
844
+ // @ts-ignore
845
+ restoredError.name = error.data.type;
846
+ }
847
+ } else {
848
+ if (error.stack) {
849
+ const lowerStack = restoredError.stack || '';
850
+ // @ts-ignore
851
+ const indexNewLine = getNewLineIndex$1(lowerStack);
852
+ const parentStack = getParentStack(error);
853
+ // @ts-ignore
854
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
855
+ }
856
+ if (error.codeFrame) {
857
+ // @ts-ignore
858
+ restoredError.codeFrame = error.codeFrame;
859
+ }
860
+ }
861
+ return restoredError;
862
+ }
863
+ if (typeof error === 'string') {
864
+ return new Error(`JsonRpc Error: ${error}`);
865
+ }
866
+ return new Error(`JsonRpc Error: ${error}`);
867
+ };
868
+ const unwrapJsonRpcResult = responseMessage => {
869
+ if ('error' in responseMessage) {
870
+ const restoredError = restoreJsonRpcError(responseMessage.error);
871
+ throw restoredError;
872
+ }
873
+ if ('result' in responseMessage) {
874
+ return responseMessage.result;
875
+ }
876
+ throw new JsonRpcError('unexpected response message');
877
+ };
762
878
  const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
763
879
  const getType = prettyError => {
764
880
  if (prettyError && prettyError.type) {
@@ -877,6 +993,86 @@ const handleJsonRpcMessage = async (...args) => {
877
993
  }
878
994
  throw new JsonRpcError('unexpected message');
879
995
  };
996
+ const invoke$2 = async (ipc, method, ...params) => {
997
+ const {
998
+ message,
999
+ promise
1000
+ } = create$2(method, params);
1001
+ ipc.send(message);
1002
+ const responseMessage = await promise;
1003
+ const result = unwrapJsonRpcResult(responseMessage);
1004
+ return result;
1005
+ };
1006
+
1007
+ const invoke$1 = (method, ...params) => {
1008
+ const ipc = get$1();
1009
+ return invoke$2(ipc, method, ...params);
1010
+ };
1011
+ const listen$2 = ipc => {
1012
+ set$1(ipc);
1013
+ };
1014
+
1015
+ const invoke = (method, ...params) => {
1016
+ return invoke$1('SearchProcess.invoke', method, ...params);
1017
+ };
1018
+
1019
+ const textSearch$2 = async (scheme, root, query, options) => {
1020
+ const ripGrepArgs = getRipGrepArgs({
1021
+ ...options,
1022
+ searchString: query
1023
+ });
1024
+ const actualOptions = {
1025
+ ripGrepArgs,
1026
+ searchDir: root
1027
+ };
1028
+ const result = await invoke('TextSearch.search', actualOptions);
1029
+ // TODO api is weird
1030
+ return result.results;
1031
+ };
1032
+
1033
+ const textSearch$1 = (scheme, root, query) => {
1034
+ // TODO ask renderer worker for files
1035
+ const entries = Object.entries({});
1036
+ const results = [];
1037
+ for (const [key, value] of entries) {
1038
+ // @ts-ignore
1039
+ if (value.includes(query)) {
1040
+ results.push([key, [{
1041
+ absoluteOffset: 0,
1042
+ preview: value
1043
+ }]]);
1044
+ }
1045
+ }
1046
+ return results;
1047
+ };
1048
+
1049
+ const getProvider = scheme => {
1050
+ switch (scheme) {
1051
+ case '':
1052
+ return textSearch$2;
1053
+ case 'web':
1054
+ return textSearch$1;
1055
+ case 'fetch':
1056
+ return textSearch$4;
1057
+ case 'html':
1058
+ return textSearch$3;
1059
+ default:
1060
+ return textSearch$5;
1061
+ }
1062
+ };
1063
+
1064
+ const textSearch = async (root, query, options) => {
1065
+ string(root);
1066
+ string(query);
1067
+ const scheme = getProtocol(root);
1068
+ const provider = getProvider(scheme);
1069
+ const results = await provider(scheme, root, query, options);
1070
+ return results;
1071
+ };
1072
+
1073
+ const commandMap = {
1074
+ 'TextSearch.textSearch': textSearch
1075
+ };
880
1076
 
881
1077
  const requiresSocket = () => {
882
1078
  return false;
@@ -1329,6 +1525,7 @@ const listen = async () => {
1329
1525
  method: Auto()
1330
1526
  });
1331
1527
  handleIpc(ipc);
1528
+ listen$2(ipc);
1332
1529
  };
1333
1530
 
1334
1531
  const main = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/text-search-worker",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "dist/textSearchWorkerMain.js",
6
6
  "type": "module",