From e8cbfc127b635f94be8d3e406699c97d8f22cf6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bra=C5=A1na?= Date: Fri, 13 Sep 2019 18:53:26 +0200 Subject: [PATCH 1/2] Custom NoticeProcessor Emit notices in "notice" event instead of writing them to stdout. --- src/connection.cc | 19 +++++++++++++++---- src/connection.h | 4 +++- test/notice.js | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 test/notice.js diff --git a/src/connection.cc b/src/connection.cc index 20fdbaa..41040ae 100644 --- a/src/connection.cc +++ b/src/connection.cc @@ -675,6 +675,8 @@ bool Connection::ConnectDB(const char* paramString) { uv_poll_init_socket(uv_default_loop(), &(this->read_watcher), fd); uv_poll_init_socket(uv_default_loop(), &(this->write_watcher), fd); + PQsetNoticeProcessor(this->pq, NoticeProcessor, (void *) this); + TRACE("Connection::ConnectSync::Success"); return true; } @@ -786,7 +788,11 @@ void Connection::DeleteCStringArray(char** array, int length) { delete [] array; } -void Connection::Emit(const char* message) { +void Connection::Emit(const char* event) { + this->EmitMessage(event, ""); +} + +void Connection::EmitMessage(const char* event, const char* message) { Nan::HandleScope scope; TRACE("ABOUT TO EMIT EVENT"); @@ -796,14 +802,19 @@ void Connection::Emit(const char* message) { assert(emit_v->IsFunction()); v8::Local emit_f = emit_v.As(); - v8::Local eventName = Nan::New(message).ToLocalChecked(); - v8::Local info[1] = { eventName }; + v8::Local eventName = Nan::New(event).ToLocalChecked(); + v8::Local eventMessage = Nan::New(message).ToLocalChecked(); + v8::Local info[2] = { eventName, eventMessage }; TRACE("CALLING EMIT"); Nan::TryCatch tc; Nan::AsyncResource *async_emit_f = new Nan::AsyncResource("libpq:connection:emit"); - async_emit_f->runInAsyncScope(handle(), emit_f, 1, info); + async_emit_f->runInAsyncScope(handle(), emit_f, 2, info); if(tc.HasCaught()) { Nan::FatalException(tc); } } +void Connection::NoticeProcessor(void *arg, const char *message) +{ + ((Connection *) arg)->EmitMessage("notice", message); +} diff --git a/src/connection.h b/src/connection.h index d73b5e7..5776f12 100644 --- a/src/connection.h +++ b/src/connection.h @@ -76,7 +76,9 @@ class Connection : public Nan::ObjectWrap { static char* NewCString(v8::Local val); static char** NewCStringArray(v8::Local jsParams); static void DeleteCStringArray(char** array, int length); - void Emit(const char* message); + void Emit(const char* event); + void EmitMessage(const char* event, const char* message); + static void NoticeProcessor(void *arg, const char *message); }; #endif diff --git a/test/notice.js b/test/notice.js new file mode 100644 index 0000000..ffd419f --- /dev/null +++ b/test/notice.js @@ -0,0 +1,19 @@ +var PQ = require('../') +var assert = require('assert'); + +describe('server notices', function() { + it('works', function(done) { + var pq = new PQ(); + pq.connect(function(err) { + assert.ifError(err); + notices = [] + pq.on('notice', function(msg){notices.push(msg);}); + pq.exec("DO $$BEGIN RAISE NOTICE 'test1'; RAISE WARNING 'test2'; END;$$"); + assert.equal(notices.length, 2); + assert.equal(notices[0], 'NOTICE: test1\n'); + assert.equal(notices[1], 'WARNING: test2\n'); + done(); + }); + }); + +}); From 8585fa6ae8fd0d6ac6b41d3a8a2c29d2b4afbcca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bra=C5=A1na?= Date: Fri, 13 Sep 2019 19:20:09 +0200 Subject: [PATCH 2/2] Set correct client_min_messages for testing --- test/notice.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/notice.js b/test/notice.js index ffd419f..943881a 100644 --- a/test/notice.js +++ b/test/notice.js @@ -8,6 +8,7 @@ describe('server notices', function() { assert.ifError(err); notices = [] pq.on('notice', function(msg){notices.push(msg);}); + pq.exec("SET SESSION client_min_messages=notice"); pq.exec("DO $$BEGIN RAISE NOTICE 'test1'; RAISE WARNING 'test2'; END;$$"); assert.equal(notices.length, 2); assert.equal(notices[0], 'NOTICE: test1\n');