Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial

  • Loading branch information...
commit 00ffa462ade46bdfb998ab23a9b9c165a15edbdc 0 parents
@dominictarr authored
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules
+node_modules/*
+npm_debug.log
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 'Dominic Tarr'
+
+Permission is hereby granted, free of charge,
+to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to
+deal in the Software without restriction, including
+without limitation the rights to use, copy, modify,
+merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom
+the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 README.md
@@ -0,0 +1,11 @@
+# scuttlebutt-schema
+
+[![server-tests](https://travis-ci.org/dominictarr/scuttlebutt-schema)
+](https://secure.travis-ci.org/dominictarr/scuttlebutt-schema.png?branch=master)
+
+[![browser-support](https://ci.testling.com/dominictarr/scuttlebutt-schema.png)
+](https://ci.testling.com/dominictarr/scuttlebutt-schema)
+
+## License
+
+MIT
5 index.js
@@ -0,0 +1,5 @@
+
+
+var exports = module.exports = require('./schema')
+
+exports.open = require('./sync')
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "scuttlebutt-schema",
+ "version": "0.0.0",
+ "homepage": "https://github.com/dominictarr/scuttlebutt-schema",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/dominictarr/scuttlebutt-schema.git"
+ },
+ "dependencies": {
+ },
+ "devDependencies": {
+ },
+ "scripts": {
+ "test": "set -e; for t in test/*.js; do node $t; done"
+ },
+ "author": "'Dominic Tarr' <dominic.tarr@gmail.com> (http://dominictarr.com)",
+ "license": "MIT"
+}
25 schema.js
@@ -0,0 +1,25 @@
+var parserx = require('parse-regexp')
+
+module.exports = function (schema) {
+ if('function' == schema)
+ return schema
+
+ var rules = []
+ for (var p in schema) {
+ rules.push({rx: parserx(p) || p, fn: schema[p]})
+ }
+
+ function match (key) {
+ for (var i in rules) {
+ var r = rules[i]
+ var m = key.match(r.rx)
+ if(m && m.index === 0)
+ return r.fn
+ }
+ }
+
+ match.schema = schema
+ match.rules = rules
+
+ return match
+}
53 sync.js
@@ -0,0 +1,53 @@
+
+var sync = module.exports = function (schema, connect) {
+ return function (name, tail, cb) {
+ if('function' == typeof tail)
+ cb = tail, tail = true
+
+ var scuttlebutt = schema(name)()
+ var es = scuttlebutt.createStream()
+ var stream = connect(name)
+
+ if(!stream)
+ return cb(new Error('unable to connect'))
+
+ stream.pipe(es).pipe(stream)
+
+ var ready = false
+ es.once('sync', function () {
+ if(ready) return
+ ready = true
+
+ //cb the stream we are loading the scuttlebutt from,
+ //incase it errors after we cb?
+ //I'm not sure about this usecase.
+ //Actually, just leave that feature out!
+ //that way I don't have to break API when I realize it was a bad idea.
+ if(cb) cb(null, scuttlebutt)
+ if(!tail) es.end()
+ })
+ //hmm, this has no way to detect that the stream has errored
+ stream.once('error', function (err) {
+ if(!ready) return cb(err)
+ })
+
+ return scuttlebutt
+ }
+}
+
+/*
+example:
+
+var sync = makeSync(function (name) {
+ //return a scuttlebutt.
+ return new Model()
+}, function (name) {
+ //return a duplex stream...
+ return http.get('http://whatever.com/replicate/'+name)
+})
+
+sync(name, function (_, scuttlebutt) {
+ //scuttlebutt is ready!
+})
+
+*/
Please sign in to comment.
Something went wrong with that request. Please try again.