sync with electron net API (for system proxy support) - working, but WIP

This commit is contained in:
azivner 2018-12-16 21:19:12 +01:00
parent 5b6d15acb3
commit b942163748
2 changed files with 58 additions and 18 deletions

View file

@ -29,7 +29,7 @@ async function loginSync(req) {
const syncVersion = req.body.syncVersion; const syncVersion = req.body.syncVersion;
if (syncVersion !== appInfo.syncVersion) { if (syncVersion !== appInfo.syncVersion) {
return [400, { message: 'Non-matching sync versions, local is version ' + appInfo.syncVersion }]; return [400, { message: `Non-matching sync versions, local is version ${appInfo.syncVersion}, remote is ${syncVersion}` }];
} }
const documentSecret = await options.getOption('documentSecret'); const documentSecret = await options.getOption('documentSecret');

View file

@ -84,7 +84,7 @@ async function doLogin() {
const documentSecret = await optionService.getOption('documentSecret'); const documentSecret = await optionService.getOption('documentSecret');
const hash = utils.hmac(documentSecret, timestamp); const hash = utils.hmac(documentSecret, timestamp);
const syncContext = { cookieJar: rp.jar() }; const syncContext = { cookieJar: {} };
const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', { const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', {
timestamp: timestamp, timestamp: timestamp,
@ -111,6 +111,10 @@ async function pullSync(syncContext) {
const resp = await syncRequest(syncContext, 'GET', changesUri); const resp = await syncRequest(syncContext, 'GET', changesUri);
stats.outstandingPulls = resp.maxSyncId - lastSyncedPull; stats.outstandingPulls = resp.maxSyncId - lastSyncedPull;
if (stats.outstandingPulls < 0) {
stats.outstandingPulls = 0;
}
const rows = resp.syncs; const rows = resp.syncs;
if (rows.length === 0) { if (rows.length === 0) {
@ -215,6 +219,42 @@ async function checkContentHash(syncContext) {
async function syncRequest(syncContext, method, uri, body) { async function syncRequest(syncContext, method, uri, body) {
const fullUri = await syncOptions.getSyncServerHost() + uri; const fullUri = await syncOptions.getSyncServerHost() + uri;
if (utils.isElectron()) {
return new Promise((resolve, reject) => {
try {
const { net } = require('electron');
const request = net.request({
method,
url: fullUri,
headers: {
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 data = '';
response.on('data', chunk => data += chunk);
response.on('end', () => resolve(data.trim() ? JSON.parse(data) : null));
});
request.end(JSON.stringify(body));
}
catch (e) {
console.log(e);
reject(e.message);
}
});
}
else {
try { try {
const options = { const options = {
method: method, method: method,
@ -232,11 +272,11 @@ async function syncRequest(syncContext, method, uri, body) {
} }
return await rp(options); return await rp(options);
} } catch (e) {
catch (e) {
throw new Error(`Request to ${method} ${fullUri} failed, error: ${e.message}`); throw new Error(`Request to ${method} ${fullUri} failed, error: ${e.message}`);
} }
} }
}
const primaryKeys = { const primaryKeys = {
"notes": "noteId", "notes": "noteId",