This Services provides actions for manipulating images using the super fast Sharp module. It utilizes the file streaming capabilities of the moleculer framework
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)
This package is available in the npm-registry. In order to use it simply install it with yarn (or npm):
yarn add moleculer-sharp
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.
Property | Type | Default | Description |
---|---|---|---|
No settings. |
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'})
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 |
Type: PromiseLike.<(Object|Error)>
Gather stats of an image
broker.call('sharp.stats',{url: 'https://pics.me.me/welcome-to-the-internet-ill-be-your-guide-28274277.png'})
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 |
Type: PromiseLike.<(Object|Error)>
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.
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 |
Type: PromiseLike.<(undefined|Error)>
Acquire a readable stream from a given source
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 |
Type: PromiseLike.<(Stream|SharpStreamAcquisitionError|Error)>
Feed a Stream into a Buffer
Property | Type | Default | Description |
---|---|---|---|
stream |
ReadableStream |
required |
Type: PromiseLike.<(Buffer|Error)>
$ docker-compose exec package yarn test
In development with watching
$ docker-compose up
moleculer-sharp is available under the MIT license.