@lvce-editor/preview-sandbox-worker 1.6.0 → 1.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.
@@ -506,6 +506,27 @@ const IpcParentWithMessagePort$1 = {
506
506
  wrap: wrap$5
507
507
  };
508
508
 
509
+ class CommandNotFoundError extends Error {
510
+ constructor(command) {
511
+ super(`Command not found ${command}`);
512
+ this.name = 'CommandNotFoundError';
513
+ }
514
+ }
515
+ const commands = Object.create(null);
516
+ const register = commandMap => {
517
+ Object.assign(commands, commandMap);
518
+ };
519
+ const getCommand = key => {
520
+ return commands[key];
521
+ };
522
+ const execute = (command, ...args) => {
523
+ const fn = getCommand(command);
524
+ if (!fn) {
525
+ throw new CommandNotFoundError(command);
526
+ }
527
+ return fn(...args);
528
+ };
529
+
509
530
  const Two$1 = '2.0';
510
531
  const callbacks$1 = Object.create(null);
511
532
  const get$2 = id => {
@@ -530,12 +551,12 @@ const getErrorConstructor = (message, type) => {
530
551
  switch (type) {
531
552
  case DomException:
532
553
  return DOMException;
533
- case TypeError$1:
534
- return TypeError;
535
- case SyntaxError$1:
536
- return SyntaxError;
537
554
  case ReferenceError$1:
538
555
  return ReferenceError;
556
+ case SyntaxError$1:
557
+ return SyntaxError;
558
+ case TypeError$1:
559
+ return TypeError;
539
560
  default:
540
561
  return Error;
541
562
  }
@@ -691,56 +712,56 @@ const getErrorProperty = (error, prettyError) => {
691
712
  if (error && error.code === E_COMMAND_NOT_FOUND) {
692
713
  return {
693
714
  code: MethodNotFound,
694
- message: error.message,
695
- data: error.stack
715
+ data: error.stack,
716
+ message: error.message
696
717
  };
697
718
  }
698
719
  return {
699
720
  code: Custom,
700
- message: prettyError.message,
701
721
  data: {
702
- stack: getStack(prettyError),
703
- codeFrame: prettyError.codeFrame,
704
- type: getErrorType(prettyError),
705
722
  code: prettyError.code,
706
- name: prettyError.name
707
- }
723
+ codeFrame: prettyError.codeFrame,
724
+ name: prettyError.name,
725
+ stack: getStack(prettyError),
726
+ type: getErrorType(prettyError)
727
+ },
728
+ message: prettyError.message
708
729
  };
709
730
  };
710
- const create$1 = (id, error) => {
731
+ const create$1$1 = (id, error) => {
711
732
  return {
712
- jsonrpc: Two$1,
733
+ error,
713
734
  id,
714
- error
735
+ jsonrpc: Two$1
715
736
  };
716
737
  };
717
738
  const getErrorResponse = (id, error, preparePrettyError, logError) => {
718
739
  const prettyError = preparePrettyError(error);
719
740
  logError(error, prettyError);
720
741
  const errorProperty = getErrorProperty(error, prettyError);
721
- return create$1(id, errorProperty);
742
+ return create$1$1(id, errorProperty);
722
743
  };
723
- const create$3 = (message, result) => {
744
+ const create$6 = (message, result) => {
724
745
  return {
725
- jsonrpc: Two$1,
726
746
  id: message.id,
747
+ jsonrpc: Two$1,
727
748
  result: result ?? null
728
749
  };
729
750
  };
730
751
  const getSuccessResponse = (message, result) => {
731
752
  const resultProperty = result ?? null;
732
- return create$3(message, resultProperty);
753
+ return create$6(message, resultProperty);
733
754
  };
734
755
  const getErrorResponseSimple = (id, error) => {
735
756
  return {
736
- jsonrpc: Two$1,
737
- id,
738
757
  error: {
739
758
  code: Custom,
759
+ data: error,
740
760
  // @ts-ignore
741
- message: error.message,
742
- data: error
743
- }
761
+ message: error.message
762
+ },
763
+ id,
764
+ jsonrpc: Two$1
744
765
  };
745
766
  };
746
767
  const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
@@ -770,35 +791,35 @@ const normalizeParams = args => {
770
791
  if (args.length === 1) {
771
792
  const options = args[0];
772
793
  return {
794
+ execute: options.execute,
773
795
  ipc: options.ipc,
796
+ logError: options.logError || defaultLogError,
774
797
  message: options.message,
775
- execute: options.execute,
776
- resolve: options.resolve || defaultResolve,
777
798
  preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
778
- logError: options.logError || defaultLogError,
779
- requiresSocket: options.requiresSocket || defaultRequiresSocket
799
+ requiresSocket: options.requiresSocket || defaultRequiresSocket,
800
+ resolve: options.resolve || defaultResolve
780
801
  };
781
802
  }
782
803
  return {
804
+ execute: args[2],
783
805
  ipc: args[0],
806
+ logError: args[5],
784
807
  message: args[1],
785
- execute: args[2],
786
- resolve: args[3],
787
808
  preparePrettyError: args[4],
788
- logError: args[5],
789
- requiresSocket: args[6]
809
+ requiresSocket: args[6],
810
+ resolve: args[3]
790
811
  };
791
812
  };
792
813
  const handleJsonRpcMessage = async (...args) => {
793
814
  const options = normalizeParams(args);
794
815
  const {
795
- message,
796
- ipc,
797
816
  execute,
798
- resolve,
799
- preparePrettyError,
817
+ ipc,
800
818
  logError,
801
- requiresSocket
819
+ message,
820
+ preparePrettyError,
821
+ requiresSocket,
822
+ resolve
802
823
  } = options;
803
824
  if ('id' in message) {
804
825
  if ('method' in message) {
@@ -821,36 +842,17 @@ const handleJsonRpcMessage = async (...args) => {
821
842
  throw new JsonRpcError('unexpected message');
822
843
  };
823
844
 
824
- class CommandNotFoundError extends Error {
825
- constructor(command) {
826
- super(`Command not found ${command}`);
827
- this.name = 'CommandNotFoundError';
828
- }
829
- }
830
- const commands = Object.create(null);
831
- const register = commandMap => {
832
- Object.assign(commands, commandMap);
833
- };
834
- const getCommand = key => {
835
- return commands[key];
836
- };
837
- const execute = (command, ...args) => {
838
- const fn = getCommand(command);
839
- if (!fn) {
840
- throw new CommandNotFoundError(command);
841
- }
842
- return fn(...args);
843
- };
844
-
845
845
  const Two = '2.0';
846
- const create$t = (method, params) => {
846
+
847
+ const create$5 = (method, params) => {
847
848
  return {
848
849
  jsonrpc: Two,
849
850
  method,
850
851
  params
851
852
  };
852
853
  };
853
- const create$s = (id, method, params) => {
854
+
855
+ const create$4 = (id, method, params) => {
854
856
  const message = {
855
857
  id,
856
858
  jsonrpc: Two,
@@ -859,15 +861,14 @@ const create$s = (id, method, params) => {
859
861
  };
860
862
  return message;
861
863
  };
864
+
862
865
  let id$1 = 0;
863
- const create$r = () => {
866
+ const create$3 = () => {
864
867
  return ++id$1;
865
868
  };
866
869
 
867
- /* eslint-disable n/no-unsupported-features/es-syntax */
868
-
869
870
  const registerPromise = map => {
870
- const id = create$r();
871
+ const id = create$3();
871
872
  const {
872
873
  promise,
873
874
  resolve
@@ -879,13 +880,12 @@ const registerPromise = map => {
879
880
  };
880
881
  };
881
882
 
882
- // @ts-ignore
883
883
  const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
884
884
  const {
885
885
  id,
886
886
  promise
887
887
  } = registerPromise(callbacks);
888
- const message = create$s(id, method, params);
888
+ const message = create$4(id, method, params);
889
889
  if (useSendAndTransfer && ipc.sendAndTransfer) {
890
890
  ipc.sendAndTransfer(message);
891
891
  } else {
@@ -921,12 +921,13 @@ const createRpc = ipc => {
921
921
  * @deprecated
922
922
  */
923
923
  send(method, ...params) {
924
- const message = create$t(method, params);
924
+ const message = create$5(method, params);
925
925
  ipc.send(message);
926
926
  }
927
927
  };
928
928
  return rpc;
929
929
  };
930
+
930
931
  const requiresSocket = () => {
931
932
  return false;
932
933
  };
@@ -941,6 +942,7 @@ const handleMessage = event => {
941
942
  const actualExecute = event?.target?.execute || execute;
942
943
  return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
943
944
  };
945
+
944
946
  const handleIpc = ipc => {
945
947
  if ('addEventListener' in ipc) {
946
948
  ipc.addEventListener('message', handleMessage);
@@ -949,6 +951,7 @@ const handleIpc = ipc => {
949
951
  ipc.on('message', handleMessage);
950
952
  }
951
953
  };
954
+
952
955
  const listen$1 = async (module, options) => {
953
956
  const rawIpc = await module.listen(options);
954
957
  if (module.signal) {
@@ -957,7 +960,8 @@ const listen$1 = async (module, options) => {
957
960
  const ipc = module.wrap(rawIpc);
958
961
  return ipc;
959
962
  };
960
- const create$5 = async ({
963
+
964
+ const create$2 = async ({
961
965
  commandMap,
962
966
  isMessagePortOpen = true,
963
967
  messagePort
@@ -974,11 +978,8 @@ const create$5 = async ({
974
978
  messagePort.start();
975
979
  return rpc;
976
980
  };
977
- const PlainMessagePortRpc = {
978
- __proto__: null,
979
- create: create$5
980
- };
981
- const create$2 = async ({
981
+
982
+ const create$1 = async ({
982
983
  commandMap
983
984
  }) => {
984
985
  // TODO create a commandMap per rpc instance
@@ -988,10 +989,9 @@ const create$2 = async ({
988
989
  const rpc = createRpc(ipc);
989
990
  return rpc;
990
991
  };
991
- const WebWorkerRpcClient = {
992
- __proto__: null,
993
- create: create$2
994
- };
992
+
993
+ const RendererWorker = 1;
994
+
995
995
  const createMockRpc = ({
996
996
  commandMap
997
997
  }) => {
@@ -1012,63 +1012,6 @@ const createMockRpc = ({
1012
1012
  return mockRpc;
1013
1013
  };
1014
1014
 
1015
- const Button$1 = 1;
1016
- const Div$1 = 4;
1017
- const H1$1 = 5;
1018
- const Input$1 = 6;
1019
- const Span$1 = 8;
1020
- const Table$1 = 9;
1021
- const TBody$1 = 10;
1022
- const Td$1 = 11;
1023
- const Text = 12;
1024
- const Th$1 = 13;
1025
- const THead$1 = 14;
1026
- const Tr$1 = 15;
1027
- const Img$1 = 17;
1028
- const Del$1 = 21;
1029
- const H2$1 = 22;
1030
- const H3$1 = 23;
1031
- const H4$1 = 24;
1032
- const H5$1 = 25;
1033
- const H6$1 = 26;
1034
- const Article$1 = 27;
1035
- const Aside$1 = 28;
1036
- const Footer$1 = 29;
1037
- const Header$1 = 30;
1038
- const Nav$1 = 40;
1039
- const Section$1 = 41;
1040
- const Search$1 = 42;
1041
- const Dd$1 = 43;
1042
- const Dl$1 = 44;
1043
- const Figcaption$1 = 45;
1044
- const Figure$1 = 46;
1045
- const Hr$1 = 47;
1046
- const Li$1 = 48;
1047
- const Ol$1 = 49;
1048
- const P$1 = 50;
1049
- const Pre$1 = 51;
1050
- const A$1 = 53;
1051
- const Abbr$1 = 54;
1052
- const Br$1 = 55;
1053
- const Cite$1 = 56;
1054
- const Data$1 = 57;
1055
- const Time$1 = 58;
1056
- const Tfoot$1 = 59;
1057
- const Ul$1 = 60;
1058
- const TextArea$1 = 62;
1059
- const Select$1 = 63;
1060
- const Option$1 = 64;
1061
- const Code$1 = 65;
1062
- const Label$1 = 66;
1063
- const Dt$1 = 67;
1064
- const Iframe$1 = 68;
1065
- const Style = 72;
1066
- const Html = 73;
1067
- const Canvas$1 = 77;
1068
- const Reference = 100;
1069
-
1070
- const RendererWorker = 1;
1071
-
1072
1015
  const rpcs = Object.create(null);
1073
1016
  const set$4 = (id, rpc) => {
1074
1017
  rpcs[id] = rpc;
@@ -1180,6 +1123,62 @@ const set$1 = (uid, instance) => {
1180
1123
  states$1.set(uid, instance);
1181
1124
  };
1182
1125
 
1126
+ const Button$1 = 1;
1127
+ const Div$1 = 4;
1128
+ const H1$1 = 5;
1129
+ const Input$1 = 6;
1130
+ const Span$1 = 8;
1131
+ const Table$1 = 9;
1132
+ const TBody$1 = 10;
1133
+ const Td$1 = 11;
1134
+ const Th$1 = 13;
1135
+ const THead$1 = 14;
1136
+ const Tr$1 = 15;
1137
+ const Img$1 = 17;
1138
+ const Del$1 = 21;
1139
+ const H2$1 = 22;
1140
+ const H3$1 = 23;
1141
+ const H4$1 = 24;
1142
+ const H5$1 = 25;
1143
+ const H6$1 = 26;
1144
+ const Article$1 = 27;
1145
+ const Aside$1 = 28;
1146
+ const Footer$1 = 29;
1147
+ const Header$1 = 30;
1148
+ const Nav$1 = 40;
1149
+ const Section$1 = 41;
1150
+ const Search$1 = 42;
1151
+ const Dd$1 = 43;
1152
+ const Dl$1 = 44;
1153
+ const Figcaption$1 = 45;
1154
+ const Figure$1 = 46;
1155
+ const Hr$1 = 47;
1156
+ const Li$1 = 48;
1157
+ const Ol$1 = 49;
1158
+ const P$1 = 50;
1159
+ const Pre$1 = 51;
1160
+ const A$1 = 53;
1161
+ const Abbr$1 = 54;
1162
+ const Br$1 = 55;
1163
+ const Cite$1 = 56;
1164
+ const Data$1 = 57;
1165
+ const Time$1 = 58;
1166
+ const Tfoot$1 = 59;
1167
+ const Ul$1 = 60;
1168
+ const TextArea$1 = 62;
1169
+ const Select$1 = 63;
1170
+ const Option$1 = 64;
1171
+ const Code$1 = 65;
1172
+ const Label$1 = 66;
1173
+ const Dt$1 = 67;
1174
+ const Iframe$1 = 68;
1175
+ const Style = 72;
1176
+ const Html = 73;
1177
+ const Canvas$1 = 77;
1178
+ const Reference = 100;
1179
+
1180
+ const Text = 12;
1181
+
1183
1182
  const text = data => {
1184
1183
  return {
1185
1184
  childCount: 0,
@@ -1455,7 +1454,7 @@ const serializeNode = (node, dom, css, context) => {
1455
1454
  uid: node.__canvasId
1456
1455
  };
1457
1456
  if (context.elementMap) {
1458
- context.elementMap.set(node.__canvasId + '', node);
1457
+ context.elementMap[node.__canvasId + ''] = node;
1459
1458
  }
1460
1459
  dom.push(refNode);
1461
1460
  return 1;
@@ -1490,7 +1489,7 @@ const serializeNode = (node, dom, css, context) => {
1490
1489
  // Assign element tracking ID for interactivity
1491
1490
  const hdId = String(context.nextId++);
1492
1491
  newNode['data-id'] = hdId;
1493
- context.elementMap.set(hdId, node)
1492
+ context.elementMap[hdId] = node
1494
1493
 
1495
1494
  // Reserve position in dom array for this node
1496
1495
  ;
@@ -1509,7 +1508,7 @@ const serializeNode = (node, dom, css, context) => {
1509
1508
  };
1510
1509
 
1511
1510
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1512
- const serialize = (document, elementMap = new Map()) => {
1511
+ const serialize = (document, elementMap = Object.create(null)) => {
1513
1512
  const dom = [];
1514
1513
  const css = [];
1515
1514
  const context = {
@@ -1595,7 +1594,7 @@ const handleClickLocal = (uid, hdId, clientX, clientY) => {
1595
1594
  return;
1596
1595
  }
1597
1596
  dispatchClickEvent(element, happyDomInstance.window, clientX, clientY);
1598
- const elementMap = new Map();
1597
+ const elementMap = Object.create(null);
1599
1598
  set$1(uid, {
1600
1599
  document: happyDomInstance.document,
1601
1600
  elementMap,
@@ -1624,7 +1623,7 @@ const handleInputLocal = (uid, hdId, value) => {
1624
1623
  }
1625
1624
  element.value = value;
1626
1625
  dispatchInputEvent(element, happyDomInstance.window);
1627
- const elementMap = new Map();
1626
+ const elementMap = Object.create(null);
1628
1627
  set$1(uid, {
1629
1628
  document: happyDomInstance.document,
1630
1629
  elementMap,
@@ -1654,7 +1653,7 @@ const handleKeydownLocal = (uid, hdId, key, code) => {
1654
1653
  return;
1655
1654
  }
1656
1655
  dispatchKeydownEvent(element, happyDomInstance.window, key, code);
1657
- const elementMap = new Map();
1656
+ const elementMap = Object.create(null);
1658
1657
  set$1(uid, {
1659
1658
  document: happyDomInstance.document,
1660
1659
  elementMap,
@@ -1684,7 +1683,7 @@ const handleKeyupLocal = (uid, hdId, key, code) => {
1684
1683
  return;
1685
1684
  }
1686
1685
  dispatchKeyupEvent(element, happyDomInstance.window, key, code);
1687
- const elementMap = new Map();
1686
+ const elementMap = Object.create(null);
1688
1687
  set$1(uid, {
1689
1688
  document: happyDomInstance.document,
1690
1689
  elementMap,
@@ -1696,7 +1695,7 @@ const handleKeyup = (uid, hdId, key, code) => {
1696
1695
  };
1697
1696
 
1698
1697
  const handleMessagePort = async port => {
1699
- const rpc = await PlainMessagePortRpc.create({
1698
+ const rpc = await create$2({
1700
1699
  commandMap: {},
1701
1700
  messagePort: port
1702
1701
  });
@@ -1722,7 +1721,7 @@ const handleMousedownLocal = (uid, hdId, clientX, clientY) => {
1722
1721
  return;
1723
1722
  }
1724
1723
  dispatchMousedownEvent(element, happyDomInstance.window, clientX, clientY);
1725
- const elementMap = new Map();
1724
+ const elementMap = Object.create(null);
1726
1725
  set$1(uid, {
1727
1726
  document: happyDomInstance.document,
1728
1727
  elementMap,
@@ -1733,6 +1732,8 @@ const handleMousedown = (uid, hdId, clientX, clientY) => {
1733
1732
  return handleMousedownLocal(uid, hdId, clientX, clientY);
1734
1733
  };
1735
1734
 
1735
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1736
+
1736
1737
  const dispatchMousemoveEvent = (element, window, clientX = 0, clientY = 0) => {
1737
1738
  const mousemoveEvent = new window.MouseEvent('mousemove', {
1738
1739
  bubbles: true,
@@ -1754,7 +1755,7 @@ const handleMousemoveLocal = (uid, hdId, clientX, clientY, x, y) => {
1754
1755
  const adjustedClientX = clientX - x;
1755
1756
  const adjustedClientY = clientY - y;
1756
1757
  dispatchMousemoveEvent(element, happyDomInstance.window, adjustedClientX, adjustedClientY);
1757
- const elementMap = new Map();
1758
+ const elementMap = Object.create(null);
1758
1759
  set$1(uid, {
1759
1760
  document: happyDomInstance.document,
1760
1761
  elementMap,
@@ -1784,7 +1785,7 @@ const handleMouseupLocal = (uid, hdId, clientX, clientY) => {
1784
1785
  return;
1785
1786
  }
1786
1787
  dispatchMouseupEvent(element, happyDomInstance.window, clientX, clientY);
1787
- const elementMap = new Map();
1788
+ const elementMap = Object.create(null);
1788
1789
  set$1(uid, {
1789
1790
  document: happyDomInstance.document,
1790
1791
  elementMap,
@@ -89360,15 +89361,13 @@ class Window extends BrowserWindow {
89360
89361
  }
89361
89362
  }
89362
89363
 
89363
- const createWindow = rawHtml => {
89364
+ const createWindow = (rawHtml, url = 'http://localhost:3000') => {
89364
89365
  const window = new Window({
89365
- url: 'https://localhost:3000'
89366
+ url
89366
89367
  });
89367
89368
  const {
89368
89369
  document
89369
89370
  } = window;
89370
-
89371
- // Parse the raw HTML into the happy-dom document
89372
89371
  document.documentElement.innerHTML = rawHtml;
89373
89372
  return {
89374
89373
  document,
@@ -89670,7 +89669,7 @@ const handleMutations = async uid => {
89670
89669
  if (!happyDomInstance) {
89671
89670
  return;
89672
89671
  }
89673
- const elementMap = new Map();
89672
+ const elementMap = Object.create(null);
89674
89673
  const serialized = serialize(happyDomInstance.document, elementMap);
89675
89674
  set$1(uid, {
89676
89675
  document: happyDomInstance.document,
@@ -89813,7 +89812,7 @@ const updateContent = async (uid, width, height, content, scripts) => {
89813
89812
  codeFrame,
89814
89813
  error
89815
89814
  } = executeScripts(happyDomWindow, happyDomDocument, scripts, width, height);
89816
- const elementMap = new Map();
89815
+ const elementMap = Object.create(null);
89817
89816
  set$1(uid, {
89818
89817
  document: happyDomDocument,
89819
89818
  elementMap,
@@ -89879,7 +89878,7 @@ const commandMap = {
89879
89878
  };
89880
89879
 
89881
89880
  const listen = async () => {
89882
- const rpc = await WebWorkerRpcClient.create({
89881
+ const rpc = await create$1({
89883
89882
  commandMap: commandMap
89884
89883
  });
89885
89884
  set$2(rpc);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/preview-sandbox-worker",
3
- "version": "1.6.0",
3
+ "version": "1.8.0",
4
4
  "description": "Preview SandBox Worker",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,7 +11,7 @@
11
11
  "type": "module",
12
12
  "main": "dist/previewSandBoxWorkerMain.js",
13
13
  "dependencies": {
14
- "@lvce-editor/virtual-dom-worker": "^8.3.0",
14
+ "@lvce-editor/virtual-dom-worker": "^8.9.0",
15
15
  "happy-dom-without-node": "^14.12.3"
16
16
  }
17
17
  }