@lvce-editor/api 8.77.0 → 8.78.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.
@@ -6,9 +6,7 @@ export interface CreateRpcOptions {
6
6
  readonly url: string;
7
7
  }
8
8
  export interface CreateNodeRpcOptions {
9
- readonly id?: string;
10
- readonly name?: string;
11
- readonly path?: string;
9
+ readonly id: string;
12
10
  }
13
11
  export declare const createRpc: ({ commandMap, contentSecurityPolicy, name, url }: CreateRpcOptions) => Promise<Rpc>;
14
- export declare const createNodeRpc: (options: CreateNodeRpcOptions) => Promise<Rpc>;
12
+ export declare const createNodeRpc: ({ id }: CreateNodeRpcOptions) => Promise<Rpc>;
@@ -1,4 +1,4 @@
1
- import { MessagePortRpcParent } from '@lvce-editor/rpc';
1
+ import { MessagePortRpcParent, WebSocketRpcParent } from '@lvce-editor/rpc';
2
2
  import { ExtensionManagementWorker } from '@lvce-editor/rpc-registry';
3
3
  const sendMessagePortToWebWorker = async (port, contentSecurityPolicy, name, url) => {
4
4
  await ExtensionManagementWorker.invokeAndTransfer('Extensions.createWebViewWorkerRpc2', { contentSecurityPolicy, name, url }, port);
@@ -17,25 +17,28 @@ const createMessagePortRpc = async (commandMap, send) => {
17
17
  export const createRpc = async ({ commandMap = {}, contentSecurityPolicy = '', name = '', url }) => {
18
18
  return createMessagePortRpc(commandMap, (port) => sendMessagePortToWebWorker(port, contentSecurityPolicy, name, url));
19
19
  };
20
- const resolveNodeRpcOptions = async ({ id, name = '', path }) => {
21
- if (id) {
22
- return ExtensionManagementWorker.invoke('Extensions.getNodeRpcInfo', id);
20
+ const isMissingCommand = (error, command) => {
21
+ return error instanceof Error && error.message.includes(command) && /command not found|not found/i.test(error.message);
22
+ };
23
+ const getNodeRpcConnection = async (id) => {
24
+ if (!id) {
25
+ throw new TypeError('createNodeRpc requires an id');
26
+ }
27
+ try {
28
+ const connectionInfo = (await ExtensionManagementWorker.invoke('Extensions.createNodeRpcConnection', id));
29
+ return connectionInfo;
23
30
  }
24
- if (path) {
25
- return { name, path };
31
+ catch (error) {
32
+ if (!isMissingCommand(error, 'Extensions.createNodeRpcConnection')) {
33
+ throw error;
34
+ }
35
+ const { name, path } = (await ExtensionManagementWorker.invoke('Extensions.getNodeRpcInfo', id));
36
+ return { name, path, type: 'old-legacy-proxy' };
26
37
  }
27
- throw new TypeError('createNodeRpc requires an id or path');
28
38
  };
29
- export const createNodeRpc = async (options) => {
30
- const { name, path } = await resolveNodeRpcOptions(options);
31
- const rpcId = await ExtensionManagementWorker.invoke('Extensions.executeCommand', 'ExtensionNodeRpc.create', name, path);
32
- const invoke = (method, ...params) => {
33
- return ExtensionManagementWorker.invoke('Extensions.executeCommand', 'ExtensionNodeRpc.invoke', rpcId, method, ...params);
34
- };
39
+ const createProxyRpc = (invoke, dispose) => {
35
40
  return {
36
- async dispose() {
37
- await ExtensionManagementWorker.invoke('Extensions.executeCommand', 'ExtensionNodeRpc.dispose', rpcId);
38
- },
41
+ dispose,
39
42
  invoke,
40
43
  invokeAndTransfer: invoke,
41
44
  send(method, ...params) {
@@ -43,3 +46,29 @@ export const createNodeRpc = async (options) => {
43
46
  },
44
47
  };
45
48
  };
49
+ const createLegacyProxyRpc = async (id) => {
50
+ const rpcId = await ExtensionManagementWorker.invoke('Extensions.createLegacyNodeRpc', id);
51
+ return createProxyRpc((method, ...params) => ExtensionManagementWorker.invoke('Extensions.invokeLegacyNodeRpc', rpcId, method, ...params), () => ExtensionManagementWorker.invoke('Extensions.disposeLegacyNodeRpc', rpcId));
52
+ };
53
+ const createOldLegacyProxyRpc = async (name, path) => {
54
+ const rpcId = await ExtensionManagementWorker.invoke('Extensions.executeCommand', 'ExtensionNodeRpc.create', name, path);
55
+ return createProxyRpc((method, ...params) => ExtensionManagementWorker.invoke('Extensions.executeCommand', 'ExtensionNodeRpc.invoke', rpcId, method, ...params), () => ExtensionManagementWorker.invoke('Extensions.executeCommand', 'ExtensionNodeRpc.dispose', rpcId));
56
+ };
57
+ export const createNodeRpc = async ({ id }) => {
58
+ const connectionInfo = await getNodeRpcConnection(id);
59
+ if (connectionInfo.type === 'message-port') {
60
+ return createMessagePortRpc({}, (port) => ExtensionManagementWorker.invokeAndTransfer('Extensions.createNodeRpcMessagePort', id, port));
61
+ }
62
+ if (connectionInfo.type === 'legacy-proxy') {
63
+ return createLegacyProxyRpc(id);
64
+ }
65
+ if (connectionInfo.type === 'old-legacy-proxy') {
66
+ return createOldLegacyProxyRpc(connectionInfo.name, connectionInfo.path);
67
+ }
68
+ const { protocols, url } = connectionInfo;
69
+ const webSocket = new WebSocket(url, protocols);
70
+ return WebSocketRpcParent.create({
71
+ commandMap: {},
72
+ webSocket,
73
+ });
74
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/api",
3
- "version": "8.77.0",
3
+ "version": "8.78.0",
4
4
  "description": "Tree-shakeable extension API for Lvce Editor extensions.",
5
5
  "keywords": [
6
6
  "lvce-editor",