Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

chore: simplify serialization a bit #22

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions src/async/deserializeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,14 @@ export function createTsonParseAsync(opts: TsonAsyncOptions): TsonParseAsync {
) {
function readLine(str: string) {
str = str.trimStart();
if (!str) {
return;
}

if (str.startsWith(",")) {
// ignore leading comma
str = str.slice(1);
}

if (!str.startsWith("[")) {
if (str.length < 2) {
// minimum length is 2: '[]'
return;
}

Expand Down Expand Up @@ -156,7 +154,7 @@ export function createTsonParseAsync(opts: TsonAsyncOptions): TsonParseAsync {
lines.push(...(lastResult.value as string).split("\n").filter(Boolean));

// console.log("got line", lines);
} while (lines.length < 4);
} while (lines.length < 2);

const [
/**
Expand All @@ -166,25 +164,16 @@ export function createTsonParseAsync(opts: TsonAsyncOptions): TsonParseAsync {
/**
* Second line is the shape of the JSON
*/
secondLine,
/**
* Third line is a `,`
*/
_thirdLine,
/**
* Fourth line is the start of the values array
*/
_fourthLine,
/**
* Buffer is the rest of the iterator that came in the chunks while we were waiting for the first 4 lines
*/
headLine,
// .. third line is a `,`
// .. fourth line is the start of the values array
...buffer
] = lines;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const secondValueParsed = JSON.parse(secondLine!) as TsonSerialized<any>;
const head = JSON.parse(headLine!) as TsonSerialized<any>;

const walk = walker(secondValueParsed.nonce);
const walk = walker(head.nonce);

void getStreamedValues(buffer, !!lastResult.done, walk).catch((cause) => {
// Something went wrong while getting the streamed values
Expand All @@ -203,7 +192,7 @@ export function createTsonParseAsync(opts: TsonAsyncOptions): TsonParseAsync {
deferreds.clear();
});

return walk(secondValueParsed.json);
return walk(head.json);
}

const result = await init().catch((cause: unknown) => {
Expand Down
1 change: 0 additions & 1 deletion src/handlers/tsonPromise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ test("stringifier - no promises", async () => {
buffer.push(value.trimEnd());
}

// expect(buffer).toHaveLength(5);
expect(buffer).toMatchInlineSnapshot(`
[
"[",
Expand Down
Loading