2018-12-17 21:34:02 +01:00
|
|
|
"use strict";
|
|
|
|
|
2025-01-02 13:47:44 +01:00
|
|
|
import { isElectron } from "./utils.js";
|
2024-07-18 21:35:17 +03:00
|
|
|
import log from "./log.js";
|
2024-07-18 21:37:45 +03:00
|
|
|
import url from "url";
|
2024-07-18 21:35:17 +03:00
|
|
|
import syncOptions from "./sync_options.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { ExecOpts } from "./request_interface.js";
|
2018-12-17 21:34:02 +01:00
|
|
|
|
|
|
|
// 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
|
2018-12-17 21:34:02 +01:00
|
|
|
|
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";
|
2020-06-15 18:24:43 +02:00
|
|
|
|
2024-02-17 21:58:35 +02:00
|
|
|
interface Request {
|
|
|
|
on(event: RequestEvent, cb: (e: any) => void): void;
|
|
|
|
end(payload?: string): void;
|
|
|
|
}
|
|
|
|
|
2024-12-22 15:42:15 +02:00
|
|
|
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);
|
2024-12-22 15:42:15 +02:00
|
|
|
|
2023-01-15 21:04:17 +01:00
|
|
|
// 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);
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2024-07-18 23:42:54 +03:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2018-12-17 21:34:02 +01:00
|
|
|
try {
|
2024-02-17 21:58:35 +02:00
|
|
|
const headers: Record<string, string | number> = {
|
2018-12-17 22:12:26 +01:00
|
|
|
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
|
|
|
};
|
2018-12-17 22:12:26 +01:00
|
|
|
|
|
|
|
if (opts.auth) {
|
2025-01-09 18:07:02 +02:00
|
|
|
headers["trilium-cred"] = Buffer.from(`dummy:${opts.auth.password}`).toString("base64");
|
2018-12-17 22:12:26 +01:00
|
|
|
}
|
|
|
|
|
2024-07-18 23:42:54 +03:00
|
|
|
const request = (await client).request({
|
2018-12-17 21:34:02 +01:00
|
|
|
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,
|
2020-06-13 10:23:36 +02:00
|
|
|
timeout: opts.timeout, // works only for node.js client
|
2019-07-24 20:47:41 +02:00
|
|
|
headers,
|
|
|
|
agent: proxyAgent
|
2018-12-17 21:34:02 +01:00
|
|
|
});
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
request.on("error", (err) => reject(generateError(opts, err)));
|
2018-12-19 21:29:35 +01:00
|
|
|
|
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"];
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
let responseStr = "";
|
2024-02-17 21:58:35 +02:00
|
|
|
let chunks: Buffer[] = [];
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
response.on("data", (chunk: Buffer) => chunks.push(chunk));
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
response.on("end", () => {
|
2023-12-14 11:10:13 +08:00
|
|
|
// 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-12-14 11:10:13 +08:00
|
|
|
|
2023-10-19 00:13:11 +02:00
|
|
|
if ([200, 201, 204].includes(response.statusCode)) {
|
|
|
|
try {
|
|
|
|
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
|
2018-12-17 21:34:02 +01:00
|
|
|
|
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));
|
|
|
|
}
|
2018-12-17 21:34:02 +01:00
|
|
|
|
2023-10-19 00:13:11 +02:00
|
|
|
reject(generateError(opts, `${response.statusCode} ${response.statusMessage} ${errorMessage}`));
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-13 22:54:00 +01:00
|
|
|
let payload;
|
|
|
|
|
|
|
|
if (opts.body) {
|
2025-01-09 18:07:02 +02:00
|
|
|
payload = typeof opts.body === "object" ? JSON.stringify(opts.body) : opts.body;
|
2021-03-13 22:54:00 +01:00
|
|
|
}
|
|
|
|
|
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));
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
2020-03-25 11:28:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-18 23:40:32 +03:00
|
|
|
async function getImage(imageUrl: string): Promise<Buffer> {
|
2020-08-10 23:45:17 +02:00
|
|
|
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,
|
2020-08-03 23:33:44 +02:00
|
|
|
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);
|
|
|
|
|
2024-07-18 23:06:08 +03:00
|
|
|
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)));
|
2020-06-15 18:24:43 +02:00
|
|
|
|
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)) {
|
2022-12-21 15:19:05 +01:00
|
|
|
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);
|
2024-07-18 23:06:08 +03:00
|
|
|
} catch (e: any) {
|
2020-03-25 11:28:44 +01:00
|
|
|
reject(generateError(opts, e.message));
|
|
|
|
}
|
|
|
|
});
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const HTTP = "http:",
|
|
|
|
HTTPS = "https:";
|
2023-11-20 23:03:19 +01:00
|
|
|
|
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;
|
|
|
|
}
|
2023-11-20 23:03:19 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const AgentClass = HTTP === protocol ? (await import("http-proxy-agent")).HttpProxyAgent : (await import("https-proxy-agent")).HttpsProxyAgent;
|
2023-11-20 23:03:19 +01:00
|
|
|
|
|
|
|
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
|
2025-01-02 13:47:44 +01:00
|
|
|
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);
|
2018-12-17 21:34:02 +01:00
|
|
|
|
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}'`);
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}`);
|
2018-12-17 21:34:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2020-03-25 11:28:44 +01:00
|
|
|
exec,
|
|
|
|
getImage
|
2020-06-13 10:23:36 +02:00
|
|
|
};
|