Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): read the message list in bounded chunks

Yudhi Armyndharis63cc24d8e022
diff --git a/src/modules/message/message-list-media-budget.spec.ts b/src/modules/message/message-list-media-budget.spec.ts
new file mode 100644
index 000000000..3b58a79ed
--- /dev/null
+++ b/src/modules/message/message-list-media-budget.spec.ts
@@ -0,0 +1,193 @@
+import 'reflect-metadata';
+import { DataSource, FindManyOptions, Repository } from 'typeorm';
+import { BadRequestException } from '@nestjs/common';
+import { MessageService, spendInlineMediaBudget } from './message.service';
+import { Message, MessageDirection } from './entities/message.entity';
+import type { EngineRegistry } from '../../engine/engine-registry.service';
+import type { MessageProjector } from '../session/message-projector.service';
+import type { HookManager } from '../../core/hooks';
+import type { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';
+import type { SendPacingService } from './send-pacing.service';
+import type { MessageSendService } from './message-send.service';
+
+/**
+ * The inline media budget bounds the response; these cases pin that it bounds the READ too. A page
+ * used to be loaded whole (every row's base64 on the heap) and trimmed afterwards, so one request
+ * for a media-heavy chat held every payload at once. Driven against a real SQLite schema because the
+ * page is now picked by a raw id/length select, which only a real database can check.
+ */
+describe('message list reads inline media within the budget', () => {
+ const SESSION = 's1';
+ const MiB = 1024 * 1024;
+
+ let ds: DataSource;
+ let repository: Repository<Message>;
+ let service: MessageService;
+ let prevBudget: string | undefined;
+
+ const payload = (bytes: number, fill = 'A'): string => fill.repeat(bytes);
+
+ // Oldest first, one second apart, so the list (newest first) serves them in reverse.
+ const seed = async (rows: Array<{ id: string; metadata: Record<string, unknown> | null; chatId?: string }>) => {
+ for (const [i, row] of rows.entries()) {
+ await repository.insert({
+ id: row.id,
+ sessionId: SESSION,
+ chatId: row.chatId ?? '[email protected]',
+ direction: MessageDirection.INCOMING,
+ metadata: (row.metadata ?? undefined) as never,
+ createdAt: new Date(Date.UTC(2026, 0, 1, 0, 0, i)),
+ });
+ }
+ };
+
+ const media = (data: string, extra: Record<string, unknown> = {}) => ({
+ media: { mimetype: 'image/jpeg', filename: 'a.jpg', data, ...extra },
+ });
+
+ /** What the list used to return: every row loaded, then trimmed. */
+ const loadThenTrim = async (budget: number): Promise<Message[]> => {
+ const rows = await repository.find({
+ where: { sessionId: SESSION },
+ order: { createdAt: 'DESC' },
+ });
+ return spendInlineMediaBudget(rows, budget);
+ };
+
+ const strip = (rows: Message[]) => rows.map(r => ({ id: r.id, metadata: r.metadata }));
+
+ beforeEach(async () => {
+ prevBudget = process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES;
+ ds = new DataSource({ type: 'better-sqlite3', database: ':memory:', entities: [Message], synchronize: true });
+ await ds.initialize();
+ repository = ds.getRepository(Message);
+ service = new MessageService(
+ repository,
+ {} as EngineRegistry,
+ {} as MessageProjector,
+ {} as HookManager,
+ {
+ findLidsForPhone: () => Promise.resolve([]),
+ findPhoneForLid: () => Promise.resolve(null),
+ } as unknown as LidMappingStoreService,
+ {} as SendPacingService,
+ {} as MessageSendService,
+ );
+ });
+
+ afterEach(async () => {
+ if (prevBudget === undefined) delete process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES;
+ else process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES = prevBudget;
+ jest.restoreAllMocks();
+ await ds.destroy();
+ });
+
+ /** Oldest to newest. The newest is a URL pointer, so the allowance must skip it. */
+ const mixedPage = () => [
+ { id: 'old-big-1', metadata: { ...media(payload(3 * MiB)), quotedMessage: { body: 'a\u0000b' } } },
+ { id: 'old-big-2', metadata: media(payload(3 * MiB), { sizeBytes: 1234 }) },
+ { id: 'text', metadata: null },
+ { id: 'small', metadata: media(payload(40_000)) },
+ { id: 'not-a-string', metadata: { media: { mimetype: 'image/jpeg', data: 42 } } },
+ { id: 'big-3', metadata: media(payload(3 * MiB)) },
+ { id: 'huge', metadata: media(payload(5 * MiB)) },
+ { id: 'pointer', metadata: media('HTTPS://cdn.example/a.jpg') },
+ ];
+
+ it('returns exactly what loading the page and trimming it returned', async () => {
+ process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES = String(4 * MiB);
+ await seed(mixedPage());
+ const expected = strip(await loadThenTrim(4 * MiB));
+
+ const { messages, total } = await service.getMessages(SESSION, { limit: 100 });
+
+ expect(total).toBe(8);
+ expect(strip(messages)).toEqual(expected);
+ // Sanity on the fixture: the allowance let the newest payload through, the rest are markers.
+ const byId = new Map(messages.map(m => [m.id, (m.metadata as { media?: Record<string, unknown> } | null)?.media]));
+ expect(byId.get('pointer')?.data).toBe('HTTPS://cdn.example/a.jpg');
+ expect(byId.get('huge')?.data).toHaveLength(5 * MiB);
+ expect(byId.get('big-3')).toMatchObject({ omitted: true, mimetype: 'image/jpeg', filename: 'a.jpg' });
+ expect(byId.get('old-big-2')).toMatchObject({ omitted: true, sizeBytes: 1234 });
+ expect(messages.find(m => m.id === 'old-big-1')?.metadata).toMatchObject({ quotedMessage: { body: 'a\u0000b' } });
+ });
+
+ it('matches the old output with inlineMedia=false, where every payload is omitted', async () => {
+ await seed(mixedPage());
+ const expected = strip(await loadThenTrim(0));
+
+ const { messages } = await service.getMessages(SESSION, { limit: 100, inlineMedia: false });
+
+ expect(strip(messages)).toEqual(expected);
+ });
+
+ it('never loads more stored metadata at once than the budget or one row', async () => {
+ const budget = 4 * MiB;
+ process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES = String(budget);
+ await seed(Array.from({ length: 10 }, (_, i) => ({ id: `m${i}`, metadata: media(payload(3 * MiB)) })));
+ const find = jest.spyOn(repository, 'find');
+
+ const { messages } = await service.getMessages(SESSION, { limit: 100 });
+
+ expect(messages).toHaveLength(10);
+ const loads = find.mock.results.map(r => r.value as Promise<Message[]>);
+ const perCall = await Promise.all(loads);
+ for (const rows of perCall) {
+ // With 3 MiB rows under a 4 MiB chunk bound, each read may hold one row, never the page.
+ expect(rows.length).toBeLessThanOrEqual(1);
+ }
+ expect(perCall.length).toBe(10);
+ });
+
+ it('reads a page of plain text in one query', async () => {
+ await seed(Array.from({ length: 20 }, (_, i) => ({ id: `t${i}`, metadata: null })));
+ const find = jest.spyOn(repository, 'find');
+
+ const { messages, total } = await service.getMessages(SESSION, { limit: 5, offset: 5 });
+
+ expect(total).toBe(20);
+ expect(messages.map(m => m.id)).toEqual(['t14', 't13', 't12', 't11', 't10']);
+ expect(find).toHaveBeenCalledTimes(1);
+ });
+
+ it('serves the same rows through the `after` cursor and keeps the 400 on an unknown one', async () => {
+ await seed(Array.from({ length: 6 }, (_, i) => ({ id: `c${i}`, metadata: media(payload(10)) })));
+
+ const { messages, total } = await service.getMessages(SESSION, { limit: 2, after: 'c4' });
+
+ expect(total).toBe(6);
+ expect(messages.map(m => m.id)).toEqual(['c3', 'c2']);
+ await expect(service.getMessages(SESSION, { after: 'nope' })).rejects.toThrow(BadRequestException);
+ });
+
+ it('applies the chat filter to the page and the total', async () => {
+ await seed([
+ { id: 'a1', metadata: null, chatId: '[email protected]' },
+ { id: 'b1', metadata: null, chatId: '[email protected]' },
+ { id: 'a2', metadata: null, chatId: '[email protected]' },
+ ]);
+
+ const { messages, total } = await service.getMessages(SESSION, { chatId: '[email protected]' });
+
+ expect(total).toBe(2);
+ expect(messages.map(m => m.id)).toEqual(['a2', 'a1']);
+ });
+
+ it('leaves out a row deleted after the page was picked', async () => {
+ await seed([
+ { id: 'keep', metadata: null },
+ { id: 'gone', metadata: null },
+ ]);
+ const find = repository.find.bind(repository);
+ jest.spyOn(repository, 'find').mockImplementation(async (options?: FindManyOptions<Message>) => {
+ await repository.delete({ id: 'gone' });
+ return find(options);
+ });
+
+ const { messages } = await service.getMessages(SESSION);
+
+ expect(messages.map(m => m.id)).toEqual(['keep']);
+ });
+});
diff --git a/src/modules/message/message-list-ordering.pg.spec.ts b/src/modules/message/message-list-ordering.pg.spec.ts
index c3560c56b..a425e32dc 100644
--- a/src/modules/message/message-list-ordering.pg.spec.ts
+++ b/src/modules/message/message-list-ordering.pg.spec.ts
@@ -196,4 +196,40 @@ const POSTGRES_ENABLED = process.env.DATABASE_TYPE === 'postgres';
);
});
});
+
+ /**
+ * The page is picked by id, sized by a raw `OCTET_LENGTH(metadata)` select and read back in chunks, so
+ * the dialect SQL and the order restore after the IN read are only proven against the real server.
+ */
+ it('reads a media page in order and omits the payloads past the budget', async () => {
+ const MEDIA_SESSION = 'sess-media-budget';
+ const prev = process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES;
+ process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES = String(3000);
+ try {
+ await repository.insert(
+ Array.from({ length: 6 }, (_, i) => ({
+ id: randomUUID(),
+ sessionId: MEDIA_SESSION,
+ chatId: '[email protected]',
+ body: `media-${i}`,
+ direction: MessageDirection.INCOMING,
+ metadata: { media: { mimetype: 'image/jpeg', data: 'A'.repeat(1000) } },
+ createdAt: new Date(Date.UTC(2026, 2, 1, 0, 0, i)),
+ })),
+ );
+
+ const { messages, total } = await service.getMessages(MEDIA_SESSION, { limit: 100 });
+
+ expect(total).toBe(6);
+ expect(messages.map(m => m.body)).toEqual(['media-5', 'media-4', 'media-3', 'media-2', 'media-1', 'media-0']);
+ const inline = messages.map(m => typeof (m.metadata as { media: { data?: unknown } }).media.data === 'string');
+ expect(inline).toEqual([true, true, true, false, false, false]);
+ } finally {
+ if (prev === undefined) delete process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES;
+ else process.env.MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES = prev;
+ await repository.delete({ sessionId: MEDIA_SESSION });
+ }
+ });
});
diff --git a/src/modules/message/message.service.spec.ts b/src/modules/message/message.service.spec.ts
index b4a2d9915..3ea933499 100644
--- a/src/modules/message/message.service.spec.ts
+++ b/src/modules/message/message.service.spec.ts
@@ -25,6 +25,30 @@ const inertPacing = (): SendPacingService =>
recordSendSuccess: jest.fn(),
}) as unknown as SendPacingService;
+/**
+ * Completes a query-builder fake with the list read getMessages performs: a count, the page as
+ * id/length pairs, then the rows themselves by id. `rows` answers with what the fake's filters match.
+ */
+function withListRead<T extends object>(qb: T, repository: Partial<Repository<Message>>, rows: () => Message[]): T {
+ Object.assign(qb, {
+ select: jest.fn().mockReturnValue(qb),
+ addSelect: jest.fn().mockReturnValue(qb),
+ clone: jest.fn().mockReturnValue(qb),
+ getCount: jest.fn().mockImplementation(() => Promise.resolve(rows().length)),
+ getRawMany: jest
+ .fn()
+ .mockImplementation(() =>
+ Promise.resolve(rows().map(r => ({ id: r.id, metadataLength: JSON.stringify(r.metadata ?? null).length }))),
+ ),
+ });
+ repository.find = jest
+ .fn()
+ .mockImplementation(({ where }: { where: { id: FindOperator<string> } }) =>
+ Promise.resolve(rows().filter(r => inList(r.id, where.id))),
+ );
+ return qb;
+}
+
function createMockEngine() {
return {
reactToMessage: jest.fn().mockResolvedValue(undefined),
@@ -156,7 +180,6 @@ describe('MessageService', () => {
skip: jest.Mock;
take: jest.Mock;
andWhere: jest.Mock;
- getManyAndCount: jest.Mock;
}
const makeQb = (): QbMock => {
const qb: QbMock = {
@@ -166,7 +189,6 @@ describe('MessageService', () => {
skip: jest.fn(),
take: jest.fn(),
andWhere: jest.fn(),
- getManyAndCount: jest.fn().mockResolvedValue([[], 0]),
};
qb.where.mockReturnValue(qb);
qb.orderBy.mockReturnValue(qb);
@@ -174,7 +196,7 @@ describe('MessageService', () => {
qb.skip.mockReturnValue(qb);
qb.take.mockReturnValue(qb);
qb.andWhere.mockReturnValue(qb);
- return qb;
+ return withListRead(qb, repository, () => []);
};
it('falls back to defaults on NaN limit/offset (never take(NaN))', async () => {
@@ -197,21 +219,21 @@ describe('MessageService', () => {
// ── getMessages keyset cursor ─────────────────────────────────────
describe('getMessages anchors on `after` instead of a count', () => {
- /** The cursor path clones for the count and reads rows separately, so getManyAndCount is unused. */
+ /** The cursor path counts the whole filter match (7 here), then reads the page after the anchor. */
const makeCursorQb = (rows: Message[]) => {
- const qb = {
- where: jest.fn().mockReturnThis(),
- orderBy: jest.fn().mockReturnThis(),
- addOrderBy: jest.fn().mockReturnThis(),
- skip: jest.fn().mockReturnThis(),
- take: jest.fn().mockReturnThis(),
- andWhere: jest.fn().mockReturnThis(),
- clone: jest.fn(),
- getCount: jest.fn().mockResolvedValue(7),
- getMany: jest.fn().mockResolvedValue(rows),
- getManyAndCount: jest.fn(),
- };
- qb.clone.mockReturnValue(qb);
+ const qb = withListRead(
+ {
+ where: jest.fn().mockReturnThis(),
+ orderBy: jest.fn().mockReturnThis(),
+ addOrderBy: jest.fn().mockReturnThis(),
+ skip: jest.fn().mockReturnThis(),
+ take: jest.fn().mockReturnThis(),
+ andWhere: jest.fn().mockReturnThis(),
+ },
+ repository,
+ () => rows,
+ ) as unknown as Record<string, jest.Mock>;
+ qb.getCount.mockResolvedValue(7);
return qb;
};
@@ -222,7 +244,6 @@ describe('MessageService', () => {
const result = await service.getMessages('sess-1', { after: 'm-1', offset: 500 });
expect(qb.skip).not.toHaveBeenCalled();
- expect(qb.getManyAndCount).not.toHaveBeenCalled();
const [clause, params] = qb.andWhere.mock.calls[0] as [string, Record<string, unknown>];
// rowid, not id: the stub repository carries no manager, which reads as "not postgres".
expect(clause).toContain('(message.createdAt, message.rowid) <');
@@ -309,17 +330,14 @@ describe('MessageService', () => {
if (params?.authorFroms) authorFroms = params.authorFroms;
return qb;
}),
- getManyAndCount: jest.fn().mockImplementation(() => {
- const matched =
- froms || authorFroms
- ? rows.filter(
- r => froms?.includes(r.from) || (r.author != null && (authorFroms?.includes(r.author) ?? false)),
- )
- : rows;
- return Promise.resolve([matched, matched.length]);
- }),
};
- return qb;
+ return withListRead(qb, repository, () =>
+ froms || authorFroms
+ ? rows.filter(
+ r => froms?.includes(r.from) || (r.author != null && (authorFroms?.includes(r.author) ?? false)),
+ )
+ : rows,
+ );
};
it('returns the lid-authored message once the table maps the lid to that phone (the hit)', async () => {
@@ -436,18 +454,17 @@ describe('MessageService', () => {
return qb;
},
),
- getManyAndCount: jest.fn().mockImplementation(() => {
- let matched = rows;
- if (chatIds) matched = matched.filter(r => chatIds!.includes(r.chatId));
- if (froms || authorFroms) {
- matched = matched.filter(
- r => froms?.includes(r.from) || (r.author != null && (authorFroms?.includes(r.author) ?? false)),
- );
- }
- return Promise.resolve([matched, matched.length]);
- }),
};
- return qb;
+ return withListRead(qb, repository, () => {
+ let matched = rows;
+ if (chatIds) matched = matched.filter(r => chatIds!.includes(r.chatId));
+ if (froms || authorFroms) {
+ matched = matched.filter(
+ r => froms?.includes(r.from) || (r.author != null && (authorFroms?.includes(r.author) ?? false)),
+ );
+ }
+ return matched;
+ });
};
it('returns group messages authored by the filtered phone (matched via author, not from)', async () => {
@@ -505,9 +522,8 @@ describe('MessageService', () => {
return qb;
},
),
- getManyAndCount: jest.fn().mockResolvedValue([[], 0]),
};
- return { qb, captured };
+ return { qb: withListRead(qb, repository, () => []), captured };
};
it('expands a @lid chatId to the phone the table names, not a stale cached one', async () => {
@@ -632,12 +648,8 @@ describe('MessageService', () => {
if (params?.chatIds) chatIds = params.chatIds;
return qb;
}),
- getManyAndCount: jest.fn().mockImplementation(() => {
- const matched = chatIds && chatIds.includes(stored.chatId) ? [stored] : [];
- return Promise.resolve([matched, matched.length]);
- }),
};
- return qb;
+ return withListRead(qb, repository, () => (chatIds && chatIds.includes(stored.chatId) ? [stored] : []));
};
it('returns a @s.whatsapp.net-stored message when filtering by the neutral @c.us chat id', async () => {
@@ -1084,9 +1096,10 @@ describe('MessageService', () => {
addOrderBy: jest.fn().mockReturnThis(),
skip: jest.fn().mockReturnThis(),
take: jest.fn().mockReturnThis(),
- getManyAndCount: jest.fn().mockResolvedValue([rows, 100]),
};
- (repository.createQueryBuilder as unknown as jest.Mock).mockReturnValue(builder);
+ (repository.createQueryBuilder as unknown as jest.Mock).mockReturnValue(
+ withListRead(builder, repository, () => rows),
+ );
const result = await service.getMessages('sess-1', { limit: 100 });
@@ -1120,9 +1133,10 @@ describe('MessageService', () => {
addOrderBy: jest.fn().mockReturnThis(),
skip: jest.fn().mockReturnThis(),
take: jest.fn().mockReturnThis(),
- getManyAndCount: jest.fn().mockResolvedValue([rows, 3]),
};
- (repository.createQueryBuilder as unknown as jest.Mock).mockReturnValue(builder);
+ (repository.createQueryBuilder as unknown as jest.Mock).mockReturnValue(
+ withListRead(builder, repository, () => rows),
+ );
const result = await service.getMessages('sess-1', { limit: 100, inlineMedia: false });
diff --git a/src/modules/message/message.service.ts b/src/modules/message/message.service.ts
index 3b0f614dc..f3e5066b8 100644
--- a/src/modules/message/message.service.ts
+++ b/src/modules/message/message.service.ts
@@ -57,6 +57,9 @@ export interface GetMessagesOptions {
* bounded upstream by `capInboundMedia` at MEDIA_DOWNLOAD_MAX_BYTES (50 MiB by default, ~68 MiB once
* base64-encoded). That is well inside V8's string ceiling and is the point: the alternative is a
* large photo no client can ever read back. Raising MEDIA_DOWNLOAD_MAX_BYTES raises this too.
+ *
+ * It bounds the read as well: getMessages loads a page in chunks and drops each omitted payload
+ * before reading the next chunk, so payloads past the budget are never all held at once.
*/
export const DEFAULT_MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES = 8 * 1024 * 1024;
@@ -69,45 +72,68 @@ export function resolveMessageListInlineMediaBudgetBytes(): number {
const MEDIA_URL_POINTER = /^https?:\/\//i;
/**
- * Spend the budget over an already-ordered (newest-first) page, replacing each payload past it with
- * the engine's own `{ omitted: true, sizeBytes }` marker — the same shape `capInboundMedia` writes
- * when inbound media is skipped on the way in, so a trimmed row is not a new shape consumers must
- * learn. Mutates and returns the rows.
- *
- * A budget, not a blanket strip: the recent media a caller is most likely reading still arrives
- * inline, and anything dropped remains fetchable from GET /:chatId/:messageId/media.
+ * The engine's own `{ omitted: true, sizeBytes }` marker for a media object whose payload is dropped,
+ * the same shape `capInboundMedia` writes when inbound media is skipped on the way in, so a trimmed
+ * row is not a new shape consumers must learn. Every descriptive field except `data` survives.
*/
-export function spendInlineMediaBudget(messages: Message[], budgetBytes: number): Message[] {
+export function omitInlineMedia(media: { data: string; sizeBytes?: number }): Record<string, unknown> {
+ const { data, ...withoutPayload } = media;
+ return {
+ ...withoutPayload,
+ omitted: true,
+ // Decoded bytes, matching what capInboundMedia reports: the caller asked how big it WAS.
+ sizeBytes: media.sizeBytes ?? Buffer.byteLength(data, 'base64'),
+ };
+}
+
+/**
+ * Stateful form of spendInlineMediaBudget: feed it the rows of one page newest-first, one at a time,
+ * so a caller can drop each omitted payload before it reads the next row.
+ */
+export function createInlineMediaSpender(budgetBytes: number): (message: Message) => void {
let spent = 0;
- for (const message of messages) {
+ return message => {
const metadata = message.metadata as Record<string, unknown> | null | undefined;
- if (!metadata || typeof metadata !== 'object') continue;
+ if (!metadata || typeof metadata !== 'object') return;
const media = metadata.media as { data?: unknown; sizeBytes?: number } | null | undefined;
- if (!media || typeof media.data !== 'string' || MEDIA_URL_POINTER.test(media.data)) continue;
+ if (!media || typeof media.data !== 'string' || MEDIA_URL_POINTER.test(media.data)) return;
const encoded = Buffer.byteLength(media.data, 'utf8');
// The newest payload is always let through when inlining is enabled at all. Without this an
// item larger than the whole budget was omitted even as the ONLY media on the page, so a single
- // large photo or video — well inside the bytes the gateway stores inline — could never be read
+ // large photo or video (well inside the bytes the gateway stores inline) could never be read
// back through this route: the dashboard thread has no other media source and caches with
// staleTime: Infinity, leaving a permanent placeholder for media WhatsApp displays.
// A budget of 0 means "do not inline", not "a very small budget", so it grants no allowance.
const allowanceApplies = spent === 0 && budgetBytes > 0;
if (spent + encoded <= budgetBytes || allowanceApplies) {
spent += encoded;
- continue;
+ return;
}
- const { data, ...withoutPayload } = media;
- metadata.media = {
- ...withoutPayload,
- omitted: true,
- // Decoded bytes, matching what capInboundMedia reports — the caller asked how big it WAS.
- sizeBytes: media.sizeBytes ?? Buffer.byteLength(data, 'base64'),
- };
- }
+ metadata.media = omitInlineMedia({ ...media, data: media.data });
+ };
+}
+
+/**
+ * Spend the budget over an already-ordered (newest-first) page, replacing each payload past it with
+ * the omitted marker (see omitInlineMedia). Mutates and returns the rows.
+ *
+ * A budget, not a blanket strip: the recent media a caller is most likely reading still arrives
+ * inline, and anything dropped remains fetchable from GET /:chatId/:messageId/media.
+ */
+export function spendInlineMediaBudget(messages: Message[], budgetBytes: number): Message[] {
+ messages.forEach(createInlineMediaSpender(budgetBytes));
return messages;
}
+/**
+ * Floor for the bytes of stored metadata one message-list read loads at once. The page is read in
+ * chunks of at most `max(budget, this)` metadata bytes (at least one row each), and each chunk's
+ * omitted payloads are dropped before the next chunk is read, so the heap holds about one chunk plus
+ * what the budget keeps instead of every payload on the page.
+ */
+const MESSAGE_LIST_READ_CHUNK_MIN_BYTES = 1024 * 1024;
+
/** Pin window applied when the caller does not choose one — WhatsApp's own default of 24h. */
export const DEFAULT_PIN_DURATION_SECONDS = 86400;
@@ -320,9 +346,10 @@ export class MessageService implements PluginMessagePort {
// what an opted-out caller asks for, so the flag picks the budget rather than a second code path.
const inlineMediaBudget = inlineMedia === false ? 0 : resolveMessageListInlineMediaBudgetBytes();
+ // `total` keeps its documented meaning, rows matching the filters, so count before narrowing.
+ const total = await query.clone().getCount();
+
if (after !== undefined) {
- // `total` keeps its documented meaning, rows matching the filters, so count before narrowing.
- const total = await query.clone().getCount();
// The anchor's sort key is resolved INSIDE the statement. Carrying it in the cursor instead
// would mean round-tripping a timestamp through JSON, and neither dialect survives that:
// SQLite holds two text shapes for one instant (`datetime('now')` writes 19 chars, a stamped
@@ -335,21 +362,91 @@ export class MessageService implements PluginMessagePort {
'WHERE anchor."id" = :after AND anchor."sessionId" = :sessionId)',
{ after, sessionId },
);
- const messages = await query.getMany();
- // An anchor that does not exist (or belongs to another session) makes the row comparison NULL,
- // which returns zero rows and reads exactly like the end of the history. Only pay for the
- // lookup on an empty page, and turn that silent truncation into a loud 400.
- if (messages.length === 0 && !(await this.messageRepository.exists({ where: { id: after, sessionId } }))) {
- throw new BadRequestException(`Unknown cursor '${after}' for this session`);
- }
- return { messages: spendInlineMediaBudget(messages, inlineMediaBudget), total };
}
- const [messages, total] = await query.getManyAndCount();
+ // The page is picked by id only. Each row's metadata can hold a whole base64 payload, so the
+ // rows themselves are read in bounded chunks below, sized from a second, id-keyed statement.
+ // The size stays out of the ordered one: a sort evaluates its result columns for every matching
+ // row, so a size there would be computed across the whole chat, not just this page.
+ const pageIds = (await query.select('message.id', 'id').getRawMany<{ id: string }>()).map(r => r.id);
+ const page = await this.readMetadataSizes(sessionId, pageIds);
+
+ // An anchor that does not exist (or belongs to another session) makes the row comparison NULL,
+ // which returns zero rows and reads exactly like the end of the history. Only pay for the
+ // lookup on an empty page, and turn that silent truncation into a loud 400.
+ if (
+ after !== undefined &&
+ page.length === 0 &&
+ !(await this.messageRepository.exists({ where: { id: after, sessionId } }))
+ ) {
+ throw new BadRequestException(`Unknown cursor '${after}' for this session`);
+ }
+
// The 1..100 clamp above bounds the ROW COUNT, not the response: each row carries its inline
// base64 in metadata.media.data. Spent newest-first (the query orders createdAt DESC), so the
// most recently viewed media still arrives inline and the rest keeps its omitted marker.
- return { messages: spendInlineMediaBudget(messages, inlineMediaBudget), total };
+ return { messages: await this.readPageSpendingBudget(sessionId, page, inlineMediaBudget), total };
+ }
+
+ /**
+ * The stored metadata size of each page row, in page order. OCTET_LENGTH, not LENGTH: both
+ * SQLite and PostgreSQL answer it from the stored header, while a character count has to read
+ * (and on PostgreSQL detoast) every payload in full, which the chunked read then does again.
+ */
+ private async readMetadataSizes(
+ sessionId: string,
+ pageIds: string[],
+ ): Promise<Array<{ id: string; metadataLength: number | string | null }>> {
+ if (pageIds.length === 0) return [];
+ const sizes = await this.messageRepository
+ .createQueryBuilder('message')
+ .select('message.id', 'id')
+ .addSelect('OCTET_LENGTH(message.metadata)', 'metadataLength')
+ .where('message.id IN (:...pageIds)', { pageIds })
+ .andWhere('message.sessionId = :sessionId', { sessionId })
+ .getRawMany<{ id: string; metadataLength: number | string | null }>();
+ const sizeById = new Map(sizes.map(r => [r.id, r.metadataLength]));
+ return pageIds.map(id => ({ id, metadataLength: sizeById.get(id) ?? null }));
+ }
+
+ /**
+ * Load the rows of a page in its order, in chunks of at most `max(budget, 1 MiB)` stored metadata
+ * bytes (at least one row per chunk), spending the inline media budget on each chunk before the
+ * next is read. Loading the whole page at once put every payload on the heap before the budget
+ * dropped most of them. A row deleted since the page was picked is left out.
+ */
+ private async readPageSpendingBudget(
+ sessionId: string,
+ page: Array<{ id: string; metadataLength: number | string | null }>,
+ budgetBytes: number,
+ ): Promise<Message[]> {
+ const spend = createInlineMediaSpender(budgetBytes);
+ const chunkLimit = Math.max(budgetBytes, MESSAGE_LIST_READ_CHUNK_MIN_BYTES);
+ const messages: Message[] = [];
+ let chunk: string[] = [];
+ let chunkBytes = 0;
+
+ const flush = async (): Promise<void> => {
+ const rows = await this.messageRepository.find({ where: { id: In(chunk), sessionId } });
+ const byId = new Map(rows.map(row => [row.id, row]));
+ for (const id of chunk) {
+ const row = byId.get(id);
+ if (!row) continue;
+ spend(row);
+ messages.push(row);
+ }
+ chunk = [];
+ chunkBytes = 0;
+ };
+
+ for (const { id, metadataLength } of page) {
+ const bytes = Number(metadataLength) || 0;
+ if (chunk.length > 0 && chunkBytes + bytes > chunkLimit) await flush();
+ chunk.push(id);
+ chunkBytes += bytes;
+ }
+ if (chunk.length > 0) await flush();
+ return messages;
}
/**