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

Commit

Permalink
feat: serialization of promises (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Oct 3, 2023
1 parent 183526a commit dc286be
Show file tree
Hide file tree
Showing 31 changed files with 1,095 additions and 48 deletions.
32 changes: 32 additions & 0 deletions .rfcs/001-serialize-async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Serializing promises and other async generators

### Finished JSON output

```js
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
const out = [
// <first line> is just a `[` that initializes the array pf the response
// <second line>
{
json: {
foo: "bar",
iterator: ["AsyncIterator", 1, "__tson"],
promise: ["Promise", 0, "__tson"],
},
nonce: "__tson",
},
// <second line>
// <second line of json>
[
// ------ this could be streamed ------
[1, ["chunk", "chunk from iterator"]],
[0, ["resolve", "result of promise"]],
[1, ["chunk", "another chunk from iterator"]],
[1, ["end"]],
],
];
```

### Serializing

> This is now implemented
7 changes: 2 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@


# [0.10.0](https://github.com/KATT/tupleson/compare/0.9.0...0.10.0) (2023-10-02)


### Features

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

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

Expand Down Expand Up @@ -72,4 +69,4 @@
### Features

- initial version ([#1](https://github.com/KATT/tupleson/issues/1)) ([ccce25b](https://github.com/KATT/tupleson/commit/ccce25b6a039cf2e5c1a774c1ab022f0946ca8d5))
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

## Introduction

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

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

### Extend with a custom serializer
## Extend with a custom serializer

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

> See test reference in [`./src/extend/temporal.test.ts`](./src/extend/temporal.test.ts)
Expand Down Expand Up @@ -134,7 +136,7 @@ const tson = createTson({
});
```

#### [Decimal.js](https://github.com/MikeMcl/decimal.js)
### [Decimal.js](https://github.com/MikeMcl/decimal.js)

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

---

<!-- ## All contributors ✨
<a href="https://github.com/KATT/tupleson/graphs/contributors">
Expand Down
10 changes: 6 additions & 4 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
"commitlint",
"contributorsrc",
"conventionalcommits",
"Iterarable",
"KATT",
"knip",
"lcov",
"markdownlintignore",
"npmpackagejsonlintrc",
"outro",
"packagejson",
"quickstart",
"stringifier",
"tson",
"tsup",
"tupleson",
"wontfix",
"tson",
"stringifier",
"KATT",
"tupleson"
"deferreds"
]
}
5 changes: 5 additions & 0 deletions src/async/asyncTypes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from "vitest";

import "./asyncTypes.js";

test.todo("check that it retains type");
53 changes: 53 additions & 0 deletions src/async/asyncTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TsonType } from "../types.js";
import { TsonBranded, TsonTypeTesterCustom } from "../types.js";
import { serialized } from "../types.js";

export type TsonAsyncStringifierIterator<TValue> = AsyncIterable<string> & {
[serialized]: TValue;
};

export type TsonAsyncStringifier = <TValue>(
value: TValue,
space?: number,
) => TsonAsyncStringifierIterator<TValue>;
export type TsonAsyncIndex = TsonBranded<number, "AsyncRegistered">;
export interface TsonTransformerSerializeDeserializeAsync<TValue> {
async: true;
/**
* From JSON-serializable value
*/
deserialize: (
v: TsonAsyncIndex,
register: (index: TsonAsyncIndex) => Promise<TValue>,
) => TValue;

/**
* The key to use when serialized
*/
key: string;
/**
* JSON-serializable value
*/
serialize: (
v: TValue,
register: (thing: TValue) => TsonAsyncIndex,
) => TsonAsyncIndex;
}

export interface TsonAsyncType<TValue>
extends TsonTransformerSerializeDeserializeAsync<TValue>,
TsonTypeTesterCustom {}
export interface TsonAsyncOptions {
/**
* The nonce function every time we start serializing a new object
* Should return a unique value every time it's called
* @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random`
*/
nonce?: () => number | string;
/**
* The list of types to use
*/
types: (TsonAsyncType<any> | TsonType<any, any> | TsonType<any, never>)[];
}
8 changes: 8 additions & 0 deletions src/async/createTsonAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TsonAsyncOptions } from "./asyncTypes.js";
import { createTsonParseAsync } from "./deserializeAsync.js";
import { createAsyncTsonStringify } from "./serializeAsync.js";

export const createTsonAsync = (opts: TsonAsyncOptions) => ({
parse: createTsonParseAsync(opts),
stringify: createAsyncTsonStringify(opts),
});
Loading

0 comments on commit dc286be

Please sign in to comment.