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

Commit dc286be

Browse files
authored
feat: serialization of promises (#20)
1 parent 183526a commit dc286be

31 files changed

+1095
-48
lines changed

.rfcs/001-serialize-async.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Serializing promises and other async generators
2+
3+
### Finished JSON output
4+
5+
```js
6+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
7+
const out = [
8+
// <first line> is just a `[` that initializes the array pf the response
9+
// <second line>
10+
{
11+
json: {
12+
foo: "bar",
13+
iterator: ["AsyncIterator", 1, "__tson"],
14+
promise: ["Promise", 0, "__tson"],
15+
},
16+
nonce: "__tson",
17+
},
18+
// <second line>
19+
// <second line of json>
20+
[
21+
// ------ this could be streamed ------
22+
[1, ["chunk", "chunk from iterator"]],
23+
[0, ["resolve", "result of promise"]],
24+
[1, ["chunk", "another chunk from iterator"]],
25+
[1, ["end"]],
26+
],
27+
];
28+
```
29+
30+
### Serializing
31+
32+
> This is now implemented

CHANGELOG.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
2-
31
# [0.10.0](https://github.com/KATT/tupleson/compare/0.9.0...0.10.0) (2023-10-02)
42

5-
63
### Features
74

8-
* use UUIDs for nonce ([#19](https://github.com/KATT/tupleson/issues/19)) ([e347640](https://github.com/KATT/tupleson/commit/e347640dd10bf6ecc6b93f99e3118f572da671b3))
5+
- use UUIDs for nonce ([#19](https://github.com/KATT/tupleson/issues/19)) ([e347640](https://github.com/KATT/tupleson/commit/e347640dd10bf6ecc6b93f99e3118f572da671b3))
96

107
# [0.9.0](https://github.com/KATT/tupleson/compare/0.8.0...0.9.0) (2023-10-01)
118

@@ -72,4 +69,4 @@
7269
### Features
7370

7471
- initial version ([#1](https://github.com/KATT/tupleson/issues/1)) ([ccce25b](https://github.com/KATT/tupleson/commit/ccce25b6a039cf2e5c1a774c1ab022f0946ca8d5))
75-
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
72+
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
## Introduction
2727

28+
> This package is still experimental (although it's pretty well tested) & is subject to big changes
29+
2830
A hackable JSON serializer/deserializer that allows you to serialize/deserialize almost[^1] anything.
2931

3032
Serialize exactly what you want; no more, no less.
@@ -104,9 +106,9 @@ type Obj = typeof obj;
104106
// -> type Obj = { foo: string; set: Set<number>; }
105107
```
106108

107-
### Extend with a custom serializer
109+
## Extend with a custom serializer
108110

109-
#### [Temporal](https://www.npmjs.com/package/@js-temporal/polyfill)
111+
### [Temporal](https://www.npmjs.com/package/@js-temporal/polyfill)
110112

111113
> See test reference in [`./src/extend/temporal.test.ts`](./src/extend/temporal.test.ts)
112114
@@ -134,7 +136,7 @@ const tson = createTson({
134136
});
135137
```
136138

137-
#### [Decimal.js](https://github.com/MikeMcl/decimal.js)
139+
### [Decimal.js](https://github.com/MikeMcl/decimal.js)
138140

139141
> See test reference in [`./src/extend/decimal.test.ts`](./src/extend/decimal.test.ts)
140142
@@ -154,6 +156,8 @@ const tson = createTson({
154156
});
155157
```
156158

159+
---
160+
157161
<!-- ## All contributors ✨
158162
159163
<a href="https://github.com/KATT/tupleson/graphs/contributors">

cspell.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
"commitlint",
1515
"contributorsrc",
1616
"conventionalcommits",
17+
"Iterarable",
18+
"KATT",
1719
"knip",
1820
"lcov",
1921
"markdownlintignore",
2022
"npmpackagejsonlintrc",
2123
"outro",
2224
"packagejson",
2325
"quickstart",
26+
"stringifier",
27+
"tson",
2428
"tsup",
29+
"tupleson",
2530
"wontfix",
26-
"tson",
27-
"stringifier",
28-
"KATT",
29-
"tupleson"
31+
"deferreds"
3032
]
3133
}

src/async/asyncTypes.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from "vitest";
2+
3+
import "./asyncTypes.js";
4+
5+
test.todo("check that it retains type");

src/async/asyncTypes.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// eslint-disable-next-line eslint-comments/disable-enable-pair
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
import { TsonType } from "../types.js";
4+
import { TsonBranded, TsonTypeTesterCustom } from "../types.js";
5+
import { serialized } from "../types.js";
6+
7+
export type TsonAsyncStringifierIterator<TValue> = AsyncIterable<string> & {
8+
[serialized]: TValue;
9+
};
10+
11+
export type TsonAsyncStringifier = <TValue>(
12+
value: TValue,
13+
space?: number,
14+
) => TsonAsyncStringifierIterator<TValue>;
15+
export type TsonAsyncIndex = TsonBranded<number, "AsyncRegistered">;
16+
export interface TsonTransformerSerializeDeserializeAsync<TValue> {
17+
async: true;
18+
/**
19+
* From JSON-serializable value
20+
*/
21+
deserialize: (
22+
v: TsonAsyncIndex,
23+
register: (index: TsonAsyncIndex) => Promise<TValue>,
24+
) => TValue;
25+
26+
/**
27+
* The key to use when serialized
28+
*/
29+
key: string;
30+
/**
31+
* JSON-serializable value
32+
*/
33+
serialize: (
34+
v: TValue,
35+
register: (thing: TValue) => TsonAsyncIndex,
36+
) => TsonAsyncIndex;
37+
}
38+
39+
export interface TsonAsyncType<TValue>
40+
extends TsonTransformerSerializeDeserializeAsync<TValue>,
41+
TsonTypeTesterCustom {}
42+
export interface TsonAsyncOptions {
43+
/**
44+
* The nonce function every time we start serializing a new object
45+
* Should return a unique value every time it's called
46+
* @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random`
47+
*/
48+
nonce?: () => number | string;
49+
/**
50+
* The list of types to use
51+
*/
52+
types: (TsonAsyncType<any> | TsonType<any, any> | TsonType<any, never>)[];
53+
}

src/async/createTsonAsync.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { TsonAsyncOptions } from "./asyncTypes.js";
2+
import { createTsonParseAsync } from "./deserializeAsync.js";
3+
import { createAsyncTsonStringify } from "./serializeAsync.js";
4+
5+
export const createTsonAsync = (opts: TsonAsyncOptions) => ({
6+
parse: createTsonParseAsync(opts),
7+
stringify: createAsyncTsonStringify(opts),
8+
});

0 commit comments

Comments
 (0)