Skip to content

Commit

Permalink
switch to node:test, add prettier, bump sharp
Browse files Browse the repository at this point in the history
  • Loading branch information
kotarac committed Jan 12, 2024
1 parent eb09763 commit 692131e
Show file tree
Hide file tree
Showing 9 changed files with 686 additions and 3,533 deletions.
17 changes: 0 additions & 17 deletions .eslintrc

This file was deleted.

18 changes: 9 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
matrix:
node-version: [18.x, 20.x]
steps:
- uses: actions/checkout@v2
- name: node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm run test
- uses: actions/checkout@v2
- name: node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm run test
9 changes: 9 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
!.prettierrc.cjs
*.pem
.DS_Store
.idea/
.vscode/
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
6 changes: 6 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
printWidth: 120,
semi: false,
singleQuote: true,
trailingComma: 'all',
}
48 changes: 22 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# slicica [![npm version](https://badge.fury.io/js/slicica.svg)](https://www.npmjs.com/package/slicica)

_diminutive for **image** in croatian_

Image serving/resizing/optimizing Express/Connect middleware using [sharp](https://github.com/lovell/sharp)/[libvips](https://github.com/jcupitt/libvips).

Intended for usage behind a proxy cache (e.g. nginx, varnish) or a CDN (e.g. CloudFlare) as it doesn't cache results on its own.


## Install

```sh
npm i -S slicica
npm add slicica
```

Installing this module will automatically fetch and build libvips and its dependencies on Linux, MacOS and Windows x64.

For more information read [sharp's documentation](https://sharp.pixelplumbing.com/).


## Usage

```js
Expand All @@ -27,38 +26,36 @@ app.use(require('slicica')(options))
const app = express()
const slicica = require('slicica')

app.use(slicica(
// default options below
{
prefix: '/', // url prefix on which to serve the images
root: '', // root folder / prefix to prepend to the requested image (path where the images reside)
maxAge: 0, // takes seconds as integer || ms compatible string || false to disable
progressive: false, // progressive scan for JPG and PNG
quality: 80, // output quality for jpeg, webp, png, tiff
compression: 6, // image compression level (0-9)
etag: true, // generate and send the ETag header
lastModified: true, // send the Last-Modified header
cache: false, // options passed to sharp.cache
concurrency: 0, // number of threads sharp will use (0 resets it to default = number of cores)
contentTypes: [
'image/jpeg',
'image/png',
'image/webp',
'image/tiff'
], // content types to serve, other requests are ignored
}
))
app.use(
slicica(
// default options below
{
prefix: '/', // url prefix on which to serve the images
root: '', // root folder / prefix to prepend to the requested image (path where the images reside)
maxAge: 0, // takes seconds as integer || ms compatible string || false to disable
progressive: false, // progressive scan for JPG and PNG
quality: 80, // output quality for jpeg, webp, png, tiff
compression: 6, // image compression level (0-9)
etag: true, // generate and send the ETag header
lastModified: true, // send the Last-Modified header
cache: false, // options passed to sharp.cache
concurrency: 0, // number of threads sharp will use (0 resets it to default = number of cores)
contentTypes: ['image/jpeg', 'image/png', 'image/webp', 'image/tiff'], // content types to serve, other requests are ignored
},
),
)
```


## Request example

```
http://localhost:####/images/random.jpg?w=800
```

```
http://localhost:####/images/random.jpg?h=600
```

```
http://localhost:####/images/random.jpg?w=300&h=200&g=north
```
Expand All @@ -76,7 +73,6 @@ if (w && h) {
}
```


## License

MIT © Stipe Kotarac (https://github.com/kotarac)
23 changes: 7 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ const mime = require('mime')
const ms = require('ms')
const sharp = require('sharp')

const imageTypes = [
'image/jpeg',
'image/png',
'image/webp',
'image/tiff',
]
const imageTypes = ['image/jpeg', 'image/png', 'image/webp', 'image/tiff']

function setHeaders (res, headers) {
function setHeaders(res, headers) {
for (const k of Object.keys(headers)) {
res.setHeader(k, headers[k])
}
}

module.exports = function (opts) {
opts = Object.assign({}, {
module.exports = function slicica(opts) {
opts = {
prefix: '/',
root: '',
maxAge: 0,
Expand All @@ -30,13 +25,9 @@ module.exports = function (opts) {
lastModified: true,
cache: false,
concurrency: 0,
contentTypes: [
'image/jpeg',
'image/png',
'image/webp',
'image/tiff',
],
}, opts)
contentTypes: ['image/jpeg', 'image/png', 'image/webp', 'image/tiff'],
...opts,
}
if (opts.prefix[0] !== '/') {
opts.prefix = `/${opts.prefix}`
}
Expand Down
Loading

0 comments on commit 692131e

Please sign in to comment.