11import * as fsSync from 'fs' ;
2+ import { URL } from 'url' ;
23import { default as createHttpsProxyAgent } from 'https-proxy-agent' ;
34import { default as TFileCache } from '@derhuerst/http-basic/lib/FileCache.js' ;
4- import * as querystring from 'querystring' ;
55import { default as request } from '@derhuerst/http-basic' ;
66import { default as ProgressBar } from 'progress' ;
77import { default as AdmZip } from 'adm-zip' ;
8-
9- import { URL } from 'url' ;
108import { argQuote , getCachePath , runTool } from './tools.js' ;
9+ import { URLSearchParams } from 'node:url' ;
1110
1211/**
1312 * @typedef {import('@derhuerst/http-basic/lib/FileCache.js').default } FileCacheInst
@@ -27,13 +26,18 @@ const requestAsync = (method, url, options = null) => new Promise((resolve, reje
2726 else resolve ( response ) ;
2827 } ) ) ;
2928
30- const normalizeS3Url = ( url ) => {
31- url = new URL ( url ) ;
32- if ( url . hostname . slice ( - 17 ) !== '.s3.amazonaws.com' ) return url . href ;
33- const query = Array . from ( url . searchParams . entries ( ) )
34- . filter ( ( [ key ] ) => key . slice ( 0 , 6 ) . toLowerCase ( ) !== 'x-amz-' )
35- . reduce ( ( query , [ key , val ] ) => ( { ...query , [ key ] : val } ) , { } ) ;
36- url . search = querystring . stringify ( query ) ;
29+ /**
30+ * Returns normalized URL with X-Amz params stripped and application/x-www-form-urlencoded format
31+ * (spaces %20 are encoded as +)
32+ * @param {string } inUrl
33+ * @return {string }
34+ */
35+ export const normalizeGithubReleaseURL = ( inUrl ) => {
36+ const url = new URL ( inUrl ) , hostname = url . hostname . toLowerCase ( ) ;
37+ if ( hostname === 'github-releases.githubusercontent.com' || hostname . endsWith ( '.s3.amazonaws.com' ) ) {
38+ url . search = new URLSearchParams ( [ ...url . searchParams ]
39+ . filter ( ( [ key ] ) => ! key . toLowerCase ( ) . startsWith ( 'x-amz-' ) ) ) . toString ( ) ;
40+ }
3741 return url . href ;
3842} ;
3943
@@ -54,7 +58,7 @@ function initHTTP() {
5458 }
5559 httpHelpers . cache = new FileCache ( getCachePath ( ) ) ;
5660 httpHelpers . cache . getCacheKey = ( url ) => {
57- return FileCache . prototype . getCacheKey ( normalizeS3Url ( url ) ) ;
61+ return FileCache . prototype . getCacheKey ( normalizeGithubReleaseURL ( url ) ) ;
5862 } ;
5963}
6064
0 commit comments