Skip to content

Commit aced054

Browse files
fix: Use arrayBuffer for fetching and caching local Snaps
1 parent 8efc3b3 commit aced054

File tree

1 file changed

+5
-18
lines changed
  • packages/snaps-controllers/src/snaps/location

1 file changed

+5
-18
lines changed

packages/snaps-controllers/src/snaps/location/http.ts

+5-18
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,7 @@ export interface HttpOptions {
2020
}
2121

2222
export class HttpLocation implements SnapLocation {
23-
// We keep contents separate because then we can use only one Blob in cache,
24-
// which we convert to Uint8Array when actually returning the file.
25-
//
26-
// That avoids deepCloning file contents.
27-
// I imagine ArrayBuffers are copy-on-write optimized, meaning
28-
// in most often case we'll only have one file contents in common case.
29-
private readonly cache = new Map<
30-
string,
31-
{ file: VirtualFile; contents: Blob }
32-
>();
23+
private readonly cache = new Map<string, VirtualFile>();
3324

3425
private validatedManifest?: VirtualFile<SnapManifest>;
3526

@@ -80,11 +71,7 @@ export class HttpLocation implements SnapLocation {
8071
const relativePath = normalizeRelative(path);
8172
const cached = this.cache.get(relativePath);
8273
if (cached !== undefined) {
83-
const { file, contents } = cached;
84-
const value = new Uint8Array(await contents.arrayBuffer());
85-
const vfile = file.clone();
86-
vfile.value = value;
87-
return vfile;
74+
return cached.clone();
8875
}
8976

9077
const canonicalPath = this.toCanonical(relativePath).toString();
@@ -94,17 +81,17 @@ export class HttpLocation implements SnapLocation {
9481
`Failed to fetch "${canonicalPath}". Status code: ${response.status}.`,
9582
);
9683
}
84+
const buffer = await response.arrayBuffer();
9785
const vfile = new VirtualFile({
98-
value: '',
86+
value: new Uint8Array(buffer),
9987
path: relativePath,
10088
data: { canonicalPath },
10189
});
102-
const blob = await response.blob();
10390
assert(
10491
!this.cache.has(relativePath),
10592
'Corrupted cache, multiple files with same path.',
10693
);
107-
this.cache.set(relativePath, { file: vfile, contents: blob });
94+
this.cache.set(relativePath, vfile);
10895

10996
return this.fetch(relativePath);
11097
}

0 commit comments

Comments
 (0)