├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package.json ├── scan_streams.js └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .tern-port 3 | npm-debug.log -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | examples 2 | test 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) Bryce B. Baril 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | redis-scanstreams 2 | ===== 3 | 4 | [![NPM](https://nodei.co/npm/redis-scanstreams.png)](https://nodei.co/npm/redis-scanstreams/) 5 | 6 | [![david-dm](https://david-dm.org/brycebaril/redis-scanstreams.png)](https://david-dm.org/brycebaril/redis-scanstreams/) 7 | [![david-dm](https://david-dm.org/brycebaril/redis-scanstreams/dev-status.png)](https://david-dm.org/brycebaril/redis-scanstreams#info=devDependencies/) 8 | 9 | Provides a streaming interface to the Redis \*SCAN commands. 10 | 11 | Replaces the SCAN, SSCAN, HSCAN, and ZSCAN methods on the [node_redis](http://npm.im/redis) client with streaming versions. 12 | 13 | You can read more about SCAN [here](http://redis.io/commands/scan). 14 | 15 | ## Example: Use with [terminus](https://github.com/brycebaril/node-terminus) 16 | 17 | ```javascript 18 | var redis = require("redis") 19 | 20 | // replace the methods for any clients 21 | require("redis-scanstreams")(redis) 22 | 23 | var client = redis.createClient() 24 | var tail = require("terminus").tail 25 | 26 | client.scan() 27 | .pipe(tail({objectMode: true}, console.log)) 28 | ``` 29 | 30 | ## Example: Use with [stream-to-array](https://github.com/stream-utils/stream-to-array) 31 | You can use `stream-to-array` to concatenate the Redis scan results into a single array. 32 | ```javascript 33 | var redis = require("redis") 34 | 35 | // replace the methods for any clients 36 | require("redis-scanstreams")(redis) 37 | 38 | var client = redis.createClient() 39 | var toArray = require('stream-to-array') 40 | 41 | toArray(client.scan(), function(err, arr) { 42 | if (err) 43 | throw err; 44 | 45 | console.log(arr) 46 | }) 47 | ``` 48 | 49 | API 50 | === 51 | 52 | `scanStreams(redis_library)` 53 | --- 54 | 55 | Replaces the \*SCAN methods in the provided library. Assumes `node_redis` or a library that similarly exposes the `RedisClient` type which has the \*SCAN methods. 56 | 57 | `client.scan(options)` 58 | --- 59 | 60 | Calls the `scan` command, walking the cursor through the entire keyspace. Returns a `stream.Readable` containing the Redis keyspace. 61 | 62 | Options: 63 | * `pattern`: the pattern to match keys against 64 | * `count`: how many (max estimate) records to return per batch 65 | 66 | e.g. 67 | ```js 68 | client.scan({pattern: "key:*", count: 1000}) 69 | ``` 70 | 71 | `client.sscan(key, options)` 72 | --- 73 | 74 | Calls the `sscan` command on key `key`. Key must be a Redis Set. Options are identical to `SCAN`. Provides a `stream.Readable` containing set members. 75 | 76 | `client.hscan(key, options)` 77 | --- 78 | 79 | Calls the `hscan` command on key `key`. Key must be a Redis Hash. Options are identical to `SCAN`. Provides a `stream.Readable` containing hash key/value pairs, i.e.: 80 | 81 | ```js 82 | [ 83 | {key: "hash_key_1", value: "value at hash_key_1"}, 84 | {key: "hash_key_2", value: "value at hash_key_2"}, 85 | ... 86 | ] 87 | ``` 88 | 89 | `client.zscan(key, options)` 90 | --- 91 | 92 | Calls the `zscan` command on key `key`. Key must be a Redis Zset. Options are identical to `SCAN`. Provides a `stream.Readable` containing hash member/score pairs, i.e.: 93 | 94 | ```js 95 | [ 96 | {key: "zset_member_1", value: "score for zset_member_1"}, 97 | {key: "zset_member_2", value: "score for zset_member_2"}, 98 | ... 99 | ] 100 | ``` 101 | 102 | LICENSE 103 | ======= 104 | 105 | MIT 106 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redis-scanstreams", 3 | "version": "1.0.3", 4 | "description": "A streaming interface to the Redis SCAN (SCAN, SSCAN, HSCAN, ZSCAN) commands in Redis 2.8.x+", 5 | "main": "scan_streams.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "node test/" 11 | }, 12 | "keywords": [ 13 | "redis", 14 | "streams", 15 | "keys", 16 | "scan", 17 | "zscan", 18 | "sscan", 19 | "hscan" 20 | ], 21 | "author": "Bryce B. Baril", 22 | "license": "MIT", 23 | "devDependencies": { 24 | "redis": "~0.12.0", 25 | "terminus": "~1.0.8", 26 | "tape": "~4.0.0" 27 | }, 28 | "dependencies": { 29 | "stream-spigot": "~3.0.3", 30 | "through2": "~0.6.5", 31 | "xtend": "^4.0.0" 32 | }, 33 | "repository": { 34 | "type": "git", 35 | "url": "git@github.com:brycebaril/redis-scanstreams.git" 36 | }, 37 | "bugs": { 38 | "url": "https://github.com/brycebaril/redis-scanstreams/issues" 39 | }, 40 | "homepage": "https://github.com/brycebaril/redis-scanstreams" 41 | } 42 | -------------------------------------------------------------------------------- /scan_streams.js: -------------------------------------------------------------------------------- 1 | module.exports = replaceScans 2 | 3 | var spigot = require("stream-spigot") 4 | var xtend = require("xtend") 5 | var through2 = require("through2") 6 | 7 | function replaceScans(redis) { 8 | var clientProto = redis.RedisClient.prototype 9 | clientProto.scan = clientProto.SCAN = scan 10 | clientProto.sscan = clientProto.sscan = sscan 11 | clientProto.hscan = clientProto.hscan = hscan 12 | clientProto.zscan = clientProto.zscan = zscan 13 | return redis 14 | } 15 | 16 | function scan(options) { 17 | var client = this 18 | options = xtend({command: "scan"}, options) 19 | if (options.match) 20 | options.pattern = options.match 21 | if (options.command != "scan" && options.key == null) { 22 | throw new Error("[S|H|Z]SCAN commands require a key name") 23 | } 24 | var ended = false 25 | 26 | // TODO replace the buffering with _writev when 0.12 is released. 27 | var buffer = [] 28 | 29 | // TODO want_buffers or detect_buffers? 30 | var stream = spigot({objectMode: true}, function (n) { 31 | var self = this 32 | if (buffer.length > 0) { 33 | return self.push(buffer.shift()) 34 | } 35 | getMore(function (err) { 36 | if (err) { 37 | self.emit("error", err) 38 | self.push(null) 39 | } 40 | if (buffer.length > 0) 41 | self.push(buffer.shift()) 42 | else if (ended) 43 | self.push(null) 44 | }) 45 | }) 46 | 47 | var cursor = 0 48 | var cursorIdx = 0 49 | 50 | var args = [] 51 | if (options.key != null) { 52 | args.push(options.key) 53 | cursorIdx = 1 54 | } 55 | args.push(cursor) 56 | if (options.pattern != null) 57 | args = args.concat(["MATCH", options.pattern]) 58 | if (options.count != null) 59 | args = args.concat(["COUNT", options.count]) 60 | 61 | function getMore(cb) { 62 | if (ended) 63 | return setImmediate(cb) 64 | 65 | args[cursorIdx] = cursor 66 | client.send_command(options.command, args, function (err, reply) { 67 | if (err) return cb(err) 68 | cursor = reply[0] 69 | if (cursor == 0) 70 | ended = true 71 | if (reply[1].length == 0 && !ended) 72 | return getMore(cb) 73 | buffer = buffer.concat(reply[1]) 74 | cb() 75 | }) 76 | } 77 | 78 | return stream 79 | } 80 | 81 | function sscan(key, options) { 82 | return this.scan(xtend({command: "sscan", key: key}, options)) 83 | } 84 | 85 | var PairWise = through2.ctor({objectMode: true}, function (chunk, encoding, callback) { 86 | var isValue = this.keyName != null 87 | if (isValue) { 88 | this.push({key: this.keyName, value: chunk}) 89 | this.keyName = null 90 | return callback() 91 | } 92 | this.keyName = chunk 93 | return callback() 94 | }) 95 | 96 | function hscan(key, options) { 97 | return this.scan(xtend({command: "hscan", key: key}, options)) 98 | .pipe(PairWise()) 99 | } 100 | 101 | function zscan(key, options) { 102 | return this.scan(xtend({command: "zscan", key: key}, options)) 103 | .pipe(PairWise()) 104 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var test = require("tape").test 2 | var terminus = require("terminus") 3 | var redis = require("redis") 4 | 5 | var PORT = 6379 6 | var HOST = '127.0.0.1' 7 | var DBNUM = 15 8 | 9 | var scanStreams = require("../scan_streams") 10 | scanStreams(redis) 11 | 12 | function getClient() { 13 | var client = redis.createClient(PORT, HOST) 14 | client.select(DBNUM) 15 | return client 16 | } 17 | 18 | 19 | test("commands substituted", function (t) { 20 | var client = getClient() 21 | t.ok(client.scan && typeof client.scan == "function", "has scan") 22 | t.ok(client.SCAN && typeof client.SCAN == "function", "has SCAN") 23 | t.equals(client.scan.length, 1, "scan replaced with custom scan") 24 | 25 | t.ok(client.sscan && typeof client.sscan == "function", "has sscan") 26 | t.ok(client.SSCAN && typeof client.SSCAN == "function", "has SSCAN") 27 | t.equals(client.sscan.length, 2, "sscan replaced with custom sscan") 28 | 29 | t.ok(client.hscan && typeof client.hscan == "function", "has hscan") 30 | t.ok(client.HSCAN && typeof client.HSCAN == "function", "has HSCAN") 31 | t.equals(client.hscan.length, 2, "hscan replaced with custom hscan") 32 | 33 | t.ok(client.zscan && typeof client.zscan == "function", "has zscan") 34 | t.ok(client.ZSCAN && typeof client.ZSCAN == "function", "has ZSCAN") 35 | t.equals(client.zscan.length, 2, "zscan replaced with custom zscan") 36 | 37 | client.quit(function () { 38 | t.end() 39 | }) 40 | }) 41 | 42 | test("setup", function (t) { 43 | var client = getClient() 44 | client.flushdb() 45 | client.send_command("DEBUG", ["POPULATE", 50]) 46 | var hash = {} 47 | var set = [] 48 | var zset = ["zset:1"] 49 | for (var i = 0; i < 500; i++) { 50 | hash["key_" + i] = "value_" + i 51 | set.push("member_" + i) 52 | zset.push(i, "z_member_" + i) 53 | } 54 | client.hmset("hash:1", hash) 55 | client.sadd("set:1", set) 56 | client.send_command("zadd", zset) 57 | client.quit(function () { 58 | t.end() 59 | }) 60 | }) 61 | 62 | test("simple scan", function (t) { 63 | var client = getClient() 64 | 65 | function checkResults(records) { 66 | t.equals(records.length, 53, "Correct number of records") 67 | t.ok(records.indexOf("key:10") >= 0, "has an expected key") 68 | t.ok(records.indexOf("hash:1") >= 0, "has an expected key") 69 | client.quit(function () { 70 | t.end() 71 | }) 72 | } 73 | 74 | client.scan() 75 | .pipe(terminus.concat({objectMode: true}, checkResults)) 76 | }) 77 | 78 | test("simple scan w/ pattern", function (t) { 79 | var client = getClient() 80 | 81 | function checkResults(records) { 82 | t.equals(records.length, 50, "Correct number of records") 83 | t.ok(records.indexOf("key:10") >= 0, "has an expected key") 84 | t.ok(records.indexOf("set:1") == -1, "key was excluded") 85 | client.quit(function () { 86 | t.end() 87 | }) 88 | } 89 | 90 | client.scan({pattern: "key:*"}) 91 | .pipe(terminus.concat({objectMode: true}, checkResults)) 92 | }) 93 | 94 | test("simple scan w/ count", function (t) { 95 | var client = getClient() 96 | 97 | // TODO should actually check that count was obeyed here... 98 | 99 | function checkResults(records) { 100 | t.equals(records.length, 53, "Correct number of records") 101 | t.ok(records.indexOf("key:10") >= 0, "has an expected key") 102 | t.ok(records.indexOf("hash:1") >= 0, "has an expected key") 103 | client.quit(function () { 104 | t.end() 105 | }) 106 | } 107 | 108 | client.scan({count: 25}) 109 | .pipe(terminus.concat({objectMode: true}, checkResults)) 110 | }) 111 | 112 | test("simple scan w/ pattern & count", function (t) { 113 | var client = getClient() 114 | 115 | // TODO should actually check that count was obeyed here... 116 | 117 | function checkResults(records) { 118 | t.equals(records.length, 50, "Correct number of records") 119 | t.ok(records.indexOf("key:10") >= 0, "has an expected key") 120 | t.ok(records.indexOf("set:1") == -1, "key was excluded") 121 | client.quit(function () { 122 | t.end() 123 | }) 124 | } 125 | 126 | client.scan({pattern: "key:*", count: 25}) 127 | .pipe(terminus.concat({objectMode: true}, checkResults)) 128 | }) 129 | 130 | test("simple hscan", function (t) { 131 | var client = getClient() 132 | 133 | function checkResults(records) { 134 | t.equals(records.length, 500, "Correct number of records") 135 | var found = records.filter(function (e) { 136 | if (e.key == "key_10") return true 137 | if (e.value == "value_333") return true 138 | return false 139 | }) 140 | t.equals(found.length, 2, "Found expected records") 141 | client.quit(function () { 142 | t.end() 143 | }) 144 | } 145 | 146 | client.hscan("hash:1") 147 | .pipe(terminus.concat({objectMode: true}, checkResults)) 148 | }) 149 | 150 | test("simple hscan w/ pattern", function (t) { 151 | var client = getClient() 152 | 153 | function checkResults(records) { 154 | t.equals(records.length, 111, "Correct number of records") 155 | var found = records.filter(function (e) { 156 | if (e.key == "key_10") return true 157 | if (e.value == "value_333") return true 158 | return false 159 | }) 160 | t.equals(found.length, 1, "Found expected records") 161 | client.quit(function () { 162 | t.end() 163 | }) 164 | } 165 | 166 | client.hscan("hash:1", {pattern: "key_3*"}) 167 | .pipe(terminus.concat({objectMode: true}, checkResults)) 168 | }) 169 | 170 | test("simple sscan", function (t) { 171 | var client = getClient() 172 | 173 | function checkResults(records) { 174 | t.equals(records.length, 500, "Correct number of records") 175 | t.ok(records.indexOf("member_10") >= 0, "has an expected key") 176 | t.ok(records.indexOf("member_333") >= 0, "has an expected value") 177 | client.quit(function () { 178 | t.end() 179 | }) 180 | } 181 | 182 | client.sscan("set:1") 183 | .pipe(terminus.concat({objectMode: true}, checkResults)) 184 | }) 185 | 186 | test("simple sscan w/ pattern", function (t) { 187 | var client = getClient() 188 | 189 | function checkResults(records) { 190 | t.equals(records.length, 111, "Correct number of records") 191 | t.ok(records.indexOf("member_10") == -1, "key was excluded") 192 | t.ok(records.indexOf("member_333") >= 0, "has an expected value") 193 | client.quit(function () { 194 | t.end() 195 | }) 196 | } 197 | 198 | client.sscan("set:1", {pattern: "member_3*"}) 199 | .pipe(terminus.concat({objectMode: true}, checkResults)) 200 | }) 201 | 202 | test("simple zscan", function (t) { 203 | var client = getClient() 204 | 205 | function checkResults(records) { 206 | t.equals(records.length, 500, "Correct number of records") 207 | var found = records.filter(function (e) { 208 | if (e.key == "z_member_10") return true 209 | if (e.key == "z_member_333") return true 210 | return false 211 | }) 212 | t.equals(found.length, 2, "Found expected records") 213 | client.quit(function () { 214 | t.end() 215 | }) 216 | } 217 | 218 | client.zscan("zset:1") 219 | .pipe(terminus.concat({objectMode: true}, checkResults)) 220 | }) 221 | 222 | test("simple zscan w/ pattern", function (t) { 223 | var client = getClient() 224 | 225 | function checkResults(records) { 226 | t.equals(records.length, 111, "Correct number of records") 227 | var found = records.filter(function (e) { 228 | if (e.key == "z_member_10") return true 229 | if (e.key == "z_member_333") return true 230 | return false 231 | }) 232 | t.equals(found.length, 1, "One key now excluded") 233 | client.quit(function () { 234 | t.end() 235 | }) 236 | } 237 | 238 | client.zscan("zset:1", {pattern: "z_member_3*"}) 239 | .pipe(terminus.concat({objectMode: true}, checkResults)) 240 | }) --------------------------------------------------------------------------------