/
node_avro.cc
228 lines (184 loc) · 6.71 KB
/
node_avro.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <BinaryEncoder.hh>
#include <Compiler.hh>
#include <Decoder.hh>
#include <Encoder.hh>
#include <Generic.hh>
#include <Specific.hh>
#include <Symbol.hh>
#include <ValidSchema.hh>
#include <buffer/Buffer.hh>
#include <buffer/BufferStream.hh>
#include <json/JsonEncoder.hh>
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
using namespace std;
using namespace v8;
Persistent<ObjectTemplate> avroSchemaTemplate;
Handle<ObjectTemplate> MakeAvroSchemaTemplate() {
HandleScope handle_scope;
Handle<ObjectTemplate> result = ObjectTemplate::New();
result->SetInternalFieldCount(1);
return handle_scope.Close(result);
}
/*
Wraps a parsed schema in a V8 schema object.
*/
Handle<Object> WrapAvroSchema(avro::ValidSchema *schema) {
HandleScope handle_scope;
if (avroSchemaTemplate.IsEmpty()) {
Handle<ObjectTemplate> rawTemplate = MakeAvroSchemaTemplate();
avroSchemaTemplate = Persistent<ObjectTemplate>::New(rawTemplate);
}
Handle<ObjectTemplate> templ = avroSchemaTemplate;
Handle<Object> result = templ->NewInstance();
Handle<External> schemaPtr = External::New(schema);
result->SetInternalField(0, schemaPtr);
return handle_scope.Close(result);
}
/*
Extracts the wrapped schema from a V8 schema object.
*/
avro::ValidSchema *UnwrapAvroSchema(Handle<Object> obj) {
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
void* ptr = field->Value();
return static_cast<avro::ValidSchema*>(ptr);
}
/*
Takes a JSON schema as input and returns a V8 schema object.
*/
static Handle<Value> ParseAvroSchemaInternal(const Arguments& args) {
HandleScope scope;
if (args.Length() != 1) {
return ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
}
Handle<Value> schemaString = args[0];
if (!schemaString->IsString()) {
return ThrowException(Exception::TypeError(String::New("Schema must be a string")));
}
String::Utf8Value schemaUtf8Value(schemaString);
uint8_t *schemaBytes = (uint8_t *) *schemaUtf8Value;
avro::ValidSchema *schema =
new avro::ValidSchema(avro::compileJsonSchemaFromMemory(schemaBytes, schemaUtf8Value.length()));
return WrapAvroSchema(schema);
}
/*
Converts C++ exceptions to V8 exceptions.
*/
static Handle<Value> ParseAvroSchema(const Arguments& args) {
try {
return ParseAvroSchemaInternal(args);
} catch(exception &e) {
return v8::ThrowException(String::New(e.what()));
}
}
/*
Takes two arguments, an Avro schema as a JSON string, and an Avro
value as a JSON string, and returns a buffer containing the encoded
data.
*/
static Handle<Value> JsonStringToAvroBufferInternal(const Arguments& args) {
HandleScope scope;
// Check arguments
if (args.Length() != 2) {
return ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
}
Handle<Object> schemaObject = args[0]->ToObject();
Handle<Value> jsonString = args[1];
if (!jsonString->IsString()) {
return ThrowException(Exception::TypeError(String::New("JSON must be a string")));
}
// Get schema
avro::ValidSchema schema = *UnwrapAvroSchema(schemaObject);
// Decode JSON from string
String::Utf8Value jsonUtf8Value(jsonString);
uint8_t *jsonBytes = (uint8_t *) *jsonUtf8Value;
auto_ptr<avro::InputStream> in = avro::memoryInputStream(jsonBytes, jsonUtf8Value.length());
avro::DecoderPtr decoder = avro::jsonDecoder(schema);
decoder->init(*in);
avro::GenericDatum datum(schema);
avro::decode(*decoder, datum);
// Encode datum to output stream backed by stringstream
stringstream ss(stringstream::out);
auto_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ss);
avro::EncoderPtr encoder = avro::binaryEncoder();
encoder->init(*out);
avro::encode(*encoder, datum);
encoder->flush();
out->flush();
string s = ss.str();
// Copy data to buffer and return it
avro::BinaryEncoder *binaryEncoder = (avro::BinaryEncoder *) &(*encoder);
avro::StreamWriter &sw = binaryEncoder->getStreamWriter();
Handle<Value> buffer = node::Buffer::New((char *) s.data(), sw.getBytesWritten())->handle_;
return scope.Close(buffer);
}
/*
Converts C++ exceptions to V8 exceptions.
*/
static Handle<Value> JsonStringToAvroBuffer(const Arguments& args) {
try {
return JsonStringToAvroBufferInternal(args);
} catch(exception &e) {
return v8::ThrowException(String::New(e.what()));
}
}
/*
Takes two arguments, an Avro schema as a JSON string, and a buffer
containing encoded data, and returns a JSON string containing the
decoded Avro value.
*/
static Handle<Value> AvroBufferToJsonStringInternal(const Arguments& args) {
HandleScope scope;
// Check arguments
if (args.Length() != 2) {
return ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
}
Handle<Object> schemaObject = args[0]->ToObject();
Handle<Value> avroBuffer = args[1];
if (!node::Buffer::HasInstance(avroBuffer)) {
return ThrowException(Exception::TypeError(String::New("Not a buffer")));
}
// Get schema
avro::ValidSchema schema = *UnwrapAvroSchema(schemaObject);
// Decode data from buffer
auto_ptr<avro::InputStream> in =
avro::memoryInputStream((uint8_t *) node::Buffer::Data(avroBuffer->ToObject()),
node::Buffer::Length(avroBuffer->ToObject()));
avro::DecoderPtr decoder = avro::binaryDecoder();
decoder->init(*in);
avro::GenericDatum datum(schema);
avro::decode(*decoder, datum);
// Encode data to JSON
stringstream ss(stringstream::out);
auto_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ss);
avro::EncoderPtr encoder = avro::jsonEncoder(schema);
encoder->init(*out);
avro::encode(*encoder, datum);
encoder->flush();
out->flush();
string s = ss.str();
avro::parsing::JsonEncoder<avro::parsing::SimpleParser<avro::parsing::JsonHandler> > *jsonEncoder =
(avro::parsing::JsonEncoder<avro::parsing::SimpleParser<avro::parsing::JsonHandler> > *) &(*encoder);
avro::StreamWriter &sw = jsonEncoder->getStreamWriter();
return scope.Close(String::New((char *) s.data(), sw.getBytesWritten()));
}
/*
Converts C++ exceptions to V8 exceptions.
*/
static Handle<Value> AvroBufferToJsonString(const Arguments& args) {
try {
return AvroBufferToJsonStringInternal(args);
} catch(exception &e) {
return v8::ThrowException(String::New(e.what()));
}
}
void init(Handle<Object> target) {
target->Set(String::NewSymbol("jsonStringToAvroBuffer"),
FunctionTemplate::New(JsonStringToAvroBuffer)->GetFunction());
target->Set(String::NewSymbol("avroBufferToJsonString"),
FunctionTemplate::New(AvroBufferToJsonString)->GetFunction());
target->Set(String::NewSymbol("parseSchema"),
FunctionTemplate::New(ParseAvroSchema)->GetFunction());
}
NODE_MODULE(avro, init)