From d3efba9dd582243d9d101cae0656fd39f8ea6990 Mon Sep 17 00:00:00 2001 From: Sean Date: Mon, 21 Oct 2019 17:17:14 +0200 Subject: [PATCH] Replace encode and decode Replaces encode and decode with manual functions --- client/package.json | 1 - .../core/core-services/websocket.service.ts | 32 +++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/client/package.json b/client/package.json index df72164c8..94e262550 100644 --- a/client/package.json +++ b/client/package.json @@ -69,7 +69,6 @@ "pdfmake": "^0.1.58", "po2json": "^1.0.0-alpha", "rxjs": "^6.5.2", - "text-encoding": "^0.7.0", "tinymce": "^5.0.14", "tslib": "^1.10.0", "uuid": "^3.3.2", diff --git a/client/src/app/core/core-services/websocket.service.ts b/client/src/app/core/core-services/websocket.service.ts index c237a96f9..d4dcdd9d4 100644 --- a/client/src/app/core/core-services/websocket.service.ts +++ b/client/src/app/core/core-services/websocket.service.ts @@ -5,7 +5,6 @@ import { Router } from '@angular/router'; import { compress, decompress } from 'lz4js'; import { Observable, Subject } from 'rxjs'; import { take } from 'rxjs/operators'; -import { TextDecoder, TextEncoder } from 'text-encoding'; import { OfflineService } from './offline.service'; import { OpenSlidesStatusService } from './openslides-status.service'; @@ -320,8 +319,7 @@ export class WebsocketService { `Recieved ${compressedSize / 1024} KB (${decompressedBuffer.byteLength / 1024} KB uncompressed), ratio ${decompressedBuffer.byteLength / compressedSize}` ); - const textDecoder = new TextDecoder(); - data = textDecoder.decode(decompressedBuffer) as string; + data = this.arrayBufferToString(decompressedBuffer); } const message: IncommingWebsocketMessage = JSON.parse(data); @@ -499,11 +497,9 @@ export class WebsocketService { this.responseCallbacks[message.id] = [success, error]; } - // Either send directly or add to queue, if not connected. 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 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; + } }