The only way is to use a typed array, for example, Uint8Array (the Buffer class is just a thin shell), and fill it with null or any other value after use.
But there are a few things to consider:
When you get credentials, for example, you most likely get them as JSON. In this case, any efforts will be in vain if you do not intercept, so the request body is not stored inside the string, as any web structure does.
When you read API keys from text files, it should be obvious why memory security may not be the biggest problem. To do this, consider the keystore.
But even then you cannot be sure that there are no leaks. For example, I doubt that node.js streams will clean up after themselves. And even if you override them manually, leaks from copying or system APIs are possible. Even password managers have problems with password leaks.
To summarize: even if you consider everything, there are likely to be leaks that you cannot prevent.
Marvin H.
source share