Skip to content

Commit

Permalink
chore: bump dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Cherry committed Oct 4, 2024
1 parent 7ece7d9 commit b88f986
Show file tree
Hide file tree
Showing 6 changed files with 3,963 additions and 6,054 deletions.
17 changes: 4 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
{
"root": true,
"extends": [
"@nodecraft",
"plugin:@typescript-eslint/recommended"
"@nodecraft/eslint-config/typescript"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2021,
"sourceType": "module"
},
"plugins": [
"json",
"node",
"unicorn",
"@typescript-eslint"
"unicorn"
],
"rules": {
"node/no-unsupported-features/es-syntax": "off",
"node/no-missing-import": "off"
"n/no-unsupported-features/node-builtins": "off"
}
}
}
4 changes: 2 additions & 2 deletions example/functions/images/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assetNegotiationPlugin from "../../..";
import assetNegotiationPlugin from '../../..';

export const onRequest: PagesFunction = assetNegotiationPlugin({
formats: ['jxl', 'avif', 'webp'],
});
});
36 changes: 18 additions & 18 deletions functions/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import type {PluginArgs} from "..";
import type { PluginArgs } from '..';

type assetNegotiationPagesPluginFunction<
Env = unknown,
Params extends string = any,
Data extends Record<string, unknown> = Record<string, unknown>
Data extends Record<string, unknown> = Record<string, unknown>,
> = PagesPluginFunction<Env, Params, Data, PluginArgs>;

// map of extensions to mimetypes for images
// TODO: should we pull this from somewhere else?
const mimetypeMap = {
'jxl': 'image/jxl',
'avif': 'image/avif',
'webp': 'image/webp',
'gif': 'image/gif',
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
jxl: 'image/jxl',
avif: 'image/avif',
webp: 'image/webp',
gif: 'image/gif',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
};
// very basic "is image" detection
const isImage = (file: string) => {
const ext = file.split('.').pop();
return Boolean(mimetypeMap[ext]);
};
export const onRequest: assetNegotiationPagesPluginFunction = async ({env, next, pluginArgs = {}, request}) => {
export const onRequest: assetNegotiationPagesPluginFunction = async ({ env, next, pluginArgs = {}, request }) => {
pluginArgs.formats ??= ['jxl', 'avif', 'webp'];
const acceptHeader = request.headers.get('accept');
if(!acceptHeader){
if (!acceptHeader) {
// no accept header, so we can't do anything
return next();
}
const url = new URL(request.url);
if(!isImage(url.pathname)){
if (!isImage(url.pathname)) {
// not an image, so we can't do anything
return next();
}
for(const format of pluginArgs.formats){
if(acceptHeader.includes(mimetypeMap[format])){
for (const format of pluginArgs.formats) {
if (acceptHeader.includes(mimetypeMap[format])) {
// found a format that we can serve
// construct a new request for the new format, and try to retrieve it
// this is kinda weird at the moment, because of differences with prod vs local
Expand All @@ -46,22 +46,22 @@ export const onRequest: assetNegotiationPagesPluginFunction = async ({env, next,
cf: request.cf,
});
const asset = await env.ASSETS.fetch(lookupReq);
if(asset && asset.status === 200){
if (asset && asset.status === 200) {
// found the asset, so we can serve it
// sometimes the asset doesn't have a valid content-type, so we should set it
// to do that, we need to create a new response, so the headers become mutable
const newHeaders = new Headers(asset.headers);
if(!asset?.headers?.has?.('content-type') || asset?.headers?.get?.('content-type') === 'application/octet-stream'){
if (!asset?.headers?.has?.('content-type') || asset?.headers?.get?.('content-type') === 'application/octet-stream') {
newHeaders.set('content-type', mimetypeMap[format]);
}
const newResponse = new Response(
[101, 204, 205, 304].includes(asset.status) ? null : asset.body,
{...asset, headers: newHeaders},
{ ...asset, headers: newHeaders },
);
return newResponse;
}
}
}
// no acceptable formats found, pass-through for original image
return next();
};
};
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export type PluginArgs = {
formats?: string[];
};
};
export default function assetNegotiationPlugin<
Env = unknown,
Params extends string = any,
Data extends Record<string, unknown> = Record<string, unknown>,
>(pluginArgs: PluginArgs): PagesPluginFunction<Env, Params, Data>;
Loading

0 comments on commit b88f986

Please sign in to comment.