@@ -4,8 +4,8 @@ import { DeviceService } from './DeviceService';
4
4
// const { Octokit } = require('@octokit/rest');
5
5
6
6
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 ;
9
9
private selectedFirmwareId : string = 'Auto' ;
10
10
// Should make firmwareOptions a private variable and use getters to access it.
11
11
// 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 {
76
76
// https://api.github.com/repos/OWNER/REPO/releases/assets/ASSET_ID
77
77
78
78
//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 ;
80
80
81
81
//method 2: use the asset ID to formulate a GET request to the GitHub API with octokit.
82
82
// Instead let's use the asset ID to formulate an GitHub API URL.
83
83
// We'll still use the 'URL' var for now but it's actually a formatted GET req
84
84
//const firmwareUrl = 'GET /repos/sparkfun/micropython/releases/assets/' + asset.id;
85
85
86
86
//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}`;
88
88
89
89
// log the asset object
90
90
console . log ( 'Asset:' , asset ) ;
@@ -137,8 +137,8 @@ export class FirmwareService {
137
137
setSelectedFirmwareId ( id : string ) : void {
138
138
if ( id in this . firmwareOptions ) {
139
139
this . selectedFirmwareId = id ;
140
- // this.firmwareString = null;
141
- // this.firmwareBlob = null;
140
+ this . firmwareString = null ;
141
+ this . firmwareBlob = null ;
142
142
localStorage . removeItem ( 'cachedFirmware' ) ;
143
143
localStorage . setItem ( 'selectedFirmwareId' , id ) ;
144
144
}
@@ -192,13 +192,12 @@ export class FirmwareService {
192
192
console . log ( "Performing fetch for firmware:" , selectedFirmware . url ) ;
193
193
194
194
// 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
+ } ) ;
202
201
203
202
// console.log('Firmware fetch result:', result);
204
203
@@ -225,74 +224,76 @@ export class FirmwareService {
225
224
// 'X-GitHub-Api-Version': '2022-11-28',
226
225
// });
227
226
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
+ // });
249
236
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);
262
238
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
+ // }
264
243
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
+ // }
273
249
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
278
252
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
+ // }
280
262
281
- throw new Error ( 'Purposeful error out during testing.' ) ; // TODO: Remove this line when done testing.
263
+ // console.log('All chunks received:', chunks);
282
264
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
286
271
// }
272
+ // console.log('All chunks combined:', chunksAll);
287
273
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 )
291
277
// .map(byte => String.fromCharCode(byte))
292
278
// .join('');
293
279
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 ;
296
297
}
297
298
298
299
}
0 commit comments