[ci] format
This commit is contained in:
parent
9892989619
commit
849fefd8b0
4 changed files with 29 additions and 17 deletions
|
@ -1,19 +1,19 @@
|
|||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { debug, error, warn } from '../utils/logger.js';
|
||||
import type { LoggerLevel } from '../utils/logger.js';
|
||||
import { debug, warn } from '../utils/logger.js';
|
||||
|
||||
const CACHE_FILE = `cache.json`;
|
||||
|
||||
interface Cache {
|
||||
[filename: string]: { expires: number }
|
||||
[filename: string]: { expires: number };
|
||||
}
|
||||
|
||||
export class ImageCache {
|
||||
#cacheDir: URL;
|
||||
#cacheFile: URL;
|
||||
#cache: Cache = { }
|
||||
#cache: Cache = {};
|
||||
#logLevel: LoggerLevel;
|
||||
|
||||
constructor(dir: URL, logLevel: LoggerLevel) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { doWork } from '@altano/tiny-async-pool';
|
||||
import type { AstroConfig } from 'astro';
|
||||
import { bgGreen, black, cyan, dim, green } from 'kleur/colors';
|
||||
import CachePolicy from 'http-cache-semantics';
|
||||
import { bgGreen, black, cyan, dim, green } from 'kleur/colors';
|
||||
import fs from 'node:fs/promises';
|
||||
import OS from 'node:os';
|
||||
import path from 'node:path';
|
||||
|
@ -19,11 +19,11 @@ async function loadLocalImage(src: string | URL) {
|
|||
// we can safely cache local images here.
|
||||
const timeToLive = new Date();
|
||||
timeToLive.setFullYear(timeToLive.getFullYear() + 1);
|
||||
|
||||
|
||||
return {
|
||||
data,
|
||||
expires: timeToLive.getTime(),
|
||||
}
|
||||
};
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
@ -32,23 +32,23 @@ async function loadLocalImage(src: string | URL) {
|
|||
function webToCachePolicyRequest({ url, method, headers: _headers }: Request): CachePolicy.Request {
|
||||
const headers: CachePolicy.Headers = {};
|
||||
for (const [key, value] of _headers) {
|
||||
headers[key] = value;
|
||||
headers[key] = value;
|
||||
}
|
||||
return {
|
||||
method,
|
||||
url,
|
||||
headers,
|
||||
method,
|
||||
url,
|
||||
headers,
|
||||
};
|
||||
}
|
||||
|
||||
function webToCachePolicyResponse({ status, headers: _headers }: Response): CachePolicy.Response {
|
||||
const headers: CachePolicy.Headers = {};
|
||||
for (const [key, value] of _headers) {
|
||||
headers[key] = value;
|
||||
headers[key] = value;
|
||||
}
|
||||
return {
|
||||
status,
|
||||
headers,
|
||||
status,
|
||||
headers,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -88,7 +88,14 @@ export interface SSGBuildParams {
|
|||
cacheDir?: URL;
|
||||
}
|
||||
|
||||
export async function ssgBuild({ loader, staticImages, config, outDir, logLevel, cacheDir }: SSGBuildParams) {
|
||||
export async function ssgBuild({
|
||||
loader,
|
||||
staticImages,
|
||||
config,
|
||||
outDir,
|
||||
logLevel,
|
||||
cacheDir,
|
||||
}: SSGBuildParams) {
|
||||
let cache: ImageCache | undefined = undefined;
|
||||
|
||||
if (cacheDir) {
|
||||
|
@ -174,7 +181,7 @@ export async function ssgBuild({ loader, staticImages, config, outDir, logLevel,
|
|||
if (cache?.has(pathRelative)) {
|
||||
data = await cache.get(pathRelative);
|
||||
}
|
||||
|
||||
|
||||
// a valid cache file wasn't found, transform the image and cache it
|
||||
if (!data) {
|
||||
const transformed = await loader.transform(inputBuffer, transform);
|
||||
|
|
|
@ -129,7 +129,9 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
|
|||
}
|
||||
|
||||
if (loader && 'transform' in loader && staticImages.size > 0) {
|
||||
const cacheDir = !!resolvedOptions.cacheDir ? new URL(resolvedOptions.cacheDir, _config.root) : undefined;
|
||||
const cacheDir = !!resolvedOptions.cacheDir
|
||||
? new URL(resolvedOptions.cacheDir, _config.root)
|
||||
: undefined;
|
||||
|
||||
await ssgBuild({
|
||||
loader,
|
||||
|
|
|
@ -263,7 +263,10 @@ describe('SSG images - build', function () {
|
|||
|
||||
verifyImage(image.attr('src'), size);
|
||||
|
||||
const url = new URL('./fixtures/basic-image/node_modules/.astro/image' + image.attr('src'), import.meta.url);
|
||||
const url = new URL(
|
||||
'./fixtures/basic-image/node_modules/.astro/image' + image.attr('src'),
|
||||
import.meta.url
|
||||
);
|
||||
expect(await fs.stat(url), 'transformed image was cached').to.not.be.undefined;
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue