├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── api └── index.js ├── fixture.gif ├── fixture.png ├── license ├── now.json ├── package-lock.json ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .now 2 | node_modules 3 | yarn.lock 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '12' 4 | -------------------------------------------------------------------------------- /api/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const path = require('path'); 3 | const cwepb = require('cwebp-bin'); 4 | const execa = require('execa'); 5 | const got = require('got'); 6 | 7 | const handleError = (error, response) => { 8 | console.error(error); 9 | 10 | response.status(500); 11 | response.end(); 12 | }; 13 | 14 | module.exports = (request, response) => { 15 | const args = ['-quiet', '-mt']; 16 | const { 17 | alphaQuality, 18 | autoFilter, 19 | filter, 20 | height = 0, 21 | lossless, 22 | method, 23 | nearLossless, 24 | preset, 25 | quality, 26 | sharpness, 27 | size, 28 | sns, 29 | url, 30 | width = 0 31 | } = request.query; 32 | 33 | response.setHeader('vary', 'accept'); 34 | 35 | if (request.headers.accept && !request.headers.accept.includes('image/webp')) { 36 | response.status(302); 37 | response.setHeader('location', url); 38 | response.end(); 39 | return; 40 | } 41 | 42 | if (preset) { 43 | args.push('-preset', preset); 44 | } 45 | 46 | if (quality) { 47 | args.push('-q', quality); 48 | } 49 | 50 | if (alphaQuality) { 51 | args.push('-alpha_q', alphaQuality); 52 | } 53 | 54 | if (method) { 55 | args.push('-m', method); 56 | } 57 | 58 | if (size) { 59 | args.push('-size', size); 60 | } 61 | 62 | if (sns) { 63 | args.push('-sns', sns); 64 | } 65 | 66 | if (filter) { 67 | args.push('-f', filter); 68 | } 69 | 70 | if (autoFilter) { 71 | args.push('-af'); 72 | } 73 | 74 | if (sharpness) { 75 | args.push('-sharpness', sharpness); 76 | } 77 | 78 | if (lossless) { 79 | args.push('-lossless'); 80 | } 81 | 82 | if (nearLossless) { 83 | args.push('-near_lossless', nearLossless); 84 | } 85 | 86 | if (height || width) { 87 | args.push('-resize', width, height); 88 | } 89 | 90 | const libPath = path.join(__dirname, '..', 'lib64'); 91 | const imageStream = got.stream(url); 92 | const cwebpStream = execa(cwepb, [...args, '-o', '-', '--', '-'], { 93 | encoding: null, 94 | env: {LD_LIBRARY_PATH: `${libPath}:${process.env.LD_LIBRARY_PATH}`}, 95 | input: imageStream 96 | }); 97 | 98 | cwebpStream.stderr.setEncoding('utf8'); 99 | cwebpStream.stderr.on('data', data => { 100 | if (data.includes('Could not process file')) { 101 | response.status(302); 102 | response.setHeader('location', url); 103 | response.end(); 104 | return; 105 | } 106 | 107 | handleError(data, response); 108 | }); 109 | 110 | cwebpStream.on('error', ({message}) => { 111 | handleError(message, response); 112 | }); 113 | 114 | imageStream.on('error', ({message}) => { 115 | handleError(message, response); 116 | }); 117 | 118 | response.status(200); 119 | response.setHeader('content-type', 'image/webp'); 120 | response.setHeader('cache-control', 'public, immutable, no-transform, s-maxage=31536000, max-age=31536000'); 121 | 122 | cwebpStream.stdout.pipe(response); 123 | }; 124 | -------------------------------------------------------------------------------- /fixture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevva/to-webp/26de090cf9091deba198653a3ed0b1172a809e7b/fixture.gif -------------------------------------------------------------------------------- /fixture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevva/to-webp/26de090cf9091deba198653a3ed0b1172a809e7b/fixture.png -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Martensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /now.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "functions": { 4 | "api/index.js": { 5 | "includeFiles": "lib64" 6 | } 7 | }, 8 | "rewrites": [ 9 | { 10 | "source": "/", 11 | "destination": "/api/index.js" 12 | } 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "engines": { 4 | "node": ">=12" 5 | }, 6 | "scripts": { 7 | "copy-libs": "mkdir -p ./lib64 && cp /usr/lib64/{libGL.so.1,libX11.so.6,libXxf86vm.so.1,libXi.so.6,libGLX.so.0,libXext.so.6,libGLdispatch.so.0,libxcb.so.1,libXau.so.6} ./lib64", 8 | "now-build": "[[ $NOW_REGION != dev1 ]] && npm run copy-libs || exit 0", 9 | "test": "xo && NOW_REGION=dev1 ava" 10 | }, 11 | "dependencies": { 12 | "cwebp-bin": "^6.1.1", 13 | "execa": "^4.0.3", 14 | "got": "^10.7.0" 15 | }, 16 | "devDependencies": { 17 | "@ava/babel": "^1.0.1", 18 | "@now/node": "^1.8.4", 19 | "ava": "^3.13.0", 20 | "is-gif": "^3.0.0", 21 | "is-webp": "^1.0.1", 22 | "nock": "^11.9.1", 23 | "test-listen": "^1.1.0", 24 | "xo": "^0.25.4" 25 | }, 26 | "ava": { 27 | "babel": true 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # to-webp [![Build Status](https://travis-ci.com/kevva/to-webp.svg?branch=master)](https://travis-ci.com/kevva/to-webp) 2 | 3 | > Service for converting images to WebP 4 | 5 | [![Deploy to now](https://deploy.now.sh/static/button.svg)](https://zeit.co/new/project?template=kevva/to-webp) 6 | 7 | 8 | ## Usage 9 | 10 | ```html 11 | 12 | ``` 13 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import test from 'ava'; 3 | import {createServerWithHelpers} from '@now/node/dist/helpers'; 4 | import got from 'got'; 5 | import isGif from 'is-gif'; 6 | import isWebp from 'is-webp'; 7 | import nock from 'nock'; 8 | import testListen from 'test-listen'; 9 | import toWebp from './api'; 10 | 11 | test.before(async t => { 12 | t.context.url = await testListen( 13 | createServerWithHelpers(toWebp, {consumeEvent: () => ({})}) 14 | ); 15 | 16 | nock('http://foo.bar') 17 | .persist() 18 | .get('/fixture') 19 | .replyWithFile(200, path.join(__dirname, 'fixture.png')) 20 | .get('/invalid-fixture') 21 | .replyWithFile(200, path.join(__dirname, 'fixture.gif')); 22 | }); 23 | 24 | test('convert png to webp', async t => { 25 | const {body} = await got(t.context.url, { 26 | headers: {'x-now-bridge-request-id': 1}, 27 | responseType: 'buffer', 28 | searchParams: {url: 'http://foo.bar/fixture'} 29 | }); 30 | 31 | t.true(isWebp(body)); 32 | }); 33 | 34 | test('ignore unsupported formats', async t => { 35 | const {body} = await got(t.context.url, { 36 | headers: {'x-now-bridge-request-id': 1}, 37 | responseType: 'buffer', 38 | searchParams: {url: 'http://foo.bar/invalid-fixture'} 39 | }); 40 | 41 | t.true(isGif(body)); 42 | }); 43 | --------------------------------------------------------------------------------