mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 11:26:15 +01:00
server-ts: Port services/request
This commit is contained in:
257
src/services/request.ts
Normal file
257
src/services/request.ts
Normal file
@@ -0,0 +1,257 @@
|
||||
"use strict";
|
||||
|
||||
import utils = require('./utils');
|
||||
import log = require('./log');
|
||||
import url = require('url');
|
||||
import syncOptions = require('./sync_options');
|
||||
|
||||
// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
|
||||
// this allows supporting system proxy
|
||||
|
||||
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);
|
||||
|
||||
// hack for cases where electron.net does not work, but we don't want to set proxy
|
||||
if (opts.proxy === 'noproxy') {
|
||||
opts.proxy = null;
|
||||
}
|
||||
|
||||
const paging = opts.paging || {
|
||||
pageCount: 1,
|
||||
pageIndex: 0,
|
||||
requestId: 'n/a'
|
||||
};
|
||||
|
||||
const proxyAgent = getProxyAgent(opts);
|
||||
const parsedTargetUrl = url.parse(opts.url);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const headers: Record<string, string | number> = {
|
||||
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
|
||||
'Content-Type': paging.pageCount === 1 ? 'application/json' : 'text/plain',
|
||||
pageCount: paging.pageCount,
|
||||
pageIndex: paging.pageIndex,
|
||||
requestId: paging.requestId
|
||||
};
|
||||
|
||||
if (opts.auth) {
|
||||
headers['trilium-cred'] = Buffer.from(`dummy:${opts.auth.password}`).toString('base64');
|
||||
}
|
||||
|
||||
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,
|
||||
timeout: opts.timeout, // works only for node.js client
|
||||
headers,
|
||||
agent: proxyAgent
|
||||
});
|
||||
|
||||
request.on('error', err => reject(generateError(opts, err)));
|
||||
|
||||
request.on('response', response => {
|
||||
if (opts.cookieJar && response.headers['set-cookie']) {
|
||||
opts.cookieJar.header = response.headers['set-cookie'];
|
||||
}
|
||||
|
||||
let responseStr = '';
|
||||
let chunks: Buffer[] = [];
|
||||
|
||||
response.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
|
||||
response.on('end', () => {
|
||||
// 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')
|
||||
|
||||
if ([200, 201, 204].includes(response.statusCode)) {
|
||||
try {
|
||||
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
|
||||
|
||||
resolve(jsonObj);
|
||||
} catch (e: any) {
|
||||
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: any) {
|
||||
errorMessage = responseStr.substr(0, Math.min(responseStr.length, 100));
|
||||
}
|
||||
|
||||
reject(generateError(opts, `${response.statusCode} ${response.statusMessage} ${errorMessage}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let payload;
|
||||
|
||||
if (opts.body) {
|
||||
payload = typeof opts.body === 'object'
|
||||
? JSON.stringify(opts.body)
|
||||
: opts.body;
|
||||
}
|
||||
|
||||
request.end(payload);
|
||||
}
|
||||
catch (e: any) {
|
||||
reject(generateError(opts, e.message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getImage(imageUrl: string) {
|
||||
const proxyConf = syncOptions.getSyncProxy();
|
||||
const opts: ClientOpts = {
|
||||
method: 'GET',
|
||||
url: imageUrl,
|
||||
proxy: proxyConf !== "noproxy" ? proxyConf : null
|
||||
};
|
||||
|
||||
const client = getClient(opts);
|
||||
const proxyAgent = getProxyAgent(opts);
|
||||
const parsedTargetUrl = url.parse(opts.url);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
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,
|
||||
timeout: opts.timeout, // works only for the node client
|
||||
headers: {},
|
||||
agent: proxyAgent
|
||||
});
|
||||
|
||||
request.on('error', err => reject(generateError(opts, err)));
|
||||
|
||||
request.on('abort', err => reject(generateError(opts, err)));
|
||||
|
||||
request.on('response', response => {
|
||||
if (![200, 201, 204].includes(response.statusCode)) {
|
||||
reject(generateError(opts, `${response.statusCode} ${response.statusMessage}`));
|
||||
}
|
||||
|
||||
const chunks: Buffer[] = []
|
||||
|
||||
response.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
response.on('end', () => resolve(Buffer.concat(chunks)));
|
||||
});
|
||||
|
||||
request.end(undefined);
|
||||
}
|
||||
catch (e: any) {
|
||||
reject(generateError(opts, e.message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const HTTP = 'http:', HTTPS = 'https:';
|
||||
|
||||
function getProxyAgent(opts: ClientOpts) {
|
||||
if (!opts.proxy) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const {protocol} = url.parse(opts.url);
|
||||
|
||||
if (!protocol || ![HTTP, HTTPS].includes(protocol)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const AgentClass = HTTP === protocol
|
||||
? require("http-proxy-agent").HttpProxyAgent
|
||||
: require("https-proxy-agent").HttpsProxyAgent;
|
||||
|
||||
return new AgentClass(opts.proxy);
|
||||
}
|
||||
|
||||
function getClient(opts: ClientOpts): Client {
|
||||
// it's not clear how to explicitly configure proxy (as opposed to system proxy),
|
||||
// so in that case, we always use node's modules
|
||||
if (utils.isElectron() && !opts.proxy) {
|
||||
return require('electron').net;
|
||||
}
|
||||
else {
|
||||
const {protocol} = url.parse(opts.url);
|
||||
|
||||
if (protocol === 'http:' || protocol === 'https:') {
|
||||
return require(protocol.substr(0, protocol.length - 1));
|
||||
}
|
||||
else {
|
||||
throw new Error(`Unrecognized protocol '${protocol}'`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function generateError(opts: {
|
||||
method: string;
|
||||
url: string;
|
||||
}, message: string) {
|
||||
return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${message}`);
|
||||
}
|
||||
|
||||
export = {
|
||||
exec,
|
||||
getImage
|
||||
};
|
||||
Reference in New Issue
Block a user