@@ -18,7 +18,7 @@ use crate::{
18
18
http:: { HttpClient , HttpRequest , HttpResponse , USER_AGENT } ,
19
19
runtime:: {
20
20
package_loader:: PackageLoader ,
21
- resolver:: { Summary , WebcHash } ,
21
+ resolver:: { DistributionInfo , Summary , WebcHash } ,
22
22
} ,
23
23
} ;
24
24
@@ -76,9 +76,9 @@ impl BuiltinLoader {
76
76
Ok ( None )
77
77
}
78
78
79
- async fn download ( & self , summary : & Summary ) -> Result < Bytes , Error > {
80
- if summary . webc . scheme ( ) == "file" {
81
- if let Ok ( path) = summary . webc . to_file_path ( ) {
79
+ async fn download ( & self , dist : & DistributionInfo ) -> Result < Bytes , Error > {
80
+ if dist . webc . scheme ( ) == "file" {
81
+ if let Ok ( path) = dist . webc . to_file_path ( ) {
82
82
// FIXME: This will block the thread
83
83
let bytes = std:: fs:: read ( & path)
84
84
. with_context ( || format ! ( "Unable to read \" {}\" " , path. display( ) ) ) ?;
@@ -87,7 +87,7 @@ impl BuiltinLoader {
87
87
}
88
88
89
89
let request = HttpRequest {
90
- url : summary . webc . to_string ( ) ,
90
+ url : dist . webc . to_string ( ) ,
91
91
method : "GET" . to_string ( ) ,
92
92
headers : vec ! [
93
93
( "Accept" . to_string( ) , "application/webc" . to_string( ) ) ,
@@ -117,17 +117,17 @@ impl BuiltinLoader {
117
117
async fn save_and_load_as_mmapped (
118
118
& self ,
119
119
webc : & [ u8 ] ,
120
- summary : & Summary ,
120
+ dist : & DistributionInfo ,
121
121
) -> Result < Container , Error > {
122
122
// First, save it to disk
123
- self . fs . save ( webc, summary ) . await ?;
123
+ self . fs . save ( webc, dist ) . await ?;
124
124
125
125
// Now try to load it again. The resulting container should use
126
126
// a memory-mapped file rather than an in-memory buffer.
127
- match self . fs . lookup ( & summary . webc_sha256 ) . await ? {
127
+ match self . fs . lookup ( & dist . webc_sha256 ) . await ? {
128
128
Some ( container) => {
129
129
// we also want to make sure it's in the in-memory cache
130
- self . in_memory . save ( & container, summary . webc_sha256 ) ;
130
+ self . in_memory . save ( & container, dist . webc_sha256 ) ;
131
131
132
132
Ok ( container)
133
133
}
@@ -144,20 +144,20 @@ impl BuiltinLoader {
144
144
#[ async_trait:: async_trait]
145
145
impl PackageLoader for BuiltinLoader {
146
146
async fn load ( & self , summary : & Summary ) -> Result < Container , Error > {
147
- if let Some ( container) = self . get_cached ( & summary. webc_sha256 ) . await ? {
147
+ if let Some ( container) = self . get_cached ( & summary. dist . webc_sha256 ) . await ? {
148
148
return Ok ( container) ;
149
149
}
150
150
151
151
// looks like we had a cache miss and need to download it manually
152
152
let bytes = self
153
- . download ( summary)
153
+ . download ( & summary. dist )
154
154
. await
155
- . with_context ( || format ! ( "Unable to download \" {}\" " , summary. webc) ) ?;
155
+ . with_context ( || format ! ( "Unable to download \" {}\" " , summary. dist . webc) ) ?;
156
156
157
157
// We want to cache the container we downloaded, but we want to do it
158
158
// in a smart way to keep memory usage down.
159
159
160
- match self . save_and_load_as_mmapped ( & bytes, summary) . await {
160
+ match self . save_and_load_as_mmapped ( & bytes, & summary. dist ) . await {
161
161
Ok ( container) => {
162
162
// The happy path - we've saved to both caches and loaded the
163
163
// container from disk (hopefully using mmap) so we're done.
@@ -166,17 +166,17 @@ impl PackageLoader for BuiltinLoader {
166
166
Err ( e) => {
167
167
tracing:: warn!(
168
168
error=& * e,
169
- pkg. name=%summary. package_name ,
170
- pkg. version=%summary. version,
171
- pkg. hash=?summary. webc_sha256,
172
- pkg. url=%summary. webc,
169
+ pkg. name=%summary. pkg . name ,
170
+ pkg. version=%summary. pkg . version,
171
+ pkg. hash=?summary. dist . webc_sha256,
172
+ pkg. url=%summary. dist . webc,
173
173
"Unable to save the downloaded package to disk" ,
174
174
) ;
175
175
// The sad path - looks like we'll need to keep the whole thing
176
176
// in memory.
177
177
let container = Container :: from_bytes ( bytes) ?;
178
178
// We still want to cache it, of course
179
- self . in_memory . save ( & container, summary. webc_sha256 ) ;
179
+ self . in_memory . save ( & container, summary. dist . webc_sha256 ) ;
180
180
Ok ( container)
181
181
}
182
182
}
@@ -221,8 +221,8 @@ impl FileSystemCache {
221
221
}
222
222
}
223
223
224
- async fn save ( & self , webc : & [ u8 ] , summary : & Summary ) -> Result < ( ) , Error > {
225
- let path = self . path ( & summary . webc_sha256 ) ;
224
+ async fn save ( & self , webc : & [ u8 ] , dist : & DistributionInfo ) -> Result < ( ) , Error > {
225
+ let path = self . path ( & dist . webc_sha256 ) ;
226
226
227
227
let parent = path. parent ( ) . expect ( "Always within cache_dir" ) ;
228
228
@@ -273,7 +273,7 @@ mod tests {
273
273
274
274
use crate :: {
275
275
http:: { HttpRequest , HttpResponse } ,
276
- runtime:: resolver:: { SourceId , SourceKind } ,
276
+ runtime:: resolver:: { PackageInfo , SourceId , SourceKind } ,
277
277
} ;
278
278
279
279
use super :: * ;
@@ -320,25 +320,29 @@ mod tests {
320
320
} ] ) ) ;
321
321
let loader = BuiltinLoader :: new_with_client ( temp. path ( ) , client. clone ( ) ) ;
322
322
let summary = Summary {
323
- package_name : "python/python" . to_string ( ) ,
324
- version : "0.1.0" . parse ( ) . unwrap ( ) ,
325
- webc : "https://wapm.io/python/python" . parse ( ) . unwrap ( ) ,
326
- webc_sha256 : [ 0xaa ; 32 ] . into ( ) ,
327
- dependencies : Vec :: new ( ) ,
328
- commands : Vec :: new ( ) ,
329
- source : SourceId :: new (
330
- SourceKind :: Url ,
331
- "https://registry.wapm.io/graphql" . parse ( ) . unwrap ( ) ,
332
- ) ,
333
- entrypoint : Some ( "asdf" . to_string ( ) ) ,
323
+ pkg : PackageInfo {
324
+ name : "python/python" . to_string ( ) ,
325
+ version : "0.1.0" . parse ( ) . unwrap ( ) ,
326
+ dependencies : Vec :: new ( ) ,
327
+ commands : Vec :: new ( ) ,
328
+ entrypoint : Some ( "asdf" . to_string ( ) ) ,
329
+ } ,
330
+ dist : DistributionInfo {
331
+ webc : "https://wapm.io/python/python" . parse ( ) . unwrap ( ) ,
332
+ webc_sha256 : [ 0xaa ; 32 ] . into ( ) ,
333
+ source : SourceId :: new (
334
+ SourceKind :: Url ,
335
+ "https://registry.wapm.io/graphql" . parse ( ) . unwrap ( ) ,
336
+ ) ,
337
+ } ,
334
338
} ;
335
339
336
340
let container = loader. load ( & summary) . await . unwrap ( ) ;
337
341
338
342
// A HTTP request was sent
339
343
let requests = client. requests . lock ( ) . unwrap ( ) ;
340
344
let request = & requests[ 0 ] ;
341
- assert_eq ! ( request. url, summary. webc. to_string( ) ) ;
345
+ assert_eq ! ( request. url, summary. dist . webc. to_string( ) ) ;
342
346
assert_eq ! ( request. method, "GET" ) ;
343
347
assert_eq ! (
344
348
request. headers,
@@ -351,11 +355,11 @@ mod tests {
351
355
let manifest = container. manifest ( ) ;
352
356
assert_eq ! ( manifest. entrypoint. as_deref( ) , Some ( "python" ) ) ;
353
357
// it should have been automatically saved to disk
354
- let path = loader. fs . path ( & summary. webc_sha256 ) ;
358
+ let path = loader. fs . path ( & summary. dist . webc_sha256 ) ;
355
359
assert ! ( path. exists( ) ) ;
356
360
assert_eq ! ( std:: fs:: read( & path) . unwrap( ) , PYTHON ) ;
357
361
// and cached in memory for next time
358
362
let in_memory = loader. in_memory . 0 . read ( ) . unwrap ( ) ;
359
- assert ! ( in_memory. contains_key( & summary. webc_sha256) ) ;
363
+ assert ! ( in_memory. contains_key( & summary. dist . webc_sha256) ) ;
360
364
}
361
365
}
0 commit comments