seroval 0.5.0 → 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 +59 -9
- package/dist/cjs/development/index.cjs +372 -202
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +372 -202
- 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 -6
- 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 +25 -7
- 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 +37 -48
- package/src/index.ts +10 -16
- package/src/tree/async.ts +99 -74
- package/src/tree/deserialize.ts +32 -21
- package/src/tree/primitives.ts +46 -24
- package/src/tree/reference.ts +61 -0
- package/src/tree/serialize.ts +93 -86
- package/src/tree/sync.ts +104 -66
- package/src/tree/types.ts +32 -8
- package/src/tree/web-api.ts +42 -0
- package/src/types.ts +6 -1
package/README.md
CHANGED
|
@@ -59,11 +59,11 @@ console.log(result);
|
|
|
59
59
|
Output (as a string):
|
|
60
60
|
|
|
61
61
|
```js
|
|
62
|
-
((h,j,k,m)=>(m={number:[0.
|
|
62
|
+
((h,j,k,m)=>(m={number:[0.28952097444015235,-0,NaN,1/0,-1/0],string:["hello world","\x3Cscript>Hello World\x3C/script>"],boolean:[!0,!1],null:null,undefined:void 0,bigint:9007199254740991n,array:h=[,,,,j=new Map([["hello","world"],["mutual",k=new Set(["hello","world"])]])],regexp:/[a-z0-9]+/i,date:new Date("2023-03-22T02:53:41.129Z"),map:j,set:k},h[3]=h,j.set("self",j),k.add(k).add(h),m.self=m,m))()
|
|
63
63
|
|
|
64
64
|
// Formatted for readability
|
|
65
65
|
((h, j, k, m) => (m = {
|
|
66
|
-
number: [0.
|
|
66
|
+
number: [0.28952097444015235, -0, NaN, 1 / 0, -1 / 0],
|
|
67
67
|
string: ["hello world", "\x3Cscript>Hello World\x3C/script>"],
|
|
68
68
|
boolean: [!0, !1],
|
|
69
69
|
null: null,
|
|
@@ -74,7 +74,7 @@ Output (as a string):
|
|
|
74
74
|
["mutual", k = new Set(["hello", "world"])]
|
|
75
75
|
])],
|
|
76
76
|
regexp: /[a-z0-9]+/i,
|
|
77
|
-
date: new Date("2023-03-
|
|
77
|
+
date: new Date("2023-03-22T02:53:41.129Z"),
|
|
78
78
|
map: j,
|
|
79
79
|
set: k
|
|
80
80
|
}, h[3] = h, j.set("self", j), k.add(k).add(h), m.self = m, m))()
|
|
@@ -149,7 +149,7 @@ console.log(JSON.stringify(result));
|
|
|
149
149
|
```
|
|
150
150
|
|
|
151
151
|
```json
|
|
152
|
-
{"t":{"t":
|
|
152
|
+
{"t":{"t":16,"i":0,"d":{"k":["number","string","boolean","null","undefined","bigint","array","regexp","date","map","set","self"],"v":[{"t":15,"i":1,"l":5,"a":[{"t":0,"s":0.4350045546286634},{"t":5},{"t":8},{"t":6},{"t":7}]},{"t":15,"i":2,"l":2,"a":[{"t":1,"s":"hello world"},{"t":1,"s":"\\x3Cscript>Hello World\\x3C/script>"}]},{"t":15,"i":3,"l":2,"a":[{"t":2,"s":true},{"t":2,"s":false}]},{"t":3},{"t":4},{"t":9,"s":"9007199254740991"},{"t":15,"i":4,"l":5,"a":[null,null,null,{"t":10,"i":4},{"t":14,"i":5,"d":{"k":[{"t":1,"s":"hello"},{"t":1,"s":"self"},{"t":1,"s":"mutual"}],"v":[{"t":1,"s":"world"},{"t":10,"i":5},{"t":13,"i":6,"l":4,"a":[{"t":1,"s":"hello"},{"t":1,"s":"world"},{"t":10,"i":6},{"t":10,"i":4}]}],"s":3}}]},{"t":12,"i":7,"c":"[a-z0-9]+","m":"i"},{"t":11,"i":8,"s":"2023-03-22T02:55:33.504Z"},{"t":10,"i":5},{"t":10,"i":6},{"t":10,"i":0}],"s":12}},"r":0,"i":true,"f":8191,"m":[4,5,6,0]}
|
|
153
153
|
```
|
|
154
154
|
|
|
155
155
|
Then you can feed it to `fromJSON`:
|
|
@@ -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`:
|
|
@@ -208,6 +239,7 @@ The following values are the only values accepted by `seroval`:
|
|
|
208
239
|
- `Date`
|
|
209
240
|
- `Map`
|
|
210
241
|
- `Set`
|
|
242
|
+
- `Object.create(null)`
|
|
211
243
|
- `TypedArray`
|
|
212
244
|
- `Int8Array`
|
|
213
245
|
- `Int16Array`
|
|
@@ -230,11 +262,15 @@ The following values are the only values accepted by `seroval`:
|
|
|
230
262
|
- `URIError`
|
|
231
263
|
- `Promise` (with `serializeAsync`)
|
|
232
264
|
- [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
|
|
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)
|
|
233
268
|
- Cyclic references (both self and mutual)
|
|
269
|
+
- Isomorphic references (a reference that exist on both the serializer and deserializer side)
|
|
234
270
|
|
|
235
271
|
## Compat
|
|
236
272
|
|
|
237
|
-
`serialize`, `serializeAsync`, `toJSON` and `toJSONAsync` can accept a `{ disabledFeatures: number }` option. The `disabledFeatures` defines how
|
|
273
|
+
`serialize`, `serializeAsync`, `toJSON` and `toJSONAsync` can accept a `{ disabledFeatures: number }` option. The `disabledFeatures` defines how the output code would look like when serialized by `serialize`, `serializeAsync` and `compileJSON`.
|
|
238
274
|
|
|
239
275
|
```js
|
|
240
276
|
import { serialize, Feature } from 'seroval';
|
|
@@ -281,31 +317,45 @@ By default, all feature flags are enabled. The following are the feature flags a
|
|
|
281
317
|
- [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError)
|
|
282
318
|
- Compiles down to `Error` instead.
|
|
283
319
|
- [`ArrayPrototypeValues`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values)
|
|
284
|
-
- Used for
|
|
320
|
+
- Used for `Iterable`, uses `Symbol.iterator` instead.
|
|
285
321
|
- [`ArrowFunction`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
|
|
286
|
-
- Uses function expressions for top-level and for deferred Promise values
|
|
287
|
-
- method shorthands (if `MethodShortand` is not set) or function expressions for
|
|
322
|
+
- Uses function expressions for top-level and for deferred `Promise` values
|
|
323
|
+
- method shorthands (if `MethodShortand` is not set) or function expressions for `Iterable`.
|
|
288
324
|
- [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
|
|
289
325
|
- Throws when attempted to use, includes `BigIntTypedArray`
|
|
326
|
+
- Disables use of `BigInt`, `BigInt64Array` and `BigUint64Array`
|
|
290
327
|
- [`ErrorPrototypeStack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack)
|
|
291
328
|
- Skipped when detected.
|
|
329
|
+
- Affects both `Error` and `AggregateError`
|
|
292
330
|
- [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
|
|
293
331
|
- Throws when attempted to use.
|
|
332
|
+
- Disables serialization of `Map`
|
|
294
333
|
- [`MethodShorthand`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions)
|
|
295
334
|
- Uses function expressions instead.
|
|
335
|
+
- Only affects `Iterable`
|
|
296
336
|
- [`ObjectAssign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
|
|
297
337
|
- Uses manual object assignments instead.
|
|
338
|
+
- Affects `Iterable`, `Error`, `AggregateError` and `Object.create(null)`
|
|
298
339
|
- [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
|
299
340
|
- Throws when attempted to use in `serializeAsync` and `toJSONAsync`.
|
|
341
|
+
- Disables serialization of `Promise`
|
|
300
342
|
- [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
|
|
301
343
|
- Throws when attempted to use.
|
|
302
|
-
-
|
|
344
|
+
- Disables serialization of `Set`
|
|
345
|
+
- [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
|
|
303
346
|
- Throws when attempted to use.
|
|
347
|
+
- This disables serialization of well-known symbols and `Iterable`.
|
|
304
348
|
- [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
|
|
305
349
|
- Throws when attempted to use.
|
|
350
|
+
- Disables serialization of `TypedArray`
|
|
306
351
|
- [`BigIntTypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array)
|
|
307
352
|
- Throws when attempted to use
|
|
308
353
|
- Also throws if `BigInt` is disabled.
|
|
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)
|
|
309
359
|
|
|
310
360
|
## Sponsors
|
|
311
361
|
|