h0x91b / redis-fast-driver

78 stars 13 forks source link

how to get buffer for binary #63

Closed xxaier closed 1 year ago

xxaier commented 1 year ago

as title

h0x91b commented 1 year ago

Hi, following code works for binaries:

const bin = await fs.readFile("./binary-example.png");
  console.log("binary file size: %s", bin.length);
  r.rawCall(["set", "binary", bin.toString("binary")], (err, resp) => {
    console.log("SET via rawCall command returns err: %s, resp: %s", err, resp);
  });
  r.rawCall(["get", "binary"], async (err, resp) => {
    console.log(
      "GET via rawCall command returns err: %s, resp: %s",
      err,
      resp.length
    );
    const b = Buffer.from(resp, "binary");
    console.log("binary file size: %s", b.length);
    await fs.writeFile("./binary-example-out.png", b);
    r.end();
  });

The trick is bin.toString("binary") and then Buffer.from(resp, "binary")

Full example: example-binary.js