-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
140 lines (111 loc) · 3.05 KB
/
index.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
var redis = require('redis');
var events = require('events');
function Config(){
var config = {
'key length': 1,
'chars': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
};
this.set = function(key, value){
config[key] = value;
return true;
}
this.get = function(key, fallback){
if(key in config){
return config[key];
} else {
return fallback ? fallback : undefined;
}
}
}
var config = new Config();
function getRedisClient(){
var client = config.get('redis client');
if (!(client instanceof redis.RedisClient)){
throw "Please configure shrtn's redis client using something like shrtn.config.set('redis client', redis.createClient())";
} else {
return client;
}
}
function isURL(s){
var regexp = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
}
function generateId(callback){
var id = '';
var redisClient = getRedisClient();
var chars = config.get('chars').toString();
var keyLength = new Number(config.get('key length'));
// check for the number of existing keys
redisClient.keys('*', function(error, response){
// while half of the number of possible random keys
// is less than the number of keys set, increase the
// number of random keys possible by increasing key
// length.
while(Math.pow(chars.length, keyLength)/2 < response.length){
keyLength ++;
}
config.set('key length', keyLength);
for (var i=0; i < keyLength; i++){
id += chars[Math.floor(Math.random() * chars.length)];
}
callback(id);
});
}
function shorten(long, callback){
var redisClient = getRedisClient();
if (!(isURL(long))){
var response = {
'status': 'ERROR',
'message': 'Invalid URL: '+ long
}
if(typeof(callback) === 'function'){callback(response)}
shrtn.emit('error', response);
return false;
}
generateId(function(newId){
redisClient.setnx(newId, long, function(err, res){
if(res){
var response = {
'status': 'OK',
'id': newId,
'long': long
}
if(typeof(callback) === 'function'){callback(response)}
shrtn.emit('shortened', response);
return true;
} else {
// the attempted ID is taken
shorten(long, callback);
}
});
});
}
function expand(id, callback){
var redisClient = getRedisClient();
redisClient.get(id, function(err, response){
if (response){
var result = {
'status': 'OK',
'long': response,
'id': id
}
if(typeof(callback) === 'function'){callback(response)}
shrtn.emit('expanded', result);
return true;
}
else {
var result = {
'status': 'ERROR',
'message': 'Key not found'
}
if(typeof(callback) === 'function'){callback(response)}
shrtn.emit('error', result);
return false;
}
});
}
var shrtn = new events.EventEmitter();
shrtn.config = config;
shrtn.shorten = shorten;
shrtn.expand = expand;
module.exports = shrtn;