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

stream-orator

v0.0.25

Published

Add streaming support to DOM elements

Downloads

168

Readme

stream-orator

NPM version Playwright Tests How big is this package in your project?

This package contains a utility function, streamOrator, that does a fetch and pipes the response to a DOM element.

Example:

<details>
    <summary>HTML Specs</summary>
    <div id=test></div>
</details>
<script type=module>
    import {streamOrator} from 'node_modules/stream-orator/StreamOrator.js';
    streamOrator('https://html.spec.whatwg.org/', {}, test);
</script>

The second parameter is the reqInit object (fetch options).

What this does: It streams HTML directly from site: https://html.spec.whatwg.org/ to the div element with id test. The third parameter is expected to be a reference to an HTML Element (not a querySelector string).

So basically, streamOrator is the fetch function, with a third parameter that specifies the target.

This package is a core package that can enable us to to start simulating the power of an iframe, but within the context of our document.

The first obstacle to fully achieving this is adjusting the url's for things like hyperlinks, images, script references, etc.

The most effective utility stream-orator provides is an event to subscribe to, giving access to the root node used during the import (which might be newly constructed shadowRoot):

[TODO] provide an example.

There is an additional optional "options" parameter, where we can utilize shadowDOM and other special settings:

<details>
    <summary>HTML Specs</summary>
    <div id=test></div>
</details>
<script type=module>
    import {streamOrator} from '../StreamOrator.js';
    streamOrator('https://html.spec.whatwg.org/', {}, test, {shadowRoot: 'open', encoding: 'UTF-16'});
</script>

CORS

Many sites are not so enlightened as whatwg, and prevent cross origin requests from passing through.

There are various services that aim to provide a gateway into public sites, serving as a reverse proxy, which unfortunately seem to die at an alarming rate. One such service is corslet by yours truly:

<details>
    <summary>Supreme Court</summary>
    <div id=test></div>
</details>
<script type=module>
    import {streamOrator} from '../StreamOrator.js';
    streamOrator('https://corslet.bahrus.workers.dev/?href=https%3A%2F%2Fwww.supremecourt.gov%2Fabout%2Fmembers_text.aspx&lhs=%3Cdiv+id%3D%22pagemaindiv%22+class%3D%22col-md-9%22%3E&rhs=script&exclude_rhs=on&ts=2022-12-06T00%3A26%3A47.783Z&wrapper=%3Cdiv%3E%7C%3C%2Fdiv%3E&ua=', {}, test);
</script>

Access to the stream [One Hundo Untested]

If access to the stream chunks is needed, including modifying the chunks, a little more ceremony is needed:

<details>
    <summary>HTML Specs</summary>
    <div id=test></div>
</details>
<script type=module>
    import {StreamOrator} from '../stream-orator.js';
    const so =  new StreamOrator(target, {toShadow: true});
    so.addEventListener('new-chunk', e => {
        const chunk = e.detail.chunk;
        //search for a string.  If the first part of the string you 
        //are searching for is found at the end of the chunk, 
        //may need to ask the orator to wait before flushing to the stream.
        e.detail.flush = false;
    });
    await so.fetch('https://html.spec.whatwg.org/', {}); //fetch is happening!
</script>

Viewing Locally

  1. Install git.
  2. Fork/clone this repo.
  3. Install node.
  4. Open command window to folder where you cloned this repo.
  5. npm install

  6. npm run serve

  7. Open http://localhost:8000/demo/dev in a modern browser.

Importing in ES Modules:

import 'stream-orator/StreamOrator.js';

CDN

import 'https://esm.run/stream-orator/StreamOrator.js';