Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jul 22, 2021
1 parent 6ea0d6d commit b43b2da
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions lib/api/api-request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { Duplex } = require('stream')
const { Readable } = require('stream')
const {
InvalidArgumentError,
RequestAbortedError
Expand All @@ -15,33 +15,20 @@ const kResume = Symbol('resume')
const kDestroy = Symbol('destroy')
const kPush = Symbol('push')
const kBody = Symbol('body')
const kUsed = Symbol('state')
const kReadableDidRead = Symbol('readableDidRead')

// This is needed for pre node 17.
class BodyDuplex extends Duplex {
constructor (options) {
super(options)

// https://github.com/nodejs/node/pull/34385

if (options?.readable === false) {
this._readableState.readable = false
this._readableState.ended = true
this._readableState.endEmitted = true
}

if (options?.writable === false) {
this._writableState.writable = false
this._writableState.ending = true
this._writableState.ended = true
this._writableState.finished = true
}
}
}
class RequestDuplex extends BodyDuplex {
class RequestBody extends Readable {
constructor (resume, abort) {
super({ autoDestroy: true, read: resume, writable: false })
this[kAbort] = abort
this[kReadableDidRead] = false

if (typeof this.readableDidRead !== 'boolean') {
this.pause()
this.once('data', function () {
this[kReadableDidRead] = true
})
}
}

_destroy (err, callback) {
Expand All @@ -61,8 +48,7 @@ class Body {
constructor (resume, abort) {
this[kAbort] = abort
this[kResume] = resume
this[kBody] = new RequestDuplex(this[kResume], this[kAbort]).on('error', () => {})
this[kUsed] = false
this[kBody] = new RequestBody(this[kResume], this[kAbort]).on('error', () => {})
}

[kPush] (chunk) {
Expand All @@ -74,15 +60,15 @@ class Body {
}

get stream () {
if (this[kBody].readableDidRead) {
if (this.bodyUsed) {
throw new TypeError('disturbed')
}

return this[kBody]
}

get bodyUsed () {
return this[kBody].readableDidRead
return this[kBody].readableDidRead || this[kBody][kReadableDidRead]
}

get body () {
Expand Down

0 comments on commit b43b2da

Please sign in to comment.