Skip to content

Commit

Permalink
feat: add option "DOWNLOAD_IGNORE_MISSING_HEADER" to ignore missing r…
Browse files Browse the repository at this point in the history
…esponse-headers

fixes #865
  • Loading branch information
hasezoey committed Apr 19, 2024
1 parent 219ed36 commit f3f7764
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/api/config-options.md
Expand Up @@ -124,6 +124,16 @@ Option `DOWNLOAD_URL` is used to overwrite the ***complete*** URL (including [`D

Format: `https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.0.20.tgz`

### DOWNLOAD_IGNORE_MISSING_HEADER

| Environment Variable | PackageJson |
| :--------------------: | :-----------: |
| `DOWNLOAD_IGNORE_MISSING_HEADER` | `downloadIgnoreMissingHeader` |

Option `DOWNLOAD_IGNORE_MISSING_HEADER` can be set to `true` to ignore missing response headers like `content-length`.

Default: `false`

### PREFER_GLOBAL_PATH

| Environment Variable | PackageJson |
Expand Down
Expand Up @@ -399,14 +399,41 @@ export class MongoBinaryDownload {

return;
}

// content-length, otherwise 0
let contentLength: number;

if (typeof response.headers['content-length'] != 'string') {
reject(new DownloadError(downloadUrl, 'Response header "content-length" is empty!'));
log('Response header "content-lenght" is empty!');

contentLength = 0;
} else {
contentLength = parseInt(response.headers['content-length'], 10);

if (Number.isNaN(contentLength)) {
log('Response header "content-lenght" resolved to NaN!');

contentLength = 0;
}
}

// error if the content-length header is missing or is 0 if config option "DOWNLOAD_IGNORE_MISSING_HEADER" is not set to "true"
if (
!envToBool(resolveConfig(ResolveConfigVariables.DOWNLOAD_IGNORE_MISSING_HEADER)) &&
contentLength <= 0
) {
reject(
new DownloadError(
downloadUrl,
'Response header "content-length" does not exist or resolved to NaN'
)
);

return;
}

this.dlProgress.current = 0;
this.dlProgress.length = parseInt(response.headers['content-length'], 10);
this.dlProgress.length = contentLength;
this.dlProgress.totalMb = Math.round((this.dlProgress.length / 1048576) * 10) / 10;

const fileStream = createWriteStream(tempDownloadLocation);
Expand Down
Expand Up @@ -16,6 +16,7 @@ export enum ResolveConfigVariables {
DEBUG = 'DEBUG',
DOWNLOAD_MIRROR = 'DOWNLOAD_MIRROR',
DOWNLOAD_URL = 'DOWNLOAD_URL',
DOWNLOAD_IGNORE_MISSING_HEADER = 'DOWNLOAD_IGNORE_MISSING_HEADER',
PREFER_GLOBAL_PATH = 'PREFER_GLOBAL_PATH',
DISABLE_POSTINSTALL = 'DISABLE_POSTINSTALL',
SYSTEM_BINARY = 'SYSTEM_BINARY',
Expand Down

0 comments on commit f3f7764

Please sign in to comment.