This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
201 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import { createAsyncTsonSerializer } from "./serializeAsync.js"; | ||
import { TsonAsyncOptions } from "./types.js"; | ||
|
||
export const createTsonAsync = (opts: TsonAsyncOptions) => ({}); | ||
export const createTsonAsync = (opts: TsonAsyncOptions) => ({ | ||
serializeAsync: createAsyncTsonSerializer(opts), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
export class TsonError extends Error { | ||
constructor(message: string, opts?: ErrorOptions) { | ||
super(message, opts); | ||
this.name = this.constructor.name; | ||
this.name = "TsonError"; | ||
|
||
// set prototype | ||
} | ||
} | ||
|
||
export class CircularReferenceError extends TsonError { | ||
export class TsonCircularReferenceError extends TsonError { | ||
/** | ||
* The circular reference that was found | ||
*/ | ||
public readonly value; | ||
|
||
constructor(value: unknown) { | ||
super(`Circular reference detected`); | ||
this.name = this.constructor.name; | ||
this.name = "TsonCircularReferenceError"; | ||
this.value = value; | ||
} | ||
} | ||
|
||
export class PromiseRejectionError extends TsonError { | ||
export class TsonPromiseRejectionError extends TsonError { | ||
constructor(cause: unknown) { | ||
super(`Promise rejected`, { cause }); | ||
this.name = this.constructor.name; | ||
this.name = "TsonPromiseRejectionError"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,146 @@ | ||
import { test } from "vitest"; | ||
import { expect, test } from "vitest"; | ||
|
||
import { createTson, createTsonAsync, tsonPromise } from "../index.js"; | ||
import { createTsonAsync, tsonPromise } from "../index.js"; | ||
|
||
test("tsonPromise", async () => { | ||
const createPromise = <T>(result: () => T) => { | ||
return new Promise<T>((resolve) => { | ||
setTimeout(() => { | ||
resolve(result()); | ||
}, 1); | ||
}); | ||
}; | ||
|
||
test("serialize promise", async () => { | ||
const tson = createTsonAsync({ | ||
nonce: () => "__tson", | ||
types: [tsonPromise], | ||
}); | ||
|
||
const promise = Promise.resolve(42); | ||
|
||
const serialized = tson.stringify(promise); | ||
const deserialized = tson.parse(serialized); | ||
const [head, iterator] = tson.serializeAsync(promise); | ||
|
||
expect(head).toMatchInlineSnapshot(` | ||
{ | ||
"json": [ | ||
"Promise", | ||
0, | ||
"__tson", | ||
], | ||
"nonce": "__tson", | ||
} | ||
`); | ||
|
||
const values = []; | ||
for await (const value of iterator) { | ||
values.push(value); | ||
} | ||
|
||
expect(values).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
0, | ||
0, | ||
42, | ||
], | ||
] | ||
`); | ||
}); | ||
|
||
test("serialize promise that returns a promise", async () => { | ||
const tson = createTsonAsync({ | ||
nonce: () => "__tson", | ||
types: [tsonPromise], | ||
}); | ||
|
||
const obj = { | ||
promise: createPromise(() => { | ||
return { | ||
anotherPromise: createPromise(() => { | ||
return 42; | ||
}), | ||
}; | ||
}), | ||
}; | ||
|
||
const [head, iterator] = tson.serializeAsync(obj); | ||
|
||
expect(head).toMatchInlineSnapshot(` | ||
{ | ||
"json": { | ||
"promise": [ | ||
"Promise", | ||
0, | ||
"__tson", | ||
], | ||
}, | ||
"nonce": "__tson", | ||
} | ||
`); | ||
|
||
const values = []; | ||
for await (const value of iterator) { | ||
values.push(value); | ||
} | ||
|
||
expect(values).toHaveLength(2); | ||
|
||
expect(values).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
0, | ||
0, | ||
{ | ||
"anotherPromise": [ | ||
"Promise", | ||
1, | ||
"__tson", | ||
], | ||
}, | ||
], | ||
[ | ||
1, | ||
0, | ||
42, | ||
], | ||
] | ||
`); | ||
}); | ||
|
||
test("promise that rejects", async () => { | ||
const tson = createTsonAsync({ | ||
nonce: () => "__tson", | ||
types: [tsonPromise], | ||
}); | ||
|
||
const promise = Promise.reject(new Error("foo")); | ||
|
||
const [head, iterator] = tson.serializeAsync(promise); | ||
|
||
expect(head).toMatchInlineSnapshot(` | ||
{ | ||
"json": [ | ||
"Promise", | ||
0, | ||
"__tson", | ||
], | ||
"nonce": "__tson", | ||
} | ||
`); | ||
|
||
const values = []; | ||
|
||
for await (const value of iterator) { | ||
values.push(value); | ||
} | ||
|
||
expect(values).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
0, | ||
1, | ||
[TsonPromiseRejectionError: Promise rejected], | ||
], | ||
] | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters