This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
/
entitytypes.js
1156 lines (924 loc) · 30.9 KB
/
entitytypes.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var util = require('util');
var assert = require('assert');
var _ = require('lodash');
var debug = require('debug')('entity:types');
var slugid = require('slugid');
var stringify = require('json-stable-stringify');
var crypto = require('crypto');
var azure = require('fast-azure-storage');
var Ajv = require('ajv');
var fmt = azure.Table.Operators;
// Check that value is of types for name and property
// Print messages and throw an error if the check fails
var checkType = function(name, property, value, types) {
if (!(types instanceof Array)) {
types = [types];
}
if (types.indexOf(typeof value) === -1) {
debug('%s \'%s\' expected %j got: %j', name, property, types, value);
throw new Error(name + ' \'' + property + '\' expected one of type(s): \'' +
types.join(',') + '\' got type: \'' + typeof value + '\'');
}
};
var checkSize = function(property, value, maxSize) {
if (value.length <= maxSize) {
return;
}
var err = new Error('Property ' + property + ' is larger than ' + maxSize +
' bytes when encoded for storage');
err.code = 'PropertyTooLarge';
throw err;
};
/******************** Base Type ********************/
/** Base class for all Entity serializable data types */
var BaseType = function(property) {
this.property = property;
};
/**
* Does elements of this type have an ordering.
*
* Decides if the type can be used with comparison operators <, <=, >, >= when
* doing a table scan or query.
*/
BaseType.prototype.isOrdered = false;
/**
* Does elements of this type have a concept of equality.
*
* Decides if the typs can be used with equality and in-equality operators when
* doing a table scan or query.
*/
BaseType.prototype.isComparable = false;
/**
* Does element of this type encrypt their content
*
* If `true` the `cryptoKey` will be given as 3rd and 2nd parameter for
* `serialize` and `deserialize`, respectively. When given the encryption key
* is always 32 bytes assumed to already random.
*/
BaseType.prototype.isEncrypted = false;
/**
* Serialize value to target for property
*
* Will serialize `value` to `target` object, given `cryptoKey` if this
* is an encrypting type (one that has `isEncrypted: true`) the type must
* encrypted the data with `cryptoKey` before saving it to target.
*/
BaseType.prototype.serialize = function(target, value, cryptoKey) {
throw new Error('Not implemented');
};
/** Compare the two values (deep comparison if necessary) */
BaseType.prototype.equal = function(value1, value2) {
// Compare using serialize(), this works because serialize(), must be
// implemented, but it might not be the cheapest implementation
var target1 = {},
target2 = {};
this.serialize(target1, value1);
this.serialize(target2, value2);
return _.isEqual(target1, target2);
};
/** Constructor a fairly deep clone of this item */
BaseType.prototype.clone = function(value) {
var virtualTarget = {};
this.serialize(virtualTarget, value);
return this.deserialize(virtualTarget);
};
/** Construct $filter string with operator */
BaseType.prototype.filterCondition = function(op) {
throw new Error('Not implemented');
};
/** Apply the filter op in-memory */
BaseType.prototype.compare = function(entity, op) {
throw new Error('Not implemented');
};
/** Get a string representation for key generation (optional) */
BaseType.prototype.string = function(value) {
throw new Error('Operation is not support for this data type');
};
/** Get a string or buffer representation for hash-key generation (optional) */
BaseType.prototype.hash = function(value) {
return this.string(value);
};
/**
* Deserialize value for property from source
*
* If this is an encrypting type (one that has `isEncrypted: true`) the type
* must decrypted the data with `cryptoKey` before deserializing it.
*/
BaseType.prototype.deserialize = function(source, cryptoKey) {
throw new Error('Not implemented');
};
// Export BaseType
exports.BaseType = BaseType;
/******************** Value Type ********************/
/** Base class Value Entity types */
var BaseValueType = function(property) {
BaseType.apply(this, arguments);
};
// Inherit from BaseType
util.inherits(BaseValueType, BaseType);
BaseValueType.prototype.isOrdered = true;
BaseValueType.prototype.isComparable = true;
/** Validate the type of the value */
BaseValueType.prototype.validate = function(value) {
throw new Error('Not implemented');
};
BaseValueType.prototype.serialize = function(target, value) {
this.validate(value);
target[this.property] = value;
};
BaseValueType.prototype.equal = function(value1, value2) {
return value1 === value2;
};
BaseValueType.prototype.clone = function(value) {
return value;
};
BaseValueType.prototype.string = function(value) {
this.validate(value);
return value;
};
BaseValueType.prototype.deserialize = function(source) {
var value = source[this.property];
this.validate(value);
return value;
};
// Export BaseValueType
exports.BaseValueType = BaseValueType;
/******************** String Type ********************/
/** String Entity type */
var StringType = function(property) {
BaseValueType.apply(this, arguments);
};
// Inherit from BaseValueType
util.inherits(StringType, BaseValueType);
StringType.prototype.validate = function(value) {
checkType('StringType', this.property, value, 'string');
};
StringType.prototype.filterCondition = function(op) {
this.validate(op.operand);
return this.property + ' ' + op.operator + ' ' + fmt.string(op.operand);
};
StringType.prototype.compare = function(entity, op) {
return op.compare(entity[this.property], op.operand);
};
// Export StringType as String
exports.String = StringType;
/******************** Boolean Type ********************/
/** Boolean Entity type */
var BooleanType = function(property) {
BaseValueType.apply(this, arguments);
};
// Inherit from BaseValueType
util.inherits(BooleanType, BaseValueType);
BooleanType.prototype.isOrdered = false;
BooleanType.prototype.validate = function(value) {
checkType('BooleanType', this.property, value, 'boolean');
};
BooleanType.prototype.string = function(value) {
this.validate(value);
return value.toString();
};
BooleanType.prototype.filterCondition = function(op) {
this.validate(op.operand);
return this.property + ' ' + op.operator + ' ' + op.operand.toString();
};
BooleanType.prototype.compare = function(entity, op) {
return op.compare(entity[this.property], op.operand);
};
// Export BooleanType as Boolean
exports.Boolean = BooleanType;
/******************** Number Type ********************/
/** Number Entity type */
var NumberType = function(property) {
BaseValueType.apply(this, arguments);
};
// Inherit from BaseValueType
util.inherits(NumberType, BaseValueType);
NumberType.prototype.validate = function(value) {
checkType('NumberType', this.property, value, 'number');
};
NumberType.prototype.serialize = function(target, value) {
this.validate(value);
if (value % 1 === 0 && Math.abs(value) >= 2147483648) {
target[this.property] = value.toString();
target[this.property + '@odata.type'] = 'Edm.Int64';
} else {
// No type info for Edm.Double or Edm.Int32
target[this.property] = value;
}
};
NumberType.prototype.string = function(value) {
this.validate(value);
return value.toString();
};
NumberType.prototype.deserialize = function(source) {
var value = source[this.property];
if (source[this.property + '@odata.type'] === 'Edm.Int64') {
value = parseInt(value, 10);
}
this.validate(value);
return value;
};
NumberType.prototype.filterCondition = function(op) {
this.validate(op.operand);
return this.property + ' ' + op.operator + ' ' + fmt.number(op.operand);
};
NumberType.prototype.compare = function(entity, op) {
return op.compare(+entity[this.property], +op.operand);
};
// Export NumberType as Number
exports.Number = NumberType;
/******************** Positive Integer Type ********************/
/** Positive Integer Entity type */
var PositiveIntegerType = function(property) {
NumberType.apply(this, arguments);
};
// Inherit from NumberType
util.inherits(PositiveIntegerType, NumberType);
PositiveIntegerType.prototype.validate = function(value) {
checkType('PositiveIntegerType', this.property, value, 'number');
if (!isNaN(value) && value % 1 !== 0) {
throw new Error('PositiveIntegerType \'' + this.property + '\'' +
' expected an integer got a float or NaN');
}
if (value < 0) {
throw new Error('PositiveIntegerType \'' + this.property + '\'' +
' expected a positive integer, got less than zero');
}
if (value > Math.pow(2, 32)) {
throw new Error('PositiveIntegerType \'' + this.property + '\'' +
' expected an integer, got more than 2^32');
}
};
// Export PositiveIntegerType as PositiveInteger
exports.PositiveInteger = PositiveIntegerType;
/******************** Date Type ********************/
/** Date Entity type */
var DateType = function(property) {
BaseType.apply(this, arguments);
};
// Inherit from BaseType
util.inherits(DateType, BaseType);
DateType.prototype.isOrdered = true;
DateType.prototype.isComparable = true;
DateType.prototype.validate = function(value) {
if (!(value instanceof Date)) {
throw new Error('DateType \'' + this.property +
'\' expected a date got type: ' + typeof value);
}
};
DateType.prototype.serialize = function(target, value) {
this.validate(value);
target[this.property + '@odata.type'] = 'Edm.DateTime';
target[this.property] = value.toJSON();
};
DateType.prototype.equal = function(value1, value2) {
this.validate(value1);
this.validate(value2);
return value1.getTime() === value2.getTime();
};
DateType.prototype.clone = function(value) {
this.validate(value);
return new Date(value);
};
DateType.prototype.string = function(value) {
this.validate(value);
return value.toJSON();
};
DateType.prototype.deserialize = function(source) {
var value = new Date(source[this.property]);
this.validate(value);
return value;
};
DateType.prototype.filterCondition = function(op) {
this.validate(op.operand);
return this.property + ' ' + op.operator + ' ' + fmt.date(op.operand);
};
DateType.prototype.compare = function(entity, op) {
return op.compare(new Date(entity[this.property]).getTime(), op.operand.getTime());
};
// Export DateType as Date
exports.Date = DateType;
/******************** UUID Type ********************/
/** UUID Entity type */
var UUIDType = function(property) {
BaseValueType.apply(this, arguments);
};
// Inherit from BaseValueType
util.inherits(UUIDType, BaseValueType);
UUIDType.prototype.isOrdered = true;
UUIDType.prototype.isComparable = true;
var _uuidExpr = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i;
UUIDType.prototype.validate = function(value) {
checkType('UUIDType', this.property, value, 'string');
if (!_uuidExpr.test(value)) {
throw new Error('UUIDType \'' + this.property + '\' expected a uuid got: '
+ value);
}
};
UUIDType.prototype.equal = function(value1, value2) {
return value1.toLowerCase() === value2.toLowerCase();
};
UUIDType.prototype.string = function(value) {
return value.toLowerCase();
};
UUIDType.prototype.serialize = function(target, value) {
this.validate(value);
target[this.property + '@odata.type'] = 'Edm.Guid';
target[this.property] = value;
};
UUIDType.prototype.filterCondition = function(op) {
this.validate(op.operand);
return this.property + ' ' + op.operator + ' ' + fmt.guid(op.operand);
};
UUIDType.prototype.compare = function(entity, op) {
throw new Error('Not implemented');
};
// Export UUIDType as UUID
exports.UUID = UUIDType;
/******************** SlugId Type ********************/
/** SlugId Entity type */
var SlugIdType = function(property) {
BaseValueType.apply(this, arguments);
};
// Inherit from BaseValueType
util.inherits(SlugIdType, BaseValueType);
SlugIdType.prototype.isOrdered = true;
SlugIdType.prototype.isComparable = true;
var _slugIdExpr = /^[A-Za-z0-9_-]{8}[Q-T][A-Za-z0-9_-][CGKOSWaeimquy26-][A-Za-z0-9_-]{10}[AQgw]$/i;
SlugIdType.prototype.validate = function(value) {
checkType('SlugIdType', this.property, value, 'string');
if (!_slugIdExpr.test(value)) {
throw new Error('SlugIdType \'' + this.property +
'\' expected a slugid got: ' + value);
}
};
SlugIdType.prototype.serialize = function(target, value) {
this.validate(value);
target[this.property + '@odata.type'] = 'Edm.Guid';
target[this.property] = slugid.decode(value);
};
SlugIdType.prototype.deserialize = function(source) {
return slugid.encode(source[this.property]);
};
SlugIdType.prototype.filterCondition = function(op) {
this.validate(op.operand);
return this.property + ' ' + op.operator + ' ' +
fmt.guid(slugid.encode(op.operand));
};
SlugIdType.prototype.compare = function(entity, op) {
throw new Error('Not implemented');
};
// Export SlugIdType as SlugId
exports.SlugId = SlugIdType;
/******************** Buffer Type ********************/
/** Abstract type of all buffer based Entity types
*
* Subclasses will get `hash`, `serialize` and `deserialize` for free if they
* implement `toBuffer` and `fromBuffer`.
*/
var BaseBufferType = function(property) {
BaseType.apply(this, arguments);
};
// Inherit from BaseType
util.inherits(BaseBufferType, BaseType);
BaseBufferType.prototype.isOrdered = false;
BaseBufferType.prototype.isComparable = false;
/** Transform value to buffer */
BaseBufferType.prototype.toBuffer = function(value, cryptoKey) {
throw new Error('Not implemented');
};
/** Transform value from buffer */
BaseBufferType.prototype.fromBuffer = function(buffer, cryptoKey) {
throw new Error('Not implemented');
};
BaseBufferType.prototype.serialize = function(target, value, cryptoKey) {
value = this.toBuffer(value, cryptoKey);
checkSize(this.property, value, 256 * 1024);
// We have one chunk per 64kb
var chunks = Math.ceil(value.length / (64 * 1024));
for (var i = 0; i < chunks; i++) {
var end = Math.min((i + 1) * 64 * 1024, value.length);
var chunk = value.slice(i * 64 * 1024, end);
target['__buf' + i + '_' + this.property + '@odata.type'] = 'Edm.Binary';
target['__buf' + i + '_' + this.property] = chunk.toString('base64');
}
target['__bufchunks_' + this.property] = chunks;
};
BaseBufferType.prototype.hash = function(value) {
return this.toBuffer(value);
};
BaseBufferType.prototype.deserialize = function(source, cryptoKey) {
var n = source['__bufchunks_' + this.property];
checkType('BaseBufferType', '__bufchunks_' + this.property, n, 'number');
var chunks = [];
for (var i = 0; i < n; i++) {
chunks[i] = Buffer.from(source['__buf' + i + '_' + this.property], 'base64');
}
return this.fromBuffer(Buffer.concat(chunks), cryptoKey);
};
BaseBufferType.prototype.filterCondition = function(op) {
throw new Error('Buffer based types are not comparable!');
};
SlugIdType.prototype.compare = function(entity, op) {
throw new Error('Buffer based types are not comparable!');
};
// Export BaseBufferType as BaseBufferType
exports.BaseBufferType = BaseBufferType;
/******************** Blob Type ********************/
/** Blob Entity type */
var BlobType = function(property) {
BaseBufferType.apply(this, arguments);
};
// Inherit from BaseBufferType
util.inherits(BlobType, BaseBufferType);
BlobType.prototype.validate = function(value) {
assert(Buffer.isBuffer(value),
'BlobType \'' + this.property + '\' expected a Buffer');
};
BlobType.prototype.toBuffer = function(value) {
this.validate(value);
return value;
};
BlobType.prototype.fromBuffer = function(value) {
this.validate(value);
return value;
};
BlobType.prototype.equal = function(value1, value2) {
this.validate(value1);
this.validate(value2);
if (value1 === value2) {
return true;
}
if (value1.length !== value2.length) {
return false;
}
return Buffer.compare(value1, value2) === 0;
};
BlobType.prototype.clone = function(value) {
this.validate(value);
return Buffer.from(value);
};
// Export BlobType as Blob
exports.Blob = BlobType;
/******************** Text Type ********************/
/** Text Entity type */
var TextType = function(property) {
BaseBufferType.apply(this, arguments);
};
// Inherit from BaseBufferType
util.inherits(TextType, BaseBufferType);
TextType.prototype.validate = function(value) {
checkType('TextType', this.property, value, 'string');
};
TextType.prototype.toBuffer = function(value) {
this.validate(value);
return Buffer.from(value, 'utf8');
};
TextType.prototype.fromBuffer = function(value) {
return value.toString('utf8');
};
TextType.prototype.equal = function(value1, value2) {
return value1 === value2;
};
TextType.prototype.hash = function(value) {
return value;
};
TextType.prototype.clone = function(value) {
return value;
};
// Export TextType as Text
exports.Text = TextType;
/******************** JSON Type ********************/
/** JSON Entity type */
var JSONType = function(property) {
BaseBufferType.apply(this, arguments);
};
// Inherit from BaseBufferType
util.inherits(JSONType, BaseBufferType);
JSONType.prototype.validate = function(value) {
checkType('JSONType', this.property, value, [
'string',
'number',
'object',
'boolean',
]);
};
JSONType.prototype.toBuffer = function(value) {
this.validate(value);
return Buffer.from(JSON.stringify(value), 'utf8');
};
JSONType.prototype.fromBuffer = function(value) {
return JSON.parse(value.toString('utf8'));
};
JSONType.prototype.equal = function(value1, value2) {
return _.isEqual(value1, value2);
};
JSONType.prototype.hash = function(value) {
return stringify(value);
};
JSONType.prototype.clone = function(value) {
return _.cloneDeep(value);
};
// Export JSONType as JSON
exports.JSON = JSONType;
/******************** Schema Type ********************/
// Export SchemaEnforcedType as Schema
exports.Schema = function(schema) {
let ajv = new Ajv({useDefaults: true});
let validate = ajv.compile(schema);
/** Schema Entity type */
var SchemaEnforcedType = function(property) {
JSONType.apply(this, arguments);
};
// Inherit from JSONType
util.inherits(SchemaEnforcedType, JSONType);
SchemaEnforcedType.prototype.validate = function(value) {
if (validate(value)) {
return;
}
let err = new Error(
'SchemaEnforcedType \'' + this.property +
'\' schema validation failed: ' + ajv.errorsText(validate.errors),
);
err.errors = validate.errors;
err.value = value;
throw err;
};
return SchemaEnforcedType;
};
/******************** Encrypted Base Type ********************/
/** Encrypted Base Entity type */
var EncryptedBaseType = function(property) {
BaseBufferType.apply(this, arguments);
};
// Inherit from BaseBufferType
util.inherits(EncryptedBaseType, BaseBufferType);
// This type is encrypted an will need the encryption key
EncryptedBaseType.prototype.isEncrypted = true;
/** Transform value to buffer */
EncryptedBaseType.prototype.toPlainBuffer = function(value) {
throw new Error('Not implemented');
};
/** Transform value from buffer */
EncryptedBaseType.prototype.fromPlainBuffer = function(buffer) {
throw new Error('Not implemented');
};
EncryptedBaseType.prototype.toBuffer = function(value, cryptoKey) {
var plainBuffer = this.toPlainBuffer(value);
// Need room for initialization vector and any padding
checkSize(this.property, plainBuffer, 256 * 1024 - 32);
var iv = crypto.randomBytes(16);
var cipher = crypto.createCipheriv('aes-256-cbc', cryptoKey, iv);
var c1 = cipher.update(plainBuffer);
var c2 = cipher.final();
return Buffer.concat([iv, c1, c2]);
};
EncryptedBaseType.prototype.fromBuffer = function(buffer, cryptoKey) {
var iv = buffer.slice(0, 16);
var decipher = crypto.createDecipheriv('aes-256-cbc', cryptoKey, iv);
var b1 = decipher.update(buffer.slice(16));
var b2 = decipher.final();
return this.fromPlainBuffer(Buffer.concat([b1, b2]));
};
EncryptedBaseType.prototype.hash = function(value) {
return this.toPlainBuffer(value);
};
/******************** Encrypted Blob Type ********************/
/** Encrypted Blob Entity type */
var EncryptedBlobType = function(property) {
EncryptedBaseType.apply(this, arguments);
};
// Inherit from EncryptedBaseType
util.inherits(EncryptedBlobType, EncryptedBaseType);
EncryptedBlobType.prototype.validate = function(value) {
assert(Buffer.isBuffer(value),
'EncryptedBlobType \'' + this.property + '\' expected a Buffer');
};
EncryptedBlobType.prototype.toPlainBuffer = function(value) {
this.validate(value);
return value;
};
EncryptedBlobType.prototype.fromPlainBuffer = function(value) {
this.validate(value);
return value;
};
EncryptedBlobType.prototype.equal = function(value1, value2) {
this.validate(value1);
this.validate(value2);
if (value1 === value2) {
return true;
}
if (value1.length !== value2.length) {
return false;
}
return Buffer.compare(value1, value2) === 0;
};
EncryptedBlobType.prototype.clone = function(value) {
this.validate(value);
return Buffer.from(value);
};
// Export EncryptedBlobType as EncryptedBlob
exports.EncryptedBlob = EncryptedBlobType;
/******************** Encrypted Text Type ********************/
/** Encrypted Text Entity type */
var EncryptedTextType = function(property) {
EncryptedBaseType.apply(this, arguments);
};
// Inherit from EncryptedBaseType
util.inherits(EncryptedTextType, EncryptedBaseType);
EncryptedTextType.prototype.validate = function(value) {
checkType('EncryptedTextType', this.property, value, 'string');
};
EncryptedTextType.prototype.toPlainBuffer = function(value) {
this.validate(value);
return Buffer.from(value, 'utf8');
};
EncryptedTextType.prototype.fromPlainBuffer = function(value) {
return value.toString('utf8');
};
EncryptedTextType.prototype.equal = function(value1, value2) {
return value1 === value2;
};
EncryptedTextType.prototype.hash = function(value) {
return value;
};
EncryptedTextType.prototype.clone = function(value) {
return value;
};
// Export EncryptedTextType as Text
exports.EncryptedText = EncryptedTextType;
/******************** Encrypted JSON Type ********************/
/** Encrypted JSON Entity type */
var EncryptedJSONType = function(property) {
EncryptedBaseType.apply(this, arguments);
};
// Inherit from EncryptedBaseType
util.inherits(EncryptedJSONType, EncryptedBaseType);
EncryptedJSONType.prototype.validate = function(value) {
checkType('EncryptedJSONType', this.property, value, [
'string',
'number',
'object',
'boolean',
]);
};
EncryptedJSONType.prototype.toPlainBuffer = function(value) {
this.validate(value);
return Buffer.from(JSON.stringify(value), 'utf8');
};
EncryptedJSONType.prototype.fromPlainBuffer = function(value) {
return JSON.parse(value.toString('utf8'));
};
EncryptedJSONType.prototype.equal = function(value1, value2) {
return _.isEqual(value1, value2);
};
EncryptedJSONType.prototype.hash = function(value) {
return stringify(value);
};
EncryptedJSONType.prototype.clone = function(value) {
return _.cloneDeep(value);
};
// Export EncryptedJSONType as EncryptedJSON
exports.EncryptedJSON = EncryptedJSONType;
/******************** EncryptedSchema Type ********************/
// Export EncryptedSchemaEnforcedType as EncryptedSchema
exports.EncryptedSchema = function(schema) {
let ajv = new Ajv({useDefaults: true});
let validate = ajv.compile(schema);
/** Schema Entity type */
var EncryptedSchemaEnforcedType = function(property) {
EncryptedJSONType.apply(this, arguments);
};
// Inherit from EncryptedJSONType
util.inherits(EncryptedSchemaEnforcedType, EncryptedJSONType);
EncryptedSchemaEnforcedType.prototype.validate = function(value) {
if (validate(value)) {
return;
}
let err = new Error(
'EncryptedSchemaEnforcedType \'' + this.property +
'\' schema validation failed: ' + ajv.errorsText(validate.errors),
);
err.errors = validate.errors;
err.value = value;
throw err;
};
return EncryptedSchemaEnforcedType;
};
/******************** SlugIdArray Type ********************/
// SIZE of a slugid
var SLUGID_SIZE = 128 / 8;
// Convert slugid to buffer
var slugIdToBuffer = function(slug) {
var base64 = slug
.replace(/-/g, '+')
.replace(/_/g, '/')
+ '==';
return Buffer.from(base64, 'base64');
};
// Convert buffer to slugId where `entryIndex` is the slugId entry index to retrieve
var bufferToSlugId = function(bufferView, entryIndex) {
return bufferView.toString('base64', entryIndex * SLUGID_SIZE, SLUGID_SIZE * (entryIndex + 1))
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/==/g, '');
};
/** Array of slugids packed into a buffer for space and speed */
var SlugIdArray = function() {
this.buffer = Buffer.alloc(SLUGID_SIZE * 32);
this.length = 0;
this.avail = 32;
};
/** Retrieve all the entries added to the buffer in their original format
* i.e., before being turned into base64 slugIds
*/
SlugIdArray.prototype.toArray = function() {
const buffer = this.getBufferView();
let result = [];
for (let i = 0; i < this.length; i++) {
const slug = bufferToSlugId(buffer, i);
result.push(slug);
}
return result;
};
/** Added slugid to end of the array */
SlugIdArray.prototype.push = function(slug) {
this.realloc();
slugIdToBuffer(slug).copy(this.buffer, this.length * SLUGID_SIZE);
this.length += 1;
this.avail -= 1;
};
/** Allocate more space if needed, and less space if below threshold */
SlugIdArray.prototype.realloc = function() {
if (this.avail === 0 && this.length === 0) {
this.buffer = Buffer.alloc(SLUGID_SIZE * 32);
this.length = 0;
this.avail = 32;
return true;
}
// Allocate more space, if needed, we this by doubling the underlying buffer
if (this.avail === 0) {
var buffer = Buffer.alloc(this.length * 2 * SLUGID_SIZE);
this.buffer.copy(buffer);
this.buffer = buffer;
this.avail = this.length;
return true;
}
// Shrink the buffer if it is less than 1/3 full
if (this.avail > this.length * 2 && this.buffer.length > SLUGID_SIZE * 32) {
this.buffer = Buffer.from(this.getBufferView());
this.avail = 0;
return true;
}
return false;
};
/** Get indexOf of a slugid, -1 if it is not in the array */
SlugIdArray.prototype.indexOf = function(slug) {
var slug = slugIdToBuffer(slug);
var index = this.buffer.indexOf(slug);
while (index !== -1 && index < this.length * SLUGID_SIZE) {
if (index % SLUGID_SIZE === 0) {
return index / SLUGID_SIZE;
}
index = this.buffer.indexOf(slug, index + 1);
}
return -1;
};
/** Determines whether it includes a certain element, returning true or false as appropriate. */
SlugIdArray.prototype.includes = function(slug) {
return this.indexOf(slug) !== -1 ? true : false;
};
/**
* The shift() method removes the first element. Each operation will take a
* time proportional to the number of the array length */
SlugIdArray.prototype.shift = function() {
if (this.length === 0) {