Skip to content

Commit

Permalink
fix some e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Varixo committed Aug 4, 2024
1 parent a94c826 commit 3d50bec
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,15 @@ const parseRequest = async (
const data = query.get(QDATA_KEY);
if (data) {
try {
return qwikSerializer._deserialize(decodeURIComponent(data)) as JSONValue | undefined;
const deserializedData = qwikSerializer._deserialize(decodeURIComponent(data));
return [deserializedData] as JSONValue | undefined;
} catch (err) {
//
}
}
}
return qwikSerializer._deserialize(await request.text()) as JSONValue | undefined;
const deserializedData = qwikSerializer._deserialize(await request.text());
return [deserializedData] as JSONValue | undefined;
}
return undefined;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ async function pureServerFunction(ev: RequestEvent) {
ev.exit();
const isDev = getRequestMode(ev) === 'dev';
const qwikSerializer = (ev as RequestEventInternal)[RequestEvQwikSerializer];
const data = await ev.parseBody();
const [data] = (await ev.parseBody()) as unknown[];
if (Array.isArray(data)) {
const [qrl, ...args] = data;
if (isQrl(qrl) && qrl.getHash() === fn) {
Expand All @@ -311,11 +311,11 @@ async function pureServerFunction(ev: RequestEvent) {
} catch (err) {
if (err instanceof ServerError) {
ev.headers.set('Content-Type', 'application/qwik-json');
ev.send(err.status, await qwikSerializer._serialize(err.data));
ev.send(err.status, await qwikSerializer._serialize([err.data]));
return;
}
ev.headers.set('Content-Type', 'application/qwik-json');
ev.send(500, await qwikSerializer._serialize(err));
ev.send(500, await qwikSerializer._serialize([err]));
return;
}
if (isAsyncIterator(result)) {
Expand All @@ -326,7 +326,7 @@ async function pureServerFunction(ev: RequestEvent) {
if (isDev) {
verifySerializable(qwikSerializer, item, qrl);
}
const message = await qwikSerializer._serialize(item);
const message = await qwikSerializer._serialize([item]);
if (ev.signal.aborted) {
break;
}
Expand All @@ -336,7 +336,7 @@ async function pureServerFunction(ev: RequestEvent) {
} else {
verifySerializable(qwikSerializer, result, qrl);
ev.headers.set('Content-Type', 'application/qwik-json');
const message = await qwikSerializer._serialize(result);
const message = await qwikSerializer._serialize([result]);
ev.send(200, message);
}
return;
Expand Down Expand Up @@ -542,7 +542,7 @@ export async function renderQData(requestEv: RequestEvent) {
const writer = requestEv.getWritableStream().getWriter();
const qwikSerializer = (requestEv as RequestEventInternal)[RequestEvQwikSerializer];
// write just the page json data to the response body
const data = await qwikSerializer._serialize(qData);
const data = await qwikSerializer._serialize([qData]);
writer.write(encoder.encode(data));
requestEv.sharedMap.set('qData', qData);

Expand Down
10 changes: 7 additions & 3 deletions packages/qwik-city/runtime/src/server-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ export const zodQrl = ((
return z.object(obj);
}
});
const data = inputData ?? (await ev.parseBody());
let data = inputData;
if (!data) {
data = await ev.parseBody();
}
const result = await (await schema).safeParseAsync(data);
if (result.success) {
return result;
Expand Down Expand Up @@ -376,7 +379,7 @@ export const serverQrl = <T extends ServerFunction>(
})();
} else if (contentType === 'application/qwik-json') {
const str = await res.text();
const obj = _deserialize(str, ctxElm ?? document.documentElement);
const [obj] = _deserialize(str, ctxElm ?? document.documentElement);
if (res.status === 500) {
throw obj;
}
Expand Down Expand Up @@ -456,7 +459,8 @@ const deserializeStream = async function* (
const lines = buffer.split(/\n/);
buffer = lines.pop()!;
for (const line of lines) {
yield _deserialize(line, ctxElm);
const [deserializedData] = _deserialize(line, ctxElm);
yield deserializedData;
}
}
} finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik-city/runtime/src/use-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const loadClientData = async (
if ((rsp.headers.get('content-type') || '').includes('json')) {
// we are safe we are reading a q-data.json
return rsp.text().then((text) => {
const clientData = _deserialize(text, element) as ClientPageData | null;
const [clientData] = _deserialize(text, element) as [ClientPageData];
if (!clientData) {
location.href = url.href;
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik-city/static/worker-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async function workerRender(
};
});

const serialized = await _serialize(qData);
const serialized = await _serialize([qData]);
dataWriter.write(serialized);

writePromises.push(
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik/src/core/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export interface DelHTMLAttributes<T extends Element> extends Attrs<'del', T> {
}

// @internal (undocumented)
export function _deserialize(rawStateData: string, element?: unknown): unknown[] | unknown;
export function _deserialize(rawStateData: string | null, element?: unknown): unknown[];

// @internal (undocumented)
export const _deserializeData: (data: string, element?: unknown) => any;
Expand Down Expand Up @@ -1047,7 +1047,7 @@ export interface SelectHTMLAttributes<T extends Element> extends Attrs<'select',
}

// @internal (undocumented)
export function _serialize(data: unknown): Promise<string>;
export function _serialize(data: unknown[]): Promise<string>;

// @internal (undocumented)
export const _serializeData: (data: any, pureQRL?: boolean) => Promise<string>;
Expand Down
52 changes: 44 additions & 8 deletions packages/qwik/src/core/v2/shared/shared-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { getOrCreateProxy, isStore } from '../../state/store';
import { Task, type ResourceReturnInternal } from '../../use/use-task';
import { throwErrorAndStop } from '../../util/log';
import { isPromise } from '../../util/promises';
import type { ValueOrPromise } from '../../util/types';
import { isSerializableObject, type ValueOrPromise } from '../../util/types';
import { getDomContainer, type DomContainer } from '../client/dom-container';
import { vnode_getNode, vnode_isVNode, vnode_locate } from '../client/vnode';
import type { SymbolToChunkResolver } from '../ssr/ssr-types';
Expand Down Expand Up @@ -1243,17 +1243,15 @@ export function qrlToString(
}

/** @internal */
export async function _serialize(data: unknown): Promise<string> {
export async function _serialize(data: unknown[]): Promise<string> {
const serializationContext = createSerializationContext(
null,
new WeakMap(),
() => '',
() => {}
);

const roots = Array.isArray(data) ? data : [data];

for (const root of roots) {
for (const root of data) {
serializationContext.$addRoot$(root);
}
await serializationContext.$breakCircularDepsAndAwaitPromises$();
Expand All @@ -1262,10 +1260,13 @@ export async function _serialize(data: unknown): Promise<string> {
}

/** @internal */
export function _deserialize(rawStateData: string, element?: unknown): unknown[] | unknown {
export function _deserialize(rawStateData: string | null, element?: unknown): unknown[] {
if (rawStateData == null) {
return [];
}
const stateData = JSON.parse(rawStateData);
if (!Array.isArray(stateData)) {
return null;
return [];
}

let container: DomContainer | undefined = undefined;
Expand All @@ -1281,7 +1282,7 @@ export function _deserialize(rawStateData: string, element?: unknown): unknown[]

function deserializeData(
stateData: unknown[],
serializedData: string,
serializedData: unknown,
container?: DeserializeContainer
) {
let typeCode: number;
Expand All @@ -1301,6 +1302,41 @@ function deserializeData(
}
}
return propValue;
} else if (serializedData && typeof serializedData === 'object') {
if (Array.isArray(serializedData)) {
return deserializeArray(stateData, serializedData, container);
} else {
return deserializeObject(stateData, serializedData, container);
}
}
return serializedData;
}

function deserializeObject(
stateData: unknown[],
serializedData: object,
container?: DeserializeContainer
) {
if (!isSerializableObject(serializedData)) {
return serializedData;
}
for (const key in serializedData) {
if (Object.prototype.hasOwnProperty.call(serializedData, key)) {
const value = serializedData[key];
serializedData[key] = deserializeData(stateData, value, container);
}
}
return serializedData;
}

function deserializeArray(
stateData: unknown[],
serializedData: Array<unknown>,
container?: DeserializeContainer
) {
for (let i = 0; i < serializedData.length; i++) {
const value = serializedData[i];
serializedData[i] = deserializeData(stateData, value, container);
}
return serializedData;
}
Expand Down

0 comments on commit 3d50bec

Please sign in to comment.