OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library hosted_source; | 5 library hosted_source; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
10 import 'dart:uri'; | 10 import 'dart:uri'; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 log.message('Downloading $id...'); | 71 log.message('Downloading $id...'); |
72 | 72 |
73 // Download and extract the archive to a temp directory. | 73 // Download and extract the archive to a temp directory. |
74 var tempDir; | 74 var tempDir; |
75 return Futures.wait([ | 75 return Futures.wait([ |
76 httpClient.send(new http.Request("GET", new Uri.fromString(fullUrl))) | 76 httpClient.send(new http.Request("GET", new Uri.fromString(fullUrl))) |
77 .then((response) => response.stream), | 77 .then((response) => response.stream), |
78 systemCache.createTempDir() | 78 systemCache.createTempDir() |
79 ]).then((args) { | 79 ]).then((args) { |
80 tempDir = args[1]; | 80 tempDir = args[1]; |
81 return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT, | 81 var stream = wrapByteStream(args[0]); |
82 return timeout(extractTarGz(stream, tempDir), HTTP_TIMEOUT, | |
82 'fetching URL "$fullUrl"'); | 83 'fetching URL "$fullUrl"'); |
83 }).then((_) { | 84 }).then((_) { |
84 // Now that the install has succeeded, move it to the real location in | 85 // Now that the install has succeeded, move it to the real location in |
85 // the cache. This ensures that we don't leave half-busted ghost | 86 // the cache. This ensures that we don't leave half-busted ghost |
86 // directories in the user's pub cache if an install fails. | 87 // directories in the user's pub cache if an install fails. |
87 return renameDir(tempDir, destPath); | 88 return renameDir(tempDir, destPath); |
88 }).then((_) => true); | 89 }).then((_) => true); |
89 } | 90 } |
90 | 91 |
91 /// The system cache directory for the hosted source contains subdirectories | 92 /// The system cache directory for the hosted source contains subdirectories |
(...skipping 19 matching lines...) Expand all Loading... | |
111 /// There are two valid formats. A plain string refers to a package with the | 112 /// There are two valid formats. A plain string refers to a package with the |
112 /// given name from the default host, while a map with keys "name" and "url" | 113 /// given name from the default host, while a map with keys "name" and "url" |
113 /// refers to a package with the given name from the host at the given URL. | 114 /// refers to a package with the given name from the host at the given URL. |
114 void validateDescription(description, {bool fromLockFile: false}) { | 115 void validateDescription(description, {bool fromLockFile: false}) { |
115 _parseDescription(description); | 116 _parseDescription(description); |
116 } | 117 } |
117 | 118 |
118 /// When an error occurs trying to read something about [package] from [url], | 119 /// When an error occurs trying to read something about [package] from [url], |
119 /// this tries to translate into a more user friendly error message. Always | 120 /// this tries to translate into a more user friendly error message. Always |
120 /// throws an error, either the original one or a better one. | 121 /// throws an error, either the original one or a better one. |
121 void _throwFriendlyError(ex, package, url) { | 122 void _throwFriendlyError(asyncError, package, url) { |
Bob Nystrom
2013/01/09 16:49:23
Can we type annotate this too?
| |
122 ex = getRealError(ex); | 123 var ex = getRealError(asyncError); |
123 | 124 |
124 if (ex is PubHttpException && ex.response.statusCode == 404) { | 125 if (ex is PubHttpException && ex.response.statusCode == 404) { |
125 throw 'Could not find package "$package" at $url.'; | 126 throw 'Could not find package "$package" at $url.'; |
126 } | 127 } |
127 | 128 |
128 if (ex is TimeoutException) { | 129 if (ex is TimeoutException) { |
129 throw 'Timed out trying to find package "$package" at $url.'; | 130 throw 'Timed out trying to find package "$package" at $url.'; |
130 } | 131 } |
131 | 132 |
132 if (ex is io.SocketIOException) { | 133 if (ex is io.SocketIOException) { |
133 throw 'Got socket error trying to find package "$package" at $url.\n' | 134 throw 'Got socket error trying to find package "$package" at $url.\n' |
134 '${ex.osError}'; | 135 '${ex.osError}'; |
135 } | 136 } |
136 | 137 |
137 // Otherwise re-throw the original exception. | 138 // Otherwise re-throw the original exception. |
138 throw ex; | 139 throw asyncError; |
Bob Nystrom
2013/01/09 16:49:23
Argh good catch. Completely misleading comment on
| |
139 } | 140 } |
140 | 141 |
141 /// Parses the description for a package. | 142 /// Parses the description for a package. |
142 /// | 143 /// |
143 /// If the package parses correctly, this returns a (name, url) pair. If not, | 144 /// If the package parses correctly, this returns a (name, url) pair. If not, |
144 /// this throws a descriptive FormatException. | 145 /// this throws a descriptive FormatException. |
145 Pair<String, String> _parseDescription(description) { | 146 Pair<String, String> _parseDescription(description) { |
146 if (description is String) { | 147 if (description is String) { |
147 return new Pair<String, String>(description, defaultUrl); | 148 return new Pair<String, String>(description, defaultUrl); |
148 } | 149 } |
(...skipping 10 matching lines...) Expand all Loading... | |
159 | 160 |
160 var name = description["name"]; | 161 var name = description["name"]; |
161 if (name is! String) { | 162 if (name is! String) { |
162 throw new FormatException("The 'name' key must have a string value."); | 163 throw new FormatException("The 'name' key must have a string value."); |
163 } | 164 } |
164 | 165 |
165 var url = description.containsKey("url") ? description["url"] : defaultUrl; | 166 var url = description.containsKey("url") ? description["url"] : defaultUrl; |
166 return new Pair<String, String>(name, url); | 167 return new Pair<String, String>(name, url); |
167 } | 168 } |
168 } | 169 } |
OLD | NEW |