⚠ Add error message if file too large

This commit is contained in:
C-3PO 2018-07-04 19:36:25 +02:00
parent d843ce1200
commit e4c2754493
Signed by: c3po
GPG key ID: 62993C4BB4D86F24

View file

@ -1,5 +1,7 @@
import * as http from 'http'; import * as http from 'http';
const MAX_MEMORY_SIZE = 100 * 1024 * 1024;
export default function getUrlContents({ host, path }: {host: string, path: string}): Promise<ArrayBuffer> { export default function getUrlContents({ host, path }: {host: string, path: string}): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const request = http.request({ const request = http.request({
@ -11,6 +13,11 @@ export default function getUrlContents({ host, path }: {host: string, path: stri
return reject(`Expected status code 200 but received ${response.statusCode}`); return reject(`Expected status code 200 but received ${response.statusCode}`);
} }
const headerLength = Number(response.headers['content-length']); const headerLength = Number(response.headers['content-length']);
if (headerLength > MAX_MEMORY_SIZE) {
reject('File size too large to be handled in memory.');
request.abort();
return;
}
const chunkList: Buffer[] = []; const chunkList: Buffer[] = [];
let totalLength = 0; let totalLength = 0;