Notes/src/services/request.ts

239 lines
7.5 KiB
TypeScript
Raw Normal View History

"use strict";
import utils from "./utils.js";
import log from "./log.js";
import url from "url";
import syncOptions from "./sync_options.js";
2024-02-18 13:10:51 +02:00
import { ExecOpts } from './request_interface';
// 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
2024-02-17 21:58:35 +02:00
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");
2024-02-17 21:58:35 +02:00
interface Request {
on(event: RequestEvent, cb: (e: any) => void): void;
end(payload?: string): void;
}
interface Client {
request(opts: ClientOpts): Request;
}
2024-07-18 23:40:32 +03:00
async function exec<T>(opts: ExecOpts): Promise<T> {
2024-02-17 21:58:35 +02:00
const client = getClient(opts);
// 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;
}
2024-02-17 21:58:35 +02:00
const paging = opts.paging || {
pageCount: 1,
pageIndex: 0,
requestId: 'n/a'
};
2021-01-10 21:56:40 +01:00
2024-07-18 23:40:32 +03:00
const proxyAgent = await getProxyAgent(opts);
2018-12-17 22:54:54 +01:00
const parsedTargetUrl = url.parse(opts.url);
2020-06-20 12:31:38 +02:00
return new Promise((resolve, reject) => {
try {
2024-02-17 21:58:35 +02:00
const headers: Record<string, string | number> = {
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
2024-02-17 21:58:35 +02:00
'Content-Type': paging.pageCount === 1 ? 'application/json' : 'text/plain',
pageCount: paging.pageCount,
pageIndex: paging.pageIndex,
requestId: paging.requestId
2021-01-11 22:48:51 +01:00
};
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
2019-07-24 20:47:41 +02:00
protocol: parsedTargetUrl.protocol,
host: parsedTargetUrl.hostname,
port: parsedTargetUrl.port,
path: parsedTargetUrl.path,
timeout: opts.timeout, // works only for node.js client
2019-07-24 20:47:41 +02:00
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 = '';
2024-02-17 21:58:35 +02:00
let chunks: Buffer[] = [];
2024-02-17 21:58:35 +02:00
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')
2023-10-19 00:13:11 +02:00
if ([200, 201, 204].includes(response.statusCode)) {
try {
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
2023-10-19 00:13:11 +02:00
resolve(jsonObj);
2024-02-17 21:58:35 +02:00
} catch (e: any) {
2023-10-19 00:13:11 +02:00
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 || '';
2024-02-17 21:58:35 +02:00
} catch (e: any) {
2023-10-19 00:13:11 +02:00
errorMessage = responseStr.substr(0, Math.min(responseStr.length, 100));
}
2023-10-19 00:13:11 +02:00
reject(generateError(opts, `${response.statusCode} ${response.statusMessage} ${errorMessage}`));
}
});
});
let payload;
if (opts.body) {
payload = typeof opts.body === 'object'
? JSON.stringify(opts.body)
: opts.body;
}
2024-04-03 23:18:39 +03:00
request.end(payload as string);
}
2024-02-17 21:58:35 +02:00
catch (e: any) {
2018-12-18 20:39:56 +01:00
reject(generateError(opts, e.message));
}
2020-03-25 11:28:44 +01:00
});
}
2024-07-18 23:40:32 +03:00
async function getImage(imageUrl: string): Promise<Buffer> {
const proxyConf = syncOptions.getSyncProxy();
2024-02-17 21:58:35 +02:00
const opts: ClientOpts = {
2020-03-25 11:28:44 +01:00
method: 'GET',
url: imageUrl,
proxy: proxyConf !== "noproxy" ? proxyConf : null
2020-03-25 11:28:44 +01:00
};
const client = getClient(opts);
2024-07-18 23:40:32 +03:00
const proxyAgent = await getProxyAgent(opts);
2020-03-25 11:28:44 +01:00
const parsedTargetUrl = url.parse(opts.url);
return new Promise<Buffer>((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)));
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)) {
reject(generateError(opts, `${response.statusCode} ${response.statusMessage}`));
2020-03-25 11:28:44 +01:00
}
2024-02-17 21:58:35 +02:00
const chunks: Buffer[] = []
2020-03-25 11:28:44 +01:00
2024-02-17 21:58:35 +02:00
response.on('data', (chunk: Buffer) => chunks.push(chunk));
2020-03-25 11:28:44 +01:00
response.on('end', () => resolve(Buffer.concat(chunks)));
});
request.end(undefined);
} catch (e: any) {
2020-03-25 11:28:44 +01:00
reject(generateError(opts, e.message));
}
});
}
const HTTP = 'http:', HTTPS = 'https:';
2024-07-18 23:40:32 +03:00
async function getProxyAgent(opts: ClientOpts) {
2019-07-24 20:47:41 +02:00
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);
2024-02-17 21:58:35 +02:00
if (!protocol || ![HTTP, HTTPS].includes(protocol)) {
2019-07-24 20:47:41 +02:00
return null;
}
const AgentClass = HTTP === protocol
2024-07-18 23:40:32 +03:00
? (await import('http-proxy-agent')).HttpProxyAgent
: (await import('https-proxy-agent')).HttpsProxyAgent;
return new AgentClass(opts.proxy);
2019-07-24 20:47:41 +02:00
}
2024-02-17 21:58:35 +02:00
function getClient(opts: ClientOpts): Client {
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) {
2024-02-18 12:19:09 +02:00
return require('electron').net as Client;
}
else {
2019-07-24 20:47:41 +02:00
const {protocol} = url.parse(opts.url);
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}'`);
}
}
}
2024-02-17 21:58:35 +02:00
function generateError(opts: {
method: string;
url: string;
}, message: string) {
2018-12-18 20:39:56 +01:00
return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${message}`);
}
export default {
2020-03-25 11:28:44 +01:00
exec,
getImage
};