-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJsonSchemaHelper.ts
More file actions
398 lines (342 loc) · 13.1 KB
/
Copy pathJsonSchemaHelper.ts
File metadata and controls
398 lines (342 loc) · 13.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import * as crypto from "crypto";
import { logger } from "../utils/logger";
import {
JsonSchemaType,
EncodingType,
DecodedType,
type JsonSchema,
} from "@use-tusk/drift-schemas/core/json_schema";
// Standardized schema type mapping
const jsToJsonSchemaTypeMapping = {
["long"]: JsonSchemaType.NUMBER,
["undefined"]: JsonSchemaType.UNDEFINED,
["string"]: JsonSchemaType.STRING,
["number"]: JsonSchemaType.NUMBER,
["Number"]: JsonSchemaType.NUMBER,
["boolean"]: JsonSchemaType.BOOLEAN,
["null"]: JsonSchemaType.NULL,
["bigint"]: JsonSchemaType.NUMBER,
["Error"]: JsonSchemaType.OBJECT,
["function"]: JsonSchemaType.FUNCTION,
["RegExp"]: JsonSchemaType.OBJECT,
["Set"]: JsonSchemaType.UNORDERED_LIST,
["symbol"]: JsonSchemaType.STRING,
["Date"]: JsonSchemaType.STRING,
["Int8Array"]: JsonSchemaType.STRING,
["Uint8Array"]: JsonSchemaType.STRING,
["Uint8ClampedArray"]: JsonSchemaType.STRING,
["Int16Array"]: JsonSchemaType.STRING,
["Uint16Array"]: JsonSchemaType.STRING,
["Int32Array"]: JsonSchemaType.STRING,
["Uint32Array"]: JsonSchemaType.STRING,
["Float32Array"]: JsonSchemaType.STRING,
["Float64Array"]: JsonSchemaType.STRING,
["DataView"]: JsonSchemaType.STRING,
["ArrayBuffer"]: JsonSchemaType.STRING,
["object"]: JsonSchemaType.OBJECT,
["Object"]: JsonSchemaType.OBJECT,
["Map"]: JsonSchemaType.OBJECT,
["Array"]: JsonSchemaType.ORDERED_LIST,
["Arguments"]: JsonSchemaType.ORDERED_LIST,
} as const;
// Re-export proto types for convenience
export { JsonSchemaType, EncodingType, DecodedType, type JsonSchema };
// The following types can be merged with the generated schema to provide additional information
// This is set in the instrumentation layer and merged with the generated schema
export interface SchemaMergeTypes {
encoding?: EncodingType;
decodedType?: DecodedType;
matchImportance?: number; // Should be between 0 and 1, 0 being the lowest importance and 1 being the highest importance
}
export type SchemaMerges = Record<string, SchemaMergeTypes>;
/**
* Utility class for JSON schema generation and hashing
*/
export class JsonSchemaHelper {
/**
* Determine the detailed type of a value using standardized mapping
*/
static getDetailedType(value: any): JsonSchemaType {
if (value === null) {
return jsToJsonSchemaTypeMapping["null"];
}
if (value === undefined) {
return jsToJsonSchemaTypeMapping["undefined"];
}
const primitiveType = typeof value;
if (primitiveType === "object") {
// More specific object type detection
const objectType = Object.prototype.toString.call(value).slice(8, -1);
if (objectType === "Array") {
return jsToJsonSchemaTypeMapping["Array"];
}
if (objectType === "Date") {
return jsToJsonSchemaTypeMapping["Date"];
}
if (objectType === "RegExp") {
return jsToJsonSchemaTypeMapping["RegExp"];
}
if (objectType === "Error") {
return jsToJsonSchemaTypeMapping["Error"];
}
if (objectType === "Set") {
return jsToJsonSchemaTypeMapping["Set"];
}
if (objectType === "Map") {
return jsToJsonSchemaTypeMapping["Map"];
}
// Typed arrays
if (objectType === "Int8Array") return jsToJsonSchemaTypeMapping["Int8Array"];
if (objectType === "Uint8Array") return jsToJsonSchemaTypeMapping["Uint8Array"];
if (objectType === "Uint8ClampedArray") return jsToJsonSchemaTypeMapping["Uint8ClampedArray"];
if (objectType === "Int16Array") return jsToJsonSchemaTypeMapping["Int16Array"];
if (objectType === "Uint16Array") return jsToJsonSchemaTypeMapping["Uint16Array"];
if (objectType === "Int32Array") return jsToJsonSchemaTypeMapping["Int32Array"];
if (objectType === "Uint32Array") return jsToJsonSchemaTypeMapping["Uint32Array"];
if (objectType === "Float32Array") return jsToJsonSchemaTypeMapping["Float32Array"];
if (objectType === "Float64Array") return jsToJsonSchemaTypeMapping["Float64Array"];
if (objectType === "DataView") return jsToJsonSchemaTypeMapping["DataView"];
if (objectType === "ArrayBuffer") return jsToJsonSchemaTypeMapping["ArrayBuffer"];
if (objectType === "Arguments") {
return jsToJsonSchemaTypeMapping["Arguments"];
}
// Default to generic object
return jsToJsonSchemaTypeMapping["object"];
}
if (primitiveType === "string") {
return jsToJsonSchemaTypeMapping["string"];
}
if (primitiveType === "number") {
return jsToJsonSchemaTypeMapping["number"];
}
if (primitiveType === "bigint") {
return jsToJsonSchemaTypeMapping["bigint"];
}
if (primitiveType === "boolean") {
return jsToJsonSchemaTypeMapping["boolean"];
}
if (primitiveType === "function") {
return jsToJsonSchemaTypeMapping["function"];
}
if (primitiveType === "symbol") {
return jsToJsonSchemaTypeMapping["symbol"];
}
// Fallback for unknown types
return jsToJsonSchemaTypeMapping["string"];
}
/**
* Merge schema override with generated schema
* This allows partial overrides while preserving generated properties
*/
private static mergeSchemaWithMerges(
generatedSchema: JsonSchema,
merges: SchemaMergeTypes,
): JsonSchema {
const merged: JsonSchema = { ...generatedSchema };
// Apply merges properties
Object.keys(merges).forEach((key) => {
(merged as any)[key] = (merges as any)[key];
});
return merged;
}
/**
* Generate schema from data object using standardized types
*
* Note: We properties always exists on JsonSchema because proto3 maps cannot be marked optional.
* The JSON data is a bit inefficient because of this, but the easiest way to handle this is to keep it for now.
*/
static generateSchema(data: any, schemaMerges?: SchemaMerges): JsonSchema {
if (data === null) {
return { type: jsToJsonSchemaTypeMapping["null"], properties: {} };
}
if (data === undefined) {
return { type: jsToJsonSchemaTypeMapping["undefined"], properties: {} };
}
const detailedType = JsonSchemaHelper.getDetailedType(data);
if (detailedType === JsonSchemaType.ORDERED_LIST) {
if (Array.isArray(data) && data.length === 0) {
return { type: JsonSchemaType.ORDERED_LIST, properties: {} };
}
const items = Array.isArray(data) && data.length > 0 ? JsonSchemaHelper.generateSchema(data[0]) : undefined;
if (items !== undefined) {
return {
type: JsonSchemaType.ORDERED_LIST,
items,
properties: {},
};
}
return { type: JsonSchemaType.ORDERED_LIST, properties: {} };
}
if (detailedType === JsonSchemaType.UNORDERED_LIST) {
// Handle Set objects
if (data instanceof Set) {
const firstItem = data.size > 0 ? data.values().next().value : null;
if (firstItem !== null) {
return {
type: JsonSchemaType.UNORDERED_LIST,
items: JsonSchemaHelper.generateSchema(firstItem),
properties: {},
};
}
}
return { type: JsonSchemaType.UNORDERED_LIST, properties: {} };
}
if (detailedType === JsonSchemaType.OBJECT) {
const schema: JsonSchema = { type: JsonSchemaType.OBJECT, properties: {} };
// Handle Map objects
if (data instanceof Map) {
data.forEach((value, key) => {
const keyString = String(key);
const generatedSchema = JsonSchemaHelper.generateSchema(value);
// Check for schema override for this key
if (schemaMerges && schemaMerges[keyString]) {
schema.properties[keyString] = JsonSchemaHelper.mergeSchemaWithMerges(
generatedSchema,
schemaMerges[keyString],
);
} else {
schema.properties[keyString] = generatedSchema;
}
});
} else if (typeof data === "object") {
Object.keys(data).forEach((key) => {
const generatedSchema = JsonSchemaHelper.generateSchema(data[key]);
// Check for schema override for this key
if (schemaMerges && schemaMerges[key]) {
schema.properties[key] = JsonSchemaHelper.mergeSchemaWithMerges(
generatedSchema,
schemaMerges[key],
);
} else {
schema.properties[key] = generatedSchema;
}
});
}
return schema;
}
// For primitive types, return just the type
return { type: detailedType, properties: {} };
}
/**
* Generate deterministic hash for any data
*/
static generateDeterministicHash(data: any): string {
// Sort object keys to ensure deterministic hashing
const sortedData = JsonSchemaHelper.sortObjectKeysRecursively(data);
const jsonString = JSON.stringify(sortedData);
return crypto.createHash("sha256").update(jsonString).digest("hex");
}
/**
* Recursively sort object keys for deterministic hashing
*/
static sortObjectKeysRecursively(obj: any): any {
if (obj === null || obj === undefined) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item) => JsonSchemaHelper.sortObjectKeysRecursively(item));
}
if (typeof obj === "object") {
const sortedObj: any = {};
Object.keys(obj)
.sort()
.forEach((key) => {
sortedObj[key] = JsonSchemaHelper.sortObjectKeysRecursively(obj[key]);
});
return sortedObj;
}
return obj;
}
/**
* Decode data based on schema merges containing encoding and decodedType
*/
private static decodeDataWithMerges(data: any, schemaMerges?: SchemaMerges): any {
if (!schemaMerges) {
return data;
}
const decodedData = { ...data };
// Process each schema override that has encoding and decodedType
for (const [key, schema] of Object.entries(schemaMerges)) {
if (schema.encoding && schema.decodedType && data[key] !== undefined) {
try {
let decodedValue = data[key];
// Decode based on encoding type
if (schema.encoding === EncodingType.BASE64 && typeof decodedValue === "string") {
const buffer = Buffer.from(decodedValue, "base64");
decodedValue = buffer.toString("utf8");
}
// Parse based on decoded type
if (schema.decodedType === DecodedType.JSON && typeof decodedValue === "string") {
decodedValue = JSON.parse(decodedValue);
}
decodedData[key] = decodedValue;
} catch (error) {
logger.warn(`[JsonSchemaHelper] Failed to decode ${key}:`, error);
// Keep original value if decoding fails
decodedData[key] = data[key];
}
}
}
return decodedData;
}
/**
* Generate schema and hash for input/output data
*
* This method handles schema merges by decoding data (e.g., base64 -> JSON) and
* generating schemas/hashes with schema merges applied.
*
* @param data - The original input data to process
* @param schemaMerges - Optional schema merges could contain encoding, decodedType, and/or matchImportance info
* @returns Object containing:
* - schema: JsonSchema for the original data with merges applied
* - decodedValueHash: Hash of the decoded data values (what the data looks like after decoding)
* - decodedSchemaHash: Hash of the decoded data schema (structure of decoded data) with schema merges applied
*
*
* E.g. if body is base64-encoded JSON, the schema will show
*
* "body": {
* "type": "OBJECT",
* "properties": {
* "location": { "type": "STRING" },
* "current": {
* "type": "OBJECT",
* "properties": {
* "temp_F": { "type": "STRING" },
* "humidity": { "type": "STRING" },
* }
* },
* },
* "encoding": "BASE64",
* "decodedType": "JSON"
* },
*
* Where the actual body is stored as a base64-encoded JSON string but the schema still demonstrates the structure of the JSON object.
*/
static generateSchemaAndHash(
data: any,
schemaMerges?: SchemaMerges,
): {
schema: JsonSchema;
decodedValueHash: string;
decodedSchemaHash: string;
} {
// Simulate the same JSON serialization/deserialization that happens during recording
// This removes undefined values and normalizes the structure
const normalizedData = JSON.parse(JSON.stringify(data));
// Decode data based on schema merges (e.g., base64 -> JSON)
const decodedData = JsonSchemaHelper.decodeDataWithMerges(normalizedData, schemaMerges);
// Generate schema for the decoded data with the schema merges applied
const schema = JsonSchemaHelper.generateSchema(decodedData, schemaMerges);
// Generate hashes for decoded data
const decodedValueHash = JsonSchemaHelper.generateDeterministicHash(decodedData);
const decodedSchemaHash = JsonSchemaHelper.generateDeterministicHash(schema);
return { schema, decodedValueHash, decodedSchemaHash };
}
/**
* Get the standardized type mapping for reference
*/
static getTypeMapping(): typeof jsToJsonSchemaTypeMapping {
return jsToJsonSchemaTypeMapping;
}
}