Skip to content

Commit 813bbbf

Browse files
authored
Merge pull request lxsmnsyc#11 from lxsmnsyc/next
0.6.0
2 parents 2154c03 + 9f44c8a commit 813bbbf

36 files changed

+753
-272
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,37 @@ console.log(await deserialize(result)); // 100
186186
> **Note**
187187
> `seroval` can only serialize the resolved value and so the output will always be using `Promise.resolve`. If the Promise fulfills with rejection, the rejected value is thrown before serialization happens.
188188
189+
## Serializable references
190+
191+
There are values that has no way to be serializable at all, i.e. functions, but usually in an isomorphic code, functions can exist on both client and server-side. What if we can serialize these functions in such a way we can refer to their counterparts?
192+
193+
`seroval` has `createReference` that you can use to map user-defined strings to their references.
194+
195+
```js
196+
import { createReference } from 'seroval';
197+
198+
const thisIsAnIsomorphicFunction = createReference(
199+
// This is (ideally) a unique identifier
200+
// that is used to map the serialized value
201+
// to its actual reference (and vice versa)
202+
'my-function',
203+
() => {
204+
// Ideally this function should exist on both
205+
// server and client, but we want to add the ability
206+
// to serialize and deserialize this reference on
207+
// both sides
208+
}
209+
);
210+
211+
// we can now serialize this
212+
const serialized = toJSON(thisIsAnIsomorphicFunction); // or any of the serializer
213+
thisIsAnIsomorphicFunction === fromJSON(serialized); // true
214+
```
215+
216+
> **Note**
217+
> It can only accept objects, functions and symbols and it doesn't actually
218+
> serialize their values but only the string you used to identify the reference
219+
189220
## Supports
190221

191222
The following values are the only values accepted by `seroval`:
@@ -232,7 +263,10 @@ The following values are the only values accepted by `seroval`:
232263
- `Promise` (with `serializeAsync`)
233264
- [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
234265
- [Well-known symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#static_properties)
266+
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)
267+
- [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
235268
- Cyclic references (both self and mutual)
269+
- Isomorphic references (a reference that exist on both the serializer and deserializer side)
236270

237271
## Compat
238272

@@ -318,6 +352,10 @@ By default, all feature flags are enabled. The following are the feature flags a
318352
- Throws when attempted to use
319353
- Also throws if `BigInt` is disabled.
320354
- Disables serialization of `BigInt64Array` and `BigUint64Array`
355+
- `WebAPI`
356+
- Throws and disables the following usage:
357+
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)
358+
- [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
321359

322360
## Sponsors
323361

packages/seroval/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,37 @@ console.log(await deserialize(result)); // 100
186186
> **Note**
187187
> `seroval` can only serialize the resolved value and so the output will always be using `Promise.resolve`. If the Promise fulfills with rejection, the rejected value is thrown before serialization happens.
188188
189+
## Serializable references
190+
191+
There are values that has no way to be serializable at all, i.e. functions, but usually in an isomorphic code, functions can exist on both client and server-side. What if we can serialize these functions in such a way we can refer to their counterparts?
192+
193+
`seroval` has `createReference` that you can use to map user-defined strings to their references.
194+
195+
```js
196+
import { createReference } from 'seroval';
197+
198+
const thisIsAnIsomorphicFunction = createReference(
199+
// This is (ideally) a unique identifier
200+
// that is used to map the serialized value
201+
// to its actual reference (and vice versa)
202+
'my-function',
203+
() => {
204+
// Ideally this function should exist on both
205+
// server and client, but we want to add the ability
206+
// to serialize and deserialize this reference on
207+
// both sides
208+
}
209+
);
210+
211+
// we can now serialize this
212+
const serialized = toJSON(thisIsAnIsomorphicFunction); // or any of the serializer
213+
thisIsAnIsomorphicFunction === fromJSON(serialized); // true
214+
```
215+
216+
> **Note**
217+
> It can only accept objects, functions and symbols and it doesn't actually
218+
> serialize their values but only the string you used to identify the reference
219+
189220
## Supports
190221

191222
The following values are the only values accepted by `seroval`:
@@ -232,7 +263,10 @@ The following values are the only values accepted by `seroval`:
232263
- `Promise` (with `serializeAsync`)
233264
- [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
234265
- [Well-known symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#static_properties)
266+
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)
267+
- [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
235268
- Cyclic references (both self and mutual)
269+
- Isomorphic references (a reference that exist on both the serializer and deserializer side)
236270

237271
## Compat
238272

@@ -318,6 +352,10 @@ By default, all feature flags are enabled. The following are the feature flags a
318352
- Throws when attempted to use
319353
- Also throws if `BigInt` is disabled.
320354
- Disables serialization of `BigInt64Array` and `BigUint64Array`
355+
- `WebAPI`
356+
- Throws and disables the following usage:
357+
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)
358+
- [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
321359

322360
## Sponsors
323361

packages/seroval/src/compat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const enum Feature {
1919
Symbol = 0x400,
2020
TypedArray = 0x800,
2121
BigIntTypedArray = 0x1000,
22+
WebAPI = 0x2000,
2223
}
2324

24-
export const ALL_ENABLED = 0x1FFF;
25+
export const ALL_ENABLED = 0x2FFF;

packages/seroval/src/context.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ALL_ENABLED } from './compat';
22
import getIdentifier from './get-identifier';
3-
import { AsyncServerValue } from './types';
43

54
interface IndexAssignment {
65
t: 'index';
@@ -49,7 +48,7 @@ export interface SerializationContext {
4948
// Supported features
5049
features: number;
5150

52-
valueMap: Map<number, AsyncServerValue>;
51+
valueMap: Map<number, unknown>;
5352
}
5453

5554
export interface Options {
@@ -118,9 +117,9 @@ export function getRefParam(ctx: SerializationContext, index: number) {
118117
return identifier;
119118
}
120119

121-
export function getRootID(
120+
export function getRootID<T>(
122121
ctx: ParserContext,
123-
current: AsyncServerValue,
122+
current: T,
124123
) {
125124
const ref = ctx.refs.get(current);
126125
if (ref == null) {
@@ -129,9 +128,9 @@ export function getRootID(
129128
return ref;
130129
}
131130

132-
export function createRef(
131+
export function createIndexedValue<T>(
133132
ctx: ParserContext,
134-
current: AsyncServerValue,
133+
current: T,
135134
) {
136135
const ref = ctx.refs.get(current);
137136
if (ref == null) {

packages/seroval/src/index.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ import deserializeTree from './tree/deserialize';
1212
import serializeTree, { resolvePatches } from './tree/serialize';
1313
import parseSync from './tree/sync';
1414
import { SerovalNode } from './tree/types';
15-
import {
16-
AsyncServerValue,
17-
PrimitiveValue,
18-
ServerValue,
19-
CommonServerValue,
20-
SemiPrimitiveValue,
21-
ErrorValue,
22-
} from './types';
2315

2416
export {
2517
AsyncServerValue,
@@ -28,8 +20,8 @@ export {
2820
CommonServerValue,
2921
SemiPrimitiveValue,
3022
ErrorValue,
31-
Feature,
32-
};
23+
} from './types';
24+
export { Feature } from './compat';
3325

3426
function finalize(
3527
ctx: SerializationContext,
@@ -67,7 +59,7 @@ function finalize(
6759
return result;
6860
}
6961

70-
export function serialize<T extends ServerValue>(
62+
export function serialize<T>(
7163
source: T,
7264
options?: Partial<Options>,
7365
) {
@@ -78,7 +70,7 @@ export function serialize<T extends ServerValue>(
7870
return finalize(serial, rootID, isObject, result);
7971
}
8072

81-
export async function serializeAsync<T extends AsyncServerValue>(
73+
export async function serializeAsync<T>(
8274
source: T,
8375
options?: Partial<Options>,
8476
) {
@@ -89,7 +81,7 @@ export async function serializeAsync<T extends AsyncServerValue>(
8981
return finalize(serial, rootID, isObject, result);
9082
}
9183

92-
export function deserialize<T extends AsyncServerValue>(source: string): T {
84+
export function deserialize<T>(source: string): T {
9385
// eslint-disable-next-line no-eval
9486
return (0, eval)(source) as T;
9587
}
@@ -102,7 +94,7 @@ export interface SerovalJSON {
10294
m: number[],
10395
}
10496

105-
export function toJSON<T extends ServerValue>(
97+
export function toJSON<T>(
10698
source: T,
10799
options?: Partial<Options>,
108100
): SerovalJSON {
@@ -117,7 +109,7 @@ export function toJSON<T extends ServerValue>(
117109
};
118110
}
119111

120-
export async function toJSONAsync<T extends AsyncServerValue>(
112+
export async function toJSONAsync<T>(
121113
source: T,
122114
options?: Partial<Options>,
123115
): Promise<SerovalJSON> {
@@ -141,7 +133,7 @@ export function compileJSON(source: SerovalJSON): string {
141133
return finalize(serial, source.r, source.i, result);
142134
}
143135

144-
export function fromJSON<T extends AsyncServerValue>(source: SerovalJSON): T {
136+
export function fromJSON<T>(source: SerovalJSON): T {
145137
const serial = createSerializationContext({
146138
features: source.f,
147139
markedRefs: source.m,
@@ -150,3 +142,5 @@ export function fromJSON<T extends AsyncServerValue>(source: SerovalJSON): T {
150142
}
151143

152144
export default serialize;
145+
146+
export { createReference } from './tree/reference';

0 commit comments

Comments
 (0)