npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ungap/structured-clone

v1.2.1

Published

A structuredClone polyfill

Downloads

91,944,408

Readme

structuredClone polyfill

Downloads build status Coverage Status

An env agnostic serializer and deserializer with recursion ability and types beyond JSON from the HTML standard itself.

  • Supported Types
    • not supported yet: Blob, File, FileList, ImageBitmap, ImageData, and ArrayBuffer, but typed arrays are supported without major issues, but u/int8, u/int16, and u/int32 are the only safely suppored (right now).
    • not possible to implement: the {transfer: []} option can be passed but it's completely ignored.
  • MDN Documentation
  • Serializer
  • Deserializer

Serialized values can be safely stringified as JSON too, and deserialization resurrect all values, even recursive, or more complex than what JSON allows.

Examples

Check the 100% test coverage to know even more.

// as default export
import structuredClone from '@ungap/structured-clone';
const cloned = structuredClone({any: 'serializable'});

// as independent serializer/deserializer
import {serialize, deserialize} from '@ungap/structured-clone';

// the result can be stringified as JSON without issues
// even if there is recursive data, bigint values,
// typed arrays, and so on
const serialized = serialize({any: 'serializable'});

// the result will be a replica of the original object
const deserialized = deserialize(serialized);

Global Polyfill

Note: Only monkey patch the global if needed. This polyfill works just fine as an explicit import: import structuredClone from "@ungap/structured-clone"

// Attach the polyfill as a Global function
import structuredClone from "@ungap/structured-clone";
if (!("structuredClone" in globalThis)) {
  globalThis.structuredClone = structuredClone;
}

// Or don't monkey patch
import structuredClone from "@ungap/structured-clone"
// Just use it in the file
structuredClone()

Note: Do not attach this module's default export directly to the global scope, whithout a conditional guard to detect a native implementation. In environments where there is a native global implementation of structuredClone() already, assignment to the global object will result in an infinite loop when globalThis.structuredClone() is called. See the example above for a safe way to provide the polyfill globally in your project.

Extra Features

There is no middle-ground between the structured clone algorithm and JSON:

  • JSON is more relaxed about incompatible values: it just ignores these
  • Structured clone is inflexible regarding incompatible values, yet it makes specialized instances impossible to reconstruct, plus it doesn't offer any helper, such as toJSON(), to make serialization possible, or better, with specific cases

This module specialized serialize export offers, within the optional extra argument, a lossy property to avoid throwing when incompatible types are found down the road (function, symbol, ...), so that it is possible to send with less worrying about thrown errors.

// as default export
import structuredClone from '@ungap/structured-clone';
const cloned = structuredClone(
  {
    method() {
      // ignored, won't be cloned
    },
    special: Symbol('also ignored')
  },
  {
    // avoid throwing
    lossy: true,
    // avoid throwing *and* looks for toJSON
    json: true
  }
);

The behavior is the same found in JSON when it comes to Array, so that unsupported values will result as null placeholders instead.

toJSON

If lossy option is not enough, json will actually enforce lossy and also check for toJSON method when objects are parsed.

Alternative, the json exports combines all features:

import {stringify, parse} from '@ungap/structured-clone/json';

parse(stringify({any: 'serializable'}));