Lists icon indicating copy to clipboard operation
Lists copied to clipboard

Node scripts use async methods

Open chhatch opened this issue 3 years ago • 2 comments

I just happened to be passing through and noticed that all your node scripts are using asynchronous methods for file system operations when there is no need to. It obviously works, but it makes the scripts much more complicated than they need to be. Here's an example:

// generate-adguard.js with async methods
const fs = require("fs").promises;
const path = require("path");

(async () => {
  const files = (await fs.readdir(path.join(__dirname, ".."))).filter((file) =>
    file.endsWith(".txt")
  ); // Array of strings, each representing a single file that ends in `.txt`
  await Promise.all(
    files.map(async (file) => {
      // For each file
      const fileContents = await fs.readFile(
        path.join(__dirname, "..", file),
        "utf8"
      ); // Get file contents as a string
      const adGuardFileContents = fileContents
        .replace(/^# Title: (.*?)$/gmu, "# Title: $1 (adguard)") // Add (adguard) to end of title
        .replaceAll(/^# 0\.0\.0\.0 (.*?) (.*)/gmu, "@@||$1^! $2")
        .replaceAll(/0\.0\.0\.0 (.*?)$/gmu, "||$1^")
        .replaceAll(/^#/gmu, "!");
      await fs.writeFile(
        path.join(__dirname, "..", "adguard", file.replace(".txt", "-ags.txt")),
        adGuardFileContents,
        "utf8"
      ); // Write new file to `adguard` directory
    })
  );
})();

simplifies to,

// generate-adguard.js with sync method
const fs = require("fs");
const path = require("path");

const files = fs
  .readdirSync(path.join(__dirname, ".."))
  .filter((file) => file.endsWith(".txt")); // Array of strings, each representing a single file that ends in `.txt`
files.map((file) => {
  // For each file
  const fileContents = fs.readFileSync(
    path.join(__dirname, "..", file),
    "utf8"
  ); // Get file contents as a string
  const adGuardFileContents = fileContents
    .replace(/^# Title: (.*?)$/gmu, "# Title: $1 (adguard)") // Add (adguard) to end of title
    .replaceAll(/^# 0\.0\.0\.0 (.*?) (.*)/gmu, "@@||$1^! $2")
    .replaceAll(/0\.0\.0\.0 (.*?)$/gmu, "||$1^")
    .replaceAll(/^#/gmu, "!");
  fs.writeFileSync(
    path.join(__dirname, "..", "adguard", file.replace(".txt", "-ags.txt")),
    adGuardFileContents,
    "utf8"
  ); // Write new file to `adguard` directory
});

If you're interested, I've already cloned the repo and made the changes. I haven't opened a pull request because I cannot create a branch. Keep up the good work!

chhatch avatar Mar 18 '23 12:03 chhatch

Triage Results for #953

Domain: Node scripts use async methods

DNS: Failed - [Errno -2] Name or service not known HTTP: 0 Found in lists: None

This issue has been automatically triaged by Hermes Agent.

gap579137 avatar Jun 29 '26 01:06 gap579137

This issue is being closed as part of a repository cleanup and issue triage refresh.

We are starting with a clean slate to better manage the blocklist project going forward. If this issue is still relevant, please feel free to open a new issue with:

  • Updated information
  • Current evidence/examples
  • Clear description of the request

Thank you for your contribution to the Block List Project!

AxiomOperator avatar Jul 06 '26 02:07 AxiomOperator