<commit_before>// HTTP Server
// -----------
// SocketStream does not concern itself with web servers. It simply provides a stack of Connect Middleware
// which can be used by the application in any way it wishes.

var app, connect, eventMiddleware, existslib, fileUtils, fs, loadStaticDirs, pathlib, router, settings, staticDirs, staticFiles, transformURL, useAfterStack;

fs = require('fs');

pathlib = require('path');

existslib = process.version.split('.')[1] === '6' && require('path') || require('fs');

connect = require('connect');

fileUtils = require('../utils/file');

router = new (require('./router').Router);

staticDirs = [];

staticFiles = [];

// User-configurable settings with sensible defaults
settings = {
  "static": {
    maxAge: 30 * 24 * 60 * 60 * 1000 // cache static assets in the browser for 30 days
  },
  secure: false // allow setting of the 'secure' cookie attribute when using SSL - see https://github.com/socketstream/socketstream/issues/349
};

// Create new Connect app instance which can be accessed from your app.js file with ss.http.middleware
app = connect();

// Alias app.use to indicate this will be added to the stack BEFORE SocketStream middleware
app.prepend = app.use;

// Allow Connect middleware to be added AFTER SocketStream middleware has been added to the stack
useAfterStack = [];

app.append = function() {
  var args;
  args = Array.prototype.slice.call(arguments);
  return useAfterStack.push(args);
};

module.exports = function(root) {
  return {
    // Return API
    connect: connect,
    middleware: app,
    router: router,

    // Merge optional settings    
    set: function(newSettings) {
      var k, v, _results;
      if (typeof newSettings !== 'object') {
        throw new Error('ss.http.set() takes an object e.g. {static: {maxAge: 60000}}');
      }
      _results = [];
      for (k in newSettings) {
        v = newSettings[k];
        _results.push(settings[k] = v);
      }
      return _results;
    },
    load: function(staticPath, sessionStore, sessionOptions) {
      staticPath = pathlib.join(root, staticPath);
      loadStaticDirs(staticPath);

      /* connect.compress() should be added to middleware stack on highest possible position */
      app.use(connect.compress());

      // Append SocketStream middleware upon server load      
      app.use(connect.cookieParser('SocketStream')).use(connect.favicon(staticPath + '/favicon.ico')).use(connect.session({
        cookie: {
          path: '/',
          httpOnly: false,
          maxAge: sessionOptions.maxAge,
          secure: settings.secure
        },
        store: sessionStore
      }));

      // Append any custom-defined middleware (e.g. everyauth)      
      useAfterStack.forEach(function(m) {
        return app.use.apply(app, m);
      });

      // Finally ensure static asset serving is last      
      app.use(eventMiddleware).use(connect["static"](staticPath, settings["static"]));
      return app;<commit_msg>Added support for static file caching using connect's staticCache middleware<commit_after>// HTTP Server
// -----------
// SocketStream does not concern itself with web servers. It simply provides a stack of Connect Middleware
// which can be used by the application in any way it wishes.

var app, connect, eventMiddleware, existslib, fileUtils, fs, loadStaticDirs, pathlib, router, settings, staticDirs, staticFiles, transformURL, useAfterStack;

fs = require('fs');

pathlib = require('path');

existslib = process.version.split('.')[1] === '6' && require('path') || require('fs');

connect = require('connect');

fileUtils = require('../utils/file');

router = new (require('./router').Router);

staticDirs = [];

staticFiles = [];

// User-configurable settings with sensible defaults
settings = {
  "static": {
    maxAge: 30 * 24 * 60 * 60 * 1000 // cache static assets in the browser for 30 days
  },
  "staticCache": {},
  secure: false // allow setting of the 'secure' cookie attribute when using SSL - see https://github.com/socketstream/socketstream/issues/349
};

// Create new Connect app instance which can be accessed from your app.js file with ss.http.middleware
app = connect();

// Alias app.use to indicate this will be added to the stack BEFORE SocketStream middleware
app.prepend = app.use;

// Allow Connect middleware to be added AFTER SocketStream middleware has been added to the stack
useAfterStack = [];

app.append = function() {
  var args;
  args = Array.prototype.slice.call(arguments);
  return useAfterStack.push(args);
};

module.exports = function(root) {
  return {
    // Return API
    connect: connect,
    middleware: app,
    router: router,

    // Merge optional settings    
    set: function(newSettings) {
      var k, v, _results;
      if (typeof newSettings !== 'object') {
        throw new Error('ss.http.set() takes an object e.g. {static: {maxAge: 60000}}');
      }
      _results = [];
      for (k in newSettings) {
        v = newSettings[k];
        _results.push(settings[k] = v);
      }
      return _results;
    },
    load: function(staticPath, sessionStore, sessionOptions) {
      staticPath = pathlib.join(root, staticPath);
      loadStaticDirs(staticPath);

      /* connect.compress() should be added to middleware stack on highest possible position */
      app.use(connect.compress());

      // Append SocketStream middleware upon server load      
      app.use(connect.cookieParser('SocketStream')).use(connect.favicon(staticPath + '/favicon.ico')).use(connect.session({
        cookie: {
          path: '/',
          httpOnly: false,
          maxAge: sessionOptions.maxAge,
          secure: settings.secure
        },
        store: sessionStore
      }));

      // Append any custom-defined middleware (e.g. everyauth)      
      useAfterStack.forEach(function(m) {
        return app.use.apply(app, m);
      });

      // Finally ensure static asset serving is last      
      app.use(eventMiddleware).use(connect['static'](staticPath, settings['static']));

      var inProduction = process.env.NODE_ENV === 'production' || process.env.SS_ENV === 'production';

      if (inProduction) {
        app.use(connect.staticCache(settings['staticCache']));
      }

      return app;
{'fraction_non_alphanumeric': 0.12718244433765818, 'fraction_numerical': 0.0076886112445939455, 'mean_word_length': 3.0283870967741935, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6492877', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1737, 'n_words': 540}