Build OrbitFlow frontend admin template
This commit is contained in:
50
worker/index.ts
Normal file
50
worker/index.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/** Cloudflare hosting adapter for the frontend-only OrbitFlow template. */
|
||||
import {
|
||||
handleImageOptimization,
|
||||
DEFAULT_DEVICE_SIZES,
|
||||
DEFAULT_IMAGE_SIZES,
|
||||
} from "vinext/server/image-optimization";
|
||||
import handler from "vinext/server/app-router-entry";
|
||||
|
||||
interface Env {
|
||||
ASSETS: { fetch(request: Request): Promise<Response> };
|
||||
IMAGES: {
|
||||
input(stream: ReadableStream): {
|
||||
transform(options: Record<string, unknown>): {
|
||||
output(options: { format: string; quality: number }): Promise<{ response(): Response }>;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
interface ExecutionContext {
|
||||
waitUntil(promise: Promise<unknown>): void;
|
||||
passThroughOnException(): void;
|
||||
}
|
||||
|
||||
const worker = {
|
||||
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
||||
const url = new URL(request.url);
|
||||
|
||||
if (url.pathname === "/_vinext/image") {
|
||||
const allowedWidths = [...DEFAULT_DEVICE_SIZES, ...DEFAULT_IMAGE_SIZES];
|
||||
return handleImageOptimization(
|
||||
request,
|
||||
{
|
||||
fetchAsset: (path) => env.ASSETS.fetch(new Request(new URL(path, request.url))),
|
||||
transformImage: async (body, { width, format, quality }) => {
|
||||
const result = await env.IMAGES.input(body)
|
||||
.transform(width > 0 ? { width } : {})
|
||||
.output({ format, quality });
|
||||
return result.response();
|
||||
},
|
||||
},
|
||||
allowedWidths,
|
||||
);
|
||||
}
|
||||
|
||||
return handler.fetch(request, env, ctx);
|
||||
},
|
||||
};
|
||||
|
||||
export default worker;
|
||||
Reference in New Issue
Block a user