@deepseek-ai/libreoffice-kit 0.1.1 → 0.1.3
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 +15 -4
- package/README.zh.md +16 -5
- package/lib/cli.js +318 -251
- package/lib/font-snapshot-worker.js +37 -26
- package/lib/index.js +318 -251
- package/lib/types/engine.d.ts +4 -5
- package/lib/types/font-file-identity.d.ts +13 -0
- package/lib/types/fonts.d.ts +2 -2
- package/lib/types/index.d.ts +1 -1
- package/lib/types/native.d.ts +8 -0
- package/lib/types/runtime.d.ts +1 -1
- package/lib/worker.js +26 -12
- package/package.json +7 -7
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { constants, lstatSync, readdirSync } from "node:fs";
|
|
3
|
-
import { lstat, mkdir, mkdtemp, open, readFile, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import { copyFile, lstat, mkdir, mkdtemp, open, readFile, rm, stat, symlink, unlink, writeFile } from "node:fs/promises";
|
|
4
4
|
import { homedir, tmpdir } from "node:os";
|
|
5
5
|
import { dirname, extname, isAbsolute, join, posix, relative, resolve, sep, win32 } from "node:path";
|
|
6
6
|
import { Worker } from "node:worker_threads";
|
|
@@ -559,6 +559,30 @@ function nativeEnvironment(profile, programDirectory, source = process.env, plat
|
|
|
559
559
|
});
|
|
560
560
|
}
|
|
561
561
|
/**
|
|
562
|
+
* Keep Windows engine resources reachable through a short profile-owned path until the helper exits.
|
|
563
|
+
* @param engine - Verified engine installation; its resources remain in place.
|
|
564
|
+
* @param profile - Private operation directory that owns the temporary executable and junction.
|
|
565
|
+
* @param run - Operation that settles only after its helper closes.
|
|
566
|
+
* @returns The operation result after removing the resource junction.
|
|
567
|
+
*/
|
|
568
|
+
async function withNativeEngine(engine, profile, run) {
|
|
569
|
+
if (process.platform !== "win32" || engine.executable.length < 260 && engine.programDirectory.length < 260) return run(engine);
|
|
570
|
+
const root = join(profile, "engine");
|
|
571
|
+
await symlink(engine.root, root, "junction");
|
|
572
|
+
try {
|
|
573
|
+
const executable = join(profile, "libreoffice-kit.exe");
|
|
574
|
+
await copyFile(engine.executable, executable, constants.COPYFILE_EXCL);
|
|
575
|
+
return await run({
|
|
576
|
+
...engine,
|
|
577
|
+
root,
|
|
578
|
+
executable,
|
|
579
|
+
programDirectory: join(root, relative(engine.root, engine.programDirectory))
|
|
580
|
+
});
|
|
581
|
+
} finally {
|
|
582
|
+
await unlink(root);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
562
586
|
* Run one native conversion and await bounded result transport.
|
|
563
587
|
* @param engine - Resolved native engine installation.
|
|
564
588
|
* @param options - Validated conversion limits.
|
|
@@ -578,77 +602,80 @@ async function runNative(engine, options, input, output, profile, fonts, substit
|
|
|
578
602
|
signal.throwIfAborted();
|
|
579
603
|
await prepareNativeFontProfile(profile, substitutions);
|
|
580
604
|
signal.throwIfAborted();
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
engine.
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
failure
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
618
|
-
child.stdout.setEncoding("utf8");
|
|
619
|
-
child.stderr.setEncoding("utf8");
|
|
620
|
-
child.stdout.on("data", (chunk) => {
|
|
621
|
-
stdout += chunk;
|
|
622
|
-
if (stdout.length > 65536) {
|
|
623
|
-
failure ??= /* @__PURE__ */ new Error("LibreOffice helper exceeded its response limit.");
|
|
605
|
+
return withNativeEngine(engine, profile, async (engine) => {
|
|
606
|
+
signal.throwIfAborted();
|
|
607
|
+
const env = nativeEnvironment(profile, engine.programDirectory);
|
|
608
|
+
const child = spawn(engine.executable, [
|
|
609
|
+
"--program-directory",
|
|
610
|
+
engine.programDirectory,
|
|
611
|
+
"--input-path",
|
|
612
|
+
input,
|
|
613
|
+
"--output-path",
|
|
614
|
+
output,
|
|
615
|
+
"--profile-directory",
|
|
616
|
+
profile,
|
|
617
|
+
"--max-output-bytes",
|
|
618
|
+
String(options.maxOutputBytes),
|
|
619
|
+
"--max-image-resolution",
|
|
620
|
+
String(options.maxImageResolution),
|
|
621
|
+
"--format",
|
|
622
|
+
operation.format,
|
|
623
|
+
"--recalculate",
|
|
624
|
+
String(operation.recalculate),
|
|
625
|
+
...operation.sheet === void 0 ? [] : ["--sheet", operation.sheet],
|
|
626
|
+
...fonts.flatMap((path) => ["--font-file", path])
|
|
627
|
+
], {
|
|
628
|
+
stdio: [
|
|
629
|
+
"ignore",
|
|
630
|
+
"pipe",
|
|
631
|
+
"pipe"
|
|
632
|
+
],
|
|
633
|
+
windowsHide: true,
|
|
634
|
+
env
|
|
635
|
+
});
|
|
636
|
+
let stdout = "";
|
|
637
|
+
let stderr = "";
|
|
638
|
+
let failure;
|
|
639
|
+
const abort = () => {
|
|
640
|
+
failure ??= signal.reason;
|
|
624
641
|
child.kill("SIGKILL");
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
stderr
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
else resolve(code);
|
|
639
|
-
});
|
|
642
|
+
};
|
|
643
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
644
|
+
child.stdout.setEncoding("utf8");
|
|
645
|
+
child.stderr.setEncoding("utf8");
|
|
646
|
+
child.stdout.on("data", (chunk) => {
|
|
647
|
+
stdout += chunk;
|
|
648
|
+
if (stdout.length > 65536) {
|
|
649
|
+
failure ??= /* @__PURE__ */ new Error("LibreOffice helper exceeded its response limit.");
|
|
650
|
+
child.kill("SIGKILL");
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
child.stderr.on("data", (chunk) => {
|
|
654
|
+
stderr = (stderr + chunk).slice(-65536);
|
|
640
655
|
});
|
|
641
|
-
signal.throwIfAborted();
|
|
642
|
-
let result;
|
|
643
656
|
try {
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
657
|
+
const code = await new Promise((resolve, reject) => {
|
|
658
|
+
child.once("error", (error) => {
|
|
659
|
+
failure ??= error;
|
|
660
|
+
});
|
|
661
|
+
child.once("close", (code, signal) => {
|
|
662
|
+
if (signal === "SIGXFSZ") failure ??= new ConversionError("output-too-large", "LibreOffice exceeded its output file-size limit.");
|
|
663
|
+
if (failure) reject(failure);
|
|
664
|
+
else resolve(code);
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
signal.throwIfAborted();
|
|
668
|
+
let result;
|
|
669
|
+
try {
|
|
670
|
+
result = JSON.parse(stdout);
|
|
671
|
+
} catch (cause) {
|
|
672
|
+
throw new Error(`LibreOffice helper returned an invalid response (exit ${code}). ${stderr}`, { cause });
|
|
673
|
+
}
|
|
674
|
+
if (code !== 0 || result.ok !== true) throw new ConversionError(failureCode(result), `LibreOffice native conversion failed: ${result.error ?? stderr}`);
|
|
675
|
+
} finally {
|
|
676
|
+
signal.removeEventListener("abort", abort);
|
|
647
677
|
}
|
|
648
|
-
|
|
649
|
-
} finally {
|
|
650
|
-
signal.removeEventListener("abort", abort);
|
|
651
|
-
}
|
|
678
|
+
});
|
|
652
679
|
}
|
|
653
680
|
//#endregion
|
|
654
681
|
//#region lib/types/png.js
|
|
@@ -785,179 +812,218 @@ async function renderImagesWithNative(request) {
|
|
|
785
812
|
signal.throwIfAborted();
|
|
786
813
|
await mkdir(profile, { mode: 448 });
|
|
787
814
|
await prepareNativeFontProfile(profile, substitutions);
|
|
788
|
-
|
|
789
|
-
"--operation",
|
|
790
|
-
"render-images",
|
|
791
|
-
"--program-directory",
|
|
792
|
-
engine.programDirectory,
|
|
793
|
-
"--input-path",
|
|
794
|
-
inputPath,
|
|
795
|
-
"--scratch-directory",
|
|
796
|
-
scratch,
|
|
797
|
-
"--profile-directory",
|
|
798
|
-
profile,
|
|
799
|
-
"--max-output-bytes",
|
|
800
|
-
String(options.maxOutputBytes),
|
|
801
|
-
"--max-image-resolution",
|
|
802
|
-
String(options.maxImageResolution),
|
|
803
|
-
...fonts.flatMap((path) => ["--font-file", path])
|
|
804
|
-
], {
|
|
805
|
-
stdio: [
|
|
806
|
-
"pipe",
|
|
807
|
-
"pipe",
|
|
808
|
-
"pipe"
|
|
809
|
-
],
|
|
810
|
-
windowsHide: true,
|
|
811
|
-
env: nativeEnvironment(profile, engine.programDirectory)
|
|
812
|
-
});
|
|
813
|
-
const lines = [];
|
|
814
|
-
const waiters = [];
|
|
815
|
-
const output = createInterface({ input: child.stdout });
|
|
816
|
-
output.on("line", (line) => {
|
|
817
|
-
const waiter = waiters.shift();
|
|
818
|
-
if (waiter) waiter(line);
|
|
819
|
-
else lines.push(line);
|
|
820
|
-
});
|
|
821
|
-
let stderr = "";
|
|
822
|
-
let stopped;
|
|
823
|
-
child.stderr.setEncoding("utf8");
|
|
824
|
-
child.stderr.on("data", (chunk) => {
|
|
825
|
-
stderr = (stderr + chunk).slice(-65536);
|
|
826
|
-
});
|
|
827
|
-
const closed = new Promise((resolve, reject) => {
|
|
828
|
-
child.once("error", reject);
|
|
829
|
-
child.once("close", resolve);
|
|
830
|
-
});
|
|
831
|
-
const abort = () => {
|
|
832
|
-
stopped ??= signal.reason;
|
|
833
|
-
child.kill("SIGKILL");
|
|
834
|
-
};
|
|
835
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
836
|
-
const next = async () => {
|
|
815
|
+
return withNativeEngine(engine, profile, async (engine) => {
|
|
837
816
|
signal.throwIfAborted();
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
817
|
+
const child = spawn(engine.executable, [
|
|
818
|
+
"--operation",
|
|
819
|
+
"render-images",
|
|
820
|
+
"--program-directory",
|
|
821
|
+
engine.programDirectory,
|
|
822
|
+
"--input-path",
|
|
823
|
+
inputPath,
|
|
824
|
+
"--scratch-directory",
|
|
825
|
+
scratch,
|
|
826
|
+
"--profile-directory",
|
|
827
|
+
profile,
|
|
828
|
+
"--max-output-bytes",
|
|
829
|
+
String(options.maxOutputBytes),
|
|
830
|
+
"--max-image-resolution",
|
|
831
|
+
String(options.maxImageResolution),
|
|
832
|
+
...fonts.flatMap((path) => ["--font-file", path])
|
|
833
|
+
], {
|
|
834
|
+
stdio: [
|
|
835
|
+
"pipe",
|
|
836
|
+
"pipe",
|
|
837
|
+
"pipe"
|
|
838
|
+
],
|
|
839
|
+
windowsHide: true,
|
|
840
|
+
env: nativeEnvironment(profile, engine.programDirectory)
|
|
851
841
|
});
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
const
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
842
|
+
const lines = [];
|
|
843
|
+
const waiters = [];
|
|
844
|
+
const output = createInterface({ input: child.stdout });
|
|
845
|
+
output.on("line", (line) => {
|
|
846
|
+
const waiter = waiters.shift();
|
|
847
|
+
if (waiter) waiter(line);
|
|
848
|
+
else lines.push(line);
|
|
849
|
+
});
|
|
850
|
+
let stderr = "";
|
|
851
|
+
let stopped;
|
|
852
|
+
child.stderr.setEncoding("utf8");
|
|
853
|
+
child.stderr.on("data", (chunk) => {
|
|
854
|
+
stderr = (stderr + chunk).slice(-65536);
|
|
855
|
+
});
|
|
856
|
+
const closed = new Promise((resolve, reject) => {
|
|
857
|
+
let failure;
|
|
858
|
+
child.once("error", (error) => {
|
|
859
|
+
failure = error;
|
|
860
|
+
});
|
|
861
|
+
child.once("close", (code) => {
|
|
862
|
+
if (failure) reject(failure);
|
|
863
|
+
else resolve(code);
|
|
864
|
+
});
|
|
865
|
+
});
|
|
866
|
+
const abort = () => {
|
|
867
|
+
stopped ??= signal.reason;
|
|
868
|
+
child.kill("SIGKILL");
|
|
869
|
+
};
|
|
870
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
871
|
+
const next = async () => {
|
|
872
|
+
signal.throwIfAborted();
|
|
873
|
+
if (lines.length > 0) return lines.shift();
|
|
874
|
+
return new Promise((resolve, reject) => {
|
|
875
|
+
const done = (line) => {
|
|
876
|
+
signal.removeEventListener("abort", cancel);
|
|
877
|
+
resolve(line);
|
|
878
|
+
};
|
|
879
|
+
const fail = (error) => {
|
|
880
|
+
const index = waiters.indexOf(done);
|
|
881
|
+
if (index >= 0) waiters.splice(index, 1);
|
|
882
|
+
signal.removeEventListener("abort", cancel);
|
|
883
|
+
reject(error);
|
|
884
|
+
};
|
|
885
|
+
const cancel = () => {
|
|
886
|
+
fail(signal.reason);
|
|
887
|
+
};
|
|
888
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
889
|
+
waiters.push(done);
|
|
890
|
+
closed.then((code) => {
|
|
891
|
+
fail(/* @__PURE__ */ new Error(`LibreOffice native renderer exited ${code ?? child.signalCode} before responding. ${stderr}`));
|
|
892
|
+
}, fail);
|
|
893
|
+
});
|
|
894
|
+
};
|
|
895
|
+
const response = async () => {
|
|
896
|
+
const line = await next();
|
|
897
|
+
let value;
|
|
873
898
|
try {
|
|
874
|
-
|
|
875
|
-
maxPixels: operation.maxPixels,
|
|
876
|
-
maxDimension: operation.maxDimension,
|
|
877
|
-
maxTiles: operation.maxPages - selected.length
|
|
878
|
-
});
|
|
899
|
+
value = JSON.parse(line);
|
|
879
900
|
} catch (cause) {
|
|
880
|
-
throw new
|
|
901
|
+
throw new Error(`LibreOffice native renderer returned invalid JSON. ${stderr}`, { cause });
|
|
881
902
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
rectangle
|
|
885
|
-
})));
|
|
886
|
-
}
|
|
887
|
-
for (const { page, rectangle } of selected) {
|
|
888
|
-
const { width, height } = tileDimensions({
|
|
889
|
-
pageIndex: 0,
|
|
890
|
-
...rectangle,
|
|
891
|
-
scale
|
|
892
|
-
}, [page]);
|
|
893
|
-
if (width > operation.maxDimension || height > operation.maxDimension || width * height > operation.maxPixels) throw new ConversionError("output-too-large", "Selected page exceeds maxPixels or maxDimension; reduce DPI or raise the image limits.");
|
|
894
|
-
}
|
|
895
|
-
const images = [];
|
|
896
|
-
let total = 0;
|
|
897
|
-
for (const region of selected) {
|
|
898
|
-
signal.throwIfAborted();
|
|
899
|
-
const { width, height } = tileDimensions({
|
|
900
|
-
pageIndex: 0,
|
|
901
|
-
...region.rectangle,
|
|
902
|
-
scale
|
|
903
|
-
}, [region.page]);
|
|
904
|
-
const x = region.page.x + Math.round(region.rectangle.x * 15);
|
|
905
|
-
const y = region.page.y + Math.round(region.rectangle.y * 15);
|
|
906
|
-
const twipsWidth = Math.max(1, Math.round(region.rectangle.width * 15));
|
|
907
|
-
const twipsHeight = Math.max(1, Math.round(region.rectangle.height * 15));
|
|
908
|
-
const index = images.length + 1;
|
|
909
|
-
child.stdin.write(`P ${index} ${region.page.part} ${width} ${height} ${x} ${y} ${twipsWidth} ${twipsHeight}\n`);
|
|
910
|
-
const painted = await response();
|
|
911
|
-
if (painted.kind !== "paint" || painted.index !== index || painted.width !== width || painted.height !== height) throw new Error("LibreOffice native renderer returned inconsistent tile metadata.");
|
|
912
|
-
if (resolve(painted.path) !== resolve(scratch, `tile-${index}.rgba`)) throw new Error("LibreOffice native renderer returned a tile outside its scratch directory.");
|
|
913
|
-
const raw = await readFile(painted.path);
|
|
914
|
-
if (raw.byteLength !== width * height * 4) throw new Error("LibreOffice native renderer returned incomplete pixels.");
|
|
915
|
-
const png = encodePng(width, height, ready.documentType === "pdf" ? new Uint8ClampedArray(raw) : rgbaPixels(raw, ready.tileMode));
|
|
916
|
-
total += png.byteLength;
|
|
917
|
-
if (total > options.maxOutputBytes) throw new ConversionError("output-too-large", "PNG batch exceeds maxOutputBytes.");
|
|
918
|
-
const path = join(operation.outputDir, `page-${String(index).padStart(4, "0")}.png`);
|
|
919
|
-
await writeFile(path, png, {
|
|
920
|
-
flag: "wx",
|
|
921
|
-
mode: 384
|
|
922
|
-
});
|
|
923
|
-
images.push({
|
|
924
|
-
index,
|
|
925
|
-
...region.number === void 0 ? {} : { page: region.number },
|
|
926
|
-
...region.sheet === void 0 ? {} : {
|
|
927
|
-
sheet: region.sheet,
|
|
928
|
-
range: region.range,
|
|
929
|
-
rtl: region.rtl
|
|
930
|
-
},
|
|
931
|
-
path,
|
|
932
|
-
width,
|
|
933
|
-
height,
|
|
934
|
-
rectangle: region.rectangle,
|
|
935
|
-
byteLength: png.byteLength
|
|
936
|
-
});
|
|
937
|
-
}
|
|
938
|
-
child.stdin.end("DONE\n");
|
|
939
|
-
if ((await response()).kind !== "done") throw new Error("LibreOffice native renderer did not finish.");
|
|
940
|
-
const code = await closed;
|
|
941
|
-
if (code !== 0) throw new Error(`LibreOffice native renderer exited ${code}. ${stderr}`);
|
|
942
|
-
return {
|
|
943
|
-
schemaVersion: 1,
|
|
944
|
-
backend: "native",
|
|
945
|
-
rasterEngine: ready.documentType === "pdf" ? "pdfium" : "libreoffice",
|
|
946
|
-
source: "saved",
|
|
947
|
-
inputPath: operation.inputPath,
|
|
948
|
-
sourceSha256: createHash("sha256").update(source).digest("hex"),
|
|
949
|
-
dpi: operation.dpi,
|
|
950
|
-
pageCount: planned.count,
|
|
951
|
-
images,
|
|
952
|
-
missingFonts: [...missingFonts]
|
|
903
|
+
if (!value.ok) throw failure(value, stderr);
|
|
904
|
+
return value;
|
|
953
905
|
};
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
906
|
+
try {
|
|
907
|
+
const ready = await response();
|
|
908
|
+
if (ready.kind !== "ready") throw new Error("LibreOffice native renderer did not initialize.");
|
|
909
|
+
const planned = plan(ready, operation);
|
|
910
|
+
const scale = operation.dpi / 96;
|
|
911
|
+
const selected = [];
|
|
912
|
+
for (const region of planned.regions) if (region.sheet === void 0) selected.push(region);
|
|
913
|
+
else {
|
|
914
|
+
let rectangles;
|
|
915
|
+
try {
|
|
916
|
+
rectangles = splitRasterRectangle(region.rectangle, scale, {
|
|
917
|
+
maxPixels: operation.maxPixels,
|
|
918
|
+
maxDimension: operation.maxDimension,
|
|
919
|
+
maxTiles: operation.maxPages - selected.length
|
|
920
|
+
});
|
|
921
|
+
} catch (cause) {
|
|
922
|
+
throw new ConversionError("output-too-large", String(cause), { cause });
|
|
923
|
+
}
|
|
924
|
+
selected.push(...rectangles.map((rectangle) => ({
|
|
925
|
+
...region,
|
|
926
|
+
rectangle
|
|
927
|
+
})));
|
|
928
|
+
}
|
|
929
|
+
for (const { page, rectangle } of selected) {
|
|
930
|
+
const { width, height } = tileDimensions({
|
|
931
|
+
pageIndex: 0,
|
|
932
|
+
...rectangle,
|
|
933
|
+
scale
|
|
934
|
+
}, [page]);
|
|
935
|
+
if (width > operation.maxDimension || height > operation.maxDimension || width * height > operation.maxPixels) throw new ConversionError("output-too-large", "Selected page exceeds maxPixels or maxDimension; reduce DPI or raise the image limits.");
|
|
936
|
+
}
|
|
937
|
+
const images = [];
|
|
938
|
+
let total = 0;
|
|
939
|
+
for (const region of selected) {
|
|
940
|
+
signal.throwIfAborted();
|
|
941
|
+
const { width, height } = tileDimensions({
|
|
942
|
+
pageIndex: 0,
|
|
943
|
+
...region.rectangle,
|
|
944
|
+
scale
|
|
945
|
+
}, [region.page]);
|
|
946
|
+
const x = region.page.x + Math.round(region.rectangle.x * 15);
|
|
947
|
+
const y = region.page.y + Math.round(region.rectangle.y * 15);
|
|
948
|
+
const twipsWidth = Math.max(1, Math.round(region.rectangle.width * 15));
|
|
949
|
+
const twipsHeight = Math.max(1, Math.round(region.rectangle.height * 15));
|
|
950
|
+
const index = images.length + 1;
|
|
951
|
+
child.stdin.write(`P ${index} ${region.page.part} ${width} ${height} ${x} ${y} ${twipsWidth} ${twipsHeight}\n`);
|
|
952
|
+
const painted = await response();
|
|
953
|
+
if (painted.kind !== "paint" || painted.index !== index || painted.width !== width || painted.height !== height) throw new Error("LibreOffice native renderer returned inconsistent tile metadata.");
|
|
954
|
+
if (resolve(painted.path) !== resolve(scratch, `tile-${index}.rgba`)) throw new Error("LibreOffice native renderer returned a tile outside its scratch directory.");
|
|
955
|
+
const raw = await readFile(painted.path);
|
|
956
|
+
if (raw.byteLength !== width * height * 4) throw new Error("LibreOffice native renderer returned incomplete pixels.");
|
|
957
|
+
const png = encodePng(width, height, ready.documentType === "pdf" ? new Uint8ClampedArray(raw) : rgbaPixels(raw, ready.tileMode));
|
|
958
|
+
total += png.byteLength;
|
|
959
|
+
if (total > options.maxOutputBytes) throw new ConversionError("output-too-large", "PNG batch exceeds maxOutputBytes.");
|
|
960
|
+
const path = join(operation.outputDir, `page-${String(index).padStart(4, "0")}.png`);
|
|
961
|
+
await writeFile(path, png, {
|
|
962
|
+
flag: "wx",
|
|
963
|
+
mode: 384
|
|
964
|
+
});
|
|
965
|
+
images.push({
|
|
966
|
+
index,
|
|
967
|
+
...region.number === void 0 ? {} : { page: region.number },
|
|
968
|
+
...region.sheet === void 0 ? {} : {
|
|
969
|
+
sheet: region.sheet,
|
|
970
|
+
range: region.range,
|
|
971
|
+
rtl: region.rtl
|
|
972
|
+
},
|
|
973
|
+
path,
|
|
974
|
+
width,
|
|
975
|
+
height,
|
|
976
|
+
rectangle: region.rectangle,
|
|
977
|
+
byteLength: png.byteLength
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
child.stdin.end("DONE\n");
|
|
981
|
+
if ((await response()).kind !== "done") throw new Error("LibreOffice native renderer did not finish.");
|
|
982
|
+
const code = await closed;
|
|
983
|
+
if (code !== 0) throw new Error(`LibreOffice native renderer exited ${code}. ${stderr}`);
|
|
984
|
+
return {
|
|
985
|
+
schemaVersion: 1,
|
|
986
|
+
backend: "native",
|
|
987
|
+
rasterEngine: ready.documentType === "pdf" ? "pdfium" : "libreoffice",
|
|
988
|
+
source: "saved",
|
|
989
|
+
inputPath: operation.inputPath,
|
|
990
|
+
sourceSha256: createHash("sha256").update(source).digest("hex"),
|
|
991
|
+
dpi: operation.dpi,
|
|
992
|
+
pageCount: planned.count,
|
|
993
|
+
images,
|
|
994
|
+
missingFonts: [...missingFonts]
|
|
995
|
+
};
|
|
996
|
+
} finally {
|
|
997
|
+
signal.removeEventListener("abort", abort);
|
|
998
|
+
output.close();
|
|
999
|
+
if (child.exitCode === null) child.kill("SIGKILL");
|
|
1000
|
+
await closed.catch(() => {});
|
|
1001
|
+
if (stopped !== void 0) throw stopped;
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
//#endregion
|
|
1006
|
+
//#region lib/types/font-file-identity.js
|
|
1007
|
+
/** Preserve 64-bit device and inode values without JavaScript integer rounding. */
|
|
1008
|
+
function fontFileIdentity(status) {
|
|
1009
|
+
return {
|
|
1010
|
+
dev: status.dev.toString(),
|
|
1011
|
+
ino: status.ino.toString(),
|
|
1012
|
+
size: Number(status.size),
|
|
1013
|
+
mtimeMs: Number(status.mtimeNs) / 1e6,
|
|
1014
|
+
ctimeMs: Number(status.ctimeNs) / 1e6
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
/** Compare a source descriptor's exact identity with its indexed font metadata. */
|
|
1018
|
+
function matchesFontFile(status, expected) {
|
|
1019
|
+
const current = fontFileIdentity(status);
|
|
1020
|
+
return [
|
|
1021
|
+
"dev",
|
|
1022
|
+
"ino",
|
|
1023
|
+
"size",
|
|
1024
|
+
"mtimeMs",
|
|
1025
|
+
"ctimeMs"
|
|
1026
|
+
].every((key) => current[key] === expected[key]);
|
|
961
1027
|
}
|
|
962
1028
|
//#endregion
|
|
963
1029
|
//#region lib/types/fonts.js
|
|
@@ -1209,11 +1275,18 @@ function resolveOptions(input = {}) {
|
|
|
1209
1275
|
//#region lib/types/engine.js
|
|
1210
1276
|
/** Resolve installed, version-matched engine packages without downloads or compilation. */
|
|
1211
1277
|
const require = createRequire(import.meta.url);
|
|
1212
|
-
/**
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1278
|
+
/** Node API release version returned by runtime discovery. */
|
|
1279
|
+
const ENGINE_VERSION = "0.1.3";
|
|
1280
|
+
/** Exact compatible engine versions; unchanged platforms retain their published packages. */
|
|
1281
|
+
const ENGINE_VERSIONS = Object.freeze({
|
|
1282
|
+
"darwin-arm64": "0.1.3",
|
|
1283
|
+
"darwin-x64": "0.1.3",
|
|
1284
|
+
"linux-arm64-glibc": "0.1.1",
|
|
1285
|
+
"linux-x64-glibc": "0.1.1",
|
|
1286
|
+
"win32-arm64": "0.1.3",
|
|
1287
|
+
"win32-x64": "0.1.3",
|
|
1288
|
+
wasm: "0.1.3"
|
|
1289
|
+
});
|
|
1217
1290
|
/** npm scope and name prefix shared by the engine packages this adapter installs. */
|
|
1218
1291
|
const ENGINE_PREFIX = "@deepseek-ai/libreoffice-kit";
|
|
1219
1292
|
/**
|
|
@@ -1292,7 +1365,7 @@ async function readEngine(packageFile, backend, target) {
|
|
|
1292
1365
|
const root = dirname(packageFile);
|
|
1293
1366
|
const [pkg, manifest] = await Promise.all([readFile(packageFile, "utf8").then((text) => JSON.parse(text)), readFile(resolve(root, "prebuilds.json"), "utf8").then((text) => JSON.parse(text))]);
|
|
1294
1367
|
const engine = manifest.engine;
|
|
1295
|
-
if (pkg.name !== `${ENGINE_PREFIX}-${target}` || pkg.version !==
|
|
1368
|
+
if (pkg.name !== `${ENGINE_PREFIX}-${target}` || pkg.version !== ENGINE_VERSIONS[target] || manifest.version !== pkg.version || manifest.schemaVersion !== 1 || manifest.status !== "built" || engine === void 0 || engine.kind !== backend || manifest.platform !== target) throw new Error(`Installed LibreOfficeKit ${backend} package has an incompatible or incomplete manifest.`);
|
|
1296
1369
|
const minimum = engine.glibcMinimum;
|
|
1297
1370
|
if (minimum !== void 0 && (!target.endsWith("-glibc") || !glibcVersion(minimum))) throw new Error("Installed LibreOfficeKit engine has an invalid glibcMinimum.");
|
|
1298
1371
|
const glibcFloor = minimum === void 0 ? {} : { glibcMinimum: minimum };
|
|
@@ -1410,7 +1483,7 @@ function validateOutput(bytes, format) {
|
|
|
1410
1483
|
/** Offline discovery of the installed engine and public entry points. */
|
|
1411
1484
|
/**
|
|
1412
1485
|
* Inspect installed engine metadata without starting, downloading, or building an engine.
|
|
1413
|
-
* @returns Public entry paths,
|
|
1486
|
+
* @returns Public entry paths, Node API version, and selected backend.
|
|
1414
1487
|
* @throws ConversionError when the installed engine is unavailable or invalid.
|
|
1415
1488
|
*/
|
|
1416
1489
|
async function discoverRuntime() {
|
|
@@ -1519,14 +1592,8 @@ var FontSnapshotState = class {
|
|
|
1519
1592
|
async function validateFontSnapshot(snapshot) {
|
|
1520
1593
|
await Promise.all(snapshot.records.map(async (record) => {
|
|
1521
1594
|
try {
|
|
1522
|
-
const current = await lstat(record.path);
|
|
1523
|
-
if (current.isFile() &&
|
|
1524
|
-
"dev",
|
|
1525
|
-
"ino",
|
|
1526
|
-
"size",
|
|
1527
|
-
"mtimeMs",
|
|
1528
|
-
"ctimeMs"
|
|
1529
|
-
].every((key) => current[key] === record[key])) return;
|
|
1595
|
+
const current = await lstat(record.path, { bigint: true });
|
|
1596
|
+
if (current.isFile() && matchesFontFile(current, record)) return;
|
|
1530
1597
|
} catch {}
|
|
1531
1598
|
throw new ConversionError("failed", "A system font changed during conversion; retry the operation.");
|
|
1532
1599
|
}));
|
|
@@ -1919,4 +1986,4 @@ async function createConverter(options) {
|
|
|
1919
1986
|
return createConverterWith(resolvedOptions, engine, defaultSharedState(resolvedOptions, engine));
|
|
1920
1987
|
}
|
|
1921
1988
|
//#endregion
|
|
1922
|
-
export { CONVERSION_FORMATS, ConversionError, ENGINE_VERSION, IMAGE_FORMATS, createConverter, createConverterFactory, discoverRuntime };
|
|
1989
|
+
export { CONVERSION_FORMATS, ConversionError, ENGINE_VERSION, ENGINE_VERSIONS, IMAGE_FORMATS, createConverter, createConverterFactory, discoverRuntime };
|
package/lib/types/engine.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export declare const ENGINE_VERSION = "0.1.1";
|
|
1
|
+
/** Node API release version returned by runtime discovery. */
|
|
2
|
+
export declare const ENGINE_VERSION = "0.1.3";
|
|
3
|
+
/** Exact compatible engine versions; unchanged platforms retain their published packages. */
|
|
4
|
+
export declare const ENGINE_VERSIONS: Readonly<Record<string, string>>;
|
|
6
5
|
/** Host diagnostic fields used to identify the runtime glibc version. */
|
|
7
6
|
export interface EngineHostReport {
|
|
8
7
|
readonly header?: {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Exact filesystem identifiers with JSON-compatible font metadata. */
|
|
2
|
+
import type { BigIntStats } from 'node:fs';
|
|
3
|
+
/** Preserve 64-bit device and inode values without JavaScript integer rounding. */
|
|
4
|
+
export declare function fontFileIdentity(status: BigIntStats): {
|
|
5
|
+
dev: string;
|
|
6
|
+
ino: string;
|
|
7
|
+
size: number;
|
|
8
|
+
mtimeMs: number;
|
|
9
|
+
ctimeMs: number;
|
|
10
|
+
};
|
|
11
|
+
/** Compare a source descriptor's exact identity with its indexed font metadata. */
|
|
12
|
+
export declare function matchesFontFile(status: BigIntStats, expected: ReturnType<typeof fontFileIdentity>): boolean;
|
|
13
|
+
//# sourceMappingURL=font-file-identity.d.ts.map
|