Skip to content

Commit

Permalink
Message, feat: support method json.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Aug 29, 2017
1 parent 456cd3e commit c4a7069
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions fibjs/include/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class HttpRequest : public HttpRequest_base {
virtual result_t read(int32_t bytes, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t readAll(obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t write(Buffer_base* data, AsyncEvent* ac);
virtual result_t json(v8::Local<v8::Value> data, AsyncEvent* ac);
virtual result_t get_length(int64_t& retVal);
virtual result_t end();
virtual result_t isEnded(bool& retVal);
Expand Down
1 change: 1 addition & 0 deletions fibjs/include/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class HttpResponse : public HttpResponse_base {
virtual result_t read(int32_t bytes, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t readAll(obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t write(Buffer_base* data, AsyncEvent* ac);
virtual result_t json(v8::Local<v8::Value> data, AsyncEvent* ac);
virtual result_t get_length(int64_t& retVal);
virtual result_t end();
virtual result_t isEnded(bool& retVal);
Expand Down
1 change: 1 addition & 0 deletions fibjs/include/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Message : public Message_base {
virtual result_t read(int32_t bytes, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t readAll(obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t write(Buffer_base* data, AsyncEvent* ac);
virtual result_t json(v8::Local<v8::Value> data, AsyncEvent* ac);
virtual result_t get_length(int64_t& retVal);
virtual result_t end();
virtual result_t isEnded(bool& retVal);
Expand Down
1 change: 1 addition & 0 deletions fibjs/include/WebSocketMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class WebSocketMessage : public WebSocketMessage_base {
virtual result_t read(int32_t bytes, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t readAll(obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t write(Buffer_base* data, AsyncEvent* ac);
virtual result_t json(v8::Local<v8::Value> data, AsyncEvent* ac);
virtual result_t get_length(int64_t& retVal);
virtual result_t end();
virtual result_t isEnded(bool& retVal);
Expand Down
1 change: 1 addition & 0 deletions fibjs/include/WorkerMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class WorkerMessage : public Message_base {
virtual result_t read(int32_t bytes, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t readAll(obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
virtual result_t write(Buffer_base* data, AsyncEvent* ac);
virtual result_t json(v8::Local<v8::Value> data, AsyncEvent* ac);
virtual result_t get_length(int64_t& retVal);
virtual result_t end();
virtual result_t isEnded(bool& retVal);
Expand Down
23 changes: 23 additions & 0 deletions fibjs/include/ifs/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Message_base : public object_base {
virtual result_t read(int32_t bytes, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac) = 0;
virtual result_t readAll(obj_ptr<Buffer_base>& retVal, AsyncEvent* ac) = 0;
virtual result_t write(Buffer_base* data, AsyncEvent* ac) = 0;
virtual result_t json(v8::Local<v8::Value> data, AsyncEvent* ac) = 0;
virtual result_t get_length(int64_t& retVal) = 0;
virtual result_t end() = 0;
virtual result_t isEnded(bool& retVal) = 0;
Expand Down Expand Up @@ -76,6 +77,7 @@ class Message_base : public object_base {
static void s_read(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_readAll(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_write(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_json(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_end(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_isEnded(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -91,6 +93,7 @@ class Message_base : public object_base {
ASYNC_MEMBERVALUE2(Message_base, read, int32_t, obj_ptr<Buffer_base>);
ASYNC_MEMBERVALUE1(Message_base, readAll, obj_ptr<Buffer_base>);
ASYNC_MEMBER1(Message_base, write, Buffer_base*);
ASYNC_MEMBER1(Message_base, json, v8::Local<v8::Value>);
ASYNC_MEMBER1(Message_base, sendTo, Stream_base*);
ASYNC_MEMBER1(Message_base, readFrom, Stream_base*);
};
Expand All @@ -111,6 +114,8 @@ inline ClassInfo& Message_base::class_info()
{ "readAllSync", s_readAll, false },
{ "write", s_write, false },
{ "writeSync", s_write, false },
{ "json", s_json, false },
{ "jsonSync", s_json, false },
{ "end", s_end, false },
{ "isEnded", s_isEnded, false },
{ "clear", s_clear, false },
Expand Down Expand Up @@ -338,6 +343,24 @@ inline void Message_base::s_write(const v8::FunctionCallbackInfo<v8::Value>& arg
METHOD_VOID();
}

inline void Message_base::s_json(const v8::FunctionCallbackInfo<v8::Value>& args)
{
METHOD_INSTANCE(Message_base);
METHOD_ENTER();

ASYNC_METHOD_OVER(1, 1);

ARG(v8::Local<v8::Value>, 0);

if (!cb.IsEmpty()) {
pInst->acb_json(v0, cb);
hr = CALL_RETURN_NULL;
} else
hr = pInst->ac_json(v0);

METHOD_VOID();
}

inline void Message_base::s_get_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
int64_t vr;
Expand Down
5 changes: 5 additions & 0 deletions fibjs/src/coroutine/WorkerMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ result_t WorkerMessage::write(Buffer_base* data, AsyncEvent* ac)
return m_message->write(data, ac);
}

result_t WorkerMessage::json(v8::Local<v8::Value> data, AsyncEvent* ac)
{
return m_message->json(data, ac);
}

result_t WorkerMessage::get_length(int64_t& retVal)
{
return m_message->get_length(retVal);
Expand Down
8 changes: 8 additions & 0 deletions fibjs/src/http/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ result_t HttpRequest::write(Buffer_base* data, AsyncEvent* ac)
return m_message->write(data, ac);
}

result_t HttpRequest::json(v8::Local<v8::Value> data, AsyncEvent* ac)
{
if (ac->isSync())
m_message->setHeader("Content-Type", "application/json");

return m_message->json(data, ac);
}

result_t HttpRequest::get_length(int64_t& retVal)
{
return m_message->get_length(retVal);
Expand Down
8 changes: 8 additions & 0 deletions fibjs/src/http/HttpResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ result_t HttpResponse::write(Buffer_base* data, AsyncEvent* ac)
return m_message->write(data, ac);
}

result_t HttpResponse::json(v8::Local<v8::Value> data, AsyncEvent* ac)
{
if (ac->isSync())
m_message->setHeader("Content-Type", "application/json");

return m_message->json(data, ac);
}

result_t HttpResponse::get_length(int64_t& retVal)
{
return m_message->get_length(retVal);
Expand Down
20 changes: 20 additions & 0 deletions fibjs/src/mq/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "Message.h"
#include "List.h"
#include "MemoryStream.h"
#include "Buffer.h"
#include "ifs/json.h"

namespace fibjs {

Expand Down Expand Up @@ -121,6 +123,24 @@ result_t Message::write(Buffer_base* data, AsyncEvent* ac)
return m_body->write(data, ac);
}

result_t Message::json(v8::Local<v8::Value> data, AsyncEvent* ac)
{
if (ac->isSync()) {
exlib::string str;
result_t hr = json_base::encode(data, str);
if (hr < 0)
return hr;
ac->m_ctx.resize(1);
ac->m_ctx[0] = new Buffer(str);
}

if (m_body == NULL)
m_body = new MemoryStream();

obj_ptr<Buffer_base> buf = Buffer_base::getInstance(ac->m_ctx[0].object());
return m_body->write(buf, ac);
}

result_t Message::get_length(int64_t& retVal)
{
if (m_body == NULL) {
Expand Down
5 changes: 5 additions & 0 deletions fibjs/src/websocket/WebSocketMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ result_t WebSocketMessage::write(Buffer_base* data, AsyncEvent* ac)
return m_message->write(data, ac);
}

result_t WebSocketMessage::json(v8::Local<v8::Value> data, AsyncEvent* ac)
{
return m_message->json(data, ac);
}

result_t WebSocketMessage::get_length(int64_t& retVal)
{
return m_message->get_length(retVal);
Expand Down
5 changes: 5 additions & 0 deletions idl/zh-cn/Message.idl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ interface Message : object
*/
write(Buffer data) async;

/*! @brief 以 JSON 编码写入给定的数据
@param data 给定要写入的数据
*/
json(Value data) async;

/*! @brief 消息数据部分的长度 */
readonly Long length;

Expand Down
16 changes: 16 additions & 0 deletions test/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,22 @@ describe("http", () => {
});
});

it('json', () => {
var req = new http.Request();
req.json({
a: 100
});
assert.equal(req.firstHeader('Content-Type'), "application/json");
assert.equal(req.data.toString(), '{"a":100}');

var rep = new http.Response();
rep.json({
a: 100
});
assert.equal(rep.firstHeader('Content-Type'), "application/json");
assert.equal(rep.data.toString(), '{"a":100}');
});

describe("encode", () => {
it("request", () => {
var rep = new http.Request();
Expand Down

0 comments on commit c4a7069

Please sign in to comment.