@@ -13,10 +13,10 @@ const crypto = require('crypto');
1313const tar = require ( 'tar' ) ;
1414const { Transform } = require ( 'stream' ) ;
1515const { pipeline } = require ( 'stream/promises' ) ;
16-
17- const VERSION = process . env . npm_package_version ;
16+ const { URL : NodeURL } = require ( 'url' ) ;
1817
1918let config ;
19+ let VERSION ;
2020
2121// When installing from the registry, `npm` doesn't set `npm_package_config_*`
2222// environment variables. However, unlike `yarn`, `npm` always provides a path
@@ -26,15 +26,18 @@ if (process.env.npm_package_json) {
2626 encoding : 'utf8' ,
2727 } ) ;
2828
29- ( { config } = JSON . parse ( json ) ) ;
29+ const pkg = JSON . parse ( json ) ;
30+ config = pkg . config ;
31+ VERSION = pkg . version ;
3032} else {
3133 config = {
3234 prebuildUrl : process . env . npm_package_config_prebuildUrl ,
3335 prebuildChecksum : process . env . npm_package_config_prebuildChecksum ,
3436 } ;
37+ VERSION = process . env . npm_package_version ;
3538}
3639
37- const URL = config . prebuildUrl . replace (
40+ const PREBUILD_URL = config . prebuildUrl . replace (
3841 '${npm_package_version}' , // eslint-disable-line no-template-curly-in-string
3942 VERSION
4043) ;
@@ -68,15 +71,30 @@ async function downloadIfNeeded() {
6871 await download ( ) ;
6972}
7073
71- function download ( ) {
72- console . log ( `downloading ${ URL } ` ) ;
74+ function download ( url = PREBUILD_URL ) {
75+ console . log ( `downloading ${ url } ` ) ;
7376 return new Promise ( ( resolve , reject ) => {
7477 let options = { } ;
7578 if ( process . env . HTTPS_PROXY != undefined ) {
7679 options . agent = new HttpsProxyAgent ( process . env . HTTPS_PROXY ) ;
7780 }
78- https . get ( URL , options , async res => {
81+
82+ // Parse URL if it's a string
83+ const parsedUrl = typeof url === 'string' ? new NodeURL ( url ) : url ;
84+
85+ https . get ( parsedUrl , options , async res => {
7986 try {
87+ // Handle redirects (GitHub releases use 302 redirects)
88+ if ( res . statusCode >= 300 && res . statusCode < 400 && res . headers . location ) {
89+ console . log ( `following redirect to ${ res . headers . location } ` ) ;
90+ resolve ( download ( res . headers . location ) ) ;
91+ return ;
92+ }
93+
94+ if ( res . statusCode !== 200 ) {
95+ throw new Error ( `HTTP error: ${ res . statusCode } ${ res . statusMessage } ` ) ;
96+ }
97+
8098 const out = fs . createWriteStream ( tmpFile ) ;
8199
82100 const hash = crypto . createHash ( 'sha256' ) ;
0 commit comments