Argument of type '{}' is not assignable to parameter of type 'Uint8Array' - node.js

I'm trying to clone and run an opensource project repo and having hard time fixing this issue, npm start fails with "compile failed error' and its states the following reason.
Argument of type '{}' is not assignable to parameter of type 'Uint8Array'
const [encChallenge] = await waitEvent(socket, 'data')
const challenge = decrypt(encChallenge) //This line causes the error
Following in the decrypt function
/** Decrypt data used shared key */
function decrypt(data: Uint8Array) {
if (!sharedKey) return null
const nonce = data.slice(0, sodium.crypto_box_NONCEBYTES)
const box = data.slice(sodium.crypto_box_NONCEBYTES, data.length)
const msg = crypto.decrypt(box, nonce, sharedKey)
return msg
}
Changing the parameter to any solves it but I can't do that,
How can I convert my parameter to Unit8Array?

As long as encChallenge is a typed array represents an array of 8-bit unsigned integers then you should just be able to do:
const [encChallenge] = await waitEvent(socket, 'data')
const challenge = decrypt(new Uint8Array(encChallenge);)
really i would make waitEvent encChallenge be of strongly type Uint8Array in waitEvent method, then it's abstracted away and always that type if you reuse it again.

Related

How to create a type '[*c]const [*c]const u8' for paramValues of PQexecParams

I'm trying to use the libpq library in zig. I'm trying to pass paramValues to PQexecParams. I'm just not sure how to create the required type.
The type required by the documentation is:
const char * const *paramValues
So something like:
const char data[2][2] = {"12","me"};
If do something like this in zig:
const paramValues = [_][]const u8 {"12","me"};
I get this error:
error: expected type '[*c]const [*c]const u8', found '[2][]const u8'
Use:
const paramValues = [_][*:0]const u8 {"12","me"};
PQexecParams(....., &paramValues, ....);

node.js Typescript project, key-value pair

So I try to create a value pair type and get the error message: "Cannot find name 'key'."
How does it work properly?
import * as websocket from "websocket";
let wsClientsList: {[key: string]: websocket.connection};
for(key in wsClientsList){
// ^^^ TS2304: Cannot find name 'key'.
wsClientsList[key].sendUTF(message);
console.log('send Message to: ', wsClientsList[key]);
}
You need to declare a variable named key using var, let or const
for(let key in wsClientsList) {
wsClientsList[key].sendUTF(message);
console.log('send Message to: ', wsClientsList[key]);
}
You can use const in most cases in for in and for of loops, unlike in traditional for loops with increment.

TypeScript - Type undefined is not assignable to type ICustomType

I'm quite new to TypeScript and trying to understand what is the best way to approach such situation in my code.
I have array of objects that have a custom type in my system and I use Array.find method to get one of them. However I receive a compile error saying Type 'undefined' is not assignable to type IConfig.
Here's the code example -
const config: IConfig = CONFIGS.find(({ code }) => code === 'default');
// Type 'undefined' is not assignable to type IConfig
I tried to add undefined as possible type but then I get error on the next line which uses this object Object is possibly 'undefined', e.g. -
const config: IConfig | undefined = CONFIGS.find(({ code }) => code === 'default');
// Object is possibly 'undefined'
if (config.foo) {
return 'bar';
}
What is the best way to approach such type issues?
.find will return undefined if nothing in the array passes the callback test. If you're sure that default code exists in the array, then use the non-null assertion operator:
const config: IConfig = CONFIGS.find(({ code }) => code === 'default')!;
// ^
(If you weren't sure if it exists in the array, the warning you see is there to prompt you to explicitly test for if the item exists before trying to access a property on it, otherwise you'll sometimes get a runtime error:
const config: IConfig = CONFIGS.find(({ code }) => code === 'default');
if (!config) {
return 'No match found';
}
if (config.foo) {
return 'bar';
}
)

Different result in NodeJS calculating MD5 hash using crypo

I am trying to get the MD5 has from a number in NodeJS using crypto but I am getting a different hash returned then I get from site where I can calculate the has.
According to http://onlinemd5.com/ the MD5 has for 1092000 is AF118C8D2A0D27A1D49582FDF6339B7C.
When I try to calculate the hash for that number in NodeJS it gives me a different result (ac4d61a5b76c96b00235a124dfd1bfd1). My code:
const crypto = require('crypto');
const num = 1092000;
const hash = crypto.createHash('md5').update(toString(num)).digest('hex');
console.log(hash);
If you convert it to a string normally it works:
const hash = crypto.createHash('md5').update(String(num)).digest('hex'); // or num.toString()
See the difference:
toString(num) = [object Undefined]
(1092000).toString() = "1092000"
If you console.log(this) in a Node env by default you will see that it is:
this = {} typeof = 'object'
this in a Node env is pointing at module.exports so you're calling this toString on the Object.prototype which is not the right thing to do a string conversion on anything other than module.exports.

TS2345: Argument of type X is not assignable to parameter of type Y

Weird error with TypeScript:
As the image indicates, the error is:
TS2345: Argument of type 'ErrnoException' is not assignable to
parameter of type '(err: ErrnoException) => void'. Type
'ErrnoException' provides no match for the signature '(err:
ErrnoException): void'.
Here is the code that causes the error:
export const bump = function(cb: ErrnoException){
const {pkg, pkgPath} = syncSetup();
fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), cb);
};
Anybody know what's going on here?
You are sending a value with the type ErrnoException, while the function you are calling expects a function that takes a parameter of type *ErrnoException** and returns void.
You send:
let x = new ErrnoException;
While the function you call expects
let cb = function(e: ErrnoException) {};
You can change your function to receive the correct parameter like this.
export const bump = function(cb: (err: ErrnoException) => void){
const {pkg, pkgPath} = syncSetup();
fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), cb);
};

Resources