From d7490443514f176a190a4d2d5b6d3f981c9d5a6b Mon Sep 17 00:00:00 2001 From: rowan Date: Sun, 18 May 2025 21:47:04 -0500 Subject: [PATCH] update dist and mixins --- dist/de/generic.js | 77 ++++++++++++++++++++++++++++++--------- dist/de/mixin.js | 89 ++++++++++++++++++++++++++++++++++----------- dist/decorator.d.ts | 6 +-- dist/decorator.js | 51 ++++++++------------------ dist/ser/impl.js | 4 +- dist/ser/index.d.ts | 1 + dist/ser/index.js | 1 + dist/ser/mixin.js | 2 - src/de/mixin.ts | 6 +-- src/ser/mixin.ts | 3 -- 10 files changed, 148 insertions(+), 92 deletions(-) diff --git a/dist/de/generic.js b/dist/de/generic.js index f28db74..43c3286 100644 --- a/dist/de/generic.js +++ b/dist/de/generic.js @@ -1,26 +1,69 @@ "use strict"; -var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); - else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; - return c > 3 && r && Object.defineProperty(target, key, r), r; +var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { + function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } + var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; + var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; + var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); + var _, done = false; + for (var i = decorators.length - 1; i >= 0; i--) { + var context = {}; + for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; + for (var p in contextIn.access) context.access[p] = contextIn.access[p]; + context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; + var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); + if (kind === "accessor") { + if (result === void 0) continue; + if (result === null || typeof result !== "object") throw new TypeError("Object expected"); + if (_ = accept(result.get)) descriptor.get = _; + if (_ = accept(result.set)) descriptor.set = _; + if (_ = accept(result.init)) initializers.unshift(_); + } + else if (_ = accept(result)) { + if (kind === "field") initializers.unshift(_); + else descriptor[key] = _; + } + } + if (target) Object.defineProperty(target, contextIn.name, descriptor); + done = true; +}; +var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { + var useValue = arguments.length > 2; + for (var i = 0; i < initializers.length; i++) { + value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); + } + return useValue ? value : void 0; +}; +var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) { + if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : ""; + return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name }); }; -var GenericSeed_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.GenericVisitor = exports.GenericSeed = void 0; const utils_1 = require("../utils"); -let GenericSeed = GenericSeed_1 = class GenericSeed { - static deserialize(deserializer) { - return deserializer.deserializeAny(new GenericVisitor()); - } - deserialize(deserializer) { - return GenericSeed_1.deserialize(deserializer); - } -}; +let GenericSeed = (() => { + let _classDecorators = [(0, utils_1.staticImplements)()]; + let _classDescriptor; + let _classExtraInitializers = []; + let _classThis; + var GenericSeed = _classThis = class { + static deserialize(deserializer) { + return deserializer.deserializeAny(new GenericVisitor()); + } + deserialize(deserializer) { + return GenericSeed.deserialize(deserializer); + } + }; + __setFunctionName(_classThis, "GenericSeed"); + (() => { + const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; + __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); + GenericSeed = _classThis = _classDescriptor.value; + if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); + __runInitializers(_classThis, _classExtraInitializers); + })(); + return GenericSeed = _classThis; +})(); exports.GenericSeed = GenericSeed; -exports.GenericSeed = GenericSeed = GenericSeed_1 = __decorate([ - (0, utils_1.staticImplements)() -], GenericSeed); class GenericVisitor { visitString(value) { return value; diff --git a/dist/de/mixin.js b/dist/de/mixin.js index c6bda1f..6544f19 100644 --- a/dist/de/mixin.js +++ b/dist/de/mixin.js @@ -1,31 +1,76 @@ "use strict"; -var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); - else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; - return c > 3 && r && Object.defineProperty(target, key, r), r; +var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { + function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } + var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; + var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; + var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); + var _, done = false; + for (var i = decorators.length - 1; i >= 0; i--) { + var context = {}; + for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; + for (var p in contextIn.access) context.access[p] = contextIn.access[p]; + context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; + var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); + if (kind === "accessor") { + if (result === void 0) continue; + if (result === null || typeof result !== "object") throw new TypeError("Object expected"); + if (_ = accept(result.get)) descriptor.get = _; + if (_ = accept(result.set)) descriptor.set = _; + if (_ = accept(result.init)) initializers.unshift(_); + } + else if (_ = accept(result)) { + if (kind === "field") initializers.unshift(_); + else descriptor[key] = _; + } + } + if (target) Object.defineProperty(target, contextIn.name, descriptor); + done = true; +}; +var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { + var useValue = arguments.length > 2; + for (var i = 0; i < initializers.length; i++) { + value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); + } + return useValue ? value : void 0; +}; +var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) { + if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : ""; + return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deserialize = deserialize; -const decorator_1 = require("../decorator"); const utils_1 = require("../utils"); const impl_1 = require("./impl"); function deserialize(constructor) { - let Deserializable = class Deserializable extends constructor { - static deserialize(deserializer) { - return (0, impl_1.deserializeWith)(deserializer, this, (0, decorator_1.getMetadata)(this)); - } - }; - Object.defineProperty(Deserializable, "name", { - enumerable: true, - configurable: true, - writable: true, - value: constructor.name - }); - Deserializable = __decorate([ - (0, utils_1.staticImplements)() - ], Deserializable); - // @ts-ignore - Deserializable[decorator_1.Serde] = constructor[decorator_1.Serde]; + let Deserializable = (() => { + let _classDecorators = [(0, utils_1.staticImplements)()]; + let _classDescriptor; + let _classExtraInitializers = []; + let _classThis; + let _classSuper = constructor; + var Deserializable = _classThis = class extends _classSuper { + static deserialize(deserializer) { + return (0, impl_1.deserializeWith)(deserializer, this, constructor[Symbol.metadata]); + } + }; + __setFunctionName(_classThis, "Deserializable"); + (() => { + var _a; + const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create((_a = _classSuper[Symbol.metadata]) !== null && _a !== void 0 ? _a : null) : void 0; + __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); + Deserializable = _classThis = _classDescriptor.value; + if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); + })(); + Object.defineProperty(_classThis, "name", { + enumerable: true, + configurable: true, + writable: true, + value: constructor.name + }); + (() => { + __runInitializers(_classThis, _classExtraInitializers); + })(); + return Deserializable = _classThis; + })(); return Deserializable; } diff --git a/dist/decorator.d.ts b/dist/decorator.d.ts index ee4e9d9..92ba83e 100644 --- a/dist/decorator.d.ts +++ b/dist/decorator.d.ts @@ -1,6 +1,4 @@ import { ContainerOptions, PropertyOptions } from './options'; import { Registry } from './registry'; -export declare const Serde: unique symbol; -export declare function serde(options: ContainerOptions | PropertyOptions): (target: any, property?: PropertyKey) => any; -export declare function getMetadata(value: any): any; -export declare function register(registry?: Registry): (target: any) => any; +export declare function serde(options: ContainerOptions | PropertyOptions): (target: any, context: any) => void; +export declare function register(registry?: Registry): (target: any, _context: any) => void; diff --git a/dist/decorator.js b/dist/decorator.js index 2e7f278..61a64f4 100644 --- a/dist/decorator.js +++ b/dist/decorator.js @@ -1,54 +1,33 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.Serde = void 0; exports.serde = serde; -exports.getMetadata = getMetadata; exports.register = register; const options_1 = require("./options"); const registry_1 = require("./registry"); -exports.Serde = Symbol('Serde'); -function decorateContainer(options, constructor) { - if (constructor[exports.Serde] == null) { - constructor[exports.Serde] = new options_1.SerdeOptions(constructor, options); - } - else { - constructor[exports.Serde].options = options; - } - return constructor; +function decorateContainer(target, context, options) { + const meta = context.metadata; + const serde = (meta.serde || new options_1.SerdeOptions(target)); + serde.options = options; + meta.serde = serde; } -function decorateProperty(options, target, property) { - let constructor; - if (typeof target === 'function') { - constructor = target; - } - else { - constructor = target.constructor; - } - if (constructor[exports.Serde] == null) { - constructor[exports.Serde] = options_1.SerdeOptions.from(target); - } - constructor[exports.Serde].properties.set(property, options); - return target; +function decorateProperty(target, context, options) { + const meta = context.metadata; + const serde = (meta.serde || new options_1.SerdeOptions(target)); + serde.properties.set(context, options); + meta.serde = serde; } function serde(options) { - return function (target, property) { - if (property != null) { - return decorateProperty(options, target, property); + return function (target, context) { + if (context != null) { + decorateProperty(target, context, options); } else { - return decorateContainer(options, target); + decorateContainer(target, context, options); } }; } -function getMetadata(value) { - return value[exports.Serde]; -} function register(registry = registry_1.GlobalRegistry) { - return function (target) { - if (target[exports.Serde] == null) { - target[exports.Serde] = options_1.SerdeOptions.from(target); - } + return function (target, _context) { registry.add(target); - return target; }; } diff --git a/dist/ser/impl.js b/dist/ser/impl.js index 32248dc..6200e8e 100644 --- a/dist/ser/impl.js +++ b/dist/ser/impl.js @@ -1,7 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serializeWith = serializeWith; -const decorator_1 = require("../decorator"); const options_1 = require("../options"); const utils_1 = require("../utils"); const unhandledType = (serializer, value) => new TypeError(`'${serializer.constructor.name}' has no method for value type '${typeof value}'`); @@ -13,7 +12,6 @@ function serializeEntries(serializer, value, options) { continue; } const name = (_a = options === null || options === void 0 ? void 0 : options.getPropertyName(key, options_1.Stage.Serialize)) !== null && _a !== void 0 ? _a : key; - console.log('prop name for', key, name); state = serializer.serializeKey(name); state = serializer.serializeValue(val); } @@ -34,7 +32,7 @@ function serializeIter(serializer, value, options) { return serializer.end(); } function defaultOptions(value) { - return value.constructor[decorator_1.Serde]; + return value.constructor[Symbol.metadata]; } // dispatches in the order of serializeType -> serializeAny -> throw TypeError function serializeWith(serializer, value, optionsGetter = defaultOptions) { diff --git a/dist/ser/index.d.ts b/dist/ser/index.d.ts index aad71f9..24c342b 100644 --- a/dist/ser/index.d.ts +++ b/dist/ser/index.d.ts @@ -1,3 +1,4 @@ +import '@tsmetadata/polyfill'; export * from './interface'; export * from './mixin'; export * from './impl'; diff --git a/dist/ser/index.js b/dist/ser/index.js index a4219d9..6e183b1 100644 --- a/dist/ser/index.js +++ b/dist/ser/index.js @@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); +require("@tsmetadata/polyfill"); __exportStar(require("./interface"), exports); __exportStar(require("./mixin"), exports); __exportStar(require("./impl"), exports); diff --git a/dist/ser/mixin.js b/dist/ser/mixin.js index d36bb80..3a57d8b 100644 --- a/dist/ser/mixin.js +++ b/dist/ser/mixin.js @@ -1,7 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serialize = serialize; -const decorator_1 = require("../decorator"); const impl_1 = require("./impl"); const interface_1 = require("./interface"); function serialize(constructor) { @@ -22,6 +21,5 @@ function serialize(constructor) { writable: true, value: constructor.name }); - Serializable[decorator_1.Serde] = constructor[decorator_1.Serde]; return Serializable; } diff --git a/src/de/mixin.ts b/src/de/mixin.ts index 019d338..26e1d9c 100644 --- a/src/de/mixin.ts +++ b/src/de/mixin.ts @@ -1,4 +1,3 @@ -import { getMetadata, Serde } from '../decorator' import { Constructor, staticImplements } from '../utils' import { deserializeWith } from './impl' import { Deserialize, Deserializer } from './interface' @@ -9,13 +8,10 @@ export function deserialize(constructor: C) { static name = constructor.name static deserialize(deserializer: D): T { - return deserializeWith(deserializer, this, getMetadata(this)) + return deserializeWith(deserializer, this, (constructor as any)[Symbol.metadata]) } } - // @ts-ignore - Deserializable[Serde] = (constructor as any)[Serde] - return Deserializable } diff --git a/src/ser/mixin.ts b/src/ser/mixin.ts index bfef85e..4844adc 100644 --- a/src/ser/mixin.ts +++ b/src/ser/mixin.ts @@ -1,4 +1,3 @@ -import { Serde } from '../decorator' import { Constructor } from '../utils' import { serializeWith } from './impl' import { isGenericSerializer, Serializer } from './interface' @@ -16,7 +15,5 @@ export function serialize(constructor: T) { } } - Serializable[Serde] = (constructor as any)[Serde] - return Serializable }