Add follow-redirects

Will probably switch to node-fetch when this project goes to ESM
This commit is contained in:
Sparticuz 2023-02-10 07:20:11 -05:00
parent fcfc6b72d8
commit 48b9273bfb
3 changed files with 43 additions and 3 deletions

30
package-lock.json generated
View File

@ -9,10 +9,12 @@
"version": "110.0.0",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.2",
"tar-fs": "^2.1.1"
},
"devDependencies": {
"@tsconfig/node14": "^1.0.3",
"@types/follow-redirects": "^1.14.1",
"@types/node": "^18.11.18",
"@types/tar-fs": "^2.0.1",
"clean-modules": "^2.0.6",
@ -28,6 +30,15 @@
"integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==",
"dev": true
},
"node_modules/@types/follow-redirects": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/@types/follow-redirects/-/follow-redirects-1.14.1.tgz",
"integrity": "sha512-THBEFwqsLuU/K62B5JRwab9NW97cFmL4Iy34NTMX0bMycQVzq2q7PKOkhfivIwxdpa/J72RppgC42vCHfwKJ0Q==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/node": {
"version": "18.11.18",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz",
@ -215,6 +226,25 @@
"node": ">=6"
}
},
"node_modules/follow-redirects": {
"version": "1.15.2",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz",
"integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/fs-constants": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",

View File

@ -36,10 +36,12 @@
"test": "make clean && make && make pretest && make test"
},
"dependencies": {
"follow-redirects": "^1.15.2",
"tar-fs": "^2.1.1"
},
"devDependencies": {
"@tsconfig/node14": "^1.0.3",
"@types/follow-redirects": "^1.14.1",
"@types/node": "^18.11.18",
"@types/tar-fs": "^2.0.1",
"clean-modules": "^2.0.6",

View File

@ -1,7 +1,13 @@
import { unlink } from "node:fs";
import { get } from "node:https";
import { https } from "follow-redirects";
import { tmpdir } from "node:os";
import { extract } from 'tar-fs';
import { extract } from "tar-fs";
import { parse } from "node:url";
import type { UrlWithStringQuery } from "node:url";
interface FollowRedirOptions extends UrlWithStringQuery {
maxBodyLength: number;
}
export const isValidUrl = (input: string) => {
try {
@ -13,9 +19,11 @@ export const isValidUrl = (input: string) => {
export const downloadAndExtract = async (url: string) =>
new Promise<string>((resolve, reject) => {
const getOptions = parse(url) as FollowRedirOptions;
getOptions.maxBodyLength = 60 * 1024 * 1024; // 60mb
const destDir = `${tmpdir()}/chromium-pack`
const extractObj = extract(destDir)
get(url, (response) => {
https.get(url, (response) => {
response.pipe(extractObj);
extractObj.on('finish', () => {
resolve(destDir);