@@ -57,6 +57,30 @@ export class CreateApi {
57
57
return result ;
58
58
}
59
59
60
+ /**
61
+ * `sketchPath` is not the POSIX path but the path with the user UUID, username, etc.
62
+ * See [Create.Resource#path](./typings.ts). If `cache` is `true` and a sketch exists with the path,
63
+ * the cache will be updated with the new state of the sketch.
64
+ */
65
+ // TODO: no nulls in API
66
+ async sketchByPath (
67
+ sketchPath : string ,
68
+ cache = false
69
+ ) : Promise < Create . Sketch | null > {
70
+ const url = new URL ( `${ this . domain ( ) } /sketches/byPath/${ sketchPath } ` ) ;
71
+ const headers = await this . headers ( ) ;
72
+ const sketch = await this . run < Create . Sketch > ( url , {
73
+ method : 'GET' ,
74
+ headers,
75
+ } ) ;
76
+ if ( sketch && cache ) {
77
+ this . sketchCache . addSketch ( sketch ) ;
78
+ const posixPath = createPaths . toPosixPath ( sketch . path ) ;
79
+ this . sketchCache . purgeByPath ( posixPath ) ;
80
+ }
81
+ return sketch ;
82
+ }
83
+
60
84
async sketches ( limit = 50 ) : Promise < Create . Sketch [ ] > {
61
85
const url = new URL ( `${ this . domain ( ) } /sketches` ) ;
62
86
url . searchParams . set ( 'user_id' , 'me' ) ;
@@ -86,7 +110,11 @@ export class CreateApi {
86
110
87
111
async createSketch (
88
112
posixPath : string ,
89
- contentProvider : MaybePromise < string > = this . sketchesService . defaultInoContent ( )
113
+ contentProvider : MaybePromise < string > = this . sketchesService . defaultInoContent ( ) ,
114
+ payloadOverride : Record <
115
+ string ,
116
+ string | boolean | number | Record < string , unknown >
117
+ > = { }
90
118
) : Promise < Create . Sketch > {
91
119
const url = new URL ( `${ this . domain ( ) } /sketches` ) ;
92
120
const [ headers , content ] = await Promise . all ( [
@@ -97,6 +125,7 @@ export class CreateApi {
97
125
ino : btoa ( content ) ,
98
126
path : posixPath ,
99
127
user_id : 'me' ,
128
+ ...payloadOverride ,
100
129
} ;
101
130
const init = {
102
131
method : 'PUT' ,
@@ -212,7 +241,17 @@ export class CreateApi {
212
241
return data ;
213
242
}
214
243
215
- const sketch = this . sketchCache . getSketch ( createPaths . parentPosix ( path ) ) ;
244
+ const posixPath = createPaths . parentPosix ( path ) ;
245
+ let sketch = this . sketchCache . getSketch ( posixPath ) ;
246
+ // Workaround for https://github.com/arduino/arduino-ide/issues/1999.
247
+ if ( ! sketch ) {
248
+ // Convert the ordinary sketch POSIX path to the Create path.
249
+ // For example, `/sketch_apr6a` will be transformed to `8a694e4b83878cc53472bd75ee928053:kittaakos/sketches_v2/sketch_apr6a`.
250
+ const createPathPrefix = this . sketchCache . createPathPrefix ;
251
+ if ( createPathPrefix ) {
252
+ sketch = await this . sketchByPath ( createPathPrefix + posixPath , true ) ;
253
+ }
254
+ }
216
255
217
256
if (
218
257
sketch &&
@@ -448,13 +487,18 @@ export class CreateApi {
448
487
await this . run ( url , init , ResponseResultProvider . NOOP ) ;
449
488
}
450
489
490
+ private fetchCounter = 0 ;
451
491
private async run < T > (
452
492
requestInfo : URL ,
453
493
init : RequestInit | undefined ,
454
494
resultProvider : ResponseResultProvider = ResponseResultProvider . JSON
455
495
) : Promise < T > {
456
- console . debug ( `HTTP ${ init ?. method } : ${ requestInfo . toString ( ) } ` ) ;
496
+ const fetchCount = `[${ ++ this . fetchCounter } ]` ;
497
+ const fetchStart = performance . now ( ) ;
498
+ const method = init ?. method ? `${ init . method } : ` : '' ;
499
+ const url = requestInfo . toString ( ) ;
457
500
const response = await fetch ( requestInfo . toString ( ) , init ) ;
501
+ const fetchEnd = performance . now ( ) ;
458
502
if ( ! response . ok ) {
459
503
let details : string | undefined = undefined ;
460
504
try {
@@ -465,7 +509,18 @@ export class CreateApi {
465
509
const { statusText, status } = response ;
466
510
throw new CreateError ( statusText , status , details ) ;
467
511
}
512
+ const parseStart = performance . now ( ) ;
468
513
const result = await resultProvider ( response ) ;
514
+ const parseEnd = performance . now ( ) ;
515
+ console . debug (
516
+ `HTTP ${ fetchCount } ${ method } ${ url } [fetch: ${ (
517
+ fetchEnd - fetchStart
518
+ ) . toFixed ( 2 ) } ms, parse: ${ ( parseEnd - parseStart ) . toFixed (
519
+ 2
520
+ ) } ms] body: ${
521
+ typeof result === 'string' ? result : JSON . stringify ( result )
522
+ } `
523
+ ) ;
469
524
return result ;
470
525
}
471
526
0 commit comments