Replace encode and decode

Replaces encode and decode with manual functions
This commit is contained in:
Sean 2019-10-21 17:17:14 +02:00
parent 62e5774c8d
commit d3efba9dd5
2 changed files with 26 additions and 7 deletions

View File

@ -69,7 +69,6 @@
"pdfmake": "^0.1.58", "pdfmake": "^0.1.58",
"po2json": "^1.0.0-alpha", "po2json": "^1.0.0-alpha",
"rxjs": "^6.5.2", "rxjs": "^6.5.2",
"text-encoding": "^0.7.0",
"tinymce": "^5.0.14", "tinymce": "^5.0.14",
"tslib": "^1.10.0", "tslib": "^1.10.0",
"uuid": "^3.3.2", "uuid": "^3.3.2",

View File

@ -5,7 +5,6 @@ import { Router } from '@angular/router';
import { compress, decompress } from 'lz4js'; import { compress, decompress } from 'lz4js';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { take } from 'rxjs/operators'; import { take } from 'rxjs/operators';
import { TextDecoder, TextEncoder } from 'text-encoding';
import { OfflineService } from './offline.service'; import { OfflineService } from './offline.service';
import { OpenSlidesStatusService } from './openslides-status.service'; import { OpenSlidesStatusService } from './openslides-status.service';
@ -320,8 +319,7 @@ export class WebsocketService {
`Recieved ${compressedSize / 1024} KB (${decompressedBuffer.byteLength / `Recieved ${compressedSize / 1024} KB (${decompressedBuffer.byteLength /
1024} KB uncompressed), ratio ${decompressedBuffer.byteLength / compressedSize}` 1024} KB uncompressed), ratio ${decompressedBuffer.byteLength / compressedSize}`
); );
const textDecoder = new TextDecoder(); data = this.arrayBufferToString(decompressedBuffer);
data = textDecoder.decode(decompressedBuffer) as string;
} }
const message: IncommingWebsocketMessage = JSON.parse(data); const message: IncommingWebsocketMessage = JSON.parse(data);
@ -499,11 +497,9 @@ export class WebsocketService {
this.responseCallbacks[message.id] = [success, error]; this.responseCallbacks[message.id] = [success, error];
} }
// Either send directly or add to queue, if not connected.
const jsonMessage = JSON.stringify(message); const jsonMessage = JSON.stringify(message);
const bytesMessage = this.stringToBuffer(jsonMessage);
const textEncoder = new TextEncoder();
const bytesMessage = textEncoder.encode(jsonMessage);
const compressedMessage: ArrayBuffer = compress(bytesMessage); const compressedMessage: ArrayBuffer = compress(bytesMessage);
const ratio = bytesMessage.byteLength / compressedMessage.byteLength; const ratio = bytesMessage.byteLength / compressedMessage.byteLength;
@ -542,4 +538,28 @@ export class WebsocketService {
); );
}); });
} }
/**
* Converts an ArrayBuffer to a String.
*
* @param buffer - Buffer to convert
* @returns String
*/
private arrayBufferToString(buffer: ArrayBuffer): string {
return String.fromCharCode.apply(null, Array.from(new Uint16Array(buffer)));
}
/**
* Converts a String to an ArrayBuffer.
*
* @param str - String to convert.
* @returns bufferView.
*/
private stringToBuffer(str: string): Uint8Array {
const bufferView = new Uint8Array();
for (let i = 0; i < str.length; i++) {
bufferView[i] = str.charCodeAt(i);
}
return bufferView;
}
} }