Пример #1
0
		it('should array-ize everything', function() {
			assert.equal(Array.isArray(arrayUtils.toArray([])), true, '[] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray('')), true, '[""] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray([{a: 123}])), true, '[{a: 123}] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray([1])), true, '[1] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray(['a'])), true, '["a"] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray([0])), true, '[0] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray([-1])), true, '[-1] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray('')), true, '[""] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray(0)), true, '[0] is an array');
			assert.equal(Array.isArray(arrayUtils.toArray()), true, '[] is an array');
		});
Пример #2
0
r.prototype.do = function() {
    Term.prototype._arityRange(arguments, 2, Infinity, 'r.do', this);
    var args = helper.toArray(arguments);

    var term = new Term(this).expr(args[0]);
    return term.do.apply(term, args.slice(1));
}
Пример #3
0
 Cursor.prototype[method] = function() {
     var self = this;
     if (self._eventEmitter == null) {
         self._makeEmitter();
         setImmediate(function() {
             self._each(self._eachCb.bind(self), function() {
                 self._eventEmitter.emit('end');
             });
         });
     }
     self._eventEmitter[method].apply(self._eventEmitter, helper.toArray(arguments));
 };
Пример #4
0
Connection.prototype._processResponse = function(response, token) {
    //console.log(JSON.stringify(response, null, 2));
    var self = this;

    var type = response.t;
    var result;
    var cursor;
    var currentResolve, currentReject;
    var datum;
    var options;

    if (type === responseTypes.COMPILE_ERROR) {
        self.emit('release');
        if (typeof self.rejectMap[token] === "function") {
            self.rejectMap[token](new Err.ReqlCompileError(helper.makeAtom(response), self.query[token], response));
        }

        delete self.resolveMap[token];
        delete self.rejectMap[token];
        delete self.query[token];
        delete self.options[token];
        delete self.cursors[token];

    }
    else if (type === responseTypes.CLIENT_ERROR) {
        self.emit('release');
        if (typeof self.rejectMap[token] === "function") {
            self.rejectMap[token](new Err.ReqlClientError(helper.makeAtom(response), self.query[token], response));
        }
        else if (token === -1) { // This should not happen now since 1.13 took the token out of the query
            var error = new Err.ReqlClientError(helper.makeAtom(response)+"\nClosing all outstanding queries...");
            self.emit('error', error);
            // We don't want a function to yield forever, so we just reject everything
            helper.loopKeys(self.rejectMap, function(rejectMap, key) {
                rejectMap[key](error);
            });
            self.close();
        }

        delete self.resolveMap[token];
        delete self.rejectMap[token];
        delete self.query[token];
        delete self.options[token];
        delete self.cursors[token];
    }
    else if (type === responseTypes.RUNTIME_ERROR) {
        self.emit('release');
        if (typeof self.rejectMap[token] === "function") {
            self.rejectMap[token](new Err.ReqlRuntimeError(helper.makeAtom(response), self.query[token], response));
        }

        delete self.resolveMap[token];
        delete self.rejectMap[token];
        delete self.query[token];
        delete self.options[token];
        delete self.cursors[token];
    }
    else if (type === responseTypes.SUCCESS_ATOM) {
        self.emit('release');
        if (typeof self.resolveMap[token] === "function") {
            datum = helper.makeAtom(response, self.options[token]);

            if ((Array.isArray(datum)) && (self.options[token].cursor === true)) {
                cursor = new Cursor(self, token, self.options[token], 'cursor');
                if (self.options[token].profile === true) {
                    self.resolveMap[token]({
                        profile: response.p,
                        result: cursor
                    });
                }
                else {
                    self.resolveMap[token](cursor);
                }

                cursor._push({done: true, response: { r: datum }});
            }
            else {
                if (self.options[token].profile === true) {
                    result = {
                        profile: response.p,
                        result: cursor || datum
                    }
                }
                else {
                    result = datum;
                }
                self.resolveMap[token](result);
            }
        }

        delete self.resolveMap[token];
        delete self.rejectMap[token];
        delete self.query[token];
        delete self.options[token];
    }
    else if (type === responseTypes.SUCCESS_FEED) {
        if (typeof self.resolveMap[token] === "function") {
            // We save the current resolve function because we are going to call cursor._fetch before resuming the user's yield
            currentResolve = self.resolveMap[token];

            delete self.resolveMap[token];
            delete self.rejectMap[token];

            if (!self.cursors[token]) { //No cursor, let's create one
                self.cursors[token] = true;
                cursor = new Cursor(self, token, self.options[token], 'feed');
                cursor._push({done: false, response: response});
                if (self.options[token].profile === true) {
                    result = {
                        profile: response.p,
                        result: cursor
                    }
                    //Next batches will still have a profile (even though it's the same). So it's safe to keep the options
                    //self.options[token].profile = false;

                }
                else {
                    result = cursor
                }
                currentResolve(result);
            }
            else { // That was a continue query
                currentResolve({done: false, response: response});
            }
        }
    }
    else if (type === responseTypes.SUCCESS_PARTIAL) {
        // We save the current resolve function because we are going to call cursor._fetch before resuming the user's yield
        currentResolve = self.resolveMap[token];
        currentReject = self.rejectMap[token];

        // We need to delete before calling cursor._push
        delete self.resolveMap[token];
        delete self.rejectMap[token];

        if (!self.cursors[token]) { //No cursor, let's create one
            self.cursors[token] = true;
            cursor = new Cursor(self, token, self.options[token], 'cursor');

            if (self.options[token].cursor === true) {
                // Return a cursor
                if (self.options[token].profile === true) {
                    currentResolve({
                        profile: response.p,
                        result: cursor
                    });
                }
                else {
                    currentResolve(cursor);
                }
            }
            else {
                // When we get SUCCESS_SEQUENCE, we will delete self.options[token]
                // So we keep a reference of it here
                options = self.options[token];

                // Fetch everything and return an array
                cursor.toArray().then(function(result) {
                    if (options.profile === true) {
                        currentResolve({
                            profile: response.p,
                            result: result
                        });
                    }
                    else {
                        currentResolve(result);
                    }
                }).error(currentReject)
            }
            cursor._push({done: false, response: response});
        }
        else { // That was a continue query
            currentResolve({done: false, response: response});
        }
    }
    else if (type === responseTypes.SUCCESS_SEQUENCE) {
        self.emit('release');

        currentResolve = self.resolveMap[token];
        currentReject = self.rejectMap[token];

        if (!self.cursors[token]) { //No cursor, let's create one
            cursor = new Cursor(self, token, self.options[token], 'cursor');

            if (self.options[token].cursor === true) {
                if (self.options[token].profile === true) {
                    currentResolve({
                        profile: response.p,
                        result: cursor
                    });
                }
                else {
                    currentResolve(cursor);
                }

                // We need to keep the options in the else statement, so we clean it inside the if/else blocks
                delete self.options[token];
            }
            else {
                cursor.toArray().then(function(result) {
                    if (self.options[token].profile === true) {
                        currentResolve({
                            profile: response.p,
                            result: result
                        });
                    }
                    else {
                        currentResolve(result);
                    }
                    delete self.options[token];
                }).error(currentReject)
            }
            cursor._push({done: true, response: response});
        }
        else { // That was a continue query
            currentResolve({done: true, response: response});
            delete self.options[token];
        }

        delete self.resolveMap[token];
        delete self.rejectMap[token];
        delete self.query[token];
        delete self.cursors[token];

    }
    else if (type === responseTypes.WAIT_COMPLETE) {
        self.emit('release');
        self.resolveMap[token]();

        delete self.resolveMap[token];
        delete self.rejectMap[token];
        delete self.query[token];
        delete self.options[token];
        delete self.cursors[token];
    }
}
Пример #5
0
Connection.prototype._processResponse = function(response, token) {
  //console.log('Connection.prototype._processResponse: '+token);
  //console.log(JSON.stringify(response, null, 2));
  var self = this;

  var type = response.t;
  var result;
  var cursor;
  var stream;
  var currentResolve, currentReject;
  var datum;
  var options;

  if (type === responseTypes.COMPILE_ERROR) {
    self.emit('release');
    if (typeof self.metadata[token].reject === 'function') {
      self.metadata[token].reject(new Err.ReqlCompileError(helper.makeAtom(response), self.metadata[token].query, response));
    }

    delete self.metadata[token]
  }
  else if (type === responseTypes.CLIENT_ERROR) {
    self.emit('release');
    if (typeof self.metadata[token].reject === 'function') {
      self.metadata[token].reject(new Err.ReqlClientError(helper.makeAtom(response), self.metadata[token].query, response));
    }
    else if (token === -1) { // This should not happen now since 1.13 took the token out of the query
      var error = new Err.ReqlClientError(helper.makeAtom(response)+'\nClosing all outstanding queries...');
      self.emit('error', error);
      // We don't want a function to yield forever, so we just reject everything
      helper.loopKeys(self.rejectMap, function(rejectMap, key) {
        rejectMap[key](error);
      });
      self.close();
    }

    delete self.metadata[token]
  }
  else if (type === responseTypes.RUNTIME_ERROR) {
    self.emit('release');
    if (typeof self.metadata[token].reject === 'function') {
      self.metadata[token].reject(new Err.ReqlRuntimeError(helper.makeAtom(response), self.metadata[token].query, response));
    }

    delete self.metadata[token]
  }
  else if (type === responseTypes.SUCCESS_ATOM) {
    self.emit('release');
    // self.metadata[token].resolve is always a function
    datum = helper.makeAtom(response, self.metadata[token].options);

    if ((Array.isArray(datum)) &&
        ((self.metadata[token].options.cursor === true) || ((self.metadata[token].options.cursor === undefined) && (self.r._options.cursor === true)))) {
      cursor = new Cursor(self, token, self.metadata[token].options, 'cursor');
      if (self.metadata[token].options.profile === true) {
        self.metadata[token].resolve({
          profile: response.p,
          result: cursor
        });
      }
      else {
        self.metadata[token].resolve(cursor);
      }

      cursor._push({done: true, response: { r: datum }});
    }
    else if ((Array.isArray(datum)) &&
        ((self.metadata[token].options.stream === true || self.r._options.stream === true))) {
      cursor = new Cursor(self, token, self.metadata[token].options, 'cursor');
      stream = new ReadableStream({}, cursor);
      if (self.metadata[token].options.profile === true) {
        self.metadata[token].resolve({
          profile: response.p,
          result: stream 
        });
      }
      else {
        self.metadata[token].resolve(stream);
      }

      cursor._push({done: true, response: { r: datum }});

    }
    else {
      if (self.metadata[token].options.profile === true) {
        result = {
          profile: response.p,
          result: cursor || datum
        }
      }
      else {
        result = datum;
      }
      self.metadata[token].resolve(result);
    }

    delete self.metadata[token];
  }
  else if (type === responseTypes.SUCCESS_PARTIAL) {
    // We save the current resolve function because we are going to call cursor._fetch before resuming the user's yield
    currentResolve = self.metadata[token].resolve;
    currentReject = self.metadata[token].reject;

    // We need to delete before calling cursor._push
    self.metadata[token].removeCallbacks();

    if (!self.metadata[token].cursor) { //No cursor, let's create one
      self.metadata[token].cursor = true;

      var typeResult = 'Cursor';
      var includesStates = false;;
      if (Array.isArray(response.n)) {
        for(var i=0; i<response.n.length; i++) {
          if (response.n[i] === protodef.Response.ResponseNote.SEQUENCE_FEED) {
            typeResult = 'Feed';
          }
          else if (response.n[i] === protodef.Response.ResponseNote.ATOM_FEED) {
            typeResult = 'AtomFeed';
          }
          else if (response.n[i] === protodef.Response.ResponseNote.ORDER_BY_LIMIT_FEED) {
            typeResult = 'OrderByLimitFeed';
          }
          else if (response.n[i] === protodef.Response.ResponseNote.UNIONED_FEED) {
            typeResult = 'UnionedFeed';
          }
          else if (response.n[i] === protodef.Response.ResponseNote.INCLUDES_STATES) {
            includesStates = true;
          }
          else {
            currentReject(new Err.ReqlDriverError('Unknown ResponseNote '+response.n[i]+', the driver is probably out of date.'));
            return;
          }
        }
      }
      cursor = new Cursor(self, token, self.metadata[token].options, typeResult);
      if (includesStates === true) {
        cursor.setIncludesStates();
      }
      if ((self.metadata[token].options.cursor === true) || ((self.metadata[token].options.cursor === undefined) && (self.r._options.cursor === true))) {
        // Return a cursor
        if (self.metadata[token].options.profile === true) {
          currentResolve({
            profile: response.p,
            result: cursor
          });
        }
        else {
          currentResolve(cursor);
        }
      }
      else if ((self.metadata[token].options.stream === true || self.r._options.stream === true)) {
        stream = new ReadableStream({}, cursor);
        if (self.metadata[token].options.profile === true) {
          currentResolve({
            profile: response.p,
            result: stream 
          });
        }
        else {
          currentResolve(stream);
        }
      }
      else if (typeResult !== 'Cursor') {
        // Return a feed
        if (self.metadata[token].options.profile === true) {
          currentResolve({
            profile: response.p,
            result: cursor
          });
        }
        else {
          currentResolve(cursor);
        }
      }
      else {
        // When we get SUCCESS_SEQUENCE, we will delete self.metadata[token].options
        // So we keep a reference of it here
        options = self.metadata[token].options;

        // Fetch everything and return an array
        cursor.toArray().then(function(result) {
          if (options.profile === true) {
            currentResolve({
              profile: response.p,
              result: result
            });
          }
          else {
            currentResolve(result);
          }
        }).error(currentReject)
      }
      cursor._push({done: false, response: response});
    }
    else { // That was a continue query
      currentResolve({done: false, response: response});
    }
  }
  else if (type === responseTypes.SUCCESS_SEQUENCE) {
    self.emit('release');

    if (typeof self.metadata[token].resolve === 'function') {
      currentResolve = self.metadata[token].resolve;
      currentReject = self.metadata[token].reject;
      self.metadata[token].removeCallbacks();
    }
    else if (typeof self.metadata[token].endResolve === 'function') {
      currentResolve = self.metadata[token].endResolve;
      currentReject = self.metadata[token].endReject;
      self.metadata[token].removeEndCallbacks();
    }

    if (!self.metadata[token].cursor) { // No cursor, let's create one
      cursor = new Cursor(self, token, self.metadata[token].options, 'Cursor');

      if ((self.metadata[token].options.cursor === true) || ((self.metadata[token].options.cursor === undefined) && (self.r._options.cursor === true))) {
        if (self.metadata[token].options.profile === true) {
          currentResolve({
            profile: response.p,
            result: cursor
          });
        }
        else {
          currentResolve(cursor);
        }

        // We need to keep the options in the else statement, so we clean it inside the if/else blocks
        delete self.metadata[token];
      }
      else if ((self.metadata[token].options.stream === true || self.r._options.stream === true)) {
        stream = new ReadableStream({}, cursor);
        if (self.metadata[token].options.profile === true) {
          currentResolve({
            profile: response.p,
            result: stream
          });
        }
        else {
          currentResolve(stream);
        }

        // We need to keep the options in the else statement, so we clean it inside the if/else blocks
        delete self.metadata[token];
      }
      else {
        cursor.toArray().then(function(result) {
          if (self.metadata[token].options.profile === true) {
            currentResolve({
              profile: response.p,
              result: result
            });
          }
          else {
            currentResolve(result);
          }
          delete self.metadata[token];
        }).error(currentReject)
      }
      cursor._push({done: true, response: response});
    }
    else { // That was a continue query
      currentResolve({done: true, response: response});
    }
  }
  else if (type === responseTypes.WAIT_COMPLETE) {
    self.emit('release');
    self.metadata[token].resolve();

    delete self.metadata[token];
  }
}
Пример #6
0
r.prototype.circle = function() {
    Term.prototype._arityRange(arguments, 2, 3, 'r.circle', this);
    var term = new Term(this);
    return term.circle.apply(term, helper.toArray(arguments));
}
Пример #7
0
r.prototype.polygon = function() {
    Term.prototype._arityRange(arguments, 3, Infinity, 'r.polygon', this);
    var term = new Term(this);
    return term.polygon.apply(term, helper.toArray(arguments));
}
Пример #8
0
r.prototype.point = function() {
    Term.prototype._arity(arguments, 2, 'r.point', this);
    var term = new Term(this);
    return term.point.apply(term, helper.toArray(arguments));
}
Пример #9
0
r.prototype.line = function() {
    Term.prototype._arityRange(arguments, 2, Infinity, 'r.line', this);
    var term = new Term(this);
    return term.line.apply(term, helper.toArray(arguments));
}
Пример #10
0
r.prototype.http = function() {
    var term = new Term(this);
    return term.http.apply(term, helper.toArray(arguments));
}
Пример #11
0
		it('should handle null and undefined properly', function() {
			assert.equal(arrayUtils.toArray(undefined, '')[0] === '', true, 'undefined was cleaned up as empty string');
			assert.equal(arrayUtils.toArray(undefined)[0] === '', true, 'using the default setup, undefined was cleaned up as empty string');
			assert.equal(arrayUtils.toArray(null, '')[0] === '', true, 'null was cleaned up as empty string');
			assert.equal(arrayUtils.toArray(null)[0] === '', true, 'using the default setup, null was cleaned up as empty string');
		});