Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 7d2885e2b625190c951e81f2589521f1413ab6bd @tblobaum committed Jul 13, 2012
Showing with 691 additions and 0 deletions.
  1. +44 −0 .gitignore
  2. +22 −0 LICENSE
  3. +95 −0 README.md
  4. +11 −0 example/command-parser.js
  5. +29 −0 example/hgetall-parse.js
  6. +15 −0 example/http-monitor.js
  7. +38 −0 example/keys-hvals-sadd.js
  8. +7 −0 example/monitor.js
  9. +24 −0 example/rpop-lpush.js
  10. +13 −0 example/select-database.js
  11. +8 −0 example/simple.js
  12. +90 −0 index.js
  13. +35 −0 package.json
  14. +69 −0 test/lists.js
  15. +92 −0 test/sets.js
  16. +99 −0 test/strings.js
@@ -0,0 +1,44 @@
+# Numerous always-ignore extensions
+*.diff
+*.err
+*.orig
+*.log
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+
+# OS or Editor folders
+.DS_Store
+.cache
+.project
+.settings
+nbproject
+thumbs.db
+
+# Logs
+.log
+.pid
+.sock
+.monitor
+
+# Dreamweaver added files
+_notes
+dwsync.xml
+
+# Komodo
+*.komodoproject
+.komodotools
+
+# Folders to ignore
+node_modules
+.hg
+.svn
+publish
+.idea
+_dev
+
+# build script local files
+build/buildinfo.properties
+build/config/buildinfo.properties
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2012 Thomas Blobaum <tblobaum@gmail.com>
+
+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.
@@ -0,0 +1,95 @@
+# redis-stream
+
+[![Build Status](https://secure.travis-ci.org/tblobaum/redis-stream.png)](http://travis-ci.org/tblobaum/redis-stream)
+
+# Example
+
+In the `example` directory there are various streaming examples.
+
+```js
+
+
+```
+
+# Methods
+
+``` js
+var Redis = require('redis-stream')
+ , client = new Redis(6379, localhost, 0)
+```
+
+## new Redis([port] [, host] [, database])
+Return an object that streams can be created from with the `port`, `host`, and `database` options -- `port` defaults to `6379`, `host` to `localhsot` and `database` to `0`.
+
+## client.stream([arg1] [, arg2] [, argn])
+Return a [node.js api compatible stream](http://nodejs.org/api/streams.html) that is readable, writeable, and can be piped. All calls to `write` on this stream will be prepended with the optional arguments passed to `client.stream`
+
+Create a streaming instance of rpop:
+
+``` js
+var rpop = client.stream('rpop')
+rpop.pipe(process.stdout)
+rpop.write('my-list-key')
+```
+
+Which you can then pipe redis keys to, and they resulting elements will be piped to stdout.
+
+## Redis.parse.hgetall()
+
+Return a stream that can be piped to to transform an `hmget` or `hgetall` stream into valid json, with a little help from [JSONStream](https://github.com/dominictarr/JSONStream) we can turn this into a real object.
+
+``` js
+ hgetall = client.stream('hgetall')
+ hgetall
+ .pipe(Redis.parse.hgetall())
+ .pipe(JSONStream.stringifyObject())
+ .pipe(process.stdout)
+
+ hgetall.write('my-hash-key-1')
+```
+
+## Redis.parse(array)
+It's possible to interact directly with the command parser that transforms a stream into valid redis data stream
+
+``` js
+var Redis = require('../')
+ , redis = new Redis(6379, 'localhost')
+ , stream = redis.stream()
+
+stream.pipe(Redis.es.join('\r\n')).pipe(process.stdout)
+
+// interact with the redis network connection directly
+// using `Redis.parse`, which is used internally
+stream.redis.write(Redis.parse([ 'info' ]))
+stream.redis.write(Redis.parse([ 'lpush', 'mylist', 'val' ]))
+stream.end()
+```
+
+# Install
+
+`npm install redis-stream`
+
+# License
+
+(The MIT License)
+
+Copyright (c) 2012 Thomas Blobaum <tblobaum@gmail.com>
+
+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.
@@ -0,0 +1,11 @@
+var Redis = require('../')
+ , redis = new Redis(6379, 'localhost')
+ , stream = redis.stream()
+
+stream.pipe(Redis.es.join('\r\n')).pipe(process.stdout)
+
+// interact with the redis network connection directly
+// using `Redis.parse`, which is used internally
+stream.redis.write(Redis.parse([ 'info' ]))
+stream.redis.write(Redis.parse([ 'lpush', 'mylist', 'val' ]))
+stream.end()
@@ -0,0 +1,29 @@
+
+var Redis = require('../')
+ , JSONStream = require('JSONStream')
+ , parser = JSONStream.parse([ true ])
+ , client = new Redis(6379, 'localhost')
+ , stream = client.stream()
+ , hgetall
+
+var bootstrapData = [ [ 'hmset', 'my-hash-key-1', 'field1', 'val1', 'field2', 'val2' ] ]
+
+stream.redis.write(Redis.parse(bootstrapData[0]))
+stream.on('close', function () {
+
+ //
+ // parse the streaming output of HGETALL to JSON
+ //
+ hgetall = client.stream('hgetall')
+ hgetall
+ .pipe(Redis.parse.hgetall())
+ .pipe(JSONStream.stringifyObject())
+ .pipe(process.stdout)
+
+ hgetall.write('my-hash-key-1')
+ hgetall.on('data', function (data) {
+ hgetall.end()
+ })
+})
+
+stream.end()
@@ -0,0 +1,15 @@
+var Redis = require('../')
+ , client = new Redis(6379, 'localhost')
+
+// stream monitor to all http requests
+// curl http://0.0.0.0:3000/
+
+require('http')
+.createServer(function (request, response) {
+ var redis = client.stream()
+ redis.pipe(Redis.es.join('\r\n')).pipe(response)
+ redis.write('monitor')
+})
+.listen(3000)
+
+console.log('monitor streaming at 0.0.0.0:3000')
@@ -0,0 +1,38 @@
+
+var Redis = require('../')
+ , client = new Redis(6379, 'localhost')
+ , stream = client.stream()
+
+var bootstrapData = [
+ [ 'hmset', 'my-hash-key-1', 'field1', 'val1', 'field2', 'val2' ]
+ , [ 'hmset', 'my-hash-key-2', 'field1', 'val1', 'field2', 'val2' ]
+]
+stream.redis.write(Redis.parse(bootstrapData[0]))
+stream.redis.write(Redis.parse(bootstrapData[1]))
+stream.on('close', function () {
+
+ //
+ // Example
+ // keys => hvals => sadd
+ //
+ var keys = client.stream('keys')
+ var hvals = client.stream('hvals')
+ var sadd = client.stream('sadd', 'my-result-set')
+
+ process.stdin
+ .pipe(Redis.es.split())
+ .pipe(keys)
+ .pipe(hvals)
+ .pipe(sadd)
+ .pipe(process.stdout)
+
+ process.stdout.write('Retrieve all keys of a given pattern, then \r\n')
+ process.stdout.write('call HVALS on each of those keys, then \r\n')
+ process.stdout.write('call SADD "my-result-set" on each of the values \r\n')
+ process.stdout.write('-- \r\n')
+ process.stdout.write('type a KEYS pattern, e.g. *-hash-key-* to \r\n')
+ process.stdout.write('stream the result of the SADD command here \r\n')
+
+})
+
+stream.end()
@@ -0,0 +1,7 @@
+var Redis = require('../')
+ , client = new Redis(6379, 'localhost')
+
+// stream monitor to stdout
+var monitor = client.stream()
+monitor.pipe(Redis.es.join('\r\n')).pipe(process.stdout)
+monitor.write('monitor')
@@ -0,0 +1,24 @@
+var Redis = require('../')
+ , client = new Redis(6379, 'localhost')
+ , rpush
+ , rpop
+
+// add some data to `mylist` first
+rpush = client.stream('rpush', 'mylist')
+rpush.pipe(process.stdout)
+rpush.write('val1')
+rpush.write('val2')
+rpush.on('end', function () {
+
+ // reimplementation of rpoplpush with redis-stream
+ // http://redis.io/commands/rpoplpush
+ rpop = client.stream('rpop')
+ rpop
+ .pipe(client.stream('lpush', 'myotherlist'))
+ .pipe(process.stdout)
+
+ rpop.write('mylist')
+ rpop.end()
+
+})
+rpush.end()
@@ -0,0 +1,13 @@
+var Redis = require('../')
+ , client = new Redis(6379, 'localhost')
+ , sadd = client.stream('sadd', 'myset')
+
+sadd.pipe(process.stdout)
+
+sadd.on('error', function (err) {
+ console.error('err:', err)
+})
+
+sadd.write('value1')
+sadd.write('value2')
+sadd.end()
@@ -0,0 +1,8 @@
+var Redis = require('../')
+ , client = new Redis(6379, 'localhost', 2) // select database 2
+ , lpush = client.stream('lpush', 'mylist')
+
+lpush.pipe(process.stdout)
+lpush.write('value1')
+lpush.write('value2')
+lpush.end()
Oops, something went wrong.

0 comments on commit 7d2885e

Please sign in to comment.