mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	refactoring of network code into separate service
This commit is contained in:
		
							
								
								
									
										86
									
								
								src/services/request.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								src/services/request.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,86 @@
 | 
				
			|||||||
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const utils = require('./utils');
 | 
				
			||||||
 | 
					const url = require('url');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
 | 
				
			||||||
 | 
					// this allows to support system proxy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO: add proxy support - see https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function exec(opts) {
 | 
				
			||||||
 | 
					    const client = getClient(opts);
 | 
				
			||||||
 | 
					    const parsedUrl = url.parse(opts.url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return new Promise(async (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: parsedUrl.protocol,
 | 
				
			||||||
 | 
					                host: parsedUrl.hostname,
 | 
				
			||||||
 | 
					                port: parsedUrl.port,
 | 
				
			||||||
 | 
					                path: parsedUrl.path,
 | 
				
			||||||
 | 
					                timeout: opts.timeout,
 | 
				
			||||||
 | 
					                headers: {
 | 
				
			||||||
 | 
					                    Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
 | 
				
			||||||
 | 
					                    'Content-Type': 'application/json'
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            request.on('response', response => {
 | 
				
			||||||
 | 
					                if (opts.cookieJar && response.headers['set-cookie']) {
 | 
				
			||||||
 | 
					                    opts.cookieJar.header = response.headers['set-cookie'];
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                let responseStr = '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                response.on('data', chunk => responseStr += chunk);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                response.on('end', () => {
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
 | 
					                        const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        resolve(jsonObj);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    catch (e) {
 | 
				
			||||||
 | 
					                        log.error("Failed to deserialize sync response: " + responseStr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        reject(generateError(e));
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            request.end(opts.body ? JSON.stringify(opts.body) : undefined);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        catch (e) {
 | 
				
			||||||
 | 
					            reject(generateError(e));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function getClient(opts) {
 | 
				
			||||||
 | 
					    if (utils.isElectron()) {
 | 
				
			||||||
 | 
					        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(e) {
 | 
				
			||||||
 | 
					    return new Error(`Request to ${method} ${syncServerHost}${requestPath} failed, error: ${e.message}`);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = {
 | 
				
			||||||
 | 
					    exec
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@@ -14,6 +14,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');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let proxyToggle = true;
 | 
					let proxyToggle = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -216,80 +217,13 @@ async function checkContentHash(syncContext) {
 | 
				
			|||||||
    await contentHashService.checkContentHashes(resp.hashes);
 | 
					    await contentHashService.checkContentHashes(resp.hashes);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getClient() {
 | 
					 | 
				
			||||||
    if (utils.isElectron()) {
 | 
					 | 
				
			||||||
        return require('electron').net;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    else {
 | 
					 | 
				
			||||||
        const {protocol} = url.parse(await syncOptions.getSyncServerHost());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (protocol === 'http:' || protocol === 'https:') {
 | 
					 | 
				
			||||||
            return require(protocol.substr(0, protocol.length - 1));
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        else {
 | 
					 | 
				
			||||||
            throw new Error(`Unrecognized protocol "${protocol}"`);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function syncRequest(syncContext, method, requestPath, body) {
 | 
					async function syncRequest(syncContext, method, requestPath, body) {
 | 
				
			||||||
    const client = await getClient();
 | 
					    return await request.exec({
 | 
				
			||||||
    const syncServerHost = await syncOptions.getSyncServerHost();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    function generateError(e) {
 | 
					 | 
				
			||||||
        return new Error(`Request to ${method} ${syncServerHost}${requestPath} failed, error: ${e.message}`);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const parsedUrl = url.parse(syncServerHost);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // TODO: add proxy support - see https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return new Promise(async (resolve, reject) => {
 | 
					 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
            const request = client.request({
 | 
					 | 
				
			||||||
        method,
 | 
					        method,
 | 
				
			||||||
                // url is used by electron net module
 | 
					        url: await syncOptions.getSyncServerHost() + requestPath,
 | 
				
			||||||
                url: syncServerHost + requestPath,
 | 
					        cookieJar: syncContext.cookieJar,
 | 
				
			||||||
                // 4 fields below are used by http and https node modules
 | 
					 | 
				
			||||||
                protocol: parsedUrl.protocol,
 | 
					 | 
				
			||||||
                host: parsedUrl.hostname,
 | 
					 | 
				
			||||||
                port: parsedUrl.port,
 | 
					 | 
				
			||||||
                path: requestPath,
 | 
					 | 
				
			||||||
        timeout: await syncOptions.getSyncTimeout(),
 | 
					        timeout: await syncOptions.getSyncTimeout(),
 | 
				
			||||||
                headers: {
 | 
					        body
 | 
				
			||||||
                    Cookie: syncContext.cookieJar.header || "",
 | 
					 | 
				
			||||||
                    'Content-Type': 'application/json'
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            request.on('response', response => {
 | 
					 | 
				
			||||||
                if (response.headers['set-cookie']) {
 | 
					 | 
				
			||||||
                    syncContext.cookieJar.header = response.headers['set-cookie'];
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                let responseStr = '';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                response.on('data', chunk => responseStr += chunk);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                response.on('end', () => {
 | 
					 | 
				
			||||||
                    try {
 | 
					 | 
				
			||||||
                        const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                        resolve(jsonObj);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    catch (e) {
 | 
					 | 
				
			||||||
                        log.error("Failed to deserialize sync response: " + responseStr);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                        reject(generateError(e));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            request.end(JSON.stringify(body));
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        catch (e) {
 | 
					 | 
				
			||||||
            reject(generateError(e));
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user