100 lines
3.7 KiB
JavaScript
100 lines
3.7 KiB
JavaScript
import { isRemotePath } from '@astrojs/internal-helpers/path';
|
|
import mime from 'mime/lite.js';
|
|
import { g as getImage$1, a as getConfiguredImageService, i as isRemoteAllowed } from '../astro-assets-services_30e086a8.mjs';
|
|
import { c as createAstro, d as createComponent, A as AstroError, e as ImageMissingAlt, r as renderTemplate, m as maybeRenderHead, f as addAttribute, s as spreadAttributes } from '../astro_bfe7ba8a.mjs';
|
|
import 'clsx';
|
|
import 'html-escaper';
|
|
|
|
const fnv1a52 = (str) => {
|
|
const len = str.length;
|
|
let i = 0, t0 = 0, v0 = 8997, t1 = 0, v1 = 33826, t2 = 0, v2 = 40164, t3 = 0, v3 = 52210;
|
|
while (i < len) {
|
|
v0 ^= str.charCodeAt(i++);
|
|
t0 = v0 * 435;
|
|
t1 = v1 * 435;
|
|
t2 = v2 * 435;
|
|
t3 = v3 * 435;
|
|
t2 += v0 << 8;
|
|
t3 += v1 << 8;
|
|
t1 += t0 >>> 16;
|
|
v0 = t0 & 65535;
|
|
t2 += t1 >>> 16;
|
|
v1 = t1 & 65535;
|
|
v3 = t3 + (t2 >>> 16) & 65535;
|
|
v2 = t2 & 65535;
|
|
}
|
|
return (v3 & 15) * 281474976710656 + v2 * 4294967296 + v1 * 65536 + (v0 ^ v3 >> 4);
|
|
};
|
|
const etag = (payload, weak = false) => {
|
|
const prefix = weak ? 'W/"' : '"';
|
|
return prefix + fnv1a52(payload).toString(36) + payload.length.toString(36) + '"';
|
|
};
|
|
|
|
const $$Astro = createAstro("https://astro-reactive.dev");
|
|
const $$Image = createComponent(async ($$result, $$props, $$slots) => {
|
|
const Astro2 = $$result.createAstro($$Astro, $$props, $$slots);
|
|
Astro2.self = $$Image;
|
|
const props = Astro2.props;
|
|
if (props.alt === void 0 || props.alt === null) {
|
|
throw new AstroError(ImageMissingAlt);
|
|
}
|
|
if (typeof props.width === "string") {
|
|
props.width = parseInt(props.width);
|
|
}
|
|
if (typeof props.height === "string") {
|
|
props.height = parseInt(props.height);
|
|
}
|
|
const image = await getImage(props);
|
|
return renderTemplate`${maybeRenderHead()}<img${addAttribute(image.src, "src")}${spreadAttributes(image.attributes)}>`;
|
|
}, "/Users/ayoayco/Projects/@astro-reactive/astro-reactive/node_modules/astro/components/Image.astro", void 0);
|
|
|
|
const imageConfig = {"service":{"entrypoint":"astro/assets/services/sharp","config":{}},"domains":[],"remotePatterns":[]};
|
|
const getImage = async (options) => await getImage$1(options, imageConfig);
|
|
|
|
async function loadRemoteImage(src) {
|
|
try {
|
|
const res = await fetch(src);
|
|
if (!res.ok) {
|
|
return void 0;
|
|
}
|
|
return Buffer.from(await res.arrayBuffer());
|
|
} catch (err) {
|
|
return void 0;
|
|
}
|
|
}
|
|
const GET = async ({ request }) => {
|
|
try {
|
|
const imageService = await getConfiguredImageService();
|
|
if (!("transform" in imageService)) {
|
|
throw new Error("Configured image service is not a local service");
|
|
}
|
|
const url = new URL(request.url);
|
|
const transform = await imageService.parseURL(url, imageConfig);
|
|
if (!transform?.src) {
|
|
throw new Error("Incorrect transform returned by `parseURL`");
|
|
}
|
|
let inputBuffer = void 0;
|
|
const sourceUrl = isRemotePath(transform.src) ? new URL(transform.src) : new URL(transform.src, url.origin);
|
|
if (isRemotePath(transform.src) && isRemoteAllowed(transform.src, imageConfig) === false) {
|
|
return new Response("Forbidden", { status: 403 });
|
|
}
|
|
inputBuffer = await loadRemoteImage(sourceUrl);
|
|
if (!inputBuffer) {
|
|
return new Response("Not Found", { status: 404 });
|
|
}
|
|
const { data, format } = await imageService.transform(inputBuffer, transform, imageConfig);
|
|
return new Response(data, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": mime.getType(format) ?? `image/${format}`,
|
|
"Cache-Control": "public, max-age=31536000",
|
|
ETag: etag(data.toString()),
|
|
Date: (/* @__PURE__ */ new Date()).toUTCString()
|
|
}
|
|
});
|
|
} catch (err) {
|
|
return new Response(`Server Error: ${err}`, { status: 500 });
|
|
}
|
|
};
|
|
|
|
export { GET };
|