-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathindex.js
More file actions
315 lines (276 loc) · 6.91 KB
/
index.js
File metadata and controls
315 lines (276 loc) · 6.91 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2013 SAP AG.
//
// 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.
'use strict';
/* jshint camelcase:false */
var util = require('util');
var stream = require('stream');
var path = require('path');
var os = require('os');
var fs = require('fs');
Object.defineProperties(exports, {
setImmediate: {
get: function setImmediate() {
if (typeof global.setImmediate === 'function') {
return global.setImmediate;
}
return process.nextTick;
}
},
cid: {
get: function getClientId() {
return [exports.pid || 'nodejs', os.hostname()].join('@');
}
}
});
exports.pid = process.pid;
exports._debuglog = util.debuglog;
exports.os_user = os.userInfo().username;
exports.debuglog = function debuglog() {
if (typeof exports._debuglog === 'function') {
return exports._debuglog.apply(null, arguments);
}
return function dummyDebuglog() {};
};
var debug = exports.debuglog('hdb_util');
exports.tracefile = function tracefile() {
var timestamp = Math.floor(Date.now() / 1000);
var filename = 'hdb.trace.' + timestamp + '.log';
return path.join(os.tmpdir(), filename);
};
exports._appendFileSync = fs.appendFileSync;
exports.tracelog = function tracelog() {
if (!process.env.HDB_TRACE) {
return function dummyTracelog() {};
}
var filename = exports.tracefile();
debug('Trace to file', filename);
return function tracelog(kind, segment) {
exports._appendFileSync(filename,
kind + '\n' +
util.inspect(segment, {
depth: 9
}));
};
};
exports.stream = {
Readable: stream.Readable,
Writable: stream.Writable,
Duplex: stream.Duplex,
Transform: stream.Transform
};
function exportNativeUtil(fn) {
exports[fn] = util[fn];
}
[
'format',
'debug',
'error',
'puts',
'print',
'log',
'inspect',
'isArray',
'isRegExp',
'isDate',
'isError',
'inherits'
].forEach(exportNativeUtil);
exports.bignum = require('./bignum');
exports.calendar = require('./calendar');
exports.convert = require('./convert');
exports.Queue = require('./Queue');
extend(exports, require('./zeropad'));
function extend(obj) {
function extendOnce(source) {
/* jshint forin:false */
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
}
Array.prototype.slice.call(arguments, 1).forEach(extendOnce);
return obj;
}
exports.extend = extend;
function isBoolean(arg) {
return typeof arg === 'boolean';
}
exports.isBoolean = isBoolean;
function isNumber(arg) {
return typeof arg === 'number';
}
exports.isNumber = isNumber;
function isString(arg) {
return typeof arg === 'string';
}
exports.isString = isString;
function isUndefined(arg) {
return arg === void 0;
}
exports.isUndefined = isUndefined;
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
exports.isObject = isObject;
function isFunction(arg) {
return typeof arg === 'function';
}
exports.isFunction = isFunction;
function alignLength(length, alignment) {
if (length % alignment === 0) {
return length;
}
return length + alignment - length % alignment;
}
exports.alignLength = alignLength;
function cc2_(str) {
return str.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase();
}
exports.cc2_ = cc2_;
function _2cc(str) {
return str.toLowerCase().replace(/_([a-z])/g, function toUpperCase(match,
str) {
return str.toUpperCase();
});
}
exports._2cc = _2cc;
function readData(ds, cb) {
var length = 0;
var chunks = [];
function done(err) {
ds.removeListener('error', onerror);
ds.removeListener('data', ondata);
ds.removeListener('end', onend);
if (isFunction(cb)) {
if (err) {
return cb(err);
}
cb(null, Buffer.concat(chunks, length));
}
}
function onerror(err) {
done(err);
}
ds.on('error', onerror);
function ondata(chunk) {
chunks.push(chunk);
length += chunk.length;
}
ds.on('data', ondata);
function onend() {
done(null);
}
ds.on('end', onend);
ds.resume();
}
exports.readData = readData;
function proxyEvents(source, target, events) {
function proxyEvent(ev) {
source.on(ev, target.emit.bind(target, ev));
}
events.forEach(proxyEvent);
return target;
}
exports.proxyEvents = proxyEvents;
function createReadStream(ds, events, options) {
if (!Array.isArray(events)) {
options = events;
events = ['error', 'close'];
}
var readable = new stream.Readable(options);
readable._read = function _read() {
ds.resume();
};
function onend() {
readable.push(null);
}
ds.once('end', onend);
function ondata(chunk) {
if (!chunk || !chunk.length || readable._readableState.ended) {
return;
}
if (!readable.push(chunk)) {
ds.pause();
}
}
ds.on('data', ondata);
proxyEvents(ds, readable, events);
return readable;
}
exports.createReadStream = createReadStream;
function pipe(source, target, events) {
proxyEvents(source, target, events || ['error']);
return source.pipe(target);
}
exports.pipe = pipe;
function filter(keys) {
/* jshint validthis:true */
var obj = {};
var key;
for (var i = 0; i < keys.length; i++) {
key = keys[i];
if (Object.prototype.hasOwnProperty.call(this, key)) {
obj[key] = this[key];
}
}
return obj;
}
exports.filter = filter;
function getBooleanProperty(arg) {
if (isString(arg)) {
var upper = arg.toUpperCase();
if (upper === 'TRUE' || upper === 'YES' || upper === 'ON' || upper === '1') {
return true;
}
return false;
} else if (arg === 1) {
return true;
} else if (arg === true) {
return true;
} else {
return false;
}
}
exports.getBooleanProperty = getBooleanProperty;
function validateAndGetBoolProperty(arg) {
var value = false;
var isValid = false;
if (isString(arg)) {
var upper = arg.toUpperCase();
if (upper === 'TRUE' || upper === 'YES' || upper === 'ON' || upper === '1') {
value = true;
isValid = true;
} else if (upper === 'FALSE' || upper === 'NO' || upper === 'OFF' || upper === '0') {
value = false;
isValid = true;
}
} else if (isNumber(arg)) {
if (arg === 1) {
value = true;
isValid = true;
} else if (arg === 0) {
value = false;
isValid = true;
}
} else if (arg === true) {
value = true;
isValid = true;
} else if (arg === false) {
value = false;
isValid = true;
}
return { value: value, isValid: isValid };
}
exports.validateAndGetBoolProperty = validateAndGetBoolProperty;