Skip to content

Nodedb should handle undefined and Buffer as input #12

@qraynaud

Description

@qraynaud

I believe nodedb should support Buffer as a standard input type because they are the (only ?) "correct" way to handle binary in nodejs. I also believe undefined is quite like NULL in a database and added support for this too because this looks sensitive.

I wrote the following patch to do just that (sorry I'm not a git user and thus won't make a pull request directly on github) :

--- a/query.cc    2012-08-02 18:05:56.920615897 +0200
+++ b/query.cc  2012-08-02 18:17:57.772591855 +0200
@@ -1392,7 +1392,7 @@
 std::string node_db::Query::value(v8::Local<v8::Value> value, bool inArray, bool escape, int precision) const throw(node_db::Exception&) {
     std::ostringstream currentStream;

-    if (value->IsNull()) {
+    if (value->IsNull() || value->IsUndefined()) {
         currentStream << "NULL";
     } else if (value->IsArray()) {
         v8::Local<v8::Array> array = v8::Array::Cast(*value);
@@ -1419,8 +1419,17 @@
         v8::Handle<v8::String> valueKey = v8::String::New("value");
         v8::Handle<v8::String> escapeKey = v8::String::New("escape");

-        if (object->Has(valueKey)) {
+        if (node::Buffer::HasInstance(object)) {
+            size_t length = node::Buffer::Length(object);
+            std::string string(node::Buffer::Data(object), length);
+
+            try {
+              currentStream << this->connection->quoteString << this->connection->escape(string) << this->connection->quoteString;
+            } catch(node_db::Exception& exception) {
+              throw new node_db::Exception("Escaping of binary string failed: cannot continue");
+            }
+        } else if (object->Has(valueKey)) {
             v8::Handle<v8::String> precisionKey = v8::String::New("precision");
             int precision = -1;

This patch was written on the released file but it should work on the newer one without much adaptation.

Thanks a lot for considering this little addition to your base code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions