Skip to content

Commit 525d260

Browse files
Switch back to old way of requesting. (We are going to switch to a sparkfun repo to try to get around CORS rejections).
1 parent 4a94ca9 commit 525d260

File tree

1 file changed

+70
-69
lines changed

1 file changed

+70
-69
lines changed

kernel/src/services/FirmwareService.ts

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { DeviceService } from './DeviceService';
44
// const { Octokit } = require('@octokit/rest');
55

66
export class FirmwareService {
7-
// private firmwareString: string | null = null;
8-
// private firmwareBlob: Blob | null = null;
7+
private firmwareString: string | null = null;
8+
private firmwareBlob: Blob | null = null;
99
private selectedFirmwareId: string = 'Auto';
1010
// Should make firmwareOptions a private variable and use getters to access it.
1111
// This way we can add logic to the getters if we need to in the future. Default to empty record.
@@ -76,15 +76,15 @@ export class FirmwareService {
7676
// https://api.github.com/repos/OWNER/REPO/releases/assets/ASSET_ID
7777

7878
//method 1: use the browser_download_url from the asset object.
79-
//const firmwareUrl = asset.browser_download_url;
79+
const firmwareUrl = asset.browser_download_url;
8080

8181
//method 2: use the asset ID to formulate a GET request to the GitHub API with octokit.
8282
// Instead let's use the asset ID to formulate an GitHub API URL.
8383
// We'll still use the 'URL' var for now but it's actually a formatted GET req
8484
//const firmwareUrl = 'GET /repos/sparkfun/micropython/releases/assets/' + asset.id;
8585

8686
//method 3: use the asset ID to formulate a direct url to fetch the asset from with similar method as curl.
87-
const firmwareUrl = `https://api.github.com/repos/sparkfun/micropython/releases/assets/${asset.id}`;
87+
// const firmwareUrl = `https://api.github.com/repos/sparkfun/micropython/releases/assets/${asset.id}`;
8888

8989
// log the asset object
9090
console.log('Asset:', asset);
@@ -137,8 +137,8 @@ export class FirmwareService {
137137
setSelectedFirmwareId(id: string): void {
138138
if (id in this.firmwareOptions) {
139139
this.selectedFirmwareId = id;
140-
// this.firmwareString = null;
141-
// this.firmwareBlob = null;
140+
this.firmwareString = null;
141+
this.firmwareBlob = null;
142142
localStorage.removeItem('cachedFirmware');
143143
localStorage.setItem('selectedFirmwareId', id);
144144
}
@@ -192,13 +192,12 @@ export class FirmwareService {
192192
console.log("Performing fetch for firmware:", selectedFirmware.url);
193193

194194
// method 1: browser_download_url from the asset object.
195-
// const result = await fetch(selectedFirmware.url, {
196-
// mode: 'cors',
197-
// headers: {
198-
// 'Accept': 'application/octet-stream',
199-
// },
200-
// method: 'GET'
201-
// });
195+
const result = await fetch(selectedFirmware.url, {
196+
mode: 'cors',
197+
headers: {
198+
'Accept': 'application/octet-stream',
199+
},
200+
});
202201

203202
// console.log('Firmware fetch result:', result);
204203

@@ -225,74 +224,76 @@ export class FirmwareService {
225224
// 'X-GitHub-Api-Version': '2022-11-28',
226225
// });
227226

228-
const result = await fetch(selectedFirmware.url, {
229-
headers:{
230-
// 'Accept': 'application/vnd.github+json',
231-
'Accept': 'application/octet-stream',
232-
'X-GitHub-Api-Version': '2022-11-28',
233-
// 'Authorization': `Bearer ${import.meta.env.VITE_GITHUB_TOKEN}` // Use your GitHub token here.
234-
}
235-
});
236-
237-
console.log('Result:', result);
238-
239-
// Now we check the result to see if it's ok and then we can actually read from the body.
240-
if (!result.ok) {
241-
console.log("Error fetching firmware:", result.status, result.statusText);
242-
}
243-
244-
// stream the response
245-
const reader = result.body?.getReader();
246-
if (!reader) {
247-
throw new Error('Failed to get reader from response body.');
248-
}
227+
// Since we are getting cors errors,
228+
// const result = await fetch(selectedFirmware.url, {
229+
// headers:{
230+
// // 'Accept': 'application/vnd.github+json',
231+
// 'Accept': 'application/octet-stream',
232+
// 'X-GitHub-Api-Version': '2022-11-28',
233+
// // 'Authorization': `Bearer ${import.meta.env.VITE_GITHUB_TOKEN}` // Use your GitHub token here.
234+
// }
235+
// });
249236

250-
let receivedLength = 0; // received bytes
251-
const chunks: Uint8Array[] = []; // chunks of received data
252-
253-
while (true) {
254-
const { done, value } = await reader.read();
255-
if (done) {
256-
break;
257-
}
258-
chunks.push(value);
259-
receivedLength += value.length;
260-
console.log(`Received ${receivedLength} bytes`);
261-
}
237+
// console.log('Result:', result);
262238

263-
console.log('All chunks received:', chunks);
239+
// // Now we check the result to see if it's ok and then we can actually read from the body.
240+
// if (!result.ok) {
241+
// console.log("Error fetching firmware:", result.status, result.statusText);
242+
// }
264243

265-
// Combine all chunks into a single Uint8Array
266-
const chunksAll = new Uint8Array(receivedLength);
267-
let position = 0;
268-
for (const chunk of chunks) {
269-
chunksAll.set(chunk, position); // copy chunk to the final array
270-
position += chunk.length; // update position
271-
}
272-
console.log('All chunks combined:', chunksAll);
244+
// // stream the response
245+
// const reader = result.body?.getReader();
246+
// if (!reader) {
247+
// throw new Error('Failed to get reader from response body.');
248+
// }
273249

274-
// Convert the Uint8Array to a string
275-
const firmwareString = Array.from(chunksAll)
276-
.map(byte => String.fromCharCode(byte))
277-
.join('');
250+
// let receivedLength = 0; // received bytes
251+
// const chunks: Uint8Array[] = []; // chunks of received data
278252

279-
console.log('Firmware string:', firmwareString);
253+
// while (true) {
254+
// const { done, value } = await reader.read();
255+
// if (done) {
256+
// break;
257+
// }
258+
// chunks.push(value);
259+
// receivedLength += value.length;
260+
// console.log(`Received ${receivedLength} bytes`);
261+
// }
280262

281-
throw new Error('Purposeful error out during testing.'); // TODO: Remove this line when done testing.
263+
// console.log('All chunks received:', chunks);
282264

283-
// if (!result.ok) {
284-
// console.log("Error fetching firmware:", result.status, result.statusText);
285-
// throw new Error(`Failed to fetch firmware: ${result.status} ${result.statusText}`);
265+
// // Combine all chunks into a single Uint8Array
266+
// const chunksAll = new Uint8Array(receivedLength);
267+
// let position = 0;
268+
// for (const chunk of chunks) {
269+
// chunksAll.set(chunk, position); // copy chunk to the final array
270+
// position += chunk.length; // update position
286271
// }
272+
// console.log('All chunks combined:', chunksAll);
287273

288-
// this.firmwareBlob = await result.blob();
289-
// const uint8Array = new Uint8Array(await this.firmwareBlob.arrayBuffer());
290-
// this.firmwareString = Array.from(uint8Array)
274+
275+
// // Convert the Uint8Array to a string
276+
// const firmwareString = Array.from(chunksAll)
291277
// .map(byte => String.fromCharCode(byte))
292278
// .join('');
293279

294-
// console.log('Downloaded SFE FIRMWARE. Firmware string size:', this.firmwareString.length);
295-
// return this.firmwareString;
280+
// console.log('Firmware string:', firmwareString);
281+
282+
// throw new Error('Purposeful error out during testing.'); // TODO: Remove this line when done testing.
283+
284+
if (!result.ok) {
285+
console.log("Error fetching firmware:", result.status, result.statusText);
286+
throw new Error(`Failed to fetch firmware: ${result.status} ${result.statusText}`);
287+
}
288+
289+
this.firmwareBlob = await result.blob();
290+
const uint8Array = new Uint8Array(await this.firmwareBlob.arrayBuffer());
291+
this.firmwareString = Array.from(uint8Array)
292+
.map(byte => String.fromCharCode(byte))
293+
.join('');
294+
295+
console.log('Downloaded SFE FIRMWARE. Firmware string size:', this.firmwareString.length);
296+
return this.firmwareString;
296297
}
297298

298299
}

0 commit comments

Comments
 (0)