-
-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only download url2green when asked for. (#4354)
* Only download url2green when asked for. The URL2green file is becoming larger and larger. Skip downloading it if you don't specifically ask for it.
- Loading branch information
1 parent
da64199
commit d8a04c2
Showing
8 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
!LICENSE | ||
!npm-shrinkwrap.json | ||
!docker | ||
!tools/postinstall.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { existsSync, writeFileSync } from 'node:fs'; | ||
import { mkdirSync } from 'node:fs'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import path from 'node:path'; | ||
import https from 'node:https'; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const DEFAULT_URL2GREEN_URL = | ||
'https://raw.githubusercontent.com/sitespeedio/url2green/main/url2green.json.gz'; | ||
|
||
const URL2GREEN_FILE_PATH = `${__dirname}/../lib/plugins/sustainable/data/url2green.json.gz`; | ||
|
||
const DOWNLOAD_URL2GREEN = process.env.DOWNLOAD_URL2GREEN || 'false'; | ||
|
||
function downloadFile(url, destinationPath) { | ||
return new Promise((resolve, reject) => { | ||
https | ||
.get(url, response => { | ||
if (response.statusCode !== 200) { | ||
reject(new Error(`Failed to get '${url}' (${response.statusCode})`)); | ||
return; | ||
} | ||
|
||
const fileChunks = []; | ||
response.on('data', chunk => fileChunks.push(chunk)); | ||
response.on('end', () => { | ||
try { | ||
const fileBuffer = Buffer.concat(fileChunks); | ||
mkdirSync(path.dirname(destinationPath), { recursive: true }); | ||
writeFileSync(destinationPath, fileBuffer); | ||
resolve(); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}) | ||
.on('error', reject); | ||
}); | ||
} | ||
|
||
async function run() { | ||
if (DOWNLOAD_URL2GREEN === 'true') { | ||
if (existsSync(URL2GREEN_FILE_PATH)) { | ||
console.log('URL2GREEN file already exists. Skipping download.'); | ||
} else { | ||
console.log('URL2GREEN file is missing. Downloading...'); | ||
try { | ||
await downloadFile(DEFAULT_URL2GREEN_URL, URL2GREEN_FILE_PATH); | ||
console.log('URL2GREEN file downloaded successfully.'); | ||
} catch (error) { | ||
console.error('Failed to download URL2GREEN file:', error.message); | ||
} | ||
} | ||
} else { | ||
console.log('Skipping URL2GREEN file download (DOWNLOAD_URL2GREEN=false).'); | ||
} | ||
} | ||
|
||
await run(); |