<commit_before>/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

CLASS({
  package: 'foam.node',
  name: 'Server',
  requires: [
    'foam.node.handlers.StaticFileHandler',
    'foam.node.handlers.FileHandler',
    'foam.node.handlers.DAOHandler'
  ],
  exports: [
    'exportDAO',
    'exportFile',
    'exportDirectory',
  ],
  imports: [
    'log'
  ],
  properties: [
    {
      model_: 'StringArrayProperty',
      name: 'agents',
      help: 'List of agents to execute once loaded',
      adapt: function(_, v) {
        if ( typeof v === "string" ) return v.split(',');
        return v;
      }
    },
    {
      model_: 'IntProperty',
      name: 'port',
      defaultValue: 8000,
      adapt: function(_, v) {
        if ( typeof v == "string" ) return parseInt(v);
        return v;
      }
    },
    {
      model_: 'foam.node.NodeRequireProperty',
      name: 'http'
    },
    {
      name: 'server',
      hidden: true
    },
    {
      model_: 'ArrayProperty',
      name: 'handlers',
      hidden: true
    },
    {
      name: 'daoHandler_',
      lazyFactory: function() { return this.DAOHandler.create({ path: '/api' }); },
      postSet: function(old, handler) {
        if ( old ) {
          this.handlers.removeI(function(p) { return p === old; });
        }
        this.handlers.push(handler);
      }
    }
  ],
  methods: [
    function execute() {
      this.server = this.http.createServer(this.onRequest);
      this.server.listen(this.port);

      // TODO Find a better way to default these?
      this.agents.push('foam.node.ServeFOAM');

      for( var i = 0 ; i < this.agents.length ; i++ ) {
        this.log("Loading ", this.agents[i]);
        var f = arequire(this.agents[i])(function(m) {
          var agent = m.create(undefined, this.Y);
          if ( agent.execute ) {
            agent.execute();<commit_msg>Node: Support SSL in the server. Provide keyFile and certFile to use HTTPS.<commit_after>/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

CLASS({
  package: 'foam.node',
  name: 'Server',
  requires: [
    'foam.node.handlers.StaticFileHandler',
    'foam.node.handlers.FileHandler',
    'foam.node.handlers.DAOHandler'
  ],
  exports: [
    'exportDAO',
    'exportFile',
    'exportDirectory',
  ],
  imports: [
    'log'
  ],
  properties: [
    {
      model_: 'StringArrayProperty',
      name: 'agents',
      help: 'List of agents to execute once loaded',
      adapt: function(_, v) {
        if ( typeof v === "string" ) return v.split(',');
        return v;
      }
    },
    {
      model_: 'IntProperty',
      name: 'port',
      defaultValue: 8000,
      adapt: function(_, v) {
        if ( typeof v == "string" ) return parseInt(v);
        return v;
      }
    },
    {
      model_: 'foam.node.NodeRequireProperty',
      name: 'http'
    },
    {
      model_: 'foam.node.NodeRequireProperty',
      name: 'https'
    },
    {
      name: 'keyFile',
    },
    {
      name: 'certFile',
    },
    {
      name: 'server',
      hidden: true
    },
    {
      model_: 'ArrayProperty',
      name: 'handlers',
      hidden: true
    },
    {
      name: 'daoHandler_',
      lazyFactory: function() { return this.DAOHandler.create({ path: '/api' }); },
      postSet: function(old, handler) {
        if ( old ) {
          this.handlers.removeI(function(p) { return p === old; });
        }
        this.handlers.push(handler);
      }
    }
  ],
  methods: [
    function execute() {
      if (this.keyFile && this.certFile) {
        var fs = require('fs');
        this.server = this.https.createServer({
          key: fs.readFileSync(this.keyFile),
          cert: fs.readFileSync(this.certFile)
        }, this.onRequest);
      } else {
        this.server = this.http.createServer(this.onRequest);
      }

      this.server.listen(this.port);

      // TODO Find a better way to default these?
      this.agents.push('foam.node.ServeFOAM');

      for( var i = 0 ; i < this.agents.length ; i++ ) {
        this.log("Loading ", this.agents[i]);
        var f = arequire(this.agents[i])(function(m) {
          var agent = m.create(undefined, this.Y);
          if ( agent.execute ) {
            agent.execute();
{'fraction_non_alphanumeric': 0.13523596343965677, 'fraction_numerical': 0.004849841447491139, 'mean_word_length': 2.261557177615572, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4588518', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1634, 'n_words': 451}