ngageoint / geopackage-js

GeoPackage JavaScript Library
http://ngageoint.github.io/geopackage-js/
MIT License
308 stars 78 forks source link

Uncaught ReferenceError: Buffer is not defined #163

Closed jesperthomsen closed 4 years ago

jesperthomsen commented 4 years ago

I hope this is the right place to seek help (it's the first issue I have written, as far as I remember). When using GeoPackage js from a web worker, I get a Uncaught ReferenceError: Buffer is not defined in binarywriter.js, line 18.

It works well in my component, but when I try and run from my web worker, I get this error. I have tried adding Buffer to dependencies in package.json, but to no avail.

Sorry if this is the wrong forum.

danielbarela commented 4 years ago

@jesperthomsen This is the correct place. Do you have an example project that we could run to see your exact issue?

jesperthomsen commented 4 years ago

Thanks for your reply. I created a new project to show what I'm bumping into. I'm using angular 8 and had to mock about a bit to get it running, adding "skipLibCheck": true to tsconfig. Opening a geopackage from a component caused a Buffer not defined, but I could resolve that by adding

(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;

to polyfills.ts

Now, from the component, I can open the geopackage, but not from my web worker.

My example project is here: https://github.com/jesperthomsen/geopackagebuffer. I was running a somewhat old version of node and npm and I couldn't get npm install to work until I upgraded to the latest stable version.

I hope you can help. Thanks in advance.

danielbarela commented 4 years ago

Wow that is awesome. Thank you for the example project. We will get to work on this.

jesperthomsen commented 4 years ago

Wow that is awesome. Thank you for the example project. We will get to work on this.

Hello - any progress on this?

danielbarela commented 4 years ago

I'm sorry, I have been swamped lately. I should have some time to look at this late this week or early next week.

danielbarela commented 4 years ago

Working on this right now.

danielbarela commented 4 years ago

OK, so the problem is that in the web worker Buffer is not being set by the polyfill and when the wkx library tries to get set up, it cannot find the global variable Buffer. I am still looking into how to fix this, just wanted to give you an update.

jesperthomsen commented 4 years ago

Thanks for keeping me posted. It's pretty much what I figured. From web workers, there's no access to window or global. I appreciate you trying to fix this.

danielbarela commented 4 years ago

@jesperthomsen I think I got it I now build a version of the library which includes the Buffer object. You will need to modify the import statement used for your web worker to use that version though. So I published 3.0.4 to npm. Here is what I had to do in your worker file to make it work:

/// <reference lib="webworker" />

import * as geoPackage from '@ngageoint/geopackage/dist/geopackage-browser'; //<----- this is the important change

addEventListener('message', ({ data }) => {

  geoPackage.GeoPackageAPI.open(data).then(database => { // Throws Uncaught ReferenceError: Buffer is not defined
    postMessage('worker done');
  });
});
jesperthomsen commented 4 years ago

I'm thrilled about this. I have a working prototype using your library and some open layers that is just smooth now.

Thank you so much for your help. I really appreciate it and you have been absolutely great!