Skip to content

Commit 970bec4

Browse files
author
Akos Kitta
committed
fix: try fetch the sketch by path if not in the cache
The sketch cache might be empty, when trying to generate the secrets include in the main sketch file from the `secrets` property. Closes #1999 Signed-off-by: Akos Kitta <[email protected]>
1 parent eb1f247 commit 970bec4

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

arduino-ide-extension/src/browser/create/create-api.ts

+52-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ export class CreateApi {
5757
return result;
5858
}
5959

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+
6084
async sketches(limit = 50): Promise<Create.Sketch[]> {
6185
const url = new URL(`${this.domain()}/sketches`);
6286
url.searchParams.set('user_id', 'me');
@@ -212,7 +236,17 @@ export class CreateApi {
212236
return data;
213237
}
214238

215-
const sketch = this.sketchCache.getSketch(createPaths.parentPosix(path));
239+
const posixPath = createPaths.parentPosix(path);
240+
let sketch = this.sketchCache.getSketch(posixPath);
241+
// Workaround for https://github.com/arduino/arduino-ide/issues/1999.
242+
if (!sketch) {
243+
// Convert the ordinary sketch POSIX path to the Create path.
244+
// For example, `/sketch_apr6a` will be transformed to `8a694e4b83878cc53472bd75ee928053:kittaakos/sketches_v2/sketch_apr6a`.
245+
const createPathPrefix = this.sketchCache.createPathPrefix;
246+
if (createPathPrefix) {
247+
sketch = await this.sketchByPath(createPathPrefix + posixPath, true);
248+
}
249+
}
216250

217251
if (
218252
sketch &&
@@ -448,13 +482,18 @@ export class CreateApi {
448482
await this.run(url, init, ResponseResultProvider.NOOP);
449483
}
450484

485+
private fetchCounter = 0;
451486
private async run<T>(
452487
requestInfo: URL,
453488
init: RequestInit | undefined,
454489
resultProvider: ResponseResultProvider = ResponseResultProvider.JSON
455490
): Promise<T> {
456-
console.debug(`HTTP ${init?.method}: ${requestInfo.toString()}`);
491+
const fetchCount = `[${++this.fetchCounter}]`;
492+
const fetchStart = performance.now();
493+
const method = init?.method ? `${init.method}: ` : '';
494+
const url = requestInfo.toString();
457495
const response = await fetch(requestInfo.toString(), init);
496+
const fetchEnd = performance.now();
458497
if (!response.ok) {
459498
let details: string | undefined = undefined;
460499
try {
@@ -465,7 +504,18 @@ export class CreateApi {
465504
const { statusText, status } = response;
466505
throw new CreateError(statusText, status, details);
467506
}
507+
const parseStart = performance.now();
468508
const result = await resultProvider(response);
509+
const parseEnd = performance.now();
510+
console.debug(
511+
`HTTP ${fetchCount} ${method} ${url} [fetch: ${(
512+
fetchEnd - fetchStart
513+
).toFixed(2)} ms, parse: ${(parseEnd - parseStart).toFixed(
514+
2
515+
)} ms] body: ${
516+
typeof result === 'string' ? result : JSON.stringify(result)
517+
}`
518+
);
469519
return result;
470520
}
471521

arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketch-cache.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { FileStat } from '@theia/filesystem/lib/common/files';
22
import { injectable } from '@theia/core/shared/inversify';
3-
import { toPosixPath } from '../../create/create-paths';
3+
import { splitSketchPath } from '../../create/create-paths';
44
import { Create } from '../../create/typings';
55

66
@injectable()
77
export class SketchCache {
88
sketches: Record<string, Create.Sketch> = {};
99
fileStats: Record<string, FileStat> = {};
10+
private _createPathPrefix: string | undefined;
1011

1112
init(): void {
1213
// reset the data
@@ -32,14 +33,21 @@ export class SketchCache {
3233

3334
addSketch(sketch: Create.Sketch): void {
3435
const { path } = sketch;
35-
const posixPath = toPosixPath(path);
36+
const [pathPrefix, posixPath] = splitSketchPath(path);
37+
if (pathPrefix !== this._createPathPrefix) {
38+
this._createPathPrefix = pathPrefix;
39+
}
3640
this.sketches[posixPath] = sketch;
3741
}
3842

3943
getSketch(path: string): Create.Sketch | null {
4044
return this.sketches[path] || null;
4145
}
4246

47+
get createPathPrefix(): string | undefined {
48+
return this._createPathPrefix;
49+
}
50+
4351
toString(): string {
4452
return JSON.stringify({
4553
sketches: this.sketches,

0 commit comments

Comments
 (0)