Skip to content

Commit

Permalink
fix(remote): Fix UTF8 body sending, closes #259
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Apr 30, 2021
1 parent 590fba2 commit 07fa67a
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
1 change: 1 addition & 0 deletions e2e/remote/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
41 changes: 41 additions & 0 deletions e2e/remote/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var log = require('../..');
var startServer = require('./server');

var IS_TEST = process.argv.indexOf('--test') !== -1;

log.transports.remote.url = 'http://localhost:7777/remote-log';
log.transports.remote.level = 'verbose';
log.transports.file.level = false;
// log.transports.console.level = false;

function main() {
return startServer(7777)
.then(function (server) {
log.info('Remote logging');
return server.waitForRequest();
})
.then(function (server) {
log.info('🐛🐛 UTF8 🐛🐛');
return server.waitForRequest();
})
.then(function (server) {
if (IS_TEST) {
return server.close();
}

return server;
})
.then(function () {
if (IS_TEST) {
process.exit(0);
}
});
}

main().catch(function (e) {
// eslint-disable-next-line no-console
console.error(e);
process.exit(1);
});
15 changes: 15 additions & 0 deletions e2e/remote/main.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var helper = require('../spec-helper');
var TIMEOUT = 6000;

describe('e2e', function () {
it('remote: check log files', function () {
return helper.run('remote', TIMEOUT).then(function (logReader) {
expect(logReader.format()).toEqual([
'server.log: Request: Remote logging',
'server.log: Request: 🐛🐛 UTF8 🐛🐛',
]);
});
}, TIMEOUT);
});
8 changes: 8 additions & 0 deletions e2e/remote/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "electron-log-test-remote",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node main.js"
}
}
85 changes: 85 additions & 0 deletions e2e/remote/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

var http = require('http');
var log = require('../..');

module.exports = startServer;

var logger = log.create('server');
logger.transports.file.fileName = 'server.log';

function startServer(port) {
var onSuccessRequest;
var onFailureRequest;

return new Promise(function (resolve, reject) {
var server = http.createServer(function (req, res) {
readPostData(req)
.then(function (data) {
logger.info('Request:', data.data[0]);

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true }));

if (typeof onSuccessRequest === 'function') {
onSuccessRequest(makeServerInterface());
}
})
.catch(function (e) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: false, error: e }));

if (typeof onFailureRequest === 'function') {
onFailureRequest(e);
}
});
});

server.listen(port, function () {
resolve(makeServerInterface());
});

server.on('error', reject);

function makeServerInterface() {
return {
instance: server,
close: function () {
return new Promise(function (resolveClose) {
server.close(resolveClose);
});
},
waitForRequest: function () {
return new Promise(function (requestResolve, requestReject) {
onSuccessRequest = requestResolve;
onFailureRequest = requestReject;
});
},
};
}
});
}

function readPostData(req) {
return new Promise(function (resolve, reject) {
if (req.method !== 'POST') {
reject(new Error('POST method required'));
}

var chunks = [];

req
.on('data', function (data) { chunks.push(data) })
.on('end', function () {
var body = Buffer.concat(chunks).toString('utf8');
try {
resolve(JSON.parse(body));
} catch (e) {
reject(
new Error('Cannot read body: ' + e.message + '\nReceived: ' + body)
);
}
})
.on('error', reject);
});
}
6 changes: 5 additions & 1 deletion src/transports/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ function remoteTransportFactory(electronLog) {
variables: message.variables,
});

var request = post(transport.url, transport.requestOptions, body);
var request = post(
transport.url,
transport.requestOptions,
Buffer.from(body, 'utf8')
);

request.on('error', transport.onError || onError);

Expand Down

0 comments on commit 07fa67a

Please sign in to comment.