Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 221 lines (197 sloc) 7.908 kb
8c3c334 @kesla cleanup, use node-specific macro
authored
1 // Copyright (c) 2011 David Björklund
8bd2543 @kesla Readme and license.
authored
2 //
8c3c334 @kesla cleanup, use node-specific macro
authored
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
8bd2543 @kesla Readme and license.
authored
9 //
8c3c334 @kesla cleanup, use node-specific macro
authored
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
8bd2543 @kesla Readme and license.
authored
12 //
8c3c334 @kesla cleanup, use node-specific macro
authored
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
8bd2543 @kesla Readme and license.
authored
20
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
21 #include "./binding.h"
8bd2543 @kesla Readme and license.
authored
22
bd90848 @kesla Initial commit. Very early days.
authored
23 #include <node_buffer.h>
ce94c47 @kesla follow google style guide
authored
24 #include <snappy.h>
bd90848 @kesla Initial commit. Very early days.
authored
25
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
26 #include <string.h> // memcpy
27
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
28 #include <string>
29
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
30 namespace nodesnappy {
f07ab64 @kesla Cleanup
authored
31 template<class T> SnappyRequest<T>::SnappyRequest(const v8::Arguments& args){
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
32 v8::Local<v8::Object> object = args[0]->ToObject();
33 size_t length = node::Buffer::Length(object);
34 const char *data = node::Buffer::Data(object);
35 input = std::string(data, length);
f07ab64 @kesla Cleanup
authored
36 v8::Local<v8::Function> local = v8::Local<v8::Function>::Cast(args[1]);
37 callback = v8::Persistent<v8::Function>::New(local);
38 err = NULL;
39 }
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
40
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
41 const std::string SnappyErrors::kInvalidInput = "Invalid input";
42
43 // Base
44 // PROTECTED
45 inline void Base::CallCallback(const v8::Handle<v8::Function>& callback,
46 const v8::Handle<v8::Value>& err,
47 const v8::Handle<v8::Value>& res) {
48 v8::Handle<v8::Value> argv[2] = {err, res};
49 callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
50 }
51
52 inline void Base::CallErrCallback(const v8::Handle<v8::Function>& callback,
53 const std::string& str) {
54 v8::Handle<v8::Value> err =
55 v8::Exception::Error(v8::String::New(str.data(), str.length()));
56 v8::Handle<v8::Value> res = v8::Local<v8::Value>::New(v8::Null());
57 CallCallback(callback, err, res);
58 }
59
60 // CompressUncompressBase
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
61 // PROTECTED
285db5e @kesla Hopefully added missing bits for 0.6-compability
authored
62 async_rtn CompressUncompressBase::After(uv_work_t *req) {
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
63 v8::HandleScope scope;
f07ab64 @kesla Cleanup
authored
64 SnappyRequest<std::string>* snappy_req =
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
65 static_cast<SnappyRequest<std::string>*>(req->data);
f07ab64 @kesla Cleanup
authored
66 if (snappy_req->err != NULL) {
67 CallErrCallback(snappy_req->callback, *snappy_req->err);
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
68 } else {
f07ab64 @kesla Cleanup
authored
69 CallOkCallback(snappy_req->callback, snappy_req->result);
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
70 }
71
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
72 ev_unref(EV_DEFAULT_UC);
f07ab64 @kesla Cleanup
authored
73 snappy_req->callback.Dispose();
74 delete snappy_req;
bd15b6a @kesla Add support for node 0.6 with help of TooTallNate's shim.
authored
75 RETURN_ASYNC_AFTER;
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
76 }
77
9f56b33 @kesla Cleanup & refactoring.
authored
78 inline void
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
79 CompressUncompressBase::CallOkCallback(const v8::Handle<v8::Function>& callback,
80 const std::string& str) {
81 v8::Handle<v8::Value> err = v8::Local<v8::Value>::New(v8::Null());
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
82 node::Buffer* res = node::Buffer::New(str.length());
83 memcpy(node::Buffer::Data(res), str.c_str(), str.length());
84 CallCallback(callback, err, res->handle_);
bd90848 @kesla Initial commit. Very early days.
authored
85 }
86
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
87 // CompressBinding
88 // PUBLIC
89 v8::Handle<v8::Value> CompressBinding::Async(const v8::Arguments& args) {
bd90848 @kesla Initial commit. Very early days.
authored
90 v8::HandleScope scope;
f07ab64 @kesla Cleanup
authored
91 SnappyRequest<std::string>* snappy_req = new SnappyRequest<std::string>(args);
bd15b6a @kesla Add support for node 0.6 with help of TooTallNate's shim.
authored
92 BEGIN_ASYNC(snappy_req, AsyncOperation, After);
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
93 return v8::Undefined();
94 }
95
96 v8::Handle<v8::Value> CompressBinding::Sync(const v8::Arguments& args) {
97 v8::HandleScope scope;
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
98 v8::Local<v8::Object> input = args[0]->ToObject();
99 size_t length = node::Buffer::Length(input);
100 char *data = node::Buffer::Data(input);
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
101 std::string dst;
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
102 snappy::Compress(data, length, &dst);
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
103 CallOkCallback(v8::Local<v8::Function>::Cast(args[1]), dst);
fb9b73a @kesla Call callback directly in binding.cc. Preparation for async-versions.
authored
104 return scope.Close(v8::Undefined());
bd90848 @kesla Initial commit. Very early days.
authored
105 }
106
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
107 // PRIVATE
285db5e @kesla Hopefully added missing bits for 0.6-compability
authored
108 async_rtn CompressBinding::AsyncOperation(uv_work_t *req) {
f07ab64 @kesla Cleanup
authored
109 SnappyRequest<std::string>* snappy_req =
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
110 static_cast<SnappyRequest<std::string>*>(req->data);
111 std::string dst;
f07ab64 @kesla Cleanup
authored
112 std::string* input = &snappy_req->input;
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
113 snappy::Compress(input->data(), input->length(), &dst);
f07ab64 @kesla Cleanup
authored
114 snappy_req->result = dst;
bd15b6a @kesla Add support for node 0.6 with help of TooTallNate's shim.
authored
115 RETURN_ASYNC;
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
116 }
117
118 // UncompressBinding
119 // PUBLIC
120 v8::Handle<v8::Value> UncompressBinding::Async(const v8::Arguments& args) {
4d2e833 @kesla support isValidCompressed-method.
authored
121 v8::HandleScope scope;
f07ab64 @kesla Cleanup
authored
122 SnappyRequest<std::string>* snappy_req = new SnappyRequest<std::string>(args);
bd15b6a @kesla Add support for node 0.6 with help of TooTallNate's shim.
authored
123 BEGIN_ASYNC(snappy_req, AsyncOperation, After);
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
124 return v8::Undefined();
125 }
126
127 v8::Handle<v8::Value> UncompressBinding::Sync(const v8::Arguments& args) {
128 v8::HandleScope scope;
129 std::string dst;
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
130 v8::Local<v8::Object> input = args[0]->ToObject();
131 size_t length = node::Buffer::Length(input);
132 char *data = node::Buffer::Data(input);
f07ab64 @kesla Cleanup
authored
133 v8::Handle<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
134 if (snappy::Uncompress(data, length, &dst)) {
f07ab64 @kesla Cleanup
authored
135 CallOkCallback(callback, dst);
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
136 } else {
f07ab64 @kesla Cleanup
authored
137 CallErrCallback(callback, SnappyErrors::kInvalidInput);
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
138 }
fb9b73a @kesla Call callback directly in binding.cc. Preparation for async-versions.
authored
139 return scope.Close(v8::Undefined());
4d2e833 @kesla support isValidCompressed-method.
authored
140 }
141
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
142 // PRIVATE
285db5e @kesla Hopefully added missing bits for 0.6-compability
authored
143 async_rtn UncompressBinding::AsyncOperation(uv_work_t *req) {
f07ab64 @kesla Cleanup
authored
144 SnappyRequest<std::string>* snappy_req =
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
145 static_cast<SnappyRequest<std::string>*>(req->data);
146 std::string dst;
f07ab64 @kesla Cleanup
authored
147 std::string* input = &snappy_req->input;
148 if (snappy::Uncompress(input->data(), input->length(), &dst)) {
149 snappy_req->result = dst;
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
150 } else {
f07ab64 @kesla Cleanup
authored
151 snappy_req->err = &SnappyErrors::kInvalidInput;
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
152 }
bd15b6a @kesla Add support for node 0.6 with help of TooTallNate's shim.
authored
153 RETURN_ASYNC;
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
154 }
155
156 // IsValidCompressedBinding
157 // PUBLIC
158 v8::Handle<v8::Value>
159 IsValidCompressedBinding::Async(const v8::Arguments& args) {
bd90848 @kesla Initial commit. Very early days.
authored
160 v8::HandleScope scope;
161 std::string dst;
cfb41cc @kesla cleanup
authored
162 v8::String::Utf8Value data(args[0]->ToString());
f07ab64 @kesla Cleanup
authored
163 SnappyRequest<bool>* snappy_req = new SnappyRequest<bool>(args);
bd15b6a @kesla Add support for node 0.6 with help of TooTallNate's shim.
authored
164 BEGIN_ASYNC(snappy_req, AsyncOperation, After);
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
165 return v8::Undefined();
166 }
167
168 v8::Handle<v8::Value>
169 IsValidCompressedBinding::Sync(const v8::Arguments& args) {
170 v8::HandleScope scope;
171 std::string dst;
1f90d16 Handle non utf8-data correctly (closes #5, and possibly also #6)
David Björklund authored
172 v8::Local<v8::Object> input = args[0]->ToObject();
173 size_t length = node::Buffer::Length(input);
174 char *data = node::Buffer::Data(input);
175 bool valid = snappy::IsValidCompressedBuffer(data, length);
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
176 CallOkCallback(v8::Local<v8::Function>::Cast(args[1]), valid);
fb9b73a @kesla Call callback directly in binding.cc. Preparation for async-versions.
authored
177 return scope.Close(v8::Undefined());
bd90848 @kesla Initial commit. Very early days.
authored
178 }
179
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
180 // PRIVATE
285db5e @kesla Hopefully added missing bits for 0.6-compability
authored
181 async_rtn IsValidCompressedBinding::After(uv_work_t *req) {
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
182 v8::HandleScope scope;
f07ab64 @kesla Cleanup
authored
183 SnappyRequest<bool>* snappy_req =
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
184 static_cast<SnappyRequest<bool>*>(req->data);
f07ab64 @kesla Cleanup
authored
185 CallOkCallback(snappy_req->callback, snappy_req->result);
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
186 ev_unref(EV_DEFAULT_UC);
f07ab64 @kesla Cleanup
authored
187 snappy_req->callback.Dispose();
188 delete snappy_req;
bd15b6a @kesla Add support for node 0.6 with help of TooTallNate's shim.
authored
189 RETURN_ASYNC_AFTER;
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
190 }
191
285db5e @kesla Hopefully added missing bits for 0.6-compability
authored
192 async_rtn IsValidCompressedBinding::AsyncOperation(uv_work_t *req) {
f07ab64 @kesla Cleanup
authored
193 SnappyRequest<bool>* snappy_req = (SnappyRequest<bool>*) req->data;
194 std::string* input = &snappy_req->input;
195 snappy_req->result =
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
196 snappy::IsValidCompressedBuffer(input->data(), input->length());
285db5e @kesla Hopefully added missing bits for 0.6-compability
authored
197 RETURN_ASYNC;
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
198 }
199
9f56b33 @kesla Cleanup & refactoring.
authored
200 inline void
bab290e @kesla When doing a uncompress, return error if uncompression fails. For now, a...
authored
201 IsValidCompressedBinding::CallOkCallback(
202 const v8::Handle<v8::Function>& callback,
203 const bool data) {
f07ab64 @kesla Cleanup
authored
204 v8::Local<v8::Value> err = v8::Local<v8::Value>::New(v8::Null());
205 v8::Local<v8::Value> res = v8::Local<v8::Value>::New(v8::Boolean::New(data));
206 CallCallback(callback, err, res);
9f56b33 @kesla Cleanup & refactoring.
authored
207 }
208
bd90848 @kesla Initial commit. Very early days.
authored
209 extern "C" void
ce94c47 @kesla follow google style guide
authored
210 init(v8::Handle<v8::Object> target) {
bd90848 @kesla Initial commit. Very early days.
authored
211 v8::HandleScope scope;
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
212 NODE_SET_METHOD(target, "compress", CompressBinding::Async);
213 NODE_SET_METHOD(target, "compressSync", CompressBinding::Sync);
214 NODE_SET_METHOD(target, "uncompress", UncompressBinding::Async);
215 NODE_SET_METHOD(target, "uncompressSync", UncompressBinding::Sync);
216 NODE_SET_METHOD(target, "isValidCompressed", IsValidCompressedBinding::Async);
217 NODE_SET_METHOD(target, "isValidCompressedSync",
218 IsValidCompressedBinding::Sync);
bd90848 @kesla Initial commit. Very early days.
authored
219 }
8018dc9 @kesla compress/uncompress/isValidCompressed are now async, with synchronous ve...
authored
220 } // namespace nodesnappy
Something went wrong with that request. Please try again.