@hodfords/turbo-transformer 1.0.0 → 1.1.0
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.
- package/bundles/class-transformer.umd.js +202 -14
- package/bundles/class-transformer.umd.js.map +1 -1
- package/bundles/class-transformer.umd.min.js +1 -1
- package/bundles/class-transformer.umd.min.js.map +1 -1
- package/cjs/TransformCompiler.js +143 -11
- package/cjs/TransformCompiler.js.map +1 -1
- package/cjs/TransformOperationExecutor.js +50 -1
- package/cjs/TransformOperationExecutor.js.map +1 -1
- package/esm2015/TransformCompiler.js +141 -10
- package/esm2015/TransformCompiler.js.map +1 -1
- package/esm2015/TransformOperationExecutor.js +50 -1
- package/esm2015/TransformOperationExecutor.js.map +1 -1
- package/esm5/TransformCompiler.js +153 -13
- package/esm5/TransformCompiler.js.map +1 -1
- package/esm5/TransformOperationExecutor.js +50 -1
- package/esm5/TransformOperationExecutor.js.map +1 -1
- package/package.json +1 -1
- package/types/TransformCompiler.d.ts +16 -0
- package/types/TransformOperationExecutor.d.ts +10 -0
|
@@ -368,8 +368,25 @@
|
|
|
368
368
|
function JitCache() {
|
|
369
369
|
this.version = -1;
|
|
370
370
|
this.classes = new WeakMap();
|
|
371
|
+
this.directs = new WeakMap();
|
|
371
372
|
this.anonymous = undefined;
|
|
372
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Returns the compiled transformer to call directly for elements of arrays of given type, or null.
|
|
376
|
+
*/
|
|
377
|
+
JitCache.prototype.direct = function (targetType, executor) {
|
|
378
|
+
// get() first: it drops everything when metadata changed
|
|
379
|
+
var transformer = this.get(targetType, executor);
|
|
380
|
+
if (!transformer)
|
|
381
|
+
return null;
|
|
382
|
+
var direct = this.directs.get(targetType);
|
|
383
|
+
if (direct === undefined || direct.transformer !== transformer) {
|
|
384
|
+
var resolved = helpers.resolveChild(executor, targetType);
|
|
385
|
+
direct = resolved ? new DirectTransformer(resolved, helpers.directPrototype(targetType)) : null;
|
|
386
|
+
this.directs.set(targetType, direct);
|
|
387
|
+
}
|
|
388
|
+
return direct;
|
|
389
|
+
};
|
|
373
390
|
/**
|
|
374
391
|
* Returns the compiled transformer for given target type (undefined when it can not be compiled).
|
|
375
392
|
*/
|
|
@@ -379,6 +396,7 @@
|
|
|
379
396
|
if (version !== this.version) {
|
|
380
397
|
this.version = version;
|
|
381
398
|
this.classes = new WeakMap();
|
|
399
|
+
this.directs = new WeakMap();
|
|
382
400
|
this.anonymous = undefined;
|
|
383
401
|
}
|
|
384
402
|
if (targetType === undefined || targetType === null) {
|
|
@@ -397,6 +415,24 @@
|
|
|
397
415
|
};
|
|
398
416
|
return JitCache;
|
|
399
417
|
}());
|
|
418
|
+
/**
|
|
419
|
+
* Compiled transformer of a type, with the check of the values TransformOperationExecutor#transform would
|
|
420
|
+
* transform with it: plain objects and instances of the type which are not arrays, sets, dates, maps, buffers or
|
|
421
|
+
* promises.
|
|
422
|
+
*/
|
|
423
|
+
var DirectTransformer = /** @class */ (function () {
|
|
424
|
+
function DirectTransformer(transformer, prototype) {
|
|
425
|
+
this.transformer = transformer;
|
|
426
|
+
this.prototype = prototype;
|
|
427
|
+
}
|
|
428
|
+
DirectTransformer.prototype.accepts = function (value) {
|
|
429
|
+
if (value === null || typeof value !== 'object')
|
|
430
|
+
return false;
|
|
431
|
+
var prototype = Object.getPrototypeOf(value);
|
|
432
|
+
return (prototype === Object.prototype || prototype === this.prototype) && typeof value.then !== 'function';
|
|
433
|
+
};
|
|
434
|
+
return DirectTransformer;
|
|
435
|
+
}());
|
|
400
436
|
// ---------------------------------------------------------------------------
|
|
401
437
|
// Runtime helpers used by the generated code
|
|
402
438
|
// ---------------------------------------------------------------------------
|
|
@@ -434,6 +470,47 @@
|
|
|
434
470
|
var descriptor = getPropertyDescriptor(prototype, key);
|
|
435
471
|
return !!(descriptor && !descriptor.set);
|
|
436
472
|
},
|
|
473
|
+
/**
|
|
474
|
+
* Returns the compiled transformer to call directly for objects of given (resolved) type, or null when
|
|
475
|
+
* TransformOperationExecutor#transform would not take its object branch with this type as target.
|
|
476
|
+
*/
|
|
477
|
+
resolveChild: function (executor, type) {
|
|
478
|
+
if (typeof type !== 'function' || type === String || type === Number || type === Boolean || type === Date) {
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
if (!!getGlobal().Buffer && type === Buffer)
|
|
482
|
+
return null;
|
|
483
|
+
var jit = executor.jit;
|
|
484
|
+
return (jit && jit.get(type, executor)) || null;
|
|
485
|
+
},
|
|
486
|
+
/**
|
|
487
|
+
* Prototype of the instances that can be passed directly to the compiled transformer of given type, besides plain
|
|
488
|
+
* objects: null when instances of the type are arrays, sets, dates, maps or buffers (handled by other branches).
|
|
489
|
+
*/
|
|
490
|
+
directPrototype: function (type) {
|
|
491
|
+
var prototype = type.prototype;
|
|
492
|
+
if (!prototype)
|
|
493
|
+
return null;
|
|
494
|
+
var excluded = [Array, Set, Date, Map];
|
|
495
|
+
if (getGlobal().Buffer)
|
|
496
|
+
excluded.push(Buffer);
|
|
497
|
+
for (var _i = 0, excluded_1 = excluded; _i < excluded_1.length; _i++) {
|
|
498
|
+
var cls = excluded_1[_i];
|
|
499
|
+
if (prototype === cls.prototype || prototype instanceof cls)
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
return prototype;
|
|
503
|
+
},
|
|
504
|
+
/** Same as instantiateArrayType in TransformOperationExecutor. */
|
|
505
|
+
newArray: function (arrayType) {
|
|
506
|
+
if (!arrayType)
|
|
507
|
+
return [];
|
|
508
|
+
var array = new arrayType();
|
|
509
|
+
if (!(array instanceof Set) && !('push' in array)) {
|
|
510
|
+
return [];
|
|
511
|
+
}
|
|
512
|
+
return array;
|
|
513
|
+
},
|
|
437
514
|
prefixBlocked: function (key, prefixes) {
|
|
438
515
|
return !prefixes.every(function (prefix) { return key.substr(0, prefix.length) !== prefix; });
|
|
439
516
|
},
|
|
@@ -536,6 +613,9 @@
|
|
|
536
613
|
};
|
|
537
614
|
var str = function (value) { return JSON.stringify(value); };
|
|
538
615
|
var labelCounter = 0;
|
|
616
|
+
// per call site caches of the last resolved child type => compiled transformer
|
|
617
|
+
var siteDeclarations = [];
|
|
618
|
+
var circularCheck = !!options.enableCircularCheck;
|
|
539
619
|
// custom @Transform() functions applied to a key, filtered exactly like applyCustomTransformations
|
|
540
620
|
var getTransforms = function (key) {
|
|
541
621
|
var metadatas = storage.findTransformMetadatas(target, key, transformationType);
|
|
@@ -562,8 +642,11 @@
|
|
|
562
642
|
return options.exposeUnsetFields ? assign : "if (fv !== undefined) { ".concat(assign, " }\n");
|
|
563
643
|
};
|
|
564
644
|
/**
|
|
565
|
-
* Transformation of `fv` (sub value) into `fv
|
|
566
|
-
*
|
|
645
|
+
* Transformation of `fv` (sub value) into `fv`, same as TransformOperationExecutor#transform:
|
|
646
|
+
* - primitives are converted inline,
|
|
647
|
+
* - plain objects (or instances of the target type) and arrays of them, when the type has a compiled transformer,
|
|
648
|
+
* are passed directly to that compiled transformer,
|
|
649
|
+
* - everything else goes through TransformOperationExecutor#transform.
|
|
567
650
|
*/
|
|
568
651
|
var emitChild = function (typeExpr, kind, arrayTypeExpr, isMapExpr, valueKeyExpr) {
|
|
569
652
|
var primitive;
|
|
@@ -587,8 +670,52 @@
|
|
|
587
670
|
default:
|
|
588
671
|
primitive = isMapExpr === 'true' ? 'fv' : "(".concat(isMapExpr, " ? fv : H.conv(fv, ").concat(typeExpr, "))");
|
|
589
672
|
}
|
|
673
|
+
var slow = "fv = ex.transform(source ? source[".concat(valueKeyExpr, "] : undefined, fv, ").concat(typeExpr, ", ").concat(arrayTypeExpr, ", ").concat(isMapExpr, ", level + 1);");
|
|
674
|
+
// types which can never have a compiled transformer: no direct call
|
|
675
|
+
if (kind !== 'none' && kind !== 'dynamic') {
|
|
676
|
+
return "if (typeof fv !== 'object' || fv === null) { fv = ".concat(primitive, "; }\nelse { ").concat(slow, " }\n");
|
|
677
|
+
}
|
|
678
|
+
if (typeExpr === 'undefined') {
|
|
679
|
+
return "if (typeof fv !== 'object' || fv === null) { fv = ".concat(primitive, "; }\nelse { ").concat(slow, " }\n");
|
|
680
|
+
}
|
|
681
|
+
var site = String(siteDeclarations.length);
|
|
682
|
+
var sT = 'sT' + site; // last type seen at this call site
|
|
683
|
+
var sF = 'sF' + site; // its compiled transformer (null: not directly callable)
|
|
684
|
+
var sP = 'sP' + site; // prototype of instances which can be passed directly
|
|
685
|
+
siteDeclarations.push("let ".concat(sT, " = UNSET, ").concat(sF, " = null, ").concat(sP, " = null;\n"));
|
|
686
|
+
var callDirect = function (valueExpr, levelExpr) {
|
|
687
|
+
return circularCheck
|
|
688
|
+
? "(ex.recursionStack.add(".concat(valueExpr, "), r = ").concat(sF, "(ex, undefined, ").concat(valueExpr, ", ").concat(levelExpr, "), ex.recursionStack.delete(").concat(valueExpr, "), r)")
|
|
689
|
+
: "".concat(sF, "(ex, undefined, ").concat(valueExpr, ", ").concat(levelExpr, ")");
|
|
690
|
+
};
|
|
691
|
+
var isDirect = function (valueExpr) {
|
|
692
|
+
return "((pp = Object.getPrototypeOf(".concat(valueExpr, ")) === OP || pp === ").concat(sP, ") && typeof ").concat(valueExpr, ".then !== 'function'");
|
|
693
|
+
};
|
|
694
|
+
// array branch of TransformOperationExecutor#transform, with forEach and the element type checks unrolled
|
|
695
|
+
var newArray = isPlainToClass && arrayTypeExpr !== 'undefined' ? "H.newArray(".concat(arrayTypeExpr, ")") : '[]';
|
|
696
|
+
var add = function (valueExpr) {
|
|
697
|
+
return newArray === '[]' ? "arr.push(".concat(valueExpr, ");") : "if (isSet) arr.add(".concat(valueExpr, "); else arr.push(").concat(valueExpr, ");");
|
|
698
|
+
};
|
|
699
|
+
var arrayCode = "const n = fv.length;\nconst arr = ".concat(newArray, ";\n") +
|
|
700
|
+
(newArray === '[]' ? '' : 'const isSet = arr instanceof Set;\n') +
|
|
701
|
+
"for (let j = 0; j < n; j++) {\nif (!(j in fv)) continue;\nconst el = fv[j];\n";
|
|
702
|
+
if (circularCheck) {
|
|
703
|
+
arrayCode += "if (ex.recursionStack.has(el)) { ".concat(isClassToClass ? add('el') : '', " continue; }\n");
|
|
704
|
+
}
|
|
705
|
+
arrayCode +=
|
|
706
|
+
"let r;\n" +
|
|
707
|
+
"if (typeof el !== 'object' || el === null) r = el;\n" +
|
|
708
|
+
"else if (".concat(isDirect('el'), ") r = ").concat(callDirect('el', 'level + 2'), ";\n") +
|
|
709
|
+
"else r = ex.transform(undefined, el, t, undefined, el instanceof Map, level + 2);\n" +
|
|
710
|
+
"".concat(add('r'), "\n}\nfv = arr;\n");
|
|
590
711
|
return ("if (typeof fv !== 'object' || fv === null) { fv = ".concat(primitive, "; }\n") +
|
|
591
|
-
"else {
|
|
712
|
+
"else {\nconst t = ".concat(typeExpr, ";\n") +
|
|
713
|
+
"if (t !== ".concat(sT, ") { ").concat(sT, " = t; ").concat(sF, " = H.resolveChild(ex, t); ").concat(sP, " = ").concat(sF, " === null ? null : H.directPrototype(t); }\n") +
|
|
714
|
+
"let pp, r;\n" +
|
|
715
|
+
"if (".concat(sF, " === null || source) { ").concat(slow, " }\n") +
|
|
716
|
+
"else if (Array.isArray(fv) && fv.forEach === AFE) {\n".concat(arrayCode, "}\n") +
|
|
717
|
+
"else if (!".concat(isMapExpr, " && ").concat(isDirect('fv'), ") { fv = ").concat(callDirect('fv', 'level + 1'), "; }\n") +
|
|
718
|
+
"else { ".concat(slow, " }\n}\n"));
|
|
592
719
|
};
|
|
593
720
|
/**
|
|
594
721
|
* Generates the transformation of a single key. `key` is the static key, or undefined for the
|
|
@@ -677,10 +804,15 @@
|
|
|
677
804
|
// skip keys matching a getter / method of the new value
|
|
678
805
|
if (createsInstance) {
|
|
679
806
|
var staticSkip = key === undefined ? "H.descSkip(proto, key)" : String(helpers.descSkip(targetPrototype, newValueKey));
|
|
807
|
+
var descriptorSkip = staticSkip === 'false'
|
|
808
|
+
? "(!protoIsTarget && H.descSkip(proto, ".concat(newKeyExpr, "))")
|
|
809
|
+
: staticSkip === 'true'
|
|
810
|
+
? "(protoIsTarget || H.descSkip(proto, ".concat(newKeyExpr, "))")
|
|
811
|
+
: "(protoIsTarget ? ".concat(staticSkip, " : H.descSkip(proto, ").concat(newKeyExpr, "))");
|
|
812
|
+
// `newValue.constructor.prototype` is read once per object, but must throw here like the interpreter
|
|
680
813
|
code +=
|
|
681
|
-
"
|
|
682
|
-
"if (proto && (
|
|
683
|
-
"|| newValue[".concat(newKeyExpr, "] instanceof Function)) break ").concat(label, ";\n");
|
|
814
|
+
"if (nvc == null) newValue.constructor.prototype;\n" +
|
|
815
|
+
"if (proto && (".concat(descriptorSkip, " || newValue[").concat(newKeyExpr, "] instanceof Function)) break ").concat(label, ";\n");
|
|
684
816
|
}
|
|
685
817
|
var transforms = key === undefined ? [] : getTransforms(transformKey);
|
|
686
818
|
var main = 'let fv;\n';
|
|
@@ -722,6 +854,11 @@
|
|
|
722
854
|
// keys always transformed (exposed properties), or all keys when they are not dynamic
|
|
723
855
|
var staticKeys = ex.getKeys(target, {}, false);
|
|
724
856
|
var body = "const newValue = source ? source : ".concat(createsInstance && target ? "new ".concat(ref(target), "()") : '{}', ";\nconst nvm = newValue instanceof Map;\n");
|
|
857
|
+
if (createsInstance) {
|
|
858
|
+
body +=
|
|
859
|
+
"const nvc = newValue.constructor;\nconst proto = nvc == null ? undefined : nvc.prototype;\n" +
|
|
860
|
+
"const protoIsTarget = proto === ".concat(ref(targetPrototype), ";\n");
|
|
861
|
+
}
|
|
725
862
|
if (!dynamicKeys) {
|
|
726
863
|
for (var _i = 0, staticKeys_1 = staticKeys; _i < staticKeys_1.length; _i++) {
|
|
727
864
|
var key = staticKeys_1[_i];
|
|
@@ -740,17 +877,20 @@
|
|
|
740
877
|
}
|
|
741
878
|
knownKeys_1.delete('__proto__');
|
|
742
879
|
knownKeys_1.delete('constructor');
|
|
743
|
-
var
|
|
880
|
+
var extraKeys_1 = staticKeys.filter(function (key) { return key !== '__proto__' && key !== 'constructor'; });
|
|
744
881
|
// exposed keys missing from the object are transformed after the object keys
|
|
745
|
-
|
|
746
|
-
|
|
882
|
+
// set when the key is found in the object keys
|
|
883
|
+
extraKeys_1.forEach(function (key, index) {
|
|
884
|
+
body += "let has".concat(String(index), " = false;\n");
|
|
747
885
|
});
|
|
748
886
|
var cases_1 = "case \"__proto__\": case \"constructor\": continue;\n";
|
|
749
887
|
knownKeys_1.forEach(function (key) {
|
|
750
888
|
var probe = {};
|
|
751
889
|
probe[key] = true;
|
|
752
890
|
var blocked = !ex.getKeys(target, probe, false).includes(key);
|
|
753
|
-
|
|
891
|
+
var extraIndex = extraKeys_1.indexOf(key);
|
|
892
|
+
var seen = extraIndex === -1 ? '' : "has".concat(String(extraIndex), " = true;\n");
|
|
893
|
+
cases_1 += blocked ? "case ".concat(str(key), ": continue;\n") : "case ".concat(str(key), ": ").concat(seen).concat(emitKey(key), "continue;\n");
|
|
754
894
|
});
|
|
755
895
|
body += "const keys = Object.keys(value);\n";
|
|
756
896
|
body += "for (let i = 0; i < keys.length; i++) {\nconst key = keys[i];\nswitch (key) {\n".concat(cases_1, "}\n");
|
|
@@ -759,14 +899,13 @@
|
|
|
759
899
|
}
|
|
760
900
|
body += emitKey(undefined);
|
|
761
901
|
body += '}\n';
|
|
762
|
-
|
|
902
|
+
extraKeys_1.forEach(function (key, index) {
|
|
763
903
|
body += "if (!has".concat(String(index), ") {\n").concat(emitKey(key), "}\n");
|
|
764
904
|
});
|
|
765
905
|
}
|
|
766
906
|
body += 'return newValue;\n';
|
|
767
907
|
var declarations = constants.map(function (_, index) { return "const k".concat(index, " = K[").concat(index, "];\n"); }).join('');
|
|
768
|
-
var source = "'use strict';\n".concat(declarations, "const
|
|
769
|
-
"return function compiledTransform(ex, source, value, level) {\n".concat(body, "};");
|
|
908
|
+
var source = "'use strict';\n".concat(declarations, "const OP = Object.prototype;\nconst AFE = Array.prototype.forEach;\nconst UNSET = {};\n").concat(siteDeclarations.join('')) + "return function compiledTransform(ex, source, value, level) {\n".concat(body, "};");
|
|
770
909
|
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
771
910
|
var factory = new Function('K', 'H', source);
|
|
772
911
|
return factory(constants, helpers);
|
|
@@ -811,6 +950,11 @@
|
|
|
811
950
|
var newValue_1 = arrayType && this.transformationType === exports.TransformationType.PLAIN_TO_CLASS
|
|
812
951
|
? instantiateArrayType(arrayType)
|
|
813
952
|
: [];
|
|
953
|
+
// compiled transformer called directly for the elements, see TransformCompiler
|
|
954
|
+
var direct_1 = this.jit && !source && typeof targetType === 'function' ? this.jit.direct(targetType, this) : null;
|
|
955
|
+
if (direct_1 && Array.isArray(value) && value.forEach === Array.prototype.forEach) {
|
|
956
|
+
return this.transformArrayDirect(direct_1, value, newValue_1, targetType, level);
|
|
957
|
+
}
|
|
814
958
|
value.forEach(function (subValue, index) {
|
|
815
959
|
var subSource = source ? source[index] : undefined;
|
|
816
960
|
if (!_this.options.enableCircularCheck || !_this.isCircular(subValue)) {
|
|
@@ -841,7 +985,9 @@
|
|
|
841
985
|
else {
|
|
842
986
|
realTargetType = targetType;
|
|
843
987
|
}
|
|
844
|
-
var value_1 =
|
|
988
|
+
var value_1 = direct_1 && realTargetType === targetType && direct_1.accepts(subValue)
|
|
989
|
+
? _this.transformDirect(direct_1.transformer, subValue, level + 1)
|
|
990
|
+
: _this.transform(subSource, subValue, realTargetType, undefined, subValue instanceof Map, level + 1);
|
|
845
991
|
if (newValue_1 instanceof Set) {
|
|
846
992
|
newValue_1.add(value_1);
|
|
847
993
|
}
|
|
@@ -931,6 +1077,48 @@
|
|
|
931
1077
|
// -------------------------------------------------------------------------
|
|
932
1078
|
// Private Methods
|
|
933
1079
|
// -------------------------------------------------------------------------
|
|
1080
|
+
/**
|
|
1081
|
+
* Same as the forEach of the array branch of `transform`, when elements are transformed with a class having a
|
|
1082
|
+
* compiled transformer (no source, no discriminator).
|
|
1083
|
+
*/
|
|
1084
|
+
TransformOperationExecutor.prototype.transformArrayDirect = function (direct, value, newValue, targetType, level) {
|
|
1085
|
+
var isSet = newValue instanceof Set;
|
|
1086
|
+
var length = value.length;
|
|
1087
|
+
for (var index = 0; index < length; index++) {
|
|
1088
|
+
if (!(index in value))
|
|
1089
|
+
continue; // forEach skips holes
|
|
1090
|
+
var subValue = value[index];
|
|
1091
|
+
var transformed = void 0;
|
|
1092
|
+
if (this.options.enableCircularCheck && this.isCircular(subValue)) {
|
|
1093
|
+
if (this.transformationType !== exports.TransformationType.CLASS_TO_CLASS)
|
|
1094
|
+
continue;
|
|
1095
|
+
transformed = subValue;
|
|
1096
|
+
}
|
|
1097
|
+
else if (direct.accepts(subValue)) {
|
|
1098
|
+
transformed = this.transformDirect(direct.transformer, subValue, level + 1);
|
|
1099
|
+
}
|
|
1100
|
+
else {
|
|
1101
|
+
transformed = this.transform(undefined, subValue, targetType, undefined, subValue instanceof Map, level + 1);
|
|
1102
|
+
}
|
|
1103
|
+
if (isSet)
|
|
1104
|
+
newValue.add(transformed);
|
|
1105
|
+
else
|
|
1106
|
+
newValue.push(transformed);
|
|
1107
|
+
}
|
|
1108
|
+
return newValue;
|
|
1109
|
+
};
|
|
1110
|
+
/**
|
|
1111
|
+
* Same as `transform(undefined, value, <type of the direct transformer>, undefined, false, level)`
|
|
1112
|
+
* for a value accepted by the direct transformer.
|
|
1113
|
+
*/
|
|
1114
|
+
TransformOperationExecutor.prototype.transformDirect = function (transformer, value, level) {
|
|
1115
|
+
if (!this.options.enableCircularCheck)
|
|
1116
|
+
return transformer(this, undefined, value, level);
|
|
1117
|
+
this.recursionStack.add(value);
|
|
1118
|
+
var newValue = transformer(this, undefined, value, level);
|
|
1119
|
+
this.recursionStack.delete(value);
|
|
1120
|
+
return newValue;
|
|
1121
|
+
};
|
|
934
1122
|
/**
|
|
935
1123
|
* Interpreted transformation of a single object, used when no compiled transformer is available.
|
|
936
1124
|
*/
|