<commit_before>/**
 * Copyright 2017 IBM Corp.
 *
 * 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.
 **/

module.exports = function(RED) {
  var fs = require('fs');


  function verifyPayload(msg) {
    if (!msg.payload) {
      return Promise.reject('Missing property: msg.payload');
    } else {
      return Promise.resolve();
    }
  }

  function loadFile(msg) {
    var p = new Promise(function resolver(resolve, reject) {
        var readStream = fs.readFileSync(msg.payload);
        msg.payload = readStream;
        resolve();
    });
    return p;
  }

  function Node(config) {
    var node = this;
    RED.nodes.createNode(this, config);

    this.on('input', function(msg) {
      //var message = '';
      node.status({
        fill: 'blue',
        shape: 'dot',
        text: 'loading file'
      });

      verifyPayload(msg)
        .then(function() {
          return loadFile(msg);
        })
        .then(function() {
          node.status({});
          node.send(msg);
        })
        .catch(function(err) {
          var messageTxt = err;
          if (err.error) {
            messageTxt = err.error;
          } else if (err.description) {
            messageTxt = err.description;
          }
          node.status({
            fill: 'red',
            shape: 'dot',
            text: messageTxt
          });

          msg.result = {};
          msg.result['error'] = err;
          node.error(messageTxt, msg);
          // Note: This node.send forwards the error to the next node,
          // if this isn't desired then this line needs to be removed.
          // Should be ok as the node.error would already have recorded
          // the error in the debug console.
          node.send(msg);
        });

    });
  }

  RED.nodes.registerType('file-buffer', Node, {
    credentials: {
      token: {
        type: 'text'
      }
    }
  });
};
<commit_msg>FileLoad is now async with error checking<commit_after>/**
 * Copyright 2017 IBM Corp.
 *
 * 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.
 **/

module.exports = function(RED) {
  var fs = require('fs');

  function verifyPayload(msg) {
    if (!msg.payload) {
      return Promise.reject('Missing property: msg.payload');
    } else {
      return Promise.resolve();
    }
  }

  function loadFile(msg) {
    var p = new Promise(function resolver(resolve, reject) {
        fs.readFile(msg.payload, function(err, data){
          if (err) {
            reject(err);
          } else {
            msg.payload = data;
            resolve();
          }
        });
    });
    return p;
  }

  function reportError(node, msg, err) {
    var messageTxt = err;
    if (err.code && 'ENOENT' === err.code) {
      messageTxt = 'Invalid File Path';
    }
    if (err.error) {
      messageTxt = err.error;
    } else if (err.description) {
      messageTxt = err.description;
    }
    node.status({
      fill: 'red',
      shape: 'dot',
      text: messageTxt
    });

    msg.result = {};
    msg.result['error'] = err;
    node.error(messageTxt, msg);
  }

  function Node(config) {
    var node = this;
    RED.nodes.createNode(this, config);

    this.on('input', function(msg) {
      //var message = '';
      node.status({
        fill: 'blue',
        shape: 'dot',
        text: 'loading file'
      });

      verifyPayload(msg)
        .then(function() {
          return loadFile(msg);
        })
        .then(function() {
          node.status({});
          node.send(msg);
        })
        .catch(function(err) {
          reportError(node,msg,err);
          // Note: This node.send forwards the error to the next node,
          // if this isn't desired then this line needs to be removed.
          // Should be ok as the node.error would already have recorded
          // the error in the debug console.
          node.send(msg);
        });

    });
  }

  RED.nodes.registerType('file-buffer', Node, {
    credentials: {
      token: {
        type: 'text'
      }
    }
  });
};

{'fraction_non_alphanumeric': 0.12157568486302739, 'fraction_numerical': 0.003199360127974405, 'mean_word_length': 2.0296789824348878, 'pattern_counts': {'":': 0, '<': 3, '<?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': '285659', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1408, 'n_words': 443}