<commit_before>import process from 'webpack-worker/process'
import Papa from 'papaparse'
import JSZip from 'jszip'
import _ from 'lodash'

process((params, emit) => {
  emit(0)
  let zip = new JSZip()
  zip.loadAsync(params.data)
    .then((zipContent) => {
      zip.file('GeoLite2-Country-CSV_20171003/GeoLite2-Country-Blocks-IPv4.csv')
        .async('string')
        .then(data => {
          emit(25)
          Papa.parse(data, {
            header: true,
            complete: ipv4 => {
              emit(50)
              zip.file('GeoLite2-Country-CSV_20171003/GeoLite2-Country-Locations-en.csv')
                .async('string')
                .then(data => {
                  emit(75)
                  Papa.parse(data, {
                    header: true,
                    complete: meta => {
                      emit(100)
                      let dict = {}
                      meta.data.forEach(geo => {
                        dict[geo.geoname_id] = geo.country_name
                      })
                      return {
                        ipv4: ipv4.data,
                        meta: _.sortBy(meta.data, 'country_name'),
                        dict: dict
                      }
                    }
                  })
                })
            }
          })
        })
    })
})<commit_msg>refactor the worker code and make it properly return results in a promise<commit_after>import process from 'webpack-worker/process'
import Papa from 'papaparse'
import JSZip from 'jszip'
import _ from 'lodash'

process((params, emit) => {
  const zip = new JSZip()
  const countryBlocksZip = 'GeoLite2-Country-CSV_20171003/GeoLite2-Country-Blocks-IPv4.csv'
  const countryLocationsZip = 'GeoLite2-Country-CSV_20171003/GeoLite2-Country-Locations-en.csv'

  const parseFile = (zipFile) => {
    return zip
        .file(zipFile)
        .async('string')
        .then(data => {
          return new Promise((resolve, reject) => {
            Papa.parse(data, {
              header: true,
              error: reject,
              complete: results => {
                resolve(results)
              }
            })
          })
        })
  }

  return zip
      .loadAsync(params.data)
      .then(zipContent => Promise.all([
        parseFile(countryBlocksZip),
        parseFile(countryLocationsZip)
      ]))
      .then(([ipv4, meta]) => {
        const dict = {}
        meta.data.forEach(geo => {
          dict[geo.geoname_id] = geo.country_name
        })

        return {
          ipv4: ipv4.data,
          meta: _.sortBy(meta.data, 'country_name'),
          dict: dict
        }
      })
})
{'fraction_non_alphanumeric': 0.1165783497350492, 'fraction_numerical': 0.02195306585919758, 'mean_word_length': 1.4181152790484903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '2346841', 'n_tokens_mistral': 818, 'n_tokens_neox': 760, 'n_words': 138}