Skip to content

Commit 71d7e3a

Browse files
committed
fix: handle non-printable characters in browser responses
Fixes a regression introduced by #54 - in the browser by default the response body is interpreted as a string which can become corrupted when non-printable characters are encountered.
1 parent 35f8f70 commit 71d7e3a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"delay": "^4.3.0",
5454
"it-all": "^1.0.2",
5555
"it-drain": "^1.0.1",
56-
"it-last": "^1.0.2"
56+
"it-last": "^1.0.2",
57+
"uint8arrays": "^1.1.0"
5758
},
5859
"contributors": [
5960
"Hugo Dias <hugomrdias@gmail.com>",

src/http/fetch.browser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const fetch = (url, options = {}) => {
5050
request.upload.onprogress = options.onUploadProgress
5151
}
5252

53+
request.responseType = 'arraybuffer'
54+
5355
return new Promise((resolve, reject) => {
5456
/**
5557
* @param {Event} event

test/http.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const drain = require('it-drain')
1010
const all = require('it-all')
1111
const { isBrowser, isWebWorker } = require('../src/env')
1212
const { Buffer } = require('buffer')
13+
const uint8ArrayFromString = require('uint8arrays/from-string')
14+
const uint8ArrayEquals = require('uint8arrays/equals')
1315

1416
describe('http', function () {
1517
it('makes a GET request', async function () {
@@ -175,4 +177,13 @@ describe('http', function () {
175177
expect(upload).to.be.greaterThan(0)
176178
expect(download).to.be.greaterThan(0)
177179
})
180+
181+
it('makes a GET request with unprintable characters', async function () {
182+
const buf = uint8ArrayFromString('a163666f6f6c6461672d63626f722d626172', 'base16')
183+
const params = Array.from(buf).map(val => `data=${val.toString()}`).join('&')
184+
185+
const req = await HTTP.get(`${process.env.ECHO_SERVER}/download?${params}`)
186+
const rsp = await req.arrayBuffer()
187+
expect(uint8ArrayEquals(new Uint8Array(rsp), buf)).to.be.true()
188+
})
178189
})

0 commit comments

Comments
 (0)