Created
April 25, 2024 05:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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