Notes/src/services/request.ts

236 lines
7.5 KiB
TypeScript
Raw Normal View History

"use strict";
import { isElectron } from "./utils.js";
import log from "./log.js";
import url from "url";
import syncOptions from "./sync_options.js";
import type { ExecOpts } from "./request_interface.js";
// 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;
}
2025-01-09 18:07:02 +02:00
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 {
2024-02-17 21:58:35 +02:00
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
2025-01-09 18:07:02 +02:00
if (opts.proxy === "noproxy") {
2019-07-24 20:47:41 +02:00
opts.proxy = null;
}
2024-02-17 21:58:35 +02:00
const paging = opts.paging || {
pageCount: 1,
pageIndex: 0,
2025-01-09 18:07:02 +02:00
requestId: "n/a"
2024-02-17 21:58:35 +02:00
};
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);
2024-07-18 23:42:54 +03:00
return new Promise(async (resolve, reject) => {
try {
2024-02-17 21:58:35 +02:00
const headers: Record<string, string | number> = {
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
2025-01-09 18:07:02 +02:00
"Content-Type": paging.pageCount === 1 ? "application/json" : "text/plain",
2024-02-17 21:58:35 +02:00
pageCount: paging.pageCount,
pageIndex: paging.pageIndex,
requestId: paging.requestId
2021-01-11 22:48:51 +01:00
};
if (opts.auth) {
2025-01-09 18:07:02 +02:00
headers["trilium-cred"] = Buffer.from(`dummy:${opts.auth.password}`).toString("base64");
}
2024-07-18 23:42:54 +03:00
const request = (await 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
});
2025-01-09 18:07:02 +02:00
request.on("error", (err) => reject(generateError(opts, err)));
2025-01-09 18:07:02 +02:00
request.on("response", (response) => {
if (opts.cookieJar && response.headers["set-cookie"]) {
opts.cookieJar.header = response.headers["set-cookie"];
}
2025-01-09 18:07:02 +02:00
let responseStr = "";
2024-02-17 21:58:35 +02:00
let chunks: Buffer[] = [];
2025-01-09 18:07:02 +02:00
response.on("data", (chunk: Buffer) => chunks.push(chunk));
2025-01-09 18:07:02 +02:00
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
2025-01-09 18:07:02 +02:00
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);
2025-01-09 18:07:02 +02:00
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) {
2025-01-09 18:07:02 +02:00
payload = typeof opts.body === "object" ? JSON.stringify(opts.body) : opts.body;
}
2024-04-03 23:18:39 +03:00
request.end(payload as string);
2025-01-09 18:07:02 +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 = {
2025-01-09 18:07:02 +02:00
method: "GET",
2020-03-25 11:28:44 +01:00
url: imageUrl,
proxy: proxyConf !== "noproxy" ? proxyConf : null
2020-03-25 11:28:44 +01:00
};
2024-07-18 23:42:54 +03:00
const client = await 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
});
2025-01-09 18:07:02 +02:00
request.on("error", (err) => reject(generateError(opts, err)));
2020-03-25 11:28:44 +01:00
2025-01-09 18:07:02 +02:00
request.on("abort", (err) => reject(generateError(opts, err)));
2025-01-09 18:07:02 +02:00
request.on("response", (response) => {
2020-03-25 11:28:44 +01:00
if (![200, 201, 204].includes(response.statusCode)) {
reject(generateError(opts, `${response.statusCode} ${response.statusMessage}`));
2020-03-25 11:28:44 +01:00
}
2025-01-09 18:07:02 +02:00
const chunks: Buffer[] = [];
2020-03-25 11:28:44 +01:00
2025-01-09 18:07:02 +02:00
response.on("data", (chunk: Buffer) => chunks.push(chunk));
response.on("end", () => resolve(Buffer.concat(chunks)));
2020-03-25 11:28:44 +01:00
});
request.end(undefined);
} catch (e: any) {
2020-03-25 11:28:44 +01:00
reject(generateError(opts, e.message));
}
});
}
2025-01-09 18:07:02 +02:00
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
}
2025-01-09 18:07:02 +02:00
const { protocol } = url.parse(opts.url);
2019-07-24 20:47:41 +02:00
2024-02-17 21:58:35 +02:00
if (!protocol || ![HTTP, HTTPS].includes(protocol)) {
2019-07-24 20:47:41 +02:00
return null;
}
2025-01-09 18:07:02 +02:00
const AgentClass = HTTP === protocol ? (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-07-18 23:42:54 +03:00
async function getClient(opts: ClientOpts): Promise<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
if (isElectron() && !opts.proxy) {
2025-01-09 18:07:02 +02:00
return (await import("electron")).net as Client;
2024-07-18 23:42:54 +03:00
} else {
2025-01-09 18:07:02 +02:00
const { protocol } = url.parse(opts.url);
2025-01-09 18:07:02 +02:00
if (protocol === "http:" || protocol === "https:") {
2024-07-18 23:42:54 +03:00
return await import(protocol.substr(0, protocol.length - 1));
} else {
2023-05-04 22:16:18 +02:00
throw new Error(`Unrecognized protocol '${protocol}'`);
}
}
}
2025-01-09 18:07:02 +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
};