Giter Site home page Giter Site logo

browser-sync-stream's Introduction

browser-sync-stream

Rsync between a server and the browser. Useful if you want to sync larger assets than often change incrementally such as browserify builds.

npm install browser-sync-stream

Usage

On a server

var send = require('browser-sync-stream')
var ws = require('websocket-stream')

ws.createServer({port: 10000}, function (stream) {
  // try to tune the rabin chunker towards smaller chunks / diffs. depends on the max size of your data
  var rabinOptions = {bits: 9, min: 1024, max: 4096} // see the rabin module for options
  stream.pipe(send('a-browserify-bundle-for-example.js', rabinOptions)).pipe(stream)
})

In the browser

var receive = require('browser-sync-stream')
var level = require('level-browserify')
var ws = require('websocket-stream')

var db = level('data.db') // we need a database to store the diff
var diff = 0

var stream = receive(db, function (err, data) {
  console.log('synced a file totaling', data.length, 'bytes')
  console.log('diff was', diff, 'bytes')
})

stream.on('download', function (data) {
  diff += data.length
})

stream.pipe(ws('ws://localhost:10000')).pipe(stream)

Server API

var stream = send(filename, [rabinOptions])

Creates a send stream that syncs file the file provided by the filename. Options are forwarded to the rabin module that is used to chunk the file. Pipe this stream to a browser sync stream running in the browser to sync the file

Browser API

var stream = receive(db, callback)

Creates a receive stream that uses the data stored in the provided database (a levelup instance) to sync a file provided by the server.

The way it works is similar to rsync. It uses a simple one round trip protocol where the browser first sends all list of hashes of all the chunks it has locally in its database. This digest is usually pretty small (a few kb for large data). The server then simply chunks the file using the rabin chunker to get consistent chunks even though the file is modified, hashes each chunk and sends a diff to the client. After receiving the diff the client garbage collects old chunks that have been invalidated by the new diff and calls the callback with the total buffer.

License

MIT

browser-sync-stream's People

Contributors

mafintosh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.