mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
server-ts: Port services/request
This commit is contained in:
parent
dc22d05657
commit
669988953d
@ -8,7 +8,7 @@ const protectedSessionService = require('../services/protected_session');
|
|||||||
const log = require('../services/log');
|
const log = require('../services/log');
|
||||||
const utils = require('../services/utils');
|
const utils = require('../services/utils');
|
||||||
const revisionService = require('./revisions');
|
const revisionService = require('./revisions');
|
||||||
const request = require('./request.js');
|
const request = require('./request');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const url = require('url');
|
const url = require('url');
|
||||||
const becca = require('../becca/becca');
|
const becca = require('../becca/becca');
|
||||||
|
@ -1,14 +1,57 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const utils = require('./utils');
|
import utils = require('./utils');
|
||||||
const log = require('./log');
|
import log = require('./log');
|
||||||
const url = require('url');
|
import url = require('url');
|
||||||
const syncOptions = require('./sync_options');
|
import syncOptions = require('./sync_options');
|
||||||
|
|
||||||
// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
|
// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
|
||||||
// this allows supporting system proxy
|
// this allows supporting system proxy
|
||||||
|
|
||||||
function exec(opts) {
|
interface ExecOpts {
|
||||||
|
proxy: "noproxy" | null;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
paging?: {
|
||||||
|
pageCount: number;
|
||||||
|
pageIndex: number;
|
||||||
|
requestId: string;
|
||||||
|
};
|
||||||
|
cookieJar?: {
|
||||||
|
header?: string;
|
||||||
|
};
|
||||||
|
auth?: {
|
||||||
|
password?: string;
|
||||||
|
},
|
||||||
|
timeout: number;
|
||||||
|
body: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ClientOpts {
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
protocol?: string | null;
|
||||||
|
host?: string | null;
|
||||||
|
port?: string | null;
|
||||||
|
path?: string | null;
|
||||||
|
timeout?: number;
|
||||||
|
headers?: Record<string, string | number>;
|
||||||
|
agent?: any;
|
||||||
|
proxy?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestEvent = ("error" | "response" | "abort");
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
on(event: RequestEvent, cb: (e: any) => void): void;
|
||||||
|
end(payload?: string): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Client {
|
||||||
|
request(opts: ClientOpts): Request;
|
||||||
|
}
|
||||||
|
|
||||||
|
function exec(opts: ExecOpts) {
|
||||||
const client = getClient(opts);
|
const client = getClient(opts);
|
||||||
|
|
||||||
// hack for cases where electron.net does not work, but we don't want to set proxy
|
// hack for cases where electron.net does not work, but we don't want to set proxy
|
||||||
@ -16,25 +59,23 @@ function exec(opts) {
|
|||||||
opts.proxy = null;
|
opts.proxy = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!opts.paging) {
|
const paging = opts.paging || {
|
||||||
opts.paging = {
|
|
||||||
pageCount: 1,
|
pageCount: 1,
|
||||||
pageIndex: 0,
|
pageIndex: 0,
|
||||||
requestId: 'n/a'
|
requestId: 'n/a'
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
const proxyAgent = getProxyAgent(opts);
|
const proxyAgent = getProxyAgent(opts);
|
||||||
const parsedTargetUrl = url.parse(opts.url);
|
const parsedTargetUrl = url.parse(opts.url);
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const headers = {
|
const headers: Record<string, string | number> = {
|
||||||
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
|
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
|
||||||
'Content-Type': opts.paging.pageCount === 1 ? 'application/json' : 'text/plain',
|
'Content-Type': paging.pageCount === 1 ? 'application/json' : 'text/plain',
|
||||||
pageCount: opts.paging.pageCount,
|
pageCount: paging.pageCount,
|
||||||
pageIndex: opts.paging.pageIndex,
|
pageIndex: paging.pageIndex,
|
||||||
requestId: opts.paging.requestId
|
requestId: paging.requestId
|
||||||
};
|
};
|
||||||
|
|
||||||
if (opts.auth) {
|
if (opts.auth) {
|
||||||
@ -63,9 +104,9 @@ function exec(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let responseStr = '';
|
let responseStr = '';
|
||||||
let chunks = [];
|
let chunks: Buffer[] = [];
|
||||||
|
|
||||||
response.on('data', chunk => chunks.push(chunk));
|
response.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||||
|
|
||||||
response.on('end', () => {
|
response.on('end', () => {
|
||||||
// use Buffer instead of string concatenation to avoid implicit decoding for each chunk
|
// use Buffer instead of string concatenation to avoid implicit decoding for each chunk
|
||||||
@ -77,7 +118,7 @@ function exec(opts) {
|
|||||||
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
|
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
|
||||||
|
|
||||||
resolve(jsonObj);
|
resolve(jsonObj);
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
log.error(`Failed to deserialize sync response: ${responseStr}`);
|
log.error(`Failed to deserialize sync response: ${responseStr}`);
|
||||||
|
|
||||||
reject(generateError(opts, e.message));
|
reject(generateError(opts, e.message));
|
||||||
@ -89,7 +130,7 @@ function exec(opts) {
|
|||||||
const jsonObj = JSON.parse(responseStr);
|
const jsonObj = JSON.parse(responseStr);
|
||||||
|
|
||||||
errorMessage = jsonObj?.message || '';
|
errorMessage = jsonObj?.message || '';
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
errorMessage = responseStr.substr(0, Math.min(responseStr.length, 100));
|
errorMessage = responseStr.substr(0, Math.min(responseStr.length, 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,15 +149,15 @@ function exec(opts) {
|
|||||||
|
|
||||||
request.end(payload);
|
request.end(payload);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e: any) {
|
||||||
reject(generateError(opts, e.message));
|
reject(generateError(opts, e.message));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getImage(imageUrl) {
|
function getImage(imageUrl: string) {
|
||||||
const proxyConf = syncOptions.getSyncProxy();
|
const proxyConf = syncOptions.getSyncProxy();
|
||||||
const opts = {
|
const opts: ClientOpts = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: imageUrl,
|
url: imageUrl,
|
||||||
proxy: proxyConf !== "noproxy" ? proxyConf : null
|
proxy: proxyConf !== "noproxy" ? proxyConf : null
|
||||||
@ -151,15 +192,15 @@ function getImage(imageUrl) {
|
|||||||
reject(generateError(opts, `${response.statusCode} ${response.statusMessage}`));
|
reject(generateError(opts, `${response.statusCode} ${response.statusMessage}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunks = []
|
const chunks: Buffer[] = []
|
||||||
|
|
||||||
response.on('data', chunk => chunks.push(chunk));
|
response.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||||
response.on('end', () => resolve(Buffer.concat(chunks)));
|
response.on('end', () => resolve(Buffer.concat(chunks)));
|
||||||
});
|
});
|
||||||
|
|
||||||
request.end(undefined);
|
request.end(undefined);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e: any) {
|
||||||
reject(generateError(opts, e.message));
|
reject(generateError(opts, e.message));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -167,14 +208,14 @@ function getImage(imageUrl) {
|
|||||||
|
|
||||||
const HTTP = 'http:', HTTPS = 'https:';
|
const HTTP = 'http:', HTTPS = 'https:';
|
||||||
|
|
||||||
function getProxyAgent(opts) {
|
function getProxyAgent(opts: ClientOpts) {
|
||||||
if (!opts.proxy) {
|
if (!opts.proxy) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {protocol} = url.parse(opts.url);
|
const {protocol} = url.parse(opts.url);
|
||||||
|
|
||||||
if (![HTTP, HTTPS].includes(protocol)) {
|
if (!protocol || ![HTTP, HTTPS].includes(protocol)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +226,7 @@ function getProxyAgent(opts) {
|
|||||||
return new AgentClass(opts.proxy);
|
return new AgentClass(opts.proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getClient(opts) {
|
function getClient(opts: ClientOpts): Client {
|
||||||
// it's not clear how to explicitly configure proxy (as opposed to system proxy),
|
// it's not clear how to explicitly configure proxy (as opposed to system proxy),
|
||||||
// so in that case, we always use node's modules
|
// so in that case, we always use node's modules
|
||||||
if (utils.isElectron() && !opts.proxy) {
|
if (utils.isElectron() && !opts.proxy) {
|
||||||
@ -203,11 +244,14 @@ function getClient(opts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateError(opts, message) {
|
function generateError(opts: {
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
}, message: string) {
|
||||||
return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${message}`);
|
return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
exec,
|
exec,
|
||||||
getImage
|
getImage
|
||||||
};
|
};
|
@ -3,7 +3,7 @@ const log = require('./log');
|
|||||||
const sqlInit = require('./sql_init');
|
const sqlInit = require('./sql_init');
|
||||||
const optionService = require('./options');
|
const optionService = require('./options');
|
||||||
const syncOptions = require('./sync_options');
|
const syncOptions = require('./sync_options');
|
||||||
const request = require('./request.js');
|
const request = require('./request');
|
||||||
const appInfo = require('./app_info');
|
const appInfo = require('./app_info');
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const becca = require('../becca/becca');
|
const becca = require('../becca/becca');
|
||||||
|
@ -12,7 +12,7 @@ const appInfo = require('./app_info');
|
|||||||
const syncOptions = require('./sync_options');
|
const syncOptions = require('./sync_options');
|
||||||
const syncMutexService = require('./sync_mutex');
|
const syncMutexService = require('./sync_mutex');
|
||||||
const cls = require('./cls');
|
const cls = require('./cls');
|
||||||
const request = require('./request.js');
|
const request = require('./request');
|
||||||
const ws = require('./ws');
|
const ws = require('./ws');
|
||||||
const entityChangesService = require('./entity_changes');
|
const entityChangesService = require('./entity_changes');
|
||||||
const entityConstructor = require('../becca/entity_constructor');
|
const entityConstructor = require('../becca/entity_constructor');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user