This repository was archived by the owner on Jan 15, 2026. It is now read-only.
forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemitter_request.ts
More file actions
251 lines (224 loc) · 6.29 KB
/
Copy pathemitter_request.ts
File metadata and controls
251 lines (224 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { PAYLOAD_DATA_SCHEMA } from '../schemata';
import { EmitterEvent } from "./emitter_event";
/**
* Wrapper around a request with events to the collector.
* Provides helpers to manage the request and its events.
* Prepare the request to be sent to the collector.
*/
export interface EmitterRequest {
/**
* Add an event to the request
* @returns true if the event was added, false if the server anonymization setting does not match the existing events
*/
addEvent: (event: EmitterEvent) => boolean;
/**
* Get the events attached to the request
*/
getEvents: () => EmitterEvent[];
/**
* Creates a fetch Request object from the events
*/
toRequest: () => Request | undefined;
/**
* Whether the request is full or events can still be added
*/
isFull: () => boolean;
/**
* Size of the request in bytes
*/
countBytes: () => number;
/**
* The number of events attached to the request
*/
countEvents: () => number;
/**
* Cancel timeout timer if it is still pending.
* If not successful, the request will be aborted.
* @param successful - Whether the request was successful
* @param reason - Reason for aborting the request
*/
closeRequest: (successful: boolean, reason?: string) => void;
}
export interface EmitterRequestConfiguration {
endpoint: string;
port?: number;
protocol?: 'http' | 'https';
eventMethod?: 'get' | 'post';
customHeaders?: Record<string, string>;
connectionTimeout?: number;
keepalive?: boolean;
postPath?: string;
useStm?: boolean;
maxPostBytes?: number,
credentials?: 'omit' | 'same-origin' | 'include';
}
/**
* Enclose an array of events in a self-describing payload_data JSON string
*
* @param array - events Batch of events
* @returns string payload_data self-describing JSON
*/
export function encloseInPayloadDataEnvelope(events: Array<Record<string, unknown>>) {
return JSON.stringify({
schema: PAYLOAD_DATA_SCHEMA,
data: events,
});
}
/**
* Attaches the STM field to outbound POST events.
*
* @param events - the events to attach the STM to
*/
export function attachStmToEvent(events: Array<Record<string, unknown>>) {
const stm = new Date().getTime().toString();
for (let i = 0; i < events.length; i++) {
events[i]['stm'] = stm;
}
return events;
}
export function newEmitterRequest({
endpoint,
protocol = 'https',
port,
eventMethod = 'post',
customHeaders,
connectionTimeout,
keepalive = false,
postPath = '/com.snowplowanalytics.snowplow/tp2',
useStm = true,
maxPostBytes = 40000,
credentials = 'include',
}: EmitterRequestConfiguration): EmitterRequest {
let events: EmitterEvent[] = [];
let usePost = eventMethod.toLowerCase() === 'post';
let timer: ReturnType<typeof setTimeout> | undefined;
let abortController: AbortController | undefined;
function countBytes(): number {
let count = events.reduce(
(acc, event) => acc + (usePost ? event.getPOSTRequestBytesCount() : event.getGETRequestBytesCount()),
0
);
if (usePost) {
count += 88; // 88 bytes for the payload_data envelope
}
return count;
}
function countEvents(): number {
return events.length;
}
function getServerAnonymizationOfExistingEvents(): boolean | undefined {
return events.length > 0 ? events[0].getServerAnonymization() : undefined;
}
function addEvent(event: EmitterEvent) {
if (events.length > 0 && getServerAnonymizationOfExistingEvents() !== event.getServerAnonymization()) {
return false;
} else {
events.push(event);
return true;
}
}
function getEvents(): EmitterEvent[] {
return events;
}
function isFull(): boolean {
if (usePost) {
return countBytes() >= maxPostBytes;
} else {
return events.length >= 1;
}
}
function createHeaders(): Headers {
const headers = new Headers();
if (usePost) {
headers.append('Content-Type', 'application/json; charset=UTF-8');
}
if (customHeaders) {
Object.keys(customHeaders).forEach((key) => {
headers.append(key, customHeaders[key]);
});
}
if (getServerAnonymizationOfExistingEvents()) {
headers.append('SP-Anonymous', '*');
}
return headers;
}
function getFullCollectorUrl(): string {
let collectorUrl = endpoint;
if (!endpoint.includes('://')) {
collectorUrl = `${protocol}://${endpoint}`;
}
if (port) {
collectorUrl = `${collectorUrl}:${port}`;
}
const path = usePost ? postPath : '/i';
return collectorUrl + path;
}
function makeRequest(url: string, options: RequestInit): Request {
closeRequest(false);
abortController = new AbortController();
timer = setTimeout(() => {
const reason = 'Request timed out';
console.error(reason);
timer = undefined;
closeRequest(false, reason);
}, connectionTimeout ?? 5000);
const requestOptions: RequestInit = {
headers: createHeaders(),
signal: abortController.signal,
keepalive,
credentials,
...options,
};
const request = new Request(url, requestOptions);
return request;
}
function makePostRequest(): Request {
const batch = attachStmToEvent(events.map((event) => event.getPOSTRequestBody()));
return makeRequest(getFullCollectorUrl(), {
method: 'POST',
body: encloseInPayloadDataEnvelope(batch),
});
}
function makeGetRequest(): Request {
if (events.length !== 1) {
throw new Error('Only one event can be sent in a GET request');
}
const event = events[0];
const url = event.getGETRequestURL(getFullCollectorUrl(), useStm);
return makeRequest(url, {
method: 'GET',
});
}
function toRequest(): Request | undefined {
if (events.length === 0) {
return undefined;
}
if (usePost) {
return makePostRequest();
} else {
return makeGetRequest();
}
}
function closeRequest(successful: boolean, reason?: string) {
if (timer !== undefined) {
clearTimeout(timer);
timer = undefined;
}
if (abortController !== undefined) {
const controller = abortController;
abortController = undefined;
if (!successful) {
controller.abort(reason);
}
}
}
return {
addEvent,
getEvents,
toRequest,
countBytes,
countEvents,
isFull,
closeRequest,
};
}