|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -/* global ArrayBuffer */ |
4 | | - |
5 | 3 | /** |
6 | 4 | * Node.js `XMLHttpRequest` implementation using `http.request()`. |
7 | 5 | * |
@@ -315,13 +313,10 @@ NodeHttpXHR.prototype = Object.create( |
315 | 313 | case '': |
316 | 314 | case 'text': |
317 | 315 | return this._responseText; |
318 | | - case 'arraybuffer': |
319 | 316 | 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; |
325 | 320 | default: |
326 | 321 | throw new Error('Assertion failed: unsupported response-type: ' + type); |
327 | 322 | } |
@@ -518,18 +513,12 @@ NodeHttpXHR.prototype.send = function (data) { |
518 | 513 | if (typeof chunk === 'string') { |
519 | 514 | this._responseText += chunk; |
520 | 515 | } 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]); |
531 | 521 | } else { |
532 | | - // JSON case |
533 | 522 | this._response = chunk; |
534 | 523 | } |
535 | 524 | } |
|
0 commit comments