-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
checks.js
708 lines (649 loc) · 17.7 KB
/
checks.js
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/**
* @fileoverview Proto internal runtime checks.
*
* Checks are grouped into different severity, see:
* http://g3doc/third_party/protobuf/javascript/README.md#configurable-check-support-in-protocol-buffers
*
* Checks are also grouped into different sections:
* - CHECK_BOUNDS:
* Checks that ensure that indexed access is within bounds
* (e.g. an array being accessed past its size).
* - CHECK_STATE
* Checks related to the state of an object
* (e.g. a parser hitting an invalid case).
* - CHECK_TYPE:
* Checks that relate to type errors (e.g. code receives a number instead
* of a string).
*/
goog.module('protobuf.internal.checks');
const ByteString = goog.require('protobuf.ByteString');
const Int64 = goog.require('protobuf.Int64');
const WireType = goog.require('protobuf.binary.WireType');
//
// See
// http://g3doc/third_party/protobuf/javascript/README.md#configurable-check-support-in-protocol-buffers
//
/** @define{string} */
const CHECK_LEVEL_DEFINE = goog.define('protobuf.defines.CHECK_LEVEL', '');
/** @define{boolean} */
const POLYFILL_TEXT_ENCODING =
goog.define('protobuf.defines.POLYFILL_TEXT_ENCODING', true);
/**
* @const {number}
*/
const MAX_FIELD_NUMBER = Math.pow(2, 29) - 1;
/**
* The largest finite float32 value.
* @const {number}
*/
const FLOAT32_MAX = 3.4028234663852886e+38;
/** @enum {number} */
const CheckLevel = {
DEBUG: 0,
CRITICAL: 1,
OFF: 2
};
/** @return {!CheckLevel} */
function calculateCheckLevel() {
const definedLevel = CHECK_LEVEL_DEFINE.toUpperCase();
if (definedLevel === '') {
// user did not set a value, value now just depends on goog.DEBUG
return goog.DEBUG ? CheckLevel.DEBUG : CheckLevel.CRITICAL;
}
if (definedLevel === 'CRITICAL') {
return CheckLevel.CRITICAL;
}
if (definedLevel === 'OFF') {
return CheckLevel.OFF;
}
if (definedLevel === 'DEBUG') {
return CheckLevel.DEBUG;
}
throw new Error(`Unknown value for CHECK_LEVEL: ${CHECK_LEVEL_DEFINE}`);
}
const /** !CheckLevel */ CHECK_LEVEL = calculateCheckLevel();
const /** boolean */ CHECK_STATE = CHECK_LEVEL === CheckLevel.DEBUG;
const /** boolean */ CHECK_CRITICAL_STATE =
CHECK_LEVEL === CheckLevel.CRITICAL || CHECK_LEVEL === CheckLevel.DEBUG;
const /** boolean */ CHECK_BOUNDS = CHECK_LEVEL === CheckLevel.DEBUG;
const /** boolean */ CHECK_CRITICAL_BOUNDS =
CHECK_LEVEL === CheckLevel.CRITICAL || CHECK_LEVEL === CheckLevel.DEBUG;
const /** boolean */ CHECK_TYPE = CHECK_LEVEL === CheckLevel.DEBUG;
const /** boolean */ CHECK_CRITICAL_TYPE =
CHECK_LEVEL === CheckLevel.CRITICAL || CHECK_LEVEL === CheckLevel.DEBUG;
/**
* Ensures the truth of an expression involving the state of the calling
* instance, but not involving any parameters to the calling method.
*
* For cases where failing fast is pretty important and not failing early could
* cause bugs that are much harder to debug.
* @param {boolean} state
* @param {string=} message
* @throws {!Error} If the state is false and the check state is critical.
*/
function checkCriticalState(state, message = '') {
if (!CHECK_CRITICAL_STATE) {
return;
}
if (!state) {
throw new Error(message);
}
}
/**
* Ensures the truth of an expression involving the state of the calling
* instance, but not involving any parameters to the calling method.
*
* @param {boolean} state
* @param {string=} message
* @throws {!Error} If the state is false and the check state is debug.
*/
function checkState(state, message = '') {
if (!CHECK_STATE) {
return;
}
checkCriticalState(state, message);
}
/**
* Ensures that `index` specifies a valid position in an indexable object of
* size `size`. A position index may range from zero to size, inclusive.
* @param {number} index
* @param {number} size
* @throws {!Error} If the index is out of range and the check state is debug.
*/
function checkPositionIndex(index, size) {
if (!CHECK_BOUNDS) {
return;
}
checkCriticalPositionIndex(index, size);
}
/**
* Ensures that `index` specifies a valid position in an indexable object of
* size `size`. A position index may range from zero to size, inclusive.
* @param {number} index
* @param {number} size
* @throws {!Error} If the index is out of range and the check state is
* critical.
*/
function checkCriticalPositionIndex(index, size) {
if (!CHECK_CRITICAL_BOUNDS) {
return;
}
if (index < 0 || index > size) {
throw new Error(`Index out of bounds: index: ${index} size: ${size}`);
}
}
/**
* Ensures that `index` specifies a valid element in an indexable object of
* size `size`. A element index may range from zero to size, exclusive.
* @param {number} index
* @param {number} size
* @throws {!Error} If the index is out of range and the check state is
* debug.
*/
function checkElementIndex(index, size) {
if (!CHECK_BOUNDS) {
return;
}
checkCriticalElementIndex(index, size);
}
/**
* Ensures that `index` specifies a valid element in an indexable object of
* size `size`. A element index may range from zero to size, exclusive.
* @param {number} index
* @param {number} size
* @throws {!Error} If the index is out of range and the check state is
* critical.
*/
function checkCriticalElementIndex(index, size) {
if (!CHECK_CRITICAL_BOUNDS) {
return;
}
if (index < 0 || index >= size) {
throw new Error(`Index out of bounds: index: ${index} size: ${size}`);
}
}
/**
* Ensures the range of [start, end) is with the range of [0, size).
* @param {number} start
* @param {number} end
* @param {number} size
* @throws {!Error} If start and end are out of range and the check state is
* debug.
*/
function checkRange(start, end, size) {
if (!CHECK_BOUNDS) {
return;
}
checkCriticalRange(start, end, size);
}
/**
* Ensures the range of [start, end) is with the range of [0, size).
* @param {number} start
* @param {number} end
* @param {number} size
* @throws {!Error} If start and end are out of range and the check state is
* critical.
*/
function checkCriticalRange(start, end, size) {
if (!CHECK_CRITICAL_BOUNDS) {
return;
}
if (start < 0 || end < 0 || start > size || end > size) {
throw new Error(`Range error: start: ${start} end: ${end} size: ${size}`);
}
if (start > end) {
throw new Error(`Start > end: ${start} > ${end}`);
}
}
/**
* Ensures that field number is an integer and within the range of
* [1, MAX_FIELD_NUMBER].
* @param {number} fieldNumber
* @throws {!Error} If the field number is out of range and the check state is
* debug.
*/
function checkFieldNumber(fieldNumber) {
if (!CHECK_TYPE) {
return;
}
checkCriticalFieldNumber(fieldNumber);
}
/**
* Ensures that the value is neither null nor undefined.
*
* @param {T} value
* @return {R}
*
* @template T
* @template R :=
* mapunion(T, (V) =>
* cond(eq(V, 'null'),
* none(),
* cond(eq(V, 'undefined'),
* none(),
* V)))
* =:
*/
function checkDefAndNotNull(value) {
if (CHECK_TYPE) {
// Note that undefined == null.
if (value == null) {
throw new Error(`Value can't be null`);
}
}
return value;
}
/**
* Ensures that the value exists and is a function.
*
* @param {function(?): ?} func
*/
function checkFunctionExists(func) {
if (CHECK_TYPE) {
if (typeof func !== 'function') {
throw new Error(`${func} is not a function`);
}
}
}
/**
* Ensures that field number is an integer and within the range of
* [1, MAX_FIELD_NUMBER].
* @param {number} fieldNumber
* @throws {!Error} If the field number is out of range and the check state is
* critical.
*/
function checkCriticalFieldNumber(fieldNumber) {
if (!CHECK_CRITICAL_TYPE) {
return;
}
if (fieldNumber <= 0 || fieldNumber > MAX_FIELD_NUMBER) {
throw new Error(`Field number is out of range: ${fieldNumber}`);
}
}
/**
* Ensures that wire type is valid.
* @param {!WireType} wireType
* @throws {!Error} If the wire type is invalid and the check state is debug.
*/
function checkWireType(wireType) {
if (!CHECK_TYPE) {
return;
}
checkCriticalWireType(wireType);
}
/**
* Ensures that wire type is valid.
* @param {!WireType} wireType
* @throws {!Error} If the wire type is invalid and the check state is critical.
*/
function checkCriticalWireType(wireType) {
if (!CHECK_CRITICAL_TYPE) {
return;
}
if (wireType < WireType.VARINT || wireType > WireType.FIXED32) {
throw new Error(`Invalid wire type: ${wireType}`);
}
}
/**
* Ensures the given value has the correct type.
* @param {boolean} expression
* @param {string} errorMsg
* @throws {!Error} If the value has the wrong type and the check state is
* critical.
*/
function checkCriticalType(expression, errorMsg) {
if (!CHECK_CRITICAL_TYPE) {
return;
}
if (!expression) {
throw new Error(errorMsg);
}
}
/**
* Checks whether a given object is an array.
* @param {*} value
* @return {!Array<*>}
*/
function checkCriticalTypeArray(value) {
checkCriticalType(
Array.isArray(value), `Must be an array, but got: ${value}`);
return /** @type {!Array<*>} */ (value);
}
/**
* Checks whether a given object is an iterable.
* @param {*} value
* @return {!Iterable<*>}
*/
function checkCriticalTypeIterable(value) {
checkCriticalType(
!!value[Symbol.iterator], `Must be an iterable, but got: ${value}`);
return /** @type {!Iterable<*>} */ (value);
}
/**
* Checks whether a given object is a boolean.
* @param {*} value
*/
function checkCriticalTypeBool(value) {
checkCriticalType(
typeof value === 'boolean', `Must be a boolean, but got: ${value}`);
}
/**
* Checks whether a given object is an array of boolean.
* @param {*} values
*/
function checkCriticalTypeBoolArray(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalTypeBool(value);
}
}
/**
* Checks whether a given object is a ByteString.
* @param {*} value
*/
function checkCriticalTypeByteString(value) {
checkCriticalType(
value instanceof ByteString, `Must be a ByteString, but got: ${value}`);
}
/**
* Checks whether a given object is an array of ByteString.
* @param {*} values
*/
function checkCriticalTypeByteStringArray(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalTypeByteString(value);
}
}
/**
* Checks whether a given object is a number.
* @param {*} value
* @throws {!Error} If the value is not float and the check state is debug.
*/
function checkTypeDouble(value) {
if (!CHECK_TYPE) {
return;
}
checkCriticalTypeDouble(value);
}
/**
* Checks whether a given object is a number.
* @param {*} value
* @throws {!Error} If the value is not float and the check state is critical.
*/
function checkCriticalTypeDouble(value) {
checkCriticalType(
typeof value === 'number', `Must be a number, but got: ${value}`);
}
/**
* Checks whether a given object is an array of double.
* @param {*} values
*/
function checkCriticalTypeDoubleArray(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalTypeDouble(value);
}
}
/**
* Checks whether a given object is a number.
* @param {*} value
* @throws {!Error} If the value is not signed int32 and the check state is
* debug.
*/
function checkTypeSignedInt32(value) {
if (!CHECK_TYPE) {
return;
}
checkCriticalTypeSignedInt32(value);
}
/**
* Checks whether a given object is a number.
* @param {*} value
* @throws {!Error} If the value is not signed int32 and the check state is
* critical.
*/
function checkCriticalTypeSignedInt32(value) {
checkCriticalTypeDouble(value);
const valueAsNumber = /** @type {number} */ (value);
if (CHECK_CRITICAL_TYPE) {
if (valueAsNumber < -Math.pow(2, 31) || valueAsNumber > Math.pow(2, 31) ||
!Number.isInteger(valueAsNumber)) {
throw new Error(`Must be int32, but got: ${valueAsNumber}`);
}
}
}
/**
* Checks whether a given object is an array of numbers.
* @param {*} values
*/
function checkCriticalTypeSignedInt32Array(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalTypeSignedInt32(value);
}
}
/**
* Ensures that value is a long instance.
* @param {*} value
* @throws {!Error} If the value is not a long instance and check state is
* debug.
*/
function checkTypeSignedInt64(value) {
if (!CHECK_TYPE) {
return;
}
checkCriticalTypeSignedInt64(value);
}
/**
* Ensures that value is a long instance.
* @param {*} value
* @throws {!Error} If the value is not a long instance and check state is
* critical.
*/
function checkCriticalTypeSignedInt64(value) {
if (!CHECK_CRITICAL_TYPE) {
return;
}
if (!(value instanceof Int64)) {
throw new Error(`Must be Int64 instance, but got: ${value}`);
}
}
/**
* Checks whether a given object is an array of long instances.
* @param {*} values
* @throws {!Error} If values is not an array of long instances.
*/
function checkCriticalTypeSignedInt64Array(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalTypeSignedInt64(value);
}
}
/**
* Checks whether a given object is a number and within float32 precision.
* @param {*} value
* @throws {!Error} If the value is not float and the check state is debug.
*/
function checkTypeFloat(value) {
if (!CHECK_TYPE) {
return;
}
checkCriticalTypeFloat(value);
}
/**
* Checks whether a given object is a number and within float32 precision.
* @param {*} value
* @throws {!Error} If the value is not float and the check state is critical.
*/
function checkCriticalTypeFloat(value) {
checkCriticalTypeDouble(value);
if (CHECK_CRITICAL_TYPE) {
const valueAsNumber = /** @type {number} */ (value);
if (Number.isFinite(valueAsNumber) &&
(valueAsNumber > FLOAT32_MAX || valueAsNumber < -FLOAT32_MAX)) {
throw new Error(
`Given number does not fit into float precision: ${value}`);
}
}
}
/**
* Checks whether a given object is an iterable of floats.
* @param {*} values
*/
function checkCriticalTypeFloatIterable(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const iterable = checkCriticalTypeIterable(values);
for (const value of iterable) {
checkCriticalTypeFloat(value);
}
}
/**
* Checks whether a given object is a string.
* @param {*} value
*/
function checkCriticalTypeString(value) {
checkCriticalType(
typeof value === 'string', `Must be string, but got: ${value}`);
}
/**
* Checks whether a given object is an array of string.
* @param {*} values
*/
function checkCriticalTypeStringArray(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalTypeString(value);
}
}
/**
* Ensures that value is a valid unsigned int32.
* @param {*} value
* @throws {!Error} If the value is out of range and the check state is debug.
*/
function checkTypeUnsignedInt32(value) {
if (!CHECK_TYPE) {
return;
}
checkCriticalTypeUnsignedInt32(value);
}
/**
* Ensures that value is a valid unsigned int32.
* @param {*} value
* @throws {!Error} If the value is out of range and the check state
* is critical.
*/
function checkCriticalTypeUnsignedInt32(value) {
if (!CHECK_CRITICAL_TYPE) {
return;
}
checkCriticalTypeDouble(value);
const valueAsNumber = /** @type {number} */ (value);
if (valueAsNumber < 0 || valueAsNumber > Math.pow(2, 32) - 1 ||
!Number.isInteger(valueAsNumber)) {
throw new Error(`Must be uint32, but got: ${value}`);
}
}
/**
* Checks whether a given object is an array of unsigned int32.
* @param {*} values
*/
function checkCriticalTypeUnsignedInt32Array(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalTypeUnsignedInt32(value);
}
}
/**
* Checks whether a given object is an array of message.
* @param {*} values
*/
function checkCriticalTypeMessageArray(values) {
// TODO(b/134765672)
if (!CHECK_CRITICAL_TYPE) {
return;
}
const array = checkCriticalTypeArray(values);
for (const value of array) {
checkCriticalType(
value !== null, 'Given value is not a message instance: null');
}
}
exports = {
checkDefAndNotNull,
checkCriticalElementIndex,
checkCriticalFieldNumber,
checkCriticalPositionIndex,
checkCriticalRange,
checkCriticalState,
checkCriticalTypeBool,
checkCriticalTypeBoolArray,
checkCriticalTypeByteString,
checkCriticalTypeByteStringArray,
checkCriticalTypeDouble,
checkTypeDouble,
checkCriticalTypeDoubleArray,
checkTypeFloat,
checkCriticalTypeFloat,
checkCriticalTypeFloatIterable,
checkCriticalTypeMessageArray,
checkCriticalTypeSignedInt32,
checkCriticalTypeSignedInt32Array,
checkCriticalTypeSignedInt64,
checkTypeSignedInt64,
checkCriticalTypeSignedInt64Array,
checkCriticalTypeString,
checkCriticalTypeStringArray,
checkCriticalTypeUnsignedInt32,
checkCriticalTypeUnsignedInt32Array,
checkCriticalType,
checkCriticalWireType,
checkElementIndex,
checkFieldNumber,
checkFunctionExists,
checkPositionIndex,
checkRange,
checkState,
checkTypeUnsignedInt32,
checkTypeSignedInt32,
checkWireType,
CHECK_BOUNDS,
CHECK_CRITICAL_BOUNDS,
CHECK_STATE,
CHECK_CRITICAL_STATE,
CHECK_TYPE,
CHECK_CRITICAL_TYPE,
MAX_FIELD_NUMBER,
POLYFILL_TEXT_ENCODING,
};