Skip to content

Commit

Permalink
♻️ (re)use the FS
Browse files Browse the repository at this point in the history
  • Loading branch information
fredk3 committed Apr 12, 2024
1 parent db33c74 commit a0744b5
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 59 deletions.
1 change: 1 addition & 0 deletions apps/desktop/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function getRandomPort() {

let hasServerStarted = false;
let totalAttempsLeft = 10;
process.env.ROOT_USER_PATH = app.getPath("userData");
while (!hasServerStarted && totalAttempsLeft > 0) {
try {
process.env.PORT = getRandomPort().toString();
Expand Down
13 changes: 7 additions & 6 deletions apps/web/app/api/local-chains/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { NextRequest } from "next/server";
import { env } from "~/env";
import { getDbClient } from "~/lib/db";
import { localChains } from "~/lib/db/schema/local-chains.sql";
import { eq } from "drizzle-orm";
import { FileSystemCacheDEV } from "~/lib/fs-cache-dev";
import path from "path";
import { CACHE_KEYS } from "~/lib/cache-keys";
import { LOCAL_CHAIN_CACHE_DIR } from "~/lib/constants";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";
Expand All @@ -23,9 +24,9 @@ export async function GET(
}

const slug = ctx.params.slug;
const data = await getDbClient().then((db) =>
db.select().from(localChains).where(eq(localChains.slug, slug)),
const fsCache = new FileSystemCacheDEV(
path.join(env.ROOT_USER_PATH, LOCAL_CHAIN_CACHE_DIR),
);

return Response.json(data[0] ?? null);
return Response.json(await fsCache.get(CACHE_KEYS.networks.single(slug)));
}
77 changes: 45 additions & 32 deletions apps/web/app/api/local-chains/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,38 @@ import type { SingleNetwork } from "~/lib/network";
import { generateRandomString } from "~/lib/shared-utils";
import crypto from "crypto";
import { fileTypeFromBlob } from "file-type";
import { getDbClient } from "~/lib/db";
import { localChains } from "~/lib/db/schema/local-chains.sql";
import { revalidatePath, revalidateTag } from "next/cache";
import { CACHE_KEYS } from "~/lib/cache-keys";
import { FileSystemCacheDEV } from "~/lib/fs-cache-dev";
import path from "path";
import { LOCAL_CHAIN_CACHE_DIR } from "~/lib/constants";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";

export async function GET(request: NextRequest) {
return Response.json(
await getDbClient().then((db) => db.select().from(localChains)),
if (env.NEXT_PUBLIC_TARGET !== "electron") {
return Response.json(
{
errors: {
root: ["this feature is only available for the electron target"],
},
},
{ status: 403 },
);
}

const fsCache = new FileSystemCacheDEV(
path.join(env.ROOT_USER_PATH, LOCAL_CHAIN_CACHE_DIR),
);

const chainKeys = await fsCache.search(CACHE_KEYS.networks.single("local"));

const chains = await Promise.all(
chainKeys.map((key) => fsCache.get<SingleNetwork>(key)),
);

return Response.json(chains.filter((chain) => chain !== null));
}

export async function POST(request: NextRequest) {
Expand Down Expand Up @@ -55,23 +75,21 @@ export async function POST(request: NextRequest) {
if (logo) {
const base64Data = Buffer.from(await logo.arrayBuffer()).toString("base64");
const fileType = await fileTypeFromBlob(logo);
console.log({
fileType,
logo,
});
if (fileType?.mime) {
logoUrl = `data:${fileType.mime};base64,${base64Data}`;
}
}
const fsCache = new FileSystemCacheDEV(
path.join(env.ROOT_USER_PATH, LOCAL_CHAIN_CACHE_DIR),
);
const items = await fsCache.search(CACHE_KEYS.networks.single("local"));

const db = await getDbClient();

const existingLocalChains = await db.select().from(localChains);
const networkSlug = `local-${slugify(body.chainName)}`;

const chainData = {
chainName: body.chainName,
brand: "local",
slug: `local-${slugify(body.chainName)}`,
slug: networkSlug,
config: {
logoUrl,
rpcUrls: {
Expand All @@ -90,7 +108,7 @@ export async function POST(request: NextRequest) {
daLayer: body.daLayer,
paidVersion: false,
accountId: generateRandomString(20),
internalId: existingLocalChains.length + 1,
internalId: items.length + 1,
integrationId: crypto.randomUUID(),
createdTime: new Date(),
} satisfies SingleNetwork;
Expand All @@ -100,31 +118,26 @@ export async function POST(request: NextRequest) {
...rest
} = chainData;
console.log({
...rest,
config: { ...restConfig },
CONFIG_SAVED: {
...rest,
config: { ...restConfig },
},
});

const data = await db
.insert(localChains)
.values(chainData)
.onConflictDoUpdate({
target: localChains.slug,
set: {
config: chainData.config,
namespace: chainData.namespace,
startHeight: chainData.startHeight,
daLayer: chainData.daLayer,
},
})
.returning();
await fsCache.set(CACHE_KEYS.networks.single(networkSlug), {
...chainData,
createdTime: chainData.createdTime.getTime(),
});

const [__, localTag] = CACHE_KEYS.networks.local();
const [allTag] = CACHE_KEYS.networks.all();
revalidateTag(allTag);
revalidateTag(localTag);

revalidatePath("/", "layout");
for (const tag of CACHE_KEYS.networks.local()) {
revalidateTag(tag);
}

return Response.json({
success: true,
data: data[0],
data: chainData,
});
}
2 changes: 2 additions & 0 deletions apps/web/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { createEnv } = require("@t3-oss/env-nextjs");
const env = createEnv({
server: {
DEPLOYMENT_WEBHOOK_SECRET: z.string().optional(),
ROOT_USER_PATH: z.string().optional().default(""),
GITHUB_ACTION_TRIGGER_PERSONAL_ACCESS_TOKEN: z.string().optional(),
NAMESPACE_ENDPOINT: z.string().url().optional(),
BLOB_READ_WRITE_TOKEN: z.string().optional(),
Expand Down Expand Up @@ -74,6 +75,7 @@ const env = createEnv({
DEPLOYMENT_WEBHOOK_SECRET: process.env.DEPLOYMENT_WEBHOOK_SECRET,
GITHUB_ACTION_TRIGGER_PERSONAL_ACCESS_TOKEN:
process.env.GITHUB_ACTION_TRIGGER_PERSONAL_ACCESS_TOKEN,
ROOT_USER_PATH: process.env.ROOT_USER_PATH,
},
});

Expand Down
16 changes: 4 additions & 12 deletions apps/web/lib/cache-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ import type { HeadlessRoute } from "./headless-utils";
*/
export const CACHE_KEYS = {
networks: {
all: () => ["INTEGRATION"],
local: () => [...CACHE_KEYS.networks.all(), "LOCAL"],
all: () => ["INTEGRATION_LIST"],
local: () => [...CACHE_KEYS.networks.all(), "INTEGRATION_LOCAL"],
summary: (nexToken: string | null = null) => [
...CACHE_KEYS.networks.all(),
"INTEGRATION_SUMMARY",
"INTEGRATION_SUMMARY_NEXT_TOKEN",
nexToken?.slice(0, 20) ?? "null",
],
single: (slug: string) => [
...CACHE_KEYS.networks.all(),
"INTEGRATION_SINGLE",
slug,
],
platform: (platform: string) => [
...CACHE_KEYS.networks.all(),
"platform",
platform,
],
single: (slug: string) => ["INTEGRATION_SINGLE", slug],
platform: (platform: string) => ["platform", platform],
status: (slug: string) => [
...CACHE_KEYS.networks.single(slug),
"INTEGRATION_STATUS",
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export const OG_SIZE = {
};

export const ALWAYS_ONLINE_NETWORKS = ["celestia", "eclipse"];
export const LOCAL_CHAIN_CACHE_DIR = ".next/cache/local-chains";
24 changes: 20 additions & 4 deletions apps/web/lib/fs-cache-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ export class FileSystemCacheDEV {

private async initCacheDir(): Promise<void> {
try {
await fs.mkdir(this.cacheDir, { recursive: true });
const stats = await fs.stat(this.cacheDir);
if (!stats.isDirectory()) {
await fs.mkdir(this.cacheDir, { recursive: true });
}
} catch (error) {
if ((error as any).code === "ENOENT") {
await fs.mkdir(this.cacheDir, { recursive: true });
return;
}

console.error("Error creating cache directory:", error);
}
}
Expand All @@ -26,6 +34,7 @@ export class FileSystemCacheDEV {
}

async set<T>(key: CacheId, value: T, ttl?: number): Promise<void> {
await this.initCacheDir();
const cacheEntry: CacheEntry<T> = {
value,
expiry: ttl ? Date.now() + ttl * 1000 : null,
Expand All @@ -35,6 +44,7 @@ export class FileSystemCacheDEV {
}

async get<T>(key: CacheId): Promise<T | null> {
await this.initCacheDir();
const filePath = this.getFilePath(this.computeCacheKey(key));
try {
const data = await fs.readFile(filePath, "utf-8");
Expand All @@ -52,12 +62,18 @@ export class FileSystemCacheDEV {
}
}

/**
* Search for keys in the cache, return all keys that starts with with the key passed in argument
* @param key The key to look up for
* @returns the list of keys it found
*/
async search(key: CacheId): Promise<string[]> {
await this.initCacheDir();
const files = await fs.readdir(this.cacheDir);
return Promise.all(
files.filter((fileName) =>
fileName.startsWith(this.computeCacheKey(fileName)),
),
files
.filter((fileName) => fileName.startsWith(this.computeCacheKey(key)))
.map((file) => file.replaceAll(".json", "")),
);
}

Expand Down
6 changes: 1 addition & 5 deletions apps/web/lib/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ export const getAllNetworks = cache(async function getAllNetworks() {
next: { tags: CACHE_KEYS.networks.local() },
},
)
.then(async (r) => {
const data = JSON.parse(await r.text());
console.log({ data });
return data;
})
.then((r) => r.json())
.then(allNetworkSchema.parse);
} catch (error) {
//... do absolutely nothing
Expand Down

0 comments on commit a0744b5

Please sign in to comment.