-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
87 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ export async function GET(context: APIContext) { | |
} | ||
|
||
return response; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
"@types/*": ["src/types/*"] | ||
} | ||
} | ||
} | ||
} |