Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeJS compatibility #13

Open
SirkoS opened this issue Jun 9, 2020 · 1 comment
Open

NodeJS compatibility #13

SirkoS opened this issue Jun 9, 2020 · 1 comment

Comments

@SirkoS
Copy link

SirkoS commented Jun 9, 2020

I just tried to use this library to harvest the contents from an OpenSearch endpoint. However, it seems that the library is only intended for the browser. Using it within NodeJS I currently get this error (using pnpm instead of npm):

Error: Could not parse XML document.
    at parseXml (...\node_modules\.pnpm\[email protected]\node_modules\opensearch-browser\dist\utils.js:54:9)
    at Function.fromXml (...\node_modules\.pnpm\[email protected]\node_modules\opensearch-browser\dist\description.js:165:40)
    at Function.fromXml (...\node_modules\.pnpm\[email protected]\node_modules\opensearch-browser\dist\service.js:289:71)
    at ...\node_modules\.pnpm\[email protected]\node_modules\opensearch-browser\dist\service.js:276:34
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Looking inside util.js parseXML(), I see a hard dependency on DOMParser or ActiveXObject. Both of which are not available within NodeJS.

The configuration also seems to lack an option to pass a custom XML-parser, so for now one can not use the library using NodeJS. Maybe there are other dependencies like that, which I did not yet discover.

For NodeJS compatibility, I see two options:

  1. Include an isomorphic DOMParser like you already did with isomorphic-fetch (I'm not aware of any particular library for DOMParser, though)
  2. Allow to configure the XML-parser to be used. That way one could use, e.g., xmldom to take care of the parsing.
@PascalNoisette
Copy link

Hi ! I came across the same problem. I choose to install npm install xmldom and push it to the global object at runtime.

            import { DOMParser } from 'xmldom'; 
            import { discover } from 'opensearch-browser';
            global.DOMParser = DOMParser;
            discover('http://example.com/search').then((service) => { ...

It did the trick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants