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

writable-dom

v1.0.3

Published

Utility to stream HTML content into a live document.

Downloads

317

Readme

Installation

npm install writable-dom

How it works

This module allows you to write a stream of raw HTML chunks into an existing element in the DOM. Each chunk of HTML is handled in a way that is similar to how browsers process and display it.

Specifically blocking assets, including stylesheets and scripts, prevent adding newly parsed nodes to the target element. This means that there are no flashes of unstyled content, and that scripts execution order follows the same rules as normal.

On top of that this module will look ahead for additional assets to preload while it is blocked.

Examples

The following examples fetch an HTML stream and place the content into a #root container element.

import WritableDOMStream from "writable-dom";

const res = await fetch("http://ebay.com");
const myEl = document.getElementById("root");

await res.body
  .pipeThrough(new TextDecoderStream())
  .pipeTo(new WritableDOMStream(myEl));

The presented example relies on the WritableStreams API.

An alternative API is also available to use in case legacy browsers not implementing WritableStreams need to be supported.

The following is a version of the previous example implemented using the alternative API.

import writableDOM from "writable-dom";

const res = await fetch("https://ebay.com");
const decoder = new TextDecoder();
const reader = res.body.getReader();
const myEl = document.getElementById("root");

// create a writable object to stream data into.
const writable = writableDOM(myEl);

try {
  while (true) {
    const { value, done } = await reader.read();
    if (done) {
      // wait for blocking assets to finish.
      await writable.close();
      break;
    }

    // write partial chunks of html.
    writable.write(decoder.decode(value));
  }
} catch (err) {
  // ensure the writable dom stops if there is an error.
  writable.abort(err);
}

API

The module exposes a single default constructor function, once imported it can be used via one of the two following APIs.

WritableStream API

import writableDOMStream from "writable-dom";

new WritableDOMStream(
  target: ParentNode,
  previousSibling?: ChildNode | null
): WritableStream

By instantiating a new object via the new keyword on the constructor function, the generated object will be of type WritableStream<string>, which you can for example combine with your original stream via the pipeTo method.

You can also provide previousSibling to have all written HTML be placed after that node.

Writable API

import writableDOM from "writable-dom";

writableDOM(
  target: ParentNode,
  previousSibling?: ChildNode | null
): Writable

Calling the function directly creates a new Writable object which you can use to manually write HTML into the target element.

Again, you can also provide previousSibling to have all written HTML be placed after that node.

A Writable object provides the following methods:

  • write(html: string): void
    Writes a partial chunk of HTML content into the target element.

  • close(): Promise<void>
    Indicates that no additional HTML is going to be written. Returns a promise that will resolve when all blocking assets have loaded and the content is being displayed in the document. You should not call write after calling close.

  • abort(err: Error): void Prevents any additional HTML from being written into the document and aborts any blocking assets. You should not call write after calling abort.

Code of Conduct

This project adheres to the eBay Code of Conduct. By participating in this project you agree to abide by its terms.