Skip to content

Commit 1011f04

Browse files
committed
Update sent_at in header
1 parent db3c26e commit 1011f04

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/core/src/transports/offline.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export function makeOfflineTransport<TO>(
9090
const found = await store.shift();
9191
if (found) {
9292
log('Attempting to send previously queued event');
93+
94+
// We should to update the sent_at timestamp to the current time.
95+
found[0].sent_at = new Date().toISOString();
96+
9397
void send(found, true).catch(e => {
9498
log('Failed to retry sending', e);
9599
});

packages/core/test/lib/transports/offline.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,37 @@ describe('makeOfflineTransport', () => {
304304
START_DELAY + 2_000,
305305
);
306306

307+
it(
308+
'Updates sent_at envelope header on retry',
309+
async () => {
310+
const testStartTime = new Date();
311+
312+
// Create an envelope with a sent_at header very far in the past
313+
const env: EventEnvelope = [...ERROR_ENVELOPE];
314+
env[0].sent_at = new Date(2020, 1, 1).toISOString();
315+
316+
const { getCalls, store } = createTestStore(ERROR_ENVELOPE);
317+
const { getSentEnvelopes, baseTransport } = createTestTransport({ statusCode: 200 });
318+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
319+
const _transport = makeOfflineTransport(baseTransport)({
320+
...transportOptions,
321+
createStore: store,
322+
flushAtStartup: true,
323+
});
324+
325+
await waitUntil(() => getCalls().length >= 1, START_DELAY * 2);
326+
expect(getCalls()).toEqual(['shift']);
327+
328+
// When it gets shifted out of the store, the sent_at header should be updated
329+
const envelopes = getSentEnvelopes().map(parseEnvelope) as EventEnvelope[];
330+
expect(envelopes[0][0]).toBeDefined();
331+
const sent_at = new Date(envelopes[0][0].sent_at);
332+
333+
expect(sent_at.getTime()).toBeGreaterThan(testStartTime.getTime());
334+
},
335+
START_DELAY + 2_000,
336+
);
337+
307338
it('shouldStore can stop envelopes from being stored on send failure', async () => {
308339
const { getCalls, store } = createTestStore();
309340
const { getSendCount, baseTransport } = createTestTransport(new Error());

0 commit comments

Comments
 (0)