Skip to content

Commit

Permalink
monograph: use bun in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Nov 24, 2024
1 parent 84abfa3 commit cb0bc39
Show file tree
Hide file tree
Showing 12 changed files with 480 additions and 789 deletions.
1 change: 1 addition & 0 deletions apps/monograph/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
.dev.vars

.wrangler
/output
20 changes: 7 additions & 13 deletions apps/monograph/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@

FROM --platform=$BUILDPLATFORM node:20-alpine
FROM --platform=$BUILDPLATFORM oven/bun:1.1.36-alpine

RUN mkdir -p /home/node/app && chown -R node:node /home/node/app
RUN mkdir -p /home/bun/app && chown -R bun:bun /home/bun/app

WORKDIR /home/node/app
WORKDIR /home/bun/app

USER node
USER bun

COPY --chown=node:node build ./build
COPY --chown=bun:bun output .

RUN mv build/package.json .
RUN bun install

RUN npm install

RUN npm install --include=optional sharp

RUN ls

CMD [ "npm", "run", "start" ]
CMD [ "bun", "run", "start" ]
31 changes: 18 additions & 13 deletions apps/monograph/app/routes/$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import type { MetaFunction, LoaderFunctionArgs } from "@remix-run/node";
import { Cipher } from "@notesnook/crypto";
import { convert } from "html-to-text";
import { Flex, Text } from "@theme-ui/components";
import { useLoaderData } from "@remix-run/react";
import { MonographPage } from "../components/monographpost";
Expand Down Expand Up @@ -89,7 +88,7 @@ export async function loader({ params }: LoaderFunctionArgs) {
metadata
};
} catch (e) {
console.error(e);
// console.error(e);
return {
monograph: null,
metadata: {
Expand Down Expand Up @@ -135,22 +134,28 @@ export default function MonographPost() {
);
}

const extractParagraph = (html: string) => {
if (!html) return "";
return convert(html, {
wordwrap: false,
preserveNewlines: false,
decodeEntities: true
});
};

type Metadata = {
title: string;
fullDescription: string;
shortDescription: string;
datePublished: string;
};

function extractFirstWords(html: string, numWords = 30): string {
// Strip HTML tags and normalize whitespace
const plainText = html
.replace(/<[^>]*>/g, " ")
.replace(/\s+/g, " ")
.trim();

// Split into words and take first N
const words = plainText.split(" ").slice(0, numWords);

// Add ellipsis if text was truncated
const excerpt = words.join(" ");
return words.length < plainText.split(" ").length ? excerpt + "..." : excerpt;
}

function trimDescription(
str: string,
length: number,
Expand All @@ -175,12 +180,12 @@ function addPeriod(str: string) {
return str + "...";
}

function getMonographMetadata(monograph?: Monograph): Metadata {
function getMonographMetadata(monograph: Monograph): Metadata {
const title = monograph?.title || "Not found";
const text = monograph?.encryptedContent
? "This monograph is encrypted. Enter password to view contents."
: monograph?.content
? extractParagraph(monograph?.content.data)
? extractFirstWords(monograph?.content.data, 100)
: "";
const shortDescription = trimDescription(text, 150, true);
const fullDescription = trimDescription(text, 300, true);
Expand Down
2 changes: 1 addition & 1 deletion apps/monograph/app/utils/spam-filter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function isSpam(monograph: Monograph) {
}
return isSpam;
} catch (e) {
console.error(e);
// console.error(e);
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/monograph/app/utils/storage/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function read<T>(key: string, fallback: T): Promise<T> {
try {
return (JSON.parse(await readFile(key, "utf-8")) as T) || fallback;
} catch (e) {
console.error(e);
// console.error(e);
return fallback;
}
}
Expand Down
4 changes: 2 additions & 2 deletions apps/monograph/app/utils/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const cache: Record<
export async function read<T>(key: string, fallback: T) {
const cached = cache[key];
if (cached && cached.ttl > Date.now() - cached.cachedAt) {
return cached.value;
return cached.value as T;
}
const value = (await provider).read<T>(key, fallback);
cache[key] = {
ttl: 5 * 60 * 1000,
ttl: 60 * 60 * 1000,
value,
cachedAt: Date.now()
};
Expand Down
4 changes: 2 additions & 2 deletions apps/monograph/app/utils/storage/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export async function read<T>(key: string, fallback: T): Promise<T> {
key
});
if (typeof response === "object" && !response.success) {
console.error("failed:", response.errors);
// console.error("failed:", response.errors);
return fallback;
}
return (
JSON.parse(typeof response === "string" ? response : response.result) ||
fallback
);
} catch (e) {
console.error(e);
// console.error(e);
return fallback;
}
}
Expand Down
Loading

0 comments on commit cb0bc39

Please sign in to comment.