format:fix

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-03-13 08:55:24 +01:00
parent 558318c312
commit 48aecc4b37
117 changed files with 4455 additions and 667 deletions

View File

@@ -1,9 +1,9 @@
import type { FastifyReply, FastifyRequest } from "fastify";
import icoToPng from "ico-to-png";
import sharp from "sharp";
import type { FastifyReply, FastifyRequest } from 'fastify';
import icoToPng from 'ico-to-png';
import sharp from 'sharp';
import { createHash } from "@openpanel/common";
import { redis } from "@openpanel/redis";
import { createHash } from '@openpanel/common';
import { redis } from '@openpanel/redis';
interface GetFaviconParams {
url: string;
@@ -12,9 +12,9 @@ interface GetFaviconParams {
async function getImageBuffer(url: string) {
try {
const res = await fetch(url);
const contentType = res.headers.get("content-type");
const contentType = res.headers.get('content-type');
if (!contentType?.includes("image")) {
if (!contentType?.includes('image')) {
return null;
}
@@ -22,7 +22,7 @@ async function getImageBuffer(url: string) {
return null;
}
if (contentType === "image/x-icon" || url.endsWith(".ico")) {
if (contentType === 'image/x-icon' || url.endsWith('.ico')) {
const arrayBuffer = await res.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
return await icoToPng(buffer, 30);
@@ -30,36 +30,36 @@ async function getImageBuffer(url: string) {
return await sharp(await res.arrayBuffer())
.resize(30, 30, {
fit: "cover",
fit: 'cover',
})
.png()
.toBuffer();
} catch (e) {
console.log("Failed to get image from url", url);
console.log('Failed to get image from url', url);
console.log(e);
}
}
const imageExtensions = ["svg", "png", "jpg", "jpeg", "gif", "webp", "ico"];
const imageExtensions = ['svg', 'png', 'jpg', 'jpeg', 'gif', 'webp', 'ico'];
export async function getFavicon(
request: FastifyRequest<{
Querystring: GetFaviconParams;
}>,
reply: FastifyReply,
reply: FastifyReply
) {
function sendBuffer(buffer: Buffer, cacheKey?: string) {
if (cacheKey) {
redis.set(`favicon:${cacheKey}`, buffer.toString("base64"));
redis.set(`favicon:${cacheKey}`, buffer.toString('base64'));
}
reply.type("image/png");
console.log("buffer", buffer.byteLength);
reply.type('image/png');
console.log('buffer', buffer.byteLength);
return reply.send(buffer);
}
if (!request.query.url) {
return reply.status(404).send("Not found");
return reply.status(404).send('Not found');
}
const url = decodeURIComponent(request.query.url);
@@ -69,7 +69,7 @@ export async function getFavicon(
const cacheKey = createHash(url, 32);
const cache = await redis.get(`favicon:${cacheKey}`);
if (cache) {
return sendBuffer(Buffer.from(cache, "base64"));
return sendBuffer(Buffer.from(cache, 'base64'));
}
const buffer = await getImageBuffer(url);
if (buffer && buffer.byteLength > 0) {
@@ -80,7 +80,7 @@ export async function getFavicon(
const { hostname, origin } = new URL(url);
const cache = await redis.get(`favicon:${hostname}`);
if (cache) {
return sendBuffer(Buffer.from(cache, "base64"));
return sendBuffer(Buffer.from(cache, 'base64'));
}
// TRY FAVICON.ICO
@@ -94,7 +94,7 @@ export async function getFavicon(
function findFavicon(res: string) {
const match = res.match(
/(\<link(.+?)image\/x-icon(.+?)\>|\<link(.+?)shortcut\sicon(.+?)\>)/,
/(\<link(.+?)image\/x-icon(.+?)\>|\<link(.+?)shortcut\sicon(.+?)\>)/
);
if (!match) {
return null;
@@ -112,16 +112,16 @@ export async function getFavicon(
}
}
return reply.status(404).send("Not found");
return reply.status(404).send('Not found');
}
export async function clearFavicons(
request: FastifyRequest,
reply: FastifyReply,
reply: FastifyReply
) {
const keys = await redis.keys("favicon:*");
const keys = await redis.keys('favicon:*');
for (const key of keys) {
await redis.del(key);
}
return reply.status(404).send("OK");
return reply.status(404).send('OK');
}

View File

@@ -1,24 +1,24 @@
import { getClientIp, parseIp } from "@/utils/parseIp";
import { isUserAgentSet, parseUserAgent } from "@/utils/parseUserAgent";
import type { FastifyReply, FastifyRequest } from "fastify";
import { assocPath, pathOr } from "ramda";
import { getClientIp, parseIp } from '@/utils/parseIp';
import { isUserAgentSet, parseUserAgent } from '@/utils/parseUserAgent';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { assocPath, pathOr } from 'ramda';
import { getProfileById, upsertProfile } from "@openpanel/db";
import { getProfileById, upsertProfile } from '@openpanel/db';
import type {
IncrementProfilePayload,
UpdateProfilePayload,
} from "@openpanel/sdk";
} from '@openpanel/sdk';
export async function updateProfile(
request: FastifyRequest<{
Body: UpdateProfilePayload;
}>,
reply: FastifyReply,
reply: FastifyReply
) {
const { profileId, properties, ...rest } = request.body;
const projectId = request.projectId;
const ip = getClientIp(request)!;
const ua = request.headers["user-agent"]!;
const ua = request.headers['user-agent']!;
const uaInfo = parseUserAgent(ua);
const geo = await parseIp(ip);
@@ -40,29 +40,29 @@ export async function incrementProfileProperty(
request: FastifyRequest<{
Body: IncrementProfilePayload;
}>,
reply: FastifyReply,
reply: FastifyReply
) {
const { profileId, property, value } = request.body;
const projectId = request.projectId;
const profile = await getProfileById(profileId);
if (!profile) {
return reply.status(404).send("Not found");
return reply.status(404).send('Not found');
}
const parsed = parseInt(
pathOr<string>("0", property.split("."), profile.properties),
10,
pathOr<string>('0', property.split('.'), profile.properties),
10
);
if (isNaN(parsed)) {
return reply.status(400).send("Not number");
return reply.status(400).send('Not number');
}
profile.properties = assocPath(
property.split("."),
property.split('.'),
parsed + value,
profile.properties,
profile.properties
);
await upsertProfile({
@@ -78,29 +78,29 @@ export async function decrementProfileProperty(
request: FastifyRequest<{
Body: IncrementProfilePayload;
}>,
reply: FastifyReply,
reply: FastifyReply
) {
const { profileId, property, value } = request.body;
const projectId = request.projectId;
const profile = await getProfileById(profileId);
if (!profile) {
return reply.status(404).send("Not found");
return reply.status(404).send('Not found');
}
const parsed = parseInt(
pathOr<string>("0", property.split("."), profile.properties),
10,
pathOr<string>('0', property.split('.'), profile.properties),
10
);
if (isNaN(parsed)) {
return reply.status(400).send("Not number");
return reply.status(400).send('Not number');
}
profile.properties = assocPath(
property.split("."),
property.split('.'),
parsed - value,
profile.properties,
profile.properties
);
await upsertProfile({

View File

@@ -1,51 +1,15 @@
{
"name": "@openpanel/api",
"version": "0.0.1",
"scripts": {
"dev": "dotenv -e ../../.env -c -v WATCH=1 tsup",
"testing": "API_PORT=3333 pnpm dev",
"start": "node dist/index.js",
"build": "rm -rf dist && tsup",
"lint": "eslint .",
"format": "prettier --check \"**/*.{mjs,ts,md,json}\"",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@fastify/cors": "^9.0.0",
"@fastify/websocket": "^8.3.1",
"@logtail/pino": "^0.4.19",
"@openpanel/common": "workspace:*",
"@openpanel/db": "workspace:*",
"@openpanel/queue": "workspace:*",
"@openpanel/redis": "workspace:*",
"fastify": "^4.25.2",
"ico-to-png": "^0.2.1",
"pino": "^8.17.2",
"pino-pretty": "^10.3.1",
"ramda": "^0.29.1",
"sharp": "^0.33.2",
"ua-parser-js": "^1.0.37",
"uuid": "^9.0.1"
},
"devDependencies": {
"@openpanel/eslint-config": "workspace:*",
"@openpanel/prettier-config": "workspace:*",
"@openpanel/sdk": "workspace:*",
"@openpanel/tsconfig": "workspace:*",
"@types/ramda": "^0.29.6",
"@types/ua-parser-js": "^0.7.39",
"@types/uuid": "^9.0.8",
"@types/ws": "^8.5.10",
"eslint": "^8.48.0",
"prettier": "^3.0.3",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"eslintConfig": {
"root": true,
"extends": [
"@openpanel/eslint-config/base"
]
},
"prettier": "@openpanel/prettier-config"
import { OpenpanelProvider } from '@openpanel-test/nextjs';
import type { AppProps } from 'next/app';
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<OpenpanelProvider
clientId="0acce97f-1126-4439-b7ee-5d384e2fc94b"
url="http://localhost:3333"
trackScreenViews
/>
<Component {...pageProps} />
</>
);
}