4
4
} from "https://deno.land/std@0.95.0/mime/multipart.ts" ;
5
5
import {
6
6
readableStreamFromIterable ,
7
- readableStreamFromReader ,
8
- readerFromStreamReader ,
9
7
} from "https://deno.land/std@0.95.0/io/streams.ts" ;
10
8
11
9
interface BytesMessage {
@@ -45,7 +43,9 @@ export function streamFromMultipart(
45
43
// ReadableStream.
46
44
const multipartWriter = new MultipartWriter ( {
47
45
write ( buffer : Uint8Array ) : Promise < number > {
48
- channel . push ( { type : "bytes" , buffer } ) ;
46
+ // It is VERY important we close the buffer, there is no guarentee that the writer won't
47
+ // re-use this buffer for subsequent writes.
48
+ channel . push ( { type : "bytes" , buffer : new Uint8Array ( buffer ) } ) ;
49
49
return Promise . resolve ( buffer . length ) ;
50
50
} ,
51
51
} ) ;
@@ -54,7 +54,8 @@ export function streamFromMultipart(
54
54
writerFunction ( multipartWriter )
55
55
. then ( ( ) => {
56
56
try {
57
- multipartWriter . close ( ) ;
57
+ // Close is an async function that still writes, so we must wait for it to complete.
58
+ return multipartWriter . close ( ) ;
58
59
} catch ( _ignored ) {
59
60
// We'll try to close the writer incase the user hasn't, if they have the close function
60
61
// will throw an error we'll just ignore.
@@ -77,12 +78,5 @@ export function streamFromMultipart(
77
78
}
78
79
}
79
80
80
- // Yes I know this looks REALLY stupid, but I was having issues where if we used the potentially
81
- // broken stream we would send the data out of order. This fixes it but I have no idea why. It
82
- // doesn't allocate the entire stream on the heap, so I think this is going to stay for now.
83
- const potentiallyBrokenStream = readableStreamFromIterable ( generator ( ) ) ;
84
- const reader = readerFromStreamReader ( potentiallyBrokenStream . getReader ( ) ) ;
85
- const stream = readableStreamFromReader ( reader ) ;
86
-
87
- return [ stream , multipartWriter . boundary ] ;
81
+ return [ readableStreamFromIterable ( generator ( ) ) , multipartWriter . boundary ] ;
88
82
}
0 commit comments