1 |
|
2 | (function() {
|
3 | var array_intersection, crypto, escapable, lookup, unroll_lookup;
|
4 |
|
5 | crypto = require('crypto');
|
6 |
|
7 | exports.array_intersection = array_intersection = function(arr_a, arr_b) {
|
8 | var a, j, len, r;
|
9 | r = [];
|
10 | for (j = 0, len = arr_a.length; j < len; j++) {
|
11 | a = arr_a[j];
|
12 | if (arr_b.indexOf(a) !== -1) {
|
13 | r.push(a);
|
14 | }
|
15 | }
|
16 | return r;
|
17 | };
|
18 |
|
19 | exports.escape_selected = function(str, chars) {
|
20 | var c, i, j, l, len, map, parts, r, ref, v;
|
21 | map = {};
|
22 | chars = '%' + chars;
|
23 | for (j = 0, len = chars.length; j < len; j++) {
|
24 | c = chars[j];
|
25 | map[c] = escape(c);
|
26 | }
|
27 | r = new RegExp('([' + chars + '])');
|
28 | parts = str.split(r);
|
29 | for (i = l = 0, ref = parts.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) {
|
30 | v = parts[i];
|
31 | if (v.length === 1 && v in map) {
|
32 | parts[i] = map[v];
|
33 | }
|
34 | }
|
35 | return parts.join('');
|
36 | };
|
37 |
|
38 | exports.buffer_concat = function(buf_a, buf_b) {
|
39 | var dst;
|
40 | dst = new Buffer(buf_a.length + buf_b.length);
|
41 | buf_a.copy(dst);
|
42 | buf_b.copy(dst, buf_a.length);
|
43 | return dst;
|
44 | };
|
45 |
|
46 | exports.md5_hex = function(data) {
|
47 | return crypto.createHash('md5').update(data).digest('hex');
|
48 | };
|
49 |
|
50 | exports.sha1_base64 = function(data) {
|
51 | return crypto.createHash('sha1').update(data).digest('base64');
|
52 | };
|
53 |
|
54 | exports.timeout_chain = function(arr) {
|
55 | var fun, ref, timeout, user_fun;
|
56 | arr = arr.slice(0);
|
57 | if (!arr.length) {
|
58 | return;
|
59 | }
|
60 | ref = arr.shift(), timeout = ref[0], user_fun = ref[1];
|
61 | fun = (function(_this) {
|
62 | return function() {
|
63 | user_fun();
|
64 | return exports.timeout_chain(arr);
|
65 | };
|
66 | })(this);
|
67 | return setTimeout(fun, timeout);
|
68 | };
|
69 |
|
70 | exports.objectExtend = function(dst, src) {
|
71 | var k;
|
72 | for (k in src) {
|
73 | if (src.hasOwnProperty(k)) {
|
74 | dst[k] = src[k];
|
75 | }
|
76 | }
|
77 | return dst;
|
78 | };
|
79 |
|
80 | exports.overshadowListeners = function(ee, event, handler) {
|
81 | var new_handler, old_listeners;
|
82 | old_listeners = ee.listeners(event).slice(0);
|
83 | ee.removeAllListeners(event);
|
84 | new_handler = function() {
|
85 | var j, len, listener;
|
86 | if (handler.apply(this, arguments) !== true) {
|
87 | for (j = 0, len = old_listeners.length; j < len; j++) {
|
88 | listener = old_listeners[j];
|
89 | listener.apply(this, arguments);
|
90 | }
|
91 | return false;
|
92 | }
|
93 | return true;
|
94 | };
|
95 | return ee.addListener(event, new_handler);
|
96 | };
|
97 |
|
98 | escapable = /[\x00-\x1f\ud800-\udfff\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufff0-\uffff]/g;
|
99 |
|
100 | unroll_lookup = function(escapable) {
|
101 | var c, i, unrolled;
|
102 | unrolled = {};
|
103 | c = (function() {
|
104 | var j, results;
|
105 | results = [];
|
106 | for (i = j = 0; j < 65536; i = ++j) {
|
107 | results.push(String.fromCharCode(i));
|
108 | }
|
109 | return results;
|
110 | })();
|
111 | escapable.lastIndex = 0;
|
112 | c.join('').replace(escapable, function(a) {
|
113 | return unrolled[a] = '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
114 | });
|
115 | return unrolled;
|
116 | };
|
117 |
|
118 | lookup = unroll_lookup(escapable);
|
119 |
|
120 | exports.quote = function(string) {
|
121 | var quoted;
|
122 | quoted = JSON.stringify(string);
|
123 | escapable.lastIndex = 0;
|
124 | if (!escapable.test(quoted)) {
|
125 | return quoted;
|
126 | }
|
127 | return quoted.replace(escapable, function(a) {
|
128 | return lookup[a];
|
129 | });
|
130 | };
|
131 |
|
132 | exports.parseCookie = function(cookie_header) {
|
133 | var cookie, cookies, j, len, parts, ref;
|
134 | cookies = {};
|
135 | if (cookie_header) {
|
136 | ref = cookie_header.split(';');
|
137 | for (j = 0, len = ref.length; j < len; j++) {
|
138 | cookie = ref[j];
|
139 | parts = cookie.split('=');
|
140 | cookies[parts[0].trim()] = (parts[1] || '').trim();
|
141 | }
|
142 | }
|
143 | return cookies;
|
144 | };
|
145 |
|
146 | exports.random32 = function() {
|
147 | var foo, v;
|
148 | foo = crypto.randomBytes(4);
|
149 | v = [foo[0], foo[1], foo[2], foo[3]];
|
150 | return v[0] + (v[1] * 256) + (v[2] * 256 * 256) + (v[3] * 256 * 256 * 256);
|
151 | };
|
152 |
|
153 | }).call(this);
|