first commit
This commit is contained in:
92
node_modules/superjson/dist/index.js
generated
vendored
Normal file
92
node_modules/superjson/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
import { ClassRegistry } from './class-registry.js';
|
||||
import { Registry } from './registry.js';
|
||||
import { CustomTransformerRegistry, } from './custom-transformer-registry.js';
|
||||
import { applyReferentialEqualityAnnotations, applyValueAnnotations, generateReferentialEqualityAnnotations, walker, } from './plainer.js';
|
||||
import { copy } from 'copy-anything';
|
||||
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`.
|
||||
*/
|
||||
constructor({ dedupe = false, } = {}) {
|
||||
this.classRegistry = new ClassRegistry();
|
||||
this.symbolRegistry = new Registry(s => s.description ?? '');
|
||||
this.customTransformerRegistry = new CustomTransformerRegistry();
|
||||
this.allowedErrorProps = [];
|
||||
this.dedupe = dedupe;
|
||||
}
|
||||
serialize(object) {
|
||||
const identities = new Map();
|
||||
const output = walker(object, identities, this, this.dedupe);
|
||||
const res = {
|
||||
json: output.transformedValue,
|
||||
};
|
||||
if (output.annotations) {
|
||||
res.meta = {
|
||||
...res.meta,
|
||||
values: output.annotations,
|
||||
};
|
||||
}
|
||||
const equalityAnnotations = generateReferentialEqualityAnnotations(identities, this.dedupe);
|
||||
if (equalityAnnotations) {
|
||||
res.meta = {
|
||||
...res.meta,
|
||||
referentialEqualities: equalityAnnotations,
|
||||
};
|
||||
}
|
||||
if (res.meta)
|
||||
res.meta.v = 1;
|
||||
return res;
|
||||
}
|
||||
deserialize(payload, options) {
|
||||
const { json, meta } = payload;
|
||||
let result = options?.inPlace ? json : copy(json);
|
||||
if (meta?.values) {
|
||||
result = applyValueAnnotations(result, meta.values, meta.v ?? 0, this);
|
||||
}
|
||||
if (meta?.referentialEqualities) {
|
||||
result = applyReferentialEqualityAnnotations(result, meta.referentialEqualities, meta.v ?? 0);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
stringify(object) {
|
||||
return JSON.stringify(this.serialize(object));
|
||||
}
|
||||
parse(string) {
|
||||
return this.deserialize(JSON.parse(string), { inPlace: true });
|
||||
}
|
||||
registerClass(v, options) {
|
||||
this.classRegistry.register(v, options);
|
||||
}
|
||||
registerSymbol(v, identifier) {
|
||||
this.symbolRegistry.register(v, identifier);
|
||||
}
|
||||
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 default SuperJSON;
|
||||
export { SuperJSON };
|
||||
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
|
||||
Reference in New Issue
Block a user