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 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.9039984824241858,-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-16T11:57:24.849Z"),map:j,set:k},h[3]=h,j.set("self",j),k.add(k).add(h),m.self=m,m))()
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.9039984824241858, -0, NaN, 1 / 0, -1 / 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-16T11:57:24.849Z"),
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":8,"i":0,"d":{"k":["number","string","boolean","null","undefined","bigint","array","regexp","date","map","set","self"],"v":[{"t":7,"i":1,"a":[{"t":0,"s":0.7047255726239685},{"t":0,"s":"-0"},{"t":0,"s":null},{"t":0,"s":"1/0"},{"t":0,"s":"-1/0"}]},{"t":7,"i":2,"a":[{"t":0,"s":"\"hello world\""},{"t":0,"s":"\"\\x3Cscript>Hello World\\x3C/script>\""}]},{"t":7,"i":3,"a":[{"t":0,"s":"!0"},{"t":0,"s":"!1"}]},{"t":0,"s":null},{"t":0,"s":"void 0"},{"t":1,"s":"9007199254740991n"},{"t":7,"i":4,"a":[null,null,null,{"t":2,"i":4},{"t":6,"i":5,"d":{"k":[{"t":0,"s":"\"hello\""},{"t":0,"s":"\"self\""},{"t":0,"s":"\"mutual\""}],"v":[{"t":0,"s":"\"world\""},{"t":2,"i":5},{"t":5,"i":6,"a":[{"t":0,"s":"\"hello\""},{"t":0,"s":"\"world\""},{"t":2,"i":6},{"t":2,"i":4}]}],"s":3}}]},{"t":4,"i":7,"s":"/[a-z0-9]+/i"},{"t":3,"i":8,"s":"2023-03-16T12:01:29.836Z"},{"t":2,"i":5},{"t":2,"i":6},{"t":2,"i":0}],"s":12}},"r":0,"i":true,"f":8191,"m":[4,5,6,0]}
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 various code output of `serialize`, `serializeAsync` and `compileJSON`.
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 iterables, uses `Symbol.iterator` instead (if `SymbolIterator` is not set).
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 iterables.
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
- - [`SymbolIterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator)
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