@getcronit/pylon 3.0.0-canary-20250313071244.331210b68b70cedb9ce4101707a66d25a2981975 → 3.0.0-canary-20250313103250.6c6911913c81ddeeda2d705c53a45cd6c8ce214c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1628,9 +1628,7 @@ var setup = (app2) => {
1628
1628
  return Object.keys(supportedFormats).includes(format2);
1629
1629
  };
1630
1630
  var isSupportedFormat = isSupportedFormat2;
1631
- const sharp = (await import("sharp")).default;
1632
1631
  const { src, w, h, q = "75", format = "webp" } = c.req.query();
1633
- const queryStringHash = createHash("sha256").update(JSON.stringify(c.req.query())).digest("hex");
1634
1632
  if (!src) {
1635
1633
  return c.json({ error: "Missing parameters." }, 400);
1636
1634
  }
@@ -1643,6 +1641,22 @@ var setup = (app2) => {
1643
1641
  } catch {
1644
1642
  return c.json({ error: "Image not found" }, 404);
1645
1643
  }
1644
+ const cachedImageFileName = getCachedImagePath(
1645
+ src,
1646
+ w ? parseInt(w) : 0,
1647
+ h ? parseInt(h) : 0,
1648
+ format
1649
+ );
1650
+ if (IS_IMAGE_CACHE_POSSIBLE) {
1651
+ try {
1652
+ await fs2.promises.access(cachedImageFileName);
1653
+ const stream = fs2.createReadStream(cachedImageFileName);
1654
+ c.res.headers.set("Content-Type", getContentType(format));
1655
+ return c.body(Readable.toWeb(stream));
1656
+ } catch (e) {
1657
+ }
1658
+ }
1659
+ const sharp = (await import("sharp")).default;
1646
1660
  const metadata = await sharp(imagePath).metadata();
1647
1661
  if (!metadata.width || !metadata.height) {
1648
1662
  return c.json(
@@ -1658,26 +1672,24 @@ var setup = (app2) => {
1658
1672
  w ? parseInt(w) : void 0,
1659
1673
  h ? parseInt(h) : void 0
1660
1674
  );
1661
- const cachePath = path3.join(IMAGE_CACHE_DIR, queryStringHash);
1662
1675
  let imageFormat = format.toLowerCase();
1663
1676
  if (!isSupportedFormat2(imageFormat)) {
1664
1677
  throw new Error("Unsupported image format");
1665
1678
  }
1666
1679
  const quality = parseInt(q);
1680
+ const data = sharp(imagePath).resize(finalWidth, finalHeight).toFormat(imageFormat, {
1681
+ quality
1682
+ });
1667
1683
  if (IS_IMAGE_CACHE_POSSIBLE) {
1668
- const image = await sharp(imagePath).resize(finalWidth, finalHeight).toFormat(imageFormat, {
1669
- quality
1670
- }).toFile(cachePath);
1684
+ const image = await data.toFile(cachedImageFileName);
1671
1685
  c.res.headers.set("Content-Type", getContentType(image.format));
1672
1686
  return c.body(
1673
1687
  Readable.toWeb(
1674
- fs2.createReadStream(cachePath)
1688
+ fs2.createReadStream(cachedImageFileName)
1675
1689
  )
1676
1690
  );
1677
1691
  } else {
1678
- const image = await sharp(imagePath).resize(finalWidth, finalHeight).toFormat(imageFormat, {
1679
- quality
1680
- }).toBuffer({ resolveWithObject: true });
1692
+ const image = await data.toBuffer({ resolveWithObject: true });
1681
1693
  c.res.headers.set("Content-Type", getContentType(image.info.format));
1682
1694
  return c.body(image.data);
1683
1695
  }
@@ -1694,6 +1706,13 @@ try {
1694
1706
  } catch (error) {
1695
1707
  IS_IMAGE_CACHE_POSSIBLE = false;
1696
1708
  }
1709
+ var getCachedImagePath = (src, width, height, format) => {
1710
+ const fileName = `${path3.basename(
1711
+ createHash("md5").update(src).digest("hex"),
1712
+ path3.extname(src)
1713
+ )}-${width}x${height}.${format}`;
1714
+ return path3.join(IMAGE_CACHE_DIR, fileName);
1715
+ };
1697
1716
  var calculateDimensions = (originalWidth, originalHeight, width, height) => {
1698
1717
  if (!width && !height) {
1699
1718
  return { width: originalWidth, height: originalHeight };
@@ -1942,19 +1961,12 @@ var imagePlugin = {
1942
1961
  const image = sharp(args.path);
1943
1962
  const metadata = await image.metadata();
1944
1963
  const url = `${publicPath}/media/${path6.basename(args.path)}`;
1945
- const searchParams = new URLSearchParams({});
1946
- if (metadata.width) {
1947
- searchParams.set("w", metadata.width.toString());
1948
- }
1949
- if (metadata.height) {
1950
- searchParams.set("h", metadata.height.toString());
1951
- }
1952
1964
  const output = image.resize({
1953
1965
  width: Math.min(metadata.width ?? 16, 16),
1954
1966
  height: Math.min(metadata.height ?? 16, 16),
1955
1967
  fit: "inside"
1956
1968
  }).toFormat("webp", {
1957
- quality: 20,
1969
+ quality: 30,
1958
1970
  alphaQuality: 20,
1959
1971
  smartSubsample: true
1960
1972
  });
@@ -1962,12 +1974,14 @@ var imagePlugin = {
1962
1974
  const dataURIBase64 = `data:image/${info.format};base64,${data.toString(
1963
1975
  "base64"
1964
1976
  )}`;
1965
- if (dataURIBase64) {
1966
- searchParams.set("blurDataURL", dataURIBase64);
1967
- }
1968
1977
  return {
1969
- contents: `${url}?${searchParams.toString()}`,
1970
- loader: "text"
1978
+ contents: JSON.stringify({
1979
+ url,
1980
+ width: metadata.width,
1981
+ height: metadata.height,
1982
+ blurDataURL: dataURIBase64
1983
+ }),
1984
+ loader: "json"
1971
1985
  };
1972
1986
  });
1973
1987
  }