mirror of
https://github.com/outline/outline.git
synced 2026-08-03 13:27:25 +03:00
* Reduce server memory usage and add container memory best practices Profiling a full-service boot showed ~300MB RSS per service process, much of it client-only dependencies pulled into the server module graph, and no heap limits applied in containers: - Import date-fns locales individually rather than via the locale index, which loaded all ~90 locales into every process - Load @sentry/react dynamically in insertFiles so the browser Sentry SDK stays out of the server-side editor graph - Replace class-validator isHexColor with the existing validateColorHex in the Highlight mark - Extract icon names into a standalone IconNames module so server-side schema validation no longer imports Font Awesome icon packs, with a test to keep it in sync with IconLibrary - Require S3Storage lazily so the AWS SDK and native CRT binding are only loaded when S3 file storage is configured - Default each forked service process to a V8 heap limit derived from the cgroup memory constraint, preventing several processes from together committing more memory than the container allows - Cap glibc malloc arenas in the Docker image to reduce resident memory fragmentation in multi-threaded processes Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa * Require both file storage backends lazily for consistency Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa * Load AWS SDK via dynamic imports compatible with test environment Requiring the storage TypeScript modules lazily broke under Vitest, which cannot resolve require() of source files and does not apply vi.mock to bare requires. Move the laziness into S3Storage instead: the module is imported statically, and the AWS SDK packages are loaded with dynamic imports on first use, including deferred creation of the S3 client. Also address review feedback: handle rejection of the dynamic Sentry import, and warn when the minimum per-process heap limit exceeds the memory budget of the container. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa * Consolidate S3 SDK loading into a single lazy accessor The commands are loaded together with the client in getS3() rather than as separate dynamic imports per method. The global S3Client test mock is now a class, as a mock function is not constructable when reached through a dynamic import. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa --------- Co-authored-by: Claude <noreply@anthropic.com>
67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
import "reflect-metadata";
|
|
import { EventEmitter } from "node:events";
|
|
import { afterAll, afterEach, beforeAll, beforeEach, vi } from "vitest";
|
|
import sharedEnv from "@shared/env";
|
|
import env from "@server/env";
|
|
import { server } from "./msw";
|
|
|
|
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
|
|
afterEach(() => server.resetHandlers());
|
|
afterAll(() => server.close());
|
|
|
|
// Increase the default max listeners for EventEmitter to prevent warnings in tests
|
|
// This needs to be done before any modules that use EventEmitter are loaded
|
|
EventEmitter.defaultMaxListeners = 100;
|
|
|
|
// Mock AWS SDK S3 client and related commands. The client must be a real
|
|
// class as it is instantiated with `new` via a dynamic import, which does not
|
|
// apply the spy-constructor interop that static imports receive.
|
|
vi.mock("@aws-sdk/client-s3", () => ({
|
|
S3Client: class MockS3Client {
|
|
send = vi.fn();
|
|
},
|
|
DeleteObjectCommand: vi.fn(),
|
|
GetObjectCommand: vi.fn(),
|
|
HeadObjectCommand: vi.fn(),
|
|
CopyObjectCommand: vi.fn(),
|
|
PutObjectCommand: vi.fn(),
|
|
ObjectCannedACL: {},
|
|
}));
|
|
|
|
vi.mock("@aws-sdk/lib-storage", () => ({
|
|
Upload: vi.fn(() => ({
|
|
done: vi.fn(),
|
|
})),
|
|
}));
|
|
|
|
vi.mock("@aws-sdk/s3-presigned-post", () => ({
|
|
createPresignedPost: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@aws-sdk/s3-request-presigner", () => ({
|
|
getSignedUrl: vi.fn(),
|
|
}));
|
|
|
|
// Initialize the database models. Loaded dynamically so the
|
|
// EventEmitter.defaultMaxListeners assignment above runs first; static imports
|
|
// would be hoisted ahead of it.
|
|
await import("@server/storage/database");
|
|
|
|
// Eagerly load plugin server entry points so that PluginManager.getHooks()
|
|
// returns the registered plugins. Vitest does not support require() of TS
|
|
// files with bare imports (e.g. `@server/...`), so we use Vite's
|
|
// import.meta.glob to load them through the Vite resolver instead.
|
|
const { PluginManager } = await import("@server/utils/PluginManager");
|
|
const pluginModules = import.meta.glob(
|
|
["../../plugins/*/server/*.{js,ts}", "!**/*.test.*", "!**/schema.*"],
|
|
{ eager: true }
|
|
);
|
|
void pluginModules;
|
|
// Mark as loaded so PluginManager.loadPlugins() (which uses require()) is a
|
|
// no-op during tests.
|
|
(PluginManager as unknown as { loaded: boolean }).loaded = true;
|
|
|
|
beforeEach(() => {
|
|
env.URL = sharedEnv.URL = "https://app.outline.dev";
|
|
});
|