| [9af201e] | 1 | 'use strict';
|
|---|
| 2 | const {
|
|---|
| 3 | VOID, PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT
|
|---|
| 4 | } = require('./types.js');
|
|---|
| 5 |
|
|---|
| 6 | const EMPTY = '';
|
|---|
| 7 |
|
|---|
| 8 | const {toString} = {};
|
|---|
| 9 | const {keys} = Object;
|
|---|
| 10 |
|
|---|
| 11 | const typeOf = value => {
|
|---|
| 12 | const type = typeof value;
|
|---|
| 13 | if (type !== 'object' || !value)
|
|---|
| 14 | return [PRIMITIVE, type];
|
|---|
| 15 |
|
|---|
| 16 | const asString = toString.call(value).slice(8, -1);
|
|---|
| 17 | switch (asString) {
|
|---|
| 18 | case 'Array':
|
|---|
| 19 | return [ARRAY, EMPTY];
|
|---|
| 20 | case 'Object':
|
|---|
| 21 | return [OBJECT, EMPTY];
|
|---|
| 22 | case 'Date':
|
|---|
| 23 | return [DATE, EMPTY];
|
|---|
| 24 | case 'RegExp':
|
|---|
| 25 | return [REGEXP, EMPTY];
|
|---|
| 26 | case 'Map':
|
|---|
| 27 | return [MAP, EMPTY];
|
|---|
| 28 | case 'Set':
|
|---|
| 29 | return [SET, EMPTY];
|
|---|
| 30 | case 'DataView':
|
|---|
| 31 | return [ARRAY, asString];
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | if (asString.includes('Array'))
|
|---|
| 35 | return [ARRAY, asString];
|
|---|
| 36 |
|
|---|
| 37 | if (asString.includes('Error'))
|
|---|
| 38 | return [ERROR, asString];
|
|---|
| 39 |
|
|---|
| 40 | return [OBJECT, asString];
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | const shouldSkip = ([TYPE, type]) => (
|
|---|
| 44 | TYPE === PRIMITIVE &&
|
|---|
| 45 | (type === 'function' || type === 'symbol')
|
|---|
| 46 | );
|
|---|
| 47 |
|
|---|
| 48 | const serializer = (strict, json, $, _) => {
|
|---|
| 49 |
|
|---|
| 50 | const as = (out, value) => {
|
|---|
| 51 | const index = _.push(out) - 1;
|
|---|
| 52 | $.set(value, index);
|
|---|
| 53 | return index;
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | const pair = value => {
|
|---|
| 57 | if ($.has(value))
|
|---|
| 58 | return $.get(value);
|
|---|
| 59 |
|
|---|
| 60 | let [TYPE, type] = typeOf(value);
|
|---|
| 61 | switch (TYPE) {
|
|---|
| 62 | case PRIMITIVE: {
|
|---|
| 63 | let entry = value;
|
|---|
| 64 | switch (type) {
|
|---|
| 65 | case 'bigint':
|
|---|
| 66 | TYPE = BIGINT;
|
|---|
| 67 | entry = value.toString();
|
|---|
| 68 | break;
|
|---|
| 69 | case 'function':
|
|---|
| 70 | case 'symbol':
|
|---|
| 71 | if (strict)
|
|---|
| 72 | throw new TypeError('unable to serialize ' + type);
|
|---|
| 73 | entry = null;
|
|---|
| 74 | break;
|
|---|
| 75 | case 'undefined':
|
|---|
| 76 | return as([VOID], value);
|
|---|
| 77 | }
|
|---|
| 78 | return as([TYPE, entry], value);
|
|---|
| 79 | }
|
|---|
| 80 | case ARRAY: {
|
|---|
| 81 | if (type) {
|
|---|
| 82 | let spread = value;
|
|---|
| 83 | if (type === 'DataView') {
|
|---|
| 84 | spread = new Uint8Array(value.buffer);
|
|---|
| 85 | }
|
|---|
| 86 | else if (type === 'ArrayBuffer') {
|
|---|
| 87 | spread = new Uint8Array(value);
|
|---|
| 88 | }
|
|---|
| 89 | return as([type, [...spread]], value);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | const arr = [];
|
|---|
| 93 | const index = as([TYPE, arr], value);
|
|---|
| 94 | for (const entry of value)
|
|---|
| 95 | arr.push(pair(entry));
|
|---|
| 96 | return index;
|
|---|
| 97 | }
|
|---|
| 98 | case OBJECT: {
|
|---|
| 99 | if (type) {
|
|---|
| 100 | switch (type) {
|
|---|
| 101 | case 'BigInt':
|
|---|
| 102 | return as([type, value.toString()], value);
|
|---|
| 103 | case 'Boolean':
|
|---|
| 104 | case 'Number':
|
|---|
| 105 | case 'String':
|
|---|
| 106 | return as([type, value.valueOf()], value);
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if (json && ('toJSON' in value))
|
|---|
| 111 | return pair(value.toJSON());
|
|---|
| 112 |
|
|---|
| 113 | const entries = [];
|
|---|
| 114 | const index = as([TYPE, entries], value);
|
|---|
| 115 | for (const key of keys(value)) {
|
|---|
| 116 | if (strict || !shouldSkip(typeOf(value[key])))
|
|---|
| 117 | entries.push([pair(key), pair(value[key])]);
|
|---|
| 118 | }
|
|---|
| 119 | return index;
|
|---|
| 120 | }
|
|---|
| 121 | case DATE:
|
|---|
| 122 | return as([TYPE, value.toISOString()], value);
|
|---|
| 123 | case REGEXP: {
|
|---|
| 124 | const {source, flags} = value;
|
|---|
| 125 | return as([TYPE, {source, flags}], value);
|
|---|
| 126 | }
|
|---|
| 127 | case MAP: {
|
|---|
| 128 | const entries = [];
|
|---|
| 129 | const index = as([TYPE, entries], value);
|
|---|
| 130 | for (const [key, entry] of value) {
|
|---|
| 131 | if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry))))
|
|---|
| 132 | entries.push([pair(key), pair(entry)]);
|
|---|
| 133 | }
|
|---|
| 134 | return index;
|
|---|
| 135 | }
|
|---|
| 136 | case SET: {
|
|---|
| 137 | const entries = [];
|
|---|
| 138 | const index = as([TYPE, entries], value);
|
|---|
| 139 | for (const entry of value) {
|
|---|
| 140 | if (strict || !shouldSkip(typeOf(entry)))
|
|---|
| 141 | entries.push(pair(entry));
|
|---|
| 142 | }
|
|---|
| 143 | return index;
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | const {message} = value;
|
|---|
| 148 | return as([TYPE, {name: type, message}], value);
|
|---|
| 149 | };
|
|---|
| 150 |
|
|---|
| 151 | return pair;
|
|---|
| 152 | };
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * @typedef {Array<string,any>} Record a type representation
|
|---|
| 156 | */
|
|---|
| 157 |
|
|---|
| 158 | /**
|
|---|
| 159 | * Returns an array of serialized Records.
|
|---|
| 160 | * @param {any} value a serializable value.
|
|---|
| 161 | * @param {{json?: boolean, lossy?: boolean}?} options an object with a `lossy` or `json` property that,
|
|---|
| 162 | * if `true`, will not throw errors on incompatible types, and behave more
|
|---|
| 163 | * like JSON stringify would behave. Symbol and Function will be discarded.
|
|---|
| 164 | * @returns {Record[]}
|
|---|
| 165 | */
|
|---|
| 166 | const serialize = (value, {json, lossy} = {}) => {
|
|---|
| 167 | const _ = [];
|
|---|
| 168 | return serializer(!(json || lossy), !!json, new Map, _)(value), _;
|
|---|
| 169 | };
|
|---|
| 170 | exports.serialize = serialize;
|
|---|