fix(pack): lazy-load sharp and verify unpacked natives

Avoid crashing Electron at startup when sharp is corrupt, and fail pack if asarUnpack natives look truncated.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Ivan Fontosh
2026-08-07 07:47:36 +08:00
parent 4456eb0277
commit 8d5a68c71e
8 changed files with 370 additions and 7 deletions
+9 -1
View File
@@ -2,7 +2,7 @@
* Visually lossless re-encode for imported raster images (same pixel dimensions).
* Node-only; shared by the main app and ../project-converter (monorepo sibling).
*/
import sharp from 'sharp';
import { getSharp } from './sharpRuntime.mjs';
/** @typedef {import('node:buffer').Buffer} Buffer */
@@ -102,6 +102,7 @@ function makePassthrough(buf, meta) {
* @param {number} h0
*/
async function sameDimensionsOrThrow(outBuf, w0, h0) {
const sharp = getSharp();
const m = await sharp(outBuf).metadata();
if ((m.width ?? 0) !== w0 || (m.height ?? 0) !== h0) {
const err = new Error('encode changed dimensions');
@@ -120,6 +121,13 @@ export async function optimizeImageBufferVisuallyLossless(src) {
return makePassthrough(input, { width: 0, height: 0, format: 'png' });
}
let sharp;
try {
sharp = getSharp();
} catch {
return makePassthrough(input, null);
}
let meta0;
try {
meta0 = await sharp(input, { failOn: 'error', unlimited: true }).metadata();