2018-12-17 21:34:02 +01:00
|
|
|
"use strict";
|
|
|
|
|
2023-11-22 19:34:48 +01:00
|
|
|
const utils = require('./utils.js');
|
2024-02-16 21:17:33 +02:00
|
|
|
const log = require('./log');
|
2018-12-17 21:34:02 +01:00
|
|
|
const url = require('url');
|
2023-11-22 19:34:48 +01:00
|
|
|
const syncOptions = require('./sync_options.js');
|
2018-12-17 21:34:02 +01:00
|
|
|
|
|
|
|
// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
|
2023-06-29 22:10:13 +02:00
|
|
|
// this allows supporting system proxy
|
2018-12-17 21:34:02 +01:00
|
|
|
|
|
|
|
function exec(opts) {
|
2020-06-15 18:24:43 +02:00
|
|
|
const client = getClient(opts);
|
|
|
|
|
2023-01-15 21:04:17 +01:00
|
|
|
// hack for cases where electron.net does not work, but we don't want to set proxy
|
2019-07-24 20:47:41 +02:00
|
|
|
if (opts.proxy === 'noproxy') {
|
|
|
|
opts.proxy = null;
|
|
|
|
}
|
|
|
|
|
2021-01-10 21:56:40 +01:00
|
|
|
if (!opts.paging) {
|
|
|
|
opts.paging = {
|
|
|
|
pageCount: 1,
|
2021-01-11 22:48:51 +01:00
|
|
|
pageIndex: 0,
|
|
|
|
requestId: 'n/a'
|
2021-01-10 21:56:40 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:47:41 +02:00
|
|
|
const proxyAgent = getProxyAgent(opts);
|
2018-12-17 22:54:54 +01:00
|
|
|
const parsedTargetUrl = url.parse(opts.url);
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-12-17 21:34:02 +01:00
|
|
|
try {
|
2021-01-11 22:48:51 +01:00
|
|
|
const headers = {
|
2018-12-17 22:12:26 +01:00
|
|
|
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
|
2021-01-10 21:56:40 +01:00
|
|
|
'Content-Type': opts.paging.pageCount === 1 ? 'application/json' : 'text/plain',
|
2021-01-11 22:48:51 +01:00
|
|
|
pageCount: opts.paging.pageCount,
|
|
|
|
pageIndex: opts.paging.pageIndex,
|
|
|
|
requestId: opts.paging.requestId
|
|
|
|
};
|
2018-12-17 22:12:26 +01:00
|
|
|
|
|
|
|
if (opts.auth) {
|
2022-12-21 15:19:05 +01:00
|
|
|
headers['trilium-cred'] = Buffer.from(`dummy:${opts.auth.password}`).toString('base64');
|
2018-12-17 22:12:26 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 21:34:02 +01:00
|
|
|
const request = client.request({
|
|
|
|
method: opts.method,
|
|
|
|
// url is used by electron net module
|
|
|
|
url: opts.url,
|
|
|
|
// 4 fields below are used by http and https node modules
|
2019-07-24 20:47:41 +02:00
|
|
|
protocol: parsedTargetUrl.protocol,
|
|
|
|
host: parsedTargetUrl.hostname,
|
|
|
|
port: parsedTargetUrl.port,
|
|
|
|
path: parsedTargetUrl.path,
|
2020-06-13 10:23:36 +02:00
|
|
|
timeout: opts.timeout, // works only for node.js client
|
2019-07-24 20:47:41 +02:00
|
|
|
headers,
|
|
|
|
agent: proxyAgent
|
2018-12-17 21:34:02 +01:00
|
|
|
});
|
|
|
|
|
2018-12-19 21:29:35 +01:00
|
|
|
request.on('error', err => reject(generateError(opts, err)));
|
|
|
|
|
2018-12-17 21:34:02 +01:00
|
|
|
request.on('response', response => {
|
|
|
|
if (opts.cookieJar && response.headers['set-cookie']) {
|
|
|
|
opts.cookieJar.header = response.headers['set-cookie'];
|
|
|
|
}
|
|
|
|
|
|
|
|
let responseStr = '';
|
2023-12-14 11:10:13 +08:00
|
|
|
let chunks = [];
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2023-12-14 11:10:13 +08:00
|
|
|
response.on('data', chunk => chunks.push(chunk));
|
2018-12-17 21:34:02 +01:00
|
|
|
|
|
|
|
response.on('end', () => {
|
2023-12-14 11:10:13 +08:00
|
|
|
// use Buffer instead of string concatenation to avoid implicit decoding for each chunk
|
|
|
|
// decode the entire data chunks explicitly as utf-8
|
|
|
|
responseStr = Buffer.concat(chunks).toString('utf-8')
|
|
|
|
|
2023-10-19 00:13:11 +02:00
|
|
|
if ([200, 201, 204].includes(response.statusCode)) {
|
|
|
|
try {
|
|
|
|
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2023-10-19 00:13:11 +02:00
|
|
|
resolve(jsonObj);
|
|
|
|
} catch (e) {
|
|
|
|
log.error(`Failed to deserialize sync response: ${responseStr}`);
|
|
|
|
|
|
|
|
reject(generateError(opts, e.message));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let errorMessage;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const jsonObj = JSON.parse(responseStr);
|
|
|
|
|
|
|
|
errorMessage = jsonObj?.message || '';
|
|
|
|
} catch (e) {
|
|
|
|
errorMessage = responseStr.substr(0, Math.min(responseStr.length, 100));
|
|
|
|
}
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2023-10-19 00:13:11 +02:00
|
|
|
reject(generateError(opts, `${response.statusCode} ${response.statusMessage} ${errorMessage}`));
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-13 22:54:00 +01:00
|
|
|
let payload;
|
|
|
|
|
|
|
|
if (opts.body) {
|
|
|
|
payload = typeof opts.body === 'object'
|
|
|
|
? JSON.stringify(opts.body)
|
|
|
|
: opts.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.end(payload);
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
catch (e) {
|
2018-12-18 20:39:56 +01:00
|
|
|
reject(generateError(opts, e.message));
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
2020-03-25 11:28:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getImage(imageUrl) {
|
2020-08-10 23:45:17 +02:00
|
|
|
const proxyConf = syncOptions.getSyncProxy();
|
2020-03-25 11:28:44 +01:00
|
|
|
const opts = {
|
|
|
|
method: 'GET',
|
|
|
|
url: imageUrl,
|
2020-08-03 23:33:44 +02:00
|
|
|
proxy: proxyConf !== "noproxy" ? proxyConf : null
|
2020-03-25 11:28:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const client = getClient(opts);
|
|
|
|
const proxyAgent = getProxyAgent(opts);
|
|
|
|
const parsedTargetUrl = url.parse(opts.url);
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-03-25 11:28:44 +01:00
|
|
|
try {
|
|
|
|
const request = client.request({
|
|
|
|
method: opts.method,
|
|
|
|
// url is used by electron net module
|
|
|
|
url: opts.url,
|
|
|
|
// 4 fields below are used by http and https node modules
|
|
|
|
protocol: parsedTargetUrl.protocol,
|
|
|
|
host: parsedTargetUrl.hostname,
|
|
|
|
port: parsedTargetUrl.port,
|
|
|
|
path: parsedTargetUrl.path,
|
2023-06-30 11:18:34 +02:00
|
|
|
timeout: opts.timeout, // works only for the node client
|
2020-03-25 11:28:44 +01:00
|
|
|
headers: {},
|
|
|
|
agent: proxyAgent
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on('error', err => reject(generateError(opts, err)));
|
|
|
|
|
2020-06-15 18:24:43 +02:00
|
|
|
request.on('abort', err => reject(generateError(opts, err)));
|
|
|
|
|
2020-03-25 11:28:44 +01:00
|
|
|
request.on('response', response => {
|
|
|
|
if (![200, 201, 204].includes(response.statusCode)) {
|
2022-12-21 15:19:05 +01:00
|
|
|
reject(generateError(opts, `${response.statusCode} ${response.statusMessage}`));
|
2020-03-25 11:28:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const chunks = []
|
|
|
|
|
|
|
|
response.on('data', chunk => chunks.push(chunk));
|
|
|
|
response.on('end', () => resolve(Buffer.concat(chunks)));
|
|
|
|
});
|
|
|
|
|
|
|
|
request.end(undefined);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(generateError(opts, e.message));
|
|
|
|
}
|
|
|
|
});
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
|
2023-11-20 23:03:19 +01:00
|
|
|
const HTTP = 'http:', HTTPS = 'https:';
|
|
|
|
|
2019-07-24 20:47:41 +02:00
|
|
|
function getProxyAgent(opts) {
|
|
|
|
if (!opts.proxy) {
|
2020-03-25 11:28:44 +01:00
|
|
|
return null;
|
2019-07-24 20:47:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const {protocol} = url.parse(opts.url);
|
|
|
|
|
2023-11-20 23:03:19 +01:00
|
|
|
if (![HTTP, HTTPS].includes(protocol)) {
|
2019-07-24 20:47:41 +02:00
|
|
|
return null;
|
|
|
|
}
|
2023-11-20 23:03:19 +01:00
|
|
|
|
|
|
|
const AgentClass = HTTP === protocol
|
|
|
|
? require("http-proxy-agent").HttpProxyAgent
|
|
|
|
: require("https-proxy-agent").HttpsProxyAgent;
|
|
|
|
|
|
|
|
return new AgentClass(opts.proxy);
|
2019-07-24 20:47:41 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 21:34:02 +01:00
|
|
|
function getClient(opts) {
|
2023-06-30 11:18:34 +02:00
|
|
|
// it's not clear how to explicitly configure proxy (as opposed to system proxy),
|
2023-06-29 22:10:13 +02:00
|
|
|
// so in that case, we always use node's modules
|
2018-12-17 22:54:54 +01:00
|
|
|
if (utils.isElectron() && !opts.proxy) {
|
2018-12-17 21:34:02 +01:00
|
|
|
return require('electron').net;
|
|
|
|
}
|
|
|
|
else {
|
2019-07-24 20:47:41 +02:00
|
|
|
const {protocol} = url.parse(opts.url);
|
2018-12-17 21:34:02 +01:00
|
|
|
|
|
|
|
if (protocol === 'http:' || protocol === 'https:') {
|
|
|
|
return require(protocol.substr(0, protocol.length - 1));
|
|
|
|
}
|
|
|
|
else {
|
2023-05-04 22:16:18 +02:00
|
|
|
throw new Error(`Unrecognized protocol '${protocol}'`);
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 20:39:56 +01:00
|
|
|
function generateError(opts, message) {
|
|
|
|
return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${message}`);
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-03-25 11:28:44 +01:00
|
|
|
exec,
|
|
|
|
getImage
|
2020-06-13 10:23:36 +02:00
|
|
|
};
|