Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

chore(node-sdk): add computing process

Wenzhao Hu7ed42126154f
diff --git a/examples-node/package.json b/examples-node/package.json
index 235c8a59a9..8a6bc370b5 100644
--- a/examples-node/package.json
+++ b/examples-node/package.json
@@ -17,6 +17,7 @@
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/rpc": "workspace:*",
+ "@univerjs/rpc-node": "workspace:*",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-conditional-formatting": "workspace:*",
"@univerjs/sheets-data-validation": "workspace:*",
diff --git a/examples-node/src/sdk/index.ts b/examples-node/src/sdk/index.ts
index e462d3459a..5f01c61683 100644
--- a/examples-node/src/sdk/index.ts
+++ b/examples-node/src/sdk/index.ts
@@ -14,12 +14,17 @@
* limitations under the License.
*/
+import type { IMessageProtocol } from '@univerjs/rpc';
+import type { Serializable } from 'node:child_process';
+import { fork } from 'node:child_process';
+import path from 'node:path';
import { Univer } from '@univerjs/core';
import { UniverDataValidationPlugin } from '@univerjs/data-validation';
import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsDrawingPlugin } from '@univerjs/docs-drawing';
import { UniverDrawingPlugin } from '@univerjs/drawing';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
+import { UniverRPCNodeMainPlugin } from '@univerjs/rpc-node';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { UniverSheetsConditionalFormattingPlugin } from '@univerjs/sheets-conditional-formatting';
import { UniverSheetsDataValidationPlugin } from '@univerjs/sheets-data-validation';
@@ -28,12 +33,15 @@ import { UniverSheetsFilterPlugin } from '@univerjs/sheets-filter';
import { UniverSheetsHyperLinkPlugin } from '@univerjs/sheets-hyper-link';
import { UniverSheetsSortPlugin } from '@univerjs/sheets-sort';
import { UniverThreadCommentPlugin } from '@univerjs/thread-comment';
+import { Observable, shareReplay } from 'rxjs';
export function createUniverOnNode(): Univer {
const univer = new Univer();
registerBasicPlugins(univer);
reigsterSharedPlugins(univer);
+ registerRPCPlugin(univer);
+
registerDocPlugins(univer);
registerSheetPlugins(univer);
@@ -64,3 +72,22 @@ function registerSheetPlugins(univer: Univer): void {
univer.registerPlugin(UniverSheetsDrawingPlugin);
univer.registerPlugin(UniverSheetsSortPlugin);
}
+
+function registerRPCPlugin(univer: Univer): void {
+ const childFork = fork(path.join(__dirname, './worker.js'));
+ const messageProtocol: IMessageProtocol = {
+ send(message: unknown): void {
+ childFork.send(message as Serializable);
+ },
+ onMessage: new Observable<any>((subscriber) => {
+ const handler = (message: unknown) => {
+ subscriber.next(message);
+ };
+
+ childFork.on('message', handler);
+ return () => childFork.off('message', handler);
+ }).pipe(shareReplay(1)),
+ };
+
+ univer.registerPlugin(UniverRPCNodeMainPlugin, { messageProtocol });
+}
diff --git a/examples-node/src/sdk/worker.ts b/examples-node/src/sdk/worker.ts
new file mode 100644
index 0000000000..2743a9e3ab
--- /dev/null
+++ b/examples-node/src/sdk/worker.ts
@@ -0,0 +1,28 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { LocaleType, Univer } from '@univerjs/core';
+import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
+import { UniverRPCNodeWorkerPlugin } from '@univerjs/rpc-node';
+import { UniverSheetsPlugin } from '@univerjs/sheets';
+
+const univer = new Univer({
+ locale: LocaleType.ZH_CN,
+});
+
+univer.registerPlugin(UniverSheetsPlugin, { onlyRegisterFormulaRelatedMutations: true });
+univer.registerPlugin(UniverFormulaEnginePlugin);
+univer.registerPlugin(UniverRPCNodeWorkerPlugin);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fc88594702..89077962fa 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -10,7 +10,7 @@ importers:
devDependencies:
'@antfu/eslint-config':
specifier: 3.7.3
'@commitlint/cli':
specifier: ^19.5.0
version: 19.5.0(@types/[email protected])([email protected])
@@ -442,6 +442,9 @@ importers:
'@univerjs/rpc':
specifier: workspace:*
version: link:../packages/rpc
+ '@univerjs/rpc-node':
+ specifier: workspace:*
+ version: link:../packages-experimental/rpc-node
'@univerjs/sheets':
specifier: workspace:*
version: link:../packages/sheets
@@ -10795,7 +10798,7 @@ snapshots:
dependencies:
'@babel/runtime': 7.25.0
dependencies:
'@antfu/install-pkg': 0.4.1
'@clack/prompts': 0.7.0
@@ -10804,7 +10807,7 @@ snapshots:
'@stylistic/eslint-plugin': 2.8.0([email protected]([email protected]))([email protected])
'@typescript-eslint/parser': 8.8.1([email protected]([email protected]))([email protected])
eslint: 9.12.0([email protected])
eslint-config-flat-gitignore: 0.3.0([email protected]([email protected]))
eslint-flat-config-utils: 0.4.0
@@ -12875,7 +12878,7 @@ snapshots:
transitivePeerDependencies:
- supports-color
dependencies:
eslint: 9.12.0([email protected])
optionalDependencies: