Skip to content

Commit

Permalink
patch(vestjs-runtime): Support minification of values
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 30, 2023
1 parent 21b4884 commit e7858d2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 21 deletions.
6 changes: 5 additions & 1 deletion packages/vest/src/exports/SuiteSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ const focusMiniMap: Record<keyof IsolateFocusedPayload, string> = {
matchAll: 'mA',
};

const MiniMap = assign({}, testMiniMap, focusMiniMap);
const MiniMap = {
keys: {
data: assign({}, testMiniMap, focusMiniMap),
},
};
2 changes: 1 addition & 1 deletion packages/vestjs-runtime/src/Isolate/IsolateKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const KeyToMinified = {
// }
export const MinifiedToKey = invertKeyMap(KeyToMinified);

export function invertKeyMap(miniMap: Record<string, string>) {
export function invertKeyMap(miniMap: Record<string, string> = {}) {
return Object.entries(miniMap).reduce(
(acc, [key, minified]) =>
assign(acc, {
Expand Down
40 changes: 32 additions & 8 deletions packages/vestjs-runtime/src/exports/IsolateSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class IsolateSerializer {
// eslint-disable-next-line max-statements, complexity
static deserialize(
node: Record<string, any> | TIsolate | string,
payloadMiniMap: Maybe<MiniMap>
miniMaps: Maybe<MiniMaps>
): TIsolate {
// the assumption is that the tree is built correctly,
// but the children are missing the parent property to
Expand All @@ -35,7 +35,7 @@ export class IsolateSerializer {
// to avoid circular references during serialization.
// we need to rebuild the tree and add back the parent property to the children
// and the keys property to the parents.
const inverseMinimap = invertKeyMap(payloadMiniMap ?? {});
const inverseMinimap = invertKeyMap(miniMaps?.keys?.data);

// Validate the root object
const root = isStringValue(node)
Expand Down Expand Up @@ -107,13 +107,13 @@ export class IsolateSerializer {

static serialize(
isolate: Nullable<TIsolate>,
miniMap: Maybe<MiniMap>
miniMaps: Maybe<MiniMaps>
): string {
if (isNullish(isolate)) {
return '';
}

return JSON.stringify(transformIsolate(isolate, miniMap));
return JSON.stringify(transformIsolate(isolate, miniMaps));
}

static expandChildren(node: Record<string, any>): Nullable<TIsolate[]> {
Expand All @@ -134,18 +134,18 @@ export class IsolateSerializer {
// eslint-disable-next-line max-statements, complexity
function transformIsolate(
isolate: TIsolate,
miniMap: Maybe<MiniMap>
miniMaps: Maybe<MiniMaps>
): Record<string, any> {
const next: Record<string, any> = {};

if (isolate.children) {
next[MinifiedKeys.Children] = isolate.children.map(isolate =>
transformIsolate(isolate, miniMap)
transformIsolate(isolate, miniMaps)
);
}

if (!isEmpty(isolate.data)) {
next[MinifiedKeys.Data] = transformKeys(isolate.data, miniMap);
next[MinifiedKeys.Data] = transformKeys(isolate.data, miniMaps?.keys?.data);
}

for (const key in isolate) {
Expand All @@ -163,7 +163,7 @@ function transformIsolate(
}

const keyToUse = minifyKey(key);
next[keyToUse] = value;
next[keyToUse] = minifyValueByKey(key, value, miniMaps);
}

return next;
Expand All @@ -177,6 +177,20 @@ function minifyKey(key: string): string {
return KeyToMinified[key as keyof typeof KeyToMinified] ?? key;
}

function minifyValueByKey(
key: string,
value: any,
miniMaps: Maybe<MiniMaps>
): any {
if (isNullish(value)) {
return value;
}

const keyMap = miniMaps?.values?.[key as keyof MiniMaps['values']];

return keyMap ? keyMap[value] ?? value : value;
}

function transformKeys(
data: Record<string, any>,
keyMap: Maybe<MiniMap>
Expand All @@ -199,3 +213,13 @@ function transformKeys(
}

type MiniMap = Record<string, string>;

type MiniMaps = Partial<{
keys: Partial<{
[IsolateKeys.Data]: MiniMap;
}>;
values: Partial<{
[IsolateKeys.Status]: MiniMap;
[IsolateKeys.Type]: MiniMap;
}>;
}>;
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { Maybe } from 'vest-utils';
import { Isolate, TIsolate } from 'Isolate';
import { MinifiedKeys } from 'IsolateKeys';
import { IsolateSerializer } from 'IsolateSerializer';
import { VestRuntime } from 'vestjs-runtime';
import { IsolateMutator, VestRuntime } from 'vestjs-runtime';

describe('IsolateSerializer', () => {
describe('serialize', () => {
it('Should produce serialized dump', () => {
const { serialized } = createSerialized();
const { serialized } = createRoot();

expect(serialized).toMatchSnapshot();
});
});

describe('deserialize', () => {
it('Should fully inflate the tree', () => {
const { root, serialized } = createSerialized();
const { root, serialized } = createRoot();

const inflated = IsolateSerializer.deserialize(serialized);

Expand Down Expand Up @@ -48,7 +48,7 @@ describe('IsolateSerializer', () => {
});

test('When data is empty, should not add data property', () => {
const { serialized } = createSerialized();
const { serialized } = createRoot();
const inflated = IsolateSerializer.deserialize(serialized);

expect(inflated?.children?.[0]).toHaveProperty('data');
Expand All @@ -59,8 +59,12 @@ describe('IsolateSerializer', () => {

describe('Custom Data Serialization', () => {
it('Should serialize data with custom keys', () => {
const { serialized } = createSerialized({
some_data: 'sd',
const { serialized } = createRoot({
keys: {
data: {
some_data: 'sd',
},
},
});

const parsed = JSON.parse(serialized);
Expand All @@ -70,19 +74,67 @@ describe('IsolateSerializer', () => {
);
});

describe('value serialization', () => {
it('Should serialize values with shorthand values', () => {
const { root } = createRoot();

root.status = 'pending';
// @ts-ignore
root.children[0].status = 'done';
// @ts-ignore
root.children[1].status = 'failed';

const serialized = IsolateSerializer.serialize(root, {
values: {
status: {
pending: 'p',
done: 'd',
failed: 'f',
},
$type: {
URoot: 'UR',
UChild_1: 'UC1',
UChild_2: 'UC2',
UChild_3: 'UC3',
},
},
});

const parsed = JSON.parse(serialized);
expect(parsed[MinifiedKeys.Status]).toBe('p');
expect(parsed.C[0][MinifiedKeys.Status]).toBe('d');
expect(parsed.C[1][MinifiedKeys.Status]).toBe('f');
expect(parsed[MinifiedKeys.Type]).toBe('UR');
expect(parsed.C[0][MinifiedKeys.Type]).toBe('UC1');
expect(parsed.C[1][MinifiedKeys.Type]).toBe('UC2');
expect(parsed.C[2][MinifiedKeys.Type]).toBe('UC3');
expect(serialized).toMatchInlineSnapshot(
`"{"C":[{"D":{"some_data":true},"$":"UC1","S":"d"},{"$":"UC2","S":"f"},{"$":"UC3"}],"D":{"some_data":true},"$":"UR","S":"p"}"`
);
});
});

it('Should inflate with correct keys', () => {
const { serialized } = createSerialized({
some_data: 'sd',
const { serialized } = createRoot({
keys: {
data: {
some_data: 'sd',
},
},
});

const inflated = IsolateSerializer.deserialize(serialized, {
some_data: 'sd',
keys: {
data: {
some_data: 'sd',
},
},
});

expect(inflated.data.some_data).toBe(true);
expect(inflated).not.toHaveProperty('sd');
expect(inflated).toEqual(
IsolateSerializer.deserialize(createSerialized().serialized)
IsolateSerializer.deserialize(createRoot().serialized)
);
expect(inflated).toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -119,7 +171,7 @@ function withRunTime<T>(fn: CB<T>) {
});
}

function createSerialized(miniMap: Maybe<Record<string, string>>) {
function createRoot(miniMap: Maybe<Record<string, any>>) {
let serialized: string, root: TIsolate;

withRunTime(() => {
Expand Down

2 comments on commit e7858d2

@vercel
Copy link

@vercel vercel bot commented on e7858d2 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next.vercel.app
vest-website.vercel.app
vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app

@vercel
Copy link

@vercel vercel bot commented on e7858d2 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest – ./website

vest-ealush.vercel.app
vestjs.dev
vest-git-latest-ealush.vercel.app
vest.vercel.app
www.vestjs.dev

Please sign in to comment.