@@ -3,8 +3,6 @@ const fs = require('fs');
3
3
const path = require ( 'path' ) ;
4
4
const zlib = require ( 'zlib' ) ;
5
5
const util = require ( 'util' ) ;
6
- const request = require ( 'request' ) ;
7
- const decompress = require ( 'decompress' ) ;
8
6
const processExtension = require ( './processExtension' ) ;
9
7
const { highestBuiltinLanguageId } = require ( './storeUtils' ) ;
10
8
const {
@@ -42,34 +40,23 @@ async function syncExtensionData({ identifier }, cache, extensionDir) {
42
40
* @param {import('.').ExtensionDemand } extensionDemand
43
41
* @param {* } cache
44
42
* @param {string } extensionDir
43
+ * @param {import('./host').Host } host
45
44
*/
46
- async function downloadExtension ( extensionDemand , cache , extensionDir ) {
45
+ async function downloadExtension ( extensionDemand , cache , extensionDir , host ) {
47
46
const { identifier, version } = extensionDemand ;
48
47
const { publisher, name } = parseExtensionIdentifier ( identifier ) ;
49
48
const url = `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/${ publisher } /vsextensions/${ name } /${ version } /vspackage` ;
50
- const archive = await new Promise ( ( resolve , reject ) => {
51
- request . get ( url , { encoding : null } , ( error , res , body ) => {
52
- if ( error ) {
53
- return reject ( error ) ;
54
- }
55
- if ( res . statusCode === 404 ) {
56
- return reject (
57
- new Error ( `Could not find extension with publisher '${ publisher } ', name '${ name } ', and verion '${ version } '.` )
58
- ) ;
59
- }
60
- if ( res . statusCode !== 200 ) {
61
- return reject ( new Error ( `Failed to download extension ${ identifier } with status code ${ res . statusCode } ` ) ) ;
62
- }
63
- if ( res . headers [ 'content-encoding' ] === 'gzip' ) {
64
- return gunzip ( body ) . then ( resolve , reject ) ;
65
- }
66
-
67
- resolve ( body ) ;
68
- } ) ;
69
- } ) ;
49
+ const response = await host . fetch ( url , { encoding : null } ) ;
50
+ if ( response . statusCode === 404 ) {
51
+ throw new Error ( `Could not find extension with publisher '${ publisher } ', name '${ name } ', and verion '${ version } '.` ) ;
52
+ }
53
+ if ( response . statusCode !== 200 ) {
54
+ const details = response . body ? `. Response body:\n\n${ response . body . toString ( 'utf8' ) } ` : '' ;
55
+ throw new Error ( `Failed to download extension ${ identifier } with status code ${ response . statusCode } ${ details } ` ) ;
56
+ }
70
57
71
58
const extensionPath = getExtensionBasePath ( identifier , extensionDir ) ;
72
- await decompress ( archive , extensionPath ) ;
59
+ await host . decompress ( response . body , extensionPath ) ;
73
60
await syncExtensionData ( extensionDemand , cache , extensionDir ) ;
74
61
return extensionPath ;
75
62
}
@@ -79,24 +66,25 @@ async function downloadExtension(extensionDemand, cache, extensionDir) {
79
66
* @property {import('.').ExtensionDemand[] } extensions
80
67
* @property {* } cache
81
68
* @property {string } extensionDir
69
+ * @property {import('./host').Host } host
82
70
*/
83
71
84
72
/**
85
73
* @param {DownloadExtensionOptions } options
86
74
*/
87
- async function downloadExtensionsIfNeeded ( { extensions, cache, extensionDir } ) {
75
+ async function downloadExtensionsIfNeeded ( { extensions, cache, extensionDir, host } ) {
88
76
extensions = extensions . slice ( ) ;
89
77
while ( extensions . length ) {
90
78
const extensionDemand = extensions . shift ( ) ;
91
79
const { identifier, version } = extensionDemand ;
92
80
const extensionPath = getExtensionBasePath ( identifier , extensionDir ) ;
93
81
if ( ! fs . existsSync ( extensionPath ) ) {
94
- await downloadExtension ( extensionDemand , cache , extensionDir ) ;
82
+ await downloadExtension ( extensionDemand , cache , extensionDir , host ) ;
95
83
continue ;
96
84
}
97
85
const packageJson = getExtensionPackageJson ( identifier , extensionDir ) ;
98
86
if ( packageJson . version !== version ) {
99
- await downloadExtension ( extensionDemand , cache , extensionDir ) ;
87
+ await downloadExtension ( extensionDemand , cache , extensionDir , host ) ;
100
88
continue ;
101
89
}
102
90
0 commit comments