remote.js
2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
var http = require('http');
var https = require('https');
var url = require('url');
var transform = require('../transform');
module.exports = remoteTransportFactory;
function remoteTransportFactory(electronLog) {
transport.client = { name: 'electron-application' };
transport.depth = 6;
transport.level = false;
transport.requestOptions = {};
transport.url = null;
transport.onError = null;
transport.transformBody = function (body) { return JSON.stringify(body) };
return transport;
function transport(message) {
if (!transport.url) return;
var body = transport.transformBody({
client: transport.client,
data: transform.transform(message, [
transform.removeStyles,
transform.toJSON,
transform.maxDepthFactory(transport.depth + 1),
]),
date: message.date.getTime(),
level: message.level,
variables: message.variables,
});
var request = post(
transport.url,
transport.requestOptions,
Buffer.from(body, 'utf8')
);
request.on('error', transport.onError || onError);
function onError(error) {
electronLog.logMessageWithTransports(
{
data: [
'electron-log.transports.remote:'
+ ' cannot send HTTP request to ' + transport.url,
error,
],
level: 'warn',
},
[
electronLog.transports.console,
electronLog.transports.ipc,
electronLog.transports.file,
]
);
}
}
}
function post(serverUrl, requestOptions, body) {
var urlObject = url.parse(serverUrl);
var httpTransport = urlObject.protocol === 'https:' ? https : http;
var options = {
hostname: urlObject.hostname,
port: urlObject.port,
path: urlObject.path,
method: 'POST',
headers: {},
};
Object.assign(options, requestOptions);
options.headers['Content-Length'] = body.length;
if (!options.headers['Content-Type']) {
options.headers['Content-Type'] = 'application/json';
}
var request = httpTransport.request(options);
request.write(body);
request.end();
return request;
}