Skip to content

Latest commit

 

History

History
138 lines (119 loc) · 14.3 KB

File metadata and controls

138 lines (119 loc) · 14.3 KB

Compatibility

All serialization methods can accept a { disabledFeatures: number } option. This option influences how the serialization will process and emit a value.

import { serialize, Feature } from 'seroval';

const y = Object.create(null);
y.self = y;
y.example = 'Hello World';

function serializeWithTarget(value, disabledFeatures) {
  const result = serialize(value, {
    disabledFeatures,
  });
  console.log(result);
}

serializeWithTarget(y, Feature.ObjectAssign);
serializeWithTarget(y, 0);
(function(h){return (h=(h=Object.create(null),h.example="Hello World",h),h.self=h,h)})()
(h=>(h=Object.assign(Object.create(null),{example:"Hello World"}),h.self=h,h))()

disabledFeatures uses bit flags for faster checking, so if you need to disable multiple features, you can use the bitwise OR symbol (|).

Here's an ES2017 flag:

import { serialize, Feature } from 'seroval';

const ES2017FLAG = 
  Feature.AggregateError // ES2021
  | Feature.BigIntTypedArray // ES2020;

serialize(myValue, {
  disabledFeatures: ES2017FLAG,
})

By default, all feature flags are enabled. The following are the feature flags and their behavior when disabled:

  • AggregateError
    • Compiles down to Error instead.
  • ErrorPrototypeStack
    • Skipped when detected.
    • Affects both Error and AggregateError
  • ObjectAssign
    • Uses manual object assignments instead.
    • Affects Iterable, Error, AggregateError and Object.create(null)
  • BigIntTypedArray
    • Disables serialization of BigInt64Array and BigUint64Array
  • RegExp
    • Disables serialization (and deserialization) of RegExp

Supported Types

  • sync = serialize, toJSON, crossSerialize, toCrossJSON
  • async = serializeAsync, toJSONAsync, crossSerializeAsync, toCrossJSONAsync
  • streaming = crossSerializeStream, toCrossJSONStream, Serializer
Type sync async streaming
NaN
Infinity
-Infinity
-0
number
string
boolean
null
undefined
bigint
Array
sparse (holey) Arrays
Object
RegExp 1 1 1
Date
Map
Set
Object.create(null)
ArrayBuffer
DataView
Int8Array
Int16Array
Int32Array
Uint8Array
Uint16Array
Uint32Array
Uint8ClampedArray
Float32Array
Float64Array
BigInt64Array 2 2 2
BigUint64Array 2 2 2
Error 3 3 3
AggregateError 34 34 34
EvalError 3 3 3
RangeError 3 3 3
ReferenceError 3 3 3
SyntaxError 3 3 3
TypeError 3 3 3
URIError 3 3 3
Promise
Iterable
Well-known symbols
AsyncIterable
Built-in streaming primitive
Cyclic references
Isomorphic references

seroval-plugins/web

Type sync async streaming
URL
URLSearchParams
Blob 5
File 5
Headers
FormData 6 6
ReadableStream
Request
Response
Event
CustomEvent
DOMException
ImageData
AbortSignal

Footnotes

  1. Feature.RegExp must be enabled, otherwise throws an SerovalUnsupportedTypeError and SerovalUnsupportedNodeError. 2 3

  2. Feature.BigIntTypedArray must be enabled, otherwise throws an SerovalUnsupportedTypeError. 2 3 4 5 6

  3. Feature.ErrorPrototypeStack must be enabled if serializing Error.prototype.stack is desired. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  4. Feature.AggregateError must be enabled, otherwise AggregateError is serialized into an Error instance. 2 3

  5. Due to the nature of Blob and File being an async type (in that it returns a Promise-based serializable data) while having a sync constructor, it cannot be represented in a way that the type is consistent to its original declaration. 2

  6. FormData is partially supported if it doesn't contain any Blob or File instances. 2