Compare commits

...

23 Commits

Author SHA1 Message Date
Palanikannan M
e4f31aea08 Merge branch 'preview' into devin/1734544044-refactor-live-server 2025-03-19 16:01:26 +05:30
Palanikannan M
c2a3e47d3d fix: stop event prop on error 2025-03-19 16:01:09 +05:30
Palanikannan M
cef4110eb0 fix: handlers 2025-03-19 14:01:19 +05:30
Palanikannan M
3672ee4ef1 fix: errors and imports 2025-03-18 19:25:31 +05:30
Palanikannan M
c56097b8c0 fix: better error handling for redis client 2025-03-18 18:56:50 +05:30
Palanikannan M
0d57e0ab32 fix: file structure for error handling 2025-03-18 17:32:46 +05:30
Palanikannan M
df35ccecc9 fix: better error handling 2025-03-18 16:59:04 +05:30
Palanikannan M
38d8d3ea9b fix: dividing server code 2025-03-18 01:36:14 +05:30
Palanikannan M
3710b182d3 fix: tsup hot reloading 2025-03-18 00:16:30 +05:30
Palanikannan M
6897575a62 fix: logger and added working global error handling with sentry 2025-03-17 23:24:03 +05:30
Palanikannan M
388151b70b Merge branch 'preview' into devin/1734544044-refactor-live-server 2025-03-17 15:52:49 +05:30
Palanikannan M
3d61604569 Merge branch 'preview' into devin/1734544044-refactor-live-server 2025-02-08 20:32:50 +05:30
Palanikannan M
1b29f65664 fix: removed .js imports 2024-12-23 18:16:53 +05:30
Palanikannan M
a229508611 Merge branch 'preview' into devin/1734544044-refactor-live-server 2024-12-23 17:23:59 +05:30
Devin AI
d5bd4ef63a chore: switch from esbuild to tsup for build configuration
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-19 07:49:27 +00:00
Devin AI
146332fff3 chore: replace babel with esbuild for build configuration
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-19 07:42:19 +00:00
Devin AI
5802858772 fix: resolve typescript errors in server and decorators
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-18 18:05:01 +00:00
Devin AI
b39ce9c18a fix: add eslint config and fix websocket router interface
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-18 18:03:29 +00:00
Devin AI
a7ab5ae680 fix: resolve lint errors in decorators and collaboration controller
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-18 18:02:48 +00:00
Devin AI
f2a08853e2 feat: add start.ts entry point for live server
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-18 18:01:12 +00:00
Devin AI
23eeb45713 feat: implement collaboration controller with websocket support
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-18 17:54:15 +00:00
Devin AI
6c83a0df09 feat: implement health check controller with decorator support
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-18 17:52:40 +00:00
Devin AI
dbee7488e1 feat: add decorator system and controller registration for live server
Co-Authored-By: sriram@plane.so <sriram@plane.so>
2024-12-18 17:50:52 +00:00
52 changed files with 7272 additions and 4451 deletions

View File

@@ -3,13 +3,15 @@
"version": "0.25.2",
"license": "AGPL-3.0",
"description": "A realtime collaborative server powers Plane's rich text editor",
"main": "./src/server.ts",
"main": "./dist/start.js",
"module": "./dist/start.mjs",
"types": "./dist/start.d.ts",
"private": true,
"type": "module",
"scripts": {
"dev": "PORT=3100 concurrently \"babel src --out-dir dist --extensions '.ts,.js' --watch\" \"nodemon dist/server.js\"",
"build": "babel src --out-dir dist --extensions \".ts,.js\"",
"start": "node dist/server.js",
"dev": "tsup --watch --onSuccess 'node --env-file=.env dist/start.js'",
"build": "tsup",
"start": "node --env-file=.env dist/start.js",
"lint": "eslint src --ext .ts,.tsx",
"lint:errors": "eslint src --ext .ts,.tsx --quiet"
},
@@ -23,14 +25,16 @@
"@plane/constants": "*",
"@plane/editor": "*",
"@plane/types": "*",
"@sentry/node": "^9.0.1",
"@sentry/profiling-node": "^8.28.0",
"@sentry/node": "^9.5.0",
"@sentry/profiling-node": "^9.5.0",
"@sentry/core": "^9.5.0",
"@tiptap/core": "2.10.4",
"@tiptap/html": "2.11.0",
"axios": "^1.8.3",
"compression": "^1.7.4",
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"express-ws": "^5.0.2",
"helmet": "^7.1.0",
@@ -39,27 +43,26 @@
"morgan": "^1.10.0",
"pino-http": "^10.3.0",
"pino-pretty": "^11.2.2",
"reflect-metadata": "^0.2.2",
"uuid": "^10.0.0",
"y-prosemirror": "^1.2.15",
"y-protocols": "^1.0.6",
"yjs": "^13.6.20"
"yjs": "^13.6.20",
"zod": "^3.24.2",
"@plane/logger": "*"
},
"devDependencies": {
"@babel/cli": "^7.25.6",
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@types/compression": "^1.7.5",
"@types/cookie-parser": "^1.4.8",
"@types/cors": "^2.8.17",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.21",
"@types/express-ws": "^3.0.4",
"@types/node": "^20.14.9",
"babel-plugin-module-resolver": "^5.0.2",
"@types/reflect-metadata": "^0.1.0",
"concurrently": "^9.0.1",
"nodemon": "^3.1.7",
"ts-node": "^10.9.2",
"tsup": "^8.4.0",
"tsup": "8.3.0",
"typescript": "5.3.3"
}
}

View File

@@ -1,14 +1,25 @@
// types
import { TDocumentTypes } from "@/core/types/common.js";
import { AppError, catchAsync } from "@/core/helpers/error-handling/error-handler";
import { TDocumentTypes } from "@/core/types/common";
type TArgs = {
cookie: string | undefined;
documentType: TDocumentTypes | undefined;
interface TArgs {
cookie: string;
documentType: TDocumentTypes;
pageId: string;
params: URLSearchParams;
}
export const fetchDocument = async (args: TArgs): Promise<Uint8Array | null> => {
const { documentType } = args;
throw Error(`Fetch failed: Invalid document type ${documentType} provided.`);
}
const { cookie, documentType, pageId, params } = args;
if (!documentType) {
throw new AppError("Document type is required");
}
if (!pageId) {
throw new AppError("Page ID is required");
}
return null
};

View File

@@ -1,5 +1,5 @@
// types
import { TDocumentTypes } from "@/core/types/common.js";
import { TDocumentTypes } from "@/core/types/common";
type TArgs = {
cookie: string | undefined;

View File

@@ -1 +1 @@
export type TAdditionalDocumentTypes = {};
export type TAdditionalDocumentTypes = string;

View File

@@ -0,0 +1,66 @@
import { env } from "@/env";
import compression from "compression";
import helmet from "helmet";
import cors from "cors";
import cookieParser from "cookie-parser";
import express from "express";
import { logger } from "@plane/logger";
import { logger as loggerMiddleware } from "@/core/helpers/logger";
/**
* Configure server middleware
* @param app Express application
*/
export function configureServerMiddleware(app: express.Application): void {
// Security middleware
app.use(helmet());
// CORS configuration
configureCors(app);
// Compression middleware
app.use(
compression({
level: env.COMPRESSION_LEVEL,
threshold: env.COMPRESSION_THRESHOLD,
}) as unknown as express.RequestHandler
);
// Cookie parsing
app.use(cookieParser());
// Logging middleware
app.use(loggerMiddleware);
// Body parsing middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
}
/**
* Configure CORS
* @param app Express application
*/
function configureCors(app: express.Application): void {
const origins = env.CORS_ALLOWED_ORIGINS?.split(",").map((origin) => origin.trim()) || [];
for (const origin of origins) {
logger.info(`Adding CORS allowed origin: ${origin}`);
app.use(
cors({
origin,
credentials: true,
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization", "x-api-key"],
})
);
}
}
/**
* Server configuration
*/
export const serverConfig = {
port: env.PORT,
basePath: env.LIVE_BASE_PATH,
shutdownTimeout: env.SHUTDOWN_TIMEOUT,
};

View File

@@ -0,0 +1,99 @@
import type { Request } from "express";
import type { WebSocket as WS } from "ws";
import type { Hocuspocus } from "@hocuspocus/server";
import { Controller, WebSocket } from "@/lib/decorators";
import { BaseWebSocketController } from "@/lib/base.controller";
import { ErrorCategory } from "@/core/helpers/error-handling/error-handler";
import { logger } from "@plane/logger";
import Errors from "@/core/helpers/error-handling/error-factory";
@Controller("/collaboration")
export class CollaborationController extends BaseWebSocketController {
private metrics = {
errors: 0,
};
constructor(private readonly hocusPocusServer: Hocuspocus) {
super();
}
@WebSocket("/")
handleConnection(ws: WS, req: Request) {
const clientInfo = {
ip: req.ip,
userAgent: req.get("user-agent"),
requestId: req.id || crypto.randomUUID(),
};
try {
// Initialize the connection with Hocuspocus
this.hocusPocusServer.handleConnection(ws, req);
// Set up error handling for the connection
ws.on("error", (error) => {
this.handleConnectionError(error, clientInfo, ws);
});
} catch (error) {
this.handleConnectionError(error, clientInfo, ws);
}
}
private handleConnectionError(error: unknown, clientInfo: Record<string, any>, ws: WS) {
// Convert to AppError if needed
const appError = Errors.convertError(error instanceof Error ? error : new Error(String(error)), {
context: {
...clientInfo,
component: "WebSocketConnection",
},
});
// Log at appropriate level based on error category
if (appError.category === ErrorCategory.OPERATIONAL) {
logger.info(`WebSocket operational error: ${appError.message}`, {
error: appError,
clientInfo,
});
} else {
logger.error(`WebSocket error: ${appError.message}`, {
error: appError,
clientInfo,
stack: appError.stack,
});
}
// Alert if error threshold is reached
if (this.metrics.errors % 10 === 0) {
logger.warn(`High WebSocket error rate detected: ${this.metrics.errors} total errors`);
}
// Try to send error to client before closing
try {
if (ws.readyState === ws.OPEN) {
ws.send(
JSON.stringify({
type: "error",
message: appError.category === ErrorCategory.OPERATIONAL ? appError.message : "Internal server error",
})
);
}
} catch (sendError) {
// Ignore send errors at this point
}
// Close with informative message if connection is still open
if (ws.readyState === ws.OPEN) {
ws.close(
1011,
appError.category === ErrorCategory.OPERATIONAL
? `Error: ${appError.message}. Reconnect with exponential backoff.`
: "Internal server error. Please retry in a few moments."
);
}
}
getErrorMetrics() {
return {
errors: this.metrics.errors,
};
}
}

View File

@@ -0,0 +1,39 @@
import type { Request, Response } from "express";
// helpers
import { convertHTMLDocumentToAllFormats } from "@/core/helpers/convert-document";
// types
import { TConvertDocumentRequestBody } from "@/core/types/common";
// controller
import { BaseController } from "@/lib/base.controller";
// decorators
import { Post, CatchErrors } from "@/lib/decorators";
// logger
import { logger } from "@plane/logger";
// error handling
import validate from "@/core/helpers/error-handling/error-validation";
export class DocumentController extends BaseController {
@Post("/convert-document")
@CatchErrors()
async convertDocument(req: Request, res: Response) {
const { description_html, variant } = req.body as TConvertDocumentRequestBody;
// Validate input parameters using our new validation utility
validate(description_html, "description_html").required().string();
validate(variant, "variant").required().string();
logger.info("Converting document", { variant });
// Process document conversion
const { description, description_binary } = convertHTMLDocumentToAllFormats({
document_html: description_html,
variant,
});
// Return successful response
res.status(200).json({
description,
description_binary,
});
}
}

View File

@@ -0,0 +1,16 @@
import type { Request, Response } from "express";
import { Controller, Get, CatchErrors } from "@/lib/decorators";
import { BaseController } from "@/lib/base.controller";
@Controller("/health")
export class HealthController extends BaseController {
@Get("/")
@CatchErrors()
async healthCheck(_req: Request, res: Response) {
res.status(200).json({
status: "OK",
timestamp: new Date().toISOString(),
version: process.env.APP_VERSION || '1.0.0'
});
}
}

View File

@@ -0,0 +1,4 @@
// Export all controllers from this barrel file
export { HealthController } from "./health.controller";
export { CollaborationController } from "./collaboration.controller";
export { DocumentController } from "./document.controller";

View File

@@ -1,19 +0,0 @@
import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
// Ensure to call this before importing any other modules!
Sentry.init({
dsn: process.env.LIVE_SENTRY_DSN,
environment: process.env.LIVE_SENTRY_ENVIRONMENT || "development",
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
],
// Add Tracing by setting tracesSampleRate
// We recommend adjusting this value in production
tracesSampleRate: Number(process.env.LIVE_SENTRY_TRACES_SAMPLE_RATE) || 0.5,
// Set sampling rate for profiling
// This is relative to tracesSampleRate
profilesSampleRate: 1.0,
});

View File

@@ -0,0 +1,58 @@
import { IControllerRegistry, ControllerRegistration, WebSocketControllerRegistration } from "@/lib/controller.interface";
import { createControllerRegistry } from "@/lib/controller.utils";
// Import controllers
import { HealthController } from "@/controllers/health.controller";
import { DocumentController } from "@/controllers/document.controller";
import { CollaborationController } from "@/controllers/collaboration.controller";
/**
* Controller Registry Module
* Centralized place to register all controllers and their dependencies
*/
class ControllerRegistryModule {
// Define controller groups for better organization using registration format
private readonly CONTROLLERS = {
// Core system controllers (health checks, status endpoints)
CORE: [
{ Controller: HealthController }
],
// Document management controllers
DOCUMENT: [
{ Controller: DocumentController }
],
// WebSocket controllers for real-time functionality
WEBSOCKET: [
{ Controller: CollaborationController, dependencies: ['hocuspocus'] }
],
};
/**
* Get all REST controllers
*/
getAllRestControllers(): ControllerRegistration[] {
return [...this.CONTROLLERS.CORE, ...this.CONTROLLERS.DOCUMENT];
}
/**
* Get all WebSocket controllers
*/
getAllWebSocketControllers(): WebSocketControllerRegistration[] {
return this.CONTROLLERS.WEBSOCKET;
}
/**
* Create a controller registry with all configured controllers
*/
createRegistry(): IControllerRegistry {
return createControllerRegistry(
this.getAllRestControllers(),
this.getAllWebSocketControllers()
);
}
}
// Export a singleton instance
export const controllerRegistry = new ControllerRegistryModule();

View File

@@ -1,142 +1,127 @@
// Third-party libraries
import { Redis } from "ioredis";
// Hocuspocus extensions and core
// hocuspocus extensions and core
import { Database } from "@hocuspocus/extension-database";
import { Extension } from "@hocuspocus/server";
import { Logger } from "@hocuspocus/extension-logger";
import { Redis as HocusPocusRedis } from "@hocuspocus/extension-redis";
// core helpers and utilities
import { manualLogger } from "@/core/helpers/logger.js";
import { getRedisUrl } from "@/core/lib/utils/redis-url.js";
// core libraries
import {
fetchPageDescriptionBinary,
updatePageDescription,
} from "@/core/lib/page.js";
// plane live libraries
import { fetchDocument } from "@/plane-live/lib/fetch-document.js";
import { updateDocument } from "@/plane-live/lib/update-document.js";
import { setupRedisExtension } from "@/core/extensions/redis";
// types
import {
type HocusPocusServerContext,
type TDocumentTypes,
} from "@/core/types/common.js";
import { type HocusPocusServerContext, type TDocumentTypes } from "@/core/types/common";
import { logger } from "@plane/logger";
// error
import { catchAsync } from "@/core/helpers/error-handling/error-handler";
import { handleError } from "@/core/helpers/error-handling/error-factory";
// document handlers
import { getDocumentHandler } from "../handlers/document-handlers";
export const getExtensions: () => Promise<Extension[]> = async () => {
const extensions: Extension[] = [
new Logger({
onChange: false,
log: (message) => {
manualLogger.info(message);
logger.info(message);
},
}),
new Database({
fetch: async ({ context, documentName: pageId, requestParameters }) => {
const cookie = (context as HocusPocusServerContext).cookie;
// query params
fetch: async ({
context,
documentName: pageId,
requestParameters,
}: {
context: HocusPocusServerContext;
documentName: TDocumentTypes;
requestParameters: URLSearchParams;
}) => {
const { documentType } = context;
const params = requestParameters;
const documentType = params.get("documentType")?.toString() as
| TDocumentTypes
| undefined;
// TODO: Fix this lint error.
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
try {
let fetchedData = null;
if (documentType === "project_page") {
fetchedData = await fetchPageDescriptionBinary(
params,
pageId,
cookie,
);
} else {
fetchedData = await fetchDocument({
cookie,
documentType,
pageId,
params,
let fetchedData = null;
fetchedData = await catchAsync(
async () => {
if (!documentType) {
handleError(null, {
errorType: 'bad-request',
message: 'Document type is required',
component: 'database-extension',
operation: 'fetch',
extraContext: { pageId },
throw: true
});
}
resolve(fetchedData);
} catch (error) {
manualLogger.error("Error in fetching document", error);
const documentHandler = getDocumentHandler(documentType);
fetchedData = await documentHandler.fetch({
context: context as HocusPocusServerContext,
pageId,
params,
});
if (!fetchedData) {
handleError(null, {
errorType: 'not-found',
message: `Failed to fetch document: ${pageId}`,
component: 'database-extension',
operation: 'fetch',
extraContext: { documentType, pageId }
});
}
return fetchedData;
},
{
params: { pageId, documentType },
extra: { operation: "fetch" },
}
});
)();
return fetchedData;
},
store: async ({
context,
state,
documentName: pageId,
requestParameters,
}: {
context: HocusPocusServerContext;
state: Buffer;
documentName: TDocumentTypes;
requestParameters: URLSearchParams;
}) => {
const cookie = (context as HocusPocusServerContext).cookie;
// query params
const { agentId } = context as HocusPocusServerContext;
const params = requestParameters;
const documentType = params.get("documentType")?.toString() as
| TDocumentTypes
| undefined;
const documentType = params.get("documentType")?.toString() as TDocumentTypes | undefined;
// TODO: Fix this lint error.
// eslint-disable-next-line no-async-promise-executor
return new Promise(async () => {
try {
if (documentType === "project_page") {
await updatePageDescription(params, pageId, state, cookie);
} else {
await updateDocument({
cookie,
documentType,
pageId,
params,
updatedDescription: state,
catchAsync(
async () => {
if (!documentType) {
handleError(null, {
errorType: 'bad-request',
message: 'Document type is required',
component: 'database-extension',
operation: 'store',
extraContext: { pageId },
throw: true
});
}
} catch (error) {
manualLogger.error("Error in updating document:", error);
const documentHandler = getDocumentHandler(documentType, { agentId });
await documentHandler.store({
context: context as HocusPocusServerContext,
pageId,
state,
params,
});
},
{
params: { pageId, documentType },
extra: { operation: "store" },
}
});
)();
},
}),
];
const redisUrl = getRedisUrl();
if (redisUrl) {
try {
const redisClient = new Redis(redisUrl);
await new Promise<void>((resolve, reject) => {
redisClient.on("error", (error: any) => {
if (
error?.code === "ENOTFOUND" ||
error.message.includes("WRONGPASS") ||
error.message.includes("NOAUTH")
) {
redisClient.disconnect();
}
manualLogger.warn(
`Redis Client wasn't able to connect, continuing without Redis (you won't be able to sync data between multiple plane live servers)`,
error,
);
reject(error);
});
redisClient.on("ready", () => {
extensions.push(new HocusPocusRedis({ redis: redisClient }));
manualLogger.info("Redis Client connected ✅");
resolve();
});
});
} catch (error) {
manualLogger.warn(
`Redis Client wasn't able to connect, continuing without Redis (you won't be able to sync data between multiple plane live servers)`,
error,
);
}
} else {
manualLogger.warn(
"Redis URL is not set, continuing without Redis (you won't be able to sync data between multiple plane live servers)",
);
// Set up Redis extension if available
const redisExtension = await setupRedisExtension();
if (redisExtension) {
extensions.push(redisExtension);
}
return extensions;

View File

@@ -0,0 +1,380 @@
import { Redis } from "ioredis";
import { Redis as HocusPocusRedis } from "@hocuspocus/extension-redis";
import { Extension } from "@hocuspocus/server";
// core helpers and utilities
import { getRedisUrl } from "@/core/lib/utils/redis-url";
import { logger } from "@plane/logger";
import { handleError } from "@/core/helpers/error-handling/error-factory";
import { AppError, catchAsync } from "@/core/helpers/error-handling/error-handler";
// Keep a reference to the Redis client for cleanup purposes
let redisClient: Redis | null = null;
// Define a custom error type that includes code property
interface RedisError extends Error {
code?: string;
}
// Circuit breaker to disable Redis after too many failures
let redisCircuitBroken = false;
let redisFailureCount = 0;
const MAX_REDIS_FAILURES = 5;
/**
* Sets up the Redis extension for HocusPocus with proper error handling
* @returns Promise that resolves to a Redis extension if successful, or null if Redis is unavailable
*/
export const setupRedisExtension = async (): Promise<Extension | null> => {
// If circuit breaker is active, don't try to connect to Redis
if (redisCircuitBroken) {
logger.warn("Redis circuit breaker active - not attempting to connect");
return null;
}
const redisUrl = getRedisUrl();
if (!redisUrl) {
logger.warn(
"Redis URL is not set, continuing without Redis (you won't be able to sync data between multiple plane live servers)"
);
return null;
}
return catchAsync(
async () => {
// Clean up any existing Redis client first
if (redisClient) {
try {
await cleanupRedisClient(redisClient);
} catch (err) {
logger.warn("Error cleaning up previous Redis client", err);
}
redisClient = null;
}
// Create new Redis client with proper options
let client: Redis;
try {
client = new Redis(redisUrl, {
maxRetriesPerRequest: 3, // Limit retries to prevent overwhelming logs
retryStrategy: (times) => {
// Stop retrying after too many failures or if circuit breaker engaged
if (times > 10 || redisCircuitBroken) {
return null; // means stop retrying
}
// Backoff strategy with maximum wait time
const delay = Math.min(times * 100, 3000);
return delay;
},
// Special error handling on connection events
disconnectTimeout: 5000,
enableOfflineQueue: false, // Don't queue commands when disconnected
enableReadyCheck: true,
// Custom commands error handling
showFriendlyErrorStack: true,
});
setupRedisErrorHandlers(client);
// Store reference in module-level variable
redisClient = client;
} catch (err) {
logger.error("Failed to create Redis client", err);
incrementFailureCount();
return null;
}
// Wait for Redis connection or error with a timeout
const result = await new Promise<Extension | AppError>((resolve) => {
let hasResolved = false;
const timeoutId = setTimeout(() => {
if (!hasResolved) {
hasResolved = true;
// Example of non-throwing handleError usage
const error = handleError(new Error("Redis connection timeout"), {
errorType: "service-unavailable",
component: "redis-extension",
operation: "connect",
message: "Redis connection timeout, continuing without Redis",
});
logger.warn(error.message);
incrementFailureCount();
// Clean up the client
cleanupRedisClient(client);
resolve(error);
}
}, 5000);
client.once("ready", () => {
if (!hasResolved) {
hasResolved = true;
clearTimeout(timeoutId);
// Reset failure count on successful connection
redisFailureCount = 0;
logger.info("Redis client connected successfully ✅");
try {
// Create extension with error handling
const redisExtension = new HocusPocusRedis({
redis: client,
prefix: "plane:",
});
// Add safe destroy method to the extension
const extension = redisExtension as unknown as {
destroy?: () => Promise<void> | void;
onDestroy?: () => Promise<void> | void;
};
// Store the original destroy method
const originalDestroy = extension.destroy;
// Replace with our safe version
extension.destroy = async () => {
logger.info("Cleaning up Redis extension...");
try {
// Safely clean up the Redis client
await cleanupRedisClient(client);
} catch (err) {
logger.warn("Error cleaning up Redis client during destroy", err);
}
// Call original destroy method if it exists
if (originalDestroy) {
try {
await Promise.resolve(originalDestroy.call(extension));
} catch (err) {
logger.warn("Error in original destroy method", err);
}
}
};
resolve(redisExtension);
} catch (err) {
// Handle errors in extension setup
logger.error("Error creating Redis extension", err);
cleanupRedisClient(client);
incrementFailureCount();
resolve(
handleError(err as Error, {
errorType: "service-unavailable",
component: "redis-extension",
operation: "create-extension",
message: "Failed to create Redis extension",
})
);
}
}
});
client.once("error", (error: RedisError) => {
if (!hasResolved) {
hasResolved = true;
clearTimeout(timeoutId);
// Increment failure count
incrementFailureCount();
// Non-throwing usage with specific error details
const appError = handleError(error, {
errorType: "service-unavailable",
component: "redis-extension",
operation: "connect",
message: `Redis client wasn't able to connect, continuing without Redis`,
extraContext: {
redisUrl: redisUrl.replace(/\/\/.*:.*@/, "//***:***@"), // Mask credentials
errorCode: error?.code,
errorMessage: error?.message,
},
});
logger.warn(appError.message, { errorCode: error?.code });
// Clean up the client
cleanupRedisClient(client);
resolve(appError);
}
});
});
// If result is an error, return null to indicate Redis is not available
if (result instanceof AppError) {
redisClient = null;
return null;
}
return result;
},
{
extra: {
component: "redis-extension",
operation: "setup",
redisUrl: redisUrl.replace(/\/\/.*:.*@/, "//***:***@"), // Mask credentials for logging
},
},
{
defaultValue: null, // Return null if any error occurs
rethrow: false, // Never rethrow, always gracefully continue without Redis
}
)().catch((err) => {
// Extra safety net - catch any errors the catchAsync might miss
logger.error("Uncaught error in Redis setup", err);
return null;
});
};
/**
* Handle multiple failures with circuit breaker pattern
*/
function incrementFailureCount(): void {
redisFailureCount++;
if (redisFailureCount >= MAX_REDIS_FAILURES) {
logger.error(`Redis failures reached threshold (${MAX_REDIS_FAILURES}), activating circuit breaker`);
redisCircuitBroken = true;
// Cleanup any existing client
if (redisClient) {
cleanupRedisClient(redisClient).catch((err) =>
logger.error("Error cleaning up Redis client during circuit break", err)
);
redisClient = null;
}
// Set a timeout to reset the circuit breaker
setTimeout(() => {
logger.info("Redis circuit breaker reset, will try connecting again on next request");
redisCircuitBroken = false;
redisFailureCount = 0;
}, 60000); // Try again after 1 minute
}
}
/**
* Set up global error handlers for the Redis client
* This prevents unhandled errors from crashing the application
*/
function setupRedisErrorHandlers(client: Redis) {
// Capture ALL error events
client.on("error", (err: RedisError) => {
// Only increment failures for certain types of errors
if (
err.message?.includes("ECONNREFUSED") ||
err.message?.includes("ETIMEDOUT") ||
err.message?.includes("EPIPE") ||
err.message?.includes("MaxRetriesPerRequestError") ||
err.code === "ECONNREFUSED"
) {
incrementFailureCount();
}
// Log the error but don't crash
handleError(err, {
errorType: "service-unavailable",
component: "redis-extension",
operation: "ongoing-connection",
message: `Redis error occurred (will attempt to recover)`,
extraContext: {
errorCode: err?.code,
errorMessage: err?.message,
},
});
// Log at warn level so we don't flood logs with errors
logger.warn(`Redis error: ${err.message}`, {
errorCode: err?.code,
stack: err?.stack,
});
});
// Handle specific reconnection errors that might indicate Redis is down
client.on("reconnecting", (time: number) => {
logger.info(`Redis reconnecting after ${time}ms...`);
});
// Handle MaxRetriesPerRequestError specifically
process.on("unhandledRejection", (reason: unknown) => {
// Only handle Redis-related errors
if (
reason instanceof Error &&
(reason.message.includes("MaxRetriesPerRequestError") || reason.message.includes("Redis"))
) {
logger.warn("Caught unhandled Redis-related rejection", {
message: reason.message,
stack: reason.stack,
});
incrementFailureCount();
// Don't let it crash the process
return;
}
// Let other unhandled rejections pass through to the default handler
});
// Handle connection end events
client.on("end", () => {
logger.warn("Redis connection ended");
// This is normal during shutdown, don't increment failure count
});
}
/**
* Safely clean up a Redis client connection
*/
async function cleanupRedisClient(client: Redis | null): Promise<void> {
if (!client) return;
try {
// Remove all listeners to prevent memory leaks
client.removeAllListeners();
// Quit the connection gracefully
if (client.status !== "end") {
await Promise.race([
client.quit().catch(() => {
// If quit fails, force disconnect
client.disconnect();
}),
// Safety timeout in case quit hangs
new Promise<void>((resolve) =>
setTimeout(() => {
client.disconnect();
resolve();
}, 1000)
),
]);
}
} catch (err) {
// Just force disconnect if anything goes wrong
try {
client.disconnect();
} catch (disconnectErr) {
// At this point, we've tried our best
logger.error("Failed to properly disconnect Redis client", disconnectErr);
}
}
}
/**
* Helper to get the current Redis status
* Useful for health checks
*/
export const getRedisStatus = (): "connected" | "connecting" | "disconnected" | "circuit-broken" | "not-configured" => {
if (redisCircuitBroken) return "circuit-broken";
if (!redisClient) return "not-configured";
switch (redisClient.status) {
case "ready":
return "connected";
case "connect":
case "reconnecting":
return "connecting";
default:
return "disconnected";
}
};

View File

@@ -0,0 +1,32 @@
import { DocumentHandler, HandlerContext, HandlerDefinition } from "./types";
/**
* Class that manages handler selection based on multiple criteria
*/
export class DocumentHandlerFactory {
private handlers: HandlerDefinition[] = [];
/**
* Register a handler with its selection criteria
*/
register(definition: HandlerDefinition): void {
this.handlers.push(definition);
// Sort handlers by priority (highest first)
this.handlers.sort((a, b) => b.priority - a.priority);
}
/**
* Get the appropriate handler based on the provided context
*/
getHandler(context: HandlerContext): DocumentHandler {
// Find the first handler whose selector returns true
const matchingHandler = this.handlers.find(h => h.selector(context));
// Return the matching handler or fall back to null/undefined
// (This will cause an error if no handlers match, which is good for debugging)
return matchingHandler?.handler as DocumentHandler;
}
}
// Create the singleton instance
export const handlerFactory = new DocumentHandlerFactory();

View File

@@ -0,0 +1,31 @@
import { DocumentHandler, HandlerContext } from "@/core/types/document-handler";
import { handlerFactory } from "@/core/handlers/document-handlers/handler-factory";
// Import handler definitions
import { projectPageHandlerDefinition } from "@/core/handlers/document-handlers/project-page-handler";
// Register handlers
handlerFactory.register(projectPageHandlerDefinition);
/**
* Get a document handler based on the provided context criteria
* @param documentType The primary document type
* @param additionalContext Optional additional context criteria
* @returns The appropriate document handler
*/
export function getDocumentHandler(
documentType: string,
additionalContext: Omit<HandlerContext, 'documentType'> = {}
): DocumentHandler {
// Create a context object with all criteria
const context: HandlerContext = {
documentType: documentType as any,
...additionalContext
};
// Use the factory to get the appropriate handler
return handlerFactory.getHandler(context);
}
// Export the factory for direct access if needed
export { handlerFactory };

View File

@@ -0,0 +1,30 @@
import { fetchPageDescriptionBinary, updatePageDescription } from "@/core/lib/page";
import { DocumentHandler, DocumentFetchParams, DocumentStoreParams, HandlerDefinition } from "./types";
/**
* Handler for "project_page" document type
*/
export const projectPageHandler: DocumentHandler = {
/**
* Fetch project page description
*/
fetch: async ({ pageId, params, context }: DocumentFetchParams) => {
const { cookie } = context;
return await fetchPageDescriptionBinary(params, pageId, cookie);
},
/**
* Store project page description
*/
store: async ({ pageId, state, params, context }: DocumentStoreParams) => {
const { cookie } = context;
await updatePageDescription(params, pageId, state, cookie);
}
};
// Define the project page handler definition
export const projectPageHandlerDefinition: HandlerDefinition = {
selector: (context) => context.documentType === "project_page" && !context.agentId,
handler: projectPageHandler,
priority: 10 // Standard priority
};

View File

@@ -1,21 +0,0 @@
import { ErrorRequestHandler } from "express";
import { manualLogger } from "@/core/helpers/logger.js";
export const errorHandler: ErrorRequestHandler = (err, _req, res) => {
// Log the error
manualLogger.error(err);
// Set the response status
res.status(err.status || 500);
// Send the response
res.json({
error: {
message:
process.env.NODE_ENV === "production"
? "An unexpected error occurred"
: err.message,
...(process.env.NODE_ENV !== "production" && { stack: err.stack }),
},
});
};

View File

@@ -0,0 +1,277 @@
import { AppError, HttpStatusCode, ErrorCategory } from "./error-handler";
/**
* Map of error types to their corresponding factory functions
* This ensures that error types and their implementations stay in sync
*/
interface ErrorFactory {
statusCode: number;
category: ErrorCategory;
defaultMessage: string;
createError: (message?: string, context?: Record<string, any>) => AppError;
}
const ERROR_FACTORIES: Record<string, ErrorFactory> = {
"bad-request": {
statusCode: HttpStatusCode.BAD_REQUEST,
category: ErrorCategory.OPERATIONAL,
defaultMessage: "Bad Request",
createError: (message = "Bad Request", context?) =>
new AppError(message, HttpStatusCode.BAD_REQUEST, ErrorCategory.OPERATIONAL, context),
},
unauthorized: {
statusCode: HttpStatusCode.UNAUTHORIZED,
category: ErrorCategory.OPERATIONAL,
defaultMessage: "Unauthorized",
createError: (message = "Unauthorized", context?) =>
new AppError(message, HttpStatusCode.UNAUTHORIZED, ErrorCategory.OPERATIONAL, context),
},
forbidden: {
statusCode: HttpStatusCode.FORBIDDEN,
category: ErrorCategory.OPERATIONAL,
defaultMessage: "Forbidden",
createError: (message = "Forbidden", context?) =>
new AppError(message, HttpStatusCode.FORBIDDEN, ErrorCategory.OPERATIONAL, context),
},
"not-found": {
statusCode: HttpStatusCode.NOT_FOUND,
category: ErrorCategory.OPERATIONAL,
defaultMessage: "Resource not found",
createError: (message = "Resource not found", context?) =>
new AppError(message, HttpStatusCode.NOT_FOUND, ErrorCategory.OPERATIONAL, context),
},
conflict: {
statusCode: HttpStatusCode.CONFLICT,
category: ErrorCategory.OPERATIONAL,
defaultMessage: "Resource conflict",
createError: (message = "Resource conflict", context?) =>
new AppError(message, HttpStatusCode.CONFLICT, ErrorCategory.OPERATIONAL, context),
},
"unprocessable-entity": {
statusCode: HttpStatusCode.UNPROCESSABLE_ENTITY,
category: ErrorCategory.OPERATIONAL,
defaultMessage: "Unprocessable Entity",
createError: (message = "Unprocessable Entity", context?) =>
new AppError(message, HttpStatusCode.UNPROCESSABLE_ENTITY, ErrorCategory.OPERATIONAL, context),
},
"too-many-requests": {
statusCode: HttpStatusCode.TOO_MANY_REQUESTS,
category: ErrorCategory.OPERATIONAL,
defaultMessage: "Too many requests",
createError: (message = "Too many requests", context?) =>
new AppError(message, HttpStatusCode.TOO_MANY_REQUESTS, ErrorCategory.OPERATIONAL, context),
},
internal: {
statusCode: HttpStatusCode.INTERNAL_SERVER,
category: ErrorCategory.PROGRAMMING,
defaultMessage: "Internal Server Error",
createError: (message = "Internal Server Error", context?) =>
new AppError(message, HttpStatusCode.INTERNAL_SERVER, ErrorCategory.PROGRAMMING, context),
},
"service-unavailable": {
statusCode: HttpStatusCode.SERVICE_UNAVAILABLE,
category: ErrorCategory.SYSTEM,
defaultMessage: "Service Unavailable",
createError: (message = "Service Unavailable", context?) =>
new AppError(message, HttpStatusCode.SERVICE_UNAVAILABLE, ErrorCategory.SYSTEM, context),
},
fatal: {
statusCode: HttpStatusCode.INTERNAL_SERVER,
category: ErrorCategory.FATAL,
defaultMessage: "Fatal Error",
createError: (message = "Fatal Error", context?) =>
new AppError(message, HttpStatusCode.INTERNAL_SERVER, ErrorCategory.FATAL, context),
},
};
// Create the type from the keys of the error factories map
export type ErrorType = keyof typeof ERROR_FACTORIES;
// -------------------------------------------------------------------------
// Primary public API - Recommended for most use cases
// -------------------------------------------------------------------------
/**
* Base options for handleError function
*/
type BaseErrorHandlerOptions = {
// Error classification options
errorType?: ErrorType;
message?: string;
// Context information
component: string;
operation: string;
extraContext?: Record<string, any>;
// Behavior options
rethrowIfAppError?: boolean;
};
/**
* Options for throwing variant of handleError - discriminated by throw: true
*/
export type ThrowingOptions = BaseErrorHandlerOptions & {
throw: true;
};
/**
* Options for non-throwing variant of handleError - default behavior
*/
export type NonThrowingOptions = BaseErrorHandlerOptions;
/**
* Unified error handler that encapsulates common error handling patterns
*
* @param error The error to handle
* @param options Configuration options with throw: true to throw the error instead of returning it
* @returns Never returns - always throws
* @example
* // Throwing version
* handleError(error, {
* errorType: 'not-found',
* component: 'user-service',
* operation: 'getUserById',
* throw: true
* });
*/
export function handleError(error: unknown, options: ThrowingOptions): never;
/**
* Unified error handler that encapsulates common error handling patterns
*
* @param error The error to handle
* @param options Configuration options (non-throwing by default)
* @returns The AppError instance
* @example
* // Non-throwing version (default)
* const appError = handleError(error, {
* errorType: 'not-found',
* component: 'user-service',
* operation: 'getUserById'
* });
* return { error: appError.output() };
*/
export function handleError(error: unknown, options: NonThrowingOptions): AppError;
/**
* Implementation of handleError that handles both throwing and non-throwing cases
*/
export function handleError(error: unknown, options: ThrowingOptions | NonThrowingOptions): AppError | never {
console.log("error", error instanceof AppError);
// Only throw if throw is explicitly true
const shouldThrow = (options as ThrowingOptions).throw === true;
// If the error is already an AppError and we want to rethrow it as is
if (options.rethrowIfAppError !== false && error instanceof AppError) {
if (shouldThrow) {
throw error;
}
return error;
}
// Format the error message
const errorMessage = options.message
? error instanceof Error
? `${options.message}: ${error.message}`
: error
? `${options.message}: ${String(error)}`
: options.message
: error instanceof Error
? error.message
: error
? String(error)
: "Unknown error occurred";
// Build context object
const context = {
component: options.component,
operation: options.operation,
originalError: error,
...(options.extraContext || {}),
};
// Create the appropriate error type using our factory map
const errorType = options.errorType || "internal";
const factory = ERROR_FACTORIES[errorType];
if (!factory) {
// If no factory found, default to internal error
return ERROR_FACTORIES.internal.createError(errorMessage, context);
}
// Create the error with the factory
const appError = factory.createError(errorMessage, context);
// If we should throw, do so now
if (shouldThrow) {
throw appError;
}
return appError;
}
/**
* Utility function to convert errors or enhance existing AppErrors
*/
export const convertError = (
error: Error,
options?: {
statusCode?: number;
message?: string;
category?: ErrorCategory;
context?: Record<string, any>;
}
): AppError => {
if (error instanceof AppError) {
// If it's already an AppError and no overrides, return as is
if (!options?.statusCode && !options?.message && !options?.category) {
return error;
}
// Create a new AppError with the original as context
return new AppError(
options?.message || error.message,
options?.statusCode || error.status,
options?.category || error.category,
{
...(error.context || {}),
...(options?.context || {}),
originalError: error,
}
);
}
// Determine the appropriate error type based on status code
let errorType: ErrorType = "internal";
if (options?.statusCode) {
// Find the error type that matches the status code
const entry = Object.entries(ERROR_FACTORIES).find(([_, factory]) => factory.statusCode === options.statusCode);
if (entry) {
errorType = entry[0] as ErrorType;
}
}
// Return a new AppError using the factory
return handleError(error, {
errorType: errorType,
message: options?.message,
component: options?.context?.component || "unknown",
operation: options?.context?.operation || "convert-error",
extraContext: options?.context,
});
};
/**
* Check if an error is an AppError
*/
export const isAppError = (err: any, statusCode?: number): boolean => {
return err instanceof AppError && (!statusCode || err.status === statusCode);
};
// Export only the public API
export default {
handleError,
convertError,
isAppError,
};

View File

@@ -0,0 +1,417 @@
import { ErrorRequestHandler, Request, Response, NextFunction } from "express";
import { SentryInstance, captureException } from "@/sentry-config";
import { env } from "@/env";
import { logger } from "@plane/logger";
import { handleError } from "./error-factory";
import { ErrorContext, reportError } from "./error-reporting";
import { manualLogger } from "../logger";
import { APIService } from "@/core/services/api.service";
/**
* HTTP Status Codes
*/
export enum HttpStatusCode {
// 2xx Success
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NO_CONTENT = 204,
// 4xx Client Errors
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
CONFLICT = 409,
GONE = 410,
UNPROCESSABLE_ENTITY = 422,
TOO_MANY_REQUESTS = 429,
// 5xx Server Errors
INTERNAL_SERVER = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504,
}
/**
* Error categories to classify errors
*/
export enum ErrorCategory {
OPERATIONAL = "operational", // Expected errors that are part of normal operation (e.g. validation failures)
PROGRAMMING = "programming", // Unexpected errors that indicate bugs (e.g. null references)
SYSTEM = "system", // System errors (e.g. out of memory, connection failures)
FATAL = "fatal", // Severe errors that should crash the app (e.g. unrecoverable state)
}
/**
* Base Application Error Class
* All custom errors extend this class
*/
export class AppError extends Error {
readonly status: number;
readonly category: ErrorCategory;
readonly context?: Record<string, any>;
readonly isOperational: boolean; // Kept for backward compatibility
constructor(
message: string,
status: number = HttpStatusCode.INTERNAL_SERVER,
category: ErrorCategory = ErrorCategory.PROGRAMMING,
context?: Record<string, any>
) {
super(message);
// Set error properties
this.name = this.constructor.name;
this.status = status;
this.category = category;
this.isOperational = category === ErrorCategory.OPERATIONAL;
this.context = context;
// Capture stack trace, excluding the constructor call from the stack
Error.captureStackTrace(this, this.constructor);
// Automatically report the error (unless it's being constructed by the error utilities)
if (!context?.skipReporting) {
this.report();
}
}
/**
* Creates a formatted representation of the error
*/
output() {
return {
statusCode: this.status,
payload: {
statusCode: this.status,
error: this.getErrorName(),
message: this.message,
category: this.category,
},
headers: {},
};
}
/**
* Gets a descriptive name for the error based on status code
*/
private getErrorName(): string {
const statusCodes: Record<number, string> = {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
409: "Conflict",
410: "Gone",
422: "Unprocessable Entity",
429: "Too Many Requests",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
};
return statusCodes[this.status] || "Unknown Error";
}
/**
* Reports the error to logging and monitoring systems
*/
private report(): void {
// Different logging based on error category
if (this.category === ErrorCategory.OPERATIONAL) {
manualLogger.error(`Operational error: ${this.message}`, {
errorName: this.name,
errorStatus: this.status,
errorCategory: this.category,
context: this.context,
});
} else if (this.category === ErrorCategory.FATAL) {
manualLogger.error(`FATAL error: ${this.message}`, {
errorName: this.name,
errorStatus: this.status,
errorCategory: this.category,
stack: this.stack,
context: this.context,
});
} else {
manualLogger.error(`${this.category} error: ${this.message}`, {
errorName: this.name,
errorStatus: this.status,
errorCategory: this.category,
stack: this.stack,
context: this.context,
});
}
// Send to Sentry if configured
if (SentryInstance) {
captureException(this, {
extra: {
...this.context,
isOperational: this.isOperational,
errorCategory: this.category,
status: this.status,
},
});
}
}
}
export class FatalError extends AppError {
constructor(message: string, context?: Record<string, any>) {
super(message, HttpStatusCode.INTERNAL_SERVER, ErrorCategory.FATAL, context);
}
}
/**
* Main Express error handler middleware
*/
export const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
// Already sent response, let default Express error handler deal with it
if (res.headersSent) {
return next(err);
}
// Convert to AppError if it's not already one
const error = handleError(err, {
component: "express",
operation: "error-handler",
extraContext: {
originalError: err,
url: req.originalUrl,
method: req.method,
},
});
// Normalize status code
const statusCode = error.status;
// Set the response status
res.status(statusCode);
// Set any custom headers if provided in the error object
if (err.headers && typeof err.headers === "object") {
Object.entries(err.headers).forEach(([key, value]) => {
res.set(key, value as string);
});
}
// Prepare error response
const errorResponse: {
error: {
message: string;
status: number;
stack?: string;
};
} = {
error: {
message:
error.category === ErrorCategory.OPERATIONAL || env.NODE_ENV !== "production"
? error.message
: "An unexpected error occurred",
status: statusCode,
},
};
// Add stack trace in non-production environments
if (env.NODE_ENV !== "production") {
errorResponse.error.stack = error.stack;
}
// Send the response
res.json(errorResponse);
// If it's a fatal error, initiate shutdown after sending response
if (error.category === ErrorCategory.FATAL) {
logger.error(`FATAL ERROR OCCURRED! Initiating shutdown...`);
process.emit("SIGTERM");
}
};
export const asyncHandler = (fn: Function) => {
return (req: any, res: any, next: any) => {
Promise.resolve(fn(req, res, next)).catch((error) => {
// Convert to AppError if needed and pass to Express error middleware
const appError = handleError(error, {
errorType: "internal",
component: "express",
operation: "route-handler",
extraContext: {
url: req.originalUrl,
method: req.method,
body: req.body,
query: req.query,
params: req.params,
},
});
next(appError);
});
};
};
export interface CatchAsyncOptions<T, E = Error> {
/** Default value to return in case of error, null by default */
defaultValue?: T | null;
/** Whether to report non-AppErrors automatically */
reportErrors?: boolean;
/** Whether to rethrow the error after handling it */
rethrow?: boolean;
/** Custom error transformer function */
transformError?: (error: unknown) => E;
/** Custom error handler function that runs before standard handling */
onError?: (error: unknown) => void | Promise<void>;
/** Custom handler for specific error types */
errorHandlers?: {
[key: string]: (error: any) => T | null | Promise<T>;
};
}
export const catchAsync = <T, E = Error>(
fn: () => Promise<T>,
context?: ErrorContext,
options: CatchAsyncOptions<T, E> = {}
): (() => Promise<T | null>) => {
const { defaultValue = null, onError, rethrow = false } = options;
return async () => {
try {
return await fn();
} catch (error) {
// Apply custom error handler if provided
if (onError) {
await Promise.resolve(onError(error));
}
reportError(error, context);
if (error instanceof AppError) {
error.context;
}
if (rethrow) {
// Use handleError to ensure consistent error handling when rethrowing
handleError(error, {
component: context?.extra?.component || "unknown",
operation: context?.extra?.operation || "unknown",
extraContext: {
...context,
...(error instanceof AppError ? error.context : {}),
originalError: error,
},
throw: true,
});
}
return defaultValue;
}
};
};
/**
* Sets up global error handlers for unhandled rejections and exceptions
* @param gracefulShutdown Function to call for graceful shutdown
*/
export const setupGlobalErrorHandlers = (gracefulShutdown: () => Promise<void>): void => {
// Handle promise rejections
process.on("unhandledRejection", (reason: unknown) => {
logger.error("Unhandled Promise Rejection", { reason });
if (SentryInstance) {
SentryInstance.captureException(reason);
}
// Convert to AppError and handle
const appError = handleError(reason, {
errorType: "internal",
message: reason instanceof Error ? reason.message : String(reason),
component: "process",
operation: "unhandledRejection",
extraContext: { source: "unhandledRejection" },
});
// Only crash if it's a fatal error
if (appError.category === ErrorCategory.FATAL) {
logger.error(`FATAL ERROR ENCOUNTERED! Shutting down...`);
gracefulShutdown();
} else {
logger.error(`Unhandled rejection caught and contained`);
}
});
// Handle exceptions
process.on("uncaughtException", (error: Error) => {
logger.error("Uncaught Exception", {
error: error.message,
stack: error.stack,
});
if (SentryInstance) {
SentryInstance.captureException(error);
}
// Convert to AppError if needed
const appError = handleError(error, {
errorType: "internal",
component: "process",
operation: "uncaughtException",
extraContext: {
source: "uncaughtException",
},
});
// Only crash for fatal errors or if error handling system itself is broken
if (appError.category === ErrorCategory.FATAL) {
logger.error(`FATAL ERROR ENCOUNTERED! Shutting down...`);
gracefulShutdown();
} else {
logger.warn(`Uncaught exception contained - this should be investigated!`);
}
});
// Handle termination signals
process.on("SIGTERM", () => {
logger.info("SIGTERM received. Shutting down gracefully...");
gracefulShutdown();
});
process.on("SIGINT", () => {
logger.info("SIGINT received. Shutting down gracefully...");
gracefulShutdown();
});
};
/**
* Configure error handling middleware for the Express app
* @param app Express application instance
*/
export function configureErrorHandlers(app: any): void {
// Global error handling middleware
app.use(errorHandler);
// 404 handler must be last
app.use((_req: Request, _res: Response, next: NextFunction) => {
next(
handleError(null, {
errorType: "not-found",
message: "Resource not found",
component: "express",
operation: "route-handler",
extraContext: { path: _req.path },
throw: true,
})
);
});
}

View File

@@ -0,0 +1,56 @@
import { SentryInstance } from "@/sentry-config";
import { AppError, ErrorCategory } from "./error-handler";
import { logger } from "@plane/logger";
import { handleError } from "./error-factory";
export interface ErrorContext {
url?: string;
method?: string;
body?: any;
query?: any;
params?: any;
extra?: Record<string, any>;
}
/**
* Utility function to report errors that aren't instances of AppError
* AppError instances automatically report themselves on creation
* Only use this for external errors that don't use our error system
*/
export const reportError = (error: Error | unknown, context?: ErrorContext): void => {
if (error instanceof AppError) {
// if it's an app error, don't report it as it's already been reported
return;
}
logger.error(`External error: ${error instanceof Error ? error.stack || error.message : String(error)}`, {
error,
context,
});
// Send to Sentry
if (SentryInstance) {
SentryInstance.captureException(error, {
extra: {
...context,
errorCategory: ErrorCategory.PROGRAMMING,
},
});
}
};
export const handleFatalError = (error: Error | unknown, context?: ErrorContext): void => {
// Convert to fatal AppError
const fatalError = handleError(error, {
errorType: "fatal",
message: error instanceof Error ? error.message : String(error),
component: context?.extra?.component || "system",
operation: context?.extra?.operation || "fatal-error-handler",
extraContext: {
...context,
originalError: error,
},
});
process.emit("uncaughtException", fatalError);
};

View File

@@ -0,0 +1,158 @@
import { handleError } from "./error-factory";
/**
* A simple validation utility that integrates with our error system.
*
* This provides a fluent interface for validating data and throwing
* appropriate errors if validation fails.
*/
export class Validator<T> {
constructor(
private readonly data: T,
private readonly name: string = "data"
) {}
/**
* Ensures a value is defined (not undefined or null)
*/
required(message?: string): Validator<T> {
if (this.data === undefined || this.data === null) {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} is required`,
component: 'validator',
operation: 'required',
throw: true
});
}
return this;
}
/**
* Ensures a value is a string
*/
string(message?: string): Validator<T> {
if (typeof this.data !== "string") {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} must be a string`,
component: 'validator',
operation: 'string',
throw: true
});
}
return this;
}
/**
* Ensures a string is not empty
*/
notEmpty(message?: string): Validator<T> {
if (typeof this.data === "string" && this.data.trim() === "") {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} cannot be empty`,
component: 'validator',
operation: 'notEmpty',
throw: true
});
}
return this;
}
/**
* Ensures a value is a number
*/
number(message?: string): Validator<T> {
if (typeof this.data !== "number" || isNaN(this.data)) {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} must be a valid number`,
component: 'validator',
operation: 'number',
throw: true
});
}
return this;
}
/**
* Ensures an array is not empty
*/
nonEmptyArray(message?: string): Validator<T> {
if (!Array.isArray(this.data) || this.data.length === 0) {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} must be a non-empty array`,
component: 'validator',
operation: 'nonEmptyArray',
throw: true
});
}
return this;
}
/**
* Ensures a value matches a regular expression
*/
match(regex: RegExp, message?: string): Validator<T> {
if (typeof this.data !== "string" || !regex.test(this.data)) {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} has an invalid format`,
component: 'validator',
operation: 'match',
throw: true
});
}
return this;
}
/**
* Ensures a value is one of the allowed values
*/
oneOf(allowedValues: any[], message?: string): Validator<T> {
if (!allowedValues.includes(this.data)) {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} must be one of: ${allowedValues.join(", ")}`,
component: 'validator',
operation: 'oneOf',
throw: true
});
}
return this;
}
/**
* Custom validation function
*/
custom(validationFn: (value: T) => boolean, message?: string): Validator<T> {
if (!validationFn(this.data)) {
throw handleError(null, {
errorType: 'bad-request',
message: message || `${this.name} is invalid`,
component: 'validator',
operation: 'custom',
throw: true
});
}
return this;
}
/**
* Get the validated data
*/
get(): T {
return this.data;
}
}
/**
* Create a new validator for a value
*/
export const validate = <T>(data: T, name?: string): Validator<T> => {
return new Validator(data, name);
};
export default validate;

View File

@@ -0,0 +1,261 @@
import { handleError } from "./error-handling/error-factory";
/**
* A simple validation utility that integrates with our error system.
*
* This provides a fluent interface for validating data and throwing
* appropriate errors if validation fails.
*/
export class Validator<T> {
constructor(
private readonly data: T,
private readonly name: string = "data"
) {}
/**
* Ensures a value is defined (not undefined or null)
*/
required(message?: string): Validator<T> {
if (this.data === undefined || this.data === null) {
throw handleError(new ValidationError(this.name, message || `${this.name} is required`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateRequired',
extraContext: { field: this.name },
throw: true
});
}
return this;
}
/**
* Ensures a value is a string
*/
string(message?: string): Validator<T> {
if (typeof this.data !== "string") {
throw handleError(new ValidationError(this.name, message || `${this.name} must be a string`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateString',
extraContext: { field: this.name },
throw: true
});
}
return this;
}
/**
* Ensures a string is not empty
*/
notEmpty(message?: string): Validator<T> {
if (typeof this.data === "string" && this.data.trim() === "") {
throw handleError(new ValidationError(this.name, message || `${this.name} cannot be empty`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateNonEmptyString',
extraContext: { field: this.name },
throw: true
});
}
return this;
}
/**
* Ensures a value is a number
*/
number(message?: string): Validator<T> {
if (typeof this.data !== "number" || isNaN(this.data)) {
throw handleError(new ValidationError(this.name, message || `${this.name} must be a valid number`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateNumber',
extraContext: { field: this.name },
throw: true
});
}
return this;
}
/**
* Ensures an array is not empty
*/
nonEmptyArray(message?: string): Validator<T> {
if (!Array.isArray(this.data) || this.data.length === 0) {
throw handleError(new ValidationError(this.name, message || `${this.name} must be a non-empty array`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateArray',
extraContext: { field: this.name },
throw: true
});
}
return this;
}
/**
* Ensures a value matches a regular expression
*/
match(regex: RegExp, message?: string): Validator<T> {
if (typeof this.data !== "string" || !regex.test(this.data)) {
throw handleError(new ValidationError(this.name, message || `${this.name} has an invalid format`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateFormat',
extraContext: { field: this.name, format: regex.toString() },
throw: true
});
}
return this;
}
/**
* Ensures a value is one of the allowed values
*/
oneOf(allowedValues: any[], message?: string): Validator<T> {
if (!allowedValues.includes(this.data)) {
throw handleError(new ValidationError(this.name, message || `${this.name} must be one of: ${allowedValues.join(", ")}`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateEnum',
extraContext: { field: this.name, allowedValues },
throw: true
});
}
return this;
}
/**
* Custom validation function
*/
custom(validationFn: (value: T) => boolean, message?: string): Validator<T> {
if (!validationFn(this.data)) {
throw handleError(new ValidationError(this.name, message || `${this.name} is invalid`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateCustom',
extraContext: { field: this.name },
throw: true
});
}
return this;
}
/**
* Get the validated data
*/
get(): T {
return this.data;
}
}
/**
* Create a new validator for a value
*/
export const validate = <T>(data: T, name?: string): Validator<T> => {
return new Validator(data, name);
};
export default validate;
export class ValidationError extends Error {
constructor(public name: string, message: string) {
super(message);
this.name = name;
}
}
export const validateRequired = (value: any, name: string, message?: string) => {
if (value === undefined || value === null) {
throw handleError(new ValidationError(name, message || `${name} is required`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateRequired',
extraContext: { field: name },
throw: true
});
}
};
export const validateString = (value: any, name: string, message?: string) => {
if (typeof value !== 'string') {
throw handleError(new ValidationError(name, message || `${name} must be a string`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateString',
extraContext: { field: name },
throw: true
});
}
};
export const validateNonEmptyString = (value: string, name: string, message?: string) => {
if (!value.trim()) {
throw handleError(new ValidationError(name, message || `${name} cannot be empty`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateNonEmptyString',
extraContext: { field: name },
throw: true
});
}
};
export const validateNumber = (value: any, name: string, message?: string) => {
if (typeof value !== 'number' || isNaN(value)) {
throw handleError(new ValidationError(name, message || `${name} must be a valid number`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateNumber',
extraContext: { field: name },
throw: true
});
}
};
export const validateArray = (value: any, name: string, message?: string) => {
if (!Array.isArray(value) || value.length === 0) {
throw handleError(new ValidationError(name, message || `${name} must be a non-empty array`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateArray',
extraContext: { field: name },
throw: true
});
}
};
export const validateFormat = (value: string, name: string, format: RegExp, message?: string) => {
if (!format.test(value)) {
throw handleError(new ValidationError(name, message || `${name} has an invalid format`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateFormat',
extraContext: { field: name, format: format.toString() },
throw: true
});
}
};
export const validateEnum = (value: any, name: string, allowedValues: any[], message?: string) => {
if (!allowedValues.includes(value)) {
throw handleError(new ValidationError(name, message || `${name} must be one of: ${allowedValues.join(", ")}`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateEnum',
extraContext: { field: name, allowedValues },
throw: true
});
}
};
export const validateCustom = (value: any, name: string, validator: (value: any) => boolean, message?: string) => {
if (!validator(value)) {
throw handleError(new ValidationError(name, message || `${name} is invalid`), {
errorType: 'bad-request',
component: 'validation',
operation: 'validateCustom',
extraContext: { field: name },
throw: true
});
}
};

View File

@@ -1,17 +1,18 @@
import { Server } from "@hocuspocus/server";
import { v4 as uuidv4 } from "uuid";
import { IncomingHttpHeaders } from "http";
// lib
import { handleAuthentication } from "@/core/lib/authentication.js";
import { handleAuthentication } from "@/core/lib/authentication";
// extensions
import { getExtensions } from "@/core/extensions/index.js";
import {
DocumentCollaborativeEvents,
TDocumentEventsServer,
} from "@plane/editor/lib";
import { getExtensions } from "@/core/extensions/index";
import { DocumentCollaborativeEvents, TDocumentEventsServer } from "@plane/editor/lib";
// editor types
import { TUserDetails } from "@plane/editor";
// types
import { type HocusPocusServerContext } from "@/core/types/common.js";
import { TDocumentTypes, type HocusPocusServerContext } from "@/core/types/common";
// error handling
import { catchAsync } from "@/core/helpers/error-handling/error-handler";
import { handleError } from "@/core/helpers/error-handling/error-factory";
export const getHocusPocusServer = async () => {
const extensions = await getExtensions();
@@ -21,53 +22,79 @@ export const getHocusPocusServer = async () => {
onAuthenticate: async ({
requestHeaders,
context,
requestParameters,
// user id used as token for authentication
token,
}: {
requestHeaders: IncomingHttpHeaders;
context: HocusPocusServerContext; // Better than 'any', still allows property assignment
requestParameters: URLSearchParams;
token: string;
}) => {
let cookie: string | undefined = undefined;
let userId: string | undefined = undefined;
// need to rethrow all errors since hocuspocus needs to know to stop
// further propagation of events to other document lifecycle
return catchAsync(
async () => {
let cookie: string | undefined = undefined;
let userId: string | undefined = undefined;
// Extract cookie (fallback to request headers) and userId from token (for scenarios where
// the cookies are not passed in the request headers)
try {
const parsedToken = JSON.parse(token) as TUserDetails;
userId = parsedToken.id;
cookie = parsedToken.cookie;
} catch (error) {
// If token parsing fails, fallback to request headers
console.error("Token parsing failed, using request headers:", error);
} finally {
// If cookie is still not found, fallback to request headers
if (!cookie) {
cookie = requestHeaders.cookie?.toString();
// Extract cookie (fallback to request headers) and userId from token (for scenarios where
// the cookies are not passed in the request headers)
try {
const parsedToken = JSON.parse(token) as TUserDetails;
userId = parsedToken.id;
cookie = parsedToken.cookie;
} catch (error) {
// If token parsing fails, fallback to request headers
console.error("Token parsing failed, using request headers:", error);
} finally {
// If cookie is still not found, fallback to request headers
if (!cookie) {
cookie = requestHeaders.cookie?.toString();
}
}
if (!cookie || !userId) {
handleError(null, {
errorType: "unauthorized",
message: "Credentials not provided",
component: "hocuspocus",
operation: "authenticate",
extraContext: { tokenProvided: !!token },
throw: true,
});
}
context.documentType = requestParameters.get("documentType")?.toString() as TDocumentTypes;
context.cookie = cookie ?? requestParameters.get("cookie");
context.userId = userId;
context.workspaceSlug = requestParameters.get("workspaceSlug")?.toString() as string;
return await handleAuthentication({
cookie: context.cookie,
userId: context.userId,
workspaceSlug: context.workspaceSlug,
});
},
{ extra: { operation: "authenticate" } },
{
rethrow: true,
}
}
if (!cookie || !userId) {
throw new Error("Credentials not provided");
}
// set cookie in context, so it can be used throughout the ws connection
(context as HocusPocusServerContext).cookie = cookie;
try {
await handleAuthentication({
cookie,
userId,
});
} catch (error) {
throw Error("Authentication unsuccessful!");
}
)();
},
async onStateless({ payload, document }) {
// broadcast the client event (derived from the server event) to all the clients so that they can update their state
const response =
DocumentCollaborativeEvents[payload as TDocumentEventsServer].client;
if (response) {
document.broadcastStateless(response);
}
onStateless: async ({ payload, document }) => {
return catchAsync(
async () => {
// broadcast the client event (derived from the server event) to all the clients so that they can update their state
const response = DocumentCollaborativeEvents[payload as TDocumentEventsServer].client;
if (response) {
document.broadcastStateless(response);
}
},
{ extra: { operation: "stateless", payload } }
);
},
extensions,
debounce: 10000,
debounce: 1000,
});
};

View File

@@ -1,27 +1,47 @@
// services
import { UserService } from "@/core/services/user.service.js";
// core helpers
import { manualLogger } from "@/core/helpers/logger.js";
import { UserService } from "@/core/services/user.service";
import { handleError } from "@/core/helpers/error-handling/error-factory";
const userService = new UserService();
type Props = {
cookie: string;
userId: string;
workspaceSlug: string;
};
export const handleAuthentication = async (props: Props) => {
const { cookie, userId } = props;
const { cookie, userId, workspaceSlug } = props;
// fetch current user info
let response;
try {
response = await userService.currentUser(cookie);
} catch (error) {
manualLogger.error("Failed to fetch current user:", error);
throw error;
console.log("caught?");
handleError(error, {
errorType: "unauthorized",
message: "Failed to authenticate user",
component: "authentication",
operation: "fetch-current-user",
extraContext: {
userId,
workspaceSlug,
},
throw: true,
});
}
if (response.id !== userId) {
throw Error("Authentication failed: Token doesn't match the current user.");
handleError(null, {
errorType: "unauthorized",
message: "Authentication failed: Token doesn't match the current user.",
component: "authentication",
operation: "validate-user",
extraContext: {
userId,
workspaceSlug,
},
throw: true,
});
}
return {

View File

@@ -1,112 +1,64 @@
// helpers
import {
getAllDocumentFormatsFromBinaryData,
getBinaryDataFromHTMLString,
} from "@/core/helpers/page.js";
import { getAllDocumentFormatsFromBinaryData, getBinaryDataFromHTMLString } from "@/core/helpers/page";
// services
import { PageService } from "@/core/services/page.service.js";
import { manualLogger } from "../helpers/logger.js";
import { PageService } from "@/core/services/page.service";
const pageService = new PageService();
export const updatePageDescription = async (
params: URLSearchParams,
pageId: string,
updatedDescription: Uint8Array,
cookie: string | undefined,
cookie: string | undefined
) => {
if (!(updatedDescription instanceof Uint8Array)) {
throw new Error(
"Invalid updatedDescription: must be an instance of Uint8Array",
);
throw new Error("Invalid updatedDescription: must be an instance of Uint8Array");
}
const workspaceSlug = params.get("workspaceSlug")?.toString();
const projectId = params.get("projectId")?.toString();
if (!workspaceSlug || !projectId || !cookie) return;
const { contentBinaryEncoded, contentHTML, contentJSON } =
getAllDocumentFormatsFromBinaryData(updatedDescription);
try {
const payload = {
description_binary: contentBinaryEncoded,
description_html: contentHTML,
description: contentJSON,
};
const { contentBinaryEncoded, contentHTML, contentJSON } = getAllDocumentFormatsFromBinaryData(updatedDescription);
const payload = {
description_binary: contentBinaryEncoded,
description_html: contentHTML,
description: contentJSON,
};
await pageService.updateDescription(
workspaceSlug,
projectId,
pageId,
payload,
cookie,
);
} catch (error) {
manualLogger.error("Update error:", error);
throw error;
}
await pageService.updateDescription(workspaceSlug, projectId, pageId, payload, cookie);
};
const fetchDescriptionHTMLAndTransform = async (
workspaceSlug: string,
projectId: string,
pageId: string,
cookie: string,
cookie: string
) => {
if (!workspaceSlug || !projectId || !cookie) return;
try {
const pageDetails = await pageService.fetchDetails(
workspaceSlug,
projectId,
pageId,
cookie,
);
const { contentBinary } = getBinaryDataFromHTMLString(
pageDetails.description_html ?? "<p></p>",
);
return contentBinary;
} catch (error) {
manualLogger.error(
"Error while transforming from HTML to Uint8Array",
error,
);
throw error;
}
const pageDetails = await pageService.fetchDetails(workspaceSlug, projectId, pageId, cookie);
const { contentBinary } = getBinaryDataFromHTMLString(pageDetails.description_html ?? "<p></p>");
return contentBinary;
};
export const fetchPageDescriptionBinary = async (
params: URLSearchParams,
pageId: string,
cookie: string | undefined,
cookie: string | undefined
) => {
const workspaceSlug = params.get("workspaceSlug")?.toString();
const projectId = params.get("projectId")?.toString();
if (!workspaceSlug || !projectId || !cookie) return null;
try {
const response = await pageService.fetchDescriptionBinary(
workspaceSlug,
projectId,
pageId,
cookie,
);
const binaryData = new Uint8Array(response);
const response = await pageService.fetchDescriptionBinary(workspaceSlug, projectId, pageId, cookie);
const binaryData = new Uint8Array(response);
if (binaryData.byteLength === 0) {
const binary = await fetchDescriptionHTMLAndTransform(
workspaceSlug,
projectId,
pageId,
cookie,
);
if (binary) {
return binary;
}
if (binaryData.byteLength === 0) {
const binary = await fetchDescriptionHTMLAndTransform(workspaceSlug, projectId, pageId, cookie);
if (binary) {
return binary;
}
return binaryData;
} catch (error) {
manualLogger.error("Fetch error:", error);
throw error;
}
return binaryData;
};

View File

@@ -1,7 +1,7 @@
// types
import { TPage } from "@plane/types";
// services
import { API_BASE_URL, APIService } from "@/core/services/api.service.js";
import { API_BASE_URL, APIService } from "@/core/services/api.service";
export class PageService extends APIService {
constructor() {

View File

@@ -1,7 +1,7 @@
// types
import type { IUser } from "@plane/types";
// services
import { API_BASE_URL, APIService } from "@/core/services/api.service.js";
import { API_BASE_URL, APIService } from "@/core/services/api.service";
export class UserService extends APIService {
constructor() {

View File

@@ -0,0 +1,106 @@
// server
import { Server } from "http";
// hocuspocus server
import type { Hocuspocus } from "@hocuspocus/server";
// logger
import { logger } from "@plane/logger";
// config
import { serverConfig } from "@/config/server-config";
/**
* ShutdownManager handles graceful shutdown of server resources
*/
export class ShutdownManager {
private readonly hocusPocusServer: Hocuspocus;
private readonly httpServer: Server;
/**
* Initialize the shutdown manager
* @param hocusPocusServer Hocuspocus server instance
* @param httpServer HTTP server instance
*/
constructor(hocusPocusServer: Hocuspocus, httpServer: Server) {
this.hocusPocusServer = hocusPocusServer;
this.httpServer = httpServer;
}
/**
* Register shutdown handlers with the process
*/
registerShutdownHandlers(): void {
const gracefulShutdown = this.getGracefulShutdown();
// Handle process signals
process.on("SIGTERM", gracefulShutdown);
process.on("SIGINT", gracefulShutdown);
// Handle uncaught exceptions
process.on("uncaughtException", (error) => {
logger.error("Uncaught exception:", error);
gracefulShutdown();
});
// Handle unhandled promise rejections
process.on("unhandledRejection", (reason) => {
logger.error("Unhandled rejection:", reason);
gracefulShutdown();
});
}
/**
* Get the graceful shutdown handler
* @returns Shutdown function
*/
private getGracefulShutdown(): () => Promise<void> {
return async () => {
logger.info("Starting graceful shutdown...");
let hasShutdownCompleted = false;
// Create a timeout that will force exit if shutdown takes too long
const forceExitTimeout = setTimeout(() => {
if (!hasShutdownCompleted) {
logger.error("Forcing shutdown after timeout - some connections may not have closed gracefully.");
process.exit(1);
}
}, serverConfig.shutdownTimeout);
// Destroy Hocuspocus server first
logger.info("Shutting down Hocuspocus server...");
let hocuspocusShutdownSuccessful = false;
try {
await this.hocusPocusServer.destroy();
hocuspocusShutdownSuccessful = true;
logger.info("HocusPocus server WebSocket connections closed gracefully.");
} catch (error) {
hocuspocusShutdownSuccessful = false;
logger.error("Error during hocuspocus server shutdown:", error);
// Continue with HTTP server shutdown even if Hocuspocus shutdown fails
} finally {
logger.info(
`Proceeding to HTTP server shutdown. Hocuspocus shutdown ${hocuspocusShutdownSuccessful ? "was successful" : "had errors"}.`
);
}
// Close HTTP server
try {
logger.info("Initiating HTTP server shutdown...");
this.httpServer.close(() => {
logger.info("HTTP server closed gracefully - all connections ended.");
// Clear the timeout since we're shutting down gracefully
clearTimeout(forceExitTimeout);
hasShutdownCompleted = true;
process.exit(0);
});
logger.info("HTTP server close initiated, waiting for connections to end...");
} catch (error) {
logger.error("Error during HTTP server shutdown:", error);
clearTimeout(forceExitTimeout);
process.exit(1);
}
};
}
}

View File

@@ -1,10 +1,15 @@
// types
import { TAdditionalDocumentTypes } from "@/plane-live/types/common.js";
import { TAdditionalDocumentTypes } from "@/plane-live/types/common";
export type TDocumentTypes = "project_page" | TAdditionalDocumentTypes;
export type HocusPocusServerContext = {
cookie: string;
projectId: string;
workspaceSlug: string;
documentType: TDocumentTypes;
userId: string;
agentId: string;
};
export type TConvertDocumentRequestBody = {

View File

@@ -0,0 +1,62 @@
import { HocusPocusServerContext, TDocumentTypes } from "@/core/types/common";
/**
* Parameters for document fetch operations
*/
export interface DocumentFetchParams {
context: HocusPocusServerContext;
pageId: string;
params: URLSearchParams;
}
/**
* Parameters for document store operations
*/
export interface DocumentStoreParams {
context: HocusPocusServerContext;
pageId: string;
state: any;
params: URLSearchParams;
}
/**
* Interface defining a document handler
*/
export interface DocumentHandler {
/**
* Fetch a document
*/
fetch: (params: DocumentFetchParams) => Promise<any>;
/**
* Store a document
*/
store: (params: DocumentStoreParams) => Promise<void>;
}
/**
* Handler context interface - extend this to add new criteria for handler selection
*/
export interface HandlerContext {
documentType?: TDocumentTypes;
agentId?: string;
}
/**
* Handler selector function type - determines if a handler should be used based on context
*/
export type HandlerSelector = (context: HandlerContext) => boolean;
/**
* Handler definition combining a selector and implementation
*/
export interface HandlerDefinition {
selector: HandlerSelector;
handler: DocumentHandler;
priority: number; // Higher number means higher priority
}
/**
* Type for a handler registration function
*/
export type RegisterHandler = (definition: HandlerDefinition) => void;

View File

@@ -1 +1,2 @@
export * from "../../ce/lib/authentication.js"
export * from "../../core/lib/authentication";

View File

@@ -1 +1,2 @@
export * from "../../ce/lib/fetch-document.js"
export * from "../../ce/lib/fetch-document";

View File

@@ -1 +1,2 @@
export * from "../../ce/lib/update-document.js"
export * from "../../ce/lib/update-document";

View File

@@ -1 +1 @@
export * from "../../ce/types/common.js"
export * from "../../ce/types/common"

52
live/src/env.ts Normal file
View File

@@ -0,0 +1,52 @@
import * as dotenv from "dotenv";
import { z } from "zod";
// Load environment variables from .env file
dotenv.config();
// Define environment schema with validation
const envSchema = z.object({
// Server configuration
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
PORT: z.string().default("3000").transform(Number),
LIVE_BASE_PATH: z.string().default("/live"),
// CORS configuration
CORS_ALLOWED_ORIGINS: z
.string()
.default("*"),
// Sentry configuration
LIVE_SENTRY_DSN: z.string().optional(),
LIVE_SENTRY_ENVIRONMENT: z.string().default("development"),
LIVE_SENTRY_TRACES_SAMPLE_RATE: z.string().default("0.5").transform(Number),
LIVE_SENTRY_RELEASE: z.string().optional(),
LIVE_SENTRY_RELEASE_VERSION: z.string().optional(),
// Compression options
COMPRESSION_LEVEL: z.string().default("6").transform(Number),
COMPRESSION_THRESHOLD: z.string().default("5000").transform(Number),
// Hocuspocus server configuration
HOCUSPOCUS_URL: z.string().optional(),
HOCUSPOCUS_USERNAME: z.string().optional(),
HOCUSPOCUS_PASSWORD: z.string().optional(),
// Graceful shutdown timeout
SHUTDOWN_TIMEOUT: z.string().default("10000").transform(Number),
});
// Validate the environment variables
function validateEnv() {
const result = envSchema.safeParse(process.env);
if (!result.success) {
console.error("❌ Invalid environment variables:", JSON.stringify(result.error.format(), null, 4));
process.exit(1);
}
return result.data;
}
// Export the validated environment
export const env = validateEnv();

View File

@@ -0,0 +1,92 @@
import { Router, Request } from "express";
import { WebSocket } from "ws";
import "reflect-metadata";
import Errors from "@/core/helpers/error-handling/error-factory";
import { ErrorCategory ,asyncHandler} from "@/core/helpers/error-handling/error-handler";
import { logger } from "@plane/logger";
export abstract class BaseController {
protected router: Router;
constructor() {
this.router = Router();
}
/**
* Get the base route for this controller
*/
protected getBaseRoute(): string {
return Reflect.getMetadata("baseRoute", this.constructor) || "";
}
/**
* Register all routes for this controller
*/
public registerRoutes(router: Router): void {
const baseRoute = this.getBaseRoute();
const proto = Object.getPrototypeOf(this);
const methods = Object.getOwnPropertyNames(proto).filter(
(item) => item !== "constructor" && typeof (this as any)[item] === "function"
);
methods.forEach((methodName) => {
const route = Reflect.getMetadata("route", proto, methodName) || "";
const method = Reflect.getMetadata("method", proto, methodName) as string;
const middlewares = Reflect.getMetadata("middlewares", proto, methodName) || [];
if (route && method) {
const fullRoute = baseRoute + route;
const handler = (this as any)[methodName].bind(this);
if (method === "ws") {
// Handle WebSocket routes with error handling for graceful failures
(router as any).ws(fullRoute, (ws: WebSocket, req: Request) => {
try {
handler(ws, req);
} catch (error: unknown) {
// Convert to AppError if needed
const appError = Errors.convertError(error instanceof Error ? error : new Error(String(error)), {
context: {
controller: this.constructor.name,
method: methodName,
route: fullRoute,
requestId: req.id || "unknown",
},
});
logger.error(`WebSocket error in ${this.constructor.name}.${methodName}`, {
error: appError,
});
// Send error message to client before closing
try {
ws.send(
JSON.stringify({
type: "error",
message:
appError.category === ErrorCategory.OPERATIONAL ? appError.message : "Internal server error",
})
);
} catch (sendError) {
// Ignore send errors at this point
}
// Close the connection with an appropriate code
ws.close(1011, appError.message);
}
});
} else {
(router as any)[method](fullRoute, ...middlewares, asyncHandler(handler));
}
}
});
// Mount this controller's router on the main router
router.use(baseRoute, this.router);
}
}
export abstract class BaseWebSocketController extends BaseController {
abstract handleConnection(ws: WebSocket, req: Request): void;
}

View File

@@ -0,0 +1,64 @@
import { Router, Request } from "express";
import { WebSocket } from "ws";
import { BaseController, BaseWebSocketController } from "./base.controller";
/**
* Controller factory function interface for creating controllers with dependencies
*/
export type ControllerFactory<T extends BaseController = BaseController> = (dependencies: any) => T;
/**
* Interface for controller registration with optional dependencies
*/
export interface ControllerRegistration {
Controller: new (...args: any[]) => BaseController;
dependencies?: string[];
}
/**
* Interface for WebSocket controller registration with optional dependencies
*/
export interface WebSocketControllerRegistration {
Controller: new (...args: any[]) => BaseWebSocketController;
dependencies?: string[];
}
/**
* Controller registry interface for organizing controllers
*/
export interface IControllerRegistry {
controllers: ControllerRegistration[];
webSocketControllers: WebSocketControllerRegistration[];
}
/**
* Service container interface for dependency management
*/
export interface IServiceContainer {
get(serviceName: string): any;
register(serviceName: string, service: any): void;
}
/**
* Base controller interface that all controllers should implement
*/
export interface IController {
/**
* Register routes for RESTful endpoints
* @param router Express router to register routes on
*/
registerRoutes?(router: Router): void;
}
/**
* WebSocket controller interface for websocket handlers
*/
export interface IWebSocketController extends IController {
/**
* Handle WebSocket connections
* @param ws WebSocket connection
* @param req Express request object
*/
handleConnection(ws: WebSocket, req: Request): void;
}

View File

@@ -0,0 +1,50 @@
import { Router } from "express";
import { IControllerRegistry, IServiceContainer, ControllerRegistration, WebSocketControllerRegistration } from "./controller.interface";
import "reflect-metadata";
// Define valid HTTP methods
type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'all' | 'use' | 'options' | 'head' | 'ws';
/**
* Register all controllers from a registry with dependency injection
* @param router Express router to register routes on
* @param registry Controller registry containing all controllers
* @param container Service container for dependency injection
*/
export function registerControllers(
router: Router,
registry: IControllerRegistry,
container: IServiceContainer
): void {
// Register REST controllers
registry.controllers.forEach((registration) => {
const { Controller, dependencies = [] } = registration;
const resolvedDependencies = dependencies.map(dep => container.get(dep));
const instance = new Controller(...resolvedDependencies);
instance.registerRoutes(router);
});
// Register WebSocket controllers
registry.webSocketControllers.forEach((registration) => {
const { Controller, dependencies = [] } = registration;
const resolvedDependencies = dependencies.map(dep => container.get(dep));
const instance = new Controller(...resolvedDependencies);
instance.registerRoutes(router);
});
}
/**
* Create a controller registry with the given controllers
* @param controllers Array of REST controller registrations
* @param webSocketControllers Array of WebSocket controller registrations
* @returns Controller registry
*/
export function createControllerRegistry(
controllers: ControllerRegistration[],
webSocketControllers: WebSocketControllerRegistration[] = []
): IControllerRegistry {
return {
controllers,
webSocketControllers,
};
}

View File

@@ -0,0 +1,78 @@
import "reflect-metadata";
import type { RequestHandler } from "express";
import { asyncHandler } from "@/core/helpers/error-handling/error-handler";
export const Controller = (baseRoute = ""): ClassDecorator => {
return function (target: object) {
Reflect.defineMetadata("baseRoute", baseRoute, target);
};
};
export const Get = (route: string): MethodDecorator => {
return function (target: object, propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
Reflect.defineMetadata("route", route, target, propertyKey);
Reflect.defineMetadata("method", "get", target, propertyKey);
};
};
export const Post = (route: string): MethodDecorator => {
return function (target: object, propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
Reflect.defineMetadata("route", route, target, propertyKey);
Reflect.defineMetadata("method", "post", target, propertyKey);
};
};
export const Put = (route: string): MethodDecorator => {
return function (target: object, propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
Reflect.defineMetadata("route", route, target, propertyKey);
Reflect.defineMetadata("method", "put", target, propertyKey);
};
};
export const Patch = (route: string): MethodDecorator => {
return function (target: object, propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
Reflect.defineMetadata("route", route, target, propertyKey);
Reflect.defineMetadata("method", "patch", target, propertyKey);
};
};
export const Delete = (route: string): MethodDecorator => {
return function (target: object, propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
Reflect.defineMetadata("route", route, target, propertyKey);
Reflect.defineMetadata("method", "delete", target, propertyKey);
};
};
export const Middleware = (middleware: RequestHandler): MethodDecorator => {
return function (target: object, propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
const middlewares = Reflect.getMetadata("middlewares", target, propertyKey) || [];
middlewares.push(middleware);
Reflect.defineMetadata("middlewares", middlewares, target, propertyKey);
};
};
export const WebSocket = (route: string): MethodDecorator => {
return function (target: object, propertyKey: string | symbol, _descriptor: PropertyDescriptor) {
Reflect.defineMetadata("route", route, target, propertyKey);
Reflect.defineMetadata("method", "ws", target, propertyKey);
};
};
/**
* Decorator to wrap controller methods with error handling
* This automatically catches and processes all errors using our error handling system
*/
export const CatchErrors = (): MethodDecorator => {
return function (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
// Only apply to methods that are not WebSocket handlers
const isWebSocketHandler = Reflect.getMetadata("method", target, propertyKey) === "ws";
if (typeof originalMethod === 'function' && !isWebSocketHandler) {
descriptor.value = asyncHandler(originalMethod);
}
return descriptor;
};
};

View File

@@ -0,0 +1,30 @@
import { IServiceContainer } from "./controller.interface";
/**
* Simple service container implementation for dependency injection
*/
export class ServiceContainer implements IServiceContainer {
private services: Map<string, any> = new Map();
/**
* Register a service in the container
* @param serviceName Name of the service
* @param service The service instance
*/
register(serviceName: string, service: any): void {
this.services.set(serviceName, service);
}
/**
* Get a service from the container
* @param serviceName Name of the service
* @returns The service instance
* @throws Error if service not found
*/
get(serviceName: string): any {
if (!this.services.has(serviceName)) {
throw new Error(`Service ${serviceName} not found in container`);
}
return this.services.get(serviceName);
}
}

27
live/src/sentry-config.ts Normal file
View File

@@ -0,0 +1,27 @@
import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
import { env } from "@/env";
import { logger } from "@plane/logger";
export const initializeSentry = () => {
if (!env.LIVE_SENTRY_DSN) {
logger.warn("Sentry DSN not configured");
return;
}
logger.info(`Initializing Sentry | Version:${env.LIVE_SENTRY_RELEASE_VERSION}`);
Sentry.init({
dsn: env.LIVE_SENTRY_DSN,
integrations: [Sentry.httpIntegration(), Sentry.expressIntegration(), nodeProfilingIntegration()],
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
environment: env.NODE_ENV,
release: env.LIVE_SENTRY_RELEASE_VERSION,
});
};
export const captureException = (err: Error, context?: Record<string, any>) => {
Sentry.captureException(err, context);
};
export const SentryInstance = Sentry;

View File

@@ -1,140 +1,174 @@
import * as Sentry from "@sentry/node";
import compression from "compression";
import cors from "cors";
import expressWs from "express-ws";
import express from "express";
import helmet from "helmet";
// config
import "@/core/config/sentry-config.js";
// hocuspocus server
import { getHocusPocusServer } from "@/core/hocuspocus-server.js";
// helpers
import { convertHTMLDocumentToAllFormats } from "@/core/helpers/convert-document.js";
import { logger, manualLogger } from "@/core/helpers/logger.js";
import { errorHandler } from "@/core/helpers/error-handler.js";
// types
import { TConvertDocumentRequestBody } from "@/core/types/common.js";
import type { Application, Request, Router } from "express";
import expressWs from "express-ws";
import type * as ws from "ws";
import type { Hocuspocus } from "@hocuspocus/server";
const app = express();
expressWs(app);
// Environment and configuration
import { serverConfig, configureServerMiddleware } from "./config/server-config";
import { initializeSentry } from "./sentry-config";
app.set("port", process.env.PORT || 3000);
// Core functionality
import { getHocusPocusServer } from "@/core/hocuspocus-server";
import { controllerRegistry } from "@/core/controller-registry";
import { ShutdownManager } from "@/core/shutdown-manager";
// Security middleware
app.use(helmet());
// Service and controller related
import { IControllerRegistry, IServiceContainer } from "./lib/controller.interface";
import { registerControllers } from "./lib/controller.utils";
import { ServiceContainer } from "./lib/service-container";
// Middleware for response compression
app.use(
compression({
level: 6,
threshold: 5 * 1000,
})
);
// Logging
import { logger } from "@plane/logger";
// Logging middleware
app.use(logger);
// Error handling
import { configureErrorHandlers } from "@/core/helpers/error-handling/error-handler";
import { handleError } from "@/core/helpers/error-handling/error-factory";
// Body parsing middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// WebSocket router type definition
interface WebSocketRouter extends Router {
ws: (_path: string, _handler: (ws: ws.WebSocket, req: Request) => void) => void;
}
// cors middleware
app.use(cors());
/**
* Main server class for the application
*/
export class Server {
private readonly app: Application;
private readonly port: number;
private hocusPocusServer!: Hocuspocus;
private controllerRegistry!: IControllerRegistry;
private serviceContainer: IServiceContainer;
const router = express.Router();
/**
* Creates an instance of the server class.
* @param port Optional port number, defaults to environment configuration
*/
constructor(port?: number) {
this.app = express();
this.serviceContainer = new ServiceContainer();
this.port = port || serverConfig.port;
const HocusPocusServer = await getHocusPocusServer().catch((err) => {
manualLogger.error("Failed to initialize HocusPocusServer:", err);
process.exit(1);
});
// Initialize express-ws after Express setup
expressWs(this.app as any);
router.get("/health", (_req, res) => {
res.status(200).json({ status: "OK" });
});
router.ws("/collaboration", (ws, req) => {
try {
HocusPocusServer.handleConnection(ws, req);
} catch (err) {
manualLogger.error("WebSocket connection error:", err);
ws.close();
// Configure server
this.setupSentry();
configureServerMiddleware(this.app);
}
});
router.post("/convert-document", (req, res) => {
const { description_html, variant } = req.body as TConvertDocumentRequestBody;
try {
if (description_html === undefined || variant === undefined) {
res.status(400).send({
message: "Missing required fields",
/**
* Get the Express application instance
* Useful for testing
*/
getApp(): Application {
return this.app;
}
/**
* Set up Sentry for error tracking
*/
private setupSentry(): void {
initializeSentry();
}
/**
* Initialize the server with all required components
* @returns The server instance for chaining
*/
async initialize() {
try {
// Initialize core services
await this.initializeServices();
// Initialize controllers
await this.initializeControllers();
// Set up routes
await this.setupRoutes();
// Set up error handlers
logger.info("Setting up error handlers");
configureErrorHandlers(this.app);
return this;
} catch (error) {
logger.error("Failed to initialize server:", error);
// This will always throw (never returns) - TypeScript correctly infers this
handleError(error, {
errorType: "internal",
component: "server",
operation: "initialize",
throw: true,
});
return;
}
const { description, description_binary } = convertHTMLDocumentToAllFormats({
document_html: description_html,
variant,
});
res.status(200).json({
description,
description_binary,
});
} catch (error) {
manualLogger.error("Error in /convert-document endpoint:", error);
res.status(500).send({
message: `Internal server error. ${error}`,
});
}
});
app.use(process.env.LIVE_BASE_PATH || "/live", router);
app.use((_req, res) => {
res.status(404).send("Not Found");
});
Sentry.setupExpressErrorHandler(app);
app.use(errorHandler);
const liveServer = app.listen(app.get("port"), () => {
manualLogger.info(`Plane Live server has started at port ${app.get("port")}`);
});
const gracefulShutdown = async () => {
manualLogger.info("Starting graceful shutdown...");
try {
// Close the HocusPocus server WebSocket connections
await HocusPocusServer.destroy();
manualLogger.info("HocusPocus server WebSocket connections closed gracefully.");
// Close the Express server
liveServer.close(() => {
manualLogger.info("Express server closed gracefully.");
process.exit(1);
});
} catch (err) {
manualLogger.error("Error during shutdown:", err);
process.exit(1);
}
// Forcefully shut down after 10 seconds if not closed
setTimeout(() => {
manualLogger.error("Forcing shutdown...");
process.exit(1);
}, 10000);
};
/**
* Initialize core services
*/
private async initializeServices() {
// Initialize the Hocuspocus server
this.hocusPocusServer = await getHocusPocusServer();
// Graceful shutdown on unhandled rejection
process.on("unhandledRejection", (err: any) => {
manualLogger.info("Unhandled Rejection: ", err);
manualLogger.info(`UNHANDLED REJECTION! 💥 Shutting down...`);
gracefulShutdown();
});
// Register services in the container
this.serviceContainer.register("hocuspocus", this.hocusPocusServer);
}
// Graceful shutdown on uncaught exception
process.on("uncaughtException", (err: any) => {
manualLogger.info("Uncaught Exception: ", err);
manualLogger.info(`UNCAUGHT EXCEPTION! 💥 Shutting down...`);
gracefulShutdown();
});
/**
* Initialize controllers
*/
private async initializeControllers() {
// Create controller registry with all controllers
this.controllerRegistry = controllerRegistry.createRegistry();
}
/**
* Set up API routes and WebSocket endpoints
*/
private async setupRoutes() {
try {
const router = express.Router() as WebSocketRouter;
// Register all controllers using the registry with the service container
registerControllers(router, this.controllerRegistry, this.serviceContainer);
// Mount the router on the base path
this.app.use(serverConfig.basePath, router);
} catch (error) {
handleError(error, {
errorType: "internal",
component: "server",
operation: "setupRoutes",
throw: true,
});
}
}
/**
* Start the server
* @returns HTTP Server instance
*/
async start() {
try {
const server = this.app.listen(this.port, () => {
logger.info(`Plane Live server has started at port ${this.port}`);
});
// Setup graceful shutdown
const shutdownManager = new ShutdownManager(this.hocusPocusServer, server);
shutdownManager.registerShutdownHandlers();
return server;
} catch (error) {
handleError(error, {
errorType: "service-unavailable",
component: "server",
operation: "start",
extraContext: { port: this.port },
throw: true,
});
}
}
}

26
live/src/start.ts Normal file
View File

@@ -0,0 +1,26 @@
import { Server } from "./server";
import { env } from "./env";
import { logger } from "@plane/logger";
/**
* The main entry point for the application
* Starts the server and handles any startup errors
*/
const startServer = async () => {
try {
// Log server startup details
logger.info(`Starting Plane Live server in ${env.NODE_ENV} environment`);
// Initialize and start the server
const server = await new Server().initialize();
await server.start();
logger.info(`Server running at base path: ${env.LIVE_BASE_PATH}`);
} catch (error) {
logger.error("Failed to start server:", error);
process.exit(1);
}
};
// Start the server
startServer();

View File

@@ -1,26 +1,38 @@
{
"extends": "@plane/typescript-config/base.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ES2015"],
"module": "ES2015",
"moduleResolution": "Bundler",
"lib": [
"ES2015"
],
"outDir": "./dist",
"rootDir": ".",
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@/plane-live/*": ["./src/ce/*"]
"@/*": [
"./src/*"
],
"@/plane-live/*": [
"./src/ce/*"
]
},
"removeComments": true,
"esModuleInterop": true,
"skipLibCheck": true,
"sourceMap": true,
"inlineSources": true,
// Set `sourceRoot` to "/" to strip the build path prefix
// from generated source code references.
// This improves issue grouping in Sentry.
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"sourceRoot": "/"
},
"include": ["src/**/*.ts", "tsup.config.ts"],
"exclude": ["./dist", "./build", "./node_modules"]
"include": [
"src/**/*.ts",
"tsup.config.ts"
],
"exclude": [
"./dist",
"./build",
"./node_modules"
]
}

View File

@@ -1,11 +1,17 @@
import { defineConfig, Options } from "tsup";
import { defineConfig } from "tsup";
export default defineConfig((options: Options) => ({
entry: ["src/server.ts"],
format: ["cjs", "esm"],
export default defineConfig({
entry: ["src/start.ts"],
format: ["esm", "cjs"],
dts: true,
clean: false,
external: ["react"],
injectStyle: true,
...options,
}));
splitting: false,
sourcemap: true,
clean: true,
minify: false,
target: "node18",
outDir: "dist",
env: {
NODE_ENV: process.env.NODE_ENV || "development",
},
watch: ["src/**/*.{ts,tsx}"],
});

View File

@@ -81,7 +81,7 @@
"@types/react": "^18.3.11",
"@types/react-dom": "^18.2.18",
"postcss": "^8.4.38",
"tsup": "^8.4.0",
"tsup": "8.3.0",
"typescript": "5.3.3"
},
"keywords": [

View File

@@ -22,7 +22,7 @@
"@plane/eslint-config": "*",
"@types/node": "^22.5.4",
"@types/react": "^18.3.11",
"tsup": "^8.4.0",
"tsup": "8.3.0",
"typescript": "^5.3.3"
}
}

View File

@@ -4,11 +4,18 @@
"license": "AGPL-3.0",
"description": "Logger shared across multiple apps internally",
"private": true,
"main": "./src/index.ts",
"types": "./src/index.ts",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist/**"
],
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"lint": "eslint src --ext .ts,.tsx",
"lint:errors": "eslint src --ext .ts,.tsx --quiet"
"lint:errors": "eslint src --ext .ts,.tsx --quiet",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"dependencies": {
"winston": "^3.17.0",
@@ -17,6 +24,21 @@
"devDependencies": {
"@plane/eslint-config": "*",
"@types/node": "^22.5.4",
"tsup": "8.3.0",
"typescript": "^5.3.3"
},
"tsup": {
"entry": [
"src/index.ts"
],
"format": [
"cjs",
"esm"
],
"dts": true,
"splitting": false,
"sourcemap": true,
"clean": true,
"minify": false
}
}

View File

@@ -1,2 +1,14 @@
/**
* @plane/logger
*
* A shared logger for Plane applications that provides consistent
* logging functionality across all services.
*/
// Export all components from the logger
export * from "./config";
export * from "./middleware";
// Export default logger instance if needed
import { logger } from "./config";
export default logger;

View File

@@ -71,7 +71,7 @@
"postcss-cli": "^11.0.0",
"postcss-nested": "^6.0.1",
"storybook": "^8.1.1",
"tsup": "^8.4.0",
"tsup": "8.3.0",
"typescript": "5.3.3"
}
}

View File

@@ -29,7 +29,7 @@
"@types/node": "^22.5.4",
"@types/react": "^18.3.11",
"@types/zxcvbn": "^4.4.5",
"tsup": "^8.4.0",
"tsup": "8.3.0",
"typescript": "^5.3.3"
}
}

8260
yarn.lock

File diff suppressed because it is too large Load Diff