Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit f8e31cc

Browse files
committed
Prevent Readable streams from being re-used.
1 parent 7cdd0c8 commit f8e31cc

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ Every open-api method has the following signature:
5151

5252
Perform a call to the given endpoint corresponding with the `operationId`. Returns promise that will resolve with the body of the response, or reject with an error with details about the request attached. Rejects if the `status` > 400. Successful response objects have `status` and `statusText` properties on their prototype.
5353

54-
- `params` is an object that includes any of the required or optional endpoint parameters.
55-
- `params.body` should be an object which gets serialized to JSON automatically.
56-
- If the endpoint accepts `binary`, `params.body` can be a Node.js readable stream.
54+
- `params` is an object that includes any of the required or optional endpoint parameters.
55+
- `params.body` should be an object which gets serialized to JSON automatically.
56+
- If the endpoint accepts `binary`, `params.body` can be a Node.js readable stream or stream ctor (e.g. `() => fs.createReadStream('./foo')`). A Stream ctor function is required to support rate limit retry.
5757

5858
```js
5959
// example params
@@ -125,7 +125,7 @@ async function login () {
125125
// Open browser for authentication
126126
await openBrowser(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
127127
const accessToken = await api.getAccessToken(ticket)
128-
// API is also set up to use the returned access token as a side effect
128+
// API is also set up to use the returned access token as a side effect
129129
return accessToken // Save this for later so you can quickly set up an authenticated client
130130
}
131131
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"folder-walker": "^3.2.0",
4444
"from2-array": "0.0.4",
4545
"hasha": "^3.0.0",
46+
"is-stream": "^1.1.0",
4647
"lodash.camelcase": "^4.3.0",
4748
"lodash.flatten": "^4.4.0",
4849
"lodash.get": "^4.4.2",

src/open-api/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const camelCase = require('lodash.camelcase')
88
const { JSONHTTPError, TextHTTPError } = require('micro-api-client')
99
const debug = require('debug')('netlify:open-api')
1010
const { existy, sleep, unixNow } = require('./util')
11+
const isStream = require('is-stream')
1112

1213
exports.methods = require('./shape-swagger')
1314

@@ -93,6 +94,10 @@ exports.generateMethod = method => {
9394
try {
9495
debug(`requesting ${path}`)
9596

97+
if (isStream(opts.body) && opts.body.closed) {
98+
throw new Error('Body readable stream is already used. Try passing a stream ctor instead')
99+
}
100+
96101
// Pass in a function for readable stream ctors
97102
const fetchOpts = typeof opts.body === 'function' ? Object.assign({}, opts, { body: opts.body() }) : opts
98103

0 commit comments

Comments
 (0)