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: revert "feat: http upload/download progress handlers" #58

Merged
merged 1 commit into from
Aug 18, 2020
Merged
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
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"dist"
],
"browser": {
"./src/http/fetch.js": "./src/http/fetch.browser.js",
"./src/text-encoder.js": "./src/text-encoder.browser.js",
"./src/text-decoder.js": "./src/text-decoder.browser.js",
"./src/temp-dir.js": "./src/temp-dir.browser.js",
Expand Down Expand Up @@ -45,15 +44,15 @@
"merge-options": "^2.0.0",
"nanoid": "^3.1.3",
"node-fetch": "^2.6.0",
"stream-to-it": "^0.2.0",
"it-to-stream": "^0.1.2"
"stream-to-it": "^0.2.0"
},
"devDependencies": {
"aegir": "^25.0.0",
"delay": "^4.3.0",
"it-all": "^1.0.2",
"it-drain": "^1.0.1",
"it-last": "^1.0.2"
"it-last": "^1.0.2",
"it-to-stream": "^0.1.2"
},
"contributors": [
"Hugo Dias <[email protected]>",
Expand Down
23 changes: 19 additions & 4 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
/* eslint-disable no-undef */
'use strict'

const { fetch, Request, Headers } = require('./http/fetch')
const { TimeoutError, HTTPError } = require('./http/error')
const fetch = require('node-fetch')
const merge = require('merge-options').bind({ ignoreUndefined: true })
const { URL, URLSearchParams } = require('iso-url')
const TextDecoder = require('./text-decoder')
const AbortController = require('abort-controller')
const anySignal = require('any-signal')

const Request = fetch.Request
const Headers = fetch.Headers

class TimeoutError extends Error {
constructor () {
super('Request timed out')
this.name = 'TimeoutError'
}
}

class HTTPError extends Error {
constructor (response) {
super(response.statusText)
this.name = 'HTTPError'
this.response = response
}
}

const timeout = (promise, ms, abortController) => {
if (ms === undefined) {
return promise
Expand Down Expand Up @@ -70,8 +87,6 @@ const defaults = {
* @prop {function(URLSearchParams): URLSearchParams } [transformSearchParams]
* @prop {function(any): any} [transform] - When iterating the response body, transform each chunk with this function.
* @prop {function(Response): Promise<void>} [handleError] - Handle errors
* @prop {function({total:number, loaded:number, lengthComputable:boolean}):void} [onUploadProgress] - Can be passed to track upload progress
* @prop {function({total:number, loaded:number, lengthComputable:boolean}):void} [onDownloadProgress] - Can be passed to track download progress
*/

class HTTP {
Expand Down
26 changes: 0 additions & 26 deletions src/http/error.js

This file was deleted.

124 changes: 0 additions & 124 deletions src/http/fetch.browser.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/http/fetch.js

This file was deleted.

133 changes: 0 additions & 133 deletions src/http/fetch.node.js

This file was deleted.

25 changes: 0 additions & 25 deletions test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,4 @@ describe('http', function () {

await expect(drain(HTTP.ndjson(res.body))).to.eventually.be.rejectedWith(/aborted/)
})

it('progress events', async () => {
let upload = 0
let download = 0
const body = new Uint8Array(1000000 / 2)
const request = await HTTP.post(`${process.env.ECHO_SERVER}/echo`, {
body,
onUploadProgress: (progress) => {
expect(progress).to.have.property('lengthComputable').to.be.a('boolean')
expect(progress).to.have.property('total', body.byteLength)
expect(progress).to.have.property('loaded').to.be.a('number')
upload += 1
},
onDownloadProgress: (progress) => {
expect(progress).to.have.property('lengthComputable').to.be.a('boolean')
expect(progress).to.have.property('total').to.be.a('number')
expect(progress).to.have.property('loaded').to.be.a('number')
download += 1
}
})
await all(request.iterator())

expect(upload).to.be.greaterThan(0)
expect(download).to.be.greaterThan(0)
})
})