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

pull/8/head
sachaaaaa 7 years ago
parent 6d84eb69e7
commit 6fe70b9b85

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

File diff suppressed because it is too large Load Diff

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

Loading…
Cancel
Save