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

@substrate-system/hyperstream

v0.0.3

Published

Stream html by CSS selector.

Readme

hyperstream

tests types module semantic versioning Common Changelog install size license

Use CSS selectors & HTML as a template language.

A re-implementation of the classic @substack module, using Web Streams for compatibility with browsers, Cloudflare Workers, and Deno.

Install

npm i -S @substrate-system/hyperstream

Example

Take some template HTML, and transform it using CSS selectors.

import hyperstream from '@substrate-system/hyperstream'
import { createReadStream, createWriteStream } from 'node:fs'
import { Readable, Writable } from 'node:stream'

const hs = hyperstream({
    '#title': 'Hello World',
    '.content': { _html: '<p>Injected content</p>' }
})

const destination = Writable.toWeb(createWriteStream('./output.html'))

Readable.toWeb(createReadStream('./template.html'))
    .pipeThrough(hs.transform)
    .pipeTo(destination)

Browser Example

Because this is using Web Streams, we can run it in a browser too. A browser demo is in /example_browser.

npm start

Streams as Values

Stream a template file through hyperstream, inject streams by selector, then stream the transformed output into result.html. The selector values are streams here. You can also pass in string as values.

import hyperstream from '@substrate-system/hyperstream'
import { S } from '@substrate-system/stream'
import { createWriteStream } from 'node:fs'
import { Writable } from 'node:stream'
import { open } from 'node:fs/promises'

async function run ():Promise<void> {
    const template = await open('./template.html', 'r')
    const nav = await open('./partials/nav.html', 'r')
    const footer = await open('./partials/footer.html', 'r')

    try {
        // setup template logic
        const hs = hyperstream({
            '#main-nav': nav.readableWebStream(),
            '#main-footer': footer.readableWebStream(),
            '#build-time': S.from([new Date().toISOString()]).toStream()
        })

        // build the template
        await template.readableWebStream()
            .pipeThrough(hs.transform)
            .pipeTo(Writable.toWeb(createWriteStream('./result.html')))
    } finally {
        await Promise.allSettled([
            template.close(),
            nav.close(),
            footer.close(),
        ])
    }
}

await run()

Strings

Use strings as selector values, and use a string as the template.

// our template is a string, not stream
import { fromString } from '@substrate-system/hyperstream'

const template = `<html>
    <head>
        <title id="title"></title>
    </head>
    <body>
        <div class="content"></div>
    </body>
</html>`

const result = await fromString(template, {
    '#title': 'Hello World',
    '.content': { _html: '<p>This is the content</p>' }
})

console.log(result)

Output

<html>
    <head><title id="title">Hello World</title></head>
    <body>
        <div class="content"><p>This is the content</p></div>
    </body>
</html>

TransformStream API

Use the TransformStream interface:

import hyperstream from '@substrate-system/hyperstream'
import { S } from '@substrate-system/stream'

const hs = hyperstream({
    '#title': 'Hello World',
    '.content': { _html: '<p>This is the content</p>' }
})

const template = `<html>
    <head><title id="title"></title></head>
    <body>
        <div class="content"></div>
    </body>
    </html>
`

S.from([template]).toStream().pipeTo(hs.writable)

const result = await hs.asString()
console.log(result)

Append and prepend

Use _appendHtml, _prependHtml, _append (text), or _prepend (text) to add content before or after existing content:

import { fromString } from '@substrate-system/hyperstream'

// takes a string as input
// returns a string as output
const result = await fromString(
    '<ul class="list"><li>First</li></ul><span class="greeting">World</span>',
    {
        '.list': { _appendHtml: '<li>New item</li>' },
        '.greeting': { _prepend: 'Hello, ' }
    }
)

console.log(result)

Output:

<ul class="list"><li>First</li><li>New item</li></ul><span class="greeting">Hello, World</span>

Streams

Pass a ReadableStream as the value to insert streamed content:

import hyperstream from '@substrate-system/hyperstream'
import fs from 'node:fs'
import { S } from '@substrate-system/stream'

// Helper to convert a file to a ReadableStream
function fileToStream(path: string): ReadableStream<Uint8Array> {
    const content = fs.readFileSync(path)
    return S.from([new Uint8Array(content)]).toStream()
}

const hs = hyperstream({
    '#a': fileToStream('./content-a.html'),
    '#b': fileToStream('./content-b.html')
})

// Process template
const template = fileToStream('./template.html')
const output = template.pipeThrough(hs.transform)
const chunks = await S(output).toArray()
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0)
const bytes = new Uint8Array(totalLength)
let offset = 0
for (const chunk of chunks) {
    bytes.set(chunk, offset)
    offset += chunk.length
}

const decoder = new TextDecoder()
console.log(decoder.decode(bytes))

Attribute manipulation

Set attributes directly, or use append/prepend to modify existing values:

import { fromString } from '@substrate-system/hyperstream'

const result = await fromString(
    '<input><button class="btn">Click</button><a>Link</a>',
    {
        'input': { value: 'default', placeholder: 'Enter text...' },
        '.btn': { class: { append: ' active' } },
        'a': { href: 'https://example.com' }
    }
)

console.log(result)

Output:

<input value="default" placeholder="Enter text...">
<button class="btn active">Click</button>
<a href="https://example.com">Link</a>

Transform functions

Pass a function to transform the existing content:

import { fromString } from '@substrate-system/hyperstream'

const result = await fromString(
    '<span class="count">41</span><span class="upper">hello</span>',
    {
        '.count': (html) => String(parseInt(html) + 1),
        '.upper': (html) => html.toUpperCase()
    }
)

console.log(result)

Output:

<span class="count">42</span><span class="upper">HELLO</span>

API

hyperstream(config)

Create a Hyperstream instance with the given configuration.

Returns an object with:

  • transform: A TransformStream<Uint8Array, Uint8Array> for piping
  • readable: The readable side of the transform
  • writable: The writable side of the transform

fromString(html, config)

Convenience function to process HTML from a string.

Returns a Promise<string> with the processed HTML.

createHyperstream(config)

Create a raw TransformStream<Uint8Array, Uint8Array>.

processHyperstream(input, config)

Process a ReadableStream<Uint8Array> and return a Promise<Uint8Array>.

Configuration

The config object maps CSS selectors to values:

  • String/Number: Replace element content
  • { _html: string }: Replace content with raw HTML
  • { _text: string }: Replace content with HTML-escaped text
  • { _appendHtml: string }: Append raw HTML to content
  • { _prependHtml: string }: Prepend raw HTML to content
  • { _append: string }: Append HTML-escaped text
  • { _prepend: string }: Prepend HTML-escaped text
  • { attr: value }: Set attribute value
  • { attr: { append: string } }: Append to attribute
  • { attr: { prepend: string } }: Prepend to attribute
  • ReadableStream: Replace content with stream output
  • (html) => string: Transform existing content
  • null: Skip this selector