Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ldif

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ldif

LDIF (LDAP Directory Interchange Format) tools for Node

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
950
increased by20.87%
Maintainers
1
Weekly downloads
 
Created
Source

node-ldif

Nodejs LDIF (LDAP Data Interchange Format) parser based on RFC2849

Unless you are an LDAP aficionado you may not know about the LDIF format. I was surprised to learn that no LDIF parsing library existed for node.

So I wrote one, with peg.js. It aims to be RFC-compliant.
Now I'll never have to use that cursed perl script again!

Design Goals

  • 100% RFC-compliance; should comprehend any valid LDIF file
  • Parsed records stored internally intact
  • Methods are provided to extract record data in various formats
  • Outputs exactly compatilble LDIF for any parsed record or file
  • Automatic decoding and outputting of base64 data
  • No external library dependencies; pure Node Javascript

Usage

Installation

Install easily with npm!

npm install ldif

Parsing

Parsing strings
var ldif = require('ldif'),
    file = './rfc/example1.ldif',
    input = require('fs').readFileSync(file,'utf8');

console.log(ldif.parse(input));

After reading the file, it's parsed as a string.
There's also a shorthand to read in a file (synchronously, as above):

File parsing shorthand
var ldif = require('ldif');
console.log(ldif.parseFile('./rfc/example1.ldif'));

Parsing an LDIF file returns an object format for an entire LDIF file.
In this case, example1.ldif specifies contents of two LDAP records.

Shifting records from parsed file
var ldif = require('ldif');
    file = ldif.parseFile('./rfc/example1.ldif');

var record = file.shift();

Records are stored in an internal format, using classic Javascript objects. The type or value specified in a type property for all objects, but they can also be tested for specific constructor types:

var ldif = require('ldif');
    file = ldif.parseFile('./rfc/example1.ldif');

(file instanceof ldif.Container)        === true
(file.shift() instanceof ldif.Record)   === true

Converting

File data to plain object
var ldif = require('ldif');
    file = ldif.parseFile('./rfc/example1.ldif'),
    output_options = {};

console.log(file.toObject(output_options));

Returns the following result:

{ type: 'content',
  version: 1,
  entries: 
   [ { dn: 'cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com',
       attributes: [Object] },
     { dn: 'cn=Bjorn Jensen, ou=Accounting, dc=airius, dc=com',
       attributes: [Object] } ] }

Again, this is for the entire file. It's more common you'd want to operate on individual records:

Record to plain object
var ldif = require('ldif');
    file = ldif.parseFile('./rfc/example1.ldif'),
    output_options = {};

var record = file.shift();
console.log(record.toObject(output_options));

Output of the above code is this:

{ dn: 'cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com',
  attributes: 
   { objectclass: [ 'top', 'person', 'organizationalPerson' ],
     cn: [ 'Barbara Jensen', 'Barbara J Jensen', 'Babs Jensen' ],
     sn: 'Jensen',
     uid: 'bjensen',
     telephonenumber: '+1 408 555 1212',
     description: 'A big sailing fan.' } }

Notice the default behavior outputs attribute key/value pairs that have values of either an array or single string. Since an attribute can be single- or multi-valued, this format makes sense in most cases.

There is a way to pass an options object to this method and alter the benavior. For now that is left as an exercise for the reader. If you want a hint, look in the source for Record.defaults and how those settings interact.

Important note: The toObject() methods are not currently implemented for change format files or entries.

Outputting LDIF for parsed files

All parsed data can be written back to LDIF format using a toLDIF() method (on files or entries).

var ldif = require('ldif');
    file = ldif.parseFile('./rfc/example1.ldif');

// the whole file
console.log(file.toLDIF());

// or just a single record
console.log(file.shift().toLDIF());

TODO

  • toObject() methods for change schema and entries
  • Streaming read interface (coming soon--probably as a seperate package)
  • Construct and alter objects through code
  • More complete documentation
  • Test suite

FAQs

Package last updated on 05 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc