Skip to content

Buffer Stream

Timo Heckel edited this page Nov 10, 2020 · 1 revision

How to use Node.JS buffer streams instead of a file

If the user for example, pastes a CSR into a text field, you probably don't want to convert it to a temporary text file first in order to use it in the SSL command.
Here is how you can use the buffer stream to replace the certificate signing request with a variable:

We are going to use the following example command to verify a CSR openssl req -text -noout -verify -in CSR.csr.

First, initialize an empty object, a variable with the CSR text and convert it to a buffer variable.

let sslconfig = [];


var csr = `-----BEGIN CERTIFICATE REQUEST-----
MIIByjCCATMCAQAwgYkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
MRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMR8w
HQYDVQQLExZJbmZvcm1hdGlvbiBUZWNobm9sb2d5MRcwFQYDVQQDEw53d3cuZ29v
Z2xlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEApZtYJCHJ4VpVXHfV
IlstQTlO4qC03hjX+ZkPyvdYd1Q4+qbAeTwXmCUKYHThVRd5aXSqlPzyIBwieMZr
WFlRQddZ1IzXAlVRDWwAo60KecqeAXnnUK+5fXoTI/UgWshre8tJ+x/TMHaQKR/J
cIWPhqaQhsJuzZbvAdGA80BLxdMCAwEAAaAAMA0GCSqGSIb3DQEBBQUAA4GBAIhl
4PvFq+e7ipARgI5ZM+GZx6mpCz44DTo0JkwfRDf+BtrsaC0q68eTf2XhYOsq4fkH
Q0uA0aVog3f5iJxCa3Hp5gxbJQ6zV6kJ0TEsuaaOhEko9sdpCoPOnRBm2i/XRD2D
6iNh8f8z0ShGsFqjDgFHyF3o+lUyj+UC6H1QW7bn
-----END CERTIFICATE REQUEST-----`;

var buf = Buffer.from(csr, 'utf8');

After that, we need to build our main config object.
You are free to use other config options as long as you use an object based params config.

The name: field of the buffer array is not a fixed value. You can name it as you prefer - Just make sure its a unique name because the library will create a temporary file inside the specified path.

sslconfig["params"] = ["req", "-text", "-noout", "-verify", "-in", { name: "buf.csr", buffer: buf }];
sslconfig["path"] = "X:\\Temporary";
sslconfig["beautify"] = true;
sslconfig["appendConf"] = true;
sslconfig["useShell"] = true;
sslconfig["debugMode"] = true;
sslconfig["preferSync"] = true;
openssl.run(sslconfig, function (data) {
  console.log(data);
});

That's all you need to know about using buffer streams.