generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
json.ts
628 lines (569 loc) · 17.7 KB
/
json.ts
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import {
array,
define,
Infer,
integer,
is,
literal,
nullable,
number,
object,
omit,
optional,
record,
string,
Struct,
union,
unknown,
} from 'superstruct';
import { AssertionErrorConstructor, assertStruct } from './assert';
import {
calculateNumberSize,
calculateStringSize,
isPlainObject,
JsonSize,
} from './misc';
export const JsonStruct = define<Json>('Json', (value) => {
const [isValid] = validateJsonAndGetSize(value, true);
if (!isValid) {
return 'Expected a valid JSON-serializable value';
}
return true;
});
/**
* Any JSON-compatible value.
*/
export type Json =
| null
| boolean
| number
| string
| Json[]
| { [prop: string]: Json };
/**
* Check if the given value is a valid {@link Json} value, i.e., a value that is
* serializable to JSON.
*
* @param value - The value to check.
* @returns Whether the value is a valid {@link Json} value.
*/
export function isValidJson(value: unknown): value is Json {
return is(value, JsonStruct);
}
/**
* The string '2.0'.
*/
export const jsonrpc2 = '2.0' as const;
export const JsonRpcVersionStruct = literal(jsonrpc2);
/**
* A String specifying the version of the JSON-RPC protocol.
* MUST be exactly "2.0".
*/
export type JsonRpcVersion2 = typeof jsonrpc2;
export const JsonRpcIdStruct = nullable(union([number(), string()]));
/**
* An identifier established by the Client that MUST contain a String, Number,
* or NULL value if included. If it is not included it is assumed to be a
* notification. The value SHOULD normally not be Null and Numbers SHOULD
* NOT contain fractional parts.
*/
export type JsonRpcId = Infer<typeof JsonRpcIdStruct>;
export const JsonRpcErrorStruct = object({
code: integer(),
message: string(),
data: optional(JsonStruct),
stack: optional(string()),
});
/**
* Mark a certain key of a type as optional.
*/
export type OptionalField<
Type extends Record<string, unknown>,
Key extends keyof Type,
> = Omit<Type, Key> & Partial<Pick<Type, Key>>;
/**
* A JSON-RPC error object.
*
* Note that TypeScript infers `unknown | undefined` as `unknown`, meaning that
* the `data` field is not optional. To make it optional, we use the
* `OptionalField` helper, to explicitly make it optional.
*/
export type JsonRpcError = OptionalField<
Infer<typeof JsonRpcErrorStruct>,
'data'
>;
export const JsonRpcParamsStruct = optional(
union([record(string(), JsonStruct), array(JsonStruct)]),
);
export type JsonRpcParams = Infer<typeof JsonRpcParamsStruct>;
export const JsonRpcRequestStruct = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
method: string(),
params: JsonRpcParamsStruct,
});
export type InferWithParams<
Type extends Struct<any, unknown>,
Params extends JsonRpcParams,
> = Omit<Infer<Type>, 'params'> &
(keyof Params extends undefined
? {
params?: Params;
}
: {
params: Params;
});
/**
* A JSON-RPC request object.
*/
export type JsonRpcRequest<Params extends JsonRpcParams> = InferWithParams<
typeof JsonRpcRequestStruct,
Params
>;
export const JsonRpcNotificationStruct = omit(JsonRpcRequestStruct, ['id']);
/**
* A JSON-RPC notification object.
*/
export type JsonRpcNotification<Params extends JsonRpcParams> = InferWithParams<
typeof JsonRpcNotificationStruct,
Params
>;
/**
* Check if the given value is a valid {@link JsonRpcNotification} object.
*
* @param value - The value to check.
* @returns Whether the given value is a valid {@link JsonRpcNotification}
* object.
*/
export function isJsonRpcNotification(
value: unknown,
): value is JsonRpcNotification<JsonRpcParams> {
return is(value, JsonRpcNotificationStruct);
}
/**
* Assert that the given value is a valid {@link JsonRpcNotification} object.
*
* @param value - The value to check.
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the given value is not a valid {@link JsonRpcNotification} object.
*/
export function assertIsJsonRpcNotification(
value: unknown,
ErrorWrapper?: AssertionErrorConstructor,
): asserts value is JsonRpcNotification<JsonRpcParams> {
assertStruct(
value,
JsonRpcNotificationStruct,
'Invalid JSON-RPC notification',
ErrorWrapper,
);
}
/**
* Check if the given value is a valid {@link JsonRpcRequest} object.
*
* @param value - The value to check.
* @returns Whether the given value is a valid {@link JsonRpcRequest} object.
*/
export function isJsonRpcRequest(
value: unknown,
): value is JsonRpcRequest<JsonRpcParams> {
return is(value, JsonRpcRequestStruct);
}
/**
* Assert that the given value is a valid {@link JsonRpcRequest} object.
*
* @param value - The JSON-RPC request or notification to check.
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the given value is not a valid {@link JsonRpcRequest} object.
*/
export function assertIsJsonRpcRequest(
value: unknown,
ErrorWrapper?: AssertionErrorConstructor,
): asserts value is JsonRpcRequest<JsonRpcParams> {
assertStruct(
value,
JsonRpcRequestStruct,
'Invalid JSON-RPC request',
ErrorWrapper,
);
}
export const PendingJsonRpcResponseStruct = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
result: optional(unknown()),
error: optional(JsonRpcErrorStruct),
});
/**
* A JSON-RPC response object that has not yet been resolved.
*/
export type PendingJsonRpcResponse<Result extends Json> = Omit<
Infer<typeof PendingJsonRpcResponseStruct>,
'result'
> & {
result?: Result;
};
export const JsonRpcSuccessStruct = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
result: JsonStruct,
});
/**
* A successful JSON-RPC response object.
*/
export type JsonRpcSuccess<Result extends Json> = Omit<
Infer<typeof JsonRpcSuccessStruct>,
'result'
> & {
result: Result;
};
export const JsonRpcFailureStruct = object({
id: JsonRpcIdStruct,
jsonrpc: JsonRpcVersionStruct,
error: JsonRpcErrorStruct as Struct<JsonRpcError>,
});
/**
* A failed JSON-RPC response object.
*/
export type JsonRpcFailure = Infer<typeof JsonRpcFailureStruct>;
export const JsonRpcResponseStruct = union([
JsonRpcSuccessStruct,
JsonRpcFailureStruct,
]);
/**
* A JSON-RPC response object. Must be checked to determine whether it's a
* success or failure.
*
* @template Result - The type of the result.
*/
export type JsonRpcResponse<Result extends Json> =
| JsonRpcSuccess<Result>
| JsonRpcFailure;
/**
* Type guard to check whether specified JSON-RPC response is a
* {@link PendingJsonRpcResponse}.
*
* @param response - The JSON-RPC response to check.
* @returns Whether the specified JSON-RPC response is pending.
*/
export function isPendingJsonRpcResponse(
response: unknown,
): response is PendingJsonRpcResponse<Json> {
return is(response, PendingJsonRpcResponseStruct);
}
/**
* Assert that the given value is a valid {@link PendingJsonRpcResponse} object.
*
* @param response - The JSON-RPC response to check.
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the given value is not a valid {@link PendingJsonRpcResponse}
* object.
*/
export function assertIsPendingJsonRpcResponse(
response: unknown,
ErrorWrapper?: AssertionErrorConstructor,
): asserts response is PendingJsonRpcResponse<Json> {
assertStruct(
response,
PendingJsonRpcResponseStruct,
'Invalid pending JSON-RPC response',
ErrorWrapper,
);
}
/**
* Type guard to check if a value is a {@link JsonRpcResponse}.
*
* @param response - The object to check.
* @returns Whether the object is a JsonRpcResponse.
*/
export function isJsonRpcResponse(
response: unknown,
): response is JsonRpcResponse<Json> {
return is(response, JsonRpcResponseStruct);
}
/**
* Assert that the given value is a valid {@link JsonRpcResponse} object.
*
* @param value - The value to check.
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the given value is not a valid {@link JsonRpcResponse} object.
*/
export function assertIsJsonRpcResponse(
value: unknown,
ErrorWrapper?: AssertionErrorConstructor,
): asserts value is JsonRpcResponse<Json> {
assertStruct(
value,
JsonRpcResponseStruct,
'Invalid JSON-RPC response',
ErrorWrapper,
);
}
/**
* Check if the given value is a valid {@link JsonRpcSuccess} object.
*
* @param value - The value to check.
* @returns Whether the given value is a valid {@link JsonRpcSuccess} object.
*/
export function isJsonRpcSuccess(
value: unknown,
): value is JsonRpcSuccess<Json> {
return is(value, JsonRpcSuccessStruct);
}
/**
* Assert that the given value is a valid {@link JsonRpcSuccess} object.
*
* @param value - The value to check.
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the given value is not a valid {@link JsonRpcSuccess} object.
*/
export function assertIsJsonRpcSuccess(
value: unknown,
ErrorWrapper?: AssertionErrorConstructor,
): asserts value is JsonRpcSuccess<Json> {
assertStruct(
value,
JsonRpcSuccessStruct,
'Invalid JSON-RPC success response',
ErrorWrapper,
);
}
/**
* Check if the given value is a valid {@link JsonRpcFailure} object.
*
* @param value - The value to check.
* @returns Whether the given value is a valid {@link JsonRpcFailure} object.
*/
export function isJsonRpcFailure(value: unknown): value is JsonRpcFailure {
return is(value, JsonRpcFailureStruct);
}
/**
* Assert that the given value is a valid {@link JsonRpcFailure} object.
*
* @param value - The value to check.
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the given value is not a valid {@link JsonRpcFailure} object.
*/
export function assertIsJsonRpcFailure(
value: unknown,
ErrorWrapper?: AssertionErrorConstructor,
): asserts value is JsonRpcFailure {
assertStruct(
value,
JsonRpcFailureStruct,
'Invalid JSON-RPC failure response',
ErrorWrapper,
);
}
/**
* Check if the given value is a valid {@link JsonRpcError} object.
*
* @param value - The value to check.
* @returns Whether the given value is a valid {@link JsonRpcError} object.
*/
export function isJsonRpcError(value: unknown): value is JsonRpcError {
return is(value, JsonRpcErrorStruct);
}
/**
* Assert that the given value is a valid {@link JsonRpcError} object.
*
* @param value - The value to check.
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the given value is not a valid {@link JsonRpcError} object.
*/
export function assertIsJsonRpcError(
value: unknown,
ErrorWrapper?: AssertionErrorConstructor,
): asserts value is JsonRpcError {
assertStruct(
value,
JsonRpcErrorStruct,
'Invalid JSON-RPC error',
ErrorWrapper,
);
}
type JsonRpcValidatorOptions = {
permitEmptyString?: boolean;
permitFractions?: boolean;
permitNull?: boolean;
};
/**
* Gets a function for validating JSON-RPC request / response `id` values.
*
* By manipulating the options of this factory, you can control the behavior
* of the resulting validator for some edge cases. This is useful because e.g.
* `null` should sometimes but not always be permitted.
*
* Note that the empty string (`''`) is always permitted by the JSON-RPC
* specification, but that kind of sucks and you may want to forbid it in some
* instances anyway.
*
* For more details, see the
* [JSON-RPC Specification](https://www.jsonrpc.org/specification).
*
* @param options - An options object.
* @param options.permitEmptyString - Whether the empty string (i.e. `''`)
* should be treated as a valid ID. Default: `true`
* @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)
* should be treated as valid IDs. Default: `false`
* @param options.permitNull - Whether `null` should be treated as a valid ID.
* Default: `true`
* @returns The JSON-RPC ID validator function.
*/
export function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions) {
const { permitEmptyString, permitFractions, permitNull } = {
permitEmptyString: true,
permitFractions: false,
permitNull: true,
...options,
};
/**
* Type guard for {@link JsonRpcId}.
*
* @param id - The JSON-RPC ID value to check.
* @returns Whether the given ID is valid per the options given to the
* factory.
*/
const isValidJsonRpcId = (id: unknown): id is JsonRpcId => {
return Boolean(
(typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||
(typeof id === 'string' && (permitEmptyString || id.length > 0)) ||
(permitNull && id === null),
);
};
return isValidJsonRpcId;
}
/**
* Checks whether a value is JSON serializable and counts the total number
* of bytes needed to store the serialized version of the value.
*
* @param jsObject - Potential JSON serializable object.
* @param skipSizingProcess - Skip JSON size calculation (default: false).
* @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether
* the value was serializable and a number of bytes that it will use when serialized to JSON.
*/
export function validateJsonAndGetSize(
jsObject: unknown,
skipSizingProcess = false,
): [isValid: boolean, plainTextSizeInBytes: number] {
const seenObjects = new Set();
/**
* Get the value as it would be serialized by {@link JSON.stringify}. This
* checks if the value has a `toJSON` method and calls it if so.
*
* @param value - The value to get the JSON serializable value of.
* @returns The JSON serializable value of the given value.
*/
function getJsonValue(value: unknown) {
const optionalToJson = value as { toJSON?: () => unknown };
// Note: We cannot use `hasProperty` here, because the value isn't
// guaranteed to be an object.
if (optionalToJson?.toJSON && typeof optionalToJson.toJSON === 'function') {
return optionalToJson.toJSON();
}
return value;
}
/**
* Checks whether a value is JSON serializable and counts the total number
* of bytes needed to store the serialized version of the value.
*
* This function assumes the encoding of the JSON is done in UTF-8.
*
* @param rawValue - Potential JSON serializable value.
* @param skipSizing - Skip JSON size calculation (default: false).
* @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether
* the value was serializable and a number of bytes that it will use when serialized to JSON.
*/
function getJsonSerializableInfo(
rawValue: unknown,
skipSizing: boolean,
): [isValid: boolean, plainTextSizeInBytes: number] {
const value = getJsonValue(rawValue);
if (value === undefined) {
return [false, 0];
} else if (value === null) {
// Return already specified constant size for null (special object)
return [true, skipSizing ? 0 : JsonSize.Null];
}
// Check and calculate sizes for basic (and some special) types
const typeOfValue = typeof value;
if (typeOfValue === 'function') {
return [false, 0];
} else if (typeOfValue === 'string' || value instanceof String) {
return [
true,
skipSizing
? 0
: calculateStringSize(value as string) + JsonSize.Quote * 2,
];
} else if (typeOfValue === 'boolean' || value instanceof Boolean) {
if (skipSizing) {
return [true, 0];
}
// eslint-disable-next-line eqeqeq
return [true, value == true ? JsonSize.True : JsonSize.False];
} else if (typeOfValue === 'number' || value instanceof Number) {
if (skipSizing) {
return [true, 0];
}
return [true, calculateNumberSize(value as number)];
}
// If object is not plain and cannot be serialized properly,
// stop here and return false for serialization
if (!isPlainObject(value) && !Array.isArray(value)) {
return [false, 0];
}
// Circular object detection (handling)
// Check if the same object already exists
if (seenObjects.has(value)) {
return [false, 0];
}
// Add new object to the seen objects set
// Only the plain objects should be added (Primitive types are skipped)
seenObjects.add(value);
// Continue object decomposition
return [
true,
Object.entries(value).reduce(
(sum, [key, nestedValue], idx, arr) => {
// Recursively process next nested object or primitive type
// eslint-disable-next-line prefer-const
let [valid, size] = getJsonSerializableInfo(nestedValue, skipSizing);
if (!valid) {
throw new Error(
'JSON validation did not pass. Validation process stopped.',
);
}
// Circular object detection
// Once a child node is visited and processed remove it from the set.
// This will prevent false positives with the same adjacent objects.
seenObjects.delete(value);
if (skipSizing) {
return 0;
}
// Objects will have be serialized with "key": value,
// therefore we include the key in the calculation here
const keySize = Array.isArray(value)
? 0
: key.length + JsonSize.Comma + JsonSize.Colon * 2;
const separator = idx < arr.length - 1 ? JsonSize.Comma : 0;
return sum + keySize + size + separator;
},
// Starts at 2 because the serialized JSON string data (plain text)
// will minimally contain {}/[]
skipSizing ? 0 : JsonSize.Wrapper * 2,
),
];
}
try {
return getJsonSerializableInfo(jsObject, skipSizingProcess);
} catch (_) {
return [false, 0];
}
}