UNPKG

733 BJavaScriptView Raw
1'use strict';
2const fs = require('fs');
3const util = require('util');
4const is = require('@sindresorhus/is');
5const isFormData = require('./is-form-data');
6
7module.exports = async options => {
8 const {body} = options;
9
10 if (options.headers['content-length']) {
11 return Number(options.headers['content-length']);
12 }
13
14 if (!body && !options.stream) {
15 return 0;
16 }
17
18 if (is.string(body)) {
19 return Buffer.byteLength(body);
20 }
21
22 if (isFormData(body)) {
23 return util.promisify(body.getLength.bind(body))();
24 }
25
26 if (body instanceof fs.ReadStream) {
27 const {size} = await util.promisify(fs.stat)(body.path);
28 return size;
29 }
30
31 if (is.nodeStream(body) && is.buffer(body._buffer)) {
32 return body._buffer.length;
33 }
34
35 return null;
36};