initial commit of worker to load messages parallely successfull

This commit is contained in:
nova 2019-12-15 19:48:24 +05:30
parent 2946f31124
commit 97a89e5624
7 changed files with 50 additions and 4 deletions

View File

@ -14,6 +14,7 @@
"handlebars": "^4.5.3", "handlebars": "^4.5.3",
"loglevel": "^1.6.6", "loglevel": "^1.6.6",
"markdown-it": "^10.0.0", "markdown-it": "^10.0.0",
"promise-worker": "^2.0.1",
"sjcl": "^1.0.8", "sjcl": "^1.0.8",
"sprintf-js": "^1.1.2", "sprintf-js": "^1.1.2",
"tsify": "^4.0.1", "tsify": "^4.0.1",

View File

@ -113,6 +113,14 @@ const encryptionService: EncryptionService = new SJCLEncryptionService();
let messageCipherDTO: MessageCipherDTO = encryptionService.encrypt("password", "data"); let messageCipherDTO: MessageCipherDTO = encryptionService.encrypt("password", "data");
console.log(encryptionService.decrypt("password", messageCipherDTO)); console.log(encryptionService.decrypt("password", messageCipherDTO));
async function func(): Promise<void> {
const text = await encryptionService.decryptAsPromise("password", messageCipherDTO)
log.debug(text);
}
func();
Handlebars.registerHelper('avatar', function () { Handlebars.registerHelper('avatar', function () {
return '<div class="img_cont_msg"> <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img_msg"> </div>'; return '<div class="img_cont_msg"> <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img_msg"> </div>';
}); });

View File

@ -15,7 +15,9 @@ export class ChatModelHelper {
switch (lastMessageTime) { switch (lastMessageTime) {
case null: { case null: {
const data: ChatMessageDTO[] = await this.getPaginatedMessagesAjax(userName, page!); const data: ChatMessageDTO[] = await this.getPaginatedMessagesAjax(userName, page!);
return data.map(vm => this.toChatMessageVM(vm, passphrase)).reverse(); const data2 = Promise.all(data.map(vm => this.toChatMessageVMAsync(vm, passphrase)).reverse());
return data2;
// return data.map(vm => this.toChatMessageVM(vm, passphrase)).reverse();
} }
default: { default: {
const data: ChatMessageDTO[] = await this.getNewMessagesAjax(userName, lastMessageTime); const data: ChatMessageDTO[] = await this.getNewMessagesAjax(userName, lastMessageTime);
@ -24,6 +26,16 @@ export class ChatModelHelper {
} }
} }
private static async toChatMessageVMAsync(chatMessageDTO: ChatMessageDTO, passphrase: string): Promise<ChatMessageViewModel> {
const vm = new ChatMessageViewModel();
vm.fromUser = chatMessageDTO.fromUser;
vm.toUser = chatMessageDTO.toUser;
// vm.messageTime = chatMessageDTO.messageTime;
chatMessageDTO.messageTime == null ? log.error("Message time somehow null") : vm.messageTime = chatMessageDTO.messageTime;
vm.message = await this._encryptionService.decryptAsPromise(passphrase, chatMessageDTO.messageCipher);
return vm;
}
private static toChatMessageVM(chatMessageDTO: ChatMessageDTO, passphrase: string): ChatMessageViewModel { private static toChatMessageVM(chatMessageDTO: ChatMessageDTO, passphrase: string): ChatMessageViewModel {
const vm = new ChatMessageViewModel(); const vm = new ChatMessageViewModel();
vm.fromUser = chatMessageDTO.fromUser; vm.fromUser = chatMessageDTO.fromUser;

View File

@ -3,4 +3,5 @@ import { MessageCipherDTO } from "../dto/MessageCipherDTO";
export interface EncryptionService { export interface EncryptionService {
encrypt(passphrase: string, plainText: string): any; encrypt(passphrase: string, plainText: string): any;
decrypt(passphrase: string, cipher: MessageCipherDTO): string; decrypt(passphrase: string, cipher: MessageCipherDTO): string;
decryptAsPromise(passphrase: string, cipher: MessageCipherDTO): Promise<string>
} }

View File

@ -1,15 +1,24 @@
import { EncryptionService } from "./EncryptionService"; import { EncryptionService } from "./EncryptionService";
import * as sjcl from "sjcl"; import * as sjcl from "sjcl";
import { MessageCipherDTO } from "../dto/MessageCipherDTO"; import { MessageCipherDTO } from "../dto/MessageCipherDTO";
import PromiseWorker = require('promise-worker');
export class SJCLEncryptionService implements EncryptionService { export class SJCLEncryptionService implements EncryptionService {
private readonly params = { mode: "gcm", ts: 128, adata: "", iter: 10000 } private readonly _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 };
private readonly _worker = new Worker('/js/worker.js');
// @ts-ignore
private readonly _promiseWorker = new PromiseWorker(this._worker);
public encrypt(passphrase: string, plainText: string): MessageCipherDTO { public encrypt(passphrase: string, plainText: string): MessageCipherDTO {
// @ts-ignore // @ts-ignore
return JSON.parse(sjcl.encrypt(passphrase, plainText, this.params) as string) as MessageCipherDTO; return JSON.parse(sjcl.encrypt(passphrase, plainText, this._params) as string) as MessageCipherDTO;
} }
public decrypt(passphrase: string, cipher: MessageCipherDTO): string { public decrypt(passphrase: string, cipher: MessageCipherDTO): string {
return sjcl.decrypt(passphrase, JSON.stringify(cipher), undefined, undefined); return sjcl.decrypt(passphrase, JSON.stringify(cipher), undefined, undefined);
} }
public async decryptAsPromise(passphrase: string, cipher: MessageCipherDTO): Promise<string> {
const decrypted = await this._promiseWorker.postMessage({"passphrase": passphrase, "cipher": cipher})
return decrypted;
}
} }

View File

@ -1718,6 +1718,11 @@ process@^0.11.10, process@~0.11.0:
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI= integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI=
promise-worker@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/promise-worker/-/promise-worker-2.0.1.tgz#63bb532624ecd40cdb335b51bb7830c3c892aa6c"
integrity sha512-jR7vHqMEwWJ15i9vA3qyCKwRHihyLJp1sAa3RyY5F35m3u5s2lQUfq0nzVjbA8Xc7+3mL3Y9+9MHBO9UFRpFxA==
public-encrypt@^4.0.0: public-encrypt@^4.0.0:
version "4.0.3" version "4.0.3"
resolved "https://registry.yarnpkg.com/public-encrypt/-/public-encrypt-4.0.3.tgz#4fcc9d77a07e48ba7527e7cbe0de33d0701331e0" resolved "https://registry.yarnpkg.com/public-encrypt/-/public-encrypt-4.0.3.tgz#4fcc9d77a07e48ba7527e7cbe0de33d0701331e0"

View File

@ -0,0 +1,10 @@
// worker.js
importScripts('https://unpkg.com/promise-worker/dist/promise-worker.register.js');
importScripts('https://cdnjs.cloudflare.com/ajax/libs/sjcl/1.0.8/sjcl.min.js');
registerPromiseWorker((payload) => {
// console.log(payload)
// console.log('decrypted = ' + sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher)));
// return 'pong';
return sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher))
});