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 🙏

© 2025 – Pkg Stats / Ryan Hefner

magic-sandbox

v2.4.0

Published

A powerful library for creating HTML sandboxes that support multiple dynamic files without network requests.

Readme

magic-sandbox

A powerful library for creating HTML sandboxes that support multiple dynamic files without network requests.

npm version

Overview

The magic-sandbox library allows you to combine multiple HTML, CSS, JavaScript, and data files into a single HTML document that works in any modern browser. It intelligently intercepts network requests to provide a seamless experience for code demonstrations, educational tools, and interactive editors.

Originally extracted from the Blockbuilder Project created by Ian Johnson and described in the Medium post Architecting a Sandbox.

Features

  • File Bundling: Combines multiple files into a single HTML document
  • Request Interception: Intercepts XMLHttpRequest and fetch calls to load files without network requests
  • Automatic Processing: Inlines JavaScript and CSS files referenced in HTML
  • Protocol Fix: Converts protocol-less URLs (//example.com) to HTTPS
  • Multiple File Types: Supports HTML, CSS, JavaScript, XML, CSV, and other file formats

Installation

npm install magic-sandbox

Basic Usage

import { magicSandbox, FileCollection } from "magic-sandbox";

// Define your files collection
const files: FileCollection = {
  "index.html": `<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <h1>Hello, World!</h1>
    <script src="script.js"></script>
  </body>
</html>`,
  "styles.css": "h1 { color: blue; }",
  "script.js": 'console.log("Hello from JavaScript!");',
  "data.json": '{ "message": "This is data that can be fetched" }',
};

// Generate the HTML
const html = magicSandbox(files);

// Use in an iframe
document.getElementById("sandbox").setAttribute("srcdoc", html);

How It Works

magic-sandbox works by:

  1. Starting with your index.html file as a template
  2. Inlining referenced JavaScript and CSS files when possible
  3. Injecting a script that intercepts XMLHttpRequest and fetch calls
  4. Encoding the contents of other files for access by the interceptor
  5. Fixing protocol-less URLs to use HTTPS

The result is a single HTML document that can reference and load multiple files without making any network requests.

Advanced Example: Interactive Code Editor

import { magicSandbox, FileCollection } from "magic-sandbox";

// When files are updated in your editor
function updatePreview(files: FileCollection) {
  const iframe = document.getElementById("preview");
  const html = magicSandbox(files);
  iframe.setAttribute("srcdoc", html);
}

// Example editor setup
const editor = setupCodeEditor(); // Your editor initialization
editor.onChange((files: FileCollection) => {
  updatePreview(files);
});

Use Cases

  • Code Editors: Create live-preview code editors like CodePen or JSFiddle
  • Educational Platforms: Build interactive coding exercises for students
  • Demos & Examples: Showcase code examples with multiple files
  • Documentation: Provide runnable code samples in documentation

Browser Compatibility

Works in all modern browsers that support:

  • iframes with the srcdoc attribute
  • Fetch API
  • XMLHttpRequest
  • ES6 features

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT