<commit_before>import { PassThrough } from 'stream';
import multihash from 'multihashes';
import bs58 from 'bs58';
import invertHash from 'invert-hash';
import objectPath from 'object-path';
import readFromPDF from './readFromPDF';
import hashPDF from './hashPDF';
import { HASH_MAP } from './hashFile';

const INVERTED_HASH_MAP = invertHash(HASH_MAP);

export default function verifyPDF(input, root = 'body.content.hash', toJson) {
  let hash;
  let algorithm;
  let input1;
  let input2;

  if (typeof input === 'string') {
    input1 = input;
    input2 = input;
  } else {
    input1 = new PassThrough();
    input2 = new PassThrough();

    input.pipe(input1);
    input.pipe(input2);
  }

  return readFromPDF(input1)
    .then(json => {
      if (!json) {
        if (toJson) {
          return {verified: false, wants: hash, has: null};
        }

        throw new Error('Failed');
      }

      hash = objectPath.get(json, root);
      const buf = new Buffer(bs58.decode(hash));
      const algorithmName = multihash.decode(buf).name;
      algorithm = INVERTED_HASH_MAP[algorithmName];

      if (typeof algorithm === 'undefined') {
        throw new Error('Unsupported algorithm: ' + algorithmName);
      }

      return hashPDF(input2, algorithm, null);
    })
    .then(h => {
      if (hash === h) {
        if (toJson) {
          return {verified: true, wants: hash, has: h};
        }

        return true;
      }

      if (toJson) {
        return {verified: false, wants: hash, has: h};
      }

      throw new Error('Failed');
    });
}
<commit_msg>Bug fix<commit_after>import { PassThrough } from 'stream';
import multihash from 'multihashes';
import bs58 from 'bs58';
import invertHash from 'invert-hash';
import objectPath from 'object-path';
import readFromPDF from './readFromPDF';
import hashPDF from './hashPDF';
import { HASH_MAP } from './hashFile';

const INVERTED_HASH_MAP = invertHash(HASH_MAP);

export default function verifyPDF(input, root = 'body.content.hash', toJson) {
  let hash;
  let algorithm;
  let input1;
  let input2;

  if (typeof input === 'string') {
    input1 = input;
    input2 = input;
  } else {
    input1 = new PassThrough();
    input2 = new PassThrough();

    input.pipe(input1);
    input.pipe(input2);
  }

  return readFromPDF(input1)
    .then(json => {
      if (!json) {
        if (toJson) {
          return {verified: false, wants: null, has: null};
        }

        throw new Error('Failed');
      }

      hash = objectPath.get(json, root);
      const buf = new Buffer(bs58.decode(hash));
      const algorithmName = multihash.decode(buf).name;
      algorithm = INVERTED_HASH_MAP[algorithmName];

      if (typeof algorithm === 'undefined') {
        throw new Error('Unsupported algorithm: ' + algorithmName);
      }

      return hashPDF(input2, algorithm, null);
    })
    .then(h => {
      if (hash === h) {
        if (toJson) {
          return {verified: true, wants: hash, has: h};
        }

        return true;
      }

      if (toJson) {
        return {verified: false, wants: hash, has: h};
      }

      throw new Error('Failed');
    });
}

{'fraction_non_alphanumeric': 0.13295165394402036, 'fraction_numerical': 0.010178117048346057, 'mean_word_length': 2.6232718894009217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3357792', 'n_tokens_mistral': 1019, 'n_tokens_neox': 977, 'n_words': 273}