@univerjs-pro/exchange-node 1.3.1 → 1.5.2
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/README.md +251 -5
- package/lib/cjs/index.cjs +246 -27
- package/lib/cjs/mermaid.cjs +236 -0
- package/lib/cjs/pdf.cjs +233 -17
- package/lib/es/mermaid.js +1 -0
- package/lib/shared/runtime.cjs +8 -0
- package/lib/types/index.d.ts +12 -12
- package/lib/types/mermaid.d.ts +26 -0
- package/lib/types/pdf.d.ts +11 -5
- package/lib/types/types.d.ts +129 -50
- package/package.json +19 -9
package/lib/cjs/index.cjs
CHANGED
|
@@ -54,13 +54,13 @@ __export(index_exports, {
|
|
|
54
54
|
importFileToSnapshot: () => importFileToSnapshot
|
|
55
55
|
});
|
|
56
56
|
module.exports = __toCommonJS(index_exports);
|
|
57
|
-
var import_collaboration = require("@univerjs-pro/collaboration"), import_core = require("@univerjs/core"),
|
|
57
|
+
var import_collaboration = require("@univerjs-pro/collaboration"), import_core = require("@univerjs/core"), import_node_path2 = require("node:path"), import_promises = require("node:fs/promises"), import_node_buffer2 = require("node:buffer");
|
|
58
58
|
|
|
59
59
|
// src/binding.ts
|
|
60
|
-
var import_node_module = require("node:module"), import_node_process = __toESM(require("node:process"), 1);
|
|
60
|
+
var import_node_module = require("node:module"), import_node_process = __toESM(require("node:process"), 1), import_node_path = require("node:path"), import_node_fs = require("node:fs");
|
|
61
61
|
|
|
62
62
|
// src/types.ts
|
|
63
|
-
var ExchangeFormat = /* @__PURE__ */ ((ExchangeFormat2) => (ExchangeFormat2.XLS = "xls", ExchangeFormat2.XLSX = "xlsx", ExchangeFormat2.CSV = "csv", ExchangeFormat2.TSV = "tsv", ExchangeFormat2.DOC = "doc", ExchangeFormat2.DOCX = "docx", ExchangeFormat2.PPT = "ppt", ExchangeFormat2.PPTX = "pptx", ExchangeFormat2.PDF = "pdf", ExchangeFormat2))(ExchangeFormat || {}), FormulaCalculationMode = /* @__PURE__ */ ((FormulaCalculationMode2) => (FormulaCalculationMode2.FORCED = "forced", FormulaCalculationMode2.WHEN_EMPTY = "when_empty", FormulaCalculationMode2.NO = "no", FormulaCalculationMode2))(FormulaCalculationMode || {}), CsvTypeInferenceMode = /* @__PURE__ */ ((CsvTypeInferenceMode2) => (CsvTypeInferenceMode2.OFF = "off", CsvTypeInferenceMode2.SAFE = "safe", CsvTypeInferenceMode2.AGGRESSIVE = "aggressive", CsvTypeInferenceMode2))(CsvTypeInferenceMode || {}), CsvQuoteStyle = /* @__PURE__ */ ((CsvQuoteStyle2) => (CsvQuoteStyle2.MINIMAL = "minimal", CsvQuoteStyle2.ALWAYS = "always", CsvQuoteStyle2))(CsvQuoteStyle || {}), CsvLineEnding = /* @__PURE__ */ ((CsvLineEnding2) => (CsvLineEnding2.LF = "lf", CsvLineEnding2.CRLF = "cr_lf", CsvLineEnding2))(CsvLineEnding || {}), CsvValueMode = /* @__PURE__ */ ((CsvValueMode2) => (CsvValueMode2.DISPLAY = "display", CsvValueMode2.RAW = "raw", CsvValueMode2))(CsvValueMode || {}), CsvFormulaMode = /* @__PURE__ */ ((CsvFormulaMode2) => (CsvFormulaMode2.CACHED = "cached", CsvFormulaMode2.EXPRESSION = "expression", CsvFormulaMode2))(CsvFormulaMode || {}), BaseImportMode = /* @__PURE__ */ ((BaseImportMode2) => (BaseImportMode2.TABLES = "tables", BaseImportMode2.SHEETS = "sheets", BaseImportMode2.HYBRID = "hybrid", BaseImportMode2.AUTO = "auto", BaseImportMode2))(BaseImportMode || {}), BaseTypeInference = /* @__PURE__ */ ((BaseTypeInference2) => (BaseTypeInference2.NONE = "none", BaseTypeInference2.SAFE = "safe", BaseTypeInference2.AGGRESSIVE = "aggressive", BaseTypeInference2))(BaseTypeInference || {}), BaseFormulaPolicy = /* @__PURE__ */ ((BaseFormulaPolicy2) => (BaseFormulaPolicy2.VALUES = "values", BaseFormulaPolicy2.TEXT = "text", BaseFormulaPolicy2.FAIL = "fail", BaseFormulaPolicy2.CONVERT_THEN_VALUES = "convertThenValues", BaseFormulaPolicy2))(BaseFormulaPolicy || {}), BaseImagePolicy = /* @__PURE__ */ ((BaseImagePolicy2) => (BaseImagePolicy2.SAFE = "safe", BaseImagePolicy2.CELL_ONLY = "cell-only", BaseImagePolicy2.MEDIA_TABLE = "media-table", BaseImagePolicy2.NONE = "none", BaseImagePolicy2))(BaseImagePolicy || {}), BaseHeaderPolicy = /* @__PURE__ */ ((BaseHeaderPolicy2) => (BaseHeaderPolicy2.FIRST_ROW = "firstRow", BaseHeaderPolicy2.NONE = "none", BaseHeaderPolicy2))(BaseHeaderPolicy || {}), BaseExportMode = /* @__PURE__ */ ((BaseExportMode2) => (BaseExportMode2.TABLES = "tables", BaseExportMode2.RANGES = "ranges", BaseExportMode2))(BaseExportMode || {}), BaseAttachmentPolicy = /* @__PURE__ */ ((BaseAttachmentPolicy2) => (BaseAttachmentPolicy2.HYBRID = "hybrid", BaseAttachmentPolicy2.MANIFEST = "manifest", BaseAttachmentPolicy2.LINKS = "links", BaseAttachmentPolicy2.NONE = "none", BaseAttachmentPolicy2))(BaseAttachmentPolicy || {}), DocxCompatibilityMode = /* @__PURE__ */ ((DocxCompatibilityMode2) => (DocxCompatibilityMode2.TRADITIONAL = "traditional", DocxCompatibilityMode2.MODERN = "modern", DocxCompatibilityMode2))(DocxCompatibilityMode || {}), ExchangeErrorCode = /* @__PURE__ */ ((ExchangeErrorCode2) => (ExchangeErrorCode2.INVALID_ARGUMENT = "INVALID_ARGUMENT", ExchangeErrorCode2.UNSUPPORTED_FORMAT = "UNSUPPORTED_FORMAT", ExchangeErrorCode2.INVALID_FILE = "INVALID_FILE", ExchangeErrorCode2.INCOMPLETE_SNAPSHOT = "INCOMPLETE_SNAPSHOT", ExchangeErrorCode2.IO_ERROR = "IO_ERROR", ExchangeErrorCode2.NATIVE_LOAD_FAILED = "NATIVE_LOAD_FAILED", ExchangeErrorCode2.CONVERSION_FAILED = "CONVERSION_FAILED", ExchangeErrorCode2.LICENSE_EXPORT_QUOTA_EXCEEDED = "LICENSE_EXPORT_QUOTA_EXCEEDED", ExchangeErrorCode2.LICENSE_INVALID = "LICENSE_INVALID", ExchangeErrorCode2.LICENSE_POLICY_INVALID = "LICENSE_POLICY_INVALID", ExchangeErrorCode2.LICENSE_NOT_YET_EFFECTIVE = "LICENSE_NOT_YET_EFFECTIVE", ExchangeErrorCode2.LICENSE_UNIT_TYPE_REJECTED = "LICENSE_UNIT_TYPE_REJECTED", ExchangeErrorCode2.LICENSE_VERSION_NOT_ALLOWED = "LICENSE_VERSION_NOT_ALLOWED", ExchangeErrorCode2.LICENSE_CONCURRENCY_TIMEOUT = "LICENSE_CONCURRENCY_TIMEOUT", ExchangeErrorCode2.LICENSE_USAGE_STATE_INVALID = "LICENSE_USAGE_STATE_INVALID", ExchangeErrorCode2.LICENSE_USAGE_STORAGE_ERROR = "LICENSE_USAGE_STORAGE_ERROR", ExchangeErrorCode2))(ExchangeErrorCode || {}), ExchangeError = class extends Error {
|
|
63
|
+
var ExchangeFormat = /* @__PURE__ */ ((ExchangeFormat2) => (ExchangeFormat2.XLS = "xls", ExchangeFormat2.XLSX = "xlsx", ExchangeFormat2.CSV = "csv", ExchangeFormat2.TSV = "tsv", ExchangeFormat2.DOC = "doc", ExchangeFormat2.DOCX = "docx", ExchangeFormat2.PPT = "ppt", ExchangeFormat2.PPTX = "pptx", ExchangeFormat2.PDF = "pdf", ExchangeFormat2.MERMAID = "mermaid", ExchangeFormat2))(ExchangeFormat || {}), FormulaCalculationMode = /* @__PURE__ */ ((FormulaCalculationMode2) => (FormulaCalculationMode2.FORCED = "forced", FormulaCalculationMode2.WHEN_EMPTY = "when_empty", FormulaCalculationMode2.NO = "no", FormulaCalculationMode2))(FormulaCalculationMode || {}), CsvTypeInferenceMode = /* @__PURE__ */ ((CsvTypeInferenceMode2) => (CsvTypeInferenceMode2.OFF = "off", CsvTypeInferenceMode2.SAFE = "safe", CsvTypeInferenceMode2.AGGRESSIVE = "aggressive", CsvTypeInferenceMode2))(CsvTypeInferenceMode || {}), CsvQuoteStyle = /* @__PURE__ */ ((CsvQuoteStyle2) => (CsvQuoteStyle2.MINIMAL = "minimal", CsvQuoteStyle2.ALWAYS = "always", CsvQuoteStyle2))(CsvQuoteStyle || {}), CsvLineEnding = /* @__PURE__ */ ((CsvLineEnding2) => (CsvLineEnding2.LF = "lf", CsvLineEnding2.CRLF = "cr_lf", CsvLineEnding2))(CsvLineEnding || {}), CsvValueMode = /* @__PURE__ */ ((CsvValueMode2) => (CsvValueMode2.DISPLAY = "display", CsvValueMode2.RAW = "raw", CsvValueMode2))(CsvValueMode || {}), CsvFormulaMode = /* @__PURE__ */ ((CsvFormulaMode2) => (CsvFormulaMode2.CACHED = "cached", CsvFormulaMode2.EXPRESSION = "expression", CsvFormulaMode2))(CsvFormulaMode || {}), BaseImportMode = /* @__PURE__ */ ((BaseImportMode2) => (BaseImportMode2.TABLES = "tables", BaseImportMode2.SHEETS = "sheets", BaseImportMode2.HYBRID = "hybrid", BaseImportMode2.AUTO = "auto", BaseImportMode2))(BaseImportMode || {}), BaseTypeInference = /* @__PURE__ */ ((BaseTypeInference2) => (BaseTypeInference2.NONE = "none", BaseTypeInference2.SAFE = "safe", BaseTypeInference2.AGGRESSIVE = "aggressive", BaseTypeInference2))(BaseTypeInference || {}), BaseFormulaPolicy = /* @__PURE__ */ ((BaseFormulaPolicy2) => (BaseFormulaPolicy2.VALUES = "values", BaseFormulaPolicy2.TEXT = "text", BaseFormulaPolicy2.FAIL = "fail", BaseFormulaPolicy2.CONVERT_THEN_VALUES = "convertThenValues", BaseFormulaPolicy2))(BaseFormulaPolicy || {}), BaseImagePolicy = /* @__PURE__ */ ((BaseImagePolicy2) => (BaseImagePolicy2.SAFE = "safe", BaseImagePolicy2.CELL_ONLY = "cell-only", BaseImagePolicy2.MEDIA_TABLE = "media-table", BaseImagePolicy2.NONE = "none", BaseImagePolicy2))(BaseImagePolicy || {}), BaseHeaderPolicy = /* @__PURE__ */ ((BaseHeaderPolicy2) => (BaseHeaderPolicy2.FIRST_ROW = "firstRow", BaseHeaderPolicy2.NONE = "none", BaseHeaderPolicy2))(BaseHeaderPolicy || {}), BaseExportMode = /* @__PURE__ */ ((BaseExportMode2) => (BaseExportMode2.TABLES = "tables", BaseExportMode2.RANGES = "ranges", BaseExportMode2))(BaseExportMode || {}), BaseAttachmentPolicy = /* @__PURE__ */ ((BaseAttachmentPolicy2) => (BaseAttachmentPolicy2.HYBRID = "hybrid", BaseAttachmentPolicy2.MANIFEST = "manifest", BaseAttachmentPolicy2.LINKS = "links", BaseAttachmentPolicy2.NONE = "none", BaseAttachmentPolicy2))(BaseAttachmentPolicy || {}), DocxCompatibilityMode = /* @__PURE__ */ ((DocxCompatibilityMode2) => (DocxCompatibilityMode2.TRADITIONAL = "traditional", DocxCompatibilityMode2.MODERN = "modern", DocxCompatibilityMode2))(DocxCompatibilityMode || {}), ExchangeErrorCode = /* @__PURE__ */ ((ExchangeErrorCode2) => (ExchangeErrorCode2.INVALID_ARGUMENT = "INVALID_ARGUMENT", ExchangeErrorCode2.UNSUPPORTED_FORMAT = "UNSUPPORTED_FORMAT", ExchangeErrorCode2.INVALID_FILE = "INVALID_FILE", ExchangeErrorCode2.INCOMPLETE_SNAPSHOT = "INCOMPLETE_SNAPSHOT", ExchangeErrorCode2.IO_ERROR = "IO_ERROR", ExchangeErrorCode2.NATIVE_LOAD_FAILED = "NATIVE_LOAD_FAILED", ExchangeErrorCode2.CONVERSION_FAILED = "CONVERSION_FAILED", ExchangeErrorCode2.LICENSE_EXPORT_QUOTA_EXCEEDED = "LICENSE_EXPORT_QUOTA_EXCEEDED", ExchangeErrorCode2.LICENSE_INVALID = "LICENSE_INVALID", ExchangeErrorCode2.LICENSE_POLICY_INVALID = "LICENSE_POLICY_INVALID", ExchangeErrorCode2.LICENSE_NOT_YET_EFFECTIVE = "LICENSE_NOT_YET_EFFECTIVE", ExchangeErrorCode2.LICENSE_UNIT_TYPE_REJECTED = "LICENSE_UNIT_TYPE_REJECTED", ExchangeErrorCode2.LICENSE_VERSION_NOT_ALLOWED = "LICENSE_VERSION_NOT_ALLOWED", ExchangeErrorCode2.LICENSE_CONCURRENCY_TIMEOUT = "LICENSE_CONCURRENCY_TIMEOUT", ExchangeErrorCode2.LICENSE_USAGE_STATE_INVALID = "LICENSE_USAGE_STATE_INVALID", ExchangeErrorCode2.LICENSE_USAGE_STORAGE_ERROR = "LICENSE_USAGE_STORAGE_ERROR", ExchangeErrorCode2))(ExchangeErrorCode || {}), ExchangeError = class extends Error {
|
|
64
64
|
constructor(code, message, options) {
|
|
65
65
|
super(message, options);
|
|
66
66
|
this.code = code;
|
|
@@ -76,7 +76,7 @@ function errorMessage(error) {
|
|
|
76
76
|
}
|
|
77
77
|
function validateBinding(binding) {
|
|
78
78
|
try {
|
|
79
|
-
if (!binding || typeof binding.__internalExchangeConfigProtocolVersion != "function" || binding.__internalExchangeConfigProtocolVersion() !== 1 || typeof binding.__internalExchangeConfigure != "function" || typeof binding.__internalExchangeGetExportQuota != "function" || typeof binding.exchangeExportSnapshot != "function")
|
|
79
|
+
if (!binding || typeof binding.__internalExchangeConfigProtocolVersion != "function" || binding.__internalExchangeConfigProtocolVersion() !== 1 || typeof binding.__internalExchangeConfigure != "function" || typeof binding.__internalExchangeGetExportQuota != "function" || typeof binding.exchangeExportSnapshot != "function" || typeof binding.exchangeApplyImportFileReferences != "function" || typeof binding.exchangeExportRequirements != "function")
|
|
80
80
|
throw new ExchangeError("NATIVE_LOAD_FAILED" /* NATIVE_LOAD_FAILED */, "Incompatible native exchange configuration protocol.");
|
|
81
81
|
return binding;
|
|
82
82
|
} catch (error) {
|
|
@@ -86,7 +86,22 @@ function validateBinding(binding) {
|
|
|
86
86
|
async function loadNativeBinding() {
|
|
87
87
|
if (nativeBinding)
|
|
88
88
|
return nativeBinding;
|
|
89
|
-
let errors = [];
|
|
89
|
+
let errors = [], runtimeDirectory = import_node_process.default.env.UNIVER_NODE_SDK_RUNTIME_DIR_OVERRIDE;
|
|
90
|
+
if (runtimeDirectory !== void 0)
|
|
91
|
+
try {
|
|
92
|
+
if (!(0, import_node_path.isAbsolute)(runtimeDirectory))
|
|
93
|
+
throw new Error("Expected an absolute runtime directory path.");
|
|
94
|
+
if (!(0, import_node_fs.statSync)(runtimeDirectory).isDirectory())
|
|
95
|
+
throw new Error("Expected a runtime directory.");
|
|
96
|
+
let nativeLibraryPath = (0, import_node_path.join)(runtimeDirectory, "univer-exchange-node.node"), loaded = { exports: {} };
|
|
97
|
+
return import_node_process.default.dlopen(loaded, nativeLibraryPath), nativeBinding = validateBinding(loaded.exports), nativeBindingSource = "UNIVER_NODE_SDK_RUNTIME_DIR_OVERRIDE", nativeBinding;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
throw new ExchangeError(
|
|
100
|
+
"NATIVE_LOAD_FAILED" /* NATIVE_LOAD_FAILED */,
|
|
101
|
+
`Failed to load native exchange binding from UNIVER_NODE_SDK_RUNTIME_DIR_OVERRIDE: ${errorMessage(error)}`,
|
|
102
|
+
{ cause: error }
|
|
103
|
+
);
|
|
104
|
+
}
|
|
90
105
|
try {
|
|
91
106
|
return nativeBinding = validateBinding(nodeRequire("@univerjs-pro/exchange-node-binding")), nativeBindingSource = "@univerjs-pro/exchange-node-binding", nativeBinding;
|
|
92
107
|
} catch (error) {
|
|
@@ -105,6 +120,24 @@ async function loadNativeBinding() {
|
|
|
105
120
|
`)
|
|
106
121
|
);
|
|
107
122
|
}
|
|
123
|
+
async function loadBoardBinding() {
|
|
124
|
+
let binding = await loadNativeBinding();
|
|
125
|
+
if (typeof binding.__internalExchangeBoardProtocolVersion != "function" || binding.__internalExchangeBoardProtocolVersion() !== 1 || typeof binding.exchangeCompileMermaid != "function")
|
|
126
|
+
throw new ExchangeError(
|
|
127
|
+
"NATIVE_LOAD_FAILED" /* NATIVE_LOAD_FAILED */,
|
|
128
|
+
"Board/Mermaid requires a native exchange binding with Board protocol 1. Upgrade @univerjs-pro/exchange-node-binding."
|
|
129
|
+
);
|
|
130
|
+
return binding;
|
|
131
|
+
}
|
|
132
|
+
async function loadBoardFilesBinding() {
|
|
133
|
+
let binding = await loadBoardBinding();
|
|
134
|
+
if (typeof binding.exchangeCollectImportFiles != "function")
|
|
135
|
+
throw new ExchangeError(
|
|
136
|
+
"NATIVE_LOAD_FAILED" /* NATIVE_LOAD_FAILED */,
|
|
137
|
+
"Board resource callbacks require a newer @univerjs-pro/exchange-node-binding. Upgrade the SDK and binding together."
|
|
138
|
+
);
|
|
139
|
+
return binding;
|
|
140
|
+
}
|
|
108
141
|
|
|
109
142
|
// src/errors.ts
|
|
110
143
|
function structuredExchangeError(error, ErrorClass = ExchangeError) {
|
|
@@ -140,6 +173,29 @@ function createSnapshotCollector() {
|
|
|
140
173
|
};
|
|
141
174
|
}
|
|
142
175
|
|
|
176
|
+
// src/file-storage.ts
|
|
177
|
+
var import_node_crypto = require("node:crypto");
|
|
178
|
+
async function storeFileReferences(inputs, files, ErrorClass = ExchangeError) {
|
|
179
|
+
let groups = /* @__PURE__ */ new Map();
|
|
180
|
+
for (let { id, ...file } of inputs) {
|
|
181
|
+
let hash = (0, import_node_crypto.createHash)("sha256").update(file.bytes).digest("hex"), group = groups.get(hash) ?? { file, ids: [] };
|
|
182
|
+
group.ids.push(id), groups.set(hash, group);
|
|
183
|
+
}
|
|
184
|
+
let references = /* @__PURE__ */ new Map();
|
|
185
|
+
for (let { file, ids } of groups.values()) {
|
|
186
|
+
let decision = await files.store(file);
|
|
187
|
+
if (decision?.mode !== "inline") {
|
|
188
|
+
if (decision?.mode !== "reference" || typeof decision.source != "string" || !decision.source.trim())
|
|
189
|
+
throw new ErrorClass(
|
|
190
|
+
"INVALID_ARGUMENT" /* INVALID_ARGUMENT */,
|
|
191
|
+
'files.store must return { mode: "inline" } or { mode: "reference", source }'
|
|
192
|
+
);
|
|
193
|
+
for (let id of ids) references.set(id, decision.source);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return references;
|
|
197
|
+
}
|
|
198
|
+
|
|
143
199
|
// src/snapshot.ts
|
|
144
200
|
var import_node_buffer = require("node:buffer");
|
|
145
201
|
function bufferView(bytes) {
|
|
@@ -166,7 +222,7 @@ function toNativeSnapshot(value) {
|
|
|
166
222
|
])
|
|
167
223
|
)), snapshot.workbook = workbook;
|
|
168
224
|
}
|
|
169
|
-
for (let kind of ["doc", "slide"]) {
|
|
225
|
+
for (let kind of ["doc", "slide", "board"]) {
|
|
170
226
|
let owner = source[kind];
|
|
171
227
|
owner && (snapshot[kind] = takeMetadata(owner, kind, metadata));
|
|
172
228
|
}
|
|
@@ -188,7 +244,7 @@ function restoreMetadata(snapshot, metadata) {
|
|
|
188
244
|
let sheets = snapshot.workbook?.sheets;
|
|
189
245
|
owner = entry.ownerId ? sheets?.[entry.ownerId] : void 0;
|
|
190
246
|
} else {
|
|
191
|
-
if (entry.kind !== "workbook" && entry.kind !== "doc" && entry.kind !== "slide")
|
|
247
|
+
if (entry.kind !== "workbook" && entry.kind !== "doc" && entry.kind !== "slide" && entry.kind !== "board")
|
|
192
248
|
throw new TypeError(`unsupported native snapshot metadata kind ${entry.kind}`);
|
|
193
249
|
owner = snapshot[entry.kind];
|
|
194
250
|
}
|
|
@@ -228,8 +284,62 @@ var FORMAT_BY_EXTENSION = {
|
|
|
228
284
|
".docx": "docx" /* DOCX */,
|
|
229
285
|
".ppt": "ppt" /* PPT */,
|
|
230
286
|
".pptx": "pptx" /* PPTX */,
|
|
231
|
-
".pdf": "pdf" /* PDF
|
|
287
|
+
".pdf": "pdf" /* PDF */,
|
|
288
|
+
".mmd": "mermaid" /* MERMAID */,
|
|
289
|
+
".mermaid": "mermaid" /* MERMAID */
|
|
290
|
+
}, CallbackFailure = class extends Error {
|
|
291
|
+
constructor(failure) {
|
|
292
|
+
super("application callback failed");
|
|
293
|
+
this.failure = failure;
|
|
294
|
+
}
|
|
295
|
+
failure;
|
|
232
296
|
};
|
|
297
|
+
async function callbackResult(action) {
|
|
298
|
+
try {
|
|
299
|
+
return await action();
|
|
300
|
+
} catch (failure) {
|
|
301
|
+
throw new CallbackFailure(failure);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function invalidArgument(message) {
|
|
305
|
+
return new ExchangeError("INVALID_ARGUMENT" /* INVALID_ARGUMENT */, message);
|
|
306
|
+
}
|
|
307
|
+
function isOfficeHost(options) {
|
|
308
|
+
return options.type === import_core.UniverInstanceType.UNIVER_SHEET && options.format === "xlsx" /* XLSX */ || options.type === import_core.UniverInstanceType.UNIVER_DOC && options.format === "docx" /* DOCX */ || (options.type === import_core.UniverInstanceType.UNIVER_SLIDE || isBoard(options.type)) && options.format === "pptx" /* PPTX */;
|
|
309
|
+
}
|
|
310
|
+
function isPdfHost(options) {
|
|
311
|
+
return options.type === import_core.UniverInstanceType.UNIVER_PDF && options.format === "pdf" /* PDF */;
|
|
312
|
+
}
|
|
313
|
+
function isBoard(type) {
|
|
314
|
+
return type === import_core.UniverInstanceType.UNIVER_BOARD;
|
|
315
|
+
}
|
|
316
|
+
function validateImportFiles(options) {
|
|
317
|
+
let files = options.files;
|
|
318
|
+
if (files !== void 0) {
|
|
319
|
+
if (files === null || typeof files != "object" || typeof files.store != "function")
|
|
320
|
+
throw invalidArgument("Invalid argument: files.store must be a function.");
|
|
321
|
+
if (options.type === import_core.UniverInstanceType.UNIVER_BASE)
|
|
322
|
+
throw invalidArgument("files is supported only for Sheet, Doc, Slide, Board and PDF imports");
|
|
323
|
+
return files;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function validateExportResources(options) {
|
|
327
|
+
let { files, ole } = options;
|
|
328
|
+
if (files === void 0 && ole === void 0) return {};
|
|
329
|
+
if (!isOfficeHost(options) && !isPdfHost(options))
|
|
330
|
+
throw invalidArgument("files and ole export options require Sheet/XLSX, Doc/DOCX or Slide/Board PPTX or PDF");
|
|
331
|
+
if (files !== void 0 && (files === null || typeof files != "object" || typeof files.load != "function"))
|
|
332
|
+
throw invalidArgument("Invalid argument: files.load must be a function.");
|
|
333
|
+
if (ole !== void 0) {
|
|
334
|
+
if (ole === null || typeof ole != "object") throw invalidArgument("Invalid argument: ole.");
|
|
335
|
+
let { attachmentSource = "original", loadOnlineUnit } = ole;
|
|
336
|
+
if (attachmentSource !== "original" && attachmentSource !== "online")
|
|
337
|
+
throw invalidArgument('ole.attachmentSource must be "original" or "online"');
|
|
338
|
+
if (attachmentSource === "online" && typeof loadOnlineUnit != "function")
|
|
339
|
+
throw invalidArgument('ole.loadOnlineUnit is required when ole.attachmentSource is "online"');
|
|
340
|
+
}
|
|
341
|
+
return { files, ole };
|
|
342
|
+
}
|
|
233
343
|
function pdfAdapter(type, format) {
|
|
234
344
|
if (type !== import_core.UniverInstanceType.UNIVER_PDF && format !== "pdf" /* PDF */)
|
|
235
345
|
return;
|
|
@@ -286,7 +396,7 @@ async function getExportQuota(options = {}) {
|
|
|
286
396
|
function resolveFormat(fileName, format) {
|
|
287
397
|
if (format)
|
|
288
398
|
return format;
|
|
289
|
-
let inferred = FORMAT_BY_EXTENSION[(0,
|
|
399
|
+
let inferred = FORMAT_BY_EXTENSION[(0, import_node_path2.extname)(fileName).toLowerCase()];
|
|
290
400
|
if (!inferred)
|
|
291
401
|
throw new ExchangeError(
|
|
292
402
|
"UNSUPPORTED_FORMAT" /* UNSUPPORTED_FORMAT */,
|
|
@@ -304,16 +414,19 @@ function nativeUnitType(type) {
|
|
|
304
414
|
return "doc";
|
|
305
415
|
case import_core.UniverInstanceType.UNIVER_SLIDE:
|
|
306
416
|
return "slide";
|
|
417
|
+
case import_core.UniverInstanceType.UNIVER_BOARD:
|
|
418
|
+
return "board";
|
|
307
419
|
default:
|
|
308
420
|
throw new ExchangeError("INVALID_ARGUMENT" /* INVALID_ARGUMENT */, `Unsupported Univer unit type: ${type}`);
|
|
309
421
|
}
|
|
310
422
|
}
|
|
311
|
-
function nativeImportOptions(options, fileName) {
|
|
423
|
+
function nativeImportOptions(options, fileName, collectFiles) {
|
|
312
424
|
return {
|
|
313
425
|
format: resolveFormat(fileName, options.format),
|
|
314
426
|
unitType: nativeUnitType(options.type),
|
|
315
427
|
fileName,
|
|
316
428
|
unitId: options.unitId,
|
|
429
|
+
collectFiles: collectFiles || void 0,
|
|
317
430
|
minSheetRowCount: "minSheetRowCount" in options ? options.minSheetRowCount : void 0,
|
|
318
431
|
minSheetColumnCount: "minSheetColumnCount" in options ? options.minSheetColumnCount : void 0,
|
|
319
432
|
compatibilityMode: "compatibilityMode" in options ? options.compatibilityMode : void 0,
|
|
@@ -340,18 +453,90 @@ function nativeExportOptions(options) {
|
|
|
340
453
|
attachmentPolicy: options.attachmentPolicy,
|
|
341
454
|
mapping: options.mapping
|
|
342
455
|
} : void 0,
|
|
343
|
-
formulaCalculation: "formulaCalculation" in options ? options.formulaCalculation : void 0
|
|
456
|
+
formulaCalculation: "formulaCalculation" in options ? options.formulaCalculation : void 0,
|
|
457
|
+
ole: options.ole !== void 0 ? { attachmentSource: options.ole.attachmentSource ?? "original" } : void 0,
|
|
458
|
+
files: options.files !== void 0 || void 0,
|
|
459
|
+
deferPivotCalculation: "deferPivotCalculation" in options && options.deferPivotCalculation === !0 ? !0 : void 0
|
|
344
460
|
};
|
|
345
461
|
}
|
|
346
|
-
async function importSnapshot(input, options, fileName) {
|
|
462
|
+
async function importSnapshot(input, options, fileName, files) {
|
|
347
463
|
try {
|
|
348
|
-
let
|
|
464
|
+
let binding = await loadNativeBinding(), result = await binding.exchangeImportToSnapshot(
|
|
349
465
|
input,
|
|
350
|
-
JSON.stringify(nativeImportOptions(options, fileName))
|
|
466
|
+
JSON.stringify(nativeImportOptions(options, fileName, files !== void 0))
|
|
351
467
|
);
|
|
352
|
-
return fromNativeSnapshot(result);
|
|
468
|
+
return files && (result = await applyImportFiles(binding, result, files)), fromNativeSnapshot(result);
|
|
353
469
|
} catch (error) {
|
|
354
|
-
throw exchangeError(error, "CONVERSION_FAILED" /* CONVERSION_FAILED */);
|
|
470
|
+
throw error instanceof CallbackFailure ? error : exchangeError(error, "CONVERSION_FAILED" /* CONVERSION_FAILED */);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async function applyImportFiles(binding, result, files) {
|
|
474
|
+
let stored = await storeFileReferences(
|
|
475
|
+
(result.files ?? []).map((file) => ({
|
|
476
|
+
id: file.id,
|
|
477
|
+
name: file.name,
|
|
478
|
+
mimeType: file.mimeType,
|
|
479
|
+
size: file.size,
|
|
480
|
+
bytes: file.data
|
|
481
|
+
})),
|
|
482
|
+
{ store: (file) => callbackResult(() => files.store(file)) }
|
|
483
|
+
), references = Array.from(stored, ([id, source]) => ({ id, source }));
|
|
484
|
+
return references.length > 0 && (result = await binding.exchangeApplyImportFileReferences(result, JSON.stringify(references))), result;
|
|
485
|
+
}
|
|
486
|
+
async function collectExportResources(binding, input, nativeOptions, files, ole) {
|
|
487
|
+
let online = ole?.attachmentSource === "online";
|
|
488
|
+
if (!files && !online) return;
|
|
489
|
+
let resources = { files: [], children: [] }, provided = /* @__PURE__ */ new Set(), loaded = /* @__PURE__ */ new Map();
|
|
490
|
+
for (; ; ) {
|
|
491
|
+
let known = {
|
|
492
|
+
files: resources.files.map(({ data: _data, ...reference }) => reference),
|
|
493
|
+
children: resources.children
|
|
494
|
+
}, missing = JSON.parse(
|
|
495
|
+
await binding.exchangeExportRequirements(input, nativeOptions, known)
|
|
496
|
+
);
|
|
497
|
+
if (missing.files.length === 0 && missing.units.length === 0) return resources;
|
|
498
|
+
if (missing.files.length > 0 && !files)
|
|
499
|
+
throw invalidArgument(
|
|
500
|
+
`Export needs files.load to resolve ${missing.files.length} stored reference(s)`
|
|
501
|
+
);
|
|
502
|
+
for (let ref of missing.files) {
|
|
503
|
+
let key = `file\0${ref.unitId}\0${ref.kind}\0${ref.sourceType}\0${ref.source}`;
|
|
504
|
+
if (provided.has(key))
|
|
505
|
+
throw new ExchangeError("CONVERSION_FAILED" /* CONVERSION_FAILED */, `Export still requires ${ref.source} after it was resolved`);
|
|
506
|
+
provided.add(key);
|
|
507
|
+
let storageKey = JSON.stringify([ref.unitId, ref.sourceType, ref.source]), bytes = loaded.get(storageKey);
|
|
508
|
+
if (!bytes) {
|
|
509
|
+
let content = await callbackResult(
|
|
510
|
+
() => files.load({
|
|
511
|
+
sourceType: ref.sourceType,
|
|
512
|
+
source: ref.source,
|
|
513
|
+
unitId: ref.unitId
|
|
514
|
+
})
|
|
515
|
+
);
|
|
516
|
+
if (!(content?.bytes instanceof Uint8Array))
|
|
517
|
+
throw invalidArgument("files.load must return { bytes: Uint8Array }");
|
|
518
|
+
bytes = content.bytes, loaded.set(storageKey, bytes);
|
|
519
|
+
}
|
|
520
|
+
resources.files.push({ ...ref, data: bufferView(bytes) });
|
|
521
|
+
}
|
|
522
|
+
for (let unit of missing.units) {
|
|
523
|
+
let key = `unit\0${unit.unitId}`;
|
|
524
|
+
if (!online || provided.has(key))
|
|
525
|
+
throw new ExchangeError("CONVERSION_FAILED" /* CONVERSION_FAILED */, `Export still requires Unit ${unit.unitId} after it was provided`);
|
|
526
|
+
provided.add(key);
|
|
527
|
+
let child;
|
|
528
|
+
try {
|
|
529
|
+
child = await ole.loadOnlineUnit({
|
|
530
|
+
unitId: unit.unitId,
|
|
531
|
+
type: unit.type
|
|
532
|
+
});
|
|
533
|
+
} catch (failure) {
|
|
534
|
+
throw new CallbackFailure(failure);
|
|
535
|
+
}
|
|
536
|
+
if (!child || typeof child != "object" || !child.snapshot || !Array.isArray(child.sheetBlocks))
|
|
537
|
+
throw invalidArgument("ole.loadOnlineUnit must return an ISnapshotWithBlocks");
|
|
538
|
+
resources.children.push({ unitId: unit.unitId, unitType: unit.type, snapshot: toNativeSnapshot(child) });
|
|
539
|
+
}
|
|
355
540
|
}
|
|
356
541
|
}
|
|
357
542
|
async function snapshotToMemory(snapshot, type) {
|
|
@@ -363,6 +548,8 @@ async function snapshotToMemory(snapshot, type) {
|
|
|
363
548
|
return await (0, import_collaboration.transformSnapshotToBaseData)(snapshot.snapshot, snapshot.sheetBlocks);
|
|
364
549
|
case import_core.UniverInstanceType.UNIVER_DOC:
|
|
365
550
|
return (0, import_collaboration.transformSnapshotToDocumentData)(snapshot.snapshot);
|
|
551
|
+
case import_core.UniverInstanceType.UNIVER_BOARD:
|
|
552
|
+
return (0, import_collaboration.transformSnapshotToBoardData)(snapshot.snapshot);
|
|
366
553
|
case import_core.UniverInstanceType.UNIVER_SLIDE:
|
|
367
554
|
return (0, import_collaboration.transformSnapshotToSlideData)(snapshot.snapshot);
|
|
368
555
|
default:
|
|
@@ -379,6 +566,10 @@ async function memoryToSnapshot(data, options) {
|
|
|
379
566
|
throw new ExchangeError("INVALID_ARGUMENT" /* INVALID_ARGUMENT */, "Export data must have a non-empty id");
|
|
380
567
|
let rev = data.rev ?? 0, collector = createSnapshotCollector();
|
|
381
568
|
switch (options.type) {
|
|
569
|
+
case import_core.UniverInstanceType.UNIVER_BOARD: {
|
|
570
|
+
let board = data, { snapshot } = await (0, import_collaboration.transformBoardDataToSnapshot)(board.resources, board, unitId, rev, collector.service);
|
|
571
|
+
return { snapshot, sheetBlocks: [] };
|
|
572
|
+
}
|
|
382
573
|
case import_core.UniverInstanceType.UNIVER_SHEET: {
|
|
383
574
|
let workbook = normalizeWorkbookSheetOrder(data), { snapshot } = await (0, import_collaboration.transformWorkbookDataToSnapshot)(
|
|
384
575
|
{ metadata: void 0 },
|
|
@@ -436,24 +627,44 @@ async function readInput(path) {
|
|
|
436
627
|
async function importResult(input, options, fileName, asSnapshot) {
|
|
437
628
|
let result;
|
|
438
629
|
try {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
630
|
+
if (typeof fileName != "string" || fileName.trim().length === 0)
|
|
631
|
+
throw invalidArgument("fileName must be a non-empty string, even when format is provided.");
|
|
632
|
+
let format = resolveFormat(fileName, options.format), files = validateImportFiles(options);
|
|
633
|
+
if (format === "mermaid" /* MERMAID */ || isBoard(options.type)) {
|
|
634
|
+
if (!isBoard(options.type) || format !== "mermaid" /* MERMAID */)
|
|
635
|
+
throw invalidArgument("Board imports require Mermaid format.");
|
|
636
|
+
let adapter = import_runtime.default.getMermaid();
|
|
637
|
+
if (!adapter) throw invalidArgument("Mermaid import requires @univerjs-pro/exchange-node/mermaid.");
|
|
638
|
+
let converted = await adapter.import(input, { ...options, fileName });
|
|
639
|
+
if (files) {
|
|
640
|
+
let binding = await loadBoardFilesBinding(), aggregate = await adapter.toSnapshot(converted.value), collected = await binding.exchangeCollectImportFiles(toNativeSnapshot(aggregate)), snapshot = fromNativeSnapshot(await applyImportFiles(binding, collected, files));
|
|
641
|
+
result = { value: asSnapshot ? snapshot : await snapshotToMemory(snapshot, options.type), diagnostics: converted.diagnostics };
|
|
642
|
+
} else
|
|
643
|
+
result = {
|
|
644
|
+
value: asSnapshot ? await adapter.toSnapshot(converted.value) : converted.value,
|
|
645
|
+
diagnostics: converted.diagnostics
|
|
646
|
+
};
|
|
647
|
+
} else if (pdfAdapter(options.type, format)) {
|
|
648
|
+
let adapter = import_runtime.default.getPdf(), converted = await adapter.import(input, {
|
|
649
|
+
fileName,
|
|
650
|
+
unitId: options.unitId,
|
|
651
|
+
files: files && { store: (file) => callbackResult(() => files.store(file)) }
|
|
652
|
+
});
|
|
442
653
|
result = {
|
|
443
654
|
value: asSnapshot ? await adapter.toSnapshot(converted.value) : converted.value,
|
|
444
655
|
diagnostics: converted.diagnostics
|
|
445
656
|
};
|
|
446
657
|
} else {
|
|
447
|
-
let snapshot = await importSnapshot(input, options, fileName);
|
|
658
|
+
let snapshot = await importSnapshot(input, options, fileName, files);
|
|
448
659
|
result = { value: asSnapshot ? snapshot : await snapshotToMemory(snapshot, options.type), diagnostics: [] };
|
|
449
660
|
}
|
|
450
661
|
} catch (error) {
|
|
451
|
-
throw exchangeError(error, "CONVERSION_FAILED" /* CONVERSION_FAILED */);
|
|
662
|
+
throw error instanceof CallbackFailure ? error.failure : exchangeError(error, "CONVERSION_FAILED" /* CONVERSION_FAILED */);
|
|
452
663
|
}
|
|
453
664
|
return await deliverDiagnostics(options, result.diagnostics), result.value;
|
|
454
665
|
}
|
|
455
666
|
async function importFile(path, options) {
|
|
456
|
-
return importResult(await readInput(path), options, options.fileName ?? (0,
|
|
667
|
+
return importResult(await readInput(path), options, options.fileName ?? (0, import_node_path2.basename)(path), !1);
|
|
457
668
|
}
|
|
458
669
|
async function importBuffer(input, options) {
|
|
459
670
|
return importResult(input, options, options.fileName, !1);
|
|
@@ -462,7 +673,7 @@ async function importFileToSnapshot(path, options) {
|
|
|
462
673
|
return importResult(
|
|
463
674
|
await readInput(path),
|
|
464
675
|
options,
|
|
465
|
-
options.fileName ?? (0,
|
|
676
|
+
options.fileName ?? (0, import_node_path2.basename)(path),
|
|
466
677
|
!0
|
|
467
678
|
);
|
|
468
679
|
}
|
|
@@ -472,12 +683,18 @@ async function importBufferToSnapshot(input, options) {
|
|
|
472
683
|
async function exportResult(data, options, isSnapshot, captured) {
|
|
473
684
|
let result;
|
|
474
685
|
try {
|
|
475
|
-
let config = await captured
|
|
686
|
+
let config = await captured;
|
|
687
|
+
if ("deferPivotCalculation" in options && options.deferPivotCalculation !== void 0 && (options.type !== import_core.UniverInstanceType.UNIVER_SHEET || options.format !== "xlsx" /* XLSX */ || typeof options.deferPivotCalculation != "boolean"))
|
|
688
|
+
throw invalidArgument("deferPivotCalculation must be a boolean for Sheet XLSX export");
|
|
689
|
+
let { files, ole } = validateExportResources(options), adapter = pdfAdapter(options.type, options.format);
|
|
476
690
|
if (adapter) {
|
|
477
691
|
let unit = isSnapshot ? adapter.fromSnapshot(data) : data;
|
|
478
692
|
result = await adapter.export(
|
|
479
693
|
unit,
|
|
480
|
-
{
|
|
694
|
+
{
|
|
695
|
+
strictResources: "strictResources" in options ? options.strictResources : void 0,
|
|
696
|
+
files: files && { load: (ref) => callbackResult(() => files.load(ref)) }
|
|
697
|
+
},
|
|
481
698
|
config
|
|
482
699
|
);
|
|
483
700
|
} else {
|
|
@@ -491,13 +708,15 @@ async function exportResult(data, options, isSnapshot, captured) {
|
|
|
491
708
|
{ cause: error }
|
|
492
709
|
);
|
|
493
710
|
}
|
|
711
|
+
let binding = options.type === import_core.UniverInstanceType.UNIVER_BOARD ? await (files || ole ? loadBoardFilesBinding() : loadBoardBinding()) : await loadNativeBinding(), resources = await collectExportResources(binding, input, nativeOptions, files, ole);
|
|
494
712
|
result = {
|
|
495
|
-
|
|
713
|
+
// Without resources the native call is the same as before this option existed.
|
|
714
|
+
value: resources === void 0 ? await binding.exchangeExportSnapshot(input, nativeOptions, configBuffer(config)) : await binding.exchangeExportSnapshot(input, nativeOptions, configBuffer(config), resources),
|
|
496
715
|
diagnostics: []
|
|
497
716
|
};
|
|
498
717
|
}
|
|
499
718
|
} catch (error) {
|
|
500
|
-
throw structuredExchangeError(error);
|
|
719
|
+
throw error instanceof CallbackFailure ? error.failure : structuredExchangeError(error);
|
|
501
720
|
}
|
|
502
721
|
return await deliverDiagnostics(options, result.diagnostics), result.value;
|
|
503
722
|
}
|