@lvce-editor/file-watcher-process 3.1.0 → 3.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 +81 -35
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2,6 +2,10 @@ import * as fs from 'node:fs/promises';
2
2
  import { watch } from 'chokidar';
3
3
  import { pathToFileURL, fileURLToPath } from 'node:url';
4
4
 
5
+ const handleUncaughtExceptionMonitor = error => {
6
+ console.error(`[file-watcher-process] Uncaught Exception: ${error}`);
7
+ };
8
+
5
9
  const normalizeLine = line => {
6
10
  if (line.startsWith('Error: ')) {
7
11
  return line.slice('Error: '.length);
@@ -480,7 +484,7 @@ const listen$3 = async () => {
480
484
  parentPort
481
485
  } = await import('node:worker_threads');
482
486
  if (!parentPort) {
483
- throw new IpcError('parentPort is required');
487
+ throw new IpcError('parentPort is required for node worker threads ipc');
484
488
  }
485
489
  return parentPort;
486
490
  };
@@ -519,7 +523,7 @@ const IpcChildWithNodeWorker$1 = {
519
523
  };
520
524
 
521
525
  const Two = '2.0';
522
- const create$4$1 = (method, params) => {
526
+ const create$4 = (method, params) => {
523
527
  return {
524
528
  jsonrpc: Two,
525
529
  method,
@@ -619,6 +623,16 @@ const constructError = (message, type, name) => {
619
623
  }
620
624
  return new ErrorConstructor(message);
621
625
  };
626
+ const joinLines = lines => {
627
+ return lines.join(NewLine);
628
+ };
629
+ const splitLines = lines => {
630
+ return lines.split(NewLine);
631
+ };
632
+ const getCurrentStack = () => {
633
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(2));
634
+ return currentStack;
635
+ };
622
636
  const getNewLineIndex = (string, startIndex = undefined) => {
623
637
  return string.indexOf(NewLine, startIndex);
624
638
  };
@@ -629,19 +643,16 @@ const getParentStack = error => {
629
643
  }
630
644
  return parentStack;
631
645
  };
632
- const joinLines = lines => {
633
- return lines.join(NewLine);
634
- };
635
646
  const MethodNotFound = -32601;
636
647
  const Custom = -32001;
637
- const splitLines = lines => {
638
- return lines.split(NewLine);
639
- };
640
648
  const restoreJsonRpcError = error => {
649
+ const currentStack = getCurrentStack();
641
650
  if (error && error instanceof Error) {
651
+ if (typeof error.stack === 'string') {
652
+ error.stack = error.stack + NewLine + currentStack;
653
+ }
642
654
  return error;
643
655
  }
644
- const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
645
656
  if (error && error.code && error.code === MethodNotFound) {
646
657
  const restoredError = new JsonRpcError(error.message);
647
658
  const parentStack = getParentStack(error);
@@ -722,6 +733,17 @@ const getErrorType = prettyError => {
722
733
  }
723
734
  return undefined;
724
735
  };
736
+ const isAlreadyStack = line => {
737
+ return line.trim().startsWith('at ');
738
+ };
739
+ const getStack = prettyError => {
740
+ const stackString = prettyError.stack || '';
741
+ const newLineIndex = stackString.indexOf('\n');
742
+ if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
743
+ return stackString.slice(newLineIndex + 1);
744
+ }
745
+ return stackString;
746
+ };
725
747
  const getErrorProperty = (error, prettyError) => {
726
748
  if (error && error.code === E_COMMAND_NOT_FOUND) {
727
749
  return {
@@ -734,7 +756,7 @@ const getErrorProperty = (error, prettyError) => {
734
756
  code: Custom,
735
757
  message: prettyError.message,
736
758
  data: {
737
- stack: prettyError.stack,
759
+ stack: getStack(prettyError),
738
760
  codeFrame: prettyError.codeFrame,
739
761
  type: getErrorType(prettyError),
740
762
  code: prettyError.code,
@@ -742,18 +764,18 @@ const getErrorProperty = (error, prettyError) => {
742
764
  }
743
765
  };
744
766
  };
745
- const create$1 = (message, error) => {
767
+ const create$1 = (id, error) => {
746
768
  return {
747
769
  jsonrpc: Two,
748
- id: message.id,
770
+ id,
749
771
  error
750
772
  };
751
773
  };
752
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
774
+ const getErrorResponse = (id, error, preparePrettyError, logError) => {
753
775
  const prettyError = preparePrettyError(error);
754
776
  logError(error, prettyError);
755
777
  const errorProperty = getErrorProperty(error, prettyError);
756
- return create$1(message, errorProperty);
778
+ return create$1(id, errorProperty);
757
779
  };
758
780
  const create = (message, result) => {
759
781
  return {
@@ -766,12 +788,27 @@ const getSuccessResponse = (message, result) => {
766
788
  const resultProperty = result ?? null;
767
789
  return create(message, resultProperty);
768
790
  };
791
+ const getErrorResponseSimple = (id, error) => {
792
+ return {
793
+ jsonrpc: Two,
794
+ id,
795
+ error: {
796
+ code: Custom,
797
+ // @ts-ignore
798
+ message: error.message,
799
+ data: error
800
+ }
801
+ };
802
+ };
769
803
  const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
770
804
  try {
771
805
  const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
772
806
  return getSuccessResponse(message, result);
773
807
  } catch (error) {
774
- return getErrorResponse(message, error, preparePrettyError, logError);
808
+ if (ipc.canUseSimpleErrorResponse) {
809
+ return getErrorResponseSimple(message.id, error);
810
+ }
811
+ return getErrorResponse(message.id, error, preparePrettyError, logError);
775
812
  }
776
813
  };
777
814
  const defaultPreparePrettyError = error => {
@@ -826,7 +863,7 @@ const handleJsonRpcMessage = async (...args) => {
826
863
  try {
827
864
  ipc.send(response);
828
865
  } catch (error) {
829
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
866
+ const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
830
867
  ipc.send(errorResponse);
831
868
  }
832
869
  return;
@@ -854,7 +891,7 @@ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
854
891
  return unwrapJsonRpcResult(responseMessage);
855
892
  };
856
893
  const send = (transport, method, ...params) => {
857
- const message = create$4$1(method, params);
894
+ const message = create$4(method, params);
858
895
  transport.send(message);
859
896
  };
860
897
  const invoke$1 = (ipc, method, ...params) => {
@@ -931,24 +968,28 @@ const listen$2 = async (module, options) => {
931
968
  const ipc = module.wrap(rawIpc);
932
969
  return ipc;
933
970
  };
934
- const create$c = async ({
971
+ const create$j = async ({
935
972
  commandMap,
936
- messagePort
973
+ messagePort,
974
+ requiresSocket
937
975
  }) => {
938
976
  // TODO create a commandMap per rpc instance
939
977
  register(commandMap);
940
978
  const ipc = await listen$2(IpcChildWithElectronMessagePort$1, {
941
979
  messagePort
942
980
  });
981
+ if (requiresSocket) {
982
+ ipc.requiresSocket = requiresSocket;
983
+ }
943
984
  handleIpc(ipc);
944
985
  const rpc = createRpc(ipc);
945
986
  return rpc;
946
987
  };
947
988
  const ElectronMessagePortRpcClient = {
948
989
  __proto__: null,
949
- create: create$c
990
+ create: create$j
950
991
  };
951
- const create$b = async ({
992
+ const create$i = async ({
952
993
  commandMap
953
994
  }) => {
954
995
  // TODO create a commandMap per rpc instance
@@ -960,9 +1001,9 @@ const create$b = async ({
960
1001
  };
961
1002
  const ElectronUtilityProcessRpcClient = {
962
1003
  __proto__: null,
963
- create: create$b
1004
+ create: create$i
964
1005
  };
965
- const create$7 = async ({
1006
+ const create$9 = async ({
966
1007
  commandMap
967
1008
  }) => {
968
1009
  // TODO create a commandMap per rpc instance
@@ -974,9 +1015,9 @@ const create$7 = async ({
974
1015
  };
975
1016
  const NodeForkedProcessRpcClient = {
976
1017
  __proto__: null,
977
- create: create$7
1018
+ create: create$9
978
1019
  };
979
- const create$4 = async ({
1020
+ const create$6 = async ({
980
1021
  commandMap
981
1022
  }) => {
982
1023
  // TODO create a commandMap per rpc instance
@@ -988,7 +1029,7 @@ const create$4 = async ({
988
1029
  };
989
1030
  const NodeWorkerRpcClient = {
990
1031
  __proto__: null,
991
- create: create$4
1032
+ create: create$6
992
1033
  };
993
1034
 
994
1035
  const NodeWorker = 1;
@@ -1047,7 +1088,7 @@ const handleElectronMessagePort = async messagePort => {
1047
1088
  const SharedProcess = 1;
1048
1089
 
1049
1090
  const rpcs = Object.create(null);
1050
- const set$3 = (id, rpc) => {
1091
+ const set$g = (id, rpc) => {
1051
1092
  rpcs[id] = rpc;
1052
1093
  };
1053
1094
  const get = id => {
@@ -1094,17 +1135,21 @@ const waitForWatcherToBeReady = async watcher => {
1094
1135
  await promise;
1095
1136
  };
1096
1137
 
1138
+ const callBackInternal = async (eventName, path, stats) => {
1139
+ const event = normalizeEvent(eventName, path);
1140
+ await invoke('FileWatcher.handleChange', event);
1141
+ };
1142
+ const callback = (eventName, path, stats) => {
1143
+ void callBackInternal(eventName, path);
1144
+ };
1145
+ const errorCallback = error => {
1146
+ console.error(`[file-watcher-process] ${error}`);
1147
+ };
1097
1148
  const watchFolder = async uri => {
1098
- const callBackInternal = async (eventName, path, stats) => {
1099
- const event = normalizeEvent(eventName, path);
1100
- await invoke('FileWatcher.handleChange', event);
1101
- };
1102
- const callback = (eventName, path, stats) => {
1103
- void callBackInternal(eventName, path);
1104
- };
1105
1149
  const path = fileURLToPath(uri);
1106
1150
  const watcher = watch(path);
1107
1151
  watcher.on('all', callback);
1152
+ watcher.on('error', errorCallback);
1108
1153
  await waitForWatcherToBeReady(watcher);
1109
1154
  };
1110
1155
 
@@ -1160,10 +1205,11 @@ const listen = async () => {
1160
1205
  method: Auto(),
1161
1206
  commandMap: commandMap
1162
1207
  });
1163
- set$3(SharedProcess, rpc);
1208
+ set$g(SharedProcess, rpc);
1164
1209
  };
1165
1210
 
1166
1211
  const main = async () => {
1212
+ process.on('uncaughtExceptionMonitor', handleUncaughtExceptionMonitor);
1167
1213
  await listen();
1168
1214
  };
1169
1215
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-watcher-process",
3
- "version": "3.1.0",
3
+ "version": "3.3.0",
4
4
  "description": "File Watcher Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",