feat: add async SQLite on Node 24 and improve messaging diagnostics (#520)
Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics. Preserve migration, restart, and concurrency coverage with the async SQLite driver.
This commit is contained in:
1 parent
20f48d11ec
commit
c7428363cb
170 files changed
+7748
-3913
No files matched your search
@@ -62,13 +62,14 @@ function converterContext(): ConverterContext {
|
||||
};
|
||||
}
|
||||
|
||||
function makeStore(): { store: MessageStore; close(): void } {
|
||||
async function makeStore(): Promise<{ store: MessageStore; close(): Promise<void> }> {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-history-sync-'));
|
||||
const store = new MessageStore(path.join(dir, 'messages.db'));
|
||||
await store.initialization;
|
||||
return {
|
||||
store,
|
||||
close: () => {
|
||||
store.close();
|
||||
close: async () => {
|
||||
await store.close();
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
},
|
||||
};
|
||||
@@ -122,10 +123,10 @@ function makeBridge(
|
||||
} as unknown as BridgeInterface;
|
||||
}
|
||||
|
||||
const resources: Array<ReturnType<typeof makeStore>> = [];
|
||||
afterEach(() => {
|
||||
const resources: Array<Awaited<ReturnType<typeof makeStore>>> = [];
|
||||
afterEach(async () => {
|
||||
for (const resource of resources.splice(0)) {
|
||||
resource.close();
|
||||
await resource.close();
|
||||
}
|
||||
|
||||
vi.restoreAllMocks();
|
||||
@@ -139,7 +140,7 @@ describe('login history sync', () => {
|
||||
delays.push(ms);
|
||||
},
|
||||
});
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const bridge = makeBridge();
|
||||
|
||||
@@ -197,7 +198,7 @@ describe('login history sync', () => {
|
||||
groupPage,
|
||||
privateLatestPage,
|
||||
});
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
|
||||
@@ -242,7 +243,7 @@ describe('login history sync', () => {
|
||||
friendCount: LOGIN_HISTORY_SYNC_LIMITS.scannedPrivateUsers + 1,
|
||||
probe,
|
||||
});
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
|
||||
@@ -266,7 +267,7 @@ describe('login history sync', () => {
|
||||
});
|
||||
|
||||
it('fills a known local gap from its oldest missing sequence', async () => {
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const local = groupMessage(30001, 50);
|
||||
const localEvent = {
|
||||
@@ -277,7 +278,7 @@ describe('login history sync', () => {
|
||||
message_seq: 50,
|
||||
group_id: 30001,
|
||||
};
|
||||
resource.store.storeEvent(123, true, 30001, 50, 'group_message', localEvent);
|
||||
await resource.store.storeEvent(123, true, 30001, 50, 'group_message', localEvent);
|
||||
|
||||
const groupPage = vi.fn(async () => []);
|
||||
const bridge = makeBridge({
|
||||
@@ -304,9 +305,9 @@ describe('login history sync', () => {
|
||||
});
|
||||
|
||||
it('backfills past a newer metadata-only authoritative row', async () => {
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
resource.store.storeEvent(123, true, 30001, 50, 'group_message', {
|
||||
await resource.store.storeEvent(123, true, 30001, 50, 'group_message', {
|
||||
time: 1_700_000_050,
|
||||
post_type: 'message',
|
||||
message_type: 'group',
|
||||
@@ -314,7 +315,7 @@ describe('login history sync', () => {
|
||||
message_seq: 50,
|
||||
group_id: 30001,
|
||||
});
|
||||
resource.store.storeMeta(124, {
|
||||
await resource.store.storeMeta(124, {
|
||||
isGroup: true,
|
||||
targetId: 30001,
|
||||
sequence: 100,
|
||||
@@ -324,8 +325,8 @@ describe('login history sync', () => {
|
||||
random: 0,
|
||||
timestamp: 1_700_000_100,
|
||||
});
|
||||
expect(resource.store.findLatestAuthoritativeSequence(true, 30001)).toBe(100);
|
||||
expect(resource.store.findLatestPersistedAuthoritativeSequence(true, 30001)).toBe(50);
|
||||
expect(await resource.store.findLatestAuthoritativeSequence(true, 30001)).toBe(100);
|
||||
expect(await resource.store.findLatestPersistedAuthoritativeSequence(true, 30001)).toBe(50);
|
||||
|
||||
const groupPage = vi.fn(async () => []);
|
||||
const bridge = makeBridge({
|
||||
@@ -352,9 +353,9 @@ describe('login history sync', () => {
|
||||
});
|
||||
|
||||
it('backfills a metadata-only session even when the server read cursor is current', async () => {
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
resource.store.storeMeta(124, {
|
||||
await resource.store.storeMeta(124, {
|
||||
isGroup: true,
|
||||
targetId: 30001,
|
||||
sequence: 100,
|
||||
@@ -364,7 +365,9 @@ describe('login history sync', () => {
|
||||
random: 0,
|
||||
timestamp: 1_700_000_100,
|
||||
});
|
||||
expect(resource.store.findLatestPersistedAuthoritativeSequence(true, 30001)).toBeNull();
|
||||
expect(
|
||||
await resource.store.findLatestPersistedAuthoritativeSequence(true, 30001),
|
||||
).toBeNull();
|
||||
|
||||
const groupPage = vi.fn(async () => []);
|
||||
const bridge = makeBridge({
|
||||
@@ -391,9 +394,9 @@ describe('login history sync', () => {
|
||||
});
|
||||
|
||||
it('uses the authoritative private sequence for a known local gap', async () => {
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
resource.store.storeEvent(
|
||||
await resource.store.storeEvent(
|
||||
456,
|
||||
false,
|
||||
40001,
|
||||
@@ -472,7 +475,7 @@ describe('login history sync', () => {
|
||||
})),
|
||||
groupPage,
|
||||
});
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
|
||||
@@ -507,7 +510,7 @@ describe('login history sync', () => {
|
||||
})),
|
||||
groupPage,
|
||||
});
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
|
||||
@@ -535,51 +538,73 @@ describe('login history sync', () => {
|
||||
).resolves.toHaveLength(1);
|
||||
});
|
||||
|
||||
it('does not restore a private message hidden by a recall tombstone', async () => {
|
||||
const resource = makeStore();
|
||||
resources.push(resource);
|
||||
const recalled = friendMessage(40001, 1);
|
||||
resource.store.recordPrivateRecall(40001, recalled.clientSeq!, false, recalled.time + 1);
|
||||
const bridge = makeBridge({
|
||||
groupCount: 0,
|
||||
probe: vi.fn(async () => ({
|
||||
groups: [],
|
||||
privateUsers: [
|
||||
{
|
||||
userId: 40001,
|
||||
uid: 'u_40001',
|
||||
readSeq: 0,
|
||||
latestSeq: 1,
|
||||
lastMsgTime: recalled.time,
|
||||
it.each(['before reading', 'before persistence'] as const)(
|
||||
'does not restore or count a private message recalled %s',
|
||||
async (timing) => {
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const recalled = friendMessage(40001, 1);
|
||||
const recall = () =>
|
||||
resource.store.recordPrivateRecall(
|
||||
40001,
|
||||
recalled.clientSeq!,
|
||||
false,
|
||||
recalled.time + 1,
|
||||
);
|
||||
|
||||
if (timing === 'before reading') {
|
||||
await recall();
|
||||
} else {
|
||||
const persist = resource.store.storeHistoryEvent.bind(resource.store);
|
||||
vi.spyOn(resource.store, 'storeHistoryEvent').mockImplementationOnce(
|
||||
async (...args) => {
|
||||
await recall();
|
||||
return persist(...args);
|
||||
},
|
||||
],
|
||||
})),
|
||||
privateLatestPage: vi.fn(async () => [recalled]),
|
||||
});
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
);
|
||||
}
|
||||
|
||||
const result = await coordinator.schedule(
|
||||
{
|
||||
selfId: SELF_ID,
|
||||
bridge,
|
||||
messageStore: resource.store,
|
||||
converterCtx: converterContext(),
|
||||
},
|
||||
new AbortController().signal,
|
||||
);
|
||||
const bridge = makeBridge({
|
||||
groupCount: 0,
|
||||
probe: vi.fn(async () => ({
|
||||
groups: [],
|
||||
privateUsers: [
|
||||
{
|
||||
userId: 40001,
|
||||
uid: 'u_40001',
|
||||
readSeq: 0,
|
||||
latestSeq: 1,
|
||||
lastMsgTime: recalled.time,
|
||||
},
|
||||
],
|
||||
})),
|
||||
privateLatestPage: vi.fn(async () => [recalled]),
|
||||
});
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
|
||||
expect(result).toMatchObject({
|
||||
fetchedMessages: 1,
|
||||
storedMessages: 0,
|
||||
failedSessions: 0,
|
||||
});
|
||||
await expect(getFriendMsgHistory(resource.store, 40001, undefined, 20)).resolves.toEqual(
|
||||
[],
|
||||
);
|
||||
});
|
||||
const result = await coordinator.schedule(
|
||||
{
|
||||
selfId: SELF_ID,
|
||||
bridge,
|
||||
messageStore: resource.store,
|
||||
converterCtx: converterContext(),
|
||||
},
|
||||
new AbortController().signal,
|
||||
);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
fetchedMessages: 1,
|
||||
storedMessages: 0,
|
||||
failedSessions: 0,
|
||||
});
|
||||
await expect(
|
||||
getFriendMsgHistory(resource.store, 40001, undefined, 20),
|
||||
).resolves.toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it('rejects private history attributed to a different peer', async () => {
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const bridge = makeBridge({
|
||||
groupCount: 0,
|
||||
@@ -629,7 +654,7 @@ describe('login history sync', () => {
|
||||
const mediaUrlResolver = vi.fn(async () => {
|
||||
throw new Error('media resolver must not run');
|
||||
});
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const bridge = makeBridge({
|
||||
friendCount: 0,
|
||||
@@ -682,8 +707,8 @@ describe('login history sync', () => {
|
||||
}),
|
||||
);
|
||||
const secondProbe = vi.fn(async () => ({ groups: [], privateUsers: [] }));
|
||||
const firstStore = makeStore();
|
||||
const secondStore = makeStore();
|
||||
const firstStore = await makeStore();
|
||||
const secondStore = await makeStore();
|
||||
resources.push(firstStore, secondStore);
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
|
||||
@@ -726,9 +751,9 @@ describe('login history sync', () => {
|
||||
);
|
||||
const cancelledProbe = vi.fn(async () => ({ groups: [], privateUsers: [] }));
|
||||
const thirdProbe = vi.fn(async () => ({ groups: [], privateUsers: [] }));
|
||||
const firstStore = makeStore();
|
||||
const cancelledStore = makeStore();
|
||||
const thirdStore = makeStore();
|
||||
const firstStore = await makeStore();
|
||||
const cancelledStore = await makeStore();
|
||||
const thirdStore = await makeStore();
|
||||
resources.push(firstStore, cancelledStore, thirdStore);
|
||||
const coordinator = new LoginHistorySyncCoordinator({ sleep: async () => undefined });
|
||||
const cancelledController = new AbortController();
|
||||
@@ -782,7 +807,7 @@ describe('login history sync', () => {
|
||||
releasePage = () => resolve([groupMessage(30001, 1)]);
|
||||
}),
|
||||
);
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const bridge = makeBridge({
|
||||
friendCount: 0,
|
||||
@@ -815,7 +840,7 @@ describe('login history sync', () => {
|
||||
});
|
||||
|
||||
it('stops conversion side effects when disposal happens during conversion', async () => {
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const controller = new AbortController();
|
||||
const mediaSegmentSink = vi.fn();
|
||||
@@ -867,7 +892,7 @@ describe('login history sync', () => {
|
||||
signal.addEventListener('abort', () => reject(signal.reason), { once: true });
|
||||
}),
|
||||
});
|
||||
const resource = makeStore();
|
||||
const resource = await makeStore();
|
||||
resources.push(resource);
|
||||
const controller = new AbortController();
|
||||
|
||||
|
||||
Reference in new issue
Block a user