bare-structured-clone
Structured cloning algorithm for JavaScript
bare-structured-clone — Structured cloning algorithm for JavaScript. It is a native addon and requires Bare >=1.2.0.
npm i bare-structured-cloneUsage
const structuredClone = require('bare-structured-clone')
const copy = structuredClone({ hello: 'world' })
const buffer = new ArrayBuffer(4)
const transferred = structuredClone(buffer, { transfer: [buffer] })To install structuredClone as a global, require the global submodule:
require('bare-structured-clone/global')
const copy = structuredClone({ hello: 'world' })API
DataCloneError
DataCloneError.DataCloneError.ALREADY_TRANSFERRED(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code ALREADY_TRANSFERRED.
DataCloneError.DataCloneError.INVALID_INTERFACE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code INVALID_INTERFACE.
DataCloneError.DataCloneError.INVALID_REFERENCE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code INVALID_REFERENCE.
DataCloneError.DataCloneError.INVALID_VERSION(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code INVALID_VERSION.
DataCloneError.DataCloneError.UNSERIALIZABLE_TYPE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code UNSERIALIZABLE_TYPE.
DataCloneError.DataCloneError.UNTRANSFERABLE_TYPE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code UNTRANSFERABLE_TYPE.
Functions
structuredClone
structuredClone<T extends SerializableValue | TransferableValue>(value: T, opts?: StructuredCloneOptions): TClone value by serializing and then deserializing it. opts may include a transfer list and an interfaces list.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | T | — | The value to clone. |
opts? | StructuredCloneOptions | — | Options carrying the optional transfer and interfaces lists. |
Returns T — A deep copy of value, with any transferred objects detached from the original.
Throws
UNSERIALIZABLE_TYPE—value, or a value it references, is of a type that cannot be serialized (e.g. a function, a symbol, or a detachedArrayBuffer).UNTRANSFERABLE_TYPE— a value in thetransferlist cannot be transferred.ALREADY_TRANSFERRED— a value in thetransferlist has already been transferred.INVALID_INTERFACE— a serializable or transferable value has an interface that is not present in theinterfaceslist.
Types
StructuredCloneOptions
interface StructuredCloneOptions {
transfer: TransferableValue[]
interfaces: (SerializableConstructor | TransferableConstructor)[]
}SerializableConstructor
interface SerializableConstructor<T = unknown> {
new (...args: any[]): Serializable<T>
[symbols.deserialize](serialized: T): Serializable<T>
[symbols.interface]?: symbol
}TransferableConstructor
interface TransferableConstructor<T = unknown> {
new (...args: any[]): Transferable<T>
[symbols.attach](serialized: T): Transferable<T>
[symbols.interface]?: symbol
}Classes
Serializable
Source
class Serializable {
}Transferable
Source
class Transferable {
detached: boolean
}bare-structured-clone/constants
Constants and variables
constants
constants: {
VERSION: number
type: {
// Primitive types
UNDEFINED: 0
NULL: 1
TRUE: 2
FALSE: 3
NUMBER: 4
BIGINT: 5
STRING: 6
// Builtin objects
DATE: 7
REGEXP: 8
ERROR: 9
// Builtin binary data objects
ARRAYBUFFER: 10
RESIZABLEARRAYBUFFER: 11
SHAREDARRAYBUFFER: 12
GROWABLESHAREDARRAYBUFFER: 13
TYPEDARRAY: 14
DATAVIEW: 15
// Builtin composite objects
MAP: 16
SET: 17
ARRAY: 18
OBJECT: 19
// Object references
REFERENCE: 20
// Object transfers
TRANSFER: 21
// Platform objects
URL: 22
BUFFER: 23
EXTERNAL: 24
SERIALIZABLE: 25
TRANSFERABLE: 26
typedarray: {
UINT8ARRAY: 1
UINT8CLAMPEDARRAY: 2
INT8ARRAY: 3
UINT16ARRAY: 4
INT16ARRAY: 5
UINT32ARRAY: 6
INT32ARRAY: 7
BIGUINT64ARRAY: 8
BIGINT64ARRAY: 9
FLOAT16ARRAY: 12
FLOAT32ARRAY: 10
FLOAT64ARRAY: 11
}
error: {
AGGREGATE: 1
EVAL: 2
RANGE: 3
REFERENCE: 4
SYNTAX: 5
TYPE: 6
URI: 7
}
}
}Numeric tags used in serialized values. Also available as require('bare-structured-clone/constants').
bare-structured-clone/errors
DataCloneError
DataCloneError.DataCloneError.ALREADY_TRANSFERRED(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code ALREADY_TRANSFERRED.
DataCloneError.DataCloneError.INVALID_INTERFACE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code INVALID_INTERFACE.
DataCloneError.DataCloneError.INVALID_REFERENCE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code INVALID_REFERENCE.
DataCloneError.DataCloneError.INVALID_VERSION(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code INVALID_VERSION.
DataCloneError.DataCloneError.UNSERIALIZABLE_TYPE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code UNSERIALIZABLE_TYPE.
DataCloneError.DataCloneError.UNTRANSFERABLE_TYPE(msg: string): DataCloneError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns DataCloneError — A new DataCloneError with code UNTRANSFERABLE_TYPE.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.