Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Buffer #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/bson.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ typedef struct writer_s {
// fprintf(stderr, "write array[%d] (len=%d)\n", i, len);
write(arr->Get(i));
}
} else if (node::Buffer::HasInstance(value)) {
*(current++) = bson::Buffer;
char *data = node::Buffer::Data(value);
uint32_t len = node::Buffer::Length(value);
ensureCapacity(sizeof(uint32_t) + len);
*reinterpret_cast<uint32_t*>(current) = len;
current += sizeof(uint32_t);
for(uint32_t i = 0; i < len; i++) {
*(current++) = data[i];
}
} else { // TODO: support for other object types
*(current++) = bson::Object;
Local<Array> names = obj->GetOwnPropertyNames();
Expand Down Expand Up @@ -216,6 +226,18 @@ static v8::Local<v8::Value> parse(const uint8_t*& data, object_wrapper_t*& objec
}
return curr->object;
}
case bson::Buffer:
len = *reinterpret_cast<const uint32_t*>(data);
data += sizeof(uint32_t);
{
char *retval = new char[len];
for(uint32_t i = 0; i < len; i++) {
retval[i] = data[i];
}

Local<Object> obj = Nan::NewBuffer(retval, len).ToLocalChecked();
return obj;
}
}
assert("should not reach here");
return Local<Value>();
Expand Down
3 changes: 2 additions & 1 deletion src/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace bson {
String,
Array,
Object,
ObjectRef
ObjectRef,
Buffer
} TYPES;

class BSONValue {
Expand Down