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