1 | 'use strict'
|
2 |
|
3 | var jsonSafeStringify = require('json-stringify-safe')
|
4 | , crypto = require('crypto')
|
5 |
|
6 | function deferMethod() {
|
7 | if (typeof setImmediate === 'undefined') {
|
8 | return process.nextTick
|
9 | }
|
10 |
|
11 | return setImmediate
|
12 | }
|
13 |
|
14 | function isFunction(value) {
|
15 | return typeof value === 'function'
|
16 | }
|
17 |
|
18 | function paramsHaveRequestBody(params) {
|
19 | return (
|
20 | params.body ||
|
21 | params.requestBodyStream ||
|
22 | (params.json && typeof params.json !== 'boolean') ||
|
23 | params.multipart
|
24 | )
|
25 | }
|
26 |
|
27 | function safeStringify (obj) {
|
28 | var ret
|
29 | try {
|
30 | ret = JSON.stringify(obj)
|
31 | } catch (e) {
|
32 | ret = jsonSafeStringify(obj)
|
33 | }
|
34 | return ret
|
35 | }
|
36 |
|
37 | function md5 (str) {
|
38 | return crypto.createHash('md5').update(str).digest('hex')
|
39 | }
|
40 |
|
41 | function isReadStream (rs) {
|
42 | return rs.readable && rs.path && rs.mode
|
43 | }
|
44 |
|
45 | function toBase64 (str) {
|
46 | return (new Buffer(str || '', 'utf8')).toString('base64')
|
47 | }
|
48 |
|
49 | function copy (obj) {
|
50 | var o = {}
|
51 | Object.keys(obj).forEach(function (i) {
|
52 | o[i] = obj[i]
|
53 | })
|
54 | return o
|
55 | }
|
56 |
|
57 | function version () {
|
58 | var numbers = process.version.replace('v', '').split('.')
|
59 | return {
|
60 | major: parseInt(numbers[0], 10),
|
61 | minor: parseInt(numbers[1], 10),
|
62 | patch: parseInt(numbers[2], 10)
|
63 | }
|
64 | }
|
65 |
|
66 | exports.isFunction = isFunction
|
67 | exports.paramsHaveRequestBody = paramsHaveRequestBody
|
68 | exports.safeStringify = safeStringify
|
69 | exports.md5 = md5
|
70 | exports.isReadStream = isReadStream
|
71 | exports.toBase64 = toBase64
|
72 | exports.copy = copy
|
73 | exports.version = version
|
74 | exports.defer = deferMethod()
|