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 1.10.1 to 1.11.0

4

dist/class-registry.d.ts

@@ -7,3 +7,3 @@ import { Registry } from './registry';

}
declare class _ClassRegistry extends Registry<Class> {
export declare class ClassRegistry extends Registry<Class> {
constructor();

@@ -14,3 +14,1 @@ private classToAllowedProps;

}
export declare const ClassRegistry: _ClassRegistry;
export {};

@@ -20,5 +20,5 @@ "use strict";

var registry_1 = require("./registry");
var _ClassRegistry = /** @class */ (function (_super) {
__extends(_ClassRegistry, _super);
function _ClassRegistry() {
var ClassRegistry = /** @class */ (function (_super) {
__extends(ClassRegistry, _super);
function ClassRegistry() {
var _this = _super.call(this, function (c) { return c.name; }) || this;

@@ -28,3 +28,3 @@ _this.classToAllowedProps = new Map();

}
_ClassRegistry.prototype.register = function (value, options) {
ClassRegistry.prototype.register = function (value, options) {
if (typeof options === 'object') {

@@ -40,8 +40,8 @@ if (options.allowProps) {

};
_ClassRegistry.prototype.getAllowedProps = function (value) {
ClassRegistry.prototype.getAllowedProps = function (value) {
return this.classToAllowedProps.get(value);
};
return _ClassRegistry;
return ClassRegistry;
}(registry_1.Registry));
exports.ClassRegistry = new _ClassRegistry();
exports.ClassRegistry = ClassRegistry;
//# sourceMappingURL=class-registry.js.map

@@ -8,6 +8,7 @@ import { JSONValue } from './types';

}
export declare const CustomTransformerRegistry: {
export declare class CustomTransformerRegistry {
private transfomers;
register<I, O extends JSONValue>(transformer: CustomTransfomer<I, O>): void;
findApplicable<T>(v: T): CustomTransfomer<T, JSONValue> | undefined;
findByName(name: string): CustomTransfomer<any, any>;
};
}

@@ -5,14 +5,20 @@ "use strict";

var util_1 = require("./util");
var transfomers = {};
exports.CustomTransformerRegistry = {
register: function (transformer) {
transfomers[transformer.name] = transformer;
},
findApplicable: function (v) {
return util_1.find(transfomers, function (transformer) { return transformer.isApplicable(v); });
},
findByName: function (name) {
return transfomers[name];
var CustomTransformerRegistry = /** @class */ (function () {
function CustomTransformerRegistry() {
this.transfomers = {};
}
};
CustomTransformerRegistry.prototype.register = function (transformer) {
this.transfomers[transformer.name] = transformer;
};
CustomTransformerRegistry.prototype.findApplicable = function (v) {
return util_1.find(this.transfomers, function (transformer) {
return transformer.isApplicable(v);
});
};
CustomTransformerRegistry.prototype.findByName = function (name) {
return this.transfomers[name];
};
return CustomTransformerRegistry;
}());
exports.CustomTransformerRegistry = CustomTransformerRegistry;
//# sourceMappingURL=custom-transformer-registry.js.map

@@ -7,3 +7,3 @@ import { Registry } from './registry';

}
declare class _ClassRegistry extends Registry<Class> {
export declare class ClassRegistry extends Registry<Class> {
constructor();

@@ -14,3 +14,1 @@ private classToAllowedProps;

}
export declare const ClassRegistry: _ClassRegistry;
export {};

@@ -17,5 +17,5 @@ var __extends = (this && this.__extends) || (function () {

import { Registry } from './registry';
var _ClassRegistry = /** @class */ (function (_super) {
__extends(_ClassRegistry, _super);
function _ClassRegistry() {
var ClassRegistry = /** @class */ (function (_super) {
__extends(ClassRegistry, _super);
function ClassRegistry() {
var _this = _super.call(this, function (c) { return c.name; }) || this;

@@ -25,3 +25,3 @@ _this.classToAllowedProps = new Map();

}
_ClassRegistry.prototype.register = function (value, options) {
ClassRegistry.prototype.register = function (value, options) {
if (typeof options === 'object') {

@@ -37,8 +37,8 @@ if (options.allowProps) {

};
_ClassRegistry.prototype.getAllowedProps = function (value) {
ClassRegistry.prototype.getAllowedProps = function (value) {
return this.classToAllowedProps.get(value);
};
return _ClassRegistry;
return ClassRegistry;
}(Registry));
export var ClassRegistry = new _ClassRegistry();
export { ClassRegistry };
//# sourceMappingURL=class-registry.js.map

@@ -8,6 +8,7 @@ import { JSONValue } from './types';

}
export declare const CustomTransformerRegistry: {
export declare class CustomTransformerRegistry {
private transfomers;
register<I, O extends JSONValue>(transformer: CustomTransfomer<I, O>): void;
findApplicable<T>(v: T): CustomTransfomer<T, JSONValue> | undefined;
findByName(name: string): CustomTransfomer<any, any>;
};
}
import { find } from './util';
var transfomers = {};
export var CustomTransformerRegistry = {
register: function (transformer) {
transfomers[transformer.name] = transformer;
},
findApplicable: function (v) {
return find(transfomers, function (transformer) { return transformer.isApplicable(v); });
},
findByName: function (name) {
return transfomers[name];
var CustomTransformerRegistry = /** @class */ (function () {
function CustomTransformerRegistry() {
this.transfomers = {};
}
};
CustomTransformerRegistry.prototype.register = function (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) {
return this.transfomers[name];
};
return CustomTransformerRegistry;
}());
export { CustomTransformerRegistry };
//# sourceMappingURL=custom-transformer-registry.js.map
import { SuperJSONResult, SuperJSONValue, Class, JSONValue } from './types';
import { RegisterOptions } from './class-registry';
import { CustomTransfomer } from './custom-transformer-registry';
import { ClassRegistry, RegisterOptions } from './class-registry';
import { Registry } from './registry';
import { CustomTransfomer, CustomTransformerRegistry } from './custom-transformer-registry';
export default class SuperJSON {
serialize(object: SuperJSONValue): SuperJSONResult;
deserialize<T = unknown>(payload: SuperJSONResult): T;
stringify(object: SuperJSONValue): string;
parse<T = unknown>(string: string): T;
readonly classRegistry: ClassRegistry;
registerClass(v: Class, options?: RegisterOptions | string): void;
readonly symbolRegistry: Registry<Symbol>;
registerSymbol(v: Symbol, identifier?: string): void;
readonly customTransformerRegistry: CustomTransformerRegistry;
registerCustom<I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, 'name'>, name: string): void;
readonly allowedErrorProps: string[];
allowErrorProps(...props: string[]): void;
private static defaultInstance;
static serialize: (object: SuperJSONValue) => SuperJSONResult;
static deserialize: <T = unknown>(payload: SuperJSONResult) => T;
static stringify: (object: SuperJSONValue) => string;
static parse: <T = unknown>(string: string) => T;
static registerClass: (v: Class, options?: string | RegisterOptions | undefined) => void;
static registerSymbol: (v: Symbol, identifier?: string | undefined) => void;
static registerCustom: <I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, "name">, name: string) => void;
static allowErrorProps: (...props: string[]) => void;
}
export declare const serialize: (object: SuperJSONValue) => SuperJSONResult;

@@ -8,15 +32,1 @@ export declare const deserialize: <T = unknown>(payload: SuperJSONResult) => T;

export declare const parse: <T = unknown>(string: string) => T;
export declare const registerClass: (v: Class, options?: string | RegisterOptions | undefined) => void;
export declare const registerSymbol: (v: Symbol, identifier?: string | undefined) => void;
export declare const registerCustom: <I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, "name">, name: string) => void;
declare const _default: {
stringify: (object: any) => string;
parse: <T = unknown>(string: string) => T;
serialize: (object: any) => SuperJSONResult;
deserialize: <T_1 = unknown>(payload: SuperJSONResult) => T_1;
registerClass: (v: Class, options?: string | RegisterOptions | undefined) => void;
registerSymbol: (v: Symbol, identifier?: string | undefined) => void;
registerCustom: <I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, "name">, name: string) => void;
allowErrorProps: (...props: string[]) => void;
};
export default _default;

@@ -12,59 +12,100 @@ var __assign = (this && this.__assign) || function () {

};
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';
import { SymbolRegistry } from './symbol-registry';
import { Registry } from './registry';
import { CustomTransformerRegistry, } from './custom-transformer-registry';
import { allowErrorProps } from './error-props';
import { walker, applyReferentialEqualityAnnotations, applyValueAnnotations, generateReferentialEqualityAnnotations, } from './plainer';
import { copy } from 'copy-anything';
export var serialize = function (object) {
var identities = new Map();
var output = walker(object, identities);
var res = {
json: output.transformedValue
var SuperJSON = /** @class */ (function () {
function SuperJSON() {
this.classRegistry = new ClassRegistry();
this.symbolRegistry = new Registry(function (s) { var _a; return (_a = s.description) !== null && _a !== void 0 ? _a : ''; });
this.customTransformerRegistry = new CustomTransformerRegistry();
this.allowedErrorProps = [];
}
SuperJSON.prototype.serialize = function (object) {
var identities = new Map();
var output = walker(object, identities, this);
var res = {
json: output.transformedValue
};
if (output.annotations) {
res.meta = __assign(__assign({}, res.meta), { values: output.annotations });
}
var equalityAnnotations = generateReferentialEqualityAnnotations(identities);
if (equalityAnnotations) {
res.meta = __assign(__assign({}, res.meta), { referentialEqualities: equalityAnnotations });
}
return res;
};
if (output.annotations) {
res.meta = __assign(__assign({}, res.meta), { values: output.annotations });
}
var equalityAnnotations = generateReferentialEqualityAnnotations(identities);
if (equalityAnnotations) {
res.meta = __assign(__assign({}, res.meta), { referentialEqualities: equalityAnnotations });
}
return res;
};
export var deserialize = function (payload) {
var json = payload.json, meta = payload.meta;
var result = copy(json);
if (meta === null || meta === void 0 ? void 0 : meta.values) {
result = applyValueAnnotations(result, meta.values);
}
if (meta === null || meta === void 0 ? void 0 : meta.referentialEqualities) {
result = applyReferentialEqualityAnnotations(result, meta.referentialEqualities);
}
return result;
};
export var stringify = function (object) {
return JSON.stringify(serialize(object));
};
export var parse = function (string) {
return deserialize(JSON.parse(string));
};
export var registerClass = function (v, options) {
return ClassRegistry.register(v, options);
};
export var registerSymbol = function (v, identifier) {
return SymbolRegistry.register(v, identifier);
};
export var registerCustom = function (transformer, name) {
return CustomTransformerRegistry.register(__assign({ name: name }, transformer));
};
export default {
stringify: stringify,
parse: parse,
serialize: serialize,
deserialize: deserialize,
registerClass: registerClass,
registerSymbol: registerSymbol,
registerCustom: registerCustom,
allowErrorProps: allowErrorProps
};
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) {
result = applyValueAnnotations(result, meta.values, this);
}
if (meta === null || meta === void 0 ? void 0 : meta.referentialEqualities) {
result = applyReferentialEqualityAnnotations(result, meta.referentialEqualities);
}
return result;
};
SuperJSON.prototype.stringify = function (object) {
return JSON.stringify(this.serialize(object));
};
SuperJSON.prototype.parse = function (string) {
return this.deserialize(JSON.parse(string));
};
SuperJSON.prototype.registerClass = function (v, options) {
this.classRegistry.register(v, options);
};
SuperJSON.prototype.registerSymbol = function (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;
export var serialize = SuperJSON.serialize;
export var deserialize = SuperJSON.deserialize;
export var stringify = SuperJSON.stringify;
export var parse = SuperJSON.parse;
//# sourceMappingURL=index.js.map
import { TypeAnnotation } from './transformer';
import SuperJSON from '.';
declare type Tree<T> = InnerNode<T> | Leaf<T>;

@@ -6,3 +7,3 @@ declare type Leaf<T> = [T];

export declare type MinimisedTree<T> = Tree<T> | Record<string, Tree<T>> | undefined;
export declare function applyValueAnnotations(plain: any, annotations: MinimisedTree<TypeAnnotation>): any;
export declare function applyValueAnnotations(plain: any, annotations: MinimisedTree<TypeAnnotation>, superJson: SuperJSON): any;
export declare function applyReferentialEqualityAnnotations(plain: any, annotations: ReferentialEqualityAnnotations): any;

@@ -15,3 +16,3 @@ interface Result {

export declare function generateReferentialEqualityAnnotations(identitites: Map<any, any[][]>): ReferentialEqualityAnnotations | undefined;
export declare const walker: (object: any, identities: Map<any, any[][]>, path?: any[], objectsInThisPath?: any[]) => Result;
export declare const walker: (object: any, identities: Map<any, any[][]>, superJson: SuperJSON, path?: any[], objectsInThisPath?: any[]) => Result;
export {};

@@ -47,5 +47,5 @@ var __read = (this && this.__read) || function (o, n) {

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

@@ -75,3 +75,3 @@ return plain;

}
var isDeep = function (object) {
var isDeep = function (object, superJson) {
return isPlainObject(object) ||

@@ -81,3 +81,3 @@ isArray(object) ||

isSet(object) ||
isInstanceOfRegisteredClass(object);
isInstanceOfRegisteredClass(object, superJson);
};

@@ -122,3 +122,3 @@ function addIdentity(object, path, identities) {

}
export var walker = function (object, identities, path, objectsInThisPath) {
export var walker = function (object, identities, superJson, path, objectsInThisPath) {
var _a;

@@ -130,4 +130,4 @@ if (path === void 0) { path = []; }

}
if (!isDeep(object)) {
var transformed_1 = transformValue(object);
if (!isDeep(object, superJson)) {
var transformed_1 = transformValue(object, superJson);
if (transformed_1) {

@@ -150,3 +150,3 @@ return {

}
var transformationResult = transformValue(object);
var transformationResult = transformValue(object, superJson);
var transformed = (_a = transformationResult === null || transformationResult === void 0 ? void 0 : transformationResult.value) !== null && _a !== void 0 ? _a : object;

@@ -159,3 +159,3 @@ if (!isPrimitive(object)) {

forEach(transformed, function (value, index) {
var recursiveResult = walker(value, identities, __spreadArray(__spreadArray([], __read(path)), [index]), objectsInThisPath);
var recursiveResult = walker(value, identities, superJson, __spreadArray(__spreadArray([], __read(path)), [index]), objectsInThisPath);
transformedValue[index] = recursiveResult.transformedValue;

@@ -162,0 +162,0 @@ if (isArray(recursiveResult.annotations)) {

@@ -0,1 +1,2 @@

import SuperJSON from '.';
export declare type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint';

@@ -10,8 +11,8 @@ declare type LeafTypeAnnotation = PrimitiveTypeAnnotation | 'regexp' | 'Date' | 'Error' | 'URL';

export declare type TypeAnnotation = SimpleTypeAnnotation | CompositeTypeAnnotation;
export declare function isInstanceOfRegisteredClass(potentialClass: any): potentialClass is any;
export declare const transformValue: (value: any) => {
export declare function isInstanceOfRegisteredClass(potentialClass: any, superJson: SuperJSON): potentialClass is any;
export declare const transformValue: (value: any, superJson: SuperJSON) => {
value: any;
type: TypeAnnotation;
} | undefined;
export declare const untransformValue: (json: any, type: TypeAnnotation) => any;
export declare const untransformValue: (json: any, type: TypeAnnotation, superJson: SuperJSON) => any;
export {};

@@ -34,6 +34,2 @@ var __assign = (this && this.__assign) || function () {

import { isBigint, isDate, isInfinite, isMap, isNaNValue, isRegExp, isSet, isUndefined, isSymbol, isArray, isError, isTypedArray, isURL, } from './is';
import { ClassRegistry } from './class-registry';
import { SymbolRegistry } from './symbol-registry';
import { CustomTransformerRegistry } from './custom-transformer-registry';
import { allowedErrorProps } from './error-props';
import { findArr } from './util';

@@ -58,3 +54,3 @@ function simpleTransformation(isApplicable, annotation, transform, untransform) {

simpleTransformation(isDate, 'Date', function (v) { return v.toISOString(); }, function (v) { return new Date(v); }),
simpleTransformation(isError, 'Error', function (v) {
simpleTransformation(isError, 'Error', function (v, superJson) {
var baseError = {

@@ -64,11 +60,11 @@ name: v.name,

};
allowedErrorProps.forEach(function (prop) {
superJson.allowedErrorProps.forEach(function (prop) {
baseError[prop] = v[prop];
});
return baseError;
}, function (v) {
}, function (v, superJson) {
var e = new Error(v.message);
e.name = v.name;
e.stack = v.stack;
allowedErrorProps.forEach(function (prop) {
superJson.allowedErrorProps.forEach(function (prop) {
e[prop] = v[prop];

@@ -112,13 +108,13 @@ });

}
var symbolRule = compositeTransformation(function (s) {
var symbolRule = compositeTransformation(function (s, superJson) {
if (isSymbol(s)) {
var isRegistered = !!SymbolRegistry.getIdentifier(s);
var isRegistered = !!superJson.symbolRegistry.getIdentifier(s);
return isRegistered;
}
return false;
}, function (s) {
var identifier = SymbolRegistry.getIdentifier(s);
}, function (s, superJson) {
var identifier = superJson.symbolRegistry.getIdentifier(s);
return ['symbol', identifier];
}, function (v) { return v.description; }, function (_, a) {
var value = SymbolRegistry.getValue(a[1]);
}, function (v) { return v.description; }, function (_, a, superJson) {
var value = superJson.symbolRegistry.getValue(a[1]);
if (!value) {

@@ -150,5 +146,5 @@ throw new Error('Trying to deserialize unknown symbol');

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

@@ -158,7 +154,7 @@ }

}
var classRule = compositeTransformation(isInstanceOfRegisteredClass, function (clazz) {
var identifier = ClassRegistry.getIdentifier(clazz.constructor);
var classRule = compositeTransformation(isInstanceOfRegisteredClass, function (clazz, superJson) {
var identifier = superJson.classRegistry.getIdentifier(clazz.constructor);
return ['class', identifier];
}, function (clazz) {
var allowedProps = ClassRegistry.getAllowedProps(clazz.constructor);
}, function (clazz, superJson) {
var allowedProps = superJson.classRegistry.getAllowedProps(clazz.constructor);
if (!allowedProps) {

@@ -172,4 +168,4 @@ return __assign({}, clazz);

return result;
}, function (v, a) {
var clazz = ClassRegistry.getValue(a[1]);
}, function (v, a, superJson) {
var clazz = superJson.classRegistry.getValue(a[1]);
if (!clazz) {

@@ -180,12 +176,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) {
return !!CustomTransformerRegistry.findApplicable(value);
}, function (value) {
var transformer = CustomTransformerRegistry.findApplicable(value);
var customRule = compositeTransformation(function (value, superJson) {
return !!superJson.customTransformerRegistry.findApplicable(value);
}, function (value, superJson) {
var transformer = superJson.customTransformerRegistry.findApplicable(value);
return ['custom', transformer.name];
}, function (value) {
var transformer = CustomTransformerRegistry.findApplicable(value);
}, function (value, superJson) {
var transformer = superJson.customTransformerRegistry.findApplicable(value);
return transformer.serialize(value);
}, function (v, a) {
var transformer = CustomTransformerRegistry.findByName(a[1]);
}, function (v, a, superJson) {
var transformer = superJson.customTransformerRegistry.findByName(a[1]);
if (!transformer) {

@@ -197,18 +193,18 @@ throw new Error('Trying to deserialize unknown custom value');

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

@@ -223,13 +219,13 @@ };

});
export var untransformValue = function (json, type) {
export var untransformValue = function (json, type, superJson) {
if (isArray(type)) {
switch (type[0]) {
case 'symbol':
return symbolRule.untransform(json, type);
return symbolRule.untransform(json, type, superJson);
case 'class':
return classRule.untransform(json, type);
return classRule.untransform(json, type, superJson);
case 'custom':
return customRule.untransform(json, type);
return customRule.untransform(json, type, superJson);
case 'typed-array':
return typedArrayRule.untransform(json, type);
return typedArrayRule.untransform(json, type, superJson);
default:

@@ -244,5 +240,5 @@ throw new Error('Unknown transformation: ' + type);

}
return transformation.untransform(json);
return transformation.untransform(json, superJson);
}
};
//# sourceMappingURL=transformer.js.map
import { SuperJSONResult, SuperJSONValue, Class, JSONValue } from './types';
import { RegisterOptions } from './class-registry';
import { CustomTransfomer } from './custom-transformer-registry';
import { ClassRegistry, RegisterOptions } from './class-registry';
import { Registry } from './registry';
import { CustomTransfomer, CustomTransformerRegistry } from './custom-transformer-registry';
export default class SuperJSON {
serialize(object: SuperJSONValue): SuperJSONResult;
deserialize<T = unknown>(payload: SuperJSONResult): T;
stringify(object: SuperJSONValue): string;
parse<T = unknown>(string: string): T;
readonly classRegistry: ClassRegistry;
registerClass(v: Class, options?: RegisterOptions | string): void;
readonly symbolRegistry: Registry<Symbol>;
registerSymbol(v: Symbol, identifier?: string): void;
readonly customTransformerRegistry: CustomTransformerRegistry;
registerCustom<I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, 'name'>, name: string): void;
readonly allowedErrorProps: string[];
allowErrorProps(...props: string[]): void;
private static defaultInstance;
static serialize: (object: SuperJSONValue) => SuperJSONResult;
static deserialize: <T = unknown>(payload: SuperJSONResult) => T;
static stringify: (object: SuperJSONValue) => string;
static parse: <T = unknown>(string: string) => T;
static registerClass: (v: Class, options?: string | RegisterOptions | undefined) => void;
static registerSymbol: (v: Symbol, identifier?: string | undefined) => void;
static registerCustom: <I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, "name">, name: string) => void;
static allowErrorProps: (...props: string[]) => void;
}
export declare const serialize: (object: SuperJSONValue) => SuperJSONResult;

@@ -8,15 +32,1 @@ export declare const deserialize: <T = unknown>(payload: SuperJSONResult) => T;

export declare const parse: <T = unknown>(string: string) => T;
export declare const registerClass: (v: Class, options?: string | RegisterOptions | undefined) => void;
export declare const registerSymbol: (v: Symbol, identifier?: string | undefined) => void;
export declare const registerCustom: <I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, "name">, name: string) => void;
declare const _default: {
stringify: (object: any) => string;
parse: <T = unknown>(string: string) => T;
serialize: (object: any) => SuperJSONResult;
deserialize: <T_1 = unknown>(payload: SuperJSONResult) => T_1;
registerClass: (v: Class, options?: string | RegisterOptions | undefined) => void;
registerSymbol: (v: Symbol, identifier?: string | undefined) => void;
registerCustom: <I, O extends JSONValue>(transformer: Omit<CustomTransfomer<I, O>, "name">, name: string) => void;
allowErrorProps: (...props: string[]) => void;
};
export default _default;

@@ -13,68 +13,102 @@ "use strict";

};
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;
};
exports.__esModule = true;
exports.registerCustom = exports.registerSymbol = exports.registerClass = exports.parse = exports.stringify = exports.deserialize = exports.serialize = void 0;
exports.parse = exports.stringify = exports.deserialize = exports.serialize = void 0;
var class_registry_1 = require("./class-registry");
var symbol_registry_1 = require("./symbol-registry");
var registry_1 = require("./registry");
var custom_transformer_registry_1 = require("./custom-transformer-registry");
var error_props_1 = require("./error-props");
var plainer_1 = require("./plainer");
var copy_anything_1 = require("copy-anything");
var serialize = function (object) {
var identities = new Map();
var output = plainer_1.walker(object, identities);
var res = {
json: output.transformedValue
var SuperJSON = /** @class */ (function () {
function SuperJSON() {
this.classRegistry = new class_registry_1.ClassRegistry();
this.symbolRegistry = new registry_1.Registry(function (s) { var _a; return (_a = s.description) !== null && _a !== void 0 ? _a : ''; });
this.customTransformerRegistry = new custom_transformer_registry_1.CustomTransformerRegistry();
this.allowedErrorProps = [];
}
SuperJSON.prototype.serialize = function (object) {
var identities = new Map();
var output = plainer_1.walker(object, identities, this);
var res = {
json: output.transformedValue
};
if (output.annotations) {
res.meta = __assign(__assign({}, res.meta), { values: output.annotations });
}
var equalityAnnotations = plainer_1.generateReferentialEqualityAnnotations(identities);
if (equalityAnnotations) {
res.meta = __assign(__assign({}, res.meta), { referentialEqualities: equalityAnnotations });
}
return res;
};
if (output.annotations) {
res.meta = __assign(__assign({}, res.meta), { values: output.annotations });
}
var equalityAnnotations = plainer_1.generateReferentialEqualityAnnotations(identities);
if (equalityAnnotations) {
res.meta = __assign(__assign({}, res.meta), { referentialEqualities: equalityAnnotations });
}
return res;
};
exports.serialize = serialize;
var deserialize = function (payload) {
var json = payload.json, meta = payload.meta;
var result = copy_anything_1.copy(json);
if (meta === null || meta === void 0 ? void 0 : meta.values) {
result = plainer_1.applyValueAnnotations(result, meta.values);
}
if (meta === null || meta === void 0 ? void 0 : meta.referentialEqualities) {
result = plainer_1.applyReferentialEqualityAnnotations(result, meta.referentialEqualities);
}
return result;
};
exports.deserialize = deserialize;
var stringify = function (object) {
return JSON.stringify(exports.serialize(object));
};
exports.stringify = stringify;
var parse = function (string) {
return exports.deserialize(JSON.parse(string));
};
exports.parse = parse;
var registerClass = function (v, options) {
return class_registry_1.ClassRegistry.register(v, options);
};
exports.registerClass = registerClass;
var registerSymbol = function (v, identifier) {
return symbol_registry_1.SymbolRegistry.register(v, identifier);
};
exports.registerSymbol = registerSymbol;
var registerCustom = function (transformer, name) {
return custom_transformer_registry_1.CustomTransformerRegistry.register(__assign({ name: name }, transformer));
};
exports.registerCustom = registerCustom;
exports["default"] = {
stringify: exports.stringify,
parse: exports.parse,
serialize: exports.serialize,
deserialize: exports.deserialize,
registerClass: exports.registerClass,
registerSymbol: exports.registerSymbol,
registerCustom: exports.registerCustom,
allowErrorProps: error_props_1.allowErrorProps
};
SuperJSON.prototype.deserialize = function (payload) {
var json = payload.json, meta = payload.meta;
var result = copy_anything_1.copy(json);
if (meta === null || meta === void 0 ? void 0 : meta.values) {
result = plainer_1.applyValueAnnotations(result, meta.values, this);
}
if (meta === null || meta === void 0 ? void 0 : meta.referentialEqualities) {
result = plainer_1.applyReferentialEqualityAnnotations(result, meta.referentialEqualities);
}
return result;
};
SuperJSON.prototype.stringify = function (object) {
return JSON.stringify(this.serialize(object));
};
SuperJSON.prototype.parse = function (string) {
return this.deserialize(JSON.parse(string));
};
SuperJSON.prototype.registerClass = function (v, options) {
this.classRegistry.register(v, options);
};
SuperJSON.prototype.registerSymbol = function (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;
}());
exports["default"] = SuperJSON;
exports.serialize = SuperJSON.serialize;
exports.deserialize = SuperJSON.deserialize;
exports.stringify = SuperJSON.stringify;
exports.parse = SuperJSON.parse;
//# sourceMappingURL=index.js.map
import { TypeAnnotation } from './transformer';
import SuperJSON from '.';
declare type Tree<T> = InnerNode<T> | Leaf<T>;

@@ -6,3 +7,3 @@ declare type Leaf<T> = [T];

export declare type MinimisedTree<T> = Tree<T> | Record<string, Tree<T>> | undefined;
export declare function applyValueAnnotations(plain: any, annotations: MinimisedTree<TypeAnnotation>): any;
export declare function applyValueAnnotations(plain: any, annotations: MinimisedTree<TypeAnnotation>, superJson: SuperJSON): any;
export declare function applyReferentialEqualityAnnotations(plain: any, annotations: ReferentialEqualityAnnotations): any;

@@ -15,3 +16,3 @@ interface Result {

export declare function generateReferentialEqualityAnnotations(identitites: Map<any, any[][]>): ReferentialEqualityAnnotations | undefined;
export declare const walker: (object: any, identities: Map<any, any[][]>, path?: any[], objectsInThisPath?: any[]) => Result;
export declare const walker: (object: any, identities: Map<any, any[][]>, superJson: SuperJSON, path?: any[], objectsInThisPath?: any[]) => Result;
export {};

@@ -50,5 +50,5 @@ "use strict";

}
function applyValueAnnotations(plain, annotations) {
function applyValueAnnotations(plain, annotations, superJson) {
traverse(annotations, function (type, path) {
plain = accessDeep_1.setDeep(plain, path, function (v) { return transformer_1.untransformValue(v, type); });
plain = accessDeep_1.setDeep(plain, path, function (v) { return transformer_1.untransformValue(v, type, superJson); });
});

@@ -80,3 +80,3 @@ return plain;

exports.applyReferentialEqualityAnnotations = applyReferentialEqualityAnnotations;
var isDeep = function (object) {
var isDeep = function (object, superJson) {
return is_1.isPlainObject(object) ||

@@ -86,3 +86,3 @@ is_1.isArray(object) ||

is_1.isSet(object) ||
transformer_1.isInstanceOfRegisteredClass(object);
transformer_1.isInstanceOfRegisteredClass(object, superJson);
};

@@ -128,3 +128,3 @@ function addIdentity(object, path, identities) {

exports.generateReferentialEqualityAnnotations = generateReferentialEqualityAnnotations;
var walker = function (object, identities, path, objectsInThisPath) {
var walker = function (object, identities, superJson, path, objectsInThisPath) {
var _a;

@@ -136,4 +136,4 @@ if (path === void 0) { path = []; }

}
if (!isDeep(object)) {
var transformed_1 = transformer_1.transformValue(object);
if (!isDeep(object, superJson)) {
var transformed_1 = transformer_1.transformValue(object, superJson);
if (transformed_1) {

@@ -156,3 +156,3 @@ return {

}
var transformationResult = transformer_1.transformValue(object);
var transformationResult = transformer_1.transformValue(object, superJson);
var transformed = (_a = transformationResult === null || transformationResult === void 0 ? void 0 : transformationResult.value) !== null && _a !== void 0 ? _a : object;

@@ -165,3 +165,3 @@ if (!is_1.isPrimitive(object)) {

util_1.forEach(transformed, function (value, index) {
var recursiveResult = exports.walker(value, identities, __spreadArray(__spreadArray([], __read(path)), [index]), objectsInThisPath);
var recursiveResult = exports.walker(value, identities, superJson, __spreadArray(__spreadArray([], __read(path)), [index]), objectsInThisPath);
transformedValue[index] = recursiveResult.transformedValue;

@@ -168,0 +168,0 @@ if (is_1.isArray(recursiveResult.annotations)) {

@@ -0,1 +1,2 @@

import SuperJSON from '.';
export declare type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint';

@@ -10,8 +11,8 @@ declare type LeafTypeAnnotation = PrimitiveTypeAnnotation | 'regexp' | 'Date' | 'Error' | 'URL';

export declare type TypeAnnotation = SimpleTypeAnnotation | CompositeTypeAnnotation;
export declare function isInstanceOfRegisteredClass(potentialClass: any): potentialClass is any;
export declare const transformValue: (value: any) => {
export declare function isInstanceOfRegisteredClass(potentialClass: any, superJson: SuperJSON): potentialClass is any;
export declare const transformValue: (value: any, superJson: SuperJSON) => {
value: any;
type: TypeAnnotation;
} | undefined;
export declare const untransformValue: (json: any, type: TypeAnnotation) => any;
export declare const untransformValue: (json: any, type: TypeAnnotation, superJson: SuperJSON) => any;
export {};

@@ -37,6 +37,2 @@ "use strict";

var is_1 = require("./is");
var class_registry_1 = require("./class-registry");
var symbol_registry_1 = require("./symbol-registry");
var custom_transformer_registry_1 = require("./custom-transformer-registry");
var error_props_1 = require("./error-props");
var util_1 = require("./util");

@@ -61,3 +57,3 @@ function simpleTransformation(isApplicable, annotation, transform, untransform) {

simpleTransformation(is_1.isDate, 'Date', function (v) { return v.toISOString(); }, function (v) { return new Date(v); }),
simpleTransformation(is_1.isError, 'Error', function (v) {
simpleTransformation(is_1.isError, 'Error', function (v, superJson) {
var baseError = {

@@ -67,11 +63,11 @@ name: v.name,

};
error_props_1.allowedErrorProps.forEach(function (prop) {
superJson.allowedErrorProps.forEach(function (prop) {
baseError[prop] = v[prop];
});
return baseError;
}, function (v) {
}, function (v, superJson) {
var e = new Error(v.message);
e.name = v.name;
e.stack = v.stack;
error_props_1.allowedErrorProps.forEach(function (prop) {
superJson.allowedErrorProps.forEach(function (prop) {
e[prop] = v[prop];

@@ -115,13 +111,13 @@ });

}
var symbolRule = compositeTransformation(function (s) {
var symbolRule = compositeTransformation(function (s, superJson) {
if (is_1.isSymbol(s)) {
var isRegistered = !!symbol_registry_1.SymbolRegistry.getIdentifier(s);
var isRegistered = !!superJson.symbolRegistry.getIdentifier(s);
return isRegistered;
}
return false;
}, function (s) {
var identifier = symbol_registry_1.SymbolRegistry.getIdentifier(s);
}, function (s, superJson) {
var identifier = superJson.symbolRegistry.getIdentifier(s);
return ['symbol', identifier];
}, function (v) { return v.description; }, function (_, a) {
var value = symbol_registry_1.SymbolRegistry.getValue(a[1]);
}, function (v) { return v.description; }, function (_, a, superJson) {
var value = superJson.symbolRegistry.getValue(a[1]);
if (!value) {

@@ -153,5 +149,5 @@ throw new Error('Trying to deserialize unknown symbol');

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

@@ -162,7 +158,7 @@ }

exports.isInstanceOfRegisteredClass = isInstanceOfRegisteredClass;
var classRule = compositeTransformation(isInstanceOfRegisteredClass, function (clazz) {
var identifier = class_registry_1.ClassRegistry.getIdentifier(clazz.constructor);
var classRule = compositeTransformation(isInstanceOfRegisteredClass, function (clazz, superJson) {
var identifier = superJson.classRegistry.getIdentifier(clazz.constructor);
return ['class', identifier];
}, function (clazz) {
var allowedProps = class_registry_1.ClassRegistry.getAllowedProps(clazz.constructor);
}, function (clazz, superJson) {
var allowedProps = superJson.classRegistry.getAllowedProps(clazz.constructor);
if (!allowedProps) {

@@ -176,4 +172,4 @@ return __assign({}, clazz);

return result;
}, function (v, a) {
var clazz = class_registry_1.ClassRegistry.getValue(a[1]);
}, function (v, a, superJson) {
var clazz = superJson.classRegistry.getValue(a[1]);
if (!clazz) {

@@ -184,12 +180,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) {
return !!custom_transformer_registry_1.CustomTransformerRegistry.findApplicable(value);
}, function (value) {
var transformer = custom_transformer_registry_1.CustomTransformerRegistry.findApplicable(value);
var customRule = compositeTransformation(function (value, superJson) {
return !!superJson.customTransformerRegistry.findApplicable(value);
}, function (value, superJson) {
var transformer = superJson.customTransformerRegistry.findApplicable(value);
return ['custom', transformer.name];
}, function (value) {
var transformer = custom_transformer_registry_1.CustomTransformerRegistry.findApplicable(value);
}, function (value, superJson) {
var transformer = superJson.customTransformerRegistry.findApplicable(value);
return transformer.serialize(value);
}, function (v, a) {
var transformer = custom_transformer_registry_1.CustomTransformerRegistry.findByName(a[1]);
}, function (v, a, superJson) {
var transformer = superJson.customTransformerRegistry.findByName(a[1]);
if (!transformer) {

@@ -201,18 +197,18 @@ throw new Error('Trying to deserialize unknown custom value');

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

@@ -228,13 +224,13 @@ };

});
var untransformValue = function (json, type) {
var untransformValue = function (json, type, superJson) {
if (is_1.isArray(type)) {
switch (type[0]) {
case 'symbol':
return symbolRule.untransform(json, type);
return symbolRule.untransform(json, type, superJson);
case 'class':
return classRule.untransform(json, type);
return classRule.untransform(json, type, superJson);
case 'custom':
return customRule.untransform(json, type);
return customRule.untransform(json, type, superJson);
case 'typed-array':
return typedArrayRule.untransform(json, type);
return typedArrayRule.untransform(json, type, superJson);
default:

@@ -249,3 +245,3 @@ throw new Error('Unknown transformation: ' + type);

}
return transformation.untransform(json);
return transformation.untransform(json, superJson);
}

@@ -252,0 +248,0 @@ };

{
"version": "1.10.1",
"version": "1.11.0",
"license": "MIT",

@@ -4,0 +4,0 @@ "main": "dist/index.js",

@@ -9,3 +9,3 @@ import { Registry } from './registry';

class _ClassRegistry extends Registry<Class> {
export class ClassRegistry extends Registry<Class> {
constructor() {

@@ -33,3 +33,1 @@ super(c => c.name);

}
export const ClassRegistry = new _ClassRegistry();

@@ -11,18 +11,18 @@ import { JSONValue } from './types';

const transfomers: Record<string, CustomTransfomer<any, any>> = {};
export class CustomTransformerRegistry {
private transfomers: Record<string, CustomTransfomer<any, any>> = {};
export const CustomTransformerRegistry = {
register<I, O extends JSONValue>(transformer: CustomTransfomer<I, O>) {
transfomers[transformer.name] = transformer;
},
this.transfomers[transformer.name] = transformer;
}
findApplicable<T>(v: T) {
return find(transfomers, transformer => transformer.isApplicable(v)) as
| CustomTransfomer<T, JSONValue>
| undefined;
},
return find(this.transfomers, transformer =>
transformer.isApplicable(v)
) as CustomTransfomer<T, JSONValue> | undefined;
}
findByName(name: string) {
return transfomers[name];
},
};
return this.transfomers[name];
}
}

@@ -1079,1 +1079,18 @@ /* eslint-disable es5/no-for-of */

});
test('superjson instances are independent of one another', () => {
class Car {}
const s1 = new SuperJSON();
s1.registerClass(Car);
const s2 = new SuperJSON();
const value = {
car: new Car(),
};
const res1 = s1.serialize(value);
expect(res1.meta?.values).toEqual({ car: [['class', 'Car']] });
const res2 = s2.serialize(value);
expect(res2.json).toEqual(value);
});
import { SuperJSONResult, SuperJSONValue, Class, JSONValue } from './types';
import { ClassRegistry, RegisterOptions } from './class-registry';
import { SymbolRegistry } from './symbol-registry';
import { Registry } from './registry';
import {

@@ -8,3 +8,2 @@ CustomTransfomer,

} from './custom-transformer-registry';
import { allowErrorProps } from './error-props';
import {

@@ -18,78 +17,114 @@ walker,

export const serialize = (object: SuperJSONValue): SuperJSONResult => {
const identities = new Map<any, any[][]>();
const output = walker(object, identities);
const res: SuperJSONResult = {
json: output.transformedValue,
};
if (output.annotations) {
res.meta = {
...res.meta,
values: output.annotations,
export default class SuperJSON {
serialize(object: SuperJSONValue): SuperJSONResult {
const identities = new Map<any, any[][]>();
const output = walker(object, identities, this);
const res: SuperJSONResult = {
json: output.transformedValue,
};
}
const equalityAnnotations = generateReferentialEqualityAnnotations(
identities
);
if (equalityAnnotations) {
res.meta = {
...res.meta,
referentialEqualities: equalityAnnotations,
};
if (output.annotations) {
res.meta = {
...res.meta,
values: output.annotations,
};
}
const equalityAnnotations = generateReferentialEqualityAnnotations(
identities
);
if (equalityAnnotations) {
res.meta = {
...res.meta,
referentialEqualities: equalityAnnotations,
};
}
return res;
}
return res;
};
deserialize<T = unknown>(payload: SuperJSONResult): T {
const { json, meta } = payload;
export const deserialize = <T = unknown>(payload: SuperJSONResult): T => {
const { json, meta } = payload;
let result: T = copy(json) as any;
let result: T = copy(json) as any;
if (meta?.values) {
result = applyValueAnnotations(result, meta.values, this);
}
if (meta?.values) {
result = applyValueAnnotations(result, meta.values);
if (meta?.referentialEqualities) {
result = applyReferentialEqualityAnnotations(
result,
meta.referentialEqualities
);
}
return result;
}
if (meta?.referentialEqualities) {
result = applyReferentialEqualityAnnotations(
result,
meta.referentialEqualities
);
stringify(object: SuperJSONValue): string {
return JSON.stringify(this.serialize(object));
}
return result;
};
parse<T = unknown>(string: string): T {
return this.deserialize(JSON.parse(string));
}
export const stringify = (object: SuperJSONValue): string =>
JSON.stringify(serialize(object));
readonly classRegistry = new ClassRegistry();
registerClass(v: Class, options?: RegisterOptions | string) {
this.classRegistry.register(v, options);
}
export const parse = <T = unknown>(string: string): T =>
deserialize(JSON.parse(string));
readonly symbolRegistry = new Registry<Symbol>(s => s.description ?? '');
registerSymbol(v: Symbol, identifier?: string) {
this.symbolRegistry.register(v, identifier);
}
export const registerClass = (v: Class, options?: RegisterOptions | string) =>
ClassRegistry.register(v, options);
readonly customTransformerRegistry = new CustomTransformerRegistry();
registerCustom<I, O extends JSONValue>(
transformer: Omit<CustomTransfomer<I, O>, 'name'>,
name: string
) {
this.customTransformerRegistry.register({
name,
...transformer,
});
}
export const registerSymbol = (v: Symbol, identifier?: string) =>
SymbolRegistry.register(v, identifier);
readonly allowedErrorProps: string[] = [];
allowErrorProps(...props: string[]) {
this.allowedErrorProps.push(...props);
}
export const registerCustom = <I, O extends JSONValue>(
transformer: Omit<CustomTransfomer<I, O>, 'name'>,
name: string
) =>
CustomTransformerRegistry.register({
name,
...transformer,
});
private static defaultInstance = new SuperJSON();
static serialize = SuperJSON.defaultInstance.serialize.bind(
SuperJSON.defaultInstance
);
static deserialize = SuperJSON.defaultInstance.deserialize.bind(
SuperJSON.defaultInstance
);
static stringify = SuperJSON.defaultInstance.stringify.bind(
SuperJSON.defaultInstance
);
static parse = SuperJSON.defaultInstance.parse.bind(
SuperJSON.defaultInstance
);
static registerClass = SuperJSON.defaultInstance.registerClass.bind(
SuperJSON.defaultInstance
);
static registerSymbol = SuperJSON.defaultInstance.registerSymbol.bind(
SuperJSON.defaultInstance
);
static registerCustom = SuperJSON.defaultInstance.registerCustom.bind(
SuperJSON.defaultInstance
);
static allowErrorProps = SuperJSON.defaultInstance.allowErrorProps.bind(
SuperJSON.defaultInstance
);
}
export default {
stringify,
parse,
serialize,
deserialize,
registerClass,
registerSymbol,
registerCustom,
allowErrorProps,
};
export const serialize = SuperJSON.serialize;
export const deserialize = SuperJSON.deserialize;
export const stringify = SuperJSON.stringify;
export const parse = SuperJSON.parse;

@@ -0,1 +1,2 @@

import SuperJSON from '.';
import { walker } from './plainer';

@@ -10,3 +11,4 @@

},
new Map()
new Map(),
new SuperJSON()
)

@@ -13,0 +15,0 @@ ).toEqual({

@@ -19,2 +19,3 @@ import {

import { getDeep, setDeep } from './accessDeep';
import SuperJSON from '.';

@@ -55,6 +56,7 @@ type Tree<T> = InnerNode<T> | Leaf<T>;

plain: any,
annotations: MinimisedTree<TypeAnnotation>
annotations: MinimisedTree<TypeAnnotation>,
superJson: SuperJSON
) {
traverse(annotations, (type, path) => {
plain = setDeep(plain, path, v => untransformValue(v, type));
plain = setDeep(plain, path, v => untransformValue(v, type, superJson));
});

@@ -93,3 +95,3 @@

const isDeep = (object: any): boolean =>
const isDeep = (object: any, superJson: SuperJSON): boolean =>
isPlainObject(object) ||

@@ -99,3 +101,3 @@ isArray(object) ||

isSet(object) ||
isInstanceOfRegisteredClass(object);
isInstanceOfRegisteredClass(object, superJson);

@@ -158,2 +160,3 @@ function addIdentity(object: any, path: any[], identities: Map<any, any[][]>) {

identities: Map<any, any[][]>,
superJson: SuperJSON,
path: any[] = [],

@@ -166,4 +169,4 @@ objectsInThisPath: any[] = []

if (!isDeep(object)) {
const transformed = transformValue(object);
if (!isDeep(object, superJson)) {
const transformed = transformValue(object, superJson);
if (transformed) {

@@ -187,3 +190,3 @@ return {

const transformationResult = transformValue(object);
const transformationResult = transformValue(object, superJson);
const transformed = transformationResult?.value ?? object;

@@ -202,2 +205,3 @@

identities,
superJson,
[...path, index],

@@ -204,0 +208,0 @@ objectsInThisPath

@@ -17,7 +17,4 @@ import {

} from './is';
import { ClassRegistry } from './class-registry';
import { SymbolRegistry } from './symbol-registry';
import { CustomTransformerRegistry } from './custom-transformer-registry';
import { allowedErrorProps } from './error-props';
import { findArr } from './util';
import SuperJSON from '.';

@@ -49,6 +46,6 @@ export type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint';

function simpleTransformation<I, O, A extends SimpleTypeAnnotation>(
isApplicable: (v: any) => v is I,
isApplicable: (v: any, superJson: SuperJSON) => v is I,
annotation: A,
transform: (v: I) => O,
untransform: (v: O) => I
transform: (v: I, superJson: SuperJSON) => O,
untransform: (v: O, superJson: SuperJSON) => I
) {

@@ -94,3 +91,3 @@ return {

'Error',
v => {
(v, superJson) => {
const baseError: any = {

@@ -101,3 +98,3 @@ name: v.name,

allowedErrorProps.forEach(prop => {
superJson.allowedErrorProps.forEach(prop => {
baseError[prop] = (v as any)[prop];

@@ -108,3 +105,3 @@ });

},
v => {
(v, superJson) => {
const e = new Error(v.message);

@@ -114,3 +111,3 @@ e.name = v.name;

allowedErrorProps.forEach(prop => {
superJson.allowedErrorProps.forEach(prop => {
(e as any)[prop] = v[prop];

@@ -184,6 +181,6 @@ });

function compositeTransformation<I, O, A extends CompositeTypeAnnotation>(
isApplicable: (v: any) => v is I,
annotation: (v: I) => A,
transform: (v: I) => O,
untransform: (v: O, a: A) => I
isApplicable: (v: any, superJson: SuperJSON) => v is I,
annotation: (v: I, superJson: SuperJSON) => A,
transform: (v: I, superJson: SuperJSON) => O,
untransform: (v: O, a: A, superJson: SuperJSON) => I
) {

@@ -199,5 +196,5 @@ return {

const symbolRule = compositeTransformation(
(s): s is Symbol => {
(s, superJson): s is Symbol => {
if (isSymbol(s)) {
const isRegistered = !!SymbolRegistry.getIdentifier(s);
const isRegistered = !!superJson.symbolRegistry.getIdentifier(s);
return isRegistered;

@@ -207,9 +204,9 @@ }

},
s => {
const identifier = SymbolRegistry.getIdentifier(s);
(s, superJson) => {
const identifier = superJson.symbolRegistry.getIdentifier(s);
return ['symbol', identifier!];
},
v => v.description,
(_, a) => {
const value = SymbolRegistry.getValue(a[1]);
(_, a, superJson) => {
const value = superJson.symbolRegistry.getValue(a[1]);
if (!value) {

@@ -253,6 +250,7 @@ throw new Error('Trying to deserialize unknown symbol');

export function isInstanceOfRegisteredClass(
potentialClass: any
potentialClass: any,
superJson: SuperJSON
): potentialClass is any {
if (potentialClass?.constructor) {
const isRegistered = !!ClassRegistry.getIdentifier(
const isRegistered = !!superJson.classRegistry.getIdentifier(
potentialClass.constructor

@@ -267,8 +265,10 @@ );

isInstanceOfRegisteredClass,
clazz => {
const identifier = ClassRegistry.getIdentifier(clazz.constructor);
(clazz, superJson) => {
const identifier = superJson.classRegistry.getIdentifier(clazz.constructor);
return ['class', identifier!];
},
clazz => {
const allowedProps = ClassRegistry.getAllowedProps(clazz.constructor);
(clazz, superJson) => {
const allowedProps = superJson.classRegistry.getAllowedProps(
clazz.constructor
);
if (!allowedProps) {

@@ -284,4 +284,4 @@ return { ...clazz };

},
(v, a) => {
const clazz = ClassRegistry.getValue(a[1]);
(v, a, superJson) => {
const clazz = superJson.classRegistry.getValue(a[1]);

@@ -299,15 +299,19 @@ if (!clazz) {

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

@@ -323,11 +327,12 @@ throw new Error('Trying to deserialize unknown custom value');

export const transformValue = (
value: any
value: any,
superJson: SuperJSON
): { value: any; type: TypeAnnotation } | undefined => {
const applicableCompositeRule = findArr(compositeRules, rule =>
rule.isApplicable(value)
rule.isApplicable(value, superJson)
);
if (applicableCompositeRule) {
return {
value: applicableCompositeRule.transform(value as never),
type: applicableCompositeRule.annotation(value),
value: applicableCompositeRule.transform(value as never, superJson),
type: applicableCompositeRule.annotation(value, superJson),
};

@@ -337,3 +342,3 @@ }

const applicableSimpleRule = findArr(simpleRules, rule =>
rule.isApplicable(value)
rule.isApplicable(value, superJson)
);

@@ -343,3 +348,3 @@

return {
value: applicableSimpleRule.transform(value as never),
value: applicableSimpleRule.transform(value as never, superJson),
type: applicableSimpleRule.annotation,

@@ -357,13 +362,17 @@ };

export const untransformValue = (json: any, type: TypeAnnotation) => {
export const untransformValue = (
json: any,
type: TypeAnnotation,
superJson: SuperJSON
) => {
if (isArray(type)) {
switch (type[0]) {
case 'symbol':
return symbolRule.untransform(json, type);
return symbolRule.untransform(json, type, superJson);
case 'class':
return classRule.untransform(json, type);
return classRule.untransform(json, type, superJson);
case 'custom':
return customRule.untransform(json, type);
return customRule.untransform(json, type, superJson);
case 'typed-array':
return typedArrayRule.untransform(json, type);
return typedArrayRule.untransform(json, type, superJson);
default:

@@ -378,4 +387,4 @@ throw new Error('Unknown transformation: ' + type);

return transformation.untransform(json as never);
return transformation.untransform(json as never, superJson);
}
};

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