Files
outline/server/utils/zod.ts
T
24f95cadc3 perf: Reduce server memory usage and add container memory best practices (#13042)
* 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>
2026-07-18 18:34:32 -04:00

77 lines
2.0 KiB
TypeScript

import emojiRegex from "emoji-regex";
import { z } from "zod";
import { iconNames } from "@shared/utils/IconNames";
import { UrlHelper } from "@shared/utils/UrlHelper";
/**
* Creates a zod enum schema from the keys of an object.
*
* @param input the object whose keys become the enum values.
* @returns a zod enum schema of the object's keys.
*/
export function zodEnumFromObjectKeys<
TI extends Record<string, unknown>,
K extends string & keyof TI = string & keyof TI,
>(input: TI) {
const keys = Object.keys(input) as [K, ...K[]];
return z.enum(keys);
}
/**
* Returns a zod schema that validates a model identifier, either a UUID or a
* URL slug.
*
* @returns a zod schema for identifiers.
*/
export const zodIdType = () =>
z.union([z.string().regex(UrlHelper.SLUG_URL_REGEX), z.uuid()], {
error: "Must be a valid UUID or slug",
});
/**
* Returns a zod schema that validates an icon value, either an emoji, a named
* icon from the icon library, or an attachment UUID.
*
* @returns a zod schema for icons.
*/
export const zodIconType = () =>
z.union([z.string().regex(emojiRegex()), z.enum(iconNames), z.uuid()]);
/**
* Returns a zod schema that validates an emoji value, either an emoji
* character or a custom emoji UUID.
*
* @returns a zod schema for emoji.
*/
export const zodEmojiType = () =>
z.union([z.string().regex(emojiRegex()), z.uuid()]);
/**
* Returns a zod schema that validates a share identifier, either a UUID or a
* share URL slug.
*
* @returns a zod schema for share identifiers.
*/
export const zodShareIdType = () =>
z.union([z.uuid(), z.string().regex(UrlHelper.SHARE_URL_SLUG_REGEX)]);
/**
* Returns a zod schema that validates an IANA timezone name.
*
* @returns a zod schema for timezones.
*/
export const zodTimezone = () =>
z.string().refine(
(timezone) => {
try {
Intl.DateTimeFormat(undefined, { timeZone: timezone });
return true;
} catch {
return false;
}
},
{
error: "invalid timezone",
}
);