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

added optional window to avoid requiring 'jsdom' everytime #43

Closed
wants to merge 3 commits into from

Conversation

akhoury
Copy link

@akhoury akhoury commented Jan 6, 2014

I added an optional options.window so I can now require('jsdom') and createWindow() before hand

kinda like that.

var posts = [ .... ]; // a large array of html content posts
var md = require('html-md');
var doc = require('jsdom').jsdom(null, null, {
          features: {
            FetchExternalResources: false
          },
          url: window ? window.document.baseURI : "file://" + (process.cwd()) 
        });
var win = doc.createWindow();

for (var i = 0; i < posts.length; i++) {
   posts[i].content = md(posts[i].content), {window: win});
}

I built and successfully tested locally, but didn't commit build files per your contribution guide
Thanks, great module btw.

closes issue#42

# Create a DOM if `window` doesn't exist (i.e. when running in node).
@win = window ? null
# Use the options.window if one is passed, issue #42
# If not, create a DOM if `window` doesn't exist (i.e. when running in node).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rephrase this to not include the issue reference? For example:

# Use the `window` option when specified or create a DOM if `window` doesn't exist (i.e. when running in node).

@neocotic
Copy link
Owner

neocotic commented Jan 6, 2014

Although this does seem a lot like supporting a workaround instead of implementing a fix, I'm not sure how much this is a problem. I doubt the majority of use-cases will encounter the memory leak mentioned in #42 but I'm happy to accommodate you advanced users 😉

I would have preferred to only call jsdom(markup, level, options) once during the first call but, unfortunately, I see no way of changing the url of an existing instance, so a new one is created each time. Setting this URL is required in order to support resolving relative URL's in the markup.

If you could make the changes I've suggested, I'll merge it in and update the README and maybe get a minor release out.

@akhoury
Copy link
Author

akhoury commented Jan 7, 2014

you were right, the bottleneck still in jsdom(markup, level, options) so I couldn't do more than 'requiring' jsdom once. Also updated the comment and another statement per your notes.

Thanks

otherwise coffee compiler will generate the var within the constructor's scope, so it would still be required every time.
@akhoury
Copy link
Author

akhoury commented Jan 25, 2014

@neocotic any plans to merge that in?
just a friendly reminder, thank you - let me know if you need more changes.

@neocotic
Copy link
Owner

Yes, sorry. Thanks for the reminder 😄

@akhoury
Copy link
Author

akhoury commented Feb 15, 2014

don't mean to nag, ✌️ just another friendly reminder.

@neocotic
Copy link
Owner

@akhoury Don't worry. I've not forgotten. Honestly, I'm currently looking at alternatives to jsdom since it's possibly too much overhead (especially for Windows users). Please stay with me and, if the investigation takes too long, I'll simply merge this in for the time being, at least.

Thanks for your patience.

@neocotic
Copy link
Owner

I haven't forgotten 😄 Will probably merge this in this weekend (have set a reminder).

@toddself
Copy link

toddself commented Mar 7, 2014

@neocotic getting this into html-md would be awesome -- I'm trying to use html-md in a loop during a data migration here at work, and am running into this leak.

@akhoury
Copy link
Author

akhoury commented Mar 7, 2014

@toddself - I have full confidence in @neocotic to address this corner-case issue one way or another, but till then, I was able to get away using this temporary close-enough-fork: (see why it's not a regular fork)

package.json:

"dependencies": {
        "html-md-optional_window": "git://github.com/akhoury/html-md-optional_window.git#master"
    }

then follow the example in the initial post of this thread for an example on how to use it.

IMPORTANT:

as I state in the readme, I WILL delete the repo once this issue is resolved.

@toddself
Copy link

toddself commented Mar 7, 2014

@akhoury I saw that :) and resolved with an npm i akhoury/html.md#ff7bee1d11291c17b698b53f8b922d8108e45cf7

However, still getting a Heap::ReserveSpace Allocation failure. :(. I'm going to profile this and see if I can't find some other heap issues.

@akhoury
Copy link
Author

akhoury commented Mar 7, 2014

can you provide an example for me to replicate the heap issue? I'd love to take a look.

@toddself
Copy link

toddself commented Mar 7, 2014

Working on reducing down my example to something manageable :)

@neocotic
Copy link
Owner

I was looking at doing this this week (honestly!)

@akhoury, can you please coordinate with @toddself to see if there's any other changes you need to make your PR "fully featured". I'm hesitant to merge this in if it doesn't fix the core problem with this edge case.

@akhoury
Copy link
Author

akhoury commented Mar 10, 2014

^ fair enough - still waiting on @toddself's reduced example to reproduce the heap issue.

@neocotic
Copy link
Owner

@toddself, any update on the problems you were experiencing?

@neocotic
Copy link
Owner

@akhoury, @toddself, I was wondering if there is any update on this as I'm keen to get this edge case fixed and behind us?

@akhoury
Copy link
Author

akhoury commented Feb 12, 2015

@neocotic I was not able to reproduce the heap issue that @toddself mentioned.
It's your call.

@neocotic
Copy link
Owner

neocotic commented Jun 9, 2017

Sorry for taking so so very long to address this. I finally got around to doing the massive rewrite of this library that I wanted to. It's been renamed Europa and it's totally changed.

One key change that fixes this issue is that I've split up the project into several modules. europa is now only for browsers (and browser bundlers - e.g. browserify, webpack) and node-europa is now available for Node.js. This allows all Node.js-specific dependencies to be completely separated from that for the browser version. node-europa also contains the node-powered CLI.

I also bumped the dependency for jsdom to v11. While this means that node-europa requires Node.js version 6 or newer to be compatible with the same requirement on jsdom, it means no more contextify (yay! 🎆) This should be better for Windows users as well, who often had issues during the installation due to the somewhat complex external dependencies for contextify.

Most of all, if you want to install for browser or Node.js, now all you need is npm:

# For browser:
$ npm install --save europa
# For Node.js:
$ npm install --save node-europa

Both install quickly and have no confusing dependencies or external dependencies.

I'm closing this PR now as it's no longer valid. If you still use html-md, please give node-europa a shot and let me know how you get on.

Sorry again for the massive delay.

@neocotic neocotic closed this Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

require('jsdom'), if called within a large loop, is causing memory issues
3 participants