• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3var jsonSafeStringify = require('json-stringify-safe')
4var crypto = require('crypto')
5var Buffer = require('safe-buffer').Buffer
6
7var defer = typeof setImmediate === 'undefined'
8  ? process.nextTick
9  : setImmediate
10
11function paramsHaveRequestBody (params) {
12  return (
13    params.body ||
14    params.requestBodyStream ||
15    (params.json && typeof params.json !== 'boolean') ||
16    params.multipart
17  )
18}
19
20function 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
30function md5 (str) {
31  return crypto.createHash('md5').update(str).digest('hex')
32}
33
34function isReadStream (rs) {
35  return rs.readable && rs.path && rs.mode
36}
37
38function toBase64 (str) {
39  return Buffer.from(str || '', 'utf8').toString('base64')
40}
41
42function copy (obj) {
43  var o = {}
44  Object.keys(obj).forEach(function (i) {
45    o[i] = obj[i]
46  })
47  return o
48}
49
50function 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
59exports.paramsHaveRequestBody = paramsHaveRequestBody
60exports.safeStringify = safeStringify
61exports.md5 = md5
62exports.isReadStream = isReadStream
63exports.toBase64 = toBase64
64exports.copy = copy
65exports.version = version
66exports.defer = defer
67