-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(remote): Fix UTF8 body sending, closes #259
- Loading branch information
Showing
6 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters