Skip to content

Commit 0c370c9

Browse files
committed
simplify handling of arraybuffer case and properly handle json data
1 parent 4804d0b commit 0c370c9

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

lib/index.js

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
/* global ArrayBuffer */
4-
53
/**
64
* Node.js `XMLHttpRequest` implementation using `http.request()`.
75
*
@@ -315,13 +313,10 @@ NodeHttpXHR.prototype = Object.create(
315313
case '':
316314
case 'text':
317315
return this._responseText;
318-
case 'arraybuffer':
319316
case 'json':
320-
if (this._response instanceof ArrayBuffer) {
321-
return this._response;
322-
} else if (this._response instanceof Buffer) {
323-
return this._response.buffer;
324-
}
317+
return JSON.parse(this._responseText);
318+
case 'arraybuffer':
319+
return this._response.buffer;
325320
default:
326321
throw new Error('Assertion failed: unsupported response-type: ' + type);
327322
}
@@ -518,18 +513,12 @@ NodeHttpXHR.prototype.send = function (data) {
518513
if (typeof chunk === 'string') {
519514
this._responseText += chunk;
520515
} else if (typeof chunk === 'object') {
521-
if (chunk instanceof Buffer) {
522-
if (this._response) {
523-
this._response = Buffer.concat([this._response, chunk]);
524-
} else {
525-
this._response = chunk;
526-
}
527-
// binary data usually comes in one chunk (no chunky transfer-encoding)
528-
// or at least, that's what we'll support here for now for ArrayBuffer
529-
} else if (chunk instanceof ArrayBuffer) {
530-
this._response = chunk;
516+
if (!(chunk instanceof Buffer)) {
517+
throw new Error('Assertion failed: Response-data should be of `Buffer` type');
518+
}
519+
if (this._response) {
520+
this._response = Buffer.concat([this._response, chunk]);
531521
} else {
532-
// JSON case
533522
this._response = chunk;
534523
}
535524
}

0 commit comments

Comments
 (0)