Skip to content

Commit

Permalink
fix(body): pass stream to original Body
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgw committed Aug 28, 2019
1 parent ede18b8 commit aa8a491
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const exec = require('child_process').execFileSync
const path = require('path')
const Stream = require('stream')
const _fetch = require('node-fetch')

function fetch (resource, init) {
Expand Down Expand Up @@ -61,16 +62,18 @@ const _checkBody = Symbol('checkBody')

class Request extends _fetch.Request {
constructor (resource, init, bodyError) {
let buffer
if (init) {
init = { ...init }
if (init.body) {
init.body = parseBody(init.body)
buffer = parseBody(init.body)
init.body = createStream(buffer)
}
}

super(resource, init)

defineBuffer(this, init && init.body)
defineBuffer(this, buffer)
if (bodyError) defineBodyError(this, bodyError)
}

Expand Down Expand Up @@ -119,7 +122,7 @@ class Request extends _fetch.Request {
class Response extends _fetch.Response {
constructor (body, init, bodyError) {
const buffer = parseBody(body)
super(buffer, init)
super(createStream(buffer), init)
defineBuffer(this, buffer)
if (bodyError) defineBodyError(this, bodyError)
}
Expand Down Expand Up @@ -219,6 +222,15 @@ function parseBody (body, type = parseBodyType(body)) {
}
}

function createStream (buffer) {
return new Stream.Transform({
read () {
this.push(buffer)
this.push(null)
}
})
}

fetch.Headers = _fetch.Headers
fetch.FetchError = _fetch.FetchError
fetch.Request = Request
Expand Down

0 comments on commit aa8a491

Please sign in to comment.