refactor: Change Sync Functions to Promises to Prevent Blocking Event Loop (#896)

* initial commit

* lint

* lint 2
This commit is contained in:
vini
2024-07-03 04:29:16 -03:00
committed by GitHub
parent fdf9d48162
commit dbf25771a1
5 changed files with 21 additions and 15 deletions

View File

@@ -1,9 +1,12 @@
import { createCipheriv, createDecipheriv, createHash, createHmac, pbkdf2Sync, randomBytes } from 'crypto'
import { createCipheriv, createDecipheriv, createHash, createHmac, pbkdf2, randomBytes } from 'crypto'
import HKDF from 'futoin-hkdf'
import * as libsignal from 'libsignal'
import { promisify } from 'util'
import { KEY_BUNDLE_TYPE } from '../Defaults'
import { KeyPair } from '../Types'
const pbkdf2Promise = promisify(pbkdf2)
/** prefix version byte to the pub keys, required for some curve crypto functions */
export const generateSignalPubKey = (pubKey: Uint8Array | Buffer) => (
pubKey.length === 33
@@ -126,6 +129,6 @@ export function hkdf(buffer: Uint8Array | Buffer, expandedLength: number, info:
return HKDF(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info)
}
export function derivePairingCodeKey(pairingCode: string, salt: Buffer) {
return pbkdf2Sync(pairingCode, salt, 2 << 16, 32, 'sha256')
}
export async function derivePairingCodeKey(pairingCode: string, salt: Buffer) {
return await pbkdf2Promise(pairingCode, salt, 2 << 16, 32, 'sha256')
}

View File

@@ -163,7 +163,7 @@ export const makeNoiseHandler = ({
return frame
},
decodeFrame: (newData: Buffer | Uint8Array, onFrame: (buff: Uint8Array | BinaryNode) => void) => {
decodeFrame: async(newData: Buffer | Uint8Array, onFrame: (buff: Uint8Array | BinaryNode) => void) => {
// the binary protocol uses its own framing mechanism
// on top of the WS frames
// so we get this data and separate out the frames
@@ -184,7 +184,7 @@ export const makeNoiseHandler = ({
if(isFinished) {
const result = decrypt(frame as Uint8Array)
frame = decodeBinaryNode(result)
frame = await decodeBinaryNode(result)
}
logger.trace({ msg: (frame as any)?.attrs?.id }, 'recv frame')