Skip to content

Commit f1eef81

Browse files
committed
Throw proper error when passing a non-readable stream
Fixes #92
1 parent 9c0b288 commit f1eef81

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"localtunnel": "^2.0.0",
3838
"nock": "^13.0.5",
3939
"p-retry": "^4.2.0",
40+
"request": "^2.88.2",
4041
"temp": "^0.9.1",
4142
"tsd": "^0.14.0"
4243
},

Diff for: src/Transloadit.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,18 @@ class TransloaditClient {
127127
}
128128

129129
// Convert uploads to streams
130-
const streamsMap = fromPairs(Object.entries(uploads).map(([label, value]) => [
131-
label,
132-
isStream.readable(value) ? value : intoStream(value),
133-
]))
130+
const streamsMap = fromPairs(Object.entries(uploads).map(([label, value]) => {
131+
const isReadable = isStream.readable(value)
132+
if (!isReadable && isStream(value)) {
133+
// https://github.com/transloadit/node-sdk/issues/92
134+
throw new Error(`Upload named "${label}" is not a Readable stream`)
135+
}
136+
137+
return [
138+
label,
139+
isStream.readable(value) ? value : intoStream(value),
140+
]
141+
}))
134142

135143
// Wrap in object structure (so we can know if it's a pathless stream or not)
136144
const allStreamsMap = fromPairs(Object.entries(streamsMap).map(([label, stream]) => [label, { stream }]))

Diff for: test/integration/__tests__/live-api.js

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const { pipeline: streamPipeline } = require('stream')
1515
const got = require('got')
1616
const pipeline = promisify(streamPipeline)
1717
const intoStream = require('into-stream')
18+
const request = require('request')
1819

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

@@ -260,6 +261,15 @@ describe('API integration', function () {
260261
})
261262
})
262263

264+
it('should throw a proper error for request stream', async () => {
265+
const client = createClient()
266+
267+
const req = request(genericImg)
268+
269+
const promise = client.createAssembly({ uploads: { file: req } })
270+
await expect(promise).rejects.toThrow(expect.objectContaining({ message: 'Upload named "file" is not a Readable stream' }))
271+
})
272+
263273
async function testUploadProgress (isResumable) {
264274
const client = createClient()
265275

0 commit comments

Comments
 (0)