-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (67 loc) · 1.83 KB
/
index.js
File metadata and controls
80 lines (67 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var es = require('event-stream');
var fs = require('fs');
var ldif = require('ldif');
var Streamer = function(stream,options){
var chain = stream, self = this;
options = ldif._extend({},Streamer.defaults,options);
// Normalize pipeline array
if (!Array.isArray(options.pipeline))
options.pipeline = [ options.pipeline ];
// Chunk by delimiter
if (options.delimiter)
chain = chain.pipe(es.split(options.delimiter));
// Default parser
chain = chain.pipe(options.parser);
// Attach pipeline
options.pipeline.forEach(function(item){
if (typeof item == 'function')
chain = chain.pipe(item(options));
});
return chain;
};
// Default LDIF parser
Streamer.parse = es.map(function(data,cb){
try{ cb(null,ldif.parse(data+'\n')); }
catch(err) { cb(); }
});
// Maps LDIF values
Streamer.ldif = function(options){
return es.map(function(data,cb){
try{ cb(null,data.toLDIF(options.width)); }
catch (err) { cb(); }
});
};
// Maps each record to a plain object
Streamer.object = function(options){
return es.map(function(data,cb){
try{ cb(null,data.toObject(options)); }
catch (err) { cb(); }
});
};
// Record parser
// (emit stream object for each record contained in a file)
Streamer.record = function(options){
return es.through(function(data){
var row, self = this;
data.entries.forEach(function(item){
self.emit('data',item);
});
});
};
// Default options
Streamer.defaults = {
width: 78,
delimiter: "\n\n",
parser: Streamer.parse,
pipeline: [ Streamer.object, Streamer.record ],
flatten: true,
single: false,
preserveOptions: true,
preferOptions: []
};
// Convenience method to stream a file
Streamer.file = function(filename,options){
var stream = fs.createReadStream(filename,'utf8');
return Streamer(stream,options);
};
module.exports = Streamer;