seroval 0.5.1 → 0.7.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 +51 -1
- package/dist/cjs/development/index.cjs +762 -228
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +762 -228
- 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 +14 -9
- package/dist/types/index.d.ts +9 -9
- package/dist/types/string.d.ts +3 -0
- 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 +7 -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 +73 -12
- package/dist/types/tree/web-api.d.ts +6 -0
- package/dist/types/types.d.ts +2 -1
- package/package.json +4 -3
- package/src/compat.ts +2 -1
- package/src/context.ts +19 -12
- package/src/index.ts +10 -16
- package/src/{quote.ts → string.ts} +27 -30
- package/src/tree/async.ts +164 -62
- package/src/tree/deserialize.ts +195 -67
- package/src/tree/primitives.ts +108 -22
- package/src/tree/reference.ts +61 -0
- package/src/tree/serialize.ts +236 -59
- package/src/tree/sync.ts +160 -55
- package/src/tree/types.ts +139 -14
- package/src/tree/web-api.ts +92 -0
- package/src/types.ts +6 -1
- package/dist/types/quote.d.ts +0 -2
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`:
|
|
@@ -209,6 +240,8 @@ The following values are the only values accepted by `seroval`:
|
|
|
209
240
|
- `Map`
|
|
210
241
|
- `Set`
|
|
211
242
|
- `Object.create(null)`
|
|
243
|
+
- `ArrayBuffer`
|
|
244
|
+
- `DataView`
|
|
212
245
|
- `TypedArray`
|
|
213
246
|
- `Int8Array`
|
|
214
247
|
- `Int16Array`
|
|
@@ -229,10 +262,19 @@ The following values are the only values accepted by `seroval`:
|
|
|
229
262
|
- `SyntaxError`
|
|
230
263
|
- `TypeError`
|
|
231
264
|
- `URIError`
|
|
232
|
-
- `Promise` (with `serializeAsync`)
|
|
265
|
+
- `Promise` (with `serializeAsync` and `toJSONAsync`)
|
|
233
266
|
- [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
|
|
234
267
|
- [Well-known symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#static_properties)
|
|
268
|
+
- Web API
|
|
269
|
+
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)
|
|
270
|
+
- [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
|
271
|
+
- [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) (with `serializeAsync` and `toJSONAsync`)
|
|
272
|
+
- [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) (with `serializeAsync` and `toJSONAsync`)
|
|
273
|
+
- [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
|
|
274
|
+
- [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)
|
|
275
|
+
- If `FormData` has a `Blob`/`File` entry, it can only be serialized with `serializeAsync` and `toJSONAsync`
|
|
235
276
|
- Cyclic references (both self and mutual)
|
|
277
|
+
- Isomorphic references (a reference that exist on both the serializer and deserializer side)
|
|
236
278
|
|
|
237
279
|
## Compat
|
|
238
280
|
|
|
@@ -318,6 +360,14 @@ By default, all feature flags are enabled. The following are the feature flags a
|
|
|
318
360
|
- Throws when attempted to use
|
|
319
361
|
- Also throws if `BigInt` is disabled.
|
|
320
362
|
- Disables serialization of `BigInt64Array` and `BigUint64Array`
|
|
363
|
+
- `WebAPI`
|
|
364
|
+
- Throws and disables the following usage:
|
|
365
|
+
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)
|
|
366
|
+
- [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
|
367
|
+
- [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
|
|
368
|
+
- [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File)
|
|
369
|
+
- [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
|
|
370
|
+
- [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)
|
|
321
371
|
|
|
322
372
|
## Sponsors
|
|
323
373
|
|