@@ -3,8 +3,6 @@ const fs = require('fs');
33const path = require ( 'path' ) ;
44const zlib = require ( 'zlib' ) ;
55const util = require ( 'util' ) ;
6- const request = require ( 'request' ) ;
7- const decompress = require ( 'decompress' ) ;
86const processExtension = require ( './processExtension' ) ;
97const { highestBuiltinLanguageId } = require ( './storeUtils' ) ;
108const {
@@ -42,34 +40,23 @@ async function syncExtensionData({ identifier }, cache, extensionDir) {
4240 * @param {import('.').ExtensionDemand } extensionDemand
4341 * @param {* } cache
4442 * @param {string } extensionDir
43+ * @param {import('./host').Host } host
4544 */
46- async function downloadExtension ( extensionDemand , cache , extensionDir ) {
45+ async function downloadExtension ( extensionDemand , cache , extensionDir , host ) {
4746 const { identifier, version } = extensionDemand ;
4847 const { publisher, name } = parseExtensionIdentifier ( identifier ) ;
4948 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+ }
7057
7158 const extensionPath = getExtensionBasePath ( identifier , extensionDir ) ;
72- await decompress ( archive , extensionPath ) ;
59+ await host . decompress ( response . body , extensionPath ) ;
7360 await syncExtensionData ( extensionDemand , cache , extensionDir ) ;
7461 return extensionPath ;
7562}
@@ -79,24 +66,25 @@ async function downloadExtension(extensionDemand, cache, extensionDir) {
7966 * @property {import('.').ExtensionDemand[] } extensions
8067 * @property {* } cache
8168 * @property {string } extensionDir
69+ * @property {import('./host').Host } host
8270 */
8371
8472/**
8573 * @param {DownloadExtensionOptions } options
8674 */
87- async function downloadExtensionsIfNeeded ( { extensions, cache, extensionDir } ) {
75+ async function downloadExtensionsIfNeeded ( { extensions, cache, extensionDir, host } ) {
8876 extensions = extensions . slice ( ) ;
8977 while ( extensions . length ) {
9078 const extensionDemand = extensions . shift ( ) ;
9179 const { identifier, version } = extensionDemand ;
9280 const extensionPath = getExtensionBasePath ( identifier , extensionDir ) ;
9381 if ( ! fs . existsSync ( extensionPath ) ) {
94- await downloadExtension ( extensionDemand , cache , extensionDir ) ;
82+ await downloadExtension ( extensionDemand , cache , extensionDir , host ) ;
9583 continue ;
9684 }
9785 const packageJson = getExtensionPackageJson ( identifier , extensionDir ) ;
9886 if ( packageJson . version !== version ) {
99- await downloadExtension ( extensionDemand , cache , extensionDir ) ;
87+ await downloadExtension ( extensionDemand , cache , extensionDir , host ) ;
10088 continue ;
10189 }
10290
0 commit comments