Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-node-event-size-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-node': patch
---

Fix the per-event and batch size limits, which were not enforced. `ContextBatch.calculateSize` used a broken byte-count regex (`split(/%..|i/)`) that counted only `%XX` escapes plus the letter "i" instead of UTF-8 bytes, so a ~39 KB event measured as ~21 bytes and passed the 32 KB per-event guard. Restored the correct byte count so oversized events are rejected before they are sent.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ContextBatch } from '../context-batch'
import { NodeEventFactory } from '../../../app/event-factory'
import { Context } from '../../../app/context'

const eventFactory = new NodeEventFactory()
const pending = (context: Context) => ({ resolver: () => undefined, context })

describe('ContextBatch', () => {
it('rejects an event that exceeds the 32 KB per-event size limit', () => {
// ~40 KB of JSON, well over the 32 KB per-event cap
const event = eventFactory.track(
'big',
{ data: 'a'.repeat(40000) },
{ userId: 'u' }
)
const batch = new ContextBatch(100)
const result = batch.tryAdd(pending(new Context(event)))
expect(result.success).toBe(false)
if (!result.success) {
expect(result.message).toContain('exceeds maximum event size')
}
})

it('accepts a normal small event', () => {
const event = eventFactory.track('small', { ok: true }, { userId: 'u' })
const batch = new ContextBatch(100)
expect(batch.tryAdd(pending(new Context(event))).success).toBe(true)
})
})
2 changes: 1 addition & 1 deletion packages/node/src/plugins/segmentio/context-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ContextBatch {
}

private calculateSize(ctx: Context): number {
return encodeURI(JSON.stringify(ctx.event)).split(/%..|i/).length
return encodeURI(JSON.stringify(ctx.event)).split(/%..|./).length - 1
}

getEvents(): SegmentEvent[] {
Expand Down