node-avo-inspector 1.2.0-alpha.1 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +268 -4
  3. package/package.json +17 -1
  4. package/.claude/scheduled_tasks.lock +0 -1
  5. package/.claude/settings.local.json +0 -11
  6. package/.claude/worktrees/release-1.1.1/LICENSE +0 -21
  7. package/.claude/worktrees/release-1.1.1/README.md +0 -69
  8. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/index.js +0 -30
  9. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/package.json +0 -15
  10. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/yarn.lock +0 -6
  11. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/dist/index.js +0 -34
  12. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/index.ts +0 -30
  13. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/package.json +0 -18
  14. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/tsconfig.json +0 -15
  15. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/yarn.lock +0 -11
  16. package/.claude/worktrees/release-1.1.1/package-lock.json +0 -4039
  17. package/.claude/worktrees/release-1.1.1/package.json +0 -48
  18. package/.claude/worktrees/release-1.1.1/reason/AvoInspector.rei +0 -11
  19. package/.claude/worktrees/release-1.1.1/reason/NodeAvoInspector.re +0 -27
  20. package/.claude/worktrees/release-1.1.1/src/AvoDeduplicator.ts +0 -146
  21. package/.claude/worktrees/release-1.1.1/src/AvoEncryption.ts +0 -90
  22. package/.claude/worktrees/release-1.1.1/src/AvoGuid.ts +0 -9
  23. package/.claude/worktrees/release-1.1.1/src/AvoInspector.ts +0 -383
  24. package/.claude/worktrees/release-1.1.1/src/AvoInspectorEnv.ts +0 -8
  25. package/.claude/worktrees/release-1.1.1/src/AvoNetworkCallsHandler.ts +0 -267
  26. package/.claude/worktrees/release-1.1.1/src/AvoSchemaParser.ts +0 -115
  27. package/.claude/worktrees/release-1.1.1/src/AvoStreamId.ts +0 -16
  28. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEncryption.ts +0 -515
  29. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEventSpecCache.ts +0 -242
  30. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEventSpecFetcher.ts +0 -241
  31. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoInspector.ts +0 -360
  32. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoStreamId.ts +0 -72
  33. package/.claude/worktrees/release-1.1.1/src/__tests__/TestDeduplicator.ts +0 -191
  34. package/.claude/worktrees/release-1.1.1/src/__tests__/TestEventValidator.ts +0 -231
  35. package/.claude/worktrees/release-1.1.1/src/__tests__/TestNetworkCallsHandler.ts +0 -179
  36. package/.claude/worktrees/release-1.1.1/src/__tests__/TestNetworkCallsHandlerPublic.ts +0 -208
  37. package/.claude/worktrees/release-1.1.1/src/__tests__/TestParsing.ts +0 -153
  38. package/.claude/worktrees/release-1.1.1/src/__tests__/constants.ts +0 -63
  39. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecCache.ts +0 -109
  40. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecFetchTypes.ts +0 -47
  41. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecFetcher.ts +0 -106
  42. package/.claude/worktrees/release-1.1.1/src/eventSpec/EventValidator.ts +0 -74
  43. package/.claude/worktrees/release-1.1.1/src/index.ts +0 -2
  44. package/.claude/worktrees/release-1.1.1/src/utils.ts +0 -49
  45. package/.claude/worktrees/release-1.1.1/tsconfig.json +0 -13
  46. package/.claude/worktrees/release-1.1.1/yarn.lock +0 -2310
  47. package/.github/workflows/claude.yml +0 -55
  48. package/dist/__tests__/TestAvoEncryption.d.ts +0 -1
  49. package/dist/__tests__/TestAvoEncryption.js +0 -288
  50. package/dist/__tests__/TestAvoEventSpecCache.d.ts +0 -1
  51. package/dist/__tests__/TestAvoEventSpecCache.js +0 -194
  52. package/dist/__tests__/TestAvoEventSpecFetcher.d.ts +0 -1
  53. package/dist/__tests__/TestAvoEventSpecFetcher.js +0 -219
  54. package/dist/__tests__/TestAvoInspector.d.ts +0 -1
  55. package/dist/__tests__/TestAvoInspector.js +0 -291
  56. package/dist/__tests__/TestAvoStreamId.d.ts +0 -1
  57. package/dist/__tests__/TestAvoStreamId.js +0 -55
  58. package/dist/__tests__/TestDeduplicator.d.ts +0 -1
  59. package/dist/__tests__/TestDeduplicator.js +0 -102
  60. package/dist/__tests__/TestEventValidator.d.ts +0 -1
  61. package/dist/__tests__/TestEventValidator.js +0 -196
  62. package/dist/__tests__/TestNetworkCallsHandler.d.ts +0 -1
  63. package/dist/__tests__/TestNetworkCallsHandler.js +0 -86
  64. package/dist/__tests__/TestNetworkCallsHandlerPublic.d.ts +0 -1
  65. package/dist/__tests__/TestNetworkCallsHandlerPublic.js +0 -160
  66. package/dist/__tests__/TestParsing.d.ts +0 -1
  67. package/dist/__tests__/TestParsing.js +0 -128
  68. package/dist/__tests__/constants.d.ts +0 -42
  69. package/dist/__tests__/constants.js +0 -53
  70. package/examples/node-inspector-js/index.js +0 -30
  71. package/examples/node-inspector-js/package.json +0 -15
  72. package/examples/node-inspector-js/yarn.lock +0 -6
  73. package/examples/node-inspector-ts/dist/index.js +0 -34
  74. package/examples/node-inspector-ts/index.ts +0 -30
  75. package/examples/node-inspector-ts/package.json +0 -18
  76. package/examples/node-inspector-ts/tsconfig.json +0 -15
  77. package/examples/node-inspector-ts/yarn.lock +0 -11
  78. package/reason/AvoInspector.rei +0 -11
  79. package/reason/NodeAvoInspector.re +0 -27
  80. package/src/AvoDeduplicator.ts +0 -150
  81. package/src/AvoEncryption.ts +0 -90
  82. package/src/AvoGuid.ts +0 -9
  83. package/src/AvoInspector.ts +0 -498
  84. package/src/AvoInspectorEnv.ts +0 -8
  85. package/src/AvoNetworkCallsHandler.ts +0 -340
  86. package/src/AvoSchemaParser.ts +0 -115
  87. package/src/AvoStreamId.ts +0 -16
  88. package/src/__tests__/TestAvoEncryption.ts +0 -515
  89. package/src/__tests__/TestAvoEventSpecCache.ts +0 -242
  90. package/src/__tests__/TestAvoEventSpecFetcher.ts +0 -279
  91. package/src/__tests__/TestAvoInspector.ts +0 -376
  92. package/src/__tests__/TestAvoStreamId.ts +0 -72
  93. package/src/__tests__/TestDeduplicator.ts +0 -191
  94. package/src/__tests__/TestEventValidator.ts +0 -231
  95. package/src/__tests__/TestNetworkCallsHandler.ts +0 -181
  96. package/src/__tests__/TestNetworkCallsHandlerPublic.ts +0 -208
  97. package/src/__tests__/TestParsing.ts +0 -153
  98. package/src/__tests__/constants.ts +0 -63
  99. package/src/eventSpec/AvoEventSpecCache.ts +0 -119
  100. package/src/eventSpec/AvoEventSpecFetchTypes.ts +0 -47
  101. package/src/eventSpec/AvoEventSpecFetcher.ts +0 -186
  102. package/src/eventSpec/EventValidator.ts +0 -74
  103. package/src/index.ts +0 -2
  104. package/src/utils.ts +0 -49
  105. package/tsconfig.json +0 -13
@@ -1,340 +0,0 @@
1
- import { AvoGuid } from "./AvoGuid";
2
- import { AvoInspector } from "./AvoInspector";
3
- import { AvoEncryption } from "./AvoEncryption";
4
- import { request } from "https";
5
- import { EventSpecMetadata, PropertyValidationResult } from "./eventSpec/AvoEventSpecFetchTypes";
6
-
7
- export interface BaseBody {
8
- apiKey: string;
9
- appName: string;
10
- appVersion: string;
11
- libVersion: string;
12
- env: string;
13
- libPlatform: "node";
14
- messageId: string;
15
- trackingId: string;
16
- sessionId: string;
17
- anonymousId: string;
18
- createdAt: string;
19
- samplingRate: number;
20
- publicEncryptionKey?: string;
21
- }
22
-
23
- export interface EventPropertyEncrypted {
24
- propertyName: string;
25
- propertyType: string;
26
- encryptedPropertyValue: string;
27
- children?: any;
28
- }
29
-
30
- export interface EventPropertyPlain {
31
- propertyName: string;
32
- propertyType: string;
33
- children?: any;
34
- }
35
-
36
- export interface EventPropertyValidation {
37
- failedEventIds?: string[];
38
- passedEventIds?: string[];
39
- }
40
-
41
- export type EventProperty = (EventPropertyEncrypted | EventPropertyPlain) & EventPropertyValidation;
42
-
43
- export interface EventSchemaBody extends BaseBody {
44
- type: "event";
45
- eventName: string;
46
- eventProperties: Array<EventProperty>;
47
- avoFunction: boolean;
48
- eventId: string | null;
49
- eventHash: string | null;
50
- streamId?: string;
51
- eventSpecMetadata?: EventSpecMetadata;
52
- }
53
-
54
- export type InspectorBody = EventSchemaBody;
55
-
56
- export class AvoNetworkCallsHandler {
57
- private apiKey: string;
58
- private envName: string;
59
- private appName: string;
60
- private appVersion: string;
61
- private libVersion: string;
62
- private samplingRate: number = 1.0;
63
- private publicEncryptionKey?: string;
64
-
65
- private static trackingEndpoint = "/inspector/v1/track";
66
-
67
- constructor(
68
- apiKey: string,
69
- envName: string,
70
- appName: string,
71
- appVersion: string,
72
- libVersion: string,
73
- publicEncryptionKey?: string
74
- ) {
75
- this.apiKey = apiKey;
76
- this.envName = envName;
77
- this.appName = appName;
78
- this.appVersion = appVersion;
79
- this.libVersion = libVersion;
80
- this.publicEncryptionKey = publicEncryptionKey;
81
- }
82
-
83
- callInspectorWithBatchBody(
84
- inEvents: Array<InspectorBody>
85
- ): Promise<void> {
86
- const events = inEvents.filter((x) => x != null);
87
-
88
- if (events.length === 0) {
89
- return Promise.resolve();
90
- }
91
-
92
- if (Math.random() > this.samplingRate) {
93
- if (AvoInspector.shouldLog) {
94
- console.log(
95
- "Avo Inspector: last event schema dropped due to sampling rate."
96
- );
97
- }
98
- return Promise.resolve();
99
- }
100
-
101
- if (AvoInspector.shouldLog) {
102
- events.forEach(function (event) {
103
- const eventProps = event.eventProperties
104
- .map(p => '\t"' + p.propertyName + '": "' + p.propertyType + '"')
105
- .join(";\n");
106
- const validated = event.eventSpecMetadata ? " (validated)" : "";
107
- console.log(
108
- "Avo Inspector: Sending event " +
109
- event.eventName + validated +
110
- " with schema {\n" + eventProps + "\n}"
111
- );
112
- });
113
- }
114
-
115
- return new Promise((resolve, reject) => {
116
- const data = JSON.stringify(events);
117
- const options = {
118
- hostname: "api.avo.app",
119
- port: 443,
120
- path: AvoNetworkCallsHandler.trackingEndpoint,
121
- method: "POST",
122
- headers: {
123
- "Accept": "application/json",
124
- "Content-Type": "application/json",
125
- "Content-Length": Buffer.byteLength(data),
126
- },
127
- };
128
-
129
- if (AvoInspector.shouldLog) {
130
- console.log("Avo Inspector: [network] POST https://" + options.hostname + options.path);
131
- console.log("Avo Inspector: [network] Request body (" + Buffer.byteLength(data) + " bytes)");
132
- }
133
-
134
- const req = request(options, (res: any) => {
135
- if (AvoInspector.shouldLog) {
136
- console.log("Avo Inspector: [network] Response status: " + res.statusCode + " " + res.statusMessage);
137
- }
138
- const chunks: any = [];
139
- res.on("data", (data: any) => chunks.push(data));
140
- res.on("end", () => {
141
- if (res.statusCode === 200) {
142
- try {
143
- const responseBody = Buffer.concat(chunks).toString();
144
- const data = JSON.parse(responseBody);
145
- if (typeof data.samplingRate === "number" && data.samplingRate >= 0 && data.samplingRate <= 1) {
146
- this.samplingRate = data.samplingRate;
147
- }
148
- } catch (e) {
149
- if (AvoInspector.shouldLog) {
150
- console.warn("Avo Inspector: [network] Failed to parse response JSON: " + e);
151
- }
152
- }
153
- } else if (AvoInspector.shouldLog) {
154
- console.warn("Avo Inspector: [network] Non-200 response: " + res.statusCode);
155
- }
156
- resolve();
157
- });
158
- });
159
- req.write(data);
160
- req.setTimeout(10_000);
161
- req.on("error", (err: any) => {
162
- if (AvoInspector.shouldLog) {
163
- console.error("Avo Inspector: [network] Request error: " + err);
164
- }
165
- reject("Request failed");
166
- });
167
- req.on("timeout", () => {
168
- if (AvoInspector.shouldLog) {
169
- console.error("Avo Inspector: [network] Request timed out after 10s");
170
- }
171
- req.destroy();
172
- reject("Request timed out");
173
- });
174
- req.end();
175
- });
176
- }
177
-
178
- bodyForEventSchemaCall(
179
- anonymousId: string,
180
- eventName: string,
181
- eventProperties: Array<{
182
- propertyName: string;
183
- propertyType: string;
184
- children?: any;
185
- }>,
186
- eventId: string | null,
187
- eventHash: string | null,
188
- rawEventProperties?: { [propName: string]: any }
189
- ): EventSchemaBody {
190
- let eventSchemaBody = this.createBaseCallBody(anonymousId) as EventSchemaBody;
191
- eventSchemaBody.type = "event";
192
- eventSchemaBody.eventName = eventName;
193
-
194
- if (AvoEncryption.shouldEncrypt(this.envName, this.publicEncryptionKey) && rawEventProperties) {
195
- eventSchemaBody.eventProperties = this.encryptProperties(eventProperties, rawEventProperties);
196
- } else {
197
- eventSchemaBody.eventProperties = eventProperties;
198
- }
199
-
200
- if (eventId != null) {
201
- eventSchemaBody.avoFunction = true;
202
- eventSchemaBody.eventId = eventId;
203
- eventSchemaBody.eventHash = eventHash;
204
- } else {
205
- eventSchemaBody.avoFunction = false;
206
- eventSchemaBody.eventId = null;
207
- eventSchemaBody.eventHash = null;
208
- }
209
-
210
- return eventSchemaBody;
211
- }
212
-
213
- buildEventProperties(
214
- eventProperties: Array<{
215
- propertyName: string;
216
- propertyType: string;
217
- children?: any;
218
- }>,
219
- rawEventProperties?: { [propName: string]: any }
220
- ): Array<EventProperty> {
221
- if (AvoEncryption.shouldEncrypt(this.envName, this.publicEncryptionKey) && rawEventProperties) {
222
- return this.encryptProperties(eventProperties, rawEventProperties);
223
- }
224
- return eventProperties;
225
- }
226
-
227
- bodyForValidatedEventSchemaCall(
228
- anonymousId: string,
229
- eventName: string,
230
- eventProperties: Array<EventProperty>,
231
- eventId: string | null,
232
- eventHash: string | null,
233
- eventSpecMetadata: EventSpecMetadata,
234
- propertyResults: PropertyValidationResult[]
235
- ): EventSchemaBody {
236
- // Build a map of validation results by property name
237
- const validationMap = new Map<string, PropertyValidationResult>();
238
- for (const result of propertyResults) {
239
- validationMap.set(result.propertyName, result);
240
- }
241
-
242
- // Merge validation results into eventProperties (matching Android)
243
- const mergedProperties: Array<EventProperty> = eventProperties.map((prop) => {
244
- const validation = validationMap.get(prop.propertyName);
245
- if (validation) {
246
- const merged: EventProperty = { ...prop };
247
- if (validation.failedEventIds.length > 0) {
248
- merged.failedEventIds = validation.failedEventIds;
249
- }
250
- if (validation.passedEventIds.length > 0) {
251
- merged.passedEventIds = validation.passedEventIds;
252
- }
253
- return merged;
254
- }
255
- return prop;
256
- });
257
-
258
- let body = this.createBaseCallBody(anonymousId) as EventSchemaBody;
259
- body.type = "event";
260
- body.eventName = eventName;
261
- body.eventProperties = mergedProperties;
262
- body.streamId = anonymousId;
263
- body.eventSpecMetadata = eventSpecMetadata;
264
-
265
- if (eventId != null) {
266
- body.avoFunction = true;
267
- body.eventId = eventId;
268
- body.eventHash = eventHash;
269
- } else {
270
- body.avoFunction = false;
271
- body.eventId = null;
272
- body.eventHash = null;
273
- }
274
-
275
- return body;
276
- }
277
-
278
- private encryptProperties(
279
- properties: Array<{
280
- propertyName: string;
281
- propertyType: string;
282
- children?: any;
283
- }>,
284
- rawEventProperties: { [propName: string]: any }
285
- ): Array<EventProperty> {
286
- const result: Array<EventProperty> = [];
287
-
288
- for (const prop of properties) {
289
- // List-type properties: omit entirely
290
- if (AvoEncryption.isListType(prop.propertyType)) {
291
- continue;
292
- }
293
-
294
- const rawValue = rawEventProperties[prop.propertyName];
295
- const jsonValue = JSON.stringify(rawValue) ?? "null";
296
-
297
- const encrypted = AvoEncryption.encryptValue(
298
- jsonValue,
299
- this.publicEncryptionKey!
300
- );
301
-
302
- if (encrypted === null) {
303
- // Encryption failure: omit the property (warning already logged by encryptValue)
304
- continue;
305
- }
306
-
307
- result.push({
308
- propertyName: prop.propertyName,
309
- propertyType: prop.propertyType,
310
- encryptedPropertyValue: encrypted,
311
- ...(prop.children !== undefined ? { children: prop.children } : {}),
312
- });
313
- }
314
-
315
- return result;
316
- }
317
-
318
- private createBaseCallBody(anonymousId: string): BaseBody {
319
- const body: BaseBody = {
320
- apiKey: this.apiKey,
321
- appName: this.appName,
322
- appVersion: this.appVersion,
323
- libVersion: this.libVersion,
324
- env: this.envName,
325
- libPlatform: "node",
326
- messageId: AvoGuid.newGuid(),
327
- trackingId: "",
328
- sessionId: "",
329
- anonymousId: anonymousId,
330
- createdAt: new Date().toISOString(),
331
- samplingRate: this.samplingRate,
332
- };
333
-
334
- if (this.publicEncryptionKey && this.publicEncryptionKey.length > 0) {
335
- body.publicEncryptionKey = this.publicEncryptionKey;
336
- }
337
-
338
- return body;
339
- }
340
- }
@@ -1,115 +0,0 @@
1
- let isArray = (obj: any): boolean => {
2
- return Object.prototype.toString.call(obj) === "[object Array]";
3
- };
4
-
5
- export class AvoSchemaParser {
6
- static extractSchema(eventProperties: {
7
- [propName: string]: any;
8
- }): Array<{
9
- propertyName: string;
10
- propertyType: string;
11
- children?: any;
12
- }> {
13
- if (eventProperties === null || eventProperties === undefined) {
14
- return [];
15
- }
16
-
17
- let mapping = (object: any) => {
18
- if (isArray(object)) {
19
- let list = object.map((x: any) => {
20
- return mapping(x);
21
- });
22
- return this.removeDuplicates(list);
23
- } else if (typeof object === "object") {
24
- let mappedResult: any = [];
25
- for (var key in object) {
26
- if (object.hasOwnProperty(key)) {
27
- let val = object[key];
28
-
29
- let mappedEntry: {
30
- propertyName: string;
31
- propertyType: string;
32
- children?: any;
33
- } = {
34
- propertyName: key,
35
- propertyType: this.getPropValueType(val),
36
- };
37
-
38
- if (typeof val === "object" && val != null) {
39
- mappedEntry["children"] = mapping(val);
40
- }
41
-
42
- mappedResult.push(mappedEntry);
43
- }
44
- }
45
-
46
- return mappedResult;
47
- } else {
48
- return this.getPropValueType(object);
49
- }
50
- };
51
-
52
- var mappedEventProps = mapping(eventProperties);
53
-
54
- return mappedEventProps;
55
- }
56
-
57
- private static removeDuplicates(array: Array<any>): Array<any> {
58
- // XXX TODO fix any types
59
- var primitives: any = { boolean: {}, number: {}, string: {} };
60
- var objects: Array<any> = [];
61
-
62
- return array.filter((item: any) => {
63
- var type: string = typeof item;
64
- if (type in primitives) {
65
- return primitives[type].hasOwnProperty(item)
66
- ? false
67
- : (primitives[type][item] = true);
68
- } else {
69
- return objects.indexOf(item) >= 0 ? false : objects.push(item);
70
- }
71
- });
72
- }
73
-
74
-
75
- private static getBasicPropType(propValue: any): string {
76
- let propType = typeof propValue;
77
- if (propValue == null) {
78
- return "null";
79
- } else if (propType === "string") {
80
- return "string";
81
- } else if (propType === "number" || propType === "bigint") {
82
- if ((propValue + "").indexOf(".") >= 0) {
83
- return "float";
84
- } else {
85
- return "int";
86
- }
87
- } else if (propType === "boolean") {
88
- return "boolean";
89
- } else if (propType === "object") {
90
- return "object"
91
- }
92
- else {
93
- return "unknown";
94
- }
95
- }
96
-
97
- private static getPropValueType(propValue: any): string {
98
- if (isArray(propValue)){
99
-
100
- //we now know that propValue is an array. get first element in propValue array
101
- let propElement = propValue[0];
102
-
103
- if (propElement == null) {
104
- return "list(string)"; // Default to list(string) if the list is empty.
105
- }
106
- else {
107
- let propElementType = this.getBasicPropType(propElement);
108
- return `list(${propElementType})`
109
- }
110
- }
111
- else {
112
- return this.getBasicPropType(propValue);
113
- }
114
- }
115
- }
@@ -1,16 +0,0 @@
1
- export class AvoStreamId {
2
- private _streamId: string;
3
-
4
- constructor(streamId?: string) {
5
- this._streamId = streamId ?? "";
6
- if (this._streamId.includes(":")) {
7
- console.warn(
8
- "[Avo Inspector] Warning: streamId contains ':' which is not supported"
9
- );
10
- }
11
- }
12
-
13
- get streamId(): string {
14
- return this._streamId;
15
- }
16
- }