-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor UI client side only / Fix memory leak with compression
- Loading branch information
1 parent
f6e0425
commit 1063dc9
Showing
17 changed files
with
207 additions
and
48 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { NextResponse } from "next/server"; | ||
import { decompressModConfig } from "~/services/compressionService"; | ||
|
||
const GET = async (request: Request, { params }: { params: { feedId: string } }) => { | ||
try { | ||
const { feedId } = params; | ||
|
||
const modConfig = decompressModConfig(feedId); | ||
if (!modConfig) { | ||
return new NextResponse("Bad Request - Invalid feed id", { status: 400 }); | ||
} | ||
|
||
return new NextResponse(JSON.stringify(modConfig), { | ||
headers: { "Cache-Control": "max-age=86400", "Content-Type": "application/json" }, | ||
}); | ||
} catch (errorMessage) { | ||
console.error(errorMessage); | ||
return new NextResponse((errorMessage as string | undefined) ?? "Unexpected Error", { | ||
status: 500, | ||
}); | ||
} | ||
}; | ||
|
||
export { GET }; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { NextResponse } from "next/server"; | ||
import modConfigSchema from "~/schemas/modConfig"; | ||
import { compressModConfig } from "~/services/compressionService"; | ||
|
||
const POST = async (request: Request) => { | ||
try { | ||
const body = (await request.json()) as unknown; | ||
|
||
const { data: modConfig, error } = modConfigSchema.safeParse(body); | ||
if (error) { | ||
return new NextResponse("Bad Request - Invalid mod config", { status: 400 }); | ||
} | ||
|
||
const feedId = compressModConfig(modConfig); | ||
if (!feedId) { | ||
return new NextResponse("Bad Request - Could not compress mod config", { status: 400 }); | ||
} | ||
|
||
return new NextResponse(feedId, { headers: { "Cache-Control": "max-age=86400" } }); | ||
} catch (errorMessage) { | ||
console.error(errorMessage); | ||
return new NextResponse((errorMessage as string | undefined) ?? "Unexpected Error", { | ||
status: 500, | ||
}); | ||
} | ||
}; | ||
|
||
export { POST }; |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { NextResponse } from "next/server"; | ||
import { z } from "zod"; | ||
import { fetchFeedData } from "~/services/feedService"; | ||
|
||
const POST = async (request: Request) => { | ||
try { | ||
const body = (await request.json()) as unknown; | ||
|
||
const { data: urls, error } = z.array(z.string().url()).safeParse(body); | ||
if (error) { | ||
return new NextResponse("Bad Request - Invalid urls", { status: 400 }); | ||
} | ||
|
||
const feedData = await fetchFeedData(urls); | ||
if (!feedData) { | ||
return new NextResponse("Bad Request - Invalid sources", { status: 400 }); | ||
} | ||
|
||
return new NextResponse(JSON.stringify(feedData), { | ||
headers: { "Cache-Control": "max-age=900", "Content-Type": "application/json" }, | ||
}); | ||
} catch (errorMessage) { | ||
console.error(errorMessage); | ||
return new NextResponse((errorMessage as string | undefined) ?? "Unexpected Error", { | ||
status: 500, | ||
}); | ||
} | ||
}; | ||
|
||
export { POST }; |
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,3 +1,5 @@ | ||
"use client"; | ||
|
||
import formatDuration from "~/utils/formatDuration"; | ||
|
||
type EpisodeCardProps = { | ||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"use client"; | ||
|
||
import type { ReactNode } from "react"; | ||
|
||
type SectionTitleProps = { | ||
|
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,17 +1,38 @@ | ||
import { compress, decompress } from "brotli-compress"; | ||
"use server"; | ||
|
||
import "server-only"; | ||
import brotli from "brotli"; | ||
import modConfigSchema from "~/schemas/modConfig"; | ||
import type { ModConfig } from "~/types/ModConfig"; | ||
|
||
const compressModConfig = async (modConfig: ModConfig): Promise<string> => { | ||
const compressedText = await compress(Buffer.from(JSON.stringify(modConfig))); | ||
return Buffer.from(compressedText).toString("hex"); | ||
const { compress, decompress } = brotli as { | ||
compress: (buffer: Buffer, options?: never) => Uint8Array; | ||
decompress: (buffer: Buffer, outputSize?: number) => Uint8Array; | ||
}; | ||
|
||
const compressModConfig = (modConfig: ModConfig): string => { | ||
const decompressedString = JSON.stringify(modConfig); | ||
const decompressedData = Buffer.from(decompressedString); | ||
|
||
const compressedData = compress(decompressedData); | ||
const compressedString = Buffer.from(compressedData.buffer).toString("hex"); | ||
|
||
return compressedString; | ||
}; | ||
|
||
const decompressModConfig = async (compressedText: string): Promise<ModConfig | undefined> => | ||
decompress(Buffer.from(compressedText, "hex")) | ||
.then((decompressedText) => Buffer.from(decompressedText).toString()) | ||
.then(JSON.parse) | ||
.then((data) => modConfigSchema.parse(data)) | ||
.catch(() => undefined); | ||
const decompressModConfig = (compressedText: string): ModConfig | undefined => { | ||
try { | ||
const compressedData = Buffer.from(compressedText, "hex"); | ||
|
||
const decompressedData = decompress(compressedData); | ||
const decompressedText = Buffer.from(decompressedData.buffer).toString(); | ||
|
||
const modConfig = JSON.parse(decompressedText) as unknown; | ||
|
||
return modConfigSchema.parse(modConfig); | ||
} catch { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export { compressModConfig, decompressModConfig }; |
Oops, something went wrong.