Skip to content

Instantly share code, notes, and snippets.

@KyGuy2002
Created April 25, 2024 05:32
Show Gist options
  • Save KyGuy2002/8ff7313f2c5bf00d7375dec1ebbef807 to your computer and use it in GitHub Desktop.
Save KyGuy2002/8ff7313f2c5bf00d7375dec1ebbef807 to your computer and use it in GitHub Desktop.
Converts the Cloudflare D1 binding to the hash used as the sqlite filename by Miniflare
import crypto from "crypto";
function convert(binding) {
const key = crypto.createHash("sha256").update("miniflare-D1DatabaseObject").digest();
const nameHmac = crypto
.createHmac("sha256", key)
.update(binding)
.digest()
.subarray(0, 16);
const hmac = crypto
.createHmac("sha256", key)
.update(nameHmac)
.digest()
.subarray(0, 16);
return Buffer.concat([nameHmac, hmac]).toString("hex");
}
console.log(convert("DB"))
// OUTPUT: e7352547963de7050bd7d94658afc4fe78b61811b7815da12d90be8e863abf4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment