Home / File/ sharp.ts — astro Source File

sharp.ts — astro Source File

Architecture documentation for sharp.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 4 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  e98aa86d_990d_0999_8b69_e22d93343858["sharp.ts"]
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  e98aa86d_990d_0999_8b69_e22d93343858 --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  416eda3d_47e6_c298_102b_b7a37d86a44e["../../assets/types.js"]
  e98aa86d_990d_0999_8b69_e22d93343858 --> 416eda3d_47e6_c298_102b_b7a37d86a44e
  af2a1f33_901c_456d_9c42_3a41fec45451["./services/service.js"]
  e98aa86d_990d_0999_8b69_e22d93343858 --> af2a1f33_901c_456d_9c42_3a41fec45451
  30fa69a2_cadd_3d32_cb12_b743a834f699["sharp"]
  e98aa86d_990d_0999_8b69_e22d93343858 --> 30fa69a2_cadd_3d32_cb12_b743a834f699
  style e98aa86d_990d_0999_8b69_e22d93343858 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { FitEnum, FormatEnum, ResizeOptions, SharpOptions } from 'sharp';
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import type { ImageFit, ImageOutputFormat, ImageQualityPreset } from '../types.js';
import {
	type BaseServiceTransform,
	baseService,
	type LocalImageService,
	parseQuality,
} from './service.js';

export interface SharpImageServiceConfig {
	/**
	 * The `limitInputPixels` option passed to Sharp. See https://sharp.pixelplumbing.com/api-constructor for more information
	 */
	limitInputPixels?: SharpOptions['limitInputPixels'];

	/**
	 * The `kernel` option is passed to resize calls. See https://sharp.pixelplumbing.com/api-resize/ for more information
	 */
	kernel?: ResizeOptions['kernel'];
}

let sharp: typeof import('sharp');

const qualityTable: Record<ImageQualityPreset, number> = {
	low: 25,
	mid: 50,
	high: 80,
	max: 100,
};

async function loadSharp() {
	let sharpImport: typeof import('sharp');
	try {
		sharpImport = (await import('sharp')).default;
	} catch {
		throw new AstroError(AstroErrorData.MissingSharp);
	}

	// Disable the `sharp` `libvips` cache as it errors when the file is too small and operations are happening too fast (runs into a race condition) https://github.com/lovell/sharp/issues/3935#issuecomment-1881866341
	sharpImport.cache(false);

	return sharpImport;
}

const fitMap: Record<ImageFit, keyof FitEnum> = {
	fill: 'fill',
	contain: 'inside',
	cover: 'cover',
	none: 'outside',
	'scale-down': 'inside',
	outside: 'outside',
	inside: 'inside',
};

const sharpService: LocalImageService<SharpImageServiceConfig> = {
	validateOptions: baseService.validateOptions,
	getURL: baseService.getURL,
	parseURL: baseService.parseURL,
	getHTMLAttributes: baseService.getHTMLAttributes,
// ... (95 more lines)

Domain

Subdomains

Dependencies

  • ../../assets/types.js
  • ../core/errors/index.js
  • ./services/service.js
  • sharp

Frequently Asked Questions

What does sharp.ts do?
sharp.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in sharp.ts?
sharp.ts defines 2 function(s): loadSharp, sharpService.transform.
What does sharp.ts depend on?
sharp.ts imports 4 module(s): ../../assets/types.js, ../core/errors/index.js, ./services/service.js, sharp.
Where is sharp.ts in the architecture?
sharp.ts is located at packages/astro/src/assets/services/sharp.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/services).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free