OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M4-003): P4: @openpanel/logger + common/server dissolve into core (shared/clients); consumers repointed; tests travel
ralph046e7366bbcd
diff --git a/apps/api/package.json b/apps/api/package.jsonindex 69b6860cd..f1d9f3d3b 100644--- a/apps/api/package.json+++ b/apps/api/package.json@@ -36,7 +36,6 @@"@openpanel/geo": "workspace:*","@openpanel/integrations": "workspace:^","@openpanel/json": "workspace:*",- "@openpanel/logger": "workspace:*","@openpanel/mcp": "workspace:*","@openpanel/payments": "workspace:*","@openpanel/queue": "workspace:*",diff --git a/apps/api/scripts/mock.ts b/apps/api/scripts/mock.tsindex a4bdbf7c3..3f6c8db32 100644--- a/apps/api/scripts/mock.ts+++ b/apps/api/scripts/mock.ts@@ -1,7 +1,7 @@import fs from 'node:fs';import * as faker from '@faker-js/faker';import { generateId } from '@openpanel/common';-import { hashPassword } from '@openpanel/common/server';+import { hashPassword } from '@openpanel/core';import { ClientType, db } from '@openpanel/db';import { getRedisCache } from '@openpanel/redis';import { v4 as uuidv4 } from 'uuid';@@ -133,7 +133,7 @@ function generateEvents(): Event[] {clientId,profile: profiles[i % PROFILE_COUNT]!,eventsCount: Math.floor(Math.random() * 10),- }),+ }));}});@@ -150,7 +150,7 @@ let lastTriggeredIndex = 0;async function triggerEvents(generatedEvents: any[]) {const EVENTS_PER_SECOND = Number.parseInt(process.env.EVENTS_PER_SECOND || '100',- 10,+ 10);const INTERVAL_MS = 1000 / EVENTS_PER_SECOND;@@ -164,7 +164,7 @@ async function triggerEvents(generatedEvents: any[]) {await trackit(event);console.log(`Event ${lastTriggeredIndex + 1} sent successfully`);console.log(- `sending ${event.track.payload?.properties?.__path} from user ${event.headers['user-agent']}`,+ `sending ${event.track.payload?.properties?.__path} from user ${event.headers['user-agent']}`);} catch (error) {console.error(`Failed to send event ${lastTriggeredIndex + 1}:`, error);@@ -174,7 +174,7 @@ async function triggerEvents(generatedEvents: any[]) {const remainingEvents = generatedEvents.length - lastTriggeredIndex;console.log(- `Triggered ${lastTriggeredIndex} events. Remaining: ${remainingEvents}`,+ `Triggered ${lastTriggeredIndex} events. Remaining: ${remainingEvents}`);if (remainingEvents > 0) {@@ -215,7 +215,7 @@ async function createMock(file: string) {fs.writeFileSync(file,JSON.stringify(insertFakeEvents(scrambleEvents(generateEvents())), null, 2),- 'utf-8',+ 'utf-8');}@@ -438,7 +438,7 @@ async function simultaneousRequests() {if (group.parallel && group.tracks.length > 1) {// Parallel execution for same-flagged tracksconsole.log(- `Firing ${group.tracks.length} parallel requests with flag '${group.parallel}'`,+ `Firing ${group.tracks.length} parallel requests with flag '${group.parallel}'`);const promises = group.tracks.map(async (track) => {const { name, parallel, ...properties } = track;diff --git a/apps/api/src/app.ts b/apps/api/src/app.tsindex 176fa63d9..d1cacbba6 100644--- a/apps/api/src/app.ts+++ b/apps/api/src/app.ts@@ -1,23 +1,36 @@/** biome-ignore-all lint/suspicious/useAwait: fastify need async or done callbacks */++import { toFastifyHandler } from '@better-agent/adapters';import compress from '@fastify/compress';import cookie from '@fastify/cookie';import cors, { type FastifyCorsOptions } from '@fastify/cors';import fastifySwagger from '@fastify/swagger';import fastifySwaggerUI from '@fastify/swagger-ui';import {+ decodeSessionToken,EMPTY_SESSION,type SessionValidationResult,- decodeSessionToken,validateSessionToken,} from '@openpanel/auth';import { generateId } from '@openpanel/common';-import { getTrustedIpFromHeaders } from '@openpanel/common/server/get-client-ip';-import { type IServiceClientWithProject, runWithAlsSession } from '@openpanel/db';+import { getTrustedIpFromHeaders } from '@openpanel/core';+import {+ getConversationById,+ getOrganizationByProjectIdCached,+ getProjectAccess,+ getSettingsForProject,+ type IServiceClientWithProject,+ runWithAlsSession,+} from '@openpanel/db';import type { AppRouter } from '@openpanel/trpc';import { appRouter, createContext } from '@openpanel/trpc';import type { FastifyTRPCPluginOptions } from '@trpc/server/adapters/fastify';import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';-import type { FastifyBaseLogger, FastifyInstance, FastifyRequest } from 'fastify';+import type {+ FastifyBaseLogger,+ FastifyInstance,+ FastifyRequest,+} from 'fastify';import Fastify from 'fastify';import metricsPlugin from 'fastify-metrics';import {@@ -26,6 +39,8 @@ import {serializerCompiler,validatorCompiler,} from 'fastify-zod-openapi';+import { chatApp } from './agents/app';+import { chatRunContext } from './agents/run-context';import {healthcheck,liveness,@@ -35,16 +50,6 @@ import { ipHook } from './hooks/ip.hook';import { requestIdHook } from './hooks/request-id.hook';import { requestLoggingHook } from './hooks/request-logging.hook';import { timestampHook } from './hooks/timestamp.hook';-import { toFastifyHandler } from '@better-agent/adapters';-import { chatApp } from './agents/app';-import { chatRunContext } from './agents/run-context';-import {- db,- getConversationById,- getOrganizationByProjectIdCached,- getProjectAccess,- getSettingsForProject,-} from '@openpanel/db';import eventRouter from './routes/event.router';import exportRouter from './routes/export.router';import gscCallbackRouter from './routes/gsc-callback.router';@@ -79,7 +84,7 @@ export interface BuildAppOptions {}export async function buildApp(- options: BuildAppOptions = {},+ options: BuildAppOptions = {}): Promise<FastifyInstance> {const { testing = false } = options;@@ -111,14 +116,17 @@ export async function buildApp(fastify.register(cors, () => {return (req: FastifyRequest,- callback: (error: Error | null, options: FastifyCorsOptions) => void,+ callback: (error: Error | null, options: FastifyCorsOptions) => void) => {const isPrivatePath = corsPaths.some((p) => req.url.startsWith(p));if (isPrivatePath) {const origin = req.headers.origin;const isAllowed = origin && dashboardOrigins.includes(origin);- return callback(null, { origin: isAllowed ? origin : false, credentials: true });+ return callback(null, {+ origin: isAllowed ? origin : false,+ credentials: true,+ });}return callback(null, { origin: '*', maxAge: 86_400 * 7 });@@ -147,7 +155,7 @@ export async function buildApp(try {const sessionId = decodeSessionToken(req.cookies?.session);const session = await runWithAlsSession(sessionId, () =>- validateSessionToken(req.cookies.session),+ validateSessionToken(req.cookies.session));req.session = session;} catch {@@ -156,7 +164,7 @@ export async function buildApp(} else if (process.env.DEMO_USER_ID) {try {const session = await runWithAlsSession('1', () =>- validateSessionToken(null),+ validateSessionToken(null));req.session = session;} catch {@@ -173,7 +181,10 @@ export async function buildApp(router: appRouter,createContext,onError(ctx) {- if (ctx.error.code === 'UNAUTHORIZED' && ctx.path === 'organization.list') {+ if (+ ctx.error.code === 'UNAUTHORIZED' &&+ ctx.path === 'organization.list'+ ) {return;}@@ -182,7 +193,7 @@ export async function buildApp(// Cloudflare when an abuser needs blocking at the edge.const { ip, header } = getTrustedIpFromHeaders(ctx.req.headers,- ctx.req.socket?.remoteAddress,+ ctx.req.socket?.remoteAddress);const payload = {err: ctx.error,@@ -253,8 +264,7 @@ export async function buildApp(// "claude-sonnet-4-5/run" → run// "claude-sonnet-4-5/conversations/abc" → load conversation// "__titler/run" → title stream (no project context)- const wildcard =- (request.params as { '*'?: string })['*'] ?? '';+ const wildcard = (request.params as { '*'?: string })['*'] ?? '';const segments = wildcard.split('/').filter(Boolean);const agentName = segments[0] ?? '';const route = segments[1] ?? '';@@ -294,7 +304,7 @@ export async function buildApp(const projectId = body?.context?.projectId;const organizationIdFromBody = body?.context?.organizationId;- if (!projectId || !organizationIdFromBody) {+ if (!(projectId && organizationIdFromBody)) {return reply.status(400).send({message: 'Missing projectId or organizationId in context',});@@ -311,8 +321,7 @@ export async function buildApp(getSettingsForProject(projectId).catch(() => ({ timezone: 'UTC' })),]);if (- !access ||- !organization ||+ !(access && organization) ||organization.id !== organizationIdFromBody) {return reply@@ -333,7 +342,7 @@ export async function buildApp(organizationId: organization.id,timezone: settings.timezone || 'UTC',},- () => agentHandler(request, reply),+ () => agentHandler(request, reply));});}@@ -354,7 +363,10 @@ export async function buildApp({ name: 'Import', description: 'Import historical data' },{ name: 'Insights', description: 'Query analytics data' },{ name: 'Manage', description: 'Manage projects and clients' },- { name: 'Event', description: 'Legacy event ingestion (deprecated, use /track)' },+ {+ name: 'Event',+ description: 'Legacy event ingestion (deprecated, use /track)',+ },],},...fastifyZodOpenApiTransformers,@@ -365,7 +377,9 @@ export async function buildApp(return fastifyZodOpenApiTransformers.transform(args);},});- await instance.register(fastifySwaggerUI, { routePrefix: '/documentation' });+ await instance.register(fastifySwaggerUI, {+ routePrefix: '/documentation',+ });// Prometheus metrics: skip in tests (causes global state conflicts across test runs)if (!testing) {@@ -385,7 +399,10 @@ export async function buildApp(instance.get('/healthz/live', { schema: { hide: true } }, liveness);instance.get('/healthz/ready', { schema: { hide: true } }, readiness);instance.get('/', { schema: { hide: true } }, (_request, reply) =>- reply.send({ status: 'ok', message: 'Successfully running OpenPanel.dev API' }),+ reply.send({+ status: 'ok',+ message: 'Successfully running OpenPanel.dev API',+ }));});diff --git a/apps/api/src/controllers/event.controller.ts b/apps/api/src/controllers/event.controller.tsindex 6ccf85110..40f37b903 100644--- a/apps/api/src/controllers/event.controller.ts+++ b/apps/api/src/controllers/event.controller.ts@@ -1,5 +1,5 @@import { generateId } from '@openpanel/common';-import { parseUserAgent } from '@openpanel/common/server';+import { parseUserAgent } from '@openpanel/core';import { getSalts } from '@openpanel/db';import { getAsnInfo, getGeoLocation } from '@openpanel/geo';import {diff --git a/apps/api/src/controllers/manage.controller.ts b/apps/api/src/controllers/manage.controller.tsindex 028955f9a..6685bce34 100644--- a/apps/api/src/controllers/manage.controller.ts+++ b/apps/api/src/controllers/manage.controller.ts@@ -1,6 +1,6 @@import crypto from 'node:crypto';import { stripTrailingSlash } from '@openpanel/common';-import { hashPassword } from '@openpanel/common/server';+import { hashPassword } from '@openpanel/core';import {db,getClientByIdCached,@@ -180,9 +180,7 @@ export async function updateProject(updateData.name = body.name;}if (body.domain !== undefined) {- updateData.domain = body.domain- ? stripTrailingSlash(body.domain)- : null;+ updateData.domain = body.domain ? stripTrailingSlash(body.domain) : null;}if (body.cors !== undefined) {updateData.cors = body.cors.map((c) => stripTrailingSlash(c));diff --git a/apps/api/src/controllers/misc.controller.ts b/apps/api/src/controllers/misc.controller.tsindex 75ac7ea6e..7a28b633a 100644--- a/apps/api/src/controllers/misc.controller.ts+++ b/apps/api/src/controllers/misc.controller.ts@@ -1,22 +1,25 @@import crypto from 'node:crypto';-import { logger } from '@/utils/logger';-import { parseUrlMeta } from '@/utils/parseUrlMeta';+import {+ DEFAULT_IP_HEADER_ORDER,+ getClientIpFromHeaders,+} from '@openpanel/core';+import { ch, chQuery, formatClickhouseDate, TABLE_NAMES } from '@openpanel/db';+import { type GeoLocation, getGeoLocation } from '@openpanel/geo';+import { getCache, getRedisCache } from '@openpanel/redis';+import type { FastifyReply, FastifyRequest } from 'fastify';import {ALLOWED_IMAGE_CONTENT_TYPES,normalizeContentType,processImage,processOgImage,} from '@/utils/image-proxy';-import { BlockedUrlError, assertPublicUrl, safeFetch } from '@/utils/safe-fetch';-import type { FastifyReply, FastifyRequest } from 'fastify';-+import { logger } from '@/utils/logger';+import { parseUrlMeta } from '@/utils/parseUrlMeta';import {- DEFAULT_IP_HEADER_ORDER,- getClientIpFromHeaders,-} from '@openpanel/common/server/get-client-ip';-import { TABLE_NAMES, ch, chQuery, formatClickhouseDate } from '@openpanel/db';-import { type GeoLocation, getGeoLocation } from '@openpanel/geo';-import { getCache, getRedisCache } from '@openpanel/redis';+ assertPublicUrl,+ BlockedUrlError,+ safeFetch,+} from '@/utils/safe-fetch';interface GetFaviconParams {url: string;@@ -43,7 +46,9 @@ function createCacheKey(url: string, prefix = 'favicon'): string {*/function validateUrl(raw?: string): URL | null {try {- if (!raw) throw new Error('Missing ?url');+ if (!raw) {+ throw new Error('Missing ?url');+ }const url = new URL(raw);if (url.protocol !== 'http:' && url.protocol !== 'https:') {throw new Error('Only http/https URLs are allowed');@@ -56,7 +61,7 @@ function validateUrl(raw?: string): URL | null {// Binary cache functions (more efficient than base64)async function getFromCacheBinary(- key: string,+ key: string): Promise<{ buffer: Buffer; contentType: string } | null> {const redis = getRedisCache();const [bufferBase64, contentType] = await Promise.all([@@ -64,14 +69,16 @@ async function getFromCacheBinary(redis.get(`${key}:ctype`),]);- if (!bufferBase64 || !contentType) return null;+ if (!(bufferBase64 && contentType)) {+ return null;+ }return { buffer: Buffer.from(bufferBase64, 'base64'), contentType };}async function setToCacheBinary(key: string,buffer: Buffer,- contentType: string,+ contentType: string): Promise<void> {const redis = getRedisCache();await Promise.all([@@ -82,7 +89,7 @@ async function setToCacheBinary(// Fetch image with SSRF protection, timeout and size limitsasync function fetchImage(- url: URL,+ url: URL): Promise<{ buffer: Buffer; contentType: string; status: number }> {try {// `safeFetch` validates and pins every hop, so a redirect cannot be used@@ -105,14 +112,18 @@ async function fetchImage(}const contentType = normalizeContentType(- result.headers.get('content-type'),+ result.headers.get('content-type'));if (!ALLOWED_IMAGE_CONTENT_TYPES.has(contentType)) {logger.debug({ url: url.toString(), contentType },- 'Refusing non-image response',+ 'Refusing non-image response');- return { buffer: Buffer.alloc(0), contentType: 'text/plain', status: 415 };+ return {+ buffer: Buffer.alloc(0),+ contentType: 'text/plain',+ status: 415,+ };}return { buffer: result.body, contentType, status: 200 };@@ -120,7 +131,7 @@ async function fetchImage(if (error instanceof BlockedUrlError) {logger.warn({ url: url.toString(), reason: error.message },- 'Blocked image fetch',+ 'Blocked image fetch');}return { buffer: Buffer.alloc(0), contentType: 'text/plain', status: 500 };@@ -152,7 +163,7 @@ export async function getFavicon(request: FastifyRequest<{Querystring: GetFaviconParams;}>,- reply: FastifyReply,+ reply: FastifyReply) {try {logger.info({ url: request.query.url }, 'getFavicon');@@ -188,7 +199,7 @@ export async function getFavicon(const meta = await parseUrlMeta(url.toString());logger.info({ url: url.toString(), favicon: meta?.favicon },- 'parseUrlMeta result',+ 'parseUrlMeta result');if (meta?.favicon) {imageUrl = new URL(meta.favicon);@@ -204,7 +215,7 @@ export async function getFavicon(originalUrl: url.toString(),imageUrl: imageUrl.toString(),},- 'Fetching favicon',+ 'Fetching favicon');// Fetch the image@@ -218,7 +229,7 @@ export async function getFavicon(bufferLength: buffer.length,contentType,},- 'Favicon fetch result',+ 'Favicon fetch result');// If the direct favicon fetch failed and it's not from DuckDuckGo's service,@@ -226,7 +237,7 @@ export async function getFavicon(if (buffer.length === 0 && !imageUrl.hostname.includes('duckduckgo.com')) {const { hostname } = url;const duckduckgoUrl = new URL(- `https://icons.duckduckgo.com/ip3/${hostname}.ico`,+ `https://icons.duckduckgo.com/ip3/${hostname}.ico`);logger.info(@@ -234,7 +245,7 @@ export async function getFavicon(originalUrl: url.toString(),duckduckgoUrl: duckduckgoUrl.toString(),},- 'Trying DuckDuckGo favicon service',+ 'Trying DuckDuckGo favicon service');const duckduckgoResult = await fetchImage(duckduckgoUrl);@@ -249,7 +260,7 @@ export async function getFavicon(bufferLength: buffer.length,contentType,},- 'DuckDuckGo favicon result',+ 'DuckDuckGo favicon result');}@@ -265,7 +276,7 @@ export async function getFavicon(const processedBuffer = await processImage(buffer,imageUrl.toString(),- contentType,+ contentType);logger.info(@@ -274,7 +285,7 @@ export async function getFavicon(originalBufferLength: buffer.length,processedBufferLength: processedBuffer.length,},- 'Favicon processing result',+ 'Favicon processing result');// `processImage` either passed an ICO through untouched or rasterized to@@ -293,7 +304,7 @@ export async function getFavicon(if (error instanceof BlockedUrlError) {logger.warn({ url: request.query.url, reason: error.message },- 'Blocked favicon fetch',+ 'Blocked favicon fetch');reply.header('Cache-Control', 'no-store');return reply@@ -302,10 +313,7 @@ export async function getFavicon(.send('Bad request');}- logger.error(- { err: error, url: request.query.url },- 'Favicon fetch error',- );+ logger.error({ err: error, url: request.query.url }, 'Favicon fetch error');const message =process.env.NODE_ENV === 'production'@@ -318,7 +326,7 @@ export async function getFavicon(export async function clearFavicons(request: FastifyRequest,- reply: FastifyReply,+ reply: FastifyReply) {const redis = getRedisCache();const keys = await redis.keys('favicon:*');@@ -334,7 +342,7 @@ export async function clearFavicons(export async function clearOgImages(request: FastifyRequest,- reply: FastifyReply,+ reply: FastifyReply) {const redis = getRedisCache();const keys = await redis.keys('og:*');@@ -355,7 +363,7 @@ export async function ping(count: number;};}>,- reply: FastifyReply,+ reply: FastifyReply) {try {await ch.insert({@@ -385,10 +393,10 @@ export async function ping(export async function stats(request: FastifyRequest, reply: FastifyReply) {const res = await getCache('api:stats', 60 * 60, async () => {const projects = await chQuery<{ project_id: string; count: number }>(- `SELECT project_id, count(*) as count from ${TABLE_NAMES.events} GROUP by project_id order by count()`,+ `SELECT project_id, count(*) as count from ${TABLE_NAMES.events} GROUP by project_id order by count()`);const last24h = await chQuery<{ count: number }>(- `SELECT count(*) as count from ${TABLE_NAMES.events} WHERE created_at > now() - interval '24 hours'`,+ `SELECT count(*) as count from ${TABLE_NAMES.events} WHERE created_at > now() - interval '24 hours'`);return { projects, last24hCount: last24h[0]?.count || 0 };});@@ -410,7 +418,7 @@ export async function getGeo(request: FastifyRequest, reply: FastifyReply) {ip,geo: await getGeoLocation(ip),};- }),+ }));if (!ip) {@@ -428,7 +436,7 @@ export async function getGeo(request: FastifyRequest, reply: FastifyReply) {acc[other.header] = other;return acc;},- {} as Record<string, { ip: string; header: string; geo: GeoLocation }>,+ {} as Record<string, { ip: string; header: string; geo: GeoLocation }>),});}@@ -439,7 +447,7 @@ export async function getOgImage(url: string;};}>,- reply: FastifyReply,+ reply: FastifyReply) {try {const url = validateUrl(request.query.url);@@ -494,7 +502,7 @@ export async function getOgImage(if (error instanceof BlockedUrlError) {logger.warn({ url: request.query.url, reason: error.message },- 'Blocked OG image fetch',+ 'Blocked OG image fetch');reply.header('Cache-Control', 'no-store');return reply@@ -505,7 +513,7 @@ export async function getOgImage(logger.error({ err: error, url: request.query.url },- 'OG image fetch error',+ 'OG image fetch error');const message =diff --git a/apps/api/src/controllers/profile.controller.ts b/apps/api/src/controllers/profile.controller.tsindex 9dd5efd7b..351f0312c 100644--- a/apps/api/src/controllers/profile.controller.ts+++ b/apps/api/src/controllers/profile.controller.ts@@ -1,19 +1,18 @@-import type { FastifyReply, FastifyRequest } from 'fastify';-import { assocPath, pathOr } from 'ramda';--import { parseUserAgent } from '@openpanel/common/server';+import { parseUserAgent } from '@openpanel/core';import { getProfileById, upsertProfile } from '@openpanel/db';import { getGeoLocation } from '@openpanel/geo';import type {DeprecatedIncrementProfilePayload,DeprecatedUpdateProfilePayload,} from '@openpanel/validation';+import type { FastifyReply, FastifyRequest } from 'fastify';+import { assocPath, pathOr } from 'ramda';export async function updateProfile(request: FastifyRequest<{Body: DeprecatedUpdateProfilePayload;}>,- reply: FastifyReply,+ reply: FastifyReply) {const payload = request.body;const projectId = request.client!.projectId;@@ -54,7 +53,7 @@ export async function incrementProfileProperty(request: FastifyRequest<{Body: DeprecatedIncrementProfilePayload;}>,- reply: FastifyReply,+ reply: FastifyReply) {const { profileId, property, value } = request.body;const projectId = request.client!.projectId;@@ -69,7 +68,7 @@ export async function incrementProfileProperty(const parsed = Number.parseInt(pathOr<string>('0', property.split('.'), profile.properties),- 10,+ 10);if (Number.isNaN(parsed)) {@@ -79,7 +78,7 @@ export async function incrementProfileProperty(profile.properties = assocPath(property.split('.'),parsed + value,- profile.properties,+ profile.properties);await upsertProfile({@@ -96,7 +95,7 @@ export async function decrementProfileProperty(request: FastifyRequest<{Body: DeprecatedIncrementProfilePayload;}>,- reply: FastifyReply,+ reply: FastifyReply) {const { profileId, property, value } = request.body;const projectId = request.client?.projectId;@@ -111,7 +110,7 @@ export async function decrementProfileProperty(const parsed = Number.parseInt(pathOr<string>('0', property.split('.'), profile.properties),- 10,+ 10);if (Number.isNaN(parsed)) {@@ -121,7 +120,7 @@ export async function decrementProfileProperty(profile.properties = assocPath(property.split('.'),parsed - value,- profile.properties,+ profile.properties);await upsertProfile({diff --git a/apps/api/src/controllers/tools.controller.ts b/apps/api/src/controllers/tools.controller.tsindex 752a087c4..326fd35f4 100644--- a/apps/api/src/controllers/tools.controller.ts+++ b/apps/api/src/controllers/tools.controller.ts@@ -1,19 +1,19 @@import * as dns from 'node:dns/promises';import * as net from 'node:net';import * as tls from 'node:tls';-import { getClientIpFromHeaders } from '@openpanel/common/server/get-client-ip';+import { getClientIpFromHeaders } from '@openpanel/core';+import { getGeoLocation } from '@openpanel/geo';+import * as cheerio from 'cheerio';+import type { FastifyReply, FastifyRequest } from 'fastify';import {- BlockedUrlError,assertPublicHostname,assertPublicUrl,+ BlockedUrlError,isBlockedIp,safeFetch,} from '@/utils/safe-fetch';-import { getGeoLocation } from '@openpanel/geo';-import * as cheerio from 'cheerio';-import type { FastifyReply, FastifyRequest } from 'fastify';-const TIMEOUT_MS = 10000;+const TIMEOUT_MS = 10_000;const MAX_REDIRECTS = 10;const MAX_HTML_BYTES = 5_000_000;@@ -103,11 +103,7 @@ const SITE_CHECK_MAX = 10;const IP_LOOKUP_WINDOW = 60 * 1000;const IP_LOOKUP_MAX = 20;-function checkRateLimit(- key: string,- windowMs: number,- max: number,-): boolean {+function checkRateLimit(key: string, windowMs: number, max: number): boolean {const now = Date.now();const record = rateLimitMap.get(key);@@ -131,19 +127,31 @@ function detectCDN(headers: Headers): string | null {const fastly = headers.get('fastly-request-id');const cloudfront = headers.get('x-amz-cf-id');- if (cfRay || server.includes('cloudflare')) return 'Cloudflare';- if (vercelId || server.includes('vercel')) return 'Vercel';- if (fastly || server.includes('fastly')) return 'Fastly';- if (cloudfront || server.includes('cloudfront')) return 'CloudFront';- if (server.includes('nginx')) return 'Nginx';- if (server.includes('apache')) return 'Apache';+ if (cfRay || server.includes('cloudflare')) {+ return 'Cloudflare';+ }+ if (vercelId || server.includes('vercel')) {+ return 'Vercel';+ }+ if (fastly || server.includes('fastly')) {+ return 'Fastly';+ }+ if (cloudfront || server.includes('cloudfront')) {+ return 'CloudFront';+ }+ if (server.includes('nginx')) {+ return 'Nginx';+ }+ if (server.includes('apache')) {+ return 'Apache';+ }return null;}async function checkRobotsTxt(baseUrl: string,- path: string,+ path: string): Promise<'allowed' | 'blocked' | 'error'> {try {const robotsUrl = new URL('/robots.txt', baseUrl);@@ -200,7 +208,7 @@ async function checkSitemap(baseUrl: string): Promise<boolean> {}async function getSSLInfo(- hostname: string,+ hostname: string): Promise<SiteCheckResult['technical']['ssl'] | null> {// Raw TLS handshakes are as good a probe as an HTTP request, so the host has// to clear the same check, and we connect to the address we validated.@@ -223,7 +231,7 @@ async function getSSLInfo(const cert = socket.getPeerCertificate(true);socket.destroy();- if (!cert || !cert.valid_to) {+ if (!(cert && cert.valid_to)) {resolve(null);return;}@@ -233,7 +241,7 @@ async function getSSLInfo(issuer: cert.issuer?.CN || 'Unknown',expires: cert.valid_to,});- },+ });socket.on('error', () => {@@ -274,7 +282,7 @@ async function getIPInfo(ip: string): Promise<IPInfo> {const response = await fetch(`https://ip-api.com/json/${ip}?fields=isp,as,org,query,status`,- { signal: controller.signal },+ { signal: controller.signal });clearTimeout(timeout);@@ -306,7 +314,7 @@ async function getIPInfo(ip: string): Promise<IPInfo> {async function measureConnectionTime(hostname: string,- port: number,+ port: number): Promise<{ connectTime: number; tlsTime: number }> {// Without this check the timing measurement doubles as an internal port// scanner, since open and closed ports are trivially distinguishable.@@ -363,7 +371,7 @@ async function measureConnectionTime(async function fetchWithRedirects(url: string,- maxRedirects: number = MAX_REDIRECTS,+ maxRedirects: number = MAX_REDIRECTS): Promise<{finalUrl: string;redirectChain: RedirectHop[];@@ -493,10 +501,18 @@ async function fetchWithRedirects(function calculateSecurityScore(security: SiteCheckResult['security']): number {let score = 0;- if (security.csp) score += 25;- if (security.xFrameOptions) score += 15;- if (security.xContentTypeOptions) score += 15;- if (security.hsts) score += 25;+ if (security.csp) {+ score += 25;+ }+ if (security.xFrameOptions) {+ score += 15;+ }+ if (security.xContentTypeOptions) {+ score += 15;+ }+ if (security.hsts) {+ score += 25;+ }if (security.xFrameOptions?.toLowerCase() === 'deny' ||security.xFrameOptions?.toLowerCase() === 'sameorigin'@@ -511,7 +527,7 @@ function calculateSecurityScore(security: SiteCheckResult['security']): number {export async function siteChecker(request: FastifyRequest<{ Querystring: { url?: string } }>,- reply: FastifyReply,+ reply: FastifyReply) {const urlParam = request.query.url;@@ -533,7 +549,7 @@ export async function siteChecker(return reply.status(400).send({ error: 'Invalid URL' });}- if (!url.protocol || !url.protocol.startsWith('http')) {+ if (!(url.protocol && url.protocol.startsWith('http'))) {url = new URL(`https://${urlParam}`);}@@ -609,7 +625,7 @@ export async function siteChecker(const robotsTxtStatus = await checkRobotsTxt(finalUrl.toString(),- finalUrlObj.pathname,+ finalUrlObj.pathname);const hasSitemap = await checkSitemap(finalUrl.toString());@@ -690,18 +706,27 @@ export async function siteChecker(}return reply.status(500).send({- error:- error instanceof Error ? error.message : 'Failed to analyze site',+ error: error instanceof Error ? error.message : 'Failed to analyze site',});}}function isPrivateIP(ip: string): boolean {- if (ip === '::1') return true;- if (ip.startsWith('::ffff:127.')) return true;- if (ip.startsWith('127.')) return true;- if (ip.startsWith('10.')) return true;- if (ip.startsWith('192.168.')) return true;+ if (ip === '::1') {+ return true;+ }+ if (ip.startsWith('::ffff:127.')) {+ return true;+ }+ if (ip.startsWith('127.')) {+ return true;+ }+ if (ip.startsWith('10.')) {+ return true;+ }+ if (ip.startsWith('192.168.')) {+ return true;+ }if (ip.startsWith('172.')) {const parts = ip.split('.');if (parts.length >= 2) {@@ -723,7 +748,7 @@ function isPrivateIP(ip: string): boolean {export async function ipLookup(request: FastifyRequest<{ Querystring: { ip?: string } }>,- reply: FastifyReply,+ reply: FastifyReply) {const ipParam = request.query.ip;@@ -747,7 +772,7 @@ export async function ipLookup(const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;const ipv6Regex = /^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$/;- if (!ipv4Regex.test(ipToLookup) && !ipv6Regex.test(ipToLookup)) {+ if (!(ipv4Regex.test(ipToLookup) || ipv6Regex.test(ipToLookup))) {return reply.status(400).send({ error: 'Invalid IP address format' });}@@ -772,9 +797,7 @@ export async function ipLookup(request.log.error({ err: error }, 'IP lookup error');return reply.status(500).send({error:- error instanceof Error- ? error.message- : 'Failed to lookup IP address',+ error instanceof Error ? error.message : 'Failed to lookup IP address',});}}diff --git a/apps/api/src/controllers/track.controller.ts b/apps/api/src/controllers/track.controller.tsindex eed7d403e..7bfdb14f5 100644--- a/apps/api/src/controllers/track.controller.ts+++ b/apps/api/src/controllers/track.controller.ts@@ -1,5 +1,5 @@import { generateId } from '@openpanel/common';-import { generateDeviceId, parseUserAgent } from '@openpanel/common/server';+import { generateDeviceId, parseUserAgent } from '@openpanel/core';import {convertClickhouseDateToJs,getProfileById,diff --git a/apps/api/src/hooks/ip.hook.ts b/apps/api/src/hooks/ip.hook.tsindex fe544c89a..1424b5006 100644--- a/apps/api/src/hooks/ip.hook.ts+++ b/apps/api/src/hooks/ip.hook.ts@@ -1,4 +1,4 @@-import { getClientIpFromHeaders } from '@openpanel/common/server/get-client-ip';+import { getClientIpFromHeaders } from '@openpanel/core';import type { FastifyRequest } from 'fastify';export async function ipHook(request: FastifyRequest) {diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex a071d9b56..e55b5ae07 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -3,7 +3,7 @@ process.env.TZ = 'UTC';import './utils/observability';-import { rawStderrWrite } from '@openpanel/logger';+import { rawStderrWrite } from '@openpanel/core';import { assertKafkaConfigured } from '@openpanel/queue';import { getRedisPub } from '@openpanel/redis';import sourceMapSupport from 'source-map-support';@@ -48,7 +48,7 @@ const startServer = async () => {process.on('uncaughtException', (error) => {logger.fatal({ err: error }, 'Uncaught exception — exiting');rawStderrWrite(- `Uncaught exception — exiting: ${error?.stack ?? error}\n`,+ `Uncaught exception — exiting: ${error?.stack ?? error}\n`);// Flush pino, then hard-exit. unref() so the safety net doesn't// keep the loop alive on its own.@@ -59,7 +59,7 @@ const startServer = async () => {rawStderrWrite(`Unhandled rejection — exiting: ${reason instanceof Error ? reason.stack : String(reason)- }\n`,+ }\n`);setTimeout(() => process.exit(1), 1000).unref();});@@ -72,7 +72,7 @@ const startServer = async () => {} catch (error) {logger.warn({ err: error }, 'Failed to set redis notify-keyspace-events');logger.warn(- 'If you use a managed Redis service, you may need to set this manually.',+ 'If you use a managed Redis service, you may need to set this manually.');logger.warn('Otherwise some functions may not work as expected.');}diff --git a/apps/api/src/routes/insights.router.test.ts b/apps/api/src/routes/insights.router.test.tsindex b031e0a22..7d43319ce 100644--- a/apps/api/src/routes/insights.router.test.ts+++ b/apps/api/src/routes/insights.router.test.ts@@ -22,9 +22,8 @@ vi.mock('@openpanel/db', async (importOriginal) => {});// Password verification is always truthy in tests.-vi.mock('@openpanel/common/server', async (importOriginal) => {- const actual =- await importOriginal<typeof import('@openpanel/common/server')>();+vi.mock('@openpanel/core', async (importOriginal) => {+ const actual = await importOriginal<typeof import('@openpanel/core')>();return { ...actual, verifyPassword: vi.fn().mockResolvedValue(true) };});@@ -52,8 +51,12 @@ vi.mock('@openpanel/redis', async (importOriginal) => {import { ClientType, getClientByIdCached } from '@openpanel/db';import type { FastifyInstance } from 'fastify';+import {+ FIXTURE,+ TEST_ORG_ID,+ TEST_PROJECT_ID,+} from '../../../../test/global-setup';import { buildApp } from '../app';-import { FIXTURE, TEST_ORG_ID, TEST_PROJECT_ID } from '../../../../test/global-setup';// ─── Test client constants ────────────────────────────────────────────────────diff --git a/apps/api/src/utils/auth.ts b/apps/api/src/utils/auth.tsindex 70cb59862..3b1a6a74a 100644--- a/apps/api/src/utils/auth.ts+++ b/apps/api/src/utils/auth.ts@@ -1,4 +1,4 @@-import { verifyPassword } from '@openpanel/common/server';+import { verifyPassword } from '@openpanel/core';import type { IServiceClientWithProject } from '@openpanel/db';import { ClientType, getClientByIdCached } from '@openpanel/db';import { getCache } from '@openpanel/redis';diff --git a/apps/api/src/utils/ids.ts b/apps/api/src/utils/ids.tsindex 21611ac38..fe7ab12ff 100644--- a/apps/api/src/utils/ids.ts+++ b/apps/api/src/utils/ids.ts@@ -1,11 +1,11 @@import crypto from 'node:crypto';-import { generateDeviceId } from '@openpanel/common/server';+import { generateDeviceId } from '@openpanel/core';+import type { IClickhouseSession } from '@openpanel/db';import {convertClickhouseDateToJs,SESSION_TIMEOUT_MS,sessionBuffer,} from '@openpanel/db';-import type { IClickhouseSession } from '@openpanel/db';export async function getDeviceId({projectId,@@ -129,7 +129,7 @@ async function getInfoFromSession({projectId,deviceId: primary,eventMs: eventTimeMs,- graceMs: Math.min(5_000, Math.floor(SESSION_TIMEOUT_MS / 6)),+ graceMs: Math.min(5000, Math.floor(SESSION_TIMEOUT_MS / 6)),windowMs: SESSION_TIMEOUT_MS,}),};diff --git a/apps/api/src/utils/logger.ts b/apps/api/src/utils/logger.tsindex 99ee30c65..6a087835e 100644--- a/apps/api/src/utils/logger.ts+++ b/apps/api/src/utils/logger.ts@@ -1,3 +1,3 @@-import { createLogger } from '@openpanel/logger';+import { createLogger } from '@openpanel/core';export const logger = createLogger({ name: 'api' });diff --git a/apps/api/src/utils/observability.ts b/apps/api/src/utils/observability.tsindex 82a468596..c70be1333 100644--- a/apps/api/src/utils/observability.ts+++ b/apps/api/src/utils/observability.ts@@ -1,5 +1,5 @@import { init } from '@hyperdx/node-opentelemetry';-import { getServiceName, interceptProcessOutput } from '@openpanel/logger';+import { getServiceName, interceptProcessOutput } from '@openpanel/core';import { logger } from './logger';// Side-effect module — must stay the first import in index.ts.diff --git a/apps/api/src/utils/rate-limiter.ts b/apps/api/src/utils/rate-limiter.tsindex b30e96fb6..16dce8d30 100644--- a/apps/api/src/utils/rate-limiter.ts+++ b/apps/api/src/utils/rate-limiter.ts@@ -1,4 +1,4 @@-import { getTrustedIpFromHeaders } from '@openpanel/common/server/get-client-ip';+import { getTrustedIpFromHeaders } from '@openpanel/core';import { getRedisCache } from '@openpanel/redis';import type { FastifyInstance, FastifyRequest } from 'fastify';@@ -42,7 +42,7 @@ export async function activateRateLimiter<T extends FastifyRequest>({onExceeded: (req) => {const { ip, header } = getTrustedIpFromHeaders(req.headers,- req.socket?.remoteAddress,+ req.socket?.remoteAddress);req.log.warn({@@ -52,7 +52,7 @@ export async function activateRateLimiter<T extends FastifyRequest>({url: req.url,userAgent: req.headers['user-agent'],},- 'rate limit exceeded',+ 'rate limit exceeded');},});diff --git a/apps/api/src/utils/safe-fetch.ts b/apps/api/src/utils/safe-fetch.tsindex 8cb361103..5d2e5d722 100644--- a/apps/api/src/utils/safe-fetch.ts+++ b/apps/api/src/utils/safe-fetch.ts@@ -1,5 +1,5 @@/**- * The SSRF guard lives in `@openpanel/common/server/safe-fetch` so that every+ * The SSRF guard lives in `@openpanel/core` so that every* outbound-fetch site in the monorepo can reach it - the api controllers, the* worker's webhook delivery and the importer's remote-file reader alike. It* used to live here, which is why the importer never called it@@ -9,14 +9,14 @@* keep working.*/export {- BlockedUrlError,assertPublicHostname,assertPublicUrl,+ BlockedUrlError,createPinnedAgent,isBlockedIp,- safeFetch,- safeFetchStream,type SafeFetchOptions,type SafeFetchResult,type SafeFetchStreamResult,-} from '@openpanel/common/server/safe-fetch';+ safeFetch,+ safeFetchStream,+} from '@openpanel/core';diff --git a/apps/worker/package.json b/apps/worker/package.jsonindex e24a8a299..e73747ba5 100644--- a/apps/worker/package.json+++ b/apps/worker/package.json@@ -16,12 +16,12 @@"@hyperdx/node-opentelemetry": "catalog:","@openpanel/ai": "workspace:*","@openpanel/common": "workspace:*",+ "@openpanel/core": "workspace:*","@openpanel/db": "workspace:*","@openpanel/email": "workspace:*","@openpanel/importer": "workspace:*","@openpanel/integrations": "workspace:^","@openpanel/js-runtime": "workspace:*",- "@openpanel/logger": "workspace:*","@openpanel/payments": "workspace:*","@openpanel/queue": "workspace:*","@openpanel/redis": "workspace:*",diff --git a/apps/worker/src/boot-workers.ts b/apps/worker/src/boot-workers.tsindex 81eb876ba..d04aad402 100644--- a/apps/worker/src/boot-workers.ts+++ b/apps/worker/src/boot-workers.ts@@ -1,6 +1,6 @@import { performance } from 'node:perf_hooks';import { setTimeout as sleep } from 'node:timers/promises';-import { rawStderrWrite } from '@openpanel/logger';+import { rawStderrWrite } from '@openpanel/core';import {cohortComputeQueue,cronQueue,diff --git a/apps/worker/src/jobs/cron.flush-exports.ts b/apps/worker/src/jobs/cron.flush-exports.tsindex 77b6037ca..60f1a1ade 100644--- a/apps/worker/src/jobs/cron.flush-exports.ts+++ b/apps/worker/src/jobs/cron.flush-exports.ts@@ -1,4 +1,5 @@import { DateTime } from '@openpanel/common';+import { createLogger } from '@openpanel/core';import {ch,clickhouseEventToExportEvent,@@ -15,7 +16,6 @@ import {} from '@openpanel/db';import type { IObjectStoreAdapter } from '@openpanel/integrations/src/object-store';import { getServerIntegration } from '@openpanel/integrations/src/registry';-import { createLogger } from '@openpanel/logger';import type { CronQueuePayload } from '@openpanel/queue';import {type IGCSExportConfig,@@ -33,7 +33,10 @@ const logger = createLogger({ name: 'flush-exports' });const LAG_SECONDS = Number.parseInt(process.env.EXPORT_LAG_SECONDS || '60', 10);// Max rows per object/batch and max batches drained per (project, integration)// per run. A backlog drains over subsequent ticks rather than in one giant pass.-const BATCH_SIZE = Number.parseInt(process.env.EXPORT_BATCH_SIZE || '50000', 10);+const BATCH_SIZE = Number.parseInt(+ process.env.EXPORT_BATCH_SIZE || '50000',+ 10+);const MAX_BATCHES_PER_RUN = Number.parseInt(process.env.EXPORT_MAX_BATCHES_PER_RUN || '20',10@@ -263,7 +266,12 @@ async function loadCursor(// explicit operation (reset the watermark).const now = new Date();await db.exportWatermark.create({- data: { projectId, integrationId, lastInsertedAt: now, lastEventId: NIL_UUID },+ data: {+ projectId,+ integrationId,+ lastInsertedAt: now,+ lastEventId: NIL_UUID,+ },});return { insertedAt: formatCh(now), eventId: NIL_UUID };}diff --git a/apps/worker/src/jobs/cron.salt.ts b/apps/worker/src/jobs/cron.salt.tsindex 8b5b02e4f..16236db86 100644--- a/apps/worker/src/jobs/cron.salt.ts+++ b/apps/worker/src/jobs/cron.salt.ts@@ -1,4 +1,4 @@-import { generateSalt } from '@openpanel/common/server';+import { generateSalt } from '@openpanel/core';import { db, getSalts } from '@openpanel/db';async function generateNewSalt() {@@ -47,7 +47,7 @@ export async function salt() {} catch (error) {if (retryCount < ALLOWED_RETRIES) {await new Promise((resolve) =>- setTimeout(resolve, BASE_DELAY * 2 ** retryCount),+ setTimeout(resolve, BASE_DELAY * 2 ** retryCount));return generateNewSaltWithRetry(retryCount + 1);}diff --git a/apps/worker/src/jobs/events.incoming-event.ts b/apps/worker/src/jobs/events.incoming-event.tsindex 8b9471f57..cb0b9d338 100644--- a/apps/worker/src/jobs/events.incoming-event.ts+++ b/apps/worker/src/jobs/events.incoming-event.ts@@ -1,5 +1,9 @@import { getTime, isSameDomain, parsePath } from '@openpanel/common';-import { getReferrerWithQuery, parseReferrer } from '@openpanel/common/server';+import {+ getReferrerWithQuery,+ type ILogger,+ parseReferrer,+} from '@openpanel/core';import type {IServiceCreateEventPayload,IServiceCreateEventPayloadWithId,@@ -13,7 +17,6 @@ import {matchEvent,sessionBuffer,} from '@openpanel/db';-import type { ILogger } from '@openpanel/logger';import type { EventsQueuePayloadIncomingEvent } from '@openpanel/queue';import { anyPass, isEmpty, isNil, mergeDeepRight, omit, reject } from 'ramda';import { sessionEndsEnqueued, sessionsStarted } from '@/metrics';diff --git a/apps/worker/src/jobs/import.ts b/apps/worker/src/jobs/import.tsindex ac40a244a..a767c7e30 100644--- a/apps/worker/src/jobs/import.ts+++ b/apps/worker/src/jobs/import.ts@@ -1,3 +1,4 @@+import type { ILogger } from '@openpanel/core';import {backfillSessionsToProduction,cleanupSessionStartEndEvents,@@ -19,7 +20,6 @@ import {MixpanelProvider,UmamiProvider,} from '@openpanel/importer';-import type { ILogger } from '@openpanel/logger';import type { ImportQueuePayload } from '@openpanel/queue';import type { Job } from 'bullmq';import { logger } from '../utils/logger';@@ -369,7 +369,7 @@ export async function importJob(job: Job<ImportQueuePayload>) {} catch (markError) {jobLogger.error({ err: error, markError },- 'Failed to mark import as failed',+ 'Failed to mark import as failed');}diff --git a/apps/worker/src/utils/logger.ts b/apps/worker/src/utils/logger.tsindex 835996632..df9b72691 100644--- a/apps/worker/src/utils/logger.ts+++ b/apps/worker/src/utils/logger.ts@@ -1,3 +1,3 @@-import { createLogger } from '@openpanel/logger';+import { createLogger } from '@openpanel/core';export const logger = createLogger({ name: 'worker' });diff --git a/apps/worker/src/utils/observability.ts b/apps/worker/src/utils/observability.tsindex 627b13c82..bd06d6a56 100644--- a/apps/worker/src/utils/observability.ts+++ b/apps/worker/src/utils/observability.ts@@ -1,5 +1,5 @@import { init } from '@hyperdx/node-opentelemetry';-import { getServiceName, interceptProcessOutput } from '@openpanel/logger';+import { getServiceName, interceptProcessOutput } from '@openpanel/core';import { logger } from './logger';// Side-effect module — must stay the first import in index.ts.diff --git a/package.json b/package.jsonindex 9d4bf58b9..f06d872bb 100644--- a/package.json+++ b/package.json@@ -9,7 +9,7 @@"scripts": {"test": "vitest run","gen:bots": "pnpm -r --filter api gen:bots",- "gen:referrers": "pnpm -r --filter common gen:referrers",+ "gen:referrers": "pnpm -r --filter core gen:referrers","dock:up": "docker compose up -d","dock:down": "docker compose down","dock:ch": "docker compose exec -it op-ch clickhouse-client -d openpanel",diff --git a/packages/common/index.ts b/packages/common/index.tsindex 24dcb4095..329ab7223 100644--- a/packages/common/index.ts+++ b/packages/common/index.ts@@ -1,13 +1,12 @@export * from './src/date';-export * from './src/timezones';-export * from './src/object';-export * from './src/names';-export * from './src/string';-export * from './src/math';-export * from './src/slug';-export * from './src/url';-export * from './src/id';export * from './src/get-previous-metric';export * from './src/group-by-labels';+export * from './src/id';+export * from './src/math';+export * from './src/names';+export * from './src/object';+export * from './src/slug';+export * from './src/string';+export * from './src/timezones';export * from './src/try-catch';-export * from './server/get-client-ip';+export * from './src/url';diff --git a/packages/common/package.json b/packages/common/package.jsonindex ec0ce3c01..5501526e7 100644--- a/packages/common/package.json+++ b/packages/common/package.json@@ -4,29 +4,21 @@"type": "module","main": "index.ts","exports": {- ".": "./index.ts",- "./server": "./server/index.ts",- "./server/get-client-ip": "./server/get-client-ip.ts",- "./server/safe-fetch": "./server/safe-fetch.ts"+ ".": "./index.ts"},"scripts": {"test": "vitest",- "typecheck": "tsc --noEmit",- "gen:referrers": "jiti scripts/get-referrers.ts && biome format --write ./server/referrers/index.ts"+ "typecheck": "tsc --noEmit"},"dependencies": {"@openpanel/constants": "workspace:*","date-fns": "^3.3.1",- "ipaddr.js": "^2.5.0",- "lru-cache": "^11.2.4","luxon": "^3.7.2","mathjs": "^12.3.2","nanoid": "^5.1.6","ramda": "^0.29.1","slugify": "^1.6.6","superjson": "^1.13.3",- "ua-parser-js": "^2.0.6",- "undici": "^8.10.0","unique-names-generator": "^4.7.1"},"devDependencies": {@@ -35,7 +27,6 @@"@types/luxon": "^3.7.1","@types/node": "catalog:","@types/ramda": "^0.29.6",- "@types/ua-parser-js": "^0.7.39","prisma": "^5.1.1","typescript": "catalog:"}diff --git a/packages/common/server/id.ts b/packages/common/server/id.tsdeleted file mode 100644index 248c1d86e..000000000--- a/packages/common/server/id.ts+++ /dev/null@@ -1,5 +0,0 @@-import { nanoid } from 'nanoid';--export function generateSecureId(prefix: string) {- return `${prefix}_${nanoid(18)}`;-}diff --git a/packages/common/server/index.ts b/packages/common/server/index.tsdeleted file mode 100644index 9a2343cd1..000000000--- a/packages/common/server/index.ts+++ /dev/null@@ -1,7 +0,0 @@-export * from './crypto';-export * from './encryption';-export * from './profileId';-export * from './parser-user-agent';-export * from './parse-referrer';-export * from './id';-export * from './ssrf';diff --git a/packages/core/package.json b/packages/core/package.jsonindex cca02a875..e3abb0ae1 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -10,19 +10,27 @@},"scripts": {"typecheck": "tsc --noEmit",- "test": "bun test --isolate"+ "test": "bun test --isolate",+ "gen:referrers": "bun scripts/get-referrers.ts && biome format --write ./src/shared/referrers/index.ts"},"dependencies": {"@elysiajs/cors": "1.4.2","@elysiajs/openapi": "1.4.15",+ "@hyperdx/node-opentelemetry": "catalog:","@openpanel/auth": "workspace:^","@openpanel/common": "workspace:*","@trpc/server": "^11.17.0","bullmq": "6.3.3","elysia": "1.4.30",+ "ipaddr.js": "^2.5.0",+ "lru-cache": "^11.2.4","nanoid": "^5.1.6",+ "pino": "catalog:",+ "pino-pretty": "catalog:","prom-client": "^15.1.3","superjson": "^1.13.3",+ "ua-parser-js": "^2.0.6",+ "undici": "^8.10.0","zod": "catalog:"},"devDependencies": {diff --git a/packages/common/scripts/get-referrers.ts b/packages/core/scripts/get-referrers.tssimilarity index 98%rename from packages/common/scripts/get-referrers.tsrename to packages/core/scripts/get-referrers.tsindex eee3ff19f..208f33606 100644--- a/packages/common/scripts/get-referrers.ts+++ b/packages/core/scripts/get-referrers.ts@@ -129,7 +129,7 @@ async function main() {).then((res) => res.json());fs.writeFileSync(- path.resolve(__dirname, '../server/referrers/index.ts'),+ path.resolve(__dirname, '../src/shared/referrers/index.ts'),['// This file is generated by the script get-referrers.ts','',diff --git a/packages/logger/index.ts b/packages/core/src/clients/logger.tssimilarity index 89%rename from packages/logger/index.tsrename to packages/core/src/clients/logger.tsindex fc75ecf0d..2c9c8f5c2 100644--- a/packages/logger/index.ts+++ b/packages/core/src/clients/logger.ts@@ -1,7 +1,16 @@+// Concrete pino implementation. `../logger.ts` is the structural interface+// every module and service codes against; this file is what actually+// instantiates pino, and it is deliberately the only place in core that+// imports it (ADR-007 layout: "pino instantiated in apps/api" — apps/api,+// and the still-live V1 apps/worker, both build their named logger by+// calling `createLogger` from here rather than each owning a copy).+//+// Ported from @openpanel/logger, unchanged (dissolved into core — M4-003).+import * as HyperDX from '@hyperdx/node-opentelemetry';-import pino, { type Logger } from 'pino';+import pino, { type Logger as PinoLogger } from 'pino';-export type ILogger = Logger;+export type ILogger = PinoLogger;const logLevel = process.env.LOG_LEVEL ?? 'info';const silent = process.env.LOG_SILENT === 'true';@@ -10,8 +19,7 @@ const silent = process.env.LOG_SILENT === 'true';// - 'otlp': pino ships via the HyperDX transport (requires HYPERDX_API_KEY).// - 'stdout': pino writes JSON to stdout; an external collector ships it.const logExporter =- process.env.LOG_EXPORTER ??- (process.env.HYPERDX_API_KEY ? 'otlp' : 'stdout');+ process.env.LOG_EXPORTER ?? (process.env.HYPERDX_API_KEY ? 'otlp' : 'stdout');// Originals captured before interceptProcessOutput wraps the streams. Code// that must bypass capture (e.g. crash handlers mirroring fatals to stderr)@@ -219,7 +227,9 @@ export function interceptProcessOutput(logger: ILogger): void {return original(chunk as never, encodingOrCallback as never, callback);}const cb =- typeof encodingOrCallback === 'function' ? encodingOrCallback : callback;+ typeof encodingOrCallback === 'function'+ ? encodingOrCallback+ : callback;if (typeof cb === 'function') {process.nextTick(cb);}diff --git a/packages/core/src/http/hooks.ts b/packages/core/src/http/hooks.tsindex 6a19f1f15..8c320f638 100644--- a/packages/core/src/http/hooks.ts+++ b/packages/core/src/http/hooks.ts@@ -12,9 +12,9 @@// reads the request logger off `ctx` and would otherwise import this file's// consumer.-import { getClientIpFromHeaders } from '@openpanel/common/server/get-client-ip';import { Elysia } from 'elysia';import { REQUEST_ID_HEADER, REQUEST_ID_LENGTH } from '../logger';+import { getClientIpFromHeaders } from '../shared/get-client-ip';import { generateId } from '../shared/id';// ADR-018 R2: an inbound request-id is honoured but never trusted verbatim —diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex a62343b14..d70984403 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -6,6 +6,18 @@// once and mount the three route surfaces plus the tRPC router over it; a// service, a client or a buffer is not reachable from here by design.+// The concrete pino implementation (dissolved from @openpanel/logger,+// M4-003). `./logger` above is the structural interface every module codes+// against; this is what apps/api, and the still-live apps/worker, call to+// build one.+export type { ILogger } from './clients/logger';+export {+ createLogger,+ getServiceName,+ interceptProcessOutput,+ rawStderrWrite,+ rawStdoutWrite,+} from './clients/logger';export type {AppDeps,Ctx,@@ -71,3 +83,48 @@ export type {ProjectAccessLike,} from './shared/access';export { createAccessChecks } from './shared/access';+// Dissolved from @openpanel/common/server (M4-003) — a still-live V1 package+// (db, importer, integrations, mcp, queue, apps/worker) reaches these the+// same way apps/api and core itself do, until its own module lands.+export {+ createHash,+ generateSalt,+ hashPassword,+ verifyPassword,+} from './shared/crypto';+export {+ decrypt,+ decryptCredential,+ encrypt,+ encryptCredential,+ isEncrypted,+} from './shared/encryption';+export {+ DEFAULT_IP_HEADER_ORDER,+ getClientIpFromHeaders,+ getTrustedIpFromHeaders,+ TRUSTED_IP_HEADER_ORDER,+} from './shared/get-client-ip';+export { generateId, generateSecureId, shortId } from './shared/id';+export { getReferrerWithQuery, parseReferrer } from './shared/parse-referrer';+export type {+ UserAgentInfo,+ UserAgentResult,+} from './shared/parser-user-agent';+export { getDevice, parseUserAgent } from './shared/parser-user-agent';+export { generateDeviceId } from './shared/profileId';+export type {+ SafeFetchOptions,+ SafeFetchResult,+ SafeFetchStreamResult,+} from './shared/safe-fetch';+export {+ assertPublicHostname,+ assertPublicUrl,+ BlockedUrlError,+ createPinnedAgent,+ isBlockedIp,+ safeFetch,+ safeFetchStream,+} from './shared/safe-fetch';+export { assertSafeUrl, createPinnedLookup } from './shared/ssrf';diff --git a/packages/core/src/rpc/handler.ts b/packages/core/src/rpc/handler.tsindex 8939e67fc..eeb57e5ef 100644--- a/packages/core/src/rpc/handler.ts+++ b/packages/core/src/rpc/handler.ts@@ -7,11 +7,11 @@// logger. The messages, the levels, the payload keys and the `organization.list`// drop are unchanged.-import { getTrustedIpFromHeaders } from '@openpanel/common/server/get-client-ip';import type { AnyRouter, TRPCError } from '@trpc/server';import { fetchRequestHandler } from '@trpc/server/adapters/fetch';import type { HttpCtx } from '../context';import type { Logger } from '../logger';+import { getTrustedIpFromHeaders } from '../shared/get-client-ip';import {makeTrpcContext,type TrpcContext,diff --git a/packages/common/server/crypto.ts b/packages/core/src/shared/crypto.tssimilarity index 90%rename from packages/common/server/crypto.tsrename to packages/core/src/shared/crypto.tsindex d8505b144..07b6aa295 100644--- a/packages/common/server/crypto.ts+++ b/packages/core/src/shared/crypto.ts@@ -16,13 +16,15 @@ export function generateSalt() {*/export async function hashPassword(password: string,- keyLength = 32,+ keyLength = 32): Promise<string> {return new Promise((resolve, reject) => {// generate random 16 bytes long salt - recommended by NodeJS Docsconst salt = generateSalt();scrypt(password, salt, keyLength, (err, derivedKey) => {- if (err) reject(err);+ if (err) {+ reject(err);+ }// derivedKey is of type Bufferresolve(`${salt}.${derivedKey.toString('hex')}`);});@@ -38,7 +40,7 @@ export async function hashPassword(export async function verifyPassword(password: string,hash: string,- keyLength = 32,+ keyLength = 32): Promise<boolean> {return new Promise((resolve, reject) => {const [salt, hashKey] = hash.split('.');@@ -50,10 +52,7 @@ export async function verifyPassword(}// compare the new supplied password with the hashed password using timeSafeEqualresolve(- timingSafeEqual(- new Uint8Array(hashKeyBuff),- new Uint8Array(derivedKey),- ),+ timingSafeEqual(new Uint8Array(hashKeyBuff), new Uint8Array(derivedKey)));});});diff --git a/packages/common/server/encryption.test.ts b/packages/core/src/shared/encryption.test.tssimilarity index 96%rename from packages/common/server/encryption.test.tsrename to packages/core/src/shared/encryption.test.tsindex fc64724b5..665784e14 100644--- a/packages/common/server/encryption.test.ts+++ b/packages/core/src/shared/encryption.test.ts@@ -1,4 +1,4 @@-import { beforeAll, describe, expect, it } from 'vitest';+import { beforeAll, describe, expect, it } from 'bun:test';import {decrypt,decryptCredential,diff --git a/packages/common/server/encryption.ts b/packages/core/src/shared/encryption.tssimilarity index 97%rename from packages/common/server/encryption.tsrename to packages/core/src/shared/encryption.tsindex 223a32c0d..fe3140b46 100644--- a/packages/common/server/encryption.ts+++ b/packages/core/src/shared/encryption.ts@@ -20,7 +20,7 @@ function getEncryptionKey(): Buffer {if (keyHex.length !== 64) {throw new Error(- 'ENCRYPTION_KEY must be 32 bytes (64 hex characters). Generate with: openssl rand -hex 32',+ 'ENCRYPTION_KEY must be 32 bytes (64 hex characters). Generate with: openssl rand -hex 32');}@@ -57,7 +57,7 @@ export function decrypt(ciphertext: string): string {// mid-way through a multi-byte UTF-8 sequence and a per-chunk toString would// turn the split character into replacement chars.return Buffer.concat([decipher.update(encrypted), decipher.final()]).toString(- 'utf8',+ 'utf8');}@@ -115,7 +115,7 @@ export function decryptCredential(ciphertext: string): string {// Remove prefix and decode base64const combined = Buffer.from(ciphertext.slice(ENCRYPTION_PREFIX.length),- 'base64',+ 'base64');if (combined.length < IV_LENGTH + AUTH_TAG_LENGTH) {@@ -127,7 +127,7 @@ export function decryptCredential(ciphertext: string): string {const authTag = combined.subarray(combined.length - AUTH_TAG_LENGTH);const encryptedData = combined.subarray(IV_LENGTH,- combined.length - AUTH_TAG_LENGTH,+ combined.length - AUTH_TAG_LENGTH);const decipher = createDecipheriv(ALGORITHM, key, iv, {diff --git a/packages/common/server/get-client-ip.test.ts b/packages/core/src/shared/get-client-ip.test.tssimilarity index 95%rename from packages/common/server/get-client-ip.test.tsrename to packages/core/src/shared/get-client-ip.test.tsindex 4485de906..ef9972a9f 100644--- a/packages/common/server/get-client-ip.test.ts+++ b/packages/core/src/shared/get-client-ip.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from 'vitest';+import { describe, expect, it } from 'bun:test';import {getClientIpFromHeaders,getTrustedIpFromHeaders,@@ -30,13 +30,13 @@ describe('getTrustedIpFromHeaders', () => {'openpanel-client-ip': '9.9.9.9','x-client-ip': '7.7.7.7','true-client-ip': '6.6.6.6',- }).ip,+ }).ip).toBe('');});it('takes the last x-forwarded-for entry, the one the proxy appended', () => {expect(- getTrustedIpFromHeaders({ 'x-forwarded-for': '8.8.8.8, 1.2.3.4' }),+ getTrustedIpFromHeaders({ 'x-forwarded-for': '8.8.8.8, 1.2.3.4' })).toEqual({ ip: '1.2.3.4', header: 'x-forwarded-for' });});diff --git a/packages/common/server/get-client-ip.ts b/packages/core/src/shared/get-client-ip.tssimilarity index 96%rename from packages/common/server/get-client-ip.tsrename to packages/core/src/shared/get-client-ip.tsindex 8bebbe60c..86504a463 100644--- a/packages/common/server/get-client-ip.ts+++ b/packages/core/src/shared/get-client-ip.ts@@ -95,7 +95,7 @@ function isValidIp(ip: string): boolean {export function getClientIpFromHeaders(headers: Record<string, string | string[] | undefined> | Headers,- overrideHeaderName?: string,+ overrideHeaderName?: string): {ip: string;header: string;@@ -121,7 +121,9 @@ export function getClientIpFromHeaders(}}- if (!value) continue;+ if (!value) {+ continue;+ }// Handle x-forwarded-for (comma separated)if (headerName === 'x-forwarded-for') {@@ -178,7 +180,7 @@ export const TRUSTED_IP_HEADER_ORDER = [*/export function getTrustedIpFromHeaders(headers: Record<string, string | string[] | undefined> | Headers,- socketIp?: string,+ socketIp?: string): { ip: string; header: string } {const headerOrder = process.env.TRUSTED_IP_HEADER_ORDER? process.env.TRUSTED_IP_HEADER_ORDER.split(',').map((h) => h.trim())@@ -191,10 +193,13 @@ export function getTrustedIpFromHeaders(value = headers.get(headerName);} else {const headerValue = headers[headerName];- value = (Array.isArray(headerValue) ? headerValue[0] : headerValue) || null;+ value =+ (Array.isArray(headerValue) ? headerValue[0] : headerValue) || null;}- if (!value) continue;+ if (!value) {+ continue;+ }// A proxy chain appends as it goes, so the *last* entry is the one written// by the hop closest to us - the only entry a client cannot forge. This isdiff --git a/packages/core/src/shared/id.ts b/packages/core/src/shared/id.tsindex d27ed73ff..ed47a5515 100644--- a/packages/core/src/shared/id.ts+++ b/packages/core/src/shared/id.ts@@ -1,9 +1,15 @@// Ported from @openpanel/common's id.ts, unchanged. nanoid/non-secure is// deliberate: these are correlation/display ids, not secrets.++// Ported from @openpanel/common/server's id.ts (M4-003). Regular `nanoid`,+// not the non-secure variant above: these ids back password-reset tokens and+// invite links, so they must stay cryptographically random.+import { nanoid as secureNanoid } from 'nanoid';import { nanoid } from 'nanoid/non-secure';const SHORT_ID_LENGTH = 4;const DEFAULT_ID_LENGTH = 8;+const SECURE_ID_LENGTH = 18;export function shortId(): string {return nanoid(SHORT_ID_LENGTH);@@ -13,3 +19,7 @@ export function generateId(prefix?: string, length?: number): string {const id = nanoid(length ?? DEFAULT_ID_LENGTH);return prefix ? `${prefix}_${id}` : id;}++export function generateSecureId(prefix: string): string {+ return `${prefix}_${secureNanoid(SECURE_ID_LENGTH)}`;+}diff --git a/packages/common/server/parse-referrer.test.ts b/packages/core/src/shared/parse-referrer.test.tssimilarity index 97%rename from packages/common/server/parse-referrer.test.tsrename to packages/core/src/shared/parse-referrer.test.tsindex 009118b63..f3e7b1768 100644--- a/packages/common/server/parse-referrer.test.ts+++ b/packages/core/src/shared/parse-referrer.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from 'vitest';+import { describe, expect, it } from 'bun:test';import { getReferrerWithQuery, parseReferrer } from './parse-referrer';describe('parseReferrer', () => {@@ -107,7 +107,7 @@ describe('getReferrerWithQuery', () => {utm_source: 'google',ref: 'facebook',utm_referrer: 'twitter',- }),+ })).toEqual({name: 'Google',type: 'search',diff --git a/packages/common/server/parse-referrer.ts b/packages/core/src/shared/parse-referrer.tssimilarity index 86%rename from packages/common/server/parse-referrer.tsrename to packages/core/src/shared/parse-referrer.tsindex 3fe35c5ee..da052e14a 100644--- a/packages/common/server/parse-referrer.ts+++ b/packages/core/src/shared/parse-referrer.ts@@ -1,5 +1,4 @@-import { stripTrailingSlash } from '../src/string';-+import { stripTrailingSlash } from '@openpanel/common';import referrers from './referrers';function getHostname(url: string | undefined) {@@ -26,7 +25,7 @@ export function parseReferrer(url: string | undefined) {}export function getReferrerWithQuery(- query: Record<string, string> | undefined,+ query: Record<string, string> | undefined) {if (!query) {return null;@@ -47,7 +46,7 @@ export function getReferrerWithQuery(referrers[source] ||referrers[`${source}.com`] ||Object.values(referrers).find(- (referrer) => referrer.name.toLowerCase() === source,+ (referrer) => referrer.name.toLowerCase() === source);if (match) {diff --git a/packages/common/server/parser-user-agent.test.ts b/packages/core/src/shared/parser-user-agent.test.tssimilarity index 98%rename from packages/common/server/parser-user-agent.test.tsrename to packages/core/src/shared/parser-user-agent.test.tsindex 917f8566b..a5ab6f398 100644--- a/packages/common/server/parser-user-agent.test.ts+++ b/packages/core/src/shared/parser-user-agent.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from 'vitest';+import { describe, expect, it } from 'bun:test';import { getDevice, parseUserAgent } from './parser-user-agent';describe('parseUserAgent', () => {@@ -12,7 +12,7 @@ describe('parseUserAgent', () => {browserVersion: '',brand: '',model: '',- };+ } as const;expect(parseUserAgent(null)).toEqual(serverUa);expect(parseUserAgent(undefined)).toEqual(serverUa);@@ -97,7 +97,7 @@ describe('parseUserAgent', () => {browserVersion: '',brand: '',model: '',- };+ } as const;serverUas.forEach((ua) => {expect(parseUserAgent(ua)).toEqual(expectedResult);@@ -201,7 +201,7 @@ describe('getDevice', () => {describe('parseUserAgent - brand detection', () => {it('should detect Xiaomi brand from Manufacturer field', () => {const result = parseUserAgent(- 'App/1.0 (Android 12; Model=POCO X5; Manufacturer=Xiaomi)',+ 'App/1.0 (Android 12; Model=POCO X5; Manufacturer=Xiaomi)');expect(result.brand).toBe('Xiaomi');expect(result.model).toBe('POCO X5');@@ -210,7 +210,7 @@ describe('parseUserAgent - brand detection', () => {it('should detect Samsung brand from model name', () => {const result = parseUserAgent(- 'App/1.0 (Android 13; Model=Galaxy S23 Ultra)',+ 'App/1.0 (Android 13; Model=Galaxy S23 Ultra)');expect(result.brand).toBe('Samsung');expect(result.model).toBe('Galaxy S23 Ultra');@@ -228,7 +228,7 @@ describe('parseUserAgent - brand detection', () => {it('should detect OnePlus', () => {const result = parseUserAgent(- 'App/1.0 (Android 13; Model=OnePlus 11; Manufacturer=OnePlus)',+ 'App/1.0 (Android 13; Model=OnePlus 11; Manufacturer=OnePlus)');expect(result.brand).toBe('OnePlus');expect(result.model).toBe('OnePlus 11');@@ -239,7 +239,7 @@ describe('parseUserAgent - brand detection', () => {it('should detect Huawei', () => {const result = parseUserAgent(- 'App/1.0 (Android 12; Model=P60 Pro; Manufacturer=Huawei)',+ 'App/1.0 (Android 12; Model=P60 Pro; Manufacturer=Huawei)');expect(result.brand).toBe('Huawei');expect(result.model).toBe('P60 Pro');diff --git a/packages/common/server/parser-user-agent.ts b/packages/core/src/shared/parser-user-agent.tssimilarity index 97%rename from packages/common/server/parser-user-agent.tsrename to packages/core/src/shared/parser-user-agent.tsindex c859bccb2..7b2671f93 100644--- a/packages/common/server/parser-user-agent.ts+++ b/packages/core/src/shared/parser-user-agent.ts@@ -14,9 +14,9 @@ const parsedServerUa = {// Pre-compile all regex patterns for better performanceconst IPHONE_MODEL_REGEX = /(iPhone|iPad)\s*([0-9,]+)/i;-const IOS_MODEL_REGEX = /(iOS)\s*([0-9\.]+)/i;+const IOS_MODEL_REGEX = /(iOS)\s*([0-9.]+)/i;const IPAD_OS_VERSION_REGEX = /iPadOS\s*([0-9_]+)/i;-const SINGLE_NAME_VERSION_REGEX = /^[^\/]+\/[\d.]+$/;+const SINGLE_NAME_VERSION_REGEX = /^[^/]+\/[\d.]+$/;// App-style UA patterns (e.g., "Model=Redmi Note 8 Pro; Manufacturer=Xiaomi")const APP_MODEL_REGEX = /Model=([^;)]+)/i;@@ -150,7 +150,9 @@ function detectBrand(ua: string, model?: string): string | undefined {// Check if a model name indicates a phone (not tablet)function isKnownPhoneModel(model?: string): boolean {- if (!model) return false;+ if (!model) {+ return false;+ }return KNOWN_PHONE_PATTERNS.some((pattern) => pattern.test(model));}@@ -166,7 +168,7 @@ const parse = (ua: string): UAParser.IResult => {// Some user agents are not detected correctly by ua-parser-js// Doing some extra checks for ios- if (!res.device.model && !res.os.name) {+ if (!(res.device.model || res.os.name)) {const iphone = isIphone(ua);if (iphone) {const result = {@@ -213,8 +215,8 @@ const parse = (ua: string): UAParser.IResult => {...res,device: {...res.device,- model: model,- vendor: vendor,+ model,+ vendor,},};}@@ -242,9 +244,11 @@ export type UserAgentInfo = ReturnType<typeof parseUserAgent>;export type UserAgentResult = ReturnType<typeof parseUserAgent>;export function parseUserAgent(ua?: string | null,- overrides?: Record<string, unknown>,+ overrides?: Record<string, unknown>) {- if (!ua) return parsedServerUa;+ if (!ua) {+ return parsedServerUa;+ }const res = parse(ua);if (isServer(res)) {diff --git a/packages/common/server/profileId.ts b/packages/core/src/shared/profileId.tssimilarity index 100%rename from packages/common/server/profileId.tsrename to packages/core/src/shared/profileId.tsdiff --git a/packages/common/server/referrers/index.ts b/packages/core/src/shared/referrers/index.tssimilarity index 100%rename from packages/common/server/referrers/index.tsrename to packages/core/src/shared/referrers/index.tsdiff --git a/packages/common/server/referrers/referrers.readme.md b/packages/core/src/shared/referrers/referrers.readme.mdsimilarity index 100%rename from packages/common/server/referrers/referrers.readme.mdrename to packages/core/src/shared/referrers/referrers.readme.mddiff --git a/packages/common/server/safe-fetch.test.ts b/packages/core/src/shared/safe-fetch.test.tssimilarity index 93%rename from packages/common/server/safe-fetch.test.tsrename to packages/core/src/shared/safe-fetch.test.tsindex 19d774736..2ca97cf65 100644--- a/packages/common/server/safe-fetch.test.ts+++ b/packages/core/src/shared/safe-fetch.test.ts@@ -1,6 +1,6 @@+import { afterAll, beforeAll, describe, expect, it } from 'bun:test';import http from 'node:http';import type { AddressInfo } from 'node:net';-import { afterAll, beforeAll, describe, expect, it } from 'vitest';import {BlockedUrlError,isBlockedIp,@@ -59,7 +59,7 @@ describe('safeFetch', () => {res.end('INTERNAL-SECRET');});await new Promise<void>((resolve) =>- secretServer.listen(0, '127.0.0.1', resolve),+ secretServer.listen(0, '127.0.0.1', resolve));secretPort = (secretServer.address() as AddressInfo).port;@@ -70,7 +70,7 @@ describe('safeFetch', () => {res.end();});await new Promise<void>((resolve) =>- redirectServer.listen(0, '127.0.0.1', resolve),+ redirectServer.listen(0, '127.0.0.1', resolve));redirectPort = (redirectServer.address() as AddressInfo).port;});@@ -84,7 +84,7 @@ describe('safeFetch', () => {await expect(safeFetch(`http://127.0.0.1:${secretPort}/secret.png`, {timeoutMs: 3000,- }),+ })).rejects.toBeInstanceOf(BlockedUrlError);});@@ -92,7 +92,7 @@ describe('safeFetch', () => {await expect(safeFetch(`http://localhost:${secretPort}/secret.png`, {timeoutMs: 3000,- }),+ })).rejects.toBeInstanceOf(BlockedUrlError);});@@ -100,7 +100,7 @@ describe('safeFetch', () => {// The reachability gate on the caller side only inspects the first URL,// so the redirect hop has to be re-validated or the guard is bypassable.await expect(- safeFetch(`http://127.0.0.1:${redirectPort}/x.png`, { timeoutMs: 3000 }),+ safeFetch(`http://127.0.0.1:${redirectPort}/x.png`, { timeoutMs: 3000 })).rejects.toBeInstanceOf(BlockedUrlError);});@@ -108,13 +108,13 @@ describe('safeFetch', () => {await expect(safeFetch('http://169.254.169.254/latest/meta-data/', {timeoutMs: 3000,- }),+ })).rejects.toBeInstanceOf(BlockedUrlError);});it('refuses non-http schemes', async () => {await expect(safeFetch('file:///etc/passwd')).rejects.toBeInstanceOf(- BlockedUrlError,+ BlockedUrlError);});});@@ -131,7 +131,7 @@ describe('safeFetchStream', () => {res.end('INTERNAL-SECRET');});await new Promise<void>((resolve) =>- secretServer.listen(0, '127.0.0.1', resolve),+ secretServer.listen(0, '127.0.0.1', resolve));secretPort = (secretServer.address() as AddressInfo).port;@@ -142,7 +142,7 @@ describe('safeFetchStream', () => {res.end();});await new Promise<void>((resolve) =>- redirectServer.listen(0, '127.0.0.1', resolve),+ redirectServer.listen(0, '127.0.0.1', resolve));redirectPort = (redirectServer.address() as AddressInfo).port;});@@ -159,7 +159,7 @@ describe('safeFetchStream', () => {await expect(safeFetchStream(`http://127.0.0.1:${secretPort}/export.csv`, {timeoutMs: 3000,- }),+ })).rejects.toBeInstanceOf(BlockedUrlError);});@@ -167,7 +167,7 @@ describe('safeFetchStream', () => {await expect(safeFetchStream(`http://localhost:${secretPort}/export.csv`, {timeoutMs: 3000,- }),+ })).rejects.toBeInstanceOf(BlockedUrlError);});@@ -175,7 +175,7 @@ describe('safeFetchStream', () => {await expect(safeFetchStream(`http://127.0.0.1:${redirectPort}/export.csv`, {timeoutMs: 3000,- }),+ })).rejects.toBeInstanceOf(BlockedUrlError);});@@ -183,13 +183,13 @@ describe('safeFetchStream', () => {await expect(safeFetchStream('http://169.254.169.254/latest/meta-data/', {timeoutMs: 3000,- }),+ })).rejects.toBeInstanceOf(BlockedUrlError);});it('refuses non-http schemes', async () => {await expect(safeFetchStream('file:///etc/passwd')).rejects.toBeInstanceOf(- BlockedUrlError,+ BlockedUrlError);});});diff --git a/packages/common/server/safe-fetch.ts b/packages/core/src/shared/safe-fetch.tssimilarity index 96%rename from packages/common/server/safe-fetch.tsrename to packages/core/src/shared/safe-fetch.tsindex 3de76cebc..9373ea569 100644--- a/packages/common/server/safe-fetch.ts+++ b/packages/core/src/shared/safe-fetch.ts@@ -103,7 +103,7 @@ async function resolvePublicAddresses(hostname: string): Promise<string[]> {for (const { address } of addresses) {if (isBlockedIp(address)) {throw new BlockedUrlError(- `Refusing to connect to ${host} (resolves to a non-public address)`,+ `Refusing to connect to ${host} (resolves to a non-public address)`);}}@@ -125,7 +125,7 @@ export async function assertPublicUrl(url: URL): Promise<string[]> {/** Validate a bare hostname (no URL available), e.g. before a TLS probe. */export async function assertPublicHostname(- hostname: string,+ hostname: string): Promise<string[]> {return resolvePublicAddresses(hostname);}@@ -148,8 +148,8 @@ export function createPinnedLookup(address: string) {callback: (err: null,addressOrList: string | { address: string; family: number }[],- family?: number,- ) => void,+ family?: number+ ) => void) => {if (options.all) {callback(null, [{ address, family }]);@@ -208,7 +208,7 @@ const REDIRECT_STATUS = new Set([301, 302, 303, 307, 308]);async function readBodyWithLimit(body: ReadableStream<Uint8Array> | null,- maxBytes: number,+ maxBytes: number): Promise<Buffer> {if (!body) {return Buffer.alloc(0);@@ -256,7 +256,7 @@ export interface SafeFetchStreamResult {function combineSignals(timeoutSignal: AbortSignal,- callerSignal?: AbortSignal,+ callerSignal?: AbortSignal): AbortSignal {return callerSignal? AbortSignal.any([timeoutSignal, callerSignal])@@ -273,7 +273,7 @@ function combineSignals(async function walkToFinalResponse(input: string | URL,options: SafeFetchOptions,- signal: AbortSignal,+ signal: AbortSignal): Promise<{response: Awaited<ReturnType<typeof undiciFetch>>;agent: Agent;@@ -345,26 +345,26 @@ async function walkToFinalResponse(*/export async function safeFetch(input: string | URL,- options: SafeFetchOptions = {},+ options: SafeFetchOptions = {}): Promise<SafeFetchResult> {const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;const controller = new AbortController();const timeout = setTimeout(() => controller.abort(),- options.timeoutMs ?? DEFAULT_TIMEOUT_MS,+ options.timeoutMs ?? DEFAULT_TIMEOUT_MS);try {const { response, agent, finalUrl, chain } = await walkToFinalResponse(input,options,- combineSignals(controller.signal, options.signal),+ combineSignals(controller.signal, options.signal));try {const body = await readBodyWithLimit(response.body as ReadableStream<Uint8Array> | null,- maxBytes,+ maxBytes);return {status: response.status,@@ -394,19 +394,19 @@ export async function safeFetch(*/export async function safeFetchStream(input: string | URL,- options: SafeFetchOptions = {},+ options: SafeFetchOptions = {}): Promise<SafeFetchStreamResult> {const controller = new AbortController();const timeout = setTimeout(() => controller.abort(),- options.timeoutMs ?? DEFAULT_TIMEOUT_MS,+ options.timeoutMs ?? DEFAULT_TIMEOUT_MS);try {const { response, agent, finalUrl, chain } = await walkToFinalResponse(input,options,- combineSignals(controller.signal, options.signal),+ combineSignals(controller.signal, options.signal));let closed = false;diff --git a/packages/common/server/ssrf.test.ts b/packages/core/src/shared/ssrf.test.tssimilarity index 93%rename from packages/common/server/ssrf.test.tsrename to packages/core/src/shared/ssrf.test.tsindex c34c69658..8a9682ae8 100644--- a/packages/common/server/ssrf.test.ts+++ b/packages/core/src/shared/ssrf.test.ts@@ -1,4 +1,4 @@-import { afterEach, describe, expect, it, vi } from 'vitest';+import { afterEach, describe, expect, it, mock } from 'bun:test';import { assertSafeUrl, createPinnedLookup } from './ssrf';describe('assertSafeUrl', () => {@@ -28,7 +28,7 @@ describe('assertSafeUrl', () => {await expect(assertSafeUrl('http://127.0.0.1/x')).rejects.toThrow();await expect(assertSafeUrl('http://10.0.0.5/x')).rejects.toThrow();await expect(- assertSafeUrl('http://169.254.169.254/latest/meta-data/'),+ assertSafeUrl('http://169.254.169.254/latest/meta-data/')).rejects.toThrow();await expect(assertSafeUrl('http://[::1]/x')).rejects.toThrow();});@@ -63,11 +63,11 @@ describe('createPinnedLookup', () => {it('resolves every hostname to the pinned address', () => {const lookup = createPinnedLookup('93.184.216.34');- const single = vi.fn();+ const single = mock();lookup('anything.example', {}, single);expect(single).toHaveBeenCalledWith(null, '93.184.216.34', 4);- const all = vi.fn();+ const all = mock();lookup('anything.example', { all: true }, all);expect(all).toHaveBeenCalledWith(null, [{ address: '93.184.216.34', family: 4 },@@ -76,12 +76,12 @@ describe('createPinnedLookup', () => {it('reports IPv6 addresses with the right family', () => {const lookup = createPinnedLookup('2606:2800:220:1:248:1893:25c8:1946');- const cb = vi.fn();+ const cb = mock();lookup('anything.example', {}, cb);expect(cb).toHaveBeenCalledWith(null,'2606:2800:220:1:248:1893:25c8:1946',- 6,+ 6);});});diff --git a/packages/common/server/ssrf.ts b/packages/core/src/shared/ssrf.tssimilarity index 93%rename from packages/common/server/ssrf.tsrename to packages/core/src/shared/ssrf.tsindex bddd7a519..fe974b9e1 100644--- a/packages/common/server/ssrf.ts+++ b/packages/core/src/shared/ssrf.ts@@ -22,10 +22,7 @@ export { createPinnedLookup } from './safe-fetch';export async function assertSafeUrl(rawUrl: string): Promise<string[] | null> {// Compare explicitly: bare truthiness would treat SELF_HOSTED="false" as// self-hosted and silently drop the guard on the cloud.- if (- process.env.SELF_HOSTED === 'true' ||- process.env.SELF_HOSTED === '1'- ) {+ if (process.env.SELF_HOSTED === 'true' || process.env.SELF_HOSTED === '1') {return null;}diff --git a/packages/db/package.json b/packages/db/package.jsonindex 1e1da401e..910c8accd 100644--- a/packages/db/package.json+++ b/packages/db/package.json@@ -18,8 +18,8 @@"@clickhouse/client": "^1.18.5","@openpanel/common": "workspace:*","@openpanel/constants": "workspace:*",+ "@openpanel/core": "workspace:*","@openpanel/json": "workspace:*",- "@openpanel/logger": "workspace:*","@openpanel/payments": "workspace:*","@openpanel/queue": "workspace:^","@openpanel/redis": "workspace:*",diff --git a/packages/db/src/buffers/base-buffer.ts b/packages/db/src/buffers/base-buffer.tsindex 5c279788a..fc8cd4ec5 100644--- a/packages/db/src/buffers/base-buffer.ts+++ b/packages/db/src/buffers/base-buffer.ts@@ -1,7 +1,6 @@import { Readable } from 'node:stream';import type { ClickHouseSettings } from '@clickhouse/client';-import { generateSecureId } from '@openpanel/common/server';-import { createLogger, type ILogger } from '@openpanel/logger';+import { createLogger, generateSecureId, type ILogger } from '@openpanel/core';import { cronQueue } from '@openpanel/queue';import { getRedisCache } from '@openpanel/redis';diff --git a/packages/db/src/clickhouse/client.ts b/packages/db/src/clickhouse/client.tsindex 349352e60..56f0fca40 100644--- a/packages/db/src/clickhouse/client.ts+++ b/packages/db/src/clickhouse/client.ts@@ -5,7 +5,7 @@ import type {} from '@clickhouse/client';import { ClickHouseLogLevel, createClient } from '@clickhouse/client';import type { NodeClickHouseClientConfigOptions } from '@clickhouse/client/dist/config';-import { createLogger } from '@openpanel/logger';+import { createLogger } from '@openpanel/core';import type { IInterval } from '@openpanel/validation';import sqlstring from 'sqlstring';import { RoundRobinPicker, withRoundRobinRetry } from './round-robin';diff --git a/packages/db/src/clickhouse/round-robin.ts b/packages/db/src/clickhouse/round-robin.tsindex 69d9af323..72f5de752 100644--- a/packages/db/src/clickhouse/round-robin.ts+++ b/packages/db/src/clickhouse/round-robin.ts@@ -1,5 +1,5 @@import type { ClickHouseClient } from '@clickhouse/client';-import type { ILogger } from '@openpanel/logger';+import type { ILogger } from '@openpanel/core';/*** Round-robin selection across multiple ClickHouse clients with simple@@ -151,8 +151,12 @@ export function classifyError(err: unknown): ErrorClass {// ── Explicit error.code wins ──────────────────────────────────────────if (typeof e.code === 'string') {- if (NODE_DOWN_CODES.has(e.code)) return 'node-down';- if (TRANSIENT_CODES.has(e.code)) return 'transient';+ if (NODE_DOWN_CODES.has(e.code)) {+ return 'node-down';+ }+ if (TRANSIENT_CODES.has(e.code)) {+ return 'transient';+ }}// ── Message keyword fallback ──────────────────────────────────────────@@ -164,8 +168,12 @@ export function classifyError(err: unknown): ErrorClass {}// ── Bare-code substring fallback ──────────────────────────────────────- if (NODE_DOWN_BARE_CODE_REGEX.test(msg)) return 'node-down';- if (TRANSIENT_BARE_CODE_REGEX.test(msg)) return 'transient';+ if (NODE_DOWN_BARE_CODE_REGEX.test(msg)) {+ return 'node-down';+ }+ if (TRANSIENT_BARE_CODE_REGEX.test(msg)) {+ return 'transient';+ }// ── Wrapped causes (Node 18+ AggregateError, undici wrapping) ─────────if (e.cause) {diff --git a/packages/db/src/encryption.ts b/packages/db/src/encryption.tsindex 86e19e0a1..4def45b4f 100644--- a/packages/db/src/encryption.ts+++ b/packages/db/src/encryption.ts@@ -1,5 +1,5 @@-// At-rest encryption lives in @openpanel/common/server so it can be shared by+// At-rest encryption lives in @openpanel/core so it can be shared by// packages that can't depend on db (e.g. @openpanel/integrations). Re-exported// here for existing `@openpanel/db` importers (GSC tokens, TOTP). Same key// (ENCRYPTION_KEY) and same format as before — no data migration.-export { encrypt, decrypt } from '@openpanel/common/server';+export { decrypt, encrypt } from '@openpanel/core';diff --git a/packages/db/src/exports/batch-creator.ts b/packages/db/src/exports/batch-creator.tsindex 3c737f534..66be9a319 100644--- a/packages/db/src/exports/batch-creator.ts+++ b/packages/db/src/exports/batch-creator.ts@@ -1,9 +1,7 @@-import { createGzip } from 'node:zlib';import { Readable } from 'node:stream';import { pipeline } from 'node:stream/promises';-import { generateSecureId } from '@openpanel/common/server';-import { createLogger } from '@openpanel/logger';-+import { createGzip } from 'node:zlib';+import { createLogger, generateSecureId } from '@openpanel/core';import type { IExportEvent } from './export-event';const logger = createLogger({ name: 'batch-creator' });@@ -56,7 +54,7 @@ export function generateBatchPath(projectId: string,integrationId: string,batchId: string,- date: Date,+ date: Date): string {const dt = date.toISOString().split('T')[0]; // YYYY-MM-DDconst hour = date.getUTCHours().toString().padStart(2, '0'); // HH@@ -149,7 +147,7 @@ export async function createBatch(projectId: string,integrationId: string,events: IExportEvent[],- format: ExportFormat = 'jsonl_gzip',+ format: ExportFormat = 'jsonl_gzip'): Promise<IBatchResult> {const batchId = generateSecureId('batch');const now = new Date();@@ -204,7 +202,7 @@ export async function createBatch(partitionDate: dt,partitionHour: hour,},- 'Batch created',+ 'Batch created');return { info, files };diff --git a/packages/db/src/gsc.ts b/packages/db/src/gsc.tsindex 165c03eda..b1aa47ea5 100644--- a/packages/db/src/gsc.ts+++ b/packages/db/src/gsc.ts@@ -1,7 +1,7 @@+import { createLogger } from '@openpanel/core';import { cacheable } from '@openpanel/redis';import { originalCh } from './clickhouse/client';import { decrypt, encrypt } from './encryption';-import { createLogger } from '@openpanel/logger';import { db } from './prisma-client';const logger = createLogger({ name: 'db:gsc' });@@ -14,7 +14,7 @@ export interface GscSite {async function refreshGscToken(refreshToken: string): Promise<{ accessToken: string; expiresAt: Date }> {- if (!process.env.GOOGLE_CLIENT_ID || !process.env.GOOGLE_CLIENT_SECRET) {+ if (!(process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET)) {throw new Error('GOOGLE_CLIENT_ID or GOOGLE_CLIENT_SECRET is not set in this environment');@@ -57,7 +57,7 @@ export async function getGscAccessToken(projectId: string): Promise<string> {) {logger.info({ projectId, expiresAt: conn.accessTokenExpiresAt },- 'GSC using cached access token',+ 'GSC using cached access token');return decrypt(conn.accessToken);}@@ -68,7 +68,7 @@ export async function getGscAccessToken(projectId: string): Promise<string> {expiresAt: conn.accessTokenExpiresAt,hasRefreshToken: !!conn.refreshToken,},- 'GSC access token expired, attempting refresh',+ 'GSC access token expired, attempting refresh');try {@@ -77,19 +77,19 @@ export async function getGscAccessToken(projectId: string): Promise<string> {);await db.gscConnection.update({where: { projectId },- data: { accessToken: encrypt(accessToken), accessTokenExpiresAt: expiresAt },+ data: {+ accessToken: encrypt(accessToken),+ accessTokenExpiresAt: expiresAt,+ },});- logger.info(- { projectId, expiresAt },- 'GSC token refreshed successfully',- );+ logger.info({ projectId, expiresAt }, 'GSC token refreshed successfully');return accessToken;} catch (error) {const errorMessage =error instanceof Error ? error.message : 'Failed to refresh token';logger.error({ err: error, projectId, errorMessage },- 'GSC token refresh failed',+ 'GSC token refresh failed');await db.gscConnection.update({where: { projectId },@@ -152,7 +152,7 @@ async function queryGscSearchAnalytics(const allRows: GscApiRow[] = [];let startRow = 0;- const rowLimit = 25000;+ const rowLimit = 25_000;while (true) {const res = await fetch(url, {@@ -174,14 +174,18 @@ async function queryGscSearchAnalytics(if (!res.ok) {const text = await res.text();- throw new Error(`GSC query failed for dimensions [${dimensions.join(',')}]: ${text}`);+ throw new Error(+ `GSC query failed for dimensions [${dimensions.join(',')}]: ${text}`+ );}const data = (await res.json()) as { rows?: GscApiRow[] };const rows = data.rows ?? [];allRows.push(...rows);- if (rows.length < rowLimit) break;+ if (rows.length < rowLimit) {+ break;+ }startRow += rowLimit;}@@ -439,7 +443,9 @@ export const getGscCannibalization = cacheable(const totalImpressions = existing.impressions + row.impressions;if (totalImpressions > 0) {existing.position =- (existing.position * existing.impressions + row.position * row.impressions) / totalImpressions;+ (existing.position * existing.impressions ++ row.position * row.impressions) /+ totalImpressions;}existing.clicks += row.clicks;existing.impressions += row.impressions;@@ -481,16 +487,46 @@ export async function getGscPageDetails(startDate: string,endDate: string): Promise<{- timeseries: Array<{ date: string; clicks: number; impressions: number; ctr: number; position: number }>;- queries: Array<{ query: string; clicks: number; impressions: number; ctr: number; position: number }>;+ timeseries: Array<{+ date: string;+ clicks: number;+ impressions: number;+ ctr: number;+ position: number;+ }>;+ queries: Array<{+ query: string;+ clicks: number;+ impressions: number;+ ctr: number;+ position: number;+ }>;}> {- const conn = await db.gscConnection.findUniqueOrThrow({ where: { projectId } });+ const conn = await db.gscConnection.findUniqueOrThrow({+ where: { projectId },+ });const accessToken = await getGscAccessToken(projectId);- const filterGroups: GscFilterGroup[] = [{ filters: [{ dimension: 'page', operator: 'equals', expression: page }] }];+ const filterGroups: GscFilterGroup[] = [+ { filters: [{ dimension: 'page', operator: 'equals', expression: page }] },+ ];const [timeseriesRows, queryRows] = await Promise.all([- queryGscSearchAnalytics(accessToken, conn.siteUrl, startDate, endDate, ['date'], filterGroups),- queryGscSearchAnalytics(accessToken, conn.siteUrl, startDate, endDate, ['query'], filterGroups),+ queryGscSearchAnalytics(+ accessToken,+ conn.siteUrl,+ startDate,+ endDate,+ ['date'],+ filterGroups+ ),+ queryGscSearchAnalytics(+ accessToken,+ conn.siteUrl,+ startDate,+ endDate,+ ['query'],+ filterGroups+ ),]);return {@@ -517,16 +553,48 @@ export async function getGscQueryDetails(startDate: string,endDate: string): Promise<{- timeseries: Array<{ date: string; clicks: number; impressions: number; ctr: number; position: number }>;- pages: Array<{ page: string; clicks: number; impressions: number; ctr: number; position: number }>;+ timeseries: Array<{+ date: string;+ clicks: number;+ impressions: number;+ ctr: number;+ position: number;+ }>;+ pages: Array<{+ page: string;+ clicks: number;+ impressions: number;+ ctr: number;+ position: number;+ }>;}> {- const conn = await db.gscConnection.findUniqueOrThrow({ where: { projectId } });+ const conn = await db.gscConnection.findUniqueOrThrow({+ where: { projectId },+ });const accessToken = await getGscAccessToken(projectId);- const filterGroups: GscFilterGroup[] = [{ filters: [{ dimension: 'query', operator: 'equals', expression: query }] }];+ const filterGroups: GscFilterGroup[] = [+ {+ filters: [{ dimension: 'query', operator: 'equals', expression: query }],+ },+ ];const [timeseriesRows, pageRows] = await Promise.all([- queryGscSearchAnalytics(accessToken, conn.siteUrl, startDate, endDate, ['date'], filterGroups),- queryGscSearchAnalytics(accessToken, conn.siteUrl, startDate, endDate, ['page'], filterGroups),+ queryGscSearchAnalytics(+ accessToken,+ conn.siteUrl,+ startDate,+ endDate,+ ['date'],+ filterGroups+ ),+ queryGscSearchAnalytics(+ accessToken,+ conn.siteUrl,+ startDate,+ endDate,+ ['page'],+ filterGroups+ ),]);return {diff --git a/packages/db/src/logger.ts b/packages/db/src/logger.tsindex 6e023bf6a..09fdaa8d6 100644--- a/packages/db/src/logger.ts+++ b/packages/db/src/logger.ts@@ -1,3 +1,3 @@-import { createLogger } from '@openpanel/logger';+import { createLogger } from '@openpanel/core';export const logger = createLogger({ name: 'db:prisma' });diff --git a/packages/db/src/services/import.service.ts b/packages/db/src/services/import.service.tsindex dc4b3b622..2094e4585 100644--- a/packages/db/src/services/import.service.ts+++ b/packages/db/src/services/import.service.ts@@ -1,5 +1,5 @@import { createHash } from 'node:crypto';-import type { ILogger } from '@openpanel/logger';+import type { ILogger } from '@openpanel/core';import {ch,convertClickhouseDateToJs,diff --git a/packages/db/src/services/salt.service.ts b/packages/db/src/services/salt.service.tsindex 424b3e6af..6668e263b 100644--- a/packages/db/src/services/salt.service.ts+++ b/packages/db/src/services/salt.service.ts@@ -1,5 +1,4 @@-import { generateSalt } from '@openpanel/common/server';-+import { generateSalt } from '@openpanel/core';import { cacheable } from '@openpanel/redis';import { db } from '../prisma-client';@@ -24,7 +23,7 @@ export const getSalts = cacheable(return salts;},- 60 * 5,+ 60 * 5);export async function createInitialSalts() {@@ -52,7 +51,7 @@ export async function createInitialSalts() {if (retryCount < MAX_RETRIES) {const delay = BASE_DELAY * 2 ** retryCount;console.log(- `Retrying in ${delay}ms... (Attempt ${retryCount + 1}/${MAX_RETRIES})`,+ `Retrying in ${delay}ms... (Attempt ${retryCount + 1}/${MAX_RETRIES})`);await new Promise((resolve) => setTimeout(resolve, delay));return createSaltsWithRetry(retryCount + 1);diff --git a/packages/importer/package.json b/packages/importer/package.jsonindex c7fe3eeb6..732079c3e 100644--- a/packages/importer/package.json+++ b/packages/importer/package.json@@ -15,6 +15,7 @@},"dependencies": {"@openpanel/common": "workspace:*",+ "@openpanel/core": "workspace:*","@openpanel/db": "workspace:*","@openpanel/queue": "workspace:*","@openpanel/validation": "workspace:*",@@ -25,7 +26,6 @@"zod": "catalog:"},"devDependencies": {- "@openpanel/logger": "workspace:*","@types/node": "^20.0.0","@types/ramda": "^0.31.1","@types/unzipper": "^0.10.10",diff --git a/packages/importer/src/providers/amplitude.ts b/packages/importer/src/providers/amplitude.tsindex ff9a837ab..6c68e603a 100644--- a/packages/importer/src/providers/amplitude.ts+++ b/packages/importer/src/providers/amplitude.ts@@ -1,18 +1,18 @@import { randomUUID } from 'node:crypto';-import { Readable } from 'node:stream';import { createInterface } from 'node:readline';+import { Readable } from 'node:stream';import { createBrotliDecompress, createGunzip } from 'node:zlib';import { isSameDomain, parsePath, toDots } from '@openpanel/common';import {getReferrerWithQuery,+ type ILogger,parseReferrer,-} from '@openpanel/common/server';+} from '@openpanel/core';import {formatClickhouseDate,type IClickhouseEvent,type IClickhouseProfile,} from '@openpanel/db';-import type { ILogger } from '@openpanel/logger';import type { IAmplitudeImportConfig } from '@openpanel/validation';import unzipper from 'unzipper';import { z } from 'zod';@@ -357,7 +357,9 @@ export class AmplitudeProvider extends BaseImportProvider<AmplitudeRawEvent> {}const eventName = this.mapEventName(raw.event_type);- const timestamp = this.parseTimestamp(raw.event_time || raw.client_event_time);+ const timestamp = this.parseTimestamp(+ raw.event_time || raw.client_event_time+ );return {id: randomUUID(),@@ -526,7 +528,8 @@ export class AmplitudeProvider extends BaseImportProvider<AmplitudeRawEvent> {private getDeviceType(raw: AmplitudeRawEvent): string {const os = (raw.os_name || '').toLowerCase();const platform = (raw.platform || '').toLowerCase();- const family = `${raw.device_family || ''} ${raw.device_type || ''}`.toLowerCase();+ const family =+ `${raw.device_family || ''} ${raw.device_type || ''}`.toLowerCase();const isMobileOs =os === 'ios' ||@@ -558,7 +561,12 @@ export class AmplitudeProvider extends BaseImportProvider<AmplitudeRawEvent> {props: Record<string, unknown>,query: Record<string, string>): Record<string, any> {- const strip = new Set([...URL_LIKE_KEYS, 'referrer', '$referrer', ...Object.keys(query)]);+ const strip = new Set([+ ...URL_LIKE_KEYS,+ 'referrer',+ '$referrer',+ ...Object.keys(query),+ ]);const result: Record<string, any> = {};for (const [key, value] of Object.entries(props)) {diff --git a/packages/importer/src/providers/mixpanel.ts b/packages/importer/src/providers/mixpanel.tsindex b41ae0b25..6d09223c6 100644--- a/packages/importer/src/providers/mixpanel.ts+++ b/packages/importer/src/providers/mixpanel.ts@@ -2,13 +2,13 @@ import { randomUUID } from 'node:crypto';import { isSameDomain, parsePath, toDots } from '@openpanel/common';import {getReferrerWithQuery,+ type ILogger,parseReferrer,parseUserAgent,type UserAgentInfo,-} from '@openpanel/common/server';-import { formatClickhouseDate, type IClickhouseEvent } from '@openpanel/db';+} from '@openpanel/core';import type { IClickhouseProfile } from '@openpanel/db';-import type { ILogger } from '@openpanel/logger';+import { formatClickhouseDate, type IClickhouseEvent } from '@openpanel/db';import type { IMixpanelImportConfig } from '@openpanel/validation';import { z } from 'zod';import { BaseImportProvider } from '../base-provider';@@ -107,7 +107,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {requestsInWindow: this.requestTimestamps.length,waitMs,},- `Rate limit: ${this.requestTimestamps.length} requests in the last hour, waiting ${Math.ceil(waitMs / 1000)}s`,+ `Rate limit: ${this.requestTimestamps.length} requests in the last hour, waiting ${Math.ceil(waitMs / 1000)}s`);await new Promise((resolve) => setTimeout(resolve, waitMs));// Prune again after waiting@@ -182,7 +182,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {isRateLimit,willRetry: !isLastRetry,},- 'Failed to fetch events for date range',+ 'Failed to fetch events for date range');if (isLastRetry) {@@ -208,7 +208,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {chunkTo,isRateLimit,},- 'Retrying after delay',+ 'Retrying after delay');await new Promise((resolve) => setTimeout(resolve, delay));}@@ -239,7 +239,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {projectId,serviceAccount,},- 'Fetching events from Mixpanel',+ 'Fetching events from Mixpanel');const response = await fetch(`${url}?${params}`, {@@ -296,7 +296,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {} catch (error) {this.logger?.warn({ err: error, line: line.substring(0, 100) },- 'Failed to parse Mixpanel event',+ 'Failed to parse Mixpanel event');}}@@ -311,7 +311,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {} catch (error) {this.logger?.warn({ err: error, line: buffer.substring(0, 100) },- 'Failed to parse Mixpanel event (remaining buffer)',+ 'Failed to parse Mixpanel event (remaining buffer)');}}@@ -340,7 +340,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {this.logger?.info({ page, page_size: pageSize, projectId },- 'Fetching profiles from Mixpanel Engage',+ 'Fetching profiles from Mixpanel Engage');const response = await fetch(url, {@@ -370,7 +370,10 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {}const data = (await response.json()) as {- results?: Array<{ $distinct_id: string | number; $properties?: Record<string, unknown> }>;+ results?: Array<{+ $distinct_id: string | number;+ $properties?: Record<string, unknown>;+ }>;page?: number;total?: number;};@@ -383,7 +386,7 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {} else {this.logger?.warn({ row: JSON.stringify(row).slice(0, 200) },- 'Skipping invalid Mixpanel profile',+ 'Skipping invalid Mixpanel profile');}}@@ -410,9 +413,14 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {const properties: Record<string, string> = {};const stripPrefix = /^\$/;for (const [key, value] of Object.entries(props)) {- if (stripPrefix.test(key)) continue;- if (value == null) continue;- properties[key] = typeof value === 'object' ? JSON.stringify(value) : String(value);+ if (stripPrefix.test(key)) {+ continue;+ }+ if (value == null) {+ continue;+ }+ properties[key] =+ typeof value === 'object' ? JSON.stringify(value) : String(value);}return {@@ -552,7 +560,8 @@ export class MixpanelProvider extends BaseImportProvider<MixpanelRawEvent> {os: uaInfo.os || props.$os,os_version: uaInfo.osVersion || props.$osVersion,browser: uaInfo.browser || props.$browser,- browser_version: uaInfo.browserVersion || String(props.$browser_version ?? ''),+ browser_version:+ uaInfo.browserVersion || String(props.$browser_version ?? ''),device: this.getDeviceType(props.mp_lib, uaInfo, props),brand: uaInfo.brand || '',model: uaInfo.model || '',diff --git a/packages/importer/src/providers/umami.ts b/packages/importer/src/providers/umami.tsindex 758ddd85b..c873eeb40 100644--- a/packages/importer/src/providers/umami.ts+++ b/packages/importer/src/providers/umami.ts@@ -6,11 +6,11 @@ import { isSameDomain, parsePath, toDots } from '@openpanel/common';import {generateDeviceId,getReferrerWithQuery,+ type ILogger,parseReferrer,-} from '@openpanel/common/server';-import { safeFetchStream } from '@openpanel/common/server/safe-fetch';+ safeFetchStream,+} from '@openpanel/core';import { formatClickhouseDate, type IClickhouseEvent } from '@openpanel/db';-import type { ILogger } from '@openpanel/logger';import type { IUmamiImportConfig } from '@openpanel/validation';import { parse } from 'csv-parse';import { assocPath } from 'ramda';@@ -141,7 +141,9 @@ export class UmamiProvider extends BaseImportProvider<UmamiRawEvent> {contentType)) {- this.logger?.warn(`Warning: Content-Type is ${contentType}, expected CSV-ish`);+ this.logger?.warn(+ `Warning: Content-Type is ${contentType}, expected CSV-ish`+ );}if (maxBytes && contentLen && contentLen > maxBytes) {diff --git a/packages/integrations/package.json b/packages/integrations/package.jsonindex 418f549fa..dc64e20b8 100644--- a/packages/integrations/package.json+++ b/packages/integrations/package.json@@ -11,8 +11,8 @@"@aws-sdk/client-sts": "^3.974.0","@google-cloud/storage": "^7.18.0","@openpanel/common": "workspace:*",+ "@openpanel/core": "workspace:*","@openpanel/js-runtime": "workspace:*",- "@openpanel/logger": "workspace:*","@openpanel/validation": "workspace:*","@slack/bolt": "^3.18.0","@slack/oauth": "^3.0.0"diff --git a/packages/integrations/src/object-store/gcs-adapter.test.ts b/packages/integrations/src/object-store/gcs-adapter.test.tsindex d39058659..8602f35b6 100644--- a/packages/integrations/src/object-store/gcs-adapter.test.ts+++ b/packages/integrations/src/object-store/gcs-adapter.test.ts@@ -1,5 +1,5 @@import { gunzipSync, gzipSync } from 'node:zlib';-import { encryptCredential } from '@openpanel/common/server';+import { encryptCredential } from '@openpanel/core';import { beforeAll, describe, expect, it } from 'vitest';import { createGCSAdapter } from './gcs-adapter';@@ -52,7 +52,7 @@ async function createBucket(name: string): Promise<void> {async function readObject(bucket: string, key: string): Promise<Buffer> {const res = await fetch(- `${EMULATOR}/storage/v1/b/${bucket}/o/${encodeURIComponent(key)}?alt=media`,+ `${EMULATOR}/storage/v1/b/${bucket}/o/${encodeURIComponent(key)}?alt=media`);if (!res.ok) {throw new Error(`Object ${key} not readable: ${res.status}`);@@ -60,7 +60,9 @@ async function readObject(bucket: string, key: string): Promise<Buffer> {return Buffer.from(await res.arrayBuffer());}-function adapter(overrides: { bucket?: string; serviceAccountKey?: string } = {}) {+function adapter(+ overrides: { bucket?: string; serviceAccountKey?: string } = {}+) {return createGCSAdapter({type: 'gcs_export',bucket: overrides.bucket ?? BUCKET,@@ -94,7 +96,9 @@ describe('GCSAdapter credential validation', () => {});it('refuses a document that is not JSON', async () => {- const result = await adapter({ serviceAccountKey: 'nope' }).testConnection();+ const result = await adapter({+ serviceAccountKey: 'nope',+ }).testConnection();expect(result.success).toBe(false);expect(result.error).toContain('not valid JSON');});@@ -129,7 +133,7 @@ describe.skipIf(!available)('GCSAdapter (fake-gcs-server)', () => {await adapter().testConnection();await expect(- readObject(BUCKET, 'openpanel-exports/.openpanel-connection-test'),+ readObject(BUCKET, 'openpanel-exports/.openpanel-connection-test')).rejects.toThrow();});@@ -138,7 +142,7 @@ describe.skipIf(!available)('GCSAdapter (fake-gcs-server)', () => {expect(res.success).toBe(false);expect(res.error).toBe(- "Bucket 'no-such-bucket' does not exist or is not accessible",+ "Bucket 'no-such-bucket' does not exist or is not accessible");});@@ -168,7 +172,9 @@ describe.skipIf(!available)('GCSAdapter (fake-gcs-server)', () => {location: `gs://${BUCKET}/${key}`,});expect(result.etag).toBeTruthy();- expect((await readObject(BUCKET, key)).toString()).toBe('hello openpanel');+ expect((await readObject(BUCKET, key)).toString()).toBe(+ 'hello openpanel'+ );});it('accepts string content', async () => {@@ -229,7 +235,7 @@ describe.skipIf(!available)('GCSAdapter (fake-gcs-server)', () => {key: `many/file-${i}.txt`,content: `content-${i}`,contentType: 'text/plain',- })),+ })));expect(results).toHaveLength(3);@@ -238,7 +244,7 @@ describe.skipIf(!available)('GCSAdapter (fake-gcs-server)', () => {expect((result as { key: string }).key).toBe(`many/file-${i}.txt`);});expect((await readObject(BUCKET, 'many/file-1.txt')).toString()).toBe(- 'content-1',+ 'content-1');});diff --git a/packages/integrations/src/object-store/gcs-adapter.ts b/packages/integrations/src/object-store/gcs-adapter.tsindex 3d0bc5825..61656d782 100644--- a/packages/integrations/src/object-store/gcs-adapter.ts+++ b/packages/integrations/src/object-store/gcs-adapter.ts@@ -1,8 +1,9 @@import { Storage } from '@google-cloud/storage';-import { decryptCredential } from '@openpanel/common/server';-import { createLogger } from '@openpanel/logger';-import { type IGCSExportConfig, parseServiceAccountKey } from '@openpanel/validation';-+import { createLogger, decryptCredential } from '@openpanel/core';+import {+ type IGCSExportConfig,+ parseServiceAccountKey,+} from '@openpanel/validation';import type {IObjectStoreAdapter,IUploadOptions,@@ -52,7 +53,10 @@ export class GCSAdapter implements IObjectStoreAdapter {// any future caller that skips it.const parsed = parseServiceAccountKey(this.config.serviceAccountKey);if (!parsed.ok) {- logger.error({ reason: parsed.error }, 'Rejected GCS credential document');+ logger.error(+ { reason: parsed.error },+ 'Rejected GCS credential document'+ );throw new Error(`Invalid service account key: ${parsed.error}`);}const { credentials } = parsed;@@ -90,7 +94,7 @@ export class GCSAdapter implements IObjectStoreAdapter {{projectId: credentials.project_id,},- 'GCS client created',+ 'GCS client created');return this.storage;@@ -132,7 +136,7 @@ export class GCSAdapter implements IObjectStoreAdapter {key: options.key,generation: metadata?.generation,},- 'File uploaded to GCS',+ 'File uploaded to GCS');return {@@ -148,7 +152,7 @@ export class GCSAdapter implements IObjectStoreAdapter {bucket: options.bucket,key: options.key,},- 'Failed to upload file to GCS',+ 'Failed to upload file to GCS');throw error;}@@ -158,10 +162,10 @@ export class GCSAdapter implements IObjectStoreAdapter {* Upload multiple files to GCS*/async uploadMany(- options: Array<IUploadOptions>,+ options: Array<IUploadOptions>): Promise<Array<IUploadResult | Error>> {const results = await Promise.allSettled(- options.map((opt) => this.upload(opt)),+ options.map((opt) => this.upload(opt)));return results.map((result) => {diff --git a/packages/integrations/src/object-store/s3-adapter.ts b/packages/integrations/src/object-store/s3-adapter.tsindex a8777e0e2..a35b8a573 100644--- a/packages/integrations/src/object-store/s3-adapter.ts+++ b/packages/integrations/src/object-store/s3-adapter.ts@@ -1,3 +1,6 @@+import { Agent as HttpAgent } from 'node:http';+import { Agent as HttpsAgent } from 'node:https';+import type { LookupFunction } from 'node:net';import {HeadBucketCommand,PutObjectCommand,@@ -5,17 +8,13 @@ import {S3Client,} from '@aws-sdk/client-s3';import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts';-import { Agent as HttpAgent } from 'node:http';-import { Agent as HttpsAgent } from 'node:https';-import type { LookupFunction } from 'node:net';import {assertSafeUrl,+ createLogger,createPinnedLookup,decryptCredential,-} from '@openpanel/common/server';-import { createLogger } from '@openpanel/logger';+} from '@openpanel/core';import type { IS3ExportConfig } from '@openpanel/validation';-import type {IObjectStoreAdapter,IUploadOptions,@@ -93,7 +92,7 @@ export class S3Adapter implements IObjectStoreAdapter {* For R2, MinIO, DigitalOcean Spaces, etc.*/private async getClientWithAccessKeys(- pinnedAddress?: string,+ pinnedAddress?: string): Promise<S3Client> {// Access key clients don't expire, reuse if available. A cached client is// already pinned to a previously validated address, so reuse is safe.@@ -102,7 +101,9 @@ export class S3Adapter implements IObjectStoreAdapter {}if (this.config.authMode !== 'access_key') {- throw new Error('Access key auth mode required but IAM role config provided');+ throw new Error(+ 'Access key auth mode required but IAM role config provided'+ );}const client = new S3Client({@@ -126,7 +127,7 @@ export class S3Adapter implements IObjectStoreAdapter {region: this.config.region,endpoint: this.config.endpoint || 'default',},- 'S3 client created with access keys',+ 'S3 client created with access keys');// Mark as non-expiring@@ -156,7 +157,9 @@ export class S3Adapter implements IObjectStoreAdapter {*/private async createClientWithAssumedRole(): Promise<S3Client> {if (this.config.authMode !== 'iam_role') {- throw new Error('IAM role auth mode required but access key config provided');+ throw new Error(+ 'IAM role auth mode required but access key config provided'+ );}const stsClient = new STSClient({ region: this.config.region });@@ -203,7 +206,7 @@ export class S3Adapter implements IObjectStoreAdapter {roleArn: this.config.roleArn,expiresAt: new Date(this.clientExpiresAt).toISOString(),},- 'S3 client created with assumed role',+ 'S3 client created with assumed role');return s3Client;@@ -213,7 +216,7 @@ export class S3Adapter implements IObjectStoreAdapter {error,roleArn: this.config.roleArn,},- 'Failed to assume role for S3 access',+ 'Failed to assume role for S3 access');throw error;}@@ -268,7 +271,7 @@ export class S3Adapter implements IObjectStoreAdapter {key: options.key,etag: response.ETag,},- 'File uploaded to S3',+ 'File uploaded to S3');return {@@ -284,7 +287,7 @@ export class S3Adapter implements IObjectStoreAdapter {bucket: options.bucket,key: options.key,},- 'Failed to upload file to S3',+ 'Failed to upload file to S3');throw error;}@@ -294,10 +297,10 @@ export class S3Adapter implements IObjectStoreAdapter {* Upload multiple files to S3*/async uploadMany(- options: Array<IUploadOptions>,+ options: Array<IUploadOptions>): Promise<Array<IUploadResult | Error>> {const results = await Promise.allSettled(- options.map((opt) => this.upload(opt)),+ options.map((opt) => this.upload(opt)));return results.map((result) => {diff --git a/packages/integrations/src/registry.ts b/packages/integrations/src/registry.tsindex 875e9fc88..5b89f105e 100644--- a/packages/integrations/src/registry.ts+++ b/packages/integrations/src/registry.ts@@ -1,4 +1,4 @@-import { encryptCredential } from '@openpanel/common/server';+import { encryptCredential } from '@openpanel/core';import {execute as executeJavaScriptTemplate,validate as validateJavaScriptTemplate,@@ -65,7 +65,7 @@ export interface IServerIntegration<T extends IIntegrationConfig['type']> {validateConfig?(config: ConfigOf<T>): { valid: boolean; error?: string };// Optional pre-save connection test (used by the generic tRPC procedure).testConnection?(- config: ConfigOf<T>,+ config: ConfigOf<T>): Promise<{ success: boolean; error?: string }>;/*** Config values that must never travel back to a client. ONE declaration@@ -116,16 +116,15 @@ const slackServer: IServerIntegration<'slack'> = {// the customer's Slack workspace. Redact-only: the worker reads them raw, and// nothing round-trips them (the OAuth callback rewrites the whole config), so// there is no carry-over to worry about.- secretFields: [- { path: 'access_token' },- { path: 'incoming_webhook.url' },- ],+ secretFields: [{ path: 'access_token' }, { path: 'incoming_webhook.url' }],notification: {deliver: ({ config, notification }) =>sendSlackNotification({fetcher: safeWebhookFetcher,webhookUrl: config.incoming_webhook.url,- message: [`🔔 *${notification.title}*`, notification.message].join('\n'),+ message: [`🔔 *${notification.title}*`, notification.message].join(+ '\n'+ ),}),},};@@ -135,7 +134,7 @@ const discordServer: IServerIntegration<'discord'> = {testConnection: async (config) => {const res = await sendTestDiscordNotification(config.url,- safeWebhookFetcher,+ safeWebhookFetcher);return res.ok? { success: true }@@ -196,7 +195,7 @@ const webhookServer: IServerIntegration<'webhook'> = {safeWebhookFetcher,config.url,body,- config.headers ?? {},+ config.headers ?? {});},},@@ -241,7 +240,7 @@ export const SERVER_INTEGRATIONS = {* dispatch path lives here; call sites get a correctly-typed IServerIntegration<T>.*/export function getServerIntegration<T extends IIntegrationConfig['type']>(- type: T,+ type: T): IServerIntegration<T> {return SERVER_INTEGRATIONS[type] as unknown as IServerIntegration<T>;}@@ -262,7 +261,7 @@ type LooseConfig = Record<string, unknown>;/** Lenient like `isKind`: an empty/unknown config simply has no secrets. */function secretsFor(config: unknown): readonly IConfigSecret<never>[] {const type = (config as { type?: string } | null)?.type;- if (!type || !(type in SERVER_INTEGRATIONS)) {+ if (!(type && type in SERVER_INTEGRATIONS)) {return [];}const plugin = SERVER_INTEGRATIONS[@@ -312,8 +311,8 @@ function mapSecrets<C>(map: (value: string,secret: IConfigSecret<never>,- recordKey?: string,- ) => string | undefined,+ recordKey?: string+ ) => string | undefined): C {let next = config;@@ -358,7 +357,7 @@ function mapSecrets<C>(/** Encrypt every declared secret that is stored encrypted. */export function encryptConfigSecrets<C>(config: C): C {return mapSecrets(config, (value, secret) =>- secret.encrypted ? encryptCredential(value) : undefined,+ secret.encrypted ? encryptCredential(value) : undefined);}@@ -425,7 +424,7 @@ export function findMissingSecretFields(config: unknown): string[] {(secret) =>secret.encrypted &&readPath(config, secret.path) === '' &&- !secret.record,+ !secret.record).map((secret) => secret.path);}diff --git a/packages/integrations/src/safe-fetcher.ts b/packages/integrations/src/safe-fetcher.tsindex 93bf19abb..596df10b3 100644--- a/packages/integrations/src/safe-fetcher.ts+++ b/packages/integrations/src/safe-fetcher.ts@@ -1,4 +1,4 @@-import { safeFetch } from '@openpanel/common/server/safe-fetch';+import { safeFetch } from '@openpanel/core';import type { WebhookFetcher } from './fetcher';/**diff --git a/packages/logger/package.json b/packages/logger/package.jsondeleted file mode 100644index 479b364a3..000000000--- a/packages/logger/package.json+++ /dev/null@@ -1,20 +0,0 @@-{- "name": "@openpanel/logger",- "version": "0.0.1",- "type": "module",- "main": "index.ts",- "scripts": {- "typecheck": "tsc --noEmit"- },- "dependencies": {- "@hyperdx/node-opentelemetry": "catalog:",- "pino": "catalog:",- "pino-pretty": "catalog:"- },- "devDependencies": {- "@openpanel/tsconfig": "workspace:*",- "date-fns": "^3.3.1",- "prisma": "^5.1.1",- "typescript": "catalog:"- }-}diff --git a/packages/logger/tsconfig.json b/packages/logger/tsconfig.jsondeleted file mode 100644index a4a54fb86..000000000--- a/packages/logger/tsconfig.json+++ /dev/null@@ -1,13 +0,0 @@-{- "extends": "@openpanel/tsconfig/base.json",- "compilerOptions": {- "baseUrl": ".",- "paths": {- "@/*": ["./src/*"]- },- "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",- "strictNullChecks": true- },- "include": ["."],- "exclude": ["node_modules"]-}diff --git a/packages/mcp/package.json b/packages/mcp/package.jsonindex 442e002ab..e0ccabe47 100644--- a/packages/mcp/package.json+++ b/packages/mcp/package.json@@ -14,8 +14,8 @@"dependencies": {"@modelcontextprotocol/sdk": "^1.12.0","@openpanel/common": "workspace:*",+ "@openpanel/core": "workspace:*","@openpanel/db": "workspace:*",- "@openpanel/logger": "workspace:*","@openpanel/redis": "workspace:*","@openpanel/validation": "workspace:*","zod": "catalog:"diff --git a/packages/mcp/src/auth.test.ts b/packages/mcp/src/auth.test.tsindex 775e26ed1..76b7b620d 100644--- a/packages/mcp/src/auth.test.ts+++ b/packages/mcp/src/auth.test.ts@@ -1,27 +1,42 @@-import { describe, expect, it, vi, beforeEach } from 'vitest';+import { beforeEach, describe, expect, it, vi } from 'vitest';const mockGetClientByIdCached = vi.hoisted(() => vi.fn());const mockVerifyPassword = vi.hoisted(() => vi.fn());const mockGetCache = vi.hoisted(() => vi.fn());+const noopLogger = vi.hoisted(() => {+ const logger = {+ fatal: () => {},+ error: () => {},+ warn: () => {},+ info: () => {},+ debug: () => {},+ trace: () => {},+ child: () => logger,+ };+ return logger;+});vi.mock('@openpanel/db', () => ({ClientType: { write: 'write', read: 'read', root: 'root' },getClientByIdCached: mockGetClientByIdCached,}));-vi.mock('@openpanel/common/server', () => ({+vi.mock('@openpanel/core', () => ({verifyPassword: mockVerifyPassword,+ createLogger: () => noopLogger,}));vi.mock('@openpanel/redis', () => ({getCache: mockGetCache,}));-import { McpAuthError, authenticateToken, extractToken } from './auth';+import { authenticateToken, extractToken, McpAuthError } from './auth';const VALID_CLIENT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';const VALID_SECRET = 'mysecret';-const VALID_TOKEN = Buffer.from(`${VALID_CLIENT_ID}:${VALID_SECRET}`).toString('base64');+const VALID_TOKEN = Buffer.from(`${VALID_CLIENT_ID}:${VALID_SECRET}`).toString(+ 'base64'+);const baseClient = {id: VALID_CLIENT_ID,@@ -34,7 +49,9 @@ const baseClient = {beforeEach(() => {vi.clearAllMocks();// Default: cache calls through to the fn- mockGetCache.mockImplementation((_key: string, _ttl: number, fn: () => Promise<unknown>) => fn());+ mockGetCache.mockImplementation(+ (_key: string, _ttl: number, fn: () => Promise<unknown>) => fn()+ );mockVerifyPassword.mockResolvedValue(true);});@@ -52,7 +69,9 @@ describe('extractToken', () => {});it('prefers query param over header', () => {- expect(extractToken({ token: 'from-query' }, 'Bearer from-header')).toBe('from-query');+ expect(extractToken({ token: 'from-query' }, 'Bearer from-header')).toBe(+ 'from-query'+ );});it('returns undefined when neither is present', () => {@@ -71,32 +90,44 @@ describe('extractToken', () => {describe('authenticateToken', () => {it('throws McpAuthError when token is missing', async () => {await expect(authenticateToken(undefined)).rejects.toThrow(McpAuthError);- await expect(authenticateToken(undefined)).rejects.toThrow('Missing authentication token');+ await expect(authenticateToken(undefined)).rejects.toThrow(+ 'Missing authentication token'+ );});it('throws McpAuthError for non-base64 token', async () => {// Buffer.from with invalid base64 doesn't throw — but the decoded result won't have a colon- await expect(authenticateToken('!!!invalid!!!')).rejects.toThrow(McpAuthError);+ await expect(authenticateToken('!!!invalid!!!')).rejects.toThrow(+ McpAuthError+ );});it('throws McpAuthError when token has no colon separator', async () => {const token = Buffer.from('nodivider').toString('base64');- await expect(authenticateToken(token)).rejects.toThrow('Invalid token format');+ await expect(authenticateToken(token)).rejects.toThrow(+ 'Invalid token format'+ );});it('throws McpAuthError when clientId is not a UUID', async () => {const token = Buffer.from('not-a-uuid:secret').toString('base64');- await expect(authenticateToken(token)).rejects.toThrow('Invalid client ID format');+ await expect(authenticateToken(token)).rejects.toThrow(+ 'Invalid client ID format'+ );});it('throws McpAuthError when clientSecret is empty', async () => {const token = Buffer.from(`${VALID_CLIENT_ID}:`).toString('base64');- await expect(authenticateToken(token)).rejects.toThrow('Client secret is required');+ await expect(authenticateToken(token)).rejects.toThrow(+ 'Client secret is required'+ );});it('throws McpAuthError when client is not found', async () => {mockGetClientByIdCached.mockResolvedValue(null);- await expect(authenticateToken(VALID_TOKEN)).rejects.toThrow('Invalid credentials');+ await expect(authenticateToken(VALID_TOKEN)).rejects.toThrow(+ 'Invalid credentials'+ );});it('throws McpAuthError when client has no stored secret', async () => {@@ -106,13 +137,17 @@ describe('authenticateToken', () => {it('throws McpAuthError for write-only clients', async () => {mockGetClientByIdCached.mockResolvedValue({ ...baseClient, type: 'write' });- await expect(authenticateToken(VALID_TOKEN)).rejects.toThrow('Write-only clients');+ await expect(authenticateToken(VALID_TOKEN)).rejects.toThrow(+ 'Write-only clients'+ );});it('throws McpAuthError when password verification fails', async () => {mockGetClientByIdCached.mockResolvedValue(baseClient);mockVerifyPassword.mockResolvedValue(false);- await expect(authenticateToken(VALID_TOKEN)).rejects.toThrow('Invalid credentials');+ await expect(authenticateToken(VALID_TOKEN)).rejects.toThrow(+ 'Invalid credentials'+ );});it('returns read client context on success', async () => {@@ -126,7 +161,11 @@ describe('authenticateToken', () => {});it('returns root client context with null projectId', async () => {- mockGetClientByIdCached.mockResolvedValue({ ...baseClient, type: 'root', projectId: null });+ mockGetClientByIdCached.mockResolvedValue({+ ...baseClient,+ type: 'root',+ projectId: null,+ });const ctx = await authenticateToken(VALID_TOKEN);expect(ctx).toEqual({projectId: null,@@ -147,13 +186,17 @@ describe('authenticateToken', () => {it('cache key uses SHA-256 hash, not raw secret', async () => {mockGetClientByIdCached.mockResolvedValue(baseClient);let capturedKey = '';- mockGetCache.mockImplementation((key: string, _ttl: number, fn: () => Promise<unknown>) => {- capturedKey = key;- return fn();- });+ mockGetCache.mockImplementation(+ (key: string, _ttl: number, fn: () => Promise<unknown>) => {+ capturedKey = key;+ return fn();+ }+ );await authenticateToken(VALID_TOKEN);expect(capturedKey).toContain(`mcp:auth:${VALID_CLIENT_ID}:`);expect(capturedKey).not.toContain(VALID_SECRET);- expect(capturedKey).not.toContain(Buffer.from(VALID_SECRET).toString('base64'));+ expect(capturedKey).not.toContain(+ Buffer.from(VALID_SECRET).toString('base64')+ );});});diff --git a/packages/mcp/src/auth.ts b/packages/mcp/src/auth.tsindex c68e6ced0..7f9c66a31 100644--- a/packages/mcp/src/auth.ts+++ b/packages/mcp/src/auth.ts@@ -1,8 +1,7 @@import { createHash } from 'node:crypto';-import { verifyPassword } from '@openpanel/common/server';+import { createLogger, verifyPassword } from '@openpanel/core';import { ClientType, getClientByIdCached } from '@openpanel/db';import { getCache } from '@openpanel/redis';-import { createLogger } from '@openpanel/logger';const logger = createLogger({ name: 'mcp:auth' });@@ -34,7 +33,7 @@ export class McpAuthError extends Error {* - root clients get null projectId + organizationId (multi-project access)*/export async function authenticateToken(- token: string | undefined,+ token: string | undefined): Promise<McpAuthContext> {if (!token) {throw new McpAuthError('Missing authentication token');@@ -51,10 +50,10 @@ export async function authenticateToken(if (colonIndex === -1) {logger.warn({ decodedLength: decoded.length },- 'MCP auth: token has no colon separator',+ 'MCP auth: token has no colon separator');throw new McpAuthError(- 'Invalid token format — expected base64(clientId:clientSecret)',+ 'Invalid token format — expected base64(clientId:clientSecret)');}@@ -63,12 +62,12 @@ export async function authenticateToken(logger.info({ clientId, secretPrefix: clientSecret.slice(0, 6) },- 'MCP auth: decoded token',+ 'MCP auth: decoded token');if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(- clientId,+ clientId)) {logger.warn({ clientId }, 'MCP auth: invalid client ID format');@@ -87,34 +86,37 @@ export async function authenticateToken(logger.info({ clientId, type: client.type, hasSecret: !!client.secret },- 'MCP auth: client found',+ 'MCP auth: client found');if (!client.secret) {throw new McpAuthError(- 'This client has no secret — only clients with a secret can use MCP',+ 'This client has no secret — only clients with a secret can use MCP');}if (client.type === ClientType.write) {logger.warn({ clientId }, 'MCP auth: write-only client rejected');throw new McpAuthError(- 'Write-only clients cannot use MCP — use a read or root client',+ 'Write-only clients cannot use MCP — use a read or root client');}- const secretHash = createHash('sha256').update(clientSecret).digest('hex').slice(0, 16);+ const secretHash = createHash('sha256')+ .update(clientSecret)+ .digest('hex')+ .slice(0, 16);const cacheKey = `mcp:auth:${clientId}:${secretHash}`;const isVerified = await getCache(cacheKey,60 * 5,async () => await verifyPassword(clientSecret, client.secret!),- true,+ true);logger.info({ clientId, isVerified },- 'MCP auth: password verification result',+ 'MCP auth: password verification result');if (!isVerified) {@@ -136,7 +138,7 @@ export async function authenticateToken(*/export function extractToken(query: Record<string, unknown>,- authHeader: string | undefined,+ authHeader: string | undefined): string | undefined {if (typeof query['token'] === 'string') {return query['token'];@@ -146,4 +148,3 @@ export function extractToken(}return undefined;}-diff --git a/packages/mcp/src/handler.ts b/packages/mcp/src/handler.tsindex c35762922..9c609325d 100644--- a/packages/mcp/src/handler.ts+++ b/packages/mcp/src/handler.ts@@ -1,8 +1,8 @@+import type { IncomingMessage, ServerResponse } from 'node:http';import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';import type { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js';-import type { IncomingMessage, ServerResponse } from 'node:http';-import { createLogger } from '@openpanel/logger';-import { McpAuthError, authenticateToken, extractToken } from './auth';+import { createLogger } from '@openpanel/core';+import { authenticateToken, extractToken, McpAuthError } from './auth';import { createMcpServer } from './server';import type { SessionManager } from './session-manager';@@ -26,7 +26,7 @@ export async function handleMcpPost(req: IncomingMessage,res: ServerResponse,body: unknown,- query: Record<string, unknown>,+ query: Record<string, unknown>): Promise<void> {const sessionId = req.headers['mcp-session-id'] as string | undefined;const message = body as JSONRPCMessage;@@ -37,7 +37,7 @@ export async function handleMcpPost(method: 'method' in message ? message.method : 'unknown',hasAuth: !!(query['token'] || req.headers.authorization),},- 'MCP POST request',+ 'MCP POST request');if (sessionId) {@@ -45,7 +45,9 @@ export async function handleMcpPost(if (!context) {logger.warn({ sessionId }, 'MCP session not found in Redis');res.writeHead(404, { 'Content-Type': 'application/json' });- res.end(JSON.stringify({ error: 'Session not found — please reconnect' }));+ res.end(+ JSON.stringify({ error: 'Session not found — please reconnect' })+ );return;}@@ -96,7 +98,7 @@ export async function handleMcpPost(organizationId: context.organizationId,projectId: context.projectId,},- 'MCP session created',+ 'MCP session created');res.writeHead(200, {@@ -128,10 +130,17 @@ export async function handleMcpPost(async function processRequest(context: Parameters<typeof createMcpServer>[0],message: JSONRPCMessage,- isInitialize = false,+ isInitialize = false): Promise<JSONRPCMessage> {- if ('method' in message && message.method === 'tools/call' && 'params' in message) {- const { name, arguments: args } = (message.params ?? {}) as { name?: string; arguments?: unknown };+ if (+ 'method' in message &&+ message.method === 'tools/call' &&+ 'params' in message+ ) {+ const { name, arguments: args } = (message.params ?? {}) as {+ name?: string;+ arguments?: unknown;+ };logger.info({tool: name,@@ -140,10 +149,11 @@ async function processRequest(projectId: context.projectId,clientType: context.clientType,},- 'MCP tool call',+ 'MCP tool call');}- const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();+ const [clientTransport, serverTransport] =+ InMemoryTransport.createLinkedPair();const server = createMcpServer(context);await server.connect(serverTransport);@@ -178,15 +188,19 @@ async function processRequest(});if ('method' in message && message.method === 'tools/call') {- const { name } = (('params' in message && message.params) ?? {}) as { name?: string };- const isError = 'result' in response && (response.result as { isError?: boolean })?.isError;+ const { name } = (('params' in message && message.params) ?? {}) as {+ name?: string;+ };+ const isError =+ 'result' in response &&+ (response.result as { isError?: boolean })?.isError;logger.info({tool: name,durationMs: Date.now() - start,isError: isError ?? false,},- 'MCP tool result',+ 'MCP tool result');}@@ -199,8 +213,13 @@ async function processRequest(export async function handleMcpGet(_sessionManager: SessionManager,_req: IncomingMessage,- res: ServerResponse,+ res: ServerResponse): Promise<void> {- res.writeHead(405, { 'Content-Type': 'application/json', Allow: 'POST, DELETE' });- res.end(JSON.stringify({ error: 'SSE not supported — use POST for all requests' }));+ res.writeHead(405, {+ 'Content-Type': 'application/json',+ Allow: 'POST, DELETE',+ });+ res.end(+ JSON.stringify({ error: 'SSE not supported — use POST for all requests' })+ );}diff --git a/packages/mcp/src/session-manager.test.ts b/packages/mcp/src/session-manager.test.tsindex dc72ce20b..91374b471 100644--- a/packages/mcp/src/session-manager.test.ts+++ b/packages/mcp/src/session-manager.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it, vi, beforeEach } from 'vitest';+import { beforeEach, describe, expect, it, vi } from 'vitest';import type { McpAuthContext } from './auth';const mockSetJson = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));@@ -15,7 +15,7 @@ vi.mock('@openpanel/redis', () => ({}),}));-vi.mock('@openpanel/logger', () => ({+vi.mock('@openpanel/core', () => ({createLogger: () => ({info: vi.fn(),warn: vi.fn(),@@ -50,7 +50,11 @@ describe('SessionManager', () => {it('stores context in Redis with TTL', async () => {const sm = new SessionManager();await sm.setContext('sess-1', CTX);- expect(mockSetJson).toHaveBeenCalledWith('mcp:session:sess-1', 30 * 60, CTX);+ expect(mockSetJson).toHaveBeenCalledWith(+ 'mcp:session:sess-1',+ 30 * 60,+ CTX+ );});it('retrieves context from Redis', async () => {diff --git a/packages/mcp/src/session-manager.ts b/packages/mcp/src/session-manager.tsindex fdefeba37..9a986eed9 100644--- a/packages/mcp/src/session-manager.ts+++ b/packages/mcp/src/session-manager.ts@@ -1,5 +1,5 @@import { randomUUID } from 'node:crypto';-import { createLogger } from '@openpanel/logger';+import { createLogger } from '@openpanel/core';import { getRedisCache } from '@openpanel/redis';import type { McpAuthContext } from './auth';@@ -31,7 +31,7 @@ export class SessionManager {organizationId: context.organizationId,projectId: context.projectId,},- 'MCP session context stored',+ 'MCP session context stored');}diff --git a/packages/mcp/src/tools/shared.ts b/packages/mcp/src/tools/shared.tsindex fc1e16bf9..15f0bc4cc 100644--- a/packages/mcp/src/tools/shared.ts+++ b/packages/mcp/src/tools/shared.ts@@ -1,5 +1,5 @@+import { createLogger } from '@openpanel/core';import { resolveClientProjectId } from '@openpanel/db';-import { createLogger } from '@openpanel/logger';import { z } from 'zod';import type { McpAuthContext } from '../auth';@@ -11,7 +11,7 @@ const logger = createLogger({ name: 'mcp' });*/export function resolveProjectId(context: McpAuthContext,- inputProjectId: string | undefined,+ inputProjectId: string | undefined): Promise<string> {return resolveClientProjectId({clientType: context.clientType,@@ -37,7 +37,6 @@ export function projectIdSchema(context: McpAuthContext) {: z.string().optional();}-/*** Zod schema for common date range inputs. Both fields are optional and* default to the last 30 days when omitted.@@ -118,7 +117,9 @@ export interface TableResult {}function isTableResult(value: unknown): value is TableResult {- if (value === null || typeof value !== 'object') return false;+ if (value === null || typeof value !== 'object') {+ return false;+ }const candidate = value as Partial<TableResult>;return Array.isArray(candidate.columns) && Array.isArray(candidate.rows);}@@ -207,7 +208,9 @@ export function table<T extends object>(let total = 0;for (const row of tail) {const value = row[column];- if (typeof value === 'number' && Number.isFinite(value)) total += value;+ if (typeof value === 'number' && Number.isFinite(value)) {+ total += value;+ }}totals.set(column, total);}@@ -215,7 +218,9 @@ export function table<T extends object>(const [labelColumn] = columns;out.push(columns.map((column) => {- if (column === labelColumn) return `(other: ${tail.length} ${unit})`;+ if (column === labelColumn) {+ return `(other: ${tail.length} ${unit})`;+ }return totals.has(column) ? totals.get(column) : null;}));@@ -237,9 +242,13 @@ function shrinkLargestTable(payload: unknown): boolean {let largest: TableResult | undefined;const visit = (node: unknown) => {- if (node === null || typeof node !== 'object') return;+ if (node === null || typeof node !== 'object') {+ return;+ }if (isTableResult(node)) {- if (!largest || node.rows.length > largest.rows.length) largest = node;+ if (!largest || node.rows.length > largest.rows.length) {+ largest = node;+ }return;}for (const value of Object.values(node as Record<string, unknown>)) {@@ -248,7 +257,9 @@ function shrinkLargestTable(payload: unknown): boolean {};visit(payload);- if (!largest || largest.rows.length <= 1) return false;+ if (!largest || largest.rows.length <= 1) {+ return false;+ }const kept = Math.max(1, Math.floor(largest.rows.length / 2));largest.rows = largest.rows.slice(0, kept);diff --git a/packages/queue/package.json b/packages/queue/package.jsonindex 7023f98df..88a5ca367 100644--- a/packages/queue/package.json+++ b/packages/queue/package.json@@ -7,8 +7,8 @@"typecheck": "tsc --noEmit"},"dependencies": {+ "@openpanel/core": "workspace:*","@openpanel/db": "workspace:*",- "@openpanel/logger": "workspace:*","@openpanel/redis": "workspace:*","bullmq": "^5.63.0","kafkajs": "^2.2.4"diff --git a/packages/queue/src/kafka.ts b/packages/queue/src/kafka.tsindex 9f4339228..cc575d86e 100644--- a/packages/queue/src/kafka.ts+++ b/packages/queue/src/kafka.ts@@ -1,4 +1,4 @@-import { createLogger } from '@openpanel/logger';+import { createLogger } from '@openpanel/core';import {type Admin,type Consumer,diff --git a/packages/queue/src/queues.ts b/packages/queue/src/queues.tsindex 9e66b2721..e20164668 100644--- a/packages/queue/src/queues.ts+++ b/packages/queue/src/queues.ts@@ -1,10 +1,10 @@+import { createLogger } from '@openpanel/core';import type {IClickhouseSession,IServiceCreateEventPayload,IServiceEvent,Prisma,} from '@openpanel/db';-import { createLogger } from '@openpanel/logger';import { getRedisQueue } from '@openpanel/redis';import { Queue } from 'bullmq';import type { ITrackPayload } from '../../validation';diff --git a/packages/sdks/express/index.ts b/packages/sdks/express/index.tsindex 47bb623f9..96e85e766 100644--- a/packages/sdks/express/index.ts+++ b/packages/sdks/express/index.ts@@ -1,8 +1,7 @@-import { getClientIpFromHeaders } from '@openpanel/common/server/get-client-ip';-import type { NextFunction, Request, Response } from 'express';-+import { getClientIpFromHeaders } from '@openpanel/core';import type { OpenPanelOptions } from '@openpanel/sdk';import { OpenPanel } from '@openpanel/sdk';+import type { NextFunction, Request, Response } from 'express';export * from '@openpanel/sdk';diff --git a/packages/sdks/express/package.json b/packages/sdks/express/package.jsonindex da8b2f5f5..8ea3ed812 100644--- a/packages/sdks/express/package.json+++ b/packages/sdks/express/package.json@@ -12,7 +12,8 @@},"dependencies": {"@openpanel/sdk": "workspace:1.3.1-local",- "@openpanel/common": "workspace:*"+ "@openpanel/common": "workspace:*",+ "@openpanel/core": "workspace:*"},"peerDependencies": {"express": "^4.17.0 || ^5.0.0"diff --git a/packages/trpc/src/rate-limit.ts b/packages/trpc/src/rate-limit.tsindex 0a38869ee..bf9048285 100644--- a/packages/trpc/src/rate-limit.ts+++ b/packages/trpc/src/rate-limit.ts@@ -1,6 +1,6 @@-import { getTrustedIpFromHeaders } from '@openpanel/common/server/get-client-ip';import type { EnforceRateLimit } from '@openpanel/core';-import { LRUCache, getRedisCache } from '@openpanel/redis';+import { getTrustedIpFromHeaders } from '@openpanel/core';+import { getRedisCache, LRUCache } from '@openpanel/redis';import { TRPCError } from '@trpc/server';const SECOND = 1000;@@ -69,7 +69,7 @@ function formatDuration(ms: number): string {*/export function getRateLimitIdentity(headers: Headers,- remoteAddress: string | undefined,+ remoteAddress: string | undefined) {const { ip, header } = getTrustedIpFromHeaders(headers, remoteAddress);@@ -92,7 +92,11 @@ function tooManyRequests(blockMs: number): TRPCError {* Record a strike and (re)arm the block. Returns the new strike count and how* long the client is locked out for.*/-async function escalate(strikeKey: string, blockKey: string, cooldownKey: string) {+async function escalate(+ strikeKey: string,+ blockKey: string,+ cooldownKey: string+) {const redis = getRedisCache();// Counter and its expiry go out together: a strike key that lost its TTL@@ -135,7 +139,10 @@ export const enforceRateLimit: EnforceRateLimit = async ({max,windowMs,}) => {- const { fingerprint, ipHeader } = getRateLimitIdentity(headers, remoteAddress);+ const { fingerprint, ipHeader } = getRateLimitIdentity(+ headers,+ remoteAddress+ );const counterKey = key('count', path, fingerprint);const strikeKey = key('strike', path, fingerprint);@@ -144,7 +151,7 @@ export const enforceRateLimit: EnforceRateLimit = async ({const log = (message: string,- payload: { strikes: number; blockMs: number; hits?: number },+ payload: { strikes: number; blockMs: number; hits?: number }) =>logger.warn({@@ -159,7 +166,7 @@ export const enforceRateLimit: EnforceRateLimit = async ({max,windowMs,},- message,+ message);let blockTtlMs: number;@@ -208,7 +215,7 @@ export const enforceRateLimit: EnforceRateLimit = async ({'1','PX',ESCALATION_COOLDOWN_MS,- 'NX',+ 'NX');if (acquired) {diff --git a/packages/trpc/src/routers/auth.ts b/packages/trpc/src/routers/auth.tsindex 715d82af7..f511a6c60 100644--- a/packages/trpc/src/routers/auth.ts+++ b/packages/trpc/src/routers/auth.ts@@ -20,8 +20,8 @@ import {verifyPasswordHash,verifyTotpCode,} from '@openpanel/auth';-import { generateSecureId } from '@openpanel/common/server';import type { Logger } from '@openpanel/core';+import { generateSecureId } from '@openpanel/core';import {connectUserToOrganization,db,diff --git a/packages/trpc/src/routers/client.ts b/packages/trpc/src/routers/client.tsindex 75e2fefa5..05015cb6c 100644--- a/packages/trpc/src/routers/client.ts+++ b/packages/trpc/src/routers/client.ts@@ -1,10 +1,8 @@import crypto from 'node:crypto';-import { z } from 'zod';-+import { hashPassword } from '@openpanel/core';import type { Prisma } from '@openpanel/db';import { db } from '@openpanel/db';--import { hashPassword } from '@openpanel/common/server';+import { z } from 'zod';import { getClientAccess, requireOrganizationAdmin } from '../access';import { TRPCForbiddenError } from '../errors';import { createTRPCRouter, protectedProcedure } from '../trpc';@@ -14,7 +12,7 @@ export const clientRouter = createTRPCRouter({.input(z.object({projectId: z.string(),- }),+ })).query(async ({ input }) => {return db.client.findMany({@@ -28,7 +26,7 @@ export const clientRouter = createTRPCRouter({z.object({id: z.string(),name: z.string(),- }),+ })).mutation(async ({ input, ctx }) => {const access = await getClientAccess({@@ -56,7 +54,7 @@ export const clientRouter = createTRPCRouter({projectId: z.string(),organizationId: z.string(),type: z.enum(['read', 'write', 'root']).optional(),- }),+ })).mutation(async ({ input, ctx }) => {// Minting an ingestion credential - a `root` one at the caller's choosing@@ -87,7 +85,7 @@ export const clientRouter = createTRPCRouter({.input(z.object({id: z.string(),- }),+ })).mutation(async ({ input, ctx }) => {const client = await db.client.findUnique({diff --git a/packages/trpc/src/routers/onboarding.ts b/packages/trpc/src/routers/onboarding.tsindex 94eb5728e..ae72a131e 100644--- a/packages/trpc/src/routers/onboarding.ts+++ b/packages/trpc/src/routers/onboarding.ts@@ -1,18 +1,16 @@import crypto from 'node:crypto';-import type { z } from 'zod';-import { stripTrailingSlash } from '@openpanel/common';-import { db, getId, getOrganizationById, getUserById } from '@openpanel/db';+import { hashPassword } from '@openpanel/core';import type { IServiceUser, ProjectType } from '@openpanel/db';+import { db, getId, getOrganizationById, getUserById } from '@openpanel/db';import { zOnboardingProject } from '@openpanel/validation';--import { hashPassword } from '@openpanel/common/server';import { addDays } from 'date-fns';+import type { z } from 'zod';import { createTRPCRouter, protectedProcedure, publicProcedure } from '../trpc';async function createOrGetOrganization(input: z.infer<typeof zOnboardingProject>,- user: IServiceUser,+ user: IServiceUser) {if (input.organizationId) {return await getOrganizationById(input.organizationId);@@ -94,9 +92,15 @@ export const onboardingRouter = createTRPCRouter({.input(zOnboardingProject).mutation(async ({ input, ctx }) => {const types: ProjectType[] = [];- if (input.website) types.push('website');- if (input.app) types.push('app');- if (input.backend) types.push('backend');+ if (input.website) {+ types.push('website');+ }+ if (input.app) {+ types.push('app');+ }+ if (input.backend) {+ types.push('backend');+ }const user = await getUserById(ctx.session.userId);const organization = await createOrGetOrganization(input, user);diff --git a/packages/trpc/src/routers/organization.ts b/packages/trpc/src/routers/organization.tsindex 806f3f447..2183cb6cd 100644--- a/packages/trpc/src/routers/organization.ts+++ b/packages/trpc/src/routers/organization.ts@@ -1,5 +1,4 @@-import { z } from 'zod';-+import { generateSecureId } from '@openpanel/core';import {connectUserToOrganization,db,@@ -9,17 +8,16 @@ import {getOrganizationById,getOrganizations,} from '@openpanel/db';+import { sendEmail } from '@openpanel/email';import {zEditOrganization,zInviteUser,zUpdateMemberAccess,} from '@openpanel/validation';--import { generateSecureId } from '@openpanel/common/server';-import { sendEmail } from '@openpanel/email';import { addDays, addHours } from 'date-fns';+import { z } from 'zod';import { getOrganizationAccess } from '../access';-import { TRPCForbiddenError, TRPCBadRequestError } from '../errors';+import { TRPCBadRequestError, TRPCForbiddenError } from '../errors';import {createTRPCRouter,protectedProcedure,@@ -49,7 +47,9 @@ export const organizationRouter = createTRPCRouter({userId: ctx.session.userId,organizationId: input.organizationId,});- if (!access) return null;+ if (!access) {+ return null;+ }return { role: access.role };}),@@ -85,7 +85,9 @@ export const organizationRouter = createTRPCRouter({});if (access?.role !== 'org:admin') {- throw new TRPCForbiddenError('You do not have access to this organization');+ throw new TRPCForbiddenError(+ 'You do not have access to this organization'+ );}const organization = await getOrganizationById(input.organizationId);@@ -95,7 +97,7 @@ export const organizationRouter = createTRPCRouter({// cancelled (the subscription is scheduled to end), deletion is allowed.if (organization.hasSubscription && !organization.isWillBeCanceled) {throw new TRPCBadRequestError(- 'Please cancel your subscription before deleting this organization.',+ 'Please cancel your subscription before deleting this organization.');}@@ -135,7 +137,9 @@ export const organizationRouter = createTRPCRouter({});if (access?.role !== 'org:admin') {- throw new TRPCForbiddenError('You do not have access to this organization');+ throw new TRPCForbiddenError(+ 'You do not have access to this organization'+ );}await db.$transaction([@@ -191,7 +195,7 @@ export const organizationRouter = createTRPCRouter({if (alreadyMember && userExists) {throw new TRPCBadRequestError(- 'User is already a member of the organization',+ 'User is already a member of the organization');}@@ -204,7 +208,7 @@ export const organizationRouter = createTRPCRouter({if (alreadyInvited) {throw new TRPCBadRequestError(- 'User is already invited to the organization',+ 'User is already invited to the organization');}@@ -256,7 +260,7 @@ export const organizationRouter = createTRPCRouter({.input(z.object({inviteId: z.string(),- }),+ })).mutation(async ({ input, ctx }) => {const invite = await db.invite.findUniqueOrThrow({@@ -287,7 +291,7 @@ export const organizationRouter = createTRPCRouter({organizationId: z.string(),userId: z.string(),id: z.string(),- }),+ })).mutation(async ({ input, ctx }) => {const exists = await db.member.count({@@ -395,7 +399,7 @@ export const organizationRouter = createTRPCRouter({rateLimitMiddleware({max: 5,windowMs: 30_000,- }),+ })).input(z.object({ inviteId: z.string().optional() })).query(async ({ input }) => {diff --git a/packages/trpc/src/routers/project.ts b/packages/trpc/src/routers/project.tsindex 448a3c01e..0fcead2e6 100644--- a/packages/trpc/src/routers/project.ts+++ b/packages/trpc/src/routers/project.ts@@ -1,6 +1,6 @@import crypto from 'node:crypto';import { stripTrailingSlash } from '@openpanel/common';-import { hashPassword } from '@openpanel/common/server';+import { hashPassword } from '@openpanel/core';import {db,getClientByIdCached,@@ -19,7 +19,7 @@ import {requireProjectAccess,requireProjectAdmin,} from '../access';-import { TRPCForbiddenError, TRPCBadRequestError } from '../errors';+import { TRPCBadRequestError, TRPCForbiddenError } from '../errors';import { createTRPCRouter, protectedProcedure } from '../trpc';export const projectRouter = createTRPCRouter({@@ -161,7 +161,9 @@ export const projectRouter = createTRPCRouter({});if (access?.role !== 'org:admin') {- throw new TRPCForbiddenError('Only organization admins can create projects');+ throw new TRPCForbiddenError(+ 'Only organization admins can create projects'+ );}const secret = `sec_${crypto.randomBytes(10).toString('hex')}`;@@ -260,7 +262,7 @@ export const projectRouter = createTRPCRouter({// level. Cancelling it here would leave the organization unable to delete.if (project?.organization?.deleteAt) {throw new TRPCBadRequestError(- 'This organization is scheduled for deletion. Cancel the deletion from the organization settings.',+ 'This organization is scheduled for deletion. Cancel the deletion from the organization settings.');}diff --git a/packages/validation/src/integrations.ts b/packages/validation/src/integrations.tsindex ee8afaa09..c9518ff02 100644--- a/packages/validation/src/integrations.ts+++ b/packages/validation/src/integrations.ts@@ -6,7 +6,7 @@ import { z } from 'zod';/*** Prefix marking an at-rest ciphertext. Mirrors ENCRYPTION_PREFIX in- * `@openpanel/common/server/encryption.ts`, duplicated because this package is+ * `@openpanel/core/encryption.ts`, duplicated because this package is* bundled for the browser and must not pull in the node-only crypto module.** Credentials are WRITE-ONLY: the API redacts them on read, so a client never@@ -232,7 +232,7 @@ export const zGCSExportConfig = z.object({message: `Invalid service account key: ${result.error}`,});}- },+ }),});export type IGCSExportConfig = z.infer<typeof zGCSExportConfig>;@@ -325,7 +325,11 @@ export const appDescriptor = {kinds: ['notification'],setup: 'form',configSchema: zAppConfig,- catalog: { name: 'Website', description: 'In-app notifications', hidden: true },+ catalog: {+ name: 'Website',+ description: 'In-app notifications',+ hidden: true,+ },} as const satisfies IIntegrationDescriptor<'app', typeof zAppConfig>;export const emailDescriptor = {@@ -346,7 +350,10 @@ export const s3ExportDescriptor = {description:'Export events to Amazon S3 for loading into Redshift, Snowflake, Athena, or other data warehouses.',},-} as const satisfies IIntegrationDescriptor<'s3_export', typeof zS3ExportConfig>;+} as const satisfies IIntegrationDescriptor<+ 's3_export',+ typeof zS3ExportConfig+>;export const gcsExportDescriptor = {type: 'gcs_export',@@ -374,12 +381,12 @@ export const INTEGRATION_DESCRIPTORS = [] as const;const descriptorByType = new Map(- INTEGRATION_DESCRIPTORS.map((d) => [d.type, d] as const),+ INTEGRATION_DESCRIPTORS.map((d) => [d.type, d] as const));export function isKind(config: Pick<IIntegrationConfig, 'type'> | { type?: string },- kind: IIntegrationKind,+ kind: IIntegrationKind): boolean {// Lenient lookup: used as a filter predicate over all integrations, including// rows whose config is still empty (e.g. a Slack integration before its OAuth@@ -426,13 +433,13 @@ export const zCreateDiscordIntegration = zCreateIntegrationBase.extend({export const zCreateS3ExportIntegration = zCreateIntegrationBase.merge(z.object({config: zS3ExportConfig,- }),+ }));export const zCreateGCSExportIntegration = zCreateIntegrationBase.merge(z.object({config: zGCSExportConfig,- }),+ }));// ---------------------------------------------------------------------------@@ -442,11 +449,7 @@ export const zCreateGCSExportIntegration = zCreateIntegrationBase.merge(// ---------------------------------------------------------------------------type Assert<T extends true> = T;-type Equal<A, B> = [A] extends [B]- ? [B] extends [A]- ? true- : false- : false;+type Equal<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;// Every member of the union must keep a *literal* `type` discriminant. If any// widens to { type: string }, this drops it and the equality fails.diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 83b76e392..d560be9a9 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -152,9 +152,6 @@ importers:'@openpanel/json':specifier: workspace:*version: link:../../packages/json- '@openpanel/logger':- specifier: workspace:*- version: link:../../packages/logger'@openpanel/mcp':specifier: workspace:*version: link:../../packages/mcp@@ -966,6 +963,9 @@ importers:'@openpanel/common':specifier: workspace:*version: link:../../packages/common+ '@openpanel/core':+ specifier: workspace:*+ version: link:../../packages/core'@openpanel/db':specifier: workspace:*version: link:../../packages/db@@ -981,9 +981,6 @@ importers:'@openpanel/js-runtime':specifier: workspace:*version: link:../../packages/js-runtime- '@openpanel/logger':- specifier: workspace:*- version: link:../../packages/logger'@openpanel/payments':specifier: workspace:*version: link:../../packages/payments@@ -1122,12 +1119,6 @@ importers:date-fns:specifier: ^3.3.1version: 3.3.1- ipaddr.js:- specifier: ^2.5.0- version: 2.5.0- lru-cache:- specifier: ^11.2.4- version: 11.2.4luxon:specifier: ^3.7.2version: 3.7.2@@ -1146,12 +1137,6 @@ importers:superjson:specifier: ^1.13.3version: 1.13.3- ua-parser-js:- specifier: ^2.0.6- version: 2.0.6- undici:- specifier: ^8.10.0- version: 8.10.0unique-names-generator:specifier: ^4.7.1version: 4.7.1@@ -1171,9 +1156,6 @@ importers:'@types/ramda':specifier: ^0.29.6version: 0.29.10- '@types/ua-parser-js':- specifier: ^0.7.39- version: 0.7.39prisma:specifier: ^5.1.1version: 5.9.1@@ -1201,6 +1183,9 @@ importers:'@elysiajs/openapi':specifier: 1.4.15version: 1.4.15([email protected](@sinclair/[email protected])([email protected])([email protected]([email protected]))([email protected])([email protected]))+ '@hyperdx/node-opentelemetry':+ specifier: 'catalog:'+ version: 0.10.3([email protected])'@openpanel/auth':specifier: workspace:^version: link:../auth@@ -1216,15 +1201,33 @@ importers:elysia:specifier: 1.4.30version: 1.4.30(@sinclair/[email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])+ ipaddr.js:+ specifier: ^2.5.0+ version: 2.5.0+ lru-cache:+ specifier: ^11.2.4+ version: 11.5.1nanoid:specifier: ^5.1.6version: 5.1.6+ pino:+ specifier: 'catalog:'+ version: 10.3.1+ pino-pretty:+ specifier: 'catalog:'+ version: 13.1.3prom-client:specifier: ^15.1.3version: 15.1.3superjson:specifier: ^1.13.3version: 1.13.3+ ua-parser-js:+ specifier: ^2.0.6+ version: 2.0.6+ undici:+ specifier: ^8.10.0+ version: 8.10.0zod:specifier: 'catalog:'version: 4.3.6@@ -1250,12 +1253,12 @@ importers:'@openpanel/constants':specifier: workspace:*version: link:../constants+ '@openpanel/core':+ specifier: workspace:*+ version: link:../core'@openpanel/json':specifier: workspace:*version: link:../json- '@openpanel/logger':- specifier: workspace:*- version: link:../logger'@openpanel/payments':specifier: workspace:*version: link:../payments@@ -1406,6 +1409,9 @@ importers:'@openpanel/common':specifier: workspace:*version: link:../common+ '@openpanel/core':+ specifier: workspace:*+ version: link:../core'@openpanel/db':specifier: workspace:*version: link:../db@@ -1431,9 +1437,6 @@ importers:specifier: 'catalog:'version: 4.3.6devDependencies:- '@openpanel/logger':- specifier: workspace:*- version: link:../logger'@types/node':specifier: ^20.0.0version: 20.19.24@@ -1470,12 +1473,12 @@ importers:'@openpanel/common':specifier: workspace:*version: link:../common+ '@openpanel/core':+ specifier: workspace:*+ version: link:../core'@openpanel/js-runtime':specifier: workspace:*version: link:../js-runtime- '@openpanel/logger':- specifier: workspace:*- version: link:../logger'@openpanel/validation':specifier: workspace:*version: link:../validation@@ -1531,31 +1534,6 @@ importers:specifier: 'catalog:'version: 5.9.3- packages/logger:- dependencies:- '@hyperdx/node-opentelemetry':- specifier: 'catalog:'- version: 0.10.3([email protected])- pino:- specifier: 'catalog:'- version: 10.3.1- pino-pretty:- specifier: 'catalog:'- version: 13.1.3- devDependencies:- '@openpanel/tsconfig':- specifier: workspace:*- version: link:../../tooling/typescript- date-fns:- specifier: ^3.3.1- version: 3.3.1- prisma:- specifier: ^5.1.1- version: 5.9.1- typescript:- specifier: 'catalog:'- version: 5.9.3-packages/mcp:dependencies:'@modelcontextprotocol/sdk':@@ -1564,12 +1542,12 @@ importers:'@openpanel/common':specifier: workspace:*version: link:../common+ '@openpanel/core':+ specifier: workspace:*+ version: link:../core'@openpanel/db':specifier: workspace:*version: link:../db- '@openpanel/logger':- specifier: workspace:*- version: link:../logger'@openpanel/redis':specifier: workspace:*version: link:../redis@@ -1632,12 +1610,12 @@ importers:packages/queue:dependencies:+ '@openpanel/core':+ specifier: workspace:*+ version: link:../core'@openpanel/db':specifier: workspace:*version: link:../db- '@openpanel/logger':- specifier: workspace:*- version: link:../logger'@openpanel/redis':specifier: workspace:*version: link:../redis@@ -1720,6 +1698,9 @@ importers:'@openpanel/common':specifier: workspace:*version: link:../../common+ '@openpanel/core':+ specifier: workspace:*+ version: link:../../core'@openpanel/sdk':specifier: workspace:1.3.1-localversion: link:../sdk@@ -10054,9 +10035,6 @@ packages:'@types/[email protected]':resolution: {integrity: sha512-cy7BRSU8GYYgxjcx0Py+8lo5MthuDhlyu076KUcYzVNXL23luYgRHkMG2fIFEc6neckeh/ntP82mw+U4QjZq+g==}- '@types/[email protected]':- resolution: {integrity: sha512-P/oDfpofrdtF5xw433SPALpdSchtJmY7nsJItf8h3KXqOslkbySh8zq4dSWXH2oTjRvJ5PczVEoCZPow6GicLg==}-'@types/[email protected]':resolution: {integrity: sha512-TU+fZFBTBcXj/GpDpDaBmgWk/gn96kMZ+uocaFUlV2f8a6WdMzzI44QBCmGcCiYR0Y6ZlNRiyUyKKt5nl/lbzQ==}@@ -14792,10 +14770,6 @@ packages:resolution: {integrity: sha512-F9ODfyqML2coTIsQpSkRHnLSZMtkU8Q+mSfcaIyKwy58u+8k5nvAYeiNhsyMARvzNcXJ9QfWVrcPsC9e9rAxtg==}engines: {node: 20 || >=22}- resolution: {integrity: sha512-B5Y16Jr9LB9dHVkh6ZevG+vAbOsNOYCX+sXvFWFu7B3Iz5mijW3zdbMyhsh8ANd2mSWBYdJgnqi+mL7/LrOPYg==}- engines: {node: 20 || >=22}-resolution: {integrity: sha512-RPimw/7aMdv2oqRrxKwvZXcPfwBrn/JZ2xYcY9Hus/6LaS3VOAKVWKWgNLCFSiOm1ESXinjsDlidVU7JlnCN2A==}engines: {node: 20 || >=22}@@ -30469,8 +30443,6 @@ snapshots:'@types/[email protected]': {}- '@types/[email protected]': {}-'@types/[email protected]':dependencies:source-map: 0.6.1@@ -36417,8 +36389,6 @@ snapshots:- [email protected]: {}-