arangojs icon indicating copy to clipboard operation
arangojs copied to clipboard

how can i use arangojs on web worker?

Open YankeeTube opened this issue 3 years ago • 5 comments

I started using arango while looking for a DB that supports http in cloudflare workers.

However, when used using arangojs, it operates on web worker, so the window cannot be accessed, resulting in the following error.

Uncaught ReferenceError: window is not defined
  at line 1 in t.btoa
  at line 1 in setBasicAuth
  at line 1 in t.Connection
  at line 1 in _
  at line 1
  at line 1 in r
  at line 1
  at line 1

YankeeTube avatar Mar 01 '22 17:03 YankeeTube

Unfortunately it seems this is still an issue, which I ran into quite while back already: https://github.com/arangodb/arangojs/issues/727#issuecomment-884197336

The only way to use ArangoDB from a web worker (Cloudflare workers) currently is to probably use the direct HTTP endpoints.

The thing is though, those runtime environment variables are available in Workers (https://developer.mozilla.org/en-US/docs/Web/API/btoa) - but currently this library is only assuming browser (with access to window) or Node.js (which it then patches a global window object, it seems).

lostpebble avatar Apr 05 '22 14:04 lostpebble

arangojs doesn't patch a global window object in Node, it imports a version of the btoa module that uses Buffer instead.

I'll see if I can use the btoa global directly in the browser version instead.

pluma avatar Apr 19 '22 10:04 pluma

Hi!

Has there been any progress on this? We're trying to use arangojs with SvelteKit using the Cloudflare Pages adapter, but it keeps tripping over 'path', 'querystring' and 'url'. We've tried to polyfill through the vite config, but it sadly still keeps failing on 'path'.

Manually changing the 'path' import to 'path-browserify' in joinPath.web.js seems to work, so we could make a fork and go with that, but I'm guessing that wouldn't be the proper way 😅

Any pointers would be greatly appreciated!

Neicul123 avatar Jun 22 '22 07:06 Neicul123

I don't have a Cloudflare environment to try this out but v8.0.0 will use path-browserify explicitly and avoids querystring in the browser version so this may solve these issues. I was also able to slightly improve the performance of the querystringify implementation tho the browser version still is multiple times slower than the node version (ca. 4-6x).

I'm not sure how XHR works in Cloudflare but we might eventually be able to move to fetch.

pluma4345 avatar Oct 24 '22 16:10 pluma4345

The main problem is using "XDomainRequest" and "XMLHttpRequest" in the npm package "xhr". You need to replace xkr() in with place https://github.com/arangodb/arangojs/blob/83353e80280eb6b7ddd9e7b3f59d353d28c00217/src/lib/request.web.ts#L78 with Fetch API and replace this line https://github.com/arangodb/arangojs/blob/83353e80280eb6b7ddd9e7b3f59d353d28c00217/src/lib/btoa.web.ts#L13 to return btoa(unescape(encodeURIComponent(str)));

caracal7 avatar Jan 27 '23 09:01 caracal7

The upcoming 9.0.0 release replaces http.request/xhr with native fetch and drops the dependency on node-specific modules. As I understand it, that should allow using it with other environments like cloudflare workers. Please comment/reopen if this is not the case.

You can try the pre-release version yourself by installing arangojs@next.

pluma4345 avatar Apr 17 '24 09:04 pluma4345