arangojs
arangojs copied to clipboard
how can i use arangojs on web worker?
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
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).
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.
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!
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.
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)));
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
.