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

@braid-protocol/client-raw

v1.0.0

Published

This library is a simple reference implementation of the client side of the [braid protocol](https://github.com/braid-org/braid-spec/).

Downloads

5

Readme

Braid protocol raw client

This library is a simple reference implementation of the client side of the braid protocol.

This code parses and returns updates sent over a braid subscription. Each update in a braid subscription is either:

  • A snapshot (a replacement of the entire document contents), or
  • A set of patches. Each patch has a patchType specifying the type of the patch, headers and content.

Unlike the high level braid library, this implementation does not try to interpret or apply any received patches. Most applications will probably want to use the higher level client library - which returns a stream of document values.

This library (currently) offers no assistance in making it easy to modify server data.

Getting started

npm install --save @braid-protocol/client-raw

Then:

const { subscribe } = require('@braid-protocol/client-raw')

;(async () => {
  const { updates } = await subscribe('http://localhost:2001/time')
  for await (const { type, headers, value, patches } of updates) {
    if (type === 'snapshot') {
      console.log('new value:', value)
    } else {
      console.log('got patches:', patches)
    }
  }
})()

This code will work from either a web browser or from nodejs.

API

This module exposes 2 methods: subscribeRaw and subscribe.

  • subscribeRaw returns updates using raw Uint8Arrays, and makes no attempt to parse any of the braid-specific headers
  • subscribe will parse out version, content-type and patch-type headers from updates. It will also attempt to convert the contents of patches and updates into JSON objects or strings, based on the specified content-type of responses.

subscribeRaw

subscribeRaw(url: string, opts?) => Promise<{streamHeaders, updates}>

subscribeRaw initiates an HTTP request to the specified URL and attempts to start a subscription.

The options field is optional. If present, it can contain the following fields:

  • reqHeaders: An object containing additional headers to be sent as part of the HTTP request
  • (More will be added later - we also need additional options for the fetch request, like allowCredentials). Please file issues / send PRs if we're missing something important here.

subscribeRaw returns a promise containing:

  • streamHeaders: An object containing HTTP-level headers returned by the server. Eg {content-type: 'application/json'}.
  • updates: An async iterator which yields updates as they are sent from the server to the client.

You can consume the iterator using a for-await loop:

const { updates } = await subscribeRaw('http://localhost:2001/time')

for await (const { type, headers, value, patches } of updates) {
  console.log('got update of type', type, 'with headers', headers)
}

Each entry in the updates iterator contains:

  • type: Either 'snapshot' or 'patch', specifying if the value or patches field is included in the response, respectively.
  • headers: Update level headers (duh)
  • value: Snapshot updates contain a Uint8Array with the new document contents
  • patches: A list of patches returned by the server. Each patch contains headers and a body field, with the body again being a Uint8Array.

subscribe

subscribe(url: string, opts?) => Promise<{streamHeaders, updates}>

This method is a wrapper around subscribeRaw, doing some best-guess parsing of the returned content.

TODO: Document me!