diff --git a/src/async/serializeAsync.test.ts b/src/async/serializeAsync.test.ts index 295b0e25..bd075bfe 100644 --- a/src/async/serializeAsync.test.ts +++ b/src/async/serializeAsync.test.ts @@ -1,7 +1,10 @@ import { expect, test } from "vitest"; -import { tsonAsyncIterator, tsonPromise } from "../index.js"; -import { createAsyncTsonSerialize } from "./serializeAsync.js"; +import { tsonAsyncIterator, tsonBigint, tsonPromise } from "../index.js"; +import { + createAsyncTsonSerialize, + createAsyncTsonStringify, +} from "./serializeAsync.js"; test("serialize promise", async () => { const serialize = createAsyncTsonSerialize({ @@ -152,3 +155,66 @@ test("serialize async iterable", async () => { ] `); }); + +test("stringify async iterable + promise", async () => { + const stringify = createAsyncTsonStringify({ + nonce: () => "__tson", + types: [tsonAsyncIterator, tsonPromise, tsonBigint], + }); + + async function* iterable() { + await new Promise((resolve) => setTimeout(resolve, 1)); + yield 1n; + yield 2n; + } + + const obj = { + foo: "bar", + iterable: iterable(), + promise: Promise.resolve(42), + }; + const buffer: string[] = []; + for await (const value of stringify(obj, 2)) { + buffer.push(value.trimEnd()); + } + + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const head: any = JSON.parse(buffer[1]!); + + expect(buffer).toMatchInlineSnapshot(` + [ + "[", + " {\\"json\\":{\\"foo\\":\\"bar\\",\\"iterable\\":[\\"AsyncIterator\\",0,\\"__tson\\"],\\"promise\\":[\\"Promise\\",1,\\"__tson\\"]},\\"nonce\\":\\"__tson\\"}", + " ,", + " [", + " [1,[0,42]]", + " ,[0,[0,[\\"bigint\\",\\"1\\",\\"__tson\\"]]]", + " ,[0,[0,[\\"bigint\\",\\"2\\",\\"__tson\\"]]]", + " ,[0,[2]]", + " ]", + "]", + ] + `); + + expect(head).toMatchInlineSnapshot(` + { + "json": { + "foo": "bar", + "iterable": [ + "AsyncIterator", + 0, + "__tson", + ], + "promise": [ + "Promise", + 1, + "__tson", + ], + }, + "nonce": "__tson", + } + `); + + expect(head.json.iterable[0]).toBe("AsyncIterator"); + expect(head.json.promise[0]).toBe("Promise"); +}); diff --git a/src/async/serializeAsync.ts b/src/async/serializeAsync.ts index dba7251c..5114276e 100644 --- a/src/async/serializeAsync.ts +++ b/src/async/serializeAsync.ts @@ -77,7 +77,7 @@ function walkerFactory(nonce: TsonNonce, types: TsonAsyncOptions["types"]) { ); } - const valueTuple: TsonAsyncValueTuple = [idx, result.value]; + const valueTuple: TsonAsyncValueTuple = [idx, walk(result.value)]; yield valueTuple; } while (iterators.size > 0); },