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-text

v0.2.3

Published

Library for collaborative text over http using braid.

Downloads

1,023

Readme

Collaborative text over Braid-HTTP

This library provides a simple http route handler, along with client code, enabling fast text synchronization over a standard protocol.

  • Supports Braid-HTTP protocol
  • Supports Simpleton merge-type
    • Enables light clients
      • As little as 50 lines of code!
      • With zero history overhead on client
    • Supports backpressure to run smoothly on constrained servers
    • Server merges with Diamond-Types
  • Supports Diamond Types merge-type
    • Fully peer-to-peer CRDT
    • Fast / Robust / Extensively fuzz-tested
  • Developed in braid.org

This library makes it safe, easy & efficient to add collaborative text editing to every user-editable string in your web app. Make your app multiplayer!

Check out the demo video 📺 from the Braid 86 release!

Demo: a Wiki!

This will run a collaboratively-editable wiki:

npm install
node server-demo.js

Now open these URLs in your browser:

  • http://localhost:8888/demo (to see the demo text)
  • http://localhost:8888/demo?editor (to edit the text)
  • http://localhost:8888/demo?markdown-editor (to edit it as markdown)
  • http://localhost:8888/any-other-path?editor (to create a new page, just go to its URL, and then start editing)

Or try opening the URL in Braid-Chrome, or another Braid client, to edit it directly!

Check out the server-demo.js file to see examples for how to add access control, and a /pages endpoint to show all the edited pages.

General Use on Server

Install it in your project:

npm install braid-text

Import the request handler into your code, and use it to handle HTTP requests wherever you want:

var braid_text = require("braid-text")

http_server.on("request", (req, res) => {
  // Your server logic...

  // Whenever desired, serve braid text for this request/response:
  braid_text.serve(req, res)
})

Server API

braid_text.db_folder = './braid-text-db' // <-- this is the default

  • This is where the Diamond-Types history files will be stored for each resource.
  • This folder will be created if it doesn't exist.
  • The files for a resource will all be prefixed with a url-encoding of key within this folder.

braid_text.serve(req, res, options)

  • req: Incoming HTTP request object.
  • res: Outgoing HTTP response object.
  • options: [optional] An object containing additional options:
    • key: [optional] ID of text resource to sync with. Defaults to req.url.
  • This is the main method of this library, and does all the work to handle Braid-HTTP GET and PUT requests concerned with a specific text resource.

await braid_text.get(key)

  • key: ID of text resource.
  • Returns the text of the resource as a string.

await braid_text.get(key, options)

  • key: ID of text resource.
  • options: An object containing additional options, like http headers:
    • version: [optional] The version to get, as an array of strings. (The array is typically length 1.)
    • parents: [optional] The version to start the subscription at, as an array of strings.
    • subscribe: cb: [optional] Instead of returning the state; subscribes to the state, and calls cb with the initial state and each update. The function cb will be called with a Braid update of the form cb({version, parents, body, patches}).
    • merge_type: [optional] The CRDT/OT merge-type algorithm to emulate. Currently supports "simpleton" (default) and "dt".
    • peer: [optional] Unique string ID that identifies the peer making the subscription. Mutations will not be echoed back to the same peer that PUTs them, for any PUT setting the same peer header.
  • If NOT subscribing, returns {version: <current_version>, body: <current-text>}. If subscribing, returns nothing.

await braid_text.put(key, options)

  • key: ID of text resource.
  • options: An object containing additional options, like http headers:
    • version: [optional] The version being PUT, as an array of strings. Will be generated if not provided.
    • parents: [optional] The previous version being updated, as array of strings. Defaults to the server’s current version.
    • body: [optional] Use this to completely replace the existing text with this new text. See Braid updates.
    • patches: [optional] Array of patches, each of the form {unit: 'text', range: '[1:3]', content: 'hi'}, which would replace the second and third unicode code-points in the text with hi. See Braid Range-Patches.
    • peer: [optional] Identifies this peer. This mutation will not be echoed back to get subscriptions that use this same peer header.

General Use on Client

<script src="https://unpkg.com/braid-text/simpleton-client.js"></script>
<script>

  // connect to the server
  let simpleton = simpleton_client('https://example.org/some-resource', {
    apply_remote_update: ({ state, patches }) => {

      // Apply the incoming state or patches to local text here.

      // Then return the new state of textarea as a string:
      return new_state
    },
    generate_local_diff_update: (prev_state) => {

      // Compute diff between prev_state ^ and the current textarea string, such as:
      //
      //   var patches = [{
      //     range: [5:5],
      //     content: " World"
      //   }]
      //
      // ...to insert something after a prev_state of "Hello".

      // Then return the new state (as a string) and the diff (as `patches`)
      return {new_state, patches}
    },
  })
    
  ...
    
  // When changes occur in client's textarea, let simpleton know,
  // so that it can call generate_local_diff_update() to ask for them.
  simpleton.changed()

</script>

See editor.html for a simple working example.

Client API

simpleton = simpleton_client(url, options)
  • url: The URL of the resource to synchronize with.

  • options: An object containing the following properties:

    • apply_remote_update: A function that will be called whenever an update is received from the server. It should have the following signature:

      ({state, patches}) => {...}
      • state: If present, represents the new value of the text.
      • patches: If present, an array of patch objects, each representing a string-replace operation. Each patch object has the following properties:
        • range: An array of two numbers, [start, end], specifying the start and end positions of the characters to be deleted.
        • content: The text to be inserted in place of the deleted characters.

      Note that patches will always be in order, but the range positions of each patch reference the original string, i.e., the second patch's range values do not take into account the application of the first patch.

      The function should apply the state or patches to the local text and return the new state.

    • generate_local_diff_update: A function that will be called whenever a local update occurs, but may be delayed if the network is congested. It should have the following signature:

      (prev_state) => {...}

      The function should calculate the difference between prev_state and the current state, and express this difference as an array of patches (similar to the ones described in apply_remote_update).

      If a difference is detected, the function should return an object with the following properties:

      • new_state: The current state of the text.
      • patches: An array of patch objects representing the changes.

      If no difference is detected, the function should return undefined or null.

    • content_type: [optional] If set, this value will be sent in the Accept and Content-Type headers to the server.

  • simpleton.changed(): Call this function to report local updates whenever they occur, e.g., in the oninput event handler of a textarea being synchronized.

Testing

to run unit tests:

first run the demo server as usual:

npm install
node server-demo.js

then open http://localhost:8888/test.html, and the boxes should turn green as the tests pass.

to run fuzz tests:

npm install
node test.js

if the last output line looks like this, good:

t = 9999, seed = 1397019, best_n = Infinity @ NaN

but it's bad if it looks like this:

t = 9999, seed = 1397019, best_n = 5 @ 1396791

the number at the end is the random seed that generated the simplest error example