UNPKG

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