mz3
mz3

Reputation: 1344

NodeJS Memory Leak

This route is causing my server to slowly run out of memory and I can't figure out why. The ridiculous exec() expression and parsing is necessary because of the target URL's ancient SSL version and I can't get any node SSL library to trust or ignore. Any additional suggestions on improving this code are welcome.

(function() {
  var exec, express, fs, https, jsdom, qs, router, util;

  express = require("express");
  jsdom = require('jsdom');
  exec = require('exec');
  https = require('https');
  qs = require('querystring');
  fs = require('fs');
  util = require('util');

  router = express.Router();

  router.get("/getVars", function(req, res) {
    res.set({
      "Cache-Control": 'max-age=86400'
    });
    return exec("curl -m 6 -1 'https://some.url.com'", function(err, b, stderr) {
      if (b) {
        return jsdom.env(b, ["http://code.jquery.com/jquery.js"], function(err, window) {
          var eventvalidation, viewstate;
          viewstate = window.$("#__VIEWSTATE").val();
          eventvalidation = window.$("#__EVENTVALIDATION").val();
          return res.json({
            viewstate: viewstate,
            eventvalidation: eventvalidation
          });
        });
      } else {
        return res.json({
          viewstate: "verylongstring",
          eventvalidation: "verylongstring"
        });
      }
    });
  });

...

Upvotes: 0

Views: 354

Answers (1)

Tracker1
Tracker1

Reputation: 19334

I'd suggest eliminating jsdom as it's a very heavy component for your limited needs, which would likely be better served with a couple of regular expressions, see below.

  ...
  return res.json(getDataFromString(b || ''));
  ...

function getDataFromString(input){
  return {
    viewstate: (/id=\"__VIEWSTATE\" value=\"([^\"]*)\"/).exec(input)[1] || "verylongstring",
    eventvalidation: (/id=\"__EVENTVALIDATION\" value=\"([^\"]*)\"/).exec(input)[1] || "verylongstring"
  }
}

Upvotes: 1

Related Questions