@all-the.rest/astro-image-cdn 0.2.1 → 0.2.3

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/README.md CHANGED
@@ -154,7 +154,7 @@ ratchet check
154
154
 
155
155
  The tests include real Astro static builds using `Image`, `Picture`, and `getImage()`, a real dev-server request served from local output, unrequested-image checks, a repeated warm build, and assertions that optimized variants remain outside `dist/`.
156
156
 
157
- The npm workflow publishes only pushed `vMAJOR.MINOR.PATCH` version tags whose version matches `package.json`; verification and packaging use pnpm in a separate job without OIDC, while the minimal publishing job downloads the verified tarball and uses npm CLI only for OIDC publishing. The package's npm Trusted Publisher is configured for this repository's `.github/workflows/publish.yml` and the `npm publish` action.
157
+ To release, update `package.json`, commit and push the change, then create and push a matching `vMAJOR.MINOR.PATCH` tag. Pushing that tag is the release trigger; `.github/workflows/publish.yml` validates it against `package.json`, runs `pnpm check`, and packs the package in a verification job. A separate publishing job downloads that verified tarball and publishes it with npm Trusted Publishing (GitHub Actions OIDC) and provenance. The npm Trusted Publisher must be configured for this repository and `.github/workflows/publish.yml`. Do not run `pnpm publish` locally: releases go through the tagged GitHub Actions workflow, not a local OTP-based publish.
158
158
 
159
159
  Version 0.1.0 was bootstrapped manually because npm requires a package to exist before a trusted publisher can be configured ([`npm trust` prerequisites](https://docs.npmjs.com/cli/v12/commands/npm-trust/#prerequisites)). Subsequent version-tag releases use GitHub Actions OIDC and npm provenance, without a long-lived publish token. A new trusted-publisher configuration must complete its first successful publish within two days or it expires.
160
160
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@all-the.rest/astro-image-cdn",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Persistent, incremental image variants for Astro's native image components",
5
5
  "type": "module",
6
6
  "license": "ISC",
package/src/service.js CHANGED
@@ -176,6 +176,10 @@ function sourceCandidates(value, rootDir) {
176
176
  return [...candidates];
177
177
  }
178
178
 
179
+ function sourcePathWithoutQuery(value) {
180
+ return typeof value === "string" ? value.split(/[?#]/, 1)[0] : undefined;
181
+ }
182
+
179
183
  function isInside(parent, candidate) {
180
184
  const relative = path.relative(parent, candidate);
181
185
  return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative));
@@ -210,11 +214,10 @@ async function findFilesByName(directory, fileName, matches) {
210
214
  async function requestedSourcePath(source, config) {
211
215
  if (typeof source?.fsPath === "string") return path.resolve(source.fsPath);
212
216
 
213
- const sourceName = typeof source?.src === "string" ? source.src : undefined;
217
+ const sourceName = sourcePathWithoutQuery(source?.src);
214
218
  if (!sourceName) return undefined;
215
219
  const rootDir = path.resolve(config.rootDir);
216
- const sourceNameWithoutQuery = sourceName.split(/[?#]/, 1)[0];
217
- for (const candidate of sourceCandidates(sourceNameWithoutQuery, rootDir)) {
220
+ for (const candidate of sourceCandidates(sourceName, rootDir)) {
218
221
  const absoluteCandidate = path.resolve(rootDir, candidate);
219
222
  if (!isConfiguredSourcePath(absoluteCandidate, config)) continue;
220
223
  try {
@@ -226,7 +229,7 @@ async function requestedSourcePath(source, config) {
226
229
 
227
230
  // Astro's SVG component wrapper serializes image metadata without its private fsPath.
228
231
  // Resolve that requested import by its emitted basename, but reject ambiguous matches.
229
- const basename = path.posix.basename(sourceNameWithoutQuery)
232
+ const basename = path.posix.basename(sourceName)
230
233
  .replace(/\.[a-z0-9_-]{8,}(?=\.[^.]+$)/i, "");
231
234
  const matches = [];
232
235
  for (const directory of (config.sources ?? ["src"]).map((sourceDirectory) => path.resolve(rootDir, sourceDirectory))) {
@@ -271,12 +274,14 @@ function resolveImage(options, imageConfig) {
271
274
  const manifest = readManifest(imageConfig);
272
275
  const rootDir = path.resolve(imageConfig.service.config.rootDir);
273
276
  const source = options.src;
277
+ const sourceName = typeof source === "string" ? source : source?.src;
278
+ const sourcePath = sourcePathWithoutQuery(sourceName);
274
279
  const possibleSources = [];
275
280
  if (isImportedImage(source)) {
276
281
  if (typeof source.fsPath === "string") possibleSources.push(...sourceCandidates(source.fsPath, rootDir));
277
- possibleSources.push(...sourceCandidates(source.src, rootDir));
282
+ possibleSources.push(...sourceCandidates(sourcePath, rootDir));
278
283
  } else {
279
- possibleSources.push(...sourceCandidates(source, rootDir));
284
+ possibleSources.push(...sourceCandidates(sourcePath, rootDir));
280
285
  }
281
286
 
282
287
  for (const candidate of possibleSources) {
@@ -284,9 +289,8 @@ function resolveImage(options, imageConfig) {
284
289
  if (entry) return entry;
285
290
  }
286
291
 
287
- const sourceName = typeof source === "string" ? source : source?.src;
288
- if (typeof sourceName === "string") {
289
- const builtAssetName = path.basename(sourceName).replace(/\.[a-z0-9_-]{8,}(?=\.[^.]+$)/i, "");
292
+ if (typeof sourcePath === "string") {
293
+ const builtAssetName = path.basename(sourcePath).replace(/\.[a-z0-9_-]{8,}(?=\.[^.]+$)/i, "");
290
294
  const sameName = Object.values(manifest.images).filter((entry) => path.posix.basename(entry.source) === builtAssetName);
291
295
  const sameDimensions = sameName.filter((entry) => entry.width === source?.width && entry.height === source?.height);
292
296
  const candidates = sameDimensions.length > 0 ? sameDimensions : sameName;
@@ -326,11 +330,27 @@ function validateStaticTransform(options, config) {
326
330
  throw new Error(`astro-image-cdn does not support fit="${options.fit}".`);
327
331
  }
328
332
 
329
- const width = Number(options.width ?? options.src.width);
330
- const height = Number(options.height ?? (width * options.src.height / options.src.width));
331
- const targetRatio = width / height;
332
- const sourceRatio = options.src.width / options.src.height;
333
- if (Number.isFinite(targetRatio) && Math.abs(targetRatio - sourceRatio) > 0.005) {
333
+ const sourceWidth = Number(options.src.width);
334
+ const sourceHeight = Number(options.src.height);
335
+ let width = options.width == null ? undefined : Number(options.width);
336
+ let height = options.height == null ? undefined : Number(options.height);
337
+ if (width != null && height == null) {
338
+ height = Math.round(width * sourceHeight / sourceWidth);
339
+ } else if (height != null && width == null) {
340
+ width = Math.round(height * sourceWidth / sourceHeight);
341
+ } else if (width == null && height == null) {
342
+ width = sourceWidth;
343
+ height = sourceHeight;
344
+ }
345
+
346
+ width = Math.round(width);
347
+ height = Math.round(height);
348
+ const expectedHeight = Math.round(width * sourceHeight / sourceWidth);
349
+ const expectedWidth = Math.round(height * sourceWidth / sourceHeight);
350
+ const isAspectPreserving = (Number.isSafeInteger(width) && width > 0)
351
+ && (Number.isSafeInteger(height) && height > 0)
352
+ && (height === expectedHeight || width === expectedWidth);
353
+ if (!isAspectPreserving) {
334
354
  throw new Error("astro-image-cdn generates aspect-preserving variants only; per-image cropping is not supported.");
335
355
  }
336
356