Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle non-printable characters in browser responses #57

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"delay": "^4.3.0",
"it-all": "^1.0.2",
"it-drain": "^1.0.1",
"it-last": "^1.0.2"
"it-last": "^1.0.2",
"uint8arrays": "^1.1.0"
},
"contributors": [
"Hugo Dias <[email protected]>",
Expand Down
2 changes: 2 additions & 0 deletions src/http/fetch.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const fetch = (url, options = {}) => {
request.upload.onprogress = options.onUploadProgress
}

request.responseType = 'arraybuffer'

return new Promise((resolve, reject) => {
/**
* @param {Event} event
Expand Down
11 changes: 11 additions & 0 deletions test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const drain = require('it-drain')
const all = require('it-all')
const { isBrowser, isWebWorker } = require('../src/env')
const { Buffer } = require('buffer')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayEquals = require('uint8arrays/equals')

describe('http', function () {
it('makes a GET request', async function () {
Expand Down Expand Up @@ -175,4 +177,13 @@ describe('http', function () {
expect(upload).to.be.greaterThan(0)
expect(download).to.be.greaterThan(0)
})

it('makes a GET request with unprintable characters', async function () {
const buf = uint8ArrayFromString('a163666f6f6c6461672d63626f722d626172', 'base16')
const params = Array.from(buf).map(val => `data=${val.toString()}`).join('&')

const req = await HTTP.get(`${process.env.ECHO_SERVER}/download?${params}`)
const rsp = await req.arrayBuffer()
expect(uint8ArrayEquals(new Uint8Array(rsp), buf)).to.be.true()
})
})