npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ldif-hufsm

v0.7.0

Published

LDIF (LDAP Directory Interchange Format) tools for Node

Downloads

9

Readme

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.

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 compatible LDIF for any parsed record or file
  • Automatic decoding and outputting of base64 data
  • No external library dependencies; pure Node Javascript
  • Includes complete test suite
  • The library can parse special language characters

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

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.

toObject(options)

The behavior of toObject() can be altered with options below.

Option | Type | Description | Deafult ------ | ---- | ----------- | ---------- flatten | boolean | Flatten single values into strings | true single | boolean | Overrides flatten, only returns single values | false decode | boolean | Decode values (not yet well-defined, leave true) | true preserveOptions | boolean | Outputs any attribute options | true preferOptions | array | Prefer these options when preserveOptions is false | [ ]

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());

Note: toLDIF() method folds lines by default at 78 characters. If you want to change this value call toLDIF(width) where width is an integer.

Tests

To run the test suite, use npm test (you'll need the dev dependencies of mocha and chai installed).

Rebuild parser

To modify the parser, edit lib/ldif.pegjs and run npm run make (this requires the pegjs dev dependency to be installed).

TODO

  • Streaming read interface (coming soon--probably as a seperate package)
  • Construct and alter objects through code (document this)
  • More complete documentation