@@ -98,45 +98,53 @@ async function main() {
98
98
throw errUnsupported ;
99
99
}
100
100
101
+ // Read from package.json and prepare for the installation.
101
102
const pkg = await readPackageJson ( ) ;
102
103
if ( platform === "windows" ) {
103
104
// Update bin path in package.json
104
105
pkg . bin [ pkg . name ] += ".exe" ;
105
106
}
106
107
108
+ // Prepare the installation path by creating the directory if it doesn't exist.
107
109
const binPath = pkg . bin [ pkg . name ] ;
108
110
const binDir = path . dirname ( binPath ) ;
109
111
await fs . promises . mkdir ( binDir , { recursive : true } ) ;
110
112
111
- // First we will Un-GZip, then we will untar.
112
- const ungz = zlib . createGunzip ( ) ;
113
- const binName = path . basename ( binPath ) ;
114
- const untar = extract ( { cwd : binDir } , [ binName ] ) ;
115
-
116
- const url = getDownloadUrl ( pkg ) ;
117
- console . info ( "Downloading" , url ) ;
113
+ // Create the agent that will be used for all the fetch requests later.
118
114
const proxyUrl =
119
115
process . env . npm_config_https_proxy ||
120
116
process . env . npm_config_http_proxy ||
121
117
process . env . npm_config_proxy ;
122
-
123
118
// Keeps the TCP connection alive when sending multiple requests
124
119
// Ref: https://github.com/node-fetch/node-fetch/issues/1735
125
120
const agent = proxyUrl
126
121
? new HttpsProxyAgent ( proxyUrl , { keepAlive : true } )
127
122
: new Agent ( { keepAlive : true } ) ;
128
- const resp = await fetch ( url , { agent } ) ;
129
123
124
+ // First, fetch the checksum map.
125
+ const checksumMap = await fetchAndParseCheckSumFile ( pkg , agent ) ;
126
+
127
+ // Then, download the binary.
128
+ const url = getDownloadUrl ( pkg ) ;
129
+ console . info ( "Downloading" , url ) ;
130
+ const resp = await fetch ( url , { agent } ) ;
130
131
const hash = createHash ( "sha256" ) ;
131
132
const pkgNameWithPlatform = `${ pkg . name } _${ platform } _${ arch } .tar.gz` ;
132
- const checksumMap = await fetchAndParseCheckSumFile ( pkg , agent ) ;
133
133
134
+ // Then, decompress the binary -- we will first Un-GZip, then we will untar.
135
+ const ungz = zlib . createGunzip ( ) ;
136
+ const binName = path . basename ( binPath ) ;
137
+ const untar = extract ( { cwd : binDir } , [ binName ] ) ;
138
+
139
+ // Update the hash with the binary data as it's being downloaded.
134
140
resp . body
135
141
. on ( "data" , ( chunk ) => {
136
142
hash . update ( chunk ) ;
137
143
} )
144
+ // Pipe the data to the ungz stream.
138
145
. pipe ( ungz ) ;
139
146
147
+ // After the ungz stream has ended, verify the checksum.
140
148
ungz
141
149
. on ( "end" , ( ) => {
142
150
const expectedChecksum = checksumMap ?. [ pkgNameWithPlatform ] ;
@@ -151,8 +159,10 @@ async function main() {
151
159
}
152
160
console . info ( "Checksum verified." ) ;
153
161
} )
162
+ // Pipe the data to the untar stream.
154
163
. pipe ( untar ) ;
155
164
165
+ // Wait for the untar stream to finish.
156
166
await new Promise ( ( resolve , reject ) => {
157
167
untar . on ( "error" , reject ) ;
158
168
untar . on ( "end" , ( ) => resolve ( ) ) ;
0 commit comments