├── .gitignore ├── LICENSE ├── README.md ├── browser.js ├── demo ├── demo-browser.js └── demo-server.js ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | demo-bundle.js 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # browser-sync-stream 2 | 3 | Rsync between a server and the browser. 4 | Useful if you want to sync larger assets than often change incrementally such as browserify builds. 5 | 6 | ``` 7 | npm install browser-sync-stream 8 | ``` 9 | 10 | ## Usage 11 | 12 | On a server 13 | 14 | ``` js 15 | var send = require('browser-sync-stream') 16 | var ws = require('websocket-stream') 17 | 18 | ws.createServer({port: 10000}, function (stream) { 19 | // try to tune the rabin chunker towards smaller chunks / diffs. depends on the max size of your data 20 | var rabinOptions = {bits: 9, min: 1024, max: 4096} // see the rabin module for options 21 | stream.pipe(send('a-browserify-bundle-for-example.js', rabinOptions)).pipe(stream) 22 | }) 23 | ``` 24 | 25 | In the browser 26 | 27 | ``` js 28 | var receive = require('browser-sync-stream') 29 | var level = require('level-browserify') 30 | var ws = require('websocket-stream') 31 | 32 | var db = level('data.db') // we need a database to store the diff 33 | var diff = 0 34 | 35 | var stream = receive(db, function (err, data) { 36 | console.log('synced a file totaling', data.length, 'bytes') 37 | console.log('diff was', diff, 'bytes') 38 | }) 39 | 40 | stream.on('download', function (data) { 41 | diff += data.length 42 | }) 43 | 44 | stream.pipe(ws('ws://localhost:10000')).pipe(stream) 45 | ``` 46 | 47 | ## Server API 48 | 49 | #### `var stream = send(filename, [rabinOptions])` 50 | 51 | Creates a send stream that syncs file the file provided by the filename. Options are forwarded to the [rabin](https://github.com/maxogden/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 52 | 53 | ## Browser API 54 | 55 | #### `var stream = receive(db, callback)` 56 | 57 | Creates a receive stream that uses the data stored in the provided database (a levelup instance) to sync a file provided by the server. 58 | 59 | 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. 60 | 61 | ## License 62 | 63 | MIT 64 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | var duplexify = require('duplexify') 2 | var lpstream = require('length-prefixed-stream') 3 | 4 | module.exports = receive 5 | 6 | function receive (db, cb) { 7 | var decode = lpstream.decode() 8 | var encode = lpstream.encode() 9 | var old = {} 10 | var active = {} 11 | var result = [] 12 | var batch = [] 13 | var stream = duplexify.obj(decode, encode) 14 | 15 | decode.on('data', function (data) { 16 | var hash = data.slice(0, 32) 17 | var chunk = data.slice(32) 18 | var key = hash.toString('hex') 19 | if (!chunk.length) { 20 | active[key] = true 21 | chunk = old[key] 22 | } else { 23 | stream.emit('download', chunk) 24 | batch.push({type: 'put', key: key, value: data}) 25 | } 26 | result.push(chunk) 27 | }) 28 | 29 | decode.on('end', function () { 30 | var keys = Object.keys(old) 31 | 32 | for (var i = 0; i < keys.length; i++) { 33 | if (!active[keys[i]]) batch.push({type: 'del', key: keys[i]}) 34 | } 35 | 36 | db.batch(batch, function (err) { 37 | if (err) return cb(err) 38 | cb(null, Buffer.concat(result)) 39 | }) 40 | }) 41 | 42 | db.createValueStream({valueEncoding: 'binary'}) 43 | .on('data', function (data) { 44 | data = Buffer(data) 45 | var hash = data.slice(0, 32) 46 | old[hash.toString('hex')] = data.slice(32) 47 | encode.write(hash) 48 | }) 49 | .on('end', function () { 50 | encode.write(Buffer([0])) // end of stream 51 | }) 52 | 53 | return stream 54 | } 55 | -------------------------------------------------------------------------------- /demo/demo-browser.js: -------------------------------------------------------------------------------- 1 | var receive = require('../') 2 | var level = require('level-browserify') 3 | var ws = require('websocket-stream') 4 | 5 | var db = level('sync.db') 6 | var downloaded = 0 7 | 8 | var stream = receive(db, function (err, data) { 9 | if (err) throw err 10 | console.log('downloaded buffer:', data.length, '(transferred ' + downloaded + ' bytes)') 11 | }) 12 | 13 | stream.on('download', function (chunk) { 14 | downloaded += chunk.length 15 | console.log('downloading', chunk.length, 'bytes') 16 | }) 17 | 18 | stream.pipe(ws('ws://localhost:10000')).pipe(stream) 19 | -------------------------------------------------------------------------------- /demo/demo-server.js: -------------------------------------------------------------------------------- 1 | var send = require('../') 2 | var ws = require('websocket-stream') 3 | 4 | ws.createServer({port: 10000}, function (stream) { 5 | stream.pipe(send('demo-bundle.js', {bits: 9, min: 1024, max: 4096})).pipe(stream) 6 | }) 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browser-sync-stream", 3 | "version": "1.0.0", 4 | "description": "Rsync between a server and the browser.", 5 | "main": "server.js", 6 | "dependencies": { 7 | "duplexify": "^3.4.3", 8 | "length-prefixed-stream": "^1.5.0", 9 | "rabin": "^1.4.0" 10 | }, 11 | "devDependencies": { 12 | "standard": "^6.0.8" 13 | }, 14 | "scripts": { 15 | "test": "standard" 16 | }, 17 | "browser": "./browser.js", 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/mafintosh/browser-sync-stream.git" 21 | }, 22 | "author": "Mathias Buus (@mafintosh)", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/mafintosh/browser-sync-stream/issues" 26 | }, 27 | "homepage": "https://github.com/mafintosh/browser-sync-stream" 28 | } 29 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs') 2 | var rabin = require('rabin') 3 | var crypto = require('crypto') 4 | var lpstream = require('length-prefixed-stream') 5 | var duplexify = require('duplexify') 6 | 7 | module.exports = send 8 | 9 | function send (filename, opts) { 10 | var chunks = {} 11 | var decode = lpstream.decode() 12 | var encode = lpstream.encode() 13 | var stream = duplexify.obj(decode, encode) 14 | var incoming = {} 15 | var order = [] 16 | 17 | fs.createReadStream(filename).pipe(rabin(opts)) 18 | .on('data', function (data) { 19 | stream.emit('indexing', data.length) 20 | var hash = crypto.createHash('sha256').update(data).digest() 21 | var key = hash.toString('hex') 22 | chunks[key] = data 23 | order.push(key) 24 | }) 25 | .on('end', function () { 26 | decode.on('data', function (hash) { 27 | if (hash.length === 1 && hash[0] === 0) return end() 28 | incoming[hash.toString('hex')] = true 29 | }) 30 | 31 | function end () { 32 | for (var i = 0; i < order.length; i++) { 33 | var key = order[i] 34 | var hash = Buffer(key, 'hex') 35 | if (incoming[key]) { 36 | encode.write(hash) 37 | } else { 38 | stream.emit('upload', chunks[key]) 39 | encode.write(Buffer.concat([hash, chunks[key]])) 40 | } 41 | } 42 | encode.end() 43 | } 44 | }) 45 | 46 | return stream 47 | } 48 | --------------------------------------------------------------------------------