Skip to content

Commit 6897fcf

Browse files
try to add streaming
1 parent aba6518 commit 6897fcf

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

kernel/src/services/FirmwareService.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,49 @@ export class FirmwareService {
234234
});
235235

236236
console.log('Result:', result);
237+
238+
// Now we check the result to see if it's ok and then we can actually read from the body.
239+
if (!result.ok) {
240+
console.log("Error fetching firmware:", result.status, result.statusText);
241+
}
242+
243+
// stream the response
244+
const reader = result.body?.getReader();
245+
if (!reader) {
246+
throw new Error('Failed to get reader from response body.');
247+
}
248+
249+
let receivedLength = 0; // received bytes
250+
const chunks: Uint8Array[] = []; // chunks of received data
251+
252+
while (true) {
253+
const { done, value } = await reader.read();
254+
if (done) {
255+
break;
256+
}
257+
chunks.push(value);
258+
receivedLength += value.length;
259+
console.log(`Received ${receivedLength} bytes`);
260+
}
261+
262+
console.log('All chunks received:', chunks);
263+
264+
// Combine all chunks into a single Uint8Array
265+
const chunksAll = new Uint8Array(receivedLength);
266+
let position = 0;
267+
for (const chunk of chunks) {
268+
chunksAll.set(chunk, position); // copy chunk to the final array
269+
position += chunk.length; // update position
270+
}
271+
console.log('All chunks combined:', chunksAll);
272+
273+
// Convert the Uint8Array to a string
274+
const firmwareString = Array.from(chunksAll)
275+
.map(byte => String.fromCharCode(byte))
276+
.join('');
277+
278+
console.log('Firmware string:', firmwareString);
279+
237280
throw new Error('Purposeful error out during testing.'); // TODO: Remove this line when done testing.
238281

239282
// if (!result.ok) {

0 commit comments

Comments
 (0)