Ejemplo n.º 1
0
parse.string = function (input, options) {
    return parse(input, _.extend({
        inputType: 'markup'
    }, options));
};
Ejemplo n.º 2
0
parse.file = function (input, options) {
    return parse(input, _.extend({
        inputType: 'filePath'
    }, options));
};
Ejemplo n.º 3
0
/**
 * parse(input [, transformFn] [, options])
 *
 * x It's important to note that the element object
 * x passed to transformation functions is the root
 * x of the tree and does not undergo any transformations.
 * 
 * @param input (filePath<String>|markup<String>|domElement<cheerio()>)
 * @param A quick way to pass in options.fn. Takes precedence.
 *      Added this as a param because it's useful for
 *      writing quick tests.
 * @param options.modules ({moduleName: moduleFn, ...})
 */
function parse(input, transformFn, options) {
    var session, root, markup, $, templateModules, string;

    if (!_.isFunction(transformFn)) {
        options = transformFn;
        transformFn = null;
    }

    options = _.merge({
        // How to parse the input
        // in case it's a string
        // markup | file | null (auto)
        inputType: null,
        source: null,
        // Optional custom modules
        // Other than the global ones
        // {name -> fn}
        modules: null,
        // Search the source path and
        // its ancestors for
        // a leaf-modules.js file
        loadModulesConfig: true,
        // Optional function to
        // mutate the session (function (session) {})
        // Gets called AFTER all the modules
        // are loaded.
        transform: transformFn,
        cache: null,
        cheerioOptions: {
            // NOTE:
            // Cheerio started using htmlparser2.encodeXml in v 0.16.0 (now in v0.15.0)
            // which doesn't recognize &nbsp; when xmlMode is true. The solution would be
            // to not use xmlMode when parsing, but then other parts would stop working because
            // of self-closing tags and things like that. For now I'm just staying in this version
            // of cheerio.
            xmlMode: true
        },
        // xml | html (same as .stringify())
        outputFormat: 'xml'
    }, options);

    options.cache = options.cache || new Cache();
    options.modules = options.modules || {};

    if (_.isString(input)) {

        if (!options.inputType) {
            // Make an educated guess
            if (!input || utils.isHtmlString(input)) {
                options.inputType = 'markup';
            } else {
                options.inputType = 'filePath';
            }
        }

        switch (options.inputType) {
            case 'markup':
                markup = input;
                break;
            case 'filePath':
                markup = utils.loadFile(input, options.cache);
                break;
            default:
                throw new TypeError('Invalid inputType ' + options.inputType);
        }

        $ = cheerio.load(markup, options.cheerioOptions);
        root = $.root().contents();
        if (root.length < 1) throw new errors.DOMParserError('Input couldn\'t be parsed for an unknown reason');

        root.source(options.source || input);
    } else if (input instanceof cheerio) {
        $ = cheerio.load('', options.cheerioOptions);
        root = input;
        if (_.isString(options.source)) root.source(options.source);
    } else {
        throw new TypeError('input must be a string or a $(dom_element)');
    }

    // Look for <!-- modules: x, y, z -->
    templateModules = getTemplateModules(root);

    // Filter out all the junk
    root = root.filterEmptyTextAndComments();

    // Make sure only one root node is left
    if (root.length <= 0) throw new TypeError('input must not be an empty dom');
    if (root.length > 1) {
        throw new errors.LeafParseError('Input must have a single root node. Has (' + getTagNames(root) + ')');
    }

    // Get the modules from an external
    // file somewhere up the fille hierarchy from
    // the source
    if (options.loadModulesConfig) {
        options.modules = _.extend(
            getExtModules(root.source(), options.cache),
            options.modules
        );
    }

    session = new ParseSession(options.modules);
    session.options = options;
    session.cache = options.cache;
    session.$ = $;

    // Load all the modules
    _.each(templateModules, function (moduleName) {
        session.loadModule(moduleName);
    });

    // Execute the optional callback
    if (options.transform) options.transform(session);

    root = utils.compose(session.transforms.pre)(root);

    // The meat
    root = transformElement(root, session);

    root = utils.compose(session.transforms.post)(root);

    // Make a string
    string = root.stringify(options.outputFormat);

    string = utils.compose(session.transforms.string)(string);

    return string;
}
Ejemplo n.º 4
0
module.exports = function toParseHTTPBody(options) {
  options = options || {};

  // Configure body parser components

  // For URLEncoded, default the "extended" option to true for backwards compatibility,
  // and to avoid a deprecation warning (see https://github.com/expressjs/body-parser#options-3)
  // Also default request limit for JSON and URL-encoded parsers to 1mb for backwards compatibility.
  var URLEncodedBodyParser = bodyParser.urlencoded(_.extend({extended: true, limit: '1mb'}, options));
  var JSONBodyParser = bodyParser.json(_.extend({limit: '1mb'}, options));
  var MultipartBodyParser = toParseMultipartHTTPRequest(options);


  /**
   * Connet/Express/Sails-compatible middleware.
   *
   * @param  {Request}   req  [description]
   * @param  {Response}   res  [description]
   * @param  {Function} next [description]
   */

  return function _parseHTTPBody(req, res, next) {

    // Optimization: skip bodyParser for GET, OPTIONS, or body-less requests.
    if (req.method.toLowerCase() === 'get' || req.method.toLowerCase() === 'options' || req.method.toLowerCase() === 'head') {

      // But stub out a `req.file()` method with a usage error:
      req.file = function() {
        throw new Error('`req.file()` cannot be used with an HTTP GET, OPTIONS, or HEAD request.');
      };

      return next();
    }

    // TODO: Optimization: skip bodyParser for other HTTP requests w/o a body.

    // TODO: Optimization: only run bodyParser if this is a known route

    // log.verbose('Running request ('+req.method+' ' + req.url + ') through bodyParser...');

    // Mock up a req.file handler that returns a noop upstream, so that user code
    // can use `req.file` without having to check for it first.  This is useful in cases
    // where there may or may not be file params coming in.  The Multipart parser will
    // replace this with an actual upstream-acquiring function if the request isn't successfully
    // handled by one of the other parsers first.
    req.file = function(fieldName) {
      var noopUpstream = new Upstream({
        noop: true
      });
      noopUpstream.fieldName = 'NOOP_'+fieldName;
      return noopUpstream;
    };

    // Try to parse a request that has application/json content type
    JSONBodyParser(req, res, function(err) {
      if (err) return next(err);
      // If parsing was successful, exit
      if (!_.isEqual(req.body, {})) {return next();}
      // Otherwise try the URL-encoded parser (application/x-www-form-urlencoded type)
      URLEncodedBodyParser(req, res, function(err) {
        if (err) return next(err);
        // If parsing was successful, exit
        if (!_.isEqual(req.body, {})) {return next();}
        // Otherwise try the multipart parser
        MultipartBodyParser(req, res, function(err) {
          if (err) return next(err);

          /**
           * OK, here's how the re-run of the JSON bodyparser works:
           * ========================================================
           * If the original pass of the bodyParser failed to parse anything, rerun it,
           * but with an artificial `application/json` content-type header,
           * forcing it to try and parse the request body as JSON.  This is just in case
           * the user sent a JSON request body, but forgot to set the appropriate header
           * (which is pretty much every time, I think.)
           */

          // If we were able to parse something at this point (req.body isn't empty)
          // or files are/were being uploaded/ignored (req._fileparser.upstreams isn't empty)
          // or the content-type is JSON,
          var reqBodyNotEmpty = !_.isEqual(req.body, {});
          var hasUpstreams = req._fileparser && req._fileparser.upstreams.length;
          var contentTypeIsJSON = (backupContentType === 'application/json');
          // ...then the original parsing must have worked.
          // In that case, we'll skip the JSON retry stuff.
          if (contentTypeIsJSON || reqBodyNotEmpty || hasUpstreams) {
            return next();
          }

          // TODO: consider adding some basic support for this instead of relying on a peer dependency.
          // Chances are, if `req.is()` doesn't exist, we really really don't want to rerun the JSON bodyparser.
          if (!req.is) return next();

          // If the content type is explicitly set to "multipart/form-data",
          // we should not try to rerun the JSON bodyparser- it may hang forever.
          if (req.is('multipart/form-data')) return next();

          // Otherwise, set an explicit JSON content-type
          // and try parsing the request body again.
          var backupContentType = req.headers['content-type'];
          req.headers['content-type'] = 'application/json';
          JSONBodyParser(req, res, function(err) {

            // Revert content-type to what it originally was.
            // This is so we don't inadvertently corrupt `req.headers`--
            // our apps' actions might be looking for 'em.
            req.headers['content-type'] = backupContentType;

            // If an error occurred in the retry, it's not actually an error
            // (we can't assume EVERY requeset was intended to be JSON)
            if (err) {
              // log.verbose('Attempted to retry bodyParse as JSON.  But no luck.', err);
            }

            // Proceed, whether or not the body was parsed.
            next();
          });
        });
      });
    });
  };
};
Ejemplo n.º 5
0
 .then(function (response) {
   response = collection.parse(response, options);
   options.index = options.index || 'id';
   _.extend(options, { remote: false });
   return collection.save(response, options);
 });
Ejemplo n.º 6
0
function mergeOptionsWithDefaults(options) {
  return _.extend({}, constants.DEFAULTS, options);
}
Ejemplo n.º 7
0
var fs        = require('fs')
    , path      = require('path')
    , Sequelize = require('sequelize')
    , lodash    = require('lodash')
    , dbfile = "./public/pariseats.db"
    , sequelize = new Sequelize('database_name', 'username', 'password', {
        dialect: "sqlite", // or 'sqlite', mysql', 'mariadb'
        storage:    dbfile
    })
    , db = {}

fs
    .readdirSync(__dirname)
    .filter(function(file) {
        return (file.indexOf('.') !== 0) && (file !== 'index.js')
    })
    .forEach(function(file) {
        var model = sequelize.import(path.join(__dirname, file))
        db[model.name] = model
    })

Object.keys(db).forEach(function(modelName) {
    if ('associate' in db[modelName]) {
        db[modelName].associate(db)
    }
})

module.exports = lodash.extend({
    sequelize: sequelize,
    Sequelize: Sequelize
}, db);