Giter VIP home page Giter VIP logo

Comments (6)

kylefarris avatar kylefarris commented on May 24, 2024

Actually, I think there might be an issue with your code...

In the following section, you're doing a Promise.all() but you don't need to since the map you're running doesn't return any unresolved promises. In fact, it doesn't return anything (which is invalid).

const clamscan = await this.clamscan.init(this.config.clamscan);
await Promise.all(
  mail.attachments.map(async (attachment, i) => {
    try {
      const stream = isStream(attachment.content)
        ? attachment.content
        : intoStream(attachment.content);
      const {
        is_infected: isInfected,
        viruses
      } = await clamscan.scan_stream(stream);
      const name = isSANB(attachment.filename)
        ? `"${attachment.filename}"`
        : `#${i + 1}`;
      if (isInfected)
        messages.push(
          `Attachment ${name} was infected with "${viruses}".`
        );
    } catch (err) {
      this.config.logger.error(err);
    }
  })
);

Something along these lines might be better...

const clamscan = await this.clamscan.init(this.config.clamscan);
try {
  const results = await Promise.all(
    mail.attachments.map((attachment, i) => {
      const stream = isStream(attachment.content)
        ? attachment.content
        : intoStream(attachment.content);
      return clamscan.scan_stream(stream);
    })
  );

  results.forEach((result, i) => {
    const attachment = mail.attachments[i];
    const name = isSANB(attachment.filename)
      ? `"${attachment.filename}"`
      : `#${i + 1}`;
    if (result.isInfected)
      messages.push(
        `Attachment ${name} was infected with "${result.viruses}".`
      );
  });
} catch (err) {
    this.config.logger.error(err);
}

Let me know if that helps at all.

from clamscan.

kylefarris avatar kylefarris commented on May 24, 2024

Did this help at all? I'm inclined to close this ticket in a few days under the assumption that you've figured out what's going on.

from clamscan.

niftylettuce avatar niftylettuce commented on May 24, 2024

No, that did not help. You can use await Promise.all even if you don't return anything, that's totally valid.

from clamscan.

kylefarris avatar kylefarris commented on May 24, 2024

You’re misunderstanding what I’m saying. It’s not the Promise.all that’s the problem, it’s you’re map. Nothing is actually going into your Promise.all method because the map isn’t retuning anything.

The way you currently have it, you might as well just run a forEach loop and skip the Promise.all altogether.

from clamscan.

kylefarris avatar kylefarris commented on May 24, 2024

FWIW, I just wrote some tests to verify that the specific scenario you have is working and the tests pass. You can see the new tests here:

clamscan/tests/index.js

Lines 1262 to 1302 in 7ea77ae

it('should not fail when run within a Promise.all()', async () => {
clamscan = await reset_clam();
const [result1, result2] = await Promise.all([
clamscan.scan_stream(get_good_stream()),
clamscan.scan_stream(get_bad_stream()),
]);
expect(result1.is_infected).to.be.a('boolean');
expect(result1.is_infected).to.eql(false);
expect(result1.viruses).to.be.an('array');
expect(result1.viruses).to.have.length(0);
expect(result2.is_infected).to.be.a('boolean');
expect(result2.is_infected).to.eql(true);
expect(result2.viruses).to.be.an('array');
expect(result2.viruses).to.have.length(1);
});
it('should not fail when run within a weird Promise.all() (issue #59)', async () => {
clamscan = await reset_clam();
const items = [get_good_stream(), get_bad_stream()];
await Promise.all(
items.map(async (v,i) => {
const {is_infected, viruses} = await clamscan.scan_stream(v);
if (i === 0) {
expect(is_infected).to.be.a('boolean');
expect(is_infected).to.eql(false);
expect(viruses).to.be.an('array');
expect(viruses).to.have.length(0);
} else {
expect(is_infected).to.be.a('boolean');
expect(is_infected).to.eql(true);
expect(viruses).to.be.an('array');
expect(viruses).to.have.length(1);
}
})
);
});

There may be something else going on in your code but I'm not sure how to help beyond this. As far as I can test, the clamscan module is working fine. If you can provide a simplified repeatable test script showing that this module isn't working as expected, I'd be more than happy to address it (pull requests are also welcome as always).

from clamscan.

niftylettuce avatar niftylettuce commented on May 24, 2024

Thanks for following up here, will close, and follow up if I see it again.

from clamscan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.