Send outgoing messages as raw bytes (Uint8Array) and moved recipient & ttl to header

This commit is contained in:
sachaaaaa 2018-10-09 14:43:55 +11:00
parent 6d84eb69e7
commit 6fe70b9b85
3 changed files with 9143 additions and 9145 deletions

View file

@ -29,22 +29,19 @@ function initialize({ url }) {
};
log.info(options.type, options.url);
const body = JSON.stringify({
pub_key,
message: data,
ttl,
});
const fetchOptions = {
method: options.type,
body,
headers: { 'X-Loki-Messenger-Agent': 'OWD' },
body: data,
headers: {
'X-Loki-ttl': ttl,
'X-Loki-recipient': pub_key,
'Content-Length': data.byteLength,
},
timeout: options.timeout,
};
fetchOptions.headers['Content-Type'] = 'application/json; charset=utf-8';
let response;
try {
response = await fetch(options.url, fetchOptions);

File diff suppressed because it is too large Load diff

View file

@ -150,11 +150,11 @@ OutgoingMessage.prototype = {
return promise;
},
async transmitMessage(number, jsonData, timestamp) {
async transmitMessage(number, data, timestamp) {
const pubKey = number;
const ttl = 2 * 24 * 60 * 60;
try {
const [response, status] = await this.lokiserver.sendMessage(pubKey, JSON.stringify(jsonData), ttl);
const [response, status] = await this.lokiserver.sendMessage(pubKey, data, ttl);
return response;
}
catch (e) {
@ -259,7 +259,7 @@ OutgoingMessage.prototype = {
return {
type : 6, //friend request
body : new dcodeIO.ByteBuffer.wrap(ivAndCiphertext).toString('binary'),
body : ivAndCiphertext,
registrationId : null
};
}
@ -280,8 +280,7 @@ OutgoingMessage.prototype = {
address: address,
destinationDeviceId: address.getDeviceId(),
destinationRegistrationId: ciphertext.registrationId,
// TODO: simplify the binary -> string -> binary here
content: dcodeIO.ByteBuffer.wrap(ciphertext.body,'binary').toArrayBuffer(),
content: ciphertext.body,
}));
})
)
@ -290,7 +289,8 @@ OutgoingMessage.prototype = {
outgoingObjects.forEach(outgoingObject => {
promises.push(this.wrapInWebsocketMessage(outgoingObject));
});
const socketMessages = await Promise.all(promises);
// TODO: handle multiple devices/messages per transmit
const socketMessages = await promises[0];
await this.transmitMessage(number, socketMessages, this.timestamp);
this.successfulNumbers[this.successfulNumbers.length] = number;
this.numberCompleted();