| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | /** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 8 | /** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {EXPECTED_FUNCTION} CacheAssoc */
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Defines the shared type used by this module.
|
|---|
| 14 | * @template T
|
|---|
| 15 | * @typedef {WeakMap<CacheAssoc, ObjectStructure<T>>}
|
|---|
| 16 | */
|
|---|
| 17 | const cache = new WeakMap();
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Represents ObjectStructure.
|
|---|
| 21 | * @template T
|
|---|
| 22 | */
|
|---|
| 23 | class ObjectStructure {
|
|---|
| 24 | constructor() {
|
|---|
| 25 | /** @type {undefined | keyof T[]} */
|
|---|
| 26 | this.keys = undefined;
|
|---|
| 27 | /** @type {undefined | Map<keyof T, ObjectStructure<T>>} */
|
|---|
| 28 | this.children = undefined;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Returns keys.
|
|---|
| 33 | * @param {keyof T[]} keys keys
|
|---|
| 34 | * @returns {keyof T[]} keys
|
|---|
| 35 | */
|
|---|
| 36 | getKeys(keys) {
|
|---|
| 37 | if (this.keys === undefined) this.keys = keys;
|
|---|
| 38 | return this.keys;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Returns object structure.
|
|---|
| 43 | * @param {keyof T} key key
|
|---|
| 44 | * @returns {ObjectStructure<T>} object structure
|
|---|
| 45 | */
|
|---|
| 46 | key(key) {
|
|---|
| 47 | if (this.children === undefined) this.children = new Map();
|
|---|
| 48 | const child = this.children.get(key);
|
|---|
| 49 | if (child !== undefined) return child;
|
|---|
| 50 | const newChild = new ObjectStructure();
|
|---|
| 51 | this.children.set(key, newChild);
|
|---|
| 52 | return newChild;
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Returns keys.
|
|---|
| 58 | * @template T
|
|---|
| 59 | * @param {(keyof T)[]} keys keys
|
|---|
| 60 | * @param {CacheAssoc} cacheAssoc cache assoc fn
|
|---|
| 61 | * @returns {(keyof T)[]} keys
|
|---|
| 62 | */
|
|---|
| 63 | const getCachedKeys = (keys, cacheAssoc) => {
|
|---|
| 64 | let root = cache.get(cacheAssoc);
|
|---|
| 65 | if (root === undefined) {
|
|---|
| 66 | root = new ObjectStructure();
|
|---|
| 67 | cache.set(cacheAssoc, root);
|
|---|
| 68 | }
|
|---|
| 69 | let current = root;
|
|---|
| 70 | for (const key of keys) {
|
|---|
| 71 | current = current.key(key);
|
|---|
| 72 | }
|
|---|
| 73 | return current.getKeys(keys);
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | class PlainObjectSerializer {
|
|---|
| 77 | /**
|
|---|
| 78 | * Serializes this instance into the provided serializer context.
|
|---|
| 79 | * @template {object} T
|
|---|
| 80 | * @param {T} obj plain object
|
|---|
| 81 | * @param {ObjectSerializerContext} context context
|
|---|
| 82 | */
|
|---|
| 83 | serialize(obj, context) {
|
|---|
| 84 | const keys = /** @type {(keyof T)[]} */ (Object.keys(obj));
|
|---|
| 85 | if (keys.length > 128) {
|
|---|
| 86 | // Objects with so many keys are unlikely to share structure
|
|---|
| 87 | // with other objects
|
|---|
| 88 | context.write(keys);
|
|---|
| 89 | for (const key of keys) {
|
|---|
| 90 | context.write(obj[key]);
|
|---|
| 91 | }
|
|---|
| 92 | } else if (keys.length > 1) {
|
|---|
| 93 | context.write(getCachedKeys(keys, context.write));
|
|---|
| 94 | for (const key of keys) {
|
|---|
| 95 | context.write(obj[key]);
|
|---|
| 96 | }
|
|---|
| 97 | } else if (keys.length === 1) {
|
|---|
| 98 | const key = keys[0];
|
|---|
| 99 | context.write(key);
|
|---|
| 100 | context.write(obj[key]);
|
|---|
| 101 | } else {
|
|---|
| 102 | context.write(null);
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Restores this instance from the provided deserializer context.
|
|---|
| 108 | * @template {object} T
|
|---|
| 109 | * @param {ObjectDeserializerContext} context context
|
|---|
| 110 | * @returns {T} plain object
|
|---|
| 111 | */
|
|---|
| 112 | deserialize(context) {
|
|---|
| 113 | const keys = context.read();
|
|---|
| 114 | const obj = /** @type {T} */ ({});
|
|---|
| 115 | if (Array.isArray(keys)) {
|
|---|
| 116 | for (const key of keys) {
|
|---|
| 117 | obj[/** @type {keyof T} */ (key)] = context.read();
|
|---|
| 118 | }
|
|---|
| 119 | } else if (keys !== null) {
|
|---|
| 120 | obj[/** @type {keyof T} */ (keys)] = context.read();
|
|---|
| 121 | }
|
|---|
| 122 | return obj;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | module.exports = PlainObjectSerializer;
|
|---|