-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_public.api.image._index.tsx
41 lines (33 loc) · 1.25 KB
/
_public.api.image._index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { LoaderArgs, LoaderFunction } from '@remix-run/cloudflare';
import type { MimeType, Resolver } from 'remix-image/serverPure';
import { MemoryCache, RemixImageError, fetchResolver, imageLoader } from 'remix-image/serverPure';
import { ContextHack } from '~/data/types';
const cache = new MemoryCache({
maxSize: 5e7,
});
const SELF_URL = 'http://localhost:8788';
export const loader: LoaderFunction = ({ request, context }: LoaderArgs & ContextHack) => {
const resolver: Resolver = async (asset, url, options, basePath) => {
if (asset.startsWith('/') && (asset.length === 1 || asset[1] !== '/')) {
const imageResponse = await context.ASSETS.fetch(url, request.clone());
const arrBuff = await imageResponse.arrayBuffer();
if (!arrBuff || arrBuff.byteLength < 2) {
throw new RemixImageError('Invalid image retrieved from resolver!');
}
const buffer = new Uint8Array(arrBuff);
const contentType = imageResponse.headers.get('content-type')! as MimeType;
return {
buffer,
contentType,
};
} else {
return fetchResolver(asset, url, options, basePath);
}
};
const config = {
selfUrl: SELF_URL,
cache,
resolver,
};
return imageLoader(config, request);
};