Socket
Book a DemoInstallSign in
Socket

@neosyn-ee/nest-fastify-media

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@neosyn-ee/nest-fastify-media

High-performance image conversion for NestJS with Fastify

latest
npmnpm
Version
0.0.14
Version published
Maintainers
2
Created
Source

NestJS Fastify Media Service

NestJS Fastify Sharp

A high-performance image processing library for NestJS applications using Fastify. Provides image conversion, resizing, and caching capabilities.

Features

  • 🖼️ Multi-format Conversion - Convert between JPEG, PNG, WebP, and more
  • 📏 Smart Resizing - Maintain aspect ratio while resizing
  • In-Memory Caching - Avoid repeated processing of same images
  • 🔗 Base64 Output - Get ready-to-use Data URLs
  • 🚀 Fastify Optimized - Built for NestJS Fastify adapter

Installation

npm install sharp @nestjs/platform-fastify

Quick Start

  • Import Module
import { Module } from '@nestjs/common';
import { NestFastifyMediaService } from 'nest-fastify-media';

@Module({
  providers: [NestFastifyMediaService],
  exports: [NestFastifyMediaService],
})
export class MediaModule {}
  • Use in Controller
import { Controller, Get } from '@nestjs/common';
import { NestFastifyMediaService } from 'nest-fastify-media';

@Controller('images')
export class ImageController {
  constructor(private readonly mediaService: NestFastifyMediaService) {}

  @Get('convert')
  async convertImage() {
    return {
      buffer: await this.mediaService.convertImage({
        imageUrl: 'https://example.com/image.jpg',
        outputFormat: 'webp',
        width: 800,
      }),
      base64: await this.mediaService.convertImageToBase64({
        imageUrl: 'https://example.com/image.jpg',
        outputFormat: 'png',
      }),
    };
  }
}

API Reference

convertImage(dto: ConvertImageDto): Promise<Buffer>

Converts and resizes an image, returning a Buffer.

convertImageToBase64(dto: ConvertImageDto): Promise<string>

Converts an image and returns as Base64 Data URL.

ConvertImageDto

ParameterTypeDefaultDescription
imageUrlstring-Source image URL (required)
outputFormatstringpngOutput format (jpeg/png/webp)
widthnumber-Target width in pixels
heightnumber-Target height in pixels

Examples

Basic Conversion

const buffer = await mediaService.convertImage({
  imageUrl: 'https://example.com/photo.jpg',
  outputFormat: 'webp',
});

Resizing with Base64 Output

const base64 = await mediaService.convertImageToBase64({
  imageUrl: 'https://example.com/photo.jpg',
  outputFormat: 'png',
  width: 300,
  height: 200,
});

HTML Usage

<img src="data:image/png;base64,..." alt="Converted Image" />

Caching

  • Automatic in-memory caching
  • Cache key format: [url]-[format]-[width]x[height]
  • Subsequent identical requests return cached version

Keywords

nestjs

FAQs

Package last updated on 12 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts