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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cameronhunter/json-async-stringify

v1.0.0

Published

JSON stringify with support for an async replacer function

Readme

@cameronhunter/json-async-stringify

npm package npm downloads main branch status

JSON stringify with support for an async replacer function.

Why?

The standard JSON.stringify() accepts a replacer function to transform values before serialization, but it doesn't support async operations. This package provides an asyncStringify function that allows you to use an async replacer function, enabling you to perform asynchronous operations like fetching data from APIs, databases, or other async sources during the stringification process.

Installation

npm install @cameronhunter/json-async-stringify

Usage

Async Data Fetching

import { asyncStringify } from '@cameronhunter/json-async-stringify';

const userIds = [{ id: 1 }, { id: 2 }, { id: 3 }];

// Fetch full user details for each ID
const json = await asyncStringify(userIds, async (_key, value) => {
    if (typeof value === 'object' && value !== null && 'id' in value && !('name' in value)) {
        // Fetch user details from an API
        const response = await fetch(`https://api.example.com/users/${value.id}`);
        return response.json();
    }
    return value;
});

console.log(json);
// [{"id":1,"name":"Alice","email":"[email protected]"},{"id":2,"name":"Bob","email":"[email protected]"},...]

Converting Image URLs to Base64

import { asyncStringify } from '@cameronhunter/json-async-stringify';

const product = {
    name: 'Coffee Mug',
    price: 12.99,
    image: 'https://example.com/images/mug.jpg',
    thumbnail: 'https://example.com/images/mug-thumb.jpg',
};

// Convert image URLs to inline base64 data URLs
const json = await asyncStringify(product, async (key, value) => {
    // Check if this looks like an image URL
    if (typeof value === 'string' && /^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i.test(value)) {
        // Fetch the image
        const response = await fetch(value);
        const arrayBuffer = await response.arrayBuffer();
        const buffer = Buffer.from(arrayBuffer);

        // Convert to base64
        const base64 = buffer.toString('base64');
        const contentType = response.headers.get('content-type') || 'image/jpeg';

        // Return as data URL
        return `data:${contentType};base64,${base64}`;
    }
    return value;
});

console.log(json);
// {"name":"Coffee Mug","price":12.99,"image":"data:image/jpeg;base64,/9j/4AAQ...","thumbnail":"data:image/jpeg;base64,/9j/4AAQ..."}

API

asyncStringify(value, replacer, space?)

Converts a JavaScript value to a JSON string with support for async replacer functions.

Parameters

  • value (any): A JavaScript value, usually an object or array, to be converted.

  • replacer ((this: any, key: string, value: any) => Promise<any>): An async function that transforms values before serialization. The function receives:

    • key: The property key (empty string for the root value)
    • value: The property value
    • this: The parent object containing the value

    The function should return a Promise that resolves to the transformed value.

  • space (number | string, optional): Adds indentation, white space, and line break characters to make the output more readable:

    • If a number, indicates the number of space characters to use (max 10)
    • If a string, uses that string (or first 10 characters) for indentation

Returns

Promise<string>: A Promise that resolves to the JSON string representation of the value.

Throws

  • TypeError: If a circular reference is detected in the object structure
  • TypeError: If a BigInt value is encountered (BigInt values cannot be serialized to JSON)