Skip to content

Commit 8641da2

Browse files
author
Hyunje Jun
authored
Set Content-Length manually for postBinary (#42)
* Add test case to check content-length for postBinary * Set Content-Type for postBinary properly with stream
1 parent b381b8f commit 8641da2

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

lib/http.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { Readable, PassThrough } from "stream";
33
import { HTTPError, ReadError, RequestError } from "./exceptions";
44
import * as fileType from "file-type";
55

6-
const fileTypeStream = require("file-type-stream").default;
7-
86
const pkg = require("../package.json");
97

108
function wrapError(err: AxiosError) {
@@ -59,27 +57,30 @@ export function postBinary(
5957
data: Buffer | Readable,
6058
contentType?: string,
6159
): Promise<any> {
62-
let contentTypeGetter;
63-
if (contentType) {
64-
contentTypeGetter = Promise.resolve(contentType);
65-
} else if (Buffer.isBuffer(data)) {
66-
contentTypeGetter = Promise.resolve(fileType(data).mime);
60+
let getBuffer: Promise<Buffer>;
61+
62+
if (Buffer.isBuffer(data)) {
63+
getBuffer = Promise.resolve(data);
6764
} else {
68-
contentTypeGetter = new Promise(resolve => {
65+
getBuffer = new Promise((resolve, reject) => {
6966
if (data instanceof Readable) {
70-
const passThrough = new PassThrough();
71-
data
72-
.pipe(fileTypeStream((result: any) => resolve(result.mime)))
73-
.pipe(passThrough);
74-
data = passThrough;
67+
const buffers: Buffer[] = [];
68+
let size = 0;
69+
data.on("data", (chunk: Buffer) => {
70+
buffers.push(chunk);
71+
size += chunk.length;
72+
});
73+
data.on("end", () => resolve(Buffer.concat(buffers, size)));
74+
data.on("error", reject);
7575
} else {
76-
throw new Error("invalid data type for postBinary");
76+
reject(new Error("invalid data type for postBinary"));
7777
}
7878
});
7979
}
8080

81-
return contentTypeGetter.then((contentType: string) => {
82-
headers["Content-Type"] = contentType;
81+
return getBuffer.then(data => {
82+
headers["Content-Type"] = contentType || fileType(data).mime;
83+
headers["Content-Length"] = data.length;
8384
headers["User-Agent"] = userAgent;
8485
return axios
8586
.post(url, data, { headers })

package-lock.json

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
"@types/node": "^7.0.31",
4343
"axios": "^0.16.2",
4444
"body-parser": "^1.18.2",
45-
"file-type": "^7.2.0",
46-
"file-type-stream": "^1.0.0"
45+
"file-type": "^7.2.0"
4746
},
4847
"devDependencies": {
4948
"@types/express": "^4.0.35",

test/http.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ describe("http", () => {
114114
equal(req.headers["test-header-key"], testHeaders["test-header-key"]);
115115
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
116116
equal(req.headers["content-type"], "image/png");
117+
equal(req.headers["content-length"], buffer.length);
117118
},
118119
);
119120
});
@@ -126,6 +127,7 @@ describe("http", () => {
126127
const req = getRecentReq();
127128
equal(req.body, buffer.toString("base64"));
128129
equal(req.headers["content-type"], "image/jpeg");
130+
equal(req.headers["content-length"], buffer.length);
129131
},
130132
);
131133
});
@@ -134,9 +136,12 @@ describe("http", () => {
134136
const filepath = join(__dirname, "/helpers/line-icon.png");
135137
const stream = createReadStream(filepath);
136138
return postBinary(`${TEST_URL}/post/binary`, {}, stream).then(() => {
139+
const buffer = readFileSync(filepath);
140+
137141
const req = getRecentReq();
138-
equal(req.body, readFileSync(filepath).toString("base64"));
142+
equal(req.body, buffer.toString("base64"));
139143
equal(req.headers["content-type"], "image/png");
144+
equal(req.headers["content-length"], buffer.length);
140145
});
141146
});
142147

0 commit comments

Comments
 (0)