Skip to content

Commit cf68bed

Browse files
authored
Merge pull request titel-media#3 from dollarshaveclub/use-byte-readable-stream
Support BYOB ReadableStream
1 parent 68995f3 commit cf68bed

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/body.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ function readableNodeToWeb(nodeStream, instance) {
5858
// TODO: Should we only do Buffer.from() if chunk is a UInt8Array?
5959
// Potentially it makes more sense for down-stream consumers of fetch to cast to Buffer, instead?
6060
// if(isUInt8Array(chunk)) {
61-
controller.enqueue(Buffer.from(chunk));
61+
controller.enqueue(new Uint8Array(Buffer.from(chunk)));
6262

6363
// HELP WANTED: The node-web-streams package pauses the nodeStream here, however,
6464
// if we do that, then it gets permanently paused. Why?
6565
// nodeStream.pause();
6666
});
67-
nodeStream.on('end', () => controller.close());
67+
nodeStream.on('end', () => {
68+
controller.close();
69+
70+
const pending = controller.byobRequest;
71+
if (pending) {
72+
pending.respond(0);
73+
}
74+
});
6875
nodeStream.on('error', (err) => {
6976
controller.error(new FetchError(`Invalid response body while trying to fetch ${instance.url}: ${err.message}`, 'system', err))
7077
});
@@ -74,7 +81,8 @@ function readableNodeToWeb(nodeStream, instance) {
7481
},
7582
cancel(reason) {
7683
nodeStream.pause();
77-
}
84+
},
85+
type: "bytes"
7886
});
7987
}
8088

@@ -92,7 +100,7 @@ export function createReadableStream(instance) {
92100
// TODO: Should we only do Buffer.from() if chunk is a UInt8Array?
93101
// Potentially it makes more sense for down-stream consumers of fetch to cast to Buffer, instead?
94102
// if(isUInt8Array(chunk)) {
95-
controller.enqueue(Buffer.from(chunk))
103+
controller.enqueue(new Uint8Array(Buffer.from(chunk)));
96104
}
97105
}));
98106
}
@@ -107,46 +115,47 @@ export function createReadableStream(instance) {
107115
switch (bodyType) {
108116
case "String":
109117
// body is a string:
110-
controller.enqueue(Buffer.from(body));
118+
controller.enqueue(new Uint8Array(Buffer.from(body)));
111119
controller.close();
112120
break;
113121
case "URLSearchParams":
114122
// body is a URLSearchParams
115-
controller.enqueue(Buffer.from(body.toString()));
123+
controller.enqueue(new Uint8Array(Buffer.from(body.toString())));
116124
controller.close();
117125
break;
118126
case "Blob":
119127
// body is blob
120-
controller.enqueue(Buffer.from(body[BUFFER]));
128+
controller.enqueue(new Uint8Array(Buffer.from(body[BUFFER])));
121129
controller.close();
122130
break;
123131
case "Buffer":
124132
// body is Buffer
125-
controller.enqueue(Buffer.from(body))
133+
controller.enqueue(new Uint8Array(Buffer.from(body)));
126134
controller.close();
127135
break;
128136
case "ArrayBuffer":
129137
// body is ArrayBuffer
130-
controller.enqueue(Buffer.from(body))
138+
controller.enqueue(new Uint8Array(Buffer.from(body)));
131139
controller.close();
132140
break;
133141
case "ArrayBufferView":
134142
// body is ArrayBufferView
135-
controller.enqueue(Buffer.from(body.buffer))
143+
controller.enqueue(new Uint8Array(Buffer.from(body.buffer)));
136144
controller.close();
137145
break;
138146
case "FormData":
139-
controller.enqueue(Buffer.from(body.toString()));
147+
controller.enqueue(new Uint8Array(Buffer.from(body.toString())));
140148
controller.close();
141149
break;
142150
case "other":
143-
controller.enqueue(Buffer.from(String(body)));
151+
controller.enqueue(new Uint8Array(Buffer.from(String(body))));
144152
controller.close();
145153
break;
146154
default:
147155
throw new Error("createReadableStream received an instance body that getTypeOfBody could not understand");
148156
}
149-
}
157+
},
158+
type: "bytes"
150159
});
151160

152161
return readable;

0 commit comments

Comments
 (0)