sharp_CG4EIo5e.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. globalThis.process ??= {}; globalThis.process.env ??= {};
  2. import { A as AstroError, at as MissingSharp } from './astro/server_WO3f6Mge.mjs';
  3. import { b as baseService, p as parseQuality } from './image-endpoint_DVs7qPGs.mjs';
  4. let sharp;
  5. const qualityTable = {
  6. low: 25,
  7. mid: 50,
  8. high: 80,
  9. max: 100
  10. };
  11. async function loadSharp() {
  12. let sharpImport;
  13. try {
  14. sharpImport = (await import('sharp')).default;
  15. } catch {
  16. throw new AstroError(MissingSharp);
  17. }
  18. sharpImport.cache(false);
  19. return sharpImport;
  20. }
  21. const fitMap = {
  22. fill: "fill",
  23. contain: "inside",
  24. cover: "cover",
  25. none: "outside",
  26. "scale-down": "inside",
  27. outside: "outside",
  28. inside: "inside"
  29. };
  30. const sharpService = {
  31. validateOptions: baseService.validateOptions,
  32. getURL: baseService.getURL,
  33. parseURL: baseService.parseURL,
  34. getHTMLAttributes: baseService.getHTMLAttributes,
  35. getSrcSet: baseService.getSrcSet,
  36. async transform(inputBuffer, transformOptions, config) {
  37. if (!sharp) sharp = await loadSharp();
  38. const transform = transformOptions;
  39. if (transform.format === "svg") return { data: inputBuffer, format: "svg" };
  40. const result = sharp(inputBuffer, {
  41. failOnError: false,
  42. pages: -1,
  43. limitInputPixels: config.service.config.limitInputPixels
  44. });
  45. result.rotate();
  46. const withoutEnlargement = Boolean(transform.fit);
  47. if (transform.width && transform.height && transform.fit) {
  48. const fit = fitMap[transform.fit] ?? "inside";
  49. result.resize({
  50. width: Math.round(transform.width),
  51. height: Math.round(transform.height),
  52. fit,
  53. position: transform.position,
  54. withoutEnlargement
  55. });
  56. } else if (transform.height && !transform.width) {
  57. result.resize({
  58. height: Math.round(transform.height),
  59. withoutEnlargement
  60. });
  61. } else if (transform.width) {
  62. result.resize({
  63. width: Math.round(transform.width),
  64. withoutEnlargement
  65. });
  66. }
  67. if (transform.format) {
  68. let quality = void 0;
  69. if (transform.quality) {
  70. const parsedQuality = parseQuality(transform.quality);
  71. if (typeof parsedQuality === "number") {
  72. quality = parsedQuality;
  73. } else {
  74. quality = transform.quality in qualityTable ? qualityTable[transform.quality] : void 0;
  75. }
  76. }
  77. const isGifInput = inputBuffer[0] === 71 && // 'G'
  78. inputBuffer[1] === 73 && // 'I'
  79. inputBuffer[2] === 70 && // 'F'
  80. inputBuffer[3] === 56 && // '8'
  81. (inputBuffer[4] === 57 || inputBuffer[4] === 55) && // '9' or '7'
  82. inputBuffer[5] === 97;
  83. if (transform.format === "webp" && isGifInput) {
  84. result.webp({ quality: typeof quality === "number" ? quality : void 0, loop: 0 });
  85. } else {
  86. result.toFormat(transform.format, { quality });
  87. }
  88. }
  89. const { data, info } = await result.toBuffer({ resolveWithObject: true });
  90. const needsCopy = "buffer" in data && data.buffer instanceof SharedArrayBuffer;
  91. return {
  92. data: needsCopy ? new Uint8Array(data) : data,
  93. format: info.format
  94. };
  95. }
  96. };
  97. var sharp_default = sharpService;
  98. export { sharp_default as default };