forked from jhs/probe_couchdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.js
135 lines (110 loc) · 3.69 KB
/
lib.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
// Miscellaneous helpers
//
// Copyright 2011 Iris Couch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var debug = require('debug')
var assert = require('assert')
var uglify = require('uglify-js')
var defaultable = require('defaultable')
var package = require('./package.json')
defaultable(module,
{ 'log_label': 'probe_couchdb'
, 'log_level': 'info'
}, function(module, exports, DEFS, require) {
module.exports = { "getLogger" : getLogger
, "get_creds" : get_creds
, "isArray" : isArray
, "join" : join_and_fix_slashes
, "encode_id" : encode_doc_id
, "isRegExp" : isRegExp
, "check_expr" : check_expr
};
function getLogger(sub_label) {
var label = package.name
if(sub_label)
label += ':' + sub_label
var logger = debug(label)
var log = { 'trace': logger
, 'debug': logger
, 'info' : logger
, 'warn' : logger
, 'error': logger
, 'fatal': logger
}
return log
}
var url_parts = /(https?:\/\/)([^:]+:[^@]+@)?(.*)$/;
function get_creds(url) {
var match = url_parts.exec(url);
if(!match)
throw new Error("Cannot parse URL: " + url);
var auth = match[2];
match = /^(.*?):(.*)@$/.exec(auth);
if(!match)
return [null, null];
return [match[1], match[2]];
}
function scrub_creds(url) {
if(typeof url === 'string')
url = url.replace(url_parts, '$1$3'); // Scrub username and password
return url;
}
function join_and_fix_slashes() {
return Array.prototype.map.apply(arguments, [function trimmed(arg) {
return arg.replace(/^\/+/, "").replace(/\/+$/, "");
}]).join('/');
}
// Encode a document ID, which means escape it *except* the slash in a design document.
function encode_doc_id(id) {
var encoded = encodeURIComponent(id);
return encoded.replace(/^_design%2[fF]/, '_design/');
}
function isRegExp(obj) {
var str = '' + obj;
return !! ( obj instanceof RegExp ||
typeof obj === 'function' &&
obj.constructor.name === 'RegExp' &&
obj.compile &&
obj.test &&
obj.exec &&
str.match(/^\/.*\/[gim]{0,3}$/)
)
}
function check_expr(source_code, type) {
var ast = uglify.parser.parse('(' + source_code + ')');
assert.equal(ast[0], 'toplevel');
var statements = ast[1];
assert.equal(statements.length, 1);
var func_expr = statements[0];
assert.equal(func_expr.length, 2);
assert.equal(func_expr[0], 'stat');
var func_def = func_expr[1];
assert.equal(func_def[0], 'function');
// assert.equal(func_def[1], 'map');
var formals = func_def[2];
function assert_formals() {
var expected = Array.prototype.slice.apply(arguments);
if(JSON.stringify(formals) !== JSON.stringify(expected))
throw new Error('Nonstandard '+type+' function; expected function('+expected.join(', ')+'), got function('+formals.join(', ')+')');
}
if(type == 'map')
assert_formals('doc');
if(type == 'reduce')
assert_formals('keys', 'values', 'rereduce');
}
function isArray(obj) {
return Array.isArray(obj);
}
function noop() {}
}) // defaultable