@lvce-editor/file-system-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.
- package/dist/fileSystemWorkerMain.js +199 -31
- package/package.json +1 -1
|
@@ -368,6 +368,9 @@ const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
|
368
368
|
listen: listen$6,
|
|
369
369
|
wrap: wrap$e
|
|
370
370
|
};
|
|
371
|
+
const Error$3 = 1;
|
|
372
|
+
const Open = 2;
|
|
373
|
+
const Close = 3;
|
|
371
374
|
const addListener = (emitter, type, callback) => {
|
|
372
375
|
if ('addEventListener' in emitter) {
|
|
373
376
|
emitter.addEventListener(type, callback);
|
|
@@ -462,9 +465,69 @@ const IpcParentWithMessagePort$1 = {
|
|
|
462
465
|
signal: signal$1,
|
|
463
466
|
wrap: wrap$5
|
|
464
467
|
};
|
|
468
|
+
const stringifyCompact = value => {
|
|
469
|
+
return JSON.stringify(value);
|
|
470
|
+
};
|
|
471
|
+
const parse = content => {
|
|
472
|
+
if (content === 'undefined') {
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
try {
|
|
476
|
+
return JSON.parse(content);
|
|
477
|
+
} catch (error) {
|
|
478
|
+
throw new VError(error, 'failed to parse json');
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
const waitForWebSocketToBeOpen = webSocket => {
|
|
482
|
+
return getFirstEvent(webSocket, {
|
|
483
|
+
open: Open,
|
|
484
|
+
close: Close,
|
|
485
|
+
error: Error$3
|
|
486
|
+
});
|
|
487
|
+
};
|
|
488
|
+
const create$6 = async ({
|
|
489
|
+
webSocket
|
|
490
|
+
}) => {
|
|
491
|
+
const firstWebSocketEvent = await waitForWebSocketToBeOpen(webSocket);
|
|
492
|
+
if (firstWebSocketEvent.type === Error$3) {
|
|
493
|
+
throw new IpcError(`WebSocket connection error`);
|
|
494
|
+
}
|
|
495
|
+
if (firstWebSocketEvent.type === Close) {
|
|
496
|
+
throw new IpcError('Websocket connection was immediately closed');
|
|
497
|
+
}
|
|
498
|
+
return webSocket;
|
|
499
|
+
};
|
|
500
|
+
class IpcParentWithWebSocket extends Ipc {
|
|
501
|
+
getData(event) {
|
|
502
|
+
return parse(event.data);
|
|
503
|
+
}
|
|
504
|
+
send(message) {
|
|
505
|
+
this._rawIpc.send(stringifyCompact(message));
|
|
506
|
+
}
|
|
507
|
+
sendAndTransfer(message) {
|
|
508
|
+
throw new Error('sendAndTransfer not supported');
|
|
509
|
+
}
|
|
510
|
+
dispose() {
|
|
511
|
+
this._rawIpc.close();
|
|
512
|
+
}
|
|
513
|
+
onClose(callback) {
|
|
514
|
+
this._rawIpc.addEventListener('close', callback);
|
|
515
|
+
}
|
|
516
|
+
onMessage(callback) {
|
|
517
|
+
this._rawIpc.addEventListener('message', callback);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
const wrap = webSocket => {
|
|
521
|
+
return new IpcParentWithWebSocket(webSocket);
|
|
522
|
+
};
|
|
523
|
+
const IpcParentWithWebSocket$1 = {
|
|
524
|
+
__proto__: null,
|
|
525
|
+
create: create$6,
|
|
526
|
+
wrap
|
|
527
|
+
};
|
|
465
528
|
|
|
466
529
|
const Two = '2.0';
|
|
467
|
-
const create$4 = (method, params) => {
|
|
530
|
+
const create$4$1 = (method, params) => {
|
|
468
531
|
return {
|
|
469
532
|
jsonrpc: Two,
|
|
470
533
|
method,
|
|
@@ -478,7 +541,7 @@ const set$2 = (id, fn) => {
|
|
|
478
541
|
const get$1 = id => {
|
|
479
542
|
return callbacks[id];
|
|
480
543
|
};
|
|
481
|
-
const remove = id => {
|
|
544
|
+
const remove$1 = id => {
|
|
482
545
|
delete callbacks[id];
|
|
483
546
|
};
|
|
484
547
|
let id = 0;
|
|
@@ -655,7 +718,7 @@ const resolve = (id, response) => {
|
|
|
655
718
|
return;
|
|
656
719
|
}
|
|
657
720
|
fn(response);
|
|
658
|
-
remove(id);
|
|
721
|
+
remove$1(id);
|
|
659
722
|
};
|
|
660
723
|
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
661
724
|
const getErrorType = prettyError => {
|
|
@@ -799,10 +862,10 @@ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
|
|
|
799
862
|
return unwrapJsonRpcResult(responseMessage);
|
|
800
863
|
};
|
|
801
864
|
const send = (transport, method, ...params) => {
|
|
802
|
-
const message = create$4(method, params);
|
|
865
|
+
const message = create$4$1(method, params);
|
|
803
866
|
transport.send(message);
|
|
804
867
|
};
|
|
805
|
-
const invoke = (ipc, method, ...params) => {
|
|
868
|
+
const invoke$2 = (ipc, method, ...params) => {
|
|
806
869
|
return invokeHelper(ipc, method, params, false);
|
|
807
870
|
};
|
|
808
871
|
const invokeAndTransfer$1 = (ipc, method, ...params) => {
|
|
@@ -835,7 +898,7 @@ const createRpc = ipc => {
|
|
|
835
898
|
send(ipc, method, ...params);
|
|
836
899
|
},
|
|
837
900
|
invoke(method, ...params) {
|
|
838
|
-
return invoke(ipc, method, ...params);
|
|
901
|
+
return invoke$2(ipc, method, ...params);
|
|
839
902
|
},
|
|
840
903
|
invokeAndTransfer(method, ...params) {
|
|
841
904
|
return invokeAndTransfer$1(ipc, method, ...params);
|
|
@@ -906,6 +969,24 @@ const PlainMessagePortRpcParent = {
|
|
|
906
969
|
create: create$2
|
|
907
970
|
};
|
|
908
971
|
const create$1 = async ({
|
|
972
|
+
commandMap,
|
|
973
|
+
webSocket
|
|
974
|
+
}) => {
|
|
975
|
+
// TODO create a commandMap per rpc instance
|
|
976
|
+
register(commandMap);
|
|
977
|
+
const rawIpc = await IpcParentWithWebSocket$1.create({
|
|
978
|
+
webSocket
|
|
979
|
+
});
|
|
980
|
+
const ipc = IpcParentWithWebSocket$1.wrap(rawIpc);
|
|
981
|
+
handleIpc(ipc);
|
|
982
|
+
const rpc = createRpc(ipc);
|
|
983
|
+
return rpc;
|
|
984
|
+
};
|
|
985
|
+
const WebSocketRpcParent = {
|
|
986
|
+
__proto__: null,
|
|
987
|
+
create: create$1
|
|
988
|
+
};
|
|
989
|
+
const create$4 = async ({
|
|
909
990
|
commandMap
|
|
910
991
|
}) => {
|
|
911
992
|
// TODO create a commandMap per rpc instance
|
|
@@ -917,18 +998,7 @@ const create$1 = async ({
|
|
|
917
998
|
};
|
|
918
999
|
const WebWorkerRpcClient = {
|
|
919
1000
|
__proto__: null,
|
|
920
|
-
create: create$
|
|
921
|
-
};
|
|
922
|
-
|
|
923
|
-
const getPortTuple = () => {
|
|
924
|
-
const {
|
|
925
|
-
port1,
|
|
926
|
-
port2
|
|
927
|
-
} = new MessageChannel();
|
|
928
|
-
return {
|
|
929
|
-
port1,
|
|
930
|
-
port2
|
|
931
|
-
};
|
|
1001
|
+
create: create$4
|
|
932
1002
|
};
|
|
933
1003
|
|
|
934
1004
|
const rpcs = Object.create(null);
|
|
@@ -963,26 +1033,88 @@ const create = rpcId => {
|
|
|
963
1033
|
const RendererWorker$1 = 1;
|
|
964
1034
|
const FileSystemProcess$1 = 210;
|
|
965
1035
|
const {
|
|
966
|
-
invokeAndTransfer: invokeAndTransfer$3
|
|
1036
|
+
invokeAndTransfer: invokeAndTransfer$3,
|
|
1037
|
+
set: set$3
|
|
1038
|
+
} = create(RendererWorker$1);
|
|
967
1039
|
const RendererWorker = {
|
|
968
1040
|
__proto__: null,
|
|
969
|
-
invokeAndTransfer: invokeAndTransfer$3
|
|
1041
|
+
invokeAndTransfer: invokeAndTransfer$3,
|
|
1042
|
+
set: set$3
|
|
1043
|
+
};
|
|
970
1044
|
const {
|
|
971
|
-
|
|
1045
|
+
invoke: invoke$1,
|
|
1046
|
+
set: set$1$1
|
|
972
1047
|
} = create(FileSystemProcess$1);
|
|
973
1048
|
const FileSystemProcess = {
|
|
974
1049
|
__proto__: null,
|
|
1050
|
+
invoke: invoke$1,
|
|
1051
|
+
set: set$1$1
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
const {
|
|
1055
|
+
invoke,
|
|
975
1056
|
set: set$1
|
|
1057
|
+
} = FileSystemProcess;
|
|
1058
|
+
|
|
1059
|
+
const remove = async dirent => {
|
|
1060
|
+
return invoke('FileSystem.remove', dirent);
|
|
1061
|
+
};
|
|
1062
|
+
const readFile = async uri => {
|
|
1063
|
+
return invoke('FileSystem.readFile', uri);
|
|
1064
|
+
};
|
|
1065
|
+
const readDirWithFileTypes = async uri => {
|
|
1066
|
+
return invoke('FileSystem.readDirWithFileTypes', uri);
|
|
1067
|
+
};
|
|
1068
|
+
const getPathSeparator = async root => {
|
|
1069
|
+
// @ts-ignore
|
|
1070
|
+
return invoke('FileSystem.getPathSeparator', root);
|
|
1071
|
+
};
|
|
1072
|
+
const readJson = async uri => {
|
|
1073
|
+
// @ts-ignore
|
|
1074
|
+
return invoke('FileSystem.readJson', uri);
|
|
1075
|
+
};
|
|
1076
|
+
const getRealPath = async path => {
|
|
1077
|
+
return invoke('FileSystem.getRealPath', path);
|
|
1078
|
+
};
|
|
1079
|
+
const stat = async dirent => {
|
|
1080
|
+
return invoke('FileSystem.stat', dirent);
|
|
1081
|
+
};
|
|
1082
|
+
const createFile = async uri => {
|
|
1083
|
+
return invoke('FileSystem.writeFile', uri, '');
|
|
1084
|
+
};
|
|
1085
|
+
const writeFile = async (uri, content) => {
|
|
1086
|
+
return invoke('FileSystem.writeFile', uri, content);
|
|
1087
|
+
};
|
|
1088
|
+
const mkdir = async uri => {
|
|
1089
|
+
return invoke('FileSystem.mkdir', uri);
|
|
1090
|
+
};
|
|
1091
|
+
const rename = async (oldUri, newUri) => {
|
|
1092
|
+
return invoke('FileSystem.rename', oldUri, newUri);
|
|
1093
|
+
};
|
|
1094
|
+
const copy = async (oldUri, newUri) => {
|
|
1095
|
+
return invoke('FileSystem.copy', oldUri, newUri);
|
|
1096
|
+
};
|
|
1097
|
+
|
|
1098
|
+
const getPortTuple = () => {
|
|
1099
|
+
const {
|
|
1100
|
+
port1,
|
|
1101
|
+
port2
|
|
1102
|
+
} = new MessageChannel();
|
|
1103
|
+
return {
|
|
1104
|
+
port1,
|
|
1105
|
+
port2
|
|
1106
|
+
};
|
|
976
1107
|
};
|
|
977
1108
|
|
|
978
1109
|
const {
|
|
979
|
-
invokeAndTransfer
|
|
1110
|
+
invokeAndTransfer,
|
|
1111
|
+
set
|
|
1112
|
+
} = RendererWorker;
|
|
980
1113
|
|
|
981
1114
|
const FileSystemWorker = 209;
|
|
982
1115
|
|
|
983
1116
|
const sendMessagePortToFileSystemProcess = async port => {
|
|
984
1117
|
const command = 'HandleMessagePort.handleMessagePort2';
|
|
985
|
-
// @ts-ignore
|
|
986
1118
|
await invokeAndTransfer('SendMessagePortToFileSystemProcess.sendMessagePortToFileSystemProcess', port, command, FileSystemWorker);
|
|
987
1119
|
};
|
|
988
1120
|
|
|
@@ -1003,10 +1135,37 @@ const createFileSystemProcessRpcElectron = async () => {
|
|
|
1003
1135
|
}
|
|
1004
1136
|
};
|
|
1005
1137
|
|
|
1138
|
+
const Https = 'https:';
|
|
1139
|
+
const Ws = 'ws:';
|
|
1140
|
+
const Wss = 'wss:';
|
|
1141
|
+
|
|
1142
|
+
const getWebSocketProtocol = locationProtocol => {
|
|
1143
|
+
return locationProtocol === Https ? Wss : Ws;
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
const getWebSocketUrl = (type, host, locationProtocol) => {
|
|
1147
|
+
const wsProtocol = getWebSocketProtocol(locationProtocol);
|
|
1148
|
+
return `${wsProtocol}//${host}/websocket/${type}`;
|
|
1149
|
+
};
|
|
1150
|
+
|
|
1151
|
+
const getHost = () => {
|
|
1152
|
+
return location.host;
|
|
1153
|
+
};
|
|
1154
|
+
const getProtocol = () => {
|
|
1155
|
+
return location.protocol;
|
|
1156
|
+
};
|
|
1157
|
+
|
|
1006
1158
|
const createFileSystemProcessRpcNode = async () => {
|
|
1007
1159
|
try {
|
|
1008
|
-
|
|
1009
|
-
|
|
1160
|
+
const host = getHost();
|
|
1161
|
+
const protocol = getProtocol();
|
|
1162
|
+
const wsUrl = getWebSocketUrl('file-system-process', host, protocol);
|
|
1163
|
+
const webSocket = new WebSocket(wsUrl);
|
|
1164
|
+
const rpc = await WebSocketRpcParent.create({
|
|
1165
|
+
webSocket,
|
|
1166
|
+
commandMap: {}
|
|
1167
|
+
});
|
|
1168
|
+
return rpc;
|
|
1010
1169
|
} catch (error) {
|
|
1011
1170
|
throw new VError(error, `Failed to create file system process rpc`);
|
|
1012
1171
|
}
|
|
@@ -1026,23 +1185,32 @@ const createFileSystemProcessRpc = async platform => {
|
|
|
1026
1185
|
}
|
|
1027
1186
|
};
|
|
1028
1187
|
|
|
1029
|
-
const {
|
|
1030
|
-
set
|
|
1031
|
-
} = FileSystemProcess;
|
|
1032
|
-
|
|
1033
1188
|
const initialize = async platform => {
|
|
1034
1189
|
const rpc = await createFileSystemProcessRpc(platform);
|
|
1035
|
-
set(rpc);
|
|
1190
|
+
set$1(rpc);
|
|
1036
1191
|
};
|
|
1037
1192
|
|
|
1038
1193
|
const commandMap = {
|
|
1194
|
+
'FileSystem.copy': copy,
|
|
1195
|
+
'FileSystem.createFile': createFile,
|
|
1196
|
+
'FileSystem.getPathSeparator': getPathSeparator,
|
|
1197
|
+
'FileSystem.getRealPath': getRealPath,
|
|
1198
|
+
'FileSystem.mkdir': mkdir,
|
|
1199
|
+
'FileSystem.readDirWithFileTypes': readDirWithFileTypes,
|
|
1200
|
+
'FileSystem.readFile': readFile,
|
|
1201
|
+
'FileSystem.readJson': readJson,
|
|
1202
|
+
'FileSystem.remove': remove,
|
|
1203
|
+
'FileSystem.rename': rename,
|
|
1204
|
+
'FileSystem.stat': stat,
|
|
1205
|
+
'FileSystem.writeFile': writeFile,
|
|
1039
1206
|
'Initialize.initialize': initialize
|
|
1040
1207
|
};
|
|
1041
1208
|
|
|
1042
1209
|
const listen = async () => {
|
|
1043
|
-
await WebWorkerRpcClient.create({
|
|
1210
|
+
const rpc = await WebWorkerRpcClient.create({
|
|
1044
1211
|
commandMap: commandMap
|
|
1045
1212
|
});
|
|
1213
|
+
set(rpc);
|
|
1046
1214
|
};
|
|
1047
1215
|
|
|
1048
1216
|
const main = async () => {
|