Skip to content

Commit 4aa1ca5

Browse files
committed
Update sent_at in header
1 parent db3c26e commit 4aa1ca5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type {
2+
BaseEnvelopeHeaders,
23
ClientReport,
34
Envelope,
45
EventEnvelope,
6+
EventEnvelopeHeaders,
57
EventItem,
68
InternalBaseTransportOptions,
79
ReplayEnvelope,
@@ -304,6 +306,37 @@ describe('makeOfflineTransport', () => {
304306
START_DELAY + 2_000,
305307
);
306308

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

0 commit comments

Comments
 (0)