Add skeleton for Loki Message API. Wrap data in json (for now?)
This commit is contained in:
parent
8f33419b52
commit
1e14022e2f
|
@ -0,0 +1,113 @@
|
|||
const fetch = require('node-fetch');
|
||||
const is = require('@sindresorhus/is');
|
||||
|
||||
module.exports = {
|
||||
initialize,
|
||||
};
|
||||
|
||||
function initialize({ url }) {
|
||||
if (!is.string(url)) {
|
||||
throw new Error('WebAPI.initialize: Invalid server url');
|
||||
}
|
||||
|
||||
return {
|
||||
connect,
|
||||
};
|
||||
|
||||
function connect() {
|
||||
return {
|
||||
sendMessage
|
||||
};
|
||||
|
||||
function sendMessage(options)
|
||||
{
|
||||
return new Promise((resolve, reject) => {
|
||||
// const url = providedUrl || `${options.host}/${options.path}`;
|
||||
log.info(options.type, url);
|
||||
const timeout =
|
||||
typeof options.timeout !== 'undefined' ? options.timeout : 10000;
|
||||
|
||||
body = JSON.stringify({
|
||||
pub_key: options.pub_key,
|
||||
message: String.fromCharCode.apply(null, options.data),
|
||||
ttl: options.ttl,
|
||||
})
|
||||
|
||||
const fetchOptions = {
|
||||
method: 'PUT',
|
||||
body,
|
||||
headers: { 'X-Loki-Messenger-Agent': 'OWD' },
|
||||
timeout,
|
||||
};
|
||||
|
||||
if (fetchOptions.body instanceof ArrayBuffer) {
|
||||
// node-fetch doesn't support ArrayBuffer, only node Buffer
|
||||
const contentLength = fetchOptions.body.byteLength;
|
||||
fetchOptions.body = Buffer.from(fetchOptions.body);
|
||||
|
||||
// node-fetch doesn't set content-length like S3 requires
|
||||
fetchOptions.headers['Content-Length'] = contentLength;
|
||||
}
|
||||
fetchOptions.headers['Content-Type'] = 'application/json; charset=utf-8';
|
||||
|
||||
fetch(url, fetchOptions)
|
||||
.then(response => {
|
||||
let resultPromise;
|
||||
if (
|
||||
options.responseType === 'json' &&
|
||||
response.headers.get('Content-Type') === 'application/json'
|
||||
) {
|
||||
resultPromise = response.json();
|
||||
} else if (options.responseType === 'arraybuffer') {
|
||||
resultPromise = response.buffer();
|
||||
} else {
|
||||
resultPromise = response.text();
|
||||
}
|
||||
return resultPromise.then(result => {
|
||||
if (options.responseType === 'arraybuffer') {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
result = result.buffer.slice(
|
||||
result.byteOffset,
|
||||
result.byteOffset + result.byteLength
|
||||
);
|
||||
}
|
||||
if (options.responseType === 'json') {
|
||||
if (options.validateResponse) {
|
||||
if (!_validateResponse(result, options.validateResponse)) {
|
||||
log.error(options.type, url, response.status, 'Error');
|
||||
reject(
|
||||
HTTPError(
|
||||
'promiseAjax: invalid response',
|
||||
response.status,
|
||||
result,
|
||||
options.stack
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (response.status >= 0 && response.status < 400) {
|
||||
log.info(options.type, url, response.status, 'Success');
|
||||
resolve(result, response.status);
|
||||
} else {
|
||||
log.error(options.type, url, response.status, 'Error');
|
||||
reject(
|
||||
HTTPError(
|
||||
'promiseAjax: error response',
|
||||
response.status,
|
||||
result,
|
||||
options.stack
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(e => {
|
||||
log.error(options.type, url, 0, 'Error');
|
||||
const stack = `${e.stack}\nInitial stack:\n${options.stack}`;
|
||||
reject(HTTPError('promiseAjax catch', 0, e.toString(), stack));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,6 +23,8 @@ function OutgoingMessage(
|
|||
this.callback = callback;
|
||||
this.silent = silent;
|
||||
|
||||
this.lokiserver = window.LokiAPI.connect()
|
||||
|
||||
this.numbersCompleted = 0;
|
||||
this.errors = [];
|
||||
this.successfulNumbers = [];
|
||||
|
@ -306,6 +308,19 @@ OutgoingMessage.prototype = {
|
|||
},
|
||||
|
||||
sendToNumber(number) {
|
||||
const options = {
|
||||
pub_key: number,
|
||||
data: this.getPlaintext(),
|
||||
ttl: 2 * 24 * 60 * 60
|
||||
};
|
||||
return this.lokiserver.sendMessage(options).then(
|
||||
(result, status) => {
|
||||
this.successfulNumbers[this.successfulNumbers.length] = number;
|
||||
this.numberCompleted();
|
||||
},
|
||||
(error) => console.log('Loki sendMessage failed')
|
||||
);
|
||||
/*
|
||||
return this.getStaleDeviceIdsForNumber(number).then(updateDevices =>
|
||||
this.getKeysForNumber(number, updateDevices)
|
||||
.then(this.reloadDevicesAndSend(number, true))
|
||||
|
@ -327,6 +342,6 @@ OutgoingMessage.prototype = {
|
|||
);
|
||||
}
|
||||
})
|
||||
);
|
||||
);*/
|
||||
},
|
||||
};
|
||||
|
|
1
main.js
1
main.js
|
@ -134,6 +134,7 @@ function prepareURL(pathSegments, moreKeys) {
|
|||
serverUrl: config.get('serverUrl'),
|
||||
cdnUrl: config.get('cdnUrl'),
|
||||
certificateAuthority: config.get('certificateAuthority'),
|
||||
skipEncryption: config.get('skipEncryption'),
|
||||
environment: config.environment,
|
||||
node_version: process.versions.node,
|
||||
hostname: os.hostname(),
|
||||
|
|
|
@ -200,6 +200,12 @@ window.WebAPI = initializeWebAPI({
|
|||
proxyUrl: config.proxyUrl,
|
||||
});
|
||||
|
||||
const { initialize: initializeLokiAPI } = require('./js/modules/loki_message_api');
|
||||
|
||||
window.LokiAPI = initializeLokiAPI({
|
||||
url: config.serverUrl,
|
||||
});
|
||||
|
||||
// Linux seems to periodically let the event loop stop, so this is a global workaround
|
||||
setInterval(() => {
|
||||
window.nodeSetImmediate(() => {});
|
||||
|
|
Loading…
Reference in New Issue