Skip to content

Commit

Permalink
refactor: Migrate more modules to TypeScript (#33636)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Oct 21, 2024
1 parent ddc64d8 commit 5fddfcc
Show file tree
Hide file tree
Showing 37 changed files with 742 additions and 452 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import { settings } from '../../../settings/client';
import { sdk } from '../../../utils/client/lib/SDKClient';
import { CannedResponse } from '../collections/CannedResponse';

const events = {
changed: ({ type, ...response }) => {
CannedResponse.upsert({ _id: response._id }, response);
},
removed: (response) => CannedResponse.remove({ _id: response._id }),
};

Meteor.startup(() => {
Tracker.autorun(async (c) => {
if (!Meteor.userId()) {
Expand All @@ -27,12 +20,24 @@ Meteor.startup(() => {
Tracker.afterFlush(() => {
try {
// TODO: check options
sdk.stream('canned-responses', ['canned-responses'], (response, options) => {
sdk.stream('canned-responses', ['canned-responses'], (...[response, options]) => {
const { agentsId } = options || {};
if (Array.isArray(agentsId) && !agentsId.includes(Meteor.userId())) {
return;
}
events[response.type](response);

switch (response.type) {
case 'changed': {
const { type, ...fields } = response;
CannedResponse.upsert({ _id: response._id }, fields);
break;
}

case 'removed': {
CannedResponse.remove({ _id: response._id });
break;
}
}
});
} catch (error) {
console.log(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Accounts } from 'meteor/accounts-base';
import { e2e } from './rocketchat.e2e';

Accounts.onLogout(() => {
e2e.stopClient();
void e2e.stopClient();
});
Original file line number Diff line number Diff line change
@@ -1,67 +1,63 @@
import { Random } from '@rocket.chat/random';
import ByteBuffer from 'bytebuffer';

// eslint-disable-next-line no-proto
const StaticArrayBufferProto = new ArrayBuffer().__proto__;

export function toString(thing) {
export function toString(thing: any) {
if (typeof thing === 'string') {
return thing;
}
// eslint-disable-next-line new-cap
return new ByteBuffer.wrap(thing).toString('binary');

return ByteBuffer.wrap(thing).toString('binary');
}

export function toArrayBuffer(thing) {
export function toArrayBuffer(thing: any) {
if (thing === undefined) {
return undefined;
}
if (thing === Object(thing)) {
// eslint-disable-next-line no-proto
if (thing.__proto__ === StaticArrayBufferProto) {
if (typeof thing === 'object') {
if (Object.getPrototypeOf(thing) === ArrayBuffer.prototype) {
return thing;
}
}

if (typeof thing !== 'string') {
throw new Error(`Tried to convert a non-string of type ${typeof thing} to an array buffer`);
}
// eslint-disable-next-line new-cap
return new ByteBuffer.wrap(thing, 'binary').toArrayBuffer();

return ByteBuffer.wrap(thing, 'binary').toArrayBuffer();
}

export function joinVectorAndEcryptedData(vector, encryptedData) {
export function joinVectorAndEcryptedData(vector: any, encryptedData: any) {
const cipherText = new Uint8Array(encryptedData);
const output = new Uint8Array(vector.length + cipherText.length);
output.set(vector, 0);
output.set(cipherText, vector.length);
return output;
}

export function splitVectorAndEcryptedData(cipherText) {
export function splitVectorAndEcryptedData(cipherText: any) {
const vector = cipherText.slice(0, 16);
const encryptedData = cipherText.slice(16);

return [vector, encryptedData];
}

export async function encryptRSA(key, data) {
export async function encryptRSA(key: any, data: any) {
return crypto.subtle.encrypt({ name: 'RSA-OAEP' }, key, data);
}

export async function encryptAES(vector, key, data) {
export async function encryptAES(vector: any, key: any, data: any) {
return crypto.subtle.encrypt({ name: 'AES-CBC', iv: vector }, key, data);
}

export async function encryptAESCTR(vector, key, data) {
export async function encryptAESCTR(vector: any, key: any, data: any) {
return crypto.subtle.encrypt({ name: 'AES-CTR', counter: vector, length: 64 }, key, data);
}

export async function decryptRSA(key, data) {
export async function decryptRSA(key: any, data: any) {
return crypto.subtle.decrypt({ name: 'RSA-OAEP' }, key, data);
}

export async function decryptAES(vector, key, data) {
export async function decryptAES(vector: any, key: any, data: any) {
return crypto.subtle.decrypt({ name: 'AES-CBC', iv: vector }, key, data);
}

Expand All @@ -86,54 +82,54 @@ export async function generateRSAKey() {
);
}

export async function exportJWKKey(key) {
export async function exportJWKKey(key: any) {
return crypto.subtle.exportKey('jwk', key);
}

export async function importRSAKey(keyData, keyUsages = ['encrypt', 'decrypt']) {
export async function importRSAKey(keyData: any, keyUsages: ReadonlyArray<KeyUsage> = ['encrypt', 'decrypt']) {
return crypto.subtle.importKey(
'jwk',
'jwk' as any,
keyData,
{
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: 'SHA-256' },
},
} as any,
true,
keyUsages,
);
}

export async function importAESKey(keyData, keyUsages = ['encrypt', 'decrypt']) {
export async function importAESKey(keyData: any, keyUsages: ReadonlyArray<KeyUsage> = ['encrypt', 'decrypt']) {
return crypto.subtle.importKey('jwk', keyData, { name: 'AES-CBC' }, true, keyUsages);
}

export async function importRawKey(keyData, keyUsages = ['deriveKey']) {
export async function importRawKey(keyData: any, keyUsages: ReadonlyArray<KeyUsage> = ['deriveKey']) {
return crypto.subtle.importKey('raw', keyData, { name: 'PBKDF2' }, false, keyUsages);
}

export async function deriveKey(salt, baseKey, keyUsages = ['encrypt', 'decrypt']) {
export async function deriveKey(salt: any, baseKey: any, keyUsages: ReadonlyArray<KeyUsage> = ['encrypt', 'decrypt']) {
const iterations = 1000;
const hash = 'SHA-256';

return crypto.subtle.deriveKey({ name: 'PBKDF2', salt, iterations, hash }, baseKey, { name: 'AES-CBC', length: 256 }, true, keyUsages);
}

export async function readFileAsArrayBuffer(file) {
return new Promise((resolve, reject) => {
export async function readFileAsArrayBuffer(file: any) {
return new Promise<any>((resolve, reject) => {
const reader = new FileReader();
reader.onload = function (evt) {
resolve(evt.target.result);
reader.onload = (evt) => {
resolve(evt.target?.result);
};
reader.onerror = function (evt) {
reader.onerror = (evt) => {
reject(evt);
};
reader.readAsArrayBuffer(file);
});
}

export async function generateMnemonicPhrase(n, sep = ' ') {
export async function generateMnemonicPhrase(n: any, sep = ' ') {
const { default: wordList } = await import('./wordList');
const result = new Array(n);
let len = wordList.length;
Expand All @@ -147,14 +143,14 @@ export async function generateMnemonicPhrase(n, sep = ' ') {
return result.join(sep);
}

export async function createSha256HashFromText(data) {
export async function createSha256HashFromText(data: any) {
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(data));
return Array.from(new Uint8Array(hash))
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
}

export async function sha256HashFromArrayBuffer(arrayBuffer) {
export async function sha256HashFromArrayBuffer(arrayBuffer: any) {
const hashArray = Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256', arrayBuffer)));
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
}
Loading

0 comments on commit 5fddfcc

Please sign in to comment.