-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.js
58 lines (48 loc) · 1.28 KB
/
importer.js
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
var zlib = require('zlib'),
http = require('http'),
path = require('path'),
util = require('util'),
crypto = require('crypto'),
events = require('events'),
stream = require('stream'),
tar = require('tar');
var Importer = module.exports = function (options) {
if (!(this instanceof Importer)) {
return new Importer(options);
}
this.name = options.name;
this.version = options.version;
this.author = options.author
this.elasticsearch = options.elasticsearch;
stream.Duplex.call(this, { objectMode: true });
};
util.inherits(Importer, stream.Duplex);
Importer.prototype.add = function (entry) {
var data = '',
hash = crypto.createHash('sha256'),
self = this;
if (path.extname(entry.path) !== '.js') {
return;
}
hash.update(entry.path);
entry.on('data', function (chunk) {
data += chunk.toString('utf8');
});
entry.once('end', function () {
self.push({
id: self.name + '-' + hash.digest('hex'),
doc: {
package: self.name,
version: self.version,
author: self.author,
filename: entry.path.split('/').slice(1).join('/'),
content: data
}
});
});
};
Importer.prototype._read = function () {
};
Importer.prototype._write = function (chunk, encoding, cb) {
cb();
};