Skip to content

Commit f3f7764

Browse files
committed
feat: add option "DOWNLOAD_IGNORE_MISSING_HEADER" to ignore missing response-headers
fixes #865
1 parent 219ed36 commit f3f7764

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

docs/api/config-options.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ Option `DOWNLOAD_URL` is used to overwrite the ***complete*** URL (including [`D
124124

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

127+
### DOWNLOAD_IGNORE_MISSING_HEADER
128+
129+
| Environment Variable | PackageJson |
130+
| :--------------------: | :-----------: |
131+
| `DOWNLOAD_IGNORE_MISSING_HEADER` | `downloadIgnoreMissingHeader` |
132+
133+
Option `DOWNLOAD_IGNORE_MISSING_HEADER` can be set to `true` to ignore missing response headers like `content-length`.
134+
135+
Default: `false`
136+
127137
### PREFER_GLOBAL_PATH
128138

129139
| Environment Variable | PackageJson |

packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,41 @@ export class MongoBinaryDownload {
399399

400400
return;
401401
}
402+
403+
// content-length, otherwise 0
404+
let contentLength: number;
405+
402406
if (typeof response.headers['content-length'] != 'string') {
403-
reject(new DownloadError(downloadUrl, 'Response header "content-length" is empty!'));
407+
log('Response header "content-lenght" is empty!');
408+
409+
contentLength = 0;
410+
} else {
411+
contentLength = parseInt(response.headers['content-length'], 10);
412+
413+
if (Number.isNaN(contentLength)) {
414+
log('Response header "content-lenght" resolved to NaN!');
415+
416+
contentLength = 0;
417+
}
418+
}
419+
420+
// error if the content-length header is missing or is 0 if config option "DOWNLOAD_IGNORE_MISSING_HEADER" is not set to "true"
421+
if (
422+
!envToBool(resolveConfig(ResolveConfigVariables.DOWNLOAD_IGNORE_MISSING_HEADER)) &&
423+
contentLength <= 0
424+
) {
425+
reject(
426+
new DownloadError(
427+
downloadUrl,
428+
'Response header "content-length" does not exist or resolved to NaN'
429+
)
430+
);
404431

405432
return;
406433
}
407434

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

412439
const fileStream = createWriteStream(tempDownloadLocation);

packages/mongodb-memory-server-core/src/util/resolveConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export enum ResolveConfigVariables {
1616
DEBUG = 'DEBUG',
1717
DOWNLOAD_MIRROR = 'DOWNLOAD_MIRROR',
1818
DOWNLOAD_URL = 'DOWNLOAD_URL',
19+
DOWNLOAD_IGNORE_MISSING_HEADER = 'DOWNLOAD_IGNORE_MISSING_HEADER',
1920
PREFER_GLOBAL_PATH = 'PREFER_GLOBAL_PATH',
2021
DISABLE_POSTINSTALL = 'DISABLE_POSTINSTALL',
2122
SYSTEM_BINARY = 'SYSTEM_BINARY',

0 commit comments

Comments
 (0)