|
| 1 | +#include <uv.h> |
| 2 | +#include <node.h> |
| 3 | +#include <node_buffer.h> |
| 4 | +#include <iostream> |
| 5 | +using namespace v8; |
| 6 | + |
| 7 | +class NativeString { |
| 8 | + char *data; |
| 9 | + size_t length; |
| 10 | + char utf8ValueMemory[sizeof(String::Utf8Value)]; |
| 11 | + String::Utf8Value *utf8Value = nullptr; |
| 12 | +public: |
| 13 | + NativeString(const Local<Value> &value) { |
| 14 | + if (value->IsUndefined()) { |
| 15 | + data = nullptr; |
| 16 | + length = 0; |
| 17 | + } else if (value->IsString()) { |
| 18 | + utf8Value = new (utf8ValueMemory) String::Utf8Value(value); |
| 19 | + data = (**utf8Value); |
| 20 | + length = utf8Value->length(); |
| 21 | + } else if (node::Buffer::HasInstance(value)) { |
| 22 | + data = node::Buffer::Data(value); |
| 23 | + length = node::Buffer::Length(value); |
| 24 | + } else if (value->IsTypedArray()) { |
| 25 | + Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast(value); |
| 26 | + ArrayBuffer::Contents contents = arrayBufferView->Buffer()->GetContents(); |
| 27 | + length = contents.ByteLength(); |
| 28 | + data = (char *) contents.Data(); |
| 29 | + } else if (value->IsArrayBuffer()) { |
| 30 | + Local<ArrayBuffer> arrayBuffer = Local<ArrayBuffer>::Cast(value); |
| 31 | + ArrayBuffer::Contents contents = arrayBuffer->GetContents(); |
| 32 | + length = contents.ByteLength(); |
| 33 | + data = (char *) contents.Data(); |
| 34 | + } else { |
| 35 | + static char empty[] = ""; |
| 36 | + data = empty; |
| 37 | + length = 0; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + char *getData() {return data;} |
| 42 | + size_t getLength() {return length;} |
| 43 | + ~NativeString() { |
| 44 | + if (utf8Value) { |
| 45 | + utf8Value->~Utf8Value(); |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +#include "App.h" |
| 51 | +Isolate *isolate; |
| 52 | +Persistent<Object> resTemplate; |
| 53 | +Persistent<Object> reqTemplate; |
| 54 | + |
| 55 | +void res_end(const FunctionCallbackInfo<Value> &args) { |
| 56 | + |
| 57 | + // you might want to do extra work here to swap to tryEnd if passed a Buffer? |
| 58 | + // or always use tryEnd and simply grab the object as persistent? |
| 59 | + |
| 60 | + NativeString data(args[0]); |
| 61 | + ((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength())); |
| 62 | + |
| 63 | + // Return this |
| 64 | + args.GetReturnValue().Set(args.Holder()); |
| 65 | +} |
| 66 | + |
| 67 | +void res_writeHeader(const FunctionCallbackInfo<Value> &args) { |
| 68 | + // get string |
| 69 | + NativeString header(args[0]); |
| 70 | + NativeString value(args[1]); |
| 71 | + |
| 72 | + ((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), std::string_view(value.getData(), value.getLength())); |
| 73 | + |
| 74 | + args.GetReturnValue().Set(args.Holder()); |
| 75 | +} |
| 76 | + |
| 77 | +void req_getHeader(const FunctionCallbackInfo<Value> &args) { |
| 78 | + // get string |
| 79 | + NativeString data(args[0]); |
| 80 | + char *buf = data.getData(); int length = data.getLength(); |
| 81 | + |
| 82 | + std::string_view header = ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0))->getHeader(std::string_view(buf, length)); |
| 83 | + |
| 84 | + args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length())); |
| 85 | +} |
| 86 | + |
| 87 | +void uWS_App_get(const FunctionCallbackInfo<Value> &args) { |
| 88 | + uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0); |
| 89 | + |
| 90 | + NativeString nativeString(args[0]); |
| 91 | + |
| 92 | + Persistent<Function> *pf = new Persistent<Function>(); |
| 93 | + pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1])); |
| 94 | + |
| 95 | + app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) { |
| 96 | + HandleScope hs(isolate); |
| 97 | + |
| 98 | + Local<Object> resObject = Local<Object>::New(isolate, resTemplate)->Clone(); |
| 99 | + resObject->SetAlignedPointerInInternalField(0, res); |
| 100 | + |
| 101 | + Local<Object> reqObject = Local<Object>::New(isolate, reqTemplate)->Clone(); |
| 102 | + reqObject->SetAlignedPointerInInternalField(0, req); |
| 103 | + |
| 104 | + // node:: is horrible but |
| 105 | + Local<Value> argv[] = {resObject, reqObject}; |
| 106 | + node::MakeCallback(isolate, isolate->GetCurrentContext()->Global(), Local<Function>::New(isolate, *pf), 2, argv); |
| 107 | + |
| 108 | + }); |
| 109 | + |
| 110 | + args.GetReturnValue().Set(args.Holder()); |
| 111 | +} |
| 112 | + |
| 113 | +// port, callback? |
| 114 | +void uWS_App_listen(const FunctionCallbackInfo<Value> &args) { |
| 115 | + uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0); |
| 116 | + |
| 117 | + int port = args[0]->Uint32Value(); |
| 118 | + |
| 119 | + app->listen(port, [&args](auto *token) { |
| 120 | + Local<Value> argv[] = {Boolean::New(isolate, token)}; |
| 121 | + Local<Function>::Cast(args[1])->Call(isolate->GetCurrentContext()->Global(), 1, argv); |
| 122 | + }); |
| 123 | + |
| 124 | + // Return this |
| 125 | + args.GetReturnValue().Set(args.Holder()); |
| 126 | +} |
| 127 | + |
| 128 | +// uWS.App() -> object |
| 129 | +void uWS_App(const FunctionCallbackInfo<Value> &args) { |
| 130 | + // uWS.App |
| 131 | + Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate); |
| 132 | + appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App")); |
| 133 | + appTemplate->InstanceTemplate()->SetInternalFieldCount(1); |
| 134 | + |
| 135 | + // Get |
| 136 | + appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get)); |
| 137 | + |
| 138 | + // Listen |
| 139 | + appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen)); |
| 140 | + |
| 141 | + // Instantiate and set intenal pointer |
| 142 | + Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); |
| 143 | + |
| 144 | + // Delete this boy |
| 145 | + localApp->SetAlignedPointerInInternalField(0, new uWS::App()); |
| 146 | + |
| 147 | + // Return an instance of this shit |
| 148 | + args.GetReturnValue().Set(localApp); |
| 149 | +} |
| 150 | + |
| 151 | +void Main(Local<Object> exports) { |
| 152 | + isolate = exports->GetIsolate(); |
| 153 | + |
| 154 | + /*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url); |
| 155 | + reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/ |
| 156 | + |
| 157 | + // App is a function returning an object |
| 158 | + NODE_SET_METHOD(exports, "App", uWS_App); |
| 159 | + |
| 160 | + // HttpResponse template |
| 161 | + Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate); |
| 162 | + resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse")); |
| 163 | + resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1); |
| 164 | + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, res_end)); |
| 165 | + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader)); |
| 166 | + |
| 167 | + Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); |
| 168 | + resTemplate.Reset(isolate, resObjectLocal); |
| 169 | + |
| 170 | + // Request template |
| 171 | + Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate); |
| 172 | + reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest")); |
| 173 | + reqTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1); |
| 174 | + reqTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getHeader"), FunctionTemplate::New(isolate, req_getHeader)); |
| 175 | + |
| 176 | + Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); |
| 177 | + reqTemplate.Reset(isolate, reqObjectLocal); |
| 178 | +} |
| 179 | + |
| 180 | +NODE_MODULE(uWS, Main) |
0 commit comments