Skip to content

Commit

Permalink
Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
MNThomson committed Nov 17, 2023
1 parent 532beff commit f4fe363
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 85 deletions.
2 changes: 1 addition & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
}),
});
10 changes: 6 additions & 4 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
}
}
110 changes: 55 additions & 55 deletions src/lib/server/miniflare.ts
Original file line number Diff line number Diff line change
@@ -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');
};
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");
};
20 changes: 10 additions & 10 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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();
});
2 changes: 1 addition & 1 deletion src/pages/v2/[...name]/blobs/[reference]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ export async function GET(context: APIContext) {
}

return response;
};
}
2 changes: 1 addition & 1 deletion src/pages/v2/[...name]/manifests/[reference]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ export async function GET(context: APIContext) {
resp.headers.set("Content-Length", data.length.toString());

return resp;
};
}
12 changes: 6 additions & 6 deletions src/pages/v2/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>).token;
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -97,7 +97,7 @@ async function seedBlob(env: Env, hash: string) {
headers: {
Authorization: authHeader,
},
}
},
);

if (req.status != 200) {
Expand All @@ -118,4 +118,4 @@ export const GET: APIRoute = async (context) => {
console.log("DONE Seeding");

return new Response("DONE");
}
};
2 changes: 1 addition & 1 deletion src/pages/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!()");
}
};
10 changes: 5 additions & 5 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -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})$");

Expand All @@ -10,8 +10,8 @@ function parseParams(params: Record<string, string | undefined>): {
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 = "";
Expand All @@ -34,7 +34,7 @@ function parseParams(params: Record<string, string | undefined>): {
"message": "Bad Params",
"detail": "${error}"
}]}`,
{ status: 404 }
{ status: 404 },
),
};
} catch (error) {
Expand All @@ -48,7 +48,7 @@ function parseParams(params: Record<string, string | undefined>): {
"message": "Bad Params",
"detail": "Bad Params"
}]}`,
{ status: 404 }
{ status: 404 },
),
};
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"@types/*": ["src/types/*"]
}
}
}
}

0 comments on commit f4fe363

Please sign in to comment.