zhuha 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of zhuha might be problematic. Click here for more details.
- package/06-02.html +81 -0
- package/06-02.js +72 -0
- package/06-03.js +7 -0
- package/06-04.js +7 -0
- package/AnswersLW5.pdf +0 -0
- package/m0603/m0603.js +30 -0
- package/m0603/node_modules/.package-lock.json +16 -0
- package/m0603/node_modules/nodemailer/.gitattributes +6 -0
- package/m0603/node_modules/nodemailer/.prettierrc.js +8 -0
- package/m0603/node_modules/nodemailer/CHANGELOG.md +725 -0
- package/m0603/node_modules/nodemailer/CODE_OF_CONDUCT.md +76 -0
- package/m0603/node_modules/nodemailer/CONTRIBUTING.md +67 -0
- package/m0603/node_modules/nodemailer/LICENSE +16 -0
- package/m0603/node_modules/nodemailer/README.md +97 -0
- package/m0603/node_modules/nodemailer/SECURITY.txt +22 -0
- package/m0603/node_modules/nodemailer/lib/addressparser/index.js +313 -0
- package/m0603/node_modules/nodemailer/lib/base64/index.js +142 -0
- package/m0603/node_modules/nodemailer/lib/dkim/index.js +251 -0
- package/m0603/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
- package/m0603/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
- package/m0603/node_modules/nodemailer/lib/dkim/sign.js +117 -0
- package/m0603/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
- package/m0603/node_modules/nodemailer/lib/fetch/index.js +274 -0
- package/m0603/node_modules/nodemailer/lib/json-transport/index.js +82 -0
- package/m0603/node_modules/nodemailer/lib/mail-composer/index.js +558 -0
- package/m0603/node_modules/nodemailer/lib/mailer/index.js +427 -0
- package/m0603/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
- package/m0603/node_modules/nodemailer/lib/mime-funcs/index.js +625 -0
- package/m0603/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
- package/m0603/node_modules/nodemailer/lib/mime-node/index.js +1290 -0
- package/m0603/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
- package/m0603/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
- package/m0603/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
- package/m0603/node_modules/nodemailer/lib/nodemailer.js +143 -0
- package/m0603/node_modules/nodemailer/lib/qp/index.js +219 -0
- package/m0603/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
- package/m0603/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
- package/m0603/node_modules/nodemailer/lib/shared/index.js +638 -0
- package/m0603/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
- package/m0603/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
- package/m0603/node_modules/nodemailer/lib/smtp-connection/index.js +1796 -0
- package/m0603/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
- package/m0603/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
- package/m0603/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
- package/m0603/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
- package/m0603/node_modules/nodemailer/lib/well-known/index.js +47 -0
- package/m0603/node_modules/nodemailer/lib/well-known/services.json +286 -0
- package/m0603/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
- package/m0603/node_modules/nodemailer/package.json +46 -0
- package/m0603/node_modules/nodemailer/postinstall.js +101 -0
- package/m0603/package-lock.json +31 -0
- package/m0603/package.json +15 -0
- package/package.json +16 -0
@@ -0,0 +1,638 @@
|
|
1
|
+
/* eslint no-console: 0 */
|
2
|
+
|
3
|
+
'use strict';
|
4
|
+
|
5
|
+
const urllib = require('url');
|
6
|
+
const util = require('util');
|
7
|
+
const fs = require('fs');
|
8
|
+
const nmfetch = require('../fetch');
|
9
|
+
const dns = require('dns');
|
10
|
+
const net = require('net');
|
11
|
+
const os = require('os');
|
12
|
+
|
13
|
+
const DNS_TTL = 5 * 60 * 1000;
|
14
|
+
|
15
|
+
let networkInterfaces;
|
16
|
+
try {
|
17
|
+
networkInterfaces = os.networkInterfaces();
|
18
|
+
} catch (err) {
|
19
|
+
// fails on some systems
|
20
|
+
}
|
21
|
+
|
22
|
+
module.exports.networkInterfaces = networkInterfaces;
|
23
|
+
|
24
|
+
const isFamilySupported = (family, allowInternal) => {
|
25
|
+
let networkInterfaces = module.exports.networkInterfaces;
|
26
|
+
if (!networkInterfaces) {
|
27
|
+
// hope for the best
|
28
|
+
return true;
|
29
|
+
}
|
30
|
+
|
31
|
+
const familySupported =
|
32
|
+
// crux that replaces Object.values(networkInterfaces) as Object.values is not supported in nodejs v6
|
33
|
+
Object.keys(networkInterfaces)
|
34
|
+
.map(key => networkInterfaces[key])
|
35
|
+
// crux that replaces .flat() as it is not supported in older Node versions (v10 and older)
|
36
|
+
.reduce((acc, val) => acc.concat(val), [])
|
37
|
+
.filter(i => !i.internal || allowInternal)
|
38
|
+
.filter(i => i.family === 'IPv' + family || i.family === family).length > 0;
|
39
|
+
|
40
|
+
return familySupported;
|
41
|
+
};
|
42
|
+
|
43
|
+
const resolver = (family, hostname, options, callback) => {
|
44
|
+
options = options || {};
|
45
|
+
const familySupported = isFamilySupported(family, options.allowInternalNetworkInterfaces);
|
46
|
+
|
47
|
+
if (!familySupported) {
|
48
|
+
return callback(null, []);
|
49
|
+
}
|
50
|
+
|
51
|
+
const resolver = dns.Resolver ? new dns.Resolver(options) : dns;
|
52
|
+
resolver['resolve' + family](hostname, (err, addresses) => {
|
53
|
+
if (err) {
|
54
|
+
switch (err.code) {
|
55
|
+
case dns.NODATA:
|
56
|
+
case dns.NOTFOUND:
|
57
|
+
case dns.NOTIMP:
|
58
|
+
case dns.SERVFAIL:
|
59
|
+
case dns.CONNREFUSED:
|
60
|
+
case dns.REFUSED:
|
61
|
+
case 'EAI_AGAIN':
|
62
|
+
return callback(null, []);
|
63
|
+
}
|
64
|
+
return callback(err);
|
65
|
+
}
|
66
|
+
return callback(null, Array.isArray(addresses) ? addresses : [].concat(addresses || []));
|
67
|
+
});
|
68
|
+
};
|
69
|
+
|
70
|
+
const dnsCache = (module.exports.dnsCache = new Map());
|
71
|
+
|
72
|
+
const formatDNSValue = (value, extra) => {
|
73
|
+
if (!value) {
|
74
|
+
return Object.assign({}, extra || {});
|
75
|
+
}
|
76
|
+
|
77
|
+
return Object.assign(
|
78
|
+
{
|
79
|
+
servername: value.servername,
|
80
|
+
host:
|
81
|
+
!value.addresses || !value.addresses.length
|
82
|
+
? null
|
83
|
+
: value.addresses.length === 1
|
84
|
+
? value.addresses[0]
|
85
|
+
: value.addresses[Math.floor(Math.random() * value.addresses.length)]
|
86
|
+
},
|
87
|
+
extra || {}
|
88
|
+
);
|
89
|
+
};
|
90
|
+
|
91
|
+
module.exports.resolveHostname = (options, callback) => {
|
92
|
+
options = options || {};
|
93
|
+
|
94
|
+
if (!options.host && options.servername) {
|
95
|
+
options.host = options.servername;
|
96
|
+
}
|
97
|
+
|
98
|
+
if (!options.host || net.isIP(options.host)) {
|
99
|
+
// nothing to do here
|
100
|
+
let value = {
|
101
|
+
addresses: [options.host],
|
102
|
+
servername: options.servername || false
|
103
|
+
};
|
104
|
+
return callback(
|
105
|
+
null,
|
106
|
+
formatDNSValue(value, {
|
107
|
+
cached: false
|
108
|
+
})
|
109
|
+
);
|
110
|
+
}
|
111
|
+
|
112
|
+
let cached;
|
113
|
+
if (dnsCache.has(options.host)) {
|
114
|
+
cached = dnsCache.get(options.host);
|
115
|
+
|
116
|
+
if (!cached.expires || cached.expires >= Date.now()) {
|
117
|
+
return callback(
|
118
|
+
null,
|
119
|
+
formatDNSValue(cached.value, {
|
120
|
+
cached: true
|
121
|
+
})
|
122
|
+
);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
resolver(4, options.host, options, (err, addresses) => {
|
127
|
+
if (err) {
|
128
|
+
if (cached) {
|
129
|
+
// ignore error, use expired value
|
130
|
+
return callback(
|
131
|
+
null,
|
132
|
+
formatDNSValue(cached.value, {
|
133
|
+
cached: true,
|
134
|
+
error: err
|
135
|
+
})
|
136
|
+
);
|
137
|
+
}
|
138
|
+
return callback(err);
|
139
|
+
}
|
140
|
+
|
141
|
+
if (addresses && addresses.length) {
|
142
|
+
let value = {
|
143
|
+
addresses,
|
144
|
+
servername: options.servername || options.host
|
145
|
+
};
|
146
|
+
|
147
|
+
dnsCache.set(options.host, {
|
148
|
+
value,
|
149
|
+
expires: Date.now() + (options.dnsTtl || DNS_TTL)
|
150
|
+
});
|
151
|
+
|
152
|
+
return callback(
|
153
|
+
null,
|
154
|
+
formatDNSValue(value, {
|
155
|
+
cached: false
|
156
|
+
})
|
157
|
+
);
|
158
|
+
}
|
159
|
+
|
160
|
+
resolver(6, options.host, options, (err, addresses) => {
|
161
|
+
if (err) {
|
162
|
+
if (cached) {
|
163
|
+
// ignore error, use expired value
|
164
|
+
return callback(
|
165
|
+
null,
|
166
|
+
formatDNSValue(cached.value, {
|
167
|
+
cached: true,
|
168
|
+
error: err
|
169
|
+
})
|
170
|
+
);
|
171
|
+
}
|
172
|
+
return callback(err);
|
173
|
+
}
|
174
|
+
|
175
|
+
if (addresses && addresses.length) {
|
176
|
+
let value = {
|
177
|
+
addresses,
|
178
|
+
servername: options.servername || options.host
|
179
|
+
};
|
180
|
+
|
181
|
+
dnsCache.set(options.host, {
|
182
|
+
value,
|
183
|
+
expires: Date.now() + (options.dnsTtl || DNS_TTL)
|
184
|
+
});
|
185
|
+
|
186
|
+
return callback(
|
187
|
+
null,
|
188
|
+
formatDNSValue(value, {
|
189
|
+
cached: false
|
190
|
+
})
|
191
|
+
);
|
192
|
+
}
|
193
|
+
|
194
|
+
try {
|
195
|
+
dns.lookup(options.host, { all: true }, (err, addresses) => {
|
196
|
+
if (err) {
|
197
|
+
if (cached) {
|
198
|
+
// ignore error, use expired value
|
199
|
+
return callback(
|
200
|
+
null,
|
201
|
+
formatDNSValue(cached.value, {
|
202
|
+
cached: true,
|
203
|
+
error: err
|
204
|
+
})
|
205
|
+
);
|
206
|
+
}
|
207
|
+
return callback(err);
|
208
|
+
}
|
209
|
+
|
210
|
+
let address = addresses
|
211
|
+
? addresses
|
212
|
+
.filter(addr => isFamilySupported(addr.family))
|
213
|
+
.map(addr => addr.address)
|
214
|
+
.shift()
|
215
|
+
: false;
|
216
|
+
|
217
|
+
if (addresses && addresses.length && !address) {
|
218
|
+
// there are addresses but none can be used
|
219
|
+
console.warn(`Failed to resolve IPv${addresses[0].family} addresses with current network`);
|
220
|
+
}
|
221
|
+
|
222
|
+
if (!address && cached) {
|
223
|
+
// nothing was found, fallback to cached value
|
224
|
+
return callback(
|
225
|
+
null,
|
226
|
+
formatDNSValue(cached.value, {
|
227
|
+
cached: true
|
228
|
+
})
|
229
|
+
);
|
230
|
+
}
|
231
|
+
|
232
|
+
let value = {
|
233
|
+
addresses: address ? [address] : [options.host],
|
234
|
+
servername: options.servername || options.host
|
235
|
+
};
|
236
|
+
|
237
|
+
dnsCache.set(options.host, {
|
238
|
+
value,
|
239
|
+
expires: Date.now() + (options.dnsTtl || DNS_TTL)
|
240
|
+
});
|
241
|
+
|
242
|
+
return callback(
|
243
|
+
null,
|
244
|
+
formatDNSValue(value, {
|
245
|
+
cached: false
|
246
|
+
})
|
247
|
+
);
|
248
|
+
});
|
249
|
+
} catch (err) {
|
250
|
+
if (cached) {
|
251
|
+
// ignore error, use expired value
|
252
|
+
return callback(
|
253
|
+
null,
|
254
|
+
formatDNSValue(cached.value, {
|
255
|
+
cached: true,
|
256
|
+
error: err
|
257
|
+
})
|
258
|
+
);
|
259
|
+
}
|
260
|
+
return callback(err);
|
261
|
+
}
|
262
|
+
});
|
263
|
+
});
|
264
|
+
};
|
265
|
+
/**
|
266
|
+
* Parses connection url to a structured configuration object
|
267
|
+
*
|
268
|
+
* @param {String} str Connection url
|
269
|
+
* @return {Object} Configuration object
|
270
|
+
*/
|
271
|
+
module.exports.parseConnectionUrl = str => {
|
272
|
+
str = str || '';
|
273
|
+
let options = {};
|
274
|
+
|
275
|
+
[urllib.parse(str, true)].forEach(url => {
|
276
|
+
let auth;
|
277
|
+
|
278
|
+
switch (url.protocol) {
|
279
|
+
case 'smtp:':
|
280
|
+
options.secure = false;
|
281
|
+
break;
|
282
|
+
case 'smtps:':
|
283
|
+
options.secure = true;
|
284
|
+
break;
|
285
|
+
case 'direct:':
|
286
|
+
options.direct = true;
|
287
|
+
break;
|
288
|
+
}
|
289
|
+
|
290
|
+
if (!isNaN(url.port) && Number(url.port)) {
|
291
|
+
options.port = Number(url.port);
|
292
|
+
}
|
293
|
+
|
294
|
+
if (url.hostname) {
|
295
|
+
options.host = url.hostname;
|
296
|
+
}
|
297
|
+
|
298
|
+
if (url.auth) {
|
299
|
+
auth = url.auth.split(':');
|
300
|
+
|
301
|
+
if (!options.auth) {
|
302
|
+
options.auth = {};
|
303
|
+
}
|
304
|
+
|
305
|
+
options.auth.user = auth.shift();
|
306
|
+
options.auth.pass = auth.join(':');
|
307
|
+
}
|
308
|
+
|
309
|
+
Object.keys(url.query || {}).forEach(key => {
|
310
|
+
let obj = options;
|
311
|
+
let lKey = key;
|
312
|
+
let value = url.query[key];
|
313
|
+
|
314
|
+
if (!isNaN(value)) {
|
315
|
+
value = Number(value);
|
316
|
+
}
|
317
|
+
|
318
|
+
switch (value) {
|
319
|
+
case 'true':
|
320
|
+
value = true;
|
321
|
+
break;
|
322
|
+
case 'false':
|
323
|
+
value = false;
|
324
|
+
break;
|
325
|
+
}
|
326
|
+
|
327
|
+
// tls is nested object
|
328
|
+
if (key.indexOf('tls.') === 0) {
|
329
|
+
lKey = key.substr(4);
|
330
|
+
if (!options.tls) {
|
331
|
+
options.tls = {};
|
332
|
+
}
|
333
|
+
obj = options.tls;
|
334
|
+
} else if (key.indexOf('.') >= 0) {
|
335
|
+
// ignore nested properties besides tls
|
336
|
+
return;
|
337
|
+
}
|
338
|
+
|
339
|
+
if (!(lKey in obj)) {
|
340
|
+
obj[lKey] = value;
|
341
|
+
}
|
342
|
+
});
|
343
|
+
});
|
344
|
+
|
345
|
+
return options;
|
346
|
+
};
|
347
|
+
|
348
|
+
module.exports._logFunc = (logger, level, defaults, data, message, ...args) => {
|
349
|
+
let entry = {};
|
350
|
+
|
351
|
+
Object.keys(defaults || {}).forEach(key => {
|
352
|
+
if (key !== 'level') {
|
353
|
+
entry[key] = defaults[key];
|
354
|
+
}
|
355
|
+
});
|
356
|
+
|
357
|
+
Object.keys(data || {}).forEach(key => {
|
358
|
+
if (key !== 'level') {
|
359
|
+
entry[key] = data[key];
|
360
|
+
}
|
361
|
+
});
|
362
|
+
|
363
|
+
logger[level](entry, message, ...args);
|
364
|
+
};
|
365
|
+
|
366
|
+
/**
|
367
|
+
* Returns a bunyan-compatible logger interface. Uses either provided logger or
|
368
|
+
* creates a default console logger
|
369
|
+
*
|
370
|
+
* @param {Object} [options] Options object that might include 'logger' value
|
371
|
+
* @return {Object} bunyan compatible logger
|
372
|
+
*/
|
373
|
+
module.exports.getLogger = (options, defaults) => {
|
374
|
+
options = options || {};
|
375
|
+
|
376
|
+
let response = {};
|
377
|
+
let levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
|
378
|
+
|
379
|
+
if (!options.logger) {
|
380
|
+
// use vanity logger
|
381
|
+
levels.forEach(level => {
|
382
|
+
response[level] = () => false;
|
383
|
+
});
|
384
|
+
return response;
|
385
|
+
}
|
386
|
+
|
387
|
+
let logger = options.logger;
|
388
|
+
|
389
|
+
if (options.logger === true) {
|
390
|
+
// create console logger
|
391
|
+
logger = createDefaultLogger(levels);
|
392
|
+
}
|
393
|
+
|
394
|
+
levels.forEach(level => {
|
395
|
+
response[level] = (data, message, ...args) => {
|
396
|
+
module.exports._logFunc(logger, level, defaults, data, message, ...args);
|
397
|
+
};
|
398
|
+
});
|
399
|
+
|
400
|
+
return response;
|
401
|
+
};
|
402
|
+
|
403
|
+
/**
|
404
|
+
* Wrapper for creating a callback that either resolves or rejects a promise
|
405
|
+
* based on input
|
406
|
+
*
|
407
|
+
* @param {Function} resolve Function to run if callback is called
|
408
|
+
* @param {Function} reject Function to run if callback ends with an error
|
409
|
+
*/
|
410
|
+
module.exports.callbackPromise = (resolve, reject) =>
|
411
|
+
function () {
|
412
|
+
let args = Array.from(arguments);
|
413
|
+
let err = args.shift();
|
414
|
+
if (err) {
|
415
|
+
reject(err);
|
416
|
+
} else {
|
417
|
+
resolve(...args);
|
418
|
+
}
|
419
|
+
};
|
420
|
+
|
421
|
+
/**
|
422
|
+
* Resolves a String or a Buffer value for content value. Useful if the value
|
423
|
+
* is a Stream or a file or an URL. If the value is a Stream, overwrites
|
424
|
+
* the stream object with the resolved value (you can't stream a value twice).
|
425
|
+
*
|
426
|
+
* This is useful when you want to create a plugin that needs a content value,
|
427
|
+
* for example the `html` or `text` value as a String or a Buffer but not as
|
428
|
+
* a file path or an URL.
|
429
|
+
*
|
430
|
+
* @param {Object} data An object or an Array you want to resolve an element for
|
431
|
+
* @param {String|Number} key Property name or an Array index
|
432
|
+
* @param {Function} callback Callback function with (err, value)
|
433
|
+
*/
|
434
|
+
module.exports.resolveContent = (data, key, callback) => {
|
435
|
+
let promise;
|
436
|
+
|
437
|
+
if (!callback) {
|
438
|
+
promise = new Promise((resolve, reject) => {
|
439
|
+
callback = module.exports.callbackPromise(resolve, reject);
|
440
|
+
});
|
441
|
+
}
|
442
|
+
|
443
|
+
let content = (data && data[key] && data[key].content) || data[key];
|
444
|
+
let contentStream;
|
445
|
+
let encoding = ((typeof data[key] === 'object' && data[key].encoding) || 'utf8')
|
446
|
+
.toString()
|
447
|
+
.toLowerCase()
|
448
|
+
.replace(/[-_\s]/g, '');
|
449
|
+
|
450
|
+
if (!content) {
|
451
|
+
return callback(null, content);
|
452
|
+
}
|
453
|
+
|
454
|
+
if (typeof content === 'object') {
|
455
|
+
if (typeof content.pipe === 'function') {
|
456
|
+
return resolveStream(content, (err, value) => {
|
457
|
+
if (err) {
|
458
|
+
return callback(err);
|
459
|
+
}
|
460
|
+
// we can't stream twice the same content, so we need
|
461
|
+
// to replace the stream object with the streaming result
|
462
|
+
if (data[key].content) {
|
463
|
+
data[key].content = value;
|
464
|
+
} else {
|
465
|
+
data[key] = value;
|
466
|
+
}
|
467
|
+
callback(null, value);
|
468
|
+
});
|
469
|
+
} else if (/^https?:\/\//i.test(content.path || content.href)) {
|
470
|
+
contentStream = nmfetch(content.path || content.href);
|
471
|
+
return resolveStream(contentStream, callback);
|
472
|
+
} else if (/^data:/i.test(content.path || content.href)) {
|
473
|
+
let parts = (content.path || content.href).match(/^data:((?:[^;]*;)*(?:[^,]*)),(.*)$/i);
|
474
|
+
if (!parts) {
|
475
|
+
return callback(null, Buffer.from(0));
|
476
|
+
}
|
477
|
+
return callback(null, /\bbase64$/i.test(parts[1]) ? Buffer.from(parts[2], 'base64') : Buffer.from(decodeURIComponent(parts[2])));
|
478
|
+
} else if (content.path) {
|
479
|
+
return resolveStream(fs.createReadStream(content.path), callback);
|
480
|
+
}
|
481
|
+
}
|
482
|
+
|
483
|
+
if (typeof data[key].content === 'string' && !['utf8', 'usascii', 'ascii'].includes(encoding)) {
|
484
|
+
content = Buffer.from(data[key].content, encoding);
|
485
|
+
}
|
486
|
+
|
487
|
+
// default action, return as is
|
488
|
+
setImmediate(() => callback(null, content));
|
489
|
+
|
490
|
+
return promise;
|
491
|
+
};
|
492
|
+
|
493
|
+
/**
|
494
|
+
* Copies properties from source objects to target objects
|
495
|
+
*/
|
496
|
+
module.exports.assign = function (/* target, ... sources */) {
|
497
|
+
let args = Array.from(arguments);
|
498
|
+
let target = args.shift() || {};
|
499
|
+
|
500
|
+
args.forEach(source => {
|
501
|
+
Object.keys(source || {}).forEach(key => {
|
502
|
+
if (['tls', 'auth'].includes(key) && source[key] && typeof source[key] === 'object') {
|
503
|
+
// tls and auth are special keys that need to be enumerated separately
|
504
|
+
// other objects are passed as is
|
505
|
+
if (!target[key]) {
|
506
|
+
// ensure that target has this key
|
507
|
+
target[key] = {};
|
508
|
+
}
|
509
|
+
Object.keys(source[key]).forEach(subKey => {
|
510
|
+
target[key][subKey] = source[key][subKey];
|
511
|
+
});
|
512
|
+
} else {
|
513
|
+
target[key] = source[key];
|
514
|
+
}
|
515
|
+
});
|
516
|
+
});
|
517
|
+
return target;
|
518
|
+
};
|
519
|
+
|
520
|
+
module.exports.encodeXText = str => {
|
521
|
+
// ! 0x21
|
522
|
+
// + 0x2B
|
523
|
+
// = 0x3D
|
524
|
+
// ~ 0x7E
|
525
|
+
if (!/[^\x21-\x2A\x2C-\x3C\x3E-\x7E]/.test(str)) {
|
526
|
+
return str;
|
527
|
+
}
|
528
|
+
let buf = Buffer.from(str);
|
529
|
+
let result = '';
|
530
|
+
for (let i = 0, len = buf.length; i < len; i++) {
|
531
|
+
let c = buf[i];
|
532
|
+
if (c < 0x21 || c > 0x7e || c === 0x2b || c === 0x3d) {
|
533
|
+
result += '+' + (c < 0x10 ? '0' : '') + c.toString(16).toUpperCase();
|
534
|
+
} else {
|
535
|
+
result += String.fromCharCode(c);
|
536
|
+
}
|
537
|
+
}
|
538
|
+
return result;
|
539
|
+
};
|
540
|
+
|
541
|
+
/**
|
542
|
+
* Streams a stream value into a Buffer
|
543
|
+
*
|
544
|
+
* @param {Object} stream Readable stream
|
545
|
+
* @param {Function} callback Callback function with (err, value)
|
546
|
+
*/
|
547
|
+
function resolveStream(stream, callback) {
|
548
|
+
let responded = false;
|
549
|
+
let chunks = [];
|
550
|
+
let chunklen = 0;
|
551
|
+
|
552
|
+
stream.on('error', err => {
|
553
|
+
if (responded) {
|
554
|
+
return;
|
555
|
+
}
|
556
|
+
|
557
|
+
responded = true;
|
558
|
+
callback(err);
|
559
|
+
});
|
560
|
+
|
561
|
+
stream.on('readable', () => {
|
562
|
+
let chunk;
|
563
|
+
while ((chunk = stream.read()) !== null) {
|
564
|
+
chunks.push(chunk);
|
565
|
+
chunklen += chunk.length;
|
566
|
+
}
|
567
|
+
});
|
568
|
+
|
569
|
+
stream.on('end', () => {
|
570
|
+
if (responded) {
|
571
|
+
return;
|
572
|
+
}
|
573
|
+
responded = true;
|
574
|
+
|
575
|
+
let value;
|
576
|
+
|
577
|
+
try {
|
578
|
+
value = Buffer.concat(chunks, chunklen);
|
579
|
+
} catch (E) {
|
580
|
+
return callback(E);
|
581
|
+
}
|
582
|
+
callback(null, value);
|
583
|
+
});
|
584
|
+
}
|
585
|
+
|
586
|
+
/**
|
587
|
+
* Generates a bunyan-like logger that prints to console
|
588
|
+
*
|
589
|
+
* @returns {Object} Bunyan logger instance
|
590
|
+
*/
|
591
|
+
function createDefaultLogger(levels) {
|
592
|
+
let levelMaxLen = 0;
|
593
|
+
let levelNames = new Map();
|
594
|
+
levels.forEach(level => {
|
595
|
+
if (level.length > levelMaxLen) {
|
596
|
+
levelMaxLen = level.length;
|
597
|
+
}
|
598
|
+
});
|
599
|
+
|
600
|
+
levels.forEach(level => {
|
601
|
+
let levelName = level.toUpperCase();
|
602
|
+
if (levelName.length < levelMaxLen) {
|
603
|
+
levelName += ' '.repeat(levelMaxLen - levelName.length);
|
604
|
+
}
|
605
|
+
levelNames.set(level, levelName);
|
606
|
+
});
|
607
|
+
|
608
|
+
let print = (level, entry, message, ...args) => {
|
609
|
+
let prefix = '';
|
610
|
+
if (entry) {
|
611
|
+
if (entry.tnx === 'server') {
|
612
|
+
prefix = 'S: ';
|
613
|
+
} else if (entry.tnx === 'client') {
|
614
|
+
prefix = 'C: ';
|
615
|
+
}
|
616
|
+
|
617
|
+
if (entry.sid) {
|
618
|
+
prefix = '[' + entry.sid + '] ' + prefix;
|
619
|
+
}
|
620
|
+
|
621
|
+
if (entry.cid) {
|
622
|
+
prefix = '[#' + entry.cid + '] ' + prefix;
|
623
|
+
}
|
624
|
+
}
|
625
|
+
|
626
|
+
message = util.format(message, ...args);
|
627
|
+
message.split(/\r?\n/).forEach(line => {
|
628
|
+
console.log('[%s] %s %s', new Date().toISOString().substr(0, 19).replace(/T/, ' '), levelNames.get(level), prefix + line);
|
629
|
+
});
|
630
|
+
};
|
631
|
+
|
632
|
+
let logger = {};
|
633
|
+
levels.forEach(level => {
|
634
|
+
logger[level] = print.bind(null, level);
|
635
|
+
});
|
636
|
+
|
637
|
+
return logger;
|
638
|
+
}
|