2018-03-25 23:09:17 +08:00
|
|
|
import utils from './utils.js';
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2019-05-05 16:59:34 +08:00
|
|
|
const REQUEST_LOGGING_ENABLED = false;
|
|
|
|
|
2019-10-19 21:12:25 +08:00
|
|
|
function getHeaders(headers) {
|
2018-03-25 23:09:17 +08:00
|
|
|
// headers need to be lowercase because node.js automatically converts them to lower case
|
|
|
|
// so hypothetical protectedSessionId becomes protectedsessionid on the backend
|
2018-11-30 22:49:35 +08:00
|
|
|
// also avoiding using underscores instead of dashes since nginx filters them out by default
|
2019-10-19 21:12:25 +08:00
|
|
|
const allHeaders = {
|
2020-01-29 04:54:28 +08:00
|
|
|
'trilium-source-id': glob.sourceId,
|
2020-04-26 20:26:57 +08:00
|
|
|
'trilium-local-now-datetime': utils.localNowDateTime(),
|
2020-01-29 04:54:28 +08:00
|
|
|
'x-csrf-token': glob.csrfToken
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|
2019-04-02 03:18:11 +08:00
|
|
|
|
2020-01-29 04:54:28 +08:00
|
|
|
for (const headerName in headers) {
|
|
|
|
if (headers[headerName]) {
|
|
|
|
allHeaders[headerName] = headers[headerName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 03:18:11 +08:00
|
|
|
if (utils.isElectron()) {
|
|
|
|
// passing it explicitely here because of the electron HTTP bypass
|
2019-10-19 21:12:25 +08:00
|
|
|
allHeaders.cookie = document.cookie;
|
2019-04-02 03:18:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-19 21:12:25 +08:00
|
|
|
return allHeaders;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2020-01-29 04:54:28 +08:00
|
|
|
async function get(url, sourceId) {
|
|
|
|
return await call('GET', url, null, {'trilium-source-id': sourceId});
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2020-01-29 04:54:28 +08:00
|
|
|
async function post(url, data, sourceId) {
|
|
|
|
return await call('POST', url, data, {'trilium-source-id': sourceId});
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2020-01-29 04:54:28 +08:00
|
|
|
async function put(url, data, sourceId) {
|
|
|
|
return await call('PUT', url, data, {'trilium-source-id': sourceId});
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2020-01-29 04:54:28 +08:00
|
|
|
async function remove(url, sourceId) {
|
|
|
|
return await call('DELETE', url, null, {'trilium-source-id': sourceId});
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-30 12:30:35 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
let i = 1;
|
|
|
|
const reqResolves = {};
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2019-12-10 06:07:45 +08:00
|
|
|
let maxKnownSyncId = 0;
|
|
|
|
|
2019-10-19 21:12:25 +08:00
|
|
|
async function call(method, url, data, headers = {}) {
|
2019-12-10 06:07:45 +08:00
|
|
|
let resp;
|
|
|
|
|
2020-02-04 04:16:33 +08:00
|
|
|
const start = Date.now();
|
|
|
|
|
2018-03-25 11:37:55 +08:00
|
|
|
if (utils.isElectron()) {
|
2020-04-12 20:22:51 +08:00
|
|
|
const ipc = utils.dynamicRequire('electron').ipcRenderer;
|
2018-03-25 23:09:17 +08:00
|
|
|
const requestId = i++;
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2019-12-10 06:07:45 +08:00
|
|
|
resp = await new Promise((resolve, reject) => {
|
2018-03-25 23:09:17 +08:00
|
|
|
reqResolves[requestId] = resolve;
|
2017-11-30 12:30:35 +08:00
|
|
|
|
2019-05-05 16:59:34 +08:00
|
|
|
if (REQUEST_LOGGING_ENABLED) {
|
|
|
|
console.log(utils.now(), "Request #" + requestId + " to " + method + " " + url);
|
|
|
|
}
|
2017-11-30 12:30:35 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
ipc.send('server-request', {
|
|
|
|
requestId: requestId,
|
2019-10-19 21:12:25 +08:00
|
|
|
headers: getHeaders(headers),
|
2018-03-25 23:09:17 +08:00
|
|
|
method: method,
|
|
|
|
url: "/" + baseApiUrl + url,
|
|
|
|
data: data
|
|
|
|
});
|
2017-11-29 11:01:02 +08:00
|
|
|
});
|
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
else {
|
2019-12-10 06:07:45 +08:00
|
|
|
resp = await ajax(url, method, data, headers);
|
|
|
|
}
|
|
|
|
|
2020-02-04 04:16:33 +08:00
|
|
|
const end = Date.now();
|
|
|
|
|
2020-02-08 17:40:58 +08:00
|
|
|
if (glob.PROFILING_LOG) {
|
|
|
|
console.log(`${method} ${url} took ${end - start}ms`);
|
|
|
|
}
|
2020-02-04 04:16:33 +08:00
|
|
|
|
2019-12-10 06:07:45 +08:00
|
|
|
const maxSyncIdStr = resp.headers['trilium-max-sync-id'];
|
|
|
|
|
|
|
|
if (maxSyncIdStr && maxSyncIdStr.trim()) {
|
|
|
|
maxKnownSyncId = Math.max(maxKnownSyncId, parseInt(maxSyncIdStr));
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
2019-12-10 06:07:45 +08:00
|
|
|
|
|
|
|
return resp.body;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2019-12-10 06:07:45 +08:00
|
|
|
function ajax(url, method, data, headers) {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const options = {
|
|
|
|
url: baseApiUrl + url,
|
|
|
|
type: method,
|
|
|
|
headers: getHeaders(headers),
|
|
|
|
timeout: 60000,
|
|
|
|
success: (body, textStatus, jqXhr) => {
|
|
|
|
const respHeaders = {};
|
|
|
|
|
|
|
|
jqXhr.getAllResponseHeaders().trim().split(/[\r\n]+/).forEach(line => {
|
|
|
|
const parts = line.split(': ');
|
|
|
|
const header = parts.shift();
|
|
|
|
respHeaders[header] = parts.join(': ');
|
|
|
|
});
|
|
|
|
|
|
|
|
res({
|
|
|
|
body,
|
|
|
|
headers: respHeaders
|
|
|
|
});
|
|
|
|
},
|
2020-02-25 18:02:59 +08:00
|
|
|
error: async (jqXhr, textStatus, error) => {
|
2019-12-10 06:07:45 +08:00
|
|
|
const message = "Error when calling " + method + " " + url + ": " + textStatus + " - " + error;
|
2020-02-25 18:02:59 +08:00
|
|
|
const toastService = (await import("./toast.js")).default;
|
2019-12-10 06:07:45 +08:00
|
|
|
toastService.showError(message);
|
|
|
|
toastService.throwError(message);
|
|
|
|
|
|
|
|
rej(error);
|
|
|
|
}
|
|
|
|
};
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-12-10 06:07:45 +08:00
|
|
|
if (data) {
|
|
|
|
try {
|
|
|
|
options.data = JSON.stringify(data);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("Can't stringify data: ", data, " because of error: ", e)
|
|
|
|
}
|
|
|
|
options.contentType = "application/json";
|
2018-03-28 09:46:38 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-12-10 06:07:45 +08:00
|
|
|
$.ajax(options);
|
2018-03-25 23:09:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-06 11:17:19 +08:00
|
|
|
if (utils.isElectron()) {
|
2020-04-12 20:22:51 +08:00
|
|
|
const ipc = utils.dynamicRequire('electron').ipcRenderer;
|
2018-03-26 01:13:26 +08:00
|
|
|
|
2018-04-06 11:17:19 +08:00
|
|
|
ipc.on('server-response', (event, arg) => {
|
2019-05-05 16:59:34 +08:00
|
|
|
if (REQUEST_LOGGING_ENABLED) {
|
|
|
|
console.log(utils.now(), "Response #" + arg.requestId + ": " + arg.statusCode);
|
|
|
|
}
|
2018-03-26 01:13:26 +08:00
|
|
|
|
2019-12-10 06:07:45 +08:00
|
|
|
reqResolves[arg.requestId]({
|
|
|
|
body: arg.body,
|
|
|
|
headers: arg.headers
|
|
|
|
});
|
2018-03-26 01:13:26 +08:00
|
|
|
|
2018-04-06 11:17:19 +08:00
|
|
|
delete reqResolves[arg.requestId];
|
|
|
|
});
|
|
|
|
}
|
2018-03-26 01:13:26 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
get,
|
|
|
|
post,
|
|
|
|
put,
|
|
|
|
remove,
|
|
|
|
ajax,
|
|
|
|
// don't remove, used from CKEditor image upload!
|
2019-12-10 06:07:45 +08:00
|
|
|
getHeaders,
|
|
|
|
getMaxKnownSyncId: () => maxKnownSyncId
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|