Skip to content

Commit 100c93f

Browse files
committed
Le dump
1 parent 47e66cc commit 100c93f

5 files changed

Lines changed: 262 additions & 0 deletions

File tree

‎Makefile‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
C_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I ../v0.15/uSockets/src ../v0.15/uSockets/src/*.c ../v0.15/uSockets/src/eventing/*.c
2+
3+
CPP_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I ../v0.15/uSockets/src -I ../v0.15/src src/addon.cpp
4+
5+
CPP_OSX := -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup
6+
7+
default:
8+
make targets
9+
NODE=targets/node-v8.1.2 ABI=57 make `(uname -s)`
10+
NODE=targets/node-v9.2.0 ABI=59 make `(uname -s)`
11+
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
12+
for f in dist/*.node; do chmod +x $$f; done
13+
targets:
14+
mkdir targets
15+
curl https://nodejs.org/dist/v8.1.2/node-v8.1.2-headers.tar.gz | tar xz -C targets
16+
curl https://nodejs.org/dist/v9.2.0/node-v9.2.0-headers.tar.gz | tar xz -C targets
17+
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets
18+
Linux:
19+
gcc $(C_SHARED)
20+
g++ $(CPP_SHARED) -I $$NODE/include/node
21+
g++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node
22+
Darwin:
23+
g++ $(CPP_SHARED) $(CPP_OSX) -I $$NODE/include/node -o dist/uws_darwin_$$ABI.node
24+
.PHONY: clean
25+
clean:
26+
rm -f dist/README.md
27+
rm -f dist/LICENSE
28+
rm -f dist/uws_*.node
29+
rm -f dist/uws.js
30+
rm -rf dist/src
31+
rm -rf targets

‎dist/uws.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = (() => {
2+
try {
3+
return require(`./uws_${process.platform}_${process.versions.modules}.node`);
4+
} catch (e) {
5+
throw new Error('This version of µWS is not compatible with your Node.js build.');
6+
}
7+
})();

‎examples/HelloWorld.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const uWS = require('../dist/uws.js');
2+
3+
const port = 3000;
4+
5+
uWS.App().get('/hello', (res, req) => {
6+
res.end('world!');
7+
}).get('/*', (res, req) => {
8+
res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!');
9+
}).listen(port, (token) => {
10+
if (token) {
11+
console.log('Listening to port ' + port);
12+
} else {
13+
console.log('Failed to listen to port ' + port);
14+
}
15+
});
16+
17+
console.log('Timers will not work until us_loop_integrate is done');

‎make.bat‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
call "%VS140COMNTOOLS%..\..\vc\vcvarsall.bat" amd64
2+
3+
if not exist targets (
4+
mkdir targets
5+
curl https://nodejs.org/dist/v8.1.2/node-v8.1.2-headers.tar.gz | tar xz -C targets
6+
curl https://nodejs.org/dist/v8.1.2/win-x64/node.lib > targets/node-v8.1.2/node.lib
7+
curl https://nodejs.org/dist/v9.2.0/node-v9.2.0-headers.tar.gz | tar xz -C targets
8+
curl https://nodejs.org/dist/v9.2.0/win-x64/node.lib > targets/node-v9.2.0/node.lib
9+
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets
10+
curl https://nodejs.org/dist/v10.0.0/win-x64/node.lib > targets/node-v10.0.0/node.lib
11+
)
12+
13+
cp README.md dist/README.md
14+
cp ../uWebSockets/LICENSE dist/LICENSE
15+
cp -r ../uWebSockets/src dist/
16+
cp src/addon.cpp dist/src/addon.cpp
17+
cp src/addon.h dist/src/addon.h
18+
cp src/http.h dist/src/http.h
19+
cp src/uws.js dist/uws.js
20+
21+
cl /I targets/node-v8.1.2/include/node /EHsc /Ox /LD /Fedist/uws_win32_57.node dist/src/*.cpp targets/node-v8.1.2/node.lib
22+
cl /I targets/node-v9.2.0/include/node /EHsc /Ox /LD /Fedist/uws_win32_59.node dist/src/*.cpp targets/node-v9.2.0/node.lib
23+
cl /I targets/node-v10.0.0/include/node /EHsc /Ox /LD /Fedist/uws_win32_64.node dist/src/*.cpp targets/node-v10.0.0/node.lib
24+
25+
rm *.obj
26+
rm dist/*.exp
27+
rm dist/*.lib

‎src/addon.cpp‎

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)