Skip to content

Commit

Permalink
Throw proper error when passing a non-readable stream
Browse files Browse the repository at this point in the history
Fixes #92
  • Loading branch information
mifi committed Jan 29, 2021
1 parent 9c0b288 commit f1eef81
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"localtunnel": "^2.0.0",
"nock": "^13.0.5",
"p-retry": "^4.2.0",
"request": "^2.88.2",
"temp": "^0.9.1",
"tsd": "^0.14.0"
},
Expand Down
16 changes: 12 additions & 4 deletions src/Transloadit.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,18 @@ class TransloaditClient {
}

// Convert uploads to streams
const streamsMap = fromPairs(Object.entries(uploads).map(([label, value]) => [
label,
isStream.readable(value) ? value : intoStream(value),
]))
const streamsMap = fromPairs(Object.entries(uploads).map(([label, value]) => {
const isReadable = isStream.readable(value)
if (!isReadable && isStream(value)) {
// https://github.com/transloadit/node-sdk/issues/92
throw new Error(`Upload named "${label}" is not a Readable stream`)
}

return [
label,
isStream.readable(value) ? value : intoStream(value),
]
}))

// Wrap in object structure (so we can know if it's a pathless stream or not)
const allStreamsMap = fromPairs(Object.entries(streamsMap).map(([label, stream]) => [label, { stream }]))
Expand Down
10 changes: 10 additions & 0 deletions test/integration/__tests__/live-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { pipeline: streamPipeline } = require('stream')
const got = require('got')
const pipeline = promisify(streamPipeline)
const intoStream = require('into-stream')
const request = require('request')

const Transloadit = require('../../../src/Transloadit')

Expand Down Expand Up @@ -260,6 +261,15 @@ describe('API integration', function () {
})
})

it('should throw a proper error for request stream', async () => {
const client = createClient()

const req = request(genericImg)

const promise = client.createAssembly({ uploads: { file: req } })
await expect(promise).rejects.toThrow(expect.objectContaining({ message: 'Upload named "file" is not a Readable stream' }))
})

async function testUploadProgress (isResumable) {
const client = createClient()

Expand Down

0 comments on commit f1eef81

Please sign in to comment.