TypeScript
fix(contracts): avoid Intl.Segmenter in monogram validation (Hermes crash) (#11984)
Aaron Queen6f7aaffe26f0
diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.tsindex dc8a0732198..5c7affdfaa1 100644--- a/packages/contracts/src/orchestration.ts+++ b/packages/contracts/src/orchestration.ts@@ -459,11 +459,33 @@ const ProjectLucideIconName = TrimmedNonEmptyString.check(const ProjectEmoji = TrimmedNonEmptyString.check(Schema.isMaxLength(32));-const monogramSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });+// Hermes (the Android/iOS JS runtime) has no Intl.Segmenter, and this module+// loads at app startup, so `new Intl.Segmenter()` crashes mobile on launch.+// Count grapheme clusters with a small approximation instead: a code point+// continues the current cluster when it is a combining mark, joiner,+// variation selector, or emoji modifier, or follows a ZWJ. This must stay+// runtime-independent (not `typeof Intl.Segmenter` feature detection) so the+// shared contract validates identically on server, web, and mobile.+const GRAPHEME_CONTINUATION = /[\p{M}\u200c\u200d\ufe0f\u{1F3FB}-\u{1F3FF}]/u;++const countGraphemes = (text: string): number => {+ let clusters = 0;+ let prevJoiner = false;+ for (const char of text) {+ if (clusters > 0 && (prevJoiner || GRAPHEME_CONTINUATION.test(char))) {+ prevJoiner = char === "\u200d";+ continue;+ }+ clusters += 1;+ prevJoiner = char === "\u200d";+ }+ return clusters;+};+export const ProjectMonogramText = TrimmedNonEmptyString.check(Schema.isMaxLength(32),Schema.isPattern(/^[\p{L}\p{N}][\p{L}\p{N}\p{M}\u200c\u200d]*$/u),- Schema.makeFilter((text) => Array.from(monogramSegmenter.segment(text)).length <= 2),+ Schema.makeFilter((text) => countGraphemes(text) <= 2),);export const ProjectIconOverride = Schema.Union([