Skip to content

Commit 6a81e19

Browse files
committed
Add a test with ReadableStream
1 parent 3335b39 commit 6a81e19

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

lib/Encoder.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Readable} from "stream"
33

44
import test from "ava"
55

6+
import {ReadableStream} from "web-streams-polyfill/ponyfill/es2018"
67
import {FormData, File, fileFromPath} from "formdata-node"
78

89
import readStream from "./__helper__/readStream"
@@ -173,6 +174,32 @@ test("Yields File's content", async t => {
173174
t.is<string>(chunks.join("\n"), expected)
174175
})
175176

177+
test("Can be used with ReadableStream", async t => {
178+
const fd = new FormData()
179+
180+
fd.set("field", "Some field value")
181+
182+
const encoder = new Encoder(fd)
183+
const iterable = encoder.encode()
184+
185+
const readable = new ReadableStream<Uint8Array>({
186+
async pull(controller) {
187+
const {value, done} = await iterable.next()
188+
189+
if (done) {
190+
return controller.close()
191+
}
192+
193+
controller.enqueue(value as Uint8Array)
194+
}
195+
})
196+
197+
const expected = await readStream(encoder)
198+
const actual = await readStream(readable)
199+
200+
t.true(actual.equals(expected))
201+
})
202+
176203
test(
177204
"Throws TypeError when the first argument is not a FormData instance",
178205
t => {

lib/__helper__/readStream.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {Readable} from "stream"
22

3-
type Input = Readable | {
3+
import {ReadableStream} from "web-streams-polyfill/ponyfill/es2018"
4+
5+
type Input = Readable | ReadableStream | {
46
[Symbol.asyncIterator](): AsyncIterableIterator<any>
57
}
68

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"pinst": "2.1.6",
5757
"ts-node": "9.1.1",
5858
"ttypescript": "1.5.12",
59-
"typescript": "4.2.4"
59+
"typescript": "4.2.4",
60+
"web-streams-polyfill": "^3.0.3"
6061
},
6162
"dependencies": {
6263
"mime-types": "2.1.30"

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)