From a8f68ba7f0f6115d31108d2d83d49de8e93293c4 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Thu, 9 Nov 2023 14:46:07 +0000 Subject: [PATCH] Add infrastructure to build and publish as wasm32 (#3840) Co-authored-by: Ingvar Stepanyan --- .github/workflows/ci.yml | 32 +++++++++++++++++++++++++++++ docs/changelog.md | 3 +++ docs/humans.txt | 3 +++ docs/install.md | 9 ++++++++ docs/search-index.json | 2 +- lib/libvips.js | 30 +++++++++++++++++++++------ lib/sharp.js | 6 +++++- lib/utility.js | 14 +++++++++---- npm/from-github-release.js | 7 ++++--- npm/package.json | 1 + npm/wasm32/package.json | 42 ++++++++++++++++++++++++++++++++++++++ package.json | 12 ++++++++++- src/binding.gyp | 20 ++++++++++++++++++ src/emscripten/common.gypi | 40 ++++++++++++++++++++++++++++++++++++ src/emscripten/pre.js | 19 +++++++++++++++++ src/utilities.cc | 5 +++++ test/unit/libvips.js | 16 +++++++++++---- 17 files changed, 241 insertions(+), 20 deletions(-) create mode 100644 npm/wasm32/package.json create mode 100644 src/emscripten/common.gypi create mode 100644 src/emscripten/pre.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a23b7fb4..fefc0137f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -161,6 +161,38 @@ jobs: npm install --ignore-scripts npx mocha --no-config --spec=test/unit/io.js --timeout=30000 [[ -n $prebuild_upload ]] && cd src && ln -s ../package.json && npx prebuild || true + github-runner-emscripten: + permissions: + contents: write + name: wasm32 - prebuild + runs-on: ubuntu-22.04 + container: "emscripten/emsdk:3.1.48" + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Dependencies + run: apt-get update && apt-get install -y pkg-config + - name: Dependencies (Node.js) + uses: actions/setup-node@v3 + with: + node-version: "20" + - name: Install + run: emmake npm install --build-from-source + - name: Test + run: emmake npm test + - name: Test packaging + run: | + emmake npm run package-from-local-build + npm pkg set "optionalDependencies.@img/sharp-wasm32=file:./npm/wasm32" + npm run clean + npm install --cpu=wasm32 + npm test + - name: Prebuild + if: startsWith(github.ref, 'refs/tags/') + env: + npm_config_nodedir: emscripten + prebuild_upload: ${{ secrets.GITHUB_TOKEN }} + run: cd src && ln -s ../package.json && emmake npx prebuild --platform=emscripten --arch=wasm32 --strip=0 macstadium-runner: permissions: contents: write diff --git a/docs/changelog.md b/docs/changelog.md index 870c75590..8626761ef 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -14,6 +14,9 @@ Requires libvips v8.15.0 * Remove `sharp.vendor`. +* Add experimental support for WebAssembly-based runtimes. + [@RReverser](https://github.com/RReverser) + * Options for `trim` operation must be an Object, add new `lineArt` option. [#2363](https://github.com/lovell/sharp/issues/2363) diff --git a/docs/humans.txt b/docs/humans.txt index 79a6b4e8d..2bd51ac59 100644 --- a/docs/humans.txt +++ b/docs/humans.txt @@ -278,3 +278,6 @@ GitHub: https://github.com/bianjunjie1981 Name: Dennis Beatty GitHub: https://github.com/dnsbty + +Name: Ingvar Stepanyan +GitHub: https://github.com/RReverser diff --git a/docs/install.md b/docs/install.md index 7b96343df..c56f01fa2 100644 --- a/docs/install.md +++ b/docs/install.md @@ -78,6 +78,15 @@ For cross-compiling, the `--platform`, `--arch` and `--libc` npm flags (or the `npm_config_platform`, `npm_config_arch` and `npm_config_libc` environment variables) can be used to configure the target environment. +## WebAssembly + +Experimental support is provided for runtime environments that provide +multi-threaded Wasm via Workers. + +```sh +npm install --cpu=wasm32 sharp +``` + ## FreeBSD The `vips` package must be installed before `npm install` is run. diff --git a/docs/search-index.json b/docs/search-index.json index 5386316c8..aa5833e4d 100644 --- a/docs/search-index.json +++ b/docs/search-index.json @@ -1 +1 @@ -[{"t":"Prerequisites","d":"Node-API v9 compatible runtime e.g. Node.js 18.17.0","k":"prerequisites node api compatible runtime","l":"/install#prerequisites"},{"t":"Prebuilt binaries","d":"Ready-compiled sharp and libvips binaries are provided for use on the most common platforms macOS x64 10.13 macOS ARM64 Linux x64 glibc 2.26, musl 1.2.2, CPU with SSE4.2 Linux ARM64 glibc 2.26, musl 1","k":"prebuilt binaries compiled libvips common platforms macos arm linux glibc musl cpu sse","l":"/install#prebuilt-binaries"},{"t":"Custom libvips","d":"To use a custom, globally-installed version of libvips instead of the provided binaries, make sure it is at least the version listed under engines.libvips in the package.json file and that it can be l","k":"custom libvips globally installed version instead binaries listed engines package json file","l":"/install#custom-libvips"},{"t":"Building from source","d":"This module will be compiled from source at npm install time when a globally-installed libvips is detected set the SHARP_IGNORE_GLOBAL_LIBVIPS environment variable to skip this, or when the npm instal","k":"building source module compiled npm install time globally installed libvips detected environment variable skip instal","l":"/install#building-from-source"},{"t":"FreeBSD","d":"The vips package must be installed before npm install is run. sh pkg install -y pkgconf vips sh cd /usr/ports/graphics/vips/ make install clean","k":"freebsd vips package installed npm install pkg pkgconf usr ports graphics clean","l":"/install#freebsd"},{"t":"Linux memory allocator","d":"The default memory allocator on most glibc-based Linux systems e.g. Debian, Red Hat is unsuitable for long-running, multi-threaded processes that involve lots of small memory allocations. For this rea","k":"linux memory allocator glibc systems debian red hat long running multi threaded processes small allocations rea","l":"/install#linux-memory-allocator"},{"t":"AWS Lambda","d":"The node_modules directory of the deployment package must include binaries for either the linux-x64 or linux-arm64 platforms depending on the chosen architecture. When building your deployment package","k":"aws lambda nodemodules directory deployment package binaries linux arm platforms depending chosen architecture building","l":"/install#aws-lambda"},{"t":"webpack","d":"Ensure sharp is excluded from bundling via the externals configuration. js externals sharp commonjs sharp","k":"webpack excluded bundling externals configuration commonjs","l":"/install#webpack"},{"t":"esbuild","d":"Ensure sharp is excluded from bundling via the external","k":"esbuild excluded bundling external","l":"/install#esbuild"},{"t":"TypeScript","d":"TypeScript definitions are published as part of the sharp package from v0.32.0. Previously these were available via the types/sharp package, which is now deprecated. When using Typescript, please ensu","k":"typescript definitions published package types deprecated ensu","l":"/install#typescript"},{"t":"Fonts","d":"When creating text images or rendering SVG images that contain text elements, fontconfig is used to find the relevant fonts. On Windows and macOS systems, all system fonts are available for use. On ma","k":"fonts creating text images rendering svg contain elements fontconfig find relevant windows macos systems system","l":"/install#fonts"},{"t":"Worker threads","d":"On some platforms, including glibc-based Linux, the main thread must call requiresharp _before_ worker threads are created. This is to ensure shared libraries remain loaded in memory until after all t","k":"worker threads platforms glibc linux main thread shared libraries remain loaded memory","l":"/install#worker-threads"},{"t":"Canvas and Windows","d":"If both canvas and sharp modules are used in the same Windows process, the following error may occur The specified procedure could not be found.","k":"canvas windows modules process error procedure could found","l":"/install#canvas-and-windows"},{"t":"Sharp","d":"Emits codeSharpeventinfo/code, codeSharpeventwarning/code a namenew_Sharp_new/a","k":"emits code fail limit input pixels unlimited sequential read density ignore icc pages page subifd level animated raw create text","l":"/api-constructor#sharp"},{"t":"clone","d":"Take a snapshot of the Sharp instance, returning a new instance. Cloned instances inherit the input of their parent instance. This allows multiple output Streams and therefore multiple processing pip","k":"clone snapshot instance returning new cloned instances inherit input parent multiple output streams processing pip","l":"/api-constructor#clone"},{"t":"metadata","d":"Fast access to uncached image metadata without decoding any compressed pixel data.","k":"metadata fast access uncached decoding compressed pixel data","l":"/api-input#metadata"},{"t":"stats","d":"Access to pixel-derived image statistics for every channel in the image. A Promise is returned when callback is not provided.","k":"stats access pixel derived statistics channel promise","l":"/api-input#stats"},{"t":"toFile","d":"Write output image data to a file.","k":"tofile write output data file","l":"/api-output#tofile"},{"t":"toBuffer","d":"Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and raw pixel data output are supported.","k":"tobuffer write output buffer jpeg png webp avif tiff gif raw pixel data resolve object","l":"/api-output#tobuffer"},{"t":"withMetadata","d":"Include all metadata EXIF, XMP, IPTC from the input image in the output image. This will also convert to and add a web-friendly sRGB ICC profile if appropriate, unless a custom output profile is prov","k":"withmetadata metadata exif xmp iptc input output convert add web friendly srgb icc profile appropriate custom prov orientation density","l":"/api-output#withmetadata"},{"t":"toFormat","d":"Force output to a given format.","k":"toformat force output format","l":"/api-output#toformat"},{"t":"jpeg","d":"Use these JPEG options for output image.","k":"jpeg output quality progressive chroma subsampling optimise coding optimize mozjpeg trellis quantisation overshoot deringing scans table quantization force","l":"/api-output#jpeg"},{"t":"png","d":"Use these PNG options for output image.","k":"png output progressive compression level adaptive filtering palette quality effort colours colors dither force","l":"/api-output#png"},{"t":"webp","d":"Use these WebP options for output image.","k":"webp output quality alpha lossless near smart subsample preset effort loop delay min size mixed force","l":"/api-output#webp"},{"t":"gif","d":"Use these GIF options for the output image.","k":"gif output reuse progressive colours colors effort dither inter frame max error palette loop delay force","l":"/api-output#gif"},{"t":"tiff","d":"Use these TIFF options for output image.","k":"tiff output quality force compression predictor pyramid tile width height xres yres resolution unit bitdepth miniswhite","l":"/api-output#tiff"},{"t":"avif","d":"Use these AVIF options for output image.","k":"avif output quality lossless effort chroma subsampling","l":"/api-output#avif"},{"t":"heif","d":"Use these HEIF options for output image.","k":"heif output compression quality lossless effort chroma subsampling","l":"/api-output#heif"},{"t":"jxl","d":"Use these JPEG-XL JXL options for output image.","k":"jxl jpeg output distance quality decoding tier lossless effort depth","l":"/api-output#jxl"},{"t":"tile","d":"Use tile-based deep zoom image pyramid output.","k":"tile deep zoom pyramid output size overlap angle background depth skip blanks container layout centre center basename","l":"/api-output#tile"},{"t":"timeout","d":"Set a timeout for processing, in seconds. Use a value of zero to continue processing indefinitely, the default behaviour.","k":"timeout processing seconds zero continue indefinitely behaviour","l":"/api-output#timeout"},{"t":"resize","d":"Resize image to width, height or width x height.","k":"resize width height fit position background kernel enlargement reduction fast shrink load","l":"/api-resize#resize"},{"t":"extend","d":"Extend / pad / extrude one or more edges of the image with either the provided background colour or pixels derived from the image. This operation will always occur after resizing and extraction, if a","k":"extend pad extrude edges background colour pixels derived operation resizing extraction","l":"/api-resize#extend"},{"t":"extract","d":"Extract/crop a region of the image.","k":"extract crop region left top width height","l":"/api-resize#extract"},{"t":"trim","d":"Trim pixels from all edges that contain values similar to the given background colour, which defaults to that of the top-left pixel.","k":"trim pixels edges contain similar background colour defaults top left pixel threshold line art","l":"/api-resize#trim"},{"t":"composite","d":"Composite images over the processed resized, extracted etc. image.","k":"composite images processed resized extracted","l":"/api-composite#composite"},{"t":"rotate","d":"Rotate the output image by either an explicit angle or auto-orient based on the EXIF Orientation tag.","k":"rotate output explicit angle auto orient exif orientation tag background","l":"/api-operation#rotate"},{"t":"flip","d":"Mirror the image vertically up-down about the x-axis. This always occurs before rotation, if any.","k":"flip mirror vertically down axis rotation","l":"/api-operation#flip"},{"t":"flop","d":"Mirror the image horizontally left-right about the y-axis. This always occurs before rotation, if any.","k":"flop mirror horizontally left right axis rotation","l":"/api-operation#flop"},{"t":"affine","d":"Perform an affine transform on an image. This operation will always occur after resizing, extraction and rotation, if any.","k":"affine transform operation resizing extraction rotation background idx idy odx ody interpolator","l":"/api-operation#affine"},{"t":"sharpen","d":"Sharpen the image.","k":"sharpen sigma","l":"/api-operation#sharpen"},{"t":"median","d":"Apply median filter. When used without parameters the default window is 3x3.","k":"median apply filter parameters window","l":"/api-operation#median"},{"t":"blur","d":"Blur the image.","k":"blur","l":"/api-operation#blur"},{"t":"flatten","d":"Merge alpha transparency channel, if any, with a background, then remove the alpha channel.","k":"flatten merge alpha transparency channel background remove","l":"/api-operation#flatten"},{"t":"unflatten","d":"Ensure the image has an alpha channel with all white pixel values made fully transparent.","k":"unflatten alpha channel white pixel made fully transparent","l":"/api-operation#unflatten"},{"t":"gamma","d":"Apply a gamma correction by reducing the encoding darken pre-resize at a factor of 1/gamma then increasing the encoding brighten post-resize at a factor of gamma. This can improve the perceived brigh","k":"gamma apply correction reducing encoding darken resize factor increasing brighten post improve perceived brigh alpha","l":"/api-operation#gamma"},{"t":"normalise","d":"Enhance output image contrast by stretching its luminance to cover a full dynamic range.","k":"normalise enhance output contrast stretching luminance cover full dynamic range lower upper","l":"/api-operation#normalise"},{"t":"normalize","d":"Alternative spelling of normalise.","k":"normalize normalise lower upper","l":"/api-operation#normalize"},{"t":"clahe","d":"Perform contrast limiting adaptive histogram equalization CLAHE.","k":"clahe contrast limiting adaptive histogram equalization width height max slope","l":"/api-operation#clahe"},{"t":"convolve","d":"Convolve the image with the specified kernel.","k":"convolve kernel","l":"/api-operation#convolve"},{"t":"threshold","d":"Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0.","k":"threshold pixel greater equal otherwise greyscale grayscale raw","l":"/api-operation#threshold"},{"t":"recomb","d":"Recombine the image with the specified matrix.","k":"recomb recombine matrix","l":"/api-operation#recomb"},{"t":"modulate","d":"Transforms the image using brightness, saturation, hue rotation, and lightness. Brightness and lightness both operate on luminance, with the difference being that brightness is multiplicative whereas","k":"modulate transforms brightness saturation hue rotation lightness operate luminance difference being multiplicative whereas","l":"/api-operation#modulate"},{"t":"removeAlpha","d":"Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel.","k":"removealpha remove alpha channel","l":"/api-channel#removealpha"},{"t":"ensureAlpha","d":"Ensure the output image has an alpha transparency channel. If missing, the added alpha channel will have the specified transparency level, defaulting to fully-opaque 1. This is a no-op if the image a","k":"ensurealpha output alpha transparency channel missing added level defaulting fully opaque","l":"/api-channel#ensurealpha"},{"t":"extractChannel","d":"Extract a single channel from a multi-channel image.","k":"extractchannel extract single channel multi","l":"/api-channel#extractchannel"},{"t":"joinChannel","d":"Join one or more channels to the image. The meaning of the added channels depends on the output colourspace, set with toColourspace. By default the output image will be web-friendly sRGB, with additi","k":"joinchannel join channels meaning added depends output colourspace tocolourspace web friendly srgb additi","l":"/api-channel#joinchannel"},{"t":"tint","d":"Tint the image using the provided chroma while preserving the image luminance. An alpha channel may be present and will be unchanged by the operation.","k":"tint chroma preserving luminance alpha channel present unchanged operation","l":"/api-colour#tint"},{"t":"greyscale","d":"Convert to 8-bit greyscale 256 shades of grey. This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use gamma with greyscale for the best results. By default t","k":"greyscale convert bit shades grey linear operation input colour space srgb gamma results","l":"/api-colour#greyscale"},{"t":"grayscale","d":"Alternative spelling of greyscale.","k":"grayscale greyscale","l":"/api-colour#grayscale"},{"t":"pipelineColorspace","d":"Alternative spelling of pipelineColourspace.","k":"","l":"/api-colour#pipelinecolorspace"},{"t":"versions","d":"An Object containing the version numbers of sharp, libvips and when using prebuilt binaries its dependencies.","k":"versions object version numbers libvips prebuilt binaries dependencies","l":"/api-utility#versions"},{"t":"interpolators","d":"An Object containing the available interpolators and their proper values","k":"interpolators object proper","l":"/api-utility#interpolators"},{"t":"queue","d":"An EventEmitter that emits a change event when a task is either - queued, waiting for _libuv_ to provide a worker thread - complete","k":"queue eventemitter emits change event queued waiting libuv worker thread complete","l":"/api-utility#queue"},{"t":"cache","d":"Gets or, when options are provided, sets the limits of _libvips_ operation cache. Existing entries in the cache will be trimmed after any change in limits. This method always returns cache statistics","k":"cache limits libvips operation existing entries trimmed change method returns statistics memory files items","l":"/api-utility#cache"},{"t":"concurrency","d":"Gets or, when a concurrency is provided, sets the maximum number of threads _libvips_ should use to process _each image_. These are from a thread pool managed by glib, which helps avoid the overhead","k":"concurrency maximum number threads libvips process thread pool managed glib helps avoid overhead","l":"/api-utility#concurrency"},{"t":"counters","d":"Provides access to internal task counters. - queue is the number of tasks this module has queued waiting for _libuv_ to provide a worker thread from its pool. - process is the number of resize tasks","k":"counters provides access internal queue number tasks module queued waiting libuv worker thread pool process resize","l":"/api-utility#counters"},{"t":"simd","d":"Get and set use of SIMD vector unit instructions. Requires libvips to have been compiled with liborc support.","k":"simd vector unit instructions libvips compiled liborc","l":"/api-utility#simd"},{"t":"block","d":"Block libvips operations at runtime.","k":"block libvips operations runtime operation","l":"/api-utility#block"},{"t":"unblock","d":"Unblock libvips operations at runtime.","k":"unblock libvips operations runtime operation","l":"/api-utility#unblock"}] \ No newline at end of file +[{"t":"Prerequisites","d":"Node-API v9 compatible runtime e.g. Node.js 18.17.0","k":"prerequisites node api compatible runtime","l":"/install#prerequisites"},{"t":"Prebuilt binaries","d":"Ready-compiled sharp and libvips binaries are provided for use on the most common platforms macOS x64 10.13 macOS ARM64 Linux ARM glibc 2.28 Linux ARM64 glibc 2.26, musl 1.2.2 Linux s390x glibc 2.28 L","k":"prebuilt binaries compiled libvips common platforms macos arm linux glibc musl","l":"/install#prebuilt-binaries"},{"t":"Custom libvips","d":"To use a custom, globally-installed version of libvips instead of the provided binaries, make sure it is at least the version listed under engines.libvips in the package.json file and that it can be l","k":"custom libvips globally installed version instead binaries listed engines package json file","l":"/install#custom-libvips"},{"t":"Building from source","d":"This module will be compiled from source at npm install time when a globally-installed libvips is detected set the SHARP_IGNORE_GLOBAL_LIBVIPS environment variable to skip this, or when the npm instal","k":"building source module compiled npm install time globally installed libvips detected environment variable skip instal","l":"/install#building-from-source"},{"t":"WebAssembly","d":"Experimental support is provided for runtime environments that provide multi-threaded Wasm via Workers. sh npm install --cpuwasm32 sharp","k":"webassembly experimental runtime environments multi threaded wasm workers npm install cpuwasm","l":"/install#webassembly"},{"t":"FreeBSD","d":"The vips package must be installed before npm install is run. sh pkg install -y pkgconf vips sh cd /usr/ports/graphics/vips/ make install clean","k":"freebsd vips package installed npm install pkg pkgconf usr ports graphics clean","l":"/install#freebsd"},{"t":"Linux memory allocator","d":"The default memory allocator on most glibc-based Linux systems e.g. Debian, Red Hat is unsuitable for long-running, multi-threaded processes that involve lots of small memory allocations. For this rea","k":"linux memory allocator glibc systems debian red hat long running multi threaded processes small allocations rea","l":"/install#linux-memory-allocator"},{"t":"AWS Lambda","d":"The node_modules directory of the deployment package must include binaries for either the linux-x64 or linux-arm64 platforms depending on the chosen architecture. When building your deployment package","k":"aws lambda nodemodules directory deployment package binaries linux arm platforms depending chosen architecture building","l":"/install#aws-lambda"},{"t":"webpack","d":"Ensure sharp is excluded from bundling via the externals configuration. js externals sharp commonjs sharp","k":"webpack excluded bundling externals configuration commonjs","l":"/install#webpack"},{"t":"esbuild","d":"Ensure sharp is excluded from bundling via the external","k":"esbuild excluded bundling external","l":"/install#esbuild"},{"t":"TypeScript","d":"TypeScript definitions are published as part of the sharp package from v0.32.0. Previously these were available via the types/sharp package, which is now deprecated. When using Typescript, please ensu","k":"typescript definitions published package types deprecated ensu","l":"/install#typescript"},{"t":"Fonts","d":"When creating text images or rendering SVG images that contain text elements, fontconfig is used to find the relevant fonts. On Windows and macOS systems, all system fonts are available for use. On ma","k":"fonts creating text images rendering svg contain elements fontconfig find relevant windows macos systems system","l":"/install#fonts"},{"t":"Canvas and Windows","d":"If both canvas and sharp modules are used in the same Windows process, the following error may occur The specified procedure could not be found.","k":"canvas windows modules process error procedure could found","l":"/install#canvas-and-windows"},{"t":"Sharp","d":"Emits codeSharpeventinfo/code, codeSharpeventwarning/code a namenew_Sharp_new/a","k":"emits code fail limit input pixels unlimited sequential read density ignore icc pages page subifd level animated raw create text","l":"/api-constructor#sharp"},{"t":"clone","d":"Take a snapshot of the Sharp instance, returning a new instance. Cloned instances inherit the input of their parent instance. This allows multiple output Streams and therefore multiple processing pip","k":"clone snapshot instance returning new cloned instances inherit input parent multiple output streams processing pip","l":"/api-constructor#clone"},{"t":"metadata","d":"Fast access to uncached image metadata without decoding any compressed pixel data.","k":"metadata fast access uncached decoding compressed pixel data","l":"/api-input#metadata"},{"t":"stats","d":"Access to pixel-derived image statistics for every channel in the image. A Promise is returned when callback is not provided.","k":"stats access pixel derived statistics channel promise","l":"/api-input#stats"},{"t":"toFile","d":"Write output image data to a file.","k":"tofile write output data file","l":"/api-output#tofile"},{"t":"toBuffer","d":"Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and raw pixel data output are supported.","k":"tobuffer write output buffer jpeg png webp avif tiff gif raw pixel data resolve object","l":"/api-output#tobuffer"},{"t":"withMetadata","d":"Include all metadata EXIF, XMP, IPTC from the input image in the output image. This will also convert to and add a web-friendly sRGB ICC profile if appropriate, unless a custom output profile is prov","k":"withmetadata metadata exif xmp iptc input output convert add web friendly srgb icc profile appropriate custom prov orientation density","l":"/api-output#withmetadata"},{"t":"toFormat","d":"Force output to a given format.","k":"toformat force output format","l":"/api-output#toformat"},{"t":"jpeg","d":"Use these JPEG options for output image.","k":"jpeg output quality progressive chroma subsampling optimise coding optimize mozjpeg trellis quantisation overshoot deringing scans table quantization force","l":"/api-output#jpeg"},{"t":"png","d":"Use these PNG options for output image.","k":"png output progressive compression level adaptive filtering palette quality effort colours colors dither force","l":"/api-output#png"},{"t":"webp","d":"Use these WebP options for output image.","k":"webp output quality alpha lossless near smart subsample preset effort loop delay min size mixed force","l":"/api-output#webp"},{"t":"gif","d":"Use these GIF options for the output image.","k":"gif output reuse progressive colours colors effort dither inter frame max error palette loop delay force","l":"/api-output#gif"},{"t":"tiff","d":"Use these TIFF options for output image.","k":"tiff output quality force compression predictor pyramid tile width height xres yres resolution unit bitdepth miniswhite","l":"/api-output#tiff"},{"t":"avif","d":"Use these AVIF options for output image.","k":"avif output quality lossless effort chroma subsampling","l":"/api-output#avif"},{"t":"heif","d":"Use these HEIF options for output image.","k":"heif output compression quality lossless effort chroma subsampling","l":"/api-output#heif"},{"t":"jxl","d":"Use these JPEG-XL JXL options for output image.","k":"jxl jpeg output distance quality decoding tier lossless effort depth","l":"/api-output#jxl"},{"t":"tile","d":"Use tile-based deep zoom image pyramid output.","k":"tile deep zoom pyramid output size overlap angle background depth skip blanks container layout centre center basename","l":"/api-output#tile"},{"t":"timeout","d":"Set a timeout for processing, in seconds. Use a value of zero to continue processing indefinitely, the default behaviour.","k":"timeout processing seconds zero continue indefinitely behaviour","l":"/api-output#timeout"},{"t":"resize","d":"Resize image to width, height or width x height.","k":"resize width height fit position background kernel enlargement reduction fast shrink load","l":"/api-resize#resize"},{"t":"extend","d":"Extend / pad / extrude one or more edges of the image with either the provided background colour or pixels derived from the image. This operation will always occur after resizing and extraction, if a","k":"extend pad extrude edges background colour pixels derived operation resizing extraction","l":"/api-resize#extend"},{"t":"extract","d":"Extract/crop a region of the image.","k":"extract crop region left top width height","l":"/api-resize#extract"},{"t":"trim","d":"Trim pixels from all edges that contain values similar to the given background colour, which defaults to that of the top-left pixel.","k":"trim pixels edges contain similar background colour defaults top left pixel threshold line art","l":"/api-resize#trim"},{"t":"composite","d":"Composite images over the processed resized, extracted etc. image.","k":"composite images processed resized extracted","l":"/api-composite#composite"},{"t":"rotate","d":"Rotate the output image by either an explicit angle or auto-orient based on the EXIF Orientation tag.","k":"rotate output explicit angle auto orient exif orientation tag background","l":"/api-operation#rotate"},{"t":"flip","d":"Mirror the image vertically up-down about the x-axis. This always occurs before rotation, if any.","k":"flip mirror vertically down axis rotation","l":"/api-operation#flip"},{"t":"flop","d":"Mirror the image horizontally left-right about the y-axis. This always occurs before rotation, if any.","k":"flop mirror horizontally left right axis rotation","l":"/api-operation#flop"},{"t":"affine","d":"Perform an affine transform on an image. This operation will always occur after resizing, extraction and rotation, if any.","k":"affine transform operation resizing extraction rotation background idx idy odx ody interpolator","l":"/api-operation#affine"},{"t":"sharpen","d":"Sharpen the image.","k":"sharpen sigma","l":"/api-operation#sharpen"},{"t":"median","d":"Apply median filter. When used without parameters the default window is 3x3.","k":"median apply filter parameters window","l":"/api-operation#median"},{"t":"blur","d":"Blur the image.","k":"blur","l":"/api-operation#blur"},{"t":"flatten","d":"Merge alpha transparency channel, if any, with a background, then remove the alpha channel.","k":"flatten merge alpha transparency channel background remove","l":"/api-operation#flatten"},{"t":"unflatten","d":"Ensure the image has an alpha channel with all white pixel values made fully transparent.","k":"unflatten alpha channel white pixel made fully transparent","l":"/api-operation#unflatten"},{"t":"gamma","d":"Apply a gamma correction by reducing the encoding darken pre-resize at a factor of 1/gamma then increasing the encoding brighten post-resize at a factor of gamma. This can improve the perceived brigh","k":"gamma apply correction reducing encoding darken resize factor increasing brighten post improve perceived brigh alpha","l":"/api-operation#gamma"},{"t":"normalise","d":"Enhance output image contrast by stretching its luminance to cover a full dynamic range.","k":"normalise enhance output contrast stretching luminance cover full dynamic range lower upper","l":"/api-operation#normalise"},{"t":"normalize","d":"Alternative spelling of normalise.","k":"normalize normalise lower upper","l":"/api-operation#normalize"},{"t":"clahe","d":"Perform contrast limiting adaptive histogram equalization CLAHE.","k":"clahe contrast limiting adaptive histogram equalization width height max slope","l":"/api-operation#clahe"},{"t":"convolve","d":"Convolve the image with the specified kernel.","k":"convolve kernel","l":"/api-operation#convolve"},{"t":"threshold","d":"Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0.","k":"threshold pixel greater equal otherwise greyscale grayscale raw","l":"/api-operation#threshold"},{"t":"recomb","d":"Recombine the image with the specified matrix.","k":"recomb recombine matrix","l":"/api-operation#recomb"},{"t":"modulate","d":"Transforms the image using brightness, saturation, hue rotation, and lightness. Brightness and lightness both operate on luminance, with the difference being that brightness is multiplicative whereas","k":"modulate transforms brightness saturation hue rotation lightness operate luminance difference being multiplicative whereas","l":"/api-operation#modulate"},{"t":"removeAlpha","d":"Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel.","k":"removealpha remove alpha channel","l":"/api-channel#removealpha"},{"t":"ensureAlpha","d":"Ensure the output image has an alpha transparency channel. If missing, the added alpha channel will have the specified transparency level, defaulting to fully-opaque 1. This is a no-op if the image a","k":"ensurealpha output alpha transparency channel missing added level defaulting fully opaque","l":"/api-channel#ensurealpha"},{"t":"extractChannel","d":"Extract a single channel from a multi-channel image.","k":"extractchannel extract single channel multi","l":"/api-channel#extractchannel"},{"t":"joinChannel","d":"Join one or more channels to the image. The meaning of the added channels depends on the output colourspace, set with toColourspace. By default the output image will be web-friendly sRGB, with additi","k":"joinchannel join channels meaning added depends output colourspace tocolourspace web friendly srgb additi","l":"/api-channel#joinchannel"},{"t":"tint","d":"Tint the image using the provided chroma while preserving the image luminance. An alpha channel may be present and will be unchanged by the operation.","k":"tint chroma preserving luminance alpha channel present unchanged operation","l":"/api-colour#tint"},{"t":"greyscale","d":"Convert to 8-bit greyscale 256 shades of grey. This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use gamma with greyscale for the best results. By default t","k":"greyscale convert bit shades grey linear operation input colour space srgb gamma results","l":"/api-colour#greyscale"},{"t":"grayscale","d":"Alternative spelling of greyscale.","k":"grayscale greyscale","l":"/api-colour#grayscale"},{"t":"pipelineColorspace","d":"Alternative spelling of pipelineColourspace.","k":"","l":"/api-colour#pipelinecolorspace"},{"t":"versions","d":"An Object containing the version numbers of sharp, libvips and when using prebuilt binaries its dependencies.","k":"versions object version numbers libvips prebuilt binaries dependencies","l":"/api-utility#versions"},{"t":"interpolators","d":"An Object containing the available interpolators and their proper values","k":"interpolators object proper","l":"/api-utility#interpolators"},{"t":"queue","d":"An EventEmitter that emits a change event when a task is either - queued, waiting for _libuv_ to provide a worker thread - complete","k":"queue eventemitter emits change event queued waiting libuv worker thread complete","l":"/api-utility#queue"},{"t":"cache","d":"Gets or, when options are provided, sets the limits of _libvips_ operation cache. Existing entries in the cache will be trimmed after any change in limits. This method always returns cache statistics","k":"cache limits libvips operation existing entries trimmed change method returns statistics memory files items","l":"/api-utility#cache"},{"t":"concurrency","d":"Gets or, when a concurrency is provided, sets the maximum number of threads _libvips_ should use to process _each image_. These are from a thread pool managed by glib, which helps avoid the overhead","k":"concurrency maximum number threads libvips process thread pool managed glib helps avoid overhead","l":"/api-utility#concurrency"},{"t":"counters","d":"Provides access to internal task counters. - queue is the number of tasks this module has queued waiting for _libuv_ to provide a worker thread from its pool. - process is the number of resize tasks","k":"counters provides access internal queue number tasks module queued waiting libuv worker thread pool process resize","l":"/api-utility#counters"},{"t":"simd","d":"Get and set use of SIMD vector unit instructions. Requires libvips to have been compiled with liborc support.","k":"simd vector unit instructions libvips compiled liborc","l":"/api-utility#simd"},{"t":"block","d":"Block libvips operations at runtime.","k":"block libvips operations runtime operation","l":"/api-utility#block"},{"t":"unblock","d":"Unblock libvips operations at runtime.","k":"unblock libvips operations runtime operation","l":"/api-utility#unblock"}] \ No newline at end of file diff --git a/lib/libvips.js b/lib/libvips.js index b86046bb0..9d0f09d0b 100644 --- a/lib/libvips.js +++ b/lib/libvips.js @@ -41,15 +41,23 @@ const runtimePlatformArch = () => `${process.platform}${runtimeLibc()}-${process /* istanbul ignore next */ const buildPlatformArch = () => { + if (isEmscripten()) { + return 'wasm32'; + } /* eslint camelcase: ["error", { allow: ["^npm_config_"] }] */ const { npm_config_arch, npm_config_platform, npm_config_libc } = process.env; - return `${npm_config_platform || process.platform}${npm_config_libc || runtimeLibc()}-${npm_config_arch || process.arch}`; + const libc = typeof npm_config_libc === 'string' ? npm_config_libc : runtimeLibc(); + return `${npm_config_platform || process.platform}${libc}-${npm_config_arch || process.arch}`; }; const buildSharpLibvipsIncludeDir = () => { try { - return require('@img/sharp-libvips-dev/include'); - } catch {} + return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/include`); + } catch { + try { + return require('@img/sharp-libvips-dev/include'); + } catch {} + } /* istanbul ignore next */ return ''; }; @@ -64,12 +72,22 @@ const buildSharpLibvipsCPlusPlusDir = () => { const buildSharpLibvipsLibDir = () => { try { - return require(`@img/sharp-libvips-${buildPlatformArch()}/lib`); - } catch {} + return require(`@img/sharp-libvips-dev-${buildPlatformArch()}/lib`); + } catch { + try { + return require(`@img/sharp-libvips-${buildPlatformArch()}/lib`); + } catch {} + } /* istanbul ignore next */ return ''; }; +/* istanbul ignore next */ +const isEmscripten = () => { + const { CC } = process.env; + return Boolean(CC && CC.endsWith('/emcc')); +}; + const isRosetta = () => { /* istanbul ignore next */ if (process.platform === 'darwin' && process.arch === 'x64') { @@ -81,7 +99,7 @@ const isRosetta = () => { /* istanbul ignore next */ const spawnRebuild = () => - spawnSync('node-gyp rebuild --directory=src', { + spawnSync(`node-gyp rebuild --directory=src ${isEmscripten() ? '--nodedir=emscripten' : ''}`, { ...spawnSyncOptions, stdio: 'inherit' }).status; diff --git a/lib/sharp.js b/lib/sharp.js index 85c3d5938..7c3828591 100644 --- a/lib/sharp.js +++ b/lib/sharp.js @@ -12,7 +12,9 @@ const runtimePlatform = runtimePlatformArch(); const paths = [ `../src/build/Release/sharp-${runtimePlatform}.node`, - `@img/sharp-${runtimePlatform}/sharp.node` + '../src/build/Release/sharp-wasm32.node', + `@img/sharp-${runtimePlatform}/sharp.node`, + '@img/sharp-wasm32/sharp.node' ]; const errors = []; @@ -47,6 +49,8 @@ if (!module.exports) { help.push(` npm install --force @img/sharp-${runtimePlatform}`); } else { help.push(`- Manually install libvips >= ${minimumLibvipsVersion}`); + help.push('- Add experimental WebAssembly-based dependencies:'); + help.push(' npm install --cpu=wasm32 sharp'); } if (isLinux && /symbol not found/i.test(messages)) { try { diff --git a/lib/utility.js b/lib/utility.js index bddfe5d9b..fcdd7fe21 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -59,11 +59,17 @@ let versions = { }; /* istanbul ignore next */ if (!libvipsVersion.isGlobal) { - try { - versions = require(`@img/sharp-${runtimePlatform}/versions`); - } catch (_) { + if (!libvipsVersion.isWasm) { try { - versions = require(`@img/sharp-libvips-${runtimePlatform}/versions`); + versions = require(`@img/sharp-${runtimePlatform}/versions`); + } catch (_) { + try { + versions = require(`@img/sharp-libvips-${runtimePlatform}/versions`); + } catch (_) {} + } + } else { + try { + versions = require('@img/sharp-wasm32/versions'); } catch (_) {} } } diff --git a/npm/from-github-release.js b/npm/from-github-release.js index f78195b2e..2d20db13e 100644 --- a/npm/from-github-release.js +++ b/npm/from-github-release.js @@ -38,7 +38,8 @@ limitations under the License. `; workspaces.map(async platform => { - const url = `https://github.com/lovell/sharp/releases/download/v${version}/sharp-v${version}-napi-v9-${platform}.tar.gz`; + const prebuildPlatform = platform === 'wasm32' ? 'emscripten-wasm32' : platform; + const url = `https://github.com/lovell/sharp/releases/download/v${version}/sharp-v${version}-napi-v9-${prebuildPlatform}.tar.gz`; const dir = path.join(__dirname, platform); const response = await fetch(url); if (!response.ok) { @@ -58,8 +59,8 @@ workspaces.map(async platform => { await writeFile(path.join(dir, 'README.md'), `# \`${name}\`\n\n${description}.\n${licensing}`); // Copy Apache-2.0 LICENSE await copyFile(path.join(__dirname, '..', 'LICENSE'), path.join(dir, 'LICENSE')); - // Copy Windows-specific files - if (platform.startsWith('win32-')) { + // Copy files for packages without an explicit sharp-libvips dependency (Windows, wasm) + if (platform.startsWith('win') || platform.startsWith('wasm')) { const sharpLibvipsDir = path.join(require(`@img/sharp-libvips-${platform}/lib`), '..'); // Copy versions.json await copyFile(path.join(sharpLibvipsDir, 'versions.json'), path.join(dir, 'versions.json')); diff --git a/npm/package.json b/npm/package.json index 761d708f6..e963c2562 100644 --- a/npm/package.json +++ b/npm/package.json @@ -11,6 +11,7 @@ "linux-x64", "linuxmusl-arm64", "linuxmusl-x64", + "wasm32", "win32-ia32", "win32-x64" ] diff --git a/npm/wasm32/package.json b/npm/wasm32/package.json new file mode 100644 index 000000000..d9eb629d2 --- /dev/null +++ b/npm/wasm32/package.json @@ -0,0 +1,42 @@ +{ + "name": "@img/sharp-wasm32", + "version": "0.33.0-alpha.10", + "description": "Prebuilt sharp for use with wasm32", + "author": "Lovell Fuller ", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp.git", + "directory": "npm/wasm32" + }, + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "files": [ + "lib", + "versions.json" + ], + "publishConfig": { + "access": "public" + }, + "type": "commonjs", + "exports": { + "./sharp.node": "./lib/sharp-wasm32.node.js", + "./package": "./package.json", + "./versions": "./versions.json" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0", + "npm": ">=9.6.5", + "yarn": ">=3.2.0", + "pnpm": ">=7.1.0" + }, + "dependencies": { + "@emnapi/runtime": "^0.43.1" + }, + "cpu": [ + "wasm32" + ] +} diff --git a/package.json b/package.json index 299162500..398eaa871 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,8 @@ "Brahim Ait elhaj ", "Mart Jansink ", "Lachlan Newman ", - "Dennis Beatty " + "Dennis Beatty ", + "Ingvar Stepanyan " ], "scripts": { "install": "node install/check", @@ -156,16 +157,20 @@ "@img/sharp-linux-x64": "0.33.0-alpha.10", "@img/sharp-linuxmusl-arm64": "0.33.0-alpha.10", "@img/sharp-linuxmusl-x64": "0.33.0-alpha.10", + "@img/sharp-wasm32": "0.33.0-alpha.10", "@img/sharp-win32-ia32": "0.33.0-alpha.10", "@img/sharp-win32-x64": "0.33.0-alpha.10" }, "devDependencies": { + "@emnapi/runtime": "^0.43.1", "@img/sharp-libvips-dev": "0.0.3", + "@img/sharp-libvips-dev-wasm32": "0.0.3", "@img/sharp-libvips-win32-ia32": "0.0.3", "@img/sharp-libvips-win32-x64": "0.0.3", "@types/node": "*", "async": "^3.2.5", "cc": "^3.0.1", + "emnapi": "^0.43.1", "exif-reader": "^2.0.0", "extract-zip": "^2.0.1", "icc": "^3.0.0", @@ -203,6 +208,11 @@ "build/include" ] }, + "nyc": { + "include": [ + "lib" + ] + }, "tsd": { "directory": "test/types/" } diff --git a/src/binding.gyp b/src/binding.gyp index ef98b13c0..3811a4ba7 100644 --- a/src/binding.gyp +++ b/src/binding.gyp @@ -179,6 +179,26 @@ '-Wl,-rpath=\'$$ORIGIN/../../../node_modules/@img/sharp-libvips-<(platform_and_arch)/lib\'' ] } + }], + ['OS == "emscripten"', { + 'product_extension': 'node.js', + 'link_settings': { + 'ldflags': [ + '-fexceptions', + '--pre-js= JSON.stringify(path.relative(process.cwd(), x))).join(\' \')")' + ], + 'configurations': { + 'Release': {} + } + } +} diff --git a/src/emscripten/pre.js b/src/emscripten/pre.js new file mode 100644 index 000000000..d37a487de --- /dev/null +++ b/src/emscripten/pre.js @@ -0,0 +1,19 @@ +// Copyright 2013 Lovell Fuller and others. +// SPDX-License-Identifier: Apache-2.0 + +/* global Module, ENV, _vips_shutdown, _uv_library_shutdown */ + +Module.preRun = () => { + ENV.VIPS_CONCURRENCY = Number(process.env.VIPS_CONCURRENCY) || 1; +}; + +Module.onRuntimeInitialized = () => { + module.exports = Module.emnapiInit({ + context: require('@emnapi/runtime').getDefaultContext() + }); + + process.once('exit', () => { + _vips_shutdown(); + _uv_library_shutdown(); + }); +}; diff --git a/src/utilities.cc b/src/utilities.cc index 9283a357b..070374048 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -102,6 +102,11 @@ Napi::Value libvipsVersion(const Napi::CallbackInfo& info) { version.Set("isGlobal", Napi::Boolean::New(env, true)); #else version.Set("isGlobal", Napi::Boolean::New(env, false)); +#endif +#ifdef __EMSCRIPTEN__ + version.Set("isWasm", Napi::Boolean::New(env, true)); +#else + version.Set("isWasm", Napi::Boolean::New(env, false)); #endif return version; } diff --git a/test/unit/libvips.js b/test/unit/libvips.js index 7ab720fd0..ea5c28b9a 100644 --- a/test/unit/libvips.js +++ b/test/unit/libvips.js @@ -69,17 +69,25 @@ describe('libvips binaries', function () { }); describe('Build time platform detection', () => { - it('Can override platform with npm_config_platform and npm_config_libc', () => { + it('Can override platform with npm_config_platform and npm_config_libc', function () { process.env.npm_config_platform = 'testplatform'; process.env.npm_config_libc = 'testlibc'; - const [platform] = libvips.buildPlatformArch().split('-'); + const platformArch = libvips.buildPlatformArch(); + if (platformArch === 'wasm32') { + return this.skip(); + } + const [platform] = platformArch.split('-'); assert.strictEqual(platform, 'testplatformtestlibc'); delete process.env.npm_config_platform; delete process.env.npm_config_libc; }); - it('Can override arch with npm_config_arch', () => { + it('Can override arch with npm_config_arch', function () { process.env.npm_config_arch = 'test'; - const [, arch] = libvips.buildPlatformArch().split('-'); + const platformArch = libvips.buildPlatformArch(); + if (platformArch === 'wasm32') { + return this.skip(); + } + const [, arch] = platformArch.split('-'); assert.strictEqual(arch, 'test'); delete process.env.npm_config_arch; });