gagle / node-properties

.properties parser/stringifier.
MIT License
133 stars 42 forks source link

[TypeError: Object #<Object> has no method 'parse'] #11

Closed Dexus closed 10 years ago

Dexus commented 10 years ago

I use this code... in function parseData() i get the error [TypeError: Object #<Object> has no method 'parse']

i have install the module as global module. And than run this script. but cant parse the data i get... :(

var net = require('net'),
    redis = require("redis").createClient();

var properties = require ("properties");

    // redis select DB 3
    redis.select(3, function() { /* ... */ });

function parseData(client, data){   
    try{
      //Certain options can throw errors, so if the callback is not used, try-catch
      //the function
      var obj = properties.parse(data);
      takeActionForRequest(client,obj);

    }catch (error){
        console.log(error);
        return client.write("action=reject\n\n");
    }
}

function takeActionForRequest(client, req){
    var email_sender = Address(req.sender);
    var email_sasl_username = Address(req.sasl_username);

    console.log(email_sender,email_sasl_username, req);

    if(req.request == 'smtpd_access_policy' && req.protocol_state == 'RCPT' && req.sasl_username !=""){

        return client.write("action=450 4.7.1 You daily outbound limit of 1000 has reached!\n\n");
    }
    ...
    ...
    ...
    else {
        return client.write("action=reject\n\n");
    }

}

var server = net.createServer(function (client) {
    console.log("client connected");

    client.on('data', function (data) {
        parseData(client, data.toString());
        //client.write("action=450 4.7.1 You daily outbound limit of 1000 has reached!\n\n");

    });
    client.on('end', function() {
        console.log('client disconnected');
    });
});

server.on('error', function (e) {
  if (e.code == 'EADDRINUSE') {
    console.log('Address in use, retrying...');
    setTimeout(function () {
      server.close();
      server.listen(8088);
    }, 1000);
  }
});

process.on('SIGTERM', function () {
  console.log("Closing");
  server.close();
});

server.on('close', function () {
  console.log("Closed");
  redis.quit();
});

server.listen(8088);
Dexus commented 10 years ago

btw i use node v0.10.26

gagle commented 10 years ago

Which module version do you have installed? Anyway, you must not call the parse() function with partial streaming data, that is, the parse() function must receive the whole data to parse, otherwise the final object won't be what you expect.

Why do you need to store config data in a redis instance? Configuration files like .properties are normally stored in a simple file and read back using a single readFile() call because they tend to be small: 1-2KB.

I have plans to implement a new native module with a streaming interface, just for parsing .properties files more faster.

Dexus commented 10 years ago

Hey, i have the latest version from npm installed 1 h ago.

It is a module for Postfix that i code to limit outgoing mails. For now its only a prototype to test the speed agains Perl. And i'm currently faster than perl for now.

the data send via a socket, so the format ist pretty key=value each line. and this module works perfekt to parse it. Okay it is maybe a littlebit overhead to use it but for trying its ok.

no i dont have the error anymore, have found my issue in an other function, witch called befor, and overwrites the module variable. -_-'

But thank you for your fast replay.