From cfb00193de02e354b5cb4f8065ed8061aac1c574 Mon Sep 17 00:00:00 2001
From: Suzanne Soy <jsmaniac.github@suzanne.soy>
Date: Thu, 16 Nov 2023 21:32:52 +0000
Subject: [PATCH] flake stuff, improved node script

---
 directory_hashes.js        | 2 +-
 find_vanity.js             | 6 ++++--
 flake.nix                  | 2 +-
 micro_ipfs.js              | 6 +++++-
 update_directory_hashes.sh | 3 +++
 5 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/directory_hashes.js b/directory_hashes.js
index 0625a31..31469a3 100644
--- a/directory_hashes.js
+++ b/directory_hashes.js
@@ -1 +1 @@
-var ipfs_directory_hashes={"vanity_text":"soy","vanity_number":3569,"tree":{"Links":[{"Name":".gitignore","Hash":"QmW9iMXzmPqLSnzL4p6DKsvsL3nC1xKS3teRB4SRdukfrz","Size":16},{"Name":".ipfsignore","Hash":"QmWMeSQQxuSYMddVwJGrnn12zrX1TTW7ueZbhZdaZ64Wr1","Size":14},{"Name":"directory_hashes.js","Hash":"","Size":0},{"Name":"find_vanity.js","Hash":"QmYcaVuBSCY3CbuXut23kQYpKHN6HuDG6F8mAHeRifdmXZ","Size":229},{"Name":"flake.lock","Hash":"QmV1UZtzknzjmyaJyvR7NHFCJ15FbFbS9bHBxTfyRMuNqg","Size":519},{"Name":"flake.nix","Hash":"QmdKZyymJ53hzDXJqxibdfZdre3eNBfp3XhMWLavuutbNE","Size":515},{"Name":"index.html","Hash":"QmZaXPFa47VxJNsNcaejw7R9GNPGCF5FjkSCDrvrmBAeP9","Size":553},{"Name":"lzw.js","Hash":"QmP2CsTDmyR9h5UrXJgXGswyMhiSHBZkQKLszdgh9Eb8Rk","Size":2034},{"Name":"micro_ipfs.js","Hash":"Qmefnv6QiZqcqAEh8G1gsVQi4hAoXxwkXjWxL3jxjx1Kk7","Size":15370},{"Name":"quine.js","Hash":"QmcHWTtgH53KkaSsWEvTxKDuuTNxipC3iJyebS3BBuF21B","Size":3099},{"Name":"result","Hash":"QmR44DbETfvZaBxxLSoW1eufyr5tY6uvXtDXYeRyAZzQBs","Size":73},{"Name":"robots.txt","Hash":"QmSqhdBa5ryzgjN41hL8rM98hjunXashEuLRW8PnKLH5yq","Size":49},{"Name":"sha256.js","Hash":"QmRhgx5Fq4JqfCgsPcMxNSYwt8M9WRBkec9omPWzJ7gdwL","Size":8553},{"Name":"sitemap.txt","Hash":"QmWsbCRCFoLa7AKdHmx8Mt6nV1jxpqEjdbAxZp1pH7tZQr","Size":548},{"Name":"update_directory_hashes.sh","Hash":"QmYmJcAQpxWb4TbLw2UwM92w6sVfrQMCKitgn8Qg62T8zU","Size":1473},{"Name":"update_quine.sh","Hash":"QmVZY9bqdiopA4BaxNRwJr5AWLbE8ikb19dAJoKCNENNez","Size":3029}],"Data":"\b\u0001"}}; if (typeof module != 'undefined') { module.exports = { ipfs_directory_hashes: ipfs_directory_hashes }; }
+jsonp_ipfs_directory_hashes({"vanity_text":"soy","vanity_number":3569,"tree":{"Links":[{"Name":".gitignore","Hash":"QmW9iMXzmPqLSnzL4p6DKsvsL3nC1xKS3teRB4SRdukfrz","Size":16},{"Name":".ipfsignore","Hash":"QmWMeSQQxuSYMddVwJGrnn12zrX1TTW7ueZbhZdaZ64Wr1","Size":14},{"Name":"directory_hashes.js","Hash":"","Size":0},{"Name":"find_vanity.js","Hash":"QmYcaVuBSCY3CbuXut23kQYpKHN6HuDG6F8mAHeRifdmXZ","Size":229},{"Name":"flake.lock","Hash":"QmV1UZtzknzjmyaJyvR7NHFCJ15FbFbS9bHBxTfyRMuNqg","Size":519},{"Name":"flake.nix","Hash":"QmdKZyymJ53hzDXJqxibdfZdre3eNBfp3XhMWLavuutbNE","Size":515},{"Name":"index.html","Hash":"QmZaXPFa47VxJNsNcaejw7R9GNPGCF5FjkSCDrvrmBAeP9","Size":553},{"Name":"lzw.js","Hash":"QmP2CsTDmyR9h5UrXJgXGswyMhiSHBZkQKLszdgh9Eb8Rk","Size":2034},{"Name":"micro_ipfs.js","Hash":"Qmefnv6QiZqcqAEh8G1gsVQi4hAoXxwkXjWxL3jxjx1Kk7","Size":15370},{"Name":"quine.js","Hash":"QmcHWTtgH53KkaSsWEvTxKDuuTNxipC3iJyebS3BBuF21B","Size":3099},{"Name":"result","Hash":"QmR44DbETfvZaBxxLSoW1eufyr5tY6uvXtDXYeRyAZzQBs","Size":73},{"Name":"robots.txt","Hash":"QmSqhdBa5ryzgjN41hL8rM98hjunXashEuLRW8PnKLH5yq","Size":49},{"Name":"sha256.js","Hash":"QmRhgx5Fq4JqfCgsPcMxNSYwt8M9WRBkec9omPWzJ7gdwL","Size":8553},{"Name":"sitemap.txt","Hash":"QmWsbCRCFoLa7AKdHmx8Mt6nV1jxpqEjdbAxZp1pH7tZQr","Size":548},{"Name":"update_directory_hashes.sh","Hash":"QmYmJcAQpxWb4TbLw2UwM92w6sVfrQMCKitgn8Qg62T8zU","Size":1473},{"Name":"update_quine.sh","Hash":"QmVZY9bqdiopA4BaxNRwJr5AWLbE8ikb19dAJoKCNENNez","Size":3029}],"Data":"\b\u0001"}});
diff --git a/find_vanity.js b/find_vanity.js
index 04bc106..12762b4 100644
--- a/find_vanity.js
+++ b/find_vanity.js
@@ -1,3 +1,5 @@
-const directory_hashes = require('./directory_hashes.js');
+const fs = require("fs");
 const micro_ipfs = require('./micro_ipfs.js');
-console.log(micro_ipfs.ipfs_self_hash.find_vanity_node(null, 'soy', 0, directory_hashes.ipfs_directory_hashes));
\ No newline at end of file
+const jsonp = fs.readFileSync(process.argv[2], 'utf-8');
+const directory_hashes = JSON.parse(jsonp.substring('jsonp_ipfs_directory_hashes('.length, jsonp.length - ');\n'.length));
+console.log(micro_ipfs.ipfs_self_hash.find_vanity_node(null, 'soy', 0, directory_hashes));
\ No newline at end of file
diff --git a/flake.nix b/flake.nix
index bfaa4d6..38884d6 100644
--- a/flake.nix
+++ b/flake.nix
@@ -13,7 +13,7 @@
           mkdir "$out/bin"
           mkdir -p "$out/nix-support"
           echo "$propagatedBuildInputs" > "$out/nix-support/propagated-build-inputs"
-          cp find_vanity.js directory_hashes.js micro_ipfs.js sha256.js "$out/bin/"
+          cp find_vanity.js micro_ipfs.js sha256.js "$out/bin/"
           cp update_directory_hashes.sh "$out/bin/update-directory-hashes"
         '';
       };
diff --git a/micro_ipfs.js b/micro_ipfs.js
index 60d0a35..7e78ea6 100644
--- a/micro_ipfs.js
+++ b/micro_ipfs.js
@@ -317,7 +317,7 @@ var ipfs_self_hash = (function() {
       ipfs_directory_hashes.vanity_number = vanity_attempt;
   
       // TODO: using JSON.stringify to recreate the file is more brittle, better store the stringified version as a hex string, and then decode it?
-      var file_directory_hashes = 'var ipfs_directory_hashes=' + JSON.stringify(ipfs_directory_hashes) + '; if (typeof module != \'undefined\') { module.exports = { ipfs_directory_hashes: ipfs_directory_hashes }; }\n';
+      var file_directory_hashes = 'jsonp_ipfs_directory_hashes(' + JSON.stringify(ipfs_directory_hashes) + ');\n';
       var foo = ipfs.hashWithLinks(16, {
         "Links": [],
         "isFile": true,
@@ -393,4 +393,8 @@ var ipfs_self_hash = (function() {
     return { get_link: get_link, find_vanity_browser: find_vanity_browser, find_vanity_node: find_vanity_node };
   })();
 
+  function jsonp_ipfs_directory_hashes(arg) {
+    ipfs_directory_hashes = arg;
+  }
+
   if (typeof module != 'undefined') { module.exports = { micro_ipfs : micro_ipfs, ipfs_self_hash : ipfs_self_hash }; }
\ No newline at end of file
diff --git a/update_directory_hashes.sh b/update_directory_hashes.sh
index 5b95a29..a319535 100755
--- a/update_directory_hashes.sh
+++ b/update_directory_hashes.sh
@@ -2,6 +2,9 @@
 
 set -euET -o pipefail
 
+node "$(dirname "$0")/find_vanity.js" "directory-hashes.js"
+exit
+
 vanity_text="${1:-xyz}"
 directory="${2:-.}"