Skip to content

Commit 3c37def

Browse files
Refactor event factory, remove spark-md5 lib, fix hash collisions on messageId (#1045)
Co-authored-by: Andrew Loyola <[email protected]>
1 parent 3658811 commit 3c37def

File tree

22 files changed

+326
-599
lines changed

22 files changed

+326
-599
lines changed

.changeset/clean-trains-perform.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
- Remove validation plugin
5+
- Remove `spark-md5` dependency
6+
- Update messageId algorithm to be consistent with node (analytics-next-[epoch time]-[uuid])
7+
- Browser Validation:
8+
- Throws errors in the EventFactory (not just in a plugin) if the event is invalid

.changeset/nasty-bulldogs-unite.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
'@segment/analytics-core': patch
3+
---
4+
Share `EventFactory` between node and browser.

packages/browser/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"dset": "^3.1.2",
5858
"js-cookie": "3.0.1",
5959
"node-fetch": "^2.6.7",
60-
"spark-md5": "^3.0.1",
6160
"tslib": "^2.4.1",
6261
"unfetch": "^4.1.0"
6362
},
@@ -76,7 +75,6 @@
7675
"@types/mime": "^2.0.3",
7776
"@types/node": "^16",
7877
"@types/serve-handler": "^6.1.0",
79-
"@types/spark-md5": "^3.0.2",
8078
"aws-sdk": "^2.814.0",
8179
"circular-dependency-plugin": "^5.2.2",
8280
"compression-webpack-plugin": "^8.0.1",

packages/browser/src/browser/__tests__/inspector.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('Inspector', () => {
5555

5656
await deliveryPromise
5757

58-
expect(enrichedFn).toHaveBeenCalledTimes(2) // will be called once for every before or enrichment plugin.
58+
expect(enrichedFn).toHaveBeenCalledTimes(1) // will be called once for every before or enrichment plugin.
5959
expect(deliveredFn).toHaveBeenCalledTimes(1)
6060
})
6161

packages/browser/src/browser/__tests__/integration.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,6 @@ describe('Dispatch', () => {
650650
"plugin_time",
651651
"plugin_time",
652652
"plugin_time",
653-
"plugin_time",
654653
"message_delivered",
655654
"delivered",
656655
]

packages/browser/src/browser/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
} from '../plugins/remote-loader'
2323
import type { RoutingRule } from '../plugins/routing-middleware'
2424
import { segmentio, SegmentioSettings } from '../plugins/segmentio'
25-
import { validation } from '../plugins/validation'
2625
import {
2726
AnalyticsBuffered,
2827
PreInitMethodCallBuffer,
@@ -255,7 +254,6 @@ async function registerPlugins(
255254
).catch(() => [])
256255

257256
const toRegister = [
258-
validation,
259257
envEnrichment,
260258
...plugins,
261259
...legacyDestinations,

packages/browser/src/core/analytics/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ export class Analytics
582582

583583
normalize(msg: SegmentEvent): SegmentEvent {
584584
console.warn(deprecationWarning)
585-
return this.eventFactory.normalize(msg)
585+
return this.eventFactory['normalize'](msg)
586586
}
587587

588588
get failedInitializations(): string[] {

packages/browser/src/core/events/__tests__/index.test.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { range, uniq } from 'lodash'
33
import { EventFactory } from '..'
44
import { getDefaultPageContext } from '../../page'
55
import { User } from '../../user'
6-
import { SegmentEvent, Options } from '../interfaces'
76

87
describe('Event Factory', () => {
98
let user: User
@@ -59,6 +58,25 @@ describe('Event Factory', () => {
5958
const group = factory.group('coolKidsId', { coolkids: true })
6059
expect(group.groupId).toEqual('coolKidsId')
6160
})
61+
62+
it('allows userId / anonymousId to be specified as an option', () => {
63+
const group = factory.group('my_group_id', undefined, {
64+
userId: 'bar',
65+
anonymousId: 'foo',
66+
})
67+
expect(group.userId).toBe('bar')
68+
expect(group.anonymousId).toBe('foo')
69+
})
70+
71+
it('uses userId / anonymousId from the user class (if specified)', function () {
72+
const user = new User()
73+
user.anonymousId = () => '123'
74+
user.id = () => 'abc'
75+
factory = new EventFactory(user)
76+
const group = factory.group('my_group_id')
77+
expect(group.userId).toBe('abc')
78+
expect(group.anonymousId).toBe('123')
79+
})
6280
})
6381

6482
describe('page', () => {
@@ -394,32 +412,6 @@ describe('Event Factory', () => {
394412
})
395413
})
396414

397-
describe('normalize', function () {
398-
const msg: SegmentEvent = { type: 'track' }
399-
const opts: Options = (msg.options = {})
400-
401-
describe('message', function () {
402-
it('should merge original with normalized', function () {
403-
msg.userId = 'user-id'
404-
opts.integrations = { Segment: true }
405-
const normalized = factory['normalize'](msg)
406-
407-
expect(normalized.messageId?.length).toBeGreaterThanOrEqual(41) // 'ajs-next-md5(content + [UUID])'
408-
delete normalized.messageId
409-
410-
expect(normalized.timestamp).toBeInstanceOf(Date)
411-
delete normalized.timestamp
412-
413-
expect(normalized).toStrictEqual({
414-
integrations: { Segment: true },
415-
type: 'track',
416-
userId: 'user-id',
417-
context: defaultContext,
418-
})
419-
})
420-
})
421-
})
422-
423415
describe('Page context augmentation', () => {
424416
// minimal tests -- more tests should be specifically around addPageContext
425417
factory = new EventFactory(new User())

0 commit comments

Comments
 (0)