diff --git a/astro.config.mjs b/astro.config.mjs index 99b069a..d3ee5c5 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -8,6 +8,6 @@ export default defineConfig({ output: "server", adapter: cloudflare({ mode: "directory", - runtime: { mode: 'local', persistTo: '.wrangler/state/v3' } + runtime: { mode: "local", persistTo: ".wrangler/state/v3" }, }), }); diff --git a/src/env.d.ts b/src/env.d.ts index 0afbcbf..3986dfd 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -9,9 +9,11 @@ type ENV = { declare namespace App { interface Locals extends DirectoryRuntime { - runtime: DirectoryRuntime & { env: { - kv: KVNamespace; - r2: R2Bucket; - } }; + runtime: DirectoryRuntime & { + env: { + kv: KVNamespace; + r2: R2Bucket; + }; + }; } } diff --git a/src/lib/server/miniflare.ts b/src/lib/server/miniflare.ts index c069af9..cd3f104 100644 --- a/src/lib/server/miniflare.ts +++ b/src/lib/server/miniflare.ts @@ -1,82 +1,82 @@ -// ABSOLUTE LEGEND: https://github.com/sveltejs/kit/issues/4292#issuecomment-1550596497 +// ABSOLUTE LEGEND: https://github.com/sveltejs/kit/issues/4292#issuecomment-1550596497 type StorageOptionsMemory = { - type: 'memory'; + type: "memory"; }; type StorageOptionsFile = { - type: 'file'; - path: string; + type: "file"; + path: string; }; export type StorageOptions = StorageOptionsMemory | StorageOptionsFile; export const createCache = async (storageOptions: StorageOptions) => { - const { Cache } = await import('@miniflare/cache'); + const { Cache } = await import("@miniflare/cache"); - if (storageOptions.type === 'memory') { - const { MemoryStorage } = await import('@miniflare/storage-memory'); - return new Cache(new MemoryStorage()); - } else if (storageOptions.type === 'file') { - const { FileStorage } = await import('@miniflare/storage-file'); - return new Cache(new FileStorage(storageOptions.path)); - } + if (storageOptions.type === "memory") { + const { MemoryStorage } = await import("@miniflare/storage-memory"); + return new Cache(new MemoryStorage()); + } else if (storageOptions.type === "file") { + const { FileStorage } = await import("@miniflare/storage-file"); + return new Cache(new FileStorage(storageOptions.path)); + } - throw new Error('StorageType not found'); + throw new Error("StorageType not found"); }; export const createD1 = async (storageOptions: StorageOptions) => { - const { createSQLiteDB } = await import('@miniflare/shared'); - const { D1Database, D1DatabaseAPI } = await import('@miniflare/d1'); - - if (storageOptions.type === 'memory') { - const sqliteDb = await createSQLiteDB(':memory:'); - return new D1Database(new D1DatabaseAPI(sqliteDb)); - } else if (storageOptions.type === 'file') { - const sqliteDb = await createSQLiteDB(storageOptions.path); - return new D1Database(new D1DatabaseAPI(sqliteDb)); - } - - throw new Error('StorageType not found'); + const { createSQLiteDB } = await import("@miniflare/shared"); + const { D1Database, D1DatabaseAPI } = await import("@miniflare/d1"); + + if (storageOptions.type === "memory") { + const sqliteDb = await createSQLiteDB(":memory:"); + return new D1Database(new D1DatabaseAPI(sqliteDb)); + } else if (storageOptions.type === "file") { + const sqliteDb = await createSQLiteDB(storageOptions.path); + return new D1Database(new D1DatabaseAPI(sqliteDb)); + } + + throw new Error("StorageType not found"); }; export const createR2 = async (storageOptions: StorageOptions) => { - const { R2Bucket } = await import('@miniflare/r2'); + const { R2Bucket } = await import("@miniflare/r2"); - if (storageOptions.type === 'memory') { - const { MemoryStorage } = await import('@miniflare/storage-memory'); - return new R2Bucket(new MemoryStorage()); - } else if (storageOptions.type === 'file') { - const { FileStorage } = await import('@miniflare/storage-file'); - return new R2Bucket(new FileStorage(storageOptions.path)); - } + if (storageOptions.type === "memory") { + const { MemoryStorage } = await import("@miniflare/storage-memory"); + return new R2Bucket(new MemoryStorage()); + } else if (storageOptions.type === "file") { + const { FileStorage } = await import("@miniflare/storage-file"); + return new R2Bucket(new FileStorage(storageOptions.path)); + } - throw new Error('StorageType not found'); + throw new Error("StorageType not found"); }; export const createKV = async (storageOptions: StorageOptions) => { - const { KVNamespace } = await import('@miniflare/kv'); + const { KVNamespace } = await import("@miniflare/kv"); - if (storageOptions.type === 'memory') { - const { MemoryStorage } = await import('@miniflare/storage-memory'); - return new KVNamespace(new MemoryStorage()); - } else if (storageOptions.type === 'file') { - const { FileStorage } = await import('@miniflare/storage-file'); - return new KVNamespace(new FileStorage(storageOptions.path)); - } + if (storageOptions.type === "memory") { + const { MemoryStorage } = await import("@miniflare/storage-memory"); + return new KVNamespace(new MemoryStorage()); + } else if (storageOptions.type === "file") { + const { FileStorage } = await import("@miniflare/storage-file"); + return new KVNamespace(new FileStorage(storageOptions.path)); + } - throw new Error('StorageType not found'); + throw new Error("StorageType not found"); }; export const createDOStorage = async (storageOptions: StorageOptions) => { - const { DurableObjectStorage } = await import('@miniflare/durable-objects'); - - if (storageOptions.type === 'memory') { - const { MemoryStorage } = await import('@miniflare/storage-memory'); - return new DurableObjectStorage(new MemoryStorage()); - } else if (storageOptions.type === 'file') { - const { FileStorage } = await import('@miniflare/storage-file'); - return new DurableObjectStorage(new FileStorage(storageOptions.path)); - } - - throw new Error('StorageType not found'); -}; \ No newline at end of file + const { DurableObjectStorage } = await import("@miniflare/durable-objects"); + + if (storageOptions.type === "memory") { + const { MemoryStorage } = await import("@miniflare/storage-memory"); + return new DurableObjectStorage(new MemoryStorage()); + } else if (storageOptions.type === "file") { + const { FileStorage } = await import("@miniflare/storage-file"); + return new DurableObjectStorage(new FileStorage(storageOptions.path)); + } + + throw new Error("StorageType not found"); +}; diff --git a/src/middleware.ts b/src/middleware.ts index d4a6522..5ba5728 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,15 +1,15 @@ -import { createKV, createR2 } from './lib/server/miniflare'; +import { createKV, createR2 } from "./lib/server/miniflare"; import { defineMiddleware } from "astro:middleware"; export const onRequest = defineMiddleware(async (context, next) => { - if (import.meta.env.DEV) { - context.locals.runtime = { - env: { - kv: await createKV({ type: 'file', path: '.mf/kv-counter' }), - r2: await createR2({ type: 'file', path: '.mf/r2-bucket' }), - }, - }; - } + if (import.meta.env.DEV) { + context.locals.runtime = { + env: { + kv: await createKV({ type: "file", path: ".mf/kv-counter" }), + r2: await createR2({ type: "file", path: ".mf/r2-bucket" }), + }, + }; + } - return next(); + return next(); }); diff --git a/src/pages/v2/[...name]/blobs/[reference]/index.ts b/src/pages/v2/[...name]/blobs/[reference]/index.ts index 78cbeba..c4ff170 100644 --- a/src/pages/v2/[...name]/blobs/[reference]/index.ts +++ b/src/pages/v2/[...name]/blobs/[reference]/index.ts @@ -20,4 +20,4 @@ export async function GET(context: APIContext) { } return response; -}; +} diff --git a/src/pages/v2/[...name]/manifests/[reference]/index.ts b/src/pages/v2/[...name]/manifests/[reference]/index.ts index 791fb8c..1fb1659 100644 --- a/src/pages/v2/[...name]/manifests/[reference]/index.ts +++ b/src/pages/v2/[...name]/manifests/[reference]/index.ts @@ -46,4 +46,4 @@ export async function GET(context: APIContext) { resp.headers.set("Content-Length", data.length.toString()); return resp; -}; +} diff --git a/src/pages/v2/seed.ts b/src/pages/v2/seed.ts index c841bbb..8256651 100644 --- a/src/pages/v2/seed.ts +++ b/src/pages/v2/seed.ts @@ -4,7 +4,7 @@ let authHeader: string; async function getAuth() { const resp = await fetch( - "https://auth.docker.io/token?scope=repository%3Alibrary%2Fhello-world%3Apull&service=registry.docker.io" + "https://auth.docker.io/token?scope=repository%3Alibrary%2Fhello-world%3Apull&service=registry.docker.io", ); return "Bearer " + ((await resp.json()) as Record).token; } @@ -19,7 +19,7 @@ async function seedTag(env: Env, tag: string) { Authorization: authHeader, Accept: "application/vnd.docker.distribution.manifest.list.v2+json", }, - } + }, ); if (label_req.status != 200) { @@ -45,7 +45,7 @@ async function seedOS(env: Env, hash: string) { Authorization: authHeader, Accept: "application/vnd.docker.distribution.manifest.list.v2+json", }, - } + }, ); if (label_req.status != 200) { @@ -72,7 +72,7 @@ async function seedImage(env: Env, hash: string) { Authorization: authHeader, Accept: "application/vnd.docker.distribution.manifest.v2+json", }, - } + }, ); if (label_req.status != 200) { @@ -97,7 +97,7 @@ async function seedBlob(env: Env, hash: string) { headers: { Authorization: authHeader, }, - } + }, ); if (req.status != 200) { @@ -118,4 +118,4 @@ export const GET: APIRoute = async (context) => { console.log("DONE Seeding"); return new Response("DONE"); -} \ No newline at end of file +}; diff --git a/src/pages/version.ts b/src/pages/version.ts index d214a38..492c066 100644 --- a/src/pages/version.ts +++ b/src/pages/version.ts @@ -3,4 +3,4 @@ export const prerender = true; export const GET: APIRoute = async () => { // return new Response(import.meta.env.PUBLIC_GIT_SHA); return new Response("todo!()"); -} +}; diff --git a/src/utils/url.ts b/src/utils/url.ts index 5172d74..f8ab661 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -1,6 +1,6 @@ // Regex from https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests const nameRegex = new RegExp( - "^[a-z\\d]+([._-][a-z\\d]+)*(/[a-z\\d]+([._-][a-z\\d]+)*)*$" + "^[a-z\\d]+([._-][a-z\\d]+)*(/[a-z\\d]+([._-][a-z\\d]+)*)*$", ); const referenceRegex = new RegExp("^(sha256:)?(\\w[\\w.-]{0,127})$"); @@ -10,8 +10,8 @@ function parseParams(params: Record): { error: Response | null; } { try { - if (!params) throw new Error; - const name = params.name + if (!params) throw new Error(); + const name = params.name; const reference = params.reference as string; let error = ""; @@ -34,7 +34,7 @@ function parseParams(params: Record): { "message": "Bad Params", "detail": "${error}" }]}`, - { status: 404 } + { status: 404 }, ), }; } catch (error) { @@ -48,7 +48,7 @@ function parseParams(params: Record): { "message": "Bad Params", "detail": "Bad Params" }]}`, - { status: 404 } + { status: 404 }, ), }; } diff --git a/tsconfig.json b/tsconfig.json index 4240bc3..0970687 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,4 +7,4 @@ "@types/*": ["src/types/*"] } } -} \ No newline at end of file +}