From 4ff1b82b9f954d39787f523aa8914d3bbcd32fa0 Mon Sep 17 00:00:00 2001 From: Dominik Gehl Date: Sat, 5 Oct 2013 06:59:07 -0400 Subject: [PATCH] ID support improvements --- lib/Connection.js | 23 ++++++++++++++++++----- lib/Parser.js | 6 +++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/Connection.js b/lib/Connection.js index afd5bf4..7bde6e7 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -309,11 +309,24 @@ Connection.prototype.getBoxes = function(namespace, cb) { Connection.prototype.id = function(identification, cb) { if (!this.serverSupports('ID')) throw new Error('Server does not support ID'); - cmd = ''; - for (var k in identification) - cmd += '"' + k + '" "' + identification[k] + '"'; - return this._enqueue('ID (' + cmd + ')', cb); -} + var cmd = 'ID'; + if (identification === null) + cmd += ' NIL'; + else { + if (Object.keys(identification).length > 30) + throw new Error('Max allowed number of keys is 30'); + cmd += ' ('; + for (var k in identification) { + if (k.length > 30) + throw new Error('Max allowed key length is 30'); + if (identification[k].length > 1024) + throw new Error('Max allowed value length is 1024'); + cmd += '"' + escape(k) + '" "' + escape(identification[k]) + '"'; + } + cmd += ')'; + } + this._enqueue(cmd, cb); +}; Connection.prototype.openBox = function(name, readOnly, cb) { if (this.state !== 'authenticated') diff --git a/lib/Parser.js b/lib/Parser.js index 6658d12..ea46c43 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -334,10 +334,10 @@ function parseESearch(text, literals) { function parseId(text, literals) { var r = parseExpr(text, literals), id = {}; - - for (var i = 0, len = r[0].length; i < len; i += 2) { + if (r[0] === null) + return null; + for (var i = 0, len = r[0].length; i < len; i += 2) id[r[0][i].toLowerCase()] = r[0][i + 1] - } return id; }