@fuuck/blurest-core
A high-performance TypeScript/Node.js library for generating and caching Blurhash placeholders for images. This library provides a integration between JavaScript and a Rust module for optimal performance, with built-in database caching to avoid redundant processing.
Features
- 🚀 High Performance: Native module implementation for fast blurhash generation
- 💾 Smart Caching: Database-backed caching system to avoid reprocessing images
- 🛡️ Type Safety: Full TypeScript support with comprehensive type definitions
- 📁 Path Validation: Automatic validation of image paths and project boundaries
- 🔧 Easy Integration: Simple API for seamless integration into existing projects
Installation
npm install @fuuck/blurest-core
yarn add @fuuck/blurhash-core
bun add @fuuck/blurhash-core
Quick Start
import { BlurhashCore } from "@fuuck/blurest-core";
const blurhash = new BlurhashCore({
databasePath: join(__dirname, "db.sqlite3"),
projectRoot: __dirname,
});
blurhash.initialize();
const result = blurhash.processImage("./images/photo.jpg");
if (result && result.success) {
console.log("Blurhash:", result.blurhash);
console.log("Dimensions:", result.width, "x", result.height);
} else if (result && !result.success) {
console.error("Error:", result.error);
}
blurhash.cleanup();
API Reference
BlurhashCore Class
Constructor
new BlurhashCore(options: BlurhashCoreOptions)
Options:
databasePath
: Path of the database file, will be created if it doesn't exist
projectRoot
: Absolute path to your project root directory
Methods
initialize(): void
Initializes the blurhash cache system. Must be called before processing any images.
blurhash.initialize();
isInitialized(): boolean
Checks if the core is properly initialized.
if (blurhash.isInitialized()) {
}
processImage(src: string): BlurhashResult | null
Processes an image and returns blurhash data. Returns null
if the image should be skipped.
const result = blurhash.processImage("./images/photo.jpg");
Returns:
BlurhashSuccessResult
on success
BlurhashErrorResult
on error
null
if processing should be skipped
cleanup(): boolean
Cleans up resources and closes database connections.
blurhash.cleanup();
getProjectRoot(): string
Returns the configured project root path.
Utility Functions
parseImageSrc(src: string): ParsedImageSource
Parses image source strings with optional dimension specifications.
import { parseImageSrc } from "@fuuck/blurest-core";
parseImageSrc("image.jpg =100x200");
parseImageSrc("image.jpg =100x");
parseImageSrc("image.jpg =x200");
parseImageSrc("image.jpg");
validateFile(src: string, projectRoot: string): FileValidationResult
Validates whether a file should be processed by the native module.
import { validateFile } from "@fuuck/blurest-core";
const validation = validateFile("./image.jpg", "/project/root");
if (validation.shouldProcess) {
console.log("File can be processed:", validation.resolvedPath);
} else {
console.log("Skipping file:", validation.reason);
}
isNetworkUrl(src: string): boolean
Checks if a URL is a network URL (HTTP/HTTPS).
import { isNetworkUrl } from "@fuuck/blurest-core";
isNetworkUrl("https://example.com/image.jpg");
isNetworkUrl("./local/image.jpg");
Type Definitions
BlurhashResult Types
interface BlurhashSuccessResult {
success: true;
blurhash: string;
width: number;
height: number;
}
interface BlurhashErrorResult {
success: false;
error: string;
}
type BlurhashResult = BlurhashSuccessResult | BlurhashErrorResult;
Configuration Types
interface BlurhashCoreOptions {
databasePath: string;
projectRoot: string;
}
interface ParsedImageSource {
cleanSrc: string;
renderWidth: number | null;
renderHeight: number | null;
}
interface FileValidationResult {
shouldProcess: boolean;
resolvedPath?: string;
reason?: string;
}
Usage Examples
Basic Image Processing
import { BlurhashCore } from "@fuuck/blurest-core";
const core = new BlurhashCore({
databasePath: join(__dirname, "db.sqlite3"),
projectRoot: __dirname,
});
core.initialize();
const result = core.processImage("./src/assets/hero-image.jpg");
if (result?.success) {
console.log(`Generated blurhash: ${result.blurhash}`);
console.log(`Image dimensions: ${result.width}x${result.height}`);
}
Batch Processing
import { BlurhashCore } from "@fuuck/blurest-core";
import { glob } from "glob";
const core = new BlurhashCore({
databasePath: join(__dirname, "db.sqlite3"),
projectRoot: __dirname,
});
core.initialize();
const imageFiles = glob.sync("./src/assets/**/*.{jpg,jpeg,png,webp}");
const results = imageFiles.map((file) => {
const result = core.processImage(file);
return { file, result };
});
console.log(`Processed ${results.length} images`);
core.cleanup();
Integration with Markdown Processing
import { BlurhashCore, parseImageSrc } from "@fuuck/blurest-core";
const core = new BlurhashCore({
databasePath: join(__dirname, "db.sqlite3"),
projectRoot: __dirname,
});
core.initialize();
function processMarkdownImage(src: string) {
const { cleanSrc, renderWidth, renderHeight } = parseImageSrc(src);
const blurhashResult = core.processImage(cleanSrc);
if (blurhashResult?.success) {
return {
src: cleanSrc,
blurhash: blurhashResult.blurhash,
intrinsicWidth: blurhashResult.width,
intrinsicHeight: blurhashResult.height,
renderWidth,
renderHeight,
};
}
return null;
}
const imageData = processMarkdownImage("./images/photo.jpg =800x600");
Error Handling
The library provides comprehensive error handling:
try {
core.initialize();
const result = core.processImage("./image.jpg");
if (result === null) {
console.log(
"Image processing was skipped (likely a network URL or invalid path)"
);
} else if (!result.success) {
console.error("Blurhash generation failed:", result.error);
} else {
console.log("Success:", result.blurhash);
}
} catch (error) {
console.error("Initialization or processing error:", error);
} finally {
core.cleanup();
}
File Validation
The library automatically validates files before processing:
- ✅ Local files only: Network URLs are automatically skipped
- ✅ Project boundary: Files must be within the specified project root
- ✅ File existence: Non-existent files are skipped
- ✅ File type: Only actual files are processed (not directories)
Performance Considerations
- Caching: The library uses a database cache to avoid reprocessing unchanged images
- Native Performance: Core blurhash generation is handled by a native module for optimal speed
- Memory Management: Automatic cleanup of resources when done processing
- Batch Processing: Efficient handling of multiple images in sequence
License
MIT