seroval 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/dist/cjs/development/index.cjs +236 -72
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +236 -72
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -3
- package/dist/types/compat.d.ts +3 -2
- package/dist/types/context.d.ts +3 -4
- package/dist/types/index.d.ts +9 -9
- package/dist/types/tree/async.d.ts +1 -2
- package/dist/types/tree/deserialize.d.ts +1 -2
- package/dist/types/tree/primitives.d.ts +4 -3
- package/dist/types/tree/reference.d.ts +6 -0
- package/dist/types/tree/sync.d.ts +1 -2
- package/dist/types/tree/types.d.ts +23 -5
- package/dist/types/tree/web-api.d.ts +4 -0
- package/dist/types/types.d.ts +2 -1
- package/package.json +3 -3
- package/src/compat.ts +2 -1
- package/src/context.ts +5 -6
- package/src/index.ts +10 -16
- package/src/tree/async.ts +82 -59
- package/src/tree/deserialize.ts +31 -20
- package/src/tree/primitives.ts +25 -3
- package/src/tree/reference.ts +61 -0
- package/src/tree/serialize.ts +30 -23
- package/src/tree/sync.ts +88 -52
- package/src/tree/types.ts +29 -5
- package/src/tree/web-api.ts +42 -0
- package/src/types.ts +6 -1
package/README.md
CHANGED
|
@@ -186,6 +186,37 @@ console.log(await deserialize(result)); // 100
|
|
|
186
186
|
> **Note**
|
|
187
187
|
> `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.
|
|
188
188
|
|
|
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
|
+
|
|
189
220
|
## Supports
|
|
190
221
|
|
|
191
222
|
The following values are the only values accepted by `seroval`:
|
|
@@ -232,7 +263,10 @@ The following values are the only values accepted by `seroval`:
|
|
|
232
263
|
- `Promise` (with `serializeAsync`)
|
|
233
264
|
- [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
|
|
234
265
|
- [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)
|
|
235
268
|
- Cyclic references (both self and mutual)
|
|
269
|
+
- Isomorphic references (a reference that exist on both the serializer and deserializer side)
|
|
236
270
|
|
|
237
271
|
## Compat
|
|
238
272
|
|
|
@@ -318,6 +352,10 @@ By default, all feature flags are enabled. The following are the feature flags a
|
|
|
318
352
|
- Throws when attempted to use
|
|
319
353
|
- Also throws if `BigInt` is disabled.
|
|
320
354
|
- 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)
|
|
321
359
|
|
|
322
360
|
## Sponsors
|
|
323
361
|
|