-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnames.js
205 lines (172 loc) · 4.69 KB
/
names.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
var debug = require('diagnostics')('names')
, httpsgc = require('./httpsgc')
, async = require('async')
, level = require('level')
, path = require('path');
function nope() { /* used to prevent double execution */ }
/**
* Names: Autocomplete for npm package names through leveldb #buzzwordbingo.
*
* Options:
*
* - url: The API endpoint where we can get the package data from. It should
* return an array with objects contain a name and description.
* - interval: Refresh interval of the data.
* - suffix: Suffix key which will be appended to key names so we can get
* a range.
* - db: The location of the database.
*
* @constructor
* @param {Object} options Configuration of the names.
* @api private
*/
function Names(options) {
if (!this) return new Names(options);
options = options || {};
this.url = options.url || 'https://raw.githubusercontent.com/polyhack/npm-github-data/master/allpackages.json';
this.interval = +options.interval || 1000 * 60 * 60;
this.suffix = options.suffix || '\xff';
this.db = level(options.db || path.join(__dirname, 'names.db'), {
valueEncoding: 'utf-8'
});
if (options.refresh) this.refresh();
this.setInterval = setInterval(this.refresh.bind(this), this.interval);
}
/**
* Refresh the current dataset.
*
* @param {Function} fn Completion callback.
* @returns {Names}
* @api private
*/
Names.prototype.refresh = function refresh(fn) {
fn = fn || function nope(err) {
if (err) debug('failed to refresh, received error', err);
};
var names = this;
async.parallel({
remote: this.fetch.bind(this),
keys: this.keys.bind(this)
}, function nexted(err, data) {
if (err) return fn(err);
var ops = []
, existing;
//
// Map existing values to an object for easy matching.
//
existing = data.keys.reduce(function reduce(memo, row) {
memo[row.key] = row.value;
return memo;
}, {});
data.remote.forEach(function each(row) {
//
// Non existing row, we should remove this from our database.
//
if (!(row.name in existing)) ops.push({ type: 'del', key: row.name });
//
// Prevent duplicate put requests, the data already exists in the database
// so we don't need to update it anymore. Saving a bit of CPU.
//
if (existing[row.name] === row.desc) return;
ops.push({ type: 'put', key: row.name, value: row.desc || names.suffix });
});
names.db.batch(ops, fn);
});
return this;
};
/**
* Fetch the remote API of things.
*
* @param {Function} fn Completion callback.
* @returns {Names}
* @api private
*/
Names.prototype.fetch = function fetch(fn) {
fn = fn || function nope(err) {
if (err) debug('failed to fetch, received error', err);
};
httpsgc(this.url, function fetched(err, body) {
if (err) return fn(err);
if (!body || !body.length) return fn(new Error('No content returned'));
var data;
try {
data = JSON.parse(body.toString());
if (!Array.isArray(data)) throw new Error('Invalid data structure');
} catch (e) { return fn(e); }
debug('received %d rows', data.length);
return fn(undefined, data.map(function reduce(row) {
return {
desc: row.description,
name: row.name
};
}).filter(function filter(row) {
return !!row.name;
}));
}, true);
return this;
};
/**
* Get all keys from the database.
*
* @param {Function} fn Completion callback
* @returns {Names}
* @api private
*/
Names.prototype.keys = function keys(fn) {
var all = [];
this.db.createReadStream()
.on('data', function dataset(data) {
all.push(data);
}).on('end', function end() {
fn(undefined, all);
}).on('error', function error(e) {
fn(e, all);
fn = nope;
});
return this;
};
/**
* Search for matches.
*
* @param {String} name The name we should search.
* @param {Function} fn Completion callback.
* @returns {Names}
* @api private
*/
Names.prototype.find = function find(name, limit, fn) {
var results = [];
if ('function' === typeof limit) {
fn = limit;
limit = -1;
}
this.db.createReadStream({
gte: name,
lte: name + this.suffix,
limit: limit
}).on('data', function receive(data) {
results.push(data);
}).on('end', function end() {
fn(undefined, results);
}).on('error', function error(e) {
fn(e, results);
fn = nope;
});
return this;
};
/**
* Destroy the database.
*
* @param {Function} fn Completion callback
* @returns {Names}
* @api public
*/
Names.prototype.destroy = function destroy(fn) {
if (this.setInterval) clearInterval(this.setInterval);
this.db.close(fn);
return this;
};
//
// Expose the leveldb names database.
//
module.exports = Names;