Skip to content

Commit

Permalink
Move exports
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Sep 8, 2023
1 parent 6f6619a commit d9eefed
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 148 deletions.
146 changes: 146 additions & 0 deletions packages/seroval/src/core/tree/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { Feature } from '../compat';
import { SerovalNodeType } from '../constants';
import type { SerovalNode } from '../types';
import parseAsync from './async';
import type {
ParserOptions,
SerializerContext,
} from './context';
import {
createDeserializerContext,
createParserContext,
createSerializerContext,
getRefParam,
getRootID,
} from './context';
import deserializeTree from './deserialize';
import serializeTree, { resolvePatches } from './serialize';
import parseSync from './sync';

function finalize(
ctx: SerializerContext,
rootID: number,
isObject: boolean,
result: string,
): string {
// Shared references detected
if (ctx.vars.length) {
const patches = resolvePatches(ctx);
let body = result;
if (patches) {
// Get (or create) a ref from the source
const index = getRefParam(ctx, rootID);
body = result + ',' + patches + index;
if (!result.startsWith(index + '=')) {
body = index + '=' + body;
}
}
let params = ctx.vars.length > 1
? ctx.vars.join(',')
: ctx.vars[0];
// Source is probably already assigned
if (ctx.features & Feature.ArrowFunction) {
params = ctx.vars.length > 1 || ctx.vars.length === 0
? '(' + params + ')'
: params;
return '(' + params + '=>(' + body + '))()';
}
return '(function(' + params + '){return ' + body + '})()';
}
if (isObject) {
return '(' + result + ')';
}
return result;
}

export function serialize<T>(
source: T,
options?: Partial<ParserOptions>,
): string {
const ctx = createParserContext(options);
const tree = parseSync(ctx, source);
const serial = createSerializerContext({
markedRefs: ctx.reference.marked,
features: ctx.features,
});
const result = serializeTree(serial, tree);
return finalize(
serial,
getRootID(ctx, source),
tree.t === SerovalNodeType.Object,
result,
);
}

export async function serializeAsync<T>(
source: T,
options?: Partial<ParserOptions>,
): Promise<string> {
const ctx = createParserContext(options);
const tree = await parseAsync(ctx, source);
const serial = createSerializerContext({
markedRefs: ctx.reference.marked,
features: ctx.features,
});
const result = serializeTree(serial, tree);
return finalize(
serial,
getRootID(ctx, source),
tree.t === SerovalNodeType.Object,
result,
);
}

export function deserialize<T>(source: string): T {
// eslint-disable-next-line no-eval
return (0, eval)(source) as T;
}

export interface SerovalJSON {
t: SerovalNode;
r: number;
f: number;
m: number[];
}

export function toJSON<T>(
source: T,
options?: Partial<ParserOptions>,
): SerovalJSON {
const ctx = createParserContext(options);
return {
t: parseSync(ctx, source),
r: getRootID(ctx, source),
f: ctx.features,
m: Array.from(ctx.reference.marked),
};
}

export async function toJSONAsync<T>(
source: T,
options?: Partial<ParserOptions>,
): Promise<SerovalJSON> {
const ctx = createParserContext(options);
return {
t: await parseAsync(ctx, source),
r: getRootID(ctx, source),
f: ctx.features,
m: Array.from(ctx.reference.marked),
};
}

export function compileJSON(source: SerovalJSON): string {
const serial = createSerializerContext({
features: source.f,
markedRefs: source.m,
});
const result = serializeTree(serial, source.t);
return finalize(serial, source.r, source.t.i === SerovalNodeType.Object, result);
}

export function fromJSON<T>(source: SerovalJSON): T {
const serial = createDeserializerContext({
markedRefs: source.m,
});
return deserializeTree(serial, source.t) as T;
}
151 changes: 3 additions & 148 deletions packages/seroval/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
/* eslint-disable no-await-in-loop */
import { Feature } from './core/compat';
import type {
SerializerContext,
ParserOptions,
} from './core/tree/context';
import {
getRefParam,
createParserContext,
createSerializerContext,
getRootID,
createDeserializerContext,
} from './core/tree/context';
import parseSync from './core/tree/sync';
import parseAsync from './core/tree/async';
import deserializeTree from './core/tree/deserialize';
import serializeTree, { resolvePatches } from './core/tree/serialize';
import type { SerovalNode } from './core/types';
import { SerovalNodeType } from './core/constants';
import { serialize } from './core/tree';

export type {
AsyncServerValue,
Expand All @@ -27,135 +9,8 @@ export type {
ErrorValue,
} from './types';
export { Feature } from './core/compat';
export { createReference } from './core/reference';

function finalize(
ctx: SerializerContext,
rootID: number,
isObject: boolean,
result: string,
): string {
// Shared references detected
if (ctx.vars.length) {
const patches = resolvePatches(ctx);
let body = result;
if (patches) {
// Get (or create) a ref from the source
const index = getRefParam(ctx, rootID);
body = result + ',' + patches + index;
if (!result.startsWith(index + '=')) {
body = index + '=' + body;
}
}
let params = ctx.vars.length > 1
? ctx.vars.join(',')
: ctx.vars[0];
// Source is probably already assigned
if (ctx.features & Feature.ArrowFunction) {
params = ctx.vars.length > 1 || ctx.vars.length === 0
? '(' + params + ')'
: params;
return '(' + params + '=>(' + body + '))()';
}
return '(function(' + params + '){return ' + body + '})()';
}
if (isObject) {
return '(' + result + ')';
}
return result;
}

export function serialize<T>(
source: T,
options?: Partial<ParserOptions>,
): string {
const ctx = createParserContext(options);
const tree = parseSync(ctx, source);
const serial = createSerializerContext({
markedRefs: ctx.reference.marked,
features: ctx.features,
});
const result = serializeTree(serial, tree);
return finalize(
serial,
getRootID(ctx, source),
tree.t === SerovalNodeType.Object,
result,
);
}

export async function serializeAsync<T>(
source: T,
options?: Partial<ParserOptions>,
): Promise<string> {
const ctx = createParserContext(options);
const tree = await parseAsync(ctx, source);
const serial = createSerializerContext({
markedRefs: ctx.reference.marked,
features: ctx.features,
});
const result = serializeTree(serial, tree);
return finalize(
serial,
getRootID(ctx, source),
tree.t === SerovalNodeType.Object,
result,
);
}

export function deserialize<T>(source: string): T {
// eslint-disable-next-line no-eval
return (0, eval)(source) as T;
}

export interface SerovalJSON {
t: SerovalNode;
r: number;
f: number;
m: number[];
}

export function toJSON<T>(
source: T,
options?: Partial<ParserOptions>,
): SerovalJSON {
const ctx = createParserContext(options);
return {
t: parseSync(ctx, source),
r: getRootID(ctx, source),
f: ctx.features,
m: Array.from(ctx.reference.marked),
};
}

export async function toJSONAsync<T>(
source: T,
options?: Partial<ParserOptions>,
): Promise<SerovalJSON> {
const ctx = createParserContext(options);
return {
t: await parseAsync(ctx, source),
r: getRootID(ctx, source),
f: ctx.features,
m: Array.from(ctx.reference.marked),
};
}

export function compileJSON(source: SerovalJSON): string {
const serial = createSerializerContext({
features: source.f,
markedRefs: source.m,
});
const result = serializeTree(serial, source.t);
return finalize(serial, source.r, source.t.i === SerovalNodeType.Object, result);
}

export function fromJSON<T>(source: SerovalJSON): T {
const serial = createDeserializerContext({
markedRefs: source.m,
});
return deserializeTree(serial, source.t) as T;
}
export * from './core/tree';

export default serialize;

export { createReference } from './core/reference';

0 comments on commit d9eefed

Please sign in to comment.