Skip to content

opendkim.chunk()

Christopher Mooney edited this page Feb 12, 2018 · 4 revisions

DESCRIPTION

Handle a chunk of message input. The input is a buffer of message data which may contain headers or body. An entire message may be fed to the API in one buffer using this function. The API will determine automatically the boundary between header fields and the body of the message and process it accordingly. Message body content should be in canonical form (e.g., with dot-stuffing removed, if any).

Lines in the data chunk are expected to be CRLF-terminated in the standard way. For input that is not, consider setting the DKIM_LIBFLAGS_FIXCRLF (see opendkim.options()), which will cause this function to attempt to auto-detect based on the first line whether the input is CRLF-terminated or not, and adapt accordingly.

opendkim.eoh() will be called implicitly by this function upon encountering the end of the message's header block, but the caller must opendkim.chunk_end() to complete processing of the message.

opendkim.chunk() is called zero or more times between using opendkim.sign() and opendkim.verify(), and opendkim.chunk_end().

For more information:
http://www.opendkim.org/libopendkim/dkim_chunk.html

ARGUMENTS

Type: Object

  • message: The message chunk with normal CRLF line termination, dot-stuffing removed, and the terminating DOT removed.
  • length: length of the message chunk.

RETURN VALUES

  • On failure, an exception is thrown that indicates the cause of the problem.

NOTES

  • Dot stuffing and the terminating dot in the message body are expected to be removed by the caller. If they appear within body, they are assumed to be part of the message body and will be included in the hashed data. This is true of any content modification that might be done by the MTA.

EXAMPLE (async/await)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
async function verify(message) {                                                                         
  var opendkim = new OpenDKIM();                                                                         
                                                                                                         
  try {                                                                                                  
    await opendkim.verify({id: undefined});                                                              
    await opendkim.chunk({                                                                               
      message: message,                                                                                  
      length: message.length                                                                             
    });                                                                                                  
    await opendkim.chunk_end();                                                                          
  } catch (err) {                                                                                        
    console.log(opendkim.sig_geterrorstr(opendkim.sig_geterror()));                                      
    console.log(err);                                                                                    
  }                                                                                                      
}                                                                                                        

EXAMPLE (sync)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
function verify_sync(message) {                                                                          
  var opendkim = new OpenDKIM();                                                                         
                                                                                                         
  try {                                                                                                  
    opendkim.verify_sync({id: undefined});                                                               
    opendkim.chunk_sync({                                                                                
      message: message,                                                                                  
      length: message.length                                                                             
    });                                                                                                  
    opendkim.chunk_end_sync();                                                                           
  } catch (err) {                                                                                        
    console.log(opendkim.sig_geterrorstr(opendkim.sig_geterror()));                                      
    console.log(err);                                                                                    
  }                                                                                                      
}                                                                                                        

EXAMPLE (errback)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
function verify(opendkim, message, callback) {                                                           
  opendkim.verify({id: undefined}, function (err, result) {                                              
    if (err) {                                                                                           
      return callback(err, result);                                                                      
    }                                                                                                    
                                                                                                         
    var options = {                                                                                      
      message: message,                                                                                  
      length: message.length                                                                             
    };                                                                                                   
                                                                                                         
    opendkim.chunk(options, function (err, result) {                                                     
      if (err) {                                                                                         
        return callback(err, result);                                                                    
      }                                                                                                  
                                                                                                         
      opendkim.chunk_end(function (err, result) {                                                        
        if (err) {                                                                                       
          return callback(err, result);                                                                  
        }                                                                                                
                                                                                                         
        return callback(err, result);                                                                    
      });                                                                                                
    });                                                                                                  
  });                                                                                                    
}                                                                                                        
                                                                                                         
var opendkim = new OpenDKIM();                                                                           
                                                                                                         
verify(opendkim, message, function (err, result) {                                                       
  if (err) {                                                                                             
    return console.log(opendkim.sig_geterrorstr(opendkim.sig_geterror()));                               
  }                                                                                                      
                                                                                                         
  // success                                                                                             
});