mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-23 23:10:17 +05:30
fix(image): lazy-load sharp with honest degradation so boot never fails
A missing/pruned @img/sharp-* binding made the top-level require('sharp')
crash src/main.js at startup, killing the packaged app before any window.
Load sharp through a cached lazy getter instead; when the native module
cannot load, executeOperation resolves the honest failure shape
{ success: false, error: 'Image operations unavailable: <sanitized>' }
(free of absolute paths), mirroring PDFOperations' Task-27 precedent.
Amit Haridas
This commit is contained in:
@@ -68,3 +68,57 @@ describe('ImageOperations', () => {
|
||||
await expect(ImageOperations.executeOperation('bogus', {})).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ImageOperations when sharp fails to load (boot resilience)', () => {
|
||||
// Reproduces the packaged-deb crash: the native @img/sharp-* bindings are
|
||||
// missing/pruned, so require('sharp') throws. Importing ImageOperations must
|
||||
// never crash the app at boot, and every operation must degrade honestly.
|
||||
const dlopenErrorMessage =
|
||||
'Could not load the "sharp" module using the linux-x64 runtime. ' +
|
||||
'ERR_DLOPEN_FAILED: libvips-cpp.so.8.17.3: cannot open shared object file ' +
|
||||
'(searched /opt/MarkdownConverter/resources/app.asar.unpacked/node_modules/@img/' +
|
||||
'sharp-linux-x64/lib, /opt/MarkdownConverter/resources/app.asar/node_modules/@img/' +
|
||||
'sharp-linux-x64/lib, ...)';
|
||||
|
||||
let isolatedModule;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.doMock('sharp', () => {
|
||||
throw new Error(dlopenErrorMessage);
|
||||
});
|
||||
jest.isolateModules(() => {
|
||||
isolatedModule = require('../../src/main/ImageOperations');
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.dontMock('sharp');
|
||||
});
|
||||
|
||||
test('requiring ImageOperations does not throw at import time', () => {
|
||||
expect(() => require('../../src/main/ImageOperations')).not.toThrow();
|
||||
});
|
||||
|
||||
test("executeOperation('convert') resolves to an honest unavailable failure", async () => {
|
||||
const result = await isolatedModule.executeOperation('convert', {
|
||||
inputPath: '/tmp/imgops-resilience-in.png',
|
||||
outputPath: '/tmp/imgops-resilience-out.jpg',
|
||||
format: 'jpeg',
|
||||
});
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
error: expect.stringContaining('Image operations unavailable'),
|
||||
});
|
||||
});
|
||||
|
||||
test('the unavailable failure message carries no absolute paths', async () => {
|
||||
const result = await isolatedModule.executeOperation('rotate', {
|
||||
inputPath: '/tmp/imgops-resilience-in.png',
|
||||
outputPath: '/tmp/imgops-resilience-out.png',
|
||||
angle: 90,
|
||||
});
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).not.toMatch(/\/opt\/|\/tmp\/|[A-Z]:\\/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user