Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

superjson

Package Overview
Dependencies
Maintainers
4
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

superjson - npm Package Compare versions

Comparing version 2.2.0 to 2.2.1

54

dist/accessDeep.js
import { isMap, isArray, isPlainObject, isSet } from './is.js';
import { includes } from './util.js';
var getNthKey = function (value, n) {
var keys = value.keys();
const getNthKey = (value, n) => {
const keys = value.keys();
while (n > 0) {

@@ -22,6 +22,6 @@ keys.next();

}
export var getDeep = function (object, path) {
export const getDeep = (object, path) => {
validatePath(path);
for (var i = 0; i < path.length; i++) {
var key = path[i];
for (let i = 0; i < path.length; i++) {
const key = path[i];
if (isSet(object)) {

@@ -31,5 +31,5 @@ object = getNthKey(object, +key);

else if (isMap(object)) {
var row = +key;
var type = +path[++i] === 0 ? 'key' : 'value';
var keyOfRow = getNthKey(object, row);
const row = +key;
const type = +path[++i] === 0 ? 'key' : 'value';
const keyOfRow = getNthKey(object, row);
switch (type) {

@@ -50,3 +50,3 @@ case 'key':

};
export var setDeep = function (object, path, mapper) {
export const setDeep = (object, path, mapper) => {
validatePath(path);

@@ -56,7 +56,7 @@ if (path.length === 0) {

}
var parent = object;
for (var i = 0; i < path.length - 1; i++) {
var key = path[i];
let parent = object;
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];
if (isArray(parent)) {
var index = +key;
const index = +key;
parent = parent[index];

@@ -68,13 +68,13 @@ }

else if (isSet(parent)) {
var row = +key;
const row = +key;
parent = getNthKey(parent, row);
}
else if (isMap(parent)) {
var isEnd = i === path.length - 2;
const isEnd = i === path.length - 2;
if (isEnd) {
break;
}
var row = +key;
var type = +path[++i] === 0 ? 'key' : 'value';
var keyOfRow = getNthKey(parent, row);
const row = +key;
const type = +path[++i] === 0 ? 'key' : 'value';
const keyOfRow = getNthKey(parent, row);
switch (type) {

@@ -90,3 +90,3 @@ case 'key':

}
var lastKey = path[path.length - 1];
const lastKey = path[path.length - 1];
if (isArray(parent)) {

@@ -99,6 +99,6 @@ parent[+lastKey] = mapper(parent[+lastKey]);

if (isSet(parent)) {
var oldValue = getNthKey(parent, +lastKey);
var newValue = mapper(oldValue);
const oldValue = getNthKey(parent, +lastKey);
const newValue = mapper(oldValue);
if (oldValue !== newValue) {
parent["delete"](oldValue);
parent.delete(oldValue);
parent.add(newValue);

@@ -108,11 +108,11 @@ }

if (isMap(parent)) {
var row = +path[path.length - 2];
var keyToRow = getNthKey(parent, row);
var type = +lastKey === 0 ? 'key' : 'value';
const row = +path[path.length - 2];
const keyToRow = getNthKey(parent, row);
const type = +lastKey === 0 ? 'key' : 'value';
switch (type) {
case 'key': {
var newKey = mapper(keyToRow);
const newKey = mapper(keyToRow);
parent.set(newKey, parent.get(keyToRow));
if (newKey !== keyToRow) {
parent["delete"](keyToRow);
parent.delete(keyToRow);
}

@@ -119,0 +119,0 @@ break;

@@ -1,25 +0,8 @@

var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Registry } from './registry.js';
var ClassRegistry = /** @class */ (function (_super) {
__extends(ClassRegistry, _super);
function ClassRegistry() {
var _this = _super.call(this, function (c) { return c.name; }) || this;
_this.classToAllowedProps = new Map();
return _this;
export class ClassRegistry extends Registry {
constructor() {
super(c => c.name);
this.classToAllowedProps = new Map();
}
ClassRegistry.prototype.register = function (value, options) {
register(value, options) {
if (typeof options === 'object') {

@@ -29,14 +12,12 @@ if (options.allowProps) {

}
_super.prototype.register.call(this, value, options.identifier);
super.register(value, options.identifier);
}
else {
_super.prototype.register.call(this, value, options);
super.register(value, options);
}
};
ClassRegistry.prototype.getAllowedProps = function (value) {
}
getAllowedProps(value) {
return this.classToAllowedProps.get(value);
};
return ClassRegistry;
}(Registry));
export { ClassRegistry };
}
}
//# sourceMappingURL=class-registry.js.map
import { find } from './util.js';
var CustomTransformerRegistry = /** @class */ (function () {
function CustomTransformerRegistry() {
export class CustomTransformerRegistry {
constructor() {
this.transfomers = {};
}
CustomTransformerRegistry.prototype.register = function (transformer) {
register(transformer) {
this.transfomers[transformer.name] = transformer;
};
CustomTransformerRegistry.prototype.findApplicable = function (v) {
return find(this.transfomers, function (transformer) {
return transformer.isApplicable(v);
});
};
CustomTransformerRegistry.prototype.findByName = function (name) {
}
findApplicable(v) {
return find(this.transfomers, transformer => transformer.isApplicable(v));
}
findByName(name) {
return this.transfomers[name];
};
return CustomTransformerRegistry;
}());
export { CustomTransformerRegistry };
}
}
//# sourceMappingURL=custom-transformer-registry.js.map

@@ -1,23 +0,21 @@

var DoubleIndexedKV = /** @class */ (function () {
function DoubleIndexedKV() {
export class DoubleIndexedKV {
constructor() {
this.keyToValue = new Map();
this.valueToKey = new Map();
}
DoubleIndexedKV.prototype.set = function (key, value) {
set(key, value) {
this.keyToValue.set(key, value);
this.valueToKey.set(value, key);
};
DoubleIndexedKV.prototype.getByKey = function (key) {
}
getByKey(key) {
return this.keyToValue.get(key);
};
DoubleIndexedKV.prototype.getByValue = function (value) {
}
getByValue(value) {
return this.valueToKey.get(value);
};
DoubleIndexedKV.prototype.clear = function () {
}
clear() {
this.keyToValue.clear();
this.valueToKey.clear();
};
return DoubleIndexedKV;
}());
export { DoubleIndexedKV };
}
}
//# sourceMappingURL=double-indexed-kv.js.map

@@ -1,33 +0,1 @@

var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
import { ClassRegistry } from './class-registry.js';

@@ -38,10 +6,9 @@ import { Registry } from './registry.js';

import { copy } from 'copy-anything';
var SuperJSON = /** @class */ (function () {
export default class SuperJSON {
/**
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`.
*/
function SuperJSON(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.dedupe, dedupe = _c === void 0 ? false : _c;
constructor({ dedupe = false, } = {}) {
this.classRegistry = new ClassRegistry();
this.symbolRegistry = new Registry(function (s) { var _a; return (_a = s.description) !== null && _a !== void 0 ? _a : ''; });
this.symbolRegistry = new Registry(s => s.description ?? '');
this.customTransformerRegistry = new CustomTransformerRegistry();

@@ -51,72 +18,74 @@ this.allowedErrorProps = [];

}
SuperJSON.prototype.serialize = function (object) {
var identities = new Map();
var output = walker(object, identities, this, this.dedupe);
var res = {
json: output.transformedValue
serialize(object) {
const identities = new Map();
const output = walker(object, identities, this, this.dedupe);
const res = {
json: output.transformedValue,
};
if (output.annotations) {
res.meta = __assign(__assign({}, res.meta), { values: output.annotations });
res.meta = {
...res.meta,
values: output.annotations,
};
}
var equalityAnnotations = generateReferentialEqualityAnnotations(identities, this.dedupe);
const equalityAnnotations = generateReferentialEqualityAnnotations(identities, this.dedupe);
if (equalityAnnotations) {
res.meta = __assign(__assign({}, res.meta), { referentialEqualities: equalityAnnotations });
res.meta = {
...res.meta,
referentialEqualities: equalityAnnotations,
};
}
return res;
};
SuperJSON.prototype.deserialize = function (payload) {
var json = payload.json, meta = payload.meta;
var result = copy(json);
if (meta === null || meta === void 0 ? void 0 : meta.values) {
}
deserialize(payload) {
const { json, meta } = payload;
let result = copy(json);
if (meta?.values) {
result = applyValueAnnotations(result, meta.values, this);
}
if (meta === null || meta === void 0 ? void 0 : meta.referentialEqualities) {
if (meta?.referentialEqualities) {
result = applyReferentialEqualityAnnotations(result, meta.referentialEqualities);
}
return result;
};
SuperJSON.prototype.stringify = function (object) {
}
stringify(object) {
return JSON.stringify(this.serialize(object));
};
SuperJSON.prototype.parse = function (string) {
}
parse(string) {
return this.deserialize(JSON.parse(string));
};
SuperJSON.prototype.registerClass = function (v, options) {
}
registerClass(v, options) {
this.classRegistry.register(v, options);
};
SuperJSON.prototype.registerSymbol = function (v, identifier) {
}
registerSymbol(v, identifier) {
this.symbolRegistry.register(v, identifier);
};
SuperJSON.prototype.registerCustom = function (transformer, name) {
this.customTransformerRegistry.register(__assign({ name: name }, transformer));
};
SuperJSON.prototype.allowErrorProps = function () {
var _a;
var props = [];
for (var _i = 0; _i < arguments.length; _i++) {
props[_i] = arguments[_i];
}
(_a = this.allowedErrorProps).push.apply(_a, __spreadArray([], __read(props)));
};
SuperJSON.defaultInstance = new SuperJSON();
SuperJSON.serialize = SuperJSON.defaultInstance.serialize.bind(SuperJSON.defaultInstance);
SuperJSON.deserialize = SuperJSON.defaultInstance.deserialize.bind(SuperJSON.defaultInstance);
SuperJSON.stringify = SuperJSON.defaultInstance.stringify.bind(SuperJSON.defaultInstance);
SuperJSON.parse = SuperJSON.defaultInstance.parse.bind(SuperJSON.defaultInstance);
SuperJSON.registerClass = SuperJSON.defaultInstance.registerClass.bind(SuperJSON.defaultInstance);
SuperJSON.registerSymbol = SuperJSON.defaultInstance.registerSymbol.bind(SuperJSON.defaultInstance);
SuperJSON.registerCustom = SuperJSON.defaultInstance.registerCustom.bind(SuperJSON.defaultInstance);
SuperJSON.allowErrorProps = SuperJSON.defaultInstance.allowErrorProps.bind(SuperJSON.defaultInstance);
return SuperJSON;
}());
export default SuperJSON;
}
registerCustom(transformer, name) {
this.customTransformerRegistry.register({
name,
...transformer,
});
}
allowErrorProps(...props) {
this.allowedErrorProps.push(...props);
}
}
SuperJSON.defaultInstance = new SuperJSON();
SuperJSON.serialize = SuperJSON.defaultInstance.serialize.bind(SuperJSON.defaultInstance);
SuperJSON.deserialize = SuperJSON.defaultInstance.deserialize.bind(SuperJSON.defaultInstance);
SuperJSON.stringify = SuperJSON.defaultInstance.stringify.bind(SuperJSON.defaultInstance);
SuperJSON.parse = SuperJSON.defaultInstance.parse.bind(SuperJSON.defaultInstance);
SuperJSON.registerClass = SuperJSON.defaultInstance.registerClass.bind(SuperJSON.defaultInstance);
SuperJSON.registerSymbol = SuperJSON.defaultInstance.registerSymbol.bind(SuperJSON.defaultInstance);
SuperJSON.registerCustom = SuperJSON.defaultInstance.registerCustom.bind(SuperJSON.defaultInstance);
SuperJSON.allowErrorProps = SuperJSON.defaultInstance.allowErrorProps.bind(SuperJSON.defaultInstance);
export { SuperJSON };
export var serialize = SuperJSON.serialize;
export var deserialize = SuperJSON.deserialize;
export var stringify = SuperJSON.stringify;
export var parse = SuperJSON.parse;
export var registerClass = SuperJSON.registerClass;
export var registerCustom = SuperJSON.registerCustom;
export var registerSymbol = SuperJSON.registerSymbol;
export var allowErrorProps = SuperJSON.allowErrorProps;
export const serialize = SuperJSON.serialize;
export const deserialize = SuperJSON.deserialize;
export const stringify = SuperJSON.stringify;
export const parse = SuperJSON.parse;
export const registerClass = SuperJSON.registerClass;
export const registerCustom = SuperJSON.registerCustom;
export const registerSymbol = SuperJSON.registerSymbol;
export const allowErrorProps = SuperJSON.allowErrorProps;
//# sourceMappingURL=index.js.map

@@ -1,9 +0,5 @@

var getType = function (payload) {
return Object.prototype.toString.call(payload).slice(8, -1);
};
export var isUndefined = function (payload) {
return typeof payload === 'undefined';
};
export var isNull = function (payload) { return payload === null; };
export var isPlainObject = function (payload) {
const getType = (payload) => Object.prototype.toString.call(payload).slice(8, -1);
export const isUndefined = (payload) => typeof payload === 'undefined';
export const isNull = (payload) => payload === null;
export const isPlainObject = (payload) => {
if (typeof payload !== 'object' || payload === null)

@@ -17,56 +13,24 @@ return false;

};
export var isEmptyObject = function (payload) {
return isPlainObject(payload) && Object.keys(payload).length === 0;
};
export var isArray = function (payload) {
return Array.isArray(payload);
};
export var isString = function (payload) {
return typeof payload === 'string';
};
export var isNumber = function (payload) {
return typeof payload === 'number' && !isNaN(payload);
};
export var isBoolean = function (payload) {
return typeof payload === 'boolean';
};
export var isRegExp = function (payload) {
return payload instanceof RegExp;
};
export var isMap = function (payload) {
return payload instanceof Map;
};
export var isSet = function (payload) {
return payload instanceof Set;
};
export var isSymbol = function (payload) {
return getType(payload) === 'Symbol';
};
export var isDate = function (payload) {
return payload instanceof Date && !isNaN(payload.valueOf());
};
export var isError = function (payload) {
return payload instanceof Error;
};
export var isNaNValue = function (payload) {
return typeof payload === 'number' && isNaN(payload);
};
export var isPrimitive = function (payload) {
return isBoolean(payload) ||
isNull(payload) ||
isUndefined(payload) ||
isNumber(payload) ||
isString(payload) ||
isSymbol(payload);
};
export var isBigint = function (payload) {
return typeof payload === 'bigint';
};
export var isInfinite = function (payload) {
return payload === Infinity || payload === -Infinity;
};
export var isTypedArray = function (payload) {
return ArrayBuffer.isView(payload) && !(payload instanceof DataView);
};
export var isURL = function (payload) { return payload instanceof URL; };
export const isEmptyObject = (payload) => isPlainObject(payload) && Object.keys(payload).length === 0;
export const isArray = (payload) => Array.isArray(payload);
export const isString = (payload) => typeof payload === 'string';
export const isNumber = (payload) => typeof payload === 'number' && !isNaN(payload);
export const isBoolean = (payload) => typeof payload === 'boolean';
export const isRegExp = (payload) => payload instanceof RegExp;
export const isMap = (payload) => payload instanceof Map;
export const isSet = (payload) => payload instanceof Set;
export const isSymbol = (payload) => getType(payload) === 'Symbol';
export const isDate = (payload) => payload instanceof Date && !isNaN(payload.valueOf());
export const isError = (payload) => payload instanceof Error;
export const isNaNValue = (payload) => typeof payload === 'number' && isNaN(payload);
export const isPrimitive = (payload) => isBoolean(payload) ||
isNull(payload) ||
isUndefined(payload) ||
isNumber(payload) ||
isString(payload) ||
isSymbol(payload);
export const isBigint = (payload) => typeof payload === 'bigint';
export const isInfinite = (payload) => payload === Infinity || payload === -Infinity;
export const isTypedArray = (payload) => ArrayBuffer.isView(payload) && !(payload instanceof DataView);
export const isURL = (payload) => payload instanceof URL;
//# sourceMappingURL=is.js.map

@@ -1,14 +0,12 @@

export var escapeKey = function (key) { return key.replace(/\./g, '\\.'); };
export var stringifyPath = function (path) {
return path
.map(String)
.map(escapeKey)
.join('.');
};
export var parsePath = function (string) {
var result = [];
var segment = '';
for (var i = 0; i < string.length; i++) {
var char = string.charAt(i);
var isEscapedDot = char === '\\' && string.charAt(i + 1) === '.';
export const escapeKey = (key) => key.replace(/\./g, '\\.');
export const stringifyPath = (path) => path
.map(String)
.map(escapeKey)
.join('.');
export const parsePath = (string) => {
const result = [];
let segment = '';
for (let i = 0; i < string.length; i++) {
let char = string.charAt(i);
const isEscapedDot = char === '\\' && string.charAt(i + 1) === '.';
if (isEscapedDot) {

@@ -19,3 +17,3 @@ segment += '.';

}
var isEndOfSegment = char === '.';
const isEndOfSegment = char === '.';
if (isEndOfSegment) {

@@ -28,3 +26,3 @@ result.push(segment);

}
var lastSegment = segment;
const lastSegment = segment;
result.push(lastSegment);

@@ -31,0 +29,0 @@ return result;

@@ -1,22 +0,1 @@

var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
import { isArray, isEmptyObject, isMap, isPlainObject, isPrimitive, isSet, } from './is.js';

@@ -28,4 +7,3 @@ import { escapeKey, stringifyPath } from './pathstringifier.js';

import { getDeep, setDeep } from './accessDeep.js';
function traverse(tree, walker, origin) {
if (origin === void 0) { origin = []; }
function traverse(tree, walker, origin = []) {
if (!tree) {

@@ -35,11 +13,9 @@ return;

if (!isArray(tree)) {
forEach(tree, function (subtree, key) {
return traverse(subtree, walker, __spreadArray(__spreadArray([], __read(origin)), __read(parsePath(key))));
});
forEach(tree, (subtree, key) => traverse(subtree, walker, [...origin, ...parsePath(key)]));
return;
}
var _a = __read(tree, 2), nodeValue = _a[0], children = _a[1];
const [nodeValue, children] = tree;
if (children) {
forEach(children, function (child, key) {
traverse(child, walker, __spreadArray(__spreadArray([], __read(origin)), __read(parsePath(key))));
forEach(children, (child, key) => {
traverse(child, walker, [...origin, ...parsePath(key)]);
});

@@ -50,4 +26,4 @@ }

export function applyValueAnnotations(plain, annotations, superJson) {
traverse(annotations, function (type, path) {
plain = setDeep(plain, path, function (v) { return untransformValue(v, type, superJson); });
traverse(annotations, (type, path) => {
plain = setDeep(plain, path, v => untransformValue(v, type, superJson));
});

@@ -58,11 +34,11 @@ return plain;

function apply(identicalPaths, path) {
var object = getDeep(plain, parsePath(path));
identicalPaths.map(parsePath).forEach(function (identicalObjectPath) {
plain = setDeep(plain, identicalObjectPath, function () { return object; });
const object = getDeep(plain, parsePath(path));
identicalPaths.map(parsePath).forEach(identicalObjectPath => {
plain = setDeep(plain, identicalObjectPath, () => object);
});
}
if (isArray(annotations)) {
var _a = __read(annotations, 2), root = _a[0], other = _a[1];
root.forEach(function (identicalPath) {
plain = setDeep(plain, parsePath(identicalPath), function () { return plain; });
const [root, other] = annotations;
root.forEach(identicalPath => {
plain = setDeep(plain, parsePath(identicalPath), () => plain);
});

@@ -78,11 +54,9 @@ if (other) {

}
var isDeep = function (object, superJson) {
return isPlainObject(object) ||
isArray(object) ||
isMap(object) ||
isSet(object) ||
isInstanceOfRegisteredClass(object, superJson);
};
const isDeep = (object, superJson) => isPlainObject(object) ||
isArray(object) ||
isMap(object) ||
isSet(object) ||
isInstanceOfRegisteredClass(object, superJson);
function addIdentity(object, path, identities) {
var existingSet = identities.get(object);
const existingSet = identities.get(object);
if (existingSet) {

@@ -96,5 +70,5 @@ existingSet.push(path);

export function generateReferentialEqualityAnnotations(identitites, dedupe) {
var result = {};
var rootEqualityPaths = undefined;
identitites.forEach(function (paths) {
const result = {};
let rootEqualityPaths = undefined;
identitites.forEach(paths => {
if (paths.length <= 1) {

@@ -108,6 +82,6 @@ return;

paths = paths
.map(function (path) { return path.map(String); })
.sort(function (a, b) { return a.length - b.length; });
.map(path => path.map(String))
.sort((a, b) => a.length - b.length);
}
var _a = __read(paths), representativePath = _a[0], identicalPaths = _a.slice(1);
const [representativePath, ...identicalPaths] = paths;
if (representativePath.length === 0) {

@@ -132,11 +106,7 @@ rootEqualityPaths = identicalPaths.map(stringifyPath);

}
export var walker = function (object, identities, superJson, dedupe, path, objectsInThisPath, seenObjects) {
var _a;
if (path === void 0) { path = []; }
if (objectsInThisPath === void 0) { objectsInThisPath = []; }
if (seenObjects === void 0) { seenObjects = new Map(); }
var primitive = isPrimitive(object);
export const walker = (object, identities, superJson, dedupe, path = [], objectsInThisPath = [], seenObjects = new Map()) => {
const primitive = isPrimitive(object);
if (!primitive) {
addIdentity(object, path, identities);
var seen = seenObjects.get(object);
const seen = seenObjects.get(object);
if (seen) {

@@ -146,3 +116,3 @@ // short-circuit result if we've seen this object before

? {
transformedValue: null
transformedValue: null,
}

@@ -153,15 +123,15 @@ : seen;

if (!isDeep(object, superJson)) {
var transformed_1 = transformValue(object, superJson);
var result_1 = transformed_1
const transformed = transformValue(object, superJson);
const result = transformed
? {
transformedValue: transformed_1.value,
annotations: [transformed_1.type]
transformedValue: transformed.value,
annotations: [transformed.type],
}
: {
transformedValue: object
transformedValue: object,
};
if (!primitive) {
seenObjects.set(object, result_1);
seenObjects.set(object, result);
}
return result_1;
return result;
}

@@ -171,16 +141,16 @@ if (includes(objectsInThisPath, object)) {

return {
transformedValue: null
transformedValue: null,
};
}
var transformationResult = transformValue(object, superJson);
var transformed = (_a = transformationResult === null || transformationResult === void 0 ? void 0 : transformationResult.value) !== null && _a !== void 0 ? _a : object;
var transformedValue = isArray(transformed) ? [] : {};
var innerAnnotations = {};
forEach(transformed, function (value, index) {
const transformationResult = transformValue(object, superJson);
const transformed = transformationResult?.value ?? object;
const transformedValue = isArray(transformed) ? [] : {};
const innerAnnotations = {};
forEach(transformed, (value, index) => {
if (index === '__proto__' ||
index === 'constructor' ||
index === 'prototype') {
throw new Error("Detected property " + index + ". This is a prototype pollution risk, please remove it from your object.");
throw new Error(`Detected property ${index}. This is a prototype pollution risk, please remove it from your object.`);
}
var recursiveResult = walker(value, identities, superJson, dedupe, __spreadArray(__spreadArray([], __read(path)), [index]), __spreadArray(__spreadArray([], __read(objectsInThisPath)), [object]), seenObjects);
const recursiveResult = walker(value, identities, superJson, dedupe, [...path, index], [...objectsInThisPath, object], seenObjects);
transformedValue[index] = recursiveResult.transformedValue;

@@ -191,3 +161,3 @@ if (isArray(recursiveResult.annotations)) {

else if (isPlainObject(recursiveResult.annotations)) {
forEach(recursiveResult.annotations, function (tree, key) {
forEach(recursiveResult.annotations, (tree, key) => {
innerAnnotations[escapeKey(index) + '.' + key] = tree;

@@ -197,14 +167,14 @@ });

});
var result = isEmptyObject(innerAnnotations)
const result = isEmptyObject(innerAnnotations)
? {
transformedValue: transformedValue,
transformedValue,
annotations: !!transformationResult
? [transformationResult.type]
: undefined
: undefined,
}
: {
transformedValue: transformedValue,
transformedValue,
annotations: !!transformationResult
? [transformationResult.type, innerAnnotations]
: innerAnnotations
: innerAnnotations,
};

@@ -211,0 +181,0 @@ if (!primitive) {

import { DoubleIndexedKV } from './double-indexed-kv.js';
var Registry = /** @class */ (function () {
function Registry(generateIdentifier) {
export class Registry {
constructor(generateIdentifier) {
this.generateIdentifier = generateIdentifier;
this.kv = new DoubleIndexedKV();
}
Registry.prototype.register = function (value, identifier) {
register(value, identifier) {
if (this.kv.getByValue(value)) {

@@ -15,15 +15,13 @@ return;

this.kv.set(identifier, value);
};
Registry.prototype.clear = function () {
}
clear() {
this.kv.clear();
};
Registry.prototype.getIdentifier = function (value) {
}
getIdentifier(value) {
return this.kv.getByValue(value);
};
Registry.prototype.getValue = function (identifier) {
}
getValue(identifier) {
return this.kv.getByKey(identifier);
};
return Registry;
}());
export { Registry };
}
}
//# sourceMappingURL=registry.js.map

@@ -1,33 +0,1 @@

var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
import { isBigint, isDate, isInfinite, isMap, isNaNValue, isRegExp, isSet, isUndefined, isSymbol, isArray, isError, isTypedArray, isURL, } from './is.js';

@@ -37,11 +5,11 @@ import { findArr } from './util.js';

return {
isApplicable: isApplicable,
annotation: annotation,
transform: transform,
untransform: untransform
isApplicable,
annotation,
transform,
untransform,
};
}
var simpleRules = [
simpleTransformation(isUndefined, 'undefined', function () { return null; }, function () { return undefined; }),
simpleTransformation(isBigint, 'bigint', function (v) { return v.toString(); }, function (v) {
const simpleRules = [
simpleTransformation(isUndefined, 'undefined', () => null, () => undefined),
simpleTransformation(isBigint, 'bigint', v => v.toString(), v => {
if (typeof BigInt !== 'undefined') {

@@ -53,17 +21,17 @@ return BigInt(v);

}),
simpleTransformation(isDate, 'Date', function (v) { return v.toISOString(); }, function (v) { return new Date(v); }),
simpleTransformation(isError, 'Error', function (v, superJson) {
var baseError = {
simpleTransformation(isDate, 'Date', v => v.toISOString(), v => new Date(v)),
simpleTransformation(isError, 'Error', (v, superJson) => {
const baseError = {
name: v.name,
message: v.message
message: v.message,
};
superJson.allowedErrorProps.forEach(function (prop) {
superJson.allowedErrorProps.forEach(prop => {
baseError[prop] = v[prop];
});
return baseError;
}, function (v, superJson) {
var e = new Error(v.message);
}, (v, superJson) => {
const e = new Error(v.message);
e.name = v.name;
e.stack = v.stack;
superJson.allowedErrorProps.forEach(function (prop) {
superJson.allowedErrorProps.forEach(prop => {
e[prop] = v[prop];

@@ -73,5 +41,5 @@ });

}),
simpleTransformation(isRegExp, 'regexp', function (v) { return '' + v; }, function (regex) {
var body = regex.slice(1, regex.lastIndexOf('/'));
var flags = regex.slice(regex.lastIndexOf('/') + 1);
simpleTransformation(isRegExp, 'regexp', v => '' + v, regex => {
const body = regex.slice(1, regex.lastIndexOf('/'));
const flags = regex.slice(regex.lastIndexOf('/') + 1);
return new RegExp(body, flags);

@@ -82,5 +50,5 @@ }),

// eslint-disable-next-line es5/no-es6-methods
function (v) { return __spreadArray([], __read(v.values())); }, function (v) { return new Set(v); }),
simpleTransformation(isMap, 'map', function (v) { return __spreadArray([], __read(v.entries())); }, function (v) { return new Map(v); }),
simpleTransformation(function (v) { return isNaNValue(v) || isInfinite(v); }, 'number', function (v) {
v => [...v.values()], v => new Set(v)),
simpleTransformation(isMap, 'map', v => [...v.entries()], v => new Map(v)),
simpleTransformation((v) => isNaNValue(v) || isInfinite(v), 'number', v => {
if (isNaNValue(v)) {

@@ -96,26 +64,26 @@ return 'NaN';

}, Number),
simpleTransformation(function (v) { return v === 0 && 1 / v === -Infinity; }, 'number', function () {
simpleTransformation((v) => v === 0 && 1 / v === -Infinity, 'number', () => {
return '-0';
}, Number),
simpleTransformation(isURL, 'URL', function (v) { return v.toString(); }, function (v) { return new URL(v); }),
simpleTransformation(isURL, 'URL', v => v.toString(), v => new URL(v)),
];
function compositeTransformation(isApplicable, annotation, transform, untransform) {
return {
isApplicable: isApplicable,
annotation: annotation,
transform: transform,
untransform: untransform
isApplicable,
annotation,
transform,
untransform,
};
}
var symbolRule = compositeTransformation(function (s, superJson) {
const symbolRule = compositeTransformation((s, superJson) => {
if (isSymbol(s)) {
var isRegistered = !!superJson.symbolRegistry.getIdentifier(s);
const isRegistered = !!superJson.symbolRegistry.getIdentifier(s);
return isRegistered;
}
return false;
}, function (s, superJson) {
var identifier = superJson.symbolRegistry.getIdentifier(s);
}, (s, superJson) => {
const identifier = superJson.symbolRegistry.getIdentifier(s);
return ['symbol', identifier];
}, function (v) { return v.description; }, function (_, a, superJson) {
var value = superJson.symbolRegistry.getValue(a[1]);
}, v => v.description, (_, a, superJson) => {
const value = superJson.symbolRegistry.getValue(a[1]);
if (!value) {

@@ -126,3 +94,3 @@ throw new Error('Trying to deserialize unknown symbol');

});
var constructorToName = [
const constructorToName = [
Int8Array,

@@ -137,8 +105,8 @@ Uint8Array,

Uint8ClampedArray,
].reduce(function (obj, ctor) {
].reduce((obj, ctor) => {
obj[ctor.name] = ctor;
return obj;
}, {});
var typedArrayRule = compositeTransformation(isTypedArray, function (v) { return ['typed-array', v.constructor.name]; }, function (v) { return __spreadArray([], __read(v)); }, function (v, a) {
var ctor = constructorToName[a[1]];
const typedArrayRule = compositeTransformation(isTypedArray, v => ['typed-array', v.constructor.name], v => [...v], (v, a) => {
const ctor = constructorToName[a[1]];
if (!ctor) {

@@ -150,4 +118,4 @@ throw new Error('Trying to deserialize unknown typed array');

export function isInstanceOfRegisteredClass(potentialClass, superJson) {
if (potentialClass === null || potentialClass === void 0 ? void 0 : potentialClass.constructor) {
var isRegistered = !!superJson.classRegistry.getIdentifier(potentialClass.constructor);
if (potentialClass?.constructor) {
const isRegistered = !!superJson.classRegistry.getIdentifier(potentialClass.constructor);
return isRegistered;

@@ -157,17 +125,17 @@ }

}
var classRule = compositeTransformation(isInstanceOfRegisteredClass, function (clazz, superJson) {
var identifier = superJson.classRegistry.getIdentifier(clazz.constructor);
const classRule = compositeTransformation(isInstanceOfRegisteredClass, (clazz, superJson) => {
const identifier = superJson.classRegistry.getIdentifier(clazz.constructor);
return ['class', identifier];
}, function (clazz, superJson) {
var allowedProps = superJson.classRegistry.getAllowedProps(clazz.constructor);
}, (clazz, superJson) => {
const allowedProps = superJson.classRegistry.getAllowedProps(clazz.constructor);
if (!allowedProps) {
return __assign({}, clazz);
return { ...clazz };
}
var result = {};
allowedProps.forEach(function (prop) {
const result = {};
allowedProps.forEach(prop => {
result[prop] = clazz[prop];
});
return result;
}, function (v, a, superJson) {
var clazz = superJson.classRegistry.getValue(a[1]);
}, (v, a, superJson) => {
const clazz = superJson.classRegistry.getValue(a[1]);
if (!clazz) {

@@ -178,12 +146,12 @@ throw new Error('Trying to deserialize unknown class - check https://github.com/blitz-js/superjson/issues/116#issuecomment-773996564');

});
var customRule = compositeTransformation(function (value, superJson) {
const customRule = compositeTransformation((value, superJson) => {
return !!superJson.customTransformerRegistry.findApplicable(value);
}, function (value, superJson) {
var transformer = superJson.customTransformerRegistry.findApplicable(value);
}, (value, superJson) => {
const transformer = superJson.customTransformerRegistry.findApplicable(value);
return ['custom', transformer.name];
}, function (value, superJson) {
var transformer = superJson.customTransformerRegistry.findApplicable(value);
}, (value, superJson) => {
const transformer = superJson.customTransformerRegistry.findApplicable(value);
return transformer.serialize(value);
}, function (v, a, superJson) {
var transformer = superJson.customTransformerRegistry.findByName(a[1]);
}, (v, a, superJson) => {
const transformer = superJson.customTransformerRegistry.findByName(a[1]);
if (!transformer) {

@@ -194,20 +162,16 @@ throw new Error('Trying to deserialize unknown custom value');

});
var compositeRules = [classRule, symbolRule, customRule, typedArrayRule];
export var transformValue = function (value, superJson) {
var applicableCompositeRule = findArr(compositeRules, function (rule) {
return rule.isApplicable(value, superJson);
});
const compositeRules = [classRule, symbolRule, customRule, typedArrayRule];
export const transformValue = (value, superJson) => {
const applicableCompositeRule = findArr(compositeRules, rule => rule.isApplicable(value, superJson));
if (applicableCompositeRule) {
return {
value: applicableCompositeRule.transform(value, superJson),
type: applicableCompositeRule.annotation(value, superJson)
type: applicableCompositeRule.annotation(value, superJson),
};
}
var applicableSimpleRule = findArr(simpleRules, function (rule) {
return rule.isApplicable(value, superJson);
});
const applicableSimpleRule = findArr(simpleRules, rule => rule.isApplicable(value, superJson));
if (applicableSimpleRule) {
return {
value: applicableSimpleRule.transform(value, superJson),
type: applicableSimpleRule.annotation
type: applicableSimpleRule.annotation,
};

@@ -217,7 +181,7 @@ }

};
var simpleRulesByAnnotation = {};
simpleRules.forEach(function (rule) {
const simpleRulesByAnnotation = {};
simpleRules.forEach(rule => {
simpleRulesByAnnotation[rule.annotation] = rule;
});
export var untransformValue = function (json, type, superJson) {
export const untransformValue = (json, type, superJson) => {
if (isArray(type)) {

@@ -238,3 +202,3 @@ switch (type[0]) {

else {
var transformation = simpleRulesByAnnotation[type];
const transformation = simpleRulesByAnnotation[type];
if (!transformation) {

@@ -241,0 +205,0 @@ throw new Error('Unknown transformation: ' + type);

@@ -1,17 +0,1 @@

var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
function valuesOfObj(record) {

@@ -22,5 +6,5 @@ if ('values' in Object) {

}
var values = [];
const values = [];
// eslint-disable-next-line no-restricted-syntax
for (var key in record) {
for (const key in record) {
if (record.hasOwnProperty(key)) {

@@ -33,3 +17,3 @@ values.push(record[key]);

export function find(record, predicate) {
var values = valuesOfObj(record);
const values = valuesOfObj(record);
if ('find' in values) {

@@ -39,5 +23,5 @@ // eslint-disable-next-line es5/no-es6-methods

}
var valuesNotNever = values;
for (var i = 0; i < valuesNotNever.length; i++) {
var value = valuesNotNever[i];
const valuesNotNever = values;
for (let i = 0; i < valuesNotNever.length; i++) {
const value = valuesNotNever[i];
if (predicate(value)) {

@@ -50,6 +34,3 @@ return value;

export function forEach(record, run) {
Object.entries(record).forEach(function (_a) {
var _b = __read(_a, 2), key = _b[0], value = _b[1];
return run(value, key);
});
Object.entries(record).forEach(([key, value]) => run(value, key));
}

@@ -60,4 +41,4 @@ export function includes(arr, value) {

export function findArr(record, predicate) {
for (var i = 0; i < record.length; i++) {
var value = record[i];
for (let i = 0; i < record.length; i++) {
const value = record[i];
if (predicate(value)) {

@@ -64,0 +45,0 @@ return value;

{
"version": "2.2.0",
"version": "2.2.1",
"license": "MIT",

@@ -4,0 +4,0 @@ "type": "module",

@@ -11,3 +11,3 @@ <p align="center">

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<a href="#contributors"><img src="https://img.shields.io/badge/all_contributors-29-orange.svg?style=flat-square" alt="All Contributors"/></a>
<a href="#contributors"><img src="https://img.shields.io/badge/all_contributors-31-orange.svg?style=flat-square" alt="All Contributors"/></a>
<!-- ALL-CONTRIBUTORS-BADGE:END -->

@@ -329,4 +329,5 @@ <a href="https://www.npmjs.com/package/superjson">

<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kidqueb"><img src="https://avatars.githubusercontent.com/u/884128?v=4?s=100" width="100px;" alt="Nick Quebbeman"/><br /><sub><b>Nick Quebbeman</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/issues?q=author%3Akidqueb" title="Bug reports">πŸ›</a> <a href="https://github.com/blitz-js/superjson/commits?author=kidqueb" title="Code">πŸ’»</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kidqueb"><img src="https://avatars.githubusercontent.com/u/884128?v=4?s=100" width="100px;" alt="Nick Quebbeman"/><br /><sub><b>Nick Quebbeman</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/commits?author=kidqueb" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://macwright.com/"><img src="https://avatars.githubusercontent.com/u/32314?v=4?s=100" width="100px;" alt="Tom MacWright"/><br /><sub><b>Tom MacWright</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/issues?q=author%3Atmcw" title="Bug reports">πŸ›</a> <a href="https://github.com/blitz-js/superjson/commits?author=tmcw" title="Code">πŸ’»</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/peterbud"><img src="https://avatars.githubusercontent.com/u/7863452?v=4?s=100" width="100px;" alt="Peter Budai"/><br /><sub><b>Peter Budai</b></sub></a><br /><a href="https://github.com/blitz-js/superjson/issues?q=author%3Apeterbud" title="Bug reports">πŸ›</a></td>
</tr>

@@ -333,0 +334,0 @@ </tbody>

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc