From 286c9903b8ba592666afef8ce2d1d8576aece1d3 Mon Sep 17 00:00:00 2001 From: Amirhossein Khalili Date: Tue, 15 Sep 2026 23:00:46 +0330 Subject: [PATCH] build: check Sharp runtime before compiling the application --- Dockerfile | 3 +++ scripts/check-image-runtime.mjs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 scripts/check-image-runtime.mjs diff --git a/Dockerfile b/Dockerfile index 42b8c31..0314fb4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,9 @@ RUN --mount=type=cache,id=portfolio-npm-node22,target=/root/.npm,sharing=locked --replace-registry-host=registry.npmjs.org \ --include=optional +COPY scripts/check-image-runtime.mjs ./scripts/check-image-runtime.mjs +RUN node scripts/check-image-runtime.mjs + FROM dependencies AS verification COPY . . diff --git a/scripts/check-image-runtime.mjs b/scripts/check-image-runtime.mjs new file mode 100644 index 0000000..896e3b7 --- /dev/null +++ b/scripts/check-image-runtime.mjs @@ -0,0 +1,26 @@ +import { createRequire } from 'node:module'; + +const require = createRequire(import.meta.url); + +// Check the fallback directly: Sharp 0.35.4 can hide its load error behind +// an unrelated `err.code.endsWith` exception on unsupported native CPUs. +try { + require('@img/sharp-wasm32/sharp.node'); + console.log('Sharp WebAssembly binding loaded.'); +} catch (error) { + console.error('Sharp WebAssembly binding failed to load:', error); + process.exit(1); +} + +try { + const { default: sharp } = await import('sharp'); + const input = await sharp({ create: { width: 8, height: 8, channels: 3, background: '#336699' } }).webp().toBuffer(); + const metadata = await sharp(input).metadata(); + if (metadata.format !== 'webp' || metadata.width !== 8 || metadata.height !== 8) { + throw new Error('Image encode/decode verification failed'); + } + console.log('Sharp ESM import and image processing passed.'); +} catch (error) { + console.error('Sharp image runtime failed:', error); + process.exit(1); +}