moleculer-sharp

1.0.0 • Public • Published

Moleculer logo FOSSA Status

Build Status Coverage Status Codacy Badge Maintainability Known Vulnerabilities npm version Run in Postman

Image Manipulation Service for the Moleculer framework

This Services provides actions for manipulating images using the super fast Sharp module. It utilizes the file streaming capabilities of the moleculer framework

Features

The following List details which features are implemented

  • Obtain Metadata of an image at a path, being streamed or available via http(s)
  • Obtain Channel Statistics of an image at a path, being streamed or available via http(s)
  • Process an image with all manipulation and transformation methods available in Sharp (such as rotation, flip, convert, resize etc)

Install

This package is available in the npm-registry. In order to use it simply install it with yarn (or npm):

yarn add moleculer-sharp

Usage

To make use of this Service, simply require it and create a new service:

const fs = require("fs");
let { ServiceBroker } = require("moleculer");
let SharpService = require("moleculer-sharp");
 
let broker = new ServiceBroker({ logger: console });
 
// Create a service
broker.createService({
    mixins: SharpService,
});
 
// Start server
broker.start()
    .then(() => broker.call('sharp.metadata',{url: 'https://pics.me.me/welcome-to-the-internet-ill-be-your-guide-28274277.png'}))
    .then(() => broker.call('sharp.stats',{url: 'https://pics.me.me/welcome-to-the-internet-ill-be-your-guide-28274277.png'}))
    .then(() => 
        broker.call('sharp.process',{url: 'https://pics.me.me/welcome-to-the-internet-ill-be-your-guide-28274277.png'}, {meta:
            steps: [
                ["resize", 200],
                ["rotate", 30, {"background": {"r": 0, "g": 0, "b": 0, "alpha": 0}}],
                "jpeg"
            ]
        }));

For a more indepth example checkout out the examples folder. It includes a docker-compose file, running docker-compose up will boot a broker with a sharp service and an API Gateway to invoke the actions of the sharp service. This project includes a published postman collection enabling you to quickly explore the service in your local environment.

Settings

Property Type Default Description

No settings.

Actions

metadata

Fast access to (uncached) image metadata without decoding any compressed image data.

broker.call('sharp.metadata',{url: 'https://pics.me.me/welcome-to-the-internet-ill-be-your-guide-28274277.png'})

Parameters

Property Type Default Description
params String, ReadableStream, Object required the image to acquire metadata for, can be a path, a stream or an object. If a path is given, this action will try to acquire a readable stream for the path. If an object is given, a http(s) stream will be acquired and the response body will be subject. For the location of the request, the url property will be used, while all other properties will be used as node-fetch-options

Results

Type: PromiseLike.<(Object|Error)>

stats

Gather stats of an image

broker.call('sharp.stats',{url: 'https://pics.me.me/welcome-to-the-internet-ill-be-your-guide-28274277.png'})

Parameters

Property Type Default Description
params String, ReadableStream, Object required the image to acquire stats for, can be a path, a stream or an object. If a path is given, this action will try to acquire a readable stream for the path. If an object is given, a http(s) stream will be acquired and the response body will be subject. For the location of the request, the url property will be used, while all other properties will be used as node-fetch-options

Results

Type: PromiseLike.<(Object|Error)>

process

Processes an image. The action parameter indicates which image to process. The actual processing instructions have to be provided via the meta.steps property of the call. Any operation that is listed on the Sharp Documentation can be included as a step instruction. Here is an example:

broker.call('sharp.process',{url: 'https://pics.me.me/welcome-to-the-internet-ill-be-your-guide-28274277.png'}, {meta:
    steps: [
        ["resize", 200],
     ["rotate", 30, {"background": {"r": 0, "g": 0, "b": 0, "alpha": 0}}],
     "jpeg"
    ]
})

If your last step instructions is a toFile instructions, the transformation output will be written to disk, and the action will respond with meta information about the image. In any other case the action will respond with a readable stream for your to further process.

Parameters

Property Type Default Description
params String, ReadableStream, Object required the image to process, can be a path, a stream or an object. If a path is given, this action will try to acquire a readable stream for the path. If an object is given, a http(s) stream will be acquired and the response body will be subject. For the location of the request, the url property will be used, while all other properties will be used as node-fetch-options

Results

Type: PromiseLike.<(undefined|Error)>

Methods

acquireReadStream

Acquire a readable stream from a given source

Parameters

Property Type Default Description
source String, ReadableStream, Object required can be a path, a stream or an object. If a path is given, this action will try to acquire a readable stream for the path. If an object is given, a http(s) stream will be acquired for the response body. For the location of the request, the url property will be used, while all other properties will be used as node-fetch-options

Results

Type: PromiseLike.<(Stream|SharpStreamAcquisitionError|Error)>

bufferStream

Feed a Stream into a Buffer

Parameters

Property Type Default Description
stream ReadableStream required

Results

Type: PromiseLike.<(Buffer|Error)>

Test

$ docker-compose exec package yarn test

In development with watching

$ docker-compose up

License

moleculer-sharp is available under the MIT license.

FOSSA Status

Package Sidebar

Install

npm i moleculer-sharp

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

31.5 kB

Total Files

40

Last publish

Collaborators

  • designtesbrot