+15
-2
@@ -11,2 +11,6 @@ function stringify(val) { | ||
| if (val == null) { | ||
| return '$-1\r\n' | ||
| } | ||
| if (Array.isArray(val)) { | ||
@@ -25,3 +29,3 @@ return '*' + val.length + '\r\n' + val.map(stringify).join('') | ||
| throw new Error('Invalid value:' + val) | ||
| throw new TypeError('Invalid value: ' + val) | ||
| } | ||
@@ -53,6 +57,13 @@ | ||
| _ = Number(content()) | ||
| if (_ === -1) { | ||
| return null | ||
| } | ||
| skip() | ||
| return str.slice(index, index + _) | ||
| case '*': | ||
| _ = new Array(Number(content())) | ||
| _ = Number(content()) | ||
| if (_ === -1) { | ||
| return null | ||
| } | ||
| _ = new Array(_) | ||
| skip() | ||
@@ -64,2 +75,4 @@ for (var i = 0; i < _.length; i++) { | ||
| return _ | ||
| default: | ||
| throw new SyntaxError('Invalid input: ' + JSON.stringify(str)) | ||
| } | ||
@@ -66,0 +79,0 @@ } |
+2
-2
| { | ||
| "name": "resp", | ||
| "version": "0.0.0", | ||
| "description": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "An implementation of the Redis Encoding and Serialization Protocol for Node.", | ||
| "main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
+36
-2
| # Resp | ||
| An implementation of the Redis Encoding and Serialization Protocol for Node.js. | ||
| An implementation of the [Redis Encoding and Serialization Protocol][resp] for | ||
| Node.js. | ||
| ## Example | ||
| ``` | ||
| resp.stringify(['MAKE', 'Widget', 6, '$0.40', true]) | ||
| // => '*5\r\n$4\r\nMAKE\r\n$6\r\nWidget\r\n:6\r\n$5\r\n$0.40\r\n:1\r\n' | ||
| resp.parse('+A-OK\r\n') | ||
| // => { message: 'A-OK' } | ||
| ``` | ||
| ## Serialize: `resp.stringify(value)` | ||
| Stringifies `value` into its RESP representation, returning the result as a | ||
| `String`. The legal values (as specified by RESP) are: | ||
| - "Status objects", which have a single `message` string (e.g. | ||
| `{ "message": "This is the status message." }`). | ||
| - Error objects | ||
| - Booleans | ||
| - Strings | ||
| - Numbers | ||
| - Arrays including any of the above | ||
| Other types and values will throw a `TypeError` describing the issue. | ||
| ## Deserialize: `resp.parse(string)` | ||
| Parses the RESP in `string`, returning the represented value as its native | ||
| type. See `resp.stringify`, above, for the supported types that may be returned. | ||
| If the input is not valid RESP, a `SyntaxError` will be thrown describing the | ||
| issue. | ||
| ## TODO | ||
| - Documentation | ||
| - Optimization | ||
| - Transform streams | ||
| [resp]: http://redis.io/topics/protocol |
+53
-0
@@ -19,2 +19,6 @@ var expect = require('chai').expect | ||
| it('should stringify Error objects', function () { | ||
| expect(resp.stringify(new Error('Native message'))).to.equal('-Error Native message\r\n') | ||
| }) | ||
| it('should stringify booleans', function () { | ||
@@ -36,2 +40,23 @@ expect(resp.stringify(true)).to.equal(':1\r\n') | ||
| }) | ||
| it('should stringify arrays of Errors', function () { | ||
| expect(resp.stringify([new Error('message')])) | ||
| .to.equal('*1\r\n-Error message\r\n') | ||
| }) | ||
| it('should stringify null', function () { | ||
| expect(resp.stringify(null)) | ||
| .to.equal('$-1\r\n') | ||
| }) | ||
| it('should stringify undefined as null', function () { | ||
| expect(resp.stringify()) | ||
| .to.equal('$-1\r\n') | ||
| }) | ||
| it('should fail on other Objects', function () { | ||
| expect(function () { | ||
| resp.stringify({}) | ||
| }).to.throw(TypeError) | ||
| }) | ||
| }) | ||
@@ -69,3 +94,31 @@ | ||
| }) | ||
| it('should parse null bulk strings', function () { | ||
| expect(resp.parse('$-1\r\n')) | ||
| .to.equal(null) | ||
| }) | ||
| it('should parse null arrays', function () { | ||
| expect(resp.parse('*-1\r\n')) | ||
| .to.equal(null) | ||
| }) | ||
| it('should fail on bad input', function () { | ||
| expect(function () { | ||
| resp.parse('invalid') | ||
| }).to.throw(SyntaxError) | ||
| }) | ||
| }) | ||
| describe('round trip', function () { | ||
| function generateTest(value) { | ||
| it(JSON.stringify(value), function () { | ||
| expect(resp.parse(resp.stringify(value))).to.deep.equal(value) | ||
| }) | ||
| } | ||
| generateTest(['WORK', 1000]) | ||
| generateTest([null]) | ||
| generateTest([{ message: 'Status' }, 42, 0, null, '', 'foobar']) | ||
| }) | ||
| }) |
Sorry, the diff of this file is not supported yet
8943
38.63%215
34.38%44
340%