Callidon / bloom-filters

JS implementation of probabilistic data structures: Bloom Filter (and its derived), HyperLogLog, Count-Min Sketch, Top-K and MinHash
https://callidon.github.io/bloom-filters/
MIT License
378 stars 43 forks source link

`Buffer is not defined` error when using in the browser. #56

Closed hipstermojo closed 2 years ago

hipstermojo commented 2 years ago

Hi I was trying to create a BloomFilter instance in the browser and it keeps throwing the error Buffer is not defined error even though I'm only importing the BloomFilter export which doesn't use the Buffer class.

Is there a workaround for this?

I'm using the library with the vite bundler on firefox 99, if that helps.

folkvir commented 2 years ago

Hi! Thank you for reporting. Is this the latest version?

hipstermojo commented 2 years ago

yes it's on version 3.0.0

folkvir commented 2 years ago

A browserify build succeeds for me for the 3.0.0.

browserify -r ./dist/api.js:bloom-filters > bundle.js with the following html file:

<script src="./bundle.js"></script>
<script type="text/javascript">
  const { BloomFilter } = require('bloom-filters')
  const a = BloomFilter.create(2, 0.01)
  a.add(1)
  console.log("1: ", a.has(1))
</script>
folkvir commented 2 years ago

Some polyfills are automatically added when using browserify such as the Buffer one. When importing the package you should import using the "bloom-filters/bloom/bloom-filter.js" syntax instead of const = {...} = require('bloom-filters') if you want only the BloomFilter class. Otherwise you'll get all the code, and your compiler/transpiler/bundler will surely stuck on the Buffer part used in other classes.

folkvir commented 2 years ago

I close the issue since it is not a bug, feel free to continue the discussion!