forked from rahul27458/studynotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil-node.js
More file actions
219 lines (190 loc) · 5.64 KB
/
util-node.js
File metadata and controls
219 lines (190 loc) · 5.64 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
var bytes = require('bytes')
var config = require('./config')
var cp = require('child_process')
var crypto = require('crypto')
var debug = require('debug')('util')
var email = require('./lib/email')
var fs = require('fs')
var htmlParser = require('html-parser')
var truncate = require('html-truncate')
var mkdirp = require('mkdirp')
var nodeUtil = require('util')
var optimist = require('optimist')
var path = require('path')
var posix = require('posix')
var touch = require('touch')
var util = require('./util') // to access the node+browser util fns
var uuid = require('node-uuid')
/**
* Truncate plaintext or HTML.
* @type {function(String, Number): String}
*/
exports.truncate = truncate
/**
* Run the given server, passing in command line options as options.
* @param {function(*)} ServerConstructor
*/
exports.run = function (ServerConstructor) {
// Clone the argv object to avoid interfering with other modules
var opts = util.extend({}, optimist.argv)
// Delete all options that are not explicitly passed in like this:
// node tracker --port 4000 --dbPort 4001
delete opts.$0
delete opts._
exports.upgradeLimits()
exports.downgradeUid()
// Create and start the server
var server = new ServerConstructor(opts, function (err) {
if (err) {
console.error('Error during ' + server.serverName + ' startup. Abort.')
console.error(err.stack)
process.exit(1)
}
})
process.on('uncaughtException', function (err) {
console.error('\nUNCAUGHT EXCEPTION')
console.error(err.stack)
email.notifyOnException({ err: err })
})
}
var MAX_SOCKETS = 10000
exports.downgradeUid = function () {
if (process.platform === 'linux' && config.isProd) {
process.setgid('www-data')
process.setuid('www-data')
debug('downgraded gid (' + process.getgid() + ') uid (' + process.getuid() + ')')
}
}
exports.upgradeLimits = function () {
posix.setrlimit('nofile', { soft: MAX_SOCKETS, hard: MAX_SOCKETS })
var limits = posix.getrlimit('nofile')
debug('upgraded resource limits to ' + limits.soft)
}
/**
* Generates a random UUID.
* @return {string}
*/
exports.uuid = function () {
return uuid.v1()
}
/**
* Recursively and synchronously delete a folder and all its subfolders.
* If the folder does not exist, then do nothing.
*
* @param {string} dirPath
* @param {function(Error)=} cb
*/
exports.rmdirRecursive = function (dirPath, cb) {
cb || (cb = function () {})
// Verify that folder exists
fs.readdir(dirPath, function (err) {
if (err) {
// Not an error if folder does not exist
cb(null)
} else {
// Ensure that directory ends in a trailing slash
if (dirPath[dirPath.length - 1] !== '/') {
dirPath += '/'
}
// Remove the directory
cp.exec('rm -r ' + dirPath, cb)
}
})
}
/**
* Express middleware that logs requests using the "debug" module so that the
* output is hidden by default.
*
* @param {function(*)} debug instance
*/
exports.expressLogger = function (debug) {
return function (req, res, next) {
var status = res.statusCode
var len = parseInt(res.getHeader('Content-Length'), 10)
var color = 32
if (status >= 500) color = 31
else if (status >= 400) color = 33
else if (status >= 300) color = 36
len = isNaN(len)
? ''
: len = ' - ' + bytes(len)
var str = '\x1B[90m' + req.method
+ ' ' + req.originalUrl + ' '
+ '\x1B[' + color + 'm' + res.statusCode
+ ' \x1B[90m'
+ len
+ '\x1B[0m'
debug(str)
next()
}
}
/**
* Manually trigger LiveReload to refresh the browser (during development)
*/
exports.triggerLiveReload = function () {
mkdirp.sync(config.tmp)
touch.sync(path.join(config.tmp, 'reload.txt'))
}
/**
* Escape a string to be included in a regular expression.
*
* From https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
*
* @param {String} str
* @return {String}
*/
exports.escapeRegExp = function (str) {
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1')
}
exports.hitsPerDay = function (hits, date) {
var days = (Date.now() - new Date(date)) / 86400000
days = Math.max(days, 0.00001) // To prevent divide by zero
return Math.round(hits / days)
}
var defaultElementsWhitelist = [
'p', 'br',
'strong', 'b', 'em', 'i', 'u',
'ol', 'ul', 'li',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'div', 'span',
'sub', 'sup'
]
var defaultAttributesWhitelist = [
]
/**
* Sanitize dirty (user-provided) HTML to remove bad html tags. Uses a
* whitelist approach, where only the tags we explicitly allow are kept.
*
* @param {String} html dirty HTML
* @param {Array=} elementsWhitelist elements to keep
* @param {Array=} attributesWhitelist attributes to keep
* @return {String} sanitized HTML
*/
exports.sanitizeHTML = function (html, elementsWhitelist, attributesWhitelist) {
elementsWhitelist || (elementsWhitelist = defaultElementsWhitelist)
attributesWhitelist || (attributesWhitelist = defaultAttributesWhitelist)
var sanitized = htmlParser.sanitize(html, {
elements: function (name) {
return elementsWhitelist.indexOf(name) === -1
},
attributes: function (name) {
return attributesWhitelist.indexOf(name) === -1
},
comments: true,
doctype: true
})
return sanitized
}
exports.randomBytes = function (length, cb) {
if (typeof length === 'function') {
cb = length
length = 20
}
if (!cb) throw new Error('argument cb required')
crypto.randomBytes(length, function (err, buf) {
if (err) return cb(err)
cb(null, buf.toString('hex'))
})
}
// Make `inherits` from node's "util" module available
exports.inherits = nodeUtil.inherits