clamscan icon indicating copy to clipboard operation
clamscan copied to clipboard

Should not remove props from options passed to init

Open kroleg opened this issue 7 months ago • 0 comments

My use case is like this: I need to re-init on scanning in case previously init failed. I am forced to use init like new NodeClam().init({ ...this.settings }) to preserve my settings props. Also i think it's generally not recommended to modify args passed to a function. There is even a eslint rule for this: https://eslint.org/docs/latest/rules/no-param-reassign

export class FileScan {
  settings: NodeClam.Options;
  clam: Promise<NodeClam>;

  constructor() {
    const { host, port, debugMode } = config.clamscan;
    this.settings = {
      clamdscan: { host, port },
      preference: 'clamdscan',
    };

    // current version of NodeClam removes the clamdscan property from the options object, pass obj clone to avoid it
    this.clam = new NodeClam().init({ ...this.settings });
    // ...
  }


  async isFileClean(filePath: string) {
    let clamscan: NodeClam;
    // if clamscan failed to initialize, awaiting this.clamscan will throw an error
    // so we need to catch the error and try to reinitialize
    try {
      clamscan = await this.clam;
    } catch (err) {
      // current version of NodeClam removes the clamdscan property from the options object, pass obj clone to avoid it
      this.clam = new NodeClam().init({ ...this.settings });
      clamscan = await this.clam;
    }
    const { isInfected } = await clamscan.isInfected(filePath);
  }

kroleg avatar Apr 04 '25 14:04 kroleg