@lvce-editor/file-system-process 5.6.0 → 5.8.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 (3) hide show
  1. package/README.md +1 -5
  2. package/dist/index.js +102 -45
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,7 +1,3 @@
1
1
  # File System Process
2
2
 
3
- File System Process
4
-
5
- ## Gitpod
6
-
7
- [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/lvce-editor/file-system-process)
3
+ File System Process
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createHash } from 'node:crypto';
1
2
  import { existsSync, constants } from 'node:fs';
2
3
  import * as fs from 'node:fs/promises';
3
4
  import * as os from 'node:os';
@@ -821,7 +822,10 @@ const constructError = (message, type, name) => {
821
822
  if (ErrorConstructor === Error) {
822
823
  const error = new Error(message);
823
824
  if (name && name !== 'VError') {
824
- error.name = name;
825
+ Object.defineProperty(error, 'name', {
826
+ configurable: true,
827
+ value: name
828
+ });
825
829
  }
826
830
  return error;
827
831
  }
@@ -838,8 +842,10 @@ const getCurrentStack = () => {
838
842
  const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
839
843
  return currentStack;
840
844
  };
841
- const getNewLineIndex = (string, startIndex = undefined) => {
842
- return string.indexOf(NewLine, startIndex);
845
+ const getNewLineIndex = (string, startIndex) => {
846
+ {
847
+ return string.indexOf(NewLine);
848
+ }
843
849
  };
844
850
  const getParentStack = error => {
845
851
  let parentStack = error.stack || error.data || error.message || '';
@@ -850,55 +856,91 @@ const getParentStack = error => {
850
856
  };
851
857
  const MethodNotFound = -32601;
852
858
  const Custom = -32001;
859
+ const setStack = (error, stack) => {
860
+ const descriptor = Object.getOwnPropertyDescriptor(error, 'stack');
861
+ if (descriptor) {
862
+ if (!descriptor.configurable && !descriptor.writable) {
863
+ return;
864
+ }
865
+ if (!descriptor.configurable && descriptor.writable) {
866
+ error.stack = stack;
867
+ return;
868
+ }
869
+ }
870
+ Object.defineProperty(error, 'stack', {
871
+ configurable: true,
872
+ value: stack,
873
+ writable: true
874
+ });
875
+ };
876
+ const restoreExistingError = (error, currentStack) => {
877
+ if (typeof error.stack === 'string') {
878
+ setStack(error, `${error.stack}${NewLine}${currentStack}`);
879
+ }
880
+ return error;
881
+ };
882
+ const restoreMethodNotFoundError = (error, currentStack) => {
883
+ const restoredError = new JsonRpcError(error.message);
884
+ const parentStack = getParentStack(error);
885
+ setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
886
+ return restoredError;
887
+ };
888
+ const restoreStackFromData = (restoredError, error, currentStack) => {
889
+ if (error.data.stack && error.data.type && error.message) {
890
+ setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
891
+ return;
892
+ }
893
+ if (error.data.stack) {
894
+ setStack(restoredError, error.data.stack);
895
+ }
896
+ };
897
+ const applyDataProperties = (restoredError, error) => {
898
+ restoreStackFromData(restoredError, error, getCurrentStack());
899
+ if (error.data.codeFrame) {
900
+ // @ts-ignore
901
+ restoredError.codeFrame = error.data.codeFrame;
902
+ }
903
+ if (error.data.code) {
904
+ // @ts-ignore
905
+ restoredError.code = error.data.code;
906
+ }
907
+ if (error.data.type) {
908
+ // @ts-ignore
909
+ restoredError.name = error.data.type;
910
+ }
911
+ };
912
+ const applyDirectProperties = (restoredError, error) => {
913
+ if (error.stack) {
914
+ const lowerStack = restoredError.stack || '';
915
+ const indexNewLine = getNewLineIndex(lowerStack);
916
+ const parentStack = getParentStack(error);
917
+ // @ts-ignore
918
+ setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
919
+ }
920
+ if (error.codeFrame) {
921
+ // @ts-ignore
922
+ restoredError.codeFrame = error.codeFrame;
923
+ }
924
+ };
925
+ const restoreMessageError = (error, _currentStack) => {
926
+ const restoredError = constructError(error.message, error.type, error.name);
927
+ if (error.data) {
928
+ applyDataProperties(restoredError, error);
929
+ } else {
930
+ applyDirectProperties(restoredError, error);
931
+ }
932
+ return restoredError;
933
+ };
853
934
  const restoreJsonRpcError = error => {
854
935
  const currentStack = getCurrentStack();
855
936
  if (error && error instanceof Error) {
856
- if (typeof error.stack === 'string') {
857
- error.stack = error.stack + NewLine + currentStack;
858
- }
859
- return error;
937
+ return restoreExistingError(error, currentStack);
860
938
  }
861
939
  if (error && error.code && error.code === MethodNotFound) {
862
- const restoredError = new JsonRpcError(error.message);
863
- const parentStack = getParentStack(error);
864
- restoredError.stack = parentStack + NewLine + currentStack;
865
- return restoredError;
940
+ return restoreMethodNotFoundError(error, currentStack);
866
941
  }
867
942
  if (error && error.message) {
868
- const restoredError = constructError(error.message, error.type, error.name);
869
- if (error.data) {
870
- if (error.data.stack && error.data.type && error.message) {
871
- restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
872
- } else if (error.data.stack) {
873
- restoredError.stack = error.data.stack;
874
- }
875
- if (error.data.codeFrame) {
876
- // @ts-ignore
877
- restoredError.codeFrame = error.data.codeFrame;
878
- }
879
- if (error.data.code) {
880
- // @ts-ignore
881
- restoredError.code = error.data.code;
882
- }
883
- if (error.data.type) {
884
- // @ts-ignore
885
- restoredError.name = error.data.type;
886
- }
887
- } else {
888
- if (error.stack) {
889
- const lowerStack = restoredError.stack || '';
890
- // @ts-ignore
891
- const indexNewLine = getNewLineIndex(lowerStack);
892
- const parentStack = getParentStack(error);
893
- // @ts-ignore
894
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
895
- }
896
- if (error.codeFrame) {
897
- // @ts-ignore
898
- restoredError.codeFrame = error.codeFrame;
899
- }
900
- }
901
- return restoredError;
943
+ return restoreMessageError(error);
902
944
  }
903
945
  if (typeof error === 'string') {
904
946
  return new Error(`JsonRpc Error: ${error}`);
@@ -1633,6 +1675,20 @@ const readFile = async (uri, encoding = Utf8) => {
1633
1675
  throw new VError(error, `Failed to read file "${uri}"`);
1634
1676
  }
1635
1677
  };
1678
+ const getFileHash = async uri => {
1679
+ try {
1680
+ string(uri);
1681
+ assertUri(uri);
1682
+ const path = fileURLToPath(uri);
1683
+ const content = await fs.readFile(path);
1684
+ return createHash('sha256').update(content).digest('hex');
1685
+ } catch (error) {
1686
+ if (isEnoentError(error)) {
1687
+ throw new FileNotFoundError(uri);
1688
+ }
1689
+ throw new VError(error, `Failed to hash file "${uri}"`);
1690
+ }
1691
+ };
1636
1692
  const writeFile = async (uri, content, encoding = Utf8) => {
1637
1693
  try {
1638
1694
  assertUri(uri);
@@ -1888,6 +1944,7 @@ const commandMap = {
1888
1944
  'FileSystem.chmod': chmod,
1889
1945
  'FileSystem.copy': copy,
1890
1946
  'FileSystem.exists': exists,
1947
+ 'FileSystem.getFileHash': getFileHash,
1891
1948
  'FileSystem.getFolderSize': getFolderSize,
1892
1949
  'FileSystem.getPathSeparator': getPathSeparator,
1893
1950
  'FileSystem.isReadonly': isReadonly,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-system-process",
3
- "version": "5.6.0",
3
+ "version": "5.8.0",
4
4
  "description": "File System Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "trash": "^10.1.1",
22
- "ws": "^8.21.0"
22
+ "ws": "^8.21.1"
23
23
  },
24
24
  "engines": {
25
25
  "node": ">=24"